def test_cuda(): import numpy import matplotlib.pyplot # load example image import pkg_resources ## Define the source of data DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/') # PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt') import scipy image = scipy.misc.ascent() image = scipy.misc.imresize(image, (256, 256)) image = image.astype(numpy.float) / numpy.max(image[...]) Nd = (256, 256) # image space size Kd = (512, 512) # k-space size Jd = (6, 6) # interpolation size # load k-space points as M * 2 array om = numpy.load(DATA_PATH + 'om2D.npz')['arr_0'] # Show the shape of om print('the shape of om = ', om.shape) # initiating NUFFT_cpu object nfft = NUFFT_cpu() # CPU NUFFT class # Plan the nfft object nfft.plan(om, Nd, Kd, Jd) # initiating NUFFT_hsa object NufftObj = NUFFT_hsa('cuda', 0, 0) # Plan the NufftObj (similar to NUFFT_cpu) NufftObj.plan(om, Nd, Kd, Jd) import time t0 = time.time() for pp in range(0, 10): y = nfft.forward(image) t_cpu = (time.time() - t0) / 10.0 ## Moving image to gpu ## gx is an gpu array, dtype = complex64 gx = NufftObj.to_device(image) t0 = time.time() for pp in range(0, 100): gy = NufftObj.forward(gx) t_cu = (time.time() - t0) / 100 print('t_cpu = ', t_cpu) print('t_cuda =, ', t_cu) print('gy close? = ', numpy.allclose(y, gy.get(), atol=numpy.linalg.norm(y) * 1e-3)) print("acceleration=", t_cpu / t_cu) maxiter = 100 import time t0 = time.time() x_cpu_cg = nfft.solve(y, 'cg', maxiter=maxiter) # x2 = nfft.solve(y2, 'L1TVLAD',maxiter=maxiter, rho = 2) t1 = time.time() - t0 # gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2)) t0 = time.time() x_cuda_cg = NufftObj.solve(gy, 'cg', maxiter=maxiter) # x = NufftObj.solve(gy,'L1TVLAD', maxiter=maxiter, rho=2) t2 = time.time() - t0 print(t1, t2) print('acceleration of cg=', t1 / t2) t0 = time.time() x_cpu_TV = nfft.solve(y, 'L1TVOLS', maxiter=maxiter, rho=2) t1 = time.time() - t0 t0 = time.time() x_cuda_TV = NufftObj.solve(gy, 'L1TVOLS', maxiter=maxiter, rho=2) t2 = time.time() - t0 print(t1, t2) print('acceleration of TV=', t1 / t2) matplotlib.pyplot.subplot(2, 2, 1) matplotlib.pyplot.imshow(x_cpu_cg.real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('CG_cpu') matplotlib.pyplot.subplot(2, 2, 2) matplotlib.pyplot.imshow(x_cuda_cg.get().real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('CG_cuda') matplotlib.pyplot.subplot(2, 2, 3) matplotlib.pyplot.imshow(x_cpu_TV.real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('TV_cpu') matplotlib.pyplot.subplot(2, 2, 4) matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('TV_cuda') matplotlib.pyplot.show() NufftObj.release() del NufftObj
def test_opencl_multicoils(): import numpy import matplotlib.pyplot # load example image import pkg_resources ## Define the source of data DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/') # PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt') import scipy image = scipy.misc.ascent()[::2, ::2] image = image.astype(numpy.float) / numpy.max(image[...]) Nd = (256, 256) # image space size Kd = (512, 512) # k-space size Jd = (6, 6) # interpolation size # load k-space points as M * 2 array om = numpy.load(DATA_PATH + 'om2D.npz')['arr_0'] # Show the shape of om print('the shape of om = ', om.shape) batch = 8 # initiating NUFFT_cpu object nfft = NUFFT_cpu() # CPU NUFFT class # Plan the nfft object nfft.plan(om, Nd, Kd, Jd, batch=batch) # initiating NUFFT_hsa object try: NufftObj = NUFFT_hsa('cuda', 0, 0) except: try: NufftObj = NUFFT_hsa('ocl', 1, 0) except: NufftObj = NUFFT_hsa('ocl', 0, 0) # Plan the NufftObj (similar to NUFFT_cpu) NufftObj.plan(om, Nd, Kd, Jd, batch=batch, radix=2) coil_sense = numpy.ones(Nd + (batch, ), dtype=numpy.complex64) for cc in range(0, batch, 2): coil_sense[int(256 / batch) * cc:int(256 / batch) * (cc + 1), :, cc].real *= 0.1 coil_sense[:, int(256 / batch) * cc:int(256 / batch) * (cc + 1), cc].imag *= -0.1 NufftObj.set_sense(coil_sense) nfft.set_sense(coil_sense) y = nfft.forward_one2many(image) import time t0 = time.time() for pp in range(0, 2): xx = nfft.adjoint_many2one(y) t_cpu = (time.time() - t0) / 2 ## Moving image to gpu ## gx is an gpu array, dtype = complex64 gx = NufftObj.to_device(image) gy = NufftObj.forward_one2many(gx) t0 = time.time() for pp in range(0, 10): gxx = NufftObj.adjoint_many2one(gy) t_cu = (time.time() - t0) / 10 print(y.shape, gy.get().shape) print('t_cpu = ', t_cpu) print('t_cuda =, ', t_cu) print('gy close? = ', numpy.allclose(y, gy.get(), atol=numpy.linalg.norm(y) * 1e-6)) print('gy error = ', numpy.linalg.norm(y - gy.get()) / numpy.linalg.norm(y)) print('gxx close? = ', numpy.allclose(xx, gxx.get(), atol=numpy.linalg.norm(xx) * 1e-6)) print('gxx error = ', numpy.linalg.norm(xx - gxx.get()) / numpy.linalg.norm(xx)) # for bb in range(0, batch): matplotlib.pyplot.subplot(1, 2, 1) matplotlib.pyplot.imshow(xx[...].real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('Adjoint_cpu_coil') matplotlib.pyplot.subplot(1, 2, 2) matplotlib.pyplot.imshow(gxx.get()[...].real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('Adjoint_hsa_coil') # matplotlib.pyplot.subplot(2, 2, 3) # matplotlib.pyplot.imshow( x_cpu_TV.real, cmap= matplotlib.cm.gray) # matplotlib.pyplot.title('TV_cpu')# x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2) # matplotlib.pyplot.subplot(2, 2, 4) # matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap= matplotlib.cm.gray) # matplotlib.pyplot.title('TV_cuda') matplotlib.pyplot.show(block=False) matplotlib.pyplot.pause(1) matplotlib.pyplot.close() print("acceleration=", t_cpu / t_cu) maxiter = 100 import time t0 = time.time() x_cpu_cg = nfft.solve(y, 'cg', maxiter=maxiter) # x2 = nfft.solve(y2, 'L1TVLAD',maxiter=maxiter, rho = 2) t1 = time.time() - t0 # gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2)) t0 = time.time() x_cuda_cg = NufftObj.solve(gy, 'cg', maxiter=maxiter) # x = NufftObj.solve(gy,'L1TVLAD', maxiter=maxiter, rho=2) print('shape of cg = ', x_cuda_cg.get().shape, x_cpu_cg.shape) t2 = time.time() - t0 print(t1, t2) print('acceleration of cg=', t1 / t2) t0 = time.time() # x_cpu_TV = nfft.solve(y, 'L1TVOLS',maxiter=maxiter, rho = 2) t1 = time.time() - t0 t0 = time.time() # x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2) t2 = time.time() - t0 print(t1, t2) # print('acceleration of TV=', t1/t2 ) # try: for bb in range(0, batch): matplotlib.pyplot.subplot(2, batch, 1 + bb) matplotlib.pyplot.imshow(x_cpu_cg[..., bb].real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('CG_cpu_coil_' + str(bb)) matplotlib.pyplot.subplot(2, batch, 1 + batch + bb) matplotlib.pyplot.imshow(x_cuda_cg.get()[..., bb].real, cmap=matplotlib.cm.gray) matplotlib.pyplot.title('CG_hsa_coil_' + str(bb)) # matplotlib.pyplot.subplot(2, 2, 3) # matplotlib.pyplot.imshow( x_cpu_TV.real, cmap= matplotlib.cm.gray) # matplotlib.pyplot.title('TV_cpu')# x_cuda_TV = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2) # matplotlib.pyplot.subplot(2, 2, 4) # matplotlib.pyplot.imshow(x_cuda_TV.get().real, cmap= matplotlib.cm.gray) # matplotlib.pyplot.title('TV_cuda') matplotlib.pyplot.show() # except: # print('no matplotlib') NufftObj.release() del NufftObj
numpy.random.seed(0) om = numpy.random.randn(int(5e+5), 3) print(om.shape) from pynufft import NUFFT_cpu, NUFFT_hsa, NUFFT_hsa_legacy NufftObj = NUFFT_hsa(API='ocl', platform_number=1, device_number=0) NufftObj.plan(om, Nd, Kd, Jd) # NufftObj.offload(API = 'cuda', platform_number = 0, device_number = 0) gx = NufftObj.thr.to_device(image.astype(numpy.complex64)) gy = NufftObj.forward(gx) import time t0 = time.time() restore_x2 = GBPDNA_old(NufftObj, gy, maxiter=5) t1 = time.time() restore_x = NufftObj.solve(gy, 'cg', maxiter=50) t2 = time.time() print("GBPDNA time = ", t1 - t0) print("CG time = ", t2 - t1) #restore_image1 = NufftObj.solve(kspace,'L1TVLAD', maxiter=300,rho=0.1) # # restore_x2 = NufftObj.solve(gy,'L1TVOLS', maxiter=100,rho=0.2) # tau_1 = 1 # tau_2 = 0.1 pyplot.subplot(1, 2, 1) pyplot.imshow(numpy.real(gx.get()[:, :, mid_slice]), label='original signal', cmap=gray) pyplot.title('original')
def test_init(): # cm = matplotlib.cm.gray # load example image import pkg_resources DATA_PATH = pkg_resources.resource_filename('pynufft', 'src/data/') # PHANTOM_FILE = pkg_resources.resource_filename('pynufft', 'data/phantom_256_256.txt') import numpy # import matplotlib.pyplot import scipy image = scipy.misc.ascent()[::2,::2] image=image.astype(numpy.float)/numpy.max(image[...]) Nd = (256, 256) # image space size Kd = (512, 512) # k-space size Jd = (6,6) # interpolation size # load k-space points om = numpy.load(DATA_PATH+'om2D.npz')['arr_0'] nfft = NUFFT_cpu() # CPU nfft.plan(om, Nd, Kd, Jd) try: NufftObj = NUFFT_hsa('cuda',0,0) except: NufftObj = NUFFT_hsa('ocl',0,0) # NufftObj2 = NUFFT_hsa('cuda',0,0) NufftObj.debug = 1 NufftObj.plan(om, Nd, Kd, Jd, radix=2) # NufftObj2.plan(om, Nd, Kd, Jd) # NufftObj.offload(API = 'cuda', platform_number = 0, device_number = 0) # NufftObj2.offload(API = 'cuda', platform_number = 0, device_number = 0) # NufftObj2.offload('cuda') # NufftObj.offload(API = 'cuda', platform_number = 0, device_number = 0) # print('api=', NufftObj.thr.api_name()) # NufftObj.offload(API = 'ocl', platform_number = 0, device_number = 0) y = nfft.k2y(nfft.xx2k(nfft.x2xx(image))) NufftObj.x_Nd = NufftObj.thr.to_device( image.astype(dtype)) gx = NufftObj.thr.copy_array(NufftObj.x_Nd) print('x close? = ', numpy.allclose(image, gx.get() , atol=1e-4)) gxx = NufftObj.x2xx(gx) print('xx close? = ', numpy.allclose(nfft.x2xx(image), gxx.get() , atol=1e-4)) gk = NufftObj.xx2k(gxx) k = nfft.xx2k(nfft.x2xx(image)) print('k close? = ', numpy.allclose(nfft.xx2k(nfft.x2xx(image)), gk.get(), atol=1e-3*numpy.linalg.norm(k))) gy = NufftObj.k2y(gk) k2 = NufftObj.y2k(gy) print('y close? = ', numpy.allclose(y, gy.get() , atol=1e-3*numpy.linalg.norm(y)), numpy.linalg.norm((y - gy.get())/numpy.linalg.norm(y))) y2 = y print('k2 close? = ', numpy.allclose(nfft.y2k(y2), k2.get(), atol=1e-3*numpy.linalg.norm(nfft.y2k(y2)) ), numpy.linalg.norm(( nfft.y2k(y2)- k2.get())/numpy.linalg.norm(nfft.y2k(y2)))) gxx2 = NufftObj.k2xx(k2) # print('xx close? = ', numpy.allclose(nfft.k2xx(nfft.y2k(y2)), NufftObj.xx_Nd.get(queue=NufftObj.queue, async=False) , atol=0.1)) gx2 = NufftObj.xx2x(gxx2) print('x close? = ', numpy.allclose(nfft.adjoint(y2), gx2.get() , atol=1e-3*numpy.linalg.norm(nfft.adjoint(y2)))) image3 = gx2.get() import time t0 = time.time() # k = nfft.xx2k(nfft.x2xx(image)) for pp in range(0,50): # y = nfft.k2y(nfft.xx2k(nfft.x2xx(image))) y = nfft.forward(image) # y = nfft.k2y(k) # k = nfft.y2k(y) # x = nfft.adjoint(y) # y = nfft.forward(image) # y2 = NufftObj.y.get( NufftObj.queue, async=False) t_cpu = (time.time() - t0)/50.0 print(t_cpu) # del nfft gy2=NufftObj.forward(gx) # gk = NufftObj.xx2k(NufftObj.x2xx(gx)) t0= time.time() for pp in range(0,20): # pass gy2 = NufftObj.forward(gx) # gy2 = NufftObj.k2y(gk) # gx2 = NufftObj.adjoint(gy2) # gk2 = NufftObj.y2k(gy2) # del gy2 # c = gx2.get() # gy=NufftObj.forward(gx) NufftObj.thr.synchronize() t_cl = (time.time() - t0)/20 print(t_cl) print('gy close? = ', numpy.allclose(y, gy.get(), atol=numpy.linalg.norm(y)*1e-3)) print("acceleration=", t_cpu/t_cl) maxiter =100 import time t0= time.time() # x2 = nfft.solve(y2, 'cg',maxiter=maxiter) x2 = nfft.solve(y2, 'L1TVOLS',maxiter=maxiter, rho = 2) t1 = time.time()-t0 # gy=NufftObj.thr.copy_array(NufftObj.thr.to_device(y2)) t0= time.time() # x = NufftObj.solve(gy,'cg', maxiter=maxiter) x = NufftObj.solve(gy,'L1TVOLS', maxiter=maxiter, rho=2) t2 = time.time() - t0 print(t1, t2) print('acceleration=', t1/t2 ) # k = x.get() # x = nfft.k2xx(k)/nfft.st['sn'] # return try: import matplotlib.pyplot matplotlib.pyplot.subplot(1, 2, 1) matplotlib.pyplot.imshow( x.get().real, cmap= matplotlib.cm.gray, vmin = 0, vmax = 1) matplotlib.pyplot.title("HSA reconstruction") matplotlib.pyplot.subplot(1, 2,2) matplotlib.pyplot.imshow(x2.real, cmap= matplotlib.cm.gray) matplotlib.pyplot.title("CPU reconstruction") matplotlib.pyplot.show(block = False) matplotlib.pyplot.pause(3) matplotlib.pyplot.close() # del NufftObj.thr # del NufftObj except: print("no graphics")