Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // ~/opticks/sysrap/tests/SEvtTest.sh
0002 #include <csignal>
0003 
0004 #include "OPTICKS_LOG.hh"
0005 #include "OpticksGenstep.h"
0006 
0007 #include "scuda.h"
0008 #include "spath.h"
0009 #include "sprof.h"
0010 #include "stran.h"
0011 #include "sdirectory.h"
0012 #include "ssys.h"
0013 
0014 #include "SEvent.hh"
0015 #include "SEventConfig.hh"
0016 #include "SEvt.hh"
0017 #include "NPFold.h"
0018 
0019 
0020 struct SEvtTest
0021 {
0022     static constexpr const int M = 1000000 ;
0023     static const char* TEST ;
0024 
0025     static int AddGenstep();
0026     static int LifeCycle();
0027     static int InputPhoton0();
0028     static int InputPhoton();
0029     static int getSaveDir();
0030     static int getDir();
0031     static int setMetaProf();
0032     static int hostside_running_resize_();
0033     static int CountNibbles();
0034     static int makeGenstepArrayFromVector();
0035     static int saveExtra();
0036 
0037     static int Main(int argc, char** argv);
0038 
0039 };
0040 
0041 
0042 const char* SEvtTest::TEST = ssys::getenvvar("TEST", "CountNibbles" );
0043 
0044 int SEvtTest::AddGenstep()
0045 {
0046     SEvt* evt = SEvt::Create(0) ;
0047     bool evt_expect = SEvt::Get(0) == evt ;
0048     assert( evt_expect );
0049     if(!evt_expect) std::raise(SIGINT);
0050 
0051     for(unsigned i=0 ; i < 10 ; i++)
0052     {
0053         quad6 q ;
0054         q.set_numphoton(1000) ;
0055         unsigned gentype = i % 2 == 0 ? OpticksGenstep_SCINTILLATION : OpticksGenstep_CERENKOV ;
0056         q.set_gentype(gentype);
0057 
0058         SEvt::AddGenstep(q);
0059     }
0060 
0061     std::cout << SEvt::Get(0)->desc() << std::endl ;
0062     return 0 ;
0063 }
0064 
0065 
0066 int SEvtTest::LifeCycle()
0067 {
0068     unsigned max_bounce = 9 ;
0069     SEventConfig::SetMaxBounce(max_bounce);
0070     SEventConfig::SetMaxRecord(max_bounce+1);
0071     SEventConfig::SetMaxRec(max_bounce+1);
0072     SEventConfig::SetMaxSeq(1);
0073 
0074     SEvt* evt = SEvt::Create(0) ;
0075 
0076     evt->setIndex(214);
0077     evt->unsetIndex();
0078 
0079     quad6 gs ;
0080     gs.set_numphoton(1) ;
0081     gs.set_gentype(OpticksGenstep_TORCH);
0082 
0083     evt->addGenstep(gs);
0084 
0085     spho label = {0,0,0,{0,0,0,0}} ;
0086 
0087     evt->beginPhoton(label);
0088 
0089     int bounce0 = 0 ;
0090     int bounce1 = 0 ;
0091 
0092     bounce0 = evt->slot[label.id] ;
0093     evt->pointPhoton(label);
0094     bounce1 = evt->slot[label.id] ;
0095 
0096     bool bounce_expect = bounce1 == bounce0 + 1 ;
0097     assert( bounce_expect );
0098     if(!bounce_expect) std::raise(SIGINT);
0099 
0100     std::cout
0101          << " i " << std::setw(3) << -1
0102          << " bounce0 " << std::setw(3) << bounce0
0103          << " : " << evt->current_ctx.p.descFlag()
0104          << std::endl
0105          ;
0106 
0107 
0108     std::vector<unsigned> history = {
0109        BOUNDARY_TRANSMIT,
0110        BOUNDARY_TRANSMIT,
0111        BULK_SCATTER,
0112        BULK_SCATTER,
0113        BULK_SCATTER,
0114        BULK_SCATTER,
0115        BULK_SCATTER,
0116        BULK_SCATTER,
0117        BULK_SCATTER,
0118        BULK_SCATTER,
0119        BULK_SCATTER,
0120        BULK_SCATTER,
0121        BULK_REEMIT,
0122        BOUNDARY_TRANSMIT,
0123        SURFACE_DETECT
0124     } ;
0125 
0126     for(int i=0 ; i < int(history.size()) ; i++)
0127     {
0128         unsigned flag = history[i] ;
0129         evt->current_ctx.p.set_flag(flag);
0130 
0131         bounce0 = evt->slot[label.id] ;
0132         evt->pointPhoton(label);
0133         bounce1 = evt->slot[label.id] ;
0134         assert( bounce1 == bounce0 + 1 );
0135 
0136         std::cout
0137              << " i " << std::setw(3) << i
0138              << " bounce0 " << std::setw(3) << bounce0
0139              << " : " << evt->current_ctx.p.descFlag()
0140              << std::endl
0141              ;
0142     }
0143 
0144     evt->finalPhoton(label);
0145 
0146     evt->save("$TMP/SEvtTest");
0147     LOG(info) << evt->desc() ;
0148     return 0 ;
0149 }
0150 
0151 int SEvtTest::InputPhoton0()
0152 {
0153     const char* ipf = SEventConfig::InputPhotonFrame();
0154     if( ipf == nullptr) return 1 ;
0155 
0156     SEvt* evt = SEvt::Create(0) ;
0157     LOG(info) << evt->desc() ;
0158 
0159     NP* ip = evt->getInputPhoton();
0160     LOG_IF(fatal, !ip ) << " FAILED TO getInputPhoton " ;
0161     if(!ip) return 1 ;
0162 
0163 
0164     const char* FOLD = spath::Resolve("$TMP/SEvtTest/test_InputPhoton");
0165     sdirectory::MakeDirs( FOLD, 0 );
0166 
0167     ip->save(FOLD, spath::Name("ipf", ipf, ".npy") );
0168 
0169     /*
0170     const qat4* q = SEvt::CF->getInputPhotonFrame();
0171     Tran<double>* tr = Tran<double>::ConvertToTran(q);
0172 
0173     NP* fr = NP::Make<float>(1,4,4);
0174     memcpy( fr->bytes(), q->cdata(), fr->arr_bytes() );
0175     fr->save(FOLD, SStr::Name("fr", ipf, ".npy" ));
0176 
0177     tr->save( FOLD, SStr::Name("tr",ipf, ".npy" )) ;
0178 
0179     */
0180     return 0 ;
0181 }
0182 
0183 int SEvtTest::InputPhoton()
0184 {
0185     SEvt* evt = SEvt::Create_EGPU() ;
0186     NP* ip = evt->getInputPhoton();
0187     std::cout << "SEvtTest::InputPhoton " << ( ip ? ip->sstr() : "-" ) << "\n" ;
0188 
0189     bool skip_flagmask_zero = false ;
0190     std::cout << "SEvtTest::InputPhoton " << sphoton::DescMinMaxPost(ip, skip_flagmask_zero) << "\n" ;
0191 
0192     return 0 ;
0193 }
0194 
0195 
0196 /**
0197 test_savedir
0198 ===============
0199 
0200 savedir1 examples::
0201 
0202     /tmp/blyth/opticks/GEOM/SEvtTest/ALL
0203     /tmp/blyth/opticks/GEOM/Cheese/SEvtTest/ALL    # when GEOM envvar is Cheese
0204 
0205 Only after the save does the savedir get set.
0206 
0207 **/
0208 
0209 
0210 int SEvtTest::getSaveDir()
0211 {
0212     SEvt* evt = SEvt::Create(0);
0213 
0214     //LOG(info) << evt->desc() ;
0215 
0216     const char* savedir0 = evt->getSaveDir() ;
0217     assert( savedir0 == nullptr );
0218 
0219     evt->save();
0220 
0221     const char* savedir1 = evt->getSaveDir() ;
0222     //assert( savedir1 != nullptr );
0223 
0224     LOG(info)
0225         << " savedir0 " << ( savedir0 ? savedir0 : "(null)" )
0226         << " savedir1 " << ( savedir1 ? savedir1 : "(null)" )
0227         ;
0228     return 0 ;
0229 }
0230 
0231 
0232 int SEvtTest::getDir()
0233 {
0234     SEvt* evt = SEvt::Create(0);
0235     const char* dir0 = evt->getDir();
0236 
0237     LOG(info)
0238         << "getDir" << std::endl
0239         << " dir0 [" << ( dir0 ? dir0 : "-" ) << "]" << std::endl
0240         ;
0241     return 0 ;
0242 }
0243 
0244 
0245 int SEvtTest::setMetaProf()
0246 {
0247     SEvt* evt = SEvt::Create(0);
0248 
0249     sprof prof = {} ;
0250     sprof::Stamp(prof);
0251     evt->setMetaProf("test_setMeta", prof );
0252 
0253     std::cout << "evt->meta" << std::endl << evt->meta << std::endl ;
0254     return 0 ;
0255 }
0256 
0257 /**
0258 SEvtTest::hostside_running_resize_
0259 ------------------------------------
0260 
0261 Profile time, VM, RSS change from hostside_running_resize
0262 
0263 **/
0264 
0265 int SEvtTest::hostside_running_resize_()
0266 {
0267     int num = 10*M ;
0268     bool edump = false ;
0269 
0270     SEventConfig::SetMaxPhoton(num);
0271 
0272     SEvt::Create_EGPU() ;
0273     SEvt* evt = SEvt::Get_EGPU();
0274 
0275     sprof p0, p1, p2  ;
0276 
0277     sprof::Stamp(p0);
0278 
0279     evt->setNumPhoton(num);
0280 
0281     if(edump)
0282     std::cout
0283         << " (SEvt)evt->(sevent)evt->descNum "
0284         << evt->evt->descNum()
0285         ;
0286 
0287     evt->hostside_running_resize_() ;
0288 
0289     sprof::Stamp(p1);
0290     std::cout
0291         << "sprof::Desc(p0, p1) : before and after setNumPhoton+hostside_running_resize_ : to " << num << std::endl
0292         << sprof::Desc(p0, p1 )
0293         ;
0294 
0295     evt->clear_output() ;
0296 
0297     sprof::Stamp(p2);
0298     std::cout
0299         << "sprof::Desc(p1,p2) : before and after : SEvt::clear_vectors  " << std::endl
0300         << "SMALL DELTA INDICATES THE RESIZE TO ZERO : DID NOT DEALLOCATE MEMORY " << std::endl
0301         << "FIND THAT NEED shrink = true TO GET THE DEALLOC TO HAPPEN " << std::endl
0302         << sprof::Desc(p1, p2 )
0303         ;
0304 
0305     return 0 ;
0306 }
0307 
0308 int SEvtTest::CountNibbles()
0309 {
0310     const char* path = spath::Resolve("$SEQPATH");
0311     NP* seq = path ? NP::LoadIfExists(path) : nullptr ;
0312     if(seq == nullptr) return 0 ;
0313 
0314     NP* seqnib = SEvt::CountNibbles(seq);
0315     NP* seqnib_table = SEvt::CountNibbles_Table(seqnib);
0316 
0317     std::cout
0318         << "SEvtTest::CountNibbles"
0319         << std::endl
0320         << " path " << ( path ? path : "-" )
0321         << std::endl
0322         << " seq " << ( seq ? seq->sstr() : "-" )
0323         << std::endl
0324         << " seqnib " << ( seqnib ? seqnib->sstr() : "-" )
0325         << std::endl
0326         << " seqnib_table " << ( seqnib_table ? seqnib_table->sstr() : "-" )
0327         << std::endl
0328         ;
0329 
0330     std::cout << seqnib_table->descTable<int>(7) ;
0331 
0332     NPFold* fold = new NPFold ;
0333     fold->add( "seqnib", seqnib );
0334     fold->add( "seqnib_table", seqnib_table );
0335     fold->save("$FOLD");
0336     return 0 ;
0337 }
0338 
0339 int SEvtTest::makeGenstepArrayFromVector()
0340 {
0341     std::cout << "[ SEvtTest::makeGenstepArrayFromVector \n";
0342 
0343     SEvt::Create_EGPU() ;
0344     SEvt* evt = SEvt::Get_EGPU();
0345 
0346     NP* gs0 = evt->makeGenstepArrayFromVector(); // get array from the genstep vector
0347     assert( gs0 == nullptr && "before adding genstep to vector expect null ") ;
0348 
0349     const NP* igs = SEvent::MakeDemoGenstep("torch");
0350     evt->addGenstep(igs);  // converts into quad6 which are added to vector
0351 
0352     NP* gs = evt->makeGenstepArrayFromVector(); // get array from the genstep vector
0353     assert( gs != nullptr && "after adding genstep to vector expect non-null array ") ;
0354 
0355 
0356     std::cout << " gs " << ( gs ? gs->sstr() : "-" ) << std::endl ;
0357 
0358     NPFold* fold = new NPFold ;
0359     fold->add( "igs", igs );
0360     fold->add( "gs", gs );
0361     fold->save("$FOLD");
0362 
0363     std::cout << "] SEvtTest::makeGenstepArrayFromVector \n";
0364     return 0 ;
0365 }
0366 
0367 int SEvtTest::saveExtra()
0368 {
0369     SEvt::Create_ECPU() ;
0370     SEvt* evt = SEvt::Get_ECPU();
0371     evt->save();
0372 
0373 
0374     std::vector<std::pair<std::string, int>> kvs = {  {"red", 0}, {"green",1}, {"blue",2} } ;
0375 
0376     NP* somearray = NP::Make<int>(1) ;
0377     somearray->set_meta_kv(kvs);
0378 
0379     NP* otherarray = NP::Make<int>(1) ;
0380     otherarray->set_meta_kv(kvs);
0381 
0382 
0383     evt->saveExtra("somearray.npy", somearray );
0384 
0385     evt->saveExtra("$TMP", "otherarray.npy", otherarray );
0386 
0387 
0388     return 0 ;
0389 }
0390 
0391 
0392 int SEvtTest::Main(int argc, char** argv)
0393 {
0394     OPTICKS_LOG(argc, argv);
0395     SEventConfig::SetRGModeTest();
0396     bool ALL = strcmp(TEST, "ALL") == 0 ;
0397 
0398     int rc = 0 ;
0399 
0400     if(ALL||strcmp(TEST, "AddGenstep") == 0 )   rc += AddGenstep();
0401     if(ALL||strcmp(TEST, "LifeCycle") == 0 )    rc += LifeCycle();
0402     if(ALL||strcmp(TEST, "InputPhoton0") == 0 ) rc += InputPhoton0();
0403     if(ALL||strcmp(TEST, "InputPhoton") == 0 )  rc += InputPhoton();
0404     if(ALL||strcmp(TEST, "getSaveDir") == 0 )   rc += getSaveDir();
0405     if(ALL||strcmp(TEST, "getDir") == 0 )       rc += getDir();
0406     if(ALL||strcmp(TEST, "setMetaProf") == 0 )  rc += setMetaProf();
0407     if(ALL||strcmp(TEST, "hostside_running_resize_") == 0 ) rc += hostside_running_resize_();
0408     if(ALL||strcmp(TEST, "CountNibbles") == 0 )  rc += CountNibbles();
0409     if(ALL||strcmp(TEST, "makeGenstepArrayFromVector") == 0 ) rc += makeGenstepArrayFromVector();
0410     if(ALL||strcmp(TEST, "saveExtra") == 0 )     rc += saveExtra();
0411 
0412     return rc ;
0413 }
0414 
0415 int main(int argc, char** argv){ return SEvtTest::Main(argc, argv); }
0416 
0417 // ~/opticks/sysrap/tests/SEvtTest.sh