def prepare_node(self, node, storage_map, compute_map, impl): from skcuda.magma import magma_init ctx = node.inputs[0].type.context if not getattr(ctx, 'is_magma_initialized', False): with ctx: magma_init() ctx.is_magma_initialized = True
def main(): import argparse parser = argparse.ArgumentParser("Demo for Magma symmetric eigen-solver") parser.add_argument('-N', default=256, type=int, help='size of matrix') parser.add_argument('-t', default='s', type=str, help='data type: s or d, default s') parser.add_argument( '-ongpu', action='store_true', help='whether initial data on gpu (using _gpu functions), default False' ) parser.add_argument( '-ngpu', default=1, type=int, help= 'number of GPU (default 1), use -1 for running multi-gpu functions with single gpu' ) parser.add_argument('-expert', action='store_true', help='use the expert function (ssyevdx, dsyevdx)') parser.add_argument('-fulltest', action='store_true', help='full test except data-on-gpu functions') args = parser.parse_args() magma_init() if not args.fulltest: _test_syev(N=args.N, type_key=args.t, data_gpu=args.ongpu, ngpu=args.ngpu, expert=args.expert, keigs=None) else: for N in [200, 2000]: for t in ['s', 'd']: print("Data_on_host") for expert in [False, True]: for ngpu in [-1, 1]: print(f"N={N}; t={t}; ngpu={ngpu}; expert={expert}") _test_syev(N=N, type_key=t, data_gpu=False, ngpu=ngpu, expert=expert) print("Data_on_device") print("\n\n") for expert in [False, True]: print(f"N={N}; t={t}; expert={expert}") _test_syev(N=N, type_key=t, data_gpu=True, ngpu=1, expert=expert) magma_finalize()
def gpu_magma_magma_gesvd_A(input): coloring_print("\nGPU: skcuda.magma_gesvd 'A' option") input_original = input.copy() # magma is descructive magma.magma_init() # #縦横を逆(≒転地)してcolumn-majorにし、GPUのcusolverDnDgesvd()に対応させる。結果配列のU,Vが逆になる n, m = input.shape # change function by data type if input.dtype == np.dtype('float64'): get_buffer = magma.magma_dgesvd_buffersize magma_svd = magma.magma_dgesvd elif input.dtype == np.dtype('float32'): get_buffer = magma.magma_sgesvd_buffersize magma_svd = magma.magma_sgesvd else: print "Error: data type must be float64 or float32" # Set up work buffers: Lwork = get_buffer('A', 'A', m, n) workspace = np.zeros(Lwork, input.dtype) # Set up output buffers: s = np.zeros(min(m, n), input.dtype) # u = np.zeros((n, n), input.dtype) # vh = np.zeros((m, m), input.dtype) u = np.zeros((m, m), input.dtype) vh = np.zeros((n, n), input.dtype) # Compute: magma_svd_A_start = time.time() status = magma_svd( 'A', # jobu 'A', # jobvt m, # m n, # n input.ctypes.data, # A m, # lda s.ctypes.data, # s u.ctypes.data, # U m, # ldu vh.ctypes.data, # VT n, # ldvt workspace.ctypes.data, # work Lwork) # lwork magma_svd_A_end = time.time() print "magma_sgesvd A option (= Total): ", magma_svd_A_end - magma_svd_A_start, "[sec]" print "Total: ", magma_svd_A_end - magma_svd_A_start, "[sec]" # magma is descructive, u and s is swapped (数学的に正しいかはわからない) check_result(input_original, vh, s, u) magma.magma_finalize()
def gpu_magma_magma_gesvd_S(input): coloring_print("\nGPU: skcuda.magma_gesvd 'S' option") input_original = input.copy() magma.magma_init() # #縦横を逆(≒転地)してcolumn-majorにし、GPUのcusolverDnDgesvd()に対応させる。結果配列のU,Vが逆になる n, m = input.shape # change function by data type if input.dtype == np.dtype('float64'): get_buffer = magma.magma_dgesvd_buffersize magma_svd = magma.magma_dgesvd elif input.dtype == np.dtype('float32'): get_buffer = magma.magma_sgesvd_buffersize magma_svd = magma.magma_sgesvd else: print "Error: data type must be float64 or float32" Lwork = get_buffer('A', 'A', m, n) workspace = np.zeros(Lwork, input.dtype) # The size of u and vh in 'S' are different from those of 'A' s = np.zeros(min(m, n), input.dtype) u = np.zeros((n, m), input.dtype) vh = np.zeros((n, n), input.dtype) # Compute: magma_svd_start = time.time() status = magma_svd( 'S', # jobu 'S', # jobvt m, # m n, # n input.ctypes.data, # A m, # lda s.ctypes.data, # s u.ctypes.data, # U m, # ldu vh.ctypes.data, # VT n, # ldvt workspace.ctypes.data, # work Lwork) # lwork magma_svd_end = time.time() print "magma_sgesvd S option (= Total): ", magma_svd_end - magma_svd_start, "[sec]", ( pycuda.driver.mem_get_info()[1] - pycuda.driver.mem_get_info()[0]) / 1024 / 1024 print "Total: ", magma_svd_end - magma_svd_start, "[sec]" # magma is descructive, u and s is swapped (数学的に正しいかはわからない) check_result(input_original, vh, s, u) magma.magma_finalize()
def setUpClass(cls): cls.ctx = make_default_context() magma.magma_init()
#!/usr/bin/env python """ Demo of how to call low-level MAGMA wrappers to perform SVD decomposition. Note MAGMA's SVD implementation is a hybrid of CPU/GPU code; the inputs therefore must be in host memory. """ import numpy as np import skcuda.magma as magma magma.magma_init() x = np.asarray([[1.80, 2.88, 2.05, -0.89], [5.25, -2.95, -0.95, -3.80], [1.58, -2.69, -2.90, -1.04], [-1.11, -0.66, -0.59, 0.80]]).astype(np.float32) x_orig = x.copy() # Need to reverse dimensions because MAGMA expects column-major matrices: n, m = x.shape # Set up output buffers: s = np.zeros(min(m, n), np.float32) u = np.zeros((m, m), np.float32) vh = np.zeros((n, n), np.float32) # Set up workspace: Lwork = magma.magma_sgesvd_buffersize('A', 'A', m, n, x.ctypes.data, m, s.ctypes.data, u.ctypes.data, m, vh.ctypes.data, n) workspace = np.zeros(Lwork, np.float32)
def eigs(a_gpu, k=None, which='LM', imag=False, return_eigenvectors=True): """ Driver for eigenvalue solver for symmetric matrix """ if len(a_gpu.shape) != 2: raise ValueError("M needs to be a rank 2 square array for eig.") magma.magma_init() ngpu=1 dtype = a_gpu.dtype.type t = typedict_[dtype] N = a_gpu.shape[1] if k is None: k=int(np.ceil(N/2)) if 'L' in which: a_gpu = -a_gpu if return_eigenvectors: jobz = 'V' else: jobz = 'N' rnge = 'I'; uplo = 'U' vl = 0; vu = 1e10 il = 1; iu = k; m = 0 print(f"k = {k}, iu = {iu}") w_gpu = np.empty((N,), dtype, order='F') # eigenvalues if t == 's': nb = magma.magma_get_ssytrd_nb(N) elif t == 'd': nb = magma.magma_get_dsytrd_nb(N) else: raise ValueError('unsupported type') lwork = N*(1 + 2*nb) if jobz: lwork = max(lwork, 1+6*N+2*N**2) work = np.empty(lwork, dtype) liwork = 3+5*N if jobz else 1 iwork = np.empty(liwork, dtype) if t == 's': status = magma.magma_ssyevdx_m(ngpu, jobz, rnge, uplo, N, a_gpu.ctypes.data, N, vl, vu, il, iu, m, w_gpu.ctypes.data, work.ctypes.data, lwork, iwork.ctypes.data, liwork) elif t == 'd': status = magma.magma_dsyevdx_m(ngpu, jobz, rnge, uplo, N, a_gpu.ctypes.data, N, vl, vu, il, iu, m, w_gpu.ctypes.data, work.ctypes.data, lwork, iwork.ctypes.data, liwork) else: raise ValueError('unsupported type') if 'L' in which: w_gpu = -w_gpu magma.magma_finalize() if jobz: return w_gpu, a_gpu else: return w_gpu