def test_shift(self): shape = (55, 66, 77) a = np.random.randint(65536, size=shape).astype(np.int32) a = bf.asarray(a, space='cuda') b = bf.empty_like(a) for _ in range(3): bf.map("b = a(_-a.shape()/2)", data={'a': a, 'b': b}) a = a.copy('system') b = b.copy('system') np.testing.assert_equal(b, np.fft.fftshift(a))
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data shape = idata.shape ind_names = ['i%i' % i for i in xrange(idata.ndim)] inds = list(ind_names) for ax in self.axes: inds[ax] = '-' + inds[ax] inds = ','.join(inds) bf.map("b = a(%s)" % inds, shape, *ind_names, a=idata, b=odata)
def test_manydim(self): known_data = np.arange(3**8).reshape([3] * 8).astype(np.float32) a = bf.asarray(known_data, space='cuda') a = a[:, :, :, :, :2, :, :, :] b = bf.empty_like(a) for _ in range(3): bf.map("b = a+1", data={'a': a, 'b': b}) a = a.copy('system') b = b.copy('system') np.testing.assert_equal(b, a + 1)
def test_scalar(self): n = 7919 # Note: Python integer division rounds to -inf, while C rounds toward 0 # We avoid the problem here by using only positive values x = np.random.randint(1, 256, size=n) x = bf.asarray(x, space='cuda') y = bf.empty_like(x) bf.map("y = (x-m)/s", x=x, y=y, m=1, s=3) x = x.copy('system') y = y.copy('system') np.testing.assert_equal(y, (x - 1) // 3)
def test_explicit_indexing(self): shape = (55,66,77) a = np.random.randint(65536, size=shape).astype(np.int32) a = bf.asarray(a, space='cuda') b = bf.empty((a.shape[2],a.shape[0], a.shape[1]), a.dtype, 'cuda') for _ in xrange(3): bf.map("b(i,j,k) = a(j,k,i)", shape=b.shape, axis_names=('i','j','k'), data={'a': a, 'b': b}, block_shape=(64,4), block_axes=('i','k')) a = a.copy('system') b = b.copy('system') np.testing.assert_equal(b, a.transpose([2,0,1]))
def test_scalar(self): n = 7919 # Note: Python integer division rounds to -inf, while C rounds toward 0 # We avoid the problem here by using only positive values x = np.random.randint(1, 256, size=n) x = bf.asarray(x, space='cuda') y = bf.empty_like(x) for _ in xrange(3): bf.map("y = (x-m)/s", data={'x': x, 'y': y, 'm': 1, 's': 3}) x = x.copy('system') y = y.copy('system') np.testing.assert_equal(y, (x - 1) // 3)
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data #print ospan.data.shape, ispan.data.shape bf.map("b(i, j, k, l) = a(i, j, k, l)", ospan.data.shape, 'i', 'j', 'k', 'l', a=ispan.data, b=ospan.data)
def test_broadcast(self): n = 89 a = np.arange(n).astype(np.float32) a = bf.asarray(a, space='cuda') b = a[:, None] c = bf.empty((a.shape[0], b.shape[0]), a.dtype, 'cuda') # TODO: Need way to compute broadcast shape bf.map("c = a*b", a=a, b=b, c=c) a = a.copy('system') b = b.copy('system') c = c.copy('system') np.testing.assert_equal(c, a * b)
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data beta = 0. if self.frame_count == 0 else 1. bf.map("b = beta * b + (b_type)a", a=idata, b=odata, beta=beta) self.frame_count += 1 if self.frame_count == self.nframe: ncommit = 1 self.frame_count = 0 else: ncommit = 0 return ncommit
def test_custom_shape(self): shape = (55,66,77) a = np.random.randint(65536, size=shape).astype(np.int32) a = bf.asarray(a, space='cuda') b = bf.empty((a.shape[0],a.shape[2]), a.dtype, 'cuda') j = 11 for _ in xrange(3): bf.map("b(i,k) = a(i,j,k)", shape=b.shape, axis_names=('i','k'), data={'a': a, 'b': b, 'j': j}) a = a.copy('system') b = b.copy('system') np.testing.assert_equal(b, a[:,j,:])
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data axes = [] for i in range(len(idata.shape)): axes.append(chr(i + ord('i'))) bf.map('b(%s) = Complex<float>(a(%s))' % (','.join(axes), ','.join(axes)), { 'a': idata, 'b': odata }, axis_names=axes, shape=idata.shape)
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data shape = idata.shape ind_names = ['i%i' % i for i in xrange(idata.ndim)] inds = list(ind_names) for ax in self.axes: if self.inverse: inds[ax] += '-(a.shape(%i)-a.shape(%i)/2)' % (ax, ax) else: inds[ax] += '-a.shape(%i)/2' % ax inds = ','.join(inds) bf.map("b = a(%s)" % inds, shape, *ind_names, a=idata, b=odata)
def run_simple_test(self, x, funcstr, func): x_orig = x x = bf.asarray(x, 'cuda') y = bf.empty_like(x) x.flags['WRITEABLE'] = False x.bf.immutable = True # TODO: Is this actually doing anything? (flags is, just not sure about bf.immutable) bf.map(funcstr, x=x, y=y) x = x.copy('system') y = y.copy('system') if isinstance(x_orig, bf.ndarray): x_orig = x # Note: Using func(x) is dangerous because bf.ndarray does things like # lazy .conj(), which break when used as if it were np.ndarray. np.testing.assert_equal(y, func(x_orig))
def run_simple_test(self, x, funcstr, func): x_orig = x x = bf.asarray(x, 'cuda_managed') y = bf.empty_like(x) x.flags['WRITEABLE'] = False x.bf.immutable = True # TODO: Is this actually doing anything? (flags is, just not sure about bf.immutable) for _ in range(3): bf.map(funcstr, {'x': x, 'y': y}) stream_synchronize() if isinstance(x_orig, bf.ndarray): x_orig = x # Note: Using func(x) is dangerous because bf.ndarray does things like # lazy .conj(), which break when used as if it were np.ndarray. np.testing.assert_equal(y, func(x_orig))
def test_complex_integer(self): n = 7919 for in_dtype in ('ci4', 'ci8', 'ci16', 'ci32'): a_orig = bf.ndarray(shape=(n, ), dtype=in_dtype, space='system') try: a_orig['re'] = np.random.randint(256, size=n) a_orig['im'] = np.random.randint(256, size=n) except ValueError: # ci4 is different a_orig['re_im'] = np.random.randint(256, size=n) for out_dtype in (in_dtype, 'cf32'): a = a_orig.copy(space='cuda') b = bf.ndarray(shape=(n, ), dtype=out_dtype, space='cuda') bf.map('b(i) = a(i)', { 'a': a, 'b': b }, shape=a.shape, axis_names=('i', )) a = a.copy(space='system') try: a = a['re'] + 1j * a['im'] except ValueError: # ci4 is different a = np.int8(a['re_im'] & 0xF0) + 1j * np.int8( (a['re_im'] & 0x0F) << 4) a /= 16 b = b.copy(space='system') try: b = b['re'] + 1j * b['im'] except ValueError: # ci4 is different b = np.int8(b['re_im'] & 0xF0) + 1j * np.int8( (b['re_im'] & 0x0F) << 4) b /= 16 except IndexError: # pass through cf32 pass np.testing.assert_equal(a, b)
def test_polarisation_products(self): n = 89 real = np.random.randint(-127, 128, size=(n,2)).astype(np.float32) imag = np.random.randint(-127, 128, size=(n,2)).astype(np.float32) a = real + 1j * imag a_orig = a a = bf.asarray(a, space='cuda') b = bf.empty_like(a) for _ in xrange(3): bf.map(''' auto x = a(_,0); auto y = a(_,1); b(_,0).assign(x.mag2(), y.mag2()); b(_,1) = x*y.conj(); ''', shape=b.shape[:-1], data={'a': a, 'b': b}) b = b.copy('system') a = a_orig gold = np.empty_like(a) def mag2(x): return x.real * x.real + x.imag * x.imag gold[...,0] = mag2(a[...,0]) + 1j * mag2(a[...,1]) gold[...,1] = a[...,0] * a[...,1].conj() np.testing.assert_equal(b, gold)
def on_data(self, ispan, ospan): idata = ispan.data odata = ospan.data itype = DataType(idata.dtype) otype = DataType(odata.dtype) if self.ifmt == 'matrix' and self.ofmt == 'matrix': # Make a full-matrix copy of the lower-only input matrix # odata[t,c,i,p,j,q] = idata[t,c,i,p,j,q] (lower filled only) shape_nopols = list(idata.shape) del shape_nopols[5] del shape_nopols[3] idata = idata.view(itype.as_vector(2)) odata = odata.view(otype.as_vector(2)) bf.map(''' bool in_lower_triangle = (i > j); if( in_lower_triangle ) { odata(t,c,i,0,j,0) = idata(t,c,i,0,j,0); odata(t,c,i,1,j,0) = idata(t,c,i,1,j,0); } else { auto x = idata(t,c,j,0,i,0); auto y = idata(t,c,j,1,i,0); auto x1 = x[1]; x[0] = x[0].conj(); x[1] = y[0].conj(); if( i != j ) { y[0] = x1.conj(); } y[1] = y[1].conj(); odata(t,c,i,0,j,0) = x; odata(t,c,i,1,j,0) = y; } ''', shape=shape_nopols, axis_names=['t', 'c', 'i', 'j'], data={ 'idata': idata, 'odata': odata }) elif self.ifmt == 'matrix' and self.ofmt == 'storage': assert (idata.shape[2] <= 2048) idata = idata.view(itype.as_vector(2)) odata = odata.view(otype.as_vector(4)) # TODO: Support L/R as well as X/Y pols bf.map(''' // TODO: This only works up to 2048 in single-precision #define project_triangular(i, j) ((i)*((i)+1)/2 + (j)) int i = int((sqrt(8.f*(b)+1)-1)/2); int j = b - project_triangular(i, 0); auto x = idata(t,c,i,0,j,0); auto y = idata(t,c,i,1,j,0); if( i == j ) { x[1] = y[0].conj(); } idata_type::value_type eye(0, 1); auto I = (x[0] + y[1]); auto Q = (x[0] - y[1]); auto U = (x[1] + y[0]); auto V = (x[1] - y[0]) * eye; odata(t,b,c,0) = odata_type(I,Q,U,V); ''', shape=odata.shape[:-1], axis_names=['t', 'b', 'c'], data={ 'idata': idata, 'odata': odata }, block_shape=[64, 8]) # TODO: Tune this #elif self.ifmt == 'matrix' and self.ofmt == 'triangular': elif self.ifmt == 'storage' and self.ofmt == 'matrix': oshape_nopols = list(odata.shape) del oshape_nopols[5] del oshape_nopols[3] idata = idata.view(itype.as_vector(4)) odata = odata.view(otype.as_vector(2)) bf.map(''' bool in_upper_triangle = (i < j); auto b = in_upper_triangle ? j*(j+1)/2 + i : i*(i+1)/2 + j; auto IQUV = idata(t,b,c,0); auto I = IQUV[0], Q = IQUV[1], U = IQUV[2], V = IQUV[3]; idata_type::value_type eye(0, 1); auto xx = 0.5f*(I + Q); auto xy = 0.5f*(U - V*eye); auto yx = 0.5f*(U + V*eye); auto yy = 0.5f*(I - Q); if( i == j ) { xy = yx.conj(); } if( in_upper_triangle ) { auto tmp_xy = xy; xx = xx.conj(); xy = yx.conj(); yx = tmp_xy.conj(); yy = yy.conj(); } odata(t,c,i,0,j,0) = odata_type(xx, xy); odata(t,c,i,1,j,0) = odata_type(yx, yy); ''', shape=oshape_nopols, axis_names=['t', 'c', 'i', 'j'], data={ 'idata': idata, 'odata': odata }, block_shape=[64, 8]) # TODO: Tune this else: raise NotImplementedError