Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:46

0001 #pragma once
0002 /**
0003 srng.h : picks curandState implementation 
0004 ===========================================
0005 
0006 This is included into qudarap/qrng.h 
0007 
0008 Template specializations collecting details of the various curandState impls.  See::
0009 
0010     ~/o/sysrap/tests/srng_test.sh 
0011 
0012 
0013 https://stackoverflow.com/questions/8789867/c-template-class-specialization-why-do-common-methods-need-to-be-re-implement
0014 
0015 Each specialisation of a class template gives a different class - they do not share any members.
0016 So have to implement all methods in each specialization, or use a separate helper. 
0017 
0018 **/
0019 
0020 #include <curand_kernel.h>
0021 
0022 using XORWOW = curandStateXORWOW ;
0023 using Philox = curandStatePhilox4_32_10 ; 
0024 
0025 #if defined(RNG_PHILITEOX)
0026 #include "curandlite/curandStatePhilox4_32_10_OpticksLite.h"
0027 using PhiLiteOx = curandStatePhilox4_32_10_OpticksLite ; 
0028 #endif
0029 
0030 #if defined(RNG_XORWOW)
0031 using RNG = XORWOW ;
0032 #elif defined(RNG_PHILOX)
0033 using RNG = Philox ;
0034 #elif defined(RNG_PHILITEOX)
0035 using RNG = PhiLiteOx ;
0036 #endif
0037 
0038 
0039 #if defined(__CUDACC__) || defined(__CUDABE__)
0040 #else
0041 
0042 #include <sstream>
0043 #include <string>
0044 
0045 template<typename T> struct srng {};
0046 
0047 // template specializations for the different states
0048 template<> 
0049 struct srng<XORWOW>  
0050 { 
0051     static constexpr char CODE = 'X' ;
0052     static constexpr const char* NAME = "XORWOW" ; 
0053     static constexpr unsigned SIZE = sizeof(XORWOW) ; 
0054     static constexpr bool UPLOAD_RNG_STATES = true ; 
0055 };
0056 
0057 template<> 
0058 struct srng<Philox>  
0059 { 
0060     static constexpr char CODE = 'P' ;
0061     static constexpr const char* NAME = "Philox" ; 
0062     static constexpr unsigned SIZE = sizeof(Philox) ; 
0063     static constexpr bool UPLOAD_RNG_STATES = false ; 
0064 };
0065 
0066 #if defined(RNG_PHILITEOX)
0067 template<> 
0068 struct srng<PhiLiteOx>  
0069 { 
0070     static constexpr char CODE = 'O' ;
0071     static constexpr const char* NAME = "PhiLiteOx" ; 
0072     static constexpr unsigned SIZE = sizeof(PhiLiteOx) ; 
0073     static constexpr bool UPLOAD_RNG_STATES = false ; 
0074 };
0075 #endif
0076 
0077 // helper function
0078 template<typename T> 
0079 inline std::string srng_Desc()
0080 {
0081     std::stringstream ss ; 
0082     ss 
0083        << "[srng_Desc\n" 
0084        <<  " srng<T>::NAME " << srng<T>::NAME << "\n"
0085        <<  " srng<T>::CODE " << srng<T>::CODE << "\n"
0086        <<  " srng<T>::SIZE " << srng<T>::SIZE << "\n"
0087        << "]srng_Desc" 
0088        ; 
0089     std::string str = ss.str() ; 
0090     return str ; 
0091 }
0092 
0093 template<typename T> 
0094 inline bool srng_IsXORWOW(){ return strcmp(srng<T>::NAME, "XORWOW") == 0 ; }
0095 
0096 template<typename T> 
0097 inline bool srng_IsPhilox(){ return strcmp(srng<T>::NAME, "Philox") == 0 ; }
0098 
0099 template<typename T> 
0100 inline bool srng_IsPhiLiteOx(){ return strcmp(srng<T>::NAME, "PhiLiteOx") == 0 ; }
0101 
0102 template<typename T> 
0103 inline bool srng_Matches(const char* arg)
0104 {
0105     int match = 0 ; 
0106     if( arg && strstr(arg, "XORWOW")    && srng_IsXORWOW<T>() )    match += 1 ; 
0107     if( arg && strstr(arg, "Philox")    && srng_IsPhilox<T>() )    match += 1 ; 
0108     if( arg && strstr(arg, "PhiLiteOx") && srng_IsPhiLiteOx<T>() ) match += 1 ; 
0109     return match == 1 ; 
0110 } 
0111 
0112 #endif
0113