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",[]) + ["arrayobject.h"] 93 kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + ["%s/numpy" % 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",[]) + ["arrayobject.h"] 134 kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + ["%s/numpy" % numpy.get_include()] 135 module = build_module(**kwargs) 136 return module
137 138
139 -def inline_with_numeric(c_code, **kwargs):
140 '''This is a short wrapper around the build_module function in instant. 141 142 It creates a module given that 143 the input is a valid C function. It is only possible 144 to inline one C function each time. The difference between 145 this function and the inline function is that C-arrays can be used. 146 The following example illustrates that. 147 148 Usage: 149 150 >>> import numpy 151 >>> import time 152 >>> from instant import inline_with_numeric 153 >>> c_code = """ 154 double sum (int n1, double* array1){ 155 double tmp = 0.0; 156 for (int i=0; i<n1; i++) { 157 tmp += array1[i]; 158 } 159 return tmp; 160 } 161 """ 162 >>> sum_func = inline_with_numeric(c_code, arrays = [['n1', 'array1']]) 163 >>> a = numpy.arange(10000000); a = numpy.sin(a) 164 >>> sum_func(a) 165 ''' 166 instant_assert("code" not in kwargs, "Cannot specify code twice.") 167 kwargs["code"] = c_code 168 kwargs["init_code"] = kwargs.get("init_code", "") + "\nimport_array();\n" 169 kwargs["system_headers"] = kwargs.get("system_headers", []) + ["arrayobject.h"] 170 171 # TODO: This isn't very general! 172 if sys.platform=='win32': 173 inc_dirs = [sys.prefix + "/include" + "/Numeric"] 174 else: 175 inc_dirs = [sys.prefix + "/include/python" + sys.version[:3] + "/Numeric"] 176 inc_dirs.append("/usr/local/include/python" + sys.version[:3] + "/Numeric") 177 kwargs["include_dirs"] = kwargs.get("include_dirs", []) + inc_dirs 178 179 module = build_module(**kwargs) 180 func_name = get_func_name(c_code) 181 if hasattr(module, func_name): 182 return getattr(module, func_name) 183 else: 184 instant_warning("Didn't find function '%s', returning module." % func_name) 185 return module
186
187 -def inline_module_with_numeric(c_code, **kwargs):
188 '''This is a short wrapper around the build_module function in instant. 189 190 It creates a module given that 191 the input is a valid C function. It is only possible 192 to inline one C function each time. The difference between 193 this function and the inline function is that C-arrays can be used. 194 The following example illustrates that. 195 196 Usage: 197 198 >>> import numpy 199 >>> import time 200 >>> from instant import inline_with_numeric 201 >>> c_code = """ 202 double sum (int n1, double* array1){ 203 double tmp = 0.0; 204 for (int i=0; i<n1; i++) { 205 tmp += array1[i]; 206 } 207 return tmp; 208 } 209 """ 210 >>> sum_func = inline_with_numeric(c_code, arrays = [['n1', 'array1']]) 211 >>> a = numpy.arange(10000000); a = numpy.sin(a) 212 >>> sum_func(a) 213 ''' 214 instant_assert("code" not in kwargs, "Cannot specify code twice.") 215 kwargs["code"] = c_code 216 kwargs["init_code"] = kwargs.get("init_code", "") + "\nimport_array();\n" 217 kwargs["system_headers"] = kwargs.get("system_headers", []) + ["arrayobject.h"] 218 219 # TODO: This isn't very general! 220 if sys.platform=='win32': 221 inc_dirs = [sys.prefix + "/include" + "/Numeric"] 222 else: 223 inc_dirs = [sys.prefix + "/include/python" + sys.version[:3] + "/Numeric"] 224 inc_dirs.append("/usr/local/include/python" + sys.version[:3] + "/Numeric") 225 kwargs["include_dirs"] = kwargs.get("include_dirs", []) + inc_dirs 226 227 module = build_module(**kwargs) 228 return module
229 230 231
232 -def inline_with_numarray(c_code, **kwargs):
233 """This is a short wrapper around the build_module function in instant. 234 235 It creates a module given that 236 the input is a valid C function. It is only possible 237 to inline one C function each time. The difference between 238 this function and the inline function is that C-arrays can be used. 239 The following example illustrates that. 240 241 Usage: 242 243 >>> import numarray 244 >>> import time 245 >>> from instant import inline_with_numarray 246 >>> c_code = \"\"\" 247 double sum (int n1, double* array1){ 248 double tmp = 0.0; 249 for (int i=0; i<n1; i++) { 250 tmp += array1[i]; 251 } 252 return tmp; 253 } 254 \"\"\" 255 >>> sum_func = inline_with_numarray(c_code, arrays = [['n1', 'array1']]) 256 >>> a = numarray.arange(10000000); a = numarray.sin(a) 257 >>> sum_func(a) 258 """ 259 import numarray 260 261 instant_assert("code" not in kwargs, "Cannot specify code twice.") 262 kwargs["code"] = c_code 263 264 kwargs["init_code"] = kwargs.get("init_code", "") + "\nimport_array();\n" 265 kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["arrayobject.h"] 266 267 # TODO: Is the second and third path here necessary? 268 inc_dirs = [numarray.numinclude.include_dir, 269 "/usr/local/include/python" + sys.version[:3] + "/numarray", 270 "/usr/include/python" + sys.version[:3] + "/numarray" ] 271 kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + inc_dirs 272 273 func_name = get_func_name(c_code) 274 module = build_module(**kwargs) 275 if hasattr(module, func_name): 276 return getattr(module, func_name) 277 else: 278 instant_warning("Didn't find function '%s', returning module." % func_name) 279 return module
280
281 -def inline_module_with_numarray(c_code, **kwargs):
282 """This is a short wrapper around the build_module function in instant. 283 284 It creates a module given that 285 the input is a valid C function. It is only possible 286 to inline one C function each time. The difference between 287 this function and the inline function is that C-arrays can be used. 288 The following example illustrates that. 289 290 Usage: 291 292 >>> import numarray 293 >>> import time 294 >>> from instant import inline_with_numarray 295 >>> c_code = \"\"\" 296 double sum (int n1, double* array1){ 297 double tmp = 0.0; 298 for (int i=0; i<n1; i++) { 299 tmp += array1[i]; 300 } 301 return tmp; 302 } 303 \"\"\" 304 >>> sum_func = inline_with_numarray(c_code, arrays = [['n1', 'array1']]) 305 >>> a = numarray.arange(10000000); a = numarray.sin(a) 306 >>> sum_func(a) 307 """ 308 import numarray 309 310 instant_assert("code" not in kwargs, "Cannot specify code twice.") 311 kwargs["code"] = c_code 312 313 kwargs["init_code"] = kwargs.get("init_code", "") + "\nimport_array();\n" 314 kwargs["system_headers"] = kwargs.get("system_headers",[]) + ["arrayobject.h"] 315 316 # TODO: Is the second and third path here necessary? 317 inc_dirs = [numarray.numinclude.include_dir, 318 "/usr/local/include/python" + sys.version[:3] + "/numarray", 319 "/usr/include/python" + sys.version[:3] + "/numarray" ] 320 kwargs["include_dirs"] = kwargs.get("include_dirs",[]) + inc_dirs 321 module = build_module(**kwargs) 322 return module
323