Shows how to perform transforms on GPU if available.
Shows how to perform transforms on GPU if available.
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16 
   17 
   20 
   21import numpy        
   22import shtns        
   23                    
   24import time         
   25 
   26lmax = 1023         
   27 
   28sh_cpu = shtns.sht(lmax)  
   29sh_gpu = shtns.sht(lmax)  
   30 
   31
   32sh_cpu.set_grid(flags=shtns.SHT_THETA_CONTIGUOUS)   
   33sh_gpu.set_grid(flags=shtns.SHT_ALLOW_GPU + shtns.SHT_THETA_CONTIGUOUS)  
   34 
   35
   36
   37sh_cpu.print_info()
   38sh_gpu.print_info()  
   39 
   40print('cpu grid size:', sh_cpu.nlat, sh_cpu.nphi)     
   41print('gpu grid size:', sh_gpu.nlat, sh_gpu.nphi)     
   42 
   43cost = sh_gpu.cos_theta         
   44el = sh_gpu.l                   
   45l2 = el*(el+1)              
   46 
   47alm = sh_gpu.spec_array()       
   48alm[sh_gpu.idx(1, 0)] = 1.0     
   49 
   50t0 = time.perf_counter()
   51x_cpu = sh_cpu.synth(alm)           
   52t1 = time.perf_counter()
   53x_gpu = sh_gpu.synth(alm)
   54t2 = time.perf_counter()
   55 
   56print('transform on CPU took %.3g seconds' % (t1-t0))
   57print('transform on GPU took %.3g seconds (including copies between cpu and gpu -- this is %.3g times faster than CPU)' % (t2-t1, ((t1-t0)/(t2-t1))))
   58max_diff=numpy.max(abs(x_gpu-x_cpu))
   59print('maximum difference between gpu and cpu transformed data is', max_diff)
   60