File indexing completed on 2026-04-09 07:49:33
0001 #include <cassert>
0002 #include <iomanip>
0003 #include "PLOG.hh"
0004
0005 #include "spath.h"
0006 #include "sdirectory.h"
0007 #include "sstr.h"
0008
0009 #include "SCurandStateMonolithic.hh"
0010 #include "SEventConfig.hh"
0011
0012 const plog::Severity SCurandStateMonolithic::LEVEL = SLOG::EnvLevel("SCurandStateMonolithic", "DEBUG" );
0013 const char* SCurandStateMonolithic::RNGDIR = spath::Resolve("${RNGDir:-$HOME/.opticks/rngcache/RNG}") ;
0014
0015
0016 const char* SCurandStateMonolithic::NAME_PREFIX = "QCurandStateMonolithic" ;
0017 const char* SCurandStateMonolithic::DEFAULT_PATH = nullptr ;
0018
0019 std::string SCurandStateMonolithic::Desc()
0020 {
0021 const char* path = Path() ;
0022 long rngmax = RngMax() ;
0023
0024 std::stringstream ss ;
0025 ss << "SCurandStateMonolithic::Desc" << std::endl
0026 << " SEventConfig::MaxCurand() " << SEventConfig::MaxCurand() << std::endl
0027 << " SCurandStateMonolithic::Path() " << path << std::endl
0028 << " SCurandStateMonolithic::RngMax() " << rngmax << std::endl
0029 << " RNGDIR " << RNGDIR << std::endl
0030 ;
0031 std::string s = ss.str() ;
0032 return s ;
0033 }
0034
0035
0036
0037
0038
0039
0040
0041
0042 const char* SCurandStateMonolithic::Path()
0043 {
0044 if(DEFAULT_PATH == nullptr)
0045 {
0046 int rngmax = SEventConfig::MaxCurand();
0047 int seed = 0 ;
0048 int offset = 0 ;
0049
0050
0051 std::string path_ = Path_(rngmax, seed, offset );
0052 const char* path = path_.c_str() ;
0053 sdirectory::MakeDirsForFile(path);
0054 DEFAULT_PATH = strdup(path);
0055 }
0056 return DEFAULT_PATH ;
0057 }
0058
0059 std::string SCurandStateMonolithic::Stem_(ULL num, ULL seed, ULL offset)
0060 {
0061 assert( num % M == 0 );
0062 ULL value = num/M ;
0063 std::stringstream ss ;
0064 ss << NAME_PREFIX << "_" << value << "M" << "_" << seed << "_" << offset ;
0065 std::string s = ss.str();
0066 return s ;
0067 }
0068 std::string SCurandStateMonolithic::Path_(ULL num, ULL seed, ULL offset)
0069 {
0070 std::stringstream ss ;
0071 ss << RNGDIR << "/" << Stem_(num, seed, offset) << ".bin" ;
0072 std::string s = ss.str();
0073 return s ;
0074 }
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 long SCurandStateMonolithic::RngMax()
0089 {
0090 const char* path = Path();
0091 return RngMax(path) ;
0092 }
0093
0094 long SCurandStateMonolithic::RngMax(const char* path)
0095 {
0096 long file_size = spath::Filesize(path);
0097
0098 long item_size = 44 ;
0099 bool expected_file_size = file_size % item_size == 0 ;
0100 long rngmax = file_size/item_size ;
0101
0102 LOG(LEVEL)
0103 << " path " << path
0104 << " file_size " << file_size
0105 << " item_size " << item_size
0106 << " rngmax " << rngmax
0107 << " expected_file_size " << ( expected_file_size ? "YES" : "NO" )
0108 ;
0109
0110 LOG_IF(fatal, !expected_file_size) << " NOT EXPECTED FILE SIZE " ;
0111 assert( expected_file_size );
0112 return rngmax ;
0113 }
0114
0115
0116
0117
0118 SCurandStateMonolithic::SCurandStateMonolithic(ULL num_, ULL seed_, ULL offset_)
0119 :
0120 spec(nullptr),
0121 num(num_),
0122 seed(seed_),
0123 offset(offset_),
0124 path(""),
0125 exists(false),
0126 rngmax(0)
0127 {
0128 init();
0129 }
0130
0131 SCurandStateMonolithic::SCurandStateMonolithic(const char* spec_)
0132 :
0133 spec(spec_ ? strdup(spec_) : nullptr),
0134 num(0),
0135 seed(0),
0136 offset(0),
0137 path(""),
0138 exists(false),
0139 rngmax(0)
0140 {
0141 init();
0142 }
0143
0144 void SCurandStateMonolithic::init()
0145 {
0146 if(spec)
0147 {
0148 std::vector<int> ivec ;
0149 sstr::split<int>(ivec, spec, ':');
0150 unsigned num_vals = ivec.size();
0151 assert( num_vals > 0 && num_vals <= 3 );
0152
0153 num = num_vals > 0 ? ivec[0] : 1 ;
0154 seed = num_vals > 1 ? ivec[1] : 0 ;
0155 offset = num_vals > 2 ? ivec[2] : 0 ;
0156
0157 if(num <= 100) num *= 1000000 ;
0158 }
0159
0160 path = Path_(num, seed, offset);
0161 exists = spath::Exists(path.c_str());
0162 rngmax = exists ? RngMax( path.c_str() ) : 0 ;
0163 }
0164
0165 std::string SCurandStateMonolithic::desc() const
0166 {
0167 std::stringstream ss ;
0168 ss
0169 << " spec " << std::setw(10) << ( spec ? spec : "-" )
0170 << " num " << std::setw(10) << num
0171 << " seed " << std::setw(10) << seed
0172 << " offset " << std::setw(10) << offset
0173 << " path " << std::setw(60) << path
0174 << " exists " << exists
0175 << " rngmax " << rngmax
0176 ;
0177 std::string s = ss.str() ;
0178 return s ;
0179 }
0180
0181
0182