Changeset 932


Ignore:
Timestamp:
07/02/19 15:05:16 (5 years ago)
Author:
dubos
Message:

devel/Python : automatic inference of Numba spec - signature is now obsolete

Location:
codes/icosagcm/devel/Python/dynamico/dev
Files:
2 edited

Legend:

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

    r931 r932  
    99 
    1010class MeshData(numba.NumbaData): 
    11     signature=(int64,      'primal_num dual_num edge_num', 
    12                int32[:],   'primal_deg left right', 
    13                int32[:,:], 'primal_edge primal_ne', 
    14                float64[:], 'le_de le de lon_e lat_e lon_v lat_v Ai Av') 
    1511    def to_dynamico(self): 
    1612        max_primal_deg, max_dual_deg, max_trisk_deg = [x.shape[1] for x in self.primal_edge, self.dual_edge, self.trisk] 
  • codes/icosagcm/devel/Python/dynamico/dev/numba.py

    r931 r932  
    1010    def data(self): 
    1111        """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        cls = self.__class__.__name__ 
    1213        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 
     14        for name in dir(self): 
     15            attr = getattr(self,name) 
     16            if not name.startswith('__') and not callable(attr): 
     17                tp = None 
     18                if isinstance(attr, int): tp=int64 
     19                if isinstance(attr, float): tp=float64 
     20                if isinstance(attr, np.ndarray): 
     21                    dtype=attr.dtype 
     22                    if dtype == np.int32 : dtype=int32 
     23                    elif dtype == np.float64 : dtype=float64 
     24                    else: dtype=None 
     25                    if dtype is None: print('Unknown dtype ', attr.dtype) 
     26                         
     27                    if   len(attr.shape)==1 : tp = dtype[:] 
     28                    elif len(attr.shape)==2 : tp = dtype[:,:] 
     29                    elif len(attr.shape)==3 : tp = dtype[:,:,:] 
     30                    else: print('%s.%s is a numpy array with unsupported rank >3'%(cls,name)) 
     31                         
     32                if tp is None: 
     33                    print('Type of attribute %s.%s is not recognized'%(cls,name), type(attr)) 
     34                else: 
     35                    spec.append( (name, tp) ) 
     36         
    1837        @numba.jitclass(spec) 
    1938        class JitClass(object): 
    2039            def __init__(self): pass 
     40 
    2141        data=JitClass() 
    2242        for name,thetype in spec:  
    23             print( 'Copying to %s.%s : '%(self.__class__.__name__, name), type(getattr(self,name))) 
     43            print( 'Making %s.%s available @jit functions : '%(self.__class__.__name__, name), type(getattr(self,name))) 
    2444            setattr(data, name, getattr(self,name)) 
    2545        return data 
Note: See TracChangeset for help on using the changeset viewer.