def test_dirichlet(): obj = test('dirichlet', 'dirichlet') obj._A_q1, obj._A_q2 = af.Array([100]), af.Array([100]) obj._A_q1 = af.tile(obj._A_q1, 1, 1, obj.q1_center.shape[2]) obj._A_q2 = af.tile(obj._A_q2, 1, 1, obj.q1_center.shape[2]) obj.dt = 0.001 obj.f = af.constant(0, obj.q1_center.shape[0], obj.q1_center.shape[1], obj.q1_center.shape[2], dtype=af.Dtype.f64) apply_bcs_f(obj) expected = af.constant(0, obj.q1_center.shape[0], obj.q1_center.shape[1], obj.q1_center.shape[2], dtype=af.Dtype.f64) N_g = obj.N_ghost # Only ingoing characteristics should be affected: expected[:N_g] = af.select(obj.q1_center < obj.q1_start, 1, expected)[:N_g] expected[:, :N_g] = af.select(obj.q2_center < obj.q2_start, 2, expected)[:, :N_g] assert (af.max(af.abs(obj.f[:, N_g:-N_g] - expected[:, N_g:-N_g])) < 5e-14) assert (af.max(af.abs(obj.f[N_g:-N_g, :] - expected[N_g:-N_g, :])) < 5e-14)
def np2af(data, dtype=None): # return af.Array(data.ctypes.data, data.shape, data.dtype.char) print(type(data)) if dtype: return af.Array(data.ctypes.data, data.shape, dtype) else: return af.Array(data.ctypes.data, data.shape, data.dtype.char)
def _split_fourier_cuda(self, signal: Signal, step): ''' This function is called by split_fourier,and should not be used outside :param signal: signal to traverse the span :param step: the step of split fourier :return: None ''' af.set_backend('cuda') freq = fftfreq(len(signal.data_sample[0, :]), (signal.sps * signal.symbol_rate_in_hz)**(-1)) freq = af.Array(freq.ctypes.data, freq.shape, freq.dtype.char) signal_x = np.asarray(signal.data_sample[0, :]) signal_y = np.asarray(signal.data_sample[1, :]) signal_x = af.Array(signal_x.ctypes.data, signal_x.shape, dtype=signal_x.dtype.char) signal_y = af.Array(signal_y.ctypes.data, signal_x.shape, dtype=signal_y.dtype.char) Disper = (1j / 2) * self.beta2 * (2 * np.pi * freq)**2 * step + ( 1j / 6) * self.beta3 * ( (2 * np.pi * freq)**3 * step) - self.alphalin / 2 * step dz_Eff = (1 - np.exp(-self.alphalin * step)) / self.alphalin step_number = np.ceil(self.length / step) for number in range(int(step_number)): print(number) if number == step_number - 1: # dz = step dz = self.length - (step_number - 1) * step dz_Eff = (1 - np.exp(-self.alphalin * dz)) / self.alphalin Disper = (1j / 2) * self.beta2 * (2 * np.pi * freq)**2 * dz + ( 1j / 6) * self.beta3 * ( (2 * np.pi * freq)**3 * dz) - self.alphalin / 2 * step signal_x, signal_y = self.linear(signal_x, signal_y, Disper) energy = signal_x * af.conjg(signal_x) + signal_y * af.conjg( signal_y) signal_x, signal_y = self.nonlinear(energy, signal_x, signal_y, dz_Eff) signal_x, signal_y = self.linear(signal_x, signal_y, Disper) signal_x_array = np.array(signal_x.to_list()) signal_y_array = np.array(signal_y.to_list()) signal_x_array = signal_x_array[:, 0] + 1j * signal_x_array[:, 1] signal_y_array = signal_y_array[:, 0] + 1j * signal_y_array[:, 1] signal.data_sample[0, :] = signal_x_array signal.data_sample[1, :] = signal_y_array
def simple_array(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.Array([1, 2, 3]) display_func(a) display_func(a.T) display_func(a.H) print_func(a.shape) b = a.as_type(af.Dtype.s32) display_func(b) print_func(a.elements(), a.type(), a.dims(), a.numdims()) print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) a = af.Array(host.array("i", [4, 5, 6])) display_func(a) print_func(a.elements(), a.type(), a.dims(), a.numdims()) print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) a = af.Array(host.array("I", [7, 8, 9] * 3), (3, 3)) display_func(a) print_func(a.elements(), a.type(), a.dims(), a.numdims()) print_func(a.is_empty(), a.is_scalar(), a.is_column(), a.is_row()) print_func(a.is_complex(), a.is_real(), a.is_double(), a.is_single()) print_func(a.is_real_floating(), a.is_floating(), a.is_integer(), a.is_bool()) c = a.to_ctype() for n in range(a.elements()): print_func(c[n]) c, s = a.to_ctype(True, True) for n in range(a.elements()): print_func(c[n]) print_func(s) arr = a.to_array() lst = a.to_list(True) print_func(arr) print_func(lst) print_func(a.is_sparse())
def main2(): data = af.Array([0, 1, 2, 2.5, 8, 9, 20, 21, 22]) labels = af.Array([0, 0, 0, 0, 1, 1, 2, 2, 2]).as_type(af.Dtype.u32) num_labels = 2 # query = af.Array([1.5]) query = af.Array([1.5, 12]) clf = AfKNearestNeighbors(num_nearest=5, weight_by_dist=False) # clf = AfKNearestNeighbors(num_nearest=data.dims()[0], weight_by_dist=True) clf.train(data, labels) outputs = clf.predict(query) outputs_proba = clf.predict_proba(query) print('outputs:', outputs) print('outputs_proba:', outputs_proba) print('ndarray version:\n', outputs_proba.to_ndarray())
def __reshape__(self, newshape, order='C'): if (order is not 'C'): raise NotImplementedError if isinstance(newshape, numbers.Number): newshape = (newshape, ) # Replace a possible -1 with the if -1 in newshape: newshape = list(newshape) i = newshape.index(-1) newshape[i] = 1 if -1 in newshape: raise ValueError('Only one -1 allowed in shape') newshape[i] = self.size / numpy.prod(newshape) if self.size != numpy.prod(newshape): raise ValueError('total size of new array must be unchanged') if len(newshape) != 0: # No need to modify the af_array for empty shapes # af_shape = numpy.array(pu.c2f(newshape), dtype=pu.dim_t) # s = arrayfire.Array() # arrayfire.backend.get().af_moddims(ctypes.pointer(s.arr), self.d_array.arr, af_shape.size, ctypes.c_void_p(af_shape.ctypes.data)) # self.d_array = s if tuple(newshape) == self.shape: # No need to do anything return af_shape = numpy.array(pu.c2f(newshape), dtype=pu.dim_t) s = arrayfire.Array() arrayfire.backend.get().af_moddims( ctypes.pointer(s.arr), self.d_array.arr, af_shape.size, ctypes.c_void_p(af_shape.ctypes.data)) self.d_array = s self._shape = tuple(newshape)
def forward(nn_np, data): """ Given a dictionary representing a feed forward neural net and an input data matrix compute the network's output and store it within the dictionary :param nn: neural network dictionary :param data: a numpy n by m matrix where m in the number of input units in nn :return: the output layer activations """ nn = nn_np nn['activations'] = [] afData = data.ctypes.data, data.shape, data.dtype.char for i in range(data.shape[0]): nn['activations'].append( af.Array(data[i].ctypes.data, data[i].shape, data[i].dtype.char)) nn['zs'] = [] for w, s, b in map(None, nn['weights'], nn['nonlin'], nn['biases']): #print af.transpose(nn['activations'][-1]) print nn['activations'][-1] print "" print w z = af.dot(nn['activations'][-1], w) nn['zs'].append(af.transpose(z)) nn['activations'].append(s[0](af.transpose(z))) return nn['activations'][-1]
def f_left(f, t, q1, q2, p1, p2, p3, params): k = params.boltzmann_constant E_upper = params.E_band T = params.initial_temperature mu = params.initial_mu t = params.current_time omega = 2. * np.pi * params.AC_freq q1_start = af.Array([domain.q1_start]) q2_start = af.Array([domain.q2_start]) q2_end = af.Array([domain.q2_end]) #x_start, y_start = coords.get_cartesian_coords(q1_start, q2_start) #x_start, y_end = coords.get_cartesian_coords(q1_start, q2_end) y_start = -24. y_end = -4.6 contact_width = y_end - y_start #19.486 if (params.source_type == 'AC'): vel_drift_x_out = -params.vel_drift_y_in / contact_width * np.sin( omega * t) elif (params.source_type == 'DC'): vel_drift_x_out = -params.vel_drift_y_in / contact_width else: raise NotImplementedError('Unsupported source_type') if (params.p_space_grid == 'cartesian'): p_x = p1 p_y = p2 elif (params.p_space_grid == 'polar2D'): p_x = p1 * af.cos(p2) p_y = p1 * af.sin(p2) else: raise NotImplementedError('Unsupported coordinate system in p_space') fermi_dirac_out = (1. / (af.exp( (E_upper - vel_drift_x_out * p_x - mu) / (k * T)) + 1.)) if params.zero_temperature: fermi_dirac_out = fermi_dirac_out - 0.5 f_left = fermi_dirac_out af.eval(f_left) return (f_left)
def testFromArrayfire(self): a = af.Array([1, 2, 3, 4]) b = Array.from_arrayfire(a) self.assertNotEqual(a.arr, 0) self.assertNotEqual(b.arr_reference, 0) self.assertNotEqual(a.arr, b.arr_reference) np.testing.assert_array_equal(np.asarray(a.to_list()), np.asarray(b.to_list()))
def simple_device(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) print_func(af.device_info()) print_func(af.get_device_count()) print_func(af.is_dbl_supported()) af.sync() curr_dev = af.get_device() print_func(curr_dev) for k in range(af.get_device_count()): af.set_device(k) dev = af.get_device() assert(k == dev) print_func(af.is_dbl_supported(k)) af.device_gc() mem_info_old = af.device_mem_info() a = af.randu(100, 100) af.sync(dev) mem_info = af.device_mem_info() assert(mem_info['alloc']['buffers'] == 1 + mem_info_old['alloc']['buffers']) assert(mem_info[ 'lock']['buffers'] == 1 + mem_info_old[ 'lock']['buffers']) af.set_device(curr_dev) a = af.randu(10,10) display_func(a) dev_ptr = af.get_device_ptr(a) print_func(dev_ptr) b = af.Array(src=dev_ptr, dims=a.dims(), dtype=a.dtype(), is_device=True) display_func(b) c = af.randu(10,10) af.lock_array(c) af.unlock_array(c) a = af.constant(1, 3, 3) b = af.constant(2, 3, 3) af.eval(a) af.eval(b) print_func(a) print_func(b) c = a + b d = a - b af.eval(c, d) print_func(c) print_func(d) print_func(af.set_manual_eval_flag(True)) assert(af.get_manual_eval_flag() == True) print_func(af.set_manual_eval_flag(False)) assert(af.get_manual_eval_flag() == False) display_func(af.is_locked_array(a))
def setup_mnist(frac, expand_labels): root_path = os.path.dirname(os.path.abspath(__file__)) file_path = root_path + '/../../assets/examples/data/mnist/' idims, idata = read_idx(file_path + 'images-subset') ldims, ldata = read_idx(file_path + 'labels-subset') idims.reverse() numdims = len(idims) images = af.Array(idata, tuple(idims)) R = af.randu(10000, 1) cond = R < min(frac, 0.8) train_indices = af.where(cond) test_indices = af.where(~cond) train_images = af.lookup(images, train_indices, 2) / 255 test_images = af.lookup(images, test_indices, 2) / 255 num_classes = 10 num_train = train_images.dims()[2] num_test = test_images.dims()[2] if expand_labels: train_labels = af.constant(0, num_classes, num_train) test_labels = af.constant(0, num_classes, num_test) h_train_idx = train_indices.to_list() h_test_idx = test_indices.to_list() for i in range(num_train): train_labels[ldata[h_train_idx[i]], i] = 1 for i in range(num_test): test_labels[ldata[h_test_idx[i]], i] = 1 else: labels = af.Array(ldata, tuple(ldims)) train_labels = labels[train_indices] test_labels = labels[test_indices] return (num_classes, num_train, num_test, train_images, test_images, train_labels, test_labels)
def test_dx_dxi_analytical(): ''' Test to check the dx_dxi_analytical in wave equation module for an element and compare it with an analytical value. ''' threshold = 1e-14 nodes = af.Array([2, 6]) check_analytical_dx_dxi = af.sum( af.abs(wave_equation.dx_dxi_analytical(nodes, 0) - 2)) <= threshold assert check_analytical_dx_dxi
def af_gaussianDerivative1D(s,order): radius = int(round(3*s + 0.5*order)) size = radius*2+1 csize = ct.c_int(size) csigma = ct.c_double(s) d_k = af.Array() af.safe_call(af.backend.get().af_gaussian_kernel(ct.pointer(d_k.arr),csize,1,csigma,0)) if order == 1: afx=af.range(size)-radius return -afx/s/s*d_k if order == 2: afx=((af.range(size)-radius)**2-s*s) return afx/(s**4)*d_k return d_k
def test_gauss_weights(): ''' Test to check the gaussian weights calculated. ''' threshold = 2e-8 analytical_gauss_weights = af.Array([0.23692688505618908, 0.47862867049936647,\ 0.5688888888888889, 0.47862867049936647, \ 0.23692688505618908 ] ) calculated_gauss_weights = lagrange.gaussian_weights(5) assert af.max(af.abs(analytical_gauss_weights - calculated_gauss_weights))\ <= threshold
def test(): x1 =np.arange(2000) x = af.Array(x1.ctypes.data, x1.shape, x1.dtype.char) alpha = 2/3 s_old = x[0] a = time.time() for i in range(1, 2000): s = alpha * x[i] + (1- alpha) * s_old s_old = s #time.sleep(1) b = time.time() print(b - a) return s
def visitbatches(nn, batches, labelBatches, errlist, it=1000): afBatches = [] for group in batches: gp = [] for item in group: tmp = [] tmp.append(item[0]) tmp.append(item[1]) gp.append(af.Array(tmp)) afBatches.append(gp) for c in range(it): #print "len of things " + str(len(nn)) + " " + str(len(batches[0])) nnDif.master_node(nn, afBatches, labelBatches)
def forward(self, end): if len(self.layers) is 0: return self.data out = af.Array() data = self.data for key in self.layers.viewkeys(): if key in self.params: data = self.layers[key](image = data, weights = self.params[key]['weights'], biases = self.params[key]['biases']) else: data = self.layers[key](image = data) if key is end: return data return data
def imag(self): ret_type = numpy.real(numpy.zeros((), dtype=self.dtype)).dtype shape = list(self.shape) if not numpy.issubdtype(self.dtype, numpy.complexfloating): return afnumpy.zeros(self.shape) shape[-1] *= 2 dims = numpy.array(pu.c2f(shape), dtype=pu.dim_t) s = arrayfire.Array() arrayfire.backend.get().af_device_array( ctypes.pointer(s.arr), ctypes.c_void_p(self.d_array.device_ptr()), self.ndim, ctypes.c_void_p(dims.ctypes.data), pu.typemap(ret_type).value) arrayfire.backend.get().af_retain_array(ctypes.pointer(s.arr), s.arr) a = ndarray(shape, dtype=ret_type, af_array=s) ret = a[..., 1::2] ret._base = a ret._base_index = (Ellipsis, slice(1, None, 2)) return ret
def to_arrayfire(self): """ Creates an Arrayfire array from this KHIVA array. This need to be used carefully as the same array reference is oging to be used by both of them. Once the Arrayfire array is created, the destructor of the KHIVA array is not going to free the allocated array. :return: an Arrayfire Array """ try: import arrayfire as af except ModuleNotFoundError: logging.error( "In order to use `to_arrayfire()` function, you need to install the Arrayfire Python library" ) sys.exit(1) result = af.Array() result.arr = self.arr_reference self.arrayfire_reference = True return result
def af_gauss_sigmas_sep(data,sigmas): d_kernels = [] for s in sigmas: csize = ct.c_int(int(3*s + 0.5)*2+1) csigma = ct.c_double(s) d_k = af.Array() af.safe_call(af.backend.get().af_gaussian_kernel(ct.pointer(d_k.arr),csize,1,csigma,ct.c_double(0.0))) d_kernels.append(d_k) out = [] for d in data: d_img = af.np_to_af_array(d) for d_k,i in zip(d_kernels,range(len(d_kernels))): res = af.convolve2_separable(d_k, af.transpose(d_k), d_img) # create numpy array out.append(res.__array__()) return out
def simple_device(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) print_func(af.device_info()) print_func(af.get_device_count()) print_func(af.is_dbl_supported()) af.sync() curr_dev = af.get_device() print_func(curr_dev) for k in range(af.get_device_count()): af.set_device(k) dev = af.get_device() assert (k == dev) print_func(af.is_dbl_supported(k)) af.device_gc() mem_info_old = af.device_mem_info() a = af.randu(100, 100) af.sync(dev) mem_info = af.device_mem_info() assert (mem_info['alloc']['buffers'] == 1 + mem_info_old['alloc']['buffers']) assert (mem_info['lock']['buffers'] == 1 + mem_info_old['lock']['buffers']) af.set_device(curr_dev) a = af.randu(10, 10) display_func(a) dev_ptr = af.get_device_ptr(a) print_func(dev_ptr) b = af.Array(src=dev_ptr, dims=a.dims(), dtype=a.dtype(), is_device=True) display_func(b) af.lock_device_ptr(b) af.unlock_device_ptr(b)
def array(object: tp.Union[tp.Sequence, np.ndarray, pyarray.array], dtype: tp.Optional[np.generic] = None, copy: bool = True, order: tp.Optional = None, subok: bool = False, ndmin: int = 0) \ -> ndarray: """ Create an array. """ if order: raise ValueError('order != None is not supported') # if dtype is not None: # raise ValueError("parameter dtype != None is not supported") af_array = None shape = None if isinstance(object, np.ndarray): if object.ndim == 0: # object = [np.asscalar(object)] object = np.reshape(object, (1, )) af_array = af.to_array(object) shape = object.shape elif isinstance(object, list): np_array = np.array(object, dtype=dtype) af_array = af.to_array(np_array) shape = np_array.shape else: af_array = af.Array(object) result = ndarray(af_array, shape) if dtype is not None and result.dtype != dtype: result = result.astype(dtype) return result
def master_node(nn, data, labels): nabla_w = [] nabla_b = [] l = 1 for i in range(len(data[0])): for n in range(len(nn)): r = forward(nn[n], data[n][i]) tmp = [] tmp.append(labels[n][i][0]) delta = d_cost(r, af.Array(tmp)) w, b = gradient(nn[n], delta) nabla_w += w nabla_b += b nabla_w = [x / len(nn) for x in nabla_w] nabla_b = [x / len(nn) for x in nabla_b] for net in nn: backprop(net, nabla_w, nabla_b)
def simple_index(verbose=False): display_func = _util.display_func(verbose) a = af.randu(5, 5) display_func(a) b = af.Array(a) display_func(b) c = a.copy() display_func(c) display_func(a[0, 0]) display_func(a[0]) display_func(a[:]) display_func(a[:, :]) display_func(a[0:3, ]) display_func(a[-2:-1, -1]) display_func(a[0:5]) display_func(a[0:5:2]) idx = af.Array(host.array("i", [0, 3, 2])) display_func(idx) aa = a[idx] display_func(aa) a[0] = 1 display_func(a) a[0] = af.randu(1, 5) display_func(a) a[:] = af.randu(5, 5) display_func(a) a[:, -1] = af.randu(5, 1) display_func(a) a[0:5:2] = af.randu(3, 5) display_func(a) a[idx, idx] = af.randu(3, 3) display_func(a) a = af.randu(5, 1) b = af.randu(5, 1) display_func(a) display_func(b) for ii in ParallelRange(1, 3): a[ii] = b[ii] display_func(a) for ii in ParallelRange(2, 5): b[ii] = 2 display_func(b) a = af.randu(3, 2) rows = af.constant(0, 1, dtype=af.Dtype.s32) b = a[:, rows] display_func(b) for r in range(rows.elements()): display_func(r) display_func(b[:, r]) a = af.randu(3) c = af.randu(3) b = af.constant(1, 3, dtype=af.Dtype.b8) display_func(a) a[b] = c display_func(a)
solver_method_in_p = 'FVM' reconstruction_method_in_q = 'weno5' reconstruction_method_in_p = 'weno5' riemann_solver_in_q = 'upwind-flux' riemann_solver_in_p = 'upwind-flux' # Dimensionality considered in velocity space: p_dim = 1 # Number of devices(GPUs/Accelerators) on each node: num_devices = 1 # Constants: mass = af.Array([1, 100], (1, 2)) boltzmann_constant = 1 charge = af.Array([-1, 1], (1, 2)) # Initial Conditions used in initialize: rho_background_e = 1 rho_background_i = 1 temperature_background_e = 2.5 temperature_background_i = 1 # Parameter controlling amplitude of perturbation introduced: alpha = 0.01 # Time parameters: N_cfl = 0.1
def mode1_fdtd(Ez, Bx, By, length_domain_x, length_domain_y, ghost_cells, Jx, Jy, Jz, dt): forward_row = af.Array([1, -1, 0]) forward_column = af.Array([1, -1, 0]) backward_row = af.Array([0, 1, -1]) backward_column = af.Array([0, 1, -1]) identity = af.Array([0, 1, 0]) """ Number of grid points in the field's domain""" (x_number_of_points, y_number_of_points) = Ez.dims() """ number of grid zones from the input fields """ Nx = x_number_of_points - 2 * ghost_cells - 1 Ny = y_number_of_points - 2 * ghost_cells - 1 """ local variables for storing the input fields """ Ez_local = Ez.copy() Bx_local = Bx.copy() By_local = By.copy() """Enforcing BC's""" Ez_local = periodic_ghost(Ez_local, ghost_cells) Bx_local = periodic_ghost(Bx_local, ghost_cells) By_local = periodic_ghost(By_local, ghost_cells) """ Setting division size and time steps""" dx = np.float(length_domain_x / (Nx)) dy = np.float(length_domain_y / (Ny)) """ defining variables for convenience """ dt_by_dx = dt / (dx) dt_by_dy = dt / (dy) """ Updating the Electric field using the current too """ Ez_local += dt_by_dx * (af.signal.convolve2_separable(identity, backward_column, By_local)) \ - dt_by_dy * (af.signal.convolve2_separable(backward_row, identity, Bx_local)) \ - dt*(Jz) # dEz/dt = dBy/dx - dBx/dy """ Implementing periodic boundary conditions using ghost cells """ Ez_local = periodic_ghost(Ez_local, ghost_cells) """ Updating the Magnetic fields """ Bx_local += -dt_by_dy * (af.signal.convolve2_separable( forward_row, identity, Ez_local)) # dBx/dt = -dEz/dy By_local += dt_by_dx * (af.signal.convolve2_separable( identity, forward_column, Ez_local)) # dBy/dt = +dEz/dx """ Implementing periodic boundary conditions using ghost cells """ Bx_local = periodic_ghost(Bx_local, ghost_cells) By_local = periodic_ghost(By_local, ghost_cells) af.eval(Ez_local, Bx_local, By_local) return Ez_local, Bx_local, By_local
def mode2_fdtd( Bz, Ex, Ey, length_domain_x, length_domain_y, ghost_cells, Jx, Jy, Jz, dt, ): forward_row = af.Array([1, -1, 0]) forward_column = af.Array([1, -1, 0]) backward_row = af.Array([0, 1, -1]) backward_column = af.Array([0, 1, -1]) identity = af.Array([0, 1, 0]) """ Number of grid points in the field's domain """ (x_number_of_points, y_number_of_points) = Bz.dims() """ number of grid zones calculated from the input fields """ Nx = x_number_of_points - 2 * ghost_cells - 1 Ny = y_number_of_points - 2 * ghost_cells - 1 """ local variables for storing the input fields """ Bz_local = Bz.copy() Ex_local = Ex.copy() Ey_local = Ey.copy() """Enforcing periodic BC's""" Bz_local = periodic_ghost(Bz_local, ghost_cells) Ex_local = periodic_ghost(Ex_local, ghost_cells) Ey_local = periodic_ghost(Ey_local, ghost_cells) """ Setting division size and time steps""" dx = np.float(length_domain_x / (Nx)) dy = np.float(length_domain_y / (Ny)) """ defining variable for convenience """ dt_by_dx = dt / (dx) dt_by_dy = dt / (dy) """ Updating the Electric fields using the current too """ Ex_local += dt_by_dy * (af.signal.convolve2_separable( backward_row, identity, Bz_local)) - (Jx) * dt # dEx/dt = + dBz/dy Ey_local += -dt_by_dx * (af.signal.convolve2_separable( identity, backward_column, Bz_local)) - (Jy) * dt # dEy/dt = - dBz/dx """ Implementing periodic boundary conditions using ghost cells """ Ex_local = periodic_ghost(Ex_local, ghost_cells) Ey_local = periodic_ghost(Ey_local, ghost_cells) """ Updating the Magnetic field """ Bz_local += - dt_by_dx * (af.signal.convolve2_separable(identity, forward_column, Ey_local)) \ + dt_by_dy * (af.signal.convolve2_separable(forward_row, identity, Ex_local)) # dBz/dt = - ( dEy/dx - dEx/dy ) #Implementing periodic boundary conditions using ghost cells Bz_local = periodic_ghost(Bz_local, ghost_cells) af.eval(Bz_local, Ex_local, Ey_local) return Bz_local, Ex_local, Ey_local
def testArrayfire(self): a = af.Array([1, 2, 3, 4]) a_data = a.to_list() b = Array.from_arrayfire(a) np.testing.assert_array_equal(np.asarray(a_data), np.asarray(b.to_list()))
def af_gaussian2D(s): csize = ct.c_int(int(3*s)*2+1) csigma = ct.c_double(s) d_k = af.Array() af.safe_call(af.backend.get().af_gaussian_kernel(ct.pointer(d_k.arr),csize,csize,csigma,csigma)) return d_k
af.set_backend('cuda') v = af.random.randu(size_of_vector) time_sort_device = af.timer.timeit(af.algorithm.sort, v) print('Device time: {}'.format(time_sort_device)) print('Speedup: {0:.2f}x'.format(time_sort_host / time_sort_device)) # Test de convolution n = 1000 print('') print('=== 2d convolution on a {}x{} matrix ==='.format(n, n)) af.set_backend('cpu') m = af.random.randu(n, n) numpy_kernel = np.array([[0.1, 0.2, 0.1], [0.2, 0.3, 0.2], [0.1, 0.2, 0.1]]) af_kernel = af.Array(numpy_kernel.ctypes.data, numpy_kernel.shape, numpy_kernel.dtype.char) time_convolve_host = af.timer.timeit(af.convolve, m, af_kernel) print('Host time: {}'.format(time_convolve_host)) af.set_backend('cuda') af_kernel = af.Array(numpy_kernel.ctypes.data, numpy_kernel.shape, numpy_kernel.dtype.char) m = af.random.randu(n, n) time_convolve_device = af.timer.timeit(af.convolve, m, af_kernel) print('Device time: {}'.format(time_convolve_device)) print('Speedup: {0:.2f}x'.format(time_convolve_host / time_convolve_device)) # Interop numpy print('')