Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /**
0002 s_mock_texture_test.cc
0003 ========================
0004 
0005 Aim is to be able to use QBnd.hh/qbnd.h in MOCK_CUDA manner on CPU. 
0006 
0007 ::
0008 
0009    ./s_mock_texture_test.sh
0010 
0011 
0012 **/
0013 
0014 #include <cstdio>
0015 #include "NPFold.h"
0016 
0017 #include "s_mock_texture.h"
0018 MockTextureManager* MockTextureManager::INSTANCE = nullptr ; 
0019 
0020 
0021 cudaTextureObject_t create_boundary_texture()
0022 {
0023     const char* base = "$HOME/.opticks/GEOM/$GEOM/CSGFoundry/SSim/stree/standard" ;
0024     const NP* bnd     = NP::Load(base, "bnd.npy"); 
0025 
0026 
0027     cudaTextureObject_t tex = MockTextureManager::Add(bnd) ;  ;  
0028     return tex ; 
0029 }
0030 
0031 NPFold* test_bnd()
0032 {
0033     cudaTextureObject_t obj = create_boundary_texture(); 
0034 
0035     std::cout << MockTextureManager::Desc() ; 
0036     std::cout << " obj : " << obj << std::endl ; 
0037 
0038     MockTextureManager* mgr = MockTextureManager::Get();  
0039     std::cout << mgr->dump<float>(obj) ; 
0040     std::cout << mgr->dump<float4>(obj) ; 
0041 
0042     const MockTexture tex = mgr->get(obj) ;  
0043     const NP* a = tex.a ;
0044  
0045 
0046     NP* b = NP::MakeLike(a) ; 
0047     float4* bb = b->values<float4>() ; 
0048 
0049     for(int iy=0 ; iy < tex.height ; iy++)
0050     for(int ix=0 ; ix < tex.width  ; ix++)
0051     {
0052         float x = (float(ix)+0.5f)/tex.width   ; 
0053         float y = (float(iy)+0.5f)/tex.height  ; 
0054         float4 payload = tex2D<float4>(obj, x, y ) ;
0055         int idx = iy*tex.width + ix ; 
0056         bb[idx] = payload ; 
0057     }
0058 
0059     NPFold* f = new NPFold ; 
0060     f->add("a",   a ); 
0061     f->add("b",   b ); 
0062     return f ; 
0063 }
0064 
0065 NPFold* test_demo_3()
0066 {
0067     int ny = 8 ;  // height
0068     int nx = 4 ;  // width 
0069 
0070     NP* a0 = NP::Make<float>(ny,nx,4) ; 
0071     cudaTextureObject_t obj = MockTextureManager::Add(a0) ;  ;  
0072     MockTextureManager* mgr = MockTextureManager::Get();  
0073 
0074     NP* a = mgr->tt[obj].a ; 
0075     float4* aa = a->values<float4>() ; 
0076 
0077     for(int iy=0 ; iy < ny ; iy++ )
0078     for(int ix=0 ; ix < nx ; ix++ )
0079     {
0080         int idx = iy*nx+ix ; 
0081         aa[idx] = make_float4( iy, ix, 0.f, 0.f );   // slower dimension first 
0082     }
0083 
0084     NP* b = NP::MakeLike(a) ; 
0085     float4* bb = b->values<float4>() ; 
0086 
0087     for(int iy=0 ; iy < ny ; iy++)
0088     for(int ix=0 ; ix < nx  ; ix++)
0089     {
0090         float x = (float(ix)+0.5f)/nx   ; 
0091         float y = (float(iy)+0.5f)/ny  ; 
0092         int idx = iy*nx + ix ; 
0093         bb[idx] = tex2D<float4>(obj, x, y ) ;
0094     }
0095 
0096     NPFold* f = new NPFold ; 
0097     f->add("a",   a ); 
0098     f->add("b",   b ); 
0099     return f ; 
0100 }
0101 
0102 
0103 NPFold* test_demo_5()
0104 {
0105     int ni = 10 ; 
0106     int nj =  4 ; 
0107     int nk =  2 ; 
0108     int nl = 100 ; 
0109     int nn =  4 ; 
0110 
0111     int ny = ni*nj*nk ;  
0112     int nx = nl ;
0113  
0114     int height = ny ; 
0115     int width = nx ; 
0116 
0117     NP* a0 = NP::Make<float>(ni,nj,nk,nl,nn) ; 
0118 
0119     cudaTextureObject_t obj = MockTextureManager::Add(a0) ;  ;  
0120     MockTextureManager* mgr = MockTextureManager::Get();  
0121 
0122     MockTexture tex = mgr->get(obj) ; 
0123 
0124     NP* a = const_cast<NP*>(tex.a) ;  // unusual to set the values after adding 
0125     float4* aa = a->values<float4>() ; 
0126 
0127     for(int i=0 ; i < ni ; i++)
0128     for(int j=0 ; j < nj ; j++)
0129     for(int k=0 ; k < nk ; k++)
0130     for(int l=0 ; l < nl ; l++)
0131     {
0132         int idx = i*nj*nk*nl + j*nk*nl + k*nl + l ; 
0133         aa[idx] = make_float4( i, j, k, l );   // slower dimension first 
0134     }
0135 
0136     NP* b = NP::MakeLike(a) ; 
0137     float4* bb = b->values<float4>() ; 
0138 
0139     for(int iy=0 ; iy < tex.height ; iy++)
0140     for(int ix=0 ; ix < tex.width  ; ix++)
0141     {
0142         float x = (float(ix)+0.5f)/tex.width   ; 
0143         float y = (float(iy)+0.5f)/tex.height  ; 
0144         int idx = iy*tex.width + ix ; 
0145         bb[idx] = tex2D<float4>(obj, x, y ) ;
0146     }
0147 
0148     NPFold* f = new NPFold ; 
0149     f->add("a",   a ); 
0150     f->add("b",   b ); 
0151     return f ; 
0152 }
0153 
0154 
0155 
0156 
0157 /**
0158 boundary_lookup
0159 -----------------
0160 
0161 HMM: this is duplicating stuff from qbnd. 
0162 Thats not what is needed need to implement stuff that mocks what 
0163 CUDA tex lookups are doing 
0164              
0165 ::
0166 
0167 
0168                   wl_samples
0169                   /
0170      (52, 4, 2, 761, 4, )
0171       |   |   \       \
0172      bnd omat paygrp   payvals
0173          osur
0174          isur
0175          imat 
0176 
0177     (ni, nj, nk, nl, nn )
0178 
0179     (bnd,species,paygrp) -> line "x"
0180     (wl) -> "y" 
0181 
0182 
0183 See:: 
0184 
0185    QBnd::MakeBoundaryTex
0186    qbnd::boundary_lookup
0187 
0188 **/
0189 
0190 float4 boundary_lookup(cudaTextureObject_t obj,  float nm, int line, int k ) 
0191 {
0192     MockTexture tex = MockTextureManager::Get(obj) ; 
0193  
0194     // follow qbnd::boundary_lookup
0195     float fx = (nm - tex.dom.x)/tex.dom.z ;   
0196     float x = (fx+0.5f)/float(tex.width) ; 
0197 
0198     int iy = 2*line + k ;    // k is 0 or 1 
0199     float y = (float(iy)+0.5f)/float(tex.height) ; 
0200 
0201     float4 props = tex2D<float4>(obj, x, y );  
0202     return props ; 
0203 }
0204 
0205 
0206 NPFold* test_boundary_lookup()
0207 {
0208     cudaTextureObject_t obj = create_boundary_texture(); 
0209     MockTexture tex = MockTextureManager::Get(obj) ; 
0210 
0211     const std::vector<int>& sh = tex.a->shape ; 
0212     int nd = sh.size() ; 
0213     assert( nd == 5 ); 
0214     assert( sh[nd-1] == 4 );  
0215 
0216     int ni = sh[0] ;  // bnd     0... ~50
0217     int nj = sh[1] ;  // species 0,1,2,3
0218     int nk = sh[2] ;  // group   0,1 
0219 
0220     NP* a = NP::Make<float>(ni,nj,nk,4) ; 
0221     float4* aa = a->values<float4>();  
0222 
0223     float wavelength = 440.f ; 
0224 
0225     for(int i=0 ; i < ni ; i++)
0226     for(int j=0 ; j < nj ; j++)
0227     for(int k=0 ; k < nk ; k++)
0228     {
0229         int line = i*nj+ j ; 
0230         float4 prop = boundary_lookup( obj, wavelength, line, k ); 
0231 
0232         int idx = i*nj*nk + j*nk + k ; 
0233         aa[idx] = prop ; 
0234     }
0235 
0236     NPFold* f = new NPFold ; 
0237     f->add("a", a) ; 
0238     return f ; 
0239 }
0240 
0241 int main(int argc, char** argv)
0242 {
0243     //NPFold* f = test_demo_3(); 
0244     //NPFold* f = test_demo_5(); 
0245     NPFold* f = test_bnd(); 
0246     //NPFold* f = test_boundary_lookup(); 
0247 
0248     f->save("$FOLD"); 
0249     return 0;
0250 }
0251