instant.inlining

Package instant :: Module inlining |
[hide private]
[frames] | no frames]

# Source Code for Module instant.inlining

      1  """This module contains the inline* functions, which allows easy inlining of C/C++ functions."""
      2
      3  import sys
      4  from output import instant_assert, instant_warning, instant_error
      5  from build import build_module
      6
      7
      8 -def get_func_name(c_code):
      9      # TODO: Something more robust? Regexp?
     10      try:
     11          func = c_code[:c_code.index('(')]
     12          ret, func_name = func.split()
     13      except:
     14          instant_error("Failed to extract function name from c_code.")
     15      return func_name
     16
     17
     18 -def inline(c_code, **kwargs):
     19      """This is a short wrapper around the build_module function in instant.
     20
     21      It creates a module given that
     22      the input is a valid C function. It is only possible
     23      to inline one C function each time.
     24
     25      Usage:
     26
     27      >>> from instant import inline
     28      >>> add_func = inline("double add(double a, double b){ return a+b; }")
     29      >>> print "The sum of 3 and 4.5 is ", add_func(3, 4.5)
     30      """
     31      instant_assert("code" not in kwargs, "Cannot specify code twice.")
     32      kwargs["code"] = c_code
     33      func_name = get_func_name(c_code)
     34      module = build_module(**kwargs)
     35      if hasattr(module, func_name):
     36          return getattr(module, func_name)
     37      else:
     38          instant_warning("Didn't find function '%s', returning module." % func_name)
     39      return module
     40
     41 -def inline_module(c_code, **kwargs):
     42      """This is a short wrapper around the build_module function in instant.
     43
     44      It creates a module given that
     45      the input is a valid C function. It is only possible
     46      to inline one C function each time.
     47
     48      Usage:
     49
     50      >>> from instant import inline
     51      >>> add_func = inline("double add(double a, double b){ return a+b; }")
     52      >>> print "The sum of 3 and 4.5 is ", add_func(3, 4.5)
     53      """
     54      instant_assert("code" not in kwargs, "Cannot specify code twice.")
     55      kwargs["code"] = c_code
     56      module = build_module(**kwargs)
     57      return module
     58
     59
     60
     61 -def inline_with_numpy(c_code, **kwargs):
     62      '''This is a short wrapper around the build_module function in instant.
     63
     64      It creates a module given that
     65      the input is a valid C function. It is only possible
     66      to inline one C function each time. The difference between
     67      this function and the inline function is that C-arrays can be used.
     68      The following example illustrates that.
     69
     70      Usage:
     71
     72      >>> import numpy
     73      >>> import time
     74      >>> from instant import inline_with_numpy
     75      >>> c_code = """
     76          double sum (int n1, double* array1){
     77              double tmp = 0.0;
     78              for (int i=0; i<n1; i++) {
     79                  tmp += array1[i];
     80              }
     81              return tmp;
     82          }
     83          """
     84      >>> sum_func = inline_with_numpy(c_code,  arrays = [['n1', 'array1']])
     85      >>> a = numpy.arange(10000000); a = numpy.sin(a)
     86      >>> sum_func(a)
     87      '''
     88      import numpy
     89      instant_assert("code" not in kwargs, "Cannot specify code twice.")
     90      kwargs["code"] = c_code
     91      kwargs["init_code"]      = kwargs.get("init_code","")      + "\nimport_array();\n"
     92      kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["numpy/arrayobject.h"]
     93      kwargs["include_dirs"]   = kwargs.get("include_dirs",[])   + ["%s" %numpy.get_include()]
     94      func_name = get_func_name(c_code)
     95      module = build_module(**kwargs)
     96      if hasattr(module, func_name):
     97          return getattr(module, func_name)
     98      else:
     99          instant_warning("Didn't find function '%s', returning module." % func_name)
    100      return module
    101
    102 -def inline_module_with_numpy(c_code, **kwargs):
    103      '''This is a short wrapper around the build_module function in instant.
    104
    105      It creates a module given that
    106      the input is a valid C function. It is only possible
    107      to inline one C function each time. The difference between
    108      this function and the inline function is that C-arrays can be used.
    109      The following example illustrates that.
    110
    111      Usage:
    112
    113      >>> import numpy
    114      >>> import time
    115      >>> from instant import inline_with_numpy
    116      >>> c_code = """
    117          double sum (int n1, double* array1){
    118              double tmp = 0.0;
    119              for (int i=0; i<n1; i++) {
    120                  tmp += array1[i];
    121              }
    122              return tmp;
    123          }
    124          """
    125      >>> sum_func = inline_with_numpy(c_code,  arrays = [['n1', 'array1']])
    126      >>> a = numpy.arange(10000000); a = numpy.sin(a)
    127      >>> sum_func(a)
    128      '''
    129      import numpy
    130      instant_assert("code" not in kwargs, "Cannot specify code twice.")
    131      kwargs["code"] = c_code
    132      kwargs["init_code"]      = kwargs.get("init_code","")      + "\nimport_array();\n"
    133      kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["numpy/arrayobject.h"]
    134      kwargs["include_dirs"]   = kwargs.get("include_dirs",[])   + ["%s" % numpy.get_include()]
    135      module = build_module(**kwargs)
    136      return module
    137