Exemple #1
0
 def add_to_cuda_preamble(self, pa):
     """
     Deprecated.  Use add_to_preamble(..., backend="cuda").
     """
     if isinstance(pa, str):
         pa = [cpp_ast.Line(pa)]
     self.add_to_preamble(pa, backend="cuda")
Exemple #2
0
    def add_variant(self,
                    variant_name,
                    variant_func,
                    run_check_func=lambda *args, **kwargs: True):
        """
        Add a variant of this function.  Must have same call signature.  Variant names must be unique.
        The variant_func parameter should be a CodePy Function object or a string defining the function.
        The run_check_func parameter should be a lambda function with signature run(*args,**kwargs).
        """
        if variant_name in self.variant_names:
            raise Exception(
                "Attempting to add a variant with an already existing name %s to %s"
                % (variant_name, self.name))
        self.variant_names.append(variant_name)
        self.variant_funcs.append(variant_func)
        self.run_check_funcs.append(run_check_func)

        if isinstance(self.backend.module, scala_module.ScalaModule):
            self.backend.module.add_to_module(variant_func)
            self.backend.module.add_to_init(variant_name)
        elif isinstance(variant_func, basestring):
            if isinstance(
                    self.backend.module, codepy.cuda.CudaModule
            ):  #HACK because codepy's CudaModule doesn't have add_to_init()
                self.backend.module.boost_module.add_to_module(
                    [cpp_ast.Line(variant_func)])
                self.backend.module.boost_module.add_to_init([
                    cpp_ast.Statement("boost::python::def(\"%s\", &%s)" %
                                      (variant_name, variant_name))
                ])
            else:
                self.backend.module.add_to_module([cpp_ast.Line(variant_func)])
                if self.call_policy == "python_gc":
                    self.backend.module.add_to_init([
                        cpp_ast.Statement(
                            "boost::python::def(\"%s\", &%s, boost::python::return_value_policy<boost::python::manage_new_object>())"
                            % (variant_name, variant_name))
                    ])
                else:
                    self.backend.module.add_to_init([
                        cpp_ast.Statement("boost::python::def(\"%s\", &%s)" %
                                          (variant_name, variant_name))
                    ])
        else:
            self.backend.module.add_function(variant_func)

        self.backend.dirty = True
Exemple #3
0
 def add_to_init(self, stmt, backend="c++"):
     if isinstance(stmt, str):
         stmt = [cpp_ast.Line(stmt)]
     if backend == "cuda":
         self.backends[backend].module.boost_module.add_to_init(
             stmt
         )  #HACK because codepy's CudaModule doesn't have add_to_init()
     else:
         self.backends[backend].module.add_to_init(stmt)
Exemple #4
0
    def expose_class(self, classname, backend="c++"):
        """
        Expose a class or struct from C++ to Python, letting us pass instances back and forth
        between Python and C++.

        TODO: allow exposing *functions* within the class
        """
        self.backends[backend].module.add_to_init([
            cpp_ast.Line("boost::python::class_<%s>(\"%s\");\n" %
                         (classname, classname))
        ])
Exemple #5
0
    def add_variant(self, variant_name, variant_func):
        """
        Add a variant of this function.  Must have same call signature.  Variant names must be unique.
        The variant_func parameter should be a CodePy Function object or a string defining the function.
        """
        if variant_name in self.variant_names:
            raise Exception(
                "Attempting to add a variant with an already existing name %s to %s"
                % (variant_name, self.name))
        self.variant_names.append(variant_name)
        self.variant_funcs.append(variant_func)

        if isinstance(variant_func, str):
            self.backend.module.add_to_module([cpp_ast.Line(variant_func)])
            self.backend.module.add_to_init([
                cpp_ast.Statement("boost::python::def(\"%s\", &%s)" %
                                  (variant_name, variant_name))
            ])
        else:
            self.backend.module.add_function(variant_func)

        self.backend.dirty = True
Exemple #6
0
 def add_to_cuda_module(self, block):
     #FIXME: figure out use case for this and replace
     if isinstance(block, str):
         block = [cpp_ast.Line(block)]
     self.backends["cuda"].module.add_to_module(block)
Exemple #7
0
 def add_to_init(self, stmt, backend="c++"):
     if isinstance(stmt, str):
         stmt = [cpp_ast.Line(stmt)]
     self.backends[backend].module.add_to_init(stmt)
Exemple #8
0
 def add_to_preamble(self, pa, backend="c++"):
     if isinstance(pa, str):
         pa = [cpp_ast.Line(pa)]
     self.backends[backend].module.add_to_preamble(pa)
Exemple #9
0
 def add_to_module(self, block, backend="c++"):
     if isinstance(block, basestring):
         block = [cpp_ast.Line(block)]
     self.backends[backend].module.add_to_module(block)