def test_fd_adjoint(self, so, ndim, derivative, adjoint_name): grid = Grid(shape=tuple([51] * ndim), extent=tuple([25] * ndim)) x = grid.dimensions[0] f = Function(name='f', grid=grid, space_order=so) f_deriv = Function(name='f_deriv', grid=grid, space_order=so) g = Function(name='g', grid=grid, space_order=so) g_deriv = Function(name='g_deriv', grid=grid, space_order=so) # Fill f and g with smooth cos/sin Operator( [Eq(g, x * cos(2 * np.pi * x / 5)), Eq(f, sin(2 * np.pi * x / 8))]).apply() # Check symbolic expression are expected ones for the adjoint .T deriv = getattr(f, derivative) coeff = 1 if derivative == 'dx2' else -1 expected = coeff * getattr(f, derivative).evaluate.subs( {x.spacing: -x.spacing}) assert simplify(deriv.T.evaluate) == simplify(bypass_uneval(expected)) # Compute numerical derivatives and verify dot test # i.e <f.dx, g> = <f, g.dx.T> eq_f = Eq(f_deriv, deriv) eq_g = Eq(g_deriv, getattr(g, derivative).T) op = Operator([eq_f, eq_g]) op() a = np.dot(f_deriv.data.reshape(-1), g.data.reshape(-1)) b = np.dot(g_deriv.data.reshape(-1), f.data.reshape(-1)) assert np.isclose(1 - a / b, 0, atol=1e-5)
def trig_func(model): """ Trigonometric function of the tilt and azymuth angles. """ try: theta = model.theta except AttributeError: theta = 0 costheta = cos(theta) sintheta = sin(theta) if model.dim == 3: try: phi = model.phi except AttributeError: phi = 0 cosphi = cos(phi) sinphi = sin(phi) return costheta, sintheta, cosphi, sinphi return costheta, sintheta
def test_simd_space_invariant(self): """ Similar to test_space_invariant_v3, testing simd vectorization happens in the correct place. """ grid = Grid(shape=(10, 10, 10)) x, y, z = grid.dimensions f = Function(name='f', grid=grid) eq = Inc(f, cos(x * y) + cos(x * z)) op = Operator(eq, opt=('advanced', {'openmp': True})) iterations = FindNodes(Iteration).visit(op) assert 'omp for collapse(1) schedule(static,1)' in iterations[ 0].pragmas[0].value assert 'omp simd' in iterations[1].pragmas[0].value assert 'omp simd' in iterations[3].pragmas[0].value op.apply() assert np.isclose(np.linalg.norm(f.data), 37.1458, rtol=1e-5)
def test_array_rw(self): grid = Grid(shape=(3, 3, 3)) f = Function(name='f', grid=grid) u = TimeFunction(name='u', grid=grid, space_order=2) eqn = Eq(u.forward, u * cos(f * 2)) op = Operator(eqn, language='openmp') assert len(op.body.allocs) == 1 assert str(op.body.allocs[0]) ==\ ('float * r0_vec = (float *)' 'omp_target_alloc(x_size*y_size*z_size*sizeof(float),' 'omp_get_default_device());') assert len(op.body.maps) == 2 assert all('r0' not in str(i) for i in op.body.maps) assert len(op.body.frees) == 1 assert str(op.body.frees[0]) ==\ 'omp_target_free(r0_vec,omp_get_default_device());' assert len(op.body.unmaps) == 3 assert all('r0' not in str(i) for i in op.body.unmaps)
def test_array_rw(self): grid = Grid(shape=(3, 3, 3)) f = Function(name='f', grid=grid) u = TimeFunction(name='u', grid=grid, space_order=2) eqn = Eq(u.forward, u*cos(f*2)) op = Operator(eqn, language='openmp') assert len(op.body[2].header) == 7 assert str(op.body[2].header[0]) == 'float (*r0)[y_size][z_size];' assert op.body[2].header[1].text ==\ 'posix_memalign((void**)&r0, 64, sizeof(float[x_size][y_size][z_size]))' assert op.body[2].header[2].value ==\ ('omp target enter data map(alloc: r0[0:x_size][0:y_size][0:z_size])' '') assert len(op.body[2].footer) == 6 assert str(op.body[2].footer[0]) == '' assert op.body[2].footer[1].value ==\ ('omp target exit data map(delete: r0[0:x_size][0:y_size][0:z_size])' ' if((x_size != 0) && (y_size != 0) && (z_size != 0))') assert op.body[2].footer[2].text == 'free(r0)'