Ignore:
Timestamp:
02/08/19 08:10:04 (6 years ago)
Author:
dubos
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • codes/icosagcm/devel/Python/dynamico/dev/numba.py

    r804 r805  
    66import numba 
    77 
    8 def store_data(obj,*args): 
    9     """Returns a jitclass instance containing attributes copied from obj. *args is of the form type,names,type,names ... where names is a string 'attr1 attr2 attr3' containing space-separated names of attributes of obj. Those attributes are declared to numba with the type preceding them. The result of store_data can be used as argument to a @jit function.""" 
    10     spec = [] 
    11     for item in args: 
    12         if isinstance(item,str):  
    13             spec = spec + [(name,thetype) for name in item.split(' ')] 
    14         else: 
    15             thetype=item 
    16     @numba.jitclass(spec) 
    17     class JitClass(object): 
    18         def __init__(self): pass 
    19     data=JitClass() 
    20     for name,thetype in spec: setattr(data, name, getattr(obj,name)) 
    21     return data 
     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 
    2224 
    2325jit=numba.jit(nopython=True, nogil=True, error_model='numpy', fastmath=True) 
Note: See TracChangeset for help on using the changeset viewer.