Just-In-Time Compilation

Overview

NLCPy provides a Just-In-Time(JIT) compilation functionality. By using this, you can customize your C/C++/Fortran kernel directly on your Python program.

Here is a simple example.

>>> import nlcpy
>>> from nlcpy import ve_types
>>>
>>> ve_lib = nlcpy.jit.CustomVELibrary(
...     code=r'''
...         int ve_add(double *px, double *py, double *pz, int n) {
...             #pragma omp parallel for
...             for (int i = 0; i  < n; i++) pz[i] = px[i] + py[i];
...             return 0;
...         }
... '''
... )
>>> ve_add = ve_lib.get_function(
...     've_add',
...     args_type=(ve_types.uint64, ve_types.uint64, ve_types.uint64, ve_types.int32),
...     ret_type=ve_types.int32
... )
>>>
>>> x = nlcpy.arange(10., dtype='f8')
>>> y = nlcpy.arange(10., dtype='f8')
>>> z = nlcpy.empty(10, dtype='f8')
>>> ret = ve_add(x.ve_adr, y.ve_adr, z.ve_adr, z.size, sync=True)
>>> z
array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18.])
>>> ret
0
>>>
>>> nlcpy.jit.unload_library(ve_lib)

To compile and execute the VE function from raw source code:

  1. Define source code in Python code as a string.

  2. Pass the string source into code argument of nlcpy.jit.CustomVELibrary.

  3. Call CustomVELibrary.get_function() to get a function symbol.

  4. Execute the object that is returned from CustomVELibrary.get_function() as a function.

  5. Unload the shared library if needed.

Coding Guide

APIs

Customize VE Library and Kernel

nlcpy.jit.CustomVELibrary

Custom VE library class.

nlcpy.jit.CustomVEKernel

Custom VE kernel class.

Helper Routines

nlcpy.jit.get_default_cflags

Gets default compiler flags.

nlcpy.jit.get_default_ldflags

Gets default linker flags.

nlcpy.jit.unload_library

Unloads the shared library.

Constants

nlcpy.ve_types.void
Returns

‘void’ : string

nlcpy.ve_types.char
Returns

‘char’ : string

nlcpy.ve_types.int32
Returns

‘int32_t’ : string

nlcpy.ve_types.int64
Returns

‘int64_t’ : string

nlcpy.ve_types.uint32
Returns

‘uint32_t’ : string

nlcpy.ve_types.uint64
Returns

‘uint64_t’ : string

nlcpy.ve_types.float32
Returns

‘float’ : string

nlcpy.ve_types.float64
Returns

‘double’ : string

nlcpy.ve_types.void_p
Returns

‘void *’ : string