source: codes/icosagcm/devel/Python/dynamico/dev/numba.py @ 805

Last change on this file since 805 was 805, checked in by dubos, 5 years ago

devel/Python : moving Fortran-based features to dynamico.dev module (TBC)

File size: 1.2 KB
Line 
1from __future__ import absolute_import     
2from __future__ import print_function
3
4import numpy as np
5from numba import int32, float64
6import numba
7
8class NumbaData(object):
9    """A base class to extract data from derived class instances and use it as argument to @jit functions. Derived classes must set the 'signature' attribute."""
10    def data(self):
11        """Returns a jitclass instance containing attributes copied from self, using self.signature which is of the form type,names,type,names ... where names is a string 'attr1 attr2 attr3' containing space-separated names of attributes of self. Those attributes are declared to numba with the type preceding them. The result of data() can be used as argument to a @jit function."""
12        spec = []
13        for item in self.signature:
14            if isinstance(item,str): 
15                spec = spec + [(name,thetype) for name in item.split(' ')]
16            else:
17                thetype=item
18        @numba.jitclass(spec)
19        class JitClass(object):
20            def __init__(self): pass
21        data=JitClass()
22        for name,thetype in spec: setattr(data, name, getattr(self,name))
23        return data
24
25jit=numba.jit(nopython=True, nogil=True, error_model='numpy', fastmath=True)
Note: See TracBrowser for help on using the repository browser.