1b57c8cebSBarry Smith 2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 3*cb5b572fSBarry Smith static char vcid[] = "$Id: mpiaij.c,v 1.260 1998/08/25 19:52:04 bsmith Exp bsmith $"; 4cb512458SBarry Smith #endif 58a729477SBarry Smith 63369ce9aSBarry Smith #include "pinclude/pviewer.h" 770f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 8f5eb4b81SSatish Balay #include "src/vec/vecimpl.h" 9d9942c19SSatish Balay #include "src/inline/spops.h" 108a729477SBarry Smith 119e25ed09SBarry Smith /* local utility routine that creates a mapping from the global column 129e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 139e25ed09SBarry Smith storage of the matrix. This is done in a non scable way since the 149e25ed09SBarry Smith length of colmap equals the global matrix length. 159e25ed09SBarry Smith */ 165615d1e5SSatish Balay #undef __FUNC__ 17d4bb536fSBarry Smith #define __FUNC__ "CreateColmap_MPIAIJ_Private" 180a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat) 199e25ed09SBarry Smith { 2044a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 21ec8511deSBarry Smith Mat_SeqAIJ *B = (Mat_SeqAIJ*) aij->B->data; 22905e6a2fSBarry Smith int n = B->n,i; 23dbb450caSBarry Smith 243a40ed3dSBarry Smith PetscFunctionBegin; 25758f045eSSatish Balay aij->colmap = (int *) PetscMalloc((aij->N+1)*sizeof(int));CHKPTRQ(aij->colmap); 26464493b3SBarry Smith PLogObjectMemory(mat,aij->N*sizeof(int)); 27cddf8d76SBarry Smith PetscMemzero(aij->colmap,aij->N*sizeof(int)); 28905e6a2fSBarry Smith for ( i=0; i<n; i++ ) aij->colmap[aij->garray[i]] = i+1; 293a40ed3dSBarry Smith PetscFunctionReturn(0); 309e25ed09SBarry Smith } 319e25ed09SBarry Smith 322493cbb0SBarry Smith extern int DisAssemble_MPIAIJ(Mat); 332493cbb0SBarry Smith 340520107fSSatish Balay #define CHUNKSIZE 15 3530770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 360520107fSSatish Balay { \ 370520107fSSatish Balay \ 380520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 3930770e4dSSatish Balay rmax = aimax[row]; nrow = ailen[row]; \ 40f5e9677aSSatish Balay col1 = col - shift; \ 41f5e9677aSSatish Balay \ 42ba4e3ef2SSatish Balay low = 0; high = nrow; \ 43ba4e3ef2SSatish Balay while (high-low > 5) { \ 44ba4e3ef2SSatish Balay t = (low+high)/2; \ 45ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 46ba4e3ef2SSatish Balay else low = t; \ 47ba4e3ef2SSatish Balay } \ 480520107fSSatish Balay for ( _i=0; _i<nrow; _i++ ) { \ 49f5e9677aSSatish Balay if (rp[_i] > col1) break; \ 50f5e9677aSSatish Balay if (rp[_i] == col1) { \ 510520107fSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 520520107fSSatish Balay else ap[_i] = value; \ 5330770e4dSSatish Balay goto a_noinsert; \ 540520107fSSatish Balay } \ 550520107fSSatish Balay } \ 5689280ab3SLois Curfman McInnes if (nonew == 1) goto a_noinsert; \ 57a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \ 580520107fSSatish Balay if (nrow >= rmax) { \ 590520107fSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 600520107fSSatish Balay int new_nz = ai[a->m] + CHUNKSIZE,len,*new_i,*new_j; \ 610520107fSSatish Balay Scalar *new_a; \ 620520107fSSatish Balay \ 63a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \ 6489280ab3SLois Curfman McInnes \ 650520107fSSatish Balay /* malloc new storage space */ \ 660520107fSSatish Balay len = new_nz*(sizeof(int)+sizeof(Scalar))+(a->m+1)*sizeof(int); \ 670520107fSSatish Balay new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \ 680520107fSSatish Balay new_j = (int *) (new_a + new_nz); \ 690520107fSSatish Balay new_i = new_j + new_nz; \ 700520107fSSatish Balay \ 710520107fSSatish Balay /* copy over old data into new slots */ \ 720520107fSSatish Balay for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = ai[ii];} \ 730520107fSSatish Balay for ( ii=row+1; ii<a->m+1; ii++ ) {new_i[ii] = ai[ii]+CHUNKSIZE;} \ 740520107fSSatish Balay PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int)); \ 750520107fSSatish Balay len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \ 760520107fSSatish Balay PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \ 770520107fSSatish Balay len*sizeof(int)); \ 780520107fSSatish Balay PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(Scalar)); \ 790520107fSSatish Balay PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \ 800520107fSSatish Balay len*sizeof(Scalar)); \ 810520107fSSatish Balay /* free up old matrix storage */ \ 82f5e9677aSSatish Balay \ 830520107fSSatish Balay PetscFree(a->a); \ 840520107fSSatish Balay if (!a->singlemalloc) {PetscFree(a->i);PetscFree(a->j);} \ 850520107fSSatish Balay aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; \ 860520107fSSatish Balay a->singlemalloc = 1; \ 870520107fSSatish Balay \ 880520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 8930770e4dSSatish Balay rmax = aimax[row] = aimax[row] + CHUNKSIZE; \ 900520107fSSatish Balay PLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \ 910520107fSSatish Balay a->maxnz += CHUNKSIZE; \ 920520107fSSatish Balay a->reallocs++; \ 930520107fSSatish Balay } \ 940520107fSSatish Balay N = nrow++ - 1; a->nz++; \ 950520107fSSatish Balay /* shift up all the later entries in this row */ \ 960520107fSSatish Balay for ( ii=N; ii>=_i; ii-- ) { \ 970520107fSSatish Balay rp[ii+1] = rp[ii]; \ 980520107fSSatish Balay ap[ii+1] = ap[ii]; \ 990520107fSSatish Balay } \ 100f5e9677aSSatish Balay rp[_i] = col1; \ 1010520107fSSatish Balay ap[_i] = value; \ 10230770e4dSSatish Balay a_noinsert: ; \ 1030520107fSSatish Balay ailen[row] = nrow; \ 1040520107fSSatish Balay } 1050a198c4cSBarry Smith 10630770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 10730770e4dSSatish Balay { \ 10830770e4dSSatish Balay \ 10930770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 11030770e4dSSatish Balay rmax = bimax[row]; nrow = bilen[row]; \ 11130770e4dSSatish Balay col1 = col - shift; \ 11230770e4dSSatish Balay \ 113ba4e3ef2SSatish Balay low = 0; high = nrow; \ 114ba4e3ef2SSatish Balay while (high-low > 5) { \ 115ba4e3ef2SSatish Balay t = (low+high)/2; \ 116ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 117ba4e3ef2SSatish Balay else low = t; \ 118ba4e3ef2SSatish Balay } \ 11930770e4dSSatish Balay for ( _i=0; _i<nrow; _i++ ) { \ 12030770e4dSSatish Balay if (rp[_i] > col1) break; \ 12130770e4dSSatish Balay if (rp[_i] == col1) { \ 12230770e4dSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 12330770e4dSSatish Balay else ap[_i] = value; \ 12430770e4dSSatish Balay goto b_noinsert; \ 12530770e4dSSatish Balay } \ 12630770e4dSSatish Balay } \ 12789280ab3SLois Curfman McInnes if (nonew == 1) goto b_noinsert; \ 128a8c6a408SBarry Smith else if (nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero into matrix"); \ 12930770e4dSSatish Balay if (nrow >= rmax) { \ 13030770e4dSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 13174c639caSSatish Balay int new_nz = bi[b->m] + CHUNKSIZE,len,*new_i,*new_j; \ 13230770e4dSSatish Balay Scalar *new_a; \ 13330770e4dSSatish Balay \ 134a8c6a408SBarry Smith if (nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Inserting a new nonzero in the matrix"); \ 13589280ab3SLois Curfman McInnes \ 13630770e4dSSatish Balay /* malloc new storage space */ \ 13774c639caSSatish Balay len = new_nz*(sizeof(int)+sizeof(Scalar))+(b->m+1)*sizeof(int); \ 13830770e4dSSatish Balay new_a = (Scalar *) PetscMalloc( len ); CHKPTRQ(new_a); \ 13930770e4dSSatish Balay new_j = (int *) (new_a + new_nz); \ 14030770e4dSSatish Balay new_i = new_j + new_nz; \ 14130770e4dSSatish Balay \ 14230770e4dSSatish Balay /* copy over old data into new slots */ \ 14330770e4dSSatish Balay for ( ii=0; ii<row+1; ii++ ) {new_i[ii] = bi[ii];} \ 14474c639caSSatish Balay for ( ii=row+1; ii<b->m+1; ii++ ) {new_i[ii] = bi[ii]+CHUNKSIZE;} \ 14530770e4dSSatish Balay PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int)); \ 14630770e4dSSatish Balay len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \ 14730770e4dSSatish Balay PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \ 14830770e4dSSatish Balay len*sizeof(int)); \ 14930770e4dSSatish Balay PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(Scalar)); \ 15030770e4dSSatish Balay PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \ 15130770e4dSSatish Balay len*sizeof(Scalar)); \ 15230770e4dSSatish Balay /* free up old matrix storage */ \ 15330770e4dSSatish Balay \ 15474c639caSSatish Balay PetscFree(b->a); \ 15574c639caSSatish Balay if (!b->singlemalloc) {PetscFree(b->i);PetscFree(b->j);} \ 15674c639caSSatish Balay ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j; \ 15774c639caSSatish Balay b->singlemalloc = 1; \ 15830770e4dSSatish Balay \ 15930770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 16030770e4dSSatish Balay rmax = bimax[row] = bimax[row] + CHUNKSIZE; \ 16174c639caSSatish Balay PLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(Scalar))); \ 16274c639caSSatish Balay b->maxnz += CHUNKSIZE; \ 16374c639caSSatish Balay b->reallocs++; \ 16430770e4dSSatish Balay } \ 16574c639caSSatish Balay N = nrow++ - 1; b->nz++; \ 16630770e4dSSatish Balay /* shift up all the later entries in this row */ \ 16730770e4dSSatish Balay for ( ii=N; ii>=_i; ii-- ) { \ 16830770e4dSSatish Balay rp[ii+1] = rp[ii]; \ 16930770e4dSSatish Balay ap[ii+1] = ap[ii]; \ 17030770e4dSSatish Balay } \ 17130770e4dSSatish Balay rp[_i] = col1; \ 17230770e4dSSatish Balay ap[_i] = value; \ 17330770e4dSSatish Balay b_noinsert: ; \ 17430770e4dSSatish Balay bilen[row] = nrow; \ 17530770e4dSSatish Balay } 17630770e4dSSatish Balay 1770520107fSSatish Balay extern int MatSetValues_SeqAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode); 1785615d1e5SSatish Balay #undef __FUNC__ 1795615d1e5SSatish Balay #define __FUNC__ "MatSetValues_MPIAIJ" 1808f6be9afSLois Curfman McInnes int MatSetValues_MPIAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv) 1818a729477SBarry Smith { 18244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1834b0e389bSBarry Smith Scalar value; 1841eb62cbbSBarry Smith int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 1851eb62cbbSBarry Smith int cstart = aij->cstart, cend = aij->cend,row,col; 186905e6a2fSBarry Smith int roworiented = aij->roworiented; 1878a729477SBarry Smith 1880520107fSSatish Balay /* Some Variables required in the macro */ 1894ee7247eSSatish Balay Mat A = aij->A; 1904ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ *) A->data; 19130770e4dSSatish Balay int *aimax = a->imax, *ai = a->i, *ailen = a->ilen,*aj = a->j; 19230770e4dSSatish Balay Scalar *aa = a->a; 19330770e4dSSatish Balay 19430770e4dSSatish Balay Mat B = aij->B; 19530770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ *) B->data; 19630770e4dSSatish Balay int *bimax = b->imax, *bi = b->i, *bilen = b->ilen,*bj = b->j; 19730770e4dSSatish Balay Scalar *ba = b->a; 19830770e4dSSatish Balay 199ba4e3ef2SSatish Balay int *rp,ii,nrow,_i,rmax, N, col1,low,high,t; 20030770e4dSSatish Balay int nonew = a->nonew,shift = a->indexshift; 20130770e4dSSatish Balay Scalar *ap; 2024ee7247eSSatish Balay 2033a40ed3dSBarry Smith PetscFunctionBegin; 2048a729477SBarry Smith for ( i=0; i<m; i++ ) { 2053a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 206a8c6a408SBarry Smith if (im[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 207a8c6a408SBarry Smith if (im[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 2080a198c4cSBarry Smith #endif 2094b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 2104b0e389bSBarry Smith row = im[i] - rstart; 2111eb62cbbSBarry Smith for ( j=0; j<n; j++ ) { 2124b0e389bSBarry Smith if (in[j] >= cstart && in[j] < cend){ 2134b0e389bSBarry Smith col = in[j] - cstart; 2144b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 21530770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 2160520107fSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2171eb62cbbSBarry Smith } 2183a40ed3dSBarry Smith #if defined(USE_PETSC_BOPT_g) 219a8c6a408SBarry Smith else if (in[j] < 0) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column");} 220a8c6a408SBarry Smith else if (in[j] >= aij->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large");} 2210a198c4cSBarry Smith #endif 2221eb62cbbSBarry Smith else { 223227d817aSBarry Smith if (mat->was_assembled) { 224905e6a2fSBarry Smith if (!aij->colmap) { 225905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 226905e6a2fSBarry Smith } 227905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 228ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 2292493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr); 2304b0e389bSBarry Smith col = in[j]; 2319bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 232f9508a3cSSatish Balay B = aij->B; 233f9508a3cSSatish Balay b = (Mat_SeqAIJ *) B->data; 234f9508a3cSSatish Balay bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 235f9508a3cSSatish Balay ba = b->a; 236d6dfbf8fSBarry Smith } 237c48de900SBarry Smith } else col = in[j]; 2384b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 23930770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 24030770e4dSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2411eb62cbbSBarry Smith } 2421eb62cbbSBarry Smith } 2431eb62cbbSBarry Smith } 2441eb62cbbSBarry Smith else { 24590f02eecSBarry Smith if (roworiented && !aij->donotstash) { 2464b0e389bSBarry Smith ierr = StashValues_Private(&aij->stash,im[i],n,in,v+i*n,addv);CHKERRQ(ierr); 2474b0e389bSBarry Smith } 2484b0e389bSBarry Smith else { 24990f02eecSBarry Smith if (!aij->donotstash) { 2504b0e389bSBarry Smith row = im[i]; 2514b0e389bSBarry Smith for ( j=0; j<n; j++ ) { 2524b0e389bSBarry Smith ierr = StashValues_Private(&aij->stash,row,1,in+j,v+i+j*m,addv);CHKERRQ(ierr); 2534b0e389bSBarry Smith } 2544b0e389bSBarry Smith } 2551eb62cbbSBarry Smith } 2568a729477SBarry Smith } 25790f02eecSBarry Smith } 2583a40ed3dSBarry Smith PetscFunctionReturn(0); 2598a729477SBarry Smith } 2608a729477SBarry Smith 2615615d1e5SSatish Balay #undef __FUNC__ 2625615d1e5SSatish Balay #define __FUNC__ "MatGetValues_MPIAIJ" 2638f6be9afSLois Curfman McInnes int MatGetValues_MPIAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 264b49de8d1SLois Curfman McInnes { 265b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 266b49de8d1SLois Curfman McInnes int ierr,i,j, rstart = aij->rstart, rend = aij->rend; 267b49de8d1SLois Curfman McInnes int cstart = aij->cstart, cend = aij->cend,row,col; 268b49de8d1SLois Curfman McInnes 2693a40ed3dSBarry Smith PetscFunctionBegin; 270b49de8d1SLois Curfman McInnes for ( i=0; i<m; i++ ) { 271a8c6a408SBarry Smith if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative row"); 272a8c6a408SBarry Smith if (idxm[i] >= aij->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row too large"); 273b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 274b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 275b49de8d1SLois Curfman McInnes for ( j=0; j<n; j++ ) { 276a8c6a408SBarry Smith if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Negative column"); 277a8c6a408SBarry Smith if (idxn[j] >= aij->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Column too large"); 278b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 279b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 280b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr); 281fa852ad4SSatish Balay } else { 282905e6a2fSBarry Smith if (!aij->colmap) { 283905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 284905e6a2fSBarry Smith } 285905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 286e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 287d9d09a02SSatish Balay else { 288b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j); CHKERRQ(ierr); 289b49de8d1SLois Curfman McInnes } 290b49de8d1SLois Curfman McInnes } 291b49de8d1SLois Curfman McInnes } 292a8c6a408SBarry Smith } else { 293a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Only local values currently supported"); 294b49de8d1SLois Curfman McInnes } 295b49de8d1SLois Curfman McInnes } 2963a40ed3dSBarry Smith PetscFunctionReturn(0); 297b49de8d1SLois Curfman McInnes } 298b49de8d1SLois Curfman McInnes 2995615d1e5SSatish Balay #undef __FUNC__ 3005615d1e5SSatish Balay #define __FUNC__ "MatAssemblyBegin_MPIAIJ" 3018f6be9afSLois Curfman McInnes int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 3028a729477SBarry Smith { 30344a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 304d6dfbf8fSBarry Smith MPI_Comm comm = mat->comm; 30517699dbbSLois Curfman McInnes int size = aij->size, *owners = aij->rowners; 30617699dbbSLois Curfman McInnes int rank = aij->rank,tag = mat->tag, *owner,*starts,count,ierr; 3071eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 3086abc6512SBarry Smith int *nprocs,i,j,idx,*procs,nsends,nreceives,nmax,*work; 3091eb62cbbSBarry Smith InsertMode addv; 3101eb62cbbSBarry Smith Scalar *rvalues,*svalues; 3111eb62cbbSBarry Smith 3123a40ed3dSBarry Smith PetscFunctionBegin; 313b5bd3ad5SBarry Smith if (aij->donotstash) { 314b5bd3ad5SBarry Smith aij->svalues = 0; aij->rvalues = 0; 315b5bd3ad5SBarry Smith aij->nsends = 0; aij->nrecvs = 0; 316b5bd3ad5SBarry Smith aij->send_waits = 0; aij->recv_waits = 0; 317b5bd3ad5SBarry Smith aij->rmax = 0; 318b5bd3ad5SBarry Smith PetscFunctionReturn(0); 319b5bd3ad5SBarry Smith } 320b5bd3ad5SBarry Smith 3211eb62cbbSBarry Smith /* make sure all processors are either in INSERTMODE or ADDMODE */ 322ca161407SBarry Smith ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,comm);CHKERRQ(ierr); 323dbb450caSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) { 324a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Some processors inserted others added"); 3251eb62cbbSBarry Smith } 32647794344SBarry Smith mat->insertmode = addv; /* in case this processor had no cache */ 3271eb62cbbSBarry Smith 3281eb62cbbSBarry Smith /* first count number of contributors to each processor */ 3290452661fSBarry Smith nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs); 330cddf8d76SBarry Smith PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size; 3310452661fSBarry Smith owner = (int *) PetscMalloc( (aij->stash.n+1)*sizeof(int) ); CHKPTRQ(owner); 3321eb62cbbSBarry Smith for ( i=0; i<aij->stash.n; i++ ) { 3331eb62cbbSBarry Smith idx = aij->stash.idx[i]; 33417699dbbSLois Curfman McInnes for ( j=0; j<size; j++ ) { 3351eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 3361eb62cbbSBarry Smith nprocs[j]++; procs[j] = 1; owner[i] = j; break; 3378a729477SBarry Smith } 3388a729477SBarry Smith } 3398a729477SBarry Smith } 34017699dbbSLois Curfman McInnes nsends = 0; for ( i=0; i<size; i++ ) { nsends += procs[i];} 3411eb62cbbSBarry Smith 3421eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 3430452661fSBarry Smith work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work); 344ca161407SBarry Smith ierr = MPI_Allreduce(procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 34517699dbbSLois Curfman McInnes nreceives = work[rank]; 346ca161407SBarry Smith ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr); 34717699dbbSLois Curfman McInnes nmax = work[rank]; 3480452661fSBarry Smith PetscFree(work); 3491eb62cbbSBarry Smith 3501eb62cbbSBarry Smith /* post receives: 3511eb62cbbSBarry Smith 1) each message will consist of ordered pairs 3521eb62cbbSBarry Smith (global index,value) we store the global index as a double 353d6dfbf8fSBarry Smith to simplify the message passing. 3541eb62cbbSBarry Smith 2) since we don't know how long each individual message is we 3551eb62cbbSBarry Smith allocate the largest needed buffer for each receive. Potentially 3561eb62cbbSBarry Smith this is a lot of wasted space. 3571eb62cbbSBarry Smith 3581eb62cbbSBarry Smith 3591eb62cbbSBarry Smith This could be done better. 3601eb62cbbSBarry Smith */ 361ca161407SBarry Smith rvalues = (Scalar *) PetscMalloc(3*(nreceives+1)*(nmax+1)*sizeof(Scalar));CHKPTRQ(rvalues); 362ca161407SBarry Smith recv_waits = (MPI_Request *) PetscMalloc((nreceives+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits); 3631eb62cbbSBarry Smith for ( i=0; i<nreceives; i++ ) { 364ca161407SBarry Smith ierr = MPI_Irecv(rvalues+3*nmax*i,3*nmax,MPIU_SCALAR,MPI_ANY_SOURCE,tag, 365ca161407SBarry Smith comm,recv_waits+i);CHKERRQ(ierr); 3661eb62cbbSBarry Smith } 3671eb62cbbSBarry Smith 3681eb62cbbSBarry Smith /* do sends: 3691eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 3701eb62cbbSBarry Smith the ith processor 3711eb62cbbSBarry Smith */ 3720452661fSBarry Smith svalues = (Scalar *) PetscMalloc(3*(aij->stash.n+1)*sizeof(Scalar));CHKPTRQ(svalues); 373ca161407SBarry Smith send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits); 3740452661fSBarry Smith starts = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(starts); 3751eb62cbbSBarry Smith starts[0] = 0; 37617699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 3771eb62cbbSBarry Smith for ( i=0; i<aij->stash.n; i++ ) { 3781eb62cbbSBarry Smith svalues[3*starts[owner[i]]] = (Scalar) aij->stash.idx[i]; 3791eb62cbbSBarry Smith svalues[3*starts[owner[i]]+1] = (Scalar) aij->stash.idy[i]; 3801eb62cbbSBarry Smith svalues[3*(starts[owner[i]]++)+2] = aij->stash.array[i]; 3811eb62cbbSBarry Smith } 3820452661fSBarry Smith PetscFree(owner); 3831eb62cbbSBarry Smith starts[0] = 0; 38417699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 3851eb62cbbSBarry Smith count = 0; 38617699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 3871eb62cbbSBarry Smith if (procs[i]) { 388ca161407SBarry Smith ierr = MPI_Isend(svalues+3*starts[i],3*nprocs[i],MPIU_SCALAR,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 3891eb62cbbSBarry Smith } 3901eb62cbbSBarry Smith } 391b5bd3ad5SBarry Smith PetscFree(starts); 392b5bd3ad5SBarry Smith PetscFree(nprocs); 3931eb62cbbSBarry Smith 3941eb62cbbSBarry Smith /* Free cache space */ 39510a665d1SBarry Smith PLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Number of off-processor values %d\n",aij->stash.n); 39678b31e54SBarry Smith ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr); 3971eb62cbbSBarry Smith 3981eb62cbbSBarry Smith aij->svalues = svalues; aij->rvalues = rvalues; 3991eb62cbbSBarry Smith aij->nsends = nsends; aij->nrecvs = nreceives; 4001eb62cbbSBarry Smith aij->send_waits = send_waits; aij->recv_waits = recv_waits; 4011eb62cbbSBarry Smith aij->rmax = nmax; 4021eb62cbbSBarry Smith 4033a40ed3dSBarry Smith PetscFunctionReturn(0); 4041eb62cbbSBarry Smith } 40544a69424SLois Curfman McInnes extern int MatSetUpMultiply_MPIAIJ(Mat); 4061eb62cbbSBarry Smith 4075615d1e5SSatish Balay #undef __FUNC__ 4085615d1e5SSatish Balay #define __FUNC__ "MatAssemblyEnd_MPIAIJ" 4098f6be9afSLois Curfman McInnes int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 4101eb62cbbSBarry Smith { 41144a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 4121eb62cbbSBarry Smith MPI_Status *send_status,recv_status; 413416022c9SBarry Smith int imdex,nrecvs = aij->nrecvs, count = nrecvs, i, n, ierr; 414905e6a2fSBarry Smith int row,col,other_disassembled; 4151eb62cbbSBarry Smith Scalar *values,val; 41647794344SBarry Smith InsertMode addv = mat->insertmode; 4171eb62cbbSBarry Smith 4183a40ed3dSBarry Smith PetscFunctionBegin; 419b5bd3ad5SBarry Smith 4201eb62cbbSBarry Smith /* wait on receives */ 4211eb62cbbSBarry Smith while (count) { 422ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,aij->recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 4231eb62cbbSBarry Smith /* unpack receives into our local space */ 424d6dfbf8fSBarry Smith values = aij->rvalues + 3*imdex*aij->rmax; 425ca161407SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_SCALAR,&n);CHKERRQ(ierr); 4261eb62cbbSBarry Smith n = n/3; 4271eb62cbbSBarry Smith for ( i=0; i<n; i++ ) { 428227d817aSBarry Smith row = (int) PetscReal(values[3*i]) - aij->rstart; 429227d817aSBarry Smith col = (int) PetscReal(values[3*i+1]); 4301eb62cbbSBarry Smith val = values[3*i+2]; 4311eb62cbbSBarry Smith if (col >= aij->cstart && col < aij->cend) { 4321eb62cbbSBarry Smith col -= aij->cstart; 4336fd7127cSSatish Balay ierr = MatSetValues(aij->A,1,&row,1,&col,&val,addv); CHKERRQ(ierr); 4343a40ed3dSBarry Smith } else { 43555a1b374SBarry Smith if (mat->was_assembled || mat->assembled) { 436905e6a2fSBarry Smith if (!aij->colmap) { 437905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat); CHKERRQ(ierr); 438905e6a2fSBarry Smith } 439905e6a2fSBarry Smith col = aij->colmap[col] - 1; 440ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 4412493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr); 442227d817aSBarry Smith col = (int) PetscReal(values[3*i+1]); 443d6dfbf8fSBarry Smith } 4449e25ed09SBarry Smith } 4456fd7127cSSatish Balay ierr = MatSetValues(aij->B,1,&row,1,&col,&val,addv); CHKERRQ(ierr); 4461eb62cbbSBarry Smith } 4471eb62cbbSBarry Smith } 4481eb62cbbSBarry Smith count--; 4491eb62cbbSBarry Smith } 450570da906SBarry Smith if (aij->recv_waits) PetscFree(aij->recv_waits); 451570da906SBarry Smith if (aij->rvalues) PetscFree(aij->rvalues); 4521eb62cbbSBarry Smith 4531eb62cbbSBarry Smith /* wait on sends */ 4541eb62cbbSBarry Smith if (aij->nsends) { 4550a198c4cSBarry Smith send_status = (MPI_Status *) PetscMalloc(aij->nsends*sizeof(MPI_Status));CHKPTRQ(send_status); 456ca161407SBarry Smith ierr = MPI_Waitall(aij->nsends,aij->send_waits,send_status);CHKERRQ(ierr); 4570452661fSBarry Smith PetscFree(send_status); 4581eb62cbbSBarry Smith } 459b5bd3ad5SBarry Smith if (aij->send_waits) PetscFree(aij->send_waits); 460b5bd3ad5SBarry Smith if (aij->svalues) PetscFree(aij->svalues); 4611eb62cbbSBarry Smith 46278b31e54SBarry Smith ierr = MatAssemblyBegin(aij->A,mode); CHKERRQ(ierr); 46378b31e54SBarry Smith ierr = MatAssemblyEnd(aij->A,mode); CHKERRQ(ierr); 4641eb62cbbSBarry Smith 4652493cbb0SBarry Smith /* determine if any processor has disassembled, if so we must 4662493cbb0SBarry Smith also disassemble ourselfs, in order that we may reassemble. */ 46741e46ba1SBarry Smith /* 46841e46ba1SBarry Smith if nonzero structure of submatrix B cannot change then we know that 46941e46ba1SBarry Smith no processor disassembled thus we can skip this stuff 47041e46ba1SBarry Smith */ 47141e46ba1SBarry Smith if (!((Mat_SeqAIJ*) aij->B->data)->nonew) { 472ca161407SBarry Smith ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr); 473227d817aSBarry Smith if (mat->was_assembled && !other_disassembled) { 4742493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat); CHKERRQ(ierr); 4752493cbb0SBarry Smith } 47641e46ba1SBarry Smith } 4772493cbb0SBarry Smith 4786d4a8577SBarry Smith if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 47978b31e54SBarry Smith ierr = MatSetUpMultiply_MPIAIJ(mat); CHKERRQ(ierr); 4805e42470aSBarry Smith } 48178b31e54SBarry Smith ierr = MatAssemblyBegin(aij->B,mode); CHKERRQ(ierr); 48278b31e54SBarry Smith ierr = MatAssemblyEnd(aij->B,mode); CHKERRQ(ierr); 4835e42470aSBarry Smith 4847a0afa10SBarry Smith if (aij->rowvalues) {PetscFree(aij->rowvalues); aij->rowvalues = 0;} 4853a40ed3dSBarry Smith PetscFunctionReturn(0); 4868a729477SBarry Smith } 4878a729477SBarry Smith 4885615d1e5SSatish Balay #undef __FUNC__ 4895615d1e5SSatish Balay #define __FUNC__ "MatZeroEntries_MPIAIJ" 4908f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A) 4911eb62cbbSBarry Smith { 49244a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 493dbd7a890SLois Curfman McInnes int ierr; 4943a40ed3dSBarry Smith 4953a40ed3dSBarry Smith PetscFunctionBegin; 49678b31e54SBarry Smith ierr = MatZeroEntries(l->A); CHKERRQ(ierr); 49778b31e54SBarry Smith ierr = MatZeroEntries(l->B); CHKERRQ(ierr); 4983a40ed3dSBarry Smith PetscFunctionReturn(0); 4991eb62cbbSBarry Smith } 5001eb62cbbSBarry Smith 5015615d1e5SSatish Balay #undef __FUNC__ 5025615d1e5SSatish Balay #define __FUNC__ "MatZeroRows_MPIAIJ" 5038f6be9afSLois Curfman McInnes int MatZeroRows_MPIAIJ(Mat A,IS is,Scalar *diag) 5041eb62cbbSBarry Smith { 50544a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ *) A->data; 50617699dbbSLois Curfman McInnes int i,ierr,N, *rows,*owners = l->rowners,size = l->size; 5076a5c57faSSatish Balay int *procs,*nprocs,j,found,idx,nsends,*work,row; 50817699dbbSLois Curfman McInnes int nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank; 5095392566eSBarry Smith int *rvalues,tag = A->tag,count,base,slen,n,*source; 5106a5c57faSSatish Balay int *lens,imdex,*lrows,*values,rstart=l->rstart; 511d6dfbf8fSBarry Smith MPI_Comm comm = A->comm; 5121eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 5131eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 5141eb62cbbSBarry Smith IS istmp; 5156eb55b6aSBarry Smith PetscTruth localdiag; 5161eb62cbbSBarry Smith 5173a40ed3dSBarry Smith PetscFunctionBegin; 51877c4ece6SBarry Smith ierr = ISGetSize(is,&N); CHKERRQ(ierr); 51978b31e54SBarry Smith ierr = ISGetIndices(is,&rows); CHKERRQ(ierr); 5201eb62cbbSBarry Smith 5211eb62cbbSBarry Smith /* first count number of contributors to each processor */ 5220452661fSBarry Smith nprocs = (int *) PetscMalloc( 2*size*sizeof(int) ); CHKPTRQ(nprocs); 523cddf8d76SBarry Smith PetscMemzero(nprocs,2*size*sizeof(int)); procs = nprocs + size; 5240452661fSBarry Smith owner = (int *) PetscMalloc((N+1)*sizeof(int)); CHKPTRQ(owner); /* see note*/ 5251eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 5261eb62cbbSBarry Smith idx = rows[i]; 5271eb62cbbSBarry Smith found = 0; 52817699dbbSLois Curfman McInnes for ( j=0; j<size; j++ ) { 5291eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 5301eb62cbbSBarry Smith nprocs[j]++; procs[j] = 1; owner[i] = j; found = 1; break; 5311eb62cbbSBarry Smith } 5321eb62cbbSBarry Smith } 533a8c6a408SBarry Smith if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Index out of range"); 5341eb62cbbSBarry Smith } 53517699dbbSLois Curfman McInnes nsends = 0; for ( i=0; i<size; i++ ) { nsends += procs[i];} 5361eb62cbbSBarry Smith 5371eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 5380452661fSBarry Smith work = (int *) PetscMalloc( size*sizeof(int) ); CHKPTRQ(work); 539ca161407SBarry Smith ierr = MPI_Allreduce( procs, work,size,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 54017699dbbSLois Curfman McInnes nrecvs = work[rank]; 541ca161407SBarry Smith ierr = MPI_Allreduce( nprocs, work,size,MPI_INT,MPI_MAX,comm);CHKERRQ(ierr); 54217699dbbSLois Curfman McInnes nmax = work[rank]; 5430452661fSBarry Smith PetscFree(work); 5441eb62cbbSBarry Smith 5451eb62cbbSBarry Smith /* post receives: */ 5463a40ed3dSBarry Smith rvalues = (int *) PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int));CHKPTRQ(rvalues); 547ca161407SBarry Smith recv_waits = (MPI_Request *) PetscMalloc((nrecvs+1)*sizeof(MPI_Request));CHKPTRQ(recv_waits); 5481eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 549ca161407SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 5501eb62cbbSBarry Smith } 5511eb62cbbSBarry Smith 5521eb62cbbSBarry Smith /* do sends: 5531eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 5541eb62cbbSBarry Smith the ith processor 5551eb62cbbSBarry Smith */ 5560452661fSBarry Smith svalues = (int *) PetscMalloc( (N+1)*sizeof(int) ); CHKPTRQ(svalues); 5573a40ed3dSBarry Smith send_waits = (MPI_Request *) PetscMalloc( (nsends+1)*sizeof(MPI_Request));CHKPTRQ(send_waits); 5580452661fSBarry Smith starts = (int *) PetscMalloc( (size+1)*sizeof(int) ); CHKPTRQ(starts); 5591eb62cbbSBarry Smith starts[0] = 0; 56017699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 5611eb62cbbSBarry Smith for ( i=0; i<N; i++ ) { 5621eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 5631eb62cbbSBarry Smith } 5641eb62cbbSBarry Smith ISRestoreIndices(is,&rows); 5651eb62cbbSBarry Smith 5661eb62cbbSBarry Smith starts[0] = 0; 56717699dbbSLois Curfman McInnes for ( i=1; i<size+1; i++ ) { starts[i] = starts[i-1] + nprocs[i-1];} 5681eb62cbbSBarry Smith count = 0; 56917699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 5701eb62cbbSBarry Smith if (procs[i]) { 571ca161407SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 5721eb62cbbSBarry Smith } 5731eb62cbbSBarry Smith } 5740452661fSBarry Smith PetscFree(starts); 5751eb62cbbSBarry Smith 57617699dbbSLois Curfman McInnes base = owners[rank]; 5771eb62cbbSBarry Smith 5781eb62cbbSBarry Smith /* wait on receives */ 5790452661fSBarry Smith lens = (int *) PetscMalloc( 2*(nrecvs+1)*sizeof(int) ); CHKPTRQ(lens); 5801eb62cbbSBarry Smith source = lens + nrecvs; 5811eb62cbbSBarry Smith count = nrecvs; slen = 0; 5821eb62cbbSBarry Smith while (count) { 583ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 5841eb62cbbSBarry Smith /* unpack receives into our local space */ 585ca161407SBarry Smith ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr); 586d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 587d6dfbf8fSBarry Smith lens[imdex] = n; 5881eb62cbbSBarry Smith slen += n; 5891eb62cbbSBarry Smith count--; 5901eb62cbbSBarry Smith } 5910452661fSBarry Smith PetscFree(recv_waits); 5921eb62cbbSBarry Smith 5931eb62cbbSBarry Smith /* move the data into the send scatter */ 5940452661fSBarry Smith lrows = (int *) PetscMalloc( (slen+1)*sizeof(int) ); CHKPTRQ(lrows); 5951eb62cbbSBarry Smith count = 0; 5961eb62cbbSBarry Smith for ( i=0; i<nrecvs; i++ ) { 5971eb62cbbSBarry Smith values = rvalues + i*nmax; 5981eb62cbbSBarry Smith for ( j=0; j<lens[i]; j++ ) { 5991eb62cbbSBarry Smith lrows[count++] = values[j] - base; 6001eb62cbbSBarry Smith } 6011eb62cbbSBarry Smith } 6020452661fSBarry Smith PetscFree(rvalues); PetscFree(lens); 6030452661fSBarry Smith PetscFree(owner); PetscFree(nprocs); 6041eb62cbbSBarry Smith 6051eb62cbbSBarry Smith /* actually zap the local rows */ 606029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr); 607464493b3SBarry Smith PLogObjectParent(A,istmp); 6086a5c57faSSatish Balay 6096eb55b6aSBarry Smith /* 6106eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 6116eb55b6aSBarry Smith is square and the user wishes to set the diagonal we use seperate 6126eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 6136eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 6146eb55b6aSBarry Smith 6156eb55b6aSBarry Smith Contributed by: Mathew Knepley 6166eb55b6aSBarry Smith */ 6176eb55b6aSBarry Smith localdiag = PETSC_FALSE; 6186eb55b6aSBarry Smith if (diag && (l->A->M == l->A->N)) { 6196eb55b6aSBarry Smith localdiag = PETSC_TRUE; 6206eb55b6aSBarry Smith ierr = MatZeroRows(l->A,istmp,diag); CHKERRQ(ierr); 6216eb55b6aSBarry Smith } else { 6226a5c57faSSatish Balay ierr = MatZeroRows(l->A,istmp,0); CHKERRQ(ierr); 6236eb55b6aSBarry Smith } 62478b31e54SBarry Smith ierr = MatZeroRows(l->B,istmp,0); CHKERRQ(ierr); 62578b31e54SBarry Smith ierr = ISDestroy(istmp); CHKERRQ(ierr); 6261eb62cbbSBarry Smith 6276eb55b6aSBarry Smith if (diag && (localdiag == PETSC_FALSE)) { 6286eb55b6aSBarry Smith for ( i = 0; i < slen; i++ ) { 6296eb55b6aSBarry Smith row = lrows[i] + rstart; 6306eb55b6aSBarry Smith MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES); 6316eb55b6aSBarry Smith } 6326eb55b6aSBarry Smith MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); 6336eb55b6aSBarry Smith MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); 6346eb55b6aSBarry Smith } 6356eb55b6aSBarry Smith 6366a5c57faSSatish Balay if (diag) { 6376a5c57faSSatish Balay for ( i = 0; i < slen; i++ ) { 6386a5c57faSSatish Balay row = lrows[i] + rstart; 6396a5c57faSSatish Balay MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES); 6406a5c57faSSatish Balay } 6416a5c57faSSatish Balay MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); 6426a5c57faSSatish Balay MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); 6436a5c57faSSatish Balay } 6446a5c57faSSatish Balay PetscFree(lrows); 64572dacd9aSBarry Smith 6461eb62cbbSBarry Smith /* wait on sends */ 6471eb62cbbSBarry Smith if (nsends) { 648ca161407SBarry Smith send_status = (MPI_Status *) PetscMalloc(nsends*sizeof(MPI_Status));CHKPTRQ(send_status); 649ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 6500452661fSBarry Smith PetscFree(send_status); 6511eb62cbbSBarry Smith } 6520452661fSBarry Smith PetscFree(send_waits); PetscFree(svalues); 6531eb62cbbSBarry Smith 6543a40ed3dSBarry Smith PetscFunctionReturn(0); 6551eb62cbbSBarry Smith } 6561eb62cbbSBarry Smith 6575615d1e5SSatish Balay #undef __FUNC__ 6585615d1e5SSatish Balay #define __FUNC__ "MatMult_MPIAIJ" 6598f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 6601eb62cbbSBarry Smith { 661416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 662fbd6ef76SBarry Smith int ierr,nt; 663416022c9SBarry Smith 6643a40ed3dSBarry Smith PetscFunctionBegin; 665a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt); CHKERRQ(ierr); 666fbd6ef76SBarry Smith if (nt != a->n) { 667a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Incompatible partition of A and xx"); 668fbd6ef76SBarry Smith } 66943a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr); 670f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy); CHKERRQ(ierr); 67143a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx); CHKERRQ(ierr); 672f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy); CHKERRQ(ierr); 6733a40ed3dSBarry Smith PetscFunctionReturn(0); 6741eb62cbbSBarry Smith } 6751eb62cbbSBarry Smith 6765615d1e5SSatish Balay #undef __FUNC__ 6775615d1e5SSatish Balay #define __FUNC__ "MatMultAdd_MPIAIJ" 6788f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 679da3a660dSBarry Smith { 680416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 681da3a660dSBarry Smith int ierr; 6823a40ed3dSBarry Smith 6833a40ed3dSBarry Smith PetscFunctionBegin; 68443a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 685f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz); CHKERRQ(ierr); 68643a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 687f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz); CHKERRQ(ierr); 6883a40ed3dSBarry Smith PetscFunctionReturn(0); 689da3a660dSBarry Smith } 690da3a660dSBarry Smith 6915615d1e5SSatish Balay #undef __FUNC__ 6925615d1e5SSatish Balay #define __FUNC__ "MatMultTrans_MPIAIJ" 6938f6be9afSLois Curfman McInnes int MatMultTrans_MPIAIJ(Mat A,Vec xx,Vec yy) 694da3a660dSBarry Smith { 695416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 696da3a660dSBarry Smith int ierr; 697da3a660dSBarry Smith 6983a40ed3dSBarry Smith PetscFunctionBegin; 699da3a660dSBarry Smith /* do nondiagonal part */ 700f830108cSBarry Smith ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr); 701da3a660dSBarry Smith /* send it on its way */ 702537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 703da3a660dSBarry Smith /* do local part */ 704f830108cSBarry Smith ierr = (*a->A->ops->multtrans)(a->A,xx,yy); CHKERRQ(ierr); 705da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 706da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 707da3a660dSBarry Smith /* but is not perhaps always true. */ 708537820f0SBarry Smith ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 7093a40ed3dSBarry Smith PetscFunctionReturn(0); 710da3a660dSBarry Smith } 711da3a660dSBarry Smith 7125615d1e5SSatish Balay #undef __FUNC__ 7135615d1e5SSatish Balay #define __FUNC__ "MatMultTransAdd_MPIAIJ" 7148f6be9afSLois Curfman McInnes int MatMultTransAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 715da3a660dSBarry Smith { 716416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 717da3a660dSBarry Smith int ierr; 718da3a660dSBarry Smith 7193a40ed3dSBarry Smith PetscFunctionBegin; 720da3a660dSBarry Smith /* do nondiagonal part */ 721f830108cSBarry Smith ierr = (*a->B->ops->multtrans)(a->B,xx,a->lvec); CHKERRQ(ierr); 722da3a660dSBarry Smith /* send it on its way */ 723537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 724da3a660dSBarry Smith /* do local part */ 725f830108cSBarry Smith ierr = (*a->A->ops->multtransadd)(a->A,xx,yy,zz); CHKERRQ(ierr); 726da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 727da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 728da3a660dSBarry Smith /* but is not perhaps always true. */ 7290a198c4cSBarry Smith ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx); CHKERRQ(ierr); 7303a40ed3dSBarry Smith PetscFunctionReturn(0); 731da3a660dSBarry Smith } 732da3a660dSBarry Smith 7331eb62cbbSBarry Smith /* 7341eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 7351eb62cbbSBarry Smith diagonal block 7361eb62cbbSBarry Smith */ 7375615d1e5SSatish Balay #undef __FUNC__ 7385615d1e5SSatish Balay #define __FUNC__ "MatGetDiagonal_MPIAIJ" 7398f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v) 7401eb62cbbSBarry Smith { 7413a40ed3dSBarry Smith int ierr; 742416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 7433a40ed3dSBarry Smith 7443a40ed3dSBarry Smith PetscFunctionBegin; 745a8c6a408SBarry Smith if (a->M != a->N) SETERRQ(PETSC_ERR_SUP,0,"Supports only square matrix where A->A is diag block"); 7465baf8537SBarry Smith if (a->rstart != a->cstart || a->rend != a->cend) { 747a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"row partition must equal col partition"); 7483a40ed3dSBarry Smith } 7493a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 7503a40ed3dSBarry Smith PetscFunctionReturn(0); 7511eb62cbbSBarry Smith } 7521eb62cbbSBarry Smith 7535615d1e5SSatish Balay #undef __FUNC__ 7545615d1e5SSatish Balay #define __FUNC__ "MatScale_MPIAIJ" 7558f6be9afSLois Curfman McInnes int MatScale_MPIAIJ(Scalar *aa,Mat A) 756052efed2SBarry Smith { 757052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 758052efed2SBarry Smith int ierr; 7593a40ed3dSBarry Smith 7603a40ed3dSBarry Smith PetscFunctionBegin; 761052efed2SBarry Smith ierr = MatScale(aa,a->A); CHKERRQ(ierr); 762052efed2SBarry Smith ierr = MatScale(aa,a->B); CHKERRQ(ierr); 7633a40ed3dSBarry Smith PetscFunctionReturn(0); 764052efed2SBarry Smith } 765052efed2SBarry Smith 7665615d1e5SSatish Balay #undef __FUNC__ 767d4bb536fSBarry Smith #define __FUNC__ "MatDestroy_MPIAIJ" 768e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat) 7691eb62cbbSBarry Smith { 77044a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 7711eb62cbbSBarry Smith int ierr; 77283e2fdc7SBarry Smith 7733a40ed3dSBarry Smith PetscFunctionBegin; 77470429bc8SBarry Smith if (--mat->refct > 0) PetscFunctionReturn(0); 77570429bc8SBarry Smith 77670429bc8SBarry Smith if (mat->mapping) { 77770429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->mapping); CHKERRQ(ierr); 77870429bc8SBarry Smith } 77970429bc8SBarry Smith if (mat->bmapping) { 78070429bc8SBarry Smith ierr = ISLocalToGlobalMappingDestroy(mat->bmapping); CHKERRQ(ierr); 78170429bc8SBarry Smith } 78261b13de0SBarry Smith if (mat->rmap) { 78361b13de0SBarry Smith ierr = MapDestroy(mat->rmap);CHKERRQ(ierr); 78461b13de0SBarry Smith } 78561b13de0SBarry Smith if (mat->cmap) { 78661b13de0SBarry Smith ierr = MapDestroy(mat->cmap);CHKERRQ(ierr); 78761b13de0SBarry Smith } 7883a40ed3dSBarry Smith #if defined(USE_PETSC_LOG) 789e1311b90SBarry Smith PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",aij->M,aij->N); 790a5a9c739SBarry Smith #endif 79183e2fdc7SBarry Smith ierr = StashDestroy_Private(&aij->stash); CHKERRQ(ierr); 7920452661fSBarry Smith PetscFree(aij->rowners); 79378b31e54SBarry Smith ierr = MatDestroy(aij->A); CHKERRQ(ierr); 79478b31e54SBarry Smith ierr = MatDestroy(aij->B); CHKERRQ(ierr); 7950452661fSBarry Smith if (aij->colmap) PetscFree(aij->colmap); 7960452661fSBarry Smith if (aij->garray) PetscFree(aij->garray); 7971eb62cbbSBarry Smith if (aij->lvec) VecDestroy(aij->lvec); 798a56f8943SBarry Smith if (aij->Mvctx) VecScatterDestroy(aij->Mvctx); 7997a0afa10SBarry Smith if (aij->rowvalues) PetscFree(aij->rowvalues); 8000452661fSBarry Smith PetscFree(aij); 801a5a9c739SBarry Smith PLogObjectDestroy(mat); 8020452661fSBarry Smith PetscHeaderDestroy(mat); 8033a40ed3dSBarry Smith PetscFunctionReturn(0); 8041eb62cbbSBarry Smith } 805ee50ffe9SBarry Smith 8065615d1e5SSatish Balay #undef __FUNC__ 807d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_Binary" 8088f6be9afSLois Curfman McInnes extern int MatView_MPIAIJ_Binary(Mat mat,Viewer viewer) 8091eb62cbbSBarry Smith { 810416022c9SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 811416022c9SBarry Smith int ierr; 812416022c9SBarry Smith 8133a40ed3dSBarry Smith PetscFunctionBegin; 81417699dbbSLois Curfman McInnes if (aij->size == 1) { 815416022c9SBarry Smith ierr = MatView(aij->A,viewer); CHKERRQ(ierr); 816416022c9SBarry Smith } 817a8c6a408SBarry Smith else SETERRQ(PETSC_ERR_SUP,0,"Only uniprocessor output supported"); 8183a40ed3dSBarry Smith PetscFunctionReturn(0); 819416022c9SBarry Smith } 820416022c9SBarry Smith 8215615d1e5SSatish Balay #undef __FUNC__ 822d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ_ASCIIorDraworMatlab" 8238f6be9afSLois Curfman McInnes extern int MatView_MPIAIJ_ASCIIorDraworMatlab(Mat mat,Viewer viewer) 824416022c9SBarry Smith { 82544a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 826dbb450caSBarry Smith Mat_SeqAIJ* C = (Mat_SeqAIJ*)aij->A->data; 827a56f8943SBarry Smith int ierr, format,shift = C->indexshift,rank; 828d636dbe3SBarry Smith FILE *fd; 82919bcc07fSBarry Smith ViewerType vtype; 830416022c9SBarry Smith 8313a40ed3dSBarry Smith PetscFunctionBegin; 83219bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 83319bcc07fSBarry Smith if (vtype == ASCII_FILES_VIEWER || vtype == ASCII_FILE_VIEWER) { 83490ace30eSBarry Smith ierr = ViewerGetFormat(viewer,&format); 8350a198c4cSBarry Smith if (format == VIEWER_FORMAT_ASCII_INFO_LONG) { 8364e220ebcSLois Curfman McInnes MatInfo info; 8374e220ebcSLois Curfman McInnes int flg; 838a56f8943SBarry Smith MPI_Comm_rank(mat->comm,&rank); 83990ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 8404e220ebcSLois Curfman McInnes ierr = MatGetInfo(mat,MAT_LOCAL,&info); 84195e01e2fSLois Curfman McInnes ierr = OptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg); CHKERRQ(ierr); 84277c4ece6SBarry Smith PetscSequentialPhaseBegin(mat->comm,1); 84395e01e2fSLois Curfman McInnes if (flg) fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n", 8444e220ebcSLois Curfman McInnes rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory); 84595e01e2fSLois Curfman McInnes else fprintf(fd,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n", 8464e220ebcSLois Curfman McInnes rank,aij->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory); 8474e220ebcSLois Curfman McInnes ierr = MatGetInfo(aij->A,MAT_LOCAL,&info); 8484e220ebcSLois Curfman McInnes fprintf(fd,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used); 8494e220ebcSLois Curfman McInnes ierr = MatGetInfo(aij->B,MAT_LOCAL,&info); 8504e220ebcSLois Curfman McInnes fprintf(fd,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used); 851a56f8943SBarry Smith fflush(fd); 85277c4ece6SBarry Smith PetscSequentialPhaseEnd(mat->comm,1); 853a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer); CHKERRQ(ierr); 8543a40ed3dSBarry Smith PetscFunctionReturn(0); 8553a40ed3dSBarry Smith } else if (format == VIEWER_FORMAT_ASCII_INFO) { 8563a40ed3dSBarry Smith PetscFunctionReturn(0); 85708480c60SBarry Smith } 858416022c9SBarry Smith } 859416022c9SBarry Smith 86019bcc07fSBarry Smith if (vtype == DRAW_VIEWER) { 86119bcc07fSBarry Smith Draw draw; 86219bcc07fSBarry Smith PetscTruth isnull; 86319bcc07fSBarry Smith ierr = ViewerDrawGetDraw(viewer,&draw); CHKERRQ(ierr); 8643a40ed3dSBarry Smith ierr = DrawIsNull(draw,&isnull); CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 86519bcc07fSBarry Smith } 86619bcc07fSBarry Smith 86719bcc07fSBarry Smith if (vtype == ASCII_FILE_VIEWER) { 86890ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 86977c4ece6SBarry Smith PetscSequentialPhaseBegin(mat->comm,1); 870d13ab20cSBarry Smith fprintf(fd,"[%d] rows %d starts %d ends %d cols %d starts %d ends %d\n", 87117699dbbSLois Curfman McInnes aij->rank,aij->m,aij->rstart,aij->rend,aij->n,aij->cstart, 8721eb62cbbSBarry Smith aij->cend); 87378b31e54SBarry Smith ierr = MatView(aij->A,viewer); CHKERRQ(ierr); 87478b31e54SBarry Smith ierr = MatView(aij->B,viewer); CHKERRQ(ierr); 875d13ab20cSBarry Smith fflush(fd); 87677c4ece6SBarry Smith PetscSequentialPhaseEnd(mat->comm,1); 8773a40ed3dSBarry Smith } else { 878a56f8943SBarry Smith int size = aij->size; 879a56f8943SBarry Smith rank = aij->rank; 88017699dbbSLois Curfman McInnes if (size == 1) { 88178b31e54SBarry Smith ierr = MatView(aij->A,viewer); CHKERRQ(ierr); 8823a40ed3dSBarry Smith } else { 88395373324SBarry Smith /* assemble the entire matrix onto first processor. */ 88495373324SBarry Smith Mat A; 885ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 8862eb8c8abSBarry Smith int M = aij->M, N = aij->N,m,*ai,*aj,row,*cols,i,*ct; 88795373324SBarry Smith Scalar *a; 8882ee70a88SLois Curfman McInnes 88917699dbbSLois Curfman McInnes if (!rank) { 89055843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 8913a40ed3dSBarry Smith } else { 89255843e3eSBarry Smith ierr = MatCreateMPIAIJ(mat->comm,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);CHKERRQ(ierr); 89395373324SBarry Smith } 894464493b3SBarry Smith PLogObjectParent(mat,A); 895416022c9SBarry Smith 89695373324SBarry Smith /* copy over the A part */ 897ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->A->data; 8982ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 89995373324SBarry Smith row = aij->rstart; 900dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += aij->cstart + shift;} 90195373324SBarry Smith for ( i=0; i<m; i++ ) { 902416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 90395373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 90495373324SBarry Smith } 9052ee70a88SLois Curfman McInnes aj = Aloc->j; 906dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= aij->cstart + shift;} 90795373324SBarry Smith 90895373324SBarry Smith /* copy over the B part */ 909ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) aij->B->data; 9102ee70a88SLois Curfman McInnes m = Aloc->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 91195373324SBarry Smith row = aij->rstart; 9120452661fSBarry Smith ct = cols = (int *) PetscMalloc( (ai[m]+1)*sizeof(int) ); CHKPTRQ(cols); 913dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = aij->garray[aj[i]+shift];} 91495373324SBarry Smith for ( i=0; i<m; i++ ) { 915416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 91695373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 91795373324SBarry Smith } 9180452661fSBarry Smith PetscFree(ct); 9196d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 9206d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 92155843e3eSBarry Smith /* 92255843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 92355843e3eSBarry Smith synchronized across all processors that share the Draw object 92455843e3eSBarry Smith */ 92555843e3eSBarry Smith if (!rank || vtype == DRAW_VIEWER) { 92678b31e54SBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,viewer); CHKERRQ(ierr); 92795373324SBarry Smith } 92878b31e54SBarry Smith ierr = MatDestroy(A); CHKERRQ(ierr); 92995373324SBarry Smith } 93095373324SBarry Smith } 9313a40ed3dSBarry Smith PetscFunctionReturn(0); 9321eb62cbbSBarry Smith } 9331eb62cbbSBarry Smith 9345615d1e5SSatish Balay #undef __FUNC__ 935d4bb536fSBarry Smith #define __FUNC__ "MatView_MPIAIJ" 936e1311b90SBarry Smith int MatView_MPIAIJ(Mat mat,Viewer viewer) 937416022c9SBarry Smith { 938416022c9SBarry Smith int ierr; 93919bcc07fSBarry Smith ViewerType vtype; 940416022c9SBarry Smith 9413a40ed3dSBarry Smith PetscFunctionBegin; 94219bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 94319bcc07fSBarry Smith if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER || 94419bcc07fSBarry Smith vtype == DRAW_VIEWER || vtype == MATLAB_VIEWER) { 945d7e8b826SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworMatlab(mat,viewer); CHKERRQ(ierr); 9465cd90555SBarry Smith } else if (vtype == BINARY_FILE_VIEWER) { 9473a40ed3dSBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 9485cd90555SBarry Smith } else { 9495cd90555SBarry Smith SETERRQ(1,1,"Viewer type not supported by PETSc object"); 950416022c9SBarry Smith } 9513a40ed3dSBarry Smith PetscFunctionReturn(0); 952416022c9SBarry Smith } 953416022c9SBarry Smith 9541eb62cbbSBarry Smith /* 9551eb62cbbSBarry Smith This has to provide several versions. 9561eb62cbbSBarry Smith 9571eb62cbbSBarry Smith 2) a) use only local smoothing updating outer values only once. 9581eb62cbbSBarry Smith b) local smoothing updating outer values each inner iteration 959d6dfbf8fSBarry Smith 3) color updating out values betwen colors. 9601eb62cbbSBarry Smith */ 9615615d1e5SSatish Balay #undef __FUNC__ 9625615d1e5SSatish Balay #define __FUNC__ "MatRelax_MPIAIJ" 9638f6be9afSLois Curfman McInnes int MatRelax_MPIAIJ(Mat matin,Vec bb,double omega,MatSORType flag, 964dbb450caSBarry Smith double fshift,int its,Vec xx) 9658a729477SBarry Smith { 96644a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 967d6dfbf8fSBarry Smith Mat AA = mat->A, BB = mat->B; 968ec8511deSBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ *) AA->data, *B = (Mat_SeqAIJ *)BB->data; 969c16cb8f2SBarry Smith Scalar *b,*x,*xs,*ls,d,*v,sum; 9706abc6512SBarry Smith int ierr,*idx, *diag; 971416022c9SBarry Smith int n = mat->n, m = mat->m, i,shift = A->indexshift; 9728a729477SBarry Smith 9733a40ed3dSBarry Smith PetscFunctionBegin; 974d6dfbf8fSBarry Smith VecGetArray(xx,&x); VecGetArray(bb,&b); VecGetArray(mat->lvec,&ls); 975dbb450caSBarry Smith xs = x + shift; /* shift by one for index start of 1 */ 976dbb450caSBarry Smith ls = ls + shift; 97783e2fdc7SBarry Smith if (!A->diag) {ierr = MatMarkDiag_SeqAIJ(AA); CHKERRQ(ierr);} 978d6dfbf8fSBarry Smith diag = A->diag; 979c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 980da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 981f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 9823a40ed3dSBarry Smith PetscFunctionReturn(0); 983da3a660dSBarry Smith } 9843a40ed3dSBarry Smith ierr=VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 9853a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 986d6dfbf8fSBarry Smith while (its--) { 987d6dfbf8fSBarry Smith /* go down through the rows */ 988d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 989d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 9904d197716SBarry Smith PLogFlops(4*n+3); 991dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 992dbb450caSBarry Smith v = A->a + A->i[i] + shift; 993d6dfbf8fSBarry Smith sum = b[i]; 994d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 995dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 996d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 997dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 998dbb450caSBarry Smith v = B->a + B->i[i] + shift; 999d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 100055a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 1001d6dfbf8fSBarry Smith } 1002d6dfbf8fSBarry Smith /* come up through the rows */ 1003d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 1004d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 10054d197716SBarry Smith PLogFlops(4*n+3) 1006dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 1007dbb450caSBarry Smith v = A->a + A->i[i] + shift; 1008d6dfbf8fSBarry Smith sum = b[i]; 1009d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1010dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 1011d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 1012dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 1013dbb450caSBarry Smith v = B->a + B->i[i] + shift; 1014d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 101555a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 1016d6dfbf8fSBarry Smith } 1017d6dfbf8fSBarry Smith } 10183a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1019da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 1020f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 10213a40ed3dSBarry Smith PetscFunctionReturn(0); 1022da3a660dSBarry Smith } 10233a40ed3dSBarry Smith ierr=VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10243a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 1025d6dfbf8fSBarry Smith while (its--) { 1026d6dfbf8fSBarry Smith for ( i=0; i<m; i++ ) { 1027d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 10284d197716SBarry Smith PLogFlops(4*n+3); 1029dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 1030dbb450caSBarry Smith v = A->a + A->i[i] + shift; 1031d6dfbf8fSBarry Smith sum = b[i]; 1032d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1033dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 1034d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 1035dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 1036dbb450caSBarry Smith v = B->a + B->i[i] + shift; 1037d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 103855a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 1039d6dfbf8fSBarry Smith } 1040d6dfbf8fSBarry Smith } 10413a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1042da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 1043f830108cSBarry Smith ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,its,xx);CHKERRQ(ierr); 10443a40ed3dSBarry Smith PetscFunctionReturn(0); 1045da3a660dSBarry Smith } 104643a90d84SBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD, 104778b31e54SBarry Smith mat->Mvctx); CHKERRQ(ierr); 104843a90d84SBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD, 104978b31e54SBarry Smith mat->Mvctx); CHKERRQ(ierr); 1050d6dfbf8fSBarry Smith while (its--) { 1051d6dfbf8fSBarry Smith for ( i=m-1; i>-1; i-- ) { 1052d6dfbf8fSBarry Smith n = A->i[i+1] - A->i[i]; 10534d197716SBarry Smith PLogFlops(4*n+3); 1054dbb450caSBarry Smith idx = A->j + A->i[i] + shift; 1055dbb450caSBarry Smith v = A->a + A->i[i] + shift; 1056d6dfbf8fSBarry Smith sum = b[i]; 1057d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,xs,v,idx,n); 1058dbb450caSBarry Smith d = fshift + A->a[diag[i]+shift]; 1059d6dfbf8fSBarry Smith n = B->i[i+1] - B->i[i]; 1060dbb450caSBarry Smith idx = B->j + B->i[i] + shift; 1061dbb450caSBarry Smith v = B->a + B->i[i] + shift; 1062d6dfbf8fSBarry Smith SPARSEDENSEMDOT(sum,ls,v,idx,n); 106355a1b374SBarry Smith x[i] = (1. - omega)*x[i] + omega*(sum + A->a[diag[i]+shift]*x[i])/d; 1064d6dfbf8fSBarry Smith } 1065d6dfbf8fSBarry Smith } 10663a40ed3dSBarry Smith } else { 1067a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Parallel SOR not supported"); 1068c16cb8f2SBarry Smith } 10693a40ed3dSBarry Smith PetscFunctionReturn(0); 10708a729477SBarry Smith } 1071a66be287SLois Curfman McInnes 10725615d1e5SSatish Balay #undef __FUNC__ 1073d4bb536fSBarry Smith #define __FUNC__ "MatGetInfo_MPIAIJ" 10748f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1075a66be287SLois Curfman McInnes { 1076a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 1077a66be287SLois Curfman McInnes Mat A = mat->A, B = mat->B; 10784e220ebcSLois Curfman McInnes int ierr; 10794e220ebcSLois Curfman McInnes double isend[5], irecv[5]; 1080a66be287SLois Curfman McInnes 10813a40ed3dSBarry Smith PetscFunctionBegin; 10824e220ebcSLois Curfman McInnes info->block_size = 1.0; 10834e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info); CHKERRQ(ierr); 10844e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 10854e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 10864e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info); CHKERRQ(ierr); 10874e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 10884e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1089a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 10904e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 10914e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 10924e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 10934e220ebcSLois Curfman McInnes info->memory = isend[3]; 10944e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1095a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1096ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);CHKERRQ(ierr); 10974e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 10984e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 10994e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 11004e220ebcSLois Curfman McInnes info->memory = irecv[3]; 11014e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1102a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1103ca161407SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);CHKERRQ(ierr); 11044e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 11054e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 11064e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 11074e220ebcSLois Curfman McInnes info->memory = irecv[3]; 11084e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1109a66be287SLois Curfman McInnes } 11104e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 11114e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 11124e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 11138d700155SBarry Smith info->rows_global = (double)mat->M; 11148d700155SBarry Smith info->columns_global = (double)mat->N; 11158d700155SBarry Smith info->rows_local = (double)mat->m; 11168d700155SBarry Smith info->columns_local = (double)mat->N; 11174e220ebcSLois Curfman McInnes 11183a40ed3dSBarry Smith PetscFunctionReturn(0); 1119a66be287SLois Curfman McInnes } 1120a66be287SLois Curfman McInnes 11215615d1e5SSatish Balay #undef __FUNC__ 1122d4bb536fSBarry Smith #define __FUNC__ "MatSetOption_MPIAIJ" 11238f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op) 1124c74985f6SBarry Smith { 1125c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 1126c74985f6SBarry Smith 11273a40ed3dSBarry Smith PetscFunctionBegin; 11286d4a8577SBarry Smith if (op == MAT_NO_NEW_NONZERO_LOCATIONS || 11296d4a8577SBarry Smith op == MAT_YES_NEW_NONZERO_LOCATIONS || 11306da5968aSLois Curfman McInnes op == MAT_COLUMNS_UNSORTED || 1131c2653b3dSLois Curfman McInnes op == MAT_COLUMNS_SORTED || 113296854ed6SLois Curfman McInnes op == MAT_NEW_NONZERO_ALLOCATION_ERROR || 1133c2653b3dSLois Curfman McInnes op == MAT_NEW_NONZERO_LOCATION_ERROR) { 1134b1fbbac0SLois Curfman McInnes MatSetOption(a->A,op); 1135b1fbbac0SLois Curfman McInnes MatSetOption(a->B,op); 1136b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROW_ORIENTED) { 1137aeafbbfcSLois Curfman McInnes a->roworiented = 1; 1138c0bbcb79SLois Curfman McInnes MatSetOption(a->A,op); 1139c0bbcb79SLois Curfman McInnes MatSetOption(a->B,op); 1140b1fbbac0SLois Curfman McInnes } else if (op == MAT_ROWS_SORTED || 11416da5968aSLois Curfman McInnes op == MAT_ROWS_UNSORTED || 11426d4a8577SBarry Smith op == MAT_SYMMETRIC || 11436d4a8577SBarry Smith op == MAT_STRUCTURALLY_SYMMETRIC || 11446d4a8577SBarry Smith op == MAT_YES_NEW_DIAGONALS) 1145981c4779SBarry Smith PLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n"); 11466d4a8577SBarry Smith else if (op == MAT_COLUMN_ORIENTED) { 11474b0e389bSBarry Smith a->roworiented = 0; 11484b0e389bSBarry Smith MatSetOption(a->A,op); 11494b0e389bSBarry Smith MatSetOption(a->B,op); 11502b362799SSatish Balay } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) { 115190f02eecSBarry Smith a->donotstash = 1; 11523a40ed3dSBarry Smith } else if (op == MAT_NO_NEW_DIAGONALS){ 11533a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"MAT_NO_NEW_DIAGONALS"); 11543a40ed3dSBarry Smith } else { 11553a40ed3dSBarry Smith SETERRQ(PETSC_ERR_SUP,0,"unknown option"); 11563a40ed3dSBarry Smith } 11573a40ed3dSBarry Smith PetscFunctionReturn(0); 1158c74985f6SBarry Smith } 1159c74985f6SBarry Smith 11605615d1e5SSatish Balay #undef __FUNC__ 1161d4bb536fSBarry Smith #define __FUNC__ "MatGetSize_MPIAIJ" 11628f6be9afSLois Curfman McInnes int MatGetSize_MPIAIJ(Mat matin,int *m,int *n) 1163c74985f6SBarry Smith { 116444a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 11653a40ed3dSBarry Smith 11663a40ed3dSBarry Smith PetscFunctionBegin; 11670752156aSBarry Smith if (m) *m = mat->M; 11680752156aSBarry Smith if (n) *n = mat->N; 11693a40ed3dSBarry Smith PetscFunctionReturn(0); 1170c74985f6SBarry Smith } 1171c74985f6SBarry Smith 11725615d1e5SSatish Balay #undef __FUNC__ 1173d4bb536fSBarry Smith #define __FUNC__ "MatGetLocalSize_MPIAIJ" 11748f6be9afSLois Curfman McInnes int MatGetLocalSize_MPIAIJ(Mat matin,int *m,int *n) 1175c74985f6SBarry Smith { 117644a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 11773a40ed3dSBarry Smith 11783a40ed3dSBarry Smith PetscFunctionBegin; 11790752156aSBarry Smith if (m) *m = mat->m; 1180f830108cSBarry Smith if (n) *n = mat->n; 11813a40ed3dSBarry Smith PetscFunctionReturn(0); 1182c74985f6SBarry Smith } 1183c74985f6SBarry Smith 11845615d1e5SSatish Balay #undef __FUNC__ 1185d4bb536fSBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPIAIJ" 11868f6be9afSLois Curfman McInnes int MatGetOwnershipRange_MPIAIJ(Mat matin,int *m,int *n) 1187c74985f6SBarry Smith { 118844a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 11893a40ed3dSBarry Smith 11903a40ed3dSBarry Smith PetscFunctionBegin; 1191c74985f6SBarry Smith *m = mat->rstart; *n = mat->rend; 11923a40ed3dSBarry Smith PetscFunctionReturn(0); 1193c74985f6SBarry Smith } 1194c74985f6SBarry Smith 11956d84be18SBarry Smith extern int MatGetRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 11966d84be18SBarry Smith extern int MatRestoreRow_SeqAIJ(Mat,int,int*,int**,Scalar**); 11976d84be18SBarry Smith 11985615d1e5SSatish Balay #undef __FUNC__ 11995615d1e5SSatish Balay #define __FUNC__ "MatGetRow_MPIAIJ" 12006d84be18SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v) 120139e00950SLois Curfman McInnes { 1202154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ *) matin->data; 120370f0671dSBarry Smith Scalar *vworkA, *vworkB, **pvA, **pvB,*v_p; 1204154123eaSLois Curfman McInnes int i, ierr, *cworkA, *cworkB, **pcA, **pcB, cstart = mat->cstart; 1205154123eaSLois Curfman McInnes int nztot, nzA, nzB, lrow, rstart = mat->rstart, rend = mat->rend; 120670f0671dSBarry Smith int *cmap, *idx_p; 120739e00950SLois Curfman McInnes 12083a40ed3dSBarry Smith PetscFunctionBegin; 1209a8c6a408SBarry Smith if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Already active"); 12107a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 12117a0afa10SBarry Smith 121270f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 12137a0afa10SBarry Smith /* 12147a0afa10SBarry Smith allocate enough space to hold information from the longest row. 12157a0afa10SBarry Smith */ 12167a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ *) mat->A->data,*Ba = (Mat_SeqAIJ *) mat->B->data; 1217c16cb8f2SBarry Smith int max = 1,m = mat->m,tmp; 1218c16cb8f2SBarry Smith for ( i=0; i<m; i++ ) { 12197a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 12207a0afa10SBarry Smith if (max < tmp) { max = tmp; } 12217a0afa10SBarry Smith } 12223f97c4b0SBarry Smith mat->rowvalues = (Scalar *) PetscMalloc(max*(sizeof(int)+sizeof(Scalar)));CHKPTRQ(mat->rowvalues); 12237a0afa10SBarry Smith mat->rowindices = (int *) (mat->rowvalues + max); 12247a0afa10SBarry Smith } 12257a0afa10SBarry Smith 1226a8c6a408SBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Only local rows") 1227abc0e9e4SLois Curfman McInnes lrow = row - rstart; 122839e00950SLois Curfman McInnes 1229154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1230154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1231154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1232f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr); 1233f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr); 1234154123eaSLois Curfman McInnes nztot = nzA + nzB; 1235154123eaSLois Curfman McInnes 123670f0671dSBarry Smith cmap = mat->garray; 1237154123eaSLois Curfman McInnes if (v || idx) { 1238154123eaSLois Curfman McInnes if (nztot) { 1239154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 124070f0671dSBarry Smith int imark = -1; 1241154123eaSLois Curfman McInnes if (v) { 124270f0671dSBarry Smith *v = v_p = mat->rowvalues; 124339e00950SLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 124470f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1245154123eaSLois Curfman McInnes else break; 1246154123eaSLois Curfman McInnes } 1247154123eaSLois Curfman McInnes imark = i; 124870f0671dSBarry Smith for ( i=0; i<nzA; i++ ) v_p[imark+i] = vworkA[i]; 124970f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) v_p[nzA+i] = vworkB[i]; 1250154123eaSLois Curfman McInnes } 1251154123eaSLois Curfman McInnes if (idx) { 125270f0671dSBarry Smith *idx = idx_p = mat->rowindices; 125370f0671dSBarry Smith if (imark > -1) { 125470f0671dSBarry Smith for ( i=0; i<imark; i++ ) { 125570f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 125670f0671dSBarry Smith } 125770f0671dSBarry Smith } else { 1258154123eaSLois Curfman McInnes for ( i=0; i<nzB; i++ ) { 125970f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1260154123eaSLois Curfman McInnes else break; 1261154123eaSLois Curfman McInnes } 1262154123eaSLois Curfman McInnes imark = i; 126370f0671dSBarry Smith } 126470f0671dSBarry Smith for ( i=0; i<nzA; i++ ) idx_p[imark+i] = cstart + cworkA[i]; 126570f0671dSBarry Smith for ( i=imark; i<nzB; i++ ) idx_p[nzA+i] = cmap[cworkB[i]]; 126639e00950SLois Curfman McInnes } 12673f97c4b0SBarry Smith } else { 12681ca473b0SSatish Balay if (idx) *idx = 0; 12691ca473b0SSatish Balay if (v) *v = 0; 12701ca473b0SSatish Balay } 1271154123eaSLois Curfman McInnes } 127239e00950SLois Curfman McInnes *nz = nztot; 1273f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA); CHKERRQ(ierr); 1274f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB); CHKERRQ(ierr); 12753a40ed3dSBarry Smith PetscFunctionReturn(0); 127639e00950SLois Curfman McInnes } 127739e00950SLois Curfman McInnes 12785615d1e5SSatish Balay #undef __FUNC__ 1279d4bb536fSBarry Smith #define __FUNC__ "MatRestoreRow_MPIAIJ" 12806d84be18SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v) 128139e00950SLois Curfman McInnes { 12827a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 12833a40ed3dSBarry Smith 12843a40ed3dSBarry Smith PetscFunctionBegin; 12857a0afa10SBarry Smith if (aij->getrowactive == PETSC_FALSE) { 1286a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"MatGetRow not called"); 12877a0afa10SBarry Smith } 12887a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 12893a40ed3dSBarry Smith PetscFunctionReturn(0); 129039e00950SLois Curfman McInnes } 129139e00950SLois Curfman McInnes 12925615d1e5SSatish Balay #undef __FUNC__ 12935615d1e5SSatish Balay #define __FUNC__ "MatNorm_MPIAIJ" 12948f6be9afSLois Curfman McInnes int MatNorm_MPIAIJ(Mat mat,NormType type,double *norm) 1295855ac2c5SLois Curfman McInnes { 1296855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 1297ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*) aij->A->data, *bmat = (Mat_SeqAIJ*) aij->B->data; 1298416022c9SBarry Smith int ierr, i, j, cstart = aij->cstart,shift = amat->indexshift; 1299416022c9SBarry Smith double sum = 0.0; 130004ca555eSLois Curfman McInnes Scalar *v; 130104ca555eSLois Curfman McInnes 13023a40ed3dSBarry Smith PetscFunctionBegin; 130317699dbbSLois Curfman McInnes if (aij->size == 1) { 130414183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm); CHKERRQ(ierr); 130537fa93a5SLois Curfman McInnes } else { 130604ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 130704ca555eSLois Curfman McInnes v = amat->a; 130804ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++ ) { 13093a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 1310e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 131104ca555eSLois Curfman McInnes #else 131204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 131304ca555eSLois Curfman McInnes #endif 131404ca555eSLois Curfman McInnes } 131504ca555eSLois Curfman McInnes v = bmat->a; 131604ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++ ) { 13173a40ed3dSBarry Smith #if defined(USE_PETSC_COMPLEX) 1318e20fef11SSatish Balay sum += PetscReal(PetscConj(*v)*(*v)); v++; 131904ca555eSLois Curfman McInnes #else 132004ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 132104ca555eSLois Curfman McInnes #endif 132204ca555eSLois Curfman McInnes } 1323ca161407SBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 132404ca555eSLois Curfman McInnes *norm = sqrt(*norm); 13253a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 132604ca555eSLois Curfman McInnes double *tmp, *tmp2; 132704ca555eSLois Curfman McInnes int *jj, *garray = aij->garray; 1328758f045eSSatish Balay tmp = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp); 1329758f045eSSatish Balay tmp2 = (double *) PetscMalloc( (aij->N+1)*sizeof(double) ); CHKPTRQ(tmp2); 1330cddf8d76SBarry Smith PetscMemzero(tmp,aij->N*sizeof(double)); 133104ca555eSLois Curfman McInnes *norm = 0.0; 133204ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 133304ca555eSLois Curfman McInnes for ( j=0; j<amat->nz; j++ ) { 1334579c6b6fSBarry Smith tmp[cstart + *jj++ + shift] += PetscAbsScalar(*v); v++; 133504ca555eSLois Curfman McInnes } 133604ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 133704ca555eSLois Curfman McInnes for ( j=0; j<bmat->nz; j++ ) { 1338579c6b6fSBarry Smith tmp[garray[*jj++ + shift]] += PetscAbsScalar(*v); v++; 133904ca555eSLois Curfman McInnes } 1340ca161407SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,aij->N,MPI_DOUBLE,MPI_SUM,mat->comm);CHKERRQ(ierr); 134104ca555eSLois Curfman McInnes for ( j=0; j<aij->N; j++ ) { 134204ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 134304ca555eSLois Curfman McInnes } 13440452661fSBarry Smith PetscFree(tmp); PetscFree(tmp2); 13453a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1346515d9167SLois Curfman McInnes double ntemp = 0.0; 134704ca555eSLois Curfman McInnes for ( j=0; j<amat->m; j++ ) { 1348dbb450caSBarry Smith v = amat->a + amat->i[j] + shift; 134904ca555eSLois Curfman McInnes sum = 0.0; 135004ca555eSLois Curfman McInnes for ( i=0; i<amat->i[j+1]-amat->i[j]; i++ ) { 1351cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 135204ca555eSLois Curfman McInnes } 1353dbb450caSBarry Smith v = bmat->a + bmat->i[j] + shift; 135404ca555eSLois Curfman McInnes for ( i=0; i<bmat->i[j+1]-bmat->i[j]; i++ ) { 1355cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 135604ca555eSLois Curfman McInnes } 1357515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 135804ca555eSLois Curfman McInnes } 1359ca161407SBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPI_DOUBLE,MPI_MAX,mat->comm);CHKERRQ(ierr); 1360ca161407SBarry Smith } else { 1361a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"No support for two norm"); 136204ca555eSLois Curfman McInnes } 136337fa93a5SLois Curfman McInnes } 13643a40ed3dSBarry Smith PetscFunctionReturn(0); 1365855ac2c5SLois Curfman McInnes } 1366855ac2c5SLois Curfman McInnes 13675615d1e5SSatish Balay #undef __FUNC__ 13685615d1e5SSatish Balay #define __FUNC__ "MatTranspose_MPIAIJ" 13698f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout) 1370b7c46309SBarry Smith { 1371b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *) A->data; 1372dbb450caSBarry Smith Mat_SeqAIJ *Aloc = (Mat_SeqAIJ *) a->A->data; 1373416022c9SBarry Smith int ierr,shift = Aloc->indexshift; 1374b7c46309SBarry Smith int M = a->M, N = a->N,m,*ai,*aj,row,*cols,i,*ct; 13753a40ed3dSBarry Smith Mat B; 1376b7c46309SBarry Smith Scalar *array; 1377b7c46309SBarry Smith 13783a40ed3dSBarry Smith PetscFunctionBegin; 1379d4bb536fSBarry Smith if (matout == PETSC_NULL && M != N) { 1380a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,0,"Square matrix only for in-place"); 1381d4bb536fSBarry Smith } 1382d4bb536fSBarry Smith 1383d4bb536fSBarry Smith ierr = MatCreateMPIAIJ(A->comm,a->n,a->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);CHKERRQ(ierr); 1384b7c46309SBarry Smith 1385b7c46309SBarry Smith /* copy over the A part */ 1386ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->A->data; 1387b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1388b7c46309SBarry Smith row = a->rstart; 1389dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] += a->cstart + shift;} 1390b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1391416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1392b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 1393b7c46309SBarry Smith } 1394b7c46309SBarry Smith aj = Aloc->j; 13954af08d9eSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {aj[i] -= a->cstart + shift;} 1396b7c46309SBarry Smith 1397b7c46309SBarry Smith /* copy over the B part */ 1398ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*) a->B->data; 1399b7c46309SBarry Smith m = Aloc->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1400b7c46309SBarry Smith row = a->rstart; 14010452661fSBarry Smith ct = cols = (int *) PetscMalloc( (1+ai[m]-shift)*sizeof(int) ); CHKPTRQ(cols); 1402dbb450caSBarry Smith for ( i=0; i<ai[m]+shift; i++ ) {cols[i] = a->garray[aj[i]+shift];} 1403b7c46309SBarry Smith for ( i=0; i<m; i++ ) { 1404416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1405b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 1406b7c46309SBarry Smith } 14070452661fSBarry Smith PetscFree(ct); 14086d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 14096d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 14103638b69dSLois Curfman McInnes if (matout != PETSC_NULL) { 14110de55854SLois Curfman McInnes *matout = B; 14120de55854SLois Curfman McInnes } else { 1413f830108cSBarry Smith PetscOps *Abops; 1414f830108cSBarry Smith struct _MatOps *Aops; 1415f830108cSBarry Smith 14160de55854SLois Curfman McInnes /* This isn't really an in-place transpose .... but free data structures from a */ 14170452661fSBarry Smith PetscFree(a->rowners); 14180de55854SLois Curfman McInnes ierr = MatDestroy(a->A); CHKERRQ(ierr); 14190de55854SLois Curfman McInnes ierr = MatDestroy(a->B); CHKERRQ(ierr); 14200452661fSBarry Smith if (a->colmap) PetscFree(a->colmap); 14210452661fSBarry Smith if (a->garray) PetscFree(a->garray); 14220de55854SLois Curfman McInnes if (a->lvec) VecDestroy(a->lvec); 1423a56f8943SBarry Smith if (a->Mvctx) VecScatterDestroy(a->Mvctx); 14240452661fSBarry Smith PetscFree(a); 1425f830108cSBarry Smith 1426f830108cSBarry Smith /* 1427f830108cSBarry Smith This is horrible, horrible code. We need to keep the 1428f830108cSBarry Smith A pointers for the bops and ops but copy everything 1429f830108cSBarry Smith else from C. 1430f830108cSBarry Smith */ 1431f830108cSBarry Smith Abops = A->bops; 1432f830108cSBarry Smith Aops = A->ops; 1433f09e8eb9SSatish Balay PetscMemcpy(A,B,sizeof(struct _p_Mat)); 1434f830108cSBarry Smith A->bops = Abops; 1435f830108cSBarry Smith A->ops = Aops; 14360452661fSBarry Smith PetscHeaderDestroy(B); 14370de55854SLois Curfman McInnes } 14383a40ed3dSBarry Smith PetscFunctionReturn(0); 1439b7c46309SBarry Smith } 1440b7c46309SBarry Smith 14415615d1e5SSatish Balay #undef __FUNC__ 14425615d1e5SSatish Balay #define __FUNC__ "MatDiagonalScale_MPIAIJ" 14434b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1444a008b906SSatish Balay { 14454b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ *) mat->data; 14464b967eb1SSatish Balay Mat a = aij->A, b = aij->B; 1447a008b906SSatish Balay int ierr,s1,s2,s3; 1448a008b906SSatish Balay 14493a40ed3dSBarry Smith PetscFunctionBegin; 14504b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3); CHKERRQ(ierr); 14514b967eb1SSatish Balay if (rr) { 1452e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1453a8c6a408SBarry Smith if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,0,"right vector non-conforming local size"); 14544b967eb1SSatish Balay /* Overlap communication with computation. */ 145543a90d84SBarry Smith ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr); 1456a008b906SSatish Balay } 14574b967eb1SSatish Balay if (ll) { 1458e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1459a8c6a408SBarry Smith if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"left vector non-conforming local size"); 1460f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0); CHKERRQ(ierr); 14614b967eb1SSatish Balay } 14624b967eb1SSatish Balay /* scale the diagonal block */ 1463f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr); CHKERRQ(ierr); 14644b967eb1SSatish Balay 14654b967eb1SSatish Balay if (rr) { 14664b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 146743a90d84SBarry Smith ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx); CHKERRQ(ierr); 1468f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec); CHKERRQ(ierr); 14694b967eb1SSatish Balay } 14704b967eb1SSatish Balay 14713a40ed3dSBarry Smith PetscFunctionReturn(0); 1472a008b906SSatish Balay } 1473a008b906SSatish Balay 1474a008b906SSatish Balay 1475682d7d0cSBarry Smith extern int MatPrintHelp_SeqAIJ(Mat); 14765615d1e5SSatish Balay #undef __FUNC__ 1477d4bb536fSBarry Smith #define __FUNC__ "MatPrintHelp_MPIAIJ" 14788f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A) 1479682d7d0cSBarry Smith { 1480682d7d0cSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 14813a40ed3dSBarry Smith int ierr; 1482682d7d0cSBarry Smith 14833a40ed3dSBarry Smith PetscFunctionBegin; 14843a40ed3dSBarry Smith if (!a->rank) { 14853a40ed3dSBarry Smith ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr); 14863a40ed3dSBarry Smith } 14873a40ed3dSBarry Smith PetscFunctionReturn(0); 1488682d7d0cSBarry Smith } 1489682d7d0cSBarry Smith 14905615d1e5SSatish Balay #undef __FUNC__ 1491d4bb536fSBarry Smith #define __FUNC__ "MatGetBlockSize_MPIAIJ" 14928f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs) 14935a838052SSatish Balay { 14943a40ed3dSBarry Smith PetscFunctionBegin; 14955a838052SSatish Balay *bs = 1; 14963a40ed3dSBarry Smith PetscFunctionReturn(0); 14975a838052SSatish Balay } 14985615d1e5SSatish Balay #undef __FUNC__ 1499d4bb536fSBarry Smith #define __FUNC__ "MatSetUnfactored_MPIAIJ" 15008f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A) 1501bb5a7306SBarry Smith { 1502bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 1503bb5a7306SBarry Smith int ierr; 15043a40ed3dSBarry Smith 15053a40ed3dSBarry Smith PetscFunctionBegin; 1506bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A); CHKERRQ(ierr); 15073a40ed3dSBarry Smith PetscFunctionReturn(0); 1508bb5a7306SBarry Smith } 1509bb5a7306SBarry Smith 1510d4bb536fSBarry Smith #undef __FUNC__ 1511d4bb536fSBarry Smith #define __FUNC__ "MatEqual_MPIAIJ" 1512d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A, Mat B, PetscTruth *flag) 1513d4bb536fSBarry Smith { 1514d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ *) B->data,*matA = (Mat_MPIAIJ *) A->data; 1515d4bb536fSBarry Smith Mat a, b, c, d; 1516d4bb536fSBarry Smith PetscTruth flg; 1517d4bb536fSBarry Smith int ierr; 1518d4bb536fSBarry Smith 15193a40ed3dSBarry Smith PetscFunctionBegin; 1520a8c6a408SBarry Smith if (B->type != MATMPIAIJ) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type"); 1521d4bb536fSBarry Smith a = matA->A; b = matA->B; 1522d4bb536fSBarry Smith c = matB->A; d = matB->B; 1523d4bb536fSBarry Smith 1524d4bb536fSBarry Smith ierr = MatEqual(a, c, &flg); CHKERRQ(ierr); 1525d4bb536fSBarry Smith if (flg == PETSC_TRUE) { 1526d4bb536fSBarry Smith ierr = MatEqual(b, d, &flg); CHKERRQ(ierr); 1527d4bb536fSBarry Smith } 1528ca161407SBarry Smith ierr = MPI_Allreduce(&flg, flag, 1, MPI_INT, MPI_LAND, A->comm);CHKERRQ(ierr); 15293a40ed3dSBarry Smith PetscFunctionReturn(0); 1530d4bb536fSBarry Smith } 1531d4bb536fSBarry Smith 1532*cb5b572fSBarry Smith #undef __FUNC__ 1533*cb5b572fSBarry Smith #define __FUNC__ "MatCopy_MPIAIJ" 1534*cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1535*cb5b572fSBarry Smith { 1536*cb5b572fSBarry Smith int ierr; 1537*cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1538*cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1539*cb5b572fSBarry Smith 1540*cb5b572fSBarry Smith PetscFunctionBegin; 1541*cb5b572fSBarry Smith if (str != SAME_NONZERO_PATTERN || B->type != MATMPIAIJ) { 1542*cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1543*cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1544*cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1545*cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1546*cb5b572fSBarry Smith then copying the submatrices */ 1547*cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1548*cb5b572fSBarry Smith } else { 1549*cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1550*cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1551*cb5b572fSBarry Smith } 1552*cb5b572fSBarry Smith PetscFunctionReturn(0); 1553*cb5b572fSBarry Smith } 1554*cb5b572fSBarry Smith 15558f6be9afSLois Curfman McInnes extern int MatConvertSameType_MPIAIJ(Mat,Mat *,int); 15562f86bd48SSatish Balay extern int MatIncreaseOverlap_MPIAIJ(Mat , int, IS *, int); 15570a198c4cSBarry Smith extern int MatFDColoringCreate_MPIAIJ(Mat,ISColoring,MatFDColoring); 15580a198c4cSBarry Smith extern int MatGetSubMatrices_MPIAIJ (Mat ,int , IS *,IS *,MatGetSubMatrixCall,Mat **); 15596a6a5d1dSBarry Smith extern int MatGetSubMatrix_MPIAIJ (Mat ,IS,IS,int,MatGetSubMatrixCall,Mat *); 156000e6dbe6SBarry Smith 15618a729477SBarry Smith /* -------------------------------------------------------------------*/ 1562cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 1563cda55fadSBarry Smith MatGetRow_MPIAIJ, 1564cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 1565cda55fadSBarry Smith MatMult_MPIAIJ, 1566cda55fadSBarry Smith MatMultAdd_MPIAIJ, 1567cda55fadSBarry Smith MatMultTrans_MPIAIJ, 1568cda55fadSBarry Smith MatMultTransAdd_MPIAIJ, 1569cda55fadSBarry Smith 0, 1570cda55fadSBarry Smith 0, 1571cda55fadSBarry Smith 0, 1572cda55fadSBarry Smith 0, 1573cda55fadSBarry Smith 0, 1574cda55fadSBarry Smith 0, 157544a69424SLois Curfman McInnes MatRelax_MPIAIJ, 1576b7c46309SBarry Smith MatTranspose_MPIAIJ, 1577cda55fadSBarry Smith MatGetInfo_MPIAIJ, 1578cda55fadSBarry Smith MatEqual_MPIAIJ, 1579cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 1580cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 1581cda55fadSBarry Smith MatNorm_MPIAIJ, 1582cda55fadSBarry Smith MatAssemblyBegin_MPIAIJ, 1583cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 15841eb62cbbSBarry Smith 0, 1585cda55fadSBarry Smith MatSetOption_MPIAIJ, 1586cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 1587cda55fadSBarry Smith MatZeroRows_MPIAIJ, 1588cda55fadSBarry Smith 0, 1589cda55fadSBarry Smith 0, 1590cda55fadSBarry Smith 0, 1591cda55fadSBarry Smith 0, 1592cda55fadSBarry Smith MatGetSize_MPIAIJ, 1593cda55fadSBarry Smith MatGetLocalSize_MPIAIJ, 1594cda55fadSBarry Smith MatGetOwnershipRange_MPIAIJ, 1595cda55fadSBarry Smith 0, 1596cda55fadSBarry Smith 0, 1597cda55fadSBarry Smith 0, 1598cda55fadSBarry Smith 0, 1599cda55fadSBarry Smith MatConvertSameType_MPIAIJ, 1600cda55fadSBarry Smith 0, 1601cda55fadSBarry Smith 0, 1602cda55fadSBarry Smith 0, 1603cda55fadSBarry Smith 0, 1604cda55fadSBarry Smith 0, 1605cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 1606cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 1607cda55fadSBarry Smith MatGetValues_MPIAIJ, 1608*cb5b572fSBarry Smith MatCopy_MPIAIJ, 1609052efed2SBarry Smith MatPrintHelp_MPIAIJ, 1610cda55fadSBarry Smith MatScale_MPIAIJ, 1611cda55fadSBarry Smith 0, 1612cda55fadSBarry Smith 0, 1613cda55fadSBarry Smith 0, 1614cda55fadSBarry Smith MatGetBlockSize_MPIAIJ, 1615cda55fadSBarry Smith 0, 1616cda55fadSBarry Smith 0, 1617cda55fadSBarry Smith 0, 1618cda55fadSBarry Smith 0, 1619cda55fadSBarry Smith MatFDColoringCreate_MPIAIJ, 1620cda55fadSBarry Smith 0, 1621cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 1622cda55fadSBarry Smith 0, 1623cda55fadSBarry Smith 0, 1624cda55fadSBarry Smith MatGetSubMatrix_MPIAIJ, 1625cda55fadSBarry Smith 0, 1626cda55fadSBarry Smith 0, 1627cda55fadSBarry Smith MatGetMaps_Petsc}; 162836ce4990SBarry Smith 16298a729477SBarry Smith 16305615d1e5SSatish Balay #undef __FUNC__ 16315615d1e5SSatish Balay #define __FUNC__ "MatCreateMPIAIJ" 16321987afe7SBarry Smith /*@C 1633ff756334SLois Curfman McInnes MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 16343a511b96SLois Curfman McInnes (the default parallel PETSc format). For good matrix assembly performance 16353a511b96SLois Curfman McInnes the user should preallocate the matrix storage by setting the parameters 16363a511b96SLois Curfman McInnes d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 16373a511b96SLois Curfman McInnes performance can be increased by more than a factor of 50. 16388a729477SBarry Smith 1639db81eaa0SLois Curfman McInnes Collective on MPI_Comm 1640db81eaa0SLois Curfman McInnes 16418a729477SBarry Smith Input Parameters: 1642db81eaa0SLois Curfman McInnes + comm - MPI communicator 16437d3e4905SLois Curfman McInnes . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 164492e8d321SLois Curfman McInnes This value should be the same as the local size used in creating the 164592e8d321SLois Curfman McInnes y vector for the matrix-vector product y = Ax. 16461a3896d6SBarry Smith . n - This value should be the same as the local size used in creating the 16471a3896d6SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 16481a3896d6SBarry Smith calculated if N is given) For square matrices n is almost always m. 164960d380a7SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 165060d380a7SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 1651ab693e5aSLois Curfman McInnes . d_nz - number of nonzeros per row in diagonal portion of local submatrix 1652ff756334SLois Curfman McInnes (same for all local rows) 16532bd5e0b2SLois Curfman McInnes . d_nzz - array containing the number of nonzeros in the various rows of the 165492e8d321SLois Curfman McInnes diagonal portion of the local submatrix (possibly different for each row) 16552bd5e0b2SLois Curfman McInnes or PETSC_NULL. You must leave room for the diagonal entry even if 16562bd5e0b2SLois Curfman McInnes it is zero. 16572bd5e0b2SLois Curfman McInnes . o_nz - number of nonzeros per row in the off-diagonal portion of local 1658ab693e5aSLois Curfman McInnes submatrix (same for all local rows). 1659db81eaa0SLois Curfman McInnes - o_nzz - array containing the number of nonzeros in the various rows of the 16602bd5e0b2SLois Curfman McInnes off-diagonal portion of the local submatrix (possibly different for 16612bd5e0b2SLois Curfman McInnes each row) or PETSC_NULL. 16628a729477SBarry Smith 1663ff756334SLois Curfman McInnes Output Parameter: 166444cd7ae7SLois Curfman McInnes . A - the matrix 16658a729477SBarry Smith 1666b259b22eSLois Curfman McInnes Notes: 1667be79a94dSBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor 1668be79a94dSBarry Smith than it must be used on all processors that share the object for that argument. 1669be79a94dSBarry Smith 1670ff756334SLois Curfman McInnes The AIJ format (also called the Yale sparse matrix format or 1671ff756334SLois Curfman McInnes compressed row storage), is fully compatible with standard Fortran 77 16720002213bSLois Curfman McInnes storage. That is, the stored row and column indices can begin at 16730002213bSLois Curfman McInnes either one (as in Fortran) or zero. See the users manual for details. 1674ff756334SLois Curfman McInnes 1675ff756334SLois Curfman McInnes The user MUST specify either the local or global matrix dimensions 1676ff756334SLois Curfman McInnes (possibly both). 1677ff756334SLois Curfman McInnes 16785511cfe3SLois Curfman McInnes By default, this format uses inodes (identical nodes) when possible. 16795511cfe3SLois Curfman McInnes We search for consecutive rows with the same nonzero structure, thereby 16805511cfe3SLois Curfman McInnes reusing matrix information to achieve increased efficiency. 16815511cfe3SLois Curfman McInnes 16825511cfe3SLois Curfman McInnes Options Database Keys: 1683db81eaa0SLois Curfman McInnes + -mat_aij_no_inode - Do not use inodes 1684db81eaa0SLois Curfman McInnes . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 1685db81eaa0SLois Curfman McInnes - -mat_aij_oneindex - Internally use indexing starting at 1 1686db81eaa0SLois Curfman McInnes rather than 0. Note that when calling MatSetValues(), 1687db81eaa0SLois Curfman McInnes the user still MUST index entries starting at 0! 1688494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1689494eafd4SBarry Smith (defaults to using SeqBAIJ format on one processor) 1690494eafd4SBarry Smith . -mat_mpi - use the parallel matrix data structures even on one processor 1691494eafd4SBarry Smith (defaults to using SeqAIJ format on one processor) 1692494eafd4SBarry Smith 16935511cfe3SLois Curfman McInnes 1694e0245417SLois Curfman McInnes Storage Information: 1695e0245417SLois Curfman McInnes For a square global matrix we define each processor's diagonal portion 1696e0245417SLois Curfman McInnes to be its local rows and the corresponding columns (a square submatrix); 1697e0245417SLois Curfman McInnes each processor's off-diagonal portion encompasses the remainder of the 1698e0245417SLois Curfman McInnes local matrix (a rectangular submatrix). 1699e0245417SLois Curfman McInnes 1700e0245417SLois Curfman McInnes The user can specify preallocated storage for the diagonal part of 17015ace5be8SLois Curfman McInnes the local submatrix with either d_nz or d_nnz (not both). Set 17025ace5be8SLois Curfman McInnes d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic 17035ace5be8SLois Curfman McInnes memory allocation. Likewise, specify preallocated storage for the 17045ace5be8SLois Curfman McInnes off-diagonal part of the local submatrix with o_nz or o_nnz (not both). 1705ff756334SLois Curfman McInnes 17065511cfe3SLois Curfman McInnes Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In 17075511cfe3SLois Curfman McInnes the figure below we depict these three local rows and all columns (0-11). 17082191d07cSBarry Smith 1709db81eaa0SLois Curfman McInnes .vb 1710db81eaa0SLois Curfman McInnes 0 1 2 3 4 5 6 7 8 9 10 11 1711db81eaa0SLois Curfman McInnes ------------------- 1712db81eaa0SLois Curfman McInnes row 3 | o o o d d d o o o o o o 1713db81eaa0SLois Curfman McInnes row 4 | o o o d d d o o o o o o 1714db81eaa0SLois Curfman McInnes row 5 | o o o d d d o o o o o o 1715db81eaa0SLois Curfman McInnes ------------------- 1716db81eaa0SLois Curfman McInnes .ve 1717b810aeb4SBarry Smith 17185511cfe3SLois Curfman McInnes Thus, any entries in the d locations are stored in the d (diagonal) 17195511cfe3SLois Curfman McInnes submatrix, and any entries in the o locations are stored in the 17205511cfe3SLois Curfman McInnes o (off-diagonal) submatrix. Note that the d and the o submatrices are 17215511cfe3SLois Curfman McInnes stored simply in the MATSEQAIJ format for compressed row storage. 17225511cfe3SLois Curfman McInnes 17235511cfe3SLois Curfman McInnes Now d_nz should indicate the number of nonzeros per row in the d matrix, 17245511cfe3SLois Curfman McInnes and o_nz should indicate the number of nonzeros per row in the o matrix. 17255511cfe3SLois Curfman McInnes In general, for PDE problems in which most nonzeros are near the diagonal, 17263d323bbdSBarry Smith one expects d_nz >> o_nz. For large problems you MUST preallocate memory 172792e8d321SLois Curfman McInnes or you will get TERRIBLE performance; see the users' manual chapter on 17286da5968aSLois Curfman McInnes matrices. 17293a511b96SLois Curfman McInnes 1730dbd7a890SLois Curfman McInnes .keywords: matrix, aij, compressed row, sparse, parallel 1731ff756334SLois Curfman McInnes 1732fafbff53SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues() 17338a729477SBarry Smith @*/ 1734e1311b90SBarry 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) 17358a729477SBarry Smith { 173644cd7ae7SLois Curfman McInnes Mat B; 173744cd7ae7SLois Curfman McInnes Mat_MPIAIJ *b; 17387be0e774SBarry Smith int ierr, i,sum[2],work[2],size,flag1 = 0, flag2 = 0; 1739416022c9SBarry Smith 17403a40ed3dSBarry Smith PetscFunctionBegin; 17413914022bSBarry Smith MPI_Comm_size(comm,&size); 17427be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpiaij",&flag1); CHKERRQ(ierr); 17437be0e774SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-mat_mpi",&flag2); CHKERRQ(ierr); 17447be0e774SBarry Smith if (!flag1 && !flag2 && size == 1) { 17453914022bSBarry Smith if (M == PETSC_DECIDE) M = m; 17463914022bSBarry Smith if (N == PETSC_DECIDE) N = n; 17473914022bSBarry Smith ierr = MatCreateSeqAIJ(comm,M,N,d_nz,d_nnz,A); CHKERRQ(ierr); 17483a40ed3dSBarry Smith PetscFunctionReturn(0); 17493914022bSBarry Smith } 17503914022bSBarry Smith 175144cd7ae7SLois Curfman McInnes *A = 0; 1752f830108cSBarry Smith PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,comm,MatDestroy,MatView); 175344cd7ae7SLois Curfman McInnes PLogObjectCreate(B); 175444cd7ae7SLois Curfman McInnes B->data = (void *) (b = PetscNew(Mat_MPIAIJ)); CHKPTRQ(b); 175544cd7ae7SLois Curfman McInnes PetscMemzero(b,sizeof(Mat_MPIAIJ)); 1756cda55fadSBarry Smith PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps)); 1757e1311b90SBarry Smith B->ops->destroy = MatDestroy_MPIAIJ; 1758e1311b90SBarry Smith B->ops->view = MatView_MPIAIJ; 175944cd7ae7SLois Curfman McInnes B->factor = 0; 176044cd7ae7SLois Curfman McInnes B->assembled = PETSC_FALSE; 176190f02eecSBarry Smith B->mapping = 0; 1762d6dfbf8fSBarry Smith 176347794344SBarry Smith B->insertmode = NOT_SET_VALUES; 17649eb4d147SSatish Balay b->size = size; 176544cd7ae7SLois Curfman McInnes MPI_Comm_rank(comm,&b->rank); 17661eb62cbbSBarry Smith 17673a40ed3dSBarry Smith if (m == PETSC_DECIDE && (d_nnz != PETSC_NULL || o_nnz != PETSC_NULL)) { 1768a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Cannot have PETSC_DECIDE rows but set d_nnz or o_nnz"); 17693a40ed3dSBarry Smith } 17701987afe7SBarry Smith 1771dbd7a890SLois Curfman McInnes if (M == PETSC_DECIDE || N == PETSC_DECIDE) { 17721eb62cbbSBarry Smith work[0] = m; work[1] = n; 1773ca161407SBarry Smith ierr = MPI_Allreduce( work, sum,2,MPI_INT,MPI_SUM,comm );CHKERRQ(ierr); 1774dbd7a890SLois Curfman McInnes if (M == PETSC_DECIDE) M = sum[0]; 1775dbd7a890SLois Curfman McInnes if (N == PETSC_DECIDE) N = sum[1]; 17761eb62cbbSBarry Smith } 177744cd7ae7SLois Curfman McInnes if (m == PETSC_DECIDE) {m = M/b->size + ((M % b->size) > b->rank);} 177844cd7ae7SLois Curfman McInnes if (n == PETSC_DECIDE) {n = N/b->size + ((N % b->size) > b->rank);} 177944cd7ae7SLois Curfman McInnes b->m = m; B->m = m; 178044cd7ae7SLois Curfman McInnes b->n = n; B->n = n; 178144cd7ae7SLois Curfman McInnes b->N = N; B->N = N; 178244cd7ae7SLois Curfman McInnes b->M = M; B->M = M; 17831eb62cbbSBarry Smith 1784c7fcc2eaSBarry Smith /* the information in the maps duplicates the information computed below, eventually 1785c7fcc2eaSBarry Smith we should remove the duplicate information that is not contained in the maps */ 1786253a16ddSBarry Smith ierr = MapCreateMPI(comm,m,M,&B->rmap);CHKERRQ(ierr); 1787253a16ddSBarry Smith ierr = MapCreateMPI(comm,n,N,&B->cmap);CHKERRQ(ierr); 1788c7fcc2eaSBarry Smith 17891eb62cbbSBarry Smith /* build local table of row and column ownerships */ 179044cd7ae7SLois Curfman McInnes b->rowners = (int *) PetscMalloc(2*(b->size+2)*sizeof(int)); CHKPTRQ(b->rowners); 1791f09e8eb9SSatish Balay PLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1792603f58a4SSatish Balay b->cowners = b->rowners + b->size + 2; 1793ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 179444cd7ae7SLois Curfman McInnes b->rowners[0] = 0; 179544cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 179644cd7ae7SLois Curfman McInnes b->rowners[i] += b->rowners[i-1]; 17978a729477SBarry Smith } 179844cd7ae7SLois Curfman McInnes b->rstart = b->rowners[b->rank]; 179944cd7ae7SLois Curfman McInnes b->rend = b->rowners[b->rank+1]; 1800ca161407SBarry Smith ierr = MPI_Allgather(&n,1,MPI_INT,b->cowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 180144cd7ae7SLois Curfman McInnes b->cowners[0] = 0; 180244cd7ae7SLois Curfman McInnes for ( i=2; i<=b->size; i++ ) { 180344cd7ae7SLois Curfman McInnes b->cowners[i] += b->cowners[i-1]; 18048a729477SBarry Smith } 180544cd7ae7SLois Curfman McInnes b->cstart = b->cowners[b->rank]; 180644cd7ae7SLois Curfman McInnes b->cend = b->cowners[b->rank+1]; 18078a729477SBarry Smith 18085ace5be8SLois Curfman McInnes if (d_nz == PETSC_DEFAULT) d_nz = 5; 1809029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,n,d_nz,d_nnz,&b->A); CHKERRQ(ierr); 181044cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->A); 18117b8455f0SLois Curfman McInnes if (o_nz == PETSC_DEFAULT) o_nz = 0; 1812029af93fSBarry Smith ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,m,N,o_nz,o_nnz,&b->B); CHKERRQ(ierr); 181344cd7ae7SLois Curfman McInnes PLogObjectParent(B,b->B); 18148a729477SBarry Smith 18151eb62cbbSBarry Smith /* build cache for off array entries formed */ 181644cd7ae7SLois Curfman McInnes ierr = StashBuild_Private(&b->stash); CHKERRQ(ierr); 181790f02eecSBarry Smith b->donotstash = 0; 181844cd7ae7SLois Curfman McInnes b->colmap = 0; 181944cd7ae7SLois Curfman McInnes b->garray = 0; 182044cd7ae7SLois Curfman McInnes b->roworiented = 1; 18218a729477SBarry Smith 18221eb62cbbSBarry Smith /* stuff used for matrix vector multiply */ 182344cd7ae7SLois Curfman McInnes b->lvec = 0; 182444cd7ae7SLois Curfman McInnes b->Mvctx = 0; 18258a729477SBarry Smith 18267a0afa10SBarry Smith /* stuff for MatGetRow() */ 182744cd7ae7SLois Curfman McInnes b->rowindices = 0; 182844cd7ae7SLois Curfman McInnes b->rowvalues = 0; 182944cd7ae7SLois Curfman McInnes b->getrowactive = PETSC_FALSE; 18307a0afa10SBarry Smith 183144cd7ae7SLois Curfman McInnes *A = B; 18323a40ed3dSBarry Smith PetscFunctionReturn(0); 1833d6dfbf8fSBarry Smith } 1834c74985f6SBarry Smith 18355615d1e5SSatish Balay #undef __FUNC__ 18365615d1e5SSatish Balay #define __FUNC__ "MatConvertSameType_MPIAIJ" 18378f6be9afSLois Curfman McInnes int MatConvertSameType_MPIAIJ(Mat matin,Mat *newmat,int cpvalues) 1838d6dfbf8fSBarry Smith { 1839d6dfbf8fSBarry Smith Mat mat; 1840416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ *) matin->data; 1841a1b97e82SLois Curfman McInnes int ierr, len=0, flg; 1842d6dfbf8fSBarry Smith 18433a40ed3dSBarry Smith PetscFunctionBegin; 1844416022c9SBarry Smith *newmat = 0; 1845f830108cSBarry Smith PetscHeaderCreate(mat,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPIAIJ,matin->comm,MatDestroy,MatView); 1846a5a9c739SBarry Smith PLogObjectCreate(mat); 18470452661fSBarry Smith mat->data = (void *) (a = PetscNew(Mat_MPIAIJ)); CHKPTRQ(a); 1848cda55fadSBarry Smith PetscMemcpy(mat->ops,&MatOps_Values,sizeof(struct _MatOps)); 1849e1311b90SBarry Smith mat->ops->destroy = MatDestroy_MPIAIJ; 1850e1311b90SBarry Smith mat->ops->view = MatView_MPIAIJ; 1851d6dfbf8fSBarry Smith mat->factor = matin->factor; 1852c456f294SBarry Smith mat->assembled = PETSC_TRUE; 1853d6dfbf8fSBarry Smith 185444cd7ae7SLois Curfman McInnes a->m = mat->m = oldmat->m; 185544cd7ae7SLois Curfman McInnes a->n = mat->n = oldmat->n; 185644cd7ae7SLois Curfman McInnes a->M = mat->M = oldmat->M; 185744cd7ae7SLois Curfman McInnes a->N = mat->N = oldmat->N; 1858d6dfbf8fSBarry Smith 1859416022c9SBarry Smith a->rstart = oldmat->rstart; 1860416022c9SBarry Smith a->rend = oldmat->rend; 1861416022c9SBarry Smith a->cstart = oldmat->cstart; 1862416022c9SBarry Smith a->cend = oldmat->cend; 186317699dbbSLois Curfman McInnes a->size = oldmat->size; 186417699dbbSLois Curfman McInnes a->rank = oldmat->rank; 186547794344SBarry Smith mat->insertmode = NOT_SET_VALUES; 1866bcd2baecSBarry Smith a->rowvalues = 0; 1867bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 1868d6dfbf8fSBarry Smith 1869603f58a4SSatish Balay a->rowners = (int *) PetscMalloc(2*(a->size+2)*sizeof(int)); CHKPTRQ(a->rowners); 1870f09e8eb9SSatish Balay PLogObjectMemory(mat,2*(a->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1871603f58a4SSatish Balay a->cowners = a->rowners + a->size + 2; 1872603f58a4SSatish Balay PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int)); 1873416022c9SBarry Smith ierr = StashInitialize_Private(&a->stash); CHKERRQ(ierr); 18742ee70a88SLois Curfman McInnes if (oldmat->colmap) { 18750452661fSBarry Smith a->colmap = (int *) PetscMalloc((a->N)*sizeof(int));CHKPTRQ(a->colmap); 1876416022c9SBarry Smith PLogObjectMemory(mat,(a->N)*sizeof(int)); 1877416022c9SBarry Smith PetscMemcpy(a->colmap,oldmat->colmap,(a->N)*sizeof(int)); 1878416022c9SBarry Smith } else a->colmap = 0; 18793f41c07dSBarry Smith if (oldmat->garray) { 18803f41c07dSBarry Smith len = ((Mat_SeqAIJ *) (oldmat->B->data))->n; 18813f41c07dSBarry Smith a->garray = (int *) PetscMalloc((len+1)*sizeof(int)); CHKPTRQ(a->garray); 1882464493b3SBarry Smith PLogObjectMemory(mat,len*sizeof(int)); 18833f41c07dSBarry Smith if (len) PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int)); 1884416022c9SBarry Smith } else a->garray = 0; 1885d6dfbf8fSBarry Smith 1886416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec); CHKERRQ(ierr); 1887416022c9SBarry Smith PLogObjectParent(mat,a->lvec); 1888a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx); CHKERRQ(ierr); 1889416022c9SBarry Smith PLogObjectParent(mat,a->Mvctx); 1890416022c9SBarry Smith ierr = MatConvert(oldmat->A,MATSAME,&a->A); CHKERRQ(ierr); 1891416022c9SBarry Smith PLogObjectParent(mat,a->A); 1892416022c9SBarry Smith ierr = MatConvert(oldmat->B,MATSAME,&a->B); CHKERRQ(ierr); 1893416022c9SBarry Smith PLogObjectParent(mat,a->B); 18945dd7a6c7SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr); 189525cdf11fSBarry Smith if (flg) { 1896682d7d0cSBarry Smith ierr = MatPrintHelp(mat); CHKERRQ(ierr); 1897682d7d0cSBarry Smith } 18988a729477SBarry Smith *newmat = mat; 18993a40ed3dSBarry Smith PetscFunctionReturn(0); 19008a729477SBarry Smith } 1901416022c9SBarry Smith 190277c4ece6SBarry Smith #include "sys.h" 1903416022c9SBarry Smith 19045615d1e5SSatish Balay #undef __FUNC__ 19055615d1e5SSatish Balay #define __FUNC__ "MatLoad_MPIAIJ" 190619bcc07fSBarry Smith int MatLoad_MPIAIJ(Viewer viewer,MatType type,Mat *newmat) 1907416022c9SBarry Smith { 1908d65a2f8fSBarry Smith Mat A; 1909d65a2f8fSBarry Smith Scalar *vals,*svals; 191019bcc07fSBarry Smith MPI_Comm comm = ((PetscObject)viewer)->comm; 1911416022c9SBarry Smith MPI_Status status; 19123a40ed3dSBarry Smith int i, nz, ierr, j,rstart, rend, fd; 191317699dbbSLois Curfman McInnes int header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols; 1914d65a2f8fSBarry Smith int *ourlens,*sndcounts = 0,*procsnz = 0, *offlens,jj,*mycols,*smycols; 191519bcc07fSBarry Smith int tag = ((PetscObject)viewer)->tag; 1916416022c9SBarry Smith 19173a40ed3dSBarry Smith PetscFunctionBegin; 191817699dbbSLois Curfman McInnes MPI_Comm_size(comm,&size); MPI_Comm_rank(comm,&rank); 191917699dbbSLois Curfman McInnes if (!rank) { 192090ace30eSBarry Smith ierr = ViewerBinaryGetDescriptor(viewer,&fd); CHKERRQ(ierr); 19210752156aSBarry Smith ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT); CHKERRQ(ierr); 1922a8c6a408SBarry Smith if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"not matrix object"); 1923d64ed03dSBarry Smith if (header[3] < 0) { 1924a8c6a408SBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,1,"Matrix in special format on disk, cannot load as MPIAIJ"); 1925d64ed03dSBarry Smith } 19266c5fab8fSBarry Smith } 19276c5fab8fSBarry Smith 1928ca161407SBarry Smith ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr); 1929416022c9SBarry Smith M = header[1]; N = header[2]; 1930416022c9SBarry Smith /* determine ownership of all rows */ 193117699dbbSLois Curfman McInnes m = M/size + ((M % size) > rank); 19320452661fSBarry Smith rowners = (int *) PetscMalloc((size+2)*sizeof(int)); CHKPTRQ(rowners); 1933ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 1934416022c9SBarry Smith rowners[0] = 0; 193517699dbbSLois Curfman McInnes for ( i=2; i<=size; i++ ) { 1936416022c9SBarry Smith rowners[i] += rowners[i-1]; 1937416022c9SBarry Smith } 193817699dbbSLois Curfman McInnes rstart = rowners[rank]; 193917699dbbSLois Curfman McInnes rend = rowners[rank+1]; 1940416022c9SBarry Smith 1941416022c9SBarry Smith /* distribute row lengths to all processors */ 19420452661fSBarry Smith ourlens = (int*) PetscMalloc( 2*(rend-rstart)*sizeof(int) ); CHKPTRQ(ourlens); 1943416022c9SBarry Smith offlens = ourlens + (rend-rstart); 194417699dbbSLois Curfman McInnes if (!rank) { 19450452661fSBarry Smith rowlengths = (int*) PetscMalloc( M*sizeof(int) ); CHKPTRQ(rowlengths); 19460752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT); CHKERRQ(ierr); 19470452661fSBarry Smith sndcounts = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(sndcounts); 194817699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) sndcounts[i] = rowners[i+1] - rowners[i]; 1949ca161407SBarry Smith ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr); 19500452661fSBarry Smith PetscFree(sndcounts); 19513a40ed3dSBarry Smith } else { 1952ca161407SBarry Smith ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT, 0,comm);CHKERRQ(ierr); 1953416022c9SBarry Smith } 1954416022c9SBarry Smith 195517699dbbSLois Curfman McInnes if (!rank) { 1956416022c9SBarry Smith /* calculate the number of nonzeros on each processor */ 19570452661fSBarry Smith procsnz = (int*) PetscMalloc( size*sizeof(int) ); CHKPTRQ(procsnz); 1958cddf8d76SBarry Smith PetscMemzero(procsnz,size*sizeof(int)); 195917699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 1960416022c9SBarry Smith for ( j=rowners[i]; j< rowners[i+1]; j++ ) { 1961416022c9SBarry Smith procsnz[i] += rowlengths[j]; 1962416022c9SBarry Smith } 1963416022c9SBarry Smith } 19640452661fSBarry Smith PetscFree(rowlengths); 1965416022c9SBarry Smith 1966416022c9SBarry Smith /* determine max buffer needed and allocate it */ 1967416022c9SBarry Smith maxnz = 0; 196817699dbbSLois Curfman McInnes for ( i=0; i<size; i++ ) { 19690452661fSBarry Smith maxnz = PetscMax(maxnz,procsnz[i]); 1970416022c9SBarry Smith } 19710452661fSBarry Smith cols = (int *) PetscMalloc( maxnz*sizeof(int) ); CHKPTRQ(cols); 1972416022c9SBarry Smith 1973416022c9SBarry Smith /* read in my part of the matrix column indices */ 1974416022c9SBarry Smith nz = procsnz[0]; 19750452661fSBarry Smith mycols = (int *) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols); 19760752156aSBarry Smith ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT); CHKERRQ(ierr); 1977d65a2f8fSBarry Smith 1978d65a2f8fSBarry Smith /* read in every one elses and ship off */ 197917699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 1980d65a2f8fSBarry Smith nz = procsnz[i]; 19810752156aSBarry Smith ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT); CHKERRQ(ierr); 1982ca161407SBarry Smith ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr); 1983d65a2f8fSBarry Smith } 19840452661fSBarry Smith PetscFree(cols); 19853a40ed3dSBarry Smith } else { 1986416022c9SBarry Smith /* determine buffer space needed for message */ 1987416022c9SBarry Smith nz = 0; 1988416022c9SBarry Smith for ( i=0; i<m; i++ ) { 1989416022c9SBarry Smith nz += ourlens[i]; 1990416022c9SBarry Smith } 19910452661fSBarry Smith mycols = (int*) PetscMalloc( nz*sizeof(int) ); CHKPTRQ(mycols); 1992416022c9SBarry Smith 1993416022c9SBarry Smith /* receive message of column indices*/ 1994ca161407SBarry Smith ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 1995ca161407SBarry Smith ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr); 1996a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 1997416022c9SBarry Smith } 1998416022c9SBarry Smith 1999416022c9SBarry Smith /* loop over local rows, determining number of off diagonal entries */ 2000cddf8d76SBarry Smith PetscMemzero(offlens,m*sizeof(int)); 2001416022c9SBarry Smith jj = 0; 2002416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2003416022c9SBarry Smith for ( j=0; j<ourlens[i]; j++ ) { 2004d65a2f8fSBarry Smith if (mycols[jj] < rstart || mycols[jj] >= rend) offlens[i]++; 2005416022c9SBarry Smith jj++; 2006416022c9SBarry Smith } 2007416022c9SBarry Smith } 2008d65a2f8fSBarry Smith 2009d65a2f8fSBarry Smith /* create our matrix */ 2010416022c9SBarry Smith for ( i=0; i<m; i++ ) { 2011416022c9SBarry Smith ourlens[i] -= offlens[i]; 2012416022c9SBarry Smith } 2013d65a2f8fSBarry Smith ierr = MatCreateMPIAIJ(comm,m,PETSC_DECIDE,M,N,0,ourlens,0,offlens,newmat);CHKERRQ(ierr); 2014d65a2f8fSBarry Smith A = *newmat; 20156d4a8577SBarry Smith MatSetOption(A,MAT_COLUMNS_SORTED); 2016d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2017d65a2f8fSBarry Smith ourlens[i] += offlens[i]; 2018d65a2f8fSBarry Smith } 2019416022c9SBarry Smith 202017699dbbSLois Curfman McInnes if (!rank) { 20210452661fSBarry Smith vals = (Scalar *) PetscMalloc( maxnz*sizeof(Scalar) ); CHKPTRQ(vals); 2022416022c9SBarry Smith 2023416022c9SBarry Smith /* read in my part of the matrix numerical values */ 2024416022c9SBarry Smith nz = procsnz[0]; 20250752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr); 2026d65a2f8fSBarry Smith 2027d65a2f8fSBarry Smith /* insert into matrix */ 2028d65a2f8fSBarry Smith jj = rstart; 2029d65a2f8fSBarry Smith smycols = mycols; 2030d65a2f8fSBarry Smith svals = vals; 2031d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2032d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2033d65a2f8fSBarry Smith smycols += ourlens[i]; 2034d65a2f8fSBarry Smith svals += ourlens[i]; 2035d65a2f8fSBarry Smith jj++; 2036416022c9SBarry Smith } 2037416022c9SBarry Smith 2038d65a2f8fSBarry Smith /* read in other processors and ship out */ 203917699dbbSLois Curfman McInnes for ( i=1; i<size; i++ ) { 2040416022c9SBarry Smith nz = procsnz[i]; 20410752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR); CHKERRQ(ierr); 2042ca161407SBarry Smith ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr); 2043416022c9SBarry Smith } 20440452661fSBarry Smith PetscFree(procsnz); 20453a40ed3dSBarry Smith } else { 2046d65a2f8fSBarry Smith /* receive numeric values */ 20470452661fSBarry Smith vals = (Scalar*) PetscMalloc( nz*sizeof(Scalar) ); CHKPTRQ(vals); 2048416022c9SBarry Smith 2049d65a2f8fSBarry Smith /* receive message of values*/ 2050ca161407SBarry Smith ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr); 2051ca161407SBarry Smith ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr); 2052a8c6a408SBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,0,"something is wrong with file"); 2053d65a2f8fSBarry Smith 2054d65a2f8fSBarry Smith /* insert into matrix */ 2055d65a2f8fSBarry Smith jj = rstart; 2056d65a2f8fSBarry Smith smycols = mycols; 2057d65a2f8fSBarry Smith svals = vals; 2058d65a2f8fSBarry Smith for ( i=0; i<m; i++ ) { 2059d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2060d65a2f8fSBarry Smith smycols += ourlens[i]; 2061d65a2f8fSBarry Smith svals += ourlens[i]; 2062d65a2f8fSBarry Smith jj++; 2063d65a2f8fSBarry Smith } 2064d65a2f8fSBarry Smith } 20650452661fSBarry Smith PetscFree(ourlens); PetscFree(vals); PetscFree(mycols); PetscFree(rowners); 2066d65a2f8fSBarry Smith 20676d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20686d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 20693a40ed3dSBarry Smith PetscFunctionReturn(0); 2070416022c9SBarry Smith } 2071a0ff6018SBarry Smith 207229da9460SBarry Smith #undef __FUNC__ 207329da9460SBarry Smith #define __FUNC__ "MatGetSubMatrix_MPIAIJ" 2074a0ff6018SBarry Smith /* 207529da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 207629da9460SBarry Smith in local and then by concatenating the local matrices the end result. 207729da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 2078a0ff6018SBarry Smith */ 20796a6a5d1dSBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatGetSubMatrixCall call,Mat *newmat) 2080a0ff6018SBarry Smith { 208100e6dbe6SBarry Smith int ierr, i, m,n,rstart,row,rend,nz,*cwork,size,rank,j; 2082fee21e36SBarry Smith Mat *local,M, Mreuse; 208300e6dbe6SBarry Smith Scalar *vwork,*aa; 208400e6dbe6SBarry Smith MPI_Comm comm = mat->comm; 208500e6dbe6SBarry Smith Mat_SeqAIJ *aij; 208600e6dbe6SBarry Smith int *ii, *jj,nlocal,*dlens,*olens,dlen,olen,jend; 2087a0ff6018SBarry Smith 2088a0ff6018SBarry Smith PetscFunctionBegin; 208900e6dbe6SBarry Smith MPI_Comm_rank(comm,&rank); 209000e6dbe6SBarry Smith MPI_Comm_size(comm,&size); 209100e6dbe6SBarry Smith 2092fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 2093fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 2094fee21e36SBarry Smith if (!Mreuse) SETERRQ(1,1,"Submatrix passed in was not used before, cannot reuse"); 2095fee21e36SBarry Smith local = &Mreuse; 2096fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 2097fee21e36SBarry Smith } else { 2098a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 2099fee21e36SBarry Smith Mreuse = *local; 2100fee21e36SBarry Smith PetscFree(local); 2101fee21e36SBarry Smith } 2102a0ff6018SBarry Smith 2103a0ff6018SBarry Smith /* 2104a0ff6018SBarry Smith m - number of local rows 2105a0ff6018SBarry Smith n - number of columns (same on all processors) 2106a0ff6018SBarry Smith rstart - first row in new global matrix generated 2107a0ff6018SBarry Smith */ 2108fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 2109a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2110fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2111a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 211200e6dbe6SBarry Smith ii = aij->i; 211300e6dbe6SBarry Smith jj = aij->j; 211400e6dbe6SBarry Smith 2115a0ff6018SBarry Smith /* 211600e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 211700e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 2118a0ff6018SBarry Smith */ 211900e6dbe6SBarry Smith 212000e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 21216a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 212200e6dbe6SBarry Smith nlocal = n/size + ((n % size) > rank); 21236a6a5d1dSBarry Smith } else { 21246a6a5d1dSBarry Smith nlocal = csize; 21256a6a5d1dSBarry Smith } 2126ca161407SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 212700e6dbe6SBarry Smith rstart = rend - nlocal; 21286a6a5d1dSBarry Smith if (rank == size - 1 && rend != n) { 21296a6a5d1dSBarry Smith SETERRQ(1,1,"Local column sizes do not add up to total number of columns"); 21306a6a5d1dSBarry Smith } 213100e6dbe6SBarry Smith 213200e6dbe6SBarry Smith /* next, compute all the lengths */ 213300e6dbe6SBarry Smith dlens = (int *) PetscMalloc( (2*m+1)*sizeof(int) );CHKPTRQ(dlens); 213400e6dbe6SBarry Smith olens = dlens + m; 213500e6dbe6SBarry Smith for ( i=0; i<m; i++ ) { 213600e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 213700e6dbe6SBarry Smith olen = 0; 213800e6dbe6SBarry Smith dlen = 0; 213900e6dbe6SBarry Smith for ( j=0; j<jend; j++ ) { 214000e6dbe6SBarry Smith if ( *jj < rstart || *jj >= rend) olen++; 214100e6dbe6SBarry Smith else dlen++; 214200e6dbe6SBarry Smith jj++; 214300e6dbe6SBarry Smith } 214400e6dbe6SBarry Smith olens[i] = olen; 214500e6dbe6SBarry Smith dlens[i] = dlen; 214600e6dbe6SBarry Smith } 214700e6dbe6SBarry Smith ierr = MatCreateMPIAIJ(comm,m,PETSC_DECIDE,PETSC_DECIDE,n,0,dlens,0,olens,&M);CHKERRQ(ierr); 214800e6dbe6SBarry Smith PetscFree(dlens); 2149a0ff6018SBarry Smith } else { 2150a0ff6018SBarry Smith int ml,nl; 2151a0ff6018SBarry Smith 2152a0ff6018SBarry Smith M = *newmat; 2153a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 2154a8c6a408SBarry Smith if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,1,"Previous matrix must be same size/layout as request"); 2155a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 2156c48de900SBarry Smith /* 2157c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 2158c48de900SBarry Smith rather than the slower MatSetValues(). 2159c48de900SBarry Smith */ 2160c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 2161c48de900SBarry Smith M->assembled = PETSC_FALSE; 2162a0ff6018SBarry Smith } 2163a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend); CHKERRQ(ierr); 2164fee21e36SBarry Smith aij = (Mat_SeqAIJ *) (Mreuse)->data; 2165a8c6a408SBarry Smith if (aij->indexshift) SETERRQ(PETSC_ERR_SUP,1,"No support for index shifted matrix"); 216600e6dbe6SBarry Smith ii = aij->i; 216700e6dbe6SBarry Smith jj = aij->j; 216800e6dbe6SBarry Smith aa = aij->a; 2169a0ff6018SBarry Smith for (i=0; i<m; i++) { 2170a0ff6018SBarry Smith row = rstart + i; 217100e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 217200e6dbe6SBarry Smith cwork = jj; jj += nz; 217300e6dbe6SBarry Smith vwork = aa; aa += nz; 21748c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 2175a0ff6018SBarry Smith } 2176a0ff6018SBarry Smith 2177a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2178a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2179a0ff6018SBarry Smith *newmat = M; 2180fee21e36SBarry Smith 2181fee21e36SBarry Smith /* save submatrix used in processor for next request */ 2182fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2183fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 2184fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 2185fee21e36SBarry Smith } 2186fee21e36SBarry Smith 2187a0ff6018SBarry Smith PetscFunctionReturn(0); 2188a0ff6018SBarry Smith } 2189