Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /**
0002 QEvtTest.cc
0003 ==============
0004 
0005 ~/o/qudarap/tests/QEvtTest.sh
0006 
0007 TEST=one       ~/o/qudarap/tests/QEvtTest.sh
0008 TEST=one VERBOSE=1  ~/o/qudarap/tests/QEvtTest.sh
0009 
0010 
0011 TEST=sliced    ~/o/qudarap/tests/QEvtTest.sh
0012 TEST=many      ~/o/qudarap/tests/QEvtTest.sh
0013 TEST=loaded    ~/o/qudarap/tests/QEvtTest.sh
0014 TEST=checkEvt  ~/o/qudarap/tests/QEvtTest.sh
0015 TEST=quad6     ~/o/qudarap/tests/QEvtTest.sh
0016 
0017 TEST=ALL       ~/o/qudarap/tests/QEvtTest.sh
0018 
0019 
0020 **/
0021 
0022 
0023 #include "OPTICKS_LOG.hh"
0024 
0025 #include <csignal>
0026 #include <cuda_runtime.h>
0027 
0028 #include "OpticksGenstep.h"
0029 #include "NP.hh"
0030 #include "NPX.h"
0031 
0032 #include "QBuf.hh"
0033 
0034 #include "ssys.h"
0035 #include "spath.h"
0036 #include "scuda.h"
0037 #include "squad.h"
0038 #include "sstamp.h"
0039 #include "SGenstep.h"
0040 
0041 #include "SEvt.hh"
0042 #include "SEvent.hh"
0043 #include "QEvt.hh"
0044 
0045 // some tests moved down to sysrap/tests/SEventTest.cc
0046 
0047 struct QEvtTest
0048 {
0049     static constexpr const int M = 1000000 ;
0050     static const char* TEST ;
0051     static bool VERBOSE ;
0052 
0053     static int setGenstep_one() ;
0054     static int setGenstep_sliced() ;
0055     static int setGenstep_many() ;
0056     static int setGenstep_loaded(NP* gs);
0057     static int setGenstep_loaded();
0058     static int setGenstep_checkEvt();
0059     static int setGenstep_quad6();
0060 
0061     static int main();
0062 };
0063 
0064 
0065 const char* QEvtTest::TEST    = ssys::getenvvar("TEST", "many") ; // ALL: leads to cumulative OOM fail ?
0066 bool        QEvtTest::VERBOSE = ssys::getenvbool("VERBOSE") ;
0067 
0068 /**
0069 QEvtTest::setGenstep_one
0070 --------------------------------
0071 
0072 1. QEvt::setGenstepUpload_NP one genstep to GPU
0073 2. QEvt::gatherSeed downloads the resulting GPU seed buffer
0074 3. compares downloaded seed buffer with expectations
0075 
0076 **/
0077 
0078 int QEvtTest::setGenstep_one()
0079 {
0080 
0081     std::cout << "QEvtTest::setGenstep_one" << std::endl ;
0082     //                                             0  1  2  3  4  5  6  7  8
0083     std::vector<int> photon_counts_per_genstep = { 3, 5, 2, 0, 1, 3, 4, 2, 4 };
0084     int x_num_photon = 0  ;
0085     NP* gs = SEvent::MakeCountGenstep(photon_counts_per_genstep, &x_num_photon ) ;
0086 
0087     LOG_IF(info, VERBOSE) << "[ new QEvt "  ;
0088     QEvt* event = new QEvt ;
0089     LOG_IF(info, VERBOSE) << "] new QEvt "  ;
0090     LOG_IF(info, VERBOSE) << " event.desc (bef QEvt::setGenstep) " << event->desc() ;
0091 
0092 
0093     LOG_IF(info, VERBOSE) << "[ QEvt::setGenstepUpload_NP "  ;
0094     event->setGenstepUpload_NP(gs);
0095     LOG_IF(info, VERBOSE) << "] QEvt::setGenstepUpload_NP "  ;
0096 
0097     LOG_IF(info, VERBOSE) << " event.desc (aft QEvt::setGenstep) " << event->desc() ;
0098 
0099     int num_photon = event->getNumPhoton();
0100     bool num_photon_expect = x_num_photon == num_photon ;
0101     LOG(info) << " num_photon " << num_photon << " x_num_photon " << x_num_photon ;
0102     assert( num_photon_expect );
0103     if(!num_photon_expect) std::raise(SIGINT);
0104 
0105 #ifndef PRODUCTION
0106 
0107     std::vector<int> xseed ;
0108     SEvent::ExpectedSeeds(xseed, photon_counts_per_genstep);
0109     assert( int(xseed.size()) == x_num_photon );
0110     const int* xseed_v = xseed.data();
0111 
0112 
0113     NP* seed = event->gatherSeed();
0114     const int* seed_v = seed->values<int>();
0115     int num_seed = seed->shape[0] ;
0116 
0117     assert( num_seed == num_photon );
0118 
0119     int seed_mismatch = SEvent::CompareSeeds( seed_v, xseed_v, num_seed );
0120 
0121     LOG(info) << " seed: "   << SEvent::DescSeed(seed_v, num_seed, 100 );
0122     LOG(info) << " x_seed: " << SEvent::DescSeed(xseed_v, num_seed, 100 );
0123     LOG(info) << " seed_mismatch " << seed_mismatch ;
0124 #endif
0125     return 0 ;
0126 }
0127 
0128 
0129 int QEvtTest::setGenstep_sliced()
0130 {
0131     std::cout << "QEvtTest::setGenstep_sliced" << std::endl ;
0132 
0133     //                                             0    1    2    3  4    5    6    7    8
0134     std::vector<int> photon_counts_per_genstep = { 300, 500, 200, 0, 100, 300, 400, 200, 400 };
0135     int tot_photon = 0  ;
0136     NP* gs = SEvent::MakeCountGenstep(photon_counts_per_genstep, &tot_photon ) ;
0137 
0138     QEvt* event = new QEvt ;
0139     LOG_IF(info, VERBOSE) << " event.desc (bef QEvt::setGenstep) " << event->desc() ;
0140 
0141     int max_slot = 1000 ;
0142     std::vector<sslice> gs_slice ;
0143     SGenstep::GetGenstepSlices( gs_slice, gs, max_slot );
0144     int num_slice = gs_slice.size();
0145 
0146     std::cout
0147         << "QEvtTest::setGenstep_sliced"
0148         << " tot_photon " << tot_photon
0149         << " max_slot " << max_slot
0150         << " num_slice " << num_slice
0151         << "\n"
0152         ;
0153 
0154     for(int i=0 ; i < num_slice ; i++)
0155     {
0156         const sslice& sl = gs_slice[i] ;
0157 
0158         event->setGenstepUpload_NP(gs, &sl );
0159 
0160         int num_photon = event->getNumPhoton();
0161         bool num_photon_expect = sl.ph_count == num_photon ;
0162 
0163         std::cout
0164             << " i " << std::setw(3) << i
0165             << " sl " << sl.desc()
0166             << " num_photon " << num_photon
0167             << "\n"
0168             ;
0169 
0170         assert( num_photon_expect );
0171         if(!num_photon_expect) std::raise(SIGINT);
0172 
0173 #ifndef PRODUCTION
0174 
0175         NP* seed_ = event->gatherSeed();
0176         const int* seed_v = seed_->values<int>();
0177         int num_seed = seed_->shape[0] ;
0178 
0179         assert( num_seed  == num_photon );
0180         int edgeitems = 100 ;
0181         LOG(info) << " seed: "   << SEvent::DescSeed(seed_v, num_seed, edgeitems );
0182 
0183 #endif
0184     }
0185     return 0 ;
0186 }
0187 
0188 
0189 
0190 
0191 
0192 
0193 
0194 
0195 
0196 
0197 /**
0198 QEvtTest::setGenstep_many
0199 ----------------------------
0200 
0201 1. prepare num_v:3 gensteps together with corresponding photon and seed expectations
0202 2. repeatedly upload those three gensteps followed by seed downloads and comparisons with expectations
0203 
0204 **/
0205 
0206 int QEvtTest::setGenstep_many()
0207 {
0208     std::cout
0209         << "QEvtTest::setGenstep_many\n" ;
0210 
0211     const int num_v = 3 ;
0212 
0213     typedef std::vector<int> VI ;
0214     VI* v = new VI[num_v] ;
0215     VI* x_seed = new VI[num_v] ;
0216 
0217     NP** gs = new NP*[num_v] ;
0218     int* x_num_photon = new int[num_v] ;
0219     int* x_total = new int[num_v] ;
0220 
0221     v[0] = {3, 5, 2, 0, 1, 3, 4, 2, 4 } ;
0222     v[1] = { 30, 50, 20, 0, 10, 30, 40, 20, 40 };
0223     v[2] = { 300, 500, 200, 0, 100, 300, 400, 200, 400 } ;
0224 
0225     for(int i=0 ; i < num_v ; i++)
0226     {
0227         x_num_photon[i] = SEvent::SumCounts( v[i] ) ;
0228         gs[i] = SEvent::MakeCountGenstep( v[i], &x_total[i] );
0229         assert( x_total[i] == x_num_photon[i] );
0230 
0231         SEvent::ExpectedSeeds(x_seed[i], v[i]);
0232         assert( int(x_seed[i].size()) == x_num_photon[i] );
0233 
0234         std::cout
0235             << " i " << std::setw(3) << i
0236             << " x_num_photon[i] " << std::setw(5) << x_num_photon[i]
0237             << " gs[i] " << gs[i]->desc()
0238             << std::endl
0239             << " x_seed: " << SEvent::DescSeed(x_seed[i].data(), x_seed[i].size(), 100 )
0240             << std::endl
0241             ;
0242     }
0243 
0244     QEvt* event = new QEvt ;
0245 
0246     int num_event = SEventConfig::NumEvent();
0247     int nj = num_v*num_event ;
0248 
0249     std::cout
0250         << "QEvtTest::setGenstep_many"
0251         << " num_event " << num_event
0252         << " num_v " << num_v
0253         << " nj " << nj
0254         << "\n"
0255         ;
0256 
0257     for(int j=0 ; j < nj ; j++)
0258     {
0259         int i = j % 3 ;
0260 
0261         event->setGenstepUpload_NP(gs[i]);
0262 
0263         int num_photon = event->getNumPhoton();
0264         bool num_photon_expect = x_num_photon[i] == num_photon ;
0265         assert( num_photon_expect );
0266         if(!num_photon_expect) std::raise(SIGINT);
0267 
0268 
0269 #ifndef PRODUCTION
0270         sstamp::sleep_us(100000);
0271 
0272         NP* seed_ = event->gatherSeed();
0273         const int* seed = seed_->values<int>();
0274         int num_seed = seed_->shape[0] ;
0275 
0276         assert( num_seed  == num_photon );
0277         int seed_mismatch = SEvent::CompareSeeds( seed, x_seed[i].data(), x_seed[i].size() );
0278 
0279         std::cout
0280             << " j " << std::setw(3) << j
0281             << " i " << std::setw(3) << i
0282             << " x_num_photon[i] " << std::setw(5) << x_num_photon[i]
0283             << " num_photon " << std::setw(5) << num_photon
0284             << " seed_mismatch " << std::setw(5) << seed_mismatch
0285            //  << event->desc()
0286             << std::endl
0287             ;
0288 
0289 
0290         if( seed_mismatch > 0 )
0291         {
0292             std::cout
0293                 << " seed: " << SEvent::DescSeed(seed, num_seed, 100 )
0294                 << std::endl
0295                 ;
0296         }
0297         assert( seed_mismatch == 0 );
0298 #endif
0299 
0300     }
0301     return 0 ;
0302 }
0303 
0304 /**
0305 QEvtTest::setGenstep_loaded
0306 -------------------------------
0307 
0308 1. upload genstep with QEvt::setGenstepUpload_NP
0309 2. check that get non-zero num_photon + num_simtrace
0310 
0311 
0312 **/
0313 
0314 int QEvtTest::setGenstep_loaded(NP* gs)
0315 {
0316     std::cout
0317         << "[QEvtTest::setGenstep_loaded"
0318         << " gs " << ( gs ? gs->sstr() : "-" )
0319         << "\n"
0320         ;
0321 
0322     QEvt* event = new QEvt ;
0323     event->setGenstepUpload_NP(gs);
0324 
0325     unsigned num_photon = event->getNumPhoton() ;
0326     unsigned num_simtrace = event->getNumSimtrace() ;
0327 
0328     std::cout
0329         << "QEvtTest::setGenstep_loaded"
0330         << " num_photon " << num_photon
0331         << " num_photon/M " << num_photon/M
0332         << " num_simtrace " << num_simtrace
0333         << " num_simtrace/M " << num_simtrace/M
0334         << "\n"
0335         ;
0336 
0337     bool num_photon_expect = num_photon + num_simtrace > 0 ;
0338     assert( num_photon_expect );
0339     if(!num_photon_expect) std::raise(SIGINT);
0340 
0341     LOG(info) << event->desc() ;
0342     //event->seed->download_dump("event->seed", 10);
0343     event->checkEvt();
0344 
0345     std::cout
0346         << "]QEvtTest::setGenstep_loaded"
0347         << " gs " << ( gs ? gs->sstr() : "-" )
0348         << "\n"
0349         ;
0350 
0351     return 0 ;
0352 }
0353 
0354 int QEvtTest::setGenstep_loaded()
0355 {
0356     std::cout << "QEvtTest::setGenstep_loaded" << std::endl ;
0357     const char* path_ = "$TMP/sysrap/SEventTest/cegs.npy" ;
0358     const char* path = spath::Resolve(path_);
0359     NP* gs0 = NP::Load(path);
0360 
0361     std::cout
0362         << "QEvtTest::setGenstep_loaded"
0363         << " path " << ( path ? path : "-" )
0364         << " gs0 " << ( gs0 ? gs0->sstr() : "-" )
0365         << "\n"
0366         ;
0367 
0368 
0369     if( gs0 == nullptr )
0370     {
0371         LOG(fatal)
0372             << "failed to load from"
0373             << " path_ " << path_
0374             << " path " << path
0375             ;
0376         //assert(0);
0377         return 0 ;
0378     }
0379 
0380     gs0->dump();
0381     setGenstep_loaded(gs0);
0382     return 0 ;
0383 }
0384 
0385 
0386 int QEvtTest::setGenstep_checkEvt()
0387 {
0388     std::cout << "QEvtTest::setGenstep_checkEvt" << std::endl ;
0389     std::vector<int> photon_counts_per_genstep = { 3, 5, 2, 0, 1, 3, 4, 2, 4 };
0390     int x_num_photon = SEvent::SumCounts( photon_counts_per_genstep ) ;
0391     std::cout << " x_num_photon " << x_num_photon << std::endl ;
0392 
0393     int x_total = 0 ;
0394     NP* gs = SEvent::MakeCountGenstep(photon_counts_per_genstep, &x_total) ;
0395     assert( x_num_photon == x_total ) ;
0396 
0397     QEvt* event = new QEvt ;
0398     event->setGenstepUpload_NP(gs);
0399     event->checkEvt();
0400     return 0 ;
0401 }
0402 
0403 int QEvtTest::setGenstep_quad6()
0404 {
0405     std::cout << "QEvtTest::setGenstep_quad6" << std::endl ;
0406 
0407     std::vector<quad6> qgs(1) ;
0408 
0409     quad6& gs = qgs[0]  ;
0410     gs.q0.u = make_uint4( OpticksGenstep_CARRIER, 0u, 0u, 10u );
0411     gs.q1.u = make_uint4( 0u,0u,0u,0u );
0412     gs.q2.f = make_float4( 0.f, 0.f, 0.f, 0.f );    // post
0413     gs.q3.f = make_float4( 1.f, 0.f, 0.f, 1.f );    // dirw
0414     gs.q4.f = make_float4( 0.f, 1.f, 0.f, 500.f );  // polw
0415     gs.q5.f = make_float4( 0.f, 0.f, 0.f, 0.f );    // flag
0416 
0417     NP* a_gs = NPX::ArrayFromVec<float,quad6>( qgs, 6, 4) ;
0418 
0419 
0420     QEvt* event = new QEvt ;
0421     event->setGenstepUpload_NP( a_gs);
0422 
0423     event->gs->dump();
0424     event->checkEvt();
0425 
0426     cudaDeviceSynchronize();
0427     event->sev->save( "$TMP/QEvtTest/test_setGenstep_quad6" );
0428 
0429     return 0 ;
0430 }
0431 
0432 
0433 int QEvtTest::main()
0434 {
0435     bool ALL = strcmp(TEST, "ALL") == 0 ;
0436     int rc = 0 ;
0437     std::cout << "[QEvtTest::main ALL " << ( ALL ? "YES" : "NO " ) << "\n" ;
0438 
0439     if(ALL||0==strcmp(TEST,"one"))      rc += setGenstep_one();
0440     if(ALL||0==strcmp(TEST,"sliced"))   rc += setGenstep_sliced();
0441     if(ALL||0==strcmp(TEST,"many"))     rc += setGenstep_many();
0442     if(ALL||0==strcmp(TEST,"loaded"))   rc += setGenstep_loaded();
0443     if(ALL||0==strcmp(TEST,"checkEvt")) rc += setGenstep_checkEvt();
0444     if(ALL||0==strcmp(TEST,"quad6"))    rc += setGenstep_quad6();
0445 
0446     std::cout << "]QEvtTest::main rc [" << rc << "]\n" ;
0447     return rc  ;
0448 }
0449 
0450 
0451 int main(int argc, char** argv)
0452 {
0453     OPTICKS_LOG(argc, argv);
0454 
0455     SEvt::Create(SEvt::EGPU) ;
0456 
0457     return QEvtTest::main();
0458 }
0459