173f4d377SMatthew Knepley /*$Id: aij.c,v 1.385 2001/09/07 20:09:22 bsmith Exp $*/ 2d5d45c9bSBarry Smith /* 33369ce9aSBarry Smith Defines the basic matrix operations for the AIJ (compressed row) 4d5d45c9bSBarry Smith matrix storage format. 5d5d45c9bSBarry Smith */ 63369ce9aSBarry Smith 79e070d67SMatthew Knepley #include "src/mat/impls/aij/seq/aij.h" /*I "petscmat.h" I*/ 8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 9f5eb4b81SSatish Balay #include "src/inline/spops.h" 108d195f9aSBarry Smith #include "src/inline/dot.h" 110a835dfdSSatish Balay #include "petscbt.h" 1217ab2063SBarry Smith 13a2ce50c7SBarry Smith 14ca44d042SBarry Smith EXTERN int MatToSymmetricIJ_SeqAIJ(int,int*,int*,int,int,int**,int**); 1517ab2063SBarry Smith 164a2ae208SSatish Balay #undef __FUNCT__ 174a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ" 1836db0b34SBarry Smith int MatGetRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *m,int **ia,int **ja,PetscTruth *done) 1917ab2063SBarry Smith { 20416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 216945ee14SBarry Smith int ierr,i,ishift; 2217ab2063SBarry Smith 233a40ed3dSBarry Smith PetscFunctionBegin; 2431625ec5SSatish Balay *m = A->m; 253a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 266945ee14SBarry Smith ishift = a->indexshift; 2753e63a63SBarry Smith if (symmetric && !A->structurally_symmetric) { 28273d9f13SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 296945ee14SBarry Smith } else if (oshift == 0 && ishift == -1) { 30435da068SBarry Smith int nz = a->i[A->m] - 1; 313b2fbd54SBarry Smith /* malloc space and subtract 1 from i and j indices */ 32b0a32e0cSBarry Smith ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr); 33b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr); 343b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] - 1; 35273d9f13SBarry Smith for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] - 1; 366945ee14SBarry Smith } else if (oshift == 1 && ishift == 0) { 37435da068SBarry Smith int nz = a->i[A->m]; 383b2fbd54SBarry Smith /* malloc space and add 1 to i and j indices */ 391bc30dd5SBarry Smith ierr = PetscMalloc((A->m+1)*sizeof(int),ia);CHKERRQ(ierr); 40b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),ja);CHKERRQ(ierr); 413b2fbd54SBarry Smith for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1; 42273d9f13SBarry Smith for (i=0; i<A->m+1; i++) (*ia)[i] = a->i[i] + 1; 436945ee14SBarry Smith } else { 446945ee14SBarry Smith *ia = a->i; *ja = a->j; 45a2ce50c7SBarry Smith } 463a40ed3dSBarry Smith PetscFunctionReturn(0); 47a2744918SBarry Smith } 48a2744918SBarry Smith 494a2ae208SSatish Balay #undef __FUNCT__ 504a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ" 5136db0b34SBarry Smith int MatRestoreRowIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done) 526945ee14SBarry Smith { 536945ee14SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 54606d414cSSatish Balay int ishift = a->indexshift,ierr; 556945ee14SBarry Smith 563a40ed3dSBarry Smith PetscFunctionBegin; 573a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 5853e63a63SBarry Smith if ((symmetric && !A->structurally_symmetric) || (oshift == 0 && ishift == -1) || (oshift == 1 && ishift == 0)) { 59606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 60606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 61bcd2baecSBarry Smith } 623a40ed3dSBarry Smith PetscFunctionReturn(0); 6317ab2063SBarry Smith } 6417ab2063SBarry Smith 654a2ae208SSatish Balay #undef __FUNCT__ 664a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ" 6736db0b34SBarry Smith int MatGetColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *nn,int **ia,int **ja,PetscTruth *done) 683b2fbd54SBarry Smith { 693b2fbd54SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 70a93ec695SBarry Smith int ierr,i,ishift = a->indexshift,*collengths,*cia,*cja,n = A->n,m = A->m; 71a93ec695SBarry Smith int nz = a->i[m]+ishift,row,*jj,mr,col; 723b2fbd54SBarry Smith 733a40ed3dSBarry Smith PetscFunctionBegin; 743b2fbd54SBarry Smith *nn = A->n; 753a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 763b2fbd54SBarry Smith if (symmetric) { 77273d9f13SBarry Smith ierr = MatToSymmetricIJ_SeqAIJ(A->m,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr); 783b2fbd54SBarry Smith } else { 79b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&collengths);CHKERRQ(ierr); 80549d3d68SSatish Balay ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr); 81b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(int),&cia);CHKERRQ(ierr); 82b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),&cja);CHKERRQ(ierr); 833b2fbd54SBarry Smith jj = a->j; 843b2fbd54SBarry Smith for (i=0; i<nz; i++) { 853b2fbd54SBarry Smith collengths[jj[i] + ishift]++; 863b2fbd54SBarry Smith } 873b2fbd54SBarry Smith cia[0] = oshift; 883b2fbd54SBarry Smith for (i=0; i<n; i++) { 893b2fbd54SBarry Smith cia[i+1] = cia[i] + collengths[i]; 903b2fbd54SBarry Smith } 91549d3d68SSatish Balay ierr = PetscMemzero(collengths,n*sizeof(int));CHKERRQ(ierr); 923b2fbd54SBarry Smith jj = a->j; 93a93ec695SBarry Smith for (row=0; row<m; row++) { 94a93ec695SBarry Smith mr = a->i[row+1] - a->i[row]; 95a93ec695SBarry Smith for (i=0; i<mr; i++) { 963b2fbd54SBarry Smith col = *jj++ + ishift; 973b2fbd54SBarry Smith cja[cia[col] + collengths[col]++ - oshift] = row + oshift; 983b2fbd54SBarry Smith } 993b2fbd54SBarry Smith } 100606d414cSSatish Balay ierr = PetscFree(collengths);CHKERRQ(ierr); 1013b2fbd54SBarry Smith *ia = cia; *ja = cja; 1023b2fbd54SBarry Smith } 1033a40ed3dSBarry Smith PetscFunctionReturn(0); 1043b2fbd54SBarry Smith } 1053b2fbd54SBarry Smith 1064a2ae208SSatish Balay #undef __FUNCT__ 1074a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ" 10836db0b34SBarry Smith int MatRestoreColumnIJ_SeqAIJ(Mat A,int oshift,PetscTruth symmetric,int *n,int **ia,int **ja,PetscTruth *done) 1093b2fbd54SBarry Smith { 110606d414cSSatish Balay int ierr; 111606d414cSSatish Balay 1123a40ed3dSBarry Smith PetscFunctionBegin; 1133a40ed3dSBarry Smith if (!ia) PetscFunctionReturn(0); 1143b2fbd54SBarry Smith 115606d414cSSatish Balay ierr = PetscFree(*ia);CHKERRQ(ierr); 116606d414cSSatish Balay ierr = PetscFree(*ja);CHKERRQ(ierr); 1173b2fbd54SBarry Smith 1183a40ed3dSBarry Smith PetscFunctionReturn(0); 1193b2fbd54SBarry Smith } 1203b2fbd54SBarry Smith 121227d817aSBarry Smith #define CHUNKSIZE 15 12217ab2063SBarry Smith 1234a2ae208SSatish Balay #undef __FUNCT__ 1244a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ" 12587828ca2SBarry Smith int MatSetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v,InsertMode is) 12617ab2063SBarry Smith { 127416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 128416022c9SBarry Smith int *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,sorted = a->sorted; 129273d9f13SBarry Smith int *imax = a->imax,*ai = a->i,*ailen = a->ilen; 130549d3d68SSatish Balay int *aj = a->j,nonew = a->nonew,shift = a->indexshift,ierr; 13187828ca2SBarry Smith PetscScalar *ap,value,*aa = a->a; 13236db0b34SBarry Smith PetscTruth ignorezeroentries = ((a->ignorezeroentries && is == ADD_VALUES) ? PETSC_TRUE:PETSC_FALSE); 133273d9f13SBarry Smith PetscTruth roworiented = a->roworiented; 13417ab2063SBarry Smith 1353a40ed3dSBarry Smith PetscFunctionBegin; 13617ab2063SBarry Smith for (k=0; k<m; k++) { /* loop over added rows */ 137416022c9SBarry Smith row = im[k]; 1385ef9f2a5SBarry Smith if (row < 0) continue; 139aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 140273d9f13SBarry Smith if (row >= A->m) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",row,A->m); 1413b2fbd54SBarry Smith #endif 14217ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 14317ab2063SBarry Smith rmax = imax[row]; nrow = ailen[row]; 144416022c9SBarry Smith low = 0; 14517ab2063SBarry Smith for (l=0; l<n; l++) { /* loop over added columns */ 1465ef9f2a5SBarry Smith if (in[l] < 0) continue; 147aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 148273d9f13SBarry Smith if (in[l] >= A->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[l],A->n); 1493b2fbd54SBarry Smith #endif 1504b0e389bSBarry Smith col = in[l] - shift; 1514b0e389bSBarry Smith if (roworiented) { 1525ef9f2a5SBarry Smith value = v[l + k*n]; 153bef8e0ddSBarry Smith } else { 1544b0e389bSBarry Smith value = v[k + l*m]; 1554b0e389bSBarry Smith } 15636db0b34SBarry Smith if (value == 0.0 && ignorezeroentries) continue; 15736db0b34SBarry Smith 158416022c9SBarry Smith if (!sorted) low = 0; high = nrow; 159416022c9SBarry Smith while (high-low > 5) { 160416022c9SBarry Smith t = (low+high)/2; 161416022c9SBarry Smith if (rp[t] > col) high = t; 162416022c9SBarry Smith else low = t; 16317ab2063SBarry Smith } 164416022c9SBarry Smith for (i=low; i<high; i++) { 16517ab2063SBarry Smith if (rp[i] > col) break; 16617ab2063SBarry Smith if (rp[i] == col) { 167416022c9SBarry Smith if (is == ADD_VALUES) ap[i] += value; 16817ab2063SBarry Smith else ap[i] = value; 16917ab2063SBarry Smith goto noinsert; 17017ab2063SBarry Smith } 17117ab2063SBarry Smith } 172c2653b3dSLois Curfman McInnes if (nonew == 1) goto noinsert; 17329bbc08cSBarry Smith else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix",row,col); 17417ab2063SBarry Smith if (nrow >= rmax) { 17517ab2063SBarry Smith /* there is no extra room in row, therefore enlarge */ 176273d9f13SBarry Smith int new_nz = ai[A->m] + CHUNKSIZE,len,*new_i,*new_j; 17787828ca2SBarry Smith PetscScalar *new_a; 17817ab2063SBarry Smith 17929bbc08cSBarry Smith if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%d,%d) in the matrix requiring new malloc()",row,col); 18096854ed6SLois Curfman McInnes 18117ab2063SBarry Smith /* malloc new storage space */ 18287828ca2SBarry Smith len = new_nz*(sizeof(int)+sizeof(PetscScalar))+(A->m+1)*sizeof(int); 183b0a32e0cSBarry Smith ierr = PetscMalloc(len,&new_a);CHKERRQ(ierr); 18417ab2063SBarry Smith new_j = (int*)(new_a + new_nz); 18517ab2063SBarry Smith new_i = new_j + new_nz; 18617ab2063SBarry Smith 18717ab2063SBarry Smith /* copy over old data into new slots */ 18817ab2063SBarry Smith for (ii=0; ii<row+1; ii++) {new_i[ii] = ai[ii];} 189273d9f13SBarry Smith for (ii=row+1; ii<A->m+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} 190549d3d68SSatish Balay ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); 191416022c9SBarry Smith len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); 192549d3d68SSatish Balay ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow,len*sizeof(int));CHKERRQ(ierr); 19387828ca2SBarry Smith ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 19487828ca2SBarry Smith ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow,len*sizeof(PetscScalar));CHKERRQ(ierr); 19517ab2063SBarry Smith /* free up old matrix storage */ 196606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); 197606d414cSSatish Balay if (!a->singlemalloc) { 198606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); 199606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); 200606d414cSSatish Balay } 201416022c9SBarry Smith aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; 202f1e2ffcdSBarry Smith a->singlemalloc = PETSC_TRUE; 20317ab2063SBarry Smith 20417ab2063SBarry Smith rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 205416022c9SBarry Smith rmax = imax[row] = imax[row] + CHUNKSIZE; 20687828ca2SBarry Smith PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); 207416022c9SBarry Smith a->maxnz += CHUNKSIZE; 208b810aeb4SBarry Smith a->reallocs++; 20917ab2063SBarry Smith } 210416022c9SBarry Smith N = nrow++ - 1; a->nz++; 211416022c9SBarry Smith /* shift up all the later entries in this row */ 212416022c9SBarry Smith for (ii=N; ii>=i; ii--) { 21317ab2063SBarry Smith rp[ii+1] = rp[ii]; 21417ab2063SBarry Smith ap[ii+1] = ap[ii]; 21517ab2063SBarry Smith } 21617ab2063SBarry Smith rp[i] = col; 21717ab2063SBarry Smith ap[i] = value; 21817ab2063SBarry Smith noinsert:; 219416022c9SBarry Smith low = i + 1; 22017ab2063SBarry Smith } 22117ab2063SBarry Smith ailen[row] = nrow; 22217ab2063SBarry Smith } 2233a40ed3dSBarry Smith PetscFunctionReturn(0); 22417ab2063SBarry Smith } 22517ab2063SBarry Smith 2264a2ae208SSatish Balay #undef __FUNCT__ 2274a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ" 22887828ca2SBarry Smith int MatGetValues_SeqAIJ(Mat A,int m,int *im,int n,int *in,PetscScalar *v) 2297eb43aa7SLois Curfman McInnes { 2307eb43aa7SLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 231b49de8d1SLois Curfman McInnes int *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j; 2327eb43aa7SLois Curfman McInnes int *ai = a->i,*ailen = a->ilen,shift = a->indexshift; 23387828ca2SBarry Smith PetscScalar *ap,*aa = a->a,zero = 0.0; 2347eb43aa7SLois Curfman McInnes 2353a40ed3dSBarry Smith PetscFunctionBegin; 2367eb43aa7SLois Curfman McInnes for (k=0; k<m; k++) { /* loop over rows */ 2377eb43aa7SLois Curfman McInnes row = im[k]; 23829bbc08cSBarry Smith if (row < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",row); 239273d9f13SBarry Smith if (row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: %d",row); 2407eb43aa7SLois Curfman McInnes rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; 2417eb43aa7SLois Curfman McInnes nrow = ailen[row]; 2427eb43aa7SLois Curfman McInnes for (l=0; l<n; l++) { /* loop over columns */ 24329bbc08cSBarry Smith if (in[l] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",in[l]); 244273d9f13SBarry Smith if (in[l] >= A->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: %d",in[l]); 2457eb43aa7SLois Curfman McInnes col = in[l] - shift; 2467eb43aa7SLois Curfman McInnes high = nrow; low = 0; /* assume unsorted */ 2477eb43aa7SLois Curfman McInnes while (high-low > 5) { 2487eb43aa7SLois Curfman McInnes t = (low+high)/2; 2497eb43aa7SLois Curfman McInnes if (rp[t] > col) high = t; 2507eb43aa7SLois Curfman McInnes else low = t; 2517eb43aa7SLois Curfman McInnes } 2527eb43aa7SLois Curfman McInnes for (i=low; i<high; i++) { 2537eb43aa7SLois Curfman McInnes if (rp[i] > col) break; 2547eb43aa7SLois Curfman McInnes if (rp[i] == col) { 255b49de8d1SLois Curfman McInnes *v++ = ap[i]; 2567eb43aa7SLois Curfman McInnes goto finished; 2577eb43aa7SLois Curfman McInnes } 2587eb43aa7SLois Curfman McInnes } 259b49de8d1SLois Curfman McInnes *v++ = zero; 2607eb43aa7SLois Curfman McInnes finished:; 2617eb43aa7SLois Curfman McInnes } 2627eb43aa7SLois Curfman McInnes } 2633a40ed3dSBarry Smith PetscFunctionReturn(0); 2647eb43aa7SLois Curfman McInnes } 2657eb43aa7SLois Curfman McInnes 26617ab2063SBarry Smith 2674a2ae208SSatish Balay #undef __FUNCT__ 2684a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary" 269b0a32e0cSBarry Smith int MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer) 27017ab2063SBarry Smith { 271416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 272416022c9SBarry Smith int i,fd,*col_lens,ierr; 27317ab2063SBarry Smith 2743a40ed3dSBarry Smith PetscFunctionBegin; 275b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 276b0a32e0cSBarry Smith ierr = PetscMalloc((4+A->m)*sizeof(int),&col_lens);CHKERRQ(ierr); 277552e946dSBarry Smith col_lens[0] = MAT_FILE_COOKIE; 278273d9f13SBarry Smith col_lens[1] = A->m; 279273d9f13SBarry Smith col_lens[2] = A->n; 280416022c9SBarry Smith col_lens[3] = a->nz; 281416022c9SBarry Smith 282416022c9SBarry Smith /* store lengths of each row and write (including header) to file */ 283273d9f13SBarry Smith for (i=0; i<A->m; i++) { 284416022c9SBarry Smith col_lens[4+i] = a->i[i+1] - a->i[i]; 28517ab2063SBarry Smith } 286273d9f13SBarry Smith ierr = PetscBinaryWrite(fd,col_lens,4+A->m,PETSC_INT,1);CHKERRQ(ierr); 287606d414cSSatish Balay ierr = PetscFree(col_lens);CHKERRQ(ierr); 288416022c9SBarry Smith 289416022c9SBarry Smith /* store column indices (zero start index) */ 290416022c9SBarry Smith if (a->indexshift) { 291416022c9SBarry Smith for (i=0; i<a->nz; i++) a->j[i]--; 29217ab2063SBarry Smith } 2930752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,0);CHKERRQ(ierr); 294416022c9SBarry Smith if (a->indexshift) { 295416022c9SBarry Smith for (i=0; i<a->nz; i++) a->j[i]++; 29617ab2063SBarry Smith } 297416022c9SBarry Smith 298416022c9SBarry Smith /* store nonzero values */ 2990752156aSBarry Smith ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,0);CHKERRQ(ierr); 3003a40ed3dSBarry Smith PetscFunctionReturn(0); 30117ab2063SBarry Smith } 302416022c9SBarry Smith 3034a2ae208SSatish Balay #undef __FUNCT__ 3044a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII" 305b0a32e0cSBarry Smith int MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer) 306416022c9SBarry Smith { 307416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 308fb9695e5SSatish Balay int ierr,i,j,m = A->m,shift = a->indexshift; 309fb9695e5SSatish Balay char *name; 310f3ef73ceSBarry Smith PetscViewerFormat format; 31117ab2063SBarry Smith 3123a40ed3dSBarry Smith PetscFunctionBegin; 313435da068SBarry Smith ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr); 314b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 315fb9695e5SSatish Balay if (format == PETSC_VIEWER_ASCII_INFO_LONG || format == PETSC_VIEWER_ASCII_INFO) { 3166831982aSBarry Smith if (a->inode.size) { 317b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"using I-node routines: found %d nodes, limit used is %d\n",a->inode.node_count,a->inode.limit);CHKERRQ(ierr); 3186831982aSBarry Smith } else { 319b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node routines\n");CHKERRQ(ierr); 3206831982aSBarry Smith } 321fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_MATLAB) { 322d00d2cf4SBarry Smith int nofinalvalue = 0; 323273d9f13SBarry Smith if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->n-!shift)) { 324d00d2cf4SBarry Smith nofinalvalue = 1; 325d00d2cf4SBarry Smith } 326b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 327b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %d %d \n",m,A->n);CHKERRQ(ierr); 328b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %d \n",a->nz);CHKERRQ(ierr); 329b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%d,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr); 330b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr); 33117ab2063SBarry Smith 33217ab2063SBarry Smith for (i=0; i<m; i++) { 333416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 334aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 335b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 33617ab2063SBarry Smith #else 337b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr); 33817ab2063SBarry Smith #endif 33917ab2063SBarry Smith } 34017ab2063SBarry Smith } 341d00d2cf4SBarry Smith if (nofinalvalue) { 342b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"%d %d %18.16e\n",m,A->n,0.0);CHKERRQ(ierr); 343d00d2cf4SBarry Smith } 344fb9695e5SSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr); 345b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 346fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_COMMON) { 347b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 34844cd7ae7SLois Curfman McInnes for (i=0; i<m; i++) { 349b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr); 35044cd7ae7SLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 351aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 35236db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) { 353b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 35436db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) { 355b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 35636db0b34SBarry Smith } else if (PetscRealPart(a->a[j]) != 0.0) { 357b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 3586831982aSBarry Smith } 35944cd7ae7SLois Curfman McInnes #else 360b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);} 36144cd7ae7SLois Curfman McInnes #endif 36244cd7ae7SLois Curfman McInnes } 363b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 36444cd7ae7SLois Curfman McInnes } 365b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 366fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_SYMMODU) { 367496be53dSLois Curfman McInnes int nzd=0,fshift=1,*sptr; 368b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 369b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&sptr);CHKERRQ(ierr); 370496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 371496be53dSLois Curfman McInnes sptr[i] = nzd+1; 372496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 373496be53dSLois Curfman McInnes if (a->j[j] >= i) { 374aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 37536db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++; 376496be53dSLois Curfman McInnes #else 377496be53dSLois Curfman McInnes if (a->a[j] != 0.0) nzd++; 378496be53dSLois Curfman McInnes #endif 379496be53dSLois Curfman McInnes } 380496be53dSLois Curfman McInnes } 381496be53dSLois Curfman McInnes } 3822e44a96cSLois Curfman McInnes sptr[m] = nzd+1; 383b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %d\n\n",m,nzd);CHKERRQ(ierr); 3842e44a96cSLois Curfman McInnes for (i=0; i<m+1; i+=6) { 385b0a32e0cSBarry Smith if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);} 386b0a32e0cSBarry Smith else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);} 387b0a32e0cSBarry Smith else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);} 388b0a32e0cSBarry Smith else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d %d\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);} 389b0a32e0cSBarry Smith else if (i<m) {ierr = PetscViewerASCIIPrintf(viewer," %d %d\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);} 390b0a32e0cSBarry Smith else {ierr = PetscViewerASCIIPrintf(viewer," %d\n",sptr[i]);CHKERRQ(ierr);} 391496be53dSLois Curfman McInnes } 392b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 393606d414cSSatish Balay ierr = PetscFree(sptr);CHKERRQ(ierr); 394496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 395496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 396b0a32e0cSBarry Smith if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %d ",a->j[j]+fshift);CHKERRQ(ierr);} 397496be53dSLois Curfman McInnes } 398b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 399496be53dSLois Curfman McInnes } 400b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 401496be53dSLois Curfman McInnes for (i=0; i<m; i++) { 402496be53dSLois Curfman McInnes for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 403496be53dSLois Curfman McInnes if (a->j[j] >= i) { 404aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 40536db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) { 406b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4076831982aSBarry Smith } 408496be53dSLois Curfman McInnes #else 409b0a32e0cSBarry Smith if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);} 410496be53dSLois Curfman McInnes #endif 411496be53dSLois Curfman McInnes } 412496be53dSLois Curfman McInnes } 413b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 414496be53dSLois Curfman McInnes } 415b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 416fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_DENSE) { 41702594712SBarry Smith int cnt = 0,jcnt; 41887828ca2SBarry Smith PetscScalar value; 41902594712SBarry Smith 420b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 42102594712SBarry Smith for (i=0; i<m; i++) { 42202594712SBarry Smith jcnt = 0; 423273d9f13SBarry Smith for (j=0; j<A->n; j++) { 424e24b481bSBarry Smith if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) { 42502594712SBarry Smith value = a->a[cnt++]; 426e24b481bSBarry Smith jcnt++; 42702594712SBarry Smith } else { 42802594712SBarry Smith value = 0.0; 42902594712SBarry Smith } 430aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 431b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr); 43202594712SBarry Smith #else 433b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr); 43402594712SBarry Smith #endif 43502594712SBarry Smith } 436b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 43702594712SBarry Smith } 438b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 4393a40ed3dSBarry Smith } else { 440b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr); 44117ab2063SBarry Smith for (i=0; i<m; i++) { 442b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"row %d:",i);CHKERRQ(ierr); 443416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 444aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 44536db0b34SBarry Smith if (PetscImaginaryPart(a->a[j]) > 0.0) { 446b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g + %g i",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 44736db0b34SBarry Smith } else if (PetscImaginaryPart(a->a[j]) < 0.0) { 448b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g - %g i",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr); 4493a40ed3dSBarry Smith } else { 450b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr); 45117ab2063SBarry Smith } 45217ab2063SBarry Smith #else 453b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," %d %g ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr); 45417ab2063SBarry Smith #endif 45517ab2063SBarry Smith } 456b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr); 45717ab2063SBarry Smith } 458b0a32e0cSBarry Smith ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr); 45917ab2063SBarry Smith } 460b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 4613a40ed3dSBarry Smith PetscFunctionReturn(0); 462416022c9SBarry Smith } 463416022c9SBarry Smith 4644a2ae208SSatish Balay #undef __FUNCT__ 4654a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom" 466b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa) 467416022c9SBarry Smith { 468480ef9eaSBarry Smith Mat A = (Mat) Aa; 469416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 470273d9f13SBarry Smith int ierr,i,j,m = A->m,shift = a->indexshift,color; 47136db0b34SBarry Smith PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0; 472b0a32e0cSBarry Smith PetscViewer viewer; 473f3ef73ceSBarry Smith PetscViewerFormat format; 474cddf8d76SBarry Smith 4753a40ed3dSBarry Smith PetscFunctionBegin; 476480ef9eaSBarry Smith ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr); 477b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 47819bcc07fSBarry Smith 479b0a32e0cSBarry Smith ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr); 480416022c9SBarry Smith /* loop over matrix elements drawing boxes */ 4810513a670SBarry Smith 482fb9695e5SSatish Balay if (format != PETSC_VIEWER_DRAW_CONTOUR) { 4830513a670SBarry Smith /* Blue for negative, Cyan for zero and Red for positive */ 484b0a32e0cSBarry Smith color = PETSC_DRAW_BLUE; 485416022c9SBarry Smith for (i=0; i<m; i++) { 486cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 487416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 488cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 489aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 49036db0b34SBarry Smith if (PetscRealPart(a->a[j]) >= 0.) continue; 491cddf8d76SBarry Smith #else 492cddf8d76SBarry Smith if (a->a[j] >= 0.) continue; 493cddf8d76SBarry Smith #endif 494b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 495cddf8d76SBarry Smith } 496cddf8d76SBarry Smith } 497b0a32e0cSBarry Smith color = PETSC_DRAW_CYAN; 498cddf8d76SBarry Smith for (i=0; i<m; i++) { 499cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 500cddf8d76SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 501cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 502cddf8d76SBarry Smith if (a->a[j] != 0.) continue; 503b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 504cddf8d76SBarry Smith } 505cddf8d76SBarry Smith } 506b0a32e0cSBarry Smith color = PETSC_DRAW_RED; 507cddf8d76SBarry Smith for (i=0; i<m; i++) { 508cddf8d76SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 509cddf8d76SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 510cddf8d76SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 511aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 51236db0b34SBarry Smith if (PetscRealPart(a->a[j]) <= 0.) continue; 513cddf8d76SBarry Smith #else 514cddf8d76SBarry Smith if (a->a[j] <= 0.) continue; 515cddf8d76SBarry Smith #endif 516b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 517416022c9SBarry Smith } 518416022c9SBarry Smith } 5190513a670SBarry Smith } else { 5200513a670SBarry Smith /* use contour shading to indicate magnitude of values */ 5210513a670SBarry Smith /* first determine max of all nonzero values */ 5220513a670SBarry Smith int nz = a->nz,count; 523b0a32e0cSBarry Smith PetscDraw popup; 52436db0b34SBarry Smith PetscReal scale; 5250513a670SBarry Smith 5260513a670SBarry Smith for (i=0; i<nz; i++) { 5270513a670SBarry Smith if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]); 5280513a670SBarry Smith } 529b0a32e0cSBarry Smith scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv; 530b0a32e0cSBarry Smith ierr = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr); 531b0a32e0cSBarry Smith if (popup) {ierr = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);} 5320513a670SBarry Smith count = 0; 5330513a670SBarry Smith for (i=0; i<m; i++) { 5340513a670SBarry Smith y_l = m - i - 1.0; y_r = y_l + 1.0; 5350513a670SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 5360513a670SBarry Smith x_l = a->j[j] + shift; x_r = x_l + 1.0; 537b0a32e0cSBarry Smith color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(a->a[count])); 538b0a32e0cSBarry Smith ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr); 5390513a670SBarry Smith count++; 5400513a670SBarry Smith } 5410513a670SBarry Smith } 5420513a670SBarry Smith } 543480ef9eaSBarry Smith PetscFunctionReturn(0); 544480ef9eaSBarry Smith } 545cddf8d76SBarry Smith 5464a2ae208SSatish Balay #undef __FUNCT__ 5474a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw" 548b0a32e0cSBarry Smith int MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer) 549480ef9eaSBarry Smith { 550480ef9eaSBarry Smith int ierr; 551b0a32e0cSBarry Smith PetscDraw draw; 55236db0b34SBarry Smith PetscReal xr,yr,xl,yl,h,w; 553480ef9eaSBarry Smith PetscTruth isnull; 554480ef9eaSBarry Smith 555480ef9eaSBarry Smith PetscFunctionBegin; 556b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 557b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); 558480ef9eaSBarry Smith if (isnull) PetscFunctionReturn(0); 559480ef9eaSBarry Smith 560480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr); 561273d9f13SBarry Smith xr = A->n; yr = A->m; h = yr/10.0; w = xr/10.0; 562480ef9eaSBarry Smith xr += w; yr += h; xl = -w; yl = -h; 563b0a32e0cSBarry Smith ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr); 564b0a32e0cSBarry Smith ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr); 565480ef9eaSBarry Smith ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr); 5663a40ed3dSBarry Smith PetscFunctionReturn(0); 567416022c9SBarry Smith } 568416022c9SBarry Smith 5694a2ae208SSatish Balay #undef __FUNCT__ 5704a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ" 571b0a32e0cSBarry Smith int MatView_SeqAIJ(Mat A,PetscViewer viewer) 572416022c9SBarry Smith { 573416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 574bcd2baecSBarry Smith int ierr; 5756831982aSBarry Smith PetscTruth issocket,isascii,isbinary,isdraw; 576416022c9SBarry Smith 5773a40ed3dSBarry Smith PetscFunctionBegin; 578b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr); 579b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 580fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 581fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 5820f5bd95cSBarry Smith if (issocket) { 58301820c62SBarry Smith if (a->indexshift) { 58429bbc08cSBarry Smith SETERRQ(1,"Can only socket send sparse matrix with 0 based indexing"); 58501820c62SBarry Smith } 586b0a32e0cSBarry Smith ierr = PetscViewerSocketPutSparse_Private(viewer,A->m,A->n,a->nz,a->a,a->i,a->j);CHKERRQ(ierr); 5870f5bd95cSBarry Smith } else if (isascii) { 5883a40ed3dSBarry Smith ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr); 5890f5bd95cSBarry Smith } else if (isbinary) { 5903a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr); 5910f5bd95cSBarry Smith } else if (isdraw) { 5923a40ed3dSBarry Smith ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr); 5935cd90555SBarry Smith } else { 59429bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name); 59517ab2063SBarry Smith } 5963a40ed3dSBarry Smith PetscFunctionReturn(0); 59717ab2063SBarry Smith } 59819bcc07fSBarry Smith 5993a7fca6bSBarry Smith EXTERN int Mat_AIJ_CheckInode(Mat,PetscTruth); 6009b9367d5SHong Zhang EXTERN int MatUseSuperLU_DIST_MPIAIJ(Mat); 6014a2ae208SSatish Balay #undef __FUNCT__ 6024a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ" 6038f6be9afSLois Curfman McInnes int MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode) 60417ab2063SBarry Smith { 605416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 60641c01911SSatish Balay int fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax,ierr; 607273d9f13SBarry Smith int m = A->m,*ip,N,*ailen = a->ilen,shift = a->indexshift,rmax = 0; 60887828ca2SBarry Smith PetscScalar *aa = a->a,*ap; 6099e070d67SMatthew Knepley #if defined(PETSC_HAVE_SUPERLUDIST) 6109b9367d5SHong Zhang PetscTruth flag; 6119e070d67SMatthew Knepley #endif 61217ab2063SBarry Smith 6133a40ed3dSBarry Smith PetscFunctionBegin; 6143a40ed3dSBarry Smith if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0); 61517ab2063SBarry Smith 61643ee02c3SBarry Smith if (m) rmax = ailen[0]; /* determine row with most nonzeros */ 61717ab2063SBarry Smith for (i=1; i<m; i++) { 618416022c9SBarry Smith /* move each row back by the amount of empty slots (fshift) before it*/ 61917ab2063SBarry Smith fshift += imax[i-1] - ailen[i-1]; 62094a9d846SBarry Smith rmax = PetscMax(rmax,ailen[i]); 62117ab2063SBarry Smith if (fshift) { 6220f5bd95cSBarry Smith ip = aj + ai[i] + shift; 6230f5bd95cSBarry Smith ap = aa + ai[i] + shift; 62417ab2063SBarry Smith N = ailen[i]; 62517ab2063SBarry Smith for (j=0; j<N; j++) { 62617ab2063SBarry Smith ip[j-fshift] = ip[j]; 62717ab2063SBarry Smith ap[j-fshift] = ap[j]; 62817ab2063SBarry Smith } 62917ab2063SBarry Smith } 63017ab2063SBarry Smith ai[i] = ai[i-1] + ailen[i-1]; 63117ab2063SBarry Smith } 63217ab2063SBarry Smith if (m) { 63317ab2063SBarry Smith fshift += imax[m-1] - ailen[m-1]; 63417ab2063SBarry Smith ai[m] = ai[m-1] + ailen[m-1]; 63517ab2063SBarry Smith } 63617ab2063SBarry Smith /* reset ilen and imax for each row */ 63717ab2063SBarry Smith for (i=0; i<m; i++) { 63817ab2063SBarry Smith ailen[i] = imax[i] = ai[i+1] - ai[i]; 63917ab2063SBarry Smith } 640416022c9SBarry Smith a->nz = ai[m] + shift; 64117ab2063SBarry Smith 64217ab2063SBarry Smith /* diagonals may have moved, so kill the diagonal pointers */ 643416022c9SBarry Smith if (fshift && a->diag) { 644606d414cSSatish Balay ierr = PetscFree(a->diag);CHKERRQ(ierr); 645b0a32e0cSBarry Smith PetscLogObjectMemory(A,-(m+1)*sizeof(int)); 646416022c9SBarry Smith a->diag = 0; 64717ab2063SBarry Smith } 648b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Matrix size: %d X %d; storage space: %d unneeded,%d used\n",m,A->n,fshift,a->nz); 649b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Number of mallocs during MatSetValues() is %d\n",a->reallocs); 650b0a32e0cSBarry Smith PetscLogInfo(A,"MatAssemblyEnd_SeqAIJ:Most nonzeros in any row is %d\n",rmax); 651dd5f02e7SSatish Balay a->reallocs = 0; 6524e220ebcSLois Curfman McInnes A->info.nz_unneeded = (double)fshift; 65336db0b34SBarry Smith a->rmax = rmax; 6544e220ebcSLois Curfman McInnes 65576dd722bSSatish Balay /* check out for identical nodes. If found, use inode functions */ 6563a7fca6bSBarry Smith ierr = Mat_AIJ_CheckInode(A,(PetscTruth)(!fshift));CHKERRQ(ierr); 6579b9367d5SHong Zhang #if defined(PETSC_HAVE_SUPERLUDIST) 6589b9367d5SHong Zhang ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu_dist",&flag);CHKERRQ(ierr); 6599b9367d5SHong Zhang if (flag) { ierr = MatUseSuperLU_DIST_MPIAIJ(A);CHKERRQ(ierr); } 6609b9367d5SHong Zhang #endif 6619b9367d5SHong Zhang 6623a40ed3dSBarry Smith PetscFunctionReturn(0); 66317ab2063SBarry Smith } 66417ab2063SBarry Smith 6654a2ae208SSatish Balay #undef __FUNCT__ 6664a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ" 6678f6be9afSLois Curfman McInnes int MatZeroEntries_SeqAIJ(Mat A) 66817ab2063SBarry Smith { 669416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 670549d3d68SSatish Balay int ierr; 6713a40ed3dSBarry Smith 6723a40ed3dSBarry Smith PetscFunctionBegin; 67387828ca2SBarry Smith ierr = PetscMemzero(a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr); 6743a40ed3dSBarry Smith PetscFunctionReturn(0); 67517ab2063SBarry Smith } 676416022c9SBarry Smith 6774a2ae208SSatish Balay #undef __FUNCT__ 6784a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ" 679e1311b90SBarry Smith int MatDestroy_SeqAIJ(Mat A) 68017ab2063SBarry Smith { 681416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 68282bf6240SBarry Smith int ierr; 683d5d45c9bSBarry Smith 6843a40ed3dSBarry Smith PetscFunctionBegin; 685aa482453SBarry Smith #if defined(PETSC_USE_LOG) 686b0a32e0cSBarry Smith PetscLogObjectState((PetscObject)A,"Rows=%d, Cols=%d, NZ=%d",A->m,A->n,a->nz); 68717ab2063SBarry Smith #endif 68836db0b34SBarry Smith if (a->freedata) { 689606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); 690606d414cSSatish Balay if (!a->singlemalloc) { 691606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); 692606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); 693606d414cSSatish Balay } 69436db0b34SBarry Smith } 695c38d4ed2SBarry Smith if (a->row) { 696c38d4ed2SBarry Smith ierr = ISDestroy(a->row);CHKERRQ(ierr); 697c38d4ed2SBarry Smith } 698c38d4ed2SBarry Smith if (a->col) { 699c38d4ed2SBarry Smith ierr = ISDestroy(a->col);CHKERRQ(ierr); 700c38d4ed2SBarry Smith } 701606d414cSSatish Balay if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);} 702606d414cSSatish Balay if (a->ilen) {ierr = PetscFree(a->ilen);CHKERRQ(ierr);} 703606d414cSSatish Balay if (a->imax) {ierr = PetscFree(a->imax);CHKERRQ(ierr);} 704273d9f13SBarry Smith if (a->idiag) {ierr = PetscFree(a->idiag);CHKERRQ(ierr);} 705606d414cSSatish Balay if (a->solve_work) {ierr = PetscFree(a->solve_work);CHKERRQ(ierr);} 706606d414cSSatish Balay if (a->inode.size) {ierr = PetscFree(a->inode.size);CHKERRQ(ierr);} 70782bf6240SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} 708606d414cSSatish Balay if (a->saved_values) {ierr = PetscFree(a->saved_values);CHKERRQ(ierr);} 709cc8ba8e1SBarry Smith if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);} 710606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 7113a40ed3dSBarry Smith PetscFunctionReturn(0); 71217ab2063SBarry Smith } 71317ab2063SBarry Smith 7144a2ae208SSatish Balay #undef __FUNCT__ 7154a2ae208SSatish Balay #define __FUNCT__ "MatCompress_SeqAIJ" 7168f6be9afSLois Curfman McInnes int MatCompress_SeqAIJ(Mat A) 71717ab2063SBarry Smith { 7183a40ed3dSBarry Smith PetscFunctionBegin; 7193a40ed3dSBarry Smith PetscFunctionReturn(0); 72017ab2063SBarry Smith } 72117ab2063SBarry Smith 7224a2ae208SSatish Balay #undef __FUNCT__ 7234a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ" 7248f6be9afSLois Curfman McInnes int MatSetOption_SeqAIJ(Mat A,MatOption op) 72517ab2063SBarry Smith { 726416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 7273a40ed3dSBarry Smith 7283a40ed3dSBarry Smith PetscFunctionBegin; 729a65d3064SKris Buschelman switch (op) { 730a65d3064SKris Buschelman case MAT_ROW_ORIENTED: 731a65d3064SKris Buschelman a->roworiented = PETSC_TRUE; 732a65d3064SKris Buschelman break; 733a65d3064SKris Buschelman case MAT_KEEP_ZEROED_ROWS: 734a65d3064SKris Buschelman a->keepzeroedrows = PETSC_TRUE; 735a65d3064SKris Buschelman break; 736a65d3064SKris Buschelman case MAT_COLUMN_ORIENTED: 737a65d3064SKris Buschelman a->roworiented = PETSC_FALSE; 738a65d3064SKris Buschelman break; 739a65d3064SKris Buschelman case MAT_COLUMNS_SORTED: 740a65d3064SKris Buschelman a->sorted = PETSC_TRUE; 741a65d3064SKris Buschelman break; 742a65d3064SKris Buschelman case MAT_COLUMNS_UNSORTED: 743a65d3064SKris Buschelman a->sorted = PETSC_FALSE; 744a65d3064SKris Buschelman break; 745a65d3064SKris Buschelman case MAT_NO_NEW_NONZERO_LOCATIONS: 746a65d3064SKris Buschelman a->nonew = 1; 747a65d3064SKris Buschelman break; 748a65d3064SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 749a65d3064SKris Buschelman a->nonew = -1; 750a65d3064SKris Buschelman break; 751a65d3064SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 752a65d3064SKris Buschelman a->nonew = -2; 753a65d3064SKris Buschelman break; 754a65d3064SKris Buschelman case MAT_YES_NEW_NONZERO_LOCATIONS: 755a65d3064SKris Buschelman a->nonew = 0; 756a65d3064SKris Buschelman break; 757a65d3064SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 758a65d3064SKris Buschelman a->ignorezeroentries = PETSC_TRUE; 759a65d3064SKris Buschelman break; 760a65d3064SKris Buschelman case MAT_USE_INODES: 761a65d3064SKris Buschelman a->inode.use = PETSC_TRUE; 762a65d3064SKris Buschelman break; 763a65d3064SKris Buschelman case MAT_DO_NOT_USE_INODES: 764a65d3064SKris Buschelman a->inode.use = PETSC_FALSE; 765a65d3064SKris Buschelman break; 766a65d3064SKris Buschelman case MAT_ROWS_SORTED: 767a65d3064SKris Buschelman case MAT_ROWS_UNSORTED: 768a65d3064SKris Buschelman case MAT_YES_NEW_DIAGONALS: 769a65d3064SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 770a65d3064SKris Buschelman case MAT_USE_HASH_TABLE: 771d03495bdSKris Buschelman case MAT_USE_SINGLE_PRECISION_SOLVES: 772b0a32e0cSBarry Smith PetscLogInfo(A,"MatSetOption_SeqAIJ:Option ignored\n"); 773a65d3064SKris Buschelman break; 774a65d3064SKris Buschelman case MAT_NO_NEW_DIAGONALS: 77529bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS"); 776a65d3064SKris Buschelman case MAT_INODE_LIMIT_1: 777a65d3064SKris Buschelman a->inode.limit = 1; 778a65d3064SKris Buschelman break; 779a65d3064SKris Buschelman case MAT_INODE_LIMIT_2: 780a65d3064SKris Buschelman a->inode.limit = 2; 781a65d3064SKris Buschelman break; 782a65d3064SKris Buschelman case MAT_INODE_LIMIT_3: 783a65d3064SKris Buschelman a->inode.limit = 3; 784a65d3064SKris Buschelman break; 785a65d3064SKris Buschelman case MAT_INODE_LIMIT_4: 786a65d3064SKris Buschelman a->inode.limit = 4; 787a65d3064SKris Buschelman break; 788a65d3064SKris Buschelman case MAT_INODE_LIMIT_5: 789a65d3064SKris Buschelman a->inode.limit = 5; 790a65d3064SKris Buschelman break; 791a65d3064SKris Buschelman default: 792a65d3064SKris Buschelman SETERRQ(PETSC_ERR_SUP,"unknown option"); 793a65d3064SKris Buschelman } 7943a40ed3dSBarry Smith PetscFunctionReturn(0); 79517ab2063SBarry Smith } 79617ab2063SBarry Smith 7974a2ae208SSatish Balay #undef __FUNCT__ 7984a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ" 7998f6be9afSLois Curfman McInnes int MatGetDiagonal_SeqAIJ(Mat A,Vec v) 80017ab2063SBarry Smith { 801416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8023a40ed3dSBarry Smith int i,j,n,shift = a->indexshift,ierr; 80387828ca2SBarry Smith PetscScalar *x,zero = 0.0; 80417ab2063SBarry Smith 8053a40ed3dSBarry Smith PetscFunctionBegin; 8063a40ed3dSBarry Smith ierr = VecSet(&zero,v);CHKERRQ(ierr); 80736db0b34SBarry Smith ierr = VecGetArray(v,&x);CHKERRQ(ierr); 80836db0b34SBarry Smith ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr); 809273d9f13SBarry Smith if (n != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector"); 810273d9f13SBarry Smith for (i=0; i<A->m; i++) { 811416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 812416022c9SBarry Smith if (a->j[j]+shift == i) { 813416022c9SBarry Smith x[i] = a->a[j]; 81417ab2063SBarry Smith break; 81517ab2063SBarry Smith } 81617ab2063SBarry Smith } 81717ab2063SBarry Smith } 81836db0b34SBarry Smith ierr = VecRestoreArray(v,&x);CHKERRQ(ierr); 8193a40ed3dSBarry Smith PetscFunctionReturn(0); 82017ab2063SBarry Smith } 82117ab2063SBarry Smith 822d5d45c9bSBarry Smith 8234a2ae208SSatish Balay #undef __FUNCT__ 8244a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ" 8257c922b88SBarry Smith int MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy) 82617ab2063SBarry Smith { 827416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 8285c897100SBarry Smith PetscScalar *x,*y; 8295c897100SBarry Smith int ierr,m = A->m,shift = a->indexshift; 8305c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8315c897100SBarry Smith PetscScalar *v,alpha; 8325c897100SBarry Smith int n,i,*idx; 8335c897100SBarry Smith #endif 83417ab2063SBarry Smith 8353a40ed3dSBarry Smith PetscFunctionBegin; 8362e8a6d31SBarry Smith if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);} 837e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 838e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 83917ab2063SBarry Smith y = y + shift; /* shift for Fortran start by 1 indexing */ 8405c897100SBarry Smith 8415c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ) 8425c897100SBarry Smith fortranmulttransposeaddaij_(&m,x,a->i,a->j+shift,a->a+shift,y); 8435c897100SBarry Smith #else 84417ab2063SBarry Smith for (i=0; i<m; i++) { 845416022c9SBarry Smith idx = a->j + a->i[i] + shift; 846416022c9SBarry Smith v = a->a + a->i[i] + shift; 847416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 84817ab2063SBarry Smith alpha = x[i]; 84917ab2063SBarry Smith while (n-->0) {y[*idx++] += alpha * *v++;} 85017ab2063SBarry Smith } 8515c897100SBarry Smith #endif 852b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 853e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 854e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 8553a40ed3dSBarry Smith PetscFunctionReturn(0); 85617ab2063SBarry Smith } 85717ab2063SBarry Smith 8584a2ae208SSatish Balay #undef __FUNCT__ 8595c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ" 8605c897100SBarry Smith int MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy) 8615c897100SBarry Smith { 8628d5b0100SBarry Smith PetscScalar zero = 0.0; 8635c897100SBarry Smith int ierr; 8645c897100SBarry Smith 8655c897100SBarry Smith PetscFunctionBegin; 8665c897100SBarry Smith ierr = VecSet(&zero,yy);CHKERRQ(ierr); 8675c897100SBarry Smith ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr); 8685c897100SBarry Smith PetscFunctionReturn(0); 8695c897100SBarry Smith } 8705c897100SBarry Smith 8715c897100SBarry Smith 8725c897100SBarry Smith #undef __FUNCT__ 8734a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ" 87444cd7ae7SLois Curfman McInnes int MatMult_SeqAIJ(Mat A,Vec xx,Vec yy) 87517ab2063SBarry Smith { 876416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 87787828ca2SBarry Smith PetscScalar *x,*y,*v,sum; 878273d9f13SBarry Smith int ierr,m = A->m,*idx,shift = a->indexshift,*ii; 879aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 880e36a17ebSSatish Balay int n,i,jrow,j; 881e36a17ebSSatish Balay #endif 88217ab2063SBarry Smith 883b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT) 884fee21e36SBarry Smith #pragma disjoint(*x,*y,*v) 885fee21e36SBarry Smith #endif 886fee21e36SBarry Smith 8873a40ed3dSBarry Smith PetscFunctionBegin; 888e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 889e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 89017ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 891416022c9SBarry Smith idx = a->j; 892d64ed03dSBarry Smith v = a->a; 893416022c9SBarry Smith ii = a->i; 894aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ) 89529b5ca8aSSatish Balay fortranmultaij_(&m,x,ii,idx+shift,v+shift,y); 8968d195f9aSBarry Smith #else 897d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 898d64ed03dSBarry Smith idx += shift; 89917ab2063SBarry Smith for (i=0; i<m; i++) { 9009ea0dfa2SSatish Balay jrow = ii[i]; 9019ea0dfa2SSatish Balay n = ii[i+1] - jrow; 90217ab2063SBarry Smith sum = 0.0; 9039ea0dfa2SSatish Balay for (j=0; j<n; j++) { 9049ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 9059ea0dfa2SSatish Balay } 90617ab2063SBarry Smith y[i] = sum; 90717ab2063SBarry Smith } 9088d195f9aSBarry Smith #endif 909b0a32e0cSBarry Smith PetscLogFlops(2*a->nz - m); 910e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 911e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9123a40ed3dSBarry Smith PetscFunctionReturn(0); 91317ab2063SBarry Smith } 91417ab2063SBarry Smith 9154a2ae208SSatish Balay #undef __FUNCT__ 9164a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ" 91744cd7ae7SLois Curfman McInnes int MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz) 91817ab2063SBarry Smith { 919416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 92087828ca2SBarry Smith PetscScalar *x,*y,*z,*v,sum; 921273d9f13SBarry Smith int ierr,m = A->m,*idx,shift = a->indexshift,*ii; 922aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 923e36a17ebSSatish Balay int n,i,jrow,j; 924e36a17ebSSatish Balay #endif 9259ea0dfa2SSatish Balay 9263a40ed3dSBarry Smith PetscFunctionBegin; 927e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 928e1311b90SBarry Smith ierr = VecGetArray(yy,&y);CHKERRQ(ierr); 9292e8a6d31SBarry Smith if (zz != yy) { 930e1311b90SBarry Smith ierr = VecGetArray(zz,&z);CHKERRQ(ierr); 9312e8a6d31SBarry Smith } else { 9322e8a6d31SBarry Smith z = y; 9332e8a6d31SBarry Smith } 93417ab2063SBarry Smith x = x + shift; /* shift for Fortran start by 1 indexing */ 935cddf8d76SBarry Smith idx = a->j; 936d64ed03dSBarry Smith v = a->a; 937cddf8d76SBarry Smith ii = a->i; 938aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ) 93929b5ca8aSSatish Balay fortranmultaddaij_(&m,x,ii,idx+shift,v+shift,y,z); 94002ab625aSSatish Balay #else 941d64ed03dSBarry Smith v += shift; /* shift for Fortran start by 1 indexing */ 942d64ed03dSBarry Smith idx += shift; 94317ab2063SBarry Smith for (i=0; i<m; i++) { 9449ea0dfa2SSatish Balay jrow = ii[i]; 9459ea0dfa2SSatish Balay n = ii[i+1] - jrow; 94617ab2063SBarry Smith sum = y[i]; 9479ea0dfa2SSatish Balay for (j=0; j<n; j++) { 9489ea0dfa2SSatish Balay sum += v[jrow]*x[idx[jrow]]; jrow++; 9499ea0dfa2SSatish Balay } 95017ab2063SBarry Smith z[i] = sum; 95117ab2063SBarry Smith } 95202ab625aSSatish Balay #endif 953b0a32e0cSBarry Smith PetscLogFlops(2*a->nz); 954e1311b90SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 955e1311b90SBarry Smith ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr); 9562e8a6d31SBarry Smith if (zz != yy) { 957e1311b90SBarry Smith ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr); 9582e8a6d31SBarry Smith } 9593a40ed3dSBarry Smith PetscFunctionReturn(0); 96017ab2063SBarry Smith } 96117ab2063SBarry Smith 96217ab2063SBarry Smith /* 96317ab2063SBarry Smith Adds diagonal pointers to sparse matrix structure. 96417ab2063SBarry Smith */ 9654a2ae208SSatish Balay #undef __FUNCT__ 9664a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ" 967f1e2ffcdSBarry Smith int MatMarkDiagonal_SeqAIJ(Mat A) 96817ab2063SBarry Smith { 969416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 970b0a32e0cSBarry Smith int i,j,*diag,m = A->m,shift = a->indexshift,ierr; 97117ab2063SBarry Smith 9723a40ed3dSBarry Smith PetscFunctionBegin; 973f1e2ffcdSBarry Smith if (a->diag) PetscFunctionReturn(0); 974f1e2ffcdSBarry Smith 975b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&diag);CHKERRQ(ierr); 976b0a32e0cSBarry Smith PetscLogObjectMemory(A,(m+1)*sizeof(int)); 977273d9f13SBarry Smith for (i=0; i<A->m; i++) { 97835b0346bSBarry Smith diag[i] = a->i[i+1]; 979416022c9SBarry Smith for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) { 980416022c9SBarry Smith if (a->j[j]+shift == i) { 98117ab2063SBarry Smith diag[i] = j - shift; 98217ab2063SBarry Smith break; 98317ab2063SBarry Smith } 98417ab2063SBarry Smith } 98517ab2063SBarry Smith } 986416022c9SBarry Smith a->diag = diag; 9873a40ed3dSBarry Smith PetscFunctionReturn(0); 98817ab2063SBarry Smith } 98917ab2063SBarry Smith 990be5855fcSBarry Smith /* 991be5855fcSBarry Smith Checks for missing diagonals 992be5855fcSBarry Smith */ 9934a2ae208SSatish Balay #undef __FUNCT__ 9944a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ" 995f1e2ffcdSBarry Smith int MatMissingDiagonal_SeqAIJ(Mat A) 996be5855fcSBarry Smith { 997be5855fcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 998f1e2ffcdSBarry Smith int *diag,*jj = a->j,i,shift = a->indexshift,ierr; 999be5855fcSBarry Smith 1000be5855fcSBarry Smith PetscFunctionBegin; 1001f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1002f1e2ffcdSBarry Smith diag = a->diag; 1003273d9f13SBarry Smith for (i=0; i<A->m; i++) { 1004be5855fcSBarry Smith if (jj[diag[i]+shift] != i-shift) { 100529bbc08cSBarry Smith SETERRQ1(1,"Matrix is missing diagonal number %d",i); 1006be5855fcSBarry Smith } 1007be5855fcSBarry Smith } 1008be5855fcSBarry Smith PetscFunctionReturn(0); 1009be5855fcSBarry Smith } 1010be5855fcSBarry Smith 10114a2ae208SSatish Balay #undef __FUNCT__ 10124a2ae208SSatish Balay #define __FUNCT__ "MatRelax_SeqAIJ" 1013c14dc6b6SHong Zhang int MatRelax_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx) 101417ab2063SBarry Smith { 1015416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 101687828ca2SBarry Smith PetscScalar *x,*b,*bs, d,*xs,sum,*v = a->a,*t=0,scale,*ts,*xb,*idiag=0; 1017273d9f13SBarry Smith int ierr,*idx,*diag,n = A->n,m = A->m,i,shift = a->indexshift; 101817ab2063SBarry Smith 10193a40ed3dSBarry Smith PetscFunctionBegin; 1020b965ef7fSBarry Smith its = its*lits; 102191723122SBarry Smith if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits); 102291723122SBarry Smith 1023e1311b90SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 1024fb2e594dSBarry Smith if (xx != bb) { 1025e1311b90SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 1026fb2e594dSBarry Smith } else { 1027fb2e594dSBarry Smith b = x; 1028fb2e594dSBarry Smith } 1029fb2e594dSBarry Smith 1030f1e2ffcdSBarry Smith if (!a->diag) {ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);} 1031416022c9SBarry Smith diag = a->diag; 1032416022c9SBarry Smith xs = x + shift; /* shifted by one for index start of a or a->j*/ 103317ab2063SBarry Smith if (flag == SOR_APPLY_UPPER) { 103417ab2063SBarry Smith /* apply (U + D/omega) to the vector */ 103517ab2063SBarry Smith bs = b + shift; 103617ab2063SBarry Smith for (i=0; i<m; i++) { 1037416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1038416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1039b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1040416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1041416022c9SBarry Smith v = a->a + diag[i] + (!shift); 104217ab2063SBarry Smith sum = b[i]*d/omega; 104317ab2063SBarry Smith SPARSEDENSEDOT(sum,bs,v,idx,n); 104417ab2063SBarry Smith x[i] = sum; 104517ab2063SBarry Smith } 1046cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1047fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 10483a40ed3dSBarry Smith PetscFunctionReturn(0); 104917ab2063SBarry Smith } 1050c783ea89SBarry Smith 1051c783ea89SBarry Smith /* setup workspace for Eisenstat */ 1052c783ea89SBarry Smith if (flag & SOR_EISENSTAT) { 1053c783ea89SBarry Smith if (!a->idiag) { 105487828ca2SBarry Smith ierr = PetscMalloc(2*m*sizeof(PetscScalar),&a->idiag);CHKERRQ(ierr); 1055c783ea89SBarry Smith a->ssor = a->idiag + m; 1056c783ea89SBarry Smith v = a->a; 1057c783ea89SBarry Smith for (i=0; i<m; i++) { a->idiag[i] = 1.0/v[diag[i]];} 1058c783ea89SBarry Smith } 1059c783ea89SBarry Smith t = a->ssor; 1060c783ea89SBarry Smith idiag = a->idiag; 1061c783ea89SBarry Smith } 1062fc3d8934SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 1063fc3d8934SBarry Smith U is upper triangular, E is diagonal; This routine applies 1064fc3d8934SBarry Smith 1065fc3d8934SBarry Smith (L + E)^{-1} A (U + E)^{-1} 1066fc3d8934SBarry Smith 1067fc3d8934SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 1068fc3d8934SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 106948af12d7SBarry Smith is the relaxation factor. 1070fc3d8934SBarry Smith */ 1071fc3d8934SBarry Smith 107248af12d7SBarry Smith if (flag == SOR_APPLY_LOWER) { 107329bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented"); 107448af12d7SBarry Smith } else if ((flag & SOR_EISENSTAT) && omega == 1.0 && shift == 0 && fshift == 0.0) { 107548af12d7SBarry Smith /* special case for omega = 1.0 saves flops and some integer ops */ 107687828ca2SBarry Smith PetscScalar *v2; 107748af12d7SBarry Smith 1078733d66baSBarry Smith v2 = a->a; 1079fc3d8934SBarry Smith /* x = (E + U)^{-1} b */ 1080fc3d8934SBarry Smith for (i=m-1; i>=0; i--) { 1081fc3d8934SBarry Smith n = a->i[i+1] - diag[i] - 1; 1082fc3d8934SBarry Smith idx = a->j + diag[i] + 1; 1083fc3d8934SBarry Smith v = a->a + diag[i] + 1; 1084fc3d8934SBarry Smith sum = b[i]; 1085fc3d8934SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1086b4632166SBarry Smith x[i] = sum*idiag[i]; 1087fc3d8934SBarry Smith 1088fc3d8934SBarry Smith /* t = b - (2*E - D)x */ 1089733d66baSBarry Smith t[i] = b[i] - (v2[diag[i]])*x[i]; 1090733d66baSBarry Smith } 1091fc3d8934SBarry Smith 1092fc3d8934SBarry Smith /* t = (E + L)^{-1}t */ 1093fc3d8934SBarry Smith diag = a->diag; 1094fc3d8934SBarry Smith for (i=0; i<m; i++) { 1095fc3d8934SBarry Smith n = diag[i] - a->i[i]; 1096fc3d8934SBarry Smith idx = a->j + a->i[i]; 1097fc3d8934SBarry Smith v = a->a + a->i[i]; 1098fc3d8934SBarry Smith sum = t[i]; 1099fc3d8934SBarry Smith SPARSEDENSEMDOT(sum,t,v,idx,n); 1100b4632166SBarry Smith t[i] = sum*idiag[i]; 1101fc3d8934SBarry Smith 1102fc3d8934SBarry Smith /* x = x + t */ 1103733d66baSBarry Smith x[i] += t[i]; 1104733d66baSBarry Smith } 1105733d66baSBarry Smith 1106b0a32e0cSBarry Smith PetscLogFlops(3*m-1 + 2*a->nz); 1107fc3d8934SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1108fc3d8934SBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 1109fc3d8934SBarry Smith PetscFunctionReturn(0); 11103a40ed3dSBarry Smith } else if (flag & SOR_EISENSTAT) { 111117ab2063SBarry Smith /* Let A = L + U + D; where L is lower trianglar, 111217ab2063SBarry Smith U is upper triangular, E is diagonal; This routine applies 111317ab2063SBarry Smith 111417ab2063SBarry Smith (L + E)^{-1} A (U + E)^{-1} 111517ab2063SBarry Smith 111617ab2063SBarry Smith to a vector efficiently using Eisenstat's trick. This is for 111717ab2063SBarry Smith the case of SSOR preconditioner, so E is D/omega where omega 111817ab2063SBarry Smith is the relaxation factor. 111917ab2063SBarry Smith */ 112017ab2063SBarry Smith scale = (2.0/omega) - 1.0; 112117ab2063SBarry Smith 112217ab2063SBarry Smith /* x = (E + U)^{-1} b */ 112317ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1124416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1125416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1126416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1127416022c9SBarry Smith v = a->a + diag[i] + (!shift); 112817ab2063SBarry Smith sum = b[i]; 112917ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 113017ab2063SBarry Smith x[i] = omega*(sum/d); 113117ab2063SBarry Smith } 113217ab2063SBarry Smith 113317ab2063SBarry Smith /* t = b - (2*E - D)x */ 1134416022c9SBarry Smith v = a->a; 113517ab2063SBarry Smith for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++ + shift])*x[i]; } 113617ab2063SBarry Smith 113717ab2063SBarry Smith /* t = (E + L)^{-1}t */ 1138416022c9SBarry Smith ts = t + shift; /* shifted by one for index start of a or a->j*/ 1139416022c9SBarry Smith diag = a->diag; 114017ab2063SBarry Smith for (i=0; i<m; i++) { 1141416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1142416022c9SBarry Smith n = diag[i] - a->i[i]; 1143416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1144416022c9SBarry Smith v = a->a + a->i[i] + shift; 114517ab2063SBarry Smith sum = t[i]; 114617ab2063SBarry Smith SPARSEDENSEMDOT(sum,ts,v,idx,n); 114717ab2063SBarry Smith t[i] = omega*(sum/d); 1148733d66baSBarry Smith /* x = x + t */ 1149733d66baSBarry Smith x[i] += t[i]; 115017ab2063SBarry Smith } 115117ab2063SBarry Smith 1152b0a32e0cSBarry Smith PetscLogFlops(6*m-1 + 2*a->nz); 1153cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1154fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 11553a40ed3dSBarry Smith PetscFunctionReturn(0); 115617ab2063SBarry Smith } 115717ab2063SBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 115817ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 115977d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 116077d8c4bbSBarry Smith fortranrelaxaijforwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,b); 116177d8c4bbSBarry Smith #else 116217ab2063SBarry Smith for (i=0; i<m; i++) { 1163416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1164416022c9SBarry Smith n = diag[i] - a->i[i]; 1165b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1166416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1167416022c9SBarry Smith v = a->a + a->i[i] + shift; 116817ab2063SBarry Smith sum = b[i]; 116917ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 117017ab2063SBarry Smith x[i] = omega*(sum/d); 117117ab2063SBarry Smith } 117277d8c4bbSBarry Smith #endif 117317ab2063SBarry Smith xb = x; 11743a40ed3dSBarry Smith } else xb = b; 117517ab2063SBarry Smith if ((flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) && 117617ab2063SBarry Smith (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP)) { 117717ab2063SBarry Smith for (i=0; i<m; i++) { 1178416022c9SBarry Smith x[i] *= a->a[diag[i]+shift]; 117917ab2063SBarry Smith } 1180b0a32e0cSBarry Smith PetscLogFlops(m); 118117ab2063SBarry Smith } 118217ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 118377d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 118477d8c4bbSBarry Smith fortranrelaxaijbackwardzero_(&m,&omega,x,a->i,a->j,diag,a->a,xb); 118577d8c4bbSBarry Smith #else 118617ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1187416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1188416022c9SBarry Smith n = a->i[i+1] - diag[i] - 1; 1189b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1190416022c9SBarry Smith idx = a->j + diag[i] + (!shift); 1191416022c9SBarry Smith v = a->a + diag[i] + (!shift); 119217ab2063SBarry Smith sum = xb[i]; 119317ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 119417ab2063SBarry Smith x[i] = omega*(sum/d); 119517ab2063SBarry Smith } 119677d8c4bbSBarry Smith #endif 119717ab2063SBarry Smith } 119817ab2063SBarry Smith its--; 119917ab2063SBarry Smith } 120017ab2063SBarry Smith while (its--) { 120117ab2063SBarry Smith if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){ 120277d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 120377d8c4bbSBarry Smith fortranrelaxaijforward_(&m,&omega,x,a->i,a->j,diag,a->a,b); 120477d8c4bbSBarry Smith #else 120517ab2063SBarry Smith for (i=0; i<m; i++) { 1206416022c9SBarry Smith d = fshift + a->a[diag[i]+shift]; 1207416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1208b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1209416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1210416022c9SBarry Smith v = a->a + a->i[i] + shift; 121117ab2063SBarry Smith sum = b[i]; 121217ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 12137e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 121417ab2063SBarry Smith } 121577d8c4bbSBarry Smith #endif 121617ab2063SBarry Smith } 121717ab2063SBarry Smith if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){ 121877d8c4bbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ) 121977d8c4bbSBarry Smith fortranrelaxaijbackward_(&m,&omega,x,a->i,a->j,diag,a->a,b); 122077d8c4bbSBarry Smith #else 122117ab2063SBarry Smith for (i=m-1; i>=0; i--) { 1222416022c9SBarry Smith d = fshift + a->a[diag[i] + shift]; 1223416022c9SBarry Smith n = a->i[i+1] - a->i[i]; 1224b0a32e0cSBarry Smith PetscLogFlops(2*n-1); 1225416022c9SBarry Smith idx = a->j + a->i[i] + shift; 1226416022c9SBarry Smith v = a->a + a->i[i] + shift; 122717ab2063SBarry Smith sum = b[i]; 122817ab2063SBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 12297e33a6baSBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + a->a[diag[i]+shift]*x[i])/d; 123017ab2063SBarry Smith } 123177d8c4bbSBarry Smith #endif 123217ab2063SBarry Smith } 123317ab2063SBarry Smith } 1234cb5b572fSBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 1235fb2e594dSBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr);} 12363a40ed3dSBarry Smith PetscFunctionReturn(0); 123717ab2063SBarry Smith } 123817ab2063SBarry Smith 12394a2ae208SSatish Balay #undef __FUNCT__ 12404a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ" 12418f6be9afSLois Curfman McInnes int MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info) 124217ab2063SBarry Smith { 1243416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 12444e220ebcSLois Curfman McInnes 12453a40ed3dSBarry Smith PetscFunctionBegin; 1246273d9f13SBarry Smith info->rows_global = (double)A->m; 1247273d9f13SBarry Smith info->columns_global = (double)A->n; 1248273d9f13SBarry Smith info->rows_local = (double)A->m; 1249273d9f13SBarry Smith info->columns_local = (double)A->n; 12504e220ebcSLois Curfman McInnes info->block_size = 1.0; 12514e220ebcSLois Curfman McInnes info->nz_allocated = (double)a->maxnz; 12524e220ebcSLois Curfman McInnes info->nz_used = (double)a->nz; 12534e220ebcSLois Curfman McInnes info->nz_unneeded = (double)(a->maxnz - a->nz); 12544e220ebcSLois Curfman McInnes info->assemblies = (double)A->num_ass; 12554e220ebcSLois Curfman McInnes info->mallocs = (double)a->reallocs; 12564e220ebcSLois Curfman McInnes info->memory = A->mem; 12574e220ebcSLois Curfman McInnes if (A->factor) { 12584e220ebcSLois Curfman McInnes info->fill_ratio_given = A->info.fill_ratio_given; 12594e220ebcSLois Curfman McInnes info->fill_ratio_needed = A->info.fill_ratio_needed; 12604e220ebcSLois Curfman McInnes info->factor_mallocs = A->info.factor_mallocs; 12614e220ebcSLois Curfman McInnes } else { 12624e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; 12634e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 12644e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 12654e220ebcSLois Curfman McInnes } 12663a40ed3dSBarry Smith PetscFunctionReturn(0); 126717ab2063SBarry Smith } 126817ab2063SBarry Smith 1269b9b97703SBarry Smith EXTERN int MatLUFactorSymbolic_SeqAIJ(Mat,IS,IS,MatLUInfo*,Mat*); 1270ca44d042SBarry Smith EXTERN int MatLUFactorNumeric_SeqAIJ(Mat,Mat*); 1271b9b97703SBarry Smith EXTERN int MatLUFactor_SeqAIJ(Mat,IS,IS,MatLUInfo*); 1272ca44d042SBarry Smith EXTERN int MatSolve_SeqAIJ(Mat,Vec,Vec); 1273ca44d042SBarry Smith EXTERN int MatSolveAdd_SeqAIJ(Mat,Vec,Vec,Vec); 1274ca44d042SBarry Smith EXTERN int MatSolveTranspose_SeqAIJ(Mat,Vec,Vec); 1275ca44d042SBarry Smith EXTERN int MatSolveTransposeAdd_SeqAIJ(Mat,Vec,Vec,Vec); 127617ab2063SBarry Smith 12774a2ae208SSatish Balay #undef __FUNCT__ 12784a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ" 127987828ca2SBarry Smith int MatZeroRows_SeqAIJ(Mat A,IS is,PetscScalar *diag) 128017ab2063SBarry Smith { 1281416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1282273d9f13SBarry Smith int i,ierr,N,*rows,m = A->m - 1,shift = a->indexshift; 128317ab2063SBarry Smith 12843a40ed3dSBarry Smith PetscFunctionBegin; 1285b9b97703SBarry Smith ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr); 128617ab2063SBarry Smith ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 1287f1e2ffcdSBarry Smith if (a->keepzeroedrows) { 1288f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 128929bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 129087828ca2SBarry Smith ierr = PetscMemzero(&a->a[a->i[rows[i]]+shift],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr); 1291f1e2ffcdSBarry Smith } 1292f1e2ffcdSBarry Smith if (diag) { 1293f1e2ffcdSBarry Smith ierr = MatMissingDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1294f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr); 1295f1e2ffcdSBarry Smith for (i=0; i<N; i++) { 1296f1e2ffcdSBarry Smith a->a[a->diag[rows[i]]] = *diag; 1297f1e2ffcdSBarry Smith } 1298f1e2ffcdSBarry Smith } 1299f1e2ffcdSBarry Smith } else { 130017ab2063SBarry Smith if (diag) { 130117ab2063SBarry Smith for (i=0; i<N; i++) { 130229bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 13037ae801bdSBarry Smith if (a->ilen[rows[i]] > 0) { 1304416022c9SBarry Smith a->ilen[rows[i]] = 1; 1305416022c9SBarry Smith a->a[a->i[rows[i]]+shift] = *diag; 1306416022c9SBarry Smith a->j[a->i[rows[i]]+shift] = rows[i]+shift; 13077ae801bdSBarry Smith } else { /* in case row was completely empty */ 1308d64ed03dSBarry Smith ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],diag,INSERT_VALUES);CHKERRQ(ierr); 130917ab2063SBarry Smith } 131017ab2063SBarry Smith } 13113a40ed3dSBarry Smith } else { 131217ab2063SBarry Smith for (i=0; i<N; i++) { 131329bbc08cSBarry Smith if (rows[i] < 0 || rows[i] > m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"row out of range"); 1314416022c9SBarry Smith a->ilen[rows[i]] = 0; 131517ab2063SBarry Smith } 131617ab2063SBarry Smith } 1317f1e2ffcdSBarry Smith } 13187ae801bdSBarry Smith ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 131943a90d84SBarry Smith ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13203a40ed3dSBarry Smith PetscFunctionReturn(0); 132117ab2063SBarry Smith } 132217ab2063SBarry Smith 13234a2ae208SSatish Balay #undef __FUNCT__ 13244a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ" 132587828ca2SBarry Smith int MatGetRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v) 132617ab2063SBarry Smith { 1327416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1328b0a32e0cSBarry Smith int *itmp,i,shift = a->indexshift,ierr; 132917ab2063SBarry Smith 13303a40ed3dSBarry Smith PetscFunctionBegin; 1331273d9f13SBarry Smith if (row < 0 || row >= A->m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %d out of range",row); 133217ab2063SBarry Smith 1333416022c9SBarry Smith *nz = a->i[row+1] - a->i[row]; 1334416022c9SBarry Smith if (v) *v = a->a + a->i[row] + shift; 133517ab2063SBarry Smith if (idx) { 1336416022c9SBarry Smith itmp = a->j + a->i[row] + shift; 13374e093b46SBarry Smith if (*nz && shift) { 1338b0a32e0cSBarry Smith ierr = PetscMalloc((*nz)*sizeof(int),idx);CHKERRQ(ierr); 133917ab2063SBarry Smith for (i=0; i<(*nz); i++) {(*idx)[i] = itmp[i] + shift;} 13404e093b46SBarry Smith } else if (*nz) { 13414e093b46SBarry Smith *idx = itmp; 134217ab2063SBarry Smith } 134317ab2063SBarry Smith else *idx = 0; 134417ab2063SBarry Smith } 13453a40ed3dSBarry Smith PetscFunctionReturn(0); 134617ab2063SBarry Smith } 134717ab2063SBarry Smith 13484a2ae208SSatish Balay #undef __FUNCT__ 13494a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ" 135087828ca2SBarry Smith int MatRestoreRow_SeqAIJ(Mat A,int row,int *nz,int **idx,PetscScalar **v) 135117ab2063SBarry Smith { 13524e093b46SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1353606d414cSSatish Balay int ierr; 13543a40ed3dSBarry Smith 13553a40ed3dSBarry Smith PetscFunctionBegin; 1356606d414cSSatish Balay if (idx) {if (*idx && a->indexshift) {ierr = PetscFree(*idx);CHKERRQ(ierr);}} 13573a40ed3dSBarry Smith PetscFunctionReturn(0); 135817ab2063SBarry Smith } 135917ab2063SBarry Smith 13604a2ae208SSatish Balay #undef __FUNCT__ 13614a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ" 1362064f8208SBarry Smith int MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm) 136317ab2063SBarry Smith { 1364416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 136587828ca2SBarry Smith PetscScalar *v = a->a; 136636db0b34SBarry Smith PetscReal sum = 0.0; 1367549d3d68SSatish Balay int i,j,shift = a->indexshift,ierr; 136817ab2063SBarry Smith 13693a40ed3dSBarry Smith PetscFunctionBegin; 137017ab2063SBarry Smith if (type == NORM_FROBENIUS) { 1371416022c9SBarry Smith for (i=0; i<a->nz; i++) { 1372aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 137336db0b34SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 137417ab2063SBarry Smith #else 137517ab2063SBarry Smith sum += (*v)*(*v); v++; 137617ab2063SBarry Smith #endif 137717ab2063SBarry Smith } 1378064f8208SBarry Smith *nrm = sqrt(sum); 13793a40ed3dSBarry Smith } else if (type == NORM_1) { 138036db0b34SBarry Smith PetscReal *tmp; 1381416022c9SBarry Smith int *jj = a->j; 1382b0a32e0cSBarry Smith ierr = PetscMalloc((A->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1383273d9f13SBarry Smith ierr = PetscMemzero(tmp,A->n*sizeof(PetscReal));CHKERRQ(ierr); 1384064f8208SBarry Smith *nrm = 0.0; 1385416022c9SBarry Smith for (j=0; j<a->nz; j++) { 1386a2744918SBarry Smith tmp[*jj++ + shift] += PetscAbsScalar(*v); v++; 138717ab2063SBarry Smith } 1388273d9f13SBarry Smith for (j=0; j<A->n; j++) { 1389064f8208SBarry Smith if (tmp[j] > *nrm) *nrm = tmp[j]; 139017ab2063SBarry Smith } 1391606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 13923a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { 1393064f8208SBarry Smith *nrm = 0.0; 1394273d9f13SBarry Smith for (j=0; j<A->m; j++) { 1395416022c9SBarry Smith v = a->a + a->i[j] + shift; 139617ab2063SBarry Smith sum = 0.0; 1397416022c9SBarry Smith for (i=0; i<a->i[j+1]-a->i[j]; i++) { 1398cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 139917ab2063SBarry Smith } 1400064f8208SBarry Smith if (sum > *nrm) *nrm = sum; 140117ab2063SBarry Smith } 14023a40ed3dSBarry Smith } else { 140329bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 140417ab2063SBarry Smith } 14053a40ed3dSBarry Smith PetscFunctionReturn(0); 140617ab2063SBarry Smith } 140717ab2063SBarry Smith 14084a2ae208SSatish Balay #undef __FUNCT__ 14094a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ" 14108f6be9afSLois Curfman McInnes int MatTranspose_SeqAIJ(Mat A,Mat *B) 141117ab2063SBarry Smith { 1412416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1413416022c9SBarry Smith Mat C; 1414273d9f13SBarry Smith int i,ierr,*aj = a->j,*ai = a->i,m = A->m,len,*col; 1415273d9f13SBarry Smith int shift = a->indexshift; 141687828ca2SBarry Smith PetscScalar *array = a->a; 141717ab2063SBarry Smith 14183a40ed3dSBarry Smith PetscFunctionBegin; 1419273d9f13SBarry Smith if (!B && m != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1420b0a32e0cSBarry Smith ierr = PetscMalloc((1+A->n)*sizeof(int),&col);CHKERRQ(ierr); 1421273d9f13SBarry Smith ierr = PetscMemzero(col,(1+A->n)*sizeof(int));CHKERRQ(ierr); 142217ab2063SBarry Smith if (shift) { 142317ab2063SBarry Smith for (i=0; i<ai[m]-1; i++) aj[i] -= 1; 142417ab2063SBarry Smith } 142517ab2063SBarry Smith for (i=0; i<ai[m]+shift; i++) col[aj[i]] += 1; 1426273d9f13SBarry Smith ierr = MatCreateSeqAIJ(A->comm,A->n,m,0,col,&C);CHKERRQ(ierr); 1427606d414cSSatish Balay ierr = PetscFree(col);CHKERRQ(ierr); 142817ab2063SBarry Smith for (i=0; i<m; i++) { 142917ab2063SBarry Smith len = ai[i+1]-ai[i]; 1430416022c9SBarry Smith ierr = MatSetValues(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); 1431b9b97703SBarry Smith array += len; 1432b9b97703SBarry Smith aj += len; 143317ab2063SBarry Smith } 143417ab2063SBarry Smith if (shift) { 143517ab2063SBarry Smith for (i=0; i<ai[m]-1; i++) aj[i] += 1; 143617ab2063SBarry Smith } 143717ab2063SBarry Smith 14386d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14396d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 144017ab2063SBarry Smith 1441f1e2ffcdSBarry Smith if (B) { 1442416022c9SBarry Smith *B = C; 144317ab2063SBarry Smith } else { 1444273d9f13SBarry Smith ierr = MatHeaderCopy(A,C);CHKERRQ(ierr); 144517ab2063SBarry Smith } 14463a40ed3dSBarry Smith PetscFunctionReturn(0); 144717ab2063SBarry Smith } 144817ab2063SBarry Smith 14494a2ae208SSatish Balay #undef __FUNCT__ 14504a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ" 14518f6be9afSLois Curfman McInnes int MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr) 145217ab2063SBarry Smith { 1453416022c9SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 145487828ca2SBarry Smith PetscScalar *l,*r,x,*v; 1455273d9f13SBarry Smith int ierr,i,j,m = A->m,n = A->n,M,nz = a->nz,*jj,shift = a->indexshift; 145617ab2063SBarry Smith 14573a40ed3dSBarry Smith PetscFunctionBegin; 145817ab2063SBarry Smith if (ll) { 14593ea7c6a1SSatish Balay /* The local size is used so that VecMPI can be passed to this routine 14603ea7c6a1SSatish Balay by MatDiagonalScale_MPIAIJ */ 1461e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr); 1462273d9f13SBarry Smith if (m != A->m) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length"); 1463e1311b90SBarry Smith ierr = VecGetArray(ll,&l);CHKERRQ(ierr); 1464416022c9SBarry Smith v = a->a; 146517ab2063SBarry Smith for (i=0; i<m; i++) { 146617ab2063SBarry Smith x = l[i]; 1467416022c9SBarry Smith M = a->i[i+1] - a->i[i]; 146817ab2063SBarry Smith for (j=0; j<M; j++) { (*v++) *= x;} 146917ab2063SBarry Smith } 1470e1311b90SBarry Smith ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr); 1471b0a32e0cSBarry Smith PetscLogFlops(nz); 147217ab2063SBarry Smith } 147317ab2063SBarry Smith if (rr) { 1474e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr); 1475273d9f13SBarry Smith if (n != A->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length"); 1476e1311b90SBarry Smith ierr = VecGetArray(rr,&r);CHKERRQ(ierr); 1477416022c9SBarry Smith v = a->a; jj = a->j; 147817ab2063SBarry Smith for (i=0; i<nz; i++) { 147917ab2063SBarry Smith (*v++) *= r[*jj++ + shift]; 148017ab2063SBarry Smith } 1481e1311b90SBarry Smith ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr); 1482b0a32e0cSBarry Smith PetscLogFlops(nz); 148317ab2063SBarry Smith } 14843a40ed3dSBarry Smith PetscFunctionReturn(0); 148517ab2063SBarry Smith } 148617ab2063SBarry Smith 14874a2ae208SSatish Balay #undef __FUNCT__ 14884a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ" 14897b2a1423SBarry Smith int MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,int csize,MatReuse scall,Mat *B) 149017ab2063SBarry Smith { 1491db02288aSLois Curfman McInnes Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*c; 1492273d9f13SBarry Smith int *smap,i,k,kstart,kend,ierr,oldcols = A->n,*lens; 149336db0b34SBarry Smith int row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi; 149499141d43SSatish Balay int *irow,*icol,nrows,ncols,shift = a->indexshift,*ssmap; 149599141d43SSatish Balay int *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen; 149687828ca2SBarry Smith PetscScalar *a_new,*mat_a; 1497416022c9SBarry Smith Mat C; 1498fee21e36SBarry Smith PetscTruth stride; 149917ab2063SBarry Smith 15003a40ed3dSBarry Smith PetscFunctionBegin; 1501d64ed03dSBarry Smith ierr = ISSorted(isrow,(PetscTruth*)&i);CHKERRQ(ierr); 150229bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted"); 1503d64ed03dSBarry Smith ierr = ISSorted(iscol,(PetscTruth*)&i);CHKERRQ(ierr); 150429bbc08cSBarry Smith if (!i) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted"); 150599141d43SSatish Balay 150617ab2063SBarry Smith ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr); 1507b9b97703SBarry Smith ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr); 1508b9b97703SBarry Smith ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr); 150917ab2063SBarry Smith 1510fee21e36SBarry Smith ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr); 1511fee21e36SBarry Smith ierr = ISStride(iscol,&stride);CHKERRQ(ierr); 1512fee21e36SBarry Smith if (stride && step == 1) { 151302834360SBarry Smith /* special case of contiguous rows */ 151431ebf83bSSatish Balay ierr = PetscMalloc((2*nrows+1)*sizeof(int),&lens);CHKERRQ(ierr); 151531ebf83bSSatish Balay starts = lens + nrows; 151602834360SBarry Smith /* loop over new rows determining lens and starting points */ 151702834360SBarry Smith for (i=0; i<nrows; i++) { 1518a2744918SBarry Smith kstart = ai[irow[i]]+shift; 1519a2744918SBarry Smith kend = kstart + ailen[irow[i]]; 152002834360SBarry Smith for (k=kstart; k<kend; k++) { 1521d8ced48eSBarry Smith if (aj[k]+shift >= first) { 152202834360SBarry Smith starts[i] = k; 152302834360SBarry Smith break; 152402834360SBarry Smith } 152502834360SBarry Smith } 1526a2744918SBarry Smith sum = 0; 152702834360SBarry Smith while (k < kend) { 1528d8ced48eSBarry Smith if (aj[k++]+shift >= first+ncols) break; 1529a2744918SBarry Smith sum++; 153002834360SBarry Smith } 1531a2744918SBarry Smith lens[i] = sum; 153202834360SBarry Smith } 153302834360SBarry Smith /* create submatrix */ 1534cddf8d76SBarry Smith if (scall == MAT_REUSE_MATRIX) { 153508480c60SBarry Smith int n_cols,n_rows; 153608480c60SBarry Smith ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr); 153729bbc08cSBarry Smith if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size"); 1538d8ced48eSBarry Smith ierr = MatZeroEntries(*B);CHKERRQ(ierr); 153908480c60SBarry Smith C = *B; 15403a40ed3dSBarry Smith } else { 154102834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 154208480c60SBarry Smith } 1543db02288aSLois Curfman McInnes c = (Mat_SeqAIJ*)C->data; 1544db02288aSLois Curfman McInnes 154502834360SBarry Smith /* loop over rows inserting into submatrix */ 1546db02288aSLois Curfman McInnes a_new = c->a; 1547db02288aSLois Curfman McInnes j_new = c->j; 1548db02288aSLois Curfman McInnes i_new = c->i; 1549db02288aSLois Curfman McInnes i_new[0] = -shift; 155002834360SBarry Smith for (i=0; i<nrows; i++) { 1551a2744918SBarry Smith ii = starts[i]; 1552a2744918SBarry Smith lensi = lens[i]; 1553a2744918SBarry Smith for (k=0; k<lensi; k++) { 1554a2744918SBarry Smith *j_new++ = aj[ii+k] - first; 155502834360SBarry Smith } 155687828ca2SBarry Smith ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr); 1557a2744918SBarry Smith a_new += lensi; 1558a2744918SBarry Smith i_new[i+1] = i_new[i] + lensi; 1559a2744918SBarry Smith c->ilen[i] = lensi; 156002834360SBarry Smith } 1561606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 15623a40ed3dSBarry Smith } else { 156302834360SBarry Smith ierr = ISGetIndices(iscol,&icol);CHKERRQ(ierr); 1564b0a32e0cSBarry Smith ierr = PetscMalloc((1+oldcols)*sizeof(int),&smap);CHKERRQ(ierr); 156502834360SBarry Smith ssmap = smap + shift; 1566b0a32e0cSBarry Smith ierr = PetscMalloc((1+nrows)*sizeof(int),&lens);CHKERRQ(ierr); 1567549d3d68SSatish Balay ierr = PetscMemzero(smap,oldcols*sizeof(int));CHKERRQ(ierr); 156817ab2063SBarry Smith for (i=0; i<ncols; i++) smap[icol[i]] = i+1; 156902834360SBarry Smith /* determine lens of each row */ 157002834360SBarry Smith for (i=0; i<nrows; i++) { 1571d8ced48eSBarry Smith kstart = ai[irow[i]]+shift; 157202834360SBarry Smith kend = kstart + a->ilen[irow[i]]; 157302834360SBarry Smith lens[i] = 0; 157402834360SBarry Smith for (k=kstart; k<kend; k++) { 1575d8ced48eSBarry Smith if (ssmap[aj[k]]) { 157602834360SBarry Smith lens[i]++; 157702834360SBarry Smith } 157802834360SBarry Smith } 157902834360SBarry Smith } 158017ab2063SBarry Smith /* Create and fill new matrix */ 1581a2744918SBarry Smith if (scall == MAT_REUSE_MATRIX) { 15820f5bd95cSBarry Smith PetscTruth equal; 15830f5bd95cSBarry Smith 158499141d43SSatish Balay c = (Mat_SeqAIJ *)((*B)->data); 1585273d9f13SBarry Smith if ((*B)->m != nrows || (*B)->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size"); 1586273d9f13SBarry Smith ierr = PetscMemcmp(c->ilen,lens,(*B)->m*sizeof(int),&equal);CHKERRQ(ierr); 15870f5bd95cSBarry Smith if (!equal) { 158829bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros"); 158999141d43SSatish Balay } 1590273d9f13SBarry Smith ierr = PetscMemzero(c->ilen,(*B)->m*sizeof(int));CHKERRQ(ierr); 159108480c60SBarry Smith C = *B; 15923a40ed3dSBarry Smith } else { 159302834360SBarry Smith ierr = MatCreateSeqAIJ(A->comm,nrows,ncols,0,lens,&C);CHKERRQ(ierr); 159408480c60SBarry Smith } 159599141d43SSatish Balay c = (Mat_SeqAIJ *)(C->data); 159617ab2063SBarry Smith for (i=0; i<nrows; i++) { 159799141d43SSatish Balay row = irow[i]; 159899141d43SSatish Balay kstart = ai[row]+shift; 159999141d43SSatish Balay kend = kstart + a->ilen[row]; 160099141d43SSatish Balay mat_i = c->i[i]+shift; 160199141d43SSatish Balay mat_j = c->j + mat_i; 160299141d43SSatish Balay mat_a = c->a + mat_i; 160399141d43SSatish Balay mat_ilen = c->ilen + i; 160417ab2063SBarry Smith for (k=kstart; k<kend; k++) { 160599141d43SSatish Balay if ((tcol=ssmap[a->j[k]])) { 160699141d43SSatish Balay *mat_j++ = tcol - (!shift); 160799141d43SSatish Balay *mat_a++ = a->a[k]; 160899141d43SSatish Balay (*mat_ilen)++; 160999141d43SSatish Balay 161017ab2063SBarry Smith } 161117ab2063SBarry Smith } 161217ab2063SBarry Smith } 161302834360SBarry Smith /* Free work space */ 161402834360SBarry Smith ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr); 1615606d414cSSatish Balay ierr = PetscFree(smap);CHKERRQ(ierr); 1616606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 161702834360SBarry Smith } 16186d4a8577SBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16196d4a8577SBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 162017ab2063SBarry Smith 162117ab2063SBarry Smith ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr); 1622416022c9SBarry Smith *B = C; 16233a40ed3dSBarry Smith PetscFunctionReturn(0); 162417ab2063SBarry Smith } 162517ab2063SBarry Smith 1626a871dcd8SBarry Smith /* 1627a871dcd8SBarry Smith */ 16284a2ae208SSatish Balay #undef __FUNCT__ 16294a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ" 16305ef9f2a5SBarry Smith int MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,MatILUInfo *info) 1631a871dcd8SBarry Smith { 163263b91edcSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 163308480c60SBarry Smith int ierr; 163463b91edcSBarry Smith Mat outA; 1635b8a78c4aSBarry Smith PetscTruth row_identity,col_identity; 163663b91edcSBarry Smith 16373a40ed3dSBarry Smith PetscFunctionBegin; 163829bbc08cSBarry Smith if (info && info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu"); 1639b8a78c4aSBarry Smith ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr); 1640b8a78c4aSBarry Smith ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr); 1641b8a78c4aSBarry Smith if (!row_identity || !col_identity) { 164229bbc08cSBarry Smith SETERRQ(1,"Row and column permutations must be identity for in-place ILU"); 1643b8a78c4aSBarry Smith } 1644a871dcd8SBarry Smith 164563b91edcSBarry Smith outA = inA; 164663b91edcSBarry Smith inA->factor = FACTOR_LU; 164763b91edcSBarry Smith a->row = row; 164863b91edcSBarry Smith a->col = col; 1649c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr); 1650c38d4ed2SBarry Smith ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr); 165163b91edcSBarry Smith 165236db0b34SBarry Smith /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */ 1653b9b97703SBarry Smith if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */ 16544c49b128SBarry Smith ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr); 1655b0a32e0cSBarry Smith PetscLogObjectParent(inA,a->icol); 1656f0ec6fceSSatish Balay 165794a9d846SBarry Smith if (!a->solve_work) { /* this matrix may have been factored before */ 165887828ca2SBarry Smith ierr = PetscMalloc((inA->m+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr); 165994a9d846SBarry Smith } 166063b91edcSBarry Smith 166108480c60SBarry Smith if (!a->diag) { 1662f1e2ffcdSBarry Smith ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr); 166363b91edcSBarry Smith } 166463b91edcSBarry Smith ierr = MatLUFactorNumeric_SeqAIJ(inA,&outA);CHKERRQ(ierr); 16653a40ed3dSBarry Smith PetscFunctionReturn(0); 1666a871dcd8SBarry Smith } 1667a871dcd8SBarry Smith 1668d9eff348SSatish Balay #include "petscblaslapack.h" 16694a2ae208SSatish Balay #undef __FUNCT__ 16704a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ" 167187828ca2SBarry Smith int MatScale_SeqAIJ(PetscScalar *alpha,Mat inA) 1672f0b747eeSBarry Smith { 1673f0b747eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)inA->data; 1674f0b747eeSBarry Smith int one = 1; 16753a40ed3dSBarry Smith 16763a40ed3dSBarry Smith PetscFunctionBegin; 1677f0b747eeSBarry Smith BLscal_(&a->nz,alpha,a->a,&one); 1678b0a32e0cSBarry Smith PetscLogFlops(a->nz); 16793a40ed3dSBarry Smith PetscFunctionReturn(0); 1680f0b747eeSBarry Smith } 1681f0b747eeSBarry Smith 16824a2ae208SSatish Balay #undef __FUNCT__ 16834a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ" 16847b2a1423SBarry Smith int MatGetSubMatrices_SeqAIJ(Mat A,int n,IS *irow,IS *icol,MatReuse scall,Mat **B) 1685cddf8d76SBarry Smith { 1686cddf8d76SBarry Smith int ierr,i; 1687cddf8d76SBarry Smith 16883a40ed3dSBarry Smith PetscFunctionBegin; 1689cddf8d76SBarry Smith if (scall == MAT_INITIAL_MATRIX) { 1690b0a32e0cSBarry Smith ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr); 1691cddf8d76SBarry Smith } 1692cddf8d76SBarry Smith 1693cddf8d76SBarry Smith for (i=0; i<n; i++) { 16946a6a5d1dSBarry Smith ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr); 1695cddf8d76SBarry Smith } 16963a40ed3dSBarry Smith PetscFunctionReturn(0); 1697cddf8d76SBarry Smith } 1698cddf8d76SBarry Smith 16994a2ae208SSatish Balay #undef __FUNCT__ 17004a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_SeqAIJ" 17018f6be9afSLois Curfman McInnes int MatGetBlockSize_SeqAIJ(Mat A,int *bs) 17025a838052SSatish Balay { 1703f830108cSBarry Smith PetscFunctionBegin; 17045a838052SSatish Balay *bs = 1; 17053a40ed3dSBarry Smith PetscFunctionReturn(0); 17065a838052SSatish Balay } 17075a838052SSatish Balay 17084a2ae208SSatish Balay #undef __FUNCT__ 17094a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ" 17108f6be9afSLois Curfman McInnes int MatIncreaseOverlap_SeqAIJ(Mat A,int is_max,IS *is,int ov) 17114dcbc457SBarry Smith { 1712e4d965acSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 171306763907SSatish Balay int shift,row,i,j,k,l,m,n,*idx,ierr,*nidx,isz,val; 17148a047759SSatish Balay int start,end,*ai,*aj; 1715f1af5d2fSBarry Smith PetscBT table; 1716bbd702dbSSatish Balay 17173a40ed3dSBarry Smith PetscFunctionBegin; 17188a047759SSatish Balay shift = a->indexshift; 1719273d9f13SBarry Smith m = A->m; 1720e4d965acSSatish Balay ai = a->i; 17218a047759SSatish Balay aj = a->j+shift; 17228a047759SSatish Balay 172329bbc08cSBarry Smith if (ov < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal overlap value used"); 172406763907SSatish Balay 1725b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&nidx);CHKERRQ(ierr); 17266831982aSBarry Smith ierr = PetscBTCreate(m,table);CHKERRQ(ierr); 172706763907SSatish Balay 1728e4d965acSSatish Balay for (i=0; i<is_max; i++) { 1729b97fc60eSLois Curfman McInnes /* Initialize the two local arrays */ 1730e4d965acSSatish Balay isz = 0; 17316831982aSBarry Smith ierr = PetscBTMemzero(m,table);CHKERRQ(ierr); 1732e4d965acSSatish Balay 1733e4d965acSSatish Balay /* Extract the indices, assume there can be duplicate entries */ 17344dcbc457SBarry Smith ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr); 1735b9b97703SBarry Smith ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr); 1736e4d965acSSatish Balay 1737dd097bc3SLois Curfman McInnes /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */ 1738e4d965acSSatish Balay for (j=0; j<n ; ++j){ 1739f1af5d2fSBarry Smith if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];} 17404dcbc457SBarry Smith } 174106763907SSatish Balay ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr); 174206763907SSatish Balay ierr = ISDestroy(is[i]);CHKERRQ(ierr); 1743e4d965acSSatish Balay 174404a348a9SBarry Smith k = 0; 174504a348a9SBarry Smith for (j=0; j<ov; j++){ /* for each overlap */ 174604a348a9SBarry Smith n = isz; 174706763907SSatish Balay for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */ 1748e4d965acSSatish Balay row = nidx[k]; 1749e4d965acSSatish Balay start = ai[row]; 1750e4d965acSSatish Balay end = ai[row+1]; 175104a348a9SBarry Smith for (l = start; l<end ; l++){ 17528a047759SSatish Balay val = aj[l] + shift; 1753f1af5d2fSBarry Smith if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;} 1754e4d965acSSatish Balay } 1755e4d965acSSatish Balay } 1756e4d965acSSatish Balay } 1757029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr); 1758e4d965acSSatish Balay } 17596831982aSBarry Smith ierr = PetscBTDestroy(table);CHKERRQ(ierr); 1760606d414cSSatish Balay ierr = PetscFree(nidx);CHKERRQ(ierr); 17613a40ed3dSBarry Smith PetscFunctionReturn(0); 17624dcbc457SBarry Smith } 176317ab2063SBarry Smith 17640513a670SBarry Smith /* -------------------------------------------------------------- */ 17654a2ae208SSatish Balay #undef __FUNCT__ 17664a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ" 17670513a670SBarry Smith int MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B) 17680513a670SBarry Smith { 17690513a670SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 177087828ca2SBarry Smith PetscScalar *vwork; 1771273d9f13SBarry Smith int i,ierr,nz,m = A->m,n = A->n,*cwork; 17720513a670SBarry Smith int *row,*col,*cnew,j,*lens; 177356cd22aeSBarry Smith IS icolp,irowp; 17740513a670SBarry Smith 17753a40ed3dSBarry Smith PetscFunctionBegin; 17764c49b128SBarry Smith ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 177756cd22aeSBarry Smith ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr); 17784c49b128SBarry Smith ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 177956cd22aeSBarry Smith ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr); 17800513a670SBarry Smith 17810513a670SBarry Smith /* determine lengths of permuted rows */ 1782b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&lens);CHKERRQ(ierr); 17830513a670SBarry Smith for (i=0; i<m; i++) { 17840513a670SBarry Smith lens[row[i]] = a->i[i+1] - a->i[i]; 17850513a670SBarry Smith } 17860513a670SBarry Smith ierr = MatCreateSeqAIJ(A->comm,m,n,0,lens,B);CHKERRQ(ierr); 1787606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 17880513a670SBarry Smith 1789b0a32e0cSBarry Smith ierr = PetscMalloc(n*sizeof(int),&cnew);CHKERRQ(ierr); 17900513a670SBarry Smith for (i=0; i<m; i++) { 17910513a670SBarry Smith ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 17920513a670SBarry Smith for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];} 17930513a670SBarry Smith ierr = MatSetValues(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr); 17940513a670SBarry Smith ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 17950513a670SBarry Smith } 1796606d414cSSatish Balay ierr = PetscFree(cnew);CHKERRQ(ierr); 17970513a670SBarry Smith ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17980513a670SBarry Smith ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 179956cd22aeSBarry Smith ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr); 180056cd22aeSBarry Smith ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr); 180156cd22aeSBarry Smith ierr = ISDestroy(irowp);CHKERRQ(ierr); 180256cd22aeSBarry Smith ierr = ISDestroy(icolp);CHKERRQ(ierr); 18033a40ed3dSBarry Smith PetscFunctionReturn(0); 18040513a670SBarry Smith } 18050513a670SBarry Smith 18064a2ae208SSatish Balay #undef __FUNCT__ 18074a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_SeqAIJ" 1808682d7d0cSBarry Smith int MatPrintHelp_SeqAIJ(Mat A) 1809682d7d0cSBarry Smith { 1810c38d4ed2SBarry Smith static PetscTruth called = PETSC_FALSE; 1811682d7d0cSBarry Smith MPI_Comm comm = A->comm; 1812d132466eSBarry Smith int ierr; 1813682d7d0cSBarry Smith 18143a40ed3dSBarry Smith PetscFunctionBegin; 1815c38d4ed2SBarry Smith if (called) {PetscFunctionReturn(0);} else called = PETSC_TRUE; 1816d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," Options for MATSEQAIJ and MATMPIAIJ matrix formats (the defaults):\n");CHKERRQ(ierr); 1817d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_lu_pivotthreshold <threshold>: Set pivoting threshold\n");CHKERRQ(ierr); 1818d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_oneindex: internal indices begin at 1 instead of the default 0.\n");CHKERRQ(ierr); 1819d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_no_inode: Do not use inodes\n");CHKERRQ(ierr); 1820d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_inode_limit <limit>: Set inode limit (max limit=5)\n");CHKERRQ(ierr); 1821aa482453SBarry Smith #if defined(PETSC_HAVE_ESSL) 1822d132466eSBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_essl: Use IBM sparse LU factorization and solve.\n");CHKERRQ(ierr); 1823682d7d0cSBarry Smith #endif 1824cd5578b5SBarry Smith #if defined(PETSC_HAVE_LUSOL) 1825cd5578b5SBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_lusol: Use the Stanford LUSOL sparse factorization and solve.\n");CHKERRQ(ierr); 1826cd5578b5SBarry Smith #endif 182787828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 1828ca44d042SBarry Smith ierr = (*PetscHelpPrintf)(comm," -mat_aij_matlab: Use Matlab engine sparse LU factorization and solve.\n");CHKERRQ(ierr); 1829ca44d042SBarry Smith #endif 18303a40ed3dSBarry Smith PetscFunctionReturn(0); 1831682d7d0cSBarry Smith } 1832ca44d042SBarry Smith EXTERN int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg); 1833ca44d042SBarry Smith EXTERN int MatFDColoringCreate_SeqAIJ(Mat,ISColoring,MatFDColoring); 1834ca44d042SBarry Smith EXTERN int MatILUDTFactor_SeqAIJ(Mat,MatILUInfo*,IS,IS,Mat*); 18354a2ae208SSatish Balay #undef __FUNCT__ 18364a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ" 1837cb5b572fSBarry Smith int MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str) 1838cb5b572fSBarry Smith { 1839be6bf707SBarry Smith int ierr; 1840273d9f13SBarry Smith PetscTruth flg; 1841cb5b572fSBarry Smith 1842cb5b572fSBarry Smith PetscFunctionBegin; 1843273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg);CHKERRQ(ierr); 1844273d9f13SBarry Smith if (str == SAME_NONZERO_PATTERN && flg) { 1845be6bf707SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 1846be6bf707SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 1847be6bf707SBarry Smith 1848273d9f13SBarry Smith if (a->i[A->m]+a->indexshift != b->i[B->m]+a->indexshift) { 184929bbc08cSBarry Smith SETERRQ(1,"Number of nonzeros in two matrices are different"); 1850cb5b572fSBarry Smith } 185187828ca2SBarry Smith ierr = PetscMemcpy(b->a,a->a,(a->i[A->m]+a->indexshift)*sizeof(PetscScalar));CHKERRQ(ierr); 1852cb5b572fSBarry Smith } else { 1853cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1854cb5b572fSBarry Smith } 1855cb5b572fSBarry Smith PetscFunctionReturn(0); 1856cb5b572fSBarry Smith } 1857cb5b572fSBarry Smith 18584a2ae208SSatish Balay #undef __FUNCT__ 18594a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ" 1860273d9f13SBarry Smith int MatSetUpPreallocation_SeqAIJ(Mat A) 1861273d9f13SBarry Smith { 1862273d9f13SBarry Smith int ierr; 1863273d9f13SBarry Smith 1864273d9f13SBarry Smith PetscFunctionBegin; 1865273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(A,PETSC_DEFAULT,0);CHKERRQ(ierr); 1866273d9f13SBarry Smith PetscFunctionReturn(0); 1867273d9f13SBarry Smith } 1868273d9f13SBarry Smith 18694a2ae208SSatish Balay #undef __FUNCT__ 18704a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ" 187187828ca2SBarry Smith int MatGetArray_SeqAIJ(Mat A,PetscScalar **array) 18726c0721eeSBarry Smith { 18736c0721eeSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 18746c0721eeSBarry Smith PetscFunctionBegin; 18756c0721eeSBarry Smith *array = a->a; 18766c0721eeSBarry Smith PetscFunctionReturn(0); 18776c0721eeSBarry Smith } 18786c0721eeSBarry Smith 18794a2ae208SSatish Balay #undef __FUNCT__ 18804a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ" 188187828ca2SBarry Smith int MatRestoreArray_SeqAIJ(Mat A,PetscScalar **array) 18826c0721eeSBarry Smith { 18836c0721eeSBarry Smith PetscFunctionBegin; 18846c0721eeSBarry Smith PetscFunctionReturn(0); 18856c0721eeSBarry Smith } 1886273d9f13SBarry Smith 1887ee4f033dSBarry Smith #undef __FUNCT__ 1888ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ" 1889ee4f033dSBarry Smith int MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx) 1890ee4f033dSBarry Smith { 1891ee4f033dSBarry Smith int (*f)(void *,Vec,Vec,void*) = (int (*)(void *,Vec,Vec,void *))coloring->f; 1892ee4f033dSBarry Smith int k,ierr,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2; 189387828ca2SBarry Smith PetscScalar dx,mone = -1.0,*y,*xx,*w3_array; 189487828ca2SBarry Smith PetscScalar *vscale_array; 1895ee4f033dSBarry Smith PetscReal epsilon = coloring->error_rel,umin = coloring->umin; 1896ee4f033dSBarry Smith Vec w1,w2,w3; 1897ee4f033dSBarry Smith void *fctx = coloring->fctx; 1898ee4f033dSBarry Smith PetscTruth flg; 1899ee4f033dSBarry Smith 1900ee4f033dSBarry Smith PetscFunctionBegin; 1901ee4f033dSBarry Smith if (!coloring->w1) { 1902ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr); 1903ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w1); 1904ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr); 1905ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w2); 1906ee4f033dSBarry Smith ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr); 1907ee4f033dSBarry Smith PetscLogObjectParent(coloring,coloring->w3); 1908ee4f033dSBarry Smith } 1909ee4f033dSBarry Smith w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3; 1910ee4f033dSBarry Smith 1911ee4f033dSBarry Smith ierr = MatSetUnfactored(J);CHKERRQ(ierr); 1912ee4f033dSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_fd_coloring_dont_rezero",&flg);CHKERRQ(ierr); 1913ee4f033dSBarry Smith if (flg) { 1914ee4f033dSBarry Smith PetscLogInfo(coloring,"MatFDColoringApply_SeqAIJ: Not calling MatZeroEntries()\n"); 1915ee4f033dSBarry Smith } else { 1916ee4f033dSBarry Smith ierr = MatZeroEntries(J);CHKERRQ(ierr); 1917ee4f033dSBarry Smith } 1918ee4f033dSBarry Smith 1919ee4f033dSBarry Smith ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr); 1920ee4f033dSBarry Smith ierr = VecGetSize(x1,&N);CHKERRQ(ierr); 1921ee4f033dSBarry Smith 1922ee4f033dSBarry Smith /* 1923ee4f033dSBarry Smith This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets 1924ee4f033dSBarry Smith coloring->F for the coarser grids from the finest 1925ee4f033dSBarry Smith */ 1926ee4f033dSBarry Smith if (coloring->F) { 1927ee4f033dSBarry Smith ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr); 1928ee4f033dSBarry Smith ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr); 1929ee4f033dSBarry Smith if (m1 != m2) { 1930ee4f033dSBarry Smith coloring->F = 0; 1931ee4f033dSBarry Smith } 1932ee4f033dSBarry Smith } 1933ee4f033dSBarry Smith 1934ee4f033dSBarry Smith if (coloring->F) { 1935ee4f033dSBarry Smith w1 = coloring->F; 1936ee4f033dSBarry Smith coloring->F = 0; 1937ee4f033dSBarry Smith } else { 1938ee4f033dSBarry Smith ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr); 1939ee4f033dSBarry Smith } 1940ee4f033dSBarry Smith 1941ee4f033dSBarry Smith /* 1942ee4f033dSBarry Smith Compute all the scale factors and share with other processors 1943ee4f033dSBarry Smith */ 1944ee4f033dSBarry Smith ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start; 1945ee4f033dSBarry Smith ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start; 1946ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 1947ee4f033dSBarry Smith /* 1948ee4f033dSBarry Smith Loop over each column associated with color adding the 1949ee4f033dSBarry Smith perturbation to the vector w3. 1950ee4f033dSBarry Smith */ 1951ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 1952ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 1953ee4f033dSBarry Smith dx = xx[col]; 1954ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 1955ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 1956ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 1957ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 1958ee4f033dSBarry Smith #else 1959ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 1960ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 1961ee4f033dSBarry Smith #endif 1962ee4f033dSBarry Smith dx *= epsilon; 1963ee4f033dSBarry Smith vscale_array[col] = 1.0/dx; 1964ee4f033dSBarry Smith } 1965ee4f033dSBarry Smith } 1966ee4f033dSBarry Smith vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 1967ee4f033dSBarry Smith ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1968ee4f033dSBarry Smith ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1969ee4f033dSBarry Smith 1970ee4f033dSBarry Smith /* ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD); 1971ee4f033dSBarry Smith ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/ 1972ee4f033dSBarry Smith 1973ee4f033dSBarry Smith if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow; 1974ee4f033dSBarry Smith else vscaleforrow = coloring->columnsforrow; 1975ee4f033dSBarry Smith 1976ee4f033dSBarry Smith ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 1977ee4f033dSBarry Smith /* 1978ee4f033dSBarry Smith Loop over each color 1979ee4f033dSBarry Smith */ 1980ee4f033dSBarry Smith for (k=0; k<coloring->ncolors; k++) { 1981ee4f033dSBarry Smith ierr = VecCopy(x1,w3);CHKERRQ(ierr); 1982ee4f033dSBarry Smith ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start; 1983ee4f033dSBarry Smith /* 1984ee4f033dSBarry Smith Loop over each column associated with color adding the 1985ee4f033dSBarry Smith perturbation to the vector w3. 1986ee4f033dSBarry Smith */ 1987ee4f033dSBarry Smith for (l=0; l<coloring->ncolumns[k]; l++) { 1988ee4f033dSBarry Smith col = coloring->columns[k][l]; /* column of the matrix we are probing for */ 1989ee4f033dSBarry Smith dx = xx[col]; 1990ee4f033dSBarry Smith if (dx == 0.0) dx = 1.0; 1991ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX) 1992ee4f033dSBarry Smith if (dx < umin && dx >= 0.0) dx = umin; 1993ee4f033dSBarry Smith else if (dx < 0.0 && dx > -umin) dx = -umin; 1994ee4f033dSBarry Smith #else 1995ee4f033dSBarry Smith if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0) dx = umin; 1996ee4f033dSBarry Smith else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin; 1997ee4f033dSBarry Smith #endif 1998ee4f033dSBarry Smith dx *= epsilon; 1999ee4f033dSBarry Smith if (!PetscAbsScalar(dx)) SETERRQ(1,"Computed 0 differencing parameter"); 2000ee4f033dSBarry Smith w3_array[col] += dx; 2001ee4f033dSBarry Smith } 2002ee4f033dSBarry Smith w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr); 2003ee4f033dSBarry Smith 2004ee4f033dSBarry Smith /* 2005ee4f033dSBarry Smith Evaluate function at x1 + dx (here dx is a vector of perturbations) 2006ee4f033dSBarry Smith */ 2007ee4f033dSBarry Smith 2008ee4f033dSBarry Smith ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr); 2009ee4f033dSBarry Smith ierr = VecAXPY(&mone,w1,w2);CHKERRQ(ierr); 2010ee4f033dSBarry Smith 2011ee4f033dSBarry Smith /* 2012ee4f033dSBarry Smith Loop over rows of vector, putting results into Jacobian matrix 2013ee4f033dSBarry Smith */ 2014ee4f033dSBarry Smith ierr = VecGetArray(w2,&y);CHKERRQ(ierr); 2015ee4f033dSBarry Smith for (l=0; l<coloring->nrows[k]; l++) { 2016ee4f033dSBarry Smith row = coloring->rows[k][l]; 2017ee4f033dSBarry Smith col = coloring->columnsforrow[k][l]; 2018ee4f033dSBarry Smith y[row] *= vscale_array[vscaleforrow[k][l]]; 2019ee4f033dSBarry Smith srow = row + start; 2020ee4f033dSBarry Smith ierr = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr); 2021ee4f033dSBarry Smith } 2022ee4f033dSBarry Smith ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr); 2023ee4f033dSBarry Smith } 2024ee4f033dSBarry Smith ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr); 2025ee4f033dSBarry Smith xx = xx + start; ierr = VecRestoreArray(x1,&xx);CHKERRQ(ierr); 2026ee4f033dSBarry Smith ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2027ee4f033dSBarry Smith ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2028ee4f033dSBarry Smith PetscFunctionReturn(0); 2029ee4f033dSBarry Smith } 2030ee4f033dSBarry Smith 2031ac90fabeSBarry Smith #include "petscblaslapack.h" 2032ac90fabeSBarry Smith 2033ac90fabeSBarry Smith #undef __FUNCT__ 2034ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ" 2035ac90fabeSBarry Smith int MatAXPY_SeqAIJ(PetscScalar *a,Mat X,Mat Y,MatStructure str) 2036ac90fabeSBarry Smith { 2037*556bd4faSSatish Balay int ierr,one=1; 2038ac90fabeSBarry Smith Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data; 2039ac90fabeSBarry Smith 2040ac90fabeSBarry Smith PetscFunctionBegin; 2041ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2042ac90fabeSBarry Smith BLaxpy_(&x->nz,a,x->a,&one,y->a,&one); 2043ac90fabeSBarry Smith } else { 2044ac90fabeSBarry Smith ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr); 2045ac90fabeSBarry Smith } 2046ac90fabeSBarry Smith PetscFunctionReturn(0); 2047ac90fabeSBarry Smith } 2048ac90fabeSBarry Smith 2049ac90fabeSBarry Smith 2050682d7d0cSBarry Smith /* -------------------------------------------------------------------*/ 20510a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ, 2052cb5b572fSBarry Smith MatGetRow_SeqAIJ, 2053cb5b572fSBarry Smith MatRestoreRow_SeqAIJ, 2054cb5b572fSBarry Smith MatMult_SeqAIJ, 2055cb5b572fSBarry Smith MatMultAdd_SeqAIJ, 20567c922b88SBarry Smith MatMultTranspose_SeqAIJ, 20577c922b88SBarry Smith MatMultTransposeAdd_SeqAIJ, 2058cb5b572fSBarry Smith MatSolve_SeqAIJ, 2059cb5b572fSBarry Smith MatSolveAdd_SeqAIJ, 20607c922b88SBarry Smith MatSolveTranspose_SeqAIJ, 20617c922b88SBarry Smith MatSolveTransposeAdd_SeqAIJ, 2062cb5b572fSBarry Smith MatLUFactor_SeqAIJ, 2063cb5b572fSBarry Smith 0, 206417ab2063SBarry Smith MatRelax_SeqAIJ, 206517ab2063SBarry Smith MatTranspose_SeqAIJ, 2066cb5b572fSBarry Smith MatGetInfo_SeqAIJ, 2067cb5b572fSBarry Smith MatEqual_SeqAIJ, 2068cb5b572fSBarry Smith MatGetDiagonal_SeqAIJ, 2069cb5b572fSBarry Smith MatDiagonalScale_SeqAIJ, 2070cb5b572fSBarry Smith MatNorm_SeqAIJ, 2071cb5b572fSBarry Smith 0, 2072cb5b572fSBarry Smith MatAssemblyEnd_SeqAIJ, 207317ab2063SBarry Smith MatCompress_SeqAIJ, 2074cb5b572fSBarry Smith MatSetOption_SeqAIJ, 2075cb5b572fSBarry Smith MatZeroEntries_SeqAIJ, 2076cb5b572fSBarry Smith MatZeroRows_SeqAIJ, 2077cb5b572fSBarry Smith MatLUFactorSymbolic_SeqAIJ, 2078cb5b572fSBarry Smith MatLUFactorNumeric_SeqAIJ, 2079cb5b572fSBarry Smith 0, 2080cb5b572fSBarry Smith 0, 2081273d9f13SBarry Smith MatSetUpPreallocation_SeqAIJ, 2082cb5b572fSBarry Smith MatILUFactorSymbolic_SeqAIJ, 2083cb5b572fSBarry Smith 0, 20846c0721eeSBarry Smith MatGetArray_SeqAIJ, 20856c0721eeSBarry Smith MatRestoreArray_SeqAIJ, 2086be6bf707SBarry Smith MatDuplicate_SeqAIJ, 2087cb5b572fSBarry Smith 0, 2088cb5b572fSBarry Smith 0, 2089cb5b572fSBarry Smith MatILUFactor_SeqAIJ, 2090cb5b572fSBarry Smith 0, 2091ac90fabeSBarry Smith MatAXPY_SeqAIJ, 2092cb5b572fSBarry Smith MatGetSubMatrices_SeqAIJ, 2093cb5b572fSBarry Smith MatIncreaseOverlap_SeqAIJ, 2094cb5b572fSBarry Smith MatGetValues_SeqAIJ, 2095cb5b572fSBarry Smith MatCopy_SeqAIJ, 2096f0b747eeSBarry Smith MatPrintHelp_SeqAIJ, 2097cb5b572fSBarry Smith MatScale_SeqAIJ, 2098cb5b572fSBarry Smith 0, 2099cb5b572fSBarry Smith 0, 21006945ee14SBarry Smith MatILUDTFactor_SeqAIJ, 21016945ee14SBarry Smith MatGetBlockSize_SeqAIJ, 21023b2fbd54SBarry Smith MatGetRowIJ_SeqAIJ, 21033b2fbd54SBarry Smith MatRestoreRowIJ_SeqAIJ, 21043b2fbd54SBarry Smith MatGetColumnIJ_SeqAIJ, 2105a93ec695SBarry Smith MatRestoreColumnIJ_SeqAIJ, 2106a93ec695SBarry Smith MatFDColoringCreate_SeqAIJ, 2107b9617806SBarry Smith 0, 21080513a670SBarry Smith 0, 2109cda55fadSBarry Smith MatPermute_SeqAIJ, 2110cda55fadSBarry Smith 0, 2111cda55fadSBarry Smith 0, 2112b9b97703SBarry Smith MatDestroy_SeqAIJ, 2113b9b97703SBarry Smith MatView_SeqAIJ, 21148a124369SBarry Smith MatGetPetscMaps_Petsc, 2115ee4f033dSBarry Smith 0, 2116ee4f033dSBarry Smith 0, 2117ee4f033dSBarry Smith 0, 2118ee4f033dSBarry Smith 0, 2119ee4f033dSBarry Smith 0, 2120ee4f033dSBarry Smith 0, 2121ee4f033dSBarry Smith 0, 2122ee4f033dSBarry Smith 0, 2123ee4f033dSBarry Smith MatSetColoring_SeqAIJ, 2124ee4f033dSBarry Smith MatSetValuesAdic_SeqAIJ, 2125ee4f033dSBarry Smith MatSetValuesAdifor_SeqAIJ, 2126ee4f033dSBarry Smith MatFDColoringApply_SeqAIJ}; 212717ab2063SBarry Smith 2128ca44d042SBarry Smith EXTERN int MatUseSuperLU_SeqAIJ(Mat); 2129ca44d042SBarry Smith EXTERN int MatUseEssl_SeqAIJ(Mat); 2130cd5578b5SBarry Smith EXTERN int MatUseLUSOL_SeqAIJ(Mat); 2131ca44d042SBarry Smith EXTERN int MatUseMatlab_SeqAIJ(Mat); 2132ca44d042SBarry Smith EXTERN int MatUseDXML_SeqAIJ(Mat); 213317ab2063SBarry Smith 2134fb2e594dSBarry Smith EXTERN_C_BEGIN 21354a2ae208SSatish Balay #undef __FUNCT__ 21364a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ" 213715091d37SBarry Smith 2138bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,int *indices) 2139bef8e0ddSBarry Smith { 2140bef8e0ddSBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2141bef8e0ddSBarry Smith int i,nz,n; 2142bef8e0ddSBarry Smith 2143bef8e0ddSBarry Smith PetscFunctionBegin; 2144bef8e0ddSBarry Smith 2145bef8e0ddSBarry Smith nz = aij->maxnz; 2146273d9f13SBarry Smith n = mat->n; 2147bef8e0ddSBarry Smith for (i=0; i<nz; i++) { 2148bef8e0ddSBarry Smith aij->j[i] = indices[i]; 2149bef8e0ddSBarry Smith } 2150bef8e0ddSBarry Smith aij->nz = nz; 2151bef8e0ddSBarry Smith for (i=0; i<n; i++) { 2152bef8e0ddSBarry Smith aij->ilen[i] = aij->imax[i]; 2153bef8e0ddSBarry Smith } 2154bef8e0ddSBarry Smith 2155bef8e0ddSBarry Smith PetscFunctionReturn(0); 2156bef8e0ddSBarry Smith } 2157fb2e594dSBarry Smith EXTERN_C_END 2158bef8e0ddSBarry Smith 21594a2ae208SSatish Balay #undef __FUNCT__ 21604a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices" 2161bef8e0ddSBarry Smith /*@ 2162bef8e0ddSBarry Smith MatSeqAIJSetColumnIndices - Set the column indices for all the rows 2163bef8e0ddSBarry Smith in the matrix. 2164bef8e0ddSBarry Smith 2165bef8e0ddSBarry Smith Input Parameters: 2166bef8e0ddSBarry Smith + mat - the SeqAIJ matrix 2167bef8e0ddSBarry Smith - indices - the column indices 2168bef8e0ddSBarry Smith 216915091d37SBarry Smith Level: advanced 217015091d37SBarry Smith 2171bef8e0ddSBarry Smith Notes: 2172bef8e0ddSBarry Smith This can be called if you have precomputed the nonzero structure of the 2173bef8e0ddSBarry Smith matrix and want to provide it to the matrix object to improve the performance 2174bef8e0ddSBarry Smith of the MatSetValues() operation. 2175bef8e0ddSBarry Smith 2176bef8e0ddSBarry Smith You MUST have set the correct numbers of nonzeros per row in the call to 2177bef8e0ddSBarry Smith MatCreateSeqAIJ(). 2178bef8e0ddSBarry Smith 2179bef8e0ddSBarry Smith MUST be called before any calls to MatSetValues(); 2180bef8e0ddSBarry Smith 2181b9617806SBarry Smith The indices should start with zero, not one. 2182b9617806SBarry Smith 2183bef8e0ddSBarry Smith @*/ 2184bef8e0ddSBarry Smith int MatSeqAIJSetColumnIndices(Mat mat,int *indices) 2185bef8e0ddSBarry Smith { 2186bef8e0ddSBarry Smith int ierr,(*f)(Mat,int *); 2187bef8e0ddSBarry Smith 2188bef8e0ddSBarry Smith PetscFunctionBegin; 2189bef8e0ddSBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 2190c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr); 2191bef8e0ddSBarry Smith if (f) { 2192bef8e0ddSBarry Smith ierr = (*f)(mat,indices);CHKERRQ(ierr); 2193bef8e0ddSBarry Smith } else { 219429bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to set column indices"); 2195bef8e0ddSBarry Smith } 2196bef8e0ddSBarry Smith PetscFunctionReturn(0); 2197bef8e0ddSBarry Smith } 2198bef8e0ddSBarry Smith 2199be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/ 2200be6bf707SBarry Smith 2201fb2e594dSBarry Smith EXTERN_C_BEGIN 22024a2ae208SSatish Balay #undef __FUNCT__ 22034a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ" 2204be6bf707SBarry Smith int MatStoreValues_SeqAIJ(Mat mat) 2205be6bf707SBarry Smith { 2206be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2207273d9f13SBarry Smith int nz = aij->i[mat->m]+aij->indexshift,ierr; 2208be6bf707SBarry Smith 2209be6bf707SBarry Smith PetscFunctionBegin; 2210be6bf707SBarry Smith if (aij->nonew != 1) { 221129bbc08cSBarry Smith SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2212be6bf707SBarry Smith } 2213be6bf707SBarry Smith 2214be6bf707SBarry Smith /* allocate space for values if not already there */ 2215be6bf707SBarry Smith if (!aij->saved_values) { 221687828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr); 2217be6bf707SBarry Smith } 2218be6bf707SBarry Smith 2219be6bf707SBarry Smith /* copy values over */ 222087828ca2SBarry Smith ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2221be6bf707SBarry Smith PetscFunctionReturn(0); 2222be6bf707SBarry Smith } 2223fb2e594dSBarry Smith EXTERN_C_END 2224be6bf707SBarry Smith 22254a2ae208SSatish Balay #undef __FUNCT__ 2226b9617806SBarry Smith #define __FUNCT__ "MatStoreValues" 2227be6bf707SBarry Smith /*@ 2228be6bf707SBarry Smith MatStoreValues - Stashes a copy of the matrix values; this allows, for 2229be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2230be6bf707SBarry Smith nonlinear portion. 2231be6bf707SBarry Smith 2232be6bf707SBarry Smith Collect on Mat 2233be6bf707SBarry Smith 2234be6bf707SBarry Smith Input Parameters: 2235be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2236be6bf707SBarry Smith 223715091d37SBarry Smith Level: advanced 223815091d37SBarry Smith 2239be6bf707SBarry Smith Common Usage, with SNESSolve(): 2240be6bf707SBarry Smith $ Create Jacobian matrix 2241be6bf707SBarry Smith $ Set linear terms into matrix 2242be6bf707SBarry Smith $ Apply boundary conditions to matrix, at this time matrix must have 2243be6bf707SBarry Smith $ final nonzero structure (i.e. setting the nonlinear terms and applying 2244be6bf707SBarry Smith $ boundary conditions again will not change the nonzero structure 2245be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2246be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2247be6bf707SBarry Smith $ Call SNESSetJacobian() with matrix 2248be6bf707SBarry Smith $ In your Jacobian routine 2249be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2250be6bf707SBarry Smith $ Set nonlinear terms in matrix 2251be6bf707SBarry Smith 2252be6bf707SBarry Smith Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself: 2253be6bf707SBarry Smith $ // build linear portion of Jacobian 2254be6bf707SBarry Smith $ ierr = MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); 2255be6bf707SBarry Smith $ ierr = MatStoreValues(mat); 2256be6bf707SBarry Smith $ loop over nonlinear iterations 2257be6bf707SBarry Smith $ ierr = MatRetrieveValues(mat); 2258be6bf707SBarry Smith $ // call MatSetValues(mat,...) to set nonliner portion of Jacobian 2259be6bf707SBarry Smith $ // call MatAssemblyBegin/End() on matrix 2260be6bf707SBarry Smith $ Solve linear system with Jacobian 2261be6bf707SBarry Smith $ endloop 2262be6bf707SBarry Smith 2263be6bf707SBarry Smith Notes: 2264be6bf707SBarry Smith Matrix must already be assemblied before calling this routine 2265be6bf707SBarry Smith Must set the matrix option MatSetOption(mat,MAT_NO_NEW_NONZERO_LOCATIONS); before 2266be6bf707SBarry Smith calling this routine. 2267be6bf707SBarry Smith 2268be6bf707SBarry Smith .seealso: MatRetrieveValues() 2269be6bf707SBarry Smith 2270be6bf707SBarry Smith @*/ 2271be6bf707SBarry Smith int MatStoreValues(Mat mat) 2272be6bf707SBarry Smith { 2273be6bf707SBarry Smith int ierr,(*f)(Mat); 2274be6bf707SBarry Smith 2275be6bf707SBarry Smith PetscFunctionBegin; 2276be6bf707SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 227729bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 227829bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2279be6bf707SBarry Smith 2280c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2281be6bf707SBarry Smith if (f) { 2282be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2283be6bf707SBarry Smith } else { 228429bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to store values"); 2285be6bf707SBarry Smith } 2286be6bf707SBarry Smith PetscFunctionReturn(0); 2287be6bf707SBarry Smith } 2288be6bf707SBarry Smith 2289fb2e594dSBarry Smith EXTERN_C_BEGIN 22904a2ae208SSatish Balay #undef __FUNCT__ 22914a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ" 2292be6bf707SBarry Smith int MatRetrieveValues_SeqAIJ(Mat mat) 2293be6bf707SBarry Smith { 2294be6bf707SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data; 2295273d9f13SBarry Smith int nz = aij->i[mat->m]+aij->indexshift,ierr; 2296be6bf707SBarry Smith 2297be6bf707SBarry Smith PetscFunctionBegin; 2298be6bf707SBarry Smith if (aij->nonew != 1) { 229929bbc08cSBarry Smith SETERRQ(1,"Must call MatSetOption(A,MAT_NO_NEW_NONZERO_LOCATIONS);first"); 2300be6bf707SBarry Smith } 2301be6bf707SBarry Smith if (!aij->saved_values) { 230229bbc08cSBarry Smith SETERRQ(1,"Must call MatStoreValues(A);first"); 2303be6bf707SBarry Smith } 2304be6bf707SBarry Smith 2305be6bf707SBarry Smith /* copy values over */ 230687828ca2SBarry Smith ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr); 2307be6bf707SBarry Smith PetscFunctionReturn(0); 2308be6bf707SBarry Smith } 2309fb2e594dSBarry Smith EXTERN_C_END 2310be6bf707SBarry Smith 23114a2ae208SSatish Balay #undef __FUNCT__ 23124a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues" 2313be6bf707SBarry Smith /*@ 2314be6bf707SBarry Smith MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for 2315be6bf707SBarry Smith example, reuse of the linear part of a Jacobian, while recomputing the 2316be6bf707SBarry Smith nonlinear portion. 2317be6bf707SBarry Smith 2318be6bf707SBarry Smith Collect on Mat 2319be6bf707SBarry Smith 2320be6bf707SBarry Smith Input Parameters: 2321be6bf707SBarry Smith . mat - the matrix (currently on AIJ matrices support this option) 2322be6bf707SBarry Smith 232315091d37SBarry Smith Level: advanced 232415091d37SBarry Smith 2325be6bf707SBarry Smith .seealso: MatStoreValues() 2326be6bf707SBarry Smith 2327be6bf707SBarry Smith @*/ 2328be6bf707SBarry Smith int MatRetrieveValues(Mat mat) 2329be6bf707SBarry Smith { 2330be6bf707SBarry Smith int ierr,(*f)(Mat); 2331be6bf707SBarry Smith 2332be6bf707SBarry Smith PetscFunctionBegin; 2333be6bf707SBarry Smith PetscValidHeaderSpecific(mat,MAT_COOKIE); 233429bbc08cSBarry Smith if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 233529bbc08cSBarry Smith if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2336be6bf707SBarry Smith 2337c134de8dSSatish Balay ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr); 2338be6bf707SBarry Smith if (f) { 2339be6bf707SBarry Smith ierr = (*f)(mat);CHKERRQ(ierr); 2340be6bf707SBarry Smith } else { 234129bbc08cSBarry Smith SETERRQ(1,"Wrong type of matrix to retrieve values"); 2342be6bf707SBarry Smith } 2343be6bf707SBarry Smith PetscFunctionReturn(0); 2344be6bf707SBarry Smith } 2345be6bf707SBarry Smith 2346f83d6046SBarry Smith /* 2347f83d6046SBarry Smith This allows SeqAIJ matrices to be passed to the matlab engine 2348f83d6046SBarry Smith */ 234987828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 2350f83d6046SBarry Smith #include "engine.h" /* Matlab include file */ 2351f83d6046SBarry Smith #include "mex.h" /* Matlab include file */ 2352f83d6046SBarry Smith EXTERN_C_BEGIN 23534a2ae208SSatish Balay #undef __FUNCT__ 23544a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEnginePut_SeqAIJ" 2355f83d6046SBarry Smith int MatMatlabEnginePut_SeqAIJ(PetscObject obj,void *engine) 2356f83d6046SBarry Smith { 235766826eb6SBarry Smith int ierr,i,*ai,*aj; 2358f83d6046SBarry Smith Mat B = (Mat)obj; 235987828ca2SBarry Smith PetscScalar *array; 2360f83d6046SBarry Smith mxArray *mat; 2361d79a51d8SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)B->data; 2362d79a51d8SBarry Smith 2363f83d6046SBarry Smith PetscFunctionBegin; 236401820c62SBarry Smith mat = mxCreateSparse(B->n,B->m,aij->nz,mxREAL); 236587828ca2SBarry Smith ierr = PetscMemcpy(mxGetPr(mat),aij->a,aij->nz*sizeof(PetscScalar));CHKERRQ(ierr); 2366d79a51d8SBarry Smith /* Matlab stores by column, not row so we pass in the transpose of the matrix */ 236766826eb6SBarry Smith ierr = PetscMemcpy(mxGetIr(mat),aij->j,aij->nz*sizeof(int));CHKERRQ(ierr); 236866826eb6SBarry Smith ierr = PetscMemcpy(mxGetJc(mat),aij->i,(B->m+1)*sizeof(int));CHKERRQ(ierr); 2369f83d6046SBarry Smith 237001820c62SBarry Smith /* Matlab indices start at 0 for sparse (what a surprise) */ 237101820c62SBarry Smith if (aij->indexshift) { 237201820c62SBarry Smith for (i=0; i<B->m+1; i++) { 237301820c62SBarry Smith ai[i]--; 2374d79a51d8SBarry Smith } 2375d79a51d8SBarry Smith for (i=0; i<aij->nz; i++) { 237601820c62SBarry Smith aj[i]--; 2377d79a51d8SBarry Smith } 2378d79a51d8SBarry Smith } 2379ca44d042SBarry Smith ierr = PetscObjectName(obj);CHKERRQ(ierr); 2380f83d6046SBarry Smith mxSetName(mat,obj->name); 2381f83d6046SBarry Smith engPutArray((Engine *)engine,mat); 2382f83d6046SBarry Smith PetscFunctionReturn(0); 2383f83d6046SBarry Smith } 2384f83d6046SBarry Smith EXTERN_C_END 238566826eb6SBarry Smith 238666826eb6SBarry Smith EXTERN_C_BEGIN 23874a2ae208SSatish Balay #undef __FUNCT__ 23884a2ae208SSatish Balay #define __FUNCT__ "MatMatlabEngineGet_SeqAIJ" 238966826eb6SBarry Smith int MatMatlabEngineGet_SeqAIJ(PetscObject obj,void *engine) 239066826eb6SBarry Smith { 239166826eb6SBarry Smith int ierr,ii; 239266826eb6SBarry Smith Mat mat = (Mat)obj; 239366826eb6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data; 239466826eb6SBarry Smith mxArray *mmat; 239566826eb6SBarry Smith 23966c0721eeSBarry Smith PetscFunctionBegin; 239766826eb6SBarry Smith ierr = PetscFree(aij->a);CHKERRQ(ierr); 239866826eb6SBarry Smith aij->indexshift = 0; 239966826eb6SBarry Smith 240066826eb6SBarry Smith mmat = engGetArray((Engine *)engine,obj->name); 240166826eb6SBarry Smith 2402a65776f3SBarry Smith aij->nz = (mxGetJc(mmat))[mat->m]; 240387828ca2SBarry Smith ierr = PetscMalloc(aij->nz*(sizeof(int)+sizeof(PetscScalar))+(mat->m+1)*sizeof(int),&aij->a);CHKERRQ(ierr); 240466826eb6SBarry Smith aij->j = (int*)(aij->a + aij->nz); 240566826eb6SBarry Smith aij->i = aij->j + aij->nz; 240666826eb6SBarry Smith aij->singlemalloc = PETSC_TRUE; 240766826eb6SBarry Smith aij->freedata = PETSC_TRUE; 240866826eb6SBarry Smith 240987828ca2SBarry Smith ierr = PetscMemcpy(aij->a,mxGetPr(mmat),aij->nz*sizeof(PetscScalar));CHKERRQ(ierr); 241066826eb6SBarry Smith /* Matlab stores by column, not row so we pass in the transpose of the matrix */ 241166826eb6SBarry Smith ierr = PetscMemcpy(aij->j,mxGetIr(mmat),aij->nz*sizeof(int));CHKERRQ(ierr); 241266826eb6SBarry Smith ierr = PetscMemcpy(aij->i,mxGetJc(mmat),(mat->m+1)*sizeof(int));CHKERRQ(ierr); 241366826eb6SBarry Smith 241466826eb6SBarry Smith for (ii=0; ii<mat->m; ii++) { 241566826eb6SBarry Smith aij->ilen[ii] = aij->imax[ii] = aij->i[ii+1] - aij->i[ii]; 241666826eb6SBarry Smith } 241766826eb6SBarry Smith 241866826eb6SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 241966826eb6SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 242066826eb6SBarry Smith 242166826eb6SBarry Smith PetscFunctionReturn(0); 242266826eb6SBarry Smith } 242366826eb6SBarry Smith EXTERN_C_END 2424f83d6046SBarry Smith #endif 2425f83d6046SBarry Smith 2426be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/ 2427cb5b572fSBarry Smith 24284a2ae208SSatish Balay #undef __FUNCT__ 24294a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ" 243017ab2063SBarry Smith /*@C 2431682d7d0cSBarry Smith MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format 24320d15e28bSLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 24336e62573dSLois Curfman McInnes the user should preallocate the matrix storage by setting the parameter nz 243451c19458SBarry Smith (or the array nnz). By setting these parameters accurately, performance 24352bd5e0b2SLois Curfman McInnes during matrix assembly can be increased by more than a factor of 50. 243617ab2063SBarry Smith 2437db81eaa0SLois Curfman McInnes Collective on MPI_Comm 2438db81eaa0SLois Curfman McInnes 243917ab2063SBarry Smith Input Parameters: 2440db81eaa0SLois Curfman McInnes + comm - MPI communicator, set to PETSC_COMM_SELF 244117ab2063SBarry Smith . m - number of rows 244217ab2063SBarry Smith . n - number of columns 244317ab2063SBarry Smith . nz - number of nonzeros per row (same for all rows) 244451c19458SBarry Smith - nnz - array containing the number of nonzeros in the various rows 24452bd5e0b2SLois Curfman McInnes (possibly different for each row) or PETSC_NULL 244617ab2063SBarry Smith 244717ab2063SBarry Smith Output Parameter: 2448416022c9SBarry Smith . A - the matrix 244917ab2063SBarry Smith 2450b259b22eSLois Curfman McInnes Notes: 245117ab2063SBarry Smith The AIJ format (also called the Yale sparse matrix format or 245217ab2063SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 24530002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 245444cd7ae7SLois Curfman McInnes either one (as in Fortran) or zero. See the users' manual for details. 245517ab2063SBarry Smith 245617ab2063SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2457a40aa06bSLois Curfman McInnes Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 24583d323bbdSBarry Smith allocation. For large problems you MUST preallocate memory or you 24596da5968aSLois Curfman McInnes will get TERRIBLE performance, see the users' manual chapter on matrices. 246017ab2063SBarry Smith 2461682d7d0cSBarry Smith By default, this format uses inodes (identical nodes) when possible, to 24624fca80b9SLois Curfman McInnes improve numerical efficiency of matrix-vector products and solves. We 2463682d7d0cSBarry Smith search for consecutive rows with the same nonzero structure, thereby 24646c7ebb05SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 24656c7ebb05SLois Curfman McInnes 24666c7ebb05SLois Curfman McInnes Options Database Keys: 2467db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 2468db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2469db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 2470db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 2471db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 247217ab2063SBarry Smith 2473027ccd11SLois Curfman McInnes Level: intermediate 2474027ccd11SLois Curfman McInnes 247536db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 247636db0b34SBarry Smith 247717ab2063SBarry Smith @*/ 2478416022c9SBarry Smith int MatCreateSeqAIJ(MPI_Comm comm,int m,int n,int nz,int *nnz,Mat *A) 247917ab2063SBarry Smith { 2480273d9f13SBarry Smith int ierr; 24816945ee14SBarry Smith 24823a40ed3dSBarry Smith PetscFunctionBegin; 2483273d9f13SBarry Smith ierr = MatCreate(comm,m,n,m,n,A);CHKERRQ(ierr); 2484273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2485273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,nz,nnz);CHKERRQ(ierr); 2486273d9f13SBarry Smith PetscFunctionReturn(0); 2487273d9f13SBarry Smith } 2488273d9f13SBarry Smith 24894a2ae208SSatish Balay #undef __FUNCT__ 24904a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation" 2491273d9f13SBarry Smith /*@C 2492273d9f13SBarry Smith MatSeqAIJSetPreallocation - For good matrix assembly performance 2493273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameter nz 2494273d9f13SBarry Smith (or the array nnz). By setting these parameters accurately, performance 2495273d9f13SBarry Smith during matrix assembly can be increased by more than a factor of 50. 2496273d9f13SBarry Smith 2497273d9f13SBarry Smith Collective on MPI_Comm 2498273d9f13SBarry Smith 2499273d9f13SBarry Smith Input Parameters: 2500273d9f13SBarry Smith + comm - MPI communicator, set to PETSC_COMM_SELF 2501273d9f13SBarry Smith . m - number of rows 2502273d9f13SBarry Smith . n - number of columns 2503273d9f13SBarry Smith . nz - number of nonzeros per row (same for all rows) 2504273d9f13SBarry Smith - nnz - array containing the number of nonzeros in the various rows 2505273d9f13SBarry Smith (possibly different for each row) or PETSC_NULL 2506273d9f13SBarry Smith 2507273d9f13SBarry Smith Output Parameter: 2508273d9f13SBarry Smith . A - the matrix 2509273d9f13SBarry Smith 2510273d9f13SBarry Smith Notes: 2511273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2512273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2513273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2514273d9f13SBarry Smith either one (as in Fortran) or zero. See the users' manual for details. 2515273d9f13SBarry Smith 2516273d9f13SBarry Smith Specify the preallocated storage with either nz or nnz (not both). 2517273d9f13SBarry Smith Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory 2518273d9f13SBarry Smith allocation. For large problems you MUST preallocate memory or you 2519273d9f13SBarry Smith will get TERRIBLE performance, see the users' manual chapter on matrices. 2520273d9f13SBarry Smith 2521273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible, to 2522273d9f13SBarry Smith improve numerical efficiency of matrix-vector products and solves. We 2523273d9f13SBarry Smith search for consecutive rows with the same nonzero structure, thereby 2524273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2525273d9f13SBarry Smith 2526273d9f13SBarry Smith Options Database Keys: 2527273d9f13SBarry Smith + -mat_aij_no_inode - Do not use inodes 2528273d9f13SBarry Smith . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2529273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2530273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2531273d9f13SBarry Smith the user still MUST index entries starting at 0! 2532273d9f13SBarry Smith 2533273d9f13SBarry Smith Level: intermediate 2534273d9f13SBarry Smith 2535273d9f13SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays() 2536273d9f13SBarry Smith 2537273d9f13SBarry Smith @*/ 2538273d9f13SBarry Smith int MatSeqAIJSetPreallocation(Mat B,int nz,int *nnz) 2539273d9f13SBarry Smith { 2540273d9f13SBarry Smith Mat_SeqAIJ *b; 2541273d9f13SBarry Smith int i,len,ierr; 2542273d9f13SBarry Smith PetscTruth flg2; 2543273d9f13SBarry Smith 2544273d9f13SBarry Smith PetscFunctionBegin; 2545273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flg2);CHKERRQ(ierr); 2546273d9f13SBarry Smith if (!flg2) PetscFunctionReturn(0); 2547d5d45c9bSBarry Smith 2548435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5; 2549435da068SBarry Smith if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz); 2550b73539f3SBarry Smith if (nnz) { 2551273d9f13SBarry Smith for (i=0; i<B->m; i++) { 255229bbc08cSBarry Smith if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]); 25533a7fca6bSBarry Smith if (nnz[i] > B->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->n); 2554b73539f3SBarry Smith } 2555b73539f3SBarry Smith } 2556b73539f3SBarry Smith 2557273d9f13SBarry Smith B->preallocated = PETSC_TRUE; 2558273d9f13SBarry Smith b = (Mat_SeqAIJ*)B->data; 2559273d9f13SBarry Smith 2560b0a32e0cSBarry Smith ierr = PetscMalloc((B->m+1)*sizeof(int),&b->imax);CHKERRQ(ierr); 2561273d9f13SBarry Smith if (!nnz) { 2562435da068SBarry Smith if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10; 2563273d9f13SBarry Smith else if (nz <= 0) nz = 1; 2564273d9f13SBarry Smith for (i=0; i<B->m; i++) b->imax[i] = nz; 2565273d9f13SBarry Smith nz = nz*B->m; 2566273d9f13SBarry Smith } else { 2567273d9f13SBarry Smith nz = 0; 2568273d9f13SBarry Smith for (i=0; i<B->m; i++) {b->imax[i] = nnz[i]; nz += nnz[i];} 2569273d9f13SBarry Smith } 2570273d9f13SBarry Smith 2571273d9f13SBarry Smith /* allocate the matrix space */ 257287828ca2SBarry Smith len = nz*(sizeof(int) + sizeof(PetscScalar)) + (B->m+1)*sizeof(int); 2573b0a32e0cSBarry Smith ierr = PetscMalloc(len,&b->a);CHKERRQ(ierr); 2574273d9f13SBarry Smith b->j = (int*)(b->a + nz); 2575273d9f13SBarry Smith ierr = PetscMemzero(b->j,nz*sizeof(int));CHKERRQ(ierr); 2576273d9f13SBarry Smith b->i = b->j + nz; 2577273d9f13SBarry Smith b->singlemalloc = PETSC_TRUE; 2578273d9f13SBarry Smith b->freedata = PETSC_TRUE; 2579273d9f13SBarry Smith 2580273d9f13SBarry Smith b->i[0] = -b->indexshift; 2581273d9f13SBarry Smith for (i=1; i<B->m+1; i++) { 2582273d9f13SBarry Smith b->i[i] = b->i[i-1] + b->imax[i-1]; 2583273d9f13SBarry Smith } 2584273d9f13SBarry Smith 2585273d9f13SBarry Smith /* b->ilen will count nonzeros in each row so far. */ 2586b0a32e0cSBarry Smith ierr = PetscMalloc((B->m+1)*sizeof(int),&b->ilen);CHKERRQ(ierr); 2587b0a32e0cSBarry Smith PetscLogObjectMemory(B,len+2*(B->m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 2588273d9f13SBarry Smith for (i=0; i<B->m; i++) { b->ilen[i] = 0;} 2589273d9f13SBarry Smith 2590273d9f13SBarry Smith b->nz = 0; 2591273d9f13SBarry Smith b->maxnz = nz; 2592273d9f13SBarry Smith B->info.nz_unneeded = (double)b->maxnz; 2593273d9f13SBarry Smith PetscFunctionReturn(0); 2594273d9f13SBarry Smith } 2595273d9f13SBarry Smith 2596273d9f13SBarry Smith EXTERN_C_BEGIN 25974a2ae208SSatish Balay #undef __FUNCT__ 25984a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ" 2599273d9f13SBarry Smith int MatCreate_SeqAIJ(Mat B) 2600273d9f13SBarry Smith { 2601273d9f13SBarry Smith Mat_SeqAIJ *b; 2602273d9f13SBarry Smith int ierr,size; 2603273d9f13SBarry Smith PetscTruth flg; 2604273d9f13SBarry Smith 2605273d9f13SBarry Smith PetscFunctionBegin; 2606273d9f13SBarry Smith ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr); 2607273d9f13SBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1"); 2608273d9f13SBarry Smith 2609273d9f13SBarry Smith B->m = B->M = PetscMax(B->m,B->M); 2610273d9f13SBarry Smith B->n = B->N = PetscMax(B->n,B->N); 2611273d9f13SBarry Smith 2612b0a32e0cSBarry Smith ierr = PetscNew(Mat_SeqAIJ,&b);CHKERRQ(ierr); 2613b0a32e0cSBarry Smith B->data = (void*)b; 2614549d3d68SSatish Balay ierr = PetscMemzero(b,sizeof(Mat_SeqAIJ));CHKERRQ(ierr); 2615549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 2616416022c9SBarry Smith B->factor = 0; 2617416022c9SBarry Smith B->lupivotthreshold = 1.0; 261890f02eecSBarry Smith B->mapping = 0; 261987828ca2SBarry Smith ierr = PetscOptionsGetReal(PETSC_NULL,"-mat_lu_pivotthreshold",&B->lupivotthreshold,PETSC_NULL);CHKERRQ(ierr); 2620b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-pc_ilu_preserve_row_sums",&b->ilu_preserve_row_sums);CHKERRQ(ierr); 2621416022c9SBarry Smith b->row = 0; 2622416022c9SBarry Smith b->col = 0; 262382bf6240SBarry Smith b->icol = 0; 2624416022c9SBarry Smith b->indexshift = 0; 2625b810aeb4SBarry Smith b->reallocs = 0; 2626b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_oneindex",&flg);CHKERRQ(ierr); 262769957df2SSatish Balay if (flg) b->indexshift = -1; 262817ab2063SBarry Smith 26298a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->m,&B->rmap);CHKERRQ(ierr); 26308a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->n,B->n,&B->cmap);CHKERRQ(ierr); 2631a5ae1ecdSBarry Smith 2632f1e2ffcdSBarry Smith b->sorted = PETSC_FALSE; 263336db0b34SBarry Smith b->ignorezeroentries = PETSC_FALSE; 2634f1e2ffcdSBarry Smith b->roworiented = PETSC_TRUE; 2635416022c9SBarry Smith b->nonew = 0; 2636416022c9SBarry Smith b->diag = 0; 2637416022c9SBarry Smith b->solve_work = 0; 263871bd300dSLois Curfman McInnes b->spptr = 0; 2639b65db4caSBarry Smith b->inode.use = PETSC_TRUE; 2640754ec7b1SSatish Balay b->inode.node_count = 0; 2641754ec7b1SSatish Balay b->inode.size = 0; 26426c7ebb05SLois Curfman McInnes b->inode.limit = 5; 26436c7ebb05SLois Curfman McInnes b->inode.max_limit = 5; 2644be6bf707SBarry Smith b->saved_values = 0; 2645d7f994e1SBarry Smith b->idiag = 0; 2646d7f994e1SBarry Smith b->ssor = 0; 2647f1e2ffcdSBarry Smith b->keepzeroedrows = PETSC_FALSE; 264817ab2063SBarry Smith 264935d8aa7fSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr); 265035d8aa7fSBarry Smith 2651aa482453SBarry Smith #if defined(PETSC_HAVE_SUPERLU) 2652b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_superlu",&flg);CHKERRQ(ierr); 265369957df2SSatish Balay if (flg) { ierr = MatUseSuperLU_SeqAIJ(B);CHKERRQ(ierr); } 26544b14c69eSBarry Smith #endif 2655b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_essl",&flg);CHKERRQ(ierr); 265669957df2SSatish Balay if (flg) { ierr = MatUseEssl_SeqAIJ(B);CHKERRQ(ierr); } 2657b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_lusol",&flg);CHKERRQ(ierr); 2658cd5578b5SBarry Smith if (flg) { ierr = MatUseLUSOL_SeqAIJ(B);CHKERRQ(ierr); } 2659b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_matlab",&flg);CHKERRQ(ierr); 2660ca44d042SBarry Smith if (flg) {ierr = MatUseMatlab_SeqAIJ(B);CHKERRQ(ierr);} 2661b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_dxml",&flg);CHKERRQ(ierr); 266269957df2SSatish Balay if (flg) { 266329bbc08cSBarry Smith if (!b->indexshift) SETERRQ(PETSC_ERR_LIB,"need -mat_aij_oneindex with -mat_aij_dxml"); 2664416022c9SBarry Smith ierr = MatUseDXML_SeqAIJ(B);CHKERRQ(ierr); 266517ab2063SBarry Smith } 2666f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C", 2667bef8e0ddSBarry Smith "MatSeqAIJSetColumnIndices_SeqAIJ", 2668bc4b532fSSatish Balay MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr); 2669f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 2670be6bf707SBarry Smith "MatStoreValues_SeqAIJ", 2671bc4b532fSSatish Balay MatStoreValues_SeqAIJ);CHKERRQ(ierr); 2672f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 2673be6bf707SBarry Smith "MatRetrieveValues_SeqAIJ", 2674bc4b532fSSatish Balay MatRetrieveValues_SeqAIJ);CHKERRQ(ierr); 267587828ca2SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 2676f83d6046SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEnginePut_C","MatMatlabEnginePut_SeqAIJ",MatMatlabEnginePut_SeqAIJ);CHKERRQ(ierr); 267766826eb6SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"PetscMatlabEngineGet_C","MatMatlabEngineGet_SeqAIJ",MatMatlabEngineGet_SeqAIJ);CHKERRQ(ierr); 2678f83d6046SBarry Smith #endif 26793a40ed3dSBarry Smith PetscFunctionReturn(0); 268017ab2063SBarry Smith } 2681273d9f13SBarry Smith EXTERN_C_END 268217ab2063SBarry Smith 26834a2ae208SSatish Balay #undef __FUNCT__ 26844a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_SeqAIJ" 2685be6bf707SBarry Smith int MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B) 268617ab2063SBarry Smith { 2687416022c9SBarry Smith Mat C; 2688416022c9SBarry Smith Mat_SeqAIJ *c,*a = (Mat_SeqAIJ*)A->data; 2689273d9f13SBarry Smith int i,len,m = A->m,shift = a->indexshift,ierr; 269017ab2063SBarry Smith 26913a40ed3dSBarry Smith PetscFunctionBegin; 26924043dd9cSLois Curfman McInnes *B = 0; 2693273d9f13SBarry Smith ierr = MatCreate(A->comm,A->m,A->n,A->m,A->n,&C);CHKERRQ(ierr); 2694273d9f13SBarry Smith ierr = MatSetType(C,MATSEQAIJ);CHKERRQ(ierr); 2695273d9f13SBarry Smith c = (Mat_SeqAIJ*)C->data; 2696273d9f13SBarry Smith 2697416022c9SBarry Smith C->factor = A->factor; 2698416022c9SBarry Smith c->row = 0; 2699416022c9SBarry Smith c->col = 0; 270082bf6240SBarry Smith c->icol = 0; 2701416022c9SBarry Smith c->indexshift = shift; 2702f1e2ffcdSBarry Smith c->keepzeroedrows = a->keepzeroedrows; 2703c456f294SBarry Smith C->assembled = PETSC_TRUE; 270417ab2063SBarry Smith 2705273d9f13SBarry Smith C->M = A->m; 2706273d9f13SBarry Smith C->N = A->n; 270717ab2063SBarry Smith 2708b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->imax);CHKERRQ(ierr); 2709b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->ilen);CHKERRQ(ierr); 271017ab2063SBarry Smith for (i=0; i<m; i++) { 2711416022c9SBarry Smith c->imax[i] = a->imax[i]; 2712416022c9SBarry Smith c->ilen[i] = a->ilen[i]; 271317ab2063SBarry Smith } 271417ab2063SBarry Smith 271517ab2063SBarry Smith /* allocate the matrix space */ 2716f1e2ffcdSBarry Smith c->singlemalloc = PETSC_TRUE; 271787828ca2SBarry Smith len = (m+1)*sizeof(int)+(a->i[m])*(sizeof(PetscScalar)+sizeof(int)); 2718b0a32e0cSBarry Smith ierr = PetscMalloc(len,&c->a);CHKERRQ(ierr); 2719416022c9SBarry Smith c->j = (int*)(c->a + a->i[m] + shift); 2720416022c9SBarry Smith c->i = c->j + a->i[m] + shift; 2721549d3d68SSatish Balay ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(int));CHKERRQ(ierr); 272217ab2063SBarry Smith if (m > 0) { 2723549d3d68SSatish Balay ierr = PetscMemcpy(c->j,a->j,(a->i[m]+shift)*sizeof(int));CHKERRQ(ierr); 2724be6bf707SBarry Smith if (cpvalues == MAT_COPY_VALUES) { 272587828ca2SBarry Smith ierr = PetscMemcpy(c->a,a->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 2726be6bf707SBarry Smith } else { 272787828ca2SBarry Smith ierr = PetscMemzero(c->a,(a->i[m]+shift)*sizeof(PetscScalar));CHKERRQ(ierr); 272817ab2063SBarry Smith } 272908480c60SBarry Smith } 273017ab2063SBarry Smith 2731b0a32e0cSBarry Smith PetscLogObjectMemory(C,len+2*(m+1)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_SeqAIJ)); 2732416022c9SBarry Smith c->sorted = a->sorted; 2733416022c9SBarry Smith c->roworiented = a->roworiented; 2734416022c9SBarry Smith c->nonew = a->nonew; 27357a743949SBarry Smith c->ilu_preserve_row_sums = a->ilu_preserve_row_sums; 2736be6bf707SBarry Smith c->saved_values = 0; 2737d7f994e1SBarry Smith c->idiag = 0; 2738d7f994e1SBarry Smith c->ssor = 0; 273936db0b34SBarry Smith c->ignorezeroentries = a->ignorezeroentries; 274036db0b34SBarry Smith c->freedata = PETSC_TRUE; 274117ab2063SBarry Smith 2742416022c9SBarry Smith if (a->diag) { 2743b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->diag);CHKERRQ(ierr); 2744b0a32e0cSBarry Smith PetscLogObjectMemory(C,(m+1)*sizeof(int)); 274517ab2063SBarry Smith for (i=0; i<m; i++) { 2746416022c9SBarry Smith c->diag[i] = a->diag[i]; 274717ab2063SBarry Smith } 27483a40ed3dSBarry Smith } else c->diag = 0; 2749b65db4caSBarry Smith c->inode.use = a->inode.use; 27506c7ebb05SLois Curfman McInnes c->inode.limit = a->inode.limit; 27516c7ebb05SLois Curfman McInnes c->inode.max_limit = a->inode.max_limit; 2752754ec7b1SSatish Balay if (a->inode.size){ 2753b0a32e0cSBarry Smith ierr = PetscMalloc((m+1)*sizeof(int),&c->inode.size);CHKERRQ(ierr); 2754754ec7b1SSatish Balay c->inode.node_count = a->inode.node_count; 2755549d3d68SSatish Balay ierr = PetscMemcpy(c->inode.size,a->inode.size,(m+1)*sizeof(int));CHKERRQ(ierr); 2756754ec7b1SSatish Balay } else { 2757754ec7b1SSatish Balay c->inode.size = 0; 2758754ec7b1SSatish Balay c->inode.node_count = 0; 2759754ec7b1SSatish Balay } 2760416022c9SBarry Smith c->nz = a->nz; 2761416022c9SBarry Smith c->maxnz = a->maxnz; 2762416022c9SBarry Smith c->solve_work = 0; 276376dd722bSSatish Balay c->spptr = 0; /* Dangerous -I'm throwing away a->spptr */ 2764273d9f13SBarry Smith C->preallocated = PETSC_TRUE; 2765754ec7b1SSatish Balay 2766416022c9SBarry Smith *B = C; 2767b0a32e0cSBarry Smith ierr = PetscFListDuplicate(A->qlist,&C->qlist);CHKERRQ(ierr); 27683a40ed3dSBarry Smith PetscFunctionReturn(0); 276917ab2063SBarry Smith } 277017ab2063SBarry Smith 2771273d9f13SBarry Smith EXTERN_C_BEGIN 27724a2ae208SSatish Balay #undef __FUNCT__ 27734a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ" 2774b0a32e0cSBarry Smith int MatLoad_SeqAIJ(PetscViewer viewer,MatType type,Mat *A) 277517ab2063SBarry Smith { 2776416022c9SBarry Smith Mat_SeqAIJ *a; 2777416022c9SBarry Smith Mat B; 277817699dbbSLois Curfman McInnes int i,nz,ierr,fd,header[4],size,*rowlengths = 0,M,N,shift; 2779bcd2baecSBarry Smith MPI_Comm comm; 278017ab2063SBarry Smith 27813a40ed3dSBarry Smith PetscFunctionBegin; 2782e864ced6SBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 2783d132466eSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 278429bbc08cSBarry Smith if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor"); 2785b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 27860752156aSBarry Smith ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr); 2787552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file"); 278817ab2063SBarry Smith M = header[1]; N = header[2]; nz = header[3]; 278917ab2063SBarry Smith 2790d64ed03dSBarry Smith if (nz < 0) { 279129bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ"); 2792d64ed03dSBarry Smith } 2793d64ed03dSBarry Smith 279417ab2063SBarry Smith /* read in row lengths */ 2795b0a32e0cSBarry Smith ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr); 27960752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 279717ab2063SBarry Smith 279817ab2063SBarry Smith /* create our matrix */ 2799416022c9SBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,0,rowlengths,A);CHKERRQ(ierr); 2800416022c9SBarry Smith B = *A; 2801416022c9SBarry Smith a = (Mat_SeqAIJ*)B->data; 2802416022c9SBarry Smith shift = a->indexshift; 280317ab2063SBarry Smith 280417ab2063SBarry Smith /* read in column indices and adjust for Fortran indexing*/ 28050752156aSBarry Smith ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr); 280617ab2063SBarry Smith if (shift) { 280717ab2063SBarry Smith for (i=0; i<nz; i++) { 2808416022c9SBarry Smith a->j[i] += 1; 280917ab2063SBarry Smith } 281017ab2063SBarry Smith } 281117ab2063SBarry Smith 281217ab2063SBarry Smith /* read in nonzero values */ 28130752156aSBarry Smith ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr); 281417ab2063SBarry Smith 281517ab2063SBarry Smith /* set matrix "i" values */ 2816416022c9SBarry Smith a->i[0] = -shift; 281717ab2063SBarry Smith for (i=1; i<= M; i++) { 2818416022c9SBarry Smith a->i[i] = a->i[i-1] + rowlengths[i-1]; 2819416022c9SBarry Smith a->ilen[i-1] = rowlengths[i-1]; 282017ab2063SBarry Smith } 2821606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 282217ab2063SBarry Smith 28236d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28246d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 28253a40ed3dSBarry Smith PetscFunctionReturn(0); 282617ab2063SBarry Smith } 2827273d9f13SBarry Smith EXTERN_C_END 282817ab2063SBarry Smith 28294a2ae208SSatish Balay #undef __FUNCT__ 2830b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ" 28318f6be9afSLois Curfman McInnes int MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg) 28327264ac53SSatish Balay { 28337264ac53SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data; 28340f5bd95cSBarry Smith int ierr; 2835273d9f13SBarry Smith PetscTruth flag; 28367264ac53SSatish Balay 28373a40ed3dSBarry Smith PetscFunctionBegin; 2838273d9f13SBarry Smith ierr = PetscTypeCompare((PetscObject)B,MATSEQAIJ,&flag);CHKERRQ(ierr); 2839273d9f13SBarry Smith if (!flag) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type"); 28407264ac53SSatish Balay 28417264ac53SSatish Balay /* If the matrix dimensions are not equal,or no of nonzeros or shift */ 2842273d9f13SBarry Smith if ((A->m != B->m) || (A->n != B->n) ||(a->nz != b->nz)|| (a->indexshift != b->indexshift)) { 2843ca44d042SBarry Smith *flg = PETSC_FALSE; 2844ca44d042SBarry Smith PetscFunctionReturn(0); 2845bcd2baecSBarry Smith } 28467264ac53SSatish Balay 28477264ac53SSatish Balay /* if the a->i are the same */ 2848273d9f13SBarry Smith ierr = PetscMemcmp(a->i,b->i,(A->m+1)*sizeof(int),flg);CHKERRQ(ierr); 28490f5bd95cSBarry Smith if (*flg == PETSC_FALSE) PetscFunctionReturn(0); 28507264ac53SSatish Balay 28517264ac53SSatish Balay /* if a->j are the same */ 28520f5bd95cSBarry Smith ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),flg);CHKERRQ(ierr); 28530f5bd95cSBarry Smith if (*flg == PETSC_FALSE) PetscFunctionReturn(0); 2854bcd2baecSBarry Smith 2855bcd2baecSBarry Smith /* if a->a are the same */ 285687828ca2SBarry Smith ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr); 28570f5bd95cSBarry Smith 28583a40ed3dSBarry Smith PetscFunctionReturn(0); 28597264ac53SSatish Balay 28607264ac53SSatish Balay } 286136db0b34SBarry Smith 28624a2ae208SSatish Balay #undef __FUNCT__ 28634a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays" 286436db0b34SBarry Smith /*@C 286536db0b34SBarry Smith MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format) 286636db0b34SBarry Smith provided by the user. 286736db0b34SBarry Smith 286836db0b34SBarry Smith Coolective on MPI_Comm 286936db0b34SBarry Smith 287036db0b34SBarry Smith Input Parameters: 287136db0b34SBarry Smith + comm - must be an MPI communicator of size 1 287236db0b34SBarry Smith . m - number of rows 287336db0b34SBarry Smith . n - number of columns 287436db0b34SBarry Smith . i - row indices 287536db0b34SBarry Smith . j - column indices 287636db0b34SBarry Smith - a - matrix values 287736db0b34SBarry Smith 287836db0b34SBarry Smith Output Parameter: 287936db0b34SBarry Smith . mat - the matrix 288036db0b34SBarry Smith 288136db0b34SBarry Smith Level: intermediate 288236db0b34SBarry Smith 288336db0b34SBarry Smith Notes: 28840551d7c0SBarry Smith The i, j, and a arrays are not copied by this routine, the user must free these arrays 288536db0b34SBarry Smith once the matrix is destroyed 288636db0b34SBarry Smith 288736db0b34SBarry Smith You cannot set new nonzero locations into this matrix, that will generate an error. 288836db0b34SBarry Smith 288936db0b34SBarry Smith The i and j indices can be either 0- or 1 based 289036db0b34SBarry Smith 289136db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ() 289236db0b34SBarry Smith 289336db0b34SBarry Smith @*/ 289487828ca2SBarry Smith int MatCreateSeqAIJWithArrays(MPI_Comm comm,int m,int n,int* i,int*j,PetscScalar *a,Mat *mat) 289536db0b34SBarry Smith { 289636db0b34SBarry Smith int ierr,ii; 289736db0b34SBarry Smith Mat_SeqAIJ *aij; 289836db0b34SBarry Smith 289936db0b34SBarry Smith PetscFunctionBegin; 290036db0b34SBarry Smith ierr = MatCreateSeqAIJ(comm,m,n,0,0,mat);CHKERRQ(ierr); 290136db0b34SBarry Smith aij = (Mat_SeqAIJ*)(*mat)->data; 290236db0b34SBarry Smith ierr = PetscFree(aij->a);CHKERRQ(ierr); 290336db0b34SBarry Smith 290436db0b34SBarry Smith if (i[0] == 1) { 290536db0b34SBarry Smith aij->indexshift = -1; 290636db0b34SBarry Smith } else if (i[0]) { 290729bbc08cSBarry Smith SETERRQ(1,"i (row indices) do not start with 0 or 1"); 290836db0b34SBarry Smith } 290936db0b34SBarry Smith aij->i = i; 291036db0b34SBarry Smith aij->j = j; 291136db0b34SBarry Smith aij->a = a; 291236db0b34SBarry Smith aij->singlemalloc = PETSC_FALSE; 291336db0b34SBarry Smith aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/ 291436db0b34SBarry Smith aij->freedata = PETSC_FALSE; 291536db0b34SBarry Smith 291636db0b34SBarry Smith for (ii=0; ii<m; ii++) { 291736db0b34SBarry Smith aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii]; 29189860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g) 291929bbc08cSBarry Smith if (i[ii+1] - i[ii] < 0) SETERRQ2(1,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]); 292036db0b34SBarry Smith #endif 292136db0b34SBarry Smith } 29229860f2b2SBarry Smith #if defined(PETSC_USE_BOPT_g) 292336db0b34SBarry Smith for (ii=0; ii<aij->i[m]; ii++) { 292429bbc08cSBarry Smith if (j[ii] < -aij->indexshift) SETERRQ2(1,"Negative column index at location = %d index = %d",ii,j[ii]); 292529bbc08cSBarry Smith if (j[ii] > n - 1 -aij->indexshift) SETERRQ2(1,"Column index to large at location = %d index = %d",ii,j[ii]); 292636db0b34SBarry Smith } 292736db0b34SBarry Smith #endif 292836db0b34SBarry Smith 2929b65db4caSBarry Smith /* changes indices to start at 0 */ 2930b65db4caSBarry Smith if (i[0]) { 2931b65db4caSBarry Smith aij->indexshift = 0; 2932b65db4caSBarry Smith for (ii=0; ii<m; ii++) { 2933b65db4caSBarry Smith i[ii]--; 2934b65db4caSBarry Smith } 2935b65db4caSBarry Smith for (ii=0; ii<i[m]; ii++) { 2936b65db4caSBarry Smith j[ii]--; 2937b65db4caSBarry Smith } 2938b65db4caSBarry Smith } 2939b65db4caSBarry Smith 2940b65db4caSBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2941b65db4caSBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 294236db0b34SBarry Smith PetscFunctionReturn(0); 294336db0b34SBarry Smith } 294436db0b34SBarry Smith 2945cc8ba8e1SBarry Smith #undef __FUNCT__ 2946ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ" 2947ee4f033dSBarry Smith int MatSetColoring_SeqAIJ(Mat A,ISColoring coloring) 2948cc8ba8e1SBarry Smith { 2949cc8ba8e1SBarry Smith int ierr; 2950cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 295136db0b34SBarry Smith 2952cc8ba8e1SBarry Smith PetscFunctionBegin; 2953cc8ba8e1SBarry Smith ierr = ISColoringReference(coloring);CHKERRQ(ierr); 2954cc8ba8e1SBarry Smith a->coloring = coloring; 2955cc8ba8e1SBarry Smith PetscFunctionReturn(0); 2956cc8ba8e1SBarry Smith } 2957cc8ba8e1SBarry Smith 295887828ca2SBarry Smith #if defined(PETSC_HAVE_ADIC) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 2959ee4f033dSBarry Smith EXTERN_C_BEGIN 296029c1e371SBarry Smith #include "adic/ad_utils.h" 2961ee4f033dSBarry Smith EXTERN_C_END 2962cc8ba8e1SBarry Smith 2963cc8ba8e1SBarry Smith #undef __FUNCT__ 2964ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 2965ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 2966cc8ba8e1SBarry Smith { 2967cc8ba8e1SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 2968ee4f033dSBarry Smith int m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j,nlen; 296987828ca2SBarry Smith PetscScalar *v = a->a,*values; 2970ee4f033dSBarry Smith char *cadvalues = (char *)advalues; 2971cc8ba8e1SBarry Smith 2972cc8ba8e1SBarry Smith PetscFunctionBegin; 2973cc8ba8e1SBarry Smith if (!a->coloring) SETERRQ(1,"Coloring not set for matrix"); 297429c1e371SBarry Smith nlen = PetscADGetDerivTypeSize(); 2975cc8ba8e1SBarry Smith color = a->coloring->colors; 2976cc8ba8e1SBarry Smith /* loop over rows */ 2977cc8ba8e1SBarry Smith for (i=0; i<m; i++) { 2978cc8ba8e1SBarry Smith nz = ii[i+1] - ii[i]; 2979cc8ba8e1SBarry Smith /* loop over columns putting computed value into matrix */ 298029c1e371SBarry Smith values = PetscADGetGradArray(cadvalues); 2981cc8ba8e1SBarry Smith for (j=0; j<nz; j++) { 2982cc8ba8e1SBarry Smith *v++ = values[color[*jj++]]; 2983cc8ba8e1SBarry Smith } 2984ee4f033dSBarry Smith cadvalues += nlen; /* jump to next row of derivatives */ 2985ee4f033dSBarry Smith } 2986ee4f033dSBarry Smith PetscFunctionReturn(0); 2987ee4f033dSBarry Smith } 2988ee4f033dSBarry Smith 2989ee4f033dSBarry Smith #else 2990ee4f033dSBarry Smith 2991ee4f033dSBarry Smith #undef __FUNCT__ 2992ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ" 2993ee4f033dSBarry Smith int MatSetValuesAdic_SeqAIJ(Mat A,void *advalues) 2994ee4f033dSBarry Smith { 2995ee4f033dSBarry Smith PetscFunctionBegin; 2996ee4f033dSBarry Smith SETERRQ(1,"PETSc installed without ADIC"); 2997ee4f033dSBarry Smith } 2998ee4f033dSBarry Smith 2999ee4f033dSBarry Smith #endif 3000ee4f033dSBarry Smith 3001ee4f033dSBarry Smith #undef __FUNCT__ 3002ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ" 3003ee4f033dSBarry Smith int MatSetValuesAdifor_SeqAIJ(Mat A,int nl,void *advalues) 3004ee4f033dSBarry Smith { 3005ee4f033dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 3006ee4f033dSBarry Smith int m = A->m,*ii = a->i,*jj = a->j,nz,i,*color,j; 300787828ca2SBarry Smith PetscScalar *v = a->a,*values = (PetscScalar *)advalues; 3008ee4f033dSBarry Smith 3009ee4f033dSBarry Smith PetscFunctionBegin; 3010ee4f033dSBarry Smith if (!a->coloring) SETERRQ(1,"Coloring not set for matrix"); 3011ee4f033dSBarry Smith color = a->coloring->colors; 3012ee4f033dSBarry Smith /* loop over rows */ 3013ee4f033dSBarry Smith for (i=0; i<m; i++) { 3014ee4f033dSBarry Smith nz = ii[i+1] - ii[i]; 3015ee4f033dSBarry Smith /* loop over columns putting computed value into matrix */ 3016ee4f033dSBarry Smith for (j=0; j<nz; j++) { 3017ee4f033dSBarry Smith *v++ = values[color[*jj++]]; 3018ee4f033dSBarry Smith } 3019ee4f033dSBarry Smith values += nl; /* jump to next row of derivatives */ 3020cc8ba8e1SBarry Smith } 3021cc8ba8e1SBarry Smith PetscFunctionReturn(0); 3022cc8ba8e1SBarry Smith } 302336db0b34SBarry Smith 3024