def simple_statistics(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) display_func(af.mean(a, dim=0)) display_func(af.mean(a, weights=w, dim=0)) print_func(af.mean(a)) print_func(af.mean(a, weights=w)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) display_func(af.var(a, weights=w, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) print_func(af.var(a, weights=w)) display_func(af.stdev(a, dim=0)) print_func(af.stdev(a)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) display_func(af.median(a, dim=0)) print_func(af.median(w)) print_func(af.corrcoef(a, b))
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass_particle k = params.boltzmann_constant rho = af.select(af.abs(q2)>0.25, q1**0, 2) p1_bulk = af.select(af.abs(q2)>0.25, 0.5 + 0.01 * (af.randu(1, q1.shape[1], q2.shape[2], dtype = af.Dtype.f64 ) - 0.5 ), -0.5 - 0.01 * (af.randu(1, q1.shape[1], q2.shape[2], dtype = af.Dtype.f64 ) - 0.5 ) ) p2_bulk = 0.01 * (af.randu(1, q1.shape[1], q2.shape[2], dtype = af.Dtype.f64 ) - 0.5 ) T = (0.8 / rho) f = rho * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * p3**2 / (2 * k * T)) af.eval(f) return (f)
def simple_data(verbose=False): display_func = _util.display_func(verbose) display_func(af.constant(100, 3, 3, dtype=af.Dtype.f32)) display_func(af.constant(25, 3, 3, dtype=af.Dtype.c32)) display_func(af.constant(2**50, 3, 3, dtype=af.Dtype.s64)) display_func(af.constant(2+3j, 3, 3)) display_func(af.constant(3+5j, 3, 3, dtype=af.Dtype.c32)) display_func(af.range(3, 3)) display_func(af.iota(3, 3, tile_dims=(2, 2))) display_func(af.identity(3, 3, 1, 2, af.Dtype.b8)) display_func(af.identity(3, 3, dtype=af.Dtype.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) display_func(a) display_func(b) display_func(c) display_func(af.diag(b, extract=False)) display_func(af.diag(c, 1, extract=False)) display_func(af.join(0, a, a)) display_func(af.join(1, a, a, a)) display_func(af.tile(a, 2, 2)) display_func(af.reorder(a, 1, 0)) display_func(af.shift(a, -1, 1)) display_func(af.moddims(a, 6, 2)) display_func(af.flat(a)) display_func(af.flip(a, 0)) display_func(af.flip(a, 1)) display_func(af.lower(a, False)) display_func(af.lower(a, True)) display_func(af.upper(a, False)) display_func(af.upper(a, True)) a = af.randu(5, 5) display_func(af.transpose(a)) af.transpose_inplace(a) display_func(a) display_func(af.select(a > 0.3, a, -0.3)) af.replace(a, a > 0.3, -0.3) display_func(a) display_func(af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0)))
def simple_data(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) display_func(af.constant(100, 3,3, dtype=af.Dtype.f32)) display_func(af.constant(25, 3,3, dtype=af.Dtype.c32)) display_func(af.constant(2**50, 3,3, dtype=af.Dtype.s64)) display_func(af.constant(2+3j, 3,3)) display_func(af.constant(3+5j, 3,3, dtype=af.Dtype.c32)) display_func(af.range(3, 3)) display_func(af.iota(3, 3, tile_dims=(2,2))) display_func(af.identity(3, 3, 1, 2, af.Dtype.b8)) display_func(af.identity(3, 3, dtype=af.Dtype.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) display_func(a) display_func(b) display_func(c) display_func(af.diag(b, extract = False)) display_func(af.diag(c, 1, extract = False)) display_func(af.join(0, a, a)) display_func(af.join(1, a, a, a)) display_func(af.tile(a, 2, 2)) display_func(af.reorder(a, 1, 0)) display_func(af.shift(a, -1, 1)) display_func(af.moddims(a, 6, 2)) display_func(af.flat(a)) display_func(af.flip(a, 0)) display_func(af.flip(a, 1)) display_func(af.lower(a, False)) display_func(af.lower(a, True)) display_func(af.upper(a, False)) display_func(af.upper(a, True)) a = af.randu(5,5) display_func(af.transpose(a)) af.transpose_inplace(a) display_func(a) display_func(af.select(a > 0.3, a, -0.3)) af.replace(a, a > 0.3, -0.3) display_func(a)
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 calc_pi_device(samples): # Simple, array based API # Generate uniformly distributed random numers x = af.randu(samples) y = af.randu(samples) # Supports Just In Time Compilation # The following line generates a single kernel within_unit_circle = (x * x + y * y) < 1 # Intuitive function names return 4 * af.count(within_unit_circle) / samples
def setup_input(n, sparsity=7): T = af.randu(n, n, dtype=af.Dtype.f32) A = af.floor(T * 1000) A = A * ((A % sparsity) == 0) / 1000 A = A.T + A + n * af.identity(n, n, dtype=af.Dtype.f32) x0 = af.randu(n, dtype=af.Dtype.f32) b = af.matmul(A, x0) # printing # nnz = af.sum((A != 0)) # print "Sparsity of A: %2.2f %%" %(100*nnz/n**2,) return A, b, x0
def simple_blas(verbose=False): display_func = _util.display_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) display_func(af.matmul(a, b)) display_func(af.matmul(a, b, af.MATPROP.TRANS)) display_func(af.matmul(a, b, af.MATPROP.NONE, af.MATPROP.TRANS)) b = af.randu(5, 1) display_func(af.dot(b, b))
def setup_input(n, sparsity=7): T = af.randu(n, n, dtype=af.Dtype.f32) A = af.floor(T*1000) A = A * ((A % sparsity) == 0) / 1000 A = A.T + A + n*af.identity(n, n, dtype=af.Dtype.f32) x0 = af.randu(n, dtype=af.Dtype.f32) b = af.matmul(A, x0) # printing # nnz = af.sum((A != 0)) # print "Sparsity of A: %2.2f %%" %(100*nnz/n**2,) return A, b, x0
def weight_matrix(seed, innum, outnum, type='glorot', layer=0): """ Returns randomly initialized weight matrix of appropriate dimensions :param innum: number of neurons in the layer i :param outnum: number of neurons in layer i+1 :return: weight matrix """ np.random.seed(seed) if type == 'glorot': W = af.randu(outnum,innum) #np.random.uniform(low=-np.sqrt(6.0/(2*layer+1)), high=np.sqrt(6.0/(2*layer+1)), size=(outnum, innum)) if type == 'normal': W = af.randu(outnum,innum)# np.random.rand(outnum, innum) return W
def simple_blas(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5,5) b = af.randu(5,5) display_func(af.matmul(a,b)) display_func(af.matmul(a,b,af.MATPROP.TRANS)) display_func(af.matmul(a,b,af.MATPROP.NONE, af.MATPROP.TRANS)) b = af.randu(5,1) display_func(af.dot(b,b))
def simple_image(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5,5,3) dx,dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3,2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx,dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY))
def simple_image(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5, 5, 3) dx, dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3, 2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx, dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY))
def gamma_rand_marsaglia_and_tsang_arrayfire(alpha: float, lambda_: float, n: int) \ -> af.array: random_numbers = af.constant(0, n, dtype=Dtype.f32) # Gamma(alpha, lambda) generator using Marsaglia and Tsang method # Algorithm 4.33 if alpha >= 1.0: d = alpha - 1 / 3 c = 1.0 / np.sqrt(9.0 * d) number_generated = 0 number_generated_total = 0 while number_generated < n: number_left = n - number_generated z = af.randn(number_left, dtype=Dtype.f32) y = (1.0 + c * z) v = y * y * y accept_index_1 = ((z >= -1.0 / c) & (v > 0.0)) z_accept_1 = z[accept_index_1] # del z v_accept_1 = v[accept_index_1] # del v u_accept_1 = af.randu(v_accept_1.elements(), dtype=Dtype.f32) # del U accept_index_2 = \ u_accept_1 < af.exp((0.5 * z_accept_1 * z_accept_1 + d - d * v_accept_1 + d * af.log(v_accept_1))) x_accept = d * v_accept_1[accept_index_2] / lambda_ number_accept = x_accept.elements() random_numbers[number_generated:np.minimum(n, number_generated + number_accept)] = \ x_accept[0:np.minimum(number_left, number_accept)] number_generated += number_accept number_generated_total += number_left if GPUOptions.verbose: print(f"Acceptance ratio = {n/number_generated_total}") else: random_numbers = gamma_rand_marsaglia_and_tsang_arrayfire( alpha + 1, lambda_, n) random_numbers *= af.randu(n, dtype=Dtype.f32)**(1.0 / alpha) return random_numbers
def simple_statistics(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) display_func(af.mean(a, dim=0)) display_func(af.mean(a, weights=w, dim=0)) print_func(af.mean(a)) print_func(af.mean(a, weights=w)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) display_func(af.var(a, weights=w, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) print_func(af.var(a, weights=w)) mean, var = af.meanvar(a, dim=0) display_func(mean) display_func(var) mean, var = af.meanvar(a, weights=w, bias=af.VARIANCE.SAMPLE, dim=0) display_func(mean) display_func(var) display_func(af.stdev(a, dim=0)) print_func(af.stdev(a)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) display_func(af.median(a, dim=0)) print_func(af.median(w)) print_func(af.corrcoef(a, b)) data = af.iota(5, 3) k = 3 dim = 0 order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX assert (dim == 0) # topk currently supports first dim only values, indices = af.topk(data, k, dim, order) display_func(values) display_func(indices)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant n = af.select(af.abs(q2)>0.25, q1**0, 2) # Random Numbers under to seed the instability: seeding_velocities = 0.01 * (af.randu(1, 1, q1.shape[2], q1.shape[3], dtype = af.Dtype.f64 ) - 0.5 ) v1_bulk = af.select(af.abs(q2)>0.25, -0.5 - seeding_velocities, +0.5 + seeding_velocities ) v2_bulk = seeding_velocities T = (2.5 / n) f = n * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (v1 - v1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (v2 - v2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * v3**2 / (2 * k * T)) af.eval(f) return (f)
def weight_matrix(seed, innum, outnum, type='glorot', layer=0): """ Returns randomly initialized weight matrix of appropriate dimensions :param innum: number of neurons in the layer i :param outnum: number of neurons in layer i+1 :return: weight matrix """ np.random.seed(seed) if type == 'glorot': W = af.randu( outnum, innum ) #np.random.uniform(low=-np.sqrt(6.0/(2*layer+1)), high=np.sqrt(6.0/(2*layer+1)), size=(outnum, innum)) if type == 'normal': W = af.randu(outnum, innum) # np.random.rand(outnum, innum) return W
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() dev = af.get_device() print_func(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(dev)
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() dev = af.get_device() print_func(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(dev)
def calc_arrayfire(n): A = af.randu(n, n) af.sync() def run(iters): for t in range(iters): B = af.matmul(A, A) af.sync() return run
def bandwidth_test(n_evals): a = af.randu(32, 32, 32**3, dtype=af.Dtype.f64) b = af.randu(32, 32, 32**3, dtype=af.Dtype.f64) c = a + b af.eval(c) af.sync() tic = af.time() for i in range(n_evals): c = a + b af.eval(c) af.sync() toc = af.time() bandwidth_available = memory_bandwidth(a.elements(), 2, 1, n_evals, toc - tic) return (bandwidth_available)
def initialize_B(q1, q2, params): # Seeding with random fluctuation: B1 = params.B1 * af.randu( q1.shape[0], q1.shape[1], q1.shape[2], q1.shape[3], dtype=af.Dtype.f64) B2 = 0 * q1**0 B3 = 0 * q1**0 af.eval(B1, B2, B3) return (B1, B2, B3)
def calc_arrayfire(n): A = af.randu(n, n) af.sync() def run(iters): for t in range(iters): B = af.fft2(A) af.sync() return run
def __init__(self): self.N_p1 = 2 self.N_p2 = 3 self.N_p3 = 4 self.N_q1 = 5 self.N_q2 = 6 self.N_ghost = 1 self.p1_start = self.p2_start = self.p3_start = 0 self.dp1 = 2/self.N_p1 self.dp2 = 2/self.N_p2 self.dp3 = 2/self.N_p3 self.p1, self.p2, self.p3 = self._calculate_p_center() self.physical_system = type('obj', (object,), {'moment_exponents': moment_exponents, 'moment_coeffs': moment_coeffs } ) self.f = af.randu(self.N_p1 * self.N_p2 * self.N_p3, self.N_q1 + 2 * self.N_ghost, self.N_q2 + 2 * self.N_ghost, dtype = af.Dtype.f64 ) self._comm = PETSc.COMM_WORLD self._da_dump_f = PETSc.DMDA().create([self.N_q1, self.N_q2], dof=( self.N_p1 * self.N_p2 * self.N_p3 ), stencil_width = self.N_ghost ) self._da_dump_moments = PETSc.DMDA().create([self.N_q1, self.N_q2], dof=len(self.physical_system.\ moment_exponents ), stencil_width = self.N_ghost ) self._glob_f = self._da_dump_f.createGlobalVec() self._glob_f_array = self._glob_f.getArray() self._glob_moments = self._da_dump_moments.createGlobalVec() self._glob_moments_array =self._glob_moments.getArray() PETSc.Object.setName(self._glob_f, 'distribution_function') PETSc.Object.setName(self._glob_moments, 'moments')
def simple_statistics(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) display_func(af.mean(a, dim=0)) display_func(af.mean(a, weights=w, dim=0)) print_func(af.mean(a)) print_func(af.mean(a, weights=w)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) display_func(af.var(a, weights=w, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) print_func(af.var(a, weights=w)) display_func(af.stdev(a, dim=0)) print_func(af.stdev(a)) display_func(af.var(a, dim=0)) display_func(af.var(a, isbiased=True, dim=0)) print_func(af.var(a)) print_func(af.var(a, isbiased=True)) display_func(af.median(a, dim=0)) print_func(af.median(w)) print_func(af.corrcoef(a, b)) data = af.iota(5, 3) k = 3 dim = 0 order = af.TOPK.DEFAULT # defaults to af.TOPK.MAX assert(dim == 0) # topk currently supports first dim only values,indices = af.topk(data, k, dim, order) display_func(values) display_func(indices)
def simple_sparse(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) dd = af.randu(5, 5) ds = dd * (dd > 0.5) sp = af.create_sparse_from_dense(ds) display_func(af.sparse_get_info(sp)) display_func(af.sparse_get_values(sp)) display_func(af.sparse_get_row_idx(sp)) display_func(af.sparse_get_col_idx(sp)) print_func(af.sparse_get_nnz(sp)) print_func(af.sparse_get_storage(sp))
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 test_convert_to_q_expanded(): obj = test() test_array = af.randu( (obj.N_q1 + 2 * obj.N_ghost) * (obj.N_q2 + 2 * obj.N_ghost), obj.N_p1, obj.N_p2, obj.N_p3) modified = convert_to_q_expanded(obj, test_array) expected = af.moddims(test_array, obj.N_p1 * obj.N_p2 * obj.N_p3, (obj.N_q1 + 2 * obj.N_ghost), (obj.N_q2 + 2 * obj.N_ghost)) assert (af.sum(modified - expected) == 0)
def checkGradient(self, delta = 1e-4): """ check if the numerical gradient is similar to the analytical gradient. Only works for 64 bit data type. """ #assert af_float_datatype == af.Dtype.f64, "This will only be accurate if 64 bit datatype is used!" shape = self.phase_obj_3d.shape point = (np.random.randint(shape[0]), np.random.randint(shape[1]), np.random.randint(shape[2])) illu_idx = np.random.randint(len(self.fx_illu_list)) fx_illu = self.fx_illu_list[illu_idx] fy_illu = self.fy_illu_list[illu_idx] x = np.ones(shape, dtype = np_complex_datatype) if self._crop_obj.pad: amplitude = af.randu(shape[0]//2, shape[1]//2, dtype = af_float_datatype) else: amplitude = af.randu(shape[0], shape[1], dtype = af_float_datatype) print("Computing the gradient at point: ", point) print("fx : %5.2f, fy : %5.2f " %(fx_illu, fy_illu)) def func(x0): fields = self._scattering_obj.forward(x0, fx_illu, fy_illu) field_scattered = self._defocus_obj.forward(field_scattered, self.prop_distances) field_measure = self._crop_obj.forward(field_scattered) residual = af.abs(field_measure) - amplitude function_value = af.sum(residual*af.conjg(residual)).real return function_value numerical_gradient = calculateNumericalGradient(func, x, point, delta = delta) fields = self._scattering_obj.forward(x, fx_illu, fy_illu) forward_scattered_field = fields["forward_scattered_field"] forward_scattered_field = self._defocus_obj.forward(forward_scattered_field, self.prop_distances) field_measure = self._crop_obj.forward(forward_scattered_field) fields["forward_scattered_field"] = field_measure analytical_gradient = self._computeGradient(fields, amplitude)[0][point] print("numerical gradient: %5.5e + %5.5e j" %(numerical_gradient.real, numerical_gradient.imag)) print("analytical gradient: %5.5e + %5.5e j" %(analytical_gradient.real, analytical_gradient.imag))
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 __init__(self): self.N_q1 = 32 self.N_q2 = 32 self.N_p1 = 4 self.N_p2 = 5 self.N_p3 = 6 self.p1_start = self.p2_start = self.p3_start = 0 self.dp1 = 2 / self.N_p1 self.dp2 = 2 / self.N_p2 self.dp3 = 2 / self.N_p3 self.p1, self.p2, self.p3 = self._calculate_p_center() # Creating an object with required attributes for the test: self.physical_system = type('obj', (object, ), { 'moment_exponents': moment_exponents, 'moment_coeffs': moment_coeffs }) self.single_mode_evolution = False self.f = af.randu(self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, dtype=af.Dtype.f64) self.Y = 2 * af.fft2(self.f) / (self.N_q1 * self.N_q2) self._da_dump_f = PETSc.DMDA().create( [self.N_q1, self.N_q2], dof=(self.N_p1 * self.N_p2 * self.N_p3), ) self._da_dump_moments = PETSc.DMDA().create([self.N_q1, self.N_q2], dof = len(self.physical_system.\ moment_exponents) ) self._glob_f = self._da_dump_f.createGlobalVec() self._glob_f_value = self._da_dump_f.getVecArray(self._glob_f) self._glob_moments = self._da_dump_moments.createGlobalVec() self._glob_moments_value = self._da_dump_moments.\ getVecArray(self._glob_moments) PETSc.Object.setName(self._glob_f, 'distribution_function') PETSc.Object.setName(self._glob_moments, 'moments')
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 simple_random(verbose=False): display_func = _util.display_func(verbose) display_func(af.randu(3, 3, 1, 2)) display_func(af.randu(3, 3, 1, 2, af.Dtype.b8)) display_func(af.randu(3, 3, dtype=af.Dtype.c32)) display_func(af.randn(3, 3, 1, 2)) display_func(af.randn(3, 3, dtype=af.Dtype.c32)) af.set_seed(1024) assert (af.get_seed() == 1024) engine = af.Random_Engine(af.RANDOM_ENGINE.MERSENNE_GP11213, 100) display_func(af.randu(3, 3, 1, 2, engine=engine)) display_func(af.randu(3, 3, 1, 2, af.Dtype.s32, engine=engine)) display_func(af.randu(3, 3, dtype=af.Dtype.c32, engine=engine)) display_func(af.randn(3, 3, engine=engine)) engine.set_seed(100) assert (engine.get_seed() == 100)
def simple_random(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) display_func(af.randu(3, 3, 1, 2)) display_func(af.randu(3, 3, 1, 2, af.Dtype.b8)) display_func(af.randu(3, 3, dtype=af.Dtype.c32)) display_func(af.randn(3, 3, 1, 2)) display_func(af.randn(3, 3, dtype=af.Dtype.c32)) af.set_seed(1024) assert(af.get_seed() == 1024) engine = af.Random_Engine(af.RANDOM_ENGINE.MERSENNE_GP11213, 100) display_func(af.randu(3, 3, 1, 2, engine=engine)) display_func(af.randu(3, 3, 1, 2, af.Dtype.s32, engine=engine)) display_func(af.randu(3, 3, dtype=af.Dtype.c32, engine=engine)) display_func(af.randn(3, 3, engine=engine)) engine.set_seed(100) assert(engine.get_seed() == 100)
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af a = af.randu(5, 5) l, u, p = af.lu(a) af.display(l) af.display(u) af.display(p) p = af.lu_inplace(a, "full") af.display(a) af.display(p) a = af.randu(5, 3) q, r, t = af.qr(a) af.display(q) af.display(r) af.display(t)
def simple_signal(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(10, 1) pos0 = af.randu(10) * 10 display_func(af.approx1(a, pos0)) a = af.randu(3, 3) pos0 = af.randu(3, 3) * 10 pos1 = af.randu(3, 3) * 10 display_func(af.approx2(a, pos0, pos1)) a = af.randu(8, 1) display_func(a) display_func(af.fft(a)) display_func(af.dft(a)) display_func(af.real(af.ifft(af.fft(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4) display_func(a) display_func(af.fft2(a)) display_func(af.dft(a)) display_func(af.real(af.ifft2(af.fft2(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4, 2) display_func(a) display_func(af.fft3(a)) display_func(af.dft(a)) display_func(af.real(af.ifft3(af.fft3(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(10, 1) b = af.randu(3, 1) display_func(af.convolve1(a, b)) display_func(af.fft_convolve1(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) display_func(af.convolve2(a, b)) display_func(af.fft_convolve2(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) display_func(af.convolve3(a, b)) display_func(af.fft_convolve3(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) b = af.randu(3, 1) x = af.randu(10, 1) a = af.randu(2, 1) display_func(af.fir(b, x)) display_func(af.iir(b, a, x))
####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af af.info() print(af.device_info()) print(af.get_device_count()) print(af.is_dbl_supported()) af.sync() print('starting the loop') for k in range(af.get_device_count()): af.set_device(k) dev = af.get_device() assert(k == dev) print(af.is_dbl_supported(k)) a = af.randu(100, 100) af.sync(dev) mem_info = af.device_mem_info() assert(mem_info['alloc']['buffers'] == 1) assert(mem_info[ 'lock']['buffers'] == 1)
# The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af af.display(af.constant(100, 3,3, dtype=af.f32)) af.display(af.constant(25, 3,3, dtype=af.c32)) af.display(af.constant(2**50, 3,3, dtype=af.s64)) af.display(af.constant(2+3j, 3,3)) af.display(af.constant(3+5j, 3,3, dtype=af.c32)) af.display(af.range(3, 3)) af.display(af.iota(3, 3, tile_dims=(2,2))) af.display(af.randu(3, 3, 1, 2)) af.display(af.randu(3, 3, 1, 2, af.b8)) af.display(af.randu(3, 3, dtype=af.c32)) af.display(af.randn(3, 3, 1, 2)) af.display(af.randn(3, 3, dtype=af.c32)) af.set_seed(1024) assert(af.get_seed() == 1024) af.display(af.identity(3, 3, 1, 2, af.b8)) af.display(af.identity(3, 3, dtype=af.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True)
import sys from time import time from arrayfire import (array, randu, matmul) import arrayfire as af def bench(A, iters=100): start = time() for t in range(iters): B = af.fft2(A) af.sync() return (time() - start) / iters if __name__ == "__main__": if (len(sys.argv) > 1): af.set_device(int(sys.argv[1])) af.info() print("Benchmark N x N 2D fft") for M in range(7, 13): N = 1 << M A = af.randu(N, N) af.sync() t = bench(A) gflops = (10.0 * N * N * M) / (t * 1E9) print("Time taken for %4d x %4d: %0.4f Gflops" % (N, N, gflops))
import sys from array import array def af_assert(left, right, eps=1E-6): if (af.max(af.abs(left -right)) > eps): raise ValueError("Arrays not within dictated precision") return if __name__ == "__main__": if (len(sys.argv) > 1): af.set_device(int(sys.argv[1])) af.info() h_dx = array('f', (1.0/12, -8.0/12, 0, 8.0/12, 1.0/12)) h_spread = array('f', (1.0/5, 1.0/5, 1.0/5, 1.0/5, 1.0/5)) img = af.randu(640, 480) dx = af.Array(h_dx, (5,1)) spread = af.Array(h_spread, (1, 5)) kernel = af.matmul(dx, spread) full_res = af.convolve2(img, kernel) sep_res = af.convolve2_separable(dx, spread, img) af_assert(full_res, sep_res) print("full 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2, img, kernel))) print("separable 2D convolution time: %.5f ms" % (1000 * af.timeit(af.convolve2_separable, dx, spread, img)))
def simple_image(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5,5,3) dx,dy = af.gradient(a) display_func(dx) display_func(dy) display_func(af.resize(a, scale=0.5)) display_func(af.resize(a, odim0=8, odim1=8)) t = af.randu(3,2) display_func(af.transform(a, t)) display_func(af.rotate(a, 3.14)) display_func(af.translate(a, 1, 1)) display_func(af.scale(a, 1.2, 1.2, 7, 7)) display_func(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) display_func(h) display_func(af.hist_equal(a, h)) display_func(af.dilate(a)) display_func(af.erode(a)) display_func(af.dilate3(a3)) display_func(af.erode3(a3)) display_func(af.bilateral(a, 1, 2)) display_func(af.mean_shift(a, 1, 2, 3)) display_func(af.medfilt(a)) display_func(af.minfilt(a)) display_func(af.maxfilt(a)) display_func(af.regions(af.round(a) > 3)) dx,dy = af.sobel_derivatives(a) display_func(dx) display_func(dy) display_func(af.sobel_filter(a)) display_func(af.gaussian_kernel(3, 3)) display_func(af.gaussian_kernel(3, 3, 1, 1)) ac = af.gray2rgb(a) display_func(ac) display_func(af.rgb2gray(ac)) ah = af.rgb2hsv(ac) display_func(ah) display_func(af.hsv2rgb(ah)) display_func(af.color_space(a, af.CSPACE.RGB, af.CSPACE.GRAY)) a = af.randu(6,6) b = af.unwrap(a, 2, 2, 2, 2) c = af.wrap(b, 6, 6, 2, 2, 2, 2) display_func(a) display_func(b) display_func(c) display_func(af.sat(a)) a = af.randu(10,10,3) display_func(af.rgb2ycbcr(a)) display_func(af.ycbcr2rgb(a)) a = af.randu(10, 10) b = af.canny(a, low_threshold = 0.2, high_threshold = 0.8) display_func(af.anisotropic_diffusion(a, 0.125, 1.0, 64, af.FLUX.QUADRATIC, af.DIFFUSION.GRAD))
def simple_algorithm(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3, 3) print_func(af.sum(a), af.product(a), af.min(a), af.max(a), af.count(a), af.any_true(a), af.all_true(a)) display_func(af.sum(a, 0)) display_func(af.sum(a, 1)) display_func(af.product(a, 0)) display_func(af.product(a, 1)) display_func(af.min(a, 0)) display_func(af.min(a, 1)) display_func(af.max(a, 0)) display_func(af.max(a, 1)) display_func(af.count(a, 0)) display_func(af.count(a, 1)) display_func(af.any_true(a, 0)) display_func(af.any_true(a, 1)) display_func(af.all_true(a, 0)) display_func(af.all_true(a, 1)) display_func(af.accum(a, 0)) display_func(af.accum(a, 1)) display_func(af.sort(a, is_ascending=True)) display_func(af.sort(a, is_ascending=False)) b = (a > 0.1) * a c = (a > 0.4) * a d = b / c print_func(af.sum(d)); print_func(af.sum(d, nan_val=0.0)); display_func(af.sum(d, dim=0, nan_val=0.0)); val,idx = af.sort_index(a, is_ascending=True) display_func(val) display_func(idx) val,idx = af.sort_index(a, is_ascending=False) display_func(val) display_func(idx) b = af.randu(3,3) keys,vals = af.sort_by_key(a, b, is_ascending=True) display_func(keys) display_func(vals) keys,vals = af.sort_by_key(a, b, is_ascending=False) display_func(keys) display_func(vals) c = af.randu(5,1) d = af.randu(5,1) cc = af.set_unique(c, is_sorted=False) dd = af.set_unique(af.sort(d), is_sorted=True) display_func(cc) display_func(dd) display_func(af.set_union(cc, dd, is_unique=True)) display_func(af.set_union(cc, dd, is_unique=False)) display_func(af.set_intersect(cc, cc, is_unique=True)) display_func(af.set_intersect(cc, cc, is_unique=False))
def simple_lapack(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5,5) l,u,p = af.lu(a) display_func(l) display_func(u) display_func(p) p = af.lu_inplace(a, "full") display_func(a) display_func(p) a = af.randu(5,3) q,r,t = af.qr(a) display_func(q) display_func(r) display_func(t) af.qr_inplace(a) display_func(a) a = af.randu(5, 5) a = af.matmulTN(a, a) + 10 * af.identity(5,5) R,info = af.cholesky(a) display_func(R) print_func(info) af.cholesky_inplace(a) display_func(a) a = af.randu(5,5) ai = af.inverse(a) display_func(a) display_func(ai) x0 = af.randu(5, 3) b = af.matmul(a, x0) x1 = af.solve(a, b) display_func(x0) display_func(x1) p = af.lu_inplace(a) x2 = af.solve_lu(a, p, b) display_func(x2) print_func(af.rank(a)) print_func(af.det(a)) print_func(af.norm(a, af.NORM.EUCLID)) print_func(af.norm(a, af.NORM.MATRIX_1)) print_func(af.norm(a, af.NORM.MATRIX_INF)) print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1))
######################################################## import sys from time import time from arrayfire import (array, randu, matmul) import arrayfire as af def bench(A, iters = 100): start = time() for t in range(iters): B = af.matmul(A, A) af.sync() return (time() - start) / iters if __name__ == "__main__": if (len(sys.argv) > 1): af.set_device(int(sys.argv[1])) af.info() print("Benchmark N x N matrix multiply") for n in range(128, 2048 + 128, 128): A = af.randu(n, n) af.sync() t = bench(A) gflops = 2.0 * (n**3) / (t * 1E9) print("Time taken for %4d x %4d: %0.4f Gflops" % (n, n, gflops))
def simple_arith(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3,3,dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a ** b d = a d **= b display_func(c ** 2.0) display_func(3.0 ** d) display_func(a ** 2) display_func(3 ** a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a,b)) display_func(af.minof(a,b)) display_func(af.rem(a,b)) a = af.randu(3,3) - 0.5 b = af.randu(3,3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a,b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a/b)) display_func(af.isnan(a/a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x,y: x+y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, 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)
def calc_pi_device(samples): x = af.randu(samples) y = af.randu(samples) return 4 * af.sum(in_circle(x, y)) / samples
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af a = af.randu(5, 5) b = af.randu(5, 5) w = af.randu(5, 1) af.display(af.mean(a, dim=0)) af.display(af.mean(a, weights=w, dim=0)) print(af.mean(a)) print(af.mean(a, weights=w)) af.display(af.var(a, dim=0)) af.display(af.var(a, isbiased=True, dim=0)) af.display(af.var(a, weights=w, dim=0)) print(af.var(a)) print(af.var(a, isbiased=True)) print(af.var(a, weights=w)) af.display(af.stdev(a, dim=0)) print(af.stdev(a))
def simple_lapack(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(5, 5) l, u, p = af.lu(a) display_func(l) display_func(u) display_func(p) p = af.lu_inplace(a, "full") display_func(a) display_func(p) a = af.randu(5, 3) q, r, t = af.qr(a) display_func(q) display_func(r) display_func(t) af.qr_inplace(a) display_func(a) a = af.randu(5, 5) a = af.matmulTN(a, a.copy()) + 10 * af.identity(5, 5) R, info = af.cholesky(a) display_func(R) print_func(info) af.cholesky_inplace(a) display_func(a) a = af.randu(5, 5) ai = af.inverse(a) display_func(a) display_func(ai) x0 = af.randu(5, 3) b = af.matmul(a, x0) x1 = af.solve(a, b) display_func(x0) display_func(x1) p = af.lu_inplace(a) x2 = af.solve_lu(a, p, b) display_func(x2) print_func(af.rank(a)) print_func(af.det(a)) print_func(af.norm(a, af.NORM.EUCLID)) print_func(af.norm(a, af.NORM.MATRIX_1)) print_func(af.norm(a, af.NORM.MATRIX_INF)) print_func(af.norm(a, af.NORM.MATRIX_L_PQ, 1, 1)) a = af.randu(10, 10) display_func(a) u, s, vt = af.svd(a) display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt)) u, s, vt = af.svd_inplace(a) display_func(af.matmul(af.matmul(u, af.diag(s, 0, False)), vt))
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af a = af.randu(10, 1) pos0 = af.randu(10) * 10 af.display(af.approx1(a, pos0)) a = af.randu(3, 3) pos0 = af.randu(3, 3) * 10 pos1 = af.randu(3, 3) * 10 af.display(af.approx2(a, pos0, pos1)) a = af.randu(8, 1) af.display(a) af.display(af.fft(a)) af.display(af.dft(a)) af.display(af.real(af.ifft(af.fft(a)))) af.display(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4)
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # http://arrayfire.com/licenses/BSD-3-Clause ######################################################## import arrayfire as af a = 10 * af.randu(6, 6) a3 = 10 * af.randu(5,5,3) dx,dy = af.gradient(a) af.display(dx) af.display(dy) af.display(af.resize(a, scale=0.5)) af.display(af.resize(a, odim0=8, odim1=8)) t = af.randu(3,2) af.display(af.transform(a, t)) af.display(af.rotate(a, 3.14)) af.display(af.translate(a, 1, 1)) af.display(af.scale(a, 1.2, 1.2, 7, 7)) af.display(af.skew(a, 0.02, 0.02)) h = af.histogram(a, 3) af.display(h) af.display(af.hist_equal(a, h))
def simple_signal(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) signal = af.randu(10) x_new = af.randu(10) x_orig = af.randu(10) display_func(af.approx1(signal, x_new, xp = x_orig)) signal = af.randu(3, 3) x_new = af.randu(3, 3) x_orig = af.randu(3, 3) y_new = af.randu(3, 3) y_orig = af.randu(3, 3) display_func(af.approx2(signal, x_new, y_new, xp = x_orig, yp = y_orig)) a = af.randu(8, 1) display_func(a) display_func(af.fft(a)) display_func(af.dft(a)) display_func(af.real(af.ifft(af.fft(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft(a) af.ifft_inplace(b) display_func(b) af.fft_inplace(b) display_func(b) b = af.fft_r2c(a) c = af.fft_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4) display_func(a) display_func(af.fft2(a)) display_func(af.dft(a)) display_func(af.real(af.ifft2(af.fft2(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft2(a) af.ifft2_inplace(b) display_func(b) af.fft2_inplace(b) display_func(b) b = af.fft2_r2c(a) c = af.fft2_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4, 2) display_func(a) display_func(af.fft3(a)) display_func(af.dft(a)) display_func(af.real(af.ifft3(af.fft3(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft3(a) af.ifft3_inplace(b) display_func(b) af.fft3_inplace(b) display_func(b) b = af.fft3_r2c(a) c = af.fft3_c2r(b) display_func(b) display_func(c) a = af.randu(10, 1) b = af.randu(3, 1) display_func(af.convolve1(a, b)) display_func(af.fft_convolve1(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) display_func(af.convolve2(a, b)) display_func(af.fft_convolve2(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) display_func(af.convolve3(a, b)) display_func(af.fft_convolve3(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) b = af.randu(3, 1) x = af.randu(10, 1) a = af.randu(2, 1) display_func(af.fir(b, x)) display_func(af.iir(b, a, x)) display_func(af.medfilt1(a)) display_func(af.medfilt2(a)) display_func(af.medfilt(a))
def calc_pi_device(samples): x = randu(samples) y = randu(samples) return 4 * af.sum((x * x + y * y) < 1) / samples
#!/usr/bin/python import arrayfire as af af.print_array(af.constant(100, 3,3, dtype=af.f32)) af.print_array(af.constant(25, 3,3, dtype=af.c32)) af.print_array(af.constant(2**50, 3,3, dtype=af.s64)) af.print_array(af.constant(2+3j, 3,3)) af.print_array(af.constant(3+5j, 3,3, dtype=af.c32)) af.print_array(af.range(3, 3)) af.print_array(af.iota(3, 3, tile_dims=(2,2))) af.print_array(af.randu(3, 3, 1, 2)) af.print_array(af.randu(3, 3, 1, 2, af.b8)) af.print_array(af.randu(3, 3, dtype=af.c32)) af.print_array(af.randn(3, 3, 1, 2)) af.print_array(af.randn(3, 3, dtype=af.c32)) af.set_seed(1024) assert(af.get_seed() == 1024) af.print_array(af.identity(3, 3, 1, 2, af.b8)) af.print_array(af.identity(3, 3, dtype=af.c32)) a = af.randu(3, 4) b = af.diag(a, extract=True) c = af.diag(a, 1, extract=True) af.print_array(a) af.print_array(b)
print("Green : Cells that are new as a result of reproduction" ) print("Blue : Cells that have died due to over population" ) print("This examples is throttled to 30 FPS so as to be a better visualization") simple_win = af.Window(512, 512, "Conway's Game of Life - Current State") pretty_win = af.Window(512, 512, "Conway's Game of Life - Current State with visualization") simple_win.set_pos(25, 15) pretty_win.set_pos(600, 25) frame_count = 0 # Copy kernel that specifies neighborhood conditions kernel = af.Array(h_kernel, dims=(3,3)) # Generate the initial state with 0s and 1s state = (af.randu(game_h, game_w) > 0.4).as_type(af.Dtype.f32) # tile 3 times to display color display = af.tile(state, 1, 1, 3, 1) while (not simple_win.close()) and (not pretty_win.close()): delay = time() if (not simple_win.close()): simple_win.image(state) if (not pretty_win.close()): pretty_win.image(display) frame_count += 1 if (frame_count % reset == 0): state = (af.randu(game_h, game_w) > 0.4).as_type(af.Dtype.f32) neighborhood = af.convolve(state, kernel)