1*7c922b88SBarry Smith /*$Id: mpiaij.c,v 1.307 1999/11/05 14:45:21 bsmith Exp bsmith $*/ 28a729477SBarry Smith 370f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 4f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 5d9942c19SSatish Balay #include "src/inline/spops.h" 68a729477SBarry Smith 7bc5ccf88SSatish Balay extern int MatSetUpMultiply_MPIAIJ(Mat); 8bc5ccf88SSatish Balay extern int DisAssemble_MPIAIJ(Mat); 9bc5ccf88SSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode); 10bc5ccf88SSatish Balay extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 11bc5ccf88SSatish Balay extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 12bc5ccf88SSatish Balay extern int MatPrintHelp_SeqAIJ(Mat); 13bc5ccf88SSatish Balay 140f5bd95cSBarry Smith /* 150f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 169e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 170f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 180f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 190f5bd95cSBarry Smith has an order N integer array but is fast to acess. 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; 27dc2900e9SSatish Balay int n = B->n,i,ierr; 28dbb450caSBarry Smith 293a40ed3dSBarry Smith PetscFunctionBegin; 30aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 310f5bd95cSBarry Smith ierr = PetscTableCreate(aij->n/5,&aij->colmap);CHKERRQ(ierr); 32b1fc9764SSatish Balay for ( i=0; i<n; i++ ){ 330f5bd95cSBarry Smith ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr); 34b1fc9764SSatish Balay } 35b1fc9764SSatish Balay #else 36758f045eSSatish Balay aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap); 37464493b3SBarry Smith PLogObjectMemory(mat,aij->N*sizeof(int)); 38549d3d68SSatish Balay ierr = PetscMemzero(aij->colmap,aij->N*sizeof(int));CHKERRQ(ierr); 39905e6a2fSBarry Smith for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1; 40b1fc9764SSatish Balay #endif 413a40ed3dSBarry Smith PetscFunctionReturn(0); 429e25ed09SBarry Smith } 439e25ed09SBarry Smith 440520107fSSatish Balay #define CHUNKSIZE 15 4530770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 460520107fSSatish Balay { \ 470520107fSSatish Balay \ 480520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 4930770e4dSSatish Balay rmax = aimax[row]; nrow = ailen[row]; \ 50f5e9677aSSatish Balay col1 = col - shift; \ 51f5e9677aSSatish Balay \ 52ba4e3ef2SSatish Balay low = 0; high = nrow; \ 53ba4e3ef2SSatish Balay while (high-low > 5) { \ 54ba4e3ef2SSatish Balay t = (low+high)/2; \ 55ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 56ba4e3ef2SSatish Balay else low = t; \ 57ba4e3ef2SSatish Balay } \ 580520107fSSatish Balay for ( _i=0; _i<nrow; _i++ ) { \ 59f5e9677aSSatish Balay if (rp[_i] > col1) break; \ 60f5e9677aSSatish Balay if (rp[_i] == col1) { \ 610520107fSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 620520107fSSatish Balay else ap[_i] = value; \ 6330770e4dSSatish Balay goto a_noinsert; \ 640520107fSSatish Balay } \ 650520107fSSatish Balay } \ 6689280ab3SLois Curfman McInnes if (nonew == 1) goto a_noinsert; \ 67a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \ 680520107fSSatish Balay if (nrow >= rmax) { \ 690520107fSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 700520107fSSatish Balay int new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \ 710520107fSSatish Balay Scalar *new_a; \ 720520107fSSatish Balay \ 73a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \ 7489280ab3SLois Curfman McInnes \ 750520107fSSatish Balay /* malloc new storage space */ \ 760520107fSSatish Balay len = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \ 770520107fSSatish Balay new_a = (Scalar *) PetscMalloc( len );CHKPTRQ(new_a); \ 780520107fSSatish Balay new_j = (int *) (new_a + new_nz); \ 790520107fSSatish Balay new_i = new_j + new_nz; \ 800520107fSSatish Balay \ 810520107fSSatish Balay /* copy over old data into new slots */ \ 820520107fSSatish Balay for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \ 830520107fSSatish Balay for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \ 84549d3d68SSatish Balay ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \ 850520107fSSatish Balay len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \ 86549d3d68SSatish Balay ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \ 87549d3d68SSatish Balay len*sizeof(int));CHKERRQ(ierr); \ 88549d3d68SSatish Balay ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr); \ 89549d3d68SSatish Balay ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \ 90549d3d68SSatish Balay len*sizeof(Scalar));CHKERRQ(ierr); \ 910520107fSSatish Balay /* free up old matrix storage */ \ 92f5e9677aSSatish Balay \ 93606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); \ 94606d414cSSatish Balay if (!a->singlemalloc) { \ 95606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); \ 96606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); \ 97606d414cSSatish Balay } \ 980520107fSSatish Balay aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; \ 99*7c922b88SBarry Smith a->singlemalloc = PETSC_TRUE; \ 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;} \ 158549d3d68SSatish Balay ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \ 15930770e4dSSatish Balay len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \ 160549d3d68SSatish Balay ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \ 161549d3d68SSatish Balay len*sizeof(int));CHKERRQ(ierr); \ 162549d3d68SSatish Balay ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar));CHKERRQ(ierr); \ 163549d3d68SSatish Balay ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \ 164549d3d68SSatish Balay len*sizeof(Scalar));CHKERRQ(ierr); \ 16530770e4dSSatish Balay /* free up old matrix storage */ \ 16630770e4dSSatish Balay \ 167606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); \ 168606d414cSSatish Balay if (!b->singlemalloc) { \ 169606d414cSSatish Balay ierr = PetscFree(b->i);CHKERRQ(ierr); \ 170606d414cSSatish Balay ierr = PetscFree(b->j);CHKERRQ(ierr); \ 171606d414cSSatish Balay } \ 17274c639caSSatish Balay ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j; \ 173*7c922b88SBarry Smith b->singlemalloc = PETSC_TRUE; \ 17430770e4dSSatish Balay \ 17530770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 17630770e4dSSatish Balay rmax = bimax[row] = bimax[row] + CHUNKSIZE; \ 17774c639caSSatish Balay PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \ 17874c639caSSatish Balay b->maxnz += CHUNKSIZE; \ 17974c639caSSatish Balay b->reallocs++; \ 18030770e4dSSatish Balay } \ 18174c639caSSatish Balay N = nrow++ - 1; b->nz++; \ 18230770e4dSSatish Balay /* shift up all the later entries in this row */ \ 18330770e4dSSatish Balay for ( ii=N; ii>=_i; ii-- ) { \ 18430770e4dSSatish Balay rp[ii+1] = rp[ii]; \ 18530770e4dSSatish Balay ap[ii+1] = ap[ii]; \ 18630770e4dSSatish Balay } \ 18730770e4dSSatish Balay rp[_i] = col1; \ 18830770e4dSSatish Balay ap[_i] = value; \ 18930770e4dSSatish Balay b_noinsert: ; \ 19030770e4dSSatish Balay bilen[row] = nrow; \ 19130770e4dSSatish Balay } 19230770e4dSSatish Balay 1935615d1e5SSatish Balay #undef __FUNC__ 1945615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ" 1958f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv) 1968a729477SBarry Smith { 19744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1984b0e389bSBarry Smith Scalar value; 1991eb62cbbSBarry Smith int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 2001eb62cbbSBarry Smith int cstart = aij->cstart, cend = aij->cend,row,col; 201905e6a2fSBarry Smith int roworiented = aij->roworiented; 2028a729477SBarry Smith 2030520107fSSatish Balay /* Some Variables required in the macro */ 2044ee7247eSSatish Balay Mat A = aij->A; 2054ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 20630770e4dSSatish Balay int *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j; 20730770e4dSSatish Balay Scalar *aa = a->a; 20830770e4dSSatish Balay 20930770e4dSSatish Balay Mat B = aij->B; 21030770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data; 21130770e4dSSatish Balay int *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j; 21230770e4dSSatish Balay Scalar *ba = b->a; 21330770e4dSSatish Balay 214ba4e3ef2SSatish Balay int *rp,ii,nrow,_i,rmax, N, col1,low,high,t; 21530770e4dSSatish Balay int nonew = a->nonew,shift = a->indexshift; 21630770e4dSSatish Balay Scalar *ap; 2174ee7247eSSatish Balay 2183a40ed3dSBarry Smith PetscFunctionBegin; 2198a729477SBarry Smith for ( i=0; i<m; i++ ) { 2205ef9f2a5SBarry Smith if (im[i] < 0) continue; 221aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 222a8c6a408SBarry Smith if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 2230a198c4cSBarry Smith #endif 2244b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 2254b0e389bSBarry Smith row = im[i] - rstart; 2261eb62cbbSBarry Smith for ( j=0; j<n; j++ ) { 2274b0e389bSBarry Smith if (in[j] >= cstart && in[j] < cend){ 2284b0e389bSBarry Smith col = in[j] - cstart; 2294b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 23030770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 2310520107fSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2321eb62cbbSBarry Smith } 2335ef9f2a5SBarry Smith else if (in[j] < 0) continue; 234aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 235a8c6a408SBarry Smith else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");} 2360a198c4cSBarry Smith #endif 2371eb62cbbSBarry Smith else { 238227d817aSBarry Smith if (mat->was_assembled) { 239905e6a2fSBarry Smith if (!aij->colmap) { 240905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 241905e6a2fSBarry Smith } 242aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 2430f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 244fa46199cSSatish Balay col--; 245b1fc9764SSatish Balay #else 246905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 247b1fc9764SSatish Balay #endif 248ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 2492493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 2504b0e389bSBarry Smith col = in[j]; 2519bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 252f9508a3cSSatish Balay B = aij->B; 253f9508a3cSSatish Balay b = (Mat_SeqAIJ *) B->data; 254f9508a3cSSatish Balay bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 255f9508a3cSSatish Balay ba = b->a; 256d6dfbf8fSBarry Smith } 257c48de900SBarry Smith } else col = in[j]; 2584b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 25930770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 26030770e4dSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2611eb62cbbSBarry Smith } 2621eb62cbbSBarry Smith } 2635ef9f2a5SBarry Smith } else { 26490f02eecSBarry Smith if (!aij->donotstash) { 265d36fbae8SSatish Balay if (roworiented) { 2668798bf22SSatish Balay ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr); 267d36fbae8SSatish Balay } else { 2688798bf22SSatish Balay ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr); 2694b0e389bSBarry Smith } 2701eb62cbbSBarry Smith } 2718a729477SBarry Smith } 27290f02eecSBarry Smith } 2733a40ed3dSBarry Smith PetscFunctionReturn(0); 2748a729477SBarry Smith } 2758a729477SBarry Smith 2765615d1e5SSatish Balay #undef __FUNC__ 2775615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ" 2788f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 279b49de8d1SLois Curfman McInnes { 280b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 281b49de8d1SLois Curfman McInnes int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 282b49de8d1SLois Curfman McInnes int cstart = aij->cstart, cend = aij->cend,row,col; 283b49de8d1SLois Curfman McInnes 2843a40ed3dSBarry Smith PetscFunctionBegin; 285b49de8d1SLois Curfman McInnes for ( i=0; i<m; i++ ) { 286a8c6a408SBarry Smith if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 287a8c6a408SBarry Smith if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 288b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 289b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 290b49de8d1SLois Curfman McInnes for ( j=0; j<n; j++ ) { 291a8c6a408SBarry Smith if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column"); 292a8c6a408SBarry Smith if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large"); 293b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 294b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 295b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 296fa852ad4SSatish Balay } else { 297905e6a2fSBarry Smith if (!aij->colmap) { 298905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 299905e6a2fSBarry Smith } 300aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 3010f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 302fa46199cSSatish Balay col --; 303b1fc9764SSatish Balay #else 304905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 305b1fc9764SSatish Balay #endif 306e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 307d9d09a02SSatish Balay else { 308b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 309b49de8d1SLois Curfman McInnes } 310b49de8d1SLois Curfman McInnes } 311b49de8d1SLois Curfman McInnes } 312a8c6a408SBarry Smith } else { 313a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported"); 314b49de8d1SLois Curfman McInnes } 315b49de8d1SLois Curfman McInnes } 3163a40ed3dSBarry Smith PetscFunctionReturn(0); 317b49de8d1SLois Curfman McInnes } 318bc5ccf88SSatish Balay 319bc5ccf88SSatish Balay #undef __FUNC__ 320bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ" 321bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 322bc5ccf88SSatish Balay { 323bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 324d36fbae8SSatish Balay int ierr,nstash,reallocs; 325bc5ccf88SSatish Balay InsertMode addv; 326bc5ccf88SSatish Balay 327bc5ccf88SSatish Balay PetscFunctionBegin; 328bc5ccf88SSatish Balay if (aij->donotstash) { 329bc5ccf88SSatish Balay PetscFunctionReturn(0); 330bc5ccf88SSatish Balay } 331bc5ccf88SSatish Balay 332bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 333bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr); 334bc5ccf88SSatish Balay if (addv == (ADD_VALUES|INSERT_VALUES)) { 335bc5ccf88SSatish Balay SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added"); 336bc5ccf88SSatish Balay } 337bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 338bc5ccf88SSatish Balay 3398798bf22SSatish Balay ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr); 3408798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 3415a655dc6SBarry Smith PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs); 342bc5ccf88SSatish Balay PetscFunctionReturn(0); 343bc5ccf88SSatish Balay } 344bc5ccf88SSatish Balay 345bc5ccf88SSatish Balay 346bc5ccf88SSatish Balay #undef __FUNC__ 347bc5ccf88SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ" 348bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 349bc5ccf88SSatish Balay { 350bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 351a2d1c673SSatish Balay int i,j,rstart,ncols,n,ierr,flg; 352bc5ccf88SSatish Balay int *row,*col,other_disassembled; 353bc5ccf88SSatish Balay Scalar *val; 354bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 355bc5ccf88SSatish Balay 356bc5ccf88SSatish Balay PetscFunctionBegin; 357bc5ccf88SSatish Balay if (!aij->donotstash) { 358a2d1c673SSatish Balay while (1) { 3598798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 360a2d1c673SSatish Balay if (!flg) break; 361a2d1c673SSatish Balay 362bc5ccf88SSatish Balay for ( i=0; i<n; ) { 363bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 364bc5ccf88SSatish Balay for ( j=i,rstart=row[j]; j<n; j++ ) { if (row[j] != rstart) break; } 365bc5ccf88SSatish Balay if (j < n) ncols = j-i; 366bc5ccf88SSatish Balay else ncols = n-i; 367bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 368bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 369bc5ccf88SSatish Balay i = j; 370bc5ccf88SSatish Balay } 371bc5ccf88SSatish Balay } 3728798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 373bc5ccf88SSatish Balay } 374bc5ccf88SSatish Balay 375bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 376bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 377bc5ccf88SSatish Balay 378bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 379bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 380bc5ccf88SSatish Balay /* 381bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 382bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 383bc5ccf88SSatish Balay */ 384bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*) aij->B->data)->nonew) { 385bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr); 386bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 387bc5ccf88SSatish Balay ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 388bc5ccf88SSatish Balay } 389bc5ccf88SSatish Balay } 390bc5ccf88SSatish Balay 391bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 392bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 393bc5ccf88SSatish Balay } 394bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 395bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 396bc5ccf88SSatish Balay 397606d414cSSatish Balay if (aij->rowvalues) { 398606d414cSSatish Balay ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr); 399606d414cSSatish Balay aij->rowvalues = 0; 400606d414cSSatish Balay } 401bc5ccf88SSatish Balay PetscFunctionReturn(0); 402bc5ccf88SSatish Balay } 403bc5ccf88SSatish Balay 4045615d1e5SSatish Balay #undef __FUNC__ 4055615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ" 4068f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A) 4071eb62cbbSBarry Smith { 40844a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 409dbd7a890SLois Curfman McInnes int ierr; 4103a40ed3dSBarry Smith 4113a40ed3dSBarry Smith PetscFunctionBegin; 41278b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 41378b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 4143a40ed3dSBarry Smith PetscFunctionReturn(0); 4151eb62cbbSBarry Smith } 4161eb62cbbSBarry Smith 4175615d1e5SSatish Balay #undef __FUNC__ 4185615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ" 4198f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag) 4201eb62cbbSBarry Smith { 42144a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 42217699dbbSLois Curfman McInnes int i,ierr,N, *rows,*owners = l->rowners,size = l->size; 4236a5c57faSSatish Balay int *procs,*nprocs,j,found,idx,nsends,*work,row; 42417699dbbSLois Curfman McInnes int nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank; 4255392566eSBarry Smith int *rvalues,tag = A->tag,count,base,slen,n,*source; 4266a5c57faSSatish Balay int *lens,imdex,*lrows,*values,rstart=l->rstart; 427d6dfbf8fSBarry Smith MPI_Comm comm = A->comm; 4281eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 4291eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 4301eb62cbbSBarry Smith IS istmp; 4311eb62cbbSBarry Smith 4323a40ed3dSBarry Smith PetscFunctionBegin; 43377c4ece6SBarry Smith ierr = ISGetSize(is,&N);CHKERRQ(ierr); 43478b31e54SBarry Smith ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 4351eb62cbbSBarry Smith 4361eb62cbbSBarry Smith /* first count number of contributors to each processor */ 4370452661fSBarry Smith nprocs = (int *) PetscMalloc( 2*size*sizeof(int) );CHKPTRQ(nprocs); 438549d3d68SSatish Balay ierr = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr); 439549d3d68SSatish Balay procs = nprocs + size; 4400452661fSBarry Smith owner = (int *) PetscMalloc((N+1)*sizeof(int));CHKPTRQ(owner); /* see note*/ 4411eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 4421eb62cbbSBarry Smith idx = rows[i]; 4431eb62cbbSBarry Smith found = 0; 44417699dbbSLois Curfman McInnes for ( j=0; j<size; j++ ) { 4451eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 4461eb62cbbSBarry Smith nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break; 4471eb62cbbSBarry Smith } 4481eb62cbbSBarry Smith } 449a8c6a408SBarry Smith if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range"); 4501eb62cbbSBarry Smith } 45117699dbbSLois Curfman McInnes nsends = 0; for ( i=0; i<size; i++ ) { nsends += procs[i];} 4521eb62cbbSBarry Smith 4531eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 4546831982aSBarry Smith work = (int *) PetscMalloc( 2*size*sizeof(int) );CHKPTRQ(work); 4556831982aSBarry Smith ierr = MPI_Allreduce( nprocs, work,2*size,MPI_INT,PetscMaxSum_Op,comm);CHKERRQ(ierr); 4566831982aSBarry Smith nrecvs = work[size+rank]; 45717699dbbSLois Curfman McInnes nmax = work[rank]; 458606d414cSSatish Balay ierr = PetscFree(work);CHKERRQ(ierr); 4591eb62cbbSBarry Smith 4601eb62cbbSBarry Smith /* post receives: */ 4613a40ed3dSBarry Smith rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues); 462ca161407SBarry Smith recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits); 4631eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 464ca161407SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 4651eb62cbbSBarry Smith } 4661eb62cbbSBarry Smith 4671eb62cbbSBarry Smith /* do sends: 4681eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 4691eb62cbbSBarry Smith the ith processor 4701eb62cbbSBarry Smith */ 4710452661fSBarry Smith svalues = (int *) PetscMalloc( (N+1)*sizeof(int) );CHKPTRQ(svalues); 4723a40ed3dSBarry Smith send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits); 4730452661fSBarry Smith starts = (int *) PetscMalloc( (size+1)*sizeof(int) );CHKPTRQ(starts); 4741eb62cbbSBarry Smith starts[0] = 0; 47517699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 4761eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 4771eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 4781eb62cbbSBarry Smith } 4796831982aSBarry Smith ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 4801eb62cbbSBarry Smith 4811eb62cbbSBarry Smith starts[0] = 0; 48217699dbbSLois Curfman McInnes for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 4831eb62cbbSBarry Smith count = 0; 48417699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 4851eb62cbbSBarry Smith if (procs[i]) { 486ca161407SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 4871eb62cbbSBarry Smith } 4881eb62cbbSBarry Smith } 489606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 4901eb62cbbSBarry Smith 49117699dbbSLois Curfman McInnes base = owners[rank]; 4921eb62cbbSBarry Smith 4931eb62cbbSBarry Smith /* wait on receives */ 4940452661fSBarry Smith lens = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) );CHKPTRQ(lens); 4951eb62cbbSBarry Smith source = lens + nrecvs; 4961eb62cbbSBarry Smith count = nrecvs; slen = 0; 4971eb62cbbSBarry Smith while (count) { 498ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 4991eb62cbbSBarry Smith /* unpack receives into our local space */ 500ca161407SBarry Smith ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr); 501d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 502d6dfbf8fSBarry Smith lens[imdex] = n; 5031eb62cbbSBarry Smith slen += n; 5041eb62cbbSBarry Smith count--; 5051eb62cbbSBarry Smith } 506606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 5071eb62cbbSBarry Smith 5081eb62cbbSBarry Smith /* move the data into the send scatter */ 5090452661fSBarry Smith lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) );CHKPTRQ(lrows); 5101eb62cbbSBarry Smith count = 0; 5111eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 5121eb62cbbSBarry Smith values = rvalues + i*nmax; 5131eb62cbbSBarry Smith for ( j=0; j<lens[i]; j++ ) { 5141eb62cbbSBarry Smith lrows[count++] = values[j] - base; 5151eb62cbbSBarry Smith } 5161eb62cbbSBarry Smith } 517606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 518606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 519606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 520606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 5211eb62cbbSBarry Smith 5221eb62cbbSBarry Smith /* actually zap the local rows */ 523029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr); 524464493b3SBarry Smith PLogObjectParent(A,istmp); 5256a5c57faSSatish Balay 5266eb55b6aSBarry Smith /* 5276eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 5286eb55b6aSBarry Smith is square and the user wishes to set the diagonal we use seperate 5296eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 5306eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 5316eb55b6aSBarry Smith 5326eb55b6aSBarry Smith Contributed by: Mathew Knepley 5336eb55b6aSBarry Smith */ 534e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 535e2d53e46SBarry Smith ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr); 5366eb55b6aSBarry Smith if (diag && (l->A->M == l->A->N)) { 5376eb55b6aSBarry Smith ierr = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr); 538e2d53e46SBarry Smith } else if (diag) { 539e2d53e46SBarry Smith ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr); 540fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 541fa46199cSSatish Balay SETERRQ(PETSC_ERR_SUP,0,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 542fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 5436525c446SSatish Balay } 544e2d53e46SBarry Smith for ( i = 0; i < slen; i++ ) { 545e2d53e46SBarry Smith row = lrows[i] + rstart; 546e2d53e46SBarry Smith ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr); 547e2d53e46SBarry Smith } 548e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5506eb55b6aSBarry Smith } else { 5516a5c57faSSatish Balay ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr); 5526eb55b6aSBarry Smith } 55378b31e54SBarry Smith ierr = ISDestroy(istmp);CHKERRQ(ierr); 554606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 55572dacd9aSBarry Smith 5561eb62cbbSBarry Smith /* wait on sends */ 5571eb62cbbSBarry Smith if (nsends) { 558ca161407SBarry Smith send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status); 559ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 560606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 5611eb62cbbSBarry Smith } 562606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 563606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 5641eb62cbbSBarry Smith 5653a40ed3dSBarry Smith PetscFunctionReturn(0); 5661eb62cbbSBarry Smith } 5671eb62cbbSBarry Smith 5685615d1e5SSatish Balay #undef __FUNC__ 5695615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ" 5708f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 5711eb62cbbSBarry Smith { 572416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 573fbd6ef76SBarry Smith int ierr,nt; 574416022c9SBarry Smith 5753a40ed3dSBarry Smith PetscFunctionBegin; 576a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 577fbd6ef76SBarry Smith if (nt != a->n) { 578f1af5d2fSBarry Smith SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A (%d) and xx (%d)",a->n,nt); 579fbd6ef76SBarry Smith } 58043a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 581f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 58243a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 583f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 5843a40ed3dSBarry Smith PetscFunctionReturn(0); 5851eb62cbbSBarry Smith } 5861eb62cbbSBarry Smith 5875615d1e5SSatish Balay #undef __FUNC__ 5885615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ" 5898f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 590da3a660dSBarry Smith { 591416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 592da3a660dSBarry Smith int ierr; 5933a40ed3dSBarry Smith 5943a40ed3dSBarry Smith PetscFunctionBegin; 59543a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 596f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 59743a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 598f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 5993a40ed3dSBarry Smith PetscFunctionReturn(0); 600da3a660dSBarry Smith } 601da3a660dSBarry Smith 6025615d1e5SSatish Balay #undef __FUNC__ 603*7c922b88SBarry Smith #define __FUNC__ "MatMultTranspose_MPIAIJ" 604*7c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 605da3a660dSBarry Smith { 606416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 607da3a660dSBarry Smith int ierr; 608da3a660dSBarry Smith 6093a40ed3dSBarry Smith PetscFunctionBegin; 610da3a660dSBarry Smith /* do nondiagonal part */ 611*7c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 612da3a660dSBarry Smith /* send it on its way */ 613537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 614da3a660dSBarry Smith /* do local part */ 615*7c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 616da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 617da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 618da3a660dSBarry Smith /* but is not perhaps always true. */ 619537820f0SBarry Smith ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 6203a40ed3dSBarry Smith PetscFunctionReturn(0); 621da3a660dSBarry Smith } 622da3a660dSBarry Smith 6235615d1e5SSatish Balay #undef __FUNC__ 624*7c922b88SBarry Smith #define __FUNC__ "MatMultTransposeAdd_MPIAIJ" 625*7c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 626da3a660dSBarry Smith { 627416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 628da3a660dSBarry Smith int ierr; 629da3a660dSBarry Smith 6303a40ed3dSBarry Smith PetscFunctionBegin; 631da3a660dSBarry Smith /* do nondiagonal part */ 632*7c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 633da3a660dSBarry Smith /* send it on its way */ 634537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 635da3a660dSBarry Smith /* do local part */ 636*7c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 637da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 638da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 639da3a660dSBarry Smith /* but is not perhaps always true. */ 6400a198c4cSBarry Smith ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 6413a40ed3dSBarry Smith PetscFunctionReturn(0); 642da3a660dSBarry Smith } 643da3a660dSBarry Smith 6441eb62cbbSBarry Smith /* 6451eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 6461eb62cbbSBarry Smith diagonal block 6471eb62cbbSBarry Smith */ 6485615d1e5SSatish Balay #undef __FUNC__ 6495615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ" 6508f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v) 6511eb62cbbSBarry Smith { 6523a40ed3dSBarry Smith int ierr; 653416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 6543a40ed3dSBarry Smith 6553a40ed3dSBarry Smith PetscFunctionBegin; 656a8c6a408SBarry Smith if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block"); 6575baf8537SBarry Smith if (a->rstart != a->cstart || a->rend != a->cend) { 658a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition"); 6593a40ed3dSBarry Smith } 6603a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 6613a40ed3dSBarry Smith PetscFunctionReturn(0); 6621eb62cbbSBarry Smith } 6631eb62cbbSBarry Smith 6645615d1e5SSatish Balay #undef __FUNC__ 6655615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ" 6668f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A) 667052efed2SBarry Smith { 668052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 669052efed2SBarry Smith int ierr; 6703a40ed3dSBarry Smith 6713a40ed3dSBarry Smith PetscFunctionBegin; 672052efed2SBarry Smith ierr = MatScale(aa,a->A);CHKERRQ(ierr); 673052efed2SBarry Smith ierr = MatScale(aa,a->B);CHKERRQ(ierr); 6743a40ed3dSBarry Smith PetscFunctionReturn(0); 675052efed2SBarry Smith } 676052efed2SBarry Smith 6775615d1e5SSatish Balay #undef __FUNC__ 678d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ" 679e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat) 6801eb62cbbSBarry Smith { 68144a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 6821eb62cbbSBarry Smith int ierr; 68383e2fdc7SBarry Smith 6843a40ed3dSBarry Smith PetscFunctionBegin; 68570429bc8SBarry Smith 68670429bc8SBarry Smith if (mat->mapping) { 68770429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->mapping);CHKERRQ(ierr); 68870429bc8SBarry Smith } 68970429bc8SBarry Smith if (mat->bmapping) { 69070429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->bmapping);CHKERRQ(ierr); 69170429bc8SBarry Smith } 69261b13de0SBarry Smith if (mat->rmap) { 69361b13de0SBarry Smith ierr = MapDestroy(mat->rmap);CHKERRQ(ierr); 69461b13de0SBarry Smith } 69561b13de0SBarry Smith if (mat->cmap) { 69661b13de0SBarry Smith ierr = MapDestroy(mat->cmap);CHKERRQ(ierr); 69761b13de0SBarry Smith } 698aa482453SBarry Smith #if defined(PETSC_USE_LOG) 699e1311b90SBarry Smith PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N); 700a5a9c739SBarry Smith #endif 7018798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 702606d414cSSatish Balay ierr = PetscFree(aij->rowners);CHKERRQ(ierr); 70378b31e54SBarry Smith ierr = MatDestroy(aij->A);CHKERRQ(ierr); 70478b31e54SBarry Smith ierr = MatDestroy(aij->B);CHKERRQ(ierr); 705aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 7060f5bd95cSBarry Smith if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);} 707b1fc9764SSatish Balay #else 708606d414cSSatish Balay if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);} 709b1fc9764SSatish Balay #endif 710606d414cSSatish Balay if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);} 711*7c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 712*7c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 713606d414cSSatish Balay if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);} 714606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 715a5a9c739SBarry Smith PLogObjectDestroy(mat); 7160452661fSBarry Smith PetscHeaderDestroy(mat); 7173a40ed3dSBarry Smith PetscFunctionReturn(0); 7181eb62cbbSBarry Smith } 719ee50ffe9SBarry Smith 7205615d1e5SSatish Balay #undef __FUNC__ 721d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary" 722bc5ccf88SSatish Balay int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer) 7231eb62cbbSBarry Smith { 724416022c9SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 725416022c9SBarry Smith int ierr; 726416022c9SBarry Smith 7273a40ed3dSBarry Smith PetscFunctionBegin; 72817699dbbSLois Curfman McInnes if (aij->size == 1) { 729416022c9SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 730416022c9SBarry Smith } 731a8c6a408SBarry Smith else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported"); 7323a40ed3dSBarry Smith PetscFunctionReturn(0); 733416022c9SBarry Smith } 734416022c9SBarry Smith 7355615d1e5SSatish Balay #undef __FUNC__ 7367b2a1423SBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworSocket" 737bc5ccf88SSatish Balay int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,Viewer viewer) 738416022c9SBarry Smith { 73944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 740dbb450caSBarry Smith Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data; 7416831982aSBarry Smith int ierr, format,shift = C->indexshift,rank = aij->rank,size = aij->size; 742f1af5d2fSBarry Smith PetscTruth isdraw,isascii,flg; 743416022c9SBarry Smith 7443a40ed3dSBarry Smith PetscFunctionBegin; 7456831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&isdraw);CHKERRQ(ierr); 7466831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr); 7470f5bd95cSBarry Smith if (isascii) { 748d8467735SBarry Smith ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr); 7490a198c4cSBarry Smith if (format == VIEWER_FORMAT_ASCII_INFO_LONG) { 7504e220ebcSLois Curfman McInnes MatInfo info; 7511dab6e02SBarry Smith ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr); 752888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 75395e01e2fSLois Curfman McInnes ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr); 7546831982aSBarry Smith if (flg) { 7556831982aSBarry Smith ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n", 7566831982aSBarry Smith rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr); 7576831982aSBarry Smith } else { 7586831982aSBarry Smith ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n", 7596831982aSBarry Smith rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr); 7606831982aSBarry Smith } 761888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 7626831982aSBarry Smith ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr); 763888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 7646831982aSBarry Smith ierr = ViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr); 7656831982aSBarry Smith ierr = ViewerFlush(viewer);CHKERRQ(ierr); 766a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 7673a40ed3dSBarry Smith PetscFunctionReturn(0); 7683a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_INFO) { 7693a40ed3dSBarry Smith PetscFunctionReturn(0); 77008480c60SBarry Smith } 7710f5bd95cSBarry Smith } else if (isdraw) { 77219bcc07fSBarry Smith Draw draw; 77319bcc07fSBarry Smith PetscTruth isnull; 77477ed5343SBarry Smith ierr = ViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 7753a40ed3dSBarry Smith ierr = DrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 77619bcc07fSBarry Smith } 77719bcc07fSBarry Smith 77817699dbbSLois Curfman McInnes if (size == 1) { 77978b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 7803a40ed3dSBarry Smith } else { 78195373324SBarry Smith /* assemble the entire matrix onto first processor. */ 78295373324SBarry Smith Mat A; 783ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 7842eb8c8abSBarry Smith int M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct; 78595373324SBarry Smith Scalar *a; 7862ee70a88SLois Curfman McInnes 78717699dbbSLois Curfman McInnes if (!rank) { 78855843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 7893a40ed3dSBarry Smith } else { 79055843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 79195373324SBarry Smith } 792464493b3SBarry Smith PLogObjectParent(mat,A); 793416022c9SBarry Smith 79495373324SBarry Smith /* copy over the A part */ 795ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->A->data; 7962ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 79795373324SBarry Smith row = aij->rstart; 798dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;} 79995373324SBarry Smith for ( i=0; i<m; i++ ) { 800416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 80195373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 80295373324SBarry Smith } 8032ee70a88SLois Curfman McInnes aj = Aloc->j; 804dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;} 80595373324SBarry Smith 80695373324SBarry Smith /* copy over the B part */ 807ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->B->data; 8082ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 80995373324SBarry Smith row = aij->rstart; 8100452661fSBarry Smith ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) );CHKPTRQ(cols); 811dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];} 81295373324SBarry Smith for ( i=0; i<m; i++ ) { 813416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 81495373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 81595373324SBarry Smith } 816606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 8176d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 8186d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 81955843e3eSBarry Smith /* 82055843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 82155843e3eSBarry Smith synchronized across all processors that share the Draw object 82255843e3eSBarry Smith */ 8236831982aSBarry Smith if (!rank) { 8246831982aSBarry Smith Viewer sviewer; 8256831982aSBarry Smith ierr = ViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 8266831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 8276831982aSBarry Smith ierr = ViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 82895373324SBarry Smith } 82978b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 83095373324SBarry Smith } 8313a40ed3dSBarry Smith PetscFunctionReturn(0); 8321eb62cbbSBarry Smith } 8331eb62cbbSBarry Smith 8345615d1e5SSatish Balay #undef __FUNC__ 835d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ" 836e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer) 837416022c9SBarry Smith { 838416022c9SBarry Smith int ierr; 8396831982aSBarry Smith PetscTruth isascii,isdraw,issocket,isbinary; 840416022c9SBarry Smith 8413a40ed3dSBarry Smith PetscFunctionBegin; 8426831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr); 8436831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,DRAW_VIEWER,&isdraw);CHKERRQ(ierr); 8446831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,BINARY_VIEWER,&isbinary);CHKERRQ(ierr); 8456831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,SOCKET_VIEWER,&issocket);CHKERRQ(ierr); 8460f5bd95cSBarry Smith if (isascii || isdraw || isbinary || issocket) { 8477b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 8485cd90555SBarry Smith } else { 8490f5bd95cSBarry Smith SETERRQ1(1,1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 850416022c9SBarry Smith } 8513a40ed3dSBarry Smith PetscFunctionReturn(0); 852416022c9SBarry Smith } 853416022c9SBarry Smith 8541eb62cbbSBarry Smith /* 8551eb62cbbSBarry Smith This has to provide several versions. 8561eb62cbbSBarry Smith 8571eb62cbbSBarry Smith 2) a) use only local smoothing updating outer values only once. 8581eb62cbbSBarry Smith b) local smoothing updating outer values each inner iteration 859d6dfbf8fSBarry Smith 3) color updating out values betwen colors. 8601eb62cbbSBarry Smith */ 8615615d1e5SSatish Balay #undef __FUNC__ 8625615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ" 8638f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag, 864dbb450caSBarry Smith double fshift,int its,Vec xx) 8658a729477SBarry Smith { 86644a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 867d6dfbf8fSBarry Smith Mat AA = mat->A, BB = mat->B; 868ec8511deSBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data; 869c16cb8f2SBarry Smith Scalar *b,*x,*xs,*ls,d,*v,sum; 8706abc6512SBarry Smith int ierr,*idx, *diag; 871416022c9SBarry Smith int n = mat->n, m = mat->m, i,shift = A->indexshift; 8728a729477SBarry Smith 8733a40ed3dSBarry Smith PetscFunctionBegin; 874*7c922b88SBarry Smith if (!A->diag) {ierr = MatMarkDiagonal_SeqAIJ(AA);CHKERRQ(ierr);} 875d6dfbf8fSBarry Smith diag = A->diag; 876c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 877da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 878f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);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); 883184914b5SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 884184914b5SBarry Smith if (xx != bb) { 885184914b5SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 886184914b5SBarry Smith } else { 887184914b5SBarry Smith b = x; 888184914b5SBarry Smith } 889184914b5SBarry Smith ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr); 890184914b5SBarry Smith xs = x + shift; /* shift by one for index start of 1 */ 891184914b5SBarry Smith ls = ls + shift; 892d6dfbf8fSBarry Smith while (its--) { 893d6dfbf8fSBarry Smith /* go down through the rows */ 894d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 895d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 8964d197716SBarry Smith PLogFlops(4*n+3); 897dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 898dbb450caSBarry Smith v = A->a + A->i[i] + shift; 899d6dfbf8fSBarry Smith sum = b[i]; 900d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 901dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 902d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 903dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 904dbb450caSBarry Smith v = B->a + B->i[i] + shift; 905d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 90655a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 907d6dfbf8fSBarry Smith } 908d6dfbf8fSBarry Smith /* come up through the rows */ 909d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 910d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9114d197716SBarry Smith PLogFlops(4*n+3) 912dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 913dbb450caSBarry Smith v = A->a + A->i[i] + shift; 914d6dfbf8fSBarry Smith sum = b[i]; 915d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 916dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 917d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 918dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 919dbb450caSBarry Smith v = B->a + B->i[i] + shift; 920d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 92155a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 922d6dfbf8fSBarry Smith } 923d6dfbf8fSBarry Smith } 924184914b5SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 925184914b5SBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); } 926184914b5SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9273a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 928da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 929f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 9303a40ed3dSBarry Smith PetscFunctionReturn(0); 931da3a660dSBarry Smith } 9323a40ed3dSBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 9333a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 934184914b5SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 935184914b5SBarry Smith if (xx != bb) { 936184914b5SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 937184914b5SBarry Smith } else { 938184914b5SBarry Smith b = x; 939184914b5SBarry Smith } 940184914b5SBarry Smith ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr); 941184914b5SBarry Smith xs = x + shift; /* shift by one for index start of 1 */ 942184914b5SBarry Smith ls = ls + shift; 943d6dfbf8fSBarry Smith while (its--) { 944d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 945d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9464d197716SBarry Smith PLogFlops(4*n+3); 947dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 948dbb450caSBarry Smith v = A->a + A->i[i] + shift; 949d6dfbf8fSBarry Smith sum = b[i]; 950d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 951dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 952d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 953dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 954dbb450caSBarry Smith v = B->a + B->i[i] + shift; 955d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 95655a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 957d6dfbf8fSBarry Smith } 958d6dfbf8fSBarry Smith } 959184914b5SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 960184914b5SBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); } 961184914b5SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9623a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 963da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 964f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 9653a40ed3dSBarry Smith PetscFunctionReturn(0); 966da3a660dSBarry Smith } 9672e8a6d31SBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 9682e8a6d31SBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 969184914b5SBarry Smith ierr = VecGetArray(xx,&x);CHKERRQ(ierr); 970184914b5SBarry Smith if (xx != bb) { 971184914b5SBarry Smith ierr = VecGetArray(bb,&b);CHKERRQ(ierr); 972184914b5SBarry Smith } else { 973184914b5SBarry Smith b = x; 974184914b5SBarry Smith } 975184914b5SBarry Smith ierr = VecGetArray(mat->lvec,&ls);CHKERRQ(ierr); 976184914b5SBarry Smith xs = x + shift; /* shift by one for index start of 1 */ 977184914b5SBarry Smith ls = ls + shift; 978d6dfbf8fSBarry Smith while (its--) { 979d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 980d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9814d197716SBarry Smith PLogFlops(4*n+3); 982dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 983dbb450caSBarry Smith v = A->a + A->i[i] + shift; 984d6dfbf8fSBarry Smith sum = b[i]; 985d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 986dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 987d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 988dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 989dbb450caSBarry Smith v = B->a + B->i[i] + shift; 990d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 99155a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 992d6dfbf8fSBarry Smith } 993d6dfbf8fSBarry Smith } 994184914b5SBarry Smith ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); 995184914b5SBarry Smith if (bb != xx) {ierr = VecRestoreArray(bb,&b);CHKERRQ(ierr); } 996184914b5SBarry Smith ierr = VecRestoreArray(mat->lvec,&ls);CHKERRQ(ierr); 9973a40ed3dSBarry Smith } else { 998a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported"); 999c16cb8f2SBarry Smith } 10003a40ed3dSBarry Smith PetscFunctionReturn(0); 10018a729477SBarry Smith } 1002a66be287SLois Curfman McInnes 10035615d1e5SSatish Balay #undef __FUNC__ 1004d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ" 10058f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1006a66be287SLois Curfman McInnes { 1007a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 1008a66be287SLois Curfman McInnes Mat A = mat->A, B = mat->B; 10094e220ebcSLois Curfman McInnes int ierr; 10104e220ebcSLois Curfman McInnes double isend[5], irecv[5]; 1011a66be287SLois Curfman McInnes 10123a40ed3dSBarry Smith PetscFunctionBegin; 10134e220ebcSLois Curfman McInnes info->block_size = 1.0; 10144e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 10154e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 10164e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 10174e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 10184e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 10194e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1020a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 10214e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 10224e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 10234e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 10244e220ebcSLois Curfman McInnes info->memory = isend[3]; 10254e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1026a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1027ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr); 10284e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 10294e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 10304e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 10314e220ebcSLois Curfman McInnes info->memory = irecv[3]; 10324e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1033a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1034ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr); 10354e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 10364e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 10374e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 10384e220ebcSLois Curfman McInnes info->memory = irecv[3]; 10394e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1040a66be287SLois Curfman McInnes } 10414e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 10424e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 10434e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 10448d700155SBarry Smith info->rows_global = (double)mat->M; 10458d700155SBarry Smith info->columns_global = (double)mat->N; 10468d700155SBarry Smith info->rows_local = (double)mat->m; 10478d700155SBarry Smith info->columns_local = (double)mat->N; 10484e220ebcSLois Curfman McInnes 10493a40ed3dSBarry Smith PetscFunctionReturn(0); 1050a66be287SLois Curfman McInnes } 1051a66be287SLois Curfman McInnes 10525615d1e5SSatish Balay #undef __FUNC__ 1053d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ" 10548f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op) 1055c74985f6SBarry Smith { 1056c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 10571dee9f54SBarry Smith int ierr; 1058c74985f6SBarry Smith 10593a40ed3dSBarry Smith PetscFunctionBegin; 10606d4a8577SBarry Smith if (op == MAT_NO_NEW_NONZERO_LOCATIONS || 10616d4a8577SBarry Smith op == MAT_YES_NEW_NONZERO_LOCATIONS || 10626da5968aSLois Curfman McInnes op == MAT_COLUMNS_UNSORTED || 1063c2653b3dSLois Curfman McInnes op == MAT_COLUMNS_SORTED || 10644787f768SSatish Balay op == MAT_NEW_NONZERO_ALLOCATION_ERR || 1065*7c922b88SBarry Smith op == MAT_KEEP_ZEROED_ROWS || 10664787f768SSatish Balay op == MAT_NEW_NONZERO_LOCATION_ERR) { 10671dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10681dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 1069b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROW_ORIENTED) { 1070*7c922b88SBarry Smith a->roworiented = PETSC_TRUE; 10711dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10721dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 1073b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROWS_SORTED || 10746da5968aSLois Curfman McInnes op == MAT_ROWS_UNSORTED || 10756d4a8577SBarry Smith op == MAT_SYMMETRIC || 10766d4a8577SBarry Smith op == MAT_STRUCTURALLY_SYMMETRIC || 10776d4a8577SBarry Smith op == MAT_YES_NEW_DIAGONALS) 1078981c4779SBarry Smith PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n"); 10796d4a8577SBarry Smith else if (op == MAT_COLUMN_ORIENTED) { 1080*7c922b88SBarry Smith a->roworiented = PETSC_FALSE; 10811dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 10821dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 10832b362799SSatish Balay } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) { 1084*7c922b88SBarry Smith a->donotstash = PETSC_TRUE; 10853a40ed3dSBarry Smith } else if (op == MAT_NO_NEW_DIAGONALS){ 10863a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS"); 10873a40ed3dSBarry Smith } else { 10883a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"unknown option"); 10893a40ed3dSBarry Smith } 10903a40ed3dSBarry Smith PetscFunctionReturn(0); 1091c74985f6SBarry Smith } 1092c74985f6SBarry Smith 10935615d1e5SSatish Balay #undef __FUNC__ 1094d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ" 10958f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n) 1096c74985f6SBarry Smith { 109744a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 10983a40ed3dSBarry Smith 10993a40ed3dSBarry Smith PetscFunctionBegin; 11000752156aSBarry Smith if (m) *m = mat->M; 11010752156aSBarry Smith if (n) *n = mat->N; 11023a40ed3dSBarry Smith PetscFunctionReturn(0); 1103c74985f6SBarry Smith } 1104c74985f6SBarry Smith 11055615d1e5SSatish Balay #undef __FUNC__ 1106d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ" 11078f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n) 1108c74985f6SBarry Smith { 110944a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 11103a40ed3dSBarry Smith 11113a40ed3dSBarry Smith PetscFunctionBegin; 11120752156aSBarry Smith if (m) *m = mat->m; 1113f830108cSBarry Smith if (n) *n = mat->n; 11143a40ed3dSBarry Smith PetscFunctionReturn(0); 1115c74985f6SBarry Smith } 1116c74985f6SBarry Smith 11175615d1e5SSatish Balay #undef __FUNC__ 1118d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ" 11198f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n) 1120c74985f6SBarry Smith { 112144a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 11223a40ed3dSBarry Smith 11233a40ed3dSBarry Smith PetscFunctionBegin; 1124c74985f6SBarry Smith *m = mat->rstart; *n = mat->rend; 11253a40ed3dSBarry Smith PetscFunctionReturn(0); 1126c74985f6SBarry Smith } 1127c74985f6SBarry Smith 11285615d1e5SSatish Balay #undef __FUNC__ 11295615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ" 11306d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v) 113139e00950SLois Curfman McInnes { 1132154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 113370f0671dSBarry Smith Scalar *vworkA, *vworkB, **pvA, **pvB,*v_p; 1134154123eaSLois Curfman McInnes int i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart; 1135154123eaSLois Curfman McInnes int nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend; 113670f0671dSBarry Smith int *cmap, *idx_p; 113739e00950SLois Curfman McInnes 11383a40ed3dSBarry Smith PetscFunctionBegin; 1139a8c6a408SBarry Smith if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active"); 11407a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 11417a0afa10SBarry Smith 114270f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 11437a0afa10SBarry Smith /* 11447a0afa10SBarry Smith allocate enough space to hold information from the longest row. 11457a0afa10SBarry Smith */ 11467a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data; 1147c16cb8f2SBarry Smith int max = 1,m = mat->m,tmp; 1148c16cb8f2SBarry Smith for ( i=0; i<m; i++ ) { 11497a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 11507a0afa10SBarry Smith if (max < tmp) { max = tmp; } 11517a0afa10SBarry Smith } 11523f97c4b0SBarry Smith mat->rowvalues = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues); 11537a0afa10SBarry Smith mat->rowindices = (int *) (mat->rowvalues + max); 11547a0afa10SBarry Smith } 11557a0afa10SBarry Smith 1156a8c6a408SBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows") 1157abc0e9e4SLois Curfman McInnes lrow = row - rstart; 115839e00950SLois Curfman McInnes 1159154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1160154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1161154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1162f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1163f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1164154123eaSLois Curfman McInnes nztot = nzA + nzB; 1165154123eaSLois Curfman McInnes 116670f0671dSBarry Smith cmap = mat->garray; 1167154123eaSLois Curfman McInnes if (v || idx) { 1168154123eaSLois Curfman McInnes if (nztot) { 1169154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 117070f0671dSBarry Smith int imark = -1; 1171154123eaSLois Curfman McInnes if (v) { 117270f0671dSBarry Smith *v = v_p = mat->rowvalues; 117339e00950SLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 117470f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1175154123eaSLois Curfman McInnes else break; 1176154123eaSLois Curfman McInnes } 1177154123eaSLois Curfman McInnes imark = i; 117870f0671dSBarry Smith for ( i=0; i<nzA; i++ ) v_p[imark+i] = vworkA[i]; 117970f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) v_p[nzA+i] = vworkB[i]; 1180154123eaSLois Curfman McInnes } 1181154123eaSLois Curfman McInnes if (idx) { 118270f0671dSBarry Smith *idx = idx_p = mat->rowindices; 118370f0671dSBarry Smith if (imark > -1) { 118470f0671dSBarry Smith for ( i=0; i<imark; i++ ) { 118570f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 118670f0671dSBarry Smith } 118770f0671dSBarry Smith } else { 1188154123eaSLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 118970f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1190154123eaSLois Curfman McInnes else break; 1191154123eaSLois Curfman McInnes } 1192154123eaSLois Curfman McInnes imark = i; 119370f0671dSBarry Smith } 119470f0671dSBarry Smith for ( i=0; i<nzA; i++ ) idx_p[imark+i] = cstart + cworkA[i]; 119570f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) idx_p[nzA+i] = cmap[cworkB[i]]; 119639e00950SLois Curfman McInnes } 11973f97c4b0SBarry Smith } else { 11981ca473b0SSatish Balay if (idx) *idx = 0; 11991ca473b0SSatish Balay if (v) *v = 0; 12001ca473b0SSatish Balay } 1201154123eaSLois Curfman McInnes } 120239e00950SLois Curfman McInnes *nz = nztot; 1203f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1204f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 12053a40ed3dSBarry Smith PetscFunctionReturn(0); 120639e00950SLois Curfman McInnes } 120739e00950SLois Curfman McInnes 12085615d1e5SSatish Balay #undef __FUNC__ 1209d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ" 12106d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v) 121139e00950SLois Curfman McInnes { 12127a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 12133a40ed3dSBarry Smith 12143a40ed3dSBarry Smith PetscFunctionBegin; 12157a0afa10SBarry Smith if (aij->getrowactive == PETSC_FALSE) { 1216a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called"); 12177a0afa10SBarry Smith } 12187a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 12193a40ed3dSBarry Smith PetscFunctionReturn(0); 122039e00950SLois Curfman McInnes } 122139e00950SLois Curfman McInnes 12225615d1e5SSatish Balay #undef __FUNC__ 12235615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ" 12248f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm) 1225855ac2c5SLois Curfman McInnes { 1226855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1227ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data; 1228416022c9SBarry Smith int ierr, i, j, cstart = aij->cstart,shift = amat->indexshift; 1229416022c9SBarry Smith double sum = 0.0; 123004ca555eSLois Curfman McInnes Scalar *v; 123104ca555eSLois Curfman McInnes 12323a40ed3dSBarry Smith PetscFunctionBegin; 123317699dbbSLois Curfman McInnes if (aij->size == 1) { 123414183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 123537fa93a5SLois Curfman McInnes } else { 123604ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 123704ca555eSLois Curfman McInnes v = amat->a; 123804ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++ ) { 1239aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1240e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 124104ca555eSLois Curfman McInnes #else 124204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 124304ca555eSLois Curfman McInnes #endif 124404ca555eSLois Curfman McInnes } 124504ca555eSLois Curfman McInnes v = bmat->a; 124604ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++ ) { 1247aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1248e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 124904ca555eSLois Curfman McInnes #else 125004ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 125104ca555eSLois Curfman McInnes #endif 125204ca555eSLois Curfman McInnes } 1253ca161407SBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 125404ca555eSLois Curfman McInnes *norm = sqrt(*norm); 12553a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 125604ca555eSLois Curfman McInnes double *tmp, *tmp2; 125704ca555eSLois Curfman McInnes int *jj, *garray = aij->garray; 1258758f045eSSatish Balay tmp = (double *) PetscMalloc( (aij->N+1)*sizeof(double) );CHKPTRQ(tmp); 1259758f045eSSatish Balay tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) );CHKPTRQ(tmp2); 1260549d3d68SSatish Balay ierr = PetscMemzero(tmp,aij->N*sizeof(double));CHKERRQ(ierr); 126104ca555eSLois Curfman McInnes *norm = 0.0; 126204ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 126304ca555eSLois Curfman McInnes for ( j=0; j<amat->nz; j++ ) { 1264579c6b6fSBarry Smith tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v); v++; 126504ca555eSLois Curfman McInnes } 126604ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 126704ca555eSLois Curfman McInnes for ( j=0; j<bmat->nz; j++ ) { 1268579c6b6fSBarry Smith tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++; 126904ca555eSLois Curfman McInnes } 1270ca161407SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 127104ca555eSLois Curfman McInnes for ( j=0; j<aij->N; j++ ) { 127204ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 127304ca555eSLois Curfman McInnes } 1274606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1275606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 12763a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1277515d9167SLois Curfman McInnes double ntemp = 0.0; 127804ca555eSLois Curfman McInnes for ( j=0; j<amat->m; j++ ) { 1279dbb450caSBarry Smith v = amat->a + amat->i[j] + shift; 128004ca555eSLois Curfman McInnes sum = 0.0; 128104ca555eSLois Curfman McInnes for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) { 1282cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 128304ca555eSLois Curfman McInnes } 1284dbb450caSBarry Smith v = bmat->a + bmat->i[j] + shift; 128504ca555eSLois Curfman McInnes for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) { 1286cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 128704ca555eSLois Curfman McInnes } 1288515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 128904ca555eSLois Curfman McInnes } 1290ca161407SBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr); 1291ca161407SBarry Smith } else { 1292a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"No support for two norm"); 129304ca555eSLois Curfman McInnes } 129437fa93a5SLois Curfman McInnes } 12953a40ed3dSBarry Smith PetscFunctionReturn(0); 1296855ac2c5SLois Curfman McInnes } 1297855ac2c5SLois Curfman McInnes 12985615d1e5SSatish Balay #undef __FUNC__ 12995615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ" 13008f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout) 1301b7c46309SBarry Smith { 1302b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 1303dbb450caSBarry Smith Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data; 1304416022c9SBarry Smith int ierr,shift = Aloc->indexshift; 1305b7c46309SBarry Smith int M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct; 13063a40ed3dSBarry Smith Mat B; 1307b7c46309SBarry Smith Scalar *array; 1308b7c46309SBarry Smith 13093a40ed3dSBarry Smith PetscFunctionBegin; 1310*7c922b88SBarry Smith if (!matout && M != N) { 1311a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place"); 1312d4bb536fSBarry Smith } 1313d4bb536fSBarry Smith 1314d4bb536fSBarry Smith ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr); 1315b7c46309SBarry Smith 1316b7c46309SBarry Smith /* copy over the A part */ 1317ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->A->data; 1318b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1319b7c46309SBarry Smith row = a->rstart; 1320dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;} 1321b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1322416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1323b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 1324b7c46309SBarry Smith } 1325b7c46309SBarry Smith aj = Aloc->j; 13264af08d9eSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;} 1327b7c46309SBarry Smith 1328b7c46309SBarry Smith /* copy over the B part */ 1329ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->B->data; 1330b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1331b7c46309SBarry Smith row = a->rstart; 13320452661fSBarry Smith ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) );CHKPTRQ(cols); 1333dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];} 1334b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1335416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1336b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 1337b7c46309SBarry Smith } 1338606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 13396d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13406d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1341*7c922b88SBarry Smith if (matout) { 13420de55854SLois Curfman McInnes *matout = B; 13430de55854SLois Curfman McInnes } else { 1344f830108cSBarry Smith PetscOps *Abops; 1345f830108cSBarry Smith struct _MatOps *Aops; 1346f830108cSBarry Smith 13470de55854SLois Curfman McInnes /* This isn't really an in-place transpose .... but free data structures from a */ 1348606d414cSSatish Balay ierr = PetscFree(a->rowners);CHKERRQ(ierr); 13490de55854SLois Curfman McInnes ierr = MatDestroy(a->A);CHKERRQ(ierr); 13500de55854SLois Curfman McInnes ierr = MatDestroy(a->B);CHKERRQ(ierr); 1351aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 13520f5bd95cSBarry Smith if (a->colmap) {ierr = PetscTableDelete(a->colmap);CHKERRQ(ierr);} 1353b1fc9764SSatish Balay #else 1354606d414cSSatish Balay if (a->colmap) {ierr = PetscFree(a->colmap);CHKERRQ(ierr);} 1355b1fc9764SSatish Balay #endif 1356606d414cSSatish Balay if (a->garray) {ierr = PetscFree(a->garray);CHKERRQ(ierr);} 1357*7c922b88SBarry Smith if (a->lvec) {ierr = VecDestroy(a->lvec);CHKERRQ(ierr);} 1358*7c922b88SBarry Smith if (a->Mvctx) {ierr = VecScatterDestroy(a->Mvctx);CHKERRQ(ierr);} 1359606d414cSSatish Balay ierr = PetscFree(a);CHKERRQ(ierr); 1360f830108cSBarry Smith 1361f830108cSBarry Smith /* 1362f830108cSBarry Smith This is horrible, horrible code. We need to keep the 1363f830108cSBarry Smith A pointers for the bops and ops but copy everything 1364f830108cSBarry Smith else from C. 1365f830108cSBarry Smith */ 1366f830108cSBarry Smith Abops = A->bops; 1367f830108cSBarry Smith Aops = A->ops; 1368549d3d68SSatish Balay ierr = PetscMemcpy(A,B,sizeof(struct _p_Mat));CHKERRQ(ierr); 1369f830108cSBarry Smith A->bops = Abops; 1370f830108cSBarry Smith A->ops = Aops; 13710452661fSBarry Smith PetscHeaderDestroy(B); 13720de55854SLois Curfman McInnes } 13733a40ed3dSBarry Smith PetscFunctionReturn(0); 1374b7c46309SBarry Smith } 1375b7c46309SBarry Smith 13765615d1e5SSatish Balay #undef __FUNC__ 13775615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ" 13784b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1379a008b906SSatish Balay { 13804b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 13814b967eb1SSatish Balay Mat a = aij->A, b = aij->B; 1382a008b906SSatish Balay int ierr,s1,s2,s3; 1383a008b906SSatish Balay 13843a40ed3dSBarry Smith PetscFunctionBegin; 13854b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 13864b967eb1SSatish Balay if (rr) { 1387e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1388a8c6a408SBarry Smith if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size"); 13894b967eb1SSatish Balay /* Overlap communication with computation. */ 139043a90d84SBarry Smith ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr); 1391a008b906SSatish Balay } 13924b967eb1SSatish Balay if (ll) { 1393e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1394a8c6a408SBarry Smith if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size"); 1395f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 13964b967eb1SSatish Balay } 13974b967eb1SSatish Balay /* scale the diagonal block */ 1398f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 13994b967eb1SSatish Balay 14004b967eb1SSatish Balay if (rr) { 14014b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 140243a90d84SBarry Smith ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr); 1403f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 14044b967eb1SSatish Balay } 14054b967eb1SSatish Balay 14063a40ed3dSBarry Smith PetscFunctionReturn(0); 1407a008b906SSatish Balay } 1408a008b906SSatish Balay 1409a008b906SSatish Balay 14105615d1e5SSatish Balay #undef __FUNC__ 1411d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ" 14128f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A) 1413682d7d0cSBarry Smith { 1414682d7d0cSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 14153a40ed3dSBarry Smith int ierr; 1416682d7d0cSBarry Smith 14173a40ed3dSBarry Smith PetscFunctionBegin; 14183a40ed3dSBarry Smith if (!a->rank) { 14193a40ed3dSBarry Smith ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr); 14203a40ed3dSBarry Smith } 14213a40ed3dSBarry Smith PetscFunctionReturn(0); 1422682d7d0cSBarry Smith } 1423682d7d0cSBarry Smith 14245615d1e5SSatish Balay #undef __FUNC__ 1425d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ" 14268f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs) 14275a838052SSatish Balay { 14283a40ed3dSBarry Smith PetscFunctionBegin; 14295a838052SSatish Balay *bs = 1; 14303a40ed3dSBarry Smith PetscFunctionReturn(0); 14315a838052SSatish Balay } 14325615d1e5SSatish Balay #undef __FUNC__ 1433d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ" 14348f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A) 1435bb5a7306SBarry Smith { 1436bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 1437bb5a7306SBarry Smith int ierr; 14383a40ed3dSBarry Smith 14393a40ed3dSBarry Smith PetscFunctionBegin; 1440bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 14413a40ed3dSBarry Smith PetscFunctionReturn(0); 1442bb5a7306SBarry Smith } 1443bb5a7306SBarry Smith 1444d4bb536fSBarry Smith #undef __FUNC__ 1445d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ" 1446d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag) 1447d4bb536fSBarry Smith { 1448d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data; 1449d4bb536fSBarry Smith Mat a, b, c, d; 1450d4bb536fSBarry Smith PetscTruth flg; 1451d4bb536fSBarry Smith int ierr; 1452d4bb536fSBarry Smith 14533a40ed3dSBarry Smith PetscFunctionBegin; 1454a8c6a408SBarry Smith if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type"); 1455d4bb536fSBarry Smith a = matA->A; b = matA->B; 1456d4bb536fSBarry Smith c = matB->A; d = matB->B; 1457d4bb536fSBarry Smith 1458d4bb536fSBarry Smith ierr = MatEqual(a, c, &flg);CHKERRQ(ierr); 1459d4bb536fSBarry Smith if (flg == PETSC_TRUE) { 1460d4bb536fSBarry Smith ierr = MatEqual(b, d, &flg);CHKERRQ(ierr); 1461d4bb536fSBarry Smith } 1462ca161407SBarry Smith ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr); 14633a40ed3dSBarry Smith PetscFunctionReturn(0); 1464d4bb536fSBarry Smith } 1465d4bb536fSBarry Smith 1466cb5b572fSBarry Smith #undef __FUNC__ 1467cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ" 1468cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1469cb5b572fSBarry Smith { 1470cb5b572fSBarry Smith int ierr; 1471cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1472cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1473cb5b572fSBarry Smith 1474cb5b572fSBarry Smith PetscFunctionBegin; 1475cb5b572fSBarry Smith if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) { 1476cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1477cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1478cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1479cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1480cb5b572fSBarry Smith then copying the submatrices */ 1481cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1482cb5b572fSBarry Smith } else { 1483cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1484cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1485cb5b572fSBarry Smith } 1486cb5b572fSBarry Smith PetscFunctionReturn(0); 1487cb5b572fSBarry Smith } 1488cb5b572fSBarry Smith 14892e8a6d31SBarry Smith extern int MatDuplicate_MPIAIJ(Mat,MatDuplicateOption,Mat *); 14902f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int); 14910a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring); 14927b2a1423SBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatReuse,Mat **); 14937b2a1423SBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatReuse,Mat *); 149400e6dbe6SBarry Smith 14958a729477SBarry Smith /* -------------------------------------------------------------------*/ 1496cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 1497cda55fadSBarry Smith MatGetRow_MPIAIJ, 1498cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 1499cda55fadSBarry Smith MatMult_MPIAIJ, 1500cda55fadSBarry Smith MatMultAdd_MPIAIJ, 1501*7c922b88SBarry Smith MatMultTranspose_MPIAIJ, 1502*7c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 1503cda55fadSBarry Smith 0, 1504cda55fadSBarry Smith 0, 1505cda55fadSBarry Smith 0, 1506cda55fadSBarry Smith 0, 1507cda55fadSBarry Smith 0, 1508cda55fadSBarry Smith 0, 150944a69424SLois Curfman McInnes MatRelax_MPIAIJ, 1510b7c46309SBarry Smith MatTranspose_MPIAIJ, 1511cda55fadSBarry Smith MatGetInfo_MPIAIJ, 1512cda55fadSBarry Smith MatEqual_MPIAIJ, 1513cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 1514cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 1515cda55fadSBarry Smith MatNorm_MPIAIJ, 1516cda55fadSBarry Smith MatAssemblyBegin_MPIAIJ, 1517cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 15181eb62cbbSBarry Smith 0, 1519cda55fadSBarry Smith MatSetOption_MPIAIJ, 1520cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 1521cda55fadSBarry Smith MatZeroRows_MPIAIJ, 1522cda55fadSBarry Smith 0, 1523cda55fadSBarry Smith 0, 1524cda55fadSBarry Smith 0, 1525cda55fadSBarry Smith 0, 1526cda55fadSBarry Smith MatGetSize_MPIAIJ, 1527cda55fadSBarry Smith MatGetLocalSize_MPIAIJ, 1528cda55fadSBarry Smith MatGetOwnershipRange_MPIAIJ, 1529cda55fadSBarry Smith 0, 1530cda55fadSBarry Smith 0, 1531cda55fadSBarry Smith 0, 1532cda55fadSBarry Smith 0, 15332e8a6d31SBarry Smith MatDuplicate_MPIAIJ, 1534cda55fadSBarry Smith 0, 1535cda55fadSBarry Smith 0, 1536cda55fadSBarry Smith 0, 1537cda55fadSBarry Smith 0, 1538cda55fadSBarry Smith 0, 1539cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 1540cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 1541cda55fadSBarry Smith MatGetValues_MPIAIJ, 1542cb5b572fSBarry Smith MatCopy_MPIAIJ, 1543052efed2SBarry Smith MatPrintHelp_MPIAIJ, 1544cda55fadSBarry Smith MatScale_MPIAIJ, 1545cda55fadSBarry Smith 0, 1546cda55fadSBarry Smith 0, 1547cda55fadSBarry Smith 0, 1548cda55fadSBarry Smith MatGetBlockSize_MPIAIJ, 1549cda55fadSBarry Smith 0, 1550cda55fadSBarry Smith 0, 1551cda55fadSBarry Smith 0, 1552cda55fadSBarry Smith 0, 1553cda55fadSBarry Smith MatFDColoringCreate_MPIAIJ, 1554cda55fadSBarry Smith 0, 1555cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 1556cda55fadSBarry Smith 0, 1557cda55fadSBarry Smith 0, 1558cda55fadSBarry Smith MatGetSubMatrix_MPIAIJ, 1559cda55fadSBarry Smith 0, 1560cda55fadSBarry Smith 0, 1561cda55fadSBarry Smith MatGetMaps_Petsc}; 156236ce4990SBarry Smith 15632e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 15642e8a6d31SBarry Smith 1565fb2e594dSBarry Smith EXTERN_C_BEGIN 15662e8a6d31SBarry Smith #undef __FUNC__ 15672e8a6d31SBarry Smith #define __FUNC__ "MatStoreValues_MPIAIJ" 15682e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat) 15692e8a6d31SBarry Smith { 15702e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 15712e8a6d31SBarry Smith int ierr; 15722e8a6d31SBarry Smith 15732e8a6d31SBarry Smith PetscFunctionBegin; 15742e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 15752e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 15762e8a6d31SBarry Smith PetscFunctionReturn(0); 15772e8a6d31SBarry Smith } 1578fb2e594dSBarry Smith EXTERN_C_END 15792e8a6d31SBarry Smith 1580fb2e594dSBarry Smith EXTERN_C_BEGIN 15812e8a6d31SBarry Smith #undef __FUNC__ 15822e8a6d31SBarry Smith #define __FUNC__ "MatRetrieveValues_MPIAIJ" 15832e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat) 15842e8a6d31SBarry Smith { 15852e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 15862e8a6d31SBarry Smith int ierr; 15872e8a6d31SBarry Smith 15882e8a6d31SBarry Smith PetscFunctionBegin; 15892e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 15902e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 15912e8a6d31SBarry Smith PetscFunctionReturn(0); 15922e8a6d31SBarry Smith } 1593fb2e594dSBarry Smith EXTERN_C_END 15948a729477SBarry Smith 159527508adbSBarry Smith #include "pc.h" 159627508adbSBarry Smith EXTERN_C_BEGIN 15975ef9f2a5SBarry Smith extern int MatGetDiagonalBlock_MPIAIJ(Mat,PetscTruth *,MatReuse,Mat *); 159827508adbSBarry Smith EXTERN_C_END 159927508adbSBarry Smith 16005615d1e5SSatish Balay #undef __FUNC__ 16015615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ" 16021987afe7SBarry Smith /*@C 1603ff756334SLois Curfman McInnes MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 16043a511b96SLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 16053a511b96SLois Curfman McInnes the user should preallocate the matrix storage by setting the parameters 16063a511b96SLois Curfman McInnes d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 16073a511b96SLois Curfman McInnes performance can be increased by more than a factor of 50. 16088a729477SBarry Smith 1609db81eaa0SLois Curfman McInnes Collective on MPI_Comm 1610db81eaa0SLois Curfman McInnes 16118a729477SBarry Smith Input Parameters: 1612db81eaa0SLois Curfman McInnes + comm - MPI communicator 16137d3e4905SLois Curfman McInnes . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 161492e8d321SLois Curfman McInnes This value should be the same as the local size used in creating the 161592e8d321SLois Curfman McInnes y vector for the matrix-vector product y = Ax. 16161a3896d6SBarry Smith . n - This value should be the same as the local size used in creating the 16171a3896d6SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 16181a3896d6SBarry Smith calculated if N is given) For square matrices n is almost always m. 161960d380a7SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 162060d380a7SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 1621af1d9917SSatish Balay . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 1622af1d9917SSatish Balay (same value is used for all local rows) 1623af1d9917SSatish Balay . d_nnz - array containing the number of nonzeros in the various rows of the 1624af1d9917SSatish Balay DIAGONAL portion of the local submatrix (possibly different for each row) 1625af1d9917SSatish Balay or PETSC_NULL, if d_nz is used to specify the nonzero structure. 1626af1d9917SSatish Balay The size of this array is equal to the number of local rows, i.e 'm'. 1627af1d9917SSatish Balay You must leave room for the diagonal entry even if it is zero. 1628af1d9917SSatish Balay . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 1629af1d9917SSatish Balay submatrix (same value is used for all local rows). 1630af1d9917SSatish Balay - o_nnz - array containing the number of nonzeros in the various rows of the 1631af1d9917SSatish Balay OFF-DIAGONAL portion of the local submatrix (possibly different for 1632af1d9917SSatish Balay each row) or PETSC_NULL, if o_nz is used to specify the nonzero 1633af1d9917SSatish Balay structure. The size of this array is equal to the number 1634af1d9917SSatish Balay of local rows, i.e 'm'. 16358a729477SBarry Smith 1636ff756334SLois Curfman McInnes Output Parameter: 163744cd7ae7SLois Curfman McInnes . A - the matrix 16388a729477SBarry Smith 1639b259b22eSLois Curfman McInnes Notes: 1640af1d9917SSatish Balay m,n,M,N parameters specify the size of the matrix, and its partitioning across 1641af1d9917SSatish Balay processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 1642af1d9917SSatish Balay storage requirements for this matrix. 1643af1d9917SSatish Balay 1644af1d9917SSatish Balay If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 1645af1d9917SSatish Balay processor than it must be used on all processors that share the object for 1646af1d9917SSatish Balay that argument. 1647be79a94dSBarry Smith 1648ff756334SLois Curfman McInnes The AIJ format (also called the Yale sparse matrix format or 1649ff756334SLois Curfman McInnes compressed row storage), is fully compatible with standard Fortran 77 16500002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 16510002213bSLois Curfman McInnes either one (as in Fortran) or zero. See the users manual for details. 1652ff756334SLois Curfman McInnes 1653ff756334SLois Curfman McInnes The user MUST specify either the local or global matrix dimensions 1654ff756334SLois Curfman McInnes (possibly both). 1655ff756334SLois Curfman McInnes 1656af1d9917SSatish Balay The parallel matrix is partitioned such that the first m0 rows belong to 1657af1d9917SSatish Balay process 0, the next m1 rows belong to process 1, the next m2 rows belong 1658af1d9917SSatish Balay to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 1659af1d9917SSatish Balay 1660af1d9917SSatish Balay The DIAGONAL portion of the local submatrix of a processor can be defined 1661af1d9917SSatish Balay as the submatrix which is obtained by extraction the part corresponding 1662af1d9917SSatish Balay to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 1663af1d9917SSatish Balay first row that belongs to the processor, and r2 is the last row belonging 1664af1d9917SSatish Balay to the this processor. This is a square mxm matrix. The remaining portion 1665af1d9917SSatish Balay of the local submatrix (mxN) constitute the OFF-DIAGONAL portion. 1666af1d9917SSatish Balay 1667af1d9917SSatish Balay If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 1668af1d9917SSatish Balay 16695511cfe3SLois Curfman McInnes By default, this format uses inodes (identical nodes) when possible. 16705511cfe3SLois Curfman McInnes We search for consecutive rows with the same nonzero structure, thereby 16715511cfe3SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 16725511cfe3SLois Curfman McInnes 16735511cfe3SLois Curfman McInnes Options Database Keys: 1674db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 1675db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 1676db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 1677db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 1678db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 1679494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1680494eafd4SBarry Smith (defaults to using SeqBAIJ format on one processor) 1681494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1682494eafd4SBarry Smith (defaults to using SeqAIJ format on one processor) 1683494eafd4SBarry Smith 16845511cfe3SLois Curfman McInnes 1685af1d9917SSatish Balay Example usage: 1686e0245417SLois Curfman McInnes 1687af1d9917SSatish Balay Consider the following 8x8 matrix with 34 non-zero values, that is 1688af1d9917SSatish Balay assembled across 3 processors. Lets assume that proc0 owns 3 rows, 1689af1d9917SSatish Balay proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 1690af1d9917SSatish Balay as follows: 16912191d07cSBarry Smith 1692db81eaa0SLois Curfman McInnes .vb 1693af1d9917SSatish Balay 1 2 0 | 0 3 0 | 0 4 1694af1d9917SSatish Balay Proc0 0 5 6 | 7 0 0 | 8 0 1695af1d9917SSatish Balay 9 0 10 | 11 0 0 | 12 0 1696af1d9917SSatish Balay ------------------------------------- 1697af1d9917SSatish Balay 13 0 14 | 15 16 17 | 0 0 1698af1d9917SSatish Balay Proc1 0 18 0 | 19 20 21 | 0 0 1699af1d9917SSatish Balay 0 0 0 | 22 23 0 | 24 0 1700af1d9917SSatish Balay ------------------------------------- 1701af1d9917SSatish Balay Proc2 25 26 27 | 0 0 28 | 29 0 1702af1d9917SSatish Balay 30 0 0 | 31 32 33 | 0 34 1703db81eaa0SLois Curfman McInnes .ve 1704b810aeb4SBarry Smith 1705af1d9917SSatish Balay This can be represented as a collection of submatrices as: 17065511cfe3SLois Curfman McInnes 1707af1d9917SSatish Balay .vb 1708af1d9917SSatish Balay A B C 1709af1d9917SSatish Balay D E F 1710af1d9917SSatish Balay G H I 1711af1d9917SSatish Balay .ve 1712af1d9917SSatish Balay 1713af1d9917SSatish Balay Where the submatrices A,B,C are owned by proc0, D,E,F are 1714af1d9917SSatish Balay owned by proc1, G,H,I are owned by proc2. 1715af1d9917SSatish Balay 1716af1d9917SSatish Balay The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 1717af1d9917SSatish Balay The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 1718af1d9917SSatish Balay The 'M','N' parameters are 8,8, and have the same values on all procs. 1719af1d9917SSatish Balay 1720af1d9917SSatish Balay The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 1721af1d9917SSatish Balay submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 1722af1d9917SSatish Balay corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 1723af1d9917SSatish Balay Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 1724af1d9917SSatish Balay part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 1725af1d9917SSatish Balay matrix, ans [DF] as another SeqAIJ matrix. 1726af1d9917SSatish Balay 1727af1d9917SSatish Balay When d_nz, o_nz parameters are specified, d_nz storage elements are 1728af1d9917SSatish Balay allocated for every row of the local diagonal submatrix, and o_nz 1729af1d9917SSatish Balay storage locations are allocated for every row of the OFF-DIAGONAL submat. 1730af1d9917SSatish Balay One way to choose d_nz and o_nz is to use the max nonzerors per local 1731af1d9917SSatish Balay rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 1732af1d9917SSatish Balay In this case, the values of d_nz,o_nz are: 1733af1d9917SSatish Balay .vb 1734af1d9917SSatish Balay proc0 : dnz = 2, o_nz = 2 1735af1d9917SSatish Balay proc1 : dnz = 3, o_nz = 2 1736af1d9917SSatish Balay proc2 : dnz = 1, o_nz = 4 1737af1d9917SSatish Balay .ve 1738af1d9917SSatish Balay We are allocating m*(d_nz+o_nz) storage locations for every proc. This 1739af1d9917SSatish Balay translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 1740af1d9917SSatish Balay for proc3. i.e we are using 12+15+10=37 storage locations to store 1741af1d9917SSatish Balay 34 values. 1742af1d9917SSatish Balay 1743af1d9917SSatish Balay When d_nnz, o_nnz parameters are specified, the storage is specified 1744af1d9917SSatish Balay for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 1745af1d9917SSatish Balay In the above case the values for d_nnz,o_nnz are: 1746af1d9917SSatish Balay .vb 1747af1d9917SSatish Balay proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 1748af1d9917SSatish Balay proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 1749af1d9917SSatish Balay proc2: d_nnz = [1,1] and o_nnz = [4,4] 1750af1d9917SSatish Balay .ve 1751af1d9917SSatish Balay Here the space allocated is sum of all the above values i.e 34, and 1752af1d9917SSatish Balay hence pre-allocation is perfect. 17533a511b96SLois Curfman McInnes 1754027ccd11SLois Curfman McInnes Level: intermediate 1755027ccd11SLois Curfman McInnes 1756dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel 1757ff756334SLois Curfman McInnes 1758fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues() 17598a729477SBarry Smith @*/ 1760e1311b90SBarry 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) 17618a729477SBarry Smith { 176244cd7ae7SLois Curfman McInnes Mat B; 176344cd7ae7SLois Curfman McInnes Mat_MPIAIJ *b; 1764f1af5d2fSBarry Smith int ierr, i,size; 1765f1af5d2fSBarry Smith PetscTruth flag1, flag2; 1766416022c9SBarry Smith 17673a40ed3dSBarry Smith PetscFunctionBegin; 17681d55c564SBarry Smith 1769b73539f3SBarry Smith if (d_nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"d_nz cannot be less than -2: value %d",d_nz); 1770b73539f3SBarry Smith if (o_nz < -2) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"o_nz cannot be less than -2: value %d",o_nz); 17711d55c564SBarry Smith if (d_nnz) { 17721d55c564SBarry Smith for (i=0; i<m; i++) { 1773b73539f3SBarry Smith if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"d_nnz cannot be less than 0: local row %d value %d",i,d_nnz[i]); 17741d55c564SBarry Smith } 17751d55c564SBarry Smith } 17761d55c564SBarry Smith if (o_nnz) { 17771d55c564SBarry Smith for (i=0; i<m; i++) { 1778b73539f3SBarry Smith if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,0,"o_nnz cannot be less than 0: local row %d value %d",i,o_nnz[i]); 17791d55c564SBarry Smith } 17801d55c564SBarry Smith } 17811d55c564SBarry Smith 17821dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 17837be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1);CHKERRQ(ierr); 17847be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2);CHKERRQ(ierr); 17857be0e774SBarry Smith if (!flag1 && !flag2 && size == 1) { 17863914022bSBarry Smith if (M == PETSC_DECIDE) M = m; 17873914022bSBarry Smith if (N == PETSC_DECIDE) N = n; 17883914022bSBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A);CHKERRQ(ierr); 17893a40ed3dSBarry Smith PetscFunctionReturn(0); 17903914022bSBarry Smith } 17913914022bSBarry Smith 179244cd7ae7SLois Curfman McInnes *A = 0; 17933f1db9ecSBarry Smith PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",comm,MatDestroy,MatView); 179444cd7ae7SLois Curfman McInnes PLogObjectCreate(B); 179544cd7ae7SLois Curfman McInnes B->data = (void *) (b = PetscNew(Mat_MPIAIJ));CHKPTRQ(b); 1796549d3d68SSatish Balay ierr = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr); 1797549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 1798e1311b90SBarry Smith B->ops->destroy = MatDestroy_MPIAIJ; 1799e1311b90SBarry Smith B->ops->view = MatView_MPIAIJ; 180044cd7ae7SLois Curfman McInnes B->factor = 0; 180144cd7ae7SLois Curfman McInnes B->assembled = PETSC_FALSE; 180290f02eecSBarry Smith B->mapping = 0; 1803d6dfbf8fSBarry Smith 180447794344SBarry Smith B->insertmode = NOT_SET_VALUES; 18059eb4d147SSatish Balay b->size = size; 18061dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&b->rank);CHKERRQ(ierr); 18071eb62cbbSBarry Smith 1808*7c922b88SBarry Smith if (m == PETSC_DECIDE && (d_nnz || o_nnz)) { 1809a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz"); 18103a40ed3dSBarry Smith } 18111987afe7SBarry Smith 1812eac7125bSBarry Smith ierr = PetscSplitOwnership(comm,&m,&M);CHKERRQ(ierr); 1813eac7125bSBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 181444cd7ae7SLois Curfman McInnes b->m = m; B->m = m; 181544cd7ae7SLois Curfman McInnes b->n = n; B->n = n; 181644cd7ae7SLois Curfman McInnes b->N = N; B->N = N; 181744cd7ae7SLois Curfman McInnes b->M = M; B->M = M; 18181eb62cbbSBarry Smith 1819c7fcc2eaSBarry Smith /* the information in the maps duplicates the information computed below, eventually 1820c7fcc2eaSBarry Smith we should remove the duplicate information that is not contained in the maps */ 1821253a16ddSBarry Smith ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr); 1822253a16ddSBarry Smith ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr); 1823c7fcc2eaSBarry Smith 18241eb62cbbSBarry Smith /* build local table of row and column ownerships */ 182544cd7ae7SLois Curfman McInnes b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int));CHKPTRQ(b->rowners); 1826f09e8eb9SSatish Balay PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1827603f58a4SSatish Balay b->cowners = b->rowners + b->size + 2; 1828ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 182944cd7ae7SLois Curfman McInnes b->rowners[0] = 0; 183044cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 183144cd7ae7SLois Curfman McInnes b->rowners[i] += b->rowners[i-1]; 18328a729477SBarry Smith } 183344cd7ae7SLois Curfman McInnes b->rstart = b->rowners[b->rank]; 183444cd7ae7SLois Curfman McInnes b->rend = b->rowners[b->rank+1]; 1835ca161407SBarry Smith ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 183644cd7ae7SLois Curfman McInnes b->cowners[0] = 0; 183744cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 183844cd7ae7SLois Curfman McInnes b->cowners[i] += b->cowners[i-1]; 18398a729477SBarry Smith } 184044cd7ae7SLois Curfman McInnes b->cstart = b->cowners[b->rank]; 184144cd7ae7SLois Curfman McInnes b->cend = b->cowners[b->rank+1]; 18428a729477SBarry Smith 18435ace5be8SLois Curfman McInnes if (d_nz == PETSC_DEFAULT) d_nz = 5; 1844029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A);CHKERRQ(ierr); 184544cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->A); 18467b8455f0SLois Curfman McInnes if (o_nz == PETSC_DEFAULT) o_nz = 0; 1847029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B);CHKERRQ(ierr); 184844cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->B); 18498a729477SBarry Smith 18501eb62cbbSBarry Smith /* build cache for off array entries formed */ 18518798bf22SSatish Balay ierr = MatStashCreate_Private(comm,1,&B->stash);CHKERRQ(ierr); 1852*7c922b88SBarry Smith b->donotstash = PETSC_FALSE; 185344cd7ae7SLois Curfman McInnes b->colmap = 0; 185444cd7ae7SLois Curfman McInnes b->garray = 0; 1855*7c922b88SBarry Smith b->roworiented = PETSC_TRUE; 18568a729477SBarry Smith 18571eb62cbbSBarry Smith /* stuff used for matrix vector multiply */ 1858*7c922b88SBarry Smith b->lvec = PETSC_NULL; 1859*7c922b88SBarry Smith b->Mvctx = PETSC_NULL; 18608a729477SBarry Smith 18617a0afa10SBarry Smith /* stuff for MatGetRow() */ 186244cd7ae7SLois Curfman McInnes b->rowindices = 0; 186344cd7ae7SLois Curfman McInnes b->rowvalues = 0; 186444cd7ae7SLois Curfman McInnes b->getrowactive = PETSC_FALSE; 18657a0afa10SBarry Smith 1866f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 18672e8a6d31SBarry Smith "MatStoreValues_MPIAIJ", 18682e8a6d31SBarry Smith (void*)MatStoreValues_MPIAIJ);CHKERRQ(ierr); 1869f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 18702e8a6d31SBarry Smith "MatRetrieveValues_MPIAIJ", 18712e8a6d31SBarry Smith (void*)MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 1872f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 18735ef9f2a5SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 18745ef9f2a5SBarry Smith (void*)MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 187544cd7ae7SLois Curfman McInnes *A = B; 18763a40ed3dSBarry Smith PetscFunctionReturn(0); 1877d6dfbf8fSBarry Smith } 1878c74985f6SBarry Smith 18795615d1e5SSatish Balay #undef __FUNC__ 18802e8a6d31SBarry Smith #define __FUNC__ "MatDuplicate_MPIAIJ" 18812e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 1882d6dfbf8fSBarry Smith { 1883d6dfbf8fSBarry Smith Mat mat; 1884416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data; 1885f1af5d2fSBarry Smith int ierr, len = 0; 1886f1af5d2fSBarry Smith PetscTruth flg; 1887d6dfbf8fSBarry Smith 18883a40ed3dSBarry Smith PetscFunctionBegin; 1889416022c9SBarry Smith *newmat = 0; 18903f1db9ecSBarry Smith PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,"Mat",matin->comm,MatDestroy,MatView); 1891a5a9c739SBarry Smith PLogObjectCreate(mat); 18920452661fSBarry Smith mat->data = (void *) (a = PetscNew(Mat_MPIAIJ));CHKPTRQ(a); 1893549d3d68SSatish Balay ierr = PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 1894e1311b90SBarry Smith mat->ops->destroy = MatDestroy_MPIAIJ; 1895e1311b90SBarry Smith mat->ops->view = MatView_MPIAIJ; 1896d6dfbf8fSBarry Smith mat->factor = matin->factor; 1897c456f294SBarry Smith mat->assembled = PETSC_TRUE; 1898e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 1899d6dfbf8fSBarry Smith 190044cd7ae7SLois Curfman McInnes a->m = mat->m = oldmat->m; 190144cd7ae7SLois Curfman McInnes a->n = mat->n = oldmat->n; 190244cd7ae7SLois Curfman McInnes a->M = mat->M = oldmat->M; 190344cd7ae7SLois Curfman McInnes a->N = mat->N = oldmat->N; 1904d6dfbf8fSBarry Smith 1905416022c9SBarry Smith a->rstart = oldmat->rstart; 1906416022c9SBarry Smith a->rend = oldmat->rend; 1907416022c9SBarry Smith a->cstart = oldmat->cstart; 1908416022c9SBarry Smith a->cend = oldmat->cend; 190917699dbbSLois Curfman McInnes a->size = oldmat->size; 191017699dbbSLois Curfman McInnes a->rank = oldmat->rank; 1911e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 1912e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 1913e7641de0SSatish Balay a->rowindices = 0; 1914bcd2baecSBarry Smith a->rowvalues = 0; 1915bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 1916d6dfbf8fSBarry Smith 1917603f58a4SSatish Balay a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int));CHKPTRQ(a->rowners); 1918f09e8eb9SSatish Balay PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1919603f58a4SSatish Balay a->cowners = a->rowners + a->size + 2; 1920549d3d68SSatish Balay ierr = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr); 19218798bf22SSatish Balay ierr = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr); 19222ee70a88SLois Curfman McInnes if (oldmat->colmap) { 1923aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 19240f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 1925b1fc9764SSatish Balay #else 19260452661fSBarry Smith a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap); 1927416022c9SBarry Smith PLogObjectMemory(mat,(a->N)*sizeof(int)); 1928549d3d68SSatish Balay ierr = PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int));CHKERRQ(ierr); 1929b1fc9764SSatish Balay #endif 1930416022c9SBarry Smith } else a->colmap = 0; 19313f41c07dSBarry Smith if (oldmat->garray) { 19323f41c07dSBarry Smith len = ((Mat_SeqAIJ *) (oldmat->B->data))->n; 19333f41c07dSBarry Smith a->garray = (int *) PetscMalloc((len+1)*sizeof(int));CHKPTRQ(a->garray); 1934464493b3SBarry Smith PLogObjectMemory(mat,len*sizeof(int)); 1935549d3d68SSatish Balay if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); } 1936416022c9SBarry Smith } else a->garray = 0; 1937d6dfbf8fSBarry Smith 1938416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 1939416022c9SBarry Smith PLogObjectParent(mat,a->lvec); 1940a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 1941416022c9SBarry Smith PLogObjectParent(mat,a->Mvctx); 19422e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 1943416022c9SBarry Smith PLogObjectParent(mat,a->A); 19442e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 1945416022c9SBarry Smith PLogObjectParent(mat,a->B); 19465dd7a6c7SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 194725cdf11fSBarry Smith if (flg) { 1948682d7d0cSBarry Smith ierr = MatPrintHelp(mat);CHKERRQ(ierr); 1949682d7d0cSBarry Smith } 195027508adbSBarry Smith ierr = FListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr); 19518a729477SBarry Smith *newmat = mat; 19523a40ed3dSBarry Smith PetscFunctionReturn(0); 19538a729477SBarry Smith } 1954416022c9SBarry Smith 195577c4ece6SBarry Smith #include "sys.h" 1956416022c9SBarry Smith 19575615d1e5SSatish Balay #undef __FUNC__ 19585615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ" 195919bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat) 1960416022c9SBarry Smith { 1961d65a2f8fSBarry Smith Mat A; 1962d65a2f8fSBarry Smith Scalar *vals,*svals; 196319bcc07fSBarry Smith MPI_Comm comm = ((PetscObject)viewer)->comm; 1964416022c9SBarry Smith MPI_Status status; 19653a40ed3dSBarry Smith int i, nz, ierr, j,rstart, rend, fd; 196617699dbbSLois Curfman McInnes int header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols; 1967d65a2f8fSBarry Smith int *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols; 1968b362ba68SBarry Smith int tag = ((PetscObject)viewer)->tag,cend,cstart,n; 1969416022c9SBarry Smith 19703a40ed3dSBarry Smith PetscFunctionBegin; 19711dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 19721dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 197317699dbbSLois Curfman McInnes if (!rank) { 197490ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 19750752156aSBarry Smith ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 1976a8c6a408SBarry Smith if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object"); 1977d64ed03dSBarry Smith if (header[3] < 0) { 1978a8c6a408SBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ"); 1979d64ed03dSBarry Smith } 19806c5fab8fSBarry Smith } 19816c5fab8fSBarry Smith 1982ca161407SBarry Smith ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr); 1983416022c9SBarry Smith M = header[1]; N = header[2]; 1984416022c9SBarry Smith /* determine ownership of all rows */ 198517699dbbSLois Curfman McInnes m = M/size + ((M % size) > rank); 19860452661fSBarry Smith rowners = (int *) PetscMalloc((size+2)*sizeof(int));CHKPTRQ(rowners); 1987ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 1988416022c9SBarry Smith rowners[0] = 0; 198917699dbbSLois Curfman McInnes for ( i=2; i<=size; i++ ) { 1990416022c9SBarry Smith rowners[i] += rowners[i-1]; 1991416022c9SBarry Smith } 199217699dbbSLois Curfman McInnes rstart = rowners[rank]; 199317699dbbSLois Curfman McInnes rend = rowners[rank+1]; 1994416022c9SBarry Smith 1995416022c9SBarry Smith /* distribute row lengths to all processors */ 19960452661fSBarry Smith ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) );CHKPTRQ(ourlens); 1997416022c9SBarry Smith offlens = ourlens + (rend-rstart); 199817699dbbSLois Curfman McInnes if (!rank) { 19990452661fSBarry Smith rowlengths = (int*) PetscMalloc( M*sizeof(int) );CHKPTRQ(rowlengths); 20000752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 20010452661fSBarry Smith sndcounts = (int*) PetscMalloc( size*sizeof(int) );CHKPTRQ(sndcounts); 200217699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i]; 2003ca161407SBarry Smith ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr); 2004606d414cSSatish Balay ierr = PetscFree(sndcounts);CHKERRQ(ierr); 20053a40ed3dSBarry Smith } else { 2006ca161407SBarry Smith ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr); 2007416022c9SBarry Smith } 2008416022c9SBarry Smith 200917699dbbSLois Curfman McInnes if (!rank) { 2010416022c9SBarry Smith /* calculate the number of nonzeros on each processor */ 20110452661fSBarry Smith procsnz = (int*) PetscMalloc( size*sizeof(int) );CHKPTRQ(procsnz); 2012549d3d68SSatish Balay ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr); 201317699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 2014416022c9SBarry Smith for ( j=rowners[i]; j< rowners[i+1]; j++ ) { 2015416022c9SBarry Smith procsnz[i] += rowlengths[j]; 2016416022c9SBarry Smith } 2017416022c9SBarry Smith } 2018606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 2019416022c9SBarry Smith 2020416022c9SBarry Smith /* determine max buffer needed and allocate it */ 2021416022c9SBarry Smith maxnz = 0; 202217699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 20230452661fSBarry Smith maxnz = PetscMax(maxnz,procsnz[i]); 2024416022c9SBarry Smith } 20250452661fSBarry Smith cols = (int *) PetscMalloc( maxnz*sizeof(int) );CHKPTRQ(cols); 2026416022c9SBarry Smith 2027416022c9SBarry Smith /* read in my part of the matrix column indices */ 2028416022c9SBarry Smith nz = procsnz[0]; 20290452661fSBarry Smith mycols = (int *) PetscMalloc( nz*sizeof(int) );CHKPTRQ(mycols); 20300752156aSBarry Smith ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 2031d65a2f8fSBarry Smith 2032d65a2f8fSBarry Smith /* read in every one elses and ship off */ 203317699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 2034d65a2f8fSBarry Smith nz = procsnz[i]; 20350752156aSBarry Smith ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 2036ca161407SBarry Smith ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr); 2037d65a2f8fSBarry Smith } 2038606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 20393a40ed3dSBarry Smith } else { 2040416022c9SBarry Smith /* determine buffer space needed for message */ 2041416022c9SBarry Smith nz = 0; 2042416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2043416022c9SBarry Smith nz += ourlens[i]; 2044416022c9SBarry Smith } 20450452661fSBarry Smith mycols = (int*) PetscMalloc( nz*sizeof(int) );CHKPTRQ(mycols); 2046416022c9SBarry Smith 2047416022c9SBarry Smith /* receive message of column indices*/ 2048ca161407SBarry Smith ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 2049ca161407SBarry Smith ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr); 2050a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 2051416022c9SBarry Smith } 2052416022c9SBarry Smith 2053b362ba68SBarry Smith /* determine column ownership if matrix is not square */ 2054b362ba68SBarry Smith if (N != M) { 2055b362ba68SBarry Smith n = N/size + ((N % size) > rank); 2056b362ba68SBarry Smith ierr = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 2057b362ba68SBarry Smith cstart = cend - n; 2058b362ba68SBarry Smith } else { 2059b362ba68SBarry Smith cstart = rstart; 2060b362ba68SBarry Smith cend = rend; 2061fb2e594dSBarry Smith n = cend - cstart; 2062b362ba68SBarry Smith } 2063b362ba68SBarry Smith 2064416022c9SBarry Smith /* loop over local rows, determining number of off diagonal entries */ 2065549d3d68SSatish Balay ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr); 2066416022c9SBarry Smith jj = 0; 2067416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2068416022c9SBarry Smith for ( j=0; j<ourlens[i]; j++ ) { 2069b362ba68SBarry Smith if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 2070416022c9SBarry Smith jj++; 2071416022c9SBarry Smith } 2072416022c9SBarry Smith } 2073d65a2f8fSBarry Smith 2074d65a2f8fSBarry Smith /* create our matrix */ 2075416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2076416022c9SBarry Smith ourlens[i] -= offlens[i]; 2077416022c9SBarry Smith } 2078b362ba68SBarry Smith ierr = MatCreateMPIAIJ(comm,m,n,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr); 2079d65a2f8fSBarry Smith A = *newmat; 2080fb2e594dSBarry Smith ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr); 2081d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2082d65a2f8fSBarry Smith ourlens[i] += offlens[i]; 2083d65a2f8fSBarry Smith } 2084416022c9SBarry Smith 208517699dbbSLois Curfman McInnes if (!rank) { 20860452661fSBarry Smith vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) );CHKPTRQ(vals); 2087416022c9SBarry Smith 2088416022c9SBarry Smith /* read in my part of the matrix numerical values */ 2089416022c9SBarry Smith nz = procsnz[0]; 20900752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 2091d65a2f8fSBarry Smith 2092d65a2f8fSBarry Smith /* insert into matrix */ 2093d65a2f8fSBarry Smith jj = rstart; 2094d65a2f8fSBarry Smith smycols = mycols; 2095d65a2f8fSBarry Smith svals = vals; 2096d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2097d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2098d65a2f8fSBarry Smith smycols += ourlens[i]; 2099d65a2f8fSBarry Smith svals += ourlens[i]; 2100d65a2f8fSBarry Smith jj++; 2101416022c9SBarry Smith } 2102416022c9SBarry Smith 2103d65a2f8fSBarry Smith /* read in other processors and ship out */ 210417699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 2105416022c9SBarry Smith nz = procsnz[i]; 21060752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 2107ca161407SBarry Smith ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr); 2108416022c9SBarry Smith } 2109606d414cSSatish Balay ierr = PetscFree(procsnz);CHKERRQ(ierr); 21103a40ed3dSBarry Smith } else { 2111d65a2f8fSBarry Smith /* receive numeric values */ 21120452661fSBarry Smith vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) );CHKPTRQ(vals); 2113416022c9SBarry Smith 2114d65a2f8fSBarry Smith /* receive message of values*/ 2115ca161407SBarry Smith ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr); 2116ca161407SBarry Smith ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr); 2117a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 2118d65a2f8fSBarry Smith 2119d65a2f8fSBarry Smith /* insert into matrix */ 2120d65a2f8fSBarry Smith jj = rstart; 2121d65a2f8fSBarry Smith smycols = mycols; 2122d65a2f8fSBarry Smith svals = vals; 2123d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2124d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2125d65a2f8fSBarry Smith smycols += ourlens[i]; 2126d65a2f8fSBarry Smith svals += ourlens[i]; 2127d65a2f8fSBarry Smith jj++; 2128d65a2f8fSBarry Smith } 2129d65a2f8fSBarry Smith } 2130606d414cSSatish Balay ierr = PetscFree(ourlens);CHKERRQ(ierr); 2131606d414cSSatish Balay ierr = PetscFree(vals);CHKERRQ(ierr); 2132606d414cSSatish Balay ierr = PetscFree(mycols);CHKERRQ(ierr); 2133606d414cSSatish Balay ierr = PetscFree(rowners);CHKERRQ(ierr); 2134d65a2f8fSBarry Smith 21356d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21366d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21373a40ed3dSBarry Smith PetscFunctionReturn(0); 2138416022c9SBarry Smith } 2139a0ff6018SBarry Smith 214029da9460SBarry Smith #undef __FUNC__ 214129da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ" 2142a0ff6018SBarry Smith /* 214329da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 214429da9460SBarry Smith in local and then by concatenating the local matrices the end result. 214529da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 2146a0ff6018SBarry Smith */ 21477b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat) 2148a0ff6018SBarry Smith { 214900e6dbe6SBarry Smith int ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j; 2150fee21e36SBarry Smith Mat *local,M, Mreuse; 215100e6dbe6SBarry Smith Scalar *vwork,*aa; 215200e6dbe6SBarry Smith MPI_Comm comm = mat->comm; 215300e6dbe6SBarry Smith Mat_SeqAIJ *aij; 215400e6dbe6SBarry Smith int *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend; 2155a0ff6018SBarry Smith 2156a0ff6018SBarry Smith PetscFunctionBegin; 21571dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 21581dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 215900e6dbe6SBarry Smith 2160fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 2161fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 2162fee21e36SBarry Smith if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse"); 2163fee21e36SBarry Smith local = &Mreuse; 2164fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 2165fee21e36SBarry Smith } else { 2166a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 2167fee21e36SBarry Smith Mreuse = *local; 2168606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 2169fee21e36SBarry Smith } 2170a0ff6018SBarry Smith 2171a0ff6018SBarry Smith /* 2172a0ff6018SBarry Smith m - number of local rows 2173a0ff6018SBarry Smith n - number of columns (same on all processors) 2174a0ff6018SBarry Smith rstart - first row in new global matrix generated 2175a0ff6018SBarry Smith */ 2176fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 2177a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2178fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2179a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 218000e6dbe6SBarry Smith ii = aij->i; 218100e6dbe6SBarry Smith jj = aij->j; 218200e6dbe6SBarry Smith 2183a0ff6018SBarry Smith /* 218400e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 218500e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 2186a0ff6018SBarry Smith */ 218700e6dbe6SBarry Smith 218800e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 21896a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 219000e6dbe6SBarry Smith nlocal = n/size + ((n % size) > rank); 21916a6a5d1dSBarry Smith } else { 21926a6a5d1dSBarry Smith nlocal = csize; 21936a6a5d1dSBarry Smith } 2194ca161407SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 219500e6dbe6SBarry Smith rstart = rend - nlocal; 21966a6a5d1dSBarry Smith if (rank == size - 1 && rend != n) { 21976a6a5d1dSBarry Smith SETERRQ(1,1,"Local column sizes do not add up to total number of columns"); 21986a6a5d1dSBarry Smith } 219900e6dbe6SBarry Smith 220000e6dbe6SBarry Smith /* next, compute all the lengths */ 220100e6dbe6SBarry Smith dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens); 220200e6dbe6SBarry Smith olens = dlens + m; 220300e6dbe6SBarry Smith for ( i=0; i<m; i++ ) { 220400e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 220500e6dbe6SBarry Smith olen = 0; 220600e6dbe6SBarry Smith dlen = 0; 220700e6dbe6SBarry Smith for ( j=0; j<jend; j++ ) { 220800e6dbe6SBarry Smith if ( *jj < rstart || *jj >= rend) olen++; 220900e6dbe6SBarry Smith else dlen++; 221000e6dbe6SBarry Smith jj++; 221100e6dbe6SBarry Smith } 221200e6dbe6SBarry Smith olens[i] = olen; 221300e6dbe6SBarry Smith dlens[i] = dlen; 221400e6dbe6SBarry Smith } 22152d207970SBarry Smith ierr = MatCreateMPIAIJ(comm,m,nlocal,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr); 2216606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 2217a0ff6018SBarry Smith } else { 2218a0ff6018SBarry Smith int ml,nl; 2219a0ff6018SBarry Smith 2220a0ff6018SBarry Smith M = *newmat; 2221a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 2222a8c6a408SBarry Smith if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request"); 2223a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 2224c48de900SBarry Smith /* 2225c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 2226c48de900SBarry Smith rather than the slower MatSetValues(). 2227c48de900SBarry Smith */ 2228c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 2229c48de900SBarry Smith M->assembled = PETSC_FALSE; 2230a0ff6018SBarry Smith } 2231a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 2232fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2233a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 223400e6dbe6SBarry Smith ii = aij->i; 223500e6dbe6SBarry Smith jj = aij->j; 223600e6dbe6SBarry Smith aa = aij->a; 2237a0ff6018SBarry Smith for (i=0; i<m; i++) { 2238a0ff6018SBarry Smith row = rstart + i; 223900e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 224000e6dbe6SBarry Smith cwork = jj; jj += nz; 224100e6dbe6SBarry Smith vwork = aa; aa += nz; 22428c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 2243a0ff6018SBarry Smith } 2244a0ff6018SBarry Smith 2245a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2246a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2247a0ff6018SBarry Smith *newmat = M; 2248fee21e36SBarry Smith 2249fee21e36SBarry Smith /* save submatrix used in processor for next request */ 2250fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2251fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 2252fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 2253fee21e36SBarry Smith } 2254fee21e36SBarry Smith 2255a0ff6018SBarry Smith PetscFunctionReturn(0); 2256a0ff6018SBarry Smith } 2257