Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:20

0001 // @(#)root/mathcore:$Id$
0002 // Authors: L. Moneta    8/2015
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2015 , ROOT MathLib Team                             *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // implementation file of MixMax engine
0012 //
0013 //
0014 // Created by: Lorenzo Moneta  : Tue 4 Aug 2015
0015 //
0016 //
0017 #ifndef ROOT_Math_MixMaxEngine_icc
0018 #define ROOT_Math_MixMaxEngine_icc
0019 
0020 #ifndef ROOT_Math_MixMaxEngine
0021 #error "Do not use MixMaxEngine.icc directly. #include \"MixMaxEngine.h\" instead."
0022 #endif // ROOT_Math_MixMaxEngine
0023 
0024 //#include "Math/mixmax/mixmax.h"
0025 #include <cassert>
0026 #include "Math/Util.h"
0027 
0028 
0029 namespace ROOT {
0030 namespace Math {
0031 
0032 
0033    
0034    template<int N, int S>
0035    MixMaxEngine<N,S>::MixMaxEngine(uint64_t seed) { 
0036       // fRng = new mixmax::mixmax_engine<N>(); 
0037       // fRng->seed(seed);
0038       fRng = new MixMaxEngineImpl<N>(seed);
0039    }
0040 
0041    template<int N, int S>
0042    MixMaxEngine<N,S>::~MixMaxEngine() {
0043       if (fRng) delete fRng; 
0044    }
0045 
0046 
0047    // void template<int N, int S>
0048    //MixMaxEngine<N,S>::SeedUniqueStream(unsigned int clusterID, unsigned int machineID, unsigned int runID, unsigned int  streamID) { 
0049    //    seed_uniquestream(fRngState, clusterID,  machineID,  runID,   streamID);
0050    // }
0051 
0052    template<int N, int S>
0053    void MixMaxEngine<N,S>::SetSeed(uint64_t seed) { 
0054       //fRng->seed(seed);
0055       fRng->SetSeed(seed);
0056    }
0057 
0058    // void template<int N, int S>
0059    // MixMaxEngine<N,S>::SetSeed64(uint64_t seed) { 
0060    //    seed_spbox(fRngState, seed);
0061    //    iterate(fRngState);                    
0062    // }
0063 
0064    // unsigned int template<int N, int S>
0065    // MixMaxEngine<N,S>::GetSeed() const { 
0066    //    return get_next(fRngState);
0067    // }
0068          
0069    // generate one random number in interval ]0,1]
0070    // apply skipping when needed
0071 
0072    template<int SkipNumber>
0073    struct SkipFunction {
0074       template<class Engine>
0075       static void Apply (Engine * rng, int counter, int n) {
0076          // apply skipping
0077          if (counter < n) return; 
0078          for (int iskip = 0; iskip < SkipNumber; ++iskip)
0079             rng->Iterate();
0080       }
0081    };
0082    // specialization for SkipNumber = 0
0083    template<>
0084    struct SkipFunction<0> {
0085       template<class Engine>
0086       static void Apply (Engine *, int , int ) {
0087          // no operation
0088       }
0089    };
0090    
0091    template<int N, int S>
0092    double MixMaxEngine<N,S>::Rndm_impl()  {
0093       int counter = fRng->Counter();
0094       SkipFunction<S>::Apply(fRng, counter, N);
0095       //reset counter to original value
0096       // otherwise we would skip -1 time
0097       fRng->SetCounter(counter);
0098       return fRng->Rndm();
0099    }
0100 
0101    // generate one integer number 
0102    template<int N, int S>
0103    uint64_t MixMaxEngine<N,S>::IntRndm() {
0104       int counter = fRng->Counter();
0105       SkipFunction<S>::Apply(fRng, counter,N);
0106       fRng->SetCounter(counter);
0107       return fRng->IntRndm();
0108    }
0109 
0110    template<int N, int S>
0111    uint64_t MixMaxEngine<N,S>::MaxInt() { 
0112       //return mixmax::mixmax_engine<N>::max();
0113       return  2305843009213693951ULL; 
0114    }
0115 
0116    template<int N, int S>
0117    uint64_t MixMaxEngine<N,S>::MinInt() { 
0118       //return mixmax::mixmax_engine<N>::min();
0119       return 0; 
0120    }
0121 
0122    template<int N, int S>
0123    void MixMaxEngine<N,S>::RndmArray(int n, double *array){
0124       // Return an array of n random numbers uniformly distributed in ]0,1]
0125       for (int i = 0; i < n; ++i)
0126          array[i] = Rndm_impl();
0127    }
0128 
0129    template<int N, int S>
0130    void MixMaxEngine<N,S>::SetState(const std::vector<StateInt_t> & state) {
0131       assert(state.size() >= N);
0132       // for (int i = 0; i < N; ++i)
0133       //    fRng->S.V[i] = state[i];
0134       // //set counter  to fore iteration afterwards
0135       // fRng->S.counter = N;
0136       fRng->SetState(state);
0137       fRng->SetCounter(N); 
0138    }
0139 
0140    template<int N, int S>
0141    void MixMaxEngine<N,S>::GetState(std::vector<StateInt_t> & state) const {
0142       state.resize(N);
0143       fRng->GetState(state); 
0144    }
0145 
0146    template<int N, int S>
0147    int MixMaxEngine<N,S>::Size()  {
0148       return MixMaxEngineImpl<N>::Size(); 
0149    }
0150 
0151    template<int N, int S>
0152    int MixMaxEngine<N,S>::Counter() const {
0153       return fRng->Counter(); 
0154    }
0155 
0156    template<int N, int S>
0157    const char *MixMaxEngine<N, S>::Name() {
0158       static const std::string name = "MixMax" + Util::ToString(N) + (S > 0 ? "_" + Util::ToString(S) : "");
0159       return name.c_str();
0160    }
0161    
0162    } // namespace Math
0163 } // namespace ROOT
0164 
0165 #endif