Exemplo n.º 1
0
    def test_generic_syntax_float(self):
        ############################################################
        from pykeops.torch import Genred
        aliases = ['p=Pm(1)', 'a=Vj(1)', 'x=Vi(3)', 'y=Vj(3)']
        formula = 'Square(p-a)*Exp(x+y)'
        if pykeops.config.gpu_available:
            backend_to_test = ['auto', 'GPU_1D', 'GPU_2D', 'GPU']
        else:
            backend_to_test = ['auto']

        for b in backend_to_test:
            with self.subTest(b=b):
                # Call cuda kernel
                gamma_keops = Genred(formula, aliases, axis=1,
                                     dtype='float32')(self.sigmac,
                                                      self.gc,
                                                      self.xc,
                                                      self.yc,
                                                      backend=b)
                # Numpy version
                gamma_py = np.sum((self.sigma - self.g)**2 * np.exp(
                    (self.y.T[:, :, np.newaxis] + self.x.T[:, np.newaxis, :])),
                                  axis=1).T
                # compare output
                self.assertTrue(
                    np.allclose(gamma_keops.cpu().data.numpy(),
                                gamma_py,
                                atol=1e-6))
Exemplo n.º 2
0
    def test_generic_syntax_double(self):
        ############################################################
        from pykeops.torch import Genred

        aliases = ["p=Pm(1)", "a=Vj(1)", "x=Vi(3)", "y=Vj(3)"]
        formula = "Square(p-a)*Exp(x+y)"
        if pykeops.config.gpu_available:
            backend_to_test = ["auto", "GPU_1D", "GPU_2D", "GPU"]
        else:
            backend_to_test = ["auto"]

        for b in backend_to_test:
            with self.subTest(b=b):
                # Call cuda kernel
                gamma_keops = Genred(formula, aliases, axis=1,
                                     dtype="float64")(self.sigmacd,
                                                      self.gcd,
                                                      self.xcd,
                                                      self.ycd,
                                                      backend=b)
                # Numpy version
                gamma_py = np.sum(
                    (self.sigma - self.g)**2 * np.exp(
                        (self.y.T[:, :, np.newaxis] +
                         self.x.T[:, np.newaxis, :])),
                    axis=1,
                ).T
                # compare output
                self.assertTrue(
                    np.allclose(gamma_keops.cpu().data.numpy(),
                                gamma_py,
                                atol=1e-6))