Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #include <iostream>
0002 #include <vector>
0003 #include <csignal>
0004 
0005 #include "NP.hh"
0006 #include "ssys.h"
0007 #include "scuda.h"
0008 #include "squad.h"
0009 #include "sphotonlite.h"
0010 
0011 #include "SU.hh"
0012 
0013 struct SUTest
0014 {
0015     static unsigned populate( quad4* pp, unsigned num_p, unsigned mask );
0016     static void     dump( const quad4* pp, unsigned num_p, unsigned mask );
0017 
0018     static int monolithic();
0019     static int presized();
0020     static int copy_host_to_device_sizeof();
0021     static int upload_array_sizeof();
0022     static int hitlite();
0023 
0024     static int main(int argc, char** argv);
0025 };
0026 
0027 
0028 inline int SUTest::main(int argc, char** argv)
0029 {
0030     const char* test = "hitlite" ;
0031     const char* TEST = ssys::getenvvar("TEST", test );
0032     bool ALL = 0 == strcmp(TEST, "ALL") ;
0033 
0034     std::cout << argv[0] << " TEST[" << TEST << "]\n" ;
0035 
0036     int rc = 0 ;
0037     if(ALL||0==strcmp(TEST,"monolithic"))                 rc += monolithic() ;
0038     if(ALL||0==strcmp(TEST,"presized"))                   rc += presized() ;
0039     if(ALL||0==strcmp(TEST,"copy_host_to_device_sizeof")) rc += copy_host_to_device_sizeof() ;
0040     if(ALL||0==strcmp(TEST,"upload_array_sizeof"))        rc += upload_array_sizeof() ;
0041     if(ALL||0==strcmp(TEST,"hitlite"))                    rc += hitlite() ;
0042 
0043     return rc ;
0044 }
0045 
0046 
0047 
0048 
0049 /**
0050 populate : mockup photon array with one third marked as hits
0051 --------------------------------------------------------------
0052 
0053 Zeros pp photon array and for a third of the entries sets the flagmask entry
0054 in p.q3.u.w to the mask parameter.
0055 
0056 **/
0057 
0058 unsigned SUTest::populate( quad4* pp, unsigned num_p, unsigned mask )
0059 {
0060     unsigned num_hit = 0 ;
0061     for(unsigned i=0 ; i < num_p ; i++)
0062     {
0063         quad4& p = pp[i];
0064         p.zero();
0065 
0066         p.q0.f.x = float(i*1000) ;
0067         p.q3.u.x = i ;
0068 
0069         bool is_hit = i % 3 == 0 ;
0070         p.q3.u.w = is_hit ? mask : i  ;
0071 
0072         if(is_hit) num_hit +=1  ;
0073     }
0074     return num_hit ;
0075 }
0076 
0077 void SUTest::dump( const quad4* pp, unsigned num_p, unsigned mask )
0078 {
0079     std::cout << " dump num_p:" << num_p << " mask " << std::hex << mask << std::dec << std::endl ;
0080     for(unsigned i=0 ; i < num_p ; i++)
0081     {
0082         const quad4& h = pp[i];
0083         unsigned flag = h.q3.u.w ;
0084 
0085         std::cout
0086              << " h "
0087              << h.q3.u.x << " "
0088              << h.q3.u.y << " "
0089              << h.q3.u.z << " "
0090              << " 0x" << std::hex << flag << std::dec << " "
0091              << std::endl
0092              ;
0093 
0094         assert( ( flag & mask ) == mask );
0095     }
0096 }
0097 
0098 
0099 
0100 /**
0101 SUTest::monolithic
0102 -----------------
0103 
0104 1. populate pp
0105 2. *SU::upload*
0106 3. *SU::deprecated_select_copy_device_to_host*
0107 
0108 **/
0109 
0110 int SUTest::monolithic()
0111 {
0112     std::vector<quad4> pp(10) ;
0113     unsigned mask = 0xbeefcafe ;
0114     unsigned x_num_hit = populate(pp.data(), pp.size(), mask);
0115 
0116     unsigned num_p = pp.size();
0117     quad4* d_pp = SU::upload(pp.data(), num_p);
0118 
0119     quad4* hit ;
0120     unsigned num_hit ;
0121     qselector<quad4> selector(mask);
0122 
0123     SU::deprecated_select_copy_device_to_host( &hit, num_hit, d_pp, num_p, selector );
0124 
0125     bool num_hit_expect = x_num_hit == num_hit ;
0126     assert( num_hit_expect );
0127     if(!num_hit_expect) std::raise(SIGINT);
0128 
0129     dump( hit, num_hit, mask );
0130 
0131     return 0;
0132 }
0133 
0134 /**
0135 SUTest::presized
0136 -----------------
0137 
0138 1. populate pp
0139 2. *SU::upload* pp to device
0140 3. *SU::count_if* the hits on device
0141 4. *SU::device_alloc* device buffer sized for the number of hits
0142 5. *SU::copy_if_device_to_device_presized* from photons to hits buffer
0143 6. *SU::copy_device_to_host_presized* : copy back the hits
0144 
0145 **/
0146 
0147 
0148 int SUTest::presized()
0149 {
0150     std::vector<quad4> pp(10) ;
0151     unsigned mask = 0xbeefcafe ;
0152     unsigned x_num_hit = populate(pp.data(), pp.size(), mask);
0153 
0154     unsigned num_p = pp.size();
0155     quad4* d_p = SU::upload<quad4>(pp.data(), num_p);
0156 
0157     qselector<quad4> selector(mask);
0158     unsigned num_hit = SU::count_if<quad4>( d_p, num_p, selector );
0159     std::cout
0160          << " num_hit " << num_hit
0161          << " x_num_hit " << x_num_hit
0162          << std::endl
0163          ;
0164     assert( x_num_hit == num_hit );
0165 
0166     quad4* d_hit = SU::device_alloc<quad4>( num_hit );
0167     SU::copy_if_device_to_device_presized<quad4>( d_hit, d_p, num_p, selector );
0168 
0169     quad4* hit = new quad4[num_hit] ;
0170     SU::copy_device_to_host_presized<quad4>( hit, d_hit, num_hit );
0171 
0172     dump( hit, num_hit, mask  );
0173 
0174     return 0 ;
0175 }
0176 
0177 
0178 
0179 
0180 
0181 
0182 int SUTest::copy_host_to_device_sizeof()
0183 {
0184     const int N = 10 ;
0185     std::vector<float> demo(N) ;
0186     for(unsigned i=0 ; i < N ; i++) demo[i] = float(i)*100.f ;
0187 
0188     std::vector<float> demo2(N, 0.f);
0189 
0190 
0191     char* d_demo = SU::device_alloc_sizeof(N,sizeof(float));
0192     SU::copy_host_to_device_sizeof( d_demo, (char*)demo.data(), N, sizeof(float) );
0193 
0194     SU::copy_device_to_host_sizeof( (char*)demo2.data(), d_demo, N, sizeof(float)) ;
0195     for(unsigned i=0 ; i < N ; i++) std::cout << " demo2[" << i << "] = " << demo2[i] << std::endl ;
0196 
0197     return 0 ;
0198 }
0199 
0200 
0201 
0202 int SUTest::upload_array_sizeof()
0203 {
0204     const int N = 10 ;
0205     std::vector<float> demo(N) ;
0206     for(unsigned i=0 ; i < N ; i++) demo[i] = float(i)*100.f ;
0207 
0208     std::vector<float> demo2(N, 0.f);
0209 
0210     char* d_demo = SU::upload_array_sizeof( (char*)demo.data(), N, sizeof(float) ) ;
0211 
0212     SU::copy_device_to_host_sizeof( (char*)demo2.data(), d_demo, N, sizeof(float)) ;
0213     for(unsigned i=0 ; i < N ; i++) std::cout << " demo2[" << i << "] = " << demo2[i] << std::endl ;
0214 
0215     return 0;
0216 }
0217 
0218 int SUTest::hitlite()
0219 {
0220     NP* hitlite = NP::Load("$AFOLD/hitlite.npy");
0221     std::cout << " hitlite " << ( hitlite ? hitlite->sstr() : "-" ) << "\n" ;
0222     if(!hitlite) return 0 ;
0223 
0224     sphotonlite* ll = (sphotonlite*)hitlite->bytes();
0225     size_t N = hitlite->shape[0] ;
0226     for(size_t i=0 ; i < N ; i++)
0227     {
0228         const sphotonlite& l = ll[i];
0229         std::cout << std::setw(9) << i << " : " << l.desc() << "\n" ;
0230     }
0231 
0232     return 0 ;
0233 }
0234 
0235 
0236 
0237 
0238 int main(int argc, char** argv)
0239 {
0240     return SUTest::main(argc, argv);
0241 }
0242 
0243