Changeset 594


Ignore:
Timestamp:
Nov 18, 2004, 10:53:59 PM (20 years ago)
Author:
steve
Message:

Working on sparse. Sparse_CSR * vertor works, Sparse_CSR * matrix doesn't.

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/sparse.py

    r587 r594  
    302302            print 'FIXME: Only Numeric types implemented so far'
    303303
    304 
    305 ##        R = csr_mv(self,B)
    306        
    307         #Assume numeric types from now on
     304        return csr_mv(self,B)
     305
     306
     307
     308"""
     309Setup for C extensions
     310"""
     311
     312
     313def csr_mv(self, B):
     314
     315    from Numeric import zeros, Float
     316
     317
     318    #Assume numeric types from now on
    308319       
    309         if len(B.shape) == 0:
    310             #Scalar - use __rmul__ method
    311             R = B*self
    312            
    313         elif len(B.shape) == 1:
    314             #Vector
    315             assert B.shape[0] == self.N, 'Mismatching dimensions'
    316 
    317             R = zeros(self.M, Float) #Result
    318            
    319             #Multiply nonzero elements
    320             for i in range(self.M):
    321                 for ckey in range(self.row_ptr[i],self.row_ptr[i+1]):
    322                     j = self.colind[ckey]
    323                     R[i] += self.data[ckey]*B[j]           
    324 
    325         elif len(B.shape) == 2:
    326        
    327             R = zeros((self.M, B.shape[1]), Float) #Result matrix
    328 
    329             #Multiply nonzero elements
    330 
    331             for col in range(R.shape[1]):
    332                 #For each column
    333                 for i in range(self.M):
    334                     for ckey in range(self.row_ptr[i],self.row_ptr[i+1]):
    335                         j = self.colind[ckey]
    336                         R[i, col] += self.data[ckey]*B[j,col]           
    337 
    338         else:
    339             raise ValueError, 'Dimension too high: d=%d' %len(B.shape)
    340 
    341         return R
    342 
    343 def csr_mv_python(self, B):
    344 
    345     from Numeric import zeros, Float
    346    
    347     if len(B.shape) == 1:
     320    if len(B.shape) == 0:
     321        #Scalar - use __rmul__ method
     322        R = B*self
     323       
     324    elif len(B.shape) == 1:
    348325        #Vector
    349326        assert B.shape[0] == self.N, 'Mismatching dimensions'
    350 
     327       
    351328        R = zeros(self.M, Float) #Result
    352            
     329       
    353330        #Multiply nonzero elements
    354331        for i in range(self.M):
     
    356333                j = self.colind[ckey]
    357334                R[i] += self.data[ckey]*B[j]           
    358 
    359         return R
     335               
     336    elif len(B.shape) == 2:
     337       
     338        R = zeros((self.M, B.shape[1]), Float) #Result matrix
     339       
     340        #Multiply nonzero elements
     341       
     342        for col in range(R.shape[1]):
     343            #For each column
     344            for i in range(self.M):
     345                for ckey in range(self.row_ptr[i],self.row_ptr[i+1]):
     346                    j = self.colind[ckey]
     347                    R[i, col] += self.data[ckey]*B[j,col]           
     348                   
    360349    else:
    361350        raise ValueError, 'Dimension too high: d=%d' %len(B.shape)
    362 
     351   
    363352    return R
    364353
     
    367356if compile.can_use_C_extension('sparse_ext.c'):
    368357    #Replace python version with c implementations     
    369     from sparse_ext import csr_mv
     358    from sparse_ext import csr_mv as csr_mv
    370359
    371360
     
    428417
    429418    print C.todense()
     419
     420    C = Sparse_CSR(C)
     421
     422    y = C*[6,14,4]
     423
     424    print y
     425
     426    y2 = C*[[6,4],[4,28],[4,8]]
     427
     428    print y2
  • inundation/ga/storm_surge/pyvolution/sparse_ext.c

    r589 r594  
    1616int _csr_mv(int M,
    1717            double* data,
    18             int* colind,
    19             int* row_ptr,
     18            long* colind,
     19            long* row_ptr,
    2020            double* x,
    2121            double* y) {
    2222               
    23   int i, j, ckey;
     23  long i, j, ckey;
    2424
    2525  for (i=0; i<M; i++ )
     
    4040 
    4141  PyObject *csr_sparse, // input sparse matrix
    42     *R;                 // output wrapped vector
     42    *xin, *R;                 // output wrapped vector
    4343 
    4444  PyArrayObject
     
    4848    *x,               //Input vector array
    4949    *y;               //Return vector array
     50
     51  double *xdata;
    5052   
    5153  int dimensions[1], M, err;
    5254 
    5355  // Convert Python arguments to C 
    54   if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &x))
     56  if (!PyArg_ParseTuple(args, "OO", &csr_sparse, &xin))
    5557    return NULL;
     58
     59  x = (PyArrayObject*) PyArray_ContiguousFromObject(xin,PyArray_DOUBLE,1,2);
     60  if (!x)
     61    return NULL;
     62
     63/*   printf("x.nd = %i\n",x->nd); */
     64/*   printf("x.descr->type_num = %i %i\n",x->descr->type_num,PyArray_LONG); */
     65/*   printf("x.dimensions[0] = %i\n",x->dimensions[0]); */
     66/*   printf("x.data[0] = %g\n",((double*) x->data)[0]); */
     67/*   printf("x.data[1] = %g\n",((double*) x->data)[1]); */
     68/*   printf("x.data[2] = %g\n",((double*) x->data)[2]); */
     69/*   printf("x.data[3] = %g\n",((double*) x->data)[3]); */
     70/*   printf("x.data[4] = %g\n",((double*) x->data)[4]); */
     71/*   printf("x.data[5] = %g\n",((double*) x->data)[5]); */
     72
     73 
    5674
    5775
     
    6179    return NULL; 
    6280
     81
    6382  colind = (PyArrayObject*)
    6483    PyObject_GetAttrString(csr_sparse, "colind");
    6584  if (!colind) return NULL;   
    66  
     85
    6786  row_ptr = (PyArrayObject*)
    6887    PyObject_GetAttrString(csr_sparse, "row_ptr");   
     
    7796  err = _csr_mv(M,
    7897                (double*) data -> data,
    79                 (int*)    colind -> data,
    80                 (int*)    row_ptr -> data,
     98                (long*)    colind -> data,
     99                (long*)    row_ptr -> data,
    81100                (double*) x -> data,
    82101                (double*) y -> data);     
  • inundation/ga/storm_surge/pyvolution/test_cg_solve.py

    r586 r594  
    8181        """Standard 2d laplacian"""
    8282       
    83         n = 50
    84         m = 50
     83        n = 100
     84        m = 100
    8585
    8686        A = Sparse(m*n, m*n)
     
    106106        #print 'finish covert'
    107107        b = A*xe
    108         x = conjugate_gradient(A,b,b,iprint=10)
     108        x = conjugate_gradient(A,b,b,iprint=20)
    109109
    110110        assert allclose(x,xe)
Note: See TracChangeset for help on using the changeset viewer.