18a729477SBarry Smith 270f55243SBarry Smith #include "src/mat/impls/aij/mpi/mpiaij.h" 3d9942c19SSatish Balay #include "src/inline/spops.h" 48a729477SBarry Smith 50f5bd95cSBarry Smith /* 60f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 79e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 80f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 90f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 100f5bd95cSBarry Smith has an order N integer array but is fast to acess. 119e25ed09SBarry Smith */ 124a2ae208SSatish Balay #undef __FUNCT__ 134a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private" 140a198c4cSBarry Smith int CreateColmap_MPIAIJ_Private(Mat mat) 159e25ed09SBarry Smith { 1644a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17273d9f13SBarry Smith int n = aij->B->n,i,ierr; 18dbb450caSBarry Smith 193a40ed3dSBarry Smith PetscFunctionBegin; 20aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 21273d9f13SBarry Smith ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr); 22b1fc9764SSatish Balay for (i=0; i<n; i++){ 230f5bd95cSBarry Smith ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr); 24b1fc9764SSatish Balay } 25b1fc9764SSatish Balay #else 26b0a32e0cSBarry Smith ierr = PetscMalloc((mat->N+1)*sizeof(int),&aij->colmap);CHKERRQ(ierr); 27b0a32e0cSBarry Smith PetscLogObjectMemory(mat,mat->N*sizeof(int)); 28273d9f13SBarry Smith ierr = PetscMemzero(aij->colmap,mat->N*sizeof(int));CHKERRQ(ierr); 29905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 30b1fc9764SSatish Balay #endif 313a40ed3dSBarry Smith PetscFunctionReturn(0); 329e25ed09SBarry Smith } 339e25ed09SBarry 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 } \ 482335bdd2SSatish Balay for (_i=low; _i<high; _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; \ 57a45adfd6SMatthew Knepley else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \ 580520107fSSatish Balay if (nrow >= rmax) { \ 590520107fSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 60273d9f13SBarry Smith int new_nz = ai[am] + CHUNKSIZE,len,*new_i,*new_j; \ 6187828ca2SBarry Smith PetscScalar *new_a; \ 620520107fSSatish Balay \ 63a45adfd6SMatthew Knepley if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \ 6489280ab3SLois Curfman McInnes \ 650520107fSSatish Balay /* malloc new storage space */ \ 6687828ca2SBarry Smith len = new_nz*(sizeof(int)+sizeof(PetscScalar))+(am+1)*sizeof(int); \ 67b0a32e0cSBarry Smith ierr = PetscMalloc(len,&new_a);CHKERRQ(ierr); \ 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];} \ 73273d9f13SBarry Smith for (ii=row+1; ii<am+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;} \ 74549d3d68SSatish Balay ierr = PetscMemcpy(new_j,aj,(ai[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \ 750520107fSSatish Balay len = (new_nz - CHUNKSIZE - ai[row] - nrow - shift); \ 76549d3d68SSatish Balay ierr = PetscMemcpy(new_j+ai[row]+shift+nrow+CHUNKSIZE,aj+ai[row]+shift+nrow, \ 77549d3d68SSatish Balay len*sizeof(int));CHKERRQ(ierr); \ 7887828ca2SBarry Smith ierr = PetscMemcpy(new_a,aa,(ai[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \ 79549d3d68SSatish Balay ierr = PetscMemcpy(new_a+ai[row]+shift+nrow+CHUNKSIZE,aa+ai[row]+shift+nrow, \ 8087828ca2SBarry Smith len*sizeof(PetscScalar));CHKERRQ(ierr); \ 810520107fSSatish Balay /* free up old matrix storage */ \ 82f5e9677aSSatish Balay \ 83606d414cSSatish Balay ierr = PetscFree(a->a);CHKERRQ(ierr); \ 84606d414cSSatish Balay if (!a->singlemalloc) { \ 85606d414cSSatish Balay ierr = PetscFree(a->i);CHKERRQ(ierr); \ 86606d414cSSatish Balay ierr = PetscFree(a->j);CHKERRQ(ierr); \ 87606d414cSSatish Balay } \ 880520107fSSatish Balay aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j; \ 897c922b88SBarry Smith a->singlemalloc = PETSC_TRUE; \ 900520107fSSatish Balay \ 910520107fSSatish Balay rp = aj + ai[row] + shift; ap = aa + ai[row] + shift; \ 9230770e4dSSatish Balay rmax = aimax[row] = aimax[row] + CHUNKSIZE; \ 9387828ca2SBarry Smith PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \ 940520107fSSatish Balay a->maxnz += CHUNKSIZE; \ 950520107fSSatish Balay a->reallocs++; \ 960520107fSSatish Balay } \ 970520107fSSatish Balay N = nrow++ - 1; a->nz++; \ 980520107fSSatish Balay /* shift up all the later entries in this row */ \ 990520107fSSatish Balay for (ii=N; ii>=_i; ii--) { \ 1000520107fSSatish Balay rp[ii+1] = rp[ii]; \ 1010520107fSSatish Balay ap[ii+1] = ap[ii]; \ 1020520107fSSatish Balay } \ 103f5e9677aSSatish Balay rp[_i] = col1; \ 1040520107fSSatish Balay ap[_i] = value; \ 10530770e4dSSatish Balay a_noinsert: ; \ 1060520107fSSatish Balay ailen[row] = nrow; \ 1070520107fSSatish Balay } 1080a198c4cSBarry Smith 10930770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 11030770e4dSSatish Balay { \ 11130770e4dSSatish Balay \ 11230770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 11330770e4dSSatish Balay rmax = bimax[row]; nrow = bilen[row]; \ 11430770e4dSSatish Balay col1 = col - shift; \ 11530770e4dSSatish Balay \ 116ba4e3ef2SSatish Balay low = 0; high = nrow; \ 117ba4e3ef2SSatish Balay while (high-low > 5) { \ 118ba4e3ef2SSatish Balay t = (low+high)/2; \ 119ba4e3ef2SSatish Balay if (rp[t] > col) high = t; \ 120ba4e3ef2SSatish Balay else low = t; \ 121ba4e3ef2SSatish Balay } \ 1222335bdd2SSatish Balay for (_i=low; _i<high; _i++) { \ 12330770e4dSSatish Balay if (rp[_i] > col1) break; \ 12430770e4dSSatish Balay if (rp[_i] == col1) { \ 12530770e4dSSatish Balay if (addv == ADD_VALUES) ap[_i] += value; \ 12630770e4dSSatish Balay else ap[_i] = value; \ 12730770e4dSSatish Balay goto b_noinsert; \ 12830770e4dSSatish Balay } \ 12930770e4dSSatish Balay } \ 13089280ab3SLois Curfman McInnes if (nonew == 1) goto b_noinsert; \ 131a45adfd6SMatthew Knepley else if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) into matrix", row, col); \ 13230770e4dSSatish Balay if (nrow >= rmax) { \ 13330770e4dSSatish Balay /* there is no extra room in row, therefore enlarge */ \ 134273d9f13SBarry Smith int new_nz = bi[bm] + CHUNKSIZE,len,*new_i,*new_j; \ 13587828ca2SBarry Smith PetscScalar *new_a; \ 13630770e4dSSatish Balay \ 137a45adfd6SMatthew Knepley if (nonew == -2) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%d, %d) in the matrix", row, col); \ 13889280ab3SLois Curfman McInnes \ 13930770e4dSSatish Balay /* malloc new storage space */ \ 14087828ca2SBarry Smith len = new_nz*(sizeof(int)+sizeof(PetscScalar))+(bm+1)*sizeof(int); \ 141b0a32e0cSBarry Smith ierr = PetscMalloc(len,&new_a);CHKERRQ(ierr); \ 14230770e4dSSatish Balay new_j = (int*)(new_a + new_nz); \ 14330770e4dSSatish Balay new_i = new_j + new_nz; \ 14430770e4dSSatish Balay \ 14530770e4dSSatish Balay /* copy over old data into new slots */ \ 14630770e4dSSatish Balay for (ii=0; ii<row+1; ii++) {new_i[ii] = bi[ii];} \ 147273d9f13SBarry Smith for (ii=row+1; ii<bm+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;} \ 148549d3d68SSatish Balay ierr = PetscMemcpy(new_j,bj,(bi[row]+nrow+shift)*sizeof(int));CHKERRQ(ierr); \ 14930770e4dSSatish Balay len = (new_nz - CHUNKSIZE - bi[row] - nrow - shift); \ 150549d3d68SSatish Balay ierr = PetscMemcpy(new_j+bi[row]+shift+nrow+CHUNKSIZE,bj+bi[row]+shift+nrow, \ 151549d3d68SSatish Balay len*sizeof(int));CHKERRQ(ierr); \ 15287828ca2SBarry Smith ierr = PetscMemcpy(new_a,ba,(bi[row]+nrow+shift)*sizeof(PetscScalar));CHKERRQ(ierr); \ 153549d3d68SSatish Balay ierr = PetscMemcpy(new_a+bi[row]+shift+nrow+CHUNKSIZE,ba+bi[row]+shift+nrow, \ 15487828ca2SBarry Smith len*sizeof(PetscScalar));CHKERRQ(ierr); \ 15530770e4dSSatish Balay /* free up old matrix storage */ \ 15630770e4dSSatish Balay \ 157606d414cSSatish Balay ierr = PetscFree(b->a);CHKERRQ(ierr); \ 158606d414cSSatish Balay if (!b->singlemalloc) { \ 159606d414cSSatish Balay ierr = PetscFree(b->i);CHKERRQ(ierr); \ 160606d414cSSatish Balay ierr = PetscFree(b->j);CHKERRQ(ierr); \ 161606d414cSSatish Balay } \ 16274c639caSSatish Balay ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j; \ 1637c922b88SBarry Smith b->singlemalloc = PETSC_TRUE; \ 16430770e4dSSatish Balay \ 16530770e4dSSatish Balay rp = bj + bi[row] + shift; ap = ba + bi[row] + shift; \ 16630770e4dSSatish Balay rmax = bimax[row] = bimax[row] + CHUNKSIZE; \ 16787828ca2SBarry Smith PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + sizeof(PetscScalar))); \ 16874c639caSSatish Balay b->maxnz += CHUNKSIZE; \ 16974c639caSSatish Balay b->reallocs++; \ 17030770e4dSSatish Balay } \ 17174c639caSSatish Balay N = nrow++ - 1; b->nz++; \ 17230770e4dSSatish Balay /* shift up all the later entries in this row */ \ 17330770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 17430770e4dSSatish Balay rp[ii+1] = rp[ii]; \ 17530770e4dSSatish Balay ap[ii+1] = ap[ii]; \ 17630770e4dSSatish Balay } \ 17730770e4dSSatish Balay rp[_i] = col1; \ 17830770e4dSSatish Balay ap[_i] = value; \ 17930770e4dSSatish Balay b_noinsert: ; \ 18030770e4dSSatish Balay bilen[row] = nrow; \ 18130770e4dSSatish Balay } 18230770e4dSSatish Balay 1834a2ae208SSatish Balay #undef __FUNCT__ 1844a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 185f15d580aSBarry Smith int MatSetValues_MPIAIJ(Mat mat,int m,const int im[],int n,const int in[],const PetscScalar v[],InsertMode addv) 1868a729477SBarry Smith { 18744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 18887828ca2SBarry Smith PetscScalar value; 1891eb62cbbSBarry Smith int ierr,i,j,rstart = aij->rstart,rend = aij->rend; 1901eb62cbbSBarry Smith int cstart = aij->cstart,cend = aij->cend,row,col; 191273d9f13SBarry Smith PetscTruth roworiented = aij->roworiented; 1928a729477SBarry Smith 1930520107fSSatish Balay /* Some Variables required in the macro */ 1944ee7247eSSatish Balay Mat A = aij->A; 1954ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 19630770e4dSSatish Balay int *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 19787828ca2SBarry Smith PetscScalar *aa = a->a; 198329f5518SBarry Smith PetscTruth ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 19930770e4dSSatish Balay Mat B = aij->B; 20030770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 201273d9f13SBarry Smith int *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->m,am = aij->A->m; 20287828ca2SBarry Smith PetscScalar *ba = b->a; 20330770e4dSSatish Balay 204ba4e3ef2SSatish Balay int *rp,ii,nrow,_i,rmax,N,col1,low,high,t; 205bfec09a0SHong Zhang int nonew = a->nonew,shift=0; 20687828ca2SBarry Smith PetscScalar *ap; 2074ee7247eSSatish Balay 2083a40ed3dSBarry Smith PetscFunctionBegin; 2098a729477SBarry Smith for (i=0; i<m; i++) { 2105ef9f2a5SBarry Smith if (im[i] < 0) continue; 211aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 212590ac198SBarry Smith if (im[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",im[i],mat->M-1); 2130a198c4cSBarry Smith #endif 2144b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 2154b0e389bSBarry Smith row = im[i] - rstart; 2161eb62cbbSBarry Smith for (j=0; j<n; j++) { 2174b0e389bSBarry Smith if (in[j] >= cstart && in[j] < cend){ 2184b0e389bSBarry Smith col = in[j] - cstart; 2194b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 220329f5518SBarry Smith if (ignorezeroentries && value == 0.0) continue; 22130770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 2220520107fSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->A,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 223273d9f13SBarry Smith } else if (in[j] < 0) continue; 224aa482453SBarry Smith #if defined(PETSC_USE_BOPT_g) 225590ac198SBarry Smith else if (in[j] >= mat->N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",in[j],mat->N-1);} 2260a198c4cSBarry Smith #endif 2271eb62cbbSBarry Smith else { 228227d817aSBarry Smith if (mat->was_assembled) { 229905e6a2fSBarry Smith if (!aij->colmap) { 230905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 231905e6a2fSBarry Smith } 232aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 2330f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 234fa46199cSSatish Balay col--; 235b1fc9764SSatish Balay #else 236905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 237b1fc9764SSatish Balay #endif 238ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 2392493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 2404b0e389bSBarry Smith col = in[j]; 2419bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 242f9508a3cSSatish Balay B = aij->B; 243f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 244f9508a3cSSatish Balay bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 245f9508a3cSSatish Balay ba = b->a; 246d6dfbf8fSBarry Smith } 247c48de900SBarry Smith } else col = in[j]; 2484b0e389bSBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 249329f5518SBarry Smith if (ignorezeroentries && value == 0.0) continue; 25030770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 25130770e4dSSatish Balay /* ierr = MatSetValues_SeqAIJ(aij->B,1,&row,1,&col,&value,addv);CHKERRQ(ierr); */ 2521eb62cbbSBarry Smith } 2531eb62cbbSBarry Smith } 2545ef9f2a5SBarry Smith } else { 25590f02eecSBarry Smith if (!aij->donotstash) { 256d36fbae8SSatish Balay if (roworiented) { 257329f5518SBarry Smith if (ignorezeroentries && v[i*n] == 0.0) continue; 2588798bf22SSatish Balay ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);CHKERRQ(ierr); 259d36fbae8SSatish Balay } else { 260329f5518SBarry Smith if (ignorezeroentries && v[i] == 0.0) continue; 2618798bf22SSatish Balay ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);CHKERRQ(ierr); 2624b0e389bSBarry Smith } 2631eb62cbbSBarry Smith } 2648a729477SBarry Smith } 26590f02eecSBarry Smith } 2663a40ed3dSBarry Smith PetscFunctionReturn(0); 2678a729477SBarry Smith } 2688a729477SBarry Smith 2694a2ae208SSatish Balay #undef __FUNCT__ 2704a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 271f15d580aSBarry Smith int MatGetValues_MPIAIJ(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[]) 272b49de8d1SLois Curfman McInnes { 273b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 274b49de8d1SLois Curfman McInnes int ierr,i,j,rstart = aij->rstart,rend = aij->rend; 275b49de8d1SLois Curfman McInnes int cstart = aij->cstart,cend = aij->cend,row,col; 276b49de8d1SLois Curfman McInnes 2773a40ed3dSBarry Smith PetscFunctionBegin; 278b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 279590ac198SBarry Smith if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %d",idxm[i]); 280952d7e9fSSatish Balay if (idxm[i] >= mat->M) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %d max %d",idxm[i],mat->M-1); 281b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 282b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 283b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 284590ac198SBarry Smith if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %d",idxn[j]); 285590ac198SBarry Smith if (idxn[j] >= mat->N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %d max %d",idxn[j],mat->N-1); 286b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 287b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 288b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 289fa852ad4SSatish Balay } else { 290905e6a2fSBarry Smith if (!aij->colmap) { 291905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 292905e6a2fSBarry Smith } 293aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 2940f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 295fa46199cSSatish Balay col --; 296b1fc9764SSatish Balay #else 297905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 298b1fc9764SSatish Balay #endif 299e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 300d9d09a02SSatish Balay else { 301b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 302b49de8d1SLois Curfman McInnes } 303b49de8d1SLois Curfman McInnes } 304b49de8d1SLois Curfman McInnes } 305a8c6a408SBarry Smith } else { 30629bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"Only local values currently supported"); 307b49de8d1SLois Curfman McInnes } 308b49de8d1SLois Curfman McInnes } 3093a40ed3dSBarry Smith PetscFunctionReturn(0); 310b49de8d1SLois Curfman McInnes } 311bc5ccf88SSatish Balay 3124a2ae208SSatish Balay #undef __FUNCT__ 3134a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 314bc5ccf88SSatish Balay int MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 315bc5ccf88SSatish Balay { 316bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 317d36fbae8SSatish Balay int ierr,nstash,reallocs; 318bc5ccf88SSatish Balay InsertMode addv; 319bc5ccf88SSatish Balay 320bc5ccf88SSatish Balay PetscFunctionBegin; 321bc5ccf88SSatish Balay if (aij->donotstash) { 322bc5ccf88SSatish Balay PetscFunctionReturn(0); 323bc5ccf88SSatish Balay } 324bc5ccf88SSatish Balay 325bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 326bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);CHKERRQ(ierr); 327bc5ccf88SSatish Balay if (addv == (ADD_VALUES|INSERT_VALUES)) { 32829bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 329bc5ccf88SSatish Balay } 330bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 331bc5ccf88SSatish Balay 3328798bf22SSatish Balay ierr = MatStashScatterBegin_Private(&mat->stash,aij->rowners);CHKERRQ(ierr); 3338798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 334b0a32e0cSBarry Smith PetscLogInfo(aij->A,"MatAssemblyBegin_MPIAIJ:Stash has %d entries, uses %d mallocs.\n",nstash,reallocs); 335bc5ccf88SSatish Balay PetscFunctionReturn(0); 336bc5ccf88SSatish Balay } 337bc5ccf88SSatish Balay 338bc5ccf88SSatish Balay 3394a2ae208SSatish Balay #undef __FUNCT__ 3404a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 341bc5ccf88SSatish Balay int MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 342bc5ccf88SSatish Balay { 343bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 34424f910e3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ *)aij->A->data,*b= (Mat_SeqAIJ *)aij->B->data; 345a2d1c673SSatish Balay int i,j,rstart,ncols,n,ierr,flg; 346bc5ccf88SSatish Balay int *row,*col,other_disassembled; 34787828ca2SBarry Smith PetscScalar *val; 348bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 349bc5ccf88SSatish Balay 350bc5ccf88SSatish Balay PetscFunctionBegin; 351bc5ccf88SSatish Balay if (!aij->donotstash) { 352a2d1c673SSatish Balay while (1) { 3538798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 354a2d1c673SSatish Balay if (!flg) break; 355a2d1c673SSatish Balay 356bc5ccf88SSatish Balay for (i=0; i<n;) { 357bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 358bc5ccf88SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 359bc5ccf88SSatish Balay if (j < n) ncols = j-i; 360bc5ccf88SSatish Balay else ncols = n-i; 361bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 362bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 363bc5ccf88SSatish Balay i = j; 364bc5ccf88SSatish Balay } 365bc5ccf88SSatish Balay } 3668798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 367bc5ccf88SSatish Balay } 368bc5ccf88SSatish Balay 369bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 370bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 371bc5ccf88SSatish Balay 372bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 373bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 374bc5ccf88SSatish Balay /* 375bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 376bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 377bc5ccf88SSatish Balay */ 378bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 379bc5ccf88SSatish Balay ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);CHKERRQ(ierr); 380bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 381bc5ccf88SSatish Balay ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 382bc5ccf88SSatish Balay } 383bc5ccf88SSatish Balay } 384bc5ccf88SSatish Balay 385bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 386bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 387bc5ccf88SSatish Balay } 388bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 389bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 390bc5ccf88SSatish Balay 391606d414cSSatish Balay if (aij->rowvalues) { 392606d414cSSatish Balay ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr); 393606d414cSSatish Balay aij->rowvalues = 0; 394606d414cSSatish Balay } 395a30b2313SHong Zhang 396a30b2313SHong Zhang /* used by MatAXPY() */ 397a30b2313SHong Zhang a->xtoy = 0; b->xtoy = 0; 398a30b2313SHong Zhang a->XtoY = 0; b->XtoY = 0; 399a30b2313SHong Zhang 400bc5ccf88SSatish Balay PetscFunctionReturn(0); 401bc5ccf88SSatish Balay } 402bc5ccf88SSatish Balay 4034a2ae208SSatish Balay #undef __FUNCT__ 4044a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 4058f6be9afSLois Curfman McInnes int MatZeroEntries_MPIAIJ(Mat A) 4061eb62cbbSBarry Smith { 40744a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 408dbd7a890SLois Curfman McInnes int ierr; 4093a40ed3dSBarry Smith 4103a40ed3dSBarry Smith PetscFunctionBegin; 41178b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 41278b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 4133a40ed3dSBarry Smith PetscFunctionReturn(0); 4141eb62cbbSBarry Smith } 4151eb62cbbSBarry Smith 4164a2ae208SSatish Balay #undef __FUNCT__ 4174a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 418268466fbSBarry Smith int MatZeroRows_MPIAIJ(Mat A,IS is,const PetscScalar *diag) 4191eb62cbbSBarry Smith { 42044a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 42117699dbbSLois Curfman McInnes int i,ierr,N,*rows,*owners = l->rowners,size = l->size; 422c1dc657dSBarry Smith int *nprocs,j,idx,nsends,row; 42317699dbbSLois Curfman McInnes int nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank; 4245392566eSBarry Smith int *rvalues,tag = A->tag,count,base,slen,n,*source; 4256a5c57faSSatish Balay int *lens,imdex,*lrows,*values,rstart=l->rstart; 426d6dfbf8fSBarry Smith MPI_Comm comm = A->comm; 4271eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 4281eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 4291eb62cbbSBarry Smith IS istmp; 43035d8aa7fSBarry Smith PetscTruth found; 4311eb62cbbSBarry Smith 4323a40ed3dSBarry Smith PetscFunctionBegin; 433e03a110bSBarry Smith ierr = ISGetLocalSize(is,&N);CHKERRQ(ierr); 43478b31e54SBarry Smith ierr = ISGetIndices(is,&rows);CHKERRQ(ierr); 4351eb62cbbSBarry Smith 4361eb62cbbSBarry Smith /* first count number of contributors to each processor */ 437b0a32e0cSBarry Smith ierr = PetscMalloc(2*size*sizeof(int),&nprocs);CHKERRQ(ierr); 438549d3d68SSatish Balay ierr = PetscMemzero(nprocs,2*size*sizeof(int));CHKERRQ(ierr); 439b0a32e0cSBarry Smith ierr = PetscMalloc((N+1)*sizeof(int),&owner);CHKERRQ(ierr); /* see note*/ 4401eb62cbbSBarry Smith for (i=0; i<N; i++) { 4411eb62cbbSBarry Smith idx = rows[i]; 44235d8aa7fSBarry Smith found = PETSC_FALSE; 44317699dbbSLois Curfman McInnes for (j=0; j<size; j++) { 4441eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 445c1dc657dSBarry Smith nprocs[2*j]++; nprocs[2*j+1] = 1; owner[i] = j; found = PETSC_TRUE; break; 4461eb62cbbSBarry Smith } 4471eb62cbbSBarry Smith } 44829bbc08cSBarry Smith if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 4491eb62cbbSBarry Smith } 450c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 4511eb62cbbSBarry Smith 4521eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 453c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 4541eb62cbbSBarry Smith 4551eb62cbbSBarry Smith /* post receives: */ 456b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);CHKERRQ(ierr); 457b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 4581eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 459ca161407SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 4601eb62cbbSBarry Smith } 4611eb62cbbSBarry Smith 4621eb62cbbSBarry Smith /* do sends: 4631eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 4641eb62cbbSBarry Smith the ith processor 4651eb62cbbSBarry Smith */ 466b0a32e0cSBarry Smith ierr = PetscMalloc((N+1)*sizeof(int),&svalues);CHKERRQ(ierr); 467b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 468b0a32e0cSBarry Smith ierr = PetscMalloc((size+1)*sizeof(int),&starts);CHKERRQ(ierr); 4691eb62cbbSBarry Smith starts[0] = 0; 470c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 4711eb62cbbSBarry Smith for (i=0; i<N; i++) { 4721eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 4731eb62cbbSBarry Smith } 4746831982aSBarry Smith ierr = ISRestoreIndices(is,&rows);CHKERRQ(ierr); 4751eb62cbbSBarry Smith 4761eb62cbbSBarry Smith starts[0] = 0; 477c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 4781eb62cbbSBarry Smith count = 0; 47917699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 480c1dc657dSBarry Smith if (nprocs[2*i+1]) { 481c1dc657dSBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPI_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 4821eb62cbbSBarry Smith } 4831eb62cbbSBarry Smith } 484606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 4851eb62cbbSBarry Smith 48617699dbbSLois Curfman McInnes base = owners[rank]; 4871eb62cbbSBarry Smith 4881eb62cbbSBarry Smith /* wait on receives */ 489b0a32e0cSBarry Smith ierr = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);CHKERRQ(ierr); 4901eb62cbbSBarry Smith source = lens + nrecvs; 4911eb62cbbSBarry Smith count = nrecvs; slen = 0; 4921eb62cbbSBarry Smith while (count) { 493ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 4941eb62cbbSBarry Smith /* unpack receives into our local space */ 495ca161407SBarry Smith ierr = MPI_Get_count(&recv_status,MPI_INT,&n);CHKERRQ(ierr); 496d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 497d6dfbf8fSBarry Smith lens[imdex] = n; 4981eb62cbbSBarry Smith slen += n; 4991eb62cbbSBarry Smith count--; 5001eb62cbbSBarry Smith } 501606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 5021eb62cbbSBarry Smith 5031eb62cbbSBarry Smith /* move the data into the send scatter */ 504b0a32e0cSBarry Smith ierr = PetscMalloc((slen+1)*sizeof(int),&lrows);CHKERRQ(ierr); 5051eb62cbbSBarry Smith count = 0; 5061eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 5071eb62cbbSBarry Smith values = rvalues + i*nmax; 5081eb62cbbSBarry Smith for (j=0; j<lens[i]; j++) { 5091eb62cbbSBarry Smith lrows[count++] = values[j] - base; 5101eb62cbbSBarry Smith } 5111eb62cbbSBarry Smith } 512606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 513606d414cSSatish Balay ierr = PetscFree(lens);CHKERRQ(ierr); 514606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 515606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 5161eb62cbbSBarry Smith 5171eb62cbbSBarry Smith /* actually zap the local rows */ 518029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);CHKERRQ(ierr); 519b0a32e0cSBarry Smith PetscLogObjectParent(A,istmp); 5206a5c57faSSatish Balay 5216eb55b6aSBarry Smith /* 5226eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 5236eb55b6aSBarry Smith is square and the user wishes to set the diagonal we use seperate 5246eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 5256eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 5266eb55b6aSBarry Smith 5276eb55b6aSBarry Smith Contributed by: Mathew Knepley 5286eb55b6aSBarry Smith */ 529e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 530e2d53e46SBarry Smith ierr = MatZeroRows(l->B,istmp,0);CHKERRQ(ierr); 5316eb55b6aSBarry Smith if (diag && (l->A->M == l->A->N)) { 5326eb55b6aSBarry Smith ierr = MatZeroRows(l->A,istmp,diag);CHKERRQ(ierr); 533e2d53e46SBarry Smith } else if (diag) { 534e2d53e46SBarry Smith ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr); 535fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 53629bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 537fa46199cSSatish Balay MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 5386525c446SSatish Balay } 539e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 540e2d53e46SBarry Smith row = lrows[i] + rstart; 541e2d53e46SBarry Smith ierr = MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);CHKERRQ(ierr); 542e2d53e46SBarry Smith } 543e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 544e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5456eb55b6aSBarry Smith } else { 5466a5c57faSSatish Balay ierr = MatZeroRows(l->A,istmp,0);CHKERRQ(ierr); 5476eb55b6aSBarry Smith } 54878b31e54SBarry Smith ierr = ISDestroy(istmp);CHKERRQ(ierr); 549606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 55072dacd9aSBarry Smith 5511eb62cbbSBarry Smith /* wait on sends */ 5521eb62cbbSBarry Smith if (nsends) { 553b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 554ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 555606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 5561eb62cbbSBarry Smith } 557606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 558606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 5591eb62cbbSBarry Smith 5603a40ed3dSBarry Smith PetscFunctionReturn(0); 5611eb62cbbSBarry Smith } 5621eb62cbbSBarry Smith 5634a2ae208SSatish Balay #undef __FUNCT__ 5644a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 5658f6be9afSLois Curfman McInnes int MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 5661eb62cbbSBarry Smith { 567416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 568fbd6ef76SBarry Smith int ierr,nt; 569416022c9SBarry Smith 5703a40ed3dSBarry Smith PetscFunctionBegin; 571a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 572273d9f13SBarry Smith if (nt != A->n) { 573273d9f13SBarry Smith SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%d) and xx (%d)",A->n,nt); 574fbd6ef76SBarry Smith } 57543a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 576f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 57743a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 578f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 5793a40ed3dSBarry Smith PetscFunctionReturn(0); 5801eb62cbbSBarry Smith } 5811eb62cbbSBarry Smith 5824a2ae208SSatish Balay #undef __FUNCT__ 5834a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 5848f6be9afSLois Curfman McInnes int MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 585da3a660dSBarry Smith { 586416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 587da3a660dSBarry Smith int ierr; 5883a40ed3dSBarry Smith 5893a40ed3dSBarry Smith PetscFunctionBegin; 59043a90d84SBarry Smith ierr = VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 591f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 59243a90d84SBarry Smith ierr = VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);CHKERRQ(ierr); 593f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 5943a40ed3dSBarry Smith PetscFunctionReturn(0); 595da3a660dSBarry Smith } 596da3a660dSBarry Smith 5974a2ae208SSatish Balay #undef __FUNCT__ 5984a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 5997c922b88SBarry Smith int MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 600da3a660dSBarry Smith { 601416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 602da3a660dSBarry Smith int ierr; 603da3a660dSBarry Smith 6043a40ed3dSBarry Smith PetscFunctionBegin; 605da3a660dSBarry Smith /* do nondiagonal part */ 6067c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 607da3a660dSBarry Smith /* send it on its way */ 608537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 609da3a660dSBarry Smith /* do local part */ 6107c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 611da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 612da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 613da3a660dSBarry Smith /* but is not perhaps always true. */ 614537820f0SBarry Smith ierr = VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 6153a40ed3dSBarry Smith PetscFunctionReturn(0); 616da3a660dSBarry Smith } 617da3a660dSBarry Smith 618cd0d46ebSvictorle EXTERN_C_BEGIN 619cd0d46ebSvictorle #undef __FUNCT__ 6205fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 6215fbd3699SBarry Smith int MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscTruth *f) 622cd0d46ebSvictorle { 6234f423910Svictorle MPI_Comm comm; 624cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 62566501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 626cd0d46ebSvictorle IS Me,Notme; 6274f423910Svictorle int M,N,first,last,*notme,ntids,i, ierr; 628cd0d46ebSvictorle 629cd0d46ebSvictorle PetscFunctionBegin; 63042e5f5b4Svictorle 63142e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 63266501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 6335fbd3699SBarry Smith ierr = MatIsTranspose(Adia,Bdia,f);CHKERRQ(ierr); 634cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 6354f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 6364f423910Svictorle ierr = MPI_Comm_size(comm,&ntids);CHKERRQ(ierr); 6374f423910Svictorle if (ntids==1) PetscFunctionReturn(0); 63842e5f5b4Svictorle 63942e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 640cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 641cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 642cd0d46ebSvictorle ierr = PetscMalloc((N-last+first)*sizeof(int),¬me);CHKERRQ(ierr); 643cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 644cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 645268466fbSBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);CHKERRQ(ierr); 646268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 647268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 64866501d38Svictorle Aoff = Aoffs[0]; 649268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 65066501d38Svictorle Boff = Boffs[0]; 6515fbd3699SBarry Smith ierr = MatIsTranspose(Aoff,Boff,f);CHKERRQ(ierr); 65266501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 65366501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 65442e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 65542e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 65642e5f5b4Svictorle 657cd0d46ebSvictorle PetscFunctionReturn(0); 658cd0d46ebSvictorle } 659cd0d46ebSvictorle EXTERN_C_END 660cd0d46ebSvictorle 6614a2ae208SSatish Balay #undef __FUNCT__ 6624a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 6637c922b88SBarry Smith int MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 664da3a660dSBarry Smith { 665416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 666da3a660dSBarry Smith int ierr; 667da3a660dSBarry Smith 6683a40ed3dSBarry Smith PetscFunctionBegin; 669da3a660dSBarry Smith /* do nondiagonal part */ 6707c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 671da3a660dSBarry Smith /* send it on its way */ 672537820f0SBarry Smith ierr = VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 673da3a660dSBarry Smith /* do local part */ 6747c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 675da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 676da3a660dSBarry Smith /* inserted in yy until the next line, which is true for my implementation*/ 677da3a660dSBarry Smith /* but is not perhaps always true. */ 6780a198c4cSBarry Smith ierr = VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);CHKERRQ(ierr); 6793a40ed3dSBarry Smith PetscFunctionReturn(0); 680da3a660dSBarry Smith } 681da3a660dSBarry Smith 6821eb62cbbSBarry Smith /* 6831eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 6841eb62cbbSBarry Smith diagonal block 6851eb62cbbSBarry Smith */ 6864a2ae208SSatish Balay #undef __FUNCT__ 6874a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 6888f6be9afSLois Curfman McInnes int MatGetDiagonal_MPIAIJ(Mat A,Vec v) 6891eb62cbbSBarry Smith { 6903a40ed3dSBarry Smith int ierr; 691416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 6923a40ed3dSBarry Smith 6933a40ed3dSBarry Smith PetscFunctionBegin; 694273d9f13SBarry Smith if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); 6955baf8537SBarry Smith if (a->rstart != a->cstart || a->rend != a->cend) { 69629bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition"); 6973a40ed3dSBarry Smith } 6983a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 6993a40ed3dSBarry Smith PetscFunctionReturn(0); 7001eb62cbbSBarry Smith } 7011eb62cbbSBarry Smith 7024a2ae208SSatish Balay #undef __FUNCT__ 7034a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 704268466fbSBarry Smith int MatScale_MPIAIJ(const PetscScalar aa[],Mat A) 705052efed2SBarry Smith { 706052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 707052efed2SBarry Smith int ierr; 7083a40ed3dSBarry Smith 7093a40ed3dSBarry Smith PetscFunctionBegin; 710052efed2SBarry Smith ierr = MatScale(aa,a->A);CHKERRQ(ierr); 711052efed2SBarry Smith ierr = MatScale(aa,a->B);CHKERRQ(ierr); 7123a40ed3dSBarry Smith PetscFunctionReturn(0); 713052efed2SBarry Smith } 714052efed2SBarry Smith 7154a2ae208SSatish Balay #undef __FUNCT__ 7164a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 717e1311b90SBarry Smith int MatDestroy_MPIAIJ(Mat mat) 7181eb62cbbSBarry Smith { 71944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 7201eb62cbbSBarry Smith int ierr; 72183e2fdc7SBarry Smith 7223a40ed3dSBarry Smith PetscFunctionBegin; 723aa482453SBarry Smith #if defined(PETSC_USE_LOG) 724b0a32e0cSBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d",mat->M,mat->N); 725a5a9c739SBarry Smith #endif 7268798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 727606d414cSSatish Balay ierr = PetscFree(aij->rowners);CHKERRQ(ierr); 72878b31e54SBarry Smith ierr = MatDestroy(aij->A);CHKERRQ(ierr); 72978b31e54SBarry Smith ierr = MatDestroy(aij->B);CHKERRQ(ierr); 730aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 7310f5bd95cSBarry Smith if (aij->colmap) {ierr = PetscTableDelete(aij->colmap);CHKERRQ(ierr);} 732b1fc9764SSatish Balay #else 733606d414cSSatish Balay if (aij->colmap) {ierr = PetscFree(aij->colmap);CHKERRQ(ierr);} 734b1fc9764SSatish Balay #endif 735606d414cSSatish Balay if (aij->garray) {ierr = PetscFree(aij->garray);CHKERRQ(ierr);} 7367c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 7377c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 738606d414cSSatish Balay if (aij->rowvalues) {ierr = PetscFree(aij->rowvalues);CHKERRQ(ierr);} 739606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 7403a40ed3dSBarry Smith PetscFunctionReturn(0); 7411eb62cbbSBarry Smith } 742ee50ffe9SBarry Smith 7434a2ae208SSatish Balay #undef __FUNCT__ 7448e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 7458e2fed03SBarry Smith int MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 7468e2fed03SBarry Smith { 7478e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 7488e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 7498e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 7508e2fed03SBarry Smith int nz,fd,ierr,header[4],rank,size,*row_lengths,*range,rlen,i,tag = ((PetscObject)viewer)->tag; 751b021249fSBarry Smith int nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = aij->cstart,rnz; 7528e2fed03SBarry Smith PetscScalar *column_values; 7538e2fed03SBarry Smith 7548e2fed03SBarry Smith PetscFunctionBegin; 7558e2fed03SBarry Smith ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr); 7568e2fed03SBarry Smith ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 7578e2fed03SBarry Smith nz = A->nz + B->nz; 7588e2fed03SBarry Smith if (rank == 0) { 7598e2fed03SBarry Smith header[0] = MAT_FILE_COOKIE; 7608e2fed03SBarry Smith header[1] = mat->M; 7618e2fed03SBarry Smith header[2] = mat->N; 7628e2fed03SBarry Smith ierr = MPI_Reduce(&nz,&header[3],1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr); 7638e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 7648e2fed03SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,1);CHKERRQ(ierr); 7658e2fed03SBarry Smith /* get largest number of rows any processor has */ 7668e2fed03SBarry Smith rlen = mat->m; 7678e2fed03SBarry Smith ierr = PetscMapGetGlobalRange(mat->rmap,&range);CHKERRQ(ierr); 7688e2fed03SBarry Smith for (i=1; i<size; i++) { 7698e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 7708e2fed03SBarry Smith } 7718e2fed03SBarry Smith } else { 7728e2fed03SBarry Smith ierr = MPI_Reduce(&nz,0,1,MPI_INT,MPI_SUM,0,mat->comm);CHKERRQ(ierr); 7738e2fed03SBarry Smith rlen = mat->m; 7748e2fed03SBarry Smith } 7758e2fed03SBarry Smith 7768e2fed03SBarry Smith /* load up the local row counts */ 7778e2fed03SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(int),&row_lengths);CHKERRQ(ierr); 7788e2fed03SBarry Smith for (i=0; i<mat->m; i++) { 7798e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 7808e2fed03SBarry Smith } 7818e2fed03SBarry Smith 7828e2fed03SBarry Smith /* store the row lengths to the file */ 7838e2fed03SBarry Smith if (rank == 0) { 7848e2fed03SBarry Smith MPI_Status status; 7858e2fed03SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->m,PETSC_INT,1);CHKERRQ(ierr); 7868e2fed03SBarry Smith for (i=1; i<size; i++) { 7878e2fed03SBarry Smith rlen = range[i+1] - range[i]; 7888e2fed03SBarry Smith ierr = MPI_Recv(row_lengths,rlen,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr); 7898e2fed03SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,1);CHKERRQ(ierr); 7908e2fed03SBarry Smith } 7918e2fed03SBarry Smith } else { 7928e2fed03SBarry Smith ierr = MPI_Send(row_lengths,mat->m,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr); 7938e2fed03SBarry Smith } 7948e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 7958e2fed03SBarry Smith 7968e2fed03SBarry Smith /* load up the local column indices */ 7978e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 7988e2fed03SBarry Smith ierr = MPI_Reduce(&nz,&nzmax,1,MPI_INT,MPI_MAX,0,mat->comm);CHKERRQ(ierr); 7998e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(int),&column_indices);CHKERRQ(ierr); 8008e2fed03SBarry Smith cnt = 0; 8018e2fed03SBarry Smith for (i=0; i<mat->m; i++) { 8028e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 8038e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 8048e2fed03SBarry Smith column_indices[cnt++] = col; 8058e2fed03SBarry Smith } 8068e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 8078e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 8088e2fed03SBarry Smith } 8098e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 8108e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 8118e2fed03SBarry Smith } 8128e2fed03SBarry Smith } 8138e2fed03SBarry Smith if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz); 8148e2fed03SBarry Smith 8158e2fed03SBarry Smith /* store the column indices to the file */ 8168e2fed03SBarry Smith if (rank == 0) { 8178e2fed03SBarry Smith MPI_Status status; 8188e2fed03SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,1);CHKERRQ(ierr); 8198e2fed03SBarry Smith for (i=1; i<size; i++) { 820b021249fSBarry Smith ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr); 821b021249fSBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax); 822b021249fSBarry Smith ierr = MPI_Recv(column_indices,rnz,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr); 823b021249fSBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,1);CHKERRQ(ierr); 8248e2fed03SBarry Smith } 8258e2fed03SBarry Smith } else { 8268e2fed03SBarry Smith ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr); 8278e2fed03SBarry Smith ierr = MPI_Send(column_indices,nz,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr); 8288e2fed03SBarry Smith } 8298e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 8308e2fed03SBarry Smith 8318e2fed03SBarry Smith /* load up the local column values */ 8328e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 8338e2fed03SBarry Smith cnt = 0; 8348e2fed03SBarry Smith for (i=0; i<mat->m; i++) { 8358e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 8368e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 8378e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 8388e2fed03SBarry Smith } 8398e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 8408e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 8418e2fed03SBarry Smith } 8428e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 8438e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 8448e2fed03SBarry Smith } 8458e2fed03SBarry Smith } 8468e2fed03SBarry Smith if (cnt != A->nz + B->nz) SETERRQ2(1,"Internal PETSc error: cnt = %d nz = %d",cnt,A->nz+B->nz); 8478e2fed03SBarry Smith 8488e2fed03SBarry Smith /* store the column values to the file */ 8498e2fed03SBarry Smith if (rank == 0) { 8508e2fed03SBarry Smith MPI_Status status; 8518e2fed03SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,1);CHKERRQ(ierr); 8528e2fed03SBarry Smith for (i=1; i<size; i++) { 853b021249fSBarry Smith ierr = MPI_Recv(&rnz,1,MPI_INT,i,tag,mat->comm,&status);CHKERRQ(ierr); 854b021249fSBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %d nzmax = %d",nz,nzmax); 855b021249fSBarry Smith ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);CHKERRQ(ierr); 856b021249fSBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,1);CHKERRQ(ierr); 8578e2fed03SBarry Smith } 8588e2fed03SBarry Smith } else { 8598e2fed03SBarry Smith ierr = MPI_Send(&nz,1,MPI_INT,0,tag,mat->comm);CHKERRQ(ierr); 8608e2fed03SBarry Smith ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);CHKERRQ(ierr); 8618e2fed03SBarry Smith } 8628e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 8638e2fed03SBarry Smith PetscFunctionReturn(0); 8648e2fed03SBarry Smith } 8658e2fed03SBarry Smith 8668e2fed03SBarry Smith #undef __FUNCT__ 8674a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 868b0a32e0cSBarry Smith int MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 869416022c9SBarry Smith { 87044a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 871bfec09a0SHong Zhang int ierr,rank = aij->rank,size = aij->size; 8728e2fed03SBarry Smith PetscTruth isdraw,isascii,flg,isbinary; 873b0a32e0cSBarry Smith PetscViewer sviewer; 874f3ef73ceSBarry Smith PetscViewerFormat format; 875416022c9SBarry Smith 8763a40ed3dSBarry Smith PetscFunctionBegin; 877fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 878b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 8798e2fed03SBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 8800f5bd95cSBarry Smith if (isascii) { 881b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 882456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 8834e220ebcSLois Curfman McInnes MatInfo info; 8841dab6e02SBarry Smith ierr = MPI_Comm_rank(mat->comm,&rank);CHKERRQ(ierr); 885888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 886b0a32e0cSBarry Smith ierr = PetscOptionsHasName(PETSC_NULL,"-mat_aij_no_inode",&flg);CHKERRQ(ierr); 8876831982aSBarry Smith if (flg) { 888b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, not using I-node routines\n", 889273d9f13SBarry Smith rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr); 8906831982aSBarry Smith } else { 891b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d mem %d, using I-node routines\n", 892273d9f13SBarry Smith rank,mat->m,(int)info.nz_used,(int)info.nz_allocated,(int)info.memory);CHKERRQ(ierr); 8936831982aSBarry Smith } 894888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 895b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr); 896888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 897b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d \n",rank,(int)info.nz_used);CHKERRQ(ierr); 898b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 899a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 9003a40ed3dSBarry Smith PetscFunctionReturn(0); 901fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 9023a40ed3dSBarry Smith PetscFunctionReturn(0); 9034aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 9044aedb280SBarry Smith PetscFunctionReturn(0); 90508480c60SBarry Smith } 9068e2fed03SBarry Smith } else if (isbinary) { 9078e2fed03SBarry Smith if (size == 1) { 9088e2fed03SBarry Smith ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr); 9098e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 9108e2fed03SBarry Smith } else { 9118e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 9128e2fed03SBarry Smith } 9138e2fed03SBarry Smith PetscFunctionReturn(0); 9140f5bd95cSBarry Smith } else if (isdraw) { 915b0a32e0cSBarry Smith PetscDraw draw; 91619bcc07fSBarry Smith PetscTruth isnull; 917b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 918b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 91919bcc07fSBarry Smith } 92019bcc07fSBarry Smith 92117699dbbSLois Curfman McInnes if (size == 1) { 922e36acaf3SBarry Smith ierr = PetscObjectSetName((PetscObject)aij->A,mat->name);CHKERRQ(ierr); 92378b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 9243a40ed3dSBarry Smith } else { 92595373324SBarry Smith /* assemble the entire matrix onto first processor. */ 92695373324SBarry Smith Mat A; 927ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 928273d9f13SBarry Smith int M = mat->M,N = mat->N,m,*ai,*aj,row,*cols,i,*ct; 92987828ca2SBarry Smith PetscScalar *a; 9302ee70a88SLois Curfman McInnes 93117699dbbSLois Curfman McInnes if (!rank) { 932f204ca49SKris Buschelman ierr = MatCreate(mat->comm,M,N,M,N,&A);CHKERRQ(ierr); 9333a40ed3dSBarry Smith } else { 934f204ca49SKris Buschelman ierr = MatCreate(mat->comm,0,0,M,N,&A);CHKERRQ(ierr); 93595373324SBarry Smith } 936f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 937f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 938f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 939b0a32e0cSBarry Smith PetscLogObjectParent(mat,A); 940416022c9SBarry Smith 94195373324SBarry Smith /* copy over the A part */ 942ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 943273d9f13SBarry Smith m = aij->A->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 94495373324SBarry Smith row = aij->rstart; 945bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {aj[i] += aij->cstart ;} 94695373324SBarry Smith for (i=0; i<m; i++) { 947416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 94895373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 94995373324SBarry Smith } 9502ee70a88SLois Curfman McInnes aj = Aloc->j; 951bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {aj[i] -= aij->cstart;} 95295373324SBarry Smith 95395373324SBarry Smith /* copy over the B part */ 954ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 955273d9f13SBarry Smith m = aij->B->m; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 95695373324SBarry Smith row = aij->rstart; 957b0a32e0cSBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(int),&cols);CHKERRQ(ierr); 958b0a32e0cSBarry Smith ct = cols; 959bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 96095373324SBarry Smith for (i=0; i<m; i++) { 961416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 96295373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 96395373324SBarry Smith } 964606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 9656d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 9666d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 96755843e3eSBarry Smith /* 96855843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 969b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 97055843e3eSBarry Smith */ 971b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 972e03a110bSBarry Smith if (!rank) { 973e36acaf3SBarry Smith ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);CHKERRQ(ierr); 9746831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 97595373324SBarry Smith } 976b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 97778b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 97895373324SBarry Smith } 9793a40ed3dSBarry Smith PetscFunctionReturn(0); 9801eb62cbbSBarry Smith } 9811eb62cbbSBarry Smith 9824a2ae208SSatish Balay #undef __FUNCT__ 9834a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 984b0a32e0cSBarry Smith int MatView_MPIAIJ(Mat mat,PetscViewer viewer) 985416022c9SBarry Smith { 986416022c9SBarry Smith int ierr; 9876831982aSBarry Smith PetscTruth isascii,isdraw,issocket,isbinary; 988416022c9SBarry Smith 9893a40ed3dSBarry Smith PetscFunctionBegin; 990b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 991fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr); 992fb9695e5SSatish Balay ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr); 993b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);CHKERRQ(ierr); 9940f5bd95cSBarry Smith if (isascii || isdraw || isbinary || issocket) { 9957b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 9965cd90555SBarry Smith } else { 99729bbc08cSBarry Smith SETERRQ1(1,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 998416022c9SBarry Smith } 9993a40ed3dSBarry Smith PetscFunctionReturn(0); 1000416022c9SBarry Smith } 1001416022c9SBarry Smith 10021eb62cbbSBarry Smith 1003c14dc6b6SHong Zhang 10044a2ae208SSatish Balay #undef __FUNCT__ 10054a2ae208SSatish Balay #define __FUNCT__ "MatRelax_MPIAIJ" 1006c14dc6b6SHong Zhang int MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,int its,int lits,Vec xx) 10078a729477SBarry Smith { 100844a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1009c14dc6b6SHong Zhang int ierr; 1010c14dc6b6SHong Zhang Vec bb1; 1011a6c309e7SHong Zhang PetscScalar mone=-1.0; 10128a729477SBarry Smith 10133a40ed3dSBarry Smith PetscFunctionBegin; 101491723122SBarry Smith if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %d and local its %d both positive",its,lits); 1015c14dc6b6SHong Zhang 1016c14dc6b6SHong Zhang ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 10172798e883SHong Zhang 1018c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1019da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 1020bd3bf7d3SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);CHKERRQ(ierr); 10212798e883SHong Zhang its--; 1022da3a660dSBarry Smith } 10232798e883SHong Zhang 10242798e883SHong Zhang while (its--) { 10253a40ed3dSBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10263a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10272798e883SHong Zhang 1028c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1029c14dc6b6SHong Zhang ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr); 1030c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 10312798e883SHong Zhang 1032c14dc6b6SHong Zhang /* local sweep */ 1033bd3bf7d3SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx); 1034c14dc6b6SHong Zhang CHKERRQ(ierr); 10352798e883SHong Zhang } 10363a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1037da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 1038c14dc6b6SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr); 10392798e883SHong Zhang its--; 1040da3a660dSBarry Smith } 10412798e883SHong Zhang while (its--) { 10423a40ed3dSBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10433a40ed3dSBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10442798e883SHong Zhang 1045c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1046c14dc6b6SHong Zhang ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr); 1047c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1048c14dc6b6SHong Zhang 1049c14dc6b6SHong Zhang /* local sweep */ 1050a6c309e7SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx); 1051c14dc6b6SHong Zhang CHKERRQ(ierr); 10522798e883SHong Zhang } 10533a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1054da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 1055c14dc6b6SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);CHKERRQ(ierr); 10562798e883SHong Zhang its--; 1057da3a660dSBarry Smith } 10582798e883SHong Zhang while (its--) { 10592e8a6d31SBarry Smith ierr = VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10602e8a6d31SBarry Smith ierr = VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);CHKERRQ(ierr); 10612798e883SHong Zhang 1062c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1063c14dc6b6SHong Zhang ierr = VecScale(&mone,mat->lvec);CHKERRQ(ierr); 1064c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 10652798e883SHong Zhang 1066c14dc6b6SHong Zhang /* local sweep */ 1067a6c309e7SHong Zhang ierr = (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx); 1068c14dc6b6SHong Zhang CHKERRQ(ierr); 10692798e883SHong Zhang } 10703a40ed3dSBarry Smith } else { 107129bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported"); 1072c16cb8f2SBarry Smith } 1073c14dc6b6SHong Zhang 1074c14dc6b6SHong Zhang ierr = VecDestroy(bb1);CHKERRQ(ierr); 10753a40ed3dSBarry Smith PetscFunctionReturn(0); 10768a729477SBarry Smith } 1077a66be287SLois Curfman McInnes 10784a2ae208SSatish Balay #undef __FUNCT__ 10794a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 10808f6be9afSLois Curfman McInnes int MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1081a66be287SLois Curfman McInnes { 1082a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1083a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 10844e220ebcSLois Curfman McInnes int ierr; 1085329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1086a66be287SLois Curfman McInnes 10873a40ed3dSBarry Smith PetscFunctionBegin; 10884e220ebcSLois Curfman McInnes info->block_size = 1.0; 10894e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 10904e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 10914e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 10924e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 10934e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 10944e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1095a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 10964e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 10974e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 10984e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 10994e220ebcSLois Curfman McInnes info->memory = isend[3]; 11004e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1101a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1102d7d1e502SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);CHKERRQ(ierr); 11034e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 11044e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 11054e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 11064e220ebcSLois Curfman McInnes info->memory = irecv[3]; 11074e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1108a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1109d7d1e502SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);CHKERRQ(ierr); 11104e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 11114e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 11124e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 11134e220ebcSLois Curfman McInnes info->memory = irecv[3]; 11144e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1115a66be287SLois Curfman McInnes } 11164e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 11174e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 11184e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 1119273d9f13SBarry Smith info->rows_global = (double)matin->M; 1120273d9f13SBarry Smith info->columns_global = (double)matin->N; 1121273d9f13SBarry Smith info->rows_local = (double)matin->m; 1122273d9f13SBarry Smith info->columns_local = (double)matin->N; 11234e220ebcSLois Curfman McInnes 11243a40ed3dSBarry Smith PetscFunctionReturn(0); 1125a66be287SLois Curfman McInnes } 1126a66be287SLois Curfman McInnes 11274a2ae208SSatish Balay #undef __FUNCT__ 11284a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 11298f6be9afSLois Curfman McInnes int MatSetOption_MPIAIJ(Mat A,MatOption op) 1130c74985f6SBarry Smith { 1131c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 11321dee9f54SBarry Smith int ierr; 1133c74985f6SBarry Smith 11343a40ed3dSBarry Smith PetscFunctionBegin; 113512c028f9SKris Buschelman switch (op) { 113612c028f9SKris Buschelman case MAT_NO_NEW_NONZERO_LOCATIONS: 113712c028f9SKris Buschelman case MAT_YES_NEW_NONZERO_LOCATIONS: 113812c028f9SKris Buschelman case MAT_COLUMNS_UNSORTED: 113912c028f9SKris Buschelman case MAT_COLUMNS_SORTED: 114012c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 114112c028f9SKris Buschelman case MAT_KEEP_ZEROED_ROWS: 114212c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 114312c028f9SKris Buschelman case MAT_USE_INODES: 114412c028f9SKris Buschelman case MAT_DO_NOT_USE_INODES: 114512c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 11461dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 11471dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 114812c028f9SKris Buschelman break; 114912c028f9SKris Buschelman case MAT_ROW_ORIENTED: 11507c922b88SBarry Smith a->roworiented = PETSC_TRUE; 11511dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 11521dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 115312c028f9SKris Buschelman break; 115412c028f9SKris Buschelman case MAT_ROWS_SORTED: 115512c028f9SKris Buschelman case MAT_ROWS_UNSORTED: 115612c028f9SKris Buschelman case MAT_YES_NEW_DIAGONALS: 1157b0a32e0cSBarry Smith PetscLogInfo(A,"MatSetOption_MPIAIJ:Option ignored\n"); 115812c028f9SKris Buschelman break; 115912c028f9SKris Buschelman case MAT_COLUMN_ORIENTED: 11607c922b88SBarry Smith a->roworiented = PETSC_FALSE; 11611dee9f54SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 11621dee9f54SBarry Smith ierr = MatSetOption(a->B,op);CHKERRQ(ierr); 116312c028f9SKris Buschelman break; 116412c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 11657c922b88SBarry Smith a->donotstash = PETSC_TRUE; 116612c028f9SKris Buschelman break; 116712c028f9SKris Buschelman case MAT_NO_NEW_DIAGONALS: 116829bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS"); 116977e54ba9SKris Buschelman case MAT_SYMMETRIC: 117077e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1171bf108f30SBarry Smith case MAT_HERMITIAN: 1172bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 1173bf108f30SBarry Smith ierr = MatSetOption(a->A,op);CHKERRQ(ierr); 1174bf108f30SBarry Smith break; 11759a4540c5SBarry Smith case MAT_NOT_SYMMETRIC: 11769a4540c5SBarry Smith case MAT_NOT_STRUCTURALLY_SYMMETRIC: 11779a4540c5SBarry Smith case MAT_NOT_HERMITIAN: 11789a4540c5SBarry Smith case MAT_NOT_SYMMETRY_ETERNAL: 117977e54ba9SKris Buschelman break; 118012c028f9SKris Buschelman default: 118129bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"unknown option"); 11823a40ed3dSBarry Smith } 11833a40ed3dSBarry Smith PetscFunctionReturn(0); 1184c74985f6SBarry Smith } 1185c74985f6SBarry Smith 11864a2ae208SSatish Balay #undef __FUNCT__ 11874a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 118887828ca2SBarry Smith int MatGetRow_MPIAIJ(Mat matin,int row,int *nz,int **idx,PetscScalar **v) 118939e00950SLois Curfman McInnes { 1190154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 119187828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 1192154123eaSLois Curfman McInnes int i,ierr,*cworkA,*cworkB,**pcA,**pcB,cstart = mat->cstart; 1193154123eaSLois Curfman McInnes int nztot,nzA,nzB,lrow,rstart = mat->rstart,rend = mat->rend; 119470f0671dSBarry Smith int *cmap,*idx_p; 119539e00950SLois Curfman McInnes 11963a40ed3dSBarry Smith PetscFunctionBegin; 119729bbc08cSBarry Smith if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active"); 11987a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 11997a0afa10SBarry Smith 120070f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 12017a0afa10SBarry Smith /* 12027a0afa10SBarry Smith allocate enough space to hold information from the longest row. 12037a0afa10SBarry Smith */ 12047a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1205273d9f13SBarry Smith int max = 1,tmp; 1206273d9f13SBarry Smith for (i=0; i<matin->m; i++) { 12077a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 12087a0afa10SBarry Smith if (max < tmp) { max = tmp; } 12097a0afa10SBarry Smith } 121087828ca2SBarry Smith ierr = PetscMalloc(max*(sizeof(int)+sizeof(PetscScalar)),&mat->rowvalues);CHKERRQ(ierr); 12117a0afa10SBarry Smith mat->rowindices = (int*)(mat->rowvalues + max); 12127a0afa10SBarry Smith } 12137a0afa10SBarry Smith 121429bbc08cSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows") 1215abc0e9e4SLois Curfman McInnes lrow = row - rstart; 121639e00950SLois Curfman McInnes 1217154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1218154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1219154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1220f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1221f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1222154123eaSLois Curfman McInnes nztot = nzA + nzB; 1223154123eaSLois Curfman McInnes 122470f0671dSBarry Smith cmap = mat->garray; 1225154123eaSLois Curfman McInnes if (v || idx) { 1226154123eaSLois Curfman McInnes if (nztot) { 1227154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 122870f0671dSBarry Smith int imark = -1; 1229154123eaSLois Curfman McInnes if (v) { 123070f0671dSBarry Smith *v = v_p = mat->rowvalues; 123139e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 123270f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1233154123eaSLois Curfman McInnes else break; 1234154123eaSLois Curfman McInnes } 1235154123eaSLois Curfman McInnes imark = i; 123670f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 123770f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1238154123eaSLois Curfman McInnes } 1239154123eaSLois Curfman McInnes if (idx) { 124070f0671dSBarry Smith *idx = idx_p = mat->rowindices; 124170f0671dSBarry Smith if (imark > -1) { 124270f0671dSBarry Smith for (i=0; i<imark; i++) { 124370f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 124470f0671dSBarry Smith } 124570f0671dSBarry Smith } else { 1246154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 124770f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1248154123eaSLois Curfman McInnes else break; 1249154123eaSLois Curfman McInnes } 1250154123eaSLois Curfman McInnes imark = i; 125170f0671dSBarry Smith } 125270f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 125370f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 125439e00950SLois Curfman McInnes } 12553f97c4b0SBarry Smith } else { 12561ca473b0SSatish Balay if (idx) *idx = 0; 12571ca473b0SSatish Balay if (v) *v = 0; 12581ca473b0SSatish Balay } 1259154123eaSLois Curfman McInnes } 126039e00950SLois Curfman McInnes *nz = nztot; 1261f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1262f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 12633a40ed3dSBarry Smith PetscFunctionReturn(0); 126439e00950SLois Curfman McInnes } 126539e00950SLois Curfman McInnes 12664a2ae208SSatish Balay #undef __FUNCT__ 12674a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 126887828ca2SBarry Smith int MatRestoreRow_MPIAIJ(Mat mat,int row,int *nz,int **idx,PetscScalar **v) 126939e00950SLois Curfman McInnes { 12707a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 12713a40ed3dSBarry Smith 12723a40ed3dSBarry Smith PetscFunctionBegin; 12737a0afa10SBarry Smith if (aij->getrowactive == PETSC_FALSE) { 127429bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called"); 12757a0afa10SBarry Smith } 12767a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 12773a40ed3dSBarry Smith PetscFunctionReturn(0); 127839e00950SLois Curfman McInnes } 127939e00950SLois Curfman McInnes 12804a2ae208SSatish Balay #undef __FUNCT__ 12814a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1282329f5518SBarry Smith int MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1283855ac2c5SLois Curfman McInnes { 1284855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1285ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1286bfec09a0SHong Zhang int ierr,i,j,cstart = aij->cstart; 1287329f5518SBarry Smith PetscReal sum = 0.0; 128887828ca2SBarry Smith PetscScalar *v; 128904ca555eSLois Curfman McInnes 12903a40ed3dSBarry Smith PetscFunctionBegin; 129117699dbbSLois Curfman McInnes if (aij->size == 1) { 129214183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 129337fa93a5SLois Curfman McInnes } else { 129404ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 129504ca555eSLois Curfman McInnes v = amat->a; 129604ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1297aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1298329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 129904ca555eSLois Curfman McInnes #else 130004ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 130104ca555eSLois Curfman McInnes #endif 130204ca555eSLois Curfman McInnes } 130304ca555eSLois Curfman McInnes v = bmat->a; 130404ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1305aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1306329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 130704ca555eSLois Curfman McInnes #else 130804ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 130904ca555eSLois Curfman McInnes #endif 131004ca555eSLois Curfman McInnes } 1311d7d1e502SBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr); 131204ca555eSLois Curfman McInnes *norm = sqrt(*norm); 13133a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1314329f5518SBarry Smith PetscReal *tmp,*tmp2; 131504ca555eSLois Curfman McInnes int *jj,*garray = aij->garray; 1316b0a32e0cSBarry Smith ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1317b0a32e0cSBarry Smith ierr = PetscMalloc((mat->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1318273d9f13SBarry Smith ierr = PetscMemzero(tmp,mat->N*sizeof(PetscReal));CHKERRQ(ierr); 131904ca555eSLois Curfman McInnes *norm = 0.0; 132004ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 132104ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1322bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 132304ca555eSLois Curfman McInnes } 132404ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 132504ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1326bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 132704ca555eSLois Curfman McInnes } 1328d7d1e502SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->N,MPIU_REAL,MPI_SUM,mat->comm);CHKERRQ(ierr); 1329273d9f13SBarry Smith for (j=0; j<mat->N; j++) { 133004ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 133104ca555eSLois Curfman McInnes } 1332606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1333606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 13343a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1335329f5518SBarry Smith PetscReal ntemp = 0.0; 1336273d9f13SBarry Smith for (j=0; j<aij->A->m; j++) { 1337bfec09a0SHong Zhang v = amat->a + amat->i[j]; 133804ca555eSLois Curfman McInnes sum = 0.0; 133904ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1340cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 134104ca555eSLois Curfman McInnes } 1342bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 134304ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1344cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 134504ca555eSLois Curfman McInnes } 1346515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 134704ca555eSLois Curfman McInnes } 1348d7d1e502SBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);CHKERRQ(ierr); 1349ca161407SBarry Smith } else { 135029bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"No support for two norm"); 135104ca555eSLois Curfman McInnes } 135237fa93a5SLois Curfman McInnes } 13533a40ed3dSBarry Smith PetscFunctionReturn(0); 1354855ac2c5SLois Curfman McInnes } 1355855ac2c5SLois Curfman McInnes 13564a2ae208SSatish Balay #undef __FUNCT__ 13574a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 13588f6be9afSLois Curfman McInnes int MatTranspose_MPIAIJ(Mat A,Mat *matout) 1359b7c46309SBarry Smith { 1360b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1361dbb450caSBarry Smith Mat_SeqAIJ *Aloc = (Mat_SeqAIJ*)a->A->data; 1362bfec09a0SHong Zhang int ierr; 1363273d9f13SBarry Smith int M = A->M,N = A->N,m,*ai,*aj,row,*cols,i,*ct; 13643a40ed3dSBarry Smith Mat B; 136587828ca2SBarry Smith PetscScalar *array; 1366b7c46309SBarry Smith 13673a40ed3dSBarry Smith PetscFunctionBegin; 13687c922b88SBarry Smith if (!matout && M != N) { 136929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1370d4bb536fSBarry Smith } 1371d4bb536fSBarry Smith 1372f204ca49SKris Buschelman ierr = MatCreate(A->comm,A->n,A->m,N,M,&B);CHKERRQ(ierr); 1373f204ca49SKris Buschelman ierr = MatSetType(B,A->type_name);CHKERRQ(ierr); 1374f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 1375b7c46309SBarry Smith 1376b7c46309SBarry Smith /* copy over the A part */ 1377ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)a->A->data; 1378273d9f13SBarry Smith m = a->A->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1379b7c46309SBarry Smith row = a->rstart; 1380bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {aj[i] += a->cstart ;} 1381b7c46309SBarry Smith for (i=0; i<m; i++) { 1382416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1383b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 1384b7c46309SBarry Smith } 1385b7c46309SBarry Smith aj = Aloc->j; 1386bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {aj[i] -= a->cstart ;} 1387b7c46309SBarry Smith 1388b7c46309SBarry Smith /* copy over the B part */ 1389ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)a->B->data; 1390273d9f13SBarry Smith m = a->B->m; ai = Aloc->i; aj = Aloc->j; array = Aloc->a; 1391b7c46309SBarry Smith row = a->rstart; 1392bfec09a0SHong Zhang ierr = PetscMalloc((1+ai[m])*sizeof(int),&cols);CHKERRQ(ierr); 1393b0a32e0cSBarry Smith ct = cols; 1394bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];} 1395b7c46309SBarry Smith for (i=0; i<m; i++) { 1396416022c9SBarry Smith ierr = MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1397b7c46309SBarry Smith row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 1398b7c46309SBarry Smith } 1399606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 14006d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14016d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14027c922b88SBarry Smith if (matout) { 14030de55854SLois Curfman McInnes *matout = B; 14040de55854SLois Curfman McInnes } else { 1405273d9f13SBarry Smith ierr = MatHeaderCopy(A,B);CHKERRQ(ierr); 14060de55854SLois Curfman McInnes } 14073a40ed3dSBarry Smith PetscFunctionReturn(0); 1408b7c46309SBarry Smith } 1409b7c46309SBarry Smith 14104a2ae208SSatish Balay #undef __FUNCT__ 14114a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 14124b967eb1SSatish Balay int MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1413a008b906SSatish Balay { 14144b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 14154b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1416a008b906SSatish Balay int ierr,s1,s2,s3; 1417a008b906SSatish Balay 14183a40ed3dSBarry Smith PetscFunctionBegin; 14194b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 14204b967eb1SSatish Balay if (rr) { 1421e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 142229bbc08cSBarry Smith if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 14234b967eb1SSatish Balay /* Overlap communication with computation. */ 142443a90d84SBarry Smith ierr = VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr); 1425a008b906SSatish Balay } 14264b967eb1SSatish Balay if (ll) { 1427e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 142829bbc08cSBarry Smith if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1429f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 14304b967eb1SSatish Balay } 14314b967eb1SSatish Balay /* scale the diagonal block */ 1432f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 14334b967eb1SSatish Balay 14344b967eb1SSatish Balay if (rr) { 14354b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 143643a90d84SBarry Smith ierr = VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);CHKERRQ(ierr); 1437f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 14384b967eb1SSatish Balay } 14394b967eb1SSatish Balay 14403a40ed3dSBarry Smith PetscFunctionReturn(0); 1441a008b906SSatish Balay } 1442a008b906SSatish Balay 1443a008b906SSatish Balay 14444a2ae208SSatish Balay #undef __FUNCT__ 14454a2ae208SSatish Balay #define __FUNCT__ "MatPrintHelp_MPIAIJ" 14468f6be9afSLois Curfman McInnes int MatPrintHelp_MPIAIJ(Mat A) 1447682d7d0cSBarry Smith { 1448682d7d0cSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 14493a40ed3dSBarry Smith int ierr; 1450682d7d0cSBarry Smith 14513a40ed3dSBarry Smith PetscFunctionBegin; 14523a40ed3dSBarry Smith if (!a->rank) { 14533a40ed3dSBarry Smith ierr = MatPrintHelp_SeqAIJ(a->A);CHKERRQ(ierr); 14543a40ed3dSBarry Smith } 14553a40ed3dSBarry Smith PetscFunctionReturn(0); 1456682d7d0cSBarry Smith } 1457682d7d0cSBarry Smith 14584a2ae208SSatish Balay #undef __FUNCT__ 14594a2ae208SSatish Balay #define __FUNCT__ "MatGetBlockSize_MPIAIJ" 14608f6be9afSLois Curfman McInnes int MatGetBlockSize_MPIAIJ(Mat A,int *bs) 14615a838052SSatish Balay { 14623a40ed3dSBarry Smith PetscFunctionBegin; 14635a838052SSatish Balay *bs = 1; 14643a40ed3dSBarry Smith PetscFunctionReturn(0); 14655a838052SSatish Balay } 14664a2ae208SSatish Balay #undef __FUNCT__ 14674a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 14688f6be9afSLois Curfman McInnes int MatSetUnfactored_MPIAIJ(Mat A) 1469bb5a7306SBarry Smith { 1470bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1471bb5a7306SBarry Smith int ierr; 14723a40ed3dSBarry Smith 14733a40ed3dSBarry Smith PetscFunctionBegin; 1474bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 14753a40ed3dSBarry Smith PetscFunctionReturn(0); 1476bb5a7306SBarry Smith } 1477bb5a7306SBarry Smith 14784a2ae208SSatish Balay #undef __FUNCT__ 14794a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 1480d4bb536fSBarry Smith int MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag) 1481d4bb536fSBarry Smith { 1482d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 1483d4bb536fSBarry Smith Mat a,b,c,d; 1484d4bb536fSBarry Smith PetscTruth flg; 1485d4bb536fSBarry Smith int ierr; 1486d4bb536fSBarry Smith 14873a40ed3dSBarry Smith PetscFunctionBegin; 1488d4bb536fSBarry Smith a = matA->A; b = matA->B; 1489d4bb536fSBarry Smith c = matB->A; d = matB->B; 1490d4bb536fSBarry Smith 1491d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 1492d4bb536fSBarry Smith if (flg == PETSC_TRUE) { 1493d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 1494d4bb536fSBarry Smith } 1495ca161407SBarry Smith ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr); 14963a40ed3dSBarry Smith PetscFunctionReturn(0); 1497d4bb536fSBarry Smith } 1498d4bb536fSBarry Smith 14994a2ae208SSatish Balay #undef __FUNCT__ 15004a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 1501cb5b572fSBarry Smith int MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1502cb5b572fSBarry Smith { 1503cb5b572fSBarry Smith int ierr; 1504cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1505cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1506cb5b572fSBarry Smith 1507cb5b572fSBarry Smith PetscFunctionBegin; 150833f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 150933f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 1510cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1511cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1512cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1513cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1514cb5b572fSBarry Smith then copying the submatrices */ 1515cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1516cb5b572fSBarry Smith } else { 1517cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1518cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1519cb5b572fSBarry Smith } 1520cb5b572fSBarry Smith PetscFunctionReturn(0); 1521cb5b572fSBarry Smith } 1522cb5b572fSBarry Smith 15234a2ae208SSatish Balay #undef __FUNCT__ 15244a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 1525273d9f13SBarry Smith int MatSetUpPreallocation_MPIAIJ(Mat A) 1526273d9f13SBarry Smith { 1527273d9f13SBarry Smith int ierr; 1528273d9f13SBarry Smith 1529273d9f13SBarry Smith PetscFunctionBegin; 1530273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 1531273d9f13SBarry Smith PetscFunctionReturn(0); 1532273d9f13SBarry Smith } 1533273d9f13SBarry Smith 1534ac90fabeSBarry Smith #include "petscblaslapack.h" 1535ac90fabeSBarry Smith #undef __FUNCT__ 1536ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 1537268466fbSBarry Smith int MatAXPY_MPIAIJ(const PetscScalar a[],Mat X,Mat Y,MatStructure str) 1538ac90fabeSBarry Smith { 153924f910e3SHong Zhang int ierr,one=1,i; 1540ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 1541ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 1542ac90fabeSBarry Smith 1543ac90fabeSBarry Smith PetscFunctionBegin; 1544ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 1545ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 1546ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 1547268466fbSBarry Smith BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one); 1548ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 1549ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 1550268466fbSBarry Smith BLaxpy_(&x->nz,(PetscScalar*)a,x->a,&one,y->a,&one); 1551a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 1552c537a176SHong Zhang ierr = MatAXPY_SeqAIJ(a,xx->A,yy->A,str);CHKERRQ(ierr); 1553c537a176SHong Zhang 1554c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 1555a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 1556a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 1557a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 1558a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 1559c537a176SHong Zhang } 1560a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 156124f910e3SHong Zhang ierr = MatAXPYGetxtoy_Private(xx->B->m,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 1562a30b2313SHong Zhang y->XtoY = xx->B; 1563c537a176SHong Zhang } 1564a30b2313SHong Zhang for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += (*a)*(x->a[i]); 1565ac90fabeSBarry Smith } else { 1566ac90fabeSBarry Smith ierr = MatAXPY_Basic(a,X,Y,str);CHKERRQ(ierr); 1567ac90fabeSBarry Smith } 1568ac90fabeSBarry Smith PetscFunctionReturn(0); 1569ac90fabeSBarry Smith } 1570ac90fabeSBarry Smith 15718a729477SBarry Smith /* -------------------------------------------------------------------*/ 1572cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 1573cda55fadSBarry Smith MatGetRow_MPIAIJ, 1574cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 1575cda55fadSBarry Smith MatMult_MPIAIJ, 157697304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 15777c922b88SBarry Smith MatMultTranspose_MPIAIJ, 15787c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 1579cda55fadSBarry Smith 0, 1580cda55fadSBarry Smith 0, 1581cda55fadSBarry Smith 0, 158297304618SKris Buschelman /*10*/ 0, 1583cda55fadSBarry Smith 0, 1584cda55fadSBarry Smith 0, 158544a69424SLois Curfman McInnes MatRelax_MPIAIJ, 1586b7c46309SBarry Smith MatTranspose_MPIAIJ, 158797304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 1588cda55fadSBarry Smith MatEqual_MPIAIJ, 1589cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 1590cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 1591cda55fadSBarry Smith MatNorm_MPIAIJ, 159297304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 1593cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 15941eb62cbbSBarry Smith 0, 1595cda55fadSBarry Smith MatSetOption_MPIAIJ, 1596cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 159797304618SKris Buschelman /*25*/ MatZeroRows_MPIAIJ, 159887828ca2SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) 1599b9617806SBarry Smith MatLUFactorSymbolic_MPIAIJ_TFS, 1600b9617806SBarry Smith #else 1601cda55fadSBarry Smith 0, 1602b9617806SBarry Smith #endif 1603cda55fadSBarry Smith 0, 1604cda55fadSBarry Smith 0, 1605cda55fadSBarry Smith 0, 160697304618SKris Buschelman /*30*/ MatSetUpPreallocation_MPIAIJ, 1607cda55fadSBarry Smith 0, 1608cda55fadSBarry Smith 0, 1609cda55fadSBarry Smith 0, 1610cda55fadSBarry Smith 0, 161197304618SKris Buschelman /*35*/ MatDuplicate_MPIAIJ, 1612cda55fadSBarry Smith 0, 1613cda55fadSBarry Smith 0, 1614cda55fadSBarry Smith 0, 1615cda55fadSBarry Smith 0, 161697304618SKris Buschelman /*40*/ MatAXPY_MPIAIJ, 1617cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 1618cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 1619cda55fadSBarry Smith MatGetValues_MPIAIJ, 1620cb5b572fSBarry Smith MatCopy_MPIAIJ, 162197304618SKris Buschelman /*45*/ MatPrintHelp_MPIAIJ, 1622cda55fadSBarry Smith MatScale_MPIAIJ, 1623cda55fadSBarry Smith 0, 1624cda55fadSBarry Smith 0, 1625cda55fadSBarry Smith 0, 162697304618SKris Buschelman /*50*/ MatGetBlockSize_MPIAIJ, 1627cda55fadSBarry Smith 0, 1628cda55fadSBarry Smith 0, 1629cda55fadSBarry Smith 0, 1630cda55fadSBarry Smith 0, 163197304618SKris Buschelman /*55*/ MatFDColoringCreate_MPIAIJ, 1632cda55fadSBarry Smith 0, 1633cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 1634cda55fadSBarry Smith 0, 1635cda55fadSBarry Smith 0, 163697304618SKris Buschelman /*60*/ MatGetSubMatrix_MPIAIJ, 1637e03a110bSBarry Smith MatDestroy_MPIAIJ, 1638e03a110bSBarry Smith MatView_MPIAIJ, 16398a124369SBarry Smith MatGetPetscMaps_Petsc, 1640a2243be0SBarry Smith 0, 164197304618SKris Buschelman /*65*/ 0, 1642a2243be0SBarry Smith 0, 1643a2243be0SBarry Smith 0, 1644a2243be0SBarry Smith 0, 1645a2243be0SBarry Smith 0, 164697304618SKris Buschelman /*70*/ 0, 1647a2243be0SBarry Smith 0, 1648a2243be0SBarry Smith MatSetColoring_MPIAIJ, 1649779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 165097304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 165197304618SKris Buschelman /*75*/ 0, 165297304618SKris Buschelman 0, 165397304618SKris Buschelman 0, 165497304618SKris Buschelman 0, 165597304618SKris Buschelman 0, 165697304618SKris Buschelman /*80*/ 0, 165797304618SKris Buschelman 0, 165897304618SKris Buschelman 0, 165997304618SKris Buschelman 0, 166097304618SKris Buschelman /*85*/ MatLoad_MPIAIJ}; 166136ce4990SBarry Smith 16622e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 16632e8a6d31SBarry Smith 1664fb2e594dSBarry Smith EXTERN_C_BEGIN 16654a2ae208SSatish Balay #undef __FUNCT__ 16664a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 16672e8a6d31SBarry Smith int MatStoreValues_MPIAIJ(Mat mat) 16682e8a6d31SBarry Smith { 16692e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 16702e8a6d31SBarry Smith int ierr; 16712e8a6d31SBarry Smith 16722e8a6d31SBarry Smith PetscFunctionBegin; 16732e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 16742e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 16752e8a6d31SBarry Smith PetscFunctionReturn(0); 16762e8a6d31SBarry Smith } 1677fb2e594dSBarry Smith EXTERN_C_END 16782e8a6d31SBarry Smith 1679fb2e594dSBarry Smith EXTERN_C_BEGIN 16804a2ae208SSatish Balay #undef __FUNCT__ 16814a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 16822e8a6d31SBarry Smith int MatRetrieveValues_MPIAIJ(Mat mat) 16832e8a6d31SBarry Smith { 16842e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 16852e8a6d31SBarry Smith int ierr; 16862e8a6d31SBarry Smith 16872e8a6d31SBarry Smith PetscFunctionBegin; 16882e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 16892e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 16902e8a6d31SBarry Smith PetscFunctionReturn(0); 16912e8a6d31SBarry Smith } 1692fb2e594dSBarry Smith EXTERN_C_END 16938a729477SBarry Smith 1694e090d566SSatish Balay #include "petscpc.h" 169527508adbSBarry Smith EXTERN_C_BEGIN 16964a2ae208SSatish Balay #undef __FUNCT__ 1697a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 1698d10c748bSKris Buschelman int MatMPIAIJSetPreallocation_MPIAIJ(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[]) 1699a23d5eceSKris Buschelman { 1700a23d5eceSKris Buschelman Mat_MPIAIJ *b; 1701a23d5eceSKris Buschelman int ierr,i; 1702a23d5eceSKris Buschelman 1703a23d5eceSKris Buschelman PetscFunctionBegin; 1704a23d5eceSKris Buschelman B->preallocated = PETSC_TRUE; 1705a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 1706a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 1707a23d5eceSKris Buschelman if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz); 1708a23d5eceSKris Buschelman if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz); 1709a23d5eceSKris Buschelman if (d_nnz) { 1710a23d5eceSKris Buschelman for (i=0; i<B->m; i++) { 1711a23d5eceSKris Buschelman if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %d value %d",i,d_nnz[i]); 1712a23d5eceSKris Buschelman } 1713a23d5eceSKris Buschelman } 1714a23d5eceSKris Buschelman if (o_nnz) { 1715a23d5eceSKris Buschelman for (i=0; i<B->m; i++) { 1716a23d5eceSKris Buschelman if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %d value %d",i,o_nnz[i]); 1717a23d5eceSKris Buschelman } 1718a23d5eceSKris Buschelman } 1719a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 1720c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 1721c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 1722a23d5eceSKris Buschelman 1723a23d5eceSKris Buschelman PetscFunctionReturn(0); 1724a23d5eceSKris Buschelman } 1725a23d5eceSKris Buschelman EXTERN_C_END 1726a23d5eceSKris Buschelman 17270bad9183SKris Buschelman /*MC 1728fafad747SKris Buschelman MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 17290bad9183SKris Buschelman 17300bad9183SKris Buschelman Options Database Keys: 17310bad9183SKris Buschelman . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 17320bad9183SKris Buschelman 17330bad9183SKris Buschelman Level: beginner 17340bad9183SKris Buschelman 17350bad9183SKris Buschelman .seealso: MatCreateMPIAIJ 17360bad9183SKris Buschelman M*/ 17370bad9183SKris Buschelman 1738a23d5eceSKris Buschelman EXTERN_C_BEGIN 1739a23d5eceSKris Buschelman #undef __FUNCT__ 17404a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAIJ" 1741273d9f13SBarry Smith int MatCreate_MPIAIJ(Mat B) 17428a729477SBarry Smith { 174344cd7ae7SLois Curfman McInnes Mat_MPIAIJ *b; 1744f1af5d2fSBarry Smith int ierr,i,size; 1745416022c9SBarry Smith 17463a40ed3dSBarry Smith PetscFunctionBegin; 1747273d9f13SBarry Smith ierr = MPI_Comm_size(B->comm,&size);CHKERRQ(ierr); 17481d55c564SBarry Smith 1749b0a32e0cSBarry Smith ierr = PetscNew(Mat_MPIAIJ,&b);CHKERRQ(ierr); 1750b0a32e0cSBarry Smith B->data = (void*)b; 1751549d3d68SSatish Balay ierr = PetscMemzero(b,sizeof(Mat_MPIAIJ));CHKERRQ(ierr); 1752549d3d68SSatish Balay ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 175344cd7ae7SLois Curfman McInnes B->factor = 0; 175444cd7ae7SLois Curfman McInnes B->assembled = PETSC_FALSE; 175590f02eecSBarry Smith B->mapping = 0; 1756d6dfbf8fSBarry Smith 175747794344SBarry Smith B->insertmode = NOT_SET_VALUES; 17589eb4d147SSatish Balay b->size = size; 1759273d9f13SBarry Smith ierr = MPI_Comm_rank(B->comm,&b->rank);CHKERRQ(ierr); 17601eb62cbbSBarry Smith 1761273d9f13SBarry Smith ierr = PetscSplitOwnership(B->comm,&B->m,&B->M);CHKERRQ(ierr); 1762273d9f13SBarry Smith ierr = PetscSplitOwnership(B->comm,&B->n,&B->N);CHKERRQ(ierr); 17631eb62cbbSBarry Smith 1764c7fcc2eaSBarry Smith /* the information in the maps duplicates the information computed below, eventually 1765c7fcc2eaSBarry Smith we should remove the duplicate information that is not contained in the maps */ 17668a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->m,B->M,&B->rmap);CHKERRQ(ierr); 17678a124369SBarry Smith ierr = PetscMapCreateMPI(B->comm,B->n,B->N,&B->cmap);CHKERRQ(ierr); 1768c7fcc2eaSBarry Smith 17691eb62cbbSBarry Smith /* build local table of row and column ownerships */ 1770b0a32e0cSBarry Smith ierr = PetscMalloc(2*(b->size+2)*sizeof(int),&b->rowners);CHKERRQ(ierr); 1771b0a32e0cSBarry Smith PetscLogObjectMemory(B,2*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIAIJ)); 1772603f58a4SSatish Balay b->cowners = b->rowners + b->size + 2; 1773273d9f13SBarry Smith ierr = MPI_Allgather(&B->m,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr); 177444cd7ae7SLois Curfman McInnes b->rowners[0] = 0; 177544cd7ae7SLois Curfman McInnes for (i=2; i<=b->size; i++) { 177644cd7ae7SLois Curfman McInnes b->rowners[i] += b->rowners[i-1]; 17778a729477SBarry Smith } 177844cd7ae7SLois Curfman McInnes b->rstart = b->rowners[b->rank]; 177944cd7ae7SLois Curfman McInnes b->rend = b->rowners[b->rank+1]; 1780273d9f13SBarry Smith ierr = MPI_Allgather(&B->n,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);CHKERRQ(ierr); 178144cd7ae7SLois Curfman McInnes b->cowners[0] = 0; 178244cd7ae7SLois Curfman McInnes for (i=2; i<=b->size; i++) { 178344cd7ae7SLois Curfman McInnes b->cowners[i] += b->cowners[i-1]; 17848a729477SBarry Smith } 178544cd7ae7SLois Curfman McInnes b->cstart = b->cowners[b->rank]; 178644cd7ae7SLois Curfman McInnes b->cend = b->cowners[b->rank+1]; 17878a729477SBarry Smith 17881eb62cbbSBarry Smith /* build cache for off array entries formed */ 17897e2c5f70SBarry Smith ierr = MatStashCreate_Private(B->comm,1,&B->stash);CHKERRQ(ierr); 17907c922b88SBarry Smith b->donotstash = PETSC_FALSE; 179144cd7ae7SLois Curfman McInnes b->colmap = 0; 179244cd7ae7SLois Curfman McInnes b->garray = 0; 17937c922b88SBarry Smith b->roworiented = PETSC_TRUE; 17948a729477SBarry Smith 17951eb62cbbSBarry Smith /* stuff used for matrix vector multiply */ 17967c922b88SBarry Smith b->lvec = PETSC_NULL; 17977c922b88SBarry Smith b->Mvctx = PETSC_NULL; 17988a729477SBarry Smith 17997a0afa10SBarry Smith /* stuff for MatGetRow() */ 180044cd7ae7SLois Curfman McInnes b->rowindices = 0; 180144cd7ae7SLois Curfman McInnes b->rowvalues = 0; 180244cd7ae7SLois Curfman McInnes b->getrowactive = PETSC_FALSE; 18031a8d6bc9SBarry Smith 1804be5d1d56SKris Buschelman /* Explicitly create 2 MATSEQAIJ matrices. */ 18059c097c71SKris Buschelman ierr = MatCreate(PETSC_COMM_SELF,B->m,B->n,B->m,B->n,&b->A);CHKERRQ(ierr); 1806c60e587dSKris Buschelman ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 1807c60e587dSKris Buschelman PetscLogObjectParent(B,b->A); 18089c097c71SKris Buschelman ierr = MatCreate(PETSC_COMM_SELF,B->m,B->N,B->m,B->N,&b->B);CHKERRQ(ierr); 1809c60e587dSKris Buschelman ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 1810c60e587dSKris Buschelman PetscLogObjectParent(B,b->B); 1811c60e587dSKris Buschelman 1812f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 18132e8a6d31SBarry Smith "MatStoreValues_MPIAIJ", 18140c97f09dSSatish Balay MatStoreValues_MPIAIJ);CHKERRQ(ierr); 1815f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 18162e8a6d31SBarry Smith "MatRetrieveValues_MPIAIJ", 18170c97f09dSSatish Balay MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 1818f1af5d2fSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 18195ef9f2a5SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 18200c97f09dSSatish Balay MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 18215fbd3699SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 18225fbd3699SBarry Smith "MatIsTranspose_MPIAIJ", 18235fbd3699SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 1824a23d5eceSKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 1825a23d5eceSKris Buschelman "MatMPIAIJSetPreallocation_MPIAIJ", 1826a23d5eceSKris Buschelman MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 182792b32695SKris Buschelman ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 182892b32695SKris Buschelman "MatDiagonalScaleLocal_MPIAIJ", 182992b32695SKris Buschelman MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 18303a40ed3dSBarry Smith PetscFunctionReturn(0); 1831d6dfbf8fSBarry Smith } 1832273d9f13SBarry Smith EXTERN_C_END 1833c74985f6SBarry Smith 1834209238afSKris Buschelman /*MC 1835002d173eSKris Buschelman MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 1836209238afSKris Buschelman 1837209238afSKris Buschelman This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 183830e3259aSHong Zhang and MATMPIAIJ otherwise. As a result, for single process communicators, 183930e3259aSHong Zhang MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 184030e3259aSHong Zhang for communicators controlling multiple processes. It is recommended that you call both of 184130e3259aSHong Zhang the above preallocation routines for simplicity. 1842209238afSKris Buschelman 1843209238afSKris Buschelman Options Database Keys: 1844209238afSKris Buschelman . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 1845209238afSKris Buschelman 1846209238afSKris Buschelman Level: beginner 1847209238afSKris Buschelman 1848209238afSKris Buschelman .seealso: MatCreateMPIAIJ,MATSEQAIJ,MATMPIAIJ 1849209238afSKris Buschelman M*/ 1850209238afSKris Buschelman 1851209238afSKris Buschelman EXTERN_C_BEGIN 1852209238afSKris Buschelman #undef __FUNCT__ 1853209238afSKris Buschelman #define __FUNCT__ "MatCreate_AIJ" 1854209238afSKris Buschelman int MatCreate_AIJ(Mat A) { 1855209238afSKris Buschelman int ierr,size; 1856209238afSKris Buschelman 1857209238afSKris Buschelman PetscFunctionBegin; 1858209238afSKris Buschelman ierr = PetscObjectChangeTypeName((PetscObject)A,MATAIJ);CHKERRQ(ierr); 1859209238afSKris Buschelman ierr = MPI_Comm_size(A->comm,&size);CHKERRQ(ierr); 1860209238afSKris Buschelman if (size == 1) { 1861209238afSKris Buschelman ierr = MatSetType(A,MATSEQAIJ);CHKERRQ(ierr); 1862209238afSKris Buschelman } else { 1863209238afSKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1864209238afSKris Buschelman } 1865209238afSKris Buschelman PetscFunctionReturn(0); 1866209238afSKris Buschelman } 1867209238afSKris Buschelman EXTERN_C_END 1868209238afSKris Buschelman 18694a2ae208SSatish Balay #undef __FUNCT__ 18704a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 18712e8a6d31SBarry Smith int MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 1872d6dfbf8fSBarry Smith { 1873d6dfbf8fSBarry Smith Mat mat; 1874416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 187516d6aa21SSatish Balay int ierr; 1876d6dfbf8fSBarry Smith 18773a40ed3dSBarry Smith PetscFunctionBegin; 1878416022c9SBarry Smith *newmat = 0; 1879273d9f13SBarry Smith ierr = MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);CHKERRQ(ierr); 1880be5d1d56SKris Buschelman ierr = MatSetType(mat,matin->type_name);CHKERRQ(ierr); 1881*1d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 1882273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 1883e1b6402fSHong Zhang 1884d6dfbf8fSBarry Smith mat->factor = matin->factor; 1885c456f294SBarry Smith mat->assembled = PETSC_TRUE; 1886e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 1887273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 1888d6dfbf8fSBarry Smith 1889416022c9SBarry Smith a->rstart = oldmat->rstart; 1890416022c9SBarry Smith a->rend = oldmat->rend; 1891416022c9SBarry Smith a->cstart = oldmat->cstart; 1892416022c9SBarry Smith a->cend = oldmat->cend; 189317699dbbSLois Curfman McInnes a->size = oldmat->size; 189417699dbbSLois Curfman McInnes a->rank = oldmat->rank; 1895e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 1896e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 1897e7641de0SSatish Balay a->rowindices = 0; 1898bcd2baecSBarry Smith a->rowvalues = 0; 1899bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 1900d6dfbf8fSBarry Smith 1901549d3d68SSatish Balay ierr = PetscMemcpy(a->rowners,oldmat->rowners,2*(a->size+2)*sizeof(int));CHKERRQ(ierr); 19028798bf22SSatish Balay ierr = MatStashCreate_Private(matin->comm,1,&mat->stash);CHKERRQ(ierr); 19032ee70a88SLois Curfman McInnes if (oldmat->colmap) { 1904aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 19050f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 1906b1fc9764SSatish Balay #else 1907b0a32e0cSBarry Smith ierr = PetscMalloc((mat->N)*sizeof(int),&a->colmap);CHKERRQ(ierr); 1908b0a32e0cSBarry Smith PetscLogObjectMemory(mat,(mat->N)*sizeof(int)); 1909273d9f13SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->N)*sizeof(int));CHKERRQ(ierr); 1910b1fc9764SSatish Balay #endif 1911416022c9SBarry Smith } else a->colmap = 0; 19123f41c07dSBarry Smith if (oldmat->garray) { 191316d6aa21SSatish Balay int len; 1914273d9f13SBarry Smith len = oldmat->B->n; 1915b0a32e0cSBarry Smith ierr = PetscMalloc((len+1)*sizeof(int),&a->garray);CHKERRQ(ierr); 1916b0a32e0cSBarry Smith PetscLogObjectMemory(mat,len*sizeof(int)); 1917549d3d68SSatish Balay if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));CHKERRQ(ierr); } 1918416022c9SBarry Smith } else a->garray = 0; 1919d6dfbf8fSBarry Smith 1920416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 1921b0a32e0cSBarry Smith PetscLogObjectParent(mat,a->lvec); 1922a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 1923b0a32e0cSBarry Smith PetscLogObjectParent(mat,a->Mvctx); 19244efcb820SBarry Smith ierr = MatDestroy(a->A);CHKERRQ(ierr); 19252e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 1926b0a32e0cSBarry Smith PetscLogObjectParent(mat,a->A); 19274efcb820SBarry Smith ierr = MatDestroy(a->B);CHKERRQ(ierr); 19282e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 1929b0a32e0cSBarry Smith PetscLogObjectParent(mat,a->B); 1930b0a32e0cSBarry Smith ierr = PetscFListDuplicate(matin->qlist,&mat->qlist);CHKERRQ(ierr); 19318a729477SBarry Smith *newmat = mat; 19323a40ed3dSBarry Smith PetscFunctionReturn(0); 19338a729477SBarry Smith } 1934416022c9SBarry Smith 1935e090d566SSatish Balay #include "petscsys.h" 1936416022c9SBarry Smith 19374a2ae208SSatish Balay #undef __FUNCT__ 19384a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ" 19398e9aea5cSBarry Smith int MatLoad_MPIAIJ(PetscViewer viewer,const MatType type,Mat *newmat) 1940416022c9SBarry Smith { 1941d65a2f8fSBarry Smith Mat A; 194287828ca2SBarry Smith PetscScalar *vals,*svals; 194319bcc07fSBarry Smith MPI_Comm comm = ((PetscObject)viewer)->comm; 1944416022c9SBarry Smith MPI_Status status; 19453a40ed3dSBarry Smith int i,nz,ierr,j,rstart,rend,fd; 194617699dbbSLois Curfman McInnes int header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,maxnz,*cols; 1947d65a2f8fSBarry Smith int *ourlens,*sndcounts = 0,*procsnz = 0,*offlens,jj,*mycols,*smycols; 1948b362ba68SBarry Smith int tag = ((PetscObject)viewer)->tag,cend,cstart,n; 1949416022c9SBarry Smith 19503a40ed3dSBarry Smith PetscFunctionBegin; 19511dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 19521dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 195317699dbbSLois Curfman McInnes if (!rank) { 1954b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 19550752156aSBarry Smith ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 1956552e946dSBarry Smith if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 1957d64ed03dSBarry Smith if (header[3] < 0) { 195829bbc08cSBarry Smith SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix in special format on disk, cannot load as MPIAIJ"); 1959d64ed03dSBarry Smith } 19606c5fab8fSBarry Smith } 19616c5fab8fSBarry Smith 1962ca161407SBarry Smith ierr = MPI_Bcast(header+1,3,MPI_INT,0,comm);CHKERRQ(ierr); 1963416022c9SBarry Smith M = header[1]; N = header[2]; 1964416022c9SBarry Smith /* determine ownership of all rows */ 196517699dbbSLois Curfman McInnes m = M/size + ((M % size) > rank); 1966b0a32e0cSBarry Smith ierr = PetscMalloc((size+2)*sizeof(int),&rowners);CHKERRQ(ierr); 1967ca161407SBarry Smith ierr = MPI_Allgather(&m,1,MPI_INT,rowners+1,1,MPI_INT,comm);CHKERRQ(ierr); 1968416022c9SBarry Smith rowners[0] = 0; 196917699dbbSLois Curfman McInnes for (i=2; i<=size; i++) { 1970416022c9SBarry Smith rowners[i] += rowners[i-1]; 1971416022c9SBarry Smith } 197217699dbbSLois Curfman McInnes rstart = rowners[rank]; 197317699dbbSLois Curfman McInnes rend = rowners[rank+1]; 1974416022c9SBarry Smith 1975416022c9SBarry Smith /* distribute row lengths to all processors */ 1976b0a32e0cSBarry Smith ierr = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&ourlens);CHKERRQ(ierr); 1977416022c9SBarry Smith offlens = ourlens + (rend-rstart); 197817699dbbSLois Curfman McInnes if (!rank) { 1979b0a32e0cSBarry Smith ierr = PetscMalloc(M*sizeof(int),&rowlengths);CHKERRQ(ierr); 19800752156aSBarry Smith ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr); 1981b0a32e0cSBarry Smith ierr = PetscMalloc(size*sizeof(int),&sndcounts);CHKERRQ(ierr); 198217699dbbSLois Curfman McInnes for (i=0; i<size; i++) sndcounts[i] = rowners[i+1] - rowners[i]; 1983ca161407SBarry Smith ierr = MPI_Scatterv(rowlengths,sndcounts,rowners,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr); 1984606d414cSSatish Balay ierr = PetscFree(sndcounts);CHKERRQ(ierr); 19853a40ed3dSBarry Smith } else { 1986ca161407SBarry Smith ierr = MPI_Scatterv(0,0,0,MPI_INT,ourlens,rend-rstart,MPI_INT,0,comm);CHKERRQ(ierr); 1987416022c9SBarry Smith } 1988416022c9SBarry Smith 198917699dbbSLois Curfman McInnes if (!rank) { 1990416022c9SBarry Smith /* calculate the number of nonzeros on each processor */ 1991b0a32e0cSBarry Smith ierr = PetscMalloc(size*sizeof(int),&procsnz);CHKERRQ(ierr); 1992549d3d68SSatish Balay ierr = PetscMemzero(procsnz,size*sizeof(int));CHKERRQ(ierr); 199317699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 1994416022c9SBarry Smith for (j=rowners[i]; j< rowners[i+1]; j++) { 1995416022c9SBarry Smith procsnz[i] += rowlengths[j]; 1996416022c9SBarry Smith } 1997416022c9SBarry Smith } 1998606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 1999416022c9SBarry Smith 2000416022c9SBarry Smith /* determine max buffer needed and allocate it */ 2001416022c9SBarry Smith maxnz = 0; 200217699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 20030452661fSBarry Smith maxnz = PetscMax(maxnz,procsnz[i]); 2004416022c9SBarry Smith } 2005b0a32e0cSBarry Smith ierr = PetscMalloc(maxnz*sizeof(int),&cols);CHKERRQ(ierr); 2006416022c9SBarry Smith 2007416022c9SBarry Smith /* read in my part of the matrix column indices */ 2008416022c9SBarry Smith nz = procsnz[0]; 2009b0a32e0cSBarry Smith ierr = PetscMalloc(nz*sizeof(int),&mycols);CHKERRQ(ierr); 20100752156aSBarry Smith ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 2011d65a2f8fSBarry Smith 2012d65a2f8fSBarry Smith /* read in every one elses and ship off */ 201317699dbbSLois Curfman McInnes for (i=1; i<size; i++) { 2014d65a2f8fSBarry Smith nz = procsnz[i]; 20150752156aSBarry Smith ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 2016ca161407SBarry Smith ierr = MPI_Send(cols,nz,MPI_INT,i,tag,comm);CHKERRQ(ierr); 2017d65a2f8fSBarry Smith } 2018606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 20193a40ed3dSBarry Smith } else { 2020416022c9SBarry Smith /* determine buffer space needed for message */ 2021416022c9SBarry Smith nz = 0; 2022416022c9SBarry Smith for (i=0; i<m; i++) { 2023416022c9SBarry Smith nz += ourlens[i]; 2024416022c9SBarry Smith } 2025b0a32e0cSBarry Smith ierr = PetscMalloc((nz+1)*sizeof(int),&mycols);CHKERRQ(ierr); 2026416022c9SBarry Smith 2027416022c9SBarry Smith /* receive message of column indices*/ 2028ca161407SBarry Smith ierr = MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 2029ca161407SBarry Smith ierr = MPI_Get_count(&status,MPI_INT,&maxnz);CHKERRQ(ierr); 203029bbc08cSBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file"); 2031416022c9SBarry Smith } 2032416022c9SBarry Smith 2033b362ba68SBarry Smith /* determine column ownership if matrix is not square */ 2034b362ba68SBarry Smith if (N != M) { 2035b362ba68SBarry Smith n = N/size + ((N % size) > rank); 2036b362ba68SBarry Smith ierr = MPI_Scan(&n,&cend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 2037b362ba68SBarry Smith cstart = cend - n; 2038b362ba68SBarry Smith } else { 2039b362ba68SBarry Smith cstart = rstart; 2040b362ba68SBarry Smith cend = rend; 2041fb2e594dSBarry Smith n = cend - cstart; 2042b362ba68SBarry Smith } 2043b362ba68SBarry Smith 2044416022c9SBarry Smith /* loop over local rows, determining number of off diagonal entries */ 2045549d3d68SSatish Balay ierr = PetscMemzero(offlens,m*sizeof(int));CHKERRQ(ierr); 2046416022c9SBarry Smith jj = 0; 2047416022c9SBarry Smith for (i=0; i<m; i++) { 2048416022c9SBarry Smith for (j=0; j<ourlens[i]; j++) { 2049b362ba68SBarry Smith if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 2050416022c9SBarry Smith jj++; 2051416022c9SBarry Smith } 2052416022c9SBarry Smith } 2053d65a2f8fSBarry Smith 2054d65a2f8fSBarry Smith /* create our matrix */ 2055416022c9SBarry Smith for (i=0; i<m; i++) { 2056416022c9SBarry Smith ourlens[i] -= offlens[i]; 2057416022c9SBarry Smith } 2058d10c748bSKris Buschelman ierr = MatCreate(comm,m,n,M,N,&A);CHKERRQ(ierr); 2059d10c748bSKris Buschelman ierr = MatSetType(A,type);CHKERRQ(ierr); 2060d10c748bSKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr); 2061d10c748bSKris Buschelman 2062fb2e594dSBarry Smith ierr = MatSetOption(A,MAT_COLUMNS_SORTED);CHKERRQ(ierr); 2063d65a2f8fSBarry Smith for (i=0; i<m; i++) { 2064d65a2f8fSBarry Smith ourlens[i] += offlens[i]; 2065d65a2f8fSBarry Smith } 2066416022c9SBarry Smith 206717699dbbSLois Curfman McInnes if (!rank) { 206887828ca2SBarry Smith ierr = PetscMalloc(maxnz*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 2069416022c9SBarry Smith 2070416022c9SBarry Smith /* read in my part of the matrix numerical values */ 2071416022c9SBarry Smith nz = procsnz[0]; 20720752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 2073d65a2f8fSBarry Smith 2074d65a2f8fSBarry Smith /* insert into matrix */ 2075d65a2f8fSBarry Smith jj = rstart; 2076d65a2f8fSBarry Smith smycols = mycols; 2077d65a2f8fSBarry Smith svals = vals; 2078d65a2f8fSBarry Smith for (i=0; i<m; i++) { 2079d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2080d65a2f8fSBarry Smith smycols += ourlens[i]; 2081d65a2f8fSBarry Smith svals += ourlens[i]; 2082d65a2f8fSBarry Smith jj++; 2083416022c9SBarry Smith } 2084416022c9SBarry Smith 2085d65a2f8fSBarry Smith /* read in other processors and ship out */ 208617699dbbSLois Curfman McInnes for (i=1; i<size; i++) { 2087416022c9SBarry Smith nz = procsnz[i]; 20880752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 2089ca161407SBarry Smith ierr = MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);CHKERRQ(ierr); 2090416022c9SBarry Smith } 2091606d414cSSatish Balay ierr = PetscFree(procsnz);CHKERRQ(ierr); 20923a40ed3dSBarry Smith } else { 2093d65a2f8fSBarry Smith /* receive numeric values */ 209487828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 2095416022c9SBarry Smith 2096d65a2f8fSBarry Smith /* receive message of values*/ 2097ca161407SBarry Smith ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);CHKERRQ(ierr); 2098ca161407SBarry Smith ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);CHKERRQ(ierr); 209929bbc08cSBarry Smith if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file"); 2100d65a2f8fSBarry Smith 2101d65a2f8fSBarry Smith /* insert into matrix */ 2102d65a2f8fSBarry Smith jj = rstart; 2103d65a2f8fSBarry Smith smycols = mycols; 2104d65a2f8fSBarry Smith svals = vals; 2105d65a2f8fSBarry Smith for (i=0; i<m; i++) { 2106d65a2f8fSBarry Smith ierr = MatSetValues(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 2107d65a2f8fSBarry Smith smycols += ourlens[i]; 2108d65a2f8fSBarry Smith svals += ourlens[i]; 2109d65a2f8fSBarry Smith jj++; 2110d65a2f8fSBarry Smith } 2111d65a2f8fSBarry Smith } 2112606d414cSSatish Balay ierr = PetscFree(ourlens);CHKERRQ(ierr); 2113606d414cSSatish Balay ierr = PetscFree(vals);CHKERRQ(ierr); 2114606d414cSSatish Balay ierr = PetscFree(mycols);CHKERRQ(ierr); 2115606d414cSSatish Balay ierr = PetscFree(rowners);CHKERRQ(ierr); 2116d65a2f8fSBarry Smith 21176d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21186d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2119d10c748bSKris Buschelman *newmat = A; 21203a40ed3dSBarry Smith PetscFunctionReturn(0); 2121416022c9SBarry Smith } 2122a0ff6018SBarry Smith 21234a2ae208SSatish Balay #undef __FUNCT__ 21244a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 2125a0ff6018SBarry Smith /* 212629da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 212729da9460SBarry Smith in local and then by concatenating the local matrices the end result. 212829da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 2129a0ff6018SBarry Smith */ 21307b2a1423SBarry Smith int MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,int csize,MatReuse call,Mat *newmat) 2131a0ff6018SBarry Smith { 213200e6dbe6SBarry Smith int ierr,i,m,n,rstart,row,rend,nz,*cwork,size,rank,j; 2133ab50ec6bSBarry Smith int *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 2134fee21e36SBarry Smith Mat *local,M,Mreuse; 213587828ca2SBarry Smith PetscScalar *vwork,*aa; 213600e6dbe6SBarry Smith MPI_Comm comm = mat->comm; 213700e6dbe6SBarry Smith Mat_SeqAIJ *aij; 21387e2c5f70SBarry Smith 2139a0ff6018SBarry Smith 2140a0ff6018SBarry Smith PetscFunctionBegin; 21411dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 21421dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 214300e6dbe6SBarry Smith 2144fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 2145fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 214629bbc08cSBarry Smith if (!Mreuse) SETERRQ(1,"Submatrix passed in was not used before, cannot reuse"); 2147fee21e36SBarry Smith local = &Mreuse; 2148fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 2149fee21e36SBarry Smith } else { 2150a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 2151fee21e36SBarry Smith Mreuse = *local; 2152606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 2153fee21e36SBarry Smith } 2154a0ff6018SBarry Smith 2155a0ff6018SBarry Smith /* 2156a0ff6018SBarry Smith m - number of local rows 2157a0ff6018SBarry Smith n - number of columns (same on all processors) 2158a0ff6018SBarry Smith rstart - first row in new global matrix generated 2159a0ff6018SBarry Smith */ 2160fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 2161a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2162fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 216300e6dbe6SBarry Smith ii = aij->i; 216400e6dbe6SBarry Smith jj = aij->j; 216500e6dbe6SBarry Smith 2166a0ff6018SBarry Smith /* 216700e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 216800e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 2169a0ff6018SBarry Smith */ 217000e6dbe6SBarry Smith 217100e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 21726a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 2173ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 2174ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 2175e2c4fddaSBarry Smith nlocal = m; 21766a6a5d1dSBarry Smith } else { 2177ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 2178ab50ec6bSBarry Smith } 2179ab50ec6bSBarry Smith } else { 21806a6a5d1dSBarry Smith nlocal = csize; 21816a6a5d1dSBarry Smith } 2182ca161407SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr); 218300e6dbe6SBarry Smith rstart = rend - nlocal; 21846a6a5d1dSBarry Smith if (rank == size - 1 && rend != n) { 218519e8cfbbSBarry Smith SETERRQ2(1,"Local column sizes %d do not add up to total number of columns %d",rend,n); 21866a6a5d1dSBarry Smith } 218700e6dbe6SBarry Smith 218800e6dbe6SBarry Smith /* next, compute all the lengths */ 2189b0a32e0cSBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(int),&dlens);CHKERRQ(ierr); 219000e6dbe6SBarry Smith olens = dlens + m; 219100e6dbe6SBarry Smith for (i=0; i<m; i++) { 219200e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 219300e6dbe6SBarry Smith olen = 0; 219400e6dbe6SBarry Smith dlen = 0; 219500e6dbe6SBarry Smith for (j=0; j<jend; j++) { 219600e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 219700e6dbe6SBarry Smith else dlen++; 219800e6dbe6SBarry Smith jj++; 219900e6dbe6SBarry Smith } 220000e6dbe6SBarry Smith olens[i] = olen; 220100e6dbe6SBarry Smith dlens[i] = dlen; 220200e6dbe6SBarry Smith } 2203e2d9671bSKris Buschelman ierr = MatCreate(comm,m,nlocal,PETSC_DECIDE,n,&M);CHKERRQ(ierr); 2204e2d9671bSKris Buschelman ierr = MatSetType(M,mat->type_name);CHKERRQ(ierr); 2205e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 2206606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 2207a0ff6018SBarry Smith } else { 2208a0ff6018SBarry Smith int ml,nl; 2209a0ff6018SBarry Smith 2210a0ff6018SBarry Smith M = *newmat; 2211a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 221229bbc08cSBarry Smith if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 2213a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 2214c48de900SBarry Smith /* 2215c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 2216c48de900SBarry Smith rather than the slower MatSetValues(). 2217c48de900SBarry Smith */ 2218c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 2219c48de900SBarry Smith M->assembled = PETSC_FALSE; 2220a0ff6018SBarry Smith } 2221a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 2222fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 222300e6dbe6SBarry Smith ii = aij->i; 222400e6dbe6SBarry Smith jj = aij->j; 222500e6dbe6SBarry Smith aa = aij->a; 2226a0ff6018SBarry Smith for (i=0; i<m; i++) { 2227a0ff6018SBarry Smith row = rstart + i; 222800e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 222900e6dbe6SBarry Smith cwork = jj; jj += nz; 223000e6dbe6SBarry Smith vwork = aa; aa += nz; 22318c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 2232a0ff6018SBarry Smith } 2233a0ff6018SBarry Smith 2234a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2235a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2236a0ff6018SBarry Smith *newmat = M; 2237fee21e36SBarry Smith 2238fee21e36SBarry Smith /* save submatrix used in processor for next request */ 2239fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 2240fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 2241fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 2242fee21e36SBarry Smith } 2243fee21e36SBarry Smith 2244a0ff6018SBarry Smith PetscFunctionReturn(0); 2245a0ff6018SBarry Smith } 2246273d9f13SBarry Smith 22474a2ae208SSatish Balay #undef __FUNCT__ 22484a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 2249273d9f13SBarry Smith /*@C 2250273d9f13SBarry Smith MatMPIAIJSetPreallocation - Creates a sparse parallel matrix in AIJ format 2251273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 2252273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 2253273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 2254273d9f13SBarry Smith performance can be increased by more than a factor of 50. 2255273d9f13SBarry Smith 2256273d9f13SBarry Smith Collective on MPI_Comm 2257273d9f13SBarry Smith 2258273d9f13SBarry Smith Input Parameters: 2259273d9f13SBarry Smith + A - the matrix 2260273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 2261273d9f13SBarry Smith (same value is used for all local rows) 2262273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 2263273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 2264273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 2265273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 2266273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 2267273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 2268273d9f13SBarry Smith submatrix (same value is used for all local rows). 2269273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 2270273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 2271273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 2272273d9f13SBarry Smith structure. The size of this array is equal to the number 2273273d9f13SBarry Smith of local rows, i.e 'm'. 2274273d9f13SBarry Smith 2275273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2276273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2277273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2278273d9f13SBarry Smith either one (as in Fortran) or zero. See the users manual for details. 2279273d9f13SBarry Smith 2280273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 2281273d9f13SBarry Smith (possibly both). 2282273d9f13SBarry Smith 2283273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 2284273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 2285273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 2286273d9f13SBarry Smith 2287273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 2288273d9f13SBarry Smith as the submatrix which is obtained by extraction the part corresponding 2289273d9f13SBarry Smith to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 2290273d9f13SBarry Smith first row that belongs to the processor, and r2 is the last row belonging 2291273d9f13SBarry Smith to the this processor. This is a square mxm matrix. The remaining portion 2292273d9f13SBarry Smith of the local submatrix (mxN) constitute the OFF-DIAGONAL portion. 2293273d9f13SBarry Smith 2294273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 2295273d9f13SBarry Smith 2296273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 2297273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 2298273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2299273d9f13SBarry Smith 2300273d9f13SBarry Smith Options Database Keys: 2301273d9f13SBarry Smith + -mat_aij_no_inode - Do not use inodes 2302273d9f13SBarry Smith . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2303273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2304273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2305273d9f13SBarry Smith the user still MUST index entries starting at 0! 2306273d9f13SBarry Smith 2307273d9f13SBarry Smith Example usage: 2308273d9f13SBarry Smith 2309273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 2310273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 2311273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 2312273d9f13SBarry Smith as follows: 2313273d9f13SBarry Smith 2314273d9f13SBarry Smith .vb 2315273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 2316273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 2317273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 2318273d9f13SBarry Smith ------------------------------------- 2319273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 2320273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 2321273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 2322273d9f13SBarry Smith ------------------------------------- 2323273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 2324273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 2325273d9f13SBarry Smith .ve 2326273d9f13SBarry Smith 2327273d9f13SBarry Smith This can be represented as a collection of submatrices as: 2328273d9f13SBarry Smith 2329273d9f13SBarry Smith .vb 2330273d9f13SBarry Smith A B C 2331273d9f13SBarry Smith D E F 2332273d9f13SBarry Smith G H I 2333273d9f13SBarry Smith .ve 2334273d9f13SBarry Smith 2335273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 2336273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 2337273d9f13SBarry Smith 2338273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 2339273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 2340273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 2341273d9f13SBarry Smith 2342273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 2343273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 2344273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 2345273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 2346273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 2347273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 2348273d9f13SBarry Smith 2349273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 2350273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 2351273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 2352273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 2353273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 2354273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 2355273d9f13SBarry Smith .vb 2356273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 2357273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 2358273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 2359273d9f13SBarry Smith .ve 2360273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 2361273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 2362273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 2363273d9f13SBarry Smith 34 values. 2364273d9f13SBarry Smith 2365273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 2366273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 2367273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 2368273d9f13SBarry Smith .vb 2369273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 2370273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 2371273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 2372273d9f13SBarry Smith .ve 2373273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 2374273d9f13SBarry Smith hence pre-allocation is perfect. 2375273d9f13SBarry Smith 2376273d9f13SBarry Smith Level: intermediate 2377273d9f13SBarry Smith 2378273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 2379273d9f13SBarry Smith 2380273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues() 2381273d9f13SBarry Smith @*/ 2382ca01db9bSBarry Smith int MatMPIAIJSetPreallocation(Mat B,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[]) 2383273d9f13SBarry Smith { 2384ca01db9bSBarry Smith int ierr,(*f)(Mat,int,const int[],int,const int[]); 2385273d9f13SBarry Smith 2386273d9f13SBarry Smith PetscFunctionBegin; 2387a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 2388a23d5eceSKris Buschelman if (f) { 2389a23d5eceSKris Buschelman ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 2390273d9f13SBarry Smith } 2391273d9f13SBarry Smith PetscFunctionReturn(0); 2392273d9f13SBarry Smith } 2393273d9f13SBarry Smith 23944a2ae208SSatish Balay #undef __FUNCT__ 23954a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 2396273d9f13SBarry Smith /*@C 2397273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 2398273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 2399273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 2400273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 2401273d9f13SBarry Smith performance can be increased by more than a factor of 50. 2402273d9f13SBarry Smith 2403273d9f13SBarry Smith Collective on MPI_Comm 2404273d9f13SBarry Smith 2405273d9f13SBarry Smith Input Parameters: 2406273d9f13SBarry Smith + comm - MPI communicator 2407273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 2408273d9f13SBarry Smith This value should be the same as the local size used in creating the 2409273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 2410273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 2411273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 2412273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 2413273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 2414273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 2415273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 2416273d9f13SBarry Smith (same value is used for all local rows) 2417273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 2418273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 2419273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 2420273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 2421273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 2422273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 2423273d9f13SBarry Smith submatrix (same value is used for all local rows). 2424273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 2425273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 2426273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 2427273d9f13SBarry Smith structure. The size of this array is equal to the number 2428273d9f13SBarry Smith of local rows, i.e 'm'. 2429273d9f13SBarry Smith 2430273d9f13SBarry Smith Output Parameter: 2431273d9f13SBarry Smith . A - the matrix 2432273d9f13SBarry Smith 2433273d9f13SBarry Smith Notes: 2434273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 2435273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 2436273d9f13SBarry Smith storage requirements for this matrix. 2437273d9f13SBarry Smith 2438273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 2439273d9f13SBarry Smith processor than it must be used on all processors that share the object for 2440273d9f13SBarry Smith that argument. 2441273d9f13SBarry Smith 2442273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 2443273d9f13SBarry Smith compressed row storage), is fully compatible with standard Fortran 77 2444273d9f13SBarry Smith storage. That is, the stored row and column indices can begin at 2445273d9f13SBarry Smith either one (as in Fortran) or zero. See the users manual for details. 2446273d9f13SBarry Smith 2447273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 2448273d9f13SBarry Smith (possibly both). 2449273d9f13SBarry Smith 2450273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 2451273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 2452273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 2453273d9f13SBarry Smith 2454273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 2455273d9f13SBarry Smith as the submatrix which is obtained by extraction the part corresponding 2456273d9f13SBarry Smith to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 2457273d9f13SBarry Smith first row that belongs to the processor, and r2 is the last row belonging 2458273d9f13SBarry Smith to the this processor. This is a square mxm matrix. The remaining portion 2459273d9f13SBarry Smith of the local submatrix (mxN) constitute the OFF-DIAGONAL portion. 2460273d9f13SBarry Smith 2461273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 2462273d9f13SBarry Smith 246397d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 246497d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 246597d05335SKris Buschelman type of communicator, use the construction mechanism: 246697d05335SKris Buschelman MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...); 246797d05335SKris Buschelman 2468273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 2469273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 2470273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 2471273d9f13SBarry Smith 2472273d9f13SBarry Smith Options Database Keys: 2473273d9f13SBarry Smith + -mat_aij_no_inode - Do not use inodes 2474273d9f13SBarry Smith . -mat_aij_inode_limit <limit> - Sets inode limit (max limit=5) 2475273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 2476273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 2477273d9f13SBarry Smith the user still MUST index entries starting at 0! 2478273d9f13SBarry Smith 2479273d9f13SBarry Smith 2480273d9f13SBarry Smith Example usage: 2481273d9f13SBarry Smith 2482273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 2483273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 2484273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 2485273d9f13SBarry Smith as follows: 2486273d9f13SBarry Smith 2487273d9f13SBarry Smith .vb 2488273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 2489273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 2490273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 2491273d9f13SBarry Smith ------------------------------------- 2492273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 2493273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 2494273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 2495273d9f13SBarry Smith ------------------------------------- 2496273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 2497273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 2498273d9f13SBarry Smith .ve 2499273d9f13SBarry Smith 2500273d9f13SBarry Smith This can be represented as a collection of submatrices as: 2501273d9f13SBarry Smith 2502273d9f13SBarry Smith .vb 2503273d9f13SBarry Smith A B C 2504273d9f13SBarry Smith D E F 2505273d9f13SBarry Smith G H I 2506273d9f13SBarry Smith .ve 2507273d9f13SBarry Smith 2508273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 2509273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 2510273d9f13SBarry Smith 2511273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 2512273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 2513273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 2514273d9f13SBarry Smith 2515273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 2516273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 2517273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 2518273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 2519273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 2520273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 2521273d9f13SBarry Smith 2522273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 2523273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 2524273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 2525273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 2526273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 2527273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 2528273d9f13SBarry Smith .vb 2529273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 2530273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 2531273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 2532273d9f13SBarry Smith .ve 2533273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 2534273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 2535273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 2536273d9f13SBarry Smith 34 values. 2537273d9f13SBarry Smith 2538273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 2539273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 2540273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 2541273d9f13SBarry Smith .vb 2542273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 2543273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 2544273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 2545273d9f13SBarry Smith .ve 2546273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 2547273d9f13SBarry Smith hence pre-allocation is perfect. 2548273d9f13SBarry Smith 2549273d9f13SBarry Smith Level: intermediate 2550273d9f13SBarry Smith 2551273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 2552273d9f13SBarry Smith 2553273d9f13SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues() 2554273d9f13SBarry Smith @*/ 2555ca01db9bSBarry Smith int MatCreateMPIAIJ(MPI_Comm comm,int m,int n,int M,int N,int d_nz,const int d_nnz[],int o_nz,const int o_nnz[],Mat *A) 2556273d9f13SBarry Smith { 2557273d9f13SBarry Smith int ierr,size; 2558273d9f13SBarry Smith 2559273d9f13SBarry Smith PetscFunctionBegin; 2560273d9f13SBarry Smith ierr = MatCreate(comm,m,n,M,N,A);CHKERRQ(ierr); 2561273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 2562273d9f13SBarry Smith if (size > 1) { 2563273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 2564273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 2565273d9f13SBarry Smith } else { 2566273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 2567273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 2568273d9f13SBarry Smith } 2569273d9f13SBarry Smith PetscFunctionReturn(0); 2570273d9f13SBarry Smith } 2571195d93cdSBarry Smith 25724a2ae208SSatish Balay #undef __FUNCT__ 25734a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 2574268466fbSBarry Smith int MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,int *colmap[]) 2575195d93cdSBarry Smith { 2576195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 2577195d93cdSBarry Smith PetscFunctionBegin; 2578195d93cdSBarry Smith *Ad = a->A; 2579195d93cdSBarry Smith *Ao = a->B; 2580195d93cdSBarry Smith *colmap = a->garray; 2581195d93cdSBarry Smith PetscFunctionReturn(0); 2582195d93cdSBarry Smith } 2583a2243be0SBarry Smith 2584a2243be0SBarry Smith #undef __FUNCT__ 2585a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 2586a2243be0SBarry Smith int MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 2587a2243be0SBarry Smith { 258808b6dcc0SBarry Smith int ierr,i; 2589a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2590a2243be0SBarry Smith 2591a2243be0SBarry Smith PetscFunctionBegin; 2592a2243be0SBarry Smith if (coloring->ctype == IS_COLORING_LOCAL) { 259308b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 2594a2243be0SBarry Smith ISColoring ocoloring; 2595a2243be0SBarry Smith 2596a2243be0SBarry Smith /* set coloring for diagonal portion */ 2597a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 2598a2243be0SBarry Smith 2599a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 260008b6dcc0SBarry Smith ierr = ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 260108b6dcc0SBarry Smith ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 2602a2243be0SBarry Smith for (i=0; i<a->B->n; i++) { 2603a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 2604a2243be0SBarry Smith } 2605a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 2606a2243be0SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr); 2607a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 2608a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 2609a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 261008b6dcc0SBarry Smith ISColoringValue *colors; 261108b6dcc0SBarry Smith int *larray; 2612a2243be0SBarry Smith ISColoring ocoloring; 2613a2243be0SBarry Smith 2614a2243be0SBarry Smith /* set coloring for diagonal portion */ 2615a2243be0SBarry Smith ierr = PetscMalloc((a->A->n+1)*sizeof(int),&larray);CHKERRQ(ierr); 2616a2243be0SBarry Smith for (i=0; i<a->A->n; i++) { 2617a2243be0SBarry Smith larray[i] = i + a->cstart; 2618a2243be0SBarry Smith } 2619a2243be0SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 262008b6dcc0SBarry Smith ierr = PetscMalloc((a->A->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 2621a2243be0SBarry Smith for (i=0; i<a->A->n; i++) { 2622a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 2623a2243be0SBarry Smith } 2624a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 262512c595b3SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,a->A->n,colors,&ocoloring);CHKERRQ(ierr); 2626a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 2627a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 2628a2243be0SBarry Smith 2629a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 2630a2243be0SBarry Smith ierr = PetscMalloc((a->B->n+1)*sizeof(int),&larray);CHKERRQ(ierr); 2631a2243be0SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 263208b6dcc0SBarry Smith ierr = PetscMalloc((a->B->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 2633a2243be0SBarry Smith for (i=0; i<a->B->n; i++) { 2634a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 2635a2243be0SBarry Smith } 2636a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 2637a2243be0SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,a->B->n,colors,&ocoloring);CHKERRQ(ierr); 2638a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 2639a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 2640a2243be0SBarry Smith } else { 2641a2243be0SBarry Smith SETERRQ1(1,"No support ISColoringType %d",coloring->ctype); 2642a2243be0SBarry Smith } 2643a2243be0SBarry Smith 2644a2243be0SBarry Smith PetscFunctionReturn(0); 2645a2243be0SBarry Smith } 2646a2243be0SBarry Smith 2647a2243be0SBarry Smith #undef __FUNCT__ 2648779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 2649779c1a83SBarry Smith int MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 2650a2243be0SBarry Smith { 2651a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2652a2243be0SBarry Smith int ierr; 2653a2243be0SBarry Smith 2654a2243be0SBarry Smith PetscFunctionBegin; 2655779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 2656779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 2657779c1a83SBarry Smith PetscFunctionReturn(0); 2658779c1a83SBarry Smith } 2659779c1a83SBarry Smith 2660779c1a83SBarry Smith #undef __FUNCT__ 2661779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 2662779c1a83SBarry Smith int MatSetValuesAdifor_MPIAIJ(Mat A,int nl,void *advalues) 2663779c1a83SBarry Smith { 2664779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2665779c1a83SBarry Smith int ierr; 2666779c1a83SBarry Smith 2667779c1a83SBarry Smith PetscFunctionBegin; 2668779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 2669779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 2670a2243be0SBarry Smith PetscFunctionReturn(0); 2671a2243be0SBarry Smith } 2672c5d6d63eSBarry Smith 2673c5d6d63eSBarry Smith #undef __FUNCT__ 267451dd7536SBarry Smith #define __FUNCT__ "MatMerge" 2675c5d6d63eSBarry Smith /*@C 267651dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 267751dd7536SBarry Smith matrices from each processor 2678c5d6d63eSBarry Smith 2679c5d6d63eSBarry Smith Collective on MPI_Comm 2680c5d6d63eSBarry Smith 2681c5d6d63eSBarry Smith Input Parameters: 268251dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 268351dd7536SBarry Smith - inmat - the input sequential matrices 268451dd7536SBarry Smith 268551dd7536SBarry Smith Output Parameter: 268651dd7536SBarry Smith . outmat - the parallel matrix generated 2687c5d6d63eSBarry Smith 26887e25d530SSatish Balay Level: advanced 26897e25d530SSatish Balay 2690c5d6d63eSBarry Smith Notes: The number of columns of the matrix in EACH of the seperate files 2691c5d6d63eSBarry Smith MUST be the same. 2692c5d6d63eSBarry Smith 2693c5d6d63eSBarry Smith @*/ 269451dd7536SBarry Smith int MatMerge(MPI_Comm comm,Mat inmat, Mat *outmat) 2695c5d6d63eSBarry Smith { 2696a15ac5e4SHong Zhang int ierr,m,n,i,rstart,*indx,nnz,I,*dnz,*onz; 2697c5d6d63eSBarry Smith PetscScalar *values; 269851dd7536SBarry Smith PetscMap columnmap,rowmap; 2699c5d6d63eSBarry Smith 2700c5d6d63eSBarry Smith PetscFunctionBegin; 2701c5d6d63eSBarry Smith 270251dd7536SBarry Smith ierr = MatGetSize(inmat,&m,&n);CHKERRQ(ierr); 270351dd7536SBarry Smith 270451dd7536SBarry Smith /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 270551dd7536SBarry Smith ierr = PetscMapCreate(comm,&columnmap);CHKERRQ(ierr); 270651dd7536SBarry Smith ierr = PetscMapSetSize(columnmap,n);CHKERRQ(ierr); 270751dd7536SBarry Smith ierr = PetscMapSetType(columnmap,MAP_MPI);CHKERRQ(ierr); 270851dd7536SBarry Smith ierr = PetscMapGetLocalSize(columnmap,&n);CHKERRQ(ierr); 270951dd7536SBarry Smith ierr = PetscMapDestroy(columnmap);CHKERRQ(ierr); 271051dd7536SBarry Smith 271151dd7536SBarry Smith ierr = PetscMapCreate(comm,&rowmap);CHKERRQ(ierr); 271251dd7536SBarry Smith ierr = PetscMapSetLocalSize(rowmap,m);CHKERRQ(ierr); 271351dd7536SBarry Smith ierr = PetscMapSetType(rowmap,MAP_MPI);CHKERRQ(ierr); 271451dd7536SBarry Smith ierr = PetscMapGetLocalRange(rowmap,&rstart,0);CHKERRQ(ierr); 271551dd7536SBarry Smith ierr = PetscMapDestroy(rowmap);CHKERRQ(ierr); 271651dd7536SBarry Smith 271751dd7536SBarry Smith ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 2718c5d6d63eSBarry Smith for (i=0;i<m;i++) { 271951dd7536SBarry Smith ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 272051dd7536SBarry Smith ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 272151dd7536SBarry Smith ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 2722c5d6d63eSBarry Smith } 2723f204ca49SKris Buschelman /* This routine will ONLY return MPIAIJ type matrix */ 2724f204ca49SKris Buschelman ierr = MatCreate(comm,m,n,PETSC_DETERMINE,PETSC_DETERMINE,outmat);CHKERRQ(ierr); 2725f204ca49SKris Buschelman ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 2726f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 272751dd7536SBarry Smith ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 2728c5d6d63eSBarry Smith 272951dd7536SBarry Smith for (i=0;i<m;i++) { 273051dd7536SBarry Smith ierr = MatGetRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 273151dd7536SBarry Smith I = i + rstart; 273251dd7536SBarry Smith ierr = MatSetValues(*outmat,1,&I,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 273351dd7536SBarry Smith ierr = MatRestoreRow(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 273451dd7536SBarry Smith } 273551dd7536SBarry Smith ierr = MatDestroy(inmat);CHKERRQ(ierr); 273651dd7536SBarry Smith ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 273751dd7536SBarry Smith ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 273851dd7536SBarry Smith 2739c5d6d63eSBarry Smith PetscFunctionReturn(0); 2740c5d6d63eSBarry Smith } 2741c5d6d63eSBarry Smith 2742c5d6d63eSBarry Smith #undef __FUNCT__ 2743c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 2744c5d6d63eSBarry Smith int MatFileSplit(Mat A,char *outfile) 2745c5d6d63eSBarry Smith { 2746c5d6d63eSBarry Smith int ierr,rank,len,m,N,i,rstart,*indx,nnz; 2747c5d6d63eSBarry Smith PetscViewer out; 2748c5d6d63eSBarry Smith char *name; 2749c5d6d63eSBarry Smith Mat B; 2750c5d6d63eSBarry Smith PetscScalar *values; 2751c5d6d63eSBarry Smith 2752c5d6d63eSBarry Smith PetscFunctionBegin; 2753c5d6d63eSBarry Smith 2754c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 2755c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 2756f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 2757f204ca49SKris Buschelman ierr = MatCreate(PETSC_COMM_SELF,m,N,m,N,&B);CHKERRQ(ierr); 2758f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 2759f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 2760c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 2761c5d6d63eSBarry Smith for (i=0;i<m;i++) { 2762c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 2763c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 2764c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 2765c5d6d63eSBarry Smith } 2766c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2767c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2768c5d6d63eSBarry Smith 2769c5d6d63eSBarry Smith ierr = MPI_Comm_rank(A->comm,&rank);CHKERRQ(ierr); 2770c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 2771c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 2772c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 277345f7d322SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,PETSC_FILE_CREATE,&out);CHKERRQ(ierr); 2774c5d6d63eSBarry Smith ierr = PetscFree(name); 2775c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 2776c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 2777c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 2778c5d6d63eSBarry Smith PetscFunctionReturn(0); 2779c5d6d63eSBarry Smith } 2780