1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*d36fbae8SSatish Balay static char vcid[] = "$Id: mpiaij.c,v 1.287 1999/03/11 22:30:32 balay Exp balay $"; 3cb512458SBarry Smith #endif 48a729477SBarry Smith 570f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 6f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 7d9942c19SSatish Balay #include "src/inline/spops.h" 88a729477SBarry Smith 9bc5ccf88SSatish Balay extern int MatSetUpMultiply_MPIAIJ(Mat); 10bc5ccf88SSatish Balay extern int DisAssemble_MPIAIJ(Mat); 11bc5ccf88SSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode); 12bc5ccf88SSatish Balay extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 13bc5ccf88SSatish Balay extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 14bc5ccf88SSatish Balay extern int MatPrintHelp_SeqAIJ(Mat); 15bc5ccf88SSatish Balay 169e25ed09SBarry Smith /* local utility routine that creates a mapping from the global column 179e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 189e25ed09SBarry Smith storage of the matrix. This is done in a non scable way since the 199e25ed09SBarry Smith length of colmap equals the global matrix length. 209e25ed09SBarry Smith */ 215615d1e5SSatish Balay #undef __FUNC__ 22d4bb536fSBarry Smith #define __FUNC__ "CreateColmap_MPIAIJ_Private" 230a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat) 249e25ed09SBarry Smith { 2544a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 26ec8511deSBarry Smith Mat_SeqAIJ *B = (Mat_SeqAIJ*) aij->B->data; 276dee3f7eSBarry Smith int n = B->n,i; 286dee3f7eSBarry Smith #if defined (USE_CTABLE) 296dee3f7eSBarry Smith int ierr; 306dee3f7eSBarry Smith #endif 31dbb450caSBarry Smith 323a40ed3dSBarry Smith PetscFunctionBegin; 33b1fc9764SSatish Balay #if defined (USE_CTABLE) 34fa46199cSSatish Balay ierr = TableCreate(aij->n/5,&aij->colmap); CHKERRQ(ierr); 35b1fc9764SSatish Balay for ( i=0; i<n; i++ ){ 36b1fc9764SSatish Balay ierr = TableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr); 37b1fc9764SSatish Balay } 38b1fc9764SSatish Balay #else 39758f045eSSatish Balay aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap); 40464493b3SBarry Smith PLogObjectMemory(mat,aij->N*sizeof(int)); 41cddf8d76SBarry Smith PetscMemzero(aij->colmap,aij->N*sizeof(int)); 42905e6a2fSBarry Smith for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1; 43b1fc9764SSatish Balay #endif 443a40ed3dSBarry Smith PetscFunctionReturn(0); 459e25ed09SBarry Smith } 469e25ed09SBarry Smith 470520107fSSatish Balay #define CHUNKSIZE 15 4830770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 490520107fSSatish Balay { \ 500520107fSSatish Balay \ 510520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 5230770e4dSSatish Balay rmax = aimax[row]; nrow = ailen[row]; \ 53f5e9677aSSatish Balay col1 = col - shift; \ 54f5e9677aSSatish Balay \ 55ba4e3ef2SSatish Balay low = 0; high = nrow; \ 56ba4e3ef2SSatish Balay while (high-low > 5) { \ 57ba4e3ef2SSatish Balay t = (low+high)/2; \ 58ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 59ba4e3ef2SSatish Balay else low = t; \ 60ba4e3ef2SSatish Balay } \ 610520107fSSatish Balay for ( _i=0; _i<nrow; _i++ ) { \ 62f5e9677aSSatish Balay if (rp[_i] > col1) break; \ 63f5e9677aSSatish Balay if (rp[_i] == col1) { \ 640520107fSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 650520107fSSatish Balay else ap[_i] = value; \ 6630770e4dSSatish Balay goto a_noinsert; \ 670520107fSSatish Balay } \ 680520107fSSatish Balay } \ 6989280ab3SLois Curfman McInnes if (nonew == 1) goto a_noinsert; \ 70a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \ 710520107fSSatish Balay if (nrow >= rmax) { \ 720520107fSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 730520107fSSatish Balay int new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \ 740520107fSSatish Balay Scalar *new_a; \ 750520107fSSatish Balay \ 76a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \ 7789280ab3SLois Curfman McInnes \ 780520107fSSatish Balay /* malloc new storage space */ \ 790520107fSSatish Balay len = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \ 800520107fSSatish Balay new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \ 810520107fSSatish Balay new_j = (int *) (new_a + new_nz); \ 820520107fSSatish Balay new_i = new_j + new_nz; \ 830520107fSSatish Balay \ 840520107fSSatish Balay /* copy over old data into new slots */ \ 850520107fSSatish Balay for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \ 860520107fSSatish Balay for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \ 870520107fSSatish Balay PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); \ 880520107fSSatish Balay len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \ 890520107fSSatish Balay PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \ 900520107fSSatish Balay len*sizeof(int)); \ 910520107fSSatish Balay PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); \ 920520107fSSatish Balay PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \ 930520107fSSatish Balay len*sizeof(Scalar)); \ 940520107fSSatish Balay /* free up old matrix storage */ \ 95f5e9677aSSatish Balay \ 960520107fSSatish Balay PetscFree(a->a); \ 970520107fSSatish Balay if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} \ 980520107fSSatish Balay aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; \ 990520107fSSatish Balay a->singlemalloc = 1; \ 1000520107fSSatish Balay \ 1010520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 10230770e4dSSatish Balay rmax = aimax[row] = aimax[row] + CHUNKSIZE; \ 1030520107fSSatish Balay PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \ 1040520107fSSatish Balay a->maxnz += CHUNKSIZE; \ 1050520107fSSatish Balay a->reallocs++; \ 1060520107fSSatish Balay } \ 1070520107fSSatish Balay N = nrow++ - 1; a->nz++; \ 1080520107fSSatish Balay /* shift up all the later entries in this row */ \ 1090520107fSSatish Balay for ( ii=N; ii>=_i; ii-- ) { \ 1100520107fSSatish Balay rp[ii+1] = rp[ii]; \ 1110520107fSSatish Balay ap[ii+1] = ap[ii]; \ 1120520107fSSatish Balay } \ 113f5e9677aSSatish Balay rp[_i] = col1; \ 1140520107fSSatish Balay ap[_i] = value; \ 11530770e4dSSatish Balay a_noinsert: ; \ 1160520107fSSatish Balay ailen[row] = nrow; \ 1170520107fSSatish Balay } 1180a198c4cSBarry Smith 11930770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 12030770e4dSSatish Balay { \ 12130770e4dSSatish Balay \ 12230770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 12330770e4dSSatish Balay rmax = bimax[row]; nrow = bilen[row]; \ 12430770e4dSSatish Balay col1 = col - shift; \ 12530770e4dSSatish Balay \ 126ba4e3ef2SSatish Balay low = 0; high = nrow; \ 127ba4e3ef2SSatish Balay while (high-low > 5) { \ 128ba4e3ef2SSatish Balay t = (low+high)/2; \ 129ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 130ba4e3ef2SSatish Balay else low = t; \ 131ba4e3ef2SSatish Balay } \ 13230770e4dSSatish Balay for ( _i=0; _i<nrow; _i++ ) { \ 13330770e4dSSatish Balay if (rp[_i] > col1) break; \ 13430770e4dSSatish Balay if (rp[_i] == col1) { \ 13530770e4dSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 13630770e4dSSatish Balay else ap[_i] = value; \ 13730770e4dSSatish Balay goto b_noinsert; \ 13830770e4dSSatish Balay } \ 13930770e4dSSatish Balay } \ 14089280ab3SLois Curfman McInnes if (nonew == 1) goto b_noinsert; \ 141a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \ 14230770e4dSSatish Balay if (nrow >= rmax) { \ 14330770e4dSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 14474c639caSSatish Balay int new_nz = bi[b->m] + CHUNKSIZE,len,*new_i,*new_j; \ 14530770e4dSSatish Balay Scalar *new_a; \ 14630770e4dSSatish Balay \ 147a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \ 14889280ab3SLois Curfman McInnes \ 14930770e4dSSatish Balay /* malloc new storage space */ \ 15074c639caSSatish Balay len = new_nz*(sizeof(int)+sizeof(Scalar))+(b->m+1)*sizeof(int); \ 15130770e4dSSatish Balay new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \ 15230770e4dSSatish Balay new_j = (int *) (new_a + new_nz); \ 15330770e4dSSatish Balay new_i = new_j + new_nz; \ 15430770e4dSSatish Balay \ 15530770e4dSSatish Balay /* copy over old data into new slots */ \ 15630770e4dSSatish Balay for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = bi[ii];} \ 15774c639caSSatish Balay for ( ii=row+1; ii<b->m+1; ii++ ) {new_i[ii] = bi[ii]+CHUNKSIZE;} \ 15830770e4dSSatish Balay PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int)); \ 15930770e4dSSatish Balay len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \ 16030770e4dSSatish Balay PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \ 16130770e4dSSatish Balay len*sizeof(int)); \ 16230770e4dSSatish Balay PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar)); \ 16330770e4dSSatish Balay PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \ 16430770e4dSSatish Balay len*sizeof(Scalar)); \ 16530770e4dSSatish Balay /* free up old matrix storage */ \ 16630770e4dSSatish Balay \ 16774c639caSSatish Balay PetscFree(b->a); \ 16874c639caSSatish Balay if (!b->singlemalloc) {PetscFree(b->i);PetscFree(b->j);} \ 16974c639caSSatish Balay ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j; \ 17074c639caSSatish Balay b->singlemalloc = 1; \ 17130770e4dSSatish Balay \ 17230770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 17330770e4dSSatish Balay rmax = bimax[row] = bimax[row] + CHUNKSIZE; \ 17474c639caSSatish Balay PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \ 17574c639caSSatish Balay b->maxnz += CHUNKSIZE; \ 17674c639caSSatish Balay b->reallocs++; \ 17730770e4dSSatish Balay } \ 17874c639caSSatish Balay N = nrow++ - 1; b->nz++; \ 17930770e4dSSatish Balay /* shift up all the later entries in this row */ \ 18030770e4dSSatish Balay for ( ii=N; ii>=_i; ii-- ) { \ 18130770e4dSSatish Balay rp[ii+1] = rp[ii]; \ 18230770e4dSSatish Balay ap[ii+1] = ap[ii]; \ 18330770e4dSSatish Balay } \ 18430770e4dSSatish Balay rp[_i] = col1; \ 18530770e4dSSatish Balay ap[_i] = value; \ 18630770e4dSSatish Balay b_noinsert: ; \ 18730770e4dSSatish Balay bilen[row] = nrow; \ 18830770e4dSSatish Balay } 18930770e4dSSatish Balay 1905615d1e5SSatish Balay #undef __FUNC__ 1915615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ" 1928f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv) 1938a729477SBarry Smith { 19444a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1954b0e389bSBarry Smith Scalar value; 1961eb62cbbSBarry Smith int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 1971eb62cbbSBarry Smith int cstart = aij->cstart, cend = aij->cend,row,col; 198905e6a2fSBarry Smith int roworiented = aij->roworiented; 1998a729477SBarry Smith 2000520107fSSatish Balay /* Some Variables required in the macro */ 2014ee7247eSSatish Balay Mat A = aij->A; 2024ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 20330770e4dSSatish Balay int *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j; 20430770e4dSSatish Balay Scalar *aa = a->a; 20530770e4dSSatish Balay 20630770e4dSSatish Balay Mat B = aij->B; 20730770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data; 20830770e4dSSatish Balay int *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j; 20930770e4dSSatish Balay Scalar *ba = b->a; 21030770e4dSSatish Balay 211ba4e3ef2SSatish Balay int *rp,ii,nrow,_i,rmax, N, col1,low,high,t; 21230770e4dSSatish Balay int nonew = a->nonew,shift = a->indexshift; 21330770e4dSSatish Balay Scalar *ap; 2144ee7247eSSatish Balay 2153a40ed3dSBarry Smith PetscFunctionBegin; 2168a729477SBarry Smith for ( i=0; i<m; i++ ) { 2175ef9f2a5SBarry Smith if (im[i] < 0) continue; 2183a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 219a8c6a408SBarry Smith if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 2200a198c4cSBarry Smith #endif 2214b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 2224b0e389bSBarry Smith row = im[i] - rstart; 2231eb62cbbSBarry Smith for ( j=0; j<n; j++ ) { 2244b0e389bSBarry Smith if (in[j] >= cstart && in[j] < cend){ 2254b0e389bSBarry Smith col = in[j] - cstart; 2264b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 22730770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 2280520107fSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2291eb62cbbSBarry Smith } 2305ef9f2a5SBarry Smith else if (in[j] < 0) continue; 2313a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 232a8c6a408SBarry Smith else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");} 2330a198c4cSBarry Smith #endif 2341eb62cbbSBarry Smith else { 235227d817aSBarry Smith if (mat->was_assembled) { 236905e6a2fSBarry Smith if (!aij->colmap) { 237905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 238905e6a2fSBarry Smith } 239b1fc9764SSatish Balay #if defined (USE_CTABLE) 240fa46199cSSatish Balay ierr = TableFind(aij->colmap,in[j]+1,&col); CHKERRQ(ierr); 241fa46199cSSatish Balay col--; 242b1fc9764SSatish Balay #else 243905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 244b1fc9764SSatish Balay #endif 245ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 2462493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr); 2474b0e389bSBarry Smith col = in[j]; 2489bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 249f9508a3cSSatish Balay B = aij->B; 250f9508a3cSSatish Balay b = (Mat_SeqAIJ *) B->data; 251f9508a3cSSatish Balay bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 252f9508a3cSSatish Balay ba = b->a; 253d6dfbf8fSBarry Smith } 254c48de900SBarry Smith } else col = in[j]; 2554b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 25630770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 25730770e4dSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2581eb62cbbSBarry Smith } 2591eb62cbbSBarry Smith } 2605ef9f2a5SBarry Smith } else { 26190f02eecSBarry Smith if (!aij->donotstash) { 262*d36fbae8SSatish Balay if (roworiented) { 263*d36fbae8SSatish Balay ierr = StashValuesRoworiented_Private(&aij->stash,im[i],n,in,v+i*n);CHKERRQ(ierr); 264*d36fbae8SSatish Balay } else { 265*d36fbae8SSatish Balay ierr = StashValuesColumnoriented_Private(&aij->stash,im[i],n,in,v+i,m);CHKERRQ(ierr); 2664b0e389bSBarry Smith } 2671eb62cbbSBarry Smith } 2688a729477SBarry Smith } 26990f02eecSBarry Smith } 2703a40ed3dSBarry Smith PetscFunctionReturn(0); 2718a729477SBarry Smith } 2728a729477SBarry Smith 2735615d1e5SSatish Balay #undef __FUNC__ 2745615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ" 2758f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 276b49de8d1SLois Curfman McInnes { 277b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 278b49de8d1SLois Curfman McInnes int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 279b49de8d1SLois Curfman McInnes int cstart = aij->cstart, cend = aij->cend,row,col; 280b49de8d1SLois Curfman McInnes 2813a40ed3dSBarry Smith PetscFunctionBegin; 282b49de8d1SLois Curfman McInnes for ( i=0; i<m; i++ ) { 283a8c6a408SBarry Smith if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 284a8c6a408SBarry Smith if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 285b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 286b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 287b49de8d1SLois Curfman McInnes for ( j=0; j<n; j++ ) { 288a8c6a408SBarry Smith if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column"); 289a8c6a408SBarry Smith if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large"); 290b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 291b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 292b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr); 293fa852ad4SSatish Balay } else { 294905e6a2fSBarry Smith if (!aij->colmap) { 295905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 296905e6a2fSBarry Smith } 297b1fc9764SSatish Balay #if defined (USE_CTABLE) 298fa46199cSSatish Balay ierr = TableFind(aij->colmap,idxn[j]+1,&col); CHKERRQ(ierr); 299fa46199cSSatish Balay col --; 300b1fc9764SSatish Balay #else 301905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 302b1fc9764SSatish Balay #endif 303e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 304d9d09a02SSatish Balay else { 305b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr); 306b49de8d1SLois Curfman McInnes } 307b49de8d1SLois Curfman McInnes } 308b49de8d1SLois Curfman McInnes } 309a8c6a408SBarry Smith } else { 310a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported"); 311b49de8d1SLois Curfman McInnes } 312b49de8d1SLois Curfman McInnes } 3133a40ed3dSBarry Smith PetscFunctionReturn(0); 314b49de8d1SLois Curfman McInnes } 315bc5ccf88SSatish Balay 316bc5ccf88SSatish Balay #undef __FUNC__ 317bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ" 318bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 319bc5ccf88SSatish Balay { 320bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 321*d36fbae8SSatish Balay int ierr,nstash,reallocs; 322bc5ccf88SSatish Balay InsertMode addv; 323bc5ccf88SSatish Balay 324bc5ccf88SSatish Balay PetscFunctionBegin; 325bc5ccf88SSatish Balay if (aij->donotstash) { 326bc5ccf88SSatish Balay PetscFunctionReturn(0); 327bc5ccf88SSatish Balay } 328bc5ccf88SSatish Balay 329bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 330bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr); 331bc5ccf88SSatish Balay if (addv == (ADD_VALUES|INSERT_VALUES)) { 332bc5ccf88SSatish Balay SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added"); 333bc5ccf88SSatish Balay } 334bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 335bc5ccf88SSatish Balay 336bc5ccf88SSatish Balay ierr = StashScatterBegin_Private(&aij->stash,aij->rowners); CHKERRQ(ierr); 337*d36fbae8SSatish Balay ierr = StashGetInfo_Private(&aij->stash,&nstash,&reallocs); CHKERRQ(ierr); 338*d36fbae8SSatish Balay PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n", 339*d36fbae8SSatish Balay nstash,reallocs); 340bc5ccf88SSatish Balay PetscFunctionReturn(0); 341bc5ccf88SSatish Balay } 342bc5ccf88SSatish Balay 343bc5ccf88SSatish Balay 344bc5ccf88SSatish Balay #undef __FUNC__ 345bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ" 346bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 347bc5ccf88SSatish Balay { 348bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 349a2d1c673SSatish Balay int i,j,rstart,ncols,n,ierr,flg; 350bc5ccf88SSatish Balay int *row,*col,other_disassembled; 351bc5ccf88SSatish Balay Scalar *val; 352bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 353bc5ccf88SSatish Balay 354bc5ccf88SSatish Balay PetscFunctionBegin; 355bc5ccf88SSatish Balay if (!aij->donotstash) { 356a2d1c673SSatish Balay while (1) { 357a2d1c673SSatish Balay ierr = StashScatterGetMesg_Private(&aij->stash,&n,&row,&col,&val,&flg); CHKERRQ(ierr); 358a2d1c673SSatish Balay if (!flg) break; 359a2d1c673SSatish Balay 360bc5ccf88SSatish Balay for ( i=0; i<n; ) { 361bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 362bc5ccf88SSatish Balay for ( j=i,rstart=row[j]; j<n; j++ ) { if (row[j] != rstart) break; } 363bc5ccf88SSatish Balay if (j < n) ncols = j-i; 364bc5ccf88SSatish Balay else ncols = n-i; 365bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 366bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv); CHKERRQ(ierr); 367bc5ccf88SSatish Balay i = j; 368bc5ccf88SSatish Balay } 369bc5ccf88SSatish Balay } 370a2d1c673SSatish Balay ierr = StashScatterEnd_Private(&aij->stash); CHKERRQ(ierr); 371bc5ccf88SSatish Balay } 372bc5ccf88SSatish Balay 373bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode); CHKERRQ(ierr); 374bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode); CHKERRQ(ierr); 375bc5ccf88SSatish Balay 376bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 377bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 378bc5ccf88SSatish Balay /* 379bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 380bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 381bc5ccf88SSatish Balay */ 382bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*) aij->B->data)->nonew) { 383bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr); 384bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 385bc5ccf88SSatish Balay ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr); 386bc5ccf88SSatish Balay } 387bc5ccf88SSatish Balay } 388bc5ccf88SSatish Balay 389bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 390bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat); CHKERRQ(ierr); 391bc5ccf88SSatish Balay } 392bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode); CHKERRQ(ierr); 393bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode); CHKERRQ(ierr); 394bc5ccf88SSatish Balay 395bc5ccf88SSatish Balay if (aij->rowvalues) {PetscFree(aij->rowvalues); aij->rowvalues = 0;} 396bc5ccf88SSatish Balay PetscFunctionReturn(0); 397bc5ccf88SSatish Balay } 398bc5ccf88SSatish Balay 3995615d1e5SSatish Balay #undef __FUNC__ 4005615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ" 4018f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A) 4021eb62cbbSBarry Smith { 40344a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 404dbd7a890SLois Curfman McInnes int ierr; 4053a40ed3dSBarry Smith 4063a40ed3dSBarry Smith PetscFunctionBegin; 40778b31e54SBarry Smith ierr = MatZeroEntries(l->A); CHKERRQ(ierr); 40878b31e54SBarry Smith ierr = MatZeroEntries(l->B); CHKERRQ(ierr); 4093a40ed3dSBarry Smith PetscFunctionReturn(0); 4101eb62cbbSBarry Smith } 4111eb62cbbSBarry Smith 4125615d1e5SSatish Balay #undef __FUNC__ 4135615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ" 4148f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag) 4151eb62cbbSBarry Smith { 41644a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 41717699dbbSLois Curfman McInnes int i,ierr,N, *rows,*owners = l->rowners,size = l->size; 4186a5c57faSSatish Balay int *procs,*nprocs,j,found,idx,nsends,*work,row; 41917699dbbSLois Curfman McInnes int nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank; 4205392566eSBarry Smith int *rvalues,tag = A->tag,count,base,slen,n,*source; 4216a5c57faSSatish Balay int *lens,imdex,*lrows,*values,rstart=l->rstart; 422d6dfbf8fSBarry Smith MPI_Comm comm = A->comm; 4231eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 4241eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 4251eb62cbbSBarry Smith IS istmp; 4261eb62cbbSBarry Smith 4273a40ed3dSBarry Smith PetscFunctionBegin; 42877c4ece6SBarry Smith ierr = ISGetSize(is,&N); CHKERRQ(ierr); 42978b31e54SBarry Smith ierr = ISGetIndices(is,&rows); CHKERRQ(ierr); 4301eb62cbbSBarry Smith 4311eb62cbbSBarry Smith /* first count number of contributors to each processor */ 4320452661fSBarry Smith nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs); 433cddf8d76SBarry Smith PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size; 4340452661fSBarry Smith owner = (int *) PetscMalloc((N+1)*sizeof(int)); CHKPTRQ(owner); /* see note*/ 4351eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 4361eb62cbbSBarry Smith idx = rows[i]; 4371eb62cbbSBarry Smith found = 0; 43817699dbbSLois Curfman McInnes for ( j=0; j<size; j++ ) { 4391eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 4401eb62cbbSBarry Smith nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break; 4411eb62cbbSBarry Smith } 4421eb62cbbSBarry Smith } 443a8c6a408SBarry Smith if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range"); 4441eb62cbbSBarry Smith } 44517699dbbSLois Curfman McInnes nsends = 0; for ( i=0; i<size; i++ ) { nsends += procs[i];} 4461eb62cbbSBarry Smith 4471eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 4480452661fSBarry Smith work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work); 449ca161407SBarry Smith ierr = MPI_Allreduce( procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 45017699dbbSLois Curfman McInnes nrecvs = work[rank]; 451ca161407SBarry Smith ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr); 45217699dbbSLois Curfman McInnes nmax = work[rank]; 4530452661fSBarry Smith PetscFree(work); 4541eb62cbbSBarry Smith 4551eb62cbbSBarry Smith /* post receives: */ 4563a40ed3dSBarry Smith rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues); 457ca161407SBarry Smith recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits); 4581eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 459ca161407SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 4601eb62cbbSBarry Smith } 4611eb62cbbSBarry Smith 4621eb62cbbSBarry Smith /* do sends: 4631eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 4641eb62cbbSBarry Smith the ith processor 4651eb62cbbSBarry Smith */ 4660452661fSBarry Smith svalues = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(svalues); 4673a40ed3dSBarry Smith send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits); 4680452661fSBarry Smith starts = (int *) PetscMalloc( (size+1)*sizeof(int) ); CHKPTRQ(starts); 4691eb62cbbSBarry Smith starts[0] = 0; 47017699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 4711eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 4721eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 4731eb62cbbSBarry Smith } 4741eb62cbbSBarry Smith ISRestoreIndices(is,&rows); 4751eb62cbbSBarry Smith 4761eb62cbbSBarry Smith starts[0] = 0; 47717699dbbSLois Curfman McInnes for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 4781eb62cbbSBarry Smith count = 0; 47917699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 4801eb62cbbSBarry Smith if (procs[i]) { 481ca161407SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 4821eb62cbbSBarry Smith } 4831eb62cbbSBarry Smith } 4840452661fSBarry Smith PetscFree(starts); 4851eb62cbbSBarry Smith 48617699dbbSLois Curfman McInnes base = owners[rank]; 4871eb62cbbSBarry Smith 4881eb62cbbSBarry Smith /* wait on receives */ 4890452661fSBarry Smith lens = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) ); CHKPTRQ(lens); 4901eb62cbbSBarry Smith source = lens + nrecvs; 4911eb62cbbSBarry Smith count = nrecvs; slen = 0; 4921eb62cbbSBarry Smith while (count) { 493ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 4941eb62cbbSBarry Smith /* unpack receives into our local space */ 495ca161407SBarry Smith ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr); 496d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 497d6dfbf8fSBarry Smith lens[imdex] = n; 4981eb62cbbSBarry Smith slen += n; 4991eb62cbbSBarry Smith count--; 5001eb62cbbSBarry Smith } 5010452661fSBarry Smith PetscFree(recv_waits); 5021eb62cbbSBarry Smith 5031eb62cbbSBarry Smith /* move the data into the send scatter */ 5040452661fSBarry Smith lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) ); CHKPTRQ(lrows); 5051eb62cbbSBarry Smith count = 0; 5061eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 5071eb62cbbSBarry Smith values = rvalues + i*nmax; 5081eb62cbbSBarry Smith for ( j=0; j<lens[i]; j++ ) { 5091eb62cbbSBarry Smith lrows[count++] = values[j] - base; 5101eb62cbbSBarry Smith } 5111eb62cbbSBarry Smith } 5120452661fSBarry Smith PetscFree(rvalues); PetscFree(lens); 5130452661fSBarry Smith PetscFree(owner); PetscFree(nprocs); 5141eb62cbbSBarry Smith 5151eb62cbbSBarry Smith /* actually zap the local rows */ 516029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr); 517464493b3SBarry Smith PLogObjectParent(A,istmp); 5186a5c57faSSatish Balay 5196eb55b6aSBarry Smith /* 5206eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 5216eb55b6aSBarry Smith is square and the user wishes to set the diagonal we use seperate 5226eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 5236eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 5246eb55b6aSBarry Smith 5256eb55b6aSBarry Smith Contributed by: Mathew Knepley 5266eb55b6aSBarry Smith */ 527e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 528e2d53e46SBarry Smith ierr = MatZeroRows(l->B,istmp,0); CHKERRQ(ierr); 5296eb55b6aSBarry Smith if (diag && (l->A->M == l->A->N)) { 5306eb55b6aSBarry Smith ierr = MatZeroRows(l->A,istmp,diag); CHKERRQ(ierr); 531e2d53e46SBarry Smith } else if (diag) { 532e2d53e46SBarry Smith ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr); 533fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 534fa46199cSSatish Balay SETERRQ(PETSC_ERR_SUP,0,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 535fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 5366525c446SSatish Balay } 537e2d53e46SBarry Smith for ( i = 0; i < slen; i++ ) { 538e2d53e46SBarry Smith row = lrows[i] + rstart; 539e2d53e46SBarry Smith ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr); 540e2d53e46SBarry Smith } 541e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 542e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5436eb55b6aSBarry Smith } else { 5446a5c57faSSatish Balay ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr); 5456eb55b6aSBarry Smith } 54678b31e54SBarry Smith ierr = ISDestroy(istmp); CHKERRQ(ierr); 5476a5c57faSSatish Balay PetscFree(lrows); 54872dacd9aSBarry Smith 5491eb62cbbSBarry Smith /* wait on sends */ 5501eb62cbbSBarry Smith if (nsends) { 551ca161407SBarry Smith send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status); 552ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 5530452661fSBarry Smith PetscFree(send_status); 5541eb62cbbSBarry Smith } 5550452661fSBarry Smith PetscFree(send_waits); PetscFree(svalues); 5561eb62cbbSBarry Smith 5573a40ed3dSBarry Smith PetscFunctionReturn(0); 5581eb62cbbSBarry Smith } 5591eb62cbbSBarry Smith 5605615d1e5SSatish Balay #undef __FUNC__ 5615615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ" 5628f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 5631eb62cbbSBarry Smith { 564416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 565fbd6ef76SBarry Smith int ierr,nt; 566416022c9SBarry Smith 5673a40ed3dSBarry Smith PetscFunctionBegin; 568a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt); CHKERRQ(ierr); 569fbd6ef76SBarry Smith if (nt != a->n) { 570a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A and xx"); 571fbd6ef76SBarry Smith } 57243a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr); 573f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy); CHKERRQ(ierr); 57443a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr); 575f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy); CHKERRQ(ierr); 5763a40ed3dSBarry Smith PetscFunctionReturn(0); 5771eb62cbbSBarry Smith } 5781eb62cbbSBarry Smith 5795615d1e5SSatish Balay #undef __FUNC__ 5805615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ" 5818f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 582da3a660dSBarry Smith { 583416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 584da3a660dSBarry Smith int ierr; 5853a40ed3dSBarry Smith 5863a40ed3dSBarry Smith PetscFunctionBegin; 58743a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 588f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz); CHKERRQ(ierr); 58943a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 590f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz); CHKERRQ(ierr); 5913a40ed3dSBarry Smith PetscFunctionReturn(0); 592da3a660dSBarry Smith } 593da3a660dSBarry Smith 5945615d1e5SSatish Balay #undef __FUNC__ 5955615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_MPIAIJ" 5968f6be9afSLois Curfman McInnes int MatMultTrans_MPIAIJ(Mat A,Vec xx,Vec yy) 597da3a660dSBarry Smith { 598416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 599da3a660dSBarry Smith int ierr; 600da3a660dSBarry Smith 6013a40ed3dSBarry Smith PetscFunctionBegin; 602da3a660dSBarry Smith /* do nondiagonal part */ 603f830108cSBarry Smith ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr); 604da3a660dSBarry Smith /* send it on its way */ 605537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 606da3a660dSBarry Smith /* do local part */ 607f830108cSBarry Smith ierr = (*a->A->ops->multtrans)(a->A,xx,yy); CHKERRQ(ierr); 608da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 609da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 610da3a660dSBarry Smith /* but is not perhaps always true. */ 611537820f0SBarry Smith ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 6123a40ed3dSBarry Smith PetscFunctionReturn(0); 613da3a660dSBarry Smith } 614da3a660dSBarry Smith 6155615d1e5SSatish Balay #undef __FUNC__ 6165615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_MPIAIJ" 6178f6be9afSLois Curfman McInnes int MatMultTransAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 618da3a660dSBarry Smith { 619416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 620da3a660dSBarry Smith int ierr; 621da3a660dSBarry Smith 6223a40ed3dSBarry Smith PetscFunctionBegin; 623da3a660dSBarry Smith /* do nondiagonal part */ 624f830108cSBarry Smith ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr); 625da3a660dSBarry Smith /* send it on its way */ 626537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 627da3a660dSBarry Smith /* do local part */ 628f830108cSBarry Smith ierr = (*a->A->ops->multtransadd)(a->A,xx,yy,zz); CHKERRQ(ierr); 629da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 630da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 631da3a660dSBarry Smith /* but is not perhaps always true. */ 6320a198c4cSBarry Smith ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 6333a40ed3dSBarry Smith PetscFunctionReturn(0); 634da3a660dSBarry Smith } 635da3a660dSBarry Smith 6361eb62cbbSBarry Smith /* 6371eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 6381eb62cbbSBarry Smith diagonal block 6391eb62cbbSBarry Smith */ 6405615d1e5SSatish Balay #undef __FUNC__ 6415615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ" 6428f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v) 6431eb62cbbSBarry Smith { 6443a40ed3dSBarry Smith int ierr; 645416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 6463a40ed3dSBarry Smith 6473a40ed3dSBarry Smith PetscFunctionBegin; 648a8c6a408SBarry Smith if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block"); 6495baf8537SBarry Smith if (a->rstart != a->cstart || a->rend != a->cend) { 650a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition"); 6513a40ed3dSBarry Smith } 6523a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 6533a40ed3dSBarry Smith PetscFunctionReturn(0); 6541eb62cbbSBarry Smith } 6551eb62cbbSBarry Smith 6565615d1e5SSatish Balay #undef __FUNC__ 6575615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ" 6588f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A) 659052efed2SBarry Smith { 660052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 661052efed2SBarry Smith int ierr; 6623a40ed3dSBarry Smith 6633a40ed3dSBarry Smith PetscFunctionBegin; 664052efed2SBarry Smith ierr = MatScale(aa,a->A); CHKERRQ(ierr); 665052efed2SBarry Smith ierr = MatScale(aa,a->B); CHKERRQ(ierr); 6663a40ed3dSBarry Smith PetscFunctionReturn(0); 667052efed2SBarry Smith } 668052efed2SBarry Smith 6695615d1e5SSatish Balay #undef __FUNC__ 670d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ" 671e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat) 6721eb62cbbSBarry Smith { 67344a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 6741eb62cbbSBarry Smith int ierr; 67583e2fdc7SBarry Smith 6763a40ed3dSBarry Smith PetscFunctionBegin; 67770429bc8SBarry Smith if (--mat->refct > 0) PetscFunctionReturn(0); 67870429bc8SBarry Smith 67970429bc8SBarry Smith if (mat->mapping) { 68070429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->mapping); CHKERRQ(ierr); 68170429bc8SBarry Smith } 68270429bc8SBarry Smith if (mat->bmapping) { 68370429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->bmapping); CHKERRQ(ierr); 68470429bc8SBarry Smith } 68561b13de0SBarry Smith if (mat->rmap) { 68661b13de0SBarry Smith ierr = MapDestroy(mat->rmap);CHKERRQ(ierr); 68761b13de0SBarry Smith } 68861b13de0SBarry Smith if (mat->cmap) { 68961b13de0SBarry Smith ierr = MapDestroy(mat->cmap);CHKERRQ(ierr); 69061b13de0SBarry Smith } 6913a40ed3dSBarry Smith #if defined(USE_PETSC_LOG) 692e1311b90SBarry Smith PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N); 693a5a9c739SBarry Smith #endif 69483e2fdc7SBarry Smith ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr); 6950452661fSBarry Smith PetscFree(aij->rowners); 69678b31e54SBarry Smith ierr = MatDestroy(aij->A); CHKERRQ(ierr); 69778b31e54SBarry Smith ierr = MatDestroy(aij->B); CHKERRQ(ierr); 698b1fc9764SSatish Balay #if defined (USE_CTABLE) 699b1fc9764SSatish Balay if (aij->colmap) TableDelete(aij->colmap); 700b1fc9764SSatish Balay #else 7010452661fSBarry Smith if (aij->colmap) PetscFree(aij->colmap); 702b1fc9764SSatish Balay #endif 7030452661fSBarry Smith if (aij->garray) PetscFree(aij->garray); 7041eb62cbbSBarry Smith if (aij->lvec) VecDestroy(aij->lvec); 705a56f8943SBarry Smith if (aij->Mvctx) VecScatterDestroy(aij->Mvctx); 7067a0afa10SBarry Smith if (aij->rowvalues) PetscFree(aij->rowvalues); 7070452661fSBarry Smith PetscFree(aij); 708a5a9c739SBarry Smith PLogObjectDestroy(mat); 7090452661fSBarry Smith PetscHeaderDestroy(mat); 7103a40ed3dSBarry Smith PetscFunctionReturn(0); 7111eb62cbbSBarry Smith } 712ee50ffe9SBarry Smith 7135615d1e5SSatish Balay #undef __FUNC__ 714d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary" 715bc5ccf88SSatish Balay int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer) 7161eb62cbbSBarry Smith { 717416022c9SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 718416022c9SBarry Smith int ierr; 719416022c9SBarry Smith 7203a40ed3dSBarry Smith PetscFunctionBegin; 72117699dbbSLois Curfman McInnes if (aij->size == 1) { 722416022c9SBarry Smith ierr = MatView(aij->A,viewer); CHKERRQ(ierr); 723416022c9SBarry Smith } 724a8c6a408SBarry Smith else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported"); 7253a40ed3dSBarry Smith PetscFunctionReturn(0); 726416022c9SBarry Smith } 727416022c9SBarry Smith 7285615d1e5SSatish Balay #undef __FUNC__ 7297b2a1423SBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworSocket" 730bc5ccf88SSatish Balay int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,Viewer viewer) 731416022c9SBarry Smith { 73244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 733dbb450caSBarry Smith Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data; 73477ed5343SBarry Smith int ierr, format,shift = C->indexshift,rank = aij->rank ; 73577ed5343SBarry Smith int size = aij->size; 736d636dbe3SBarry Smith FILE *fd; 73719bcc07fSBarry Smith ViewerType vtype; 738416022c9SBarry Smith 7393a40ed3dSBarry Smith PetscFunctionBegin; 74019bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 7413f1db9ecSBarry Smith if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 742d8467735SBarry Smith ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr); 7430a198c4cSBarry Smith if (format == VIEWER_FORMAT_ASCII_INFO_LONG) { 7444e220ebcSLois Curfman McInnes MatInfo info; 7454e220ebcSLois Curfman McInnes int flg; 746a56f8943SBarry Smith MPI_Comm_rank(mat->comm,&rank); 74790ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 7484e220ebcSLois Curfman McInnes ierr = MatGetInfo(mat,MAT_LOCAL,&info); 74995e01e2fSLois Curfman McInnes ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg); CHKERRQ(ierr); 75077c4ece6SBarry Smith PetscSequentialPhaseBegin(mat->comm,1); 75195e01e2fSLois Curfman McInnes if (flg) fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n", 7524e220ebcSLois Curfman McInnes rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory); 75395e01e2fSLois Curfman McInnes else fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n", 7544e220ebcSLois Curfman McInnes rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory); 7554e220ebcSLois Curfman McInnes ierr = MatGetInfo(aij->A,MAT_LOCAL,&info); 7564e220ebcSLois Curfman McInnes fprintf(fd,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used); 7574e220ebcSLois Curfman McInnes ierr = MatGetInfo(aij->B,MAT_LOCAL,&info); 7584e220ebcSLois Curfman McInnes fprintf(fd,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used); 759a56f8943SBarry Smith fflush(fd); 76077c4ece6SBarry Smith PetscSequentialPhaseEnd(mat->comm,1); 761a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer); CHKERRQ(ierr); 7623a40ed3dSBarry Smith PetscFunctionReturn(0); 7633a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_INFO) { 7643a40ed3dSBarry Smith PetscFunctionReturn(0); 76508480c60SBarry Smith } 7663f1db9ecSBarry Smith } else if (PetscTypeCompare(vtype,DRAW_VIEWER)) { 76719bcc07fSBarry Smith Draw draw; 76819bcc07fSBarry Smith PetscTruth isnull; 76977ed5343SBarry Smith ierr = ViewerDrawGetDraw(viewer,0,&draw); CHKERRQ(ierr); 7703a40ed3dSBarry Smith ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 77119bcc07fSBarry Smith } 77219bcc07fSBarry Smith 77317699dbbSLois Curfman McInnes if (size == 1) { 77478b31e54SBarry Smith ierr = MatView(aij->A,viewer); CHKERRQ(ierr); 7753a40ed3dSBarry Smith } else { 77695373324SBarry Smith /* assemble the entire matrix onto first processor. */ 77795373324SBarry Smith Mat A; 778ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 7792eb8c8abSBarry Smith int M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct; 78095373324SBarry Smith Scalar *a; 7812ee70a88SLois Curfman McInnes 78217699dbbSLois Curfman McInnes if (!rank) { 78355843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 7843a40ed3dSBarry Smith } else { 78555843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 78695373324SBarry Smith } 787464493b3SBarry Smith PLogObjectParent(mat,A); 788416022c9SBarry Smith 78995373324SBarry Smith /* copy over the A part */ 790ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->A->data; 7912ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 79295373324SBarry Smith row = aij->rstart; 793dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;} 79495373324SBarry Smith for ( i=0; i<m; i++ ) { 795416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 79695373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 79795373324SBarry Smith } 7982ee70a88SLois Curfman McInnes aj = Aloc->j; 799dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;} 80095373324SBarry Smith 80195373324SBarry Smith /* copy over the B part */ 802ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->B->data; 8032ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 80495373324SBarry Smith row = aij->rstart; 8050452661fSBarry Smith ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) ); CHKPTRQ(cols); 806dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];} 80795373324SBarry Smith for ( i=0; i<m; i++ ) { 808416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 80995373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 81095373324SBarry Smith } 8110452661fSBarry Smith PetscFree(ct); 8126d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 8136d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 81455843e3eSBarry Smith /* 81555843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 81655843e3eSBarry Smith synchronized across all processors that share the Draw object 81755843e3eSBarry Smith */ 8183f1db9ecSBarry Smith if (!rank || PetscTypeCompare(vtype,DRAW_VIEWER)) { 81978b31e54SBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,viewer); CHKERRQ(ierr); 82095373324SBarry Smith } 82178b31e54SBarry Smith ierr = MatDestroy(A); CHKERRQ(ierr); 82295373324SBarry Smith } 8233a40ed3dSBarry Smith PetscFunctionReturn(0); 8241eb62cbbSBarry Smith } 8251eb62cbbSBarry Smith 8265615d1e5SSatish Balay #undef __FUNC__ 827d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ" 828e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer) 829416022c9SBarry Smith { 830416022c9SBarry Smith int ierr; 83119bcc07fSBarry Smith ViewerType vtype; 832416022c9SBarry Smith 8333a40ed3dSBarry Smith PetscFunctionBegin; 83419bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 8353f1db9ecSBarry Smith if (PetscTypeCompare(vtype,ASCII_VIEWER) || PetscTypeCompare(vtype,DRAW_VIEWER) || 8367b2a1423SBarry Smith PetscTypeCompare(vtype,SOCKET_VIEWER)) { 8377b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer); CHKERRQ(ierr); 8383f1db9ecSBarry Smith } else if (PetscTypeCompare(vtype,BINARY_VIEWER)) { 8393a40ed3dSBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 8405cd90555SBarry Smith } else { 8415cd90555SBarry Smith SETERRQ(1,1,"Viewer type not supported by PETSc object"); 842416022c9SBarry Smith } 8433a40ed3dSBarry Smith PetscFunctionReturn(0); 844416022c9SBarry Smith } 845416022c9SBarry Smith 8461eb62cbbSBarry Smith /* 8471eb62cbbSBarry Smith This has to provide several versions. 8481eb62cbbSBarry Smith 8491eb62cbbSBarry Smith 2) a) use only local smoothing updating outer values only once. 8501eb62cbbSBarry Smith b) local smoothing updating outer values each inner iteration 851d6dfbf8fSBarry Smith 3) color updating out values betwen colors. 8521eb62cbbSBarry Smith */ 8535615d1e5SSatish Balay #undef __FUNC__ 8545615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ" 8558f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag, 856dbb450caSBarry Smith double fshift,int its,Vec xx) 8578a729477SBarry Smith { 85844a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 859d6dfbf8fSBarry Smith Mat AA = mat->A, BB = mat->B; 860ec8511deSBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data; 861c16cb8f2SBarry Smith Scalar *b,*x,*xs,*ls,d,*v,sum; 8626abc6512SBarry Smith int ierr,*idx, *diag; 863416022c9SBarry Smith int n = mat->n, m = mat->m, i,shift = A->indexshift; 8648a729477SBarry Smith 8653a40ed3dSBarry Smith PetscFunctionBegin; 8662e8a6d31SBarry Smith ierr = VecGetArray(xx,&x); CHKERRQ(ierr); 8672e8a6d31SBarry Smith ierr = VecGetArray(bb,&b); CHKERRQ(ierr); 8682e8a6d31SBarry Smith ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr); 869dbb450caSBarry Smith xs = x + shift; /* shift by one for index start of 1 */ 870dbb450caSBarry Smith ls = ls + shift; 87183e2fdc7SBarry Smith if (!A->diag) {ierr = MatMarkDiag_SeqAIJ(AA); CHKERRQ(ierr);} 872d6dfbf8fSBarry Smith diag = A->diag; 873c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 874da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 875f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 8762e8a6d31SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 8772e8a6d31SBarry Smith ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr); 8782e8a6d31SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 8793a40ed3dSBarry Smith PetscFunctionReturn(0); 880da3a660dSBarry Smith } 8813a40ed3dSBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 8823a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 883d6dfbf8fSBarry Smith while (its--) { 884d6dfbf8fSBarry Smith /* go down through the rows */ 885d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 886d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 8874d197716SBarry Smith PLogFlops(4*n+3); 888dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 889dbb450caSBarry Smith v = A->a + A->i[i] + shift; 890d6dfbf8fSBarry Smith sum = b[i]; 891d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 892dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 893d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 894dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 895dbb450caSBarry Smith v = B->a + B->i[i] + shift; 896d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 89755a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 898d6dfbf8fSBarry Smith } 899d6dfbf8fSBarry Smith /* come up through the rows */ 900d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 901d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9024d197716SBarry Smith PLogFlops(4*n+3) 903dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 904dbb450caSBarry Smith v = A->a + A->i[i] + shift; 905d6dfbf8fSBarry Smith sum = b[i]; 906d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 907dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 908d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 909dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 910dbb450caSBarry Smith v = B->a + B->i[i] + shift; 911d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 91255a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 913d6dfbf8fSBarry Smith } 914d6dfbf8fSBarry Smith } 9153a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 916da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 917f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 9182e8a6d31SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 9192e8a6d31SBarry Smith ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr); 9202e8a6d31SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9213a40ed3dSBarry Smith PetscFunctionReturn(0); 922da3a660dSBarry Smith } 9233a40ed3dSBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 9243a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 925d6dfbf8fSBarry Smith while (its--) { 926d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 927d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9284d197716SBarry Smith PLogFlops(4*n+3); 929dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 930dbb450caSBarry Smith v = A->a + A->i[i] + shift; 931d6dfbf8fSBarry Smith sum = b[i]; 932d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 933dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 934d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 935dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 936dbb450caSBarry Smith v = B->a + B->i[i] + shift; 937d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 93855a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 939d6dfbf8fSBarry Smith } 940d6dfbf8fSBarry Smith } 9413a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 942da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 943f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 9442e8a6d31SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 9452e8a6d31SBarry Smith ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr); 9462e8a6d31SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9473a40ed3dSBarry Smith PetscFunctionReturn(0); 948da3a660dSBarry Smith } 9492e8a6d31SBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 9502e8a6d31SBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 951d6dfbf8fSBarry Smith while (its--) { 952d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 953d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9544d197716SBarry Smith PLogFlops(4*n+3); 955dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 956dbb450caSBarry Smith v = A->a + A->i[i] + shift; 957d6dfbf8fSBarry Smith sum = b[i]; 958d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 959dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 960d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 961dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 962dbb450caSBarry Smith v = B->a + B->i[i] + shift; 963d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 96455a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 965d6dfbf8fSBarry Smith } 966d6dfbf8fSBarry Smith } 9673a40ed3dSBarry Smith } else { 968a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported"); 969c16cb8f2SBarry Smith } 9702e8a6d31SBarry Smith ierr = VecRestoreArray(xx,&x); CHKERRQ(ierr); 9712e8a6d31SBarry Smith ierr = VecRestoreArray(bb,&b); CHKERRQ(ierr); 9722e8a6d31SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9733a40ed3dSBarry Smith PetscFunctionReturn(0); 9748a729477SBarry Smith } 975a66be287SLois Curfman McInnes 9765615d1e5SSatish Balay #undef __FUNC__ 977d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ" 9788f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 979a66be287SLois Curfman McInnes { 980a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 981a66be287SLois Curfman McInnes Mat A = mat->A, B = mat->B; 9824e220ebcSLois Curfman McInnes int ierr; 9834e220ebcSLois Curfman McInnes double isend[5], irecv[5]; 984a66be287SLois Curfman McInnes 9853a40ed3dSBarry Smith PetscFunctionBegin; 9864e220ebcSLois Curfman McInnes info->block_size = 1.0; 9874e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info); CHKERRQ(ierr); 9884e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 9894e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 9904e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info); CHKERRQ(ierr); 9914e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 9924e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 993a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 9944e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 9954e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 9964e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 9974e220ebcSLois Curfman McInnes info->memory = isend[3]; 9984e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 999a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1000ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr); 10014e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 10024e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 10034e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 10044e220ebcSLois Curfman McInnes info->memory = irecv[3]; 10054e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1006a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1007ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr); 10084e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 10094e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 10104e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 10114e220ebcSLois Curfman McInnes info->memory = irecv[3]; 10124e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1013a66be287SLois Curfman McInnes } 10144e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 10154e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 10164e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 10178d700155SBarry Smith info->rows_global = (double)mat->M; 10188d700155SBarry Smith info->columns_global = (double)mat->N; 10198d700155SBarry Smith info->rows_local = (double)mat->m; 10208d700155SBarry Smith info->columns_local = (double)mat->N; 10214e220ebcSLois Curfman McInnes 10223a40ed3dSBarry Smith PetscFunctionReturn(0); 1023a66be287SLois Curfman McInnes } 1024a66be287SLois Curfman McInnes 10255615d1e5SSatish Balay #undef __FUNC__ 1026d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ" 10278f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op) 1028c74985f6SBarry Smith { 1029c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 10301dee9f54SBarry Smith int ierr; 1031c74985f6SBarry Smith 10323a40ed3dSBarry Smith PetscFunctionBegin; 10336d4a8577SBarry Smith if (op == MAT_NO_NEW_NONZERO_LOCATIONS || 10346d4a8577SBarry Smith op == MAT_YES_NEW_NONZERO_LOCATIONS || 10356da5968aSLois Curfman McInnes op == MAT_COLUMNS_UNSORTED || 1036c2653b3dSLois Curfman McInnes op == MAT_COLUMNS_SORTED || 10374787f768SSatish Balay op == MAT_NEW_NONZERO_ALLOCATION_ERR || 10384787f768SSatish Balay op == MAT_NEW_NONZERO_LOCATION_ERR) { 10391dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10401dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 1041b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROW_ORIENTED) { 1042aeafbbfcSLois Curfman McInnes a->roworiented = 1; 10431dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10441dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 1045b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROWS_SORTED || 10466da5968aSLois Curfman McInnes op == MAT_ROWS_UNSORTED || 10476d4a8577SBarry Smith op == MAT_SYMMETRIC || 10486d4a8577SBarry Smith op == MAT_STRUCTURALLY_SYMMETRIC || 10496d4a8577SBarry Smith op == MAT_YES_NEW_DIAGONALS) 1050981c4779SBarry Smith PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n"); 10516d4a8577SBarry Smith else if (op == MAT_COLUMN_ORIENTED) { 10524b0e389bSBarry Smith a->roworiented = 0; 10531dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10541dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 10552b362799SSatish Balay } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) { 105690f02eecSBarry Smith a->donotstash = 1; 10573a40ed3dSBarry Smith } else if (op == MAT_NO_NEW_DIAGONALS){ 10583a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS"); 10593a40ed3dSBarry Smith } else { 10603a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"unknown option"); 10613a40ed3dSBarry Smith } 10623a40ed3dSBarry Smith PetscFunctionReturn(0); 1063c74985f6SBarry Smith } 1064c74985f6SBarry Smith 10655615d1e5SSatish Balay #undef __FUNC__ 1066d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ" 10678f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n) 1068c74985f6SBarry Smith { 106944a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 10703a40ed3dSBarry Smith 10713a40ed3dSBarry Smith PetscFunctionBegin; 10720752156aSBarry Smith if (m) *m = mat->M; 10730752156aSBarry Smith if (n) *n = mat->N; 10743a40ed3dSBarry Smith PetscFunctionReturn(0); 1075c74985f6SBarry Smith } 1076c74985f6SBarry Smith 10775615d1e5SSatish Balay #undef __FUNC__ 1078d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ" 10798f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n) 1080c74985f6SBarry Smith { 108144a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 10823a40ed3dSBarry Smith 10833a40ed3dSBarry Smith PetscFunctionBegin; 10840752156aSBarry Smith if (m) *m = mat->m; 1085f830108cSBarry Smith if (n) *n = mat->n; 10863a40ed3dSBarry Smith PetscFunctionReturn(0); 1087c74985f6SBarry Smith } 1088c74985f6SBarry Smith 10895615d1e5SSatish Balay #undef __FUNC__ 1090d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ" 10918f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n) 1092c74985f6SBarry Smith { 109344a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 10943a40ed3dSBarry Smith 10953a40ed3dSBarry Smith PetscFunctionBegin; 1096c74985f6SBarry Smith *m = mat->rstart; *n = mat->rend; 10973a40ed3dSBarry Smith PetscFunctionReturn(0); 1098c74985f6SBarry Smith } 1099c74985f6SBarry Smith 11005615d1e5SSatish Balay #undef __FUNC__ 11015615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ" 11026d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v) 110339e00950SLois Curfman McInnes { 1104154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 110570f0671dSBarry Smith Scalar *vworkA, *vworkB, **pvA, **pvB,*v_p; 1106154123eaSLois Curfman McInnes int i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart; 1107154123eaSLois Curfman McInnes int nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend; 110870f0671dSBarry Smith int *cmap, *idx_p; 110939e00950SLois Curfman McInnes 11103a40ed3dSBarry Smith PetscFunctionBegin; 1111a8c6a408SBarry Smith if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active"); 11127a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 11137a0afa10SBarry Smith 111470f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 11157a0afa10SBarry Smith /* 11167a0afa10SBarry Smith allocate enough space to hold information from the longest row. 11177a0afa10SBarry Smith */ 11187a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data; 1119c16cb8f2SBarry Smith int max = 1,m = mat->m,tmp; 1120c16cb8f2SBarry Smith for ( i=0; i<m; i++ ) { 11217a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 11227a0afa10SBarry Smith if (max < tmp) { max = tmp; } 11237a0afa10SBarry Smith } 11243f97c4b0SBarry Smith mat->rowvalues = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues); 11257a0afa10SBarry Smith mat->rowindices = (int *) (mat->rowvalues + max); 11267a0afa10SBarry Smith } 11277a0afa10SBarry Smith 1128a8c6a408SBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows") 1129abc0e9e4SLois Curfman McInnes lrow = row - rstart; 113039e00950SLois Curfman McInnes 1131154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1132154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1133154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1134f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr); 1135f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr); 1136154123eaSLois Curfman McInnes nztot = nzA + nzB; 1137154123eaSLois Curfman McInnes 113870f0671dSBarry Smith cmap = mat->garray; 1139154123eaSLois Curfman McInnes if (v || idx) { 1140154123eaSLois Curfman McInnes if (nztot) { 1141154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 114270f0671dSBarry Smith int imark = -1; 1143154123eaSLois Curfman McInnes if (v) { 114470f0671dSBarry Smith *v = v_p = mat->rowvalues; 114539e00950SLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 114670f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1147154123eaSLois Curfman McInnes else break; 1148154123eaSLois Curfman McInnes } 1149154123eaSLois Curfman McInnes imark = i; 115070f0671dSBarry Smith for ( i=0; i<nzA; i++ ) v_p[imark+i] = vworkA[i]; 115170f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) v_p[nzA+i] = vworkB[i]; 1152154123eaSLois Curfman McInnes } 1153154123eaSLois Curfman McInnes if (idx) { 115470f0671dSBarry Smith *idx = idx_p = mat->rowindices; 115570f0671dSBarry Smith if (imark > -1) { 115670f0671dSBarry Smith for ( i=0; i<imark; i++ ) { 115770f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 115870f0671dSBarry Smith } 115970f0671dSBarry Smith } else { 1160154123eaSLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 116170f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1162154123eaSLois Curfman McInnes else break; 1163154123eaSLois Curfman McInnes } 1164154123eaSLois Curfman McInnes imark = i; 116570f0671dSBarry Smith } 116670f0671dSBarry Smith for ( i=0; i<nzA; i++ ) idx_p[imark+i] = cstart + cworkA[i]; 116770f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) idx_p[nzA+i] = cmap[cworkB[i]]; 116839e00950SLois Curfman McInnes } 11693f97c4b0SBarry Smith } else { 11701ca473b0SSatish Balay if (idx) *idx = 0; 11711ca473b0SSatish Balay if (v) *v = 0; 11721ca473b0SSatish Balay } 1173154123eaSLois Curfman McInnes } 117439e00950SLois Curfman McInnes *nz = nztot; 1175f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr); 1176f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr); 11773a40ed3dSBarry Smith PetscFunctionReturn(0); 117839e00950SLois Curfman McInnes } 117939e00950SLois Curfman McInnes 11805615d1e5SSatish Balay #undef __FUNC__ 1181d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ" 11826d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v) 118339e00950SLois Curfman McInnes { 11847a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 11853a40ed3dSBarry Smith 11863a40ed3dSBarry Smith PetscFunctionBegin; 11877a0afa10SBarry Smith if (aij->getrowactive == PETSC_FALSE) { 1188a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called"); 11897a0afa10SBarry Smith } 11907a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 11913a40ed3dSBarry Smith PetscFunctionReturn(0); 119239e00950SLois Curfman McInnes } 119339e00950SLois Curfman McInnes 11945615d1e5SSatish Balay #undef __FUNC__ 11955615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ" 11968f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm) 1197855ac2c5SLois Curfman McInnes { 1198855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1199ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data; 1200416022c9SBarry Smith int ierr, i, j, cstart = aij->cstart,shift = amat->indexshift; 1201416022c9SBarry Smith double sum = 0.0; 120204ca555eSLois Curfman McInnes Scalar *v; 120304ca555eSLois Curfman McInnes 12043a40ed3dSBarry Smith PetscFunctionBegin; 120517699dbbSLois Curfman McInnes if (aij->size == 1) { 120614183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm); CHKERRQ(ierr); 120737fa93a5SLois Curfman McInnes } else { 120804ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 120904ca555eSLois Curfman McInnes v = amat->a; 121004ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++ ) { 12113a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 1212e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 121304ca555eSLois Curfman McInnes #else 121404ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 121504ca555eSLois Curfman McInnes #endif 121604ca555eSLois Curfman McInnes } 121704ca555eSLois Curfman McInnes v = bmat->a; 121804ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++ ) { 12193a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 1220e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 122104ca555eSLois Curfman McInnes #else 122204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 122304ca555eSLois Curfman McInnes #endif 122404ca555eSLois Curfman McInnes } 1225ca161407SBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 122604ca555eSLois Curfman McInnes *norm = sqrt(*norm); 12273a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 122804ca555eSLois Curfman McInnes double *tmp, *tmp2; 122904ca555eSLois Curfman McInnes int *jj, *garray = aij->garray; 1230758f045eSSatish Balay tmp = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp); 1231758f045eSSatish Balay tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp2); 1232cddf8d76SBarry Smith PetscMemzero(tmp,aij->N*sizeof(double)); 123304ca555eSLois Curfman McInnes *norm = 0.0; 123404ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 123504ca555eSLois Curfman McInnes for ( j=0; j<amat->nz; j++ ) { 1236579c6b6fSBarry Smith tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v); v++; 123704ca555eSLois Curfman McInnes } 123804ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 123904ca555eSLois Curfman McInnes for ( j=0; j<bmat->nz; j++ ) { 1240579c6b6fSBarry Smith tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++; 124104ca555eSLois Curfman McInnes } 1242ca161407SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 124304ca555eSLois Curfman McInnes for ( j=0; j<aij->N; j++ ) { 124404ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 124504ca555eSLois Curfman McInnes } 12460452661fSBarry Smith PetscFree(tmp); PetscFree(tmp2); 12473a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1248515d9167SLois Curfman McInnes double ntemp = 0.0; 124904ca555eSLois Curfman McInnes for ( j=0; j<amat->m; j++ ) { 1250dbb450caSBarry Smith v = amat->a + amat->i[j] + shift; 125104ca555eSLois Curfman McInnes sum = 0.0; 125204ca555eSLois Curfman McInnes for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) { 1253cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 125404ca555eSLois Curfman McInnes } 1255dbb450caSBarry Smith v = bmat->a + bmat->i[j] + shift; 125604ca555eSLois Curfman McInnes for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) { 1257cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 125804ca555eSLois Curfman McInnes } 1259515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 126004ca555eSLois Curfman McInnes } 1261ca161407SBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr); 1262ca161407SBarry Smith } else { 1263a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"No support for two norm"); 126404ca555eSLois Curfman McInnes } 126537fa93a5SLois Curfman McInnes } 12663a40ed3dSBarry Smith PetscFunctionReturn(0); 1267855ac2c5SLois Curfman McInnes } 1268855ac2c5SLois Curfman McInnes 12695615d1e5SSatish Balay #undef __FUNC__ 12705615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ" 12718f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout) 1272b7c46309SBarry Smith { 1273b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 1274dbb450caSBarry Smith Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data; 1275416022c9SBarry Smith int ierr,shift = Aloc->indexshift; 1276b7c46309SBarry Smith int M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct; 12773a40ed3dSBarry Smith Mat B; 1278b7c46309SBarry Smith Scalar *array; 1279b7c46309SBarry Smith 12803a40ed3dSBarry Smith PetscFunctionBegin; 1281d4bb536fSBarry Smith if (matout == PETSC_NULL && M != N) { 1282a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place"); 1283d4bb536fSBarry Smith } 1284d4bb536fSBarry Smith 1285d4bb536fSBarry Smith ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr); 1286b7c46309SBarry Smith 1287b7c46309SBarry Smith /* copy over the A part */ 1288ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->A->data; 1289b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1290b7c46309SBarry Smith row = a->rstart; 1291dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;} 1292b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1293416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1294b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 1295b7c46309SBarry Smith } 1296b7c46309SBarry Smith aj = Aloc->j; 12974af08d9eSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;} 1298b7c46309SBarry Smith 1299b7c46309SBarry Smith /* copy over the B part */ 1300ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->B->data; 1301b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1302b7c46309SBarry Smith row = a->rstart; 13030452661fSBarry Smith ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) ); CHKPTRQ(cols); 1304dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];} 1305b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1306416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1307b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 1308b7c46309SBarry Smith } 13090452661fSBarry Smith PetscFree(ct); 13106d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 13116d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 13123638b69dSLois Curfman McInnes if (matout != PETSC_NULL) { 13130de55854SLois Curfman McInnes *matout = B; 13140de55854SLois Curfman McInnes } else { 1315f830108cSBarry Smith PetscOps *Abops; 1316f830108cSBarry Smith struct _MatOps *Aops; 1317f830108cSBarry Smith 13180de55854SLois Curfman McInnes /* This isn't really an in-place transpose .... but free data structures from a */ 13190452661fSBarry Smith PetscFree(a->rowners); 13200de55854SLois Curfman McInnes ierr = MatDestroy(a->A); CHKERRQ(ierr); 13210de55854SLois Curfman McInnes ierr = MatDestroy(a->B); CHKERRQ(ierr); 1322b1fc9764SSatish Balay #if defined (USE_CTABLE) 1323b1fc9764SSatish Balay if (a->colmap) TableDelete(a->colmap); 1324b1fc9764SSatish Balay #else 13250452661fSBarry Smith if (a->colmap) PetscFree(a->colmap); 1326b1fc9764SSatish Balay #endif 13270452661fSBarry Smith if (a->garray) PetscFree(a->garray); 13280de55854SLois Curfman McInnes if (a->lvec) VecDestroy(a->lvec); 1329a56f8943SBarry Smith if (a->Mvctx) VecScatterDestroy(a->Mvctx); 13300452661fSBarry Smith PetscFree(a); 1331f830108cSBarry Smith 1332f830108cSBarry Smith /* 1333f830108cSBarry Smith This is horrible, horrible code. We need to keep the 1334f830108cSBarry Smith A pointers for the bops and ops but copy everything 1335f830108cSBarry Smith else from C. 1336f830108cSBarry Smith */ 1337f830108cSBarry Smith Abops = A->bops; 1338f830108cSBarry Smith Aops = A->ops; 1339f09e8eb9SSatish Balay PetscMemcpy(A,B,sizeof(struct _p_Mat)); 1340f830108cSBarry Smith A->bops = Abops; 1341f830108cSBarry Smith A->ops = Aops; 13420452661fSBarry Smith PetscHeaderDestroy(B); 13430de55854SLois Curfman McInnes } 13443a40ed3dSBarry Smith PetscFunctionReturn(0); 1345b7c46309SBarry Smith } 1346b7c46309SBarry Smith 13475615d1e5SSatish Balay #undef __FUNC__ 13485615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ" 13494b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1350a008b906SSatish Balay { 13514b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 13524b967eb1SSatish Balay Mat a = aij->A, b = aij->B; 1353a008b906SSatish Balay int ierr,s1,s2,s3; 1354a008b906SSatish Balay 13553a40ed3dSBarry Smith PetscFunctionBegin; 13564b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3); CHKERRQ(ierr); 13574b967eb1SSatish Balay if (rr) { 1358e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1359a8c6a408SBarry Smith if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size"); 13604b967eb1SSatish Balay /* Overlap communication with computation. */ 136143a90d84SBarry Smith ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr); 1362a008b906SSatish Balay } 13634b967eb1SSatish Balay if (ll) { 1364e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1365a8c6a408SBarry Smith if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size"); 1366f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0); CHKERRQ(ierr); 13674b967eb1SSatish Balay } 13684b967eb1SSatish Balay /* scale the diagonal block */ 1369f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr); CHKERRQ(ierr); 13704b967eb1SSatish Balay 13714b967eb1SSatish Balay if (rr) { 13724b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 137343a90d84SBarry Smith ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr); 1374f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec); CHKERRQ(ierr); 13754b967eb1SSatish Balay } 13764b967eb1SSatish Balay 13773a40ed3dSBarry Smith PetscFunctionReturn(0); 1378a008b906SSatish Balay } 1379a008b906SSatish Balay 1380a008b906SSatish Balay 13815615d1e5SSatish Balay #undef __FUNC__ 1382d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ" 13838f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A) 1384682d7d0cSBarry Smith { 1385682d7d0cSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 13863a40ed3dSBarry Smith int ierr; 1387682d7d0cSBarry Smith 13883a40ed3dSBarry Smith PetscFunctionBegin; 13893a40ed3dSBarry Smith if (!a->rank) { 13903a40ed3dSBarry Smith ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr); 13913a40ed3dSBarry Smith } 13923a40ed3dSBarry Smith PetscFunctionReturn(0); 1393682d7d0cSBarry Smith } 1394682d7d0cSBarry Smith 13955615d1e5SSatish Balay #undef __FUNC__ 1396d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ" 13978f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs) 13985a838052SSatish Balay { 13993a40ed3dSBarry Smith PetscFunctionBegin; 14005a838052SSatish Balay *bs = 1; 14013a40ed3dSBarry Smith PetscFunctionReturn(0); 14025a838052SSatish Balay } 14035615d1e5SSatish Balay #undef __FUNC__ 1404d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ" 14058f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A) 1406bb5a7306SBarry Smith { 1407bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 1408bb5a7306SBarry Smith int ierr; 14093a40ed3dSBarry Smith 14103a40ed3dSBarry Smith PetscFunctionBegin; 1411bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A); CHKERRQ(ierr); 14123a40ed3dSBarry Smith PetscFunctionReturn(0); 1413bb5a7306SBarry Smith } 1414bb5a7306SBarry Smith 1415d4bb536fSBarry Smith #undef __FUNC__ 1416d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ" 1417d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag) 1418d4bb536fSBarry Smith { 1419d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data; 1420d4bb536fSBarry Smith Mat a, b, c, d; 1421d4bb536fSBarry Smith PetscTruth flg; 1422d4bb536fSBarry Smith int ierr; 1423d4bb536fSBarry Smith 14243a40ed3dSBarry Smith PetscFunctionBegin; 1425a8c6a408SBarry Smith if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type"); 1426d4bb536fSBarry Smith a = matA->A; b = matA->B; 1427d4bb536fSBarry Smith c = matB->A; d = matB->B; 1428d4bb536fSBarry Smith 1429d4bb536fSBarry Smith ierr = MatEqual(a, c, &flg); CHKERRQ(ierr); 1430d4bb536fSBarry Smith if (flg == PETSC_TRUE) { 1431d4bb536fSBarry Smith ierr = MatEqual(b, d, &flg); CHKERRQ(ierr); 1432d4bb536fSBarry Smith } 1433ca161407SBarry Smith ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr); 14343a40ed3dSBarry Smith PetscFunctionReturn(0); 1435d4bb536fSBarry Smith } 1436d4bb536fSBarry Smith 1437cb5b572fSBarry Smith #undef __FUNC__ 1438cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ" 1439cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1440cb5b572fSBarry Smith { 1441cb5b572fSBarry Smith int ierr; 1442cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1443cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1444cb5b572fSBarry Smith 1445cb5b572fSBarry Smith PetscFunctionBegin; 1446cb5b572fSBarry Smith if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) { 1447cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1448cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1449cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1450cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1451cb5b572fSBarry Smith then copying the submatrices */ 1452cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1453cb5b572fSBarry Smith } else { 1454cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1455cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1456cb5b572fSBarry Smith } 1457cb5b572fSBarry Smith PetscFunctionReturn(0); 1458cb5b572fSBarry Smith } 1459cb5b572fSBarry Smith 14602e8a6d31SBarry Smith extern int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *); 14612f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int); 14620a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring); 14637b2a1423SBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatReuse,Mat **); 14647b2a1423SBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatReuse,Mat *); 146500e6dbe6SBarry Smith 14668a729477SBarry Smith /* -------------------------------------------------------------------*/ 1467cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 1468cda55fadSBarry Smith MatGetRow_MPIAIJ, 1469cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 1470cda55fadSBarry Smith MatMult_MPIAIJ, 1471cda55fadSBarry Smith MatMultAdd_MPIAIJ, 1472cda55fadSBarry Smith MatMultTrans_MPIAIJ, 1473cda55fadSBarry Smith MatMultTransAdd_MPIAIJ, 1474cda55fadSBarry Smith 0, 1475cda55fadSBarry Smith 0, 1476cda55fadSBarry Smith 0, 1477cda55fadSBarry Smith 0, 1478cda55fadSBarry Smith 0, 1479cda55fadSBarry Smith 0, 148044a69424SLois Curfman McInnes MatRelax_MPIAIJ, 1481b7c46309SBarry Smith MatTranspose_MPIAIJ, 1482cda55fadSBarry Smith MatGetInfo_MPIAIJ, 1483cda55fadSBarry Smith MatEqual_MPIAIJ, 1484cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 1485cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 1486cda55fadSBarry Smith MatNorm_MPIAIJ, 1487cda55fadSBarry Smith MatAssemblyBegin_MPIAIJ, 1488cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 14891eb62cbbSBarry Smith 0, 1490cda55fadSBarry Smith MatSetOption_MPIAIJ, 1491cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 1492cda55fadSBarry Smith MatZeroRows_MPIAIJ, 1493cda55fadSBarry Smith 0, 1494cda55fadSBarry Smith 0, 1495cda55fadSBarry Smith 0, 1496cda55fadSBarry Smith 0, 1497cda55fadSBarry Smith MatGetSize_MPIAIJ, 1498cda55fadSBarry Smith MatGetLocalSize_MPIAIJ, 1499cda55fadSBarry Smith MatGetOwnershipRange_MPIAIJ, 1500cda55fadSBarry Smith 0, 1501cda55fadSBarry Smith 0, 1502cda55fadSBarry Smith 0, 1503cda55fadSBarry Smith 0, 15042e8a6d31SBarry Smith MatDuplicate_MPIAIJ, 1505cda55fadSBarry Smith 0, 1506cda55fadSBarry Smith 0, 1507cda55fadSBarry Smith 0, 1508cda55fadSBarry Smith 0, 1509cda55fadSBarry Smith 0, 1510cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 1511cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 1512cda55fadSBarry Smith MatGetValues_MPIAIJ, 1513cb5b572fSBarry Smith MatCopy_MPIAIJ, 1514052efed2SBarry Smith MatPrintHelp_MPIAIJ, 1515cda55fadSBarry Smith MatScale_MPIAIJ, 1516cda55fadSBarry Smith 0, 1517cda55fadSBarry Smith 0, 1518cda55fadSBarry Smith 0, 1519cda55fadSBarry Smith MatGetBlockSize_MPIAIJ, 1520cda55fadSBarry Smith 0, 1521cda55fadSBarry Smith 0, 1522cda55fadSBarry Smith 0, 1523cda55fadSBarry Smith 0, 1524cda55fadSBarry Smith MatFDColoringCreate_MPIAIJ, 1525cda55fadSBarry Smith 0, 1526cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 1527cda55fadSBarry Smith 0, 1528cda55fadSBarry Smith 0, 1529cda55fadSBarry Smith MatGetSubMatrix_MPIAIJ, 1530cda55fadSBarry Smith 0, 1531cda55fadSBarry Smith 0, 1532cda55fadSBarry Smith MatGetMaps_Petsc}; 153336ce4990SBarry Smith 15342e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 15352e8a6d31SBarry Smith 1536fb2e594dSBarry Smith EXTERN_C_BEGIN 15372e8a6d31SBarry Smith #undef __FUNC__ 15382e8a6d31SBarry Smith #define __FUNC__ "MatStoreValues_MPIAIJ" 15392e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat) 15402e8a6d31SBarry Smith { 15412e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 15422e8a6d31SBarry Smith int ierr; 15432e8a6d31SBarry Smith 15442e8a6d31SBarry Smith PetscFunctionBegin; 15452e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 15462e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 15472e8a6d31SBarry Smith PetscFunctionReturn(0); 15482e8a6d31SBarry Smith } 1549fb2e594dSBarry Smith EXTERN_C_END 15502e8a6d31SBarry Smith 1551fb2e594dSBarry Smith EXTERN_C_BEGIN 15522e8a6d31SBarry Smith #undef __FUNC__ 15532e8a6d31SBarry Smith #define __FUNC__ "MatRetrieveValues_MPIAIJ" 15542e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat) 15552e8a6d31SBarry Smith { 15562e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 15572e8a6d31SBarry Smith int ierr; 15582e8a6d31SBarry Smith 15592e8a6d31SBarry Smith PetscFunctionBegin; 15602e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 15612e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 15622e8a6d31SBarry Smith PetscFunctionReturn(0); 15632e8a6d31SBarry Smith } 1564fb2e594dSBarry Smith EXTERN_C_END 15658a729477SBarry Smith 156627508adbSBarry Smith #include "pc.h" 156727508adbSBarry Smith EXTERN_C_BEGIN 15685ef9f2a5SBarry Smith extern int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *); 156927508adbSBarry Smith EXTERN_C_END 157027508adbSBarry Smith 15715615d1e5SSatish Balay #undef __FUNC__ 15725615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ" 15731987afe7SBarry Smith /*@C 1574ff756334SLois Curfman McInnes MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 15753a511b96SLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 15763a511b96SLois Curfman McInnes the user should preallocate the matrix storage by setting the parameters 15773a511b96SLois Curfman McInnes d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 15783a511b96SLois Curfman McInnes performance can be increased by more than a factor of 50. 15798a729477SBarry Smith 1580db81eaa0SLois Curfman McInnes Collective on MPI_Comm 1581db81eaa0SLois Curfman McInnes 15828a729477SBarry Smith Input Parameters: 1583db81eaa0SLois Curfman McInnes + comm - MPI communicator 15847d3e4905SLois Curfman McInnes . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 158592e8d321SLois Curfman McInnes This value should be the same as the local size used in creating the 158692e8d321SLois Curfman McInnes y vector for the matrix-vector product y = Ax. 15871a3896d6SBarry Smith . n - This value should be the same as the local size used in creating the 15881a3896d6SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 15891a3896d6SBarry Smith calculated if N is given) For square matrices n is almost always m. 159060d380a7SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 159160d380a7SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 1592af1d9917SSatish Balay . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 1593af1d9917SSatish Balay (same value is used for all local rows) 1594af1d9917SSatish Balay . d_nnz - array containing the number of nonzeros in the various rows of the 1595af1d9917SSatish Balay DIAGONAL portion of the local submatrix (possibly different for each row) 1596af1d9917SSatish Balay or PETSC_NULL, if d_nz is used to specify the nonzero structure. 1597af1d9917SSatish Balay The size of this array is equal to the number of local rows, i.e 'm'. 1598af1d9917SSatish Balay You must leave room for the diagonal entry even if it is zero. 1599af1d9917SSatish Balay . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 1600af1d9917SSatish Balay submatrix (same value is used for all local rows). 1601af1d9917SSatish Balay - o_nnz - array containing the number of nonzeros in the various rows of the 1602af1d9917SSatish Balay OFF-DIAGONAL portion of the local submatrix (possibly different for 1603af1d9917SSatish Balay each row) or PETSC_NULL, if o_nz is used to specify the nonzero 1604af1d9917SSatish Balay structure. The size of this array is equal to the number 1605af1d9917SSatish Balay of local rows, i.e 'm'. 16068a729477SBarry Smith 1607ff756334SLois Curfman McInnes Output Parameter: 160844cd7ae7SLois Curfman McInnes . A - the matrix 16098a729477SBarry Smith 1610b259b22eSLois Curfman McInnes Notes: 1611af1d9917SSatish Balay m,n,M,N parameters specify the size of the matrix, and its partitioning across 1612af1d9917SSatish Balay processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 1613af1d9917SSatish Balay storage requirements for this matrix. 1614af1d9917SSatish Balay 1615af1d9917SSatish Balay If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 1616af1d9917SSatish Balay processor than it must be used on all processors that share the object for 1617af1d9917SSatish Balay that argument. 1618be79a94dSBarry Smith 1619ff756334SLois Curfman McInnes The AIJ format (also called the Yale sparse matrix format or 1620ff756334SLois Curfman McInnes compressed row storage), is fully compatible with standard Fortran 77 16210002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 16220002213bSLois Curfman McInnes either one (as in Fortran) or zero. See the users manual for details. 1623ff756334SLois Curfman McInnes 1624ff756334SLois Curfman McInnes The user MUST specify either the local or global matrix dimensions 1625ff756334SLois Curfman McInnes (possibly both). 1626ff756334SLois Curfman McInnes 1627af1d9917SSatish Balay The parallel matrix is partitioned such that the first m0 rows belong to 1628af1d9917SSatish Balay process 0, the next m1 rows belong to process 1, the next m2 rows belong 1629af1d9917SSatish Balay to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 1630af1d9917SSatish Balay 1631af1d9917SSatish Balay The DIAGONAL portion of the local submatrix of a processor can be defined 1632af1d9917SSatish Balay as the submatrix which is obtained by extraction the part corresponding 1633af1d9917SSatish Balay to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 1634af1d9917SSatish Balay first row that belongs to the processor, and r2 is the last row belonging 1635af1d9917SSatish Balay to the this processor. This is a square mxm matrix. The remaining portion 1636af1d9917SSatish Balay of the local submatrix (mxN) constitute the OFF-DIAGONAL portion. 1637af1d9917SSatish Balay 1638af1d9917SSatish Balay If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 1639af1d9917SSatish Balay 16405511cfe3SLois Curfman McInnes By default, this format uses inodes (identical nodes) when possible. 16415511cfe3SLois Curfman McInnes We search for consecutive rows with the same nonzero structure, thereby 16425511cfe3SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 16435511cfe3SLois Curfman McInnes 16445511cfe3SLois Curfman McInnes Options Database Keys: 1645db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 1646db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 1647db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 1648db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 1649db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 1650494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1651494eafd4SBarry Smith (defaults to using SeqBAIJ format on one processor) 1652494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1653494eafd4SBarry Smith (defaults to using SeqAIJ format on one processor) 1654494eafd4SBarry Smith 16555511cfe3SLois Curfman McInnes 1656af1d9917SSatish Balay Example usage: 1657e0245417SLois Curfman McInnes 1658af1d9917SSatish Balay Consider the following 8x8 matrix with 34 non-zero values, that is 1659af1d9917SSatish Balay assembled across 3 processors. Lets assume that proc0 owns 3 rows, 1660af1d9917SSatish Balay proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 1661af1d9917SSatish Balay as follows: 16622191d07cSBarry Smith 1663db81eaa0SLois Curfman McInnes .vb 1664af1d9917SSatish Balay 1 2 0 | 0 3 0 | 0 4 1665af1d9917SSatish Balay Proc0 0 5 6 | 7 0 0 | 8 0 1666af1d9917SSatish Balay 9 0 10 | 11 0 0 | 12 0 1667af1d9917SSatish Balay ------------------------------------- 1668af1d9917SSatish Balay 13 0 14 | 15 16 17 | 0 0 1669af1d9917SSatish Balay Proc1 0 18 0 | 19 20 21 | 0 0 1670af1d9917SSatish Balay 0 0 0 | 22 23 0 | 24 0 1671af1d9917SSatish Balay ------------------------------------- 1672af1d9917SSatish Balay Proc2 25 26 27 | 0 0 28 | 29 0 1673af1d9917SSatish Balay 30 0 0 | 31 32 33 | 0 34 1674db81eaa0SLois Curfman McInnes .ve 1675b810aeb4SBarry Smith 1676af1d9917SSatish Balay This can be represented as a collection of submatrices as: 16775511cfe3SLois Curfman McInnes 1678af1d9917SSatish Balay .vb 1679af1d9917SSatish Balay A B C 1680af1d9917SSatish Balay D E F 1681af1d9917SSatish Balay G H I 1682af1d9917SSatish Balay .ve 1683af1d9917SSatish Balay 1684af1d9917SSatish Balay Where the submatrices A,B,C are owned by proc0, D,E,F are 1685af1d9917SSatish Balay owned by proc1, G,H,I are owned by proc2. 1686af1d9917SSatish Balay 1687af1d9917SSatish Balay The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 1688af1d9917SSatish Balay The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 1689af1d9917SSatish Balay The 'M','N' parameters are 8,8, and have the same values on all procs. 1690af1d9917SSatish Balay 1691af1d9917SSatish Balay The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 1692af1d9917SSatish Balay submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 1693af1d9917SSatish Balay corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 1694af1d9917SSatish Balay Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 1695af1d9917SSatish Balay part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 1696af1d9917SSatish Balay matrix, ans [DF] as another SeqAIJ matrix. 1697af1d9917SSatish Balay 1698af1d9917SSatish Balay When d_nz, o_nz parameters are specified, d_nz storage elements are 1699af1d9917SSatish Balay allocated for every row of the local diagonal submatrix, and o_nz 1700af1d9917SSatish Balay storage locations are allocated for every row of the OFF-DIAGONAL submat. 1701af1d9917SSatish Balay One way to choose d_nz and o_nz is to use the max nonzerors per local 1702af1d9917SSatish Balay rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 1703af1d9917SSatish Balay In this case, the values of d_nz,o_nz are: 1704af1d9917SSatish Balay .vb 1705af1d9917SSatish Balay proc0 : dnz = 2, o_nz = 2 1706af1d9917SSatish Balay proc1 : dnz = 3, o_nz = 2 1707af1d9917SSatish Balay proc2 : dnz = 1, o_nz = 4 1708af1d9917SSatish Balay .ve 1709af1d9917SSatish Balay We are allocating m*(d_nz+o_nz) storage locations for every proc. This 1710af1d9917SSatish Balay translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 1711af1d9917SSatish Balay for proc3. i.e we are using 12+15+10=37 storage locations to store 1712af1d9917SSatish Balay 34 values. 1713af1d9917SSatish Balay 1714af1d9917SSatish Balay When d_nnz, o_nnz parameters are specified, the storage is specified 1715af1d9917SSatish Balay for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 1716af1d9917SSatish Balay In the above case the values for d_nnz,o_nnz are: 1717af1d9917SSatish Balay .vb 1718af1d9917SSatish Balay proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 1719af1d9917SSatish Balay proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 1720af1d9917SSatish Balay proc2: d_nnz = [1,1] and o_nnz = [4,4] 1721af1d9917SSatish Balay .ve 1722af1d9917SSatish Balay Here the space allocated is sum of all the above values i.e 34, and 1723af1d9917SSatish Balay hence pre-allocation is perfect. 17243a511b96SLois Curfman McInnes 1725027ccd11SLois Curfman McInnes Level: intermediate 1726027ccd11SLois Curfman McInnes 1727dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel 1728ff756334SLois Curfman McInnes 1729fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues() 17308a729477SBarry Smith @*/ 1731e1311b90SBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,int *d_nnz,int o_nz,int *o_nnz,Mat *A) 17328a729477SBarry Smith { 173344cd7ae7SLois Curfman McInnes Mat B; 173444cd7ae7SLois Curfman McInnes Mat_MPIAIJ *b; 1735eac7125bSBarry Smith int ierr, i,size,flag1 = 0, flag2 = 0; 1736416022c9SBarry Smith 17373a40ed3dSBarry Smith PetscFunctionBegin; 17383914022bSBarry Smith MPI_Comm_size(comm,&size); 17397be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1); CHKERRQ(ierr); 17407be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2); CHKERRQ(ierr); 17417be0e774SBarry Smith if (!flag1 && !flag2 && size == 1) { 17423914022bSBarry Smith if (M == PETSC_DECIDE) M = m; 17433914022bSBarry Smith if (N == PETSC_DECIDE) N = n; 17443914022bSBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A); CHKERRQ(ierr); 17453a40ed3dSBarry Smith PetscFunctionReturn(0); 17463914022bSBarry Smith } 17473914022bSBarry Smith 174844cd7ae7SLois Curfman McInnes *A = 0; 17493f1db9ecSBarry Smith PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",comm,MatDestroy,MatView); 175044cd7ae7SLois Curfman McInnes PLogObjectCreate(B); 175144cd7ae7SLois Curfman McInnes B->data = (void *) (b = PetscNew(Mat_MPIAIJ)); CHKPTRQ(b); 175244cd7ae7SLois Curfman McInnes PetscMemzero(b,sizeof(Mat_MPIAIJ)); 1753cda55fadSBarry Smith PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps)); 1754e1311b90SBarry Smith B->ops->destroy = MatDestroy_MPIAIJ; 1755e1311b90SBarry Smith B->ops->view = MatView_MPIAIJ; 175644cd7ae7SLois Curfman McInnes B->factor = 0; 175744cd7ae7SLois Curfman McInnes B->assembled = PETSC_FALSE; 175890f02eecSBarry Smith B->mapping = 0; 1759d6dfbf8fSBarry Smith 176047794344SBarry Smith B->insertmode = NOT_SET_VALUES; 17619eb4d147SSatish Balay b->size = size; 176244cd7ae7SLois Curfman McInnes MPI_Comm_rank(comm,&b->rank); 17631eb62cbbSBarry Smith 17643a40ed3dSBarry Smith if (m == PETSC_DECIDE && (d_nnz != PETSC_NULL || o_nnz != PETSC_NULL)) { 1765a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz"); 17663a40ed3dSBarry Smith } 17671987afe7SBarry Smith 1768eac7125bSBarry Smith ierr = PetscSplitOwnership(comm,&m,&M);CHKERRQ(ierr); 1769eac7125bSBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 177044cd7ae7SLois Curfman McInnes b->m = m; B->m = m; 177144cd7ae7SLois Curfman McInnes b->n = n; B->n = n; 177244cd7ae7SLois Curfman McInnes b->N = N; B->N = N; 177344cd7ae7SLois Curfman McInnes b->M = M; B->M = M; 17741eb62cbbSBarry Smith 1775c7fcc2eaSBarry Smith /* the information in the maps duplicates the information computed below, eventually 1776c7fcc2eaSBarry Smith we should remove the duplicate information that is not contained in the maps */ 1777253a16ddSBarry Smith ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr); 1778253a16ddSBarry Smith ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr); 1779c7fcc2eaSBarry Smith 17801eb62cbbSBarry Smith /* build local table of row and column ownerships */ 178144cd7ae7SLois Curfman McInnes b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int)); CHKPTRQ(b->rowners); 1782f09e8eb9SSatish Balay PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1783603f58a4SSatish Balay b->cowners = b->rowners + b->size + 2; 1784ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 178544cd7ae7SLois Curfman McInnes b->rowners[0] = 0; 178644cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 178744cd7ae7SLois Curfman McInnes b->rowners[i] += b->rowners[i-1]; 17888a729477SBarry Smith } 178944cd7ae7SLois Curfman McInnes b->rstart = b->rowners[b->rank]; 179044cd7ae7SLois Curfman McInnes b->rend = b->rowners[b->rank+1]; 1791ca161407SBarry Smith ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 179244cd7ae7SLois Curfman McInnes b->cowners[0] = 0; 179344cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 179444cd7ae7SLois Curfman McInnes b->cowners[i] += b->cowners[i-1]; 17958a729477SBarry Smith } 179644cd7ae7SLois Curfman McInnes b->cstart = b->cowners[b->rank]; 179744cd7ae7SLois Curfman McInnes b->cend = b->cowners[b->rank+1]; 17988a729477SBarry Smith 17995ace5be8SLois Curfman McInnes if (d_nz == PETSC_DEFAULT) d_nz = 5; 1800029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A); CHKERRQ(ierr); 180144cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->A); 18027b8455f0SLois Curfman McInnes if (o_nz == PETSC_DEFAULT) o_nz = 0; 1803029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B); CHKERRQ(ierr); 180444cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->B); 18058a729477SBarry Smith 18061eb62cbbSBarry Smith /* build cache for off array entries formed */ 1807*d36fbae8SSatish Balay ierr = StashCreate_Private(comm,1,&b->stash); CHKERRQ(ierr); 180890f02eecSBarry Smith b->donotstash = 0; 180944cd7ae7SLois Curfman McInnes b->colmap = 0; 181044cd7ae7SLois Curfman McInnes b->garray = 0; 181144cd7ae7SLois Curfman McInnes b->roworiented = 1; 18128a729477SBarry Smith 18131eb62cbbSBarry Smith /* stuff used for matrix vector multiply */ 181444cd7ae7SLois Curfman McInnes b->lvec = 0; 181544cd7ae7SLois Curfman McInnes b->Mvctx = 0; 18168a729477SBarry Smith 18177a0afa10SBarry Smith /* stuff for MatGetRow() */ 181844cd7ae7SLois Curfman McInnes b->rowindices = 0; 181944cd7ae7SLois Curfman McInnes b->rowvalues = 0; 182044cd7ae7SLois Curfman McInnes b->getrowactive = PETSC_FALSE; 18217a0afa10SBarry Smith 18222e8a6d31SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C", 18232e8a6d31SBarry Smith "MatStoreValues_MPIAIJ", 18242e8a6d31SBarry Smith (void*)MatStoreValues_MPIAIJ);CHKERRQ(ierr); 18252e8a6d31SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C", 18262e8a6d31SBarry Smith "MatRetrieveValues_MPIAIJ", 18272e8a6d31SBarry Smith (void*)MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 18285ef9f2a5SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C", 18295ef9f2a5SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 18305ef9f2a5SBarry Smith (void*)MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 183144cd7ae7SLois Curfman McInnes *A = B; 18323a40ed3dSBarry Smith PetscFunctionReturn(0); 1833d6dfbf8fSBarry Smith } 1834c74985f6SBarry Smith 18355615d1e5SSatish Balay #undef __FUNC__ 18362e8a6d31SBarry Smith #define __FUNC__ "MatDuplicate_MPIAIJ" 18372e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 1838d6dfbf8fSBarry Smith { 1839d6dfbf8fSBarry Smith Mat mat; 1840416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data; 1841a1b97e82SLois Curfman McInnes int ierr, len=0, flg; 1842d6dfbf8fSBarry Smith 18433a40ed3dSBarry Smith PetscFunctionBegin; 1844416022c9SBarry Smith *newmat = 0; 18453f1db9ecSBarry Smith PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",matin->comm,MatDestroy,MatView); 1846a5a9c739SBarry Smith PLogObjectCreate(mat); 18470452661fSBarry Smith mat->data = (void *) (a = PetscNew(Mat_MPIAIJ)); CHKPTRQ(a); 1848cda55fadSBarry Smith PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps)); 1849e1311b90SBarry Smith mat->ops->destroy = MatDestroy_MPIAIJ; 1850e1311b90SBarry Smith mat->ops->view = MatView_MPIAIJ; 1851d6dfbf8fSBarry Smith mat->factor = matin->factor; 1852c456f294SBarry Smith mat->assembled = PETSC_TRUE; 1853e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 1854d6dfbf8fSBarry Smith 185544cd7ae7SLois Curfman McInnes a->m = mat->m = oldmat->m; 185644cd7ae7SLois Curfman McInnes a->n = mat->n = oldmat->n; 185744cd7ae7SLois Curfman McInnes a->M = mat->M = oldmat->M; 185844cd7ae7SLois Curfman McInnes a->N = mat->N = oldmat->N; 1859d6dfbf8fSBarry Smith 1860416022c9SBarry Smith a->rstart = oldmat->rstart; 1861416022c9SBarry Smith a->rend = oldmat->rend; 1862416022c9SBarry Smith a->cstart = oldmat->cstart; 1863416022c9SBarry Smith a->cend = oldmat->cend; 186417699dbbSLois Curfman McInnes a->size = oldmat->size; 186517699dbbSLois Curfman McInnes a->rank = oldmat->rank; 1866e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 1867e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 1868e7641de0SSatish Balay a->rowindices = 0; 1869bcd2baecSBarry Smith a->rowvalues = 0; 1870bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 1871d6dfbf8fSBarry Smith 1872603f58a4SSatish Balay a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int)); CHKPTRQ(a->rowners); 1873f09e8eb9SSatish Balay PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1874603f58a4SSatish Balay a->cowners = a->rowners + a->size + 2; 1875603f58a4SSatish Balay PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int)); 1876*d36fbae8SSatish Balay ierr = StashCreate_Private(matin->comm,1,&a->stash); CHKERRQ(ierr); 18772ee70a88SLois Curfman McInnes if (oldmat->colmap) { 1878b1fc9764SSatish Balay #if defined (USE_CTABLE) 1879fa46199cSSatish Balay ierr = TableCreateCopy(oldmat->colmap,&a->colmap); CHKERRQ(ierr); 1880b1fc9764SSatish Balay #else 18810452661fSBarry Smith a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap); 1882416022c9SBarry Smith PLogObjectMemory(mat,(a->N)*sizeof(int)); 1883416022c9SBarry Smith PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int)); 1884b1fc9764SSatish Balay #endif 1885416022c9SBarry Smith } else a->colmap = 0; 18863f41c07dSBarry Smith if (oldmat->garray) { 18873f41c07dSBarry Smith len = ((Mat_SeqAIJ *) (oldmat->B->data))->n; 18883f41c07dSBarry Smith a->garray = (int *) PetscMalloc((len+1)*sizeof(int)); CHKPTRQ(a->garray); 1889464493b3SBarry Smith PLogObjectMemory(mat,len*sizeof(int)); 18903f41c07dSBarry Smith if (len) PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int)); 1891416022c9SBarry Smith } else a->garray = 0; 1892d6dfbf8fSBarry Smith 1893416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec); CHKERRQ(ierr); 1894416022c9SBarry Smith PLogObjectParent(mat,a->lvec); 1895a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx); CHKERRQ(ierr); 1896416022c9SBarry Smith PLogObjectParent(mat,a->Mvctx); 18972e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A); CHKERRQ(ierr); 1898416022c9SBarry Smith PLogObjectParent(mat,a->A); 18992e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B); CHKERRQ(ierr); 1900416022c9SBarry Smith PLogObjectParent(mat,a->B); 19015dd7a6c7SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr); 190225cdf11fSBarry Smith if (flg) { 1903682d7d0cSBarry Smith ierr = MatPrintHelp(mat); CHKERRQ(ierr); 1904682d7d0cSBarry Smith } 190527508adbSBarry Smith ierr = FListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr); 19068a729477SBarry Smith *newmat = mat; 19073a40ed3dSBarry Smith PetscFunctionReturn(0); 19088a729477SBarry Smith } 1909416022c9SBarry Smith 191077c4ece6SBarry Smith #include "sys.h" 1911416022c9SBarry Smith 19125615d1e5SSatish Balay #undef __FUNC__ 19135615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ" 191419bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat) 1915416022c9SBarry Smith { 1916d65a2f8fSBarry Smith Mat A; 1917d65a2f8fSBarry Smith Scalar *vals,*svals; 191819bcc07fSBarry Smith MPI_Comm comm = ((PetscObject)viewer)->comm; 1919416022c9SBarry Smith MPI_Status status; 19203a40ed3dSBarry Smith int i, nz, ierr, j,rstart, rend, fd; 192117699dbbSLois Curfman McInnes int header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols; 1922d65a2f8fSBarry Smith int *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols; 1923b362ba68SBarry Smith int tag = ((PetscObject)viewer)->tag,cend,cstart,n; 1924416022c9SBarry Smith 19253a40ed3dSBarry Smith PetscFunctionBegin; 192617699dbbSLois Curfman McInnes MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank); 192717699dbbSLois Curfman McInnes if (!rank) { 192890ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 19290752156aSBarry Smith ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT); CHKERRQ(ierr); 1930a8c6a408SBarry Smith if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object"); 1931d64ed03dSBarry Smith if (header[3] < 0) { 1932a8c6a408SBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ"); 1933d64ed03dSBarry Smith } 19346c5fab8fSBarry Smith } 19356c5fab8fSBarry Smith 1936ca161407SBarry Smith ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr); 1937416022c9SBarry Smith M = header[1]; N = header[2]; 1938416022c9SBarry Smith /* determine ownership of all rows */ 193917699dbbSLois Curfman McInnes m = M/size + ((M % size) > rank); 19400452661fSBarry Smith rowners = (int *) PetscMalloc((size+2)*sizeof(int)); CHKPTRQ(rowners); 1941ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 1942416022c9SBarry Smith rowners[0] = 0; 194317699dbbSLois Curfman McInnes for ( i=2; i<=size; i++ ) { 1944416022c9SBarry Smith rowners[i] += rowners[i-1]; 1945416022c9SBarry Smith } 194617699dbbSLois Curfman McInnes rstart = rowners[rank]; 194717699dbbSLois Curfman McInnes rend = rowners[rank+1]; 1948416022c9SBarry Smith 1949416022c9SBarry Smith /* distribute row lengths to all processors */ 19500452661fSBarry Smith ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) ); CHKPTRQ(ourlens); 1951416022c9SBarry Smith offlens = ourlens + (rend-rstart); 195217699dbbSLois Curfman McInnes if (!rank) { 19530452661fSBarry Smith rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths); 19540752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr); 19550452661fSBarry Smith sndcounts = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(sndcounts); 195617699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i]; 1957ca161407SBarry Smith ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr); 19580452661fSBarry Smith PetscFree(sndcounts); 19593a40ed3dSBarry Smith } else { 1960ca161407SBarry Smith ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr); 1961416022c9SBarry Smith } 1962416022c9SBarry Smith 196317699dbbSLois Curfman McInnes if (!rank) { 1964416022c9SBarry Smith /* calculate the number of nonzeros on each processor */ 19650452661fSBarry Smith procsnz = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(procsnz); 1966cddf8d76SBarry Smith PetscMemzero(procsnz,size*sizeof(int)); 196717699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 1968416022c9SBarry Smith for ( j=rowners[i]; j< rowners[i+1]; j++ ) { 1969416022c9SBarry Smith procsnz[i] += rowlengths[j]; 1970416022c9SBarry Smith } 1971416022c9SBarry Smith } 19720452661fSBarry Smith PetscFree(rowlengths); 1973416022c9SBarry Smith 1974416022c9SBarry Smith /* determine max buffer needed and allocate it */ 1975416022c9SBarry Smith maxnz = 0; 197617699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 19770452661fSBarry Smith maxnz = PetscMax(maxnz,procsnz[i]); 1978416022c9SBarry Smith } 19790452661fSBarry Smith cols = (int *) PetscMalloc( maxnz*sizeof(int) ); CHKPTRQ(cols); 1980416022c9SBarry Smith 1981416022c9SBarry Smith /* read in my part of the matrix column indices */ 1982416022c9SBarry Smith nz = procsnz[0]; 19830452661fSBarry Smith mycols = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols); 19840752156aSBarry Smith ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT); CHKERRQ(ierr); 1985d65a2f8fSBarry Smith 1986d65a2f8fSBarry Smith /* read in every one elses and ship off */ 198717699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 1988d65a2f8fSBarry Smith nz = procsnz[i]; 19890752156aSBarry Smith ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT); CHKERRQ(ierr); 1990ca161407SBarry Smith ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr); 1991d65a2f8fSBarry Smith } 19920452661fSBarry Smith PetscFree(cols); 19933a40ed3dSBarry Smith } else { 1994416022c9SBarry Smith /* determine buffer space needed for message */ 1995416022c9SBarry Smith nz = 0; 1996416022c9SBarry Smith for ( i=0; i<m; i++ ) { 1997416022c9SBarry Smith nz += ourlens[i]; 1998416022c9SBarry Smith } 19990452661fSBarry Smith mycols = (int*) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols); 2000416022c9SBarry Smith 2001416022c9SBarry Smith /* receive message of column indices*/ 2002ca161407SBarry Smith ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 2003ca161407SBarry Smith ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr); 2004a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 2005416022c9SBarry Smith } 2006416022c9SBarry Smith 2007b362ba68SBarry Smith /* determine column ownership if matrix is not square */ 2008b362ba68SBarry Smith if (N != M) { 2009b362ba68SBarry Smith n = N/size + ((N % size) > rank); 2010b362ba68SBarry Smith ierr = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 2011b362ba68SBarry Smith cstart = cend - n; 2012b362ba68SBarry Smith } else { 2013b362ba68SBarry Smith cstart = rstart; 2014b362ba68SBarry Smith cend = rend; 2015fb2e594dSBarry Smith n = cend - cstart; 2016b362ba68SBarry Smith } 2017b362ba68SBarry Smith 2018416022c9SBarry Smith /* loop over local rows, determining number of off diagonal entries */ 2019cddf8d76SBarry Smith PetscMemzero(offlens,m*sizeof(int)); 2020416022c9SBarry Smith jj = 0; 2021416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2022416022c9SBarry Smith for ( j=0; j<ourlens[i]; j++ ) { 2023b362ba68SBarry Smith if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 2024416022c9SBarry Smith jj++; 2025416022c9SBarry Smith } 2026416022c9SBarry Smith } 2027d65a2f8fSBarry Smith 2028d65a2f8fSBarry Smith /* create our matrix */ 2029416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2030416022c9SBarry Smith ourlens[i] -= offlens[i]; 2031416022c9SBarry Smith } 2032b362ba68SBarry Smith ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr); 2033d65a2f8fSBarry Smith A = *newmat; 2034fb2e594dSBarry Smith ierr = MatSetOption(A,MAT_COLUMNS_SORTED); CHKERRQ(ierr); 2035d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2036d65a2f8fSBarry Smith ourlens[i] += offlens[i]; 2037d65a2f8fSBarry Smith } 2038416022c9SBarry Smith 203917699dbbSLois Curfman McInnes if (!rank) { 20400452661fSBarry Smith vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) ); CHKPTRQ(vals); 2041416022c9SBarry Smith 2042416022c9SBarry Smith /* read in my part of the matrix numerical values */ 2043416022c9SBarry Smith nz = procsnz[0]; 20440752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr); 2045d65a2f8fSBarry Smith 2046d65a2f8fSBarry Smith /* insert into matrix */ 2047d65a2f8fSBarry Smith jj = rstart; 2048d65a2f8fSBarry Smith smycols = mycols; 2049d65a2f8fSBarry Smith svals = vals; 2050d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2051d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2052d65a2f8fSBarry Smith smycols += ourlens[i]; 2053d65a2f8fSBarry Smith svals += ourlens[i]; 2054d65a2f8fSBarry Smith jj++; 2055416022c9SBarry Smith } 2056416022c9SBarry Smith 2057d65a2f8fSBarry Smith /* read in other processors and ship out */ 205817699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 2059416022c9SBarry Smith nz = procsnz[i]; 20600752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr); 2061ca161407SBarry Smith ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr); 2062416022c9SBarry Smith } 20630452661fSBarry Smith PetscFree(procsnz); 20643a40ed3dSBarry Smith } else { 2065d65a2f8fSBarry Smith /* receive numeric values */ 20660452661fSBarry Smith vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) ); CHKPTRQ(vals); 2067416022c9SBarry Smith 2068d65a2f8fSBarry Smith /* receive message of values*/ 2069ca161407SBarry Smith ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr); 2070ca161407SBarry Smith ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr); 2071a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 2072d65a2f8fSBarry Smith 2073d65a2f8fSBarry Smith /* insert into matrix */ 2074d65a2f8fSBarry Smith jj = rstart; 2075d65a2f8fSBarry Smith smycols = mycols; 2076d65a2f8fSBarry Smith svals = vals; 2077d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2078d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2079d65a2f8fSBarry Smith smycols += ourlens[i]; 2080d65a2f8fSBarry Smith svals += ourlens[i]; 2081d65a2f8fSBarry Smith jj++; 2082d65a2f8fSBarry Smith } 2083d65a2f8fSBarry Smith } 20840452661fSBarry Smith PetscFree(ourlens); PetscFree(vals); PetscFree(mycols); PetscFree(rowners); 2085d65a2f8fSBarry Smith 20866d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20876d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20883a40ed3dSBarry Smith PetscFunctionReturn(0); 2089416022c9SBarry Smith } 2090a0ff6018SBarry Smith 209129da9460SBarry Smith #undef __FUNC__ 209229da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ" 2093a0ff6018SBarry Smith /* 209429da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 209529da9460SBarry Smith in local and then by concatenating the local matrices the end result. 209629da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 2097a0ff6018SBarry Smith */ 20987b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat) 2099a0ff6018SBarry Smith { 210000e6dbe6SBarry Smith int ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j; 2101fee21e36SBarry Smith Mat *local,M, Mreuse; 210200e6dbe6SBarry Smith Scalar *vwork,*aa; 210300e6dbe6SBarry Smith MPI_Comm comm = mat->comm; 210400e6dbe6SBarry Smith Mat_SeqAIJ *aij; 210500e6dbe6SBarry Smith int *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend; 2106a0ff6018SBarry Smith 2107a0ff6018SBarry Smith PetscFunctionBegin; 210800e6dbe6SBarry Smith MPI_Comm_rank(comm,&rank); 210900e6dbe6SBarry Smith MPI_Comm_size(comm,&size); 211000e6dbe6SBarry Smith 2111fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 2112fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 2113fee21e36SBarry Smith if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse"); 2114fee21e36SBarry Smith local = &Mreuse; 2115fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 2116fee21e36SBarry Smith } else { 2117a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 2118fee21e36SBarry Smith Mreuse = *local; 2119fee21e36SBarry Smith PetscFree(local); 2120fee21e36SBarry Smith } 2121a0ff6018SBarry Smith 2122a0ff6018SBarry Smith /* 2123a0ff6018SBarry Smith m - number of local rows 2124a0ff6018SBarry Smith n - number of columns (same on all processors) 2125a0ff6018SBarry Smith rstart - first row in new global matrix generated 2126a0ff6018SBarry Smith */ 2127fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 2128a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2129fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2130a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 213100e6dbe6SBarry Smith ii = aij->i; 213200e6dbe6SBarry Smith jj = aij->j; 213300e6dbe6SBarry Smith 2134a0ff6018SBarry Smith /* 213500e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 213600e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 2137a0ff6018SBarry Smith */ 213800e6dbe6SBarry Smith 213900e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 21406a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 214100e6dbe6SBarry Smith nlocal = n/size + ((n % size) > rank); 21426a6a5d1dSBarry Smith } else { 21436a6a5d1dSBarry Smith nlocal = csize; 21446a6a5d1dSBarry Smith } 2145ca161407SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 214600e6dbe6SBarry Smith rstart = rend - nlocal; 21476a6a5d1dSBarry Smith if (rank == size - 1 && rend != n) { 21486a6a5d1dSBarry Smith SETERRQ(1,1,"Local column sizes do not add up to total number of columns"); 21496a6a5d1dSBarry Smith } 215000e6dbe6SBarry Smith 215100e6dbe6SBarry Smith /* next, compute all the lengths */ 215200e6dbe6SBarry Smith dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens); 215300e6dbe6SBarry Smith olens = dlens + m; 215400e6dbe6SBarry Smith for ( i=0; i<m; i++ ) { 215500e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 215600e6dbe6SBarry Smith olen = 0; 215700e6dbe6SBarry Smith dlen = 0; 215800e6dbe6SBarry Smith for ( j=0; j<jend; j++ ) { 215900e6dbe6SBarry Smith if ( *jj < rstart || *jj >= rend) olen++; 216000e6dbe6SBarry Smith else dlen++; 216100e6dbe6SBarry Smith jj++; 216200e6dbe6SBarry Smith } 216300e6dbe6SBarry Smith olens[i] = olen; 216400e6dbe6SBarry Smith dlens[i] = dlen; 216500e6dbe6SBarry Smith } 21662d207970SBarry Smith ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr); 216700e6dbe6SBarry Smith PetscFree(dlens); 2168a0ff6018SBarry Smith } else { 2169a0ff6018SBarry Smith int ml,nl; 2170a0ff6018SBarry Smith 2171a0ff6018SBarry Smith M = *newmat; 2172a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 2173a8c6a408SBarry Smith if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request"); 2174a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 2175c48de900SBarry Smith /* 2176c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 2177c48de900SBarry Smith rather than the slower MatSetValues(). 2178c48de900SBarry Smith */ 2179c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 2180c48de900SBarry Smith M->assembled = PETSC_FALSE; 2181a0ff6018SBarry Smith } 2182a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend); CHKERRQ(ierr); 2183fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2184a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 218500e6dbe6SBarry Smith ii = aij->i; 218600e6dbe6SBarry Smith jj = aij->j; 218700e6dbe6SBarry Smith aa = aij->a; 2188a0ff6018SBarry Smith for (i=0; i<m; i++) { 2189a0ff6018SBarry Smith row = rstart + i; 219000e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 219100e6dbe6SBarry Smith cwork = jj; jj += nz; 219200e6dbe6SBarry Smith vwork = aa; aa += nz; 21938c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 2194a0ff6018SBarry Smith } 2195a0ff6018SBarry Smith 2196a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2197a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2198a0ff6018SBarry Smith *newmat = M; 2199fee21e36SBarry Smith 2200fee21e36SBarry Smith /* save submatrix used in processor for next request */ 2201fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2202fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 2203fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 2204fee21e36SBarry Smith } 2205fee21e36SBarry Smith 2206a0ff6018SBarry Smith PetscFunctionReturn(0); 2207a0ff6018SBarry Smith } 2208