1be1d678aSKris Buschelman #define PETSCMAT_DLL 28a729477SBarry Smith 37c4f633dSBarry Smith #include "../src/mat/impls/aij/mpi/mpiaij.h" /*I "petscmat.h" I*/ 4f3da1532SBarry Smith #include "petscblaslapack.h" 58a729477SBarry Smith 6dd6ea824SBarry Smith #undef __FUNCT__ 7dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ" 8dd6ea824SBarry Smith /* 9dd6ea824SBarry Smith Distributes a SeqAIJ matrix across a set of processes. Code stolen from 10dd6ea824SBarry Smith MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type. 11dd6ea824SBarry Smith 12dd6ea824SBarry Smith Only for square matrices 13dd6ea824SBarry Smith */ 14dd6ea824SBarry Smith PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat) 15dd6ea824SBarry Smith { 16dd6ea824SBarry Smith PetscMPIInt rank,size; 17dd6ea824SBarry Smith PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld; 18dd6ea824SBarry Smith PetscErrorCode ierr; 19dd6ea824SBarry Smith Mat mat; 20dd6ea824SBarry Smith Mat_SeqAIJ *gmata; 21dd6ea824SBarry Smith PetscMPIInt tag; 22dd6ea824SBarry Smith MPI_Status status; 23dd6ea824SBarry Smith PetscTruth aij; 24dd6ea824SBarry Smith MatScalar *gmataa,*ao,*ad,*gmataarestore=0; 25dd6ea824SBarry Smith 26dd6ea824SBarry Smith PetscFunctionBegin; 27dd6ea824SBarry Smith CHKMEMQ; 28dd6ea824SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 29dd6ea824SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 30dd6ea824SBarry Smith if (!rank) { 31dd6ea824SBarry Smith ierr = PetscTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr); 3265e19b50SBarry Smith if (!aij) SETERRQ1(((PetscObject)gmat)->comm,PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name); 33dd6ea824SBarry Smith } 34dd6ea824SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 35dd6ea824SBarry Smith ierr = MatCreate(comm,&mat);CHKERRQ(ierr); 36dd6ea824SBarry Smith ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 37dd6ea824SBarry Smith ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 38dd6ea824SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 39dd6ea824SBarry Smith ierr = PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);CHKERRQ(ierr); 40dd6ea824SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 41dd6ea824SBarry Smith rowners[0] = 0; 42dd6ea824SBarry Smith for (i=2; i<=size; i++) { 43dd6ea824SBarry Smith rowners[i] += rowners[i-1]; 44dd6ea824SBarry Smith } 45dd6ea824SBarry Smith rstart = rowners[rank]; 46dd6ea824SBarry Smith rend = rowners[rank+1]; 47dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 48dd6ea824SBarry Smith if (!rank) { 49dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 50dd6ea824SBarry Smith /* send row lengths to all processors */ 51dd6ea824SBarry Smith for (i=0; i<m; i++) dlens[i] = gmata->ilen[i]; 52dd6ea824SBarry Smith for (i=1; i<size; i++) { 53dd6ea824SBarry Smith ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 54dd6ea824SBarry Smith } 55dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 56dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 57dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 58dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 59dd6ea824SBarry Smith jj = 0; 60dd6ea824SBarry Smith for (i=0; i<m; i++) { 61dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 62dd6ea824SBarry Smith if (gmata->j[jj] < rstart) ld[i]++; 63dd6ea824SBarry Smith if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++; 64dd6ea824SBarry Smith jj++; 65dd6ea824SBarry Smith } 66dd6ea824SBarry Smith } 67dd6ea824SBarry Smith /* send column indices to other processes */ 68dd6ea824SBarry Smith for (i=1; i<size; i++) { 69dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 70dd6ea824SBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 71dd6ea824SBarry Smith ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 72dd6ea824SBarry Smith } 73dd6ea824SBarry Smith 74dd6ea824SBarry Smith /* send numerical values to other processes */ 75dd6ea824SBarry Smith for (i=1; i<size; i++) { 76dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 77dd6ea824SBarry Smith ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 78dd6ea824SBarry Smith } 79dd6ea824SBarry Smith gmataa = gmata->a; 80dd6ea824SBarry Smith gmataj = gmata->j; 81dd6ea824SBarry Smith 82dd6ea824SBarry Smith } else { 83dd6ea824SBarry Smith /* receive row lengths */ 84dd6ea824SBarry Smith ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 85dd6ea824SBarry Smith /* receive column indices */ 86dd6ea824SBarry Smith ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 87dd6ea824SBarry Smith ierr = PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);CHKERRQ(ierr); 88dd6ea824SBarry Smith ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 89dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 90dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 91dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 92dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 93dd6ea824SBarry Smith jj = 0; 94dd6ea824SBarry Smith for (i=0; i<m; i++) { 95dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 96dd6ea824SBarry Smith if (gmataj[jj] < rstart) ld[i]++; 97dd6ea824SBarry Smith if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++; 98dd6ea824SBarry Smith jj++; 99dd6ea824SBarry Smith } 100dd6ea824SBarry Smith } 101dd6ea824SBarry Smith /* receive numerical values */ 102dd6ea824SBarry Smith ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 103dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 104dd6ea824SBarry Smith } 105dd6ea824SBarry Smith /* set preallocation */ 106dd6ea824SBarry Smith for (i=0; i<m; i++) { 107dd6ea824SBarry Smith dlens[i] -= olens[i]; 108dd6ea824SBarry Smith } 109dd6ea824SBarry Smith ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr); 110dd6ea824SBarry Smith ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr); 111dd6ea824SBarry Smith 112dd6ea824SBarry Smith for (i=0; i<m; i++) { 113dd6ea824SBarry Smith dlens[i] += olens[i]; 114dd6ea824SBarry Smith } 115dd6ea824SBarry Smith cnt = 0; 116dd6ea824SBarry Smith for (i=0; i<m; i++) { 117dd6ea824SBarry Smith row = rstart + i; 118dd6ea824SBarry Smith ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr); 119dd6ea824SBarry Smith cnt += dlens[i]; 120dd6ea824SBarry Smith } 121dd6ea824SBarry Smith if (rank) { 122dd6ea824SBarry Smith ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr); 123dd6ea824SBarry Smith } 124dd6ea824SBarry Smith ierr = PetscFree2(dlens,olens);CHKERRQ(ierr); 125dd6ea824SBarry Smith ierr = PetscFree(rowners);CHKERRQ(ierr); 126dd6ea824SBarry Smith ((Mat_MPIAIJ*)(mat->data))->ld = ld; 127dd6ea824SBarry Smith *inmat = mat; 128dd6ea824SBarry Smith } else { /* column indices are already set; only need to move over numerical values from process 0 */ 129dd6ea824SBarry Smith Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data; 130dd6ea824SBarry Smith Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data; 131dd6ea824SBarry Smith mat = *inmat; 132dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 133dd6ea824SBarry Smith if (!rank) { 134dd6ea824SBarry Smith /* send numerical values to other processes */ 135dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 136dd6ea824SBarry Smith ierr = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr); 137dd6ea824SBarry Smith gmataa = gmata->a; 138dd6ea824SBarry Smith for (i=1; i<size; i++) { 139dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 140dd6ea824SBarry Smith ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 141dd6ea824SBarry Smith } 142dd6ea824SBarry Smith nz = gmata->i[rowners[1]]-gmata->i[rowners[0]]; 143dd6ea824SBarry Smith } else { 144dd6ea824SBarry Smith /* receive numerical values from process 0*/ 145dd6ea824SBarry Smith nz = Ad->nz + Ao->nz; 146dd6ea824SBarry Smith ierr = PetscMalloc(nz*sizeof(PetscScalar),&gmataa);CHKERRQ(ierr); gmataarestore = gmataa; 147dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 148dd6ea824SBarry Smith } 149dd6ea824SBarry Smith /* transfer numerical values into the diagonal A and off diagonal B parts of mat */ 150dd6ea824SBarry Smith ld = ((Mat_MPIAIJ*)(mat->data))->ld; 151dd6ea824SBarry Smith ad = Ad->a; 152dd6ea824SBarry Smith ao = Ao->a; 153d0f46423SBarry Smith if (mat->rmap->n) { 154dd6ea824SBarry Smith i = 0; 155dd6ea824SBarry Smith nz = ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 156dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 157dd6ea824SBarry Smith } 158d0f46423SBarry Smith for (i=1; i<mat->rmap->n; i++) { 159dd6ea824SBarry Smith nz = Ao->i[i] - Ao->i[i-1] - ld[i-1] + ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 160dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 161dd6ea824SBarry Smith } 162dd6ea824SBarry Smith i--; 163d0f46423SBarry Smith if (mat->rmap->n) { 164dd6ea824SBarry Smith nz = Ao->i[i+1] - Ao->i[i] - ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 165dd6ea824SBarry Smith } 166dd6ea824SBarry Smith if (rank) { 167dd6ea824SBarry Smith ierr = PetscFree(gmataarestore);CHKERRQ(ierr); 168dd6ea824SBarry Smith } 169dd6ea824SBarry Smith } 170dd6ea824SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 171dd6ea824SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 172dd6ea824SBarry Smith CHKMEMQ; 173dd6ea824SBarry Smith PetscFunctionReturn(0); 174dd6ea824SBarry Smith } 175dd6ea824SBarry Smith 1760f5bd95cSBarry Smith /* 1770f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 1789e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 1790f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 1800f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 1810f5bd95cSBarry Smith has an order N integer array but is fast to acess. 1829e25ed09SBarry Smith */ 1834a2ae208SSatish Balay #undef __FUNCT__ 1844a2ae208SSatish Balay #define __FUNCT__ "CreateColmap_MPIAIJ_Private" 185dfbe8321SBarry Smith PetscErrorCode CreateColmap_MPIAIJ_Private(Mat mat) 1869e25ed09SBarry Smith { 18744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1886849ba73SBarry Smith PetscErrorCode ierr; 189d0f46423SBarry Smith PetscInt n = aij->B->cmap->n,i; 190dbb450caSBarry Smith 1913a40ed3dSBarry Smith PetscFunctionBegin; 192aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 193273d9f13SBarry Smith ierr = PetscTableCreate(n,&aij->colmap);CHKERRQ(ierr); 194b1fc9764SSatish Balay for (i=0; i<n; i++){ 1950f5bd95cSBarry Smith ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);CHKERRQ(ierr); 196b1fc9764SSatish Balay } 197b1fc9764SSatish Balay #else 198d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr); 199d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 200d0f46423SBarry Smith ierr = PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 201905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 202b1fc9764SSatish Balay #endif 2033a40ed3dSBarry Smith PetscFunctionReturn(0); 2049e25ed09SBarry Smith } 2059e25ed09SBarry Smith 20630770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 2070520107fSSatish Balay { \ 2087cd84e04SBarry Smith if (col <= lastcol1) low1 = 0; else high1 = nrow1; \ 209fd3458f5SBarry Smith lastcol1 = col;\ 210fd3458f5SBarry Smith while (high1-low1 > 5) { \ 211fd3458f5SBarry Smith t = (low1+high1)/2; \ 212fd3458f5SBarry Smith if (rp1[t] > col) high1 = t; \ 213fd3458f5SBarry Smith else low1 = t; \ 214ba4e3ef2SSatish Balay } \ 215fd3458f5SBarry Smith for (_i=low1; _i<high1; _i++) { \ 216fd3458f5SBarry Smith if (rp1[_i] > col) break; \ 217fd3458f5SBarry Smith if (rp1[_i] == col) { \ 218fd3458f5SBarry Smith if (addv == ADD_VALUES) ap1[_i] += value; \ 219fd3458f5SBarry Smith else ap1[_i] = value; \ 22030770e4dSSatish Balay goto a_noinsert; \ 2210520107fSSatish Balay } \ 2220520107fSSatish Balay } \ 223e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \ 224e44c0bd4SBarry Smith if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;} \ 225e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 226fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \ 227669a8dbcSSatish Balay N = nrow1++ - 1; a->nz++; high1++; \ 2280520107fSSatish Balay /* shift up all the later entries in this row */ \ 2290520107fSSatish Balay for (ii=N; ii>=_i; ii--) { \ 230fd3458f5SBarry Smith rp1[ii+1] = rp1[ii]; \ 231fd3458f5SBarry Smith ap1[ii+1] = ap1[ii]; \ 2320520107fSSatish Balay } \ 233fd3458f5SBarry Smith rp1[_i] = col; \ 234fd3458f5SBarry Smith ap1[_i] = value; \ 23530770e4dSSatish Balay a_noinsert: ; \ 236fd3458f5SBarry Smith ailen[row] = nrow1; \ 2370520107fSSatish Balay } 2380a198c4cSBarry Smith 239085a36d4SBarry Smith 24030770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 24130770e4dSSatish Balay { \ 2427cd84e04SBarry Smith if (col <= lastcol2) low2 = 0; else high2 = nrow2; \ 243fd3458f5SBarry Smith lastcol2 = col;\ 244fd3458f5SBarry Smith while (high2-low2 > 5) { \ 245fd3458f5SBarry Smith t = (low2+high2)/2; \ 246fd3458f5SBarry Smith if (rp2[t] > col) high2 = t; \ 247fd3458f5SBarry Smith else low2 = t; \ 248ba4e3ef2SSatish Balay } \ 249fd3458f5SBarry Smith for (_i=low2; _i<high2; _i++) { \ 250fd3458f5SBarry Smith if (rp2[_i] > col) break; \ 251fd3458f5SBarry Smith if (rp2[_i] == col) { \ 252fd3458f5SBarry Smith if (addv == ADD_VALUES) ap2[_i] += value; \ 253fd3458f5SBarry Smith else ap2[_i] = value; \ 25430770e4dSSatish Balay goto b_noinsert; \ 25530770e4dSSatish Balay } \ 25630770e4dSSatish Balay } \ 257e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 258e44c0bd4SBarry Smith if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 259e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 260fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \ 261669a8dbcSSatish Balay N = nrow2++ - 1; b->nz++; high2++; \ 26230770e4dSSatish Balay /* shift up all the later entries in this row */ \ 26330770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 264fd3458f5SBarry Smith rp2[ii+1] = rp2[ii]; \ 265fd3458f5SBarry Smith ap2[ii+1] = ap2[ii]; \ 26630770e4dSSatish Balay } \ 267fd3458f5SBarry Smith rp2[_i] = col; \ 268fd3458f5SBarry Smith ap2[_i] = value; \ 26930770e4dSSatish Balay b_noinsert: ; \ 270fd3458f5SBarry Smith bilen[row] = nrow2; \ 27130770e4dSSatish Balay } 27230770e4dSSatish Balay 2734a2ae208SSatish Balay #undef __FUNCT__ 2742fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ" 2752fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[]) 2762fd7e33dSBarry Smith { 2772fd7e33dSBarry Smith Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 2782fd7e33dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data; 2792fd7e33dSBarry Smith PetscErrorCode ierr; 2802fd7e33dSBarry Smith PetscInt l,*garray = mat->garray,diag; 2812fd7e33dSBarry Smith 2822fd7e33dSBarry Smith PetscFunctionBegin; 2832fd7e33dSBarry Smith /* code only works for square matrices A */ 2842fd7e33dSBarry Smith 2852fd7e33dSBarry Smith /* find size of row to the left of the diagonal part */ 2862fd7e33dSBarry Smith ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr); 2872fd7e33dSBarry Smith row = row - diag; 2882fd7e33dSBarry Smith for (l=0; l<b->i[row+1]-b->i[row]; l++) { 2892fd7e33dSBarry Smith if (garray[b->j[b->i[row]+l]] > diag) break; 2902fd7e33dSBarry Smith } 2912fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr); 2922fd7e33dSBarry Smith 2932fd7e33dSBarry Smith /* diagonal part */ 2942fd7e33dSBarry Smith ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr); 2952fd7e33dSBarry Smith 2962fd7e33dSBarry Smith /* right of diagonal part */ 2972fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],(b->i[row+1]-b->i[row]-l)*sizeof(PetscScalar));CHKERRQ(ierr); 2982fd7e33dSBarry Smith PetscFunctionReturn(0); 2992fd7e33dSBarry Smith } 3002fd7e33dSBarry Smith 3012fd7e33dSBarry Smith #undef __FUNCT__ 3024a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 303b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 3048a729477SBarry Smith { 30544a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 30687828ca2SBarry Smith PetscScalar value; 307dfbe8321SBarry Smith PetscErrorCode ierr; 308d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 309d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 310273d9f13SBarry Smith PetscTruth roworiented = aij->roworiented; 3118a729477SBarry Smith 3120520107fSSatish Balay /* Some Variables required in the macro */ 3134ee7247eSSatish Balay Mat A = aij->A; 3144ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 31557809a77SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 316a77337e4SBarry Smith MatScalar *aa = a->a; 317edb03aefSBarry Smith PetscTruth ignorezeroentries = a->ignorezeroentries; 31830770e4dSSatish Balay Mat B = aij->B; 31930770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 320d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 321a77337e4SBarry Smith MatScalar *ba = b->a; 32230770e4dSSatish Balay 323fd3458f5SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 324fd3458f5SBarry Smith PetscInt nonew = a->nonew; 325a77337e4SBarry Smith MatScalar *ap1,*ap2; 3264ee7247eSSatish Balay 3273a40ed3dSBarry Smith PetscFunctionBegin; 32871fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 3298a729477SBarry Smith for (i=0; i<m; i++) { 3305ef9f2a5SBarry Smith if (im[i] < 0) continue; 3312515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 332e32f2f54SBarry Smith if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1); 3330a198c4cSBarry Smith #endif 3344b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 3354b0e389bSBarry Smith row = im[i] - rstart; 336fd3458f5SBarry Smith lastcol1 = -1; 337fd3458f5SBarry Smith rp1 = aj + ai[row]; 338fd3458f5SBarry Smith ap1 = aa + ai[row]; 339fd3458f5SBarry Smith rmax1 = aimax[row]; 340fd3458f5SBarry Smith nrow1 = ailen[row]; 341fd3458f5SBarry Smith low1 = 0; 342fd3458f5SBarry Smith high1 = nrow1; 343fd3458f5SBarry Smith lastcol2 = -1; 344fd3458f5SBarry Smith rp2 = bj + bi[row]; 345d498b1e9SBarry Smith ap2 = ba + bi[row]; 346fd3458f5SBarry Smith rmax2 = bimax[row]; 347d498b1e9SBarry Smith nrow2 = bilen[row]; 348fd3458f5SBarry Smith low2 = 0; 349fd3458f5SBarry Smith high2 = nrow2; 350fd3458f5SBarry Smith 3511eb62cbbSBarry Smith for (j=0; j<n; j++) { 35216371a99SBarry Smith if (v) {if (roworiented) value = v[i*n+j]; else value = v[i+j*m];} else value = 0.0; 353abc0a331SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 354fd3458f5SBarry Smith if (in[j] >= cstart && in[j] < cend){ 355fd3458f5SBarry Smith col = in[j] - cstart; 35630770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 357273d9f13SBarry Smith } else if (in[j] < 0) continue; 3582515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 359cb9801acSJed Brown else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1); 3600a198c4cSBarry Smith #endif 3611eb62cbbSBarry Smith else { 362227d817aSBarry Smith if (mat->was_assembled) { 363905e6a2fSBarry Smith if (!aij->colmap) { 364905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 365905e6a2fSBarry Smith } 366aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 3670f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 368fa46199cSSatish Balay col--; 369b1fc9764SSatish Balay #else 370905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 371b1fc9764SSatish Balay #endif 372ec8511deSBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 3732493cbb0SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 3744b0e389bSBarry Smith col = in[j]; 3759bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 376f9508a3cSSatish Balay B = aij->B; 377f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 378e44c0bd4SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a; 379d498b1e9SBarry Smith rp2 = bj + bi[row]; 380d498b1e9SBarry Smith ap2 = ba + bi[row]; 381d498b1e9SBarry Smith rmax2 = bimax[row]; 382d498b1e9SBarry Smith nrow2 = bilen[row]; 383d498b1e9SBarry Smith low2 = 0; 384d498b1e9SBarry Smith high2 = nrow2; 385d0f46423SBarry Smith bm = aij->B->rmap->n; 386f9508a3cSSatish Balay ba = b->a; 387d6dfbf8fSBarry Smith } 388c48de900SBarry Smith } else col = in[j]; 38930770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 3901eb62cbbSBarry Smith } 3911eb62cbbSBarry Smith } 3925ef9f2a5SBarry Smith } else { 39390f02eecSBarry Smith if (!aij->donotstash) { 394d36fbae8SSatish Balay if (roworiented) { 3953b024144SHong Zhang ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscTruth)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 396d36fbae8SSatish Balay } else { 3973b024144SHong Zhang ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscTruth)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 3984b0e389bSBarry Smith } 3991eb62cbbSBarry Smith } 4008a729477SBarry Smith } 40190f02eecSBarry Smith } 4023a40ed3dSBarry Smith PetscFunctionReturn(0); 4038a729477SBarry Smith } 4048a729477SBarry Smith 4054a2ae208SSatish Balay #undef __FUNCT__ 4064a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 407b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 408b49de8d1SLois Curfman McInnes { 409b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 410dfbe8321SBarry Smith PetscErrorCode ierr; 411d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 412d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 413b49de8d1SLois Curfman McInnes 4143a40ed3dSBarry Smith PetscFunctionBegin; 415b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 416e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 417e32f2f54SBarry Smith if (idxm[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap->N-1); 418b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 419b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 420b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 421e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 422e32f2f54SBarry Smith if (idxn[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap->N-1); 423b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 424b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 425b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 426fa852ad4SSatish Balay } else { 427905e6a2fSBarry Smith if (!aij->colmap) { 428905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 429905e6a2fSBarry Smith } 430aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 4310f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 432fa46199cSSatish Balay col --; 433b1fc9764SSatish Balay #else 434905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 435b1fc9764SSatish Balay #endif 436e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 437d9d09a02SSatish Balay else { 438b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 439b49de8d1SLois Curfman McInnes } 440b49de8d1SLois Curfman McInnes } 441b49de8d1SLois Curfman McInnes } 442a8c6a408SBarry Smith } else { 443e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 444b49de8d1SLois Curfman McInnes } 445b49de8d1SLois Curfman McInnes } 4463a40ed3dSBarry Smith PetscFunctionReturn(0); 447b49de8d1SLois Curfman McInnes } 448bc5ccf88SSatish Balay 449bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 450bd0c2dcbSBarry Smith 4514a2ae208SSatish Balay #undef __FUNCT__ 4524a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 453dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 454bc5ccf88SSatish Balay { 455bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 456dfbe8321SBarry Smith PetscErrorCode ierr; 457b1d57f15SBarry Smith PetscInt nstash,reallocs; 458bc5ccf88SSatish Balay InsertMode addv; 459bc5ccf88SSatish Balay 460bc5ccf88SSatish Balay PetscFunctionBegin; 461bc5ccf88SSatish Balay if (aij->donotstash) { 462bc5ccf88SSatish Balay PetscFunctionReturn(0); 463bc5ccf88SSatish Balay } 464bc5ccf88SSatish Balay 465bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 4667adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr); 467e7e72b3dSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 468bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 469bc5ccf88SSatish Balay 470d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 4718798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 472ae15b995SBarry Smith ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 473bc5ccf88SSatish Balay PetscFunctionReturn(0); 474bc5ccf88SSatish Balay } 475bc5ccf88SSatish Balay 4764a2ae208SSatish Balay #undef __FUNCT__ 4774a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 478dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 479bc5ccf88SSatish Balay { 480bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 48191c97fd4SSatish Balay Mat_SeqAIJ *a=(Mat_SeqAIJ *)aij->A->data; 4826849ba73SBarry Smith PetscErrorCode ierr; 483b1d57f15SBarry Smith PetscMPIInt n; 484b1d57f15SBarry Smith PetscInt i,j,rstart,ncols,flg; 485e44c0bd4SBarry Smith PetscInt *row,*col; 486e44c0bd4SBarry Smith PetscTruth other_disassembled; 48787828ca2SBarry Smith PetscScalar *val; 488bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 489bc5ccf88SSatish Balay 49091c97fd4SSatish Balay /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */ 491bc5ccf88SSatish Balay PetscFunctionBegin; 492bc5ccf88SSatish Balay if (!aij->donotstash) { 493a2d1c673SSatish Balay while (1) { 4948798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 495a2d1c673SSatish Balay if (!flg) break; 496a2d1c673SSatish Balay 497bc5ccf88SSatish Balay for (i=0; i<n;) { 498bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 499bc5ccf88SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 500bc5ccf88SSatish Balay if (j < n) ncols = j-i; 501bc5ccf88SSatish Balay else ncols = n-i; 502bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 503bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 504bc5ccf88SSatish Balay i = j; 505bc5ccf88SSatish Balay } 506bc5ccf88SSatish Balay } 5078798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 508bc5ccf88SSatish Balay } 5092f53aa61SHong Zhang a->compressedrow.use = PETSC_FALSE; 510bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 511bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 512bc5ccf88SSatish Balay 513bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 514bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 515bc5ccf88SSatish Balay /* 516bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 517bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 518bc5ccf88SSatish Balay */ 519bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 5207adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr); 521bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 522bc5ccf88SSatish Balay ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 523ad59fb31SSatish Balay } 524ad59fb31SSatish Balay } 525bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 526bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 527bc5ccf88SSatish Balay } 5284e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 52991c97fd4SSatish Balay ((Mat_SeqAIJ *)aij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */ 530bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 531bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 532bc5ccf88SSatish Balay 5331d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 534606d414cSSatish Balay aij->rowvalues = 0; 535a30b2313SHong Zhang 536a30b2313SHong Zhang /* used by MatAXPY() */ 53791c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 53891c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 539a30b2313SHong Zhang 540a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);aij->diag = 0;} 541bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 542bc5ccf88SSatish Balay PetscFunctionReturn(0); 543bc5ccf88SSatish Balay } 544bc5ccf88SSatish Balay 5454a2ae208SSatish Balay #undef __FUNCT__ 5464a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 547dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 5481eb62cbbSBarry Smith { 54944a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 550dfbe8321SBarry Smith PetscErrorCode ierr; 5513a40ed3dSBarry Smith 5523a40ed3dSBarry Smith PetscFunctionBegin; 55378b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 55478b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 5553a40ed3dSBarry Smith PetscFunctionReturn(0); 5561eb62cbbSBarry Smith } 5571eb62cbbSBarry Smith 5584a2ae208SSatish Balay #undef __FUNCT__ 5594a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 560f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag) 5611eb62cbbSBarry Smith { 56244a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 5636849ba73SBarry Smith PetscErrorCode ierr; 5647adad957SLisandro Dalcin PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 565d0f46423SBarry Smith PetscInt i,*owners = A->rmap->range; 566b1d57f15SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 567b1d57f15SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 568b1d57f15SBarry Smith PetscInt *rvalues,count,base,slen,*source; 569d0f46423SBarry Smith PetscInt *lens,*lrows,*values,rstart=A->rmap->rstart; 5707adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)A)->comm; 5711eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 5721eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 5736543fbbaSBarry Smith #if defined(PETSC_DEBUG) 5746543fbbaSBarry Smith PetscTruth found = PETSC_FALSE; 5756543fbbaSBarry Smith #endif 5761eb62cbbSBarry Smith 5773a40ed3dSBarry Smith PetscFunctionBegin; 5781eb62cbbSBarry Smith /* first count number of contributors to each processor */ 579b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 580b1d57f15SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 581b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 5826543fbbaSBarry Smith j = 0; 5831eb62cbbSBarry Smith for (i=0; i<N; i++) { 5846543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 5856543fbbaSBarry Smith lastidx = idx; 5866543fbbaSBarry Smith for (; j<size; j++) { 5871eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 5886543fbbaSBarry Smith nprocs[2*j]++; 5896543fbbaSBarry Smith nprocs[2*j+1] = 1; 5906543fbbaSBarry Smith owner[i] = j; 5916543fbbaSBarry Smith #if defined(PETSC_DEBUG) 5926543fbbaSBarry Smith found = PETSC_TRUE; 5936543fbbaSBarry Smith #endif 5946543fbbaSBarry Smith break; 5951eb62cbbSBarry Smith } 5961eb62cbbSBarry Smith } 5976543fbbaSBarry Smith #if defined(PETSC_DEBUG) 598e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 5996543fbbaSBarry Smith found = PETSC_FALSE; 6006543fbbaSBarry Smith #endif 6011eb62cbbSBarry Smith } 602c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 6031eb62cbbSBarry Smith 6041eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 605c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 6061eb62cbbSBarry Smith 6071eb62cbbSBarry Smith /* post receives: */ 608b1d57f15SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 609b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 6101eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 611b1d57f15SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 6121eb62cbbSBarry Smith } 6131eb62cbbSBarry Smith 6141eb62cbbSBarry Smith /* do sends: 6151eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 6161eb62cbbSBarry Smith the ith processor 6171eb62cbbSBarry Smith */ 618b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 619b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 620b1d57f15SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 6211eb62cbbSBarry Smith starts[0] = 0; 622c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6231eb62cbbSBarry Smith for (i=0; i<N; i++) { 6241eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 6251eb62cbbSBarry Smith } 6261eb62cbbSBarry Smith 6271eb62cbbSBarry Smith starts[0] = 0; 628c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6291eb62cbbSBarry Smith count = 0; 63017699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 631c1dc657dSBarry Smith if (nprocs[2*i+1]) { 632b1d57f15SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 6331eb62cbbSBarry Smith } 6341eb62cbbSBarry Smith } 635606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 6361eb62cbbSBarry Smith 63717699dbbSLois Curfman McInnes base = owners[rank]; 6381eb62cbbSBarry Smith 6391eb62cbbSBarry Smith /* wait on receives */ 6401d79065fSBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 6411eb62cbbSBarry Smith count = nrecvs; slen = 0; 6421eb62cbbSBarry Smith while (count) { 643ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 6441eb62cbbSBarry Smith /* unpack receives into our local space */ 645b1d57f15SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 646d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 647d6dfbf8fSBarry Smith lens[imdex] = n; 6481eb62cbbSBarry Smith slen += n; 6491eb62cbbSBarry Smith count--; 6501eb62cbbSBarry Smith } 651606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 6521eb62cbbSBarry Smith 6531eb62cbbSBarry Smith /* move the data into the send scatter */ 654b1d57f15SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 6551eb62cbbSBarry Smith count = 0; 6561eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 6571eb62cbbSBarry Smith values = rvalues + i*nmax; 6581eb62cbbSBarry Smith for (j=0; j<lens[i]; j++) { 6591eb62cbbSBarry Smith lrows[count++] = values[j] - base; 6601eb62cbbSBarry Smith } 6611eb62cbbSBarry Smith } 662606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 6631d79065fSBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 664606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 665606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 6661eb62cbbSBarry Smith 6671eb62cbbSBarry Smith /* actually zap the local rows */ 6686eb55b6aSBarry Smith /* 6696eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 670a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 6716eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 6726eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 6736eb55b6aSBarry Smith 6746eb55b6aSBarry Smith */ 675e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 676f4df32b1SMatthew Knepley ierr = MatZeroRows(l->B,slen,lrows,0.0);CHKERRQ(ierr); 677d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 678f4df32b1SMatthew Knepley ierr = MatZeroRows(l->A,slen,lrows,diag);CHKERRQ(ierr); 679f4df32b1SMatthew Knepley } else if (diag != 0.0) { 680f4df32b1SMatthew Knepley ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr); 681fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 682e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 683512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 6846525c446SSatish Balay } 685e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 686e2d53e46SBarry Smith row = lrows[i] + rstart; 687f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 688e2d53e46SBarry Smith } 689e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 690e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 6916eb55b6aSBarry Smith } else { 692f4df32b1SMatthew Knepley ierr = MatZeroRows(l->A,slen,lrows,0.0);CHKERRQ(ierr); 6936eb55b6aSBarry Smith } 694606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 69572dacd9aSBarry Smith 6961eb62cbbSBarry Smith /* wait on sends */ 6971eb62cbbSBarry Smith if (nsends) { 698b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 699ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 700606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 7011eb62cbbSBarry Smith } 702606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 703606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 7041eb62cbbSBarry Smith 7053a40ed3dSBarry Smith PetscFunctionReturn(0); 7061eb62cbbSBarry Smith } 7071eb62cbbSBarry Smith 7084a2ae208SSatish Balay #undef __FUNCT__ 7094a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 710dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 7111eb62cbbSBarry Smith { 712416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 713dfbe8321SBarry Smith PetscErrorCode ierr; 714b1d57f15SBarry Smith PetscInt nt; 715416022c9SBarry Smith 7163a40ed3dSBarry Smith PetscFunctionBegin; 717a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 71865e19b50SBarry Smith if (nt != A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap->n,nt); 719ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 720f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 721ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 722f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 7233a40ed3dSBarry Smith PetscFunctionReturn(0); 7241eb62cbbSBarry Smith } 7251eb62cbbSBarry Smith 7264a2ae208SSatish Balay #undef __FUNCT__ 727bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 728bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 729bd0c2dcbSBarry Smith { 730bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 731bd0c2dcbSBarry Smith PetscErrorCode ierr; 732bd0c2dcbSBarry Smith 733bd0c2dcbSBarry Smith PetscFunctionBegin; 734bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 735bd0c2dcbSBarry Smith PetscFunctionReturn(0); 736bd0c2dcbSBarry Smith } 737bd0c2dcbSBarry Smith 738bd0c2dcbSBarry Smith #undef __FUNCT__ 7394a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 740dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 741da3a660dSBarry Smith { 742416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 743dfbe8321SBarry Smith PetscErrorCode ierr; 7443a40ed3dSBarry Smith 7453a40ed3dSBarry Smith PetscFunctionBegin; 746ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 747f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 748ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 749f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 7503a40ed3dSBarry Smith PetscFunctionReturn(0); 751da3a660dSBarry Smith } 752da3a660dSBarry Smith 7534a2ae208SSatish Balay #undef __FUNCT__ 7544a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 755dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 756da3a660dSBarry Smith { 757416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 758dfbe8321SBarry Smith PetscErrorCode ierr; 759a5ff213dSBarry Smith PetscTruth merged; 760da3a660dSBarry Smith 7613a40ed3dSBarry Smith PetscFunctionBegin; 762a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 763da3a660dSBarry Smith /* do nondiagonal part */ 7647c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 765a5ff213dSBarry Smith if (!merged) { 766da3a660dSBarry Smith /* send it on its way */ 767ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 768da3a660dSBarry Smith /* do local part */ 7697c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 770da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 771a5ff213dSBarry Smith /* added in yy until the next line, */ 772ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 773a5ff213dSBarry Smith } else { 774a5ff213dSBarry Smith /* do local part */ 775a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 776a5ff213dSBarry Smith /* send it on its way */ 777ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 778a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 779ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 780a5ff213dSBarry Smith } 7813a40ed3dSBarry Smith PetscFunctionReturn(0); 782da3a660dSBarry Smith } 783da3a660dSBarry Smith 784cd0d46ebSvictorle EXTERN_C_BEGIN 785cd0d46ebSvictorle #undef __FUNCT__ 7865fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 78713c77408SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscTruth *f) 788cd0d46ebSvictorle { 7894f423910Svictorle MPI_Comm comm; 790cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 79166501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 792cd0d46ebSvictorle IS Me,Notme; 7936849ba73SBarry Smith PetscErrorCode ierr; 794b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 795b1d57f15SBarry Smith PetscMPIInt size; 796cd0d46ebSvictorle 797cd0d46ebSvictorle PetscFunctionBegin; 79842e5f5b4Svictorle 79942e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 80066501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 8015485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 802cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 8034f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 804b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 805b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 80642e5f5b4Svictorle 80742e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 808cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 809cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 810b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 811cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 812cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 813268466fbSBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);CHKERRQ(ierr); 814268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 815268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 81666501d38Svictorle Aoff = Aoffs[0]; 817268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 81866501d38Svictorle Boff = Boffs[0]; 8195485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 82066501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 82166501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 82242e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 82342e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 82442e5f5b4Svictorle 825cd0d46ebSvictorle PetscFunctionReturn(0); 826cd0d46ebSvictorle } 827cd0d46ebSvictorle EXTERN_C_END 828cd0d46ebSvictorle 8294a2ae208SSatish Balay #undef __FUNCT__ 8304a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 831dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 832da3a660dSBarry Smith { 833416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 834dfbe8321SBarry Smith PetscErrorCode ierr; 835da3a660dSBarry Smith 8363a40ed3dSBarry Smith PetscFunctionBegin; 837da3a660dSBarry Smith /* do nondiagonal part */ 8387c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 839da3a660dSBarry Smith /* send it on its way */ 840ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 841da3a660dSBarry Smith /* do local part */ 8427c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 843a5ff213dSBarry Smith /* receive remote parts */ 844ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 8453a40ed3dSBarry Smith PetscFunctionReturn(0); 846da3a660dSBarry Smith } 847da3a660dSBarry Smith 8481eb62cbbSBarry Smith /* 8491eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 8501eb62cbbSBarry Smith diagonal block 8511eb62cbbSBarry Smith */ 8524a2ae208SSatish Balay #undef __FUNCT__ 8534a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 854dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 8551eb62cbbSBarry Smith { 856dfbe8321SBarry Smith PetscErrorCode ierr; 857416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 8583a40ed3dSBarry Smith 8593a40ed3dSBarry Smith PetscFunctionBegin; 860e7e72b3dSBarry Smith if (A->rmap->N != A->cmap->N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); 861e7e72b3dSBarry Smith if (A->rmap->rstart != A->cmap->rstart || A->rmap->rend != A->cmap->rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"row partition must equal col partition"); 8623a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 8633a40ed3dSBarry Smith PetscFunctionReturn(0); 8641eb62cbbSBarry Smith } 8651eb62cbbSBarry Smith 8664a2ae208SSatish Balay #undef __FUNCT__ 8674a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 868f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 869052efed2SBarry Smith { 870052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 871dfbe8321SBarry Smith PetscErrorCode ierr; 8723a40ed3dSBarry Smith 8733a40ed3dSBarry Smith PetscFunctionBegin; 874f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 875f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 8763a40ed3dSBarry Smith PetscFunctionReturn(0); 877052efed2SBarry Smith } 878052efed2SBarry Smith 8794a2ae208SSatish Balay #undef __FUNCT__ 8804a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 881dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 8821eb62cbbSBarry Smith { 88344a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 884dfbe8321SBarry Smith PetscErrorCode ierr; 88583e2fdc7SBarry Smith 8863a40ed3dSBarry Smith PetscFunctionBegin; 887aa482453SBarry Smith #if defined(PETSC_USE_LOG) 888d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 889a5a9c739SBarry Smith #endif 8908798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 891a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);} 89278b31e54SBarry Smith ierr = MatDestroy(aij->A);CHKERRQ(ierr); 89378b31e54SBarry Smith ierr = MatDestroy(aij->B);CHKERRQ(ierr); 894aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 8959c666560SBarry Smith if (aij->colmap) {ierr = PetscTableDestroy(aij->colmap);CHKERRQ(ierr);} 896b1fc9764SSatish Balay #else 89705b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 898b1fc9764SSatish Balay #endif 89905b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 9007c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 9017c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 90203095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 9038aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 904606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 905901853e0SKris Buschelman 906dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 907901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 908901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 909901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 910901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 911901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 912ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 913901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 914471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 9153a40ed3dSBarry Smith PetscFunctionReturn(0); 9161eb62cbbSBarry Smith } 917ee50ffe9SBarry Smith 9184a2ae208SSatish Balay #undef __FUNCT__ 9198e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 920dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 9218e2fed03SBarry Smith { 9228e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 9238e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 9248e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 9256849ba73SBarry Smith PetscErrorCode ierr; 92632dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 9276f69ff64SBarry Smith int fd; 928a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 929d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 9308e2fed03SBarry Smith PetscScalar *column_values; 9318e2fed03SBarry Smith 9328e2fed03SBarry Smith PetscFunctionBegin; 9337adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 9347adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 9358e2fed03SBarry Smith nz = A->nz + B->nz; 936958c9bccSBarry Smith if (!rank) { 9370700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 938d0f46423SBarry Smith header[1] = mat->rmap->N; 939d0f46423SBarry Smith header[2] = mat->cmap->N; 9407adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 9418e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 9426f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9438e2fed03SBarry Smith /* get largest number of rows any processor has */ 944d0f46423SBarry Smith rlen = mat->rmap->n; 945d0f46423SBarry Smith range = mat->rmap->range; 9468e2fed03SBarry Smith for (i=1; i<size; i++) { 9478e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 9488e2fed03SBarry Smith } 9498e2fed03SBarry Smith } else { 9507adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 951d0f46423SBarry Smith rlen = mat->rmap->n; 9528e2fed03SBarry Smith } 9538e2fed03SBarry Smith 9548e2fed03SBarry Smith /* load up the local row counts */ 955b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 956d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 9578e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 9588e2fed03SBarry Smith } 9598e2fed03SBarry Smith 9608e2fed03SBarry Smith /* store the row lengths to the file */ 961958c9bccSBarry Smith if (!rank) { 9628e2fed03SBarry Smith MPI_Status status; 963d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9648e2fed03SBarry Smith for (i=1; i<size; i++) { 9658e2fed03SBarry Smith rlen = range[i+1] - range[i]; 9667adad957SLisandro Dalcin ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 9676f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9688e2fed03SBarry Smith } 9698e2fed03SBarry Smith } else { 970d0f46423SBarry Smith ierr = MPI_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 9718e2fed03SBarry Smith } 9728e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 9738e2fed03SBarry Smith 9748e2fed03SBarry Smith /* load up the local column indices */ 9758e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 9767adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 977b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 9788e2fed03SBarry Smith cnt = 0; 979d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 9808e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 9818e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 9828e2fed03SBarry Smith column_indices[cnt++] = col; 9838e2fed03SBarry Smith } 9848e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 9858e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 9868e2fed03SBarry Smith } 9878e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 9888e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 9898e2fed03SBarry Smith } 9908e2fed03SBarry Smith } 991e32f2f54SBarry Smith if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz); 9928e2fed03SBarry Smith 9938e2fed03SBarry Smith /* store the column indices to the file */ 994958c9bccSBarry Smith if (!rank) { 9958e2fed03SBarry Smith MPI_Status status; 9966f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9978e2fed03SBarry Smith for (i=1; i<size; i++) { 9987adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 999e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 10007adad957SLisandro Dalcin ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 10016f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 10028e2fed03SBarry Smith } 10038e2fed03SBarry Smith } else { 10047adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10057adad957SLisandro Dalcin ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10068e2fed03SBarry Smith } 10078e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 10088e2fed03SBarry Smith 10098e2fed03SBarry Smith /* load up the local column values */ 10108e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 10118e2fed03SBarry Smith cnt = 0; 1012d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 10138e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 10148e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 10158e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 10168e2fed03SBarry Smith } 10178e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 10188e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 10198e2fed03SBarry Smith } 10208e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 10218e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 10228e2fed03SBarry Smith } 10238e2fed03SBarry Smith } 1024e32f2f54SBarry Smith if (cnt != A->nz + B->nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz); 10258e2fed03SBarry Smith 10268e2fed03SBarry Smith /* store the column values to the file */ 1027958c9bccSBarry Smith if (!rank) { 10288e2fed03SBarry Smith MPI_Status status; 10296f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 10308e2fed03SBarry Smith for (i=1; i<size; i++) { 10317adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1032e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 10337adad957SLisandro Dalcin ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 10346f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 10358e2fed03SBarry Smith } 10368e2fed03SBarry Smith } else { 10377adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10387adad957SLisandro Dalcin ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10398e2fed03SBarry Smith } 10408e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 10418e2fed03SBarry Smith PetscFunctionReturn(0); 10428e2fed03SBarry Smith } 10438e2fed03SBarry Smith 10448e2fed03SBarry Smith #undef __FUNCT__ 10454a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1046dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1047416022c9SBarry Smith { 104844a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1049dfbe8321SBarry Smith PetscErrorCode ierr; 105032dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1051d38fa0fbSBarry Smith PetscTruth isdraw,iascii,isbinary; 1052b0a32e0cSBarry Smith PetscViewer sviewer; 1053f3ef73ceSBarry Smith PetscViewerFormat format; 1054416022c9SBarry Smith 10553a40ed3dSBarry Smith PetscFunctionBegin; 10562692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 10572692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 10582692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 105932077d6dSBarry Smith if (iascii) { 1060b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1061456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 10624e220ebcSLois Curfman McInnes MatInfo info; 1063923f20ffSKris Buschelman PetscTruth inodes; 1064923f20ffSKris Buschelman 10657adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1066888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1067923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 1068923f20ffSKris Buschelman if (!inodes) { 106977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1070d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 10716831982aSBarry Smith } else { 107277431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1073d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 10746831982aSBarry Smith } 1075888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 107677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1077888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 107877431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1079b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 108007d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1081a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 10823a40ed3dSBarry Smith PetscFunctionReturn(0); 1083fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1084923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1085923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1086923f20ffSKris Buschelman if (inodes) { 1087923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1088d38fa0fbSBarry Smith } else { 1089d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1090d38fa0fbSBarry Smith } 10913a40ed3dSBarry Smith PetscFunctionReturn(0); 10924aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 10934aedb280SBarry Smith PetscFunctionReturn(0); 109408480c60SBarry Smith } 10958e2fed03SBarry Smith } else if (isbinary) { 10968e2fed03SBarry Smith if (size == 1) { 10977adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 10988e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 10998e2fed03SBarry Smith } else { 11008e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 11018e2fed03SBarry Smith } 11028e2fed03SBarry Smith PetscFunctionReturn(0); 11030f5bd95cSBarry Smith } else if (isdraw) { 1104b0a32e0cSBarry Smith PetscDraw draw; 110519bcc07fSBarry Smith PetscTruth isnull; 1106b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1107b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 110819bcc07fSBarry Smith } 110919bcc07fSBarry Smith 111017699dbbSLois Curfman McInnes if (size == 1) { 11117adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 111278b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 11133a40ed3dSBarry Smith } else { 111495373324SBarry Smith /* assemble the entire matrix onto first processor. */ 111595373324SBarry Smith Mat A; 1116ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1117d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1118dd6ea824SBarry Smith MatScalar *a; 11192ee70a88SLois Curfman McInnes 112032a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 112190d69ab7SBarry Smith PetscTruth flg = PETSC_FALSE; 112232a366e4SMatthew Knepley 11230c235cafSBarry Smith ierr = PetscOptionsGetTruth(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 112432a366e4SMatthew Knepley if (!flg) { 1125e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large."); 112632a366e4SMatthew Knepley } 112732a366e4SMatthew Knepley } 11280805154bSBarry Smith 11297adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 113017699dbbSLois Curfman McInnes if (!rank) { 1131f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 11323a40ed3dSBarry Smith } else { 1133f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 113495373324SBarry Smith } 1135f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1136f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1137f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 113852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1139416022c9SBarry Smith 114095373324SBarry Smith /* copy over the A part */ 1141ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1142d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1143d0f46423SBarry Smith row = mat->rmap->rstart; 1144d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 114595373324SBarry Smith for (i=0; i<m; i++) { 1146416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 114795373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 114895373324SBarry Smith } 11492ee70a88SLois Curfman McInnes aj = Aloc->j; 1150d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 115195373324SBarry Smith 115295373324SBarry Smith /* copy over the B part */ 1153ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1154d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1155d0f46423SBarry Smith row = mat->rmap->rstart; 1156b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1157b0a32e0cSBarry Smith ct = cols; 1158bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 115995373324SBarry Smith for (i=0; i<m; i++) { 1160416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 116195373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 116295373324SBarry Smith } 1163606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 11646d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 11656d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 116655843e3eSBarry Smith /* 116755843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1168b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 116955843e3eSBarry Smith */ 1170b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1171e03a110bSBarry Smith if (!rank) { 11727adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 11736831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 117495373324SBarry Smith } 1175b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 117678b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 117795373324SBarry Smith } 11783a40ed3dSBarry Smith PetscFunctionReturn(0); 11791eb62cbbSBarry Smith } 11801eb62cbbSBarry Smith 11814a2ae208SSatish Balay #undef __FUNCT__ 11824a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1183dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1184416022c9SBarry Smith { 1185dfbe8321SBarry Smith PetscErrorCode ierr; 118632077d6dSBarry Smith PetscTruth iascii,isdraw,issocket,isbinary; 1187416022c9SBarry Smith 11883a40ed3dSBarry Smith PetscFunctionBegin; 11892692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 11902692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 11912692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 11922692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 119332077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 11947b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 11955cd90555SBarry Smith } else { 1196e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1197416022c9SBarry Smith } 11983a40ed3dSBarry Smith PetscFunctionReturn(0); 1199416022c9SBarry Smith } 1200416022c9SBarry Smith 12014a2ae208SSatish Balay #undef __FUNCT__ 120241f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 120341f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 12048a729477SBarry Smith { 120544a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1206dfbe8321SBarry Smith PetscErrorCode ierr; 12076987fefcSBarry Smith Vec bb1 = 0; 1208bd0c2dcbSBarry Smith PetscTruth hasop; 12098a729477SBarry Smith 12103a40ed3dSBarry Smith PetscFunctionBegin; 121185911e72SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 121285911e72SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 121385911e72SJed Brown } 12142798e883SHong Zhang 1215a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 121641f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1217a2b30743SBarry Smith PetscFunctionReturn(0); 1218a2b30743SBarry Smith } 1219a2b30743SBarry Smith 1220c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1221da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 122241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12232798e883SHong Zhang its--; 1224da3a660dSBarry Smith } 12252798e883SHong Zhang 12262798e883SHong Zhang while (its--) { 1227ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1228ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12292798e883SHong Zhang 1230c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1231efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1232c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 12332798e883SHong Zhang 1234c14dc6b6SHong Zhang /* local sweep */ 123541f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 12362798e883SHong Zhang } 12373a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1238da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 123941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12402798e883SHong Zhang its--; 1241da3a660dSBarry Smith } 12422798e883SHong Zhang while (its--) { 1243ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1244ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12452798e883SHong Zhang 1246c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1247efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1248c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1249c14dc6b6SHong Zhang 1250c14dc6b6SHong Zhang /* local sweep */ 125141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 12522798e883SHong Zhang } 12533a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1254da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 125541f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12562798e883SHong Zhang its--; 1257da3a660dSBarry Smith } 12582798e883SHong Zhang while (its--) { 1259ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1260ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12612798e883SHong Zhang 1262c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1263efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1264c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 12652798e883SHong Zhang 1266c14dc6b6SHong Zhang /* local sweep */ 126741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 12682798e883SHong Zhang } 1269a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1270a7420bb7SBarry Smith Vec xx1; 1271a7420bb7SBarry Smith 1272a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 127341f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP),fshift,lits,1,xx);CHKERRQ(ierr); 1274a7420bb7SBarry Smith 1275a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1276a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1277a7420bb7SBarry Smith if (!mat->diag) { 1278a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1279a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1280a7420bb7SBarry Smith } 1281bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1282bd0c2dcbSBarry Smith if (hasop) { 1283bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1284bd0c2dcbSBarry Smith } else { 1285a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1286bd0c2dcbSBarry Smith } 1287887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1288887ee2caSBarry Smith 1289a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1290a7420bb7SBarry Smith 1291a7420bb7SBarry Smith /* local sweep */ 129241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,(MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP),fshift,lits,1,xx1);CHKERRQ(ierr); 1293a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 1294a7420bb7SBarry Smith ierr = VecDestroy(xx1);CHKERRQ(ierr); 12953a40ed3dSBarry Smith } else { 1296e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1297c16cb8f2SBarry Smith } 1298c14dc6b6SHong Zhang 12996987fefcSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 13003a40ed3dSBarry Smith PetscFunctionReturn(0); 13018a729477SBarry Smith } 1302a66be287SLois Curfman McInnes 13034a2ae208SSatish Balay #undef __FUNCT__ 130442e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 130542e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 130642e855d1Svictor { 130742e855d1Svictor MPI_Comm comm,pcomm; 13085d0c19d7SBarry Smith PetscInt first,local_size,nrows; 13095d0c19d7SBarry Smith const PetscInt *rows; 1310dbf0e21dSBarry Smith PetscMPIInt size; 131142e855d1Svictor IS crowp,growp,irowp,lrowp,lcolp,icolp; 131242e855d1Svictor PetscErrorCode ierr; 131342e855d1Svictor 131442e855d1Svictor PetscFunctionBegin; 131542e855d1Svictor ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 131642e855d1Svictor /* make a collective version of 'rowp' */ 131742e855d1Svictor ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 131842e855d1Svictor if (pcomm==comm) { 131942e855d1Svictor crowp = rowp; 132042e855d1Svictor } else { 132142e855d1Svictor ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 132242e855d1Svictor ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 132342e855d1Svictor ierr = ISCreateGeneral(comm,nrows,rows,&crowp);CHKERRQ(ierr); 132442e855d1Svictor ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 132542e855d1Svictor } 132642e855d1Svictor /* collect the global row permutation and invert it */ 132742e855d1Svictor ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 132842e855d1Svictor ierr = ISSetPermutation(growp);CHKERRQ(ierr); 132942e855d1Svictor if (pcomm!=comm) { 133042e855d1Svictor ierr = ISDestroy(crowp);CHKERRQ(ierr); 133142e855d1Svictor } 133242e855d1Svictor ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 133342e855d1Svictor /* get the local target indices */ 133442e855d1Svictor ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 133542e855d1Svictor ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 133642e855d1Svictor ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 133742e855d1Svictor ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp);CHKERRQ(ierr); 133842e855d1Svictor ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 133942e855d1Svictor ierr = ISDestroy(irowp);CHKERRQ(ierr); 134042e855d1Svictor /* the column permutation is so much easier; 134142e855d1Svictor make a local version of 'colp' and invert it */ 134242e855d1Svictor ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 1343dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 1344dbf0e21dSBarry Smith if (size==1) { 134542e855d1Svictor lcolp = colp; 134642e855d1Svictor } else { 134742e855d1Svictor ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 134842e855d1Svictor ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 134942e855d1Svictor ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp);CHKERRQ(ierr); 135042e855d1Svictor } 1351dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 135242e855d1Svictor ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 13534aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 1354dbf0e21dSBarry Smith if (size>1) { 135542e855d1Svictor ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 135642e855d1Svictor ierr = ISDestroy(lcolp);CHKERRQ(ierr); 135742e855d1Svictor } 135842e855d1Svictor /* now we just get the submatrix */ 13594aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 136042e855d1Svictor /* clean up */ 136142e855d1Svictor ierr = ISDestroy(lrowp);CHKERRQ(ierr); 136242e855d1Svictor ierr = ISDestroy(icolp);CHKERRQ(ierr); 136342e855d1Svictor PetscFunctionReturn(0); 136442e855d1Svictor } 136542e855d1Svictor 136642e855d1Svictor #undef __FUNCT__ 13674a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1368dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1369a66be287SLois Curfman McInnes { 1370a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1371a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1372dfbe8321SBarry Smith PetscErrorCode ierr; 1373329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1374a66be287SLois Curfman McInnes 13753a40ed3dSBarry Smith PetscFunctionBegin; 13764e220ebcSLois Curfman McInnes info->block_size = 1.0; 13774e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 13784e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 13794e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 13804e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 13814e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 13824e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1383a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 13844e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 13854e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 13864e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 13874e220ebcSLois Curfman McInnes info->memory = isend[3]; 13884e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1389a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 13907adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 13914e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 13924e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 13934e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 13944e220ebcSLois Curfman McInnes info->memory = irecv[3]; 13954e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1396a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 13977adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 13984e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 13994e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 14004e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 14014e220ebcSLois Curfman McInnes info->memory = irecv[3]; 14024e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1403a66be287SLois Curfman McInnes } 14044e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 14054e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 14064e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 14074e220ebcSLois Curfman McInnes 14083a40ed3dSBarry Smith PetscFunctionReturn(0); 1409a66be287SLois Curfman McInnes } 1410a66be287SLois Curfman McInnes 14114a2ae208SSatish Balay #undef __FUNCT__ 14124a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 14134e0d8c25SBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscTruth flg) 1414c74985f6SBarry Smith { 1415c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1416dfbe8321SBarry Smith PetscErrorCode ierr; 1417c74985f6SBarry Smith 14183a40ed3dSBarry Smith PetscFunctionBegin; 141912c028f9SKris Buschelman switch (op) { 1420512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 142112c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 142228b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1423a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 142412c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 142512c028f9SKris Buschelman case MAT_USE_INODES: 142612c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 14274e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 14284e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 142912c028f9SKris Buschelman break; 143012c028f9SKris Buschelman case MAT_ROW_ORIENTED: 14314e0d8c25SBarry Smith a->roworiented = flg; 14324e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 14334e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 143412c028f9SKris Buschelman break; 14354e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1436290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 143712c028f9SKris Buschelman break; 143812c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 14397c922b88SBarry Smith a->donotstash = PETSC_TRUE; 144012c028f9SKris Buschelman break; 144177e54ba9SKris Buschelman case MAT_SYMMETRIC: 14424e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 144325f421beSHong Zhang break; 144477e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1445eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1446eeffb40dSHong Zhang break; 1447bf108f30SBarry Smith case MAT_HERMITIAN: 1448eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1449eeffb40dSHong Zhang break; 1450bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 14514e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 145277e54ba9SKris Buschelman break; 145312c028f9SKris Buschelman default: 1454e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 14553a40ed3dSBarry Smith } 14563a40ed3dSBarry Smith PetscFunctionReturn(0); 1457c74985f6SBarry Smith } 1458c74985f6SBarry Smith 14594a2ae208SSatish Balay #undef __FUNCT__ 14604a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1461b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 146239e00950SLois Curfman McInnes { 1463154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 146487828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 14656849ba73SBarry Smith PetscErrorCode ierr; 1466d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1467d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1468b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 146939e00950SLois Curfman McInnes 14703a40ed3dSBarry Smith PetscFunctionBegin; 1471e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 14727a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 14737a0afa10SBarry Smith 147470f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 14757a0afa10SBarry Smith /* 14767a0afa10SBarry Smith allocate enough space to hold information from the longest row. 14777a0afa10SBarry Smith */ 14787a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1479b1d57f15SBarry Smith PetscInt max = 1,tmp; 1480d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 14817a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 14827a0afa10SBarry Smith if (max < tmp) { max = tmp; } 14837a0afa10SBarry Smith } 14841d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 14857a0afa10SBarry Smith } 14867a0afa10SBarry Smith 1487e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1488abc0e9e4SLois Curfman McInnes lrow = row - rstart; 148939e00950SLois Curfman McInnes 1490154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1491154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1492154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1493f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1494f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1495154123eaSLois Curfman McInnes nztot = nzA + nzB; 1496154123eaSLois Curfman McInnes 149770f0671dSBarry Smith cmap = mat->garray; 1498154123eaSLois Curfman McInnes if (v || idx) { 1499154123eaSLois Curfman McInnes if (nztot) { 1500154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1501b1d57f15SBarry Smith PetscInt imark = -1; 1502154123eaSLois Curfman McInnes if (v) { 150370f0671dSBarry Smith *v = v_p = mat->rowvalues; 150439e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 150570f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1506154123eaSLois Curfman McInnes else break; 1507154123eaSLois Curfman McInnes } 1508154123eaSLois Curfman McInnes imark = i; 150970f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 151070f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1511154123eaSLois Curfman McInnes } 1512154123eaSLois Curfman McInnes if (idx) { 151370f0671dSBarry Smith *idx = idx_p = mat->rowindices; 151470f0671dSBarry Smith if (imark > -1) { 151570f0671dSBarry Smith for (i=0; i<imark; i++) { 151670f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 151770f0671dSBarry Smith } 151870f0671dSBarry Smith } else { 1519154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 152070f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1521154123eaSLois Curfman McInnes else break; 1522154123eaSLois Curfman McInnes } 1523154123eaSLois Curfman McInnes imark = i; 152470f0671dSBarry Smith } 152570f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 152670f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 152739e00950SLois Curfman McInnes } 15283f97c4b0SBarry Smith } else { 15291ca473b0SSatish Balay if (idx) *idx = 0; 15301ca473b0SSatish Balay if (v) *v = 0; 15311ca473b0SSatish Balay } 1532154123eaSLois Curfman McInnes } 153339e00950SLois Curfman McInnes *nz = nztot; 1534f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1535f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 15363a40ed3dSBarry Smith PetscFunctionReturn(0); 153739e00950SLois Curfman McInnes } 153839e00950SLois Curfman McInnes 15394a2ae208SSatish Balay #undef __FUNCT__ 15404a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1541b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 154239e00950SLois Curfman McInnes { 15437a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 15443a40ed3dSBarry Smith 15453a40ed3dSBarry Smith PetscFunctionBegin; 1546e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 15477a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 15483a40ed3dSBarry Smith PetscFunctionReturn(0); 154939e00950SLois Curfman McInnes } 155039e00950SLois Curfman McInnes 15514a2ae208SSatish Balay #undef __FUNCT__ 15524a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1553dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1554855ac2c5SLois Curfman McInnes { 1555855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1556ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1557dfbe8321SBarry Smith PetscErrorCode ierr; 1558d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1559329f5518SBarry Smith PetscReal sum = 0.0; 1560a77337e4SBarry Smith MatScalar *v; 156104ca555eSLois Curfman McInnes 15623a40ed3dSBarry Smith PetscFunctionBegin; 156317699dbbSLois Curfman McInnes if (aij->size == 1) { 156414183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 156537fa93a5SLois Curfman McInnes } else { 156604ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 156704ca555eSLois Curfman McInnes v = amat->a; 156804ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1569aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1570329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 157104ca555eSLois Curfman McInnes #else 157204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 157304ca555eSLois Curfman McInnes #endif 157404ca555eSLois Curfman McInnes } 157504ca555eSLois Curfman McInnes v = bmat->a; 157604ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1577aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1578329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 157904ca555eSLois Curfman McInnes #else 158004ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 158104ca555eSLois Curfman McInnes #endif 158204ca555eSLois Curfman McInnes } 15837adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 158404ca555eSLois Curfman McInnes *norm = sqrt(*norm); 15853a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1586329f5518SBarry Smith PetscReal *tmp,*tmp2; 1587b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 1588d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1589d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1590d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 159104ca555eSLois Curfman McInnes *norm = 0.0; 159204ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 159304ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1594bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 159504ca555eSLois Curfman McInnes } 159604ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 159704ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1598bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 159904ca555eSLois Curfman McInnes } 1600d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 1601d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 160204ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 160304ca555eSLois Curfman McInnes } 1604606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1605606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 16063a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1607329f5518SBarry Smith PetscReal ntemp = 0.0; 1608d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1609bfec09a0SHong Zhang v = amat->a + amat->i[j]; 161004ca555eSLois Curfman McInnes sum = 0.0; 161104ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1612cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 161304ca555eSLois Curfman McInnes } 1614bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 161504ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1616cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 161704ca555eSLois Curfman McInnes } 1618515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 161904ca555eSLois Curfman McInnes } 16207adad957SLisandro Dalcin ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 1621ca161407SBarry Smith } else { 1622e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 162304ca555eSLois Curfman McInnes } 162437fa93a5SLois Curfman McInnes } 16253a40ed3dSBarry Smith PetscFunctionReturn(0); 1626855ac2c5SLois Curfman McInnes } 1627855ac2c5SLois Curfman McInnes 16284a2ae208SSatish Balay #undef __FUNCT__ 16294a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1630fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1631b7c46309SBarry Smith { 1632b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1633da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1634dfbe8321SBarry Smith PetscErrorCode ierr; 1635d0f46423SBarry Smith PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz; 1636d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 16373a40ed3dSBarry Smith Mat B; 1638a77337e4SBarry Smith MatScalar *array; 1639b7c46309SBarry Smith 16403a40ed3dSBarry Smith PetscFunctionBegin; 1641e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1642da668accSHong Zhang 1643d0f46423SBarry Smith ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; 1644da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1645da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1646fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 1647fc73b1b3SBarry Smith /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */ 1648fc73b1b3SBarry Smith ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr); 1649da668accSHong Zhang ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr); 1650da668accSHong Zhang for (i=0; i<ai[ma]; i++){ 1651da668accSHong Zhang d_nnz[aj[i]] ++; 1652da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1653d4bb536fSBarry Smith } 1654d4bb536fSBarry Smith 16557adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1656d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 16577adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1658da668accSHong Zhang ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr); 1659fc73b1b3SBarry Smith ierr = PetscFree(d_nnz);CHKERRQ(ierr); 1660fc4dec0aSBarry Smith } else { 1661fc4dec0aSBarry Smith B = *matout; 1662fc4dec0aSBarry Smith } 1663b7c46309SBarry Smith 1664b7c46309SBarry Smith /* copy over the A part */ 1665da668accSHong Zhang array = Aloc->a; 1666d0f46423SBarry Smith row = A->rmap->rstart; 1667da668accSHong Zhang for (i=0; i<ma; i++) { 1668da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1669da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1670da668accSHong Zhang row++; array += ncol; aj += ncol; 1671b7c46309SBarry Smith } 1672b7c46309SBarry Smith aj = Aloc->j; 1673da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1674b7c46309SBarry Smith 1675b7c46309SBarry Smith /* copy over the B part */ 1676fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1677fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 1678da668accSHong Zhang array = Bloc->a; 1679d0f46423SBarry Smith row = A->rmap->rstart; 1680da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 168161a2fbbaSHong Zhang cols_tmp = cols; 1682da668accSHong Zhang for (i=0; i<mb; i++) { 1683da668accSHong Zhang ncol = bi[i+1]-bi[i]; 168461a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 168561a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 1686b7c46309SBarry Smith } 1687fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1688fc73b1b3SBarry Smith 16896d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 16906d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1691815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 16920de55854SLois Curfman McInnes *matout = B; 16930de55854SLois Curfman McInnes } else { 1694273d9f13SBarry Smith ierr = MatHeaderCopy(A,B);CHKERRQ(ierr); 16950de55854SLois Curfman McInnes } 16963a40ed3dSBarry Smith PetscFunctionReturn(0); 1697b7c46309SBarry Smith } 1698b7c46309SBarry Smith 16994a2ae208SSatish Balay #undef __FUNCT__ 17004a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1701dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1702a008b906SSatish Balay { 17034b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17044b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1705dfbe8321SBarry Smith PetscErrorCode ierr; 1706b1d57f15SBarry Smith PetscInt s1,s2,s3; 1707a008b906SSatish Balay 17083a40ed3dSBarry Smith PetscFunctionBegin; 17094b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 17104b967eb1SSatish Balay if (rr) { 1711e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1712e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 17134b967eb1SSatish Balay /* Overlap communication with computation. */ 1714ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1715a008b906SSatish Balay } 17164b967eb1SSatish Balay if (ll) { 1717e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1718e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1719f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 17204b967eb1SSatish Balay } 17214b967eb1SSatish Balay /* scale the diagonal block */ 1722f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 17234b967eb1SSatish Balay 17244b967eb1SSatish Balay if (rr) { 17254b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1726ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1727f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 17284b967eb1SSatish Balay } 17294b967eb1SSatish Balay 17303a40ed3dSBarry Smith PetscFunctionReturn(0); 1731a008b906SSatish Balay } 1732a008b906SSatish Balay 17334a2ae208SSatish Balay #undef __FUNCT__ 1734521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ" 1735521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs) 17365a838052SSatish Balay { 1737521d7252SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1738521d7252SBarry Smith PetscErrorCode ierr; 1739521d7252SBarry Smith 17403a40ed3dSBarry Smith PetscFunctionBegin; 1741521d7252SBarry Smith ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 1742521d7252SBarry Smith ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1743829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 1744829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 17453a40ed3dSBarry Smith PetscFunctionReturn(0); 17465a838052SSatish Balay } 17474a2ae208SSatish Balay #undef __FUNCT__ 17484a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 1749dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 1750bb5a7306SBarry Smith { 1751bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1752dfbe8321SBarry Smith PetscErrorCode ierr; 17533a40ed3dSBarry Smith 17543a40ed3dSBarry Smith PetscFunctionBegin; 1755bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 17563a40ed3dSBarry Smith PetscFunctionReturn(0); 1757bb5a7306SBarry Smith } 1758bb5a7306SBarry Smith 17594a2ae208SSatish Balay #undef __FUNCT__ 17604a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 1761dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag) 1762d4bb536fSBarry Smith { 1763d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 1764d4bb536fSBarry Smith Mat a,b,c,d; 1765d4bb536fSBarry Smith PetscTruth flg; 1766dfbe8321SBarry Smith PetscErrorCode ierr; 1767d4bb536fSBarry Smith 17683a40ed3dSBarry Smith PetscFunctionBegin; 1769d4bb536fSBarry Smith a = matA->A; b = matA->B; 1770d4bb536fSBarry Smith c = matB->A; d = matB->B; 1771d4bb536fSBarry Smith 1772d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 1773abc0a331SBarry Smith if (flg) { 1774d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 1775d4bb536fSBarry Smith } 17767adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 17773a40ed3dSBarry Smith PetscFunctionReturn(0); 1778d4bb536fSBarry Smith } 1779d4bb536fSBarry Smith 17804a2ae208SSatish Balay #undef __FUNCT__ 17814a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 1782dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1783cb5b572fSBarry Smith { 1784dfbe8321SBarry Smith PetscErrorCode ierr; 1785cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1786cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1787cb5b572fSBarry Smith 1788cb5b572fSBarry Smith PetscFunctionBegin; 178933f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 179033f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 1791cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1792cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1793cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1794cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1795cb5b572fSBarry Smith then copying the submatrices */ 1796cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1797cb5b572fSBarry Smith } else { 1798cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1799cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1800cb5b572fSBarry Smith } 1801cb5b572fSBarry Smith PetscFunctionReturn(0); 1802cb5b572fSBarry Smith } 1803cb5b572fSBarry Smith 18044a2ae208SSatish Balay #undef __FUNCT__ 18054a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 1806dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A) 1807273d9f13SBarry Smith { 1808dfbe8321SBarry Smith PetscErrorCode ierr; 1809273d9f13SBarry Smith 1810273d9f13SBarry Smith PetscFunctionBegin; 1811273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 1812273d9f13SBarry Smith PetscFunctionReturn(0); 1813273d9f13SBarry Smith } 1814273d9f13SBarry Smith 1815ac90fabeSBarry Smith #undef __FUNCT__ 1816ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 1817f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 1818ac90fabeSBarry Smith { 1819dfbe8321SBarry Smith PetscErrorCode ierr; 1820b1d57f15SBarry Smith PetscInt i; 1821ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 18224ce68768SBarry Smith PetscBLASInt bnz,one=1; 1823ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 1824ac90fabeSBarry Smith 1825ac90fabeSBarry Smith PetscFunctionBegin; 1826ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 1827f4df32b1SMatthew Knepley PetscScalar alpha = a; 1828ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 1829ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 18300805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 1831f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 1832ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 1833ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 18340805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 1835f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 1836a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 1837f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 1838c537a176SHong Zhang 1839c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 1840a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 1841a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 1842a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 1843a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 1844c537a176SHong Zhang } 1845a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 1846d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 1847a30b2313SHong Zhang y->XtoY = xx->B; 1848407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 1849c537a176SHong Zhang } 1850f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 1851ac90fabeSBarry Smith } else { 1852f4df32b1SMatthew Knepley ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr); 1853ac90fabeSBarry Smith } 1854ac90fabeSBarry Smith PetscFunctionReturn(0); 1855ac90fabeSBarry Smith } 1856ac90fabeSBarry Smith 1857354c94deSBarry Smith EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat); 1858354c94deSBarry Smith 1859354c94deSBarry Smith #undef __FUNCT__ 1860354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 1861354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat) 1862354c94deSBarry Smith { 1863354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 1864354c94deSBarry Smith PetscErrorCode ierr; 1865354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 1866354c94deSBarry Smith 1867354c94deSBarry Smith PetscFunctionBegin; 1868354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 1869354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 1870354c94deSBarry Smith #else 1871354c94deSBarry Smith PetscFunctionBegin; 1872354c94deSBarry Smith #endif 1873354c94deSBarry Smith PetscFunctionReturn(0); 1874354c94deSBarry Smith } 1875354c94deSBarry Smith 187699cafbc1SBarry Smith #undef __FUNCT__ 187799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 187899cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 187999cafbc1SBarry Smith { 188099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 188199cafbc1SBarry Smith PetscErrorCode ierr; 188299cafbc1SBarry Smith 188399cafbc1SBarry Smith PetscFunctionBegin; 188499cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 188599cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 188699cafbc1SBarry Smith PetscFunctionReturn(0); 188799cafbc1SBarry Smith } 188899cafbc1SBarry Smith 188999cafbc1SBarry Smith #undef __FUNCT__ 189099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 189199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 189299cafbc1SBarry Smith { 189399cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 189499cafbc1SBarry Smith PetscErrorCode ierr; 189599cafbc1SBarry Smith 189699cafbc1SBarry Smith PetscFunctionBegin; 189799cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 189899cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 189999cafbc1SBarry Smith PetscFunctionReturn(0); 190099cafbc1SBarry Smith } 190199cafbc1SBarry Smith 1902103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 1903103bf8bdSMatthew Knepley 1904103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 1905a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 1906a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 1907a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 1908103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 1909a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 1910d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 1911103bf8bdSMatthew Knepley 1912103bf8bdSMatthew Knepley #undef __FUNCT__ 1913103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 1914103bf8bdSMatthew Knepley /* 1915103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 1916103bf8bdSMatthew Knepley */ 19170481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 1918103bf8bdSMatthew Knepley { 1919a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 1920a2c909beSMatthew Knepley 1921a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 1922a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 1923a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 1924a2c909beSMatthew Knepley 1925103bf8bdSMatthew Knepley PetscTruth row_identity, col_identity; 1926776b82aeSLisandro Dalcin PetscContainer c; 1927103bf8bdSMatthew Knepley PetscInt m, n, M, N; 1928103bf8bdSMatthew Knepley PetscErrorCode ierr; 1929103bf8bdSMatthew Knepley 1930103bf8bdSMatthew Knepley PetscFunctionBegin; 1931e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 1932103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 1933103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 1934103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 1935e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 1936103bf8bdSMatthew Knepley } 1937103bf8bdSMatthew Knepley 1938103bf8bdSMatthew Knepley process_group_type pg; 1939a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 1940a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 1941a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 1942a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 1943a2c909beSMatthew Knepley 1944103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 1945a2c909beSMatthew Knepley ilu_permuted(level_graph); 1946103bf8bdSMatthew Knepley 1947103bf8bdSMatthew Knepley /* put together the new matrix */ 19487adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 1949103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 1950103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 1951719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 1952719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 1953719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1954719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1955103bf8bdSMatthew Knepley 19567adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 1957776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 1958719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 1959103bf8bdSMatthew Knepley PetscFunctionReturn(0); 1960103bf8bdSMatthew Knepley } 1961103bf8bdSMatthew Knepley 1962103bf8bdSMatthew Knepley #undef __FUNCT__ 1963103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 19640481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 1965103bf8bdSMatthew Knepley { 1966103bf8bdSMatthew Knepley PetscFunctionBegin; 1967103bf8bdSMatthew Knepley PetscFunctionReturn(0); 1968103bf8bdSMatthew Knepley } 1969103bf8bdSMatthew Knepley 1970103bf8bdSMatthew Knepley #undef __FUNCT__ 1971103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 1972103bf8bdSMatthew Knepley /* 1973103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 1974103bf8bdSMatthew Knepley */ 1975103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 1976103bf8bdSMatthew Knepley { 1977a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 1978a2c909beSMatthew Knepley 1979a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 1980a2c909beSMatthew Knepley lgraph_type* lgraph_p; 1981776b82aeSLisandro Dalcin PetscContainer c; 1982103bf8bdSMatthew Knepley PetscErrorCode ierr; 1983103bf8bdSMatthew Knepley 1984103bf8bdSMatthew Knepley PetscFunctionBegin; 1985103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 1986776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 1987103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 1988a2c909beSMatthew Knepley 1989a2c909beSMatthew Knepley PetscScalar* array_x; 1990a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 1991a2c909beSMatthew Knepley PetscInt sx; 1992a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 1993a2c909beSMatthew Knepley 1994a2c909beSMatthew Knepley PetscScalar* array_b; 1995a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 1996a2c909beSMatthew Knepley PetscInt sb; 1997a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 1998a2c909beSMatthew Knepley 1999a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2000a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2001a2c909beSMatthew Knepley 2002a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2003a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2004a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2005a2c909beSMatthew Knepley 2006a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2007a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2008a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2009a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2010a2c909beSMatthew Knepley 2011a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2012a2c909beSMatthew Knepley 2013103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2014103bf8bdSMatthew Knepley } 2015103bf8bdSMatthew Knepley #endif 2016103bf8bdSMatthew Knepley 201769db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 201869db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 20191d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 20201d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 202169db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 202269db28dcSHong Zhang PetscErrorCode (*MatDestroy)(Mat); 202369db28dcSHong Zhang } Mat_Redundant; 202469db28dcSHong Zhang 202569db28dcSHong Zhang #undef __FUNCT__ 202669db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 202769db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 202869db28dcSHong Zhang { 202969db28dcSHong Zhang PetscErrorCode ierr; 203069db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 203169db28dcSHong Zhang PetscInt i; 203269db28dcSHong Zhang 203369db28dcSHong Zhang PetscFunctionBegin; 20341d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 203569db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 203669db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 203769db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 203869db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 203969db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 204069db28dcSHong Zhang } 20411d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 204269db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 204369db28dcSHong Zhang PetscFunctionReturn(0); 204469db28dcSHong Zhang } 204569db28dcSHong Zhang 204669db28dcSHong Zhang #undef __FUNCT__ 204769db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 204869db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 204969db28dcSHong Zhang { 205069db28dcSHong Zhang PetscErrorCode ierr; 205169db28dcSHong Zhang PetscContainer container; 205269db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 205369db28dcSHong Zhang 205469db28dcSHong Zhang PetscFunctionBegin; 205569db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 205669db28dcSHong Zhang if (container) { 205769db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 205869db28dcSHong Zhang } else { 2059e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 206069db28dcSHong Zhang } 206169db28dcSHong Zhang A->ops->destroy = redund->MatDestroy; 206269db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 206369db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 206469db28dcSHong Zhang ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 206569db28dcSHong Zhang PetscFunctionReturn(0); 206669db28dcSHong Zhang } 206769db28dcSHong Zhang 206869db28dcSHong Zhang #undef __FUNCT__ 206969db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 207069db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 207169db28dcSHong Zhang { 207269db28dcSHong Zhang PetscMPIInt rank,size; 20737adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 207469db28dcSHong Zhang PetscErrorCode ierr; 207569db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 207669db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2077d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 207869db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 207969db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 208069db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 208169db28dcSHong Zhang PetscScalar *sbuf_a; 208269db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2083d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2084d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 208569db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2086a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2087a77337e4SBarry Smith PetscScalar *vals; 208869db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 208969db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 209069db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 209169db28dcSHong Zhang MPI_Status recv_status,*send_status; 209269db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 209369db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 209469db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 209569db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 209669db28dcSHong Zhang PetscContainer container; 209769db28dcSHong Zhang 209869db28dcSHong Zhang PetscFunctionBegin; 209969db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 210069db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 210169db28dcSHong Zhang 210269db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 210369db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2104e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 210569db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2106e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 210769db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 210869db28dcSHong Zhang if (container) { 210969db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 211069db28dcSHong Zhang } else { 2111e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 211269db28dcSHong Zhang } 2113e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 211469db28dcSHong Zhang 211569db28dcSHong Zhang nsends = redund->nsends; 211669db28dcSHong Zhang nrecvs = redund->nrecvs; 21171d79065fSBarry Smith send_rank = redund->send_rank; 21181d79065fSBarry Smith recv_rank = redund->recv_rank; 21191d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 21201d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 212169db28dcSHong Zhang sbuf_j = redund->sbuf_j; 212269db28dcSHong Zhang sbuf_a = redund->sbuf_a; 212369db28dcSHong Zhang rbuf_j = redund->rbuf_j; 212469db28dcSHong Zhang rbuf_a = redund->rbuf_a; 212569db28dcSHong Zhang } 212669db28dcSHong Zhang 212769db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 212869db28dcSHong Zhang PetscMPIInt subrank,subsize; 212969db28dcSHong Zhang PetscInt nleftover,np_subcomm; 213069db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 213169db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 213269db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 21331d79065fSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank); 213469db28dcSHong Zhang np_subcomm = size/nsubcomm; 213569db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 213669db28dcSHong Zhang nsends = 0; nrecvs = 0; 213769db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 213869db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 213969db28dcSHong Zhang send_rank[nsends] = i; nsends++; 214069db28dcSHong Zhang recv_rank[nrecvs++] = i; 214169db28dcSHong Zhang } 214269db28dcSHong Zhang } 214369db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 214469db28dcSHong Zhang i = size-nleftover-1; 214569db28dcSHong Zhang j = 0; 214669db28dcSHong Zhang while (j < nsubcomm - nleftover){ 214769db28dcSHong Zhang send_rank[nsends++] = i; 214869db28dcSHong Zhang i--; j++; 214969db28dcSHong Zhang } 215069db28dcSHong Zhang } 215169db28dcSHong Zhang 215269db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 215369db28dcSHong Zhang for (i=0; i<nleftover; i++){ 215469db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 215569db28dcSHong Zhang } 215669db28dcSHong Zhang } 215769db28dcSHong Zhang 215869db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 215969db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 216069db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 216169db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 216269db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 216369db28dcSHong Zhang 216469db28dcSHong Zhang /* copy mat's local entries into the buffers */ 216569db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 216669db28dcSHong Zhang rownz_max = 0; 216769db28dcSHong Zhang rptr = sbuf_j; 216869db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 216969db28dcSHong Zhang vals = sbuf_a; 217069db28dcSHong Zhang rptr[0] = 0; 217169db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 217269db28dcSHong Zhang row = i + rstart; 217369db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 217469db28dcSHong Zhang ncols = nzA + nzB; 217569db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 217669db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 217769db28dcSHong Zhang /* load the column indices for this row into cols */ 217869db28dcSHong Zhang lwrite = 0; 217969db28dcSHong Zhang for (l=0; l<nzB; l++) { 218069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 218169db28dcSHong Zhang vals[lwrite] = aworkB[l]; 218269db28dcSHong Zhang cols[lwrite++] = ctmp; 218369db28dcSHong Zhang } 218469db28dcSHong Zhang } 218569db28dcSHong Zhang for (l=0; l<nzA; l++){ 218669db28dcSHong Zhang vals[lwrite] = aworkA[l]; 218769db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 218869db28dcSHong Zhang } 218969db28dcSHong Zhang for (l=0; l<nzB; l++) { 219069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 219169db28dcSHong Zhang vals[lwrite] = aworkB[l]; 219269db28dcSHong Zhang cols[lwrite++] = ctmp; 219369db28dcSHong Zhang } 219469db28dcSHong Zhang } 219569db28dcSHong Zhang vals += ncols; 219669db28dcSHong Zhang cols += ncols; 219769db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 219869db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 219969db28dcSHong Zhang } 2200e32f2f54SBarry Smith if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz); 220169db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 220269db28dcSHong Zhang rptr = sbuf_j; 220369db28dcSHong Zhang vals = sbuf_a; 220469db28dcSHong Zhang rptr[0] = 0; 220569db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 220669db28dcSHong Zhang row = i + rstart; 220769db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 220869db28dcSHong Zhang ncols = nzA + nzB; 220969db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 221069db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 221169db28dcSHong Zhang lwrite = 0; 221269db28dcSHong Zhang for (l=0; l<nzB; l++) { 221369db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 221469db28dcSHong Zhang } 221569db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 221669db28dcSHong Zhang for (l=0; l<nzB; l++) { 221769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 221869db28dcSHong Zhang } 221969db28dcSHong Zhang vals += ncols; 222069db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 222169db28dcSHong Zhang } 222269db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 222369db28dcSHong Zhang 222469db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 222569db28dcSHong Zhang /*--------------------------------------------------*/ 222669db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 222769db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 222869db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 222969db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 223069db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 223169db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 223269db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 223369db28dcSHong Zhang } else { 223469db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 223569db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 223669db28dcSHong Zhang } 223769db28dcSHong Zhang 223869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 223969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 224069db28dcSHong Zhang /* get new tags to keep the communication clean */ 224169db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 224269db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 22431d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 224469db28dcSHong Zhang 224569db28dcSHong Zhang /* post receives of other's nzlocal */ 224669db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 224769db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 224869db28dcSHong Zhang } 224969db28dcSHong Zhang /* send nzlocal to others */ 225069db28dcSHong Zhang for (i=0; i<nsends; i++){ 225169db28dcSHong Zhang sbuf_nz[i] = nzlocal; 225269db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 225369db28dcSHong Zhang } 225469db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 225569db28dcSHong Zhang count = nrecvs; 225669db28dcSHong Zhang while (count) { 225769db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 225869db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 225969db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 226069db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 226169db28dcSHong Zhang 226269db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 226369db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 226469db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 226569db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 226669db28dcSHong Zhang count--; 226769db28dcSHong Zhang } 226869db28dcSHong Zhang /* wait on sends of nzlocal */ 226969db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 227069db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 227169db28dcSHong Zhang /*------------------------------------------------*/ 227269db28dcSHong Zhang for (i=0; i<nsends; i++){ 227369db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 227469db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 227569db28dcSHong Zhang } 227669db28dcSHong Zhang /* wait on receives of mat->i,j */ 227769db28dcSHong Zhang /*------------------------------*/ 227869db28dcSHong Zhang count = nrecvs; 227969db28dcSHong Zhang while (count) { 228069db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2281e32f2f54SBarry Smith if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 228269db28dcSHong Zhang count--; 228369db28dcSHong Zhang } 228469db28dcSHong Zhang /* wait on sends of mat->i,j */ 228569db28dcSHong Zhang /*---------------------------*/ 228669db28dcSHong Zhang if (nsends) { 228769db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 228869db28dcSHong Zhang } 228969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 229069db28dcSHong Zhang 229169db28dcSHong Zhang /* post receives, send and receive mat->a */ 229269db28dcSHong Zhang /*----------------------------------------*/ 229369db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 229469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 229569db28dcSHong Zhang } 229669db28dcSHong Zhang for (i=0; i<nsends; i++){ 229769db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 229869db28dcSHong Zhang } 229969db28dcSHong Zhang count = nrecvs; 230069db28dcSHong Zhang while (count) { 230169db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2302e32f2f54SBarry Smith if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 230369db28dcSHong Zhang count--; 230469db28dcSHong Zhang } 230569db28dcSHong Zhang if (nsends) { 230669db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 230769db28dcSHong Zhang } 230869db28dcSHong Zhang 230969db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 231069db28dcSHong Zhang 231169db28dcSHong Zhang /* create redundant matrix */ 231269db28dcSHong Zhang /*-------------------------*/ 231369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 231469db28dcSHong Zhang /* compute rownz_max for preallocation */ 231569db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 231669db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 231769db28dcSHong Zhang rptr = rbuf_j[imdex]; 231869db28dcSHong Zhang for (i=0; i<j; i++){ 231969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 232069db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 232169db28dcSHong Zhang } 232269db28dcSHong Zhang } 232369db28dcSHong Zhang 232469db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 232569db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 232669db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 232769db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 232869db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 232969db28dcSHong Zhang } else { 233069db28dcSHong Zhang C = *matredundant; 233169db28dcSHong Zhang } 233269db28dcSHong Zhang 233369db28dcSHong Zhang /* insert local matrix entries */ 233469db28dcSHong Zhang rptr = sbuf_j; 233569db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 233669db28dcSHong Zhang vals = sbuf_a; 233769db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 233869db28dcSHong Zhang row = i + rstart; 233969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 234069db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 234169db28dcSHong Zhang vals += ncols; 234269db28dcSHong Zhang cols += ncols; 234369db28dcSHong Zhang } 234469db28dcSHong Zhang /* insert received matrix entries */ 234569db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 234669db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 234769db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 234869db28dcSHong Zhang rptr = rbuf_j[imdex]; 234969db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 235069db28dcSHong Zhang vals = rbuf_a[imdex]; 235169db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 235269db28dcSHong Zhang row = i + rstart; 235369db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 235469db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 235569db28dcSHong Zhang vals += ncols; 235669db28dcSHong Zhang cols += ncols; 235769db28dcSHong Zhang } 235869db28dcSHong Zhang } 235969db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 236069db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 236169db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2362e32f2f54SBarry Smith if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"redundant mat size %d != input mat size %d",M,mat->rmap->N); 236369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 236469db28dcSHong Zhang PetscContainer container; 236569db28dcSHong Zhang *matredundant = C; 236669db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 236738f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 236869db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 236969db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 237069db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 237169db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 237269db28dcSHong Zhang 237369db28dcSHong Zhang redund->nzlocal = nzlocal; 237469db28dcSHong Zhang redund->nsends = nsends; 237569db28dcSHong Zhang redund->nrecvs = nrecvs; 237669db28dcSHong Zhang redund->send_rank = send_rank; 23771d79065fSBarry Smith redund->recv_rank = recv_rank; 237869db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 23791d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 238069db28dcSHong Zhang redund->sbuf_j = sbuf_j; 238169db28dcSHong Zhang redund->sbuf_a = sbuf_a; 238269db28dcSHong Zhang redund->rbuf_j = rbuf_j; 238369db28dcSHong Zhang redund->rbuf_a = rbuf_a; 238469db28dcSHong Zhang 238569db28dcSHong Zhang redund->MatDestroy = C->ops->destroy; 238669db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 238769db28dcSHong Zhang } 238869db28dcSHong Zhang PetscFunctionReturn(0); 238969db28dcSHong Zhang } 239069db28dcSHong Zhang 239103bc72f1SMatthew Knepley #undef __FUNCT__ 2392c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2393c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2394c91732d9SHong Zhang { 2395c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2396c91732d9SHong Zhang PetscErrorCode ierr; 2397c91732d9SHong Zhang PetscInt i,*idxb = 0; 2398c91732d9SHong Zhang PetscScalar *va,*vb; 2399c91732d9SHong Zhang Vec vtmp; 2400c91732d9SHong Zhang 2401c91732d9SHong Zhang PetscFunctionBegin; 2402c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2403c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2404c91732d9SHong Zhang if (idx) { 2405192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2406d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2407c91732d9SHong Zhang } 2408c91732d9SHong Zhang } 2409c91732d9SHong Zhang 2410d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2411c91732d9SHong Zhang if (idx) { 2412d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2413c91732d9SHong Zhang } 2414c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2415c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2416c91732d9SHong Zhang 2417d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2418c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2419c91732d9SHong Zhang va[i] = vb[i]; 2420c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2421c91732d9SHong Zhang } 2422c91732d9SHong Zhang } 2423c91732d9SHong Zhang 2424c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2425c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2426c91732d9SHong Zhang if (idxb) { 2427c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 2428c91732d9SHong Zhang } 2429c91732d9SHong Zhang ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2430c91732d9SHong Zhang PetscFunctionReturn(0); 2431c91732d9SHong Zhang } 2432c91732d9SHong Zhang 2433c91732d9SHong Zhang #undef __FUNCT__ 2434c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2435c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2436c87e5d42SMatthew Knepley { 2437c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2438c87e5d42SMatthew Knepley PetscErrorCode ierr; 2439c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2440c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2441c87e5d42SMatthew Knepley Vec vtmp; 2442c87e5d42SMatthew Knepley 2443c87e5d42SMatthew Knepley PetscFunctionBegin; 2444c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2445c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2446c87e5d42SMatthew Knepley if (idx) { 2447c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2448c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2449c87e5d42SMatthew Knepley } 2450c87e5d42SMatthew Knepley } 2451c87e5d42SMatthew Knepley 2452c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2453c87e5d42SMatthew Knepley if (idx) { 2454c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2455c87e5d42SMatthew Knepley } 2456c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2457c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2458c87e5d42SMatthew Knepley 2459c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2460c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2461c87e5d42SMatthew Knepley va[i] = vb[i]; 2462c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2463c87e5d42SMatthew Knepley } 2464c87e5d42SMatthew Knepley } 2465c87e5d42SMatthew Knepley 2466c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2467c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2468c87e5d42SMatthew Knepley if (idxb) { 2469c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 2470c87e5d42SMatthew Knepley } 2471c87e5d42SMatthew Knepley ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2472c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2473c87e5d42SMatthew Knepley } 2474c87e5d42SMatthew Knepley 2475c87e5d42SMatthew Knepley #undef __FUNCT__ 247603bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 247703bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 247803bc72f1SMatthew Knepley { 247903bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2480d0f46423SBarry Smith PetscInt n = A->rmap->n; 2481d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 248203bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 248303bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 248403bc72f1SMatthew Knepley Vec diagV, offdiagV; 248503bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 248603bc72f1SMatthew Knepley PetscInt r; 248703bc72f1SMatthew Knepley PetscErrorCode ierr; 248803bc72f1SMatthew Knepley 248903bc72f1SMatthew Knepley PetscFunctionBegin; 249003bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2491e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2492e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 249303bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 249403bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 249503bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 249603bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 249703bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 249803bc72f1SMatthew Knepley for(r = 0; r < n; ++r) { 2499028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 250003bc72f1SMatthew Knepley a[r] = diagA[r]; 250103bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 250203bc72f1SMatthew Knepley } else { 250303bc72f1SMatthew Knepley a[r] = offdiagA[r]; 250403bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 250503bc72f1SMatthew Knepley } 250603bc72f1SMatthew Knepley } 250703bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 250803bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 250903bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 251003bc72f1SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 251103bc72f1SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 251203bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 251303bc72f1SMatthew Knepley PetscFunctionReturn(0); 251403bc72f1SMatthew Knepley } 251503bc72f1SMatthew Knepley 25165494a064SHong Zhang #undef __FUNCT__ 2517c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2518c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2519c87e5d42SMatthew Knepley { 2520c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2521c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2522c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2523c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2524c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2525c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2526c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2527c87e5d42SMatthew Knepley PetscInt r; 2528c87e5d42SMatthew Knepley PetscErrorCode ierr; 2529c87e5d42SMatthew Knepley 2530c87e5d42SMatthew Knepley PetscFunctionBegin; 2531c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2532c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2533c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 2534c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2535c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2536c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2537c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2538c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2539c87e5d42SMatthew Knepley for(r = 0; r < n; ++r) { 2540c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2541c87e5d42SMatthew Knepley a[r] = diagA[r]; 2542c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2543c87e5d42SMatthew Knepley } else { 2544c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2545c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2546c87e5d42SMatthew Knepley } 2547c87e5d42SMatthew Knepley } 2548c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2549c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2550c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2551c87e5d42SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 2552c87e5d42SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 2553c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 2554c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2555c87e5d42SMatthew Knepley } 2556c87e5d42SMatthew Knepley 2557c87e5d42SMatthew Knepley #undef __FUNCT__ 2558829201f2SHong Zhang #define __FUNCT__ "MatGetSeqNonzerostructure_MPIAIJ" 2559f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIAIJ(Mat mat,Mat *newmat) 25605494a064SHong Zhang { 25615494a064SHong Zhang PetscErrorCode ierr; 2562f6d58c54SBarry Smith Mat *dummy; 25635494a064SHong Zhang 25645494a064SHong Zhang PetscFunctionBegin; 2565f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 2566f6d58c54SBarry Smith *newmat = *dummy; 2567f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 25685494a064SHong Zhang PetscFunctionReturn(0); 25695494a064SHong Zhang } 25705494a064SHong Zhang 25713acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 25728a729477SBarry Smith /* -------------------------------------------------------------------*/ 2573cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 2574cda55fadSBarry Smith MatGetRow_MPIAIJ, 2575cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 2576cda55fadSBarry Smith MatMult_MPIAIJ, 257797304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 25787c922b88SBarry Smith MatMultTranspose_MPIAIJ, 25797c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 2580103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2581103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 2582103bf8bdSMatthew Knepley #else 2583cda55fadSBarry Smith 0, 2584103bf8bdSMatthew Knepley #endif 2585cda55fadSBarry Smith 0, 2586cda55fadSBarry Smith 0, 258797304618SKris Buschelman /*10*/ 0, 2588cda55fadSBarry Smith 0, 2589cda55fadSBarry Smith 0, 259041f059aeSBarry Smith MatSOR_MPIAIJ, 2591b7c46309SBarry Smith MatTranspose_MPIAIJ, 259297304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 2593cda55fadSBarry Smith MatEqual_MPIAIJ, 2594cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 2595cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 2596cda55fadSBarry Smith MatNorm_MPIAIJ, 259797304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 2598cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 2599cda55fadSBarry Smith MatSetOption_MPIAIJ, 2600cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 2601d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 2602cda55fadSBarry Smith 0, 2603103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2604719d5645SBarry Smith 0, 2605103bf8bdSMatthew Knepley #else 2606cda55fadSBarry Smith 0, 2607103bf8bdSMatthew Knepley #endif 2608cda55fadSBarry Smith 0, 2609cda55fadSBarry Smith 0, 2610d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIAIJ, 2611103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2612719d5645SBarry Smith 0, 2613103bf8bdSMatthew Knepley #else 2614cda55fadSBarry Smith 0, 2615103bf8bdSMatthew Knepley #endif 2616cda55fadSBarry Smith 0, 2617cda55fadSBarry Smith 0, 2618cda55fadSBarry Smith 0, 2619d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 2620cda55fadSBarry Smith 0, 2621cda55fadSBarry Smith 0, 2622cda55fadSBarry Smith 0, 2623cda55fadSBarry Smith 0, 2624d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 2625cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 2626cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 2627cda55fadSBarry Smith MatGetValues_MPIAIJ, 2628cb5b572fSBarry Smith MatCopy_MPIAIJ, 2629d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 2630cda55fadSBarry Smith MatScale_MPIAIJ, 2631cda55fadSBarry Smith 0, 2632cda55fadSBarry Smith 0, 2633cda55fadSBarry Smith 0, 2634d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_MPIAIJ, 2635cda55fadSBarry Smith 0, 2636cda55fadSBarry Smith 0, 2637cda55fadSBarry Smith 0, 2638cda55fadSBarry Smith 0, 2639d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 2640cda55fadSBarry Smith 0, 2641cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 264242e855d1Svictor MatPermute_MPIAIJ, 2643cda55fadSBarry Smith 0, 2644d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 2645e03a110bSBarry Smith MatDestroy_MPIAIJ, 2646e03a110bSBarry Smith MatView_MPIAIJ, 2647357abbc8SBarry Smith 0, 2648a2243be0SBarry Smith 0, 2649d519adbfSMatthew Knepley /*64*/ 0, 2650a2243be0SBarry Smith 0, 2651a2243be0SBarry Smith 0, 2652a2243be0SBarry Smith 0, 2653a2243be0SBarry Smith 0, 2654d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 2655c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 2656a2243be0SBarry Smith 0, 2657a2243be0SBarry Smith MatSetColoring_MPIAIJ, 2658dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2659779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 2660dcf5cc72SBarry Smith #else 2661dcf5cc72SBarry Smith 0, 2662dcf5cc72SBarry Smith #endif 266397304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 26643acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 266597304618SKris Buschelman 0, 266697304618SKris Buschelman 0, 266797304618SKris Buschelman 0, 266897304618SKris Buschelman 0, 266997304618SKris Buschelman /*80*/ 0, 267097304618SKris Buschelman 0, 267197304618SKris Buschelman 0, 2672d519adbfSMatthew Knepley /*83*/ MatLoad_MPIAIJ, 26736284ec50SHong Zhang 0, 26746284ec50SHong Zhang 0, 26756284ec50SHong Zhang 0, 26766284ec50SHong Zhang 0, 2677865e5f61SKris Buschelman 0, 2678d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 267926be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 268026be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 26817a7894deSKris Buschelman MatPtAP_Basic, 26827a7894deSKris Buschelman MatPtAPSymbolic_MPIAIJ, 2683d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ, 26847a7894deSKris Buschelman 0, 26857a7894deSKris Buschelman 0, 26867a7894deSKris Buschelman 0, 26877a7894deSKris Buschelman 0, 2688d519adbfSMatthew Knepley /*99*/ 0, 2689865e5f61SKris Buschelman MatPtAPSymbolic_MPIAIJ_MPIAIJ, 26907a7894deSKris Buschelman MatPtAPNumeric_MPIAIJ_MPIAIJ, 26912fd7e33dSBarry Smith MatConjugate_MPIAIJ, 26922fd7e33dSBarry Smith 0, 2693d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 269499cafbc1SBarry Smith MatRealPart_MPIAIJ, 269569db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 269669db28dcSHong Zhang 0, 269769db28dcSHong Zhang 0, 2698d519adbfSMatthew Knepley /*109*/0, 269903bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 27005494a064SHong Zhang MatGetRowMin_MPIAIJ, 27015494a064SHong Zhang 0, 27025494a064SHong Zhang 0, 2703bd0c2dcbSBarry Smith /*114*/MatGetSeqNonzerostructure_MPIAIJ, 2704bd0c2dcbSBarry Smith 0, 2705bd0c2dcbSBarry Smith 0, 2706bd0c2dcbSBarry Smith 0, 2707bd0c2dcbSBarry Smith 0, 2708*8fb81238SShri Abhyankar /*119*/0, 2709*8fb81238SShri Abhyankar 0, 2710*8fb81238SShri Abhyankar 0, 2711*8fb81238SShri Abhyankar 0, 2712*8fb81238SShri Abhyankar MatLoadnew_MPIAIJ 2713bd0c2dcbSBarry Smith }; 271436ce4990SBarry Smith 27152e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 27162e8a6d31SBarry Smith 2717fb2e594dSBarry Smith EXTERN_C_BEGIN 27184a2ae208SSatish Balay #undef __FUNCT__ 27194a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 2720be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat) 27212e8a6d31SBarry Smith { 27222e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2723dfbe8321SBarry Smith PetscErrorCode ierr; 27242e8a6d31SBarry Smith 27252e8a6d31SBarry Smith PetscFunctionBegin; 27262e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 27272e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 27282e8a6d31SBarry Smith PetscFunctionReturn(0); 27292e8a6d31SBarry Smith } 2730fb2e594dSBarry Smith EXTERN_C_END 27312e8a6d31SBarry Smith 2732fb2e594dSBarry Smith EXTERN_C_BEGIN 27334a2ae208SSatish Balay #undef __FUNCT__ 27344a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 2735be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat) 27362e8a6d31SBarry Smith { 27372e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2738dfbe8321SBarry Smith PetscErrorCode ierr; 27392e8a6d31SBarry Smith 27402e8a6d31SBarry Smith PetscFunctionBegin; 27412e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 27422e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 27432e8a6d31SBarry Smith PetscFunctionReturn(0); 27442e8a6d31SBarry Smith } 2745fb2e594dSBarry Smith EXTERN_C_END 27468a729477SBarry Smith 2747e090d566SSatish Balay #include "petscpc.h" 274827508adbSBarry Smith EXTERN_C_BEGIN 27494a2ae208SSatish Balay #undef __FUNCT__ 2750a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 2751be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 2752a23d5eceSKris Buschelman { 2753a23d5eceSKris Buschelman Mat_MPIAIJ *b; 2754dfbe8321SBarry Smith PetscErrorCode ierr; 2755b1d57f15SBarry Smith PetscInt i; 2756a23d5eceSKris Buschelman 2757a23d5eceSKris Buschelman PetscFunctionBegin; 2758a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 2759a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 2760e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 2761e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 2762899cda47SBarry Smith 276326283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 276426283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 276526283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 276626283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 2767a23d5eceSKris Buschelman if (d_nnz) { 2768d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 2769e32f2f54SBarry Smith if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]); 2770a23d5eceSKris Buschelman } 2771a23d5eceSKris Buschelman } 2772a23d5eceSKris Buschelman if (o_nnz) { 2773d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 2774e32f2f54SBarry Smith if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]); 2775a23d5eceSKris Buschelman } 2776a23d5eceSKris Buschelman } 2777a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 2778899cda47SBarry Smith 2779526dfc15SBarry Smith if (!B->preallocated) { 2780899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 2781899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 2782d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 2783899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 2784899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 2785899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 2786d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 2787899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 2788899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 2789526dfc15SBarry Smith } 2790899cda47SBarry Smith 2791c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 2792c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 2793526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 2794a23d5eceSKris Buschelman PetscFunctionReturn(0); 2795a23d5eceSKris Buschelman } 2796a23d5eceSKris Buschelman EXTERN_C_END 2797a23d5eceSKris Buschelman 27984a2ae208SSatish Balay #undef __FUNCT__ 27994a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 2800dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 2801d6dfbf8fSBarry Smith { 2802d6dfbf8fSBarry Smith Mat mat; 2803416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 2804dfbe8321SBarry Smith PetscErrorCode ierr; 2805d6dfbf8fSBarry Smith 28063a40ed3dSBarry Smith PetscFunctionBegin; 2807416022c9SBarry Smith *newmat = 0; 28087adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 2809d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 28107adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 28111d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 2812273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 2813e1b6402fSHong Zhang 2814d5f3da31SBarry Smith mat->factortype = matin->factortype; 2815d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 2816c456f294SBarry Smith mat->assembled = PETSC_TRUE; 2817e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 2818273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 2819d6dfbf8fSBarry Smith 282017699dbbSLois Curfman McInnes a->size = oldmat->size; 282117699dbbSLois Curfman McInnes a->rank = oldmat->rank; 2822e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 2823e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 2824e7641de0SSatish Balay a->rowindices = 0; 2825bcd2baecSBarry Smith a->rowvalues = 0; 2826bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 2827d6dfbf8fSBarry Smith 282826283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 282926283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 2830899cda47SBarry Smith 28312ee70a88SLois Curfman McInnes if (oldmat->colmap) { 2832aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 28330f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 2834b1fc9764SSatish Balay #else 2835d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 2836d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 2837d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 2838b1fc9764SSatish Balay #endif 2839416022c9SBarry Smith } else a->colmap = 0; 28403f41c07dSBarry Smith if (oldmat->garray) { 2841b1d57f15SBarry Smith PetscInt len; 2842d0f46423SBarry Smith len = oldmat->B->cmap->n; 2843b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 284452e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 2845b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 2846416022c9SBarry Smith } else a->garray = 0; 2847d6dfbf8fSBarry Smith 2848416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 284952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 2850a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 285152e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 28522e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 285352e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 28542e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 285552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 28567adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 28578a729477SBarry Smith *newmat = mat; 28583a40ed3dSBarry Smith PetscFunctionReturn(0); 28598a729477SBarry Smith } 2860416022c9SBarry Smith 28614a2ae208SSatish Balay #undef __FUNCT__ 28624a2ae208SSatish Balay #define __FUNCT__ "MatLoad_MPIAIJ" 2863a313700dSBarry Smith PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, const MatType type,Mat *newmat) 2864416022c9SBarry Smith { 2865d65a2f8fSBarry Smith Mat A; 286687828ca2SBarry Smith PetscScalar *vals,*svals; 286719bcc07fSBarry Smith MPI_Comm comm = ((PetscObject)viewer)->comm; 2868416022c9SBarry Smith MPI_Status status; 28696849ba73SBarry Smith PetscErrorCode ierr; 287013980483SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag,mpicnt,mpimaxnz; 28717e042019SMatthew Knepley PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0; 2872b1d57f15SBarry Smith PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 2873910ba992SMatthew Knepley PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 2874dc231df0SBarry Smith PetscInt cend,cstart,n,*rowners; 2875b1d57f15SBarry Smith int fd; 2876416022c9SBarry Smith 28773a40ed3dSBarry Smith PetscFunctionBegin; 28781dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 28791dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 288017699dbbSLois Curfman McInnes if (!rank) { 2881b0a32e0cSBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 28820752156aSBarry Smith ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 2883e32f2f54SBarry Smith if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 28846c5fab8fSBarry Smith } 28856c5fab8fSBarry Smith 2886b1d57f15SBarry Smith ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 2887416022c9SBarry Smith M = header[1]; N = header[2]; 2888416022c9SBarry Smith /* determine ownership of all rows */ 288929cdbbc8SSatish Balay m = M/size + ((M % size) > rank); 2890dc231df0SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 2891dc231df0SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 2892167e7480SBarry Smith 2893167e7480SBarry Smith /* First process needs enough room for process with most rows */ 2894167e7480SBarry Smith if (!rank) { 2895167e7480SBarry Smith mmax = rowners[1]; 2896167e7480SBarry Smith for (i=2; i<size; i++) { 2897167e7480SBarry Smith mmax = PetscMax(mmax,rowners[i]); 2898167e7480SBarry Smith } 2899167e7480SBarry Smith } else mmax = m; 2900167e7480SBarry Smith 2901416022c9SBarry Smith rowners[0] = 0; 290217699dbbSLois Curfman McInnes for (i=2; i<=size; i++) { 2903416022c9SBarry Smith rowners[i] += rowners[i-1]; 2904416022c9SBarry Smith } 290517699dbbSLois Curfman McInnes rstart = rowners[rank]; 290617699dbbSLois Curfman McInnes rend = rowners[rank+1]; 2907416022c9SBarry Smith 2908416022c9SBarry Smith /* distribute row lengths to all processors */ 2909167e7480SBarry Smith ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 291017699dbbSLois Curfman McInnes if (!rank) { 2911dc231df0SBarry Smith ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 2912dc231df0SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 2913b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 2914b1d57f15SBarry Smith ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 2915dc231df0SBarry Smith for (j=0; j<m; j++) { 2916dc231df0SBarry Smith procsnz[0] += ourlens[j]; 2917dc231df0SBarry Smith } 2918dc231df0SBarry Smith for (i=1; i<size; i++) { 2919dc231df0SBarry Smith ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 2920dc231df0SBarry Smith /* calculate the number of nonzeros on each processor */ 2921dc231df0SBarry Smith for (j=0; j<rowners[i+1]-rowners[i]; j++) { 2922416022c9SBarry Smith procsnz[i] += rowlengths[j]; 2923416022c9SBarry Smith } 292413980483SBarry Smith mpicnt = PetscMPIIntCast(rowners[i+1]-rowners[i]); 292513980483SBarry Smith ierr = MPI_Send(rowlengths,mpicnt,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 2926416022c9SBarry Smith } 2927606d414cSSatish Balay ierr = PetscFree(rowlengths);CHKERRQ(ierr); 2928dc231df0SBarry Smith } else { 292913980483SBarry Smith mpicnt = PetscMPIIntCast(m);CHKERRQ(ierr); 293013980483SBarry Smith ierr = MPI_Recv(ourlens,mpicnt,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 2931dc231df0SBarry Smith } 2932416022c9SBarry Smith 2933dc231df0SBarry Smith if (!rank) { 2934416022c9SBarry Smith /* determine max buffer needed and allocate it */ 2935416022c9SBarry Smith maxnz = 0; 29368a8e0b3aSBarry Smith for (i=0; i<size; i++) { 29370452661fSBarry Smith maxnz = PetscMax(maxnz,procsnz[i]); 2938416022c9SBarry Smith } 2939b1d57f15SBarry Smith ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 2940416022c9SBarry Smith 2941416022c9SBarry Smith /* read in my part of the matrix column indices */ 2942416022c9SBarry Smith nz = procsnz[0]; 2943b1d57f15SBarry Smith ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 29440752156aSBarry Smith ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 2945d65a2f8fSBarry Smith 2946d65a2f8fSBarry Smith /* read in every one elses and ship off */ 294717699dbbSLois Curfman McInnes for (i=1; i<size; i++) { 2948d65a2f8fSBarry Smith nz = procsnz[i]; 29490752156aSBarry Smith ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 295013980483SBarry Smith mpicnt = PetscMPIIntCast(nz); 295113980483SBarry Smith ierr = MPI_Send(cols,mpicnt,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 2952d65a2f8fSBarry Smith } 2953606d414cSSatish Balay ierr = PetscFree(cols);CHKERRQ(ierr); 29543a40ed3dSBarry Smith } else { 2955416022c9SBarry Smith /* determine buffer space needed for message */ 2956416022c9SBarry Smith nz = 0; 2957416022c9SBarry Smith for (i=0; i<m; i++) { 2958416022c9SBarry Smith nz += ourlens[i]; 2959416022c9SBarry Smith } 2960dc231df0SBarry Smith ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 2961416022c9SBarry Smith 2962416022c9SBarry Smith /* receive message of column indices*/ 296313980483SBarry Smith mpicnt = PetscMPIIntCast(nz);CHKERRQ(ierr); 296413980483SBarry Smith ierr = MPI_Recv(mycols,mpicnt,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 296513980483SBarry Smith ierr = MPI_Get_count(&status,MPIU_INT,&mpimaxnz);CHKERRQ(ierr); 2966cb9801acSJed Brown if (mpimaxnz == MPI_UNDEFINED) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned MPI_UNDEFINED, expected %d",mpicnt); 2967cb9801acSJed Brown else if (mpimaxnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned impossible negative value %d, expected %d",mpimaxnz,mpicnt); 2968cb9801acSJed Brown else if (mpimaxnz != mpicnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file: expected %d received %d",mpicnt,mpimaxnz); 2969416022c9SBarry Smith } 2970416022c9SBarry Smith 2971b362ba68SBarry Smith /* determine column ownership if matrix is not square */ 2972b362ba68SBarry Smith if (N != M) { 2973b362ba68SBarry Smith n = N/size + ((N % size) > rank); 2974b1d57f15SBarry Smith ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 2975b362ba68SBarry Smith cstart = cend - n; 2976b362ba68SBarry Smith } else { 2977b362ba68SBarry Smith cstart = rstart; 2978b362ba68SBarry Smith cend = rend; 2979fb2e594dSBarry Smith n = cend - cstart; 2980b362ba68SBarry Smith } 2981b362ba68SBarry Smith 2982416022c9SBarry Smith /* loop over local rows, determining number of off diagonal entries */ 2983b1d57f15SBarry Smith ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 2984416022c9SBarry Smith jj = 0; 2985416022c9SBarry Smith for (i=0; i<m; i++) { 2986416022c9SBarry Smith for (j=0; j<ourlens[i]; j++) { 2987b362ba68SBarry Smith if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 2988416022c9SBarry Smith jj++; 2989416022c9SBarry Smith } 2990416022c9SBarry Smith } 2991d65a2f8fSBarry Smith 2992d65a2f8fSBarry Smith /* create our matrix */ 2993416022c9SBarry Smith for (i=0; i<m; i++) { 2994416022c9SBarry Smith ourlens[i] -= offlens[i]; 2995416022c9SBarry Smith } 2996f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&A);CHKERRQ(ierr); 2997f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,m,n,M,N);CHKERRQ(ierr); 2998d10c748bSKris Buschelman ierr = MatSetType(A,type);CHKERRQ(ierr); 2999d10c748bSKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);CHKERRQ(ierr); 3000d10c748bSKris Buschelman 3001d65a2f8fSBarry Smith for (i=0; i<m; i++) { 3002d65a2f8fSBarry Smith ourlens[i] += offlens[i]; 3003d65a2f8fSBarry Smith } 3004416022c9SBarry Smith 300517699dbbSLois Curfman McInnes if (!rank) { 3006906b51c7SHong Zhang ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 3007416022c9SBarry Smith 3008416022c9SBarry Smith /* read in my part of the matrix numerical values */ 3009416022c9SBarry Smith nz = procsnz[0]; 30100752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3011d65a2f8fSBarry Smith 3012d65a2f8fSBarry Smith /* insert into matrix */ 3013d65a2f8fSBarry Smith jj = rstart; 3014d65a2f8fSBarry Smith smycols = mycols; 3015d65a2f8fSBarry Smith svals = vals; 3016d65a2f8fSBarry Smith for (i=0; i<m; i++) { 3017dc231df0SBarry Smith ierr = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 3018d65a2f8fSBarry Smith smycols += ourlens[i]; 3019d65a2f8fSBarry Smith svals += ourlens[i]; 3020d65a2f8fSBarry Smith jj++; 3021416022c9SBarry Smith } 3022416022c9SBarry Smith 3023d65a2f8fSBarry Smith /* read in other processors and ship out */ 302417699dbbSLois Curfman McInnes for (i=1; i<size; i++) { 3025416022c9SBarry Smith nz = procsnz[i]; 30260752156aSBarry Smith ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 302713980483SBarry Smith mpicnt = PetscMPIIntCast(nz); 302813980483SBarry Smith ierr = MPI_Send(vals,mpicnt,MPIU_SCALAR,i,((PetscObject)A)->tag,comm);CHKERRQ(ierr); 3029416022c9SBarry Smith } 3030606d414cSSatish Balay ierr = PetscFree(procsnz);CHKERRQ(ierr); 30313a40ed3dSBarry Smith } else { 3032d65a2f8fSBarry Smith /* receive numeric values */ 303387828ca2SBarry Smith ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 3034416022c9SBarry Smith 3035d65a2f8fSBarry Smith /* receive message of values*/ 303613980483SBarry Smith mpicnt = PetscMPIIntCast(nz); 303713980483SBarry Smith ierr = MPI_Recv(vals,mpicnt,MPIU_SCALAR,0,((PetscObject)A)->tag,comm,&status);CHKERRQ(ierr); 303813980483SBarry Smith ierr = MPI_Get_count(&status,MPIU_SCALAR,&mpimaxnz);CHKERRQ(ierr); 3039cb9801acSJed Brown if (mpimaxnz == MPI_UNDEFINED) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned MPI_UNDEFINED, expected %d",mpicnt); 3040cb9801acSJed Brown else if (mpimaxnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned impossible negative value %d, expected %d",mpimaxnz,mpicnt); 3041cb9801acSJed Brown else if (mpimaxnz != mpicnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file: expected %d received %d",mpicnt,mpimaxnz); 3042cb9801acSJed Brown 3043d65a2f8fSBarry Smith /* insert into matrix */ 3044d65a2f8fSBarry Smith jj = rstart; 3045d65a2f8fSBarry Smith smycols = mycols; 3046d65a2f8fSBarry Smith svals = vals; 3047d65a2f8fSBarry Smith for (i=0; i<m; i++) { 3048dc231df0SBarry Smith ierr = MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 3049d65a2f8fSBarry Smith smycols += ourlens[i]; 3050d65a2f8fSBarry Smith svals += ourlens[i]; 3051d65a2f8fSBarry Smith jj++; 3052d65a2f8fSBarry Smith } 3053d65a2f8fSBarry Smith } 3054dc231df0SBarry Smith ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 3055606d414cSSatish Balay ierr = PetscFree(vals);CHKERRQ(ierr); 3056606d414cSSatish Balay ierr = PetscFree(mycols);CHKERRQ(ierr); 3057606d414cSSatish Balay ierr = PetscFree(rowners);CHKERRQ(ierr); 3058d65a2f8fSBarry Smith 30596d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 30606d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3061d10c748bSKris Buschelman *newmat = A; 30623a40ed3dSBarry Smith PetscFunctionReturn(0); 3063416022c9SBarry Smith } 3064a0ff6018SBarry Smith 30654a2ae208SSatish Balay #undef __FUNCT__ 3066*8fb81238SShri Abhyankar #define __FUNCT__ "MatLoadnew_MPIAIJ" 3067*8fb81238SShri Abhyankar PetscErrorCode MatLoadnew_MPIAIJ(PetscViewer viewer, Mat newMat) 3068*8fb81238SShri Abhyankar { 3069*8fb81238SShri Abhyankar PetscScalar *vals,*svals; 3070*8fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 3071*8fb81238SShri Abhyankar MPI_Status status; 3072*8fb81238SShri Abhyankar PetscErrorCode ierr; 3073*8fb81238SShri Abhyankar PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag,mpicnt,mpimaxnz; 3074*8fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 3075*8fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 3076*8fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 3077*8fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 3078*8fb81238SShri Abhyankar int fd; 3079*8fb81238SShri Abhyankar 3080*8fb81238SShri Abhyankar PetscFunctionBegin; 3081*8fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3082*8fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 3083*8fb81238SShri Abhyankar if (!rank) { 3084*8fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 3085*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 3086*8fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 3087*8fb81238SShri Abhyankar } 3088*8fb81238SShri Abhyankar 3089*8fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 3090*8fb81238SShri Abhyankar 3091*8fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 3092*8fb81238SShri Abhyankar M = header[1]; N = header[2]; 3093*8fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 3094*8fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 3095*8fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 3096*8fb81238SShri Abhyankar 3097*8fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 3098*8fb81238SShri Abhyankar if (sizesset) { 3099*8fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 3100*8fb81238SShri Abhyankar } 3101*8fb81238SShri Abhyankar if (sizesset && newMat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Incostintent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows); 3102*8fb81238SShri Abhyankar if (sizesset && newMat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Incostintent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols); 3103*8fb81238SShri Abhyankar 3104*8fb81238SShri Abhyankar /* determine ownership of all rows */ 3105*8fb81238SShri Abhyankar if (newMat->rmap->n < 0 ) m = M/size + ((M % size) > rank); /* PETSC_DECIDE */ 3106*8fb81238SShri Abhyankar else m = newMat->rmap->n; 3107*8fb81238SShri Abhyankar 3108*8fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 3109*8fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 3110*8fb81238SShri Abhyankar 3111*8fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 3112*8fb81238SShri Abhyankar if (!rank) { 3113*8fb81238SShri Abhyankar mmax = rowners[1]; 3114*8fb81238SShri Abhyankar for (i=2; i<size; i++) { 3115*8fb81238SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 3116*8fb81238SShri Abhyankar } 3117*8fb81238SShri Abhyankar } else mmax = m; 3118*8fb81238SShri Abhyankar 3119*8fb81238SShri Abhyankar rowners[0] = 0; 3120*8fb81238SShri Abhyankar for (i=2; i<=size; i++) { 3121*8fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 3122*8fb81238SShri Abhyankar } 3123*8fb81238SShri Abhyankar rstart = rowners[rank]; 3124*8fb81238SShri Abhyankar rend = rowners[rank+1]; 3125*8fb81238SShri Abhyankar 3126*8fb81238SShri Abhyankar /* distribute row lengths to all processors */ 3127*8fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 3128*8fb81238SShri Abhyankar if (!rank) { 3129*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 3130*8fb81238SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 3131*8fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 3132*8fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 3133*8fb81238SShri Abhyankar for (j=0; j<m; j++) { 3134*8fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 3135*8fb81238SShri Abhyankar } 3136*8fb81238SShri Abhyankar for (i=1; i<size; i++) { 3137*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 3138*8fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 3139*8fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 3140*8fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 3141*8fb81238SShri Abhyankar } 3142*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(rowners[i+1]-rowners[i]); 3143*8fb81238SShri Abhyankar ierr = MPI_Send(rowlengths,mpicnt,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 3144*8fb81238SShri Abhyankar } 3145*8fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 3146*8fb81238SShri Abhyankar } else { 3147*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(m);CHKERRQ(ierr); 3148*8fb81238SShri Abhyankar ierr = MPI_Recv(ourlens,mpicnt,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 3149*8fb81238SShri Abhyankar } 3150*8fb81238SShri Abhyankar 3151*8fb81238SShri Abhyankar if (!rank) { 3152*8fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 3153*8fb81238SShri Abhyankar maxnz = 0; 3154*8fb81238SShri Abhyankar for (i=0; i<size; i++) { 3155*8fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 3156*8fb81238SShri Abhyankar } 3157*8fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 3158*8fb81238SShri Abhyankar 3159*8fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 3160*8fb81238SShri Abhyankar nz = procsnz[0]; 3161*8fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 3162*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 3163*8fb81238SShri Abhyankar 3164*8fb81238SShri Abhyankar /* read in every one elses and ship off */ 3165*8fb81238SShri Abhyankar for (i=1; i<size; i++) { 3166*8fb81238SShri Abhyankar nz = procsnz[i]; 3167*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 3168*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(nz); 3169*8fb81238SShri Abhyankar ierr = MPI_Send(cols,mpicnt,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 3170*8fb81238SShri Abhyankar } 3171*8fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 3172*8fb81238SShri Abhyankar } else { 3173*8fb81238SShri Abhyankar /* determine buffer space needed for message */ 3174*8fb81238SShri Abhyankar nz = 0; 3175*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3176*8fb81238SShri Abhyankar nz += ourlens[i]; 3177*8fb81238SShri Abhyankar } 3178*8fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 3179*8fb81238SShri Abhyankar 3180*8fb81238SShri Abhyankar /* receive message of column indices*/ 3181*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(nz);CHKERRQ(ierr); 3182*8fb81238SShri Abhyankar ierr = MPI_Recv(mycols,mpicnt,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 3183*8fb81238SShri Abhyankar ierr = MPI_Get_count(&status,MPIU_INT,&mpimaxnz);CHKERRQ(ierr); 3184*8fb81238SShri Abhyankar if (mpimaxnz == MPI_UNDEFINED) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned MPI_UNDEFINED, expected %d",mpicnt); 3185*8fb81238SShri Abhyankar else if (mpimaxnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned impossible negative value %d, expected %d",mpimaxnz,mpicnt); 3186*8fb81238SShri Abhyankar else if (mpimaxnz != mpicnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file: expected %d received %d",mpicnt,mpimaxnz); 3187*8fb81238SShri Abhyankar } 3188*8fb81238SShri Abhyankar 3189*8fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 3190*8fb81238SShri Abhyankar if (N != M) { 3191*8fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 3192*8fb81238SShri Abhyankar else n = newMat->cmap->n; 3193*8fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 3194*8fb81238SShri Abhyankar cstart = cend - n; 3195*8fb81238SShri Abhyankar } else { 3196*8fb81238SShri Abhyankar cstart = rstart; 3197*8fb81238SShri Abhyankar cend = rend; 3198*8fb81238SShri Abhyankar n = cend - cstart; 3199*8fb81238SShri Abhyankar } 3200*8fb81238SShri Abhyankar 3201*8fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 3202*8fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 3203*8fb81238SShri Abhyankar jj = 0; 3204*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3205*8fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 3206*8fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 3207*8fb81238SShri Abhyankar jj++; 3208*8fb81238SShri Abhyankar } 3209*8fb81238SShri Abhyankar } 3210*8fb81238SShri Abhyankar 3211*8fb81238SShri Abhyankar /* create our matrix */ 3212*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3213*8fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 3214*8fb81238SShri Abhyankar } 3215*8fb81238SShri Abhyankar if (!sizesset) { 3216*8fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 3217*8fb81238SShri Abhyankar } 3218*8fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 3219*8fb81238SShri Abhyankar 3220*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3221*8fb81238SShri Abhyankar ourlens[i] += offlens[i]; 3222*8fb81238SShri Abhyankar } 3223*8fb81238SShri Abhyankar 3224*8fb81238SShri Abhyankar if (!rank) { 3225*8fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 3226*8fb81238SShri Abhyankar 3227*8fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 3228*8fb81238SShri Abhyankar nz = procsnz[0]; 3229*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3230*8fb81238SShri Abhyankar 3231*8fb81238SShri Abhyankar /* insert into matrix */ 3232*8fb81238SShri Abhyankar jj = rstart; 3233*8fb81238SShri Abhyankar smycols = mycols; 3234*8fb81238SShri Abhyankar svals = vals; 3235*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3236*8fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 3237*8fb81238SShri Abhyankar smycols += ourlens[i]; 3238*8fb81238SShri Abhyankar svals += ourlens[i]; 3239*8fb81238SShri Abhyankar jj++; 3240*8fb81238SShri Abhyankar } 3241*8fb81238SShri Abhyankar 3242*8fb81238SShri Abhyankar /* read in other processors and ship out */ 3243*8fb81238SShri Abhyankar for (i=1; i<size; i++) { 3244*8fb81238SShri Abhyankar nz = procsnz[i]; 3245*8fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3246*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(nz); 3247*8fb81238SShri Abhyankar ierr = MPI_Send(vals,mpicnt,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 3248*8fb81238SShri Abhyankar } 3249*8fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 3250*8fb81238SShri Abhyankar } else { 3251*8fb81238SShri Abhyankar /* receive numeric values */ 3252*8fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 3253*8fb81238SShri Abhyankar 3254*8fb81238SShri Abhyankar /* receive message of values*/ 3255*8fb81238SShri Abhyankar mpicnt = PetscMPIIntCast(nz); 3256*8fb81238SShri Abhyankar ierr = MPI_Recv(vals,mpicnt,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm,&status);CHKERRQ(ierr); 3257*8fb81238SShri Abhyankar ierr = MPI_Get_count(&status,MPIU_SCALAR,&mpimaxnz);CHKERRQ(ierr); 3258*8fb81238SShri Abhyankar if (mpimaxnz == MPI_UNDEFINED) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned MPI_UNDEFINED, expected %d",mpicnt); 3259*8fb81238SShri Abhyankar else if (mpimaxnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Get_count() returned impossible negative value %d, expected %d",mpimaxnz,mpicnt); 3260*8fb81238SShri Abhyankar else if (mpimaxnz != mpicnt) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file: expected %d received %d",mpicnt,mpimaxnz); 3261*8fb81238SShri Abhyankar 3262*8fb81238SShri Abhyankar /* insert into matrix */ 3263*8fb81238SShri Abhyankar jj = rstart; 3264*8fb81238SShri Abhyankar smycols = mycols; 3265*8fb81238SShri Abhyankar svals = vals; 3266*8fb81238SShri Abhyankar for (i=0; i<m; i++) { 3267*8fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 3268*8fb81238SShri Abhyankar smycols += ourlens[i]; 3269*8fb81238SShri Abhyankar svals += ourlens[i]; 3270*8fb81238SShri Abhyankar jj++; 3271*8fb81238SShri Abhyankar } 3272*8fb81238SShri Abhyankar } 3273*8fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 3274*8fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 3275*8fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 3276*8fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 3277*8fb81238SShri Abhyankar 3278*8fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3279*8fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3280*8fb81238SShri Abhyankar PetscFunctionReturn(0); 3281*8fb81238SShri Abhyankar } 3282*8fb81238SShri Abhyankar 3283*8fb81238SShri Abhyankar #undef __FUNCT__ 32844a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 32854aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 32864aa3045dSJed Brown { 32874aa3045dSJed Brown PetscErrorCode ierr; 32884aa3045dSJed Brown IS iscol_local; 32894aa3045dSJed Brown PetscInt csize; 32904aa3045dSJed Brown 32914aa3045dSJed Brown PetscFunctionBegin; 32924aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3293b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3294b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3295e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3296b79d0421SJed Brown } else { 32974aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3298b79d0421SJed Brown } 32994aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3300b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3301b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 33024aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 3303b79d0421SJed Brown } 33044aa3045dSJed Brown PetscFunctionReturn(0); 33054aa3045dSJed Brown } 33064aa3045dSJed Brown 33074aa3045dSJed Brown #undef __FUNCT__ 33084aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3309a0ff6018SBarry Smith /* 331029da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 331129da9460SBarry Smith in local and then by concatenating the local matrices the end result. 331229da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 33134aa3045dSJed Brown 33144aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3315a0ff6018SBarry Smith */ 33164aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3317a0ff6018SBarry Smith { 3318dfbe8321SBarry Smith PetscErrorCode ierr; 331932dcc486SBarry Smith PetscMPIInt rank,size; 3320b1d57f15SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j; 3321b1d57f15SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 3322fee21e36SBarry Smith Mat *local,M,Mreuse; 3323a77337e4SBarry Smith MatScalar *vwork,*aa; 33247adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 332500e6dbe6SBarry Smith Mat_SeqAIJ *aij; 33267e2c5f70SBarry Smith 3327a0ff6018SBarry Smith 3328a0ff6018SBarry Smith PetscFunctionBegin; 33291dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 33301dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 333100e6dbe6SBarry Smith 3332fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3333fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3334e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3335fee21e36SBarry Smith local = &Mreuse; 3336fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 3337fee21e36SBarry Smith } else { 3338a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3339fee21e36SBarry Smith Mreuse = *local; 3340606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 3341fee21e36SBarry Smith } 3342a0ff6018SBarry Smith 3343a0ff6018SBarry Smith /* 3344a0ff6018SBarry Smith m - number of local rows 3345a0ff6018SBarry Smith n - number of columns (same on all processors) 3346a0ff6018SBarry Smith rstart - first row in new global matrix generated 3347a0ff6018SBarry Smith */ 3348fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3349a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3350fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 335100e6dbe6SBarry Smith ii = aij->i; 335200e6dbe6SBarry Smith jj = aij->j; 335300e6dbe6SBarry Smith 3354a0ff6018SBarry Smith /* 335500e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 335600e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3357a0ff6018SBarry Smith */ 335800e6dbe6SBarry Smith 335900e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 33606a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3361ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3362ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3363e2c4fddaSBarry Smith nlocal = m; 33646a6a5d1dSBarry Smith } else { 3365ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3366ab50ec6bSBarry Smith } 3367ab50ec6bSBarry Smith } else { 33686a6a5d1dSBarry Smith nlocal = csize; 33696a6a5d1dSBarry Smith } 3370b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 337100e6dbe6SBarry Smith rstart = rend - nlocal; 337265e19b50SBarry Smith if (rank == size - 1 && rend != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n); 337300e6dbe6SBarry Smith 337400e6dbe6SBarry Smith /* next, compute all the lengths */ 3375b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 337600e6dbe6SBarry Smith olens = dlens + m; 337700e6dbe6SBarry Smith for (i=0; i<m; i++) { 337800e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 337900e6dbe6SBarry Smith olen = 0; 338000e6dbe6SBarry Smith dlen = 0; 338100e6dbe6SBarry Smith for (j=0; j<jend; j++) { 338200e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 338300e6dbe6SBarry Smith else dlen++; 338400e6dbe6SBarry Smith jj++; 338500e6dbe6SBarry Smith } 338600e6dbe6SBarry Smith olens[i] = olen; 338700e6dbe6SBarry Smith dlens[i] = dlen; 338800e6dbe6SBarry Smith } 3389f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3390f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 33917adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3392e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3393606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3394a0ff6018SBarry Smith } else { 3395b1d57f15SBarry Smith PetscInt ml,nl; 3396a0ff6018SBarry Smith 3397a0ff6018SBarry Smith M = *newmat; 3398a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3399e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3400a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3401c48de900SBarry Smith /* 3402c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3403c48de900SBarry Smith rather than the slower MatSetValues(). 3404c48de900SBarry Smith */ 3405c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3406c48de900SBarry Smith M->assembled = PETSC_FALSE; 3407a0ff6018SBarry Smith } 3408a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3409fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 341000e6dbe6SBarry Smith ii = aij->i; 341100e6dbe6SBarry Smith jj = aij->j; 341200e6dbe6SBarry Smith aa = aij->a; 3413a0ff6018SBarry Smith for (i=0; i<m; i++) { 3414a0ff6018SBarry Smith row = rstart + i; 341500e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 341600e6dbe6SBarry Smith cwork = jj; jj += nz; 341700e6dbe6SBarry Smith vwork = aa; aa += nz; 34188c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3419a0ff6018SBarry Smith } 3420a0ff6018SBarry Smith 3421a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3422a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3423a0ff6018SBarry Smith *newmat = M; 3424fee21e36SBarry Smith 3425fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3426fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3427fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3428fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 3429fee21e36SBarry Smith } 3430fee21e36SBarry Smith 3431a0ff6018SBarry Smith PetscFunctionReturn(0); 3432a0ff6018SBarry Smith } 3433273d9f13SBarry Smith 3434e2e86b8fSSatish Balay EXTERN_C_BEGIN 34354a2ae208SSatish Balay #undef __FUNCT__ 3436ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 3437b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3438ccd8e176SBarry Smith { 3439899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3440899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3441ccd8e176SBarry Smith const PetscInt *JJ; 3442ccd8e176SBarry Smith PetscScalar *values; 3443ccd8e176SBarry Smith PetscErrorCode ierr; 3444ccd8e176SBarry Smith 3445ccd8e176SBarry Smith PetscFunctionBegin; 3446e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3447899cda47SBarry Smith 344826283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 344926283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 345026283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 345126283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3452d0f46423SBarry Smith m = B->rmap->n; 3453d0f46423SBarry Smith cstart = B->cmap->rstart; 3454d0f46423SBarry Smith cend = B->cmap->rend; 3455d0f46423SBarry Smith rstart = B->rmap->rstart; 3456899cda47SBarry Smith 34571d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3458ccd8e176SBarry Smith 3459ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3460ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3461ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3462ecc77c7aSBarry Smith JJ = J + Ii[i]; 3463e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3464ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3465d0f46423SBarry Smith if (nnz && (JJ[nnz-1] >= B->cmap->N) SETERRRQ3(PETSC_ERR_ARG_WRONGSTATE,"Row %D ends with too large a column index %D (max allowed %D)",i,JJ[nnz-1],B->cmap->N); 3466ecc77c7aSBarry Smith } 3467ecc77c7aSBarry Smith #endif 3468ecc77c7aSBarry Smith 3469ccd8e176SBarry Smith for (i=0; i<m; i++) { 3470b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3471b7940d39SSatish Balay JJ = J + Ii[i]; 3472ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3473ccd8e176SBarry Smith d = 0; 34740daa03b5SJed Brown for (j=0; j<nnz; j++) { 34750daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3476ccd8e176SBarry Smith } 3477ccd8e176SBarry Smith d_nnz[i] = d; 3478ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3479ccd8e176SBarry Smith } 3480ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 34811d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3482ccd8e176SBarry Smith 3483ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3484ccd8e176SBarry Smith else { 3485ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3486ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3487ccd8e176SBarry Smith } 3488ccd8e176SBarry Smith 3489ccd8e176SBarry Smith for (i=0; i<m; i++) { 3490ccd8e176SBarry Smith ii = i + rstart; 3491b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3492b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3493ccd8e176SBarry Smith } 3494ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3495ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3496ccd8e176SBarry Smith 3497ccd8e176SBarry Smith if (!v) { 3498ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3499ccd8e176SBarry Smith } 3500ccd8e176SBarry Smith PetscFunctionReturn(0); 3501ccd8e176SBarry Smith } 3502e2e86b8fSSatish Balay EXTERN_C_END 3503ccd8e176SBarry Smith 3504ccd8e176SBarry Smith #undef __FUNCT__ 3505ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 35061eea217eSSatish Balay /*@ 3507ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3508ccd8e176SBarry Smith (the default parallel PETSc format). 3509ccd8e176SBarry Smith 3510ccd8e176SBarry Smith Collective on MPI_Comm 3511ccd8e176SBarry Smith 3512ccd8e176SBarry Smith Input Parameters: 3513a1661176SMatthew Knepley + B - the matrix 3514ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 35150daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3516ccd8e176SBarry Smith - v - optional values in the matrix 3517ccd8e176SBarry Smith 3518ccd8e176SBarry Smith Level: developer 3519ccd8e176SBarry Smith 352012251496SSatish Balay Notes: 352112251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 352212251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 352312251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 352412251496SSatish Balay 352512251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 352612251496SSatish Balay 352712251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 352812251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 352912251496SSatish Balay as shown: 353012251496SSatish Balay 353112251496SSatish Balay 1 0 0 353212251496SSatish Balay 2 0 3 P0 353312251496SSatish Balay ------- 353412251496SSatish Balay 4 5 6 P1 353512251496SSatish Balay 353612251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 353712251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 353812251496SSatish Balay j = {0,0,2} [size = nz = 6] 353912251496SSatish Balay v = {1,2,3} [size = nz = 6] 354012251496SSatish Balay 354112251496SSatish Balay Process1 [P1]: rows_owned=[2] 354212251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 354312251496SSatish Balay j = {0,1,2} [size = nz = 6] 354412251496SSatish Balay v = {4,5,6} [size = nz = 6] 354512251496SSatish Balay 3546ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3547ccd8e176SBarry Smith 35482fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ, 35498d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3550ccd8e176SBarry Smith @*/ 3551be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3552ccd8e176SBarry Smith { 3553ccd8e176SBarry Smith PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]); 3554ccd8e176SBarry Smith 3555ccd8e176SBarry Smith PetscFunctionBegin; 3556ccd8e176SBarry Smith ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr); 3557ccd8e176SBarry Smith if (f) { 3558ccd8e176SBarry Smith ierr = (*f)(B,i,j,v);CHKERRQ(ierr); 3559ccd8e176SBarry Smith } 3560ccd8e176SBarry Smith PetscFunctionReturn(0); 3561ccd8e176SBarry Smith } 3562ccd8e176SBarry Smith 3563ccd8e176SBarry Smith #undef __FUNCT__ 35644a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3565273d9f13SBarry Smith /*@C 3566ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3567273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3568273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3569273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3570273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3571273d9f13SBarry Smith 3572273d9f13SBarry Smith Collective on MPI_Comm 3573273d9f13SBarry Smith 3574273d9f13SBarry Smith Input Parameters: 3575273d9f13SBarry Smith + A - the matrix 3576273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3577273d9f13SBarry Smith (same value is used for all local rows) 3578273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3579273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3580273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3581273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3582273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3583273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3584273d9f13SBarry Smith submatrix (same value is used for all local rows). 3585273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3586273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3587273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3588273d9f13SBarry Smith structure. The size of this array is equal to the number 3589273d9f13SBarry Smith of local rows, i.e 'm'. 3590273d9f13SBarry Smith 359149a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 359249a6f317SBarry Smith 3593273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3594ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 3595ccd8e176SBarry Smith storage. The stored row and column indices begin with zero. See the users manual for details. 3596273d9f13SBarry Smith 3597273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3598273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3599273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3600273d9f13SBarry Smith 3601273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3602273d9f13SBarry Smith as the submatrix which is obtained by extraction the part corresponding 3603273d9f13SBarry Smith to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the 3604273d9f13SBarry Smith first row that belongs to the processor, and r2 is the last row belonging 3605273d9f13SBarry Smith to the this processor. This is a square mxm matrix. The remaining portion 3606273d9f13SBarry Smith of the local submatrix (mxN) constitute the OFF-DIAGONAL portion. 3607273d9f13SBarry Smith 3608273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3609273d9f13SBarry Smith 3610aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3611aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3612aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3613aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3614aa95bbe8SBarry Smith 3615273d9f13SBarry Smith Example usage: 3616273d9f13SBarry Smith 3617273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3618273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3619273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3620273d9f13SBarry Smith as follows: 3621273d9f13SBarry Smith 3622273d9f13SBarry Smith .vb 3623273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3624273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3625273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3626273d9f13SBarry Smith ------------------------------------- 3627273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3628273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3629273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3630273d9f13SBarry Smith ------------------------------------- 3631273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3632273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3633273d9f13SBarry Smith .ve 3634273d9f13SBarry Smith 3635273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3636273d9f13SBarry Smith 3637273d9f13SBarry Smith .vb 3638273d9f13SBarry Smith A B C 3639273d9f13SBarry Smith D E F 3640273d9f13SBarry Smith G H I 3641273d9f13SBarry Smith .ve 3642273d9f13SBarry Smith 3643273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3644273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3645273d9f13SBarry Smith 3646273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3647273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3648273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3649273d9f13SBarry Smith 3650273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3651273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3652273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3653273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3654273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3655273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3656273d9f13SBarry Smith 3657273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3658273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3659273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3660273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3661273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3662273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3663273d9f13SBarry Smith .vb 3664273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3665273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3666273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3667273d9f13SBarry Smith .ve 3668273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3669273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3670273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3671273d9f13SBarry Smith 34 values. 3672273d9f13SBarry Smith 3673273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3674273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3675273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3676273d9f13SBarry Smith .vb 3677273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3678273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3679273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3680273d9f13SBarry Smith .ve 3681273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3682273d9f13SBarry Smith hence pre-allocation is perfect. 3683273d9f13SBarry Smith 3684273d9f13SBarry Smith Level: intermediate 3685273d9f13SBarry Smith 3686273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3687273d9f13SBarry Smith 3688ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(), 3689aa95bbe8SBarry Smith MPIAIJ, MatGetInfo() 3690273d9f13SBarry Smith @*/ 3691be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3692273d9f13SBarry Smith { 3693b1d57f15SBarry Smith PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]); 3694273d9f13SBarry Smith 3695273d9f13SBarry Smith PetscFunctionBegin; 3696a23d5eceSKris Buschelman ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr); 3697a23d5eceSKris Buschelman if (f) { 3698a23d5eceSKris Buschelman ierr = (*f)(B,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 3699273d9f13SBarry Smith } 3700273d9f13SBarry Smith PetscFunctionReturn(0); 3701273d9f13SBarry Smith } 3702273d9f13SBarry Smith 37034a2ae208SSatish Balay #undef __FUNCT__ 37042fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 370558d36128SBarry Smith /*@ 37062fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 37072fb0ec9aSBarry Smith CSR format the local rows. 37082fb0ec9aSBarry Smith 37092fb0ec9aSBarry Smith Collective on MPI_Comm 37102fb0ec9aSBarry Smith 37112fb0ec9aSBarry Smith Input Parameters: 37122fb0ec9aSBarry Smith + comm - MPI communicator 37132fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 37142fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 37152fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 37162fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 37172fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 37182fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 37192fb0ec9aSBarry Smith . i - row indices 37202fb0ec9aSBarry Smith . j - column indices 37212fb0ec9aSBarry Smith - a - matrix values 37222fb0ec9aSBarry Smith 37232fb0ec9aSBarry Smith Output Parameter: 37242fb0ec9aSBarry Smith . mat - the matrix 372503bfb495SBarry Smith 37262fb0ec9aSBarry Smith Level: intermediate 37272fb0ec9aSBarry Smith 37282fb0ec9aSBarry Smith Notes: 37292fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 37302fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 37318d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 37322fb0ec9aSBarry Smith 373312251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 373412251496SSatish Balay 373512251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 373612251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 373712251496SSatish Balay as shown: 373812251496SSatish Balay 373912251496SSatish Balay 1 0 0 374012251496SSatish Balay 2 0 3 P0 374112251496SSatish Balay ------- 374212251496SSatish Balay 4 5 6 P1 374312251496SSatish Balay 374412251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 374512251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 374612251496SSatish Balay j = {0,0,2} [size = nz = 6] 374712251496SSatish Balay v = {1,2,3} [size = nz = 6] 374812251496SSatish Balay 374912251496SSatish Balay Process1 [P1]: rows_owned=[2] 375012251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 375112251496SSatish Balay j = {0,1,2} [size = nz = 6] 375212251496SSatish Balay v = {4,5,6} [size = nz = 6] 37532fb0ec9aSBarry Smith 37542fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 37552fb0ec9aSBarry Smith 37562fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 37578d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 37582fb0ec9aSBarry Smith @*/ 375982b90586SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat) 37602fb0ec9aSBarry Smith { 37612fb0ec9aSBarry Smith PetscErrorCode ierr; 37622fb0ec9aSBarry Smith 37632fb0ec9aSBarry Smith PetscFunctionBegin; 37642fb0ec9aSBarry Smith if (i[0]) { 3765e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 37662fb0ec9aSBarry Smith } 3767e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 37682fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3769d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 37702fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 37712fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 37722fb0ec9aSBarry Smith PetscFunctionReturn(0); 37732fb0ec9aSBarry Smith } 37742fb0ec9aSBarry Smith 37752fb0ec9aSBarry Smith #undef __FUNCT__ 37764a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 3777273d9f13SBarry Smith /*@C 3778273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 3779273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3780273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3781273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3782273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3783273d9f13SBarry Smith 3784273d9f13SBarry Smith Collective on MPI_Comm 3785273d9f13SBarry Smith 3786273d9f13SBarry Smith Input Parameters: 3787273d9f13SBarry Smith + comm - MPI communicator 3788273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 3789273d9f13SBarry Smith This value should be the same as the local size used in creating the 3790273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 3791273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 3792273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 3793273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 3794273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3795273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 3796273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3797273d9f13SBarry Smith (same value is used for all local rows) 3798273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3799273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3800273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3801273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3802273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3803273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3804273d9f13SBarry Smith submatrix (same value is used for all local rows). 3805273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3806273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3807273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3808273d9f13SBarry Smith structure. The size of this array is equal to the number 3809273d9f13SBarry Smith of local rows, i.e 'm'. 3810273d9f13SBarry Smith 3811273d9f13SBarry Smith Output Parameter: 3812273d9f13SBarry Smith . A - the matrix 3813273d9f13SBarry Smith 3814175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3815ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3816175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3817175b88e8SBarry Smith 3818273d9f13SBarry Smith Notes: 381949a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 382049a6f317SBarry Smith 3821273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 3822273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 3823273d9f13SBarry Smith storage requirements for this matrix. 3824273d9f13SBarry Smith 3825273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 3826273d9f13SBarry Smith processor than it must be used on all processors that share the object for 3827273d9f13SBarry Smith that argument. 3828273d9f13SBarry Smith 3829273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 3830273d9f13SBarry Smith (possibly both). 3831273d9f13SBarry Smith 383233a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 383333a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 383433a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 383533a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 383633a7c187SSatish Balay values corresponding to [m x N] submatrix. 3837273d9f13SBarry Smith 383833a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 383933a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 384033a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 384133a7c187SSatish Balay 384233a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 384333a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 384433a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 384533a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 384633a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 384733a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 384833a7c187SSatish Balay illustrates this concept. 384933a7c187SSatish Balay 385033a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 385133a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 385233a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 385333a7c187SSatish Balay local matrix (a rectangular submatrix). 3854273d9f13SBarry Smith 3855273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3856273d9f13SBarry Smith 385797d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 385897d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 385997d05335SKris Buschelman type of communicator, use the construction mechanism: 386078102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 386197d05335SKris Buschelman 3862273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 3863273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 3864273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3865273d9f13SBarry Smith 3866273d9f13SBarry Smith Options Database Keys: 3867923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 3868923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3869273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3870273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3871273d9f13SBarry Smith the user still MUST index entries starting at 0! 3872273d9f13SBarry Smith 3873273d9f13SBarry Smith 3874273d9f13SBarry Smith Example usage: 3875273d9f13SBarry Smith 3876273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3877273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3878273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3879273d9f13SBarry Smith as follows: 3880273d9f13SBarry Smith 3881273d9f13SBarry Smith .vb 3882273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3883273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3884273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3885273d9f13SBarry Smith ------------------------------------- 3886273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3887273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3888273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3889273d9f13SBarry Smith ------------------------------------- 3890273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3891273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3892273d9f13SBarry Smith .ve 3893273d9f13SBarry Smith 3894273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3895273d9f13SBarry Smith 3896273d9f13SBarry Smith .vb 3897273d9f13SBarry Smith A B C 3898273d9f13SBarry Smith D E F 3899273d9f13SBarry Smith G H I 3900273d9f13SBarry Smith .ve 3901273d9f13SBarry Smith 3902273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3903273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3904273d9f13SBarry Smith 3905273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3906273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3907273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3908273d9f13SBarry Smith 3909273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3910273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3911273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3912273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3913273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3914273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3915273d9f13SBarry Smith 3916273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3917273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3918273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3919273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3920273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3921273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3922273d9f13SBarry Smith .vb 3923273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3924273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3925273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3926273d9f13SBarry Smith .ve 3927273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3928273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3929273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3930273d9f13SBarry Smith 34 values. 3931273d9f13SBarry Smith 3932273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3933273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3934273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3935273d9f13SBarry Smith .vb 3936273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3937273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3938273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3939273d9f13SBarry Smith .ve 3940273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3941273d9f13SBarry Smith hence pre-allocation is perfect. 3942273d9f13SBarry Smith 3943273d9f13SBarry Smith Level: intermediate 3944273d9f13SBarry Smith 3945273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3946273d9f13SBarry Smith 3947ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 39482fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 3949273d9f13SBarry Smith @*/ 3950be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A) 3951273d9f13SBarry Smith { 39526849ba73SBarry Smith PetscErrorCode ierr; 3953b1d57f15SBarry Smith PetscMPIInt size; 3954273d9f13SBarry Smith 3955273d9f13SBarry Smith PetscFunctionBegin; 3956f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3957f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 3958273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3959273d9f13SBarry Smith if (size > 1) { 3960273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 3961273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 3962273d9f13SBarry Smith } else { 3963273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3964273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 3965273d9f13SBarry Smith } 3966273d9f13SBarry Smith PetscFunctionReturn(0); 3967273d9f13SBarry Smith } 3968195d93cdSBarry Smith 39694a2ae208SSatish Balay #undef __FUNCT__ 39704a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 3971be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 3972195d93cdSBarry Smith { 3973195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 3974b1d57f15SBarry Smith 3975195d93cdSBarry Smith PetscFunctionBegin; 3976195d93cdSBarry Smith *Ad = a->A; 3977195d93cdSBarry Smith *Ao = a->B; 3978195d93cdSBarry Smith *colmap = a->garray; 3979195d93cdSBarry Smith PetscFunctionReturn(0); 3980195d93cdSBarry Smith } 3981a2243be0SBarry Smith 3982a2243be0SBarry Smith #undef __FUNCT__ 3983a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 3984dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 3985a2243be0SBarry Smith { 3986dfbe8321SBarry Smith PetscErrorCode ierr; 3987b1d57f15SBarry Smith PetscInt i; 3988a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 3989a2243be0SBarry Smith 3990a2243be0SBarry Smith PetscFunctionBegin; 39918ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 399208b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 3993a2243be0SBarry Smith ISColoring ocoloring; 3994a2243be0SBarry Smith 3995a2243be0SBarry Smith /* set coloring for diagonal portion */ 3996a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 3997a2243be0SBarry Smith 3998a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 39997adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 4000d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4001d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4002a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4003a2243be0SBarry Smith } 4004a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4005d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4006a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4007a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4008a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 400908b6dcc0SBarry Smith ISColoringValue *colors; 4010b1d57f15SBarry Smith PetscInt *larray; 4011a2243be0SBarry Smith ISColoring ocoloring; 4012a2243be0SBarry Smith 4013a2243be0SBarry Smith /* set coloring for diagonal portion */ 4014d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4015d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4016d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4017a2243be0SBarry Smith } 4018d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 4019d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4020d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4021a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4022a2243be0SBarry Smith } 4023a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4024d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4025a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 4026a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4027a2243be0SBarry Smith 4028a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4029d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4030d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 4031d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4032d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4033a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4034a2243be0SBarry Smith } 4035a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4036d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4037a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4038a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4039a2243be0SBarry Smith } else { 4040e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4041a2243be0SBarry Smith } 4042a2243be0SBarry Smith 4043a2243be0SBarry Smith PetscFunctionReturn(0); 4044a2243be0SBarry Smith } 4045a2243be0SBarry Smith 4046dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 4047a2243be0SBarry Smith #undef __FUNCT__ 4048779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 4049dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 4050a2243be0SBarry Smith { 4051a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4052dfbe8321SBarry Smith PetscErrorCode ierr; 4053a2243be0SBarry Smith 4054a2243be0SBarry Smith PetscFunctionBegin; 4055779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 4056779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 4057779c1a83SBarry Smith PetscFunctionReturn(0); 4058779c1a83SBarry Smith } 4059dcf5cc72SBarry Smith #endif 4060779c1a83SBarry Smith 4061779c1a83SBarry Smith #undef __FUNCT__ 4062779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4063b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4064779c1a83SBarry Smith { 4065779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4066dfbe8321SBarry Smith PetscErrorCode ierr; 4067779c1a83SBarry Smith 4068779c1a83SBarry Smith PetscFunctionBegin; 4069779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4070779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4071a2243be0SBarry Smith PetscFunctionReturn(0); 4072a2243be0SBarry Smith } 4073c5d6d63eSBarry Smith 4074c5d6d63eSBarry Smith #undef __FUNCT__ 407551dd7536SBarry Smith #define __FUNCT__ "MatMerge" 4076bc08b0f1SBarry Smith /*@ 407751dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 407851dd7536SBarry Smith matrices from each processor 4079c5d6d63eSBarry Smith 4080c5d6d63eSBarry Smith Collective on MPI_Comm 4081c5d6d63eSBarry Smith 4082c5d6d63eSBarry Smith Input Parameters: 408351dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4084d6bb3c2dSHong Zhang . inmat - the input sequential matrices 40850e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4086d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 408751dd7536SBarry Smith 408851dd7536SBarry Smith Output Parameter: 408951dd7536SBarry Smith . outmat - the parallel matrix generated 4090c5d6d63eSBarry Smith 40917e25d530SSatish Balay Level: advanced 40927e25d530SSatish Balay 4093f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4094c5d6d63eSBarry Smith 4095c5d6d63eSBarry Smith @*/ 4096be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4097c5d6d63eSBarry Smith { 4098dfbe8321SBarry Smith PetscErrorCode ierr; 4099b7940d39SSatish Balay PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz; 4100ba8c8a56SBarry Smith PetscInt *indx; 4101ba8c8a56SBarry Smith PetscScalar *values; 4102c5d6d63eSBarry Smith 4103c5d6d63eSBarry Smith PetscFunctionBegin; 41040e36024fSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4105d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4106d6bb3c2dSHong Zhang /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 41070e36024fSHong Zhang if (n == PETSC_DECIDE){ 4108357abbc8SBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 41090e36024fSHong Zhang } 4110357abbc8SBarry Smith ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4111357abbc8SBarry Smith rstart -= m; 4112d6bb3c2dSHong Zhang 4113d6bb3c2dSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4114d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4115ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4116d6bb3c2dSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 4117ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4118d6bb3c2dSHong Zhang } 4119d6bb3c2dSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 4120f69a0ea3SMatthew Knepley ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 4121f69a0ea3SMatthew Knepley ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4122d6bb3c2dSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 4123d6bb3c2dSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 4124d6bb3c2dSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 4125d6bb3c2dSHong Zhang 4126d6bb3c2dSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 4127d6bb3c2dSHong Zhang ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 4128d6bb3c2dSHong Zhang } else { 4129e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 4130d6bb3c2dSHong Zhang } 4131d6bb3c2dSHong Zhang 4132d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4133ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4134b7940d39SSatish Balay Ii = i + rstart; 4135b7940d39SSatish Balay ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4136ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4137d6bb3c2dSHong Zhang } 4138d6bb3c2dSHong Zhang ierr = MatDestroy(inmat);CHKERRQ(ierr); 4139d6bb3c2dSHong Zhang ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4140d6bb3c2dSHong Zhang ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 414151dd7536SBarry Smith 4142c5d6d63eSBarry Smith PetscFunctionReturn(0); 4143c5d6d63eSBarry Smith } 4144c5d6d63eSBarry Smith 4145c5d6d63eSBarry Smith #undef __FUNCT__ 4146c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4147dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4148c5d6d63eSBarry Smith { 4149dfbe8321SBarry Smith PetscErrorCode ierr; 415032dcc486SBarry Smith PetscMPIInt rank; 4151b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4152de4209c5SBarry Smith size_t len; 4153b1d57f15SBarry Smith const PetscInt *indx; 4154c5d6d63eSBarry Smith PetscViewer out; 4155c5d6d63eSBarry Smith char *name; 4156c5d6d63eSBarry Smith Mat B; 4157b3cc6726SBarry Smith const PetscScalar *values; 4158c5d6d63eSBarry Smith 4159c5d6d63eSBarry Smith PetscFunctionBegin; 4160c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4161c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4162f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4163f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4164f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4165f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4166f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4167c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4168c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4169c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4170c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4171c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4172c5d6d63eSBarry Smith } 4173c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4174c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4175c5d6d63eSBarry Smith 41767adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4177c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4178c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4179c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4180852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4181c5d6d63eSBarry Smith ierr = PetscFree(name); 4182c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 4183c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 4184c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 4185c5d6d63eSBarry Smith PetscFunctionReturn(0); 4186c5d6d63eSBarry Smith } 4187e5f2cdd8SHong Zhang 418851a7d1a8SHong Zhang EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat); 418951a7d1a8SHong Zhang #undef __FUNCT__ 419051a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 4191be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 419251a7d1a8SHong Zhang { 419351a7d1a8SHong Zhang PetscErrorCode ierr; 4194671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4195776b82aeSLisandro Dalcin PetscContainer container; 419651a7d1a8SHong Zhang 419751a7d1a8SHong Zhang PetscFunctionBegin; 4198671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4199671beff6SHong Zhang if (container) { 4200776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 420151a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 42023e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 42033e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 420451a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 420551a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4206533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 420702c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4208533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 420902c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 421005b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 421105b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 421205b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 421326283091SBarry Smith ierr = PetscLayoutDestroy(merge->rowmap);CHKERRQ(ierr); 4214671beff6SHong Zhang 4215776b82aeSLisandro Dalcin ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 4216671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4217671beff6SHong Zhang } 421851a7d1a8SHong Zhang ierr = PetscFree(merge);CHKERRQ(ierr); 421951a7d1a8SHong Zhang 422051a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 422151a7d1a8SHong Zhang PetscFunctionReturn(0); 422251a7d1a8SHong Zhang } 422351a7d1a8SHong Zhang 42247c4f633dSBarry Smith #include "../src/mat/utils/freespace.h" 4225be0fcf8dSHong Zhang #include "petscbt.h" 42264ebed01fSBarry Smith 4227e5f2cdd8SHong Zhang #undef __FUNCT__ 422838f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric" 4229e5f2cdd8SHong Zhang /*@C 4230f08fae4eSHong Zhang MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential 4231e5f2cdd8SHong Zhang matrices from each processor 4232e5f2cdd8SHong Zhang 4233e5f2cdd8SHong Zhang Collective on MPI_Comm 4234e5f2cdd8SHong Zhang 4235e5f2cdd8SHong Zhang Input Parameters: 4236e5f2cdd8SHong Zhang + comm - the communicators the parallel matrix will live on 4237f08fae4eSHong Zhang . seqmat - the input sequential matrices 42380e36024fSHong Zhang . m - number of local rows (or PETSC_DECIDE) 42390e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4240e5f2cdd8SHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4241e5f2cdd8SHong Zhang 4242e5f2cdd8SHong Zhang Output Parameter: 4243f08fae4eSHong Zhang . mpimat - the parallel matrix generated 4244e5f2cdd8SHong Zhang 4245e5f2cdd8SHong Zhang Level: advanced 4246e5f2cdd8SHong Zhang 4247affca5deSHong Zhang Notes: 4248affca5deSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4249affca5deSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4250affca5deSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4251e5f2cdd8SHong Zhang @*/ 4252be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat) 425355d1abb9SHong Zhang { 425455d1abb9SHong Zhang PetscErrorCode ierr; 42557adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 425655d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4257b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4258d0f46423SBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j; 4259b1d57f15SBarry Smith PetscInt proc,m; 4260b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4261b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4262b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 426355d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 426455d1abb9SHong Zhang MPI_Status *status; 4265a77337e4SBarry Smith MatScalar *aa=a->a; 4266dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 426755d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4268776b82aeSLisandro Dalcin PetscContainer container; 426955d1abb9SHong Zhang 427055d1abb9SHong Zhang PetscFunctionBegin; 42714ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 42723c2c1871SHong Zhang 427355d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 427455d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 427555d1abb9SHong Zhang 427655d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 427755d1abb9SHong Zhang if (container) { 4278776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 427955d1abb9SHong Zhang } 428055d1abb9SHong Zhang bi = merge->bi; 428155d1abb9SHong Zhang bj = merge->bj; 428255d1abb9SHong Zhang buf_ri = merge->buf_ri; 428355d1abb9SHong Zhang buf_rj = merge->buf_rj; 428455d1abb9SHong Zhang 428555d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 42867a2fc3feSBarry Smith owners = merge->rowmap->range; 428755d1abb9SHong Zhang len_s = merge->len_s; 428855d1abb9SHong Zhang 428955d1abb9SHong Zhang /* send and recv matrix values */ 429055d1abb9SHong Zhang /*-----------------------------*/ 4291357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 429255d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 429355d1abb9SHong Zhang 429455d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 429555d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 429655d1abb9SHong Zhang if (!len_s[proc]) continue; 429755d1abb9SHong Zhang i = owners[proc]; 429855d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 429955d1abb9SHong Zhang k++; 430055d1abb9SHong Zhang } 430155d1abb9SHong Zhang 43020c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 43030c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 430455d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 430555d1abb9SHong Zhang 430655d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 430755d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 430855d1abb9SHong Zhang 430955d1abb9SHong Zhang /* insert mat values of mpimat */ 431055d1abb9SHong Zhang /*----------------------------*/ 4311a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 43120572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 431355d1abb9SHong Zhang 431455d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 431555d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 431655d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 431755d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 431855d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 431955d1abb9SHong Zhang } 432055d1abb9SHong Zhang 432155d1abb9SHong Zhang /* set values of ba */ 43227a2fc3feSBarry Smith m = merge->rowmap->n; 432355d1abb9SHong Zhang for (i=0; i<m; i++) { 432455d1abb9SHong Zhang arow = owners[rank] + i; 432555d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 432655d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4327a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 432855d1abb9SHong Zhang 432955d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 433055d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 433155d1abb9SHong Zhang aj = a->j + ai[arow]; 433255d1abb9SHong Zhang aa = a->a + ai[arow]; 433355d1abb9SHong Zhang nextaj = 0; 433455d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 433555d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 433655d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 433755d1abb9SHong Zhang } 433855d1abb9SHong Zhang } 433955d1abb9SHong Zhang 434055d1abb9SHong Zhang /* add received vals into ba */ 434155d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 434255d1abb9SHong Zhang /* i-th row */ 434355d1abb9SHong Zhang if (i == *nextrow[k]) { 434455d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 434555d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 434655d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 434755d1abb9SHong Zhang nextaj = 0; 434855d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 434955d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 435055d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 435155d1abb9SHong Zhang } 435255d1abb9SHong Zhang } 435355d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 435455d1abb9SHong Zhang } 435555d1abb9SHong Zhang } 435655d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 435755d1abb9SHong Zhang } 435855d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 435955d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 436055d1abb9SHong Zhang 4361533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 436255d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 436355d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 43641d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 43654ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 436655d1abb9SHong Zhang PetscFunctionReturn(0); 436755d1abb9SHong Zhang } 436838f152feSBarry Smith 436938f152feSBarry Smith #undef __FUNCT__ 437038f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic" 4371be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4372e5f2cdd8SHong Zhang { 4373f08fae4eSHong Zhang PetscErrorCode ierr; 437455a3bba9SHong Zhang Mat B_mpi; 4375c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4376b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4377b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4378d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4379b1d57f15SBarry Smith PetscInt len,proc,*dnz,*onz; 4380b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4381b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 438255d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 438358cb9c82SHong Zhang MPI_Status *status; 4384a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4385be0fcf8dSHong Zhang PetscBT lnkbt; 438651a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4387776b82aeSLisandro Dalcin PetscContainer container; 438802c68681SHong Zhang 4389e5f2cdd8SHong Zhang PetscFunctionBegin; 43904ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 43913c2c1871SHong Zhang 439238f152feSBarry Smith /* make sure it is a PETSc comm */ 439338f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4394e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4395e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 439655d1abb9SHong Zhang 439751a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4398c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4399e5f2cdd8SHong Zhang 44006abd8857SHong Zhang /* determine row ownership */ 4401f08fae4eSHong Zhang /*---------------------------------------------------------*/ 440226283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 440326283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 440426283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 440526283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 440626283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4407b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4408b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 440955d1abb9SHong Zhang 44107a2fc3feSBarry Smith m = merge->rowmap->n; 44117a2fc3feSBarry Smith M = merge->rowmap->N; 44127a2fc3feSBarry Smith owners = merge->rowmap->range; 44136abd8857SHong Zhang 44146abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 44156abd8857SHong Zhang /*---------------------------------------------------------*/ 44163e06a4e6SHong Zhang len_s = merge->len_s; 441751a7d1a8SHong Zhang 44182257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4419c2234fe3SHong Zhang merge->nsend = 0; 4420409913e3SHong Zhang for (proc=0; proc<size; proc++){ 44212257cef7SHong Zhang len_si[proc] = 0; 44223e06a4e6SHong Zhang if (proc == rank){ 44236abd8857SHong Zhang len_s[proc] = 0; 44243e06a4e6SHong Zhang } else { 442502c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 44263e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 44273e06a4e6SHong Zhang } 44283e06a4e6SHong Zhang if (len_s[proc]) { 4429c2234fe3SHong Zhang merge->nsend++; 44302257cef7SHong Zhang nrows = 0; 44312257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 44322257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 44332257cef7SHong Zhang } 44342257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 44352257cef7SHong Zhang len += len_si[proc]; 4436409913e3SHong Zhang } 443758cb9c82SHong Zhang } 4438409913e3SHong Zhang 44392257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 44402257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 444151a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 444255d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4443671beff6SHong Zhang 44443e06a4e6SHong Zhang /* post the Irecv of j-structure */ 44453e06a4e6SHong Zhang /*-------------------------------*/ 44462c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 44473e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 444802c68681SHong Zhang 44493e06a4e6SHong Zhang /* post the Isend of j-structure */ 4450affca5deSHong Zhang /*--------------------------------*/ 44511d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 44523e06a4e6SHong Zhang 44532257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4454409913e3SHong Zhang if (!len_s[proc]) continue; 445502c68681SHong Zhang i = owners[proc]; 4456b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 445751a7d1a8SHong Zhang k++; 445851a7d1a8SHong Zhang } 445951a7d1a8SHong Zhang 44603e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 44613e06a4e6SHong Zhang /*------------------------------------------------*/ 44620c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 44630c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 446402c68681SHong Zhang 446502c68681SHong Zhang /* send and recv i-structure */ 446602c68681SHong Zhang /*---------------------------*/ 44672c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 446802c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 446902c68681SHong Zhang 4470b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 44713e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 44722257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 447302c68681SHong Zhang if (!len_s[proc]) continue; 44743e06a4e6SHong Zhang /* form outgoing message for i-structure: 44753e06a4e6SHong Zhang buf_si[0]: nrows to be sent 44763e06a4e6SHong Zhang [1:nrows]: row index (global) 44773e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 44783e06a4e6SHong Zhang */ 44793e06a4e6SHong Zhang /*-------------------------------------------*/ 44802257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 44813e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 44823e06a4e6SHong Zhang buf_si[0] = nrows; 44833e06a4e6SHong Zhang buf_si_i[0] = 0; 44843e06a4e6SHong Zhang nrows = 0; 44853e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 44863e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 44873e06a4e6SHong Zhang if (anzi) { 44883e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 44893e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 44903e06a4e6SHong Zhang nrows++; 44913e06a4e6SHong Zhang } 44923e06a4e6SHong Zhang } 4493b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 449402c68681SHong Zhang k++; 44952257cef7SHong Zhang buf_si += len_si[proc]; 449602c68681SHong Zhang } 44972257cef7SHong Zhang 44980c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 44990c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 450002c68681SHong Zhang 4501ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 45023e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4503ae15b995SBarry Smith ierr = PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);CHKERRQ(ierr); 45043e06a4e6SHong Zhang } 45053e06a4e6SHong Zhang 45063e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 450702c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 450802c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 45091d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 45102257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 45113e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4512bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 451358cb9c82SHong Zhang 4514bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4515bcc1bcd5SHong Zhang /*----------------------------------------------*/ 451658cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4517b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 451858cb9c82SHong Zhang bi[0] = 0; 451958cb9c82SHong Zhang 4520be0fcf8dSHong Zhang /* create and initialize a linked list */ 4521be0fcf8dSHong Zhang nlnk = N+1; 4522be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 452358cb9c82SHong Zhang 4524bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 452558cb9c82SHong Zhang len = 0; 4526bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4527a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 452858cb9c82SHong Zhang current_space = free_space; 452958cb9c82SHong Zhang 4530bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 45310572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 45321d79065fSBarry Smith 45333e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 45342257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 45353e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 45363e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 45372257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 45383e06a4e6SHong Zhang } 45392257cef7SHong Zhang 4540bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4541bcc1bcd5SHong Zhang len = 0; 454258cb9c82SHong Zhang for (i=0;i<m;i++) { 454358cb9c82SHong Zhang bnzi = 0; 454458cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 454558cb9c82SHong Zhang arow = owners[rank] + i; 454658cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 454758cb9c82SHong Zhang aj = a->j + ai[arow]; 4548be0fcf8dSHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 454958cb9c82SHong Zhang bnzi += nlnk; 455058cb9c82SHong Zhang /* add received col data into lnk */ 455151a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 455255d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 45533e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 45543e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 45553e06a4e6SHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 45563e06a4e6SHong Zhang bnzi += nlnk; 45573e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 45583e06a4e6SHong Zhang } 455958cb9c82SHong Zhang } 4560bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 456158cb9c82SHong Zhang 456258cb9c82SHong Zhang /* if free space is not available, make more free space */ 456358cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 45644238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 456558cb9c82SHong Zhang nspacedouble++; 456658cb9c82SHong Zhang } 456758cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4568be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4569bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4570bcc1bcd5SHong Zhang 457158cb9c82SHong Zhang current_space->array += bnzi; 457258cb9c82SHong Zhang current_space->local_used += bnzi; 457358cb9c82SHong Zhang current_space->local_remaining -= bnzi; 457458cb9c82SHong Zhang 457558cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 457658cb9c82SHong Zhang } 4577bcc1bcd5SHong Zhang 45781d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4579bcc1bcd5SHong Zhang 4580b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4581a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4582be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4583409913e3SHong Zhang 4584bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4585bcc1bcd5SHong Zhang /*---------------------------------------*/ 4586f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 458754b84b50SHong Zhang if (n==PETSC_DECIDE) { 4588f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 458954b84b50SHong Zhang } else { 4590f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 459154b84b50SHong Zhang } 4592bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4593bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4594bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 459558cb9c82SHong Zhang 45966abd8857SHong Zhang /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */ 45976abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4598affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4599affca5deSHong Zhang merge->bi = bi; 4600affca5deSHong Zhang merge->bj = bj; 460102c68681SHong Zhang merge->buf_ri = buf_ri; 460202c68681SHong Zhang merge->buf_rj = buf_rj; 4603de0260b3SHong Zhang merge->coi = PETSC_NULL; 4604de0260b3SHong Zhang merge->coj = PETSC_NULL; 4605de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4606affca5deSHong Zhang 4607affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4608776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4609776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4610affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4611affca5deSHong Zhang *mpimat = B_mpi; 461238f152feSBarry Smith 461338f152feSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 46144ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4615e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4616e5f2cdd8SHong Zhang } 461725616d81SHong Zhang 461838f152feSBarry Smith #undef __FUNCT__ 461938f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI" 4620be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 462155d1abb9SHong Zhang { 462255d1abb9SHong Zhang PetscErrorCode ierr; 462355d1abb9SHong Zhang 462455d1abb9SHong Zhang PetscFunctionBegin; 46254ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 462655d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 462755d1abb9SHong Zhang ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 462855d1abb9SHong Zhang } 462955d1abb9SHong Zhang ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr); 46304ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 463155d1abb9SHong Zhang PetscFunctionReturn(0); 463255d1abb9SHong Zhang } 46334ebed01fSBarry Smith 463425616d81SHong Zhang #undef __FUNCT__ 463525616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat" 4636bc08b0f1SBarry Smith /*@ 463732fba14fSHong Zhang MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows 463825616d81SHong Zhang 463932fba14fSHong Zhang Not Collective 464025616d81SHong Zhang 464125616d81SHong Zhang Input Parameters: 464225616d81SHong Zhang + A - the matrix 464325616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 464425616d81SHong Zhang 464525616d81SHong Zhang Output Parameter: 464625616d81SHong Zhang . A_loc - the local sequential matrix generated 464725616d81SHong Zhang 464825616d81SHong Zhang Level: developer 464925616d81SHong Zhang 465025616d81SHong Zhang @*/ 4651be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 465225616d81SHong Zhang { 465325616d81SHong Zhang PetscErrorCode ierr; 465401b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 465501b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 465601b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 4657a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 4658a77337e4SBarry Smith PetscScalar *ca; 4659d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 46605a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 466125616d81SHong Zhang 466225616d81SHong Zhang PetscFunctionBegin; 46634ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 466401b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4665dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 4666dea91ad1SHong Zhang ci[0] = 0; 466701b7ae99SHong Zhang for (i=0; i<am; i++){ 4668dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 466901b7ae99SHong Zhang } 4670dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 4671dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 4672dea91ad1SHong Zhang k = 0; 467301b7ae99SHong Zhang for (i=0; i<am; i++) { 46745a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 46755a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 467601b7ae99SHong Zhang /* off-diagonal portion of A */ 46775a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 46785a7d977cSHong Zhang col = cmap[*bj]; 46795a7d977cSHong Zhang if (col >= cstart) break; 46805a7d977cSHong Zhang cj[k] = col; bj++; 46815a7d977cSHong Zhang ca[k++] = *ba++; 46825a7d977cSHong Zhang } 46835a7d977cSHong Zhang /* diagonal portion of A */ 46845a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 46855a7d977cSHong Zhang cj[k] = cstart + *aj++; 46865a7d977cSHong Zhang ca[k++] = *aa++; 46875a7d977cSHong Zhang } 46885a7d977cSHong Zhang /* off-diagonal portion of A */ 46895a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 46905a7d977cSHong Zhang cj[k] = cmap[*bj++]; 46915a7d977cSHong Zhang ca[k++] = *ba++; 46925a7d977cSHong Zhang } 469325616d81SHong Zhang } 4694dea91ad1SHong Zhang /* put together the new matrix */ 4695d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 4696dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4697dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 4698dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 4699e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 4700e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 4701dea91ad1SHong Zhang mat->nonew = 0; 47025a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 47035a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 4704a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 47055a7d977cSHong Zhang for (i=0; i<am; i++) { 47065a7d977cSHong Zhang /* off-diagonal portion of A */ 47075a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47085a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47095a7d977cSHong Zhang col = cmap[*bj]; 47105a7d977cSHong Zhang if (col >= cstart) break; 4711a77337e4SBarry Smith *cam++ = *ba++; bj++; 47125a7d977cSHong Zhang } 47135a7d977cSHong Zhang /* diagonal portion of A */ 4714ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 4715a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 47165a7d977cSHong Zhang /* off-diagonal portion of A */ 4717f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 4718a77337e4SBarry Smith *cam++ = *ba++; bj++; 4719f33d1a9aSHong Zhang } 47205a7d977cSHong Zhang } 47215a7d977cSHong Zhang } else { 4722e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 472325616d81SHong Zhang } 472401b7ae99SHong Zhang 47254ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 472625616d81SHong Zhang PetscFunctionReturn(0); 472725616d81SHong Zhang } 472825616d81SHong Zhang 472932fba14fSHong Zhang #undef __FUNCT__ 473032fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed" 473132fba14fSHong Zhang /*@C 473232fba14fSHong Zhang MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns 473332fba14fSHong Zhang 473432fba14fSHong Zhang Not Collective 473532fba14fSHong Zhang 473632fba14fSHong Zhang Input Parameters: 473732fba14fSHong Zhang + A - the matrix 473832fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 473932fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 474032fba14fSHong Zhang 474132fba14fSHong Zhang Output Parameter: 474232fba14fSHong Zhang . A_loc - the local sequential matrix generated 474332fba14fSHong Zhang 474432fba14fSHong Zhang Level: developer 474532fba14fSHong Zhang 474632fba14fSHong Zhang @*/ 4747be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 474832fba14fSHong Zhang { 474932fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 475032fba14fSHong Zhang PetscErrorCode ierr; 475132fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 475232fba14fSHong Zhang IS isrowa,iscola; 475332fba14fSHong Zhang Mat *aloc; 475432fba14fSHong Zhang 475532fba14fSHong Zhang PetscFunctionBegin; 47564ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 475732fba14fSHong Zhang if (!row){ 4758d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 475932fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 476032fba14fSHong Zhang } else { 476132fba14fSHong Zhang isrowa = *row; 476232fba14fSHong Zhang } 476332fba14fSHong Zhang if (!col){ 4764d0f46423SBarry Smith start = A->cmap->rstart; 476532fba14fSHong Zhang cmap = a->garray; 4766d0f46423SBarry Smith nzA = a->A->cmap->n; 4767d0f46423SBarry Smith nzB = a->B->cmap->n; 476832fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 476932fba14fSHong Zhang ncols = 0; 477032fba14fSHong Zhang for (i=0; i<nzB; i++) { 477132fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 477232fba14fSHong Zhang else break; 477332fba14fSHong Zhang } 477432fba14fSHong Zhang imark = i; 477532fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 477632fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 477732fba14fSHong Zhang ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);CHKERRQ(ierr); 477832fba14fSHong Zhang ierr = PetscFree(idx);CHKERRQ(ierr); 477932fba14fSHong Zhang } else { 478032fba14fSHong Zhang iscola = *col; 478132fba14fSHong Zhang } 478232fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 478332fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 478432fba14fSHong Zhang aloc[0] = *A_loc; 478532fba14fSHong Zhang } 478632fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 478732fba14fSHong Zhang *A_loc = aloc[0]; 478832fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 478932fba14fSHong Zhang if (!row){ 479032fba14fSHong Zhang ierr = ISDestroy(isrowa);CHKERRQ(ierr); 479132fba14fSHong Zhang } 479232fba14fSHong Zhang if (!col){ 479332fba14fSHong Zhang ierr = ISDestroy(iscola);CHKERRQ(ierr); 479432fba14fSHong Zhang } 47954ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 479632fba14fSHong Zhang PetscFunctionReturn(0); 479732fba14fSHong Zhang } 479832fba14fSHong Zhang 479925616d81SHong Zhang #undef __FUNCT__ 480025616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 480125616d81SHong Zhang /*@C 480232fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 480325616d81SHong Zhang 480425616d81SHong Zhang Collective on Mat 480525616d81SHong Zhang 480625616d81SHong Zhang Input Parameters: 4807e240928fSHong Zhang + A,B - the matrices in mpiaij format 480825616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 480925616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 481025616d81SHong Zhang 481125616d81SHong Zhang Output Parameter: 481225616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 4813d0f46423SBarry Smith . brstart - row index of B_seq from which next B->rmap->n rows are taken from B's local rows 481425616d81SHong Zhang - B_seq - the sequential matrix generated 481525616d81SHong Zhang 481625616d81SHong Zhang Level: developer 481725616d81SHong Zhang 481825616d81SHong Zhang @*/ 4819be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq) 482025616d81SHong Zhang { 4821899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 482225616d81SHong Zhang PetscErrorCode ierr; 4823b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 482425616d81SHong Zhang IS isrowb,iscolb; 482525616d81SHong Zhang Mat *bseq; 482625616d81SHong Zhang 482725616d81SHong Zhang PetscFunctionBegin; 4828d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4829e32f2f54SBarry Smith SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend); 483025616d81SHong Zhang } 48314ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 483225616d81SHong Zhang 483325616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4834d0f46423SBarry Smith start = A->cmap->rstart; 483525616d81SHong Zhang cmap = a->garray; 4836d0f46423SBarry Smith nzA = a->A->cmap->n; 4837d0f46423SBarry Smith nzB = a->B->cmap->n; 4838b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 483925616d81SHong Zhang ncols = 0; 48400390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 484125616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 484225616d81SHong Zhang else break; 484325616d81SHong Zhang } 484425616d81SHong Zhang imark = i; 48450390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 48460390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 484725616d81SHong Zhang ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);CHKERRQ(ierr); 484825616d81SHong Zhang ierr = PetscFree(idx);CHKERRQ(ierr); 484925616d81SHong Zhang *brstart = imark; 4850d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 485125616d81SHong Zhang } else { 4852e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 485325616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 485425616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 485525616d81SHong Zhang bseq[0] = *B_seq; 485625616d81SHong Zhang } 485725616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 485825616d81SHong Zhang *B_seq = bseq[0]; 485925616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 486025616d81SHong Zhang if (!rowb){ 486125616d81SHong Zhang ierr = ISDestroy(isrowb);CHKERRQ(ierr); 486225616d81SHong Zhang } else { 486325616d81SHong Zhang *rowb = isrowb; 486425616d81SHong Zhang } 486525616d81SHong Zhang if (!colb){ 486625616d81SHong Zhang ierr = ISDestroy(iscolb);CHKERRQ(ierr); 486725616d81SHong Zhang } else { 486825616d81SHong Zhang *colb = iscolb; 486925616d81SHong Zhang } 48704ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 487125616d81SHong Zhang PetscFunctionReturn(0); 487225616d81SHong Zhang } 4873429d309bSHong Zhang 4874a61c8c0fSHong Zhang #undef __FUNCT__ 4875a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols" 4876429d309bSHong Zhang /*@C 4877429d309bSHong Zhang MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 487801b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 4879429d309bSHong Zhang 4880429d309bSHong Zhang Collective on Mat 4881429d309bSHong Zhang 4882429d309bSHong Zhang Input Parameters: 4883429d309bSHong Zhang + A,B - the matrices in mpiaij format 488487025532SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 488587025532SHong Zhang . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 48861d79065fSBarry Smith . startsj_r - similar to startsj for receives 488787025532SHong Zhang - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 4888429d309bSHong Zhang 4889429d309bSHong Zhang Output Parameter: 489087025532SHong Zhang + B_oth - the sequential matrix generated 4891429d309bSHong Zhang 4892429d309bSHong Zhang Level: developer 4893429d309bSHong Zhang 4894429d309bSHong Zhang @*/ 48951d79065fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 4896429d309bSHong Zhang { 4897a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 4898429d309bSHong Zhang PetscErrorCode ierr; 4899899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 490087025532SHong Zhang Mat_SeqAIJ *b_oth; 4901a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 49027adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 49037adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 4904d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 4905dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 4906dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 4907e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 4908910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 490987025532SHong Zhang MPI_Status *sstatus,rstatus; 4910aa5bb8c0SSatish Balay PetscMPIInt jj; 4911e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 4912ba8c8a56SBarry Smith PetscScalar *vals; 4913429d309bSHong Zhang 4914429d309bSHong Zhang PetscFunctionBegin; 4915d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4916e32f2f54SBarry Smith SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%d, %d) != (%d,%d)",A->cmap->rstart,A->cmap->rend,B->rmap->rstart,B->rmap->rend); 4917429d309bSHong Zhang } 49184ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4919a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 4920a6b2eed2SHong Zhang 4921a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 4922a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 4923e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 4924e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 4925a6b2eed2SHong Zhang nrecvs = gen_from->n; 4926a6b2eed2SHong Zhang nsends = gen_to->n; 4927d7ee0231SBarry Smith 4928d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 4929a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 4930a6b2eed2SHong Zhang sstarts = gen_to->starts; 4931a6b2eed2SHong Zhang sprocs = gen_to->procs; 4932a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 4933e42f35eeSHong Zhang sbs = gen_to->bs; 4934e42f35eeSHong Zhang rstarts = gen_from->starts; 4935e42f35eeSHong Zhang rprocs = gen_from->procs; 4936e42f35eeSHong Zhang rbs = gen_from->bs; 4937429d309bSHong Zhang 4938dea91ad1SHong Zhang if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 4939429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4940a6b2eed2SHong Zhang /* i-array */ 4941a6b2eed2SHong Zhang /*---------*/ 4942a6b2eed2SHong Zhang /* post receives */ 4943a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 4944e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 4945e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 494687025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 4947429d309bSHong Zhang } 4948a6b2eed2SHong Zhang 4949a6b2eed2SHong Zhang /* pack the outgoing message */ 49501d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 4951a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 4952a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 4953a6b2eed2SHong Zhang k = 0; 4954a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 4955e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 4956e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 495787025532SHong Zhang for (j=0; j<nrows; j++) { 4958d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 4959e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 4960e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 4961e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 4962e42f35eeSHong Zhang len += ncols; 4963e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 4964e42f35eeSHong Zhang } 4965a6b2eed2SHong Zhang k++; 4966429d309bSHong Zhang } 4967e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 4968dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 4969429d309bSHong Zhang } 497087025532SHong Zhang /* recvs and sends of i-array are completed */ 497187025532SHong Zhang i = nrecvs; 497287025532SHong Zhang while (i--) { 4973aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 497487025532SHong Zhang } 49750c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 4976e42f35eeSHong Zhang 4977a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 4978a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 4979a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 4980a6b2eed2SHong Zhang 498187025532SHong Zhang /* create i-array of B_oth */ 498287025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 498387025532SHong Zhang b_othi[0] = 0; 4984a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 4985a6b2eed2SHong Zhang k = 0; 4986a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 4987fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 4988e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 498987025532SHong Zhang for (j=0; j<nrows; j++) { 499087025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 4991a6b2eed2SHong Zhang len += rowlen[j]; k++; 4992a6b2eed2SHong Zhang } 4993dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 4994a6b2eed2SHong Zhang } 4995a6b2eed2SHong Zhang 499687025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 499787025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 4998dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 4999a6b2eed2SHong Zhang 500087025532SHong Zhang /* j-array */ 500187025532SHong Zhang /*---------*/ 5002a6b2eed2SHong Zhang /* post receives of j-array */ 5003a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 500487025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 500587025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5006a6b2eed2SHong Zhang } 5007e42f35eeSHong Zhang 5008e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5009a6b2eed2SHong Zhang k = 0; 5010a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5011e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5012a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 501387025532SHong Zhang for (j=0; j<nrows; j++) { 5014d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5015e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5016e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5017a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 5018a6b2eed2SHong Zhang *bufJ++ = cols[l]; 501987025532SHong Zhang } 5020e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5021e42f35eeSHong Zhang } 502287025532SHong Zhang } 502387025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 502487025532SHong Zhang } 502587025532SHong Zhang 502687025532SHong Zhang /* recvs and sends of j-array are completed */ 502787025532SHong Zhang i = nrecvs; 502887025532SHong Zhang while (i--) { 5029aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 503087025532SHong Zhang } 50310c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 503287025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 503387025532SHong Zhang sstartsj = *startsj; 50341d79065fSBarry Smith rstartsj = *startsj_r; 503587025532SHong Zhang bufa = *bufa_ptr; 503687025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 503787025532SHong Zhang b_otha = b_oth->a; 503887025532SHong Zhang } else { 5039e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 504087025532SHong Zhang } 504187025532SHong Zhang 504287025532SHong Zhang /* a-array */ 504387025532SHong Zhang /*---------*/ 504487025532SHong Zhang /* post receives of a-array */ 504587025532SHong Zhang for (i=0; i<nrecvs; i++){ 504687025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 504787025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 504887025532SHong Zhang } 5049e42f35eeSHong Zhang 5050e42f35eeSHong Zhang /* pack the outgoing message a-array */ 505187025532SHong Zhang k = 0; 505287025532SHong Zhang for (i=0; i<nsends; i++){ 5053e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 505487025532SHong Zhang bufA = bufa+sstartsj[i]; 505587025532SHong Zhang for (j=0; j<nrows; j++) { 5056d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5057e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5058e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 505987025532SHong Zhang for (l=0; l<ncols; l++){ 5060a6b2eed2SHong Zhang *bufA++ = vals[l]; 5061a6b2eed2SHong Zhang } 5062e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 5063e42f35eeSHong Zhang } 5064a6b2eed2SHong Zhang } 506587025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5066a6b2eed2SHong Zhang } 506787025532SHong Zhang /* recvs and sends of a-array are completed */ 506887025532SHong Zhang i = nrecvs; 506987025532SHong Zhang while (i--) { 5070aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 507187025532SHong Zhang } 50720c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5073d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5074a6b2eed2SHong Zhang 507587025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5076a6b2eed2SHong Zhang /* put together the new matrix */ 5077d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5078a6b2eed2SHong Zhang 5079a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5080a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 508187025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 5082e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5083e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 508487025532SHong Zhang b_oth->nonew = 0; 5085a6b2eed2SHong Zhang 5086a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5087dea91ad1SHong Zhang if (!startsj || !bufa_ptr){ 50881d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5089dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5090dea91ad1SHong Zhang } else { 509187025532SHong Zhang *startsj = sstartsj; 50921d79065fSBarry Smith *startsj_r = rstartsj; 509387025532SHong Zhang *bufa_ptr = bufa; 509487025532SHong Zhang } 5095dea91ad1SHong Zhang } 50964ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5097429d309bSHong Zhang PetscFunctionReturn(0); 5098429d309bSHong Zhang } 5099ccd8e176SBarry Smith 510043eb5e2fSMatthew Knepley #undef __FUNCT__ 510143eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 510243eb5e2fSMatthew Knepley /*@C 510343eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 510443eb5e2fSMatthew Knepley 510543eb5e2fSMatthew Knepley Not Collective 510643eb5e2fSMatthew Knepley 510743eb5e2fSMatthew Knepley Input Parameters: 510843eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 510943eb5e2fSMatthew Knepley 511043eb5e2fSMatthew Knepley Output Parameter: 511143eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 511243eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 511343eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 511443eb5e2fSMatthew Knepley 511543eb5e2fSMatthew Knepley Level: developer 511643eb5e2fSMatthew Knepley 511743eb5e2fSMatthew Knepley @*/ 511843eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 511943eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 512043eb5e2fSMatthew Knepley #else 512143eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 512243eb5e2fSMatthew Knepley #endif 512343eb5e2fSMatthew Knepley { 512443eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 512543eb5e2fSMatthew Knepley 512643eb5e2fSMatthew Knepley PetscFunctionBegin; 51270700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5128e414b56bSJed Brown PetscValidPointer(lvec, 2); 5129e414b56bSJed Brown PetscValidPointer(colmap, 3); 5130e414b56bSJed Brown PetscValidPointer(multScatter, 4); 513143eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 513243eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 513343eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 513443eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 513543eb5e2fSMatthew Knepley PetscFunctionReturn(0); 513643eb5e2fSMatthew Knepley } 513743eb5e2fSMatthew Knepley 513817667f90SBarry Smith EXTERN_C_BEGIN 51398cf70c4bSSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPICRL(Mat,const MatType,MatReuse,Mat*); 51408cf70c4bSSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPICSRPERM(Mat,const MatType,MatReuse,Mat*); 5141c4688eafSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*); 514217667f90SBarry Smith EXTERN_C_END 514317667f90SBarry Smith 5144fc4dec0aSBarry Smith #undef __FUNCT__ 5145fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5146fc4dec0aSBarry Smith /* 5147fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5148fc4dec0aSBarry Smith 5149fc4dec0aSBarry Smith n p p 5150fc4dec0aSBarry Smith ( ) ( ) ( ) 5151fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5152fc4dec0aSBarry Smith ( ) ( ) ( ) 5153fc4dec0aSBarry Smith 5154fc4dec0aSBarry Smith */ 5155fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5156fc4dec0aSBarry Smith { 5157fc4dec0aSBarry Smith PetscErrorCode ierr; 5158fc4dec0aSBarry Smith Mat At,Bt,Ct; 5159fc4dec0aSBarry Smith 5160fc4dec0aSBarry Smith PetscFunctionBegin; 5161fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5162fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5163fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 5164fc4dec0aSBarry Smith ierr = MatDestroy(At);CHKERRQ(ierr); 5165fc4dec0aSBarry Smith ierr = MatDestroy(Bt);CHKERRQ(ierr); 5166fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 5167e5e4356aSBarry Smith ierr = MatDestroy(Ct);CHKERRQ(ierr); 5168fc4dec0aSBarry Smith PetscFunctionReturn(0); 5169fc4dec0aSBarry Smith } 5170fc4dec0aSBarry Smith 5171fc4dec0aSBarry Smith #undef __FUNCT__ 5172fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5173fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5174fc4dec0aSBarry Smith { 5175fc4dec0aSBarry Smith PetscErrorCode ierr; 5176d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5177fc4dec0aSBarry Smith Mat Cmat; 5178fc4dec0aSBarry Smith 5179fc4dec0aSBarry Smith PetscFunctionBegin; 5180e32f2f54SBarry Smith if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n); 518139804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5182fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5183fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5184fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 518538556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 518638556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5187fc4dec0aSBarry Smith *C = Cmat; 5188fc4dec0aSBarry Smith PetscFunctionReturn(0); 5189fc4dec0aSBarry Smith } 5190fc4dec0aSBarry Smith 5191fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5192fc4dec0aSBarry Smith #undef __FUNCT__ 5193fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5194fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5195fc4dec0aSBarry Smith { 5196fc4dec0aSBarry Smith PetscErrorCode ierr; 5197fc4dec0aSBarry Smith 5198fc4dec0aSBarry Smith PetscFunctionBegin; 5199fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5200fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5201fc4dec0aSBarry Smith } 5202fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5203fc4dec0aSBarry Smith PetscFunctionReturn(0); 5204fc4dec0aSBarry Smith } 5205fc4dec0aSBarry Smith 52065c9eb25fSBarry Smith EXTERN_C_BEGIN 5207611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5208bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5209611f576cSBarry Smith #endif 52103bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 52113bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 52123bf14a46SMatthew Knepley #endif 5213611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 52145c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5215611f576cSBarry Smith #endif 5216611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 52175c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*); 5218611f576cSBarry Smith #endif 52195c9eb25fSBarry Smith EXTERN_C_END 52205c9eb25fSBarry Smith 5221ccd8e176SBarry Smith /*MC 5222ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5223ccd8e176SBarry Smith 5224ccd8e176SBarry Smith Options Database Keys: 5225ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5226ccd8e176SBarry Smith 5227ccd8e176SBarry Smith Level: beginner 5228ccd8e176SBarry Smith 5229175b88e8SBarry Smith .seealso: MatCreateMPIAIJ() 5230ccd8e176SBarry Smith M*/ 5231ccd8e176SBarry Smith 5232ccd8e176SBarry Smith EXTERN_C_BEGIN 5233ccd8e176SBarry Smith #undef __FUNCT__ 5234ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 5235be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B) 5236ccd8e176SBarry Smith { 5237ccd8e176SBarry Smith Mat_MPIAIJ *b; 5238ccd8e176SBarry Smith PetscErrorCode ierr; 5239ccd8e176SBarry Smith PetscMPIInt size; 5240ccd8e176SBarry Smith 5241ccd8e176SBarry Smith PetscFunctionBegin; 52427adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 5243ccd8e176SBarry Smith 524438f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5245ccd8e176SBarry Smith B->data = (void*)b; 5246ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5247d0f46423SBarry Smith B->rmap->bs = 1; 5248ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5249ccd8e176SBarry Smith B->mapping = 0; 5250ccd8e176SBarry Smith 5251ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5252ccd8e176SBarry Smith b->size = size; 52537adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5254ccd8e176SBarry Smith 5255ccd8e176SBarry Smith /* build cache for off array entries formed */ 52567adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5257ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5258ccd8e176SBarry Smith b->colmap = 0; 5259ccd8e176SBarry Smith b->garray = 0; 5260ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5261ccd8e176SBarry Smith 5262ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5263ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5264ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5265ccd8e176SBarry Smith 5266ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5267ccd8e176SBarry Smith b->rowindices = 0; 5268ccd8e176SBarry Smith b->rowvalues = 0; 5269ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5270ccd8e176SBarry Smith 5271611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 5272ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 52735c9eb25fSBarry Smith "MatGetFactor_mpiaij_spooles", 52745c9eb25fSBarry Smith MatGetFactor_mpiaij_spooles);CHKERRQ(ierr); 5275611f576cSBarry Smith #endif 5276611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5277ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5278bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5279bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5280611f576cSBarry Smith #endif 52813bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5282ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 52833bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 52843bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 52853bf14a46SMatthew Knepley #endif 5286611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5287ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 52885c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 52895c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5290611f576cSBarry Smith #endif 5291ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5292ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5293ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5294ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5295ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5296ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5297ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5298ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5299ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5300ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5301ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5302ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5303ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5304ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5305ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5306ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5307ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5308ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5309ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5310ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5311ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 531217667f90SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicsrperm_C", 531317667f90SBarry Smith "MatConvert_MPIAIJ_MPICSRPERM", 531417667f90SBarry Smith MatConvert_MPIAIJ_MPICSRPERM);CHKERRQ(ierr); 531517667f90SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicrl_C", 531617667f90SBarry Smith "MatConvert_MPIAIJ_MPICRL", 531717667f90SBarry Smith MatConvert_MPIAIJ_MPICRL);CHKERRQ(ierr); 5318471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5319471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5320471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5321fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5322fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5323fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5324fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5325fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5326fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5327fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5328fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5329fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 533017667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5331ccd8e176SBarry Smith PetscFunctionReturn(0); 5332ccd8e176SBarry Smith } 5333ccd8e176SBarry Smith EXTERN_C_END 533481824310SBarry Smith 533503bfb495SBarry Smith #undef __FUNCT__ 533603bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 533758d36128SBarry Smith /*@ 533803bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 533903bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 534003bfb495SBarry Smith 534103bfb495SBarry Smith Collective on MPI_Comm 534203bfb495SBarry Smith 534303bfb495SBarry Smith Input Parameters: 534403bfb495SBarry Smith + comm - MPI communicator 534503bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 534603bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 534703bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 534803bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 534903bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 535003bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 535103bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 535203bfb495SBarry Smith . j - column indices 535303bfb495SBarry Smith . a - matrix values 535403bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 535503bfb495SBarry Smith . oj - column indices 535603bfb495SBarry Smith - oa - matrix values 535703bfb495SBarry Smith 535803bfb495SBarry Smith Output Parameter: 535903bfb495SBarry Smith . mat - the matrix 536003bfb495SBarry Smith 536103bfb495SBarry Smith Level: advanced 536203bfb495SBarry Smith 536303bfb495SBarry Smith Notes: 536403bfb495SBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. 536503bfb495SBarry Smith 536603bfb495SBarry Smith The i and j indices are 0 based 536703bfb495SBarry Smith 536803bfb495SBarry Smith See MatCreateMPIAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 536903bfb495SBarry Smith 53707b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 53717b55108eSBarry Smith 53727b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 537303bfb495SBarry Smith 537403bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 537503bfb495SBarry Smith 537603bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 53778d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithArrays() 537803bfb495SBarry Smith @*/ 53798d7a6e47SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 538003bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 538103bfb495SBarry Smith { 538203bfb495SBarry Smith PetscErrorCode ierr; 538303bfb495SBarry Smith Mat_MPIAIJ *maij; 538403bfb495SBarry Smith 538503bfb495SBarry Smith PetscFunctionBegin; 5386e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 538703bfb495SBarry Smith if (i[0]) { 5388e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 538903bfb495SBarry Smith } 539003bfb495SBarry Smith if (oi[0]) { 5391e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 539203bfb495SBarry Smith } 539303bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 539403bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 539503bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 539603bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 53978d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 53988d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 539903bfb495SBarry Smith 540026283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->rmap,1);CHKERRQ(ierr); 540126283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->cmap,1);CHKERRQ(ierr); 540226283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 540326283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 540403bfb495SBarry Smith 540503bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5406d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 540703bfb495SBarry Smith 54088d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54098d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54108d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54118d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54128d7a6e47SBarry Smith 541303bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 541403bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 541503bfb495SBarry Smith PetscFunctionReturn(0); 541603bfb495SBarry Smith } 541703bfb495SBarry Smith 541881824310SBarry Smith /* 541981824310SBarry Smith Special version for direct calls from Fortran 542081824310SBarry Smith */ 542181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 542281824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 542381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 542481824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 542581824310SBarry Smith #endif 542681824310SBarry Smith 542781824310SBarry Smith /* Change these macros so can be used in void function */ 542881824310SBarry Smith #undef CHKERRQ 5429e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 543081824310SBarry Smith #undef SETERRQ2 5431e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 543281824310SBarry Smith #undef SETERRQ 5433e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 543481824310SBarry Smith 543581824310SBarry Smith EXTERN_C_BEGIN 543681824310SBarry Smith #undef __FUNCT__ 543781824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 54381f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 543981824310SBarry Smith { 544081824310SBarry Smith Mat mat = *mmat; 544181824310SBarry Smith PetscInt m = *mm, n = *mn; 544281824310SBarry Smith InsertMode addv = *maddv; 544381824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 544481824310SBarry Smith PetscScalar value; 544581824310SBarry Smith PetscErrorCode ierr; 5446899cda47SBarry Smith 5447d9e2c085SLisandro Dalcin ierr = MatPreallocated(mat);CHKERRQ(ierr); 544881824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 544981824310SBarry Smith mat->insertmode = addv; 545081824310SBarry Smith } 545181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 545281824310SBarry Smith else if (mat->insertmode != addv) { 5453e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 545481824310SBarry Smith } 545581824310SBarry Smith #endif 545681824310SBarry Smith { 5457d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5458d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 545981824310SBarry Smith PetscTruth roworiented = aij->roworiented; 546081824310SBarry Smith 546181824310SBarry Smith /* Some Variables required in the macro */ 546281824310SBarry Smith Mat A = aij->A; 546381824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 546481824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5465dd6ea824SBarry Smith MatScalar *aa = a->a; 546681824310SBarry Smith PetscTruth ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 546781824310SBarry Smith Mat B = aij->B; 546881824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5469d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5470dd6ea824SBarry Smith MatScalar *ba = b->a; 547181824310SBarry Smith 547281824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 547381824310SBarry Smith PetscInt nonew = a->nonew; 5474dd6ea824SBarry Smith MatScalar *ap1,*ap2; 547581824310SBarry Smith 547681824310SBarry Smith PetscFunctionBegin; 547781824310SBarry Smith for (i=0; i<m; i++) { 547881824310SBarry Smith if (im[i] < 0) continue; 547981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5480e32f2f54SBarry Smith if (im[i] >= mat->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap->N-1); 548181824310SBarry Smith #endif 548281824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 548381824310SBarry Smith row = im[i] - rstart; 548481824310SBarry Smith lastcol1 = -1; 548581824310SBarry Smith rp1 = aj + ai[row]; 548681824310SBarry Smith ap1 = aa + ai[row]; 548781824310SBarry Smith rmax1 = aimax[row]; 548881824310SBarry Smith nrow1 = ailen[row]; 548981824310SBarry Smith low1 = 0; 549081824310SBarry Smith high1 = nrow1; 549181824310SBarry Smith lastcol2 = -1; 549281824310SBarry Smith rp2 = bj + bi[row]; 549381824310SBarry Smith ap2 = ba + bi[row]; 549481824310SBarry Smith rmax2 = bimax[row]; 549581824310SBarry Smith nrow2 = bilen[row]; 549681824310SBarry Smith low2 = 0; 549781824310SBarry Smith high2 = nrow2; 549881824310SBarry Smith 549981824310SBarry Smith for (j=0; j<n; j++) { 550081824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 550181824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 550281824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 550381824310SBarry Smith col = in[j] - cstart; 550481824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 550581824310SBarry Smith } else if (in[j] < 0) continue; 550681824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5507cb9801acSJed Brown else if (in[j] >= mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap->N-1); 550881824310SBarry Smith #endif 550981824310SBarry Smith else { 551081824310SBarry Smith if (mat->was_assembled) { 551181824310SBarry Smith if (!aij->colmap) { 551281824310SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 551381824310SBarry Smith } 551481824310SBarry Smith #if defined (PETSC_USE_CTABLE) 551581824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 551681824310SBarry Smith col--; 551781824310SBarry Smith #else 551881824310SBarry Smith col = aij->colmap[in[j]] - 1; 551981824310SBarry Smith #endif 552081824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 552181824310SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 552281824310SBarry Smith col = in[j]; 552381824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 552481824310SBarry Smith B = aij->B; 552581824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 552681824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 552781824310SBarry Smith rp2 = bj + bi[row]; 552881824310SBarry Smith ap2 = ba + bi[row]; 552981824310SBarry Smith rmax2 = bimax[row]; 553081824310SBarry Smith nrow2 = bilen[row]; 553181824310SBarry Smith low2 = 0; 553281824310SBarry Smith high2 = nrow2; 5533d0f46423SBarry Smith bm = aij->B->rmap->n; 553481824310SBarry Smith ba = b->a; 553581824310SBarry Smith } 553681824310SBarry Smith } else col = in[j]; 553781824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 553881824310SBarry Smith } 553981824310SBarry Smith } 554081824310SBarry Smith } else { 554181824310SBarry Smith if (!aij->donotstash) { 554281824310SBarry Smith if (roworiented) { 55433b024144SHong Zhang ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscTruth)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 554481824310SBarry Smith } else { 55453b024144SHong Zhang ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscTruth)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 554681824310SBarry Smith } 554781824310SBarry Smith } 554881824310SBarry Smith } 554981824310SBarry Smith }} 555081824310SBarry Smith PetscFunctionReturnVoid(); 555181824310SBarry Smith } 555281824310SBarry Smith EXTERN_C_END 555303bfb495SBarry Smith 5554