Ejemplo n.º 1
0
    def compile(self, code):
        if self.backend == 'opencl':
            ext = '.cl'
            backend = 'OpenCL'
        elif self.backend == 'cuda':
            ext = '.cu'
            backend = 'CUDA'
        code = convert_to_float_if_needed(code)
        path = os.path.expanduser(
            os.path.join('~', '.pysph', 'source', get_platform_dir()))
        if not os.path.exists(path):
            os.makedirs(path)
        fname = os.path.join(path, 'generated' + ext)
        with open(fname, 'w') as fp:
            fp.write(code)
            print("{backend} code written to {fname}".format(backend=backend,
                                                             fname=fname))
        code = code.encode('ascii') if sys.version_info.major < 3 else code

        if self.backend == 'opencl':
            import pyopencl as cl
            self.program = cl.Program(self._ctx, code).build(options=['-w'])
        elif self.backend == 'cuda':
            from compyle.cuda import SourceModule
            self.program = SourceModule(code)
        return self.program
Ejemplo n.º 2
0
 def compile(self, code):
     # Note, we do not add carray or particle_array as nnps_base would
     # have been rebuilt anyway if they changed.
     root = expanduser(join('~', '.pysph', 'source', get_platform_dir()))
     depends = ["pysph.base.nnps_base"]
     # Add pysph/base directory to inc_dirs for including spatial_hash.h
     # for SpatialHashNNPS
     extra_inc_dirs = [join(dirname(dirname(realpath(__file__))), 'base')]
     self._ext_mod = ExtModule(code,
                               verbose=False,
                               root=root,
                               depends=depends,
                               extra_inc_dirs=extra_inc_dirs)
     self._module = self._ext_mod.load()
     return self._module
 def compile(self, code):
     code = convert_to_float_if_needed(code)
     path = os.path.expanduser(os.path.join(
         '~', '.pysph', 'source', get_platform_dir()
     ))
     if not os.path.exists(path):
         os.makedirs(path)
     fname = os.path.join(path, 'generated.cl')
     with open(fname, 'w') as fp:
         fp.write(code)
         print("OpenCL code written to %s" % fname)
     code = code.encode('ascii') if sys.version_info.major < 3 else code
     self.program = cl.Program(self._ctx, code).build(
         options=['-w']
     )
     return self.program
Ejemplo n.º 4
0
    def compile(self, code):
        if self.backend == 'opencl':
            ext = '.cl'
            backend = 'OpenCL'
        elif self.backend == 'cuda':
            ext = '.cu'
            backend = 'CUDA'
        code = convert_to_float_if_needed(code)
        path = os.path.expanduser(os.path.join(
            '~', '.pysph', 'source', get_platform_dir()
        ))
        if not os.path.exists(path):
            os.makedirs(path)
        md5 = get_md5(code)
        fname = os.path.join(path, 'm_{0}{1}'.format(md5, ext))
        # If the file already exists, we use it, this allows to write our
        # own edited version if we wish to.
        if os.path.exists(fname):
            msg = 'Reading code from {}'.format(fname)
            logger.info(msg)
            print(msg)
            with open(fname, 'r') as fp:
                code = fp.read()
        else:
            with open(fname, 'w') as fp:
                fp.write(code)
            msg = "{backend} code written to {fname}".format(
                backend=backend, fname=fname
            )
            print(msg)
            logger.info(msg)

        if self.backend == 'opencl':
            import pyopencl as cl
            self.program = cl.Program(self._ctx, code).build(
                options=['-w']
            )
        elif self.backend == 'cuda':
            from compyle.cuda import SourceModule
            self.program = SourceModule(code)
        return self.program