def test_initial_version(self, backend, mat, g, x): assert mat._version == 1 assert g._version == 1 assert x._version == 1 c = op2.Const(1, 1, name='c2', dtype=numpy.uint32) assert c._version == 1 c.remove_from_namespace()
def test_1d_read(self, backend, set, dat): kernel = """ void kernel_1d_read(int *x) { *x = myconstant; } """ constant = op2.Const(1, 100, dtype=numpy.int32, name="myconstant") op2.par_loop(op2.Kernel(kernel, "kernel_1d_read"), set, dat(op2.WRITE)) constant.remove_from_namespace() assert all(dat.data == constant.data)
def test_change_const_dim_matters(self, backend, iterset, diterset): d = op2.Dat(diterset, range(nelems), numpy.uint32) self.cache.clear() assert len(self.cache) == 0 k = op2.Kernel("""void k(unsigned int *x) {}""", 'k') c = op2.Const(1, 1, name='c', dtype=numpy.uint32) op2.par_loop(k, iterset, d(op2.WRITE)) op2.base._trace.evaluate(set([d]), set()) assert len(self.cache) == 1 c.remove_from_namespace() c = op2.Const(2, (1, 1), name='c', dtype=numpy.uint32) op2.par_loop(k, iterset, d(op2.WRITE)) op2.base._trace.evaluate(set([d]), set()) assert len(self.cache) == 2 c.remove_from_namespace()
def test_change_constant_works(self, backend, set, dat): k = """ void k(int *x) { *x = myconstant; } """ constant = op2.Const(1, 10, dtype=numpy.int32, name="myconstant") op2.par_loop(op2.Kernel(k, 'k'), set, dat(op2.WRITE)) assert all(dat.data == constant.data) constant.data == 11 op2.par_loop(op2.Kernel(k, 'k'), set, dat(op2.WRITE)) constant.remove_from_namespace() assert all(dat.data == constant.data)
def test_change_constant_doesnt_require_parloop_regen( self, backend, set, dat): k = """ void k(int *x) { *x = myconstant; } """ cache = op2.base.JITModule._cache cache.clear() constant = op2.Const(1, 10, dtype=numpy.int32, name="myconstant") op2.par_loop(op2.Kernel(k, 'k'), set, dat(op2.WRITE)) assert all(dat.data == constant.data) assert len(cache) == 1 constant.data == 11 op2.par_loop(op2.Kernel(k, 'k'), set, dat(op2.WRITE)) constant.remove_from_namespace() assert all(dat.data == constant.data) assert len(cache) == 1
pp[2 * e] = n pp[2 * e + 1] = i2 - 1 + (j2 - 1) * (NN - 1) A[e] = 0.25 e += 1 nodes = op2.Set(nnode, "nodes") edges = op2.Set(nedge, "edges") ppedge = op2.Map(edges, nodes, 2, pp, "ppedge") p_A = op2.Dat(edges, data=A, name="p_A") p_r = op2.Dat(nodes, data=r, name="p_r") p_u = op2.Dat(nodes, data=u, name="p_u") p_du = op2.Dat(nodes, data=du, name="p_du") alpha = op2.Const(1, data=1.0, name="alpha", dtype=fp_type) beta = op2.Global(1, data=1.0, name="beta", dtype=fp_type) res = op2.Kernel( """void res(%(t)s *A, %(t)s *u, %(t)s *du, const %(t)s *beta){ *du += (*beta)*(*A)*(*u); }""" % {'t': "double" if fp_type == np.float64 else "float"}, "res") update = op2.Kernel( """ void update(%(t)s *r, %(t)s *du, %(t)s *u, %(t)s *u_sum, %(t)s *u_max) { *u += *du + alpha * (*r); *du = %(z)s; *u_sum += (*u)*(*u); *u_max = *u_max > *u ? *u_max : *u;
def main(opt): from aero_kernels import dirichlet, dotPV, dotR, init_cg, res_calc, spMV, \ update, updateP, updateUR try: with h5py.File(opt['mesh'], 'r') as f: # sets nodes = op2.Set.fromhdf5(f, 'nodes') bnodes = op2.Set.fromhdf5(f, 'bedges') cells = op2.Set.fromhdf5(f, 'cells') # maps pbnodes = op2.Map.fromhdf5(bnodes, nodes, f, 'pbedge') pcell = op2.Map.fromhdf5(cells, nodes, f, 'pcell') pvcell = op2.Map.fromhdf5(cells, nodes, f, 'pcell') # dats p_xm = op2.Dat.fromhdf5(nodes ** 2, f, 'p_x') p_phim = op2.Dat.fromhdf5(nodes, f, 'p_phim') p_resm = op2.Dat.fromhdf5(nodes, f, 'p_resm') p_K = op2.Dat.fromhdf5(cells ** 16, f, 'p_K') p_V = op2.Dat.fromhdf5(nodes, f, 'p_V') p_P = op2.Dat.fromhdf5(nodes, f, 'p_P') p_U = op2.Dat.fromhdf5(nodes, f, 'p_U') except IOError: import sys print "Failed reading mesh: Could not read from %s\n" % opt['mesh'] sys.exit(1) # Constants gam = 1.4 gm1 = op2.Const(1, gam - 1.0, 'gm1', dtype=np.double) op2.Const(1, 1.0 / gm1.data, 'gm1i', dtype=np.double) op2.Const(2, [0.5, 0.5], 'wtg1', dtype=np.double) op2.Const(2, [0.211324865405187, 0.788675134594813], 'xi1', dtype=np.double) op2.Const(4, [0.788675134594813, 0.211324865405187, 0.211324865405187, 0.788675134594813], 'Ng1', dtype=np.double) op2.Const(4, [-1, -1, 1, 1], 'Ng1_xi', dtype=np.double) op2.Const(4, [0.25] * 4, 'wtg2', dtype=np.double) op2.Const(16, [0.622008467928146, 0.166666666666667, 0.166666666666667, 0.044658198738520, 0.166666666666667, 0.622008467928146, 0.044658198738520, 0.166666666666667, 0.166666666666667, 0.044658198738520, 0.622008467928146, 0.166666666666667, 0.044658198738520, 0.166666666666667, 0.166666666666667, 0.622008467928146], 'Ng2', dtype=np.double) op2.Const(32, [-0.788675134594813, 0.788675134594813, -0.211324865405187, 0.211324865405187, -0.788675134594813, 0.788675134594813, -0.211324865405187, 0.211324865405187, -0.211324865405187, 0.211324865405187, -0.788675134594813, 0.788675134594813, -0.211324865405187, 0.211324865405187, -0.788675134594813, 0.788675134594813, -0.788675134594813, -0.211324865405187, 0.788675134594813, 0.211324865405187, -0.211324865405187, -0.788675134594813, 0.211324865405187, 0.788675134594813, -0.788675134594813, -0.211324865405187, 0.788675134594813, 0.211324865405187, -0.211324865405187, -0.788675134594813, 0.211324865405187, 0.788675134594813], 'Ng2_xi', dtype=np.double) minf = op2.Const(1, 0.1, 'minf', dtype=np.double) op2.Const(1, minf.data ** 2, 'm2', dtype=np.double) op2.Const(1, 1, 'freq', dtype=np.double) op2.Const(1, 1, 'kappa', dtype=np.double) op2.Const(1, 0, 'nmode', dtype=np.double) op2.Const(1, 1.0, 'mfan', dtype=np.double) niter = 20 for i in xrange(1, niter + 1): op2.par_loop(res_calc, cells, p_xm(op2.READ, pvcell), p_phim(op2.READ, pcell), p_K(op2.WRITE), p_resm(op2.INC, pcell)) op2.par_loop(dirichlet, bnodes, p_resm(op2.WRITE, pbnodes[0])) c1 = op2.Global(1, data=0.0, name='c1') c2 = op2.Global(1, data=0.0, name='c2') c3 = op2.Global(1, data=0.0, name='c3') # c1 = R' * R op2.par_loop(init_cg, nodes, p_resm(op2.READ), c1(op2.INC), p_U(op2.WRITE), p_V(op2.WRITE), p_P(op2.WRITE)) # Set stopping criteria res0 = sqrt(c1.data) res = res0 res0 *= 0.1 it = 0 maxiter = 200 while res > res0 and it < maxiter: # V = Stiffness * P op2.par_loop(spMV, cells, p_V(op2.INC, pcell), p_K(op2.READ), p_P(op2.READ, pcell)) op2.par_loop(dirichlet, bnodes, p_V(op2.WRITE, pbnodes[0])) c2.data = 0.0 # c2 = P' * V op2.par_loop(dotPV, nodes, p_P(op2.READ), p_V(op2.READ), c2(op2.INC)) alpha = op2.Global(1, data=c1.data / c2.data, name='alpha') # U = U + alpha * P # resm = resm - alpha * V op2.par_loop(updateUR, nodes, p_U(op2.INC), p_resm(op2.INC), p_P(op2.READ), p_V(op2.RW), alpha(op2.READ)) c3.data = 0.0 # c3 = resm' * resm op2.par_loop(dotR, nodes, p_resm(op2.READ), c3(op2.INC)) beta = op2.Global(1, data=c3.data / c1.data, name="beta") # P = beta * P + resm op2.par_loop(updateP, nodes, p_resm(op2.READ), p_P(op2.RW), beta(op2.READ)) c1.data = c3.data res = sqrt(c1.data) it += 1 rms = op2.Global(1, data=0.0, name='rms') # phim = phim - Stiffness \ Load op2.par_loop(update, nodes, p_phim(op2.RW), p_resm(op2.WRITE), p_U(op2.READ), rms(op2.INC)) print "rms = %10.5e iter: %d" % (sqrt(rms.data) / sqrt(nodes.size), it)