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; 23ace3abfcSBarry Smith PetscBool 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; 310ace3abfcSBarry Smith PetscBool 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; 317ace3abfcSBarry Smith PetscBool 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 { 3934cb17eb5SBarry Smith if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]); 39490f02eecSBarry Smith if (!aij->donotstash) { 395d36fbae8SSatish Balay if (roworiented) { 396ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 397d36fbae8SSatish Balay } else { 398ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 3994b0e389bSBarry Smith } 4001eb62cbbSBarry Smith } 4018a729477SBarry Smith } 40290f02eecSBarry Smith } 4033a40ed3dSBarry Smith PetscFunctionReturn(0); 4048a729477SBarry Smith } 4058a729477SBarry Smith 4064a2ae208SSatish Balay #undef __FUNCT__ 4074a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 408b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 409b49de8d1SLois Curfman McInnes { 410b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 411dfbe8321SBarry Smith PetscErrorCode ierr; 412d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 413d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 414b49de8d1SLois Curfman McInnes 4153a40ed3dSBarry Smith PetscFunctionBegin; 416b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 417e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 418e32f2f54SBarry 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); 419b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 420b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 421b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 422e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 423e32f2f54SBarry 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); 424b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 425b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 426b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 427fa852ad4SSatish Balay } else { 428905e6a2fSBarry Smith if (!aij->colmap) { 429905e6a2fSBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 430905e6a2fSBarry Smith } 431aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 4320f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 433fa46199cSSatish Balay col --; 434b1fc9764SSatish Balay #else 435905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 436b1fc9764SSatish Balay #endif 437e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 438d9d09a02SSatish Balay else { 439b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 440b49de8d1SLois Curfman McInnes } 441b49de8d1SLois Curfman McInnes } 442b49de8d1SLois Curfman McInnes } 443a8c6a408SBarry Smith } else { 444e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 445b49de8d1SLois Curfman McInnes } 446b49de8d1SLois Curfman McInnes } 4473a40ed3dSBarry Smith PetscFunctionReturn(0); 448b49de8d1SLois Curfman McInnes } 449bc5ccf88SSatish Balay 450bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 451bd0c2dcbSBarry Smith 4524a2ae208SSatish Balay #undef __FUNCT__ 4534a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 454dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 455bc5ccf88SSatish Balay { 456bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 457dfbe8321SBarry Smith PetscErrorCode ierr; 458b1d57f15SBarry Smith PetscInt nstash,reallocs; 459bc5ccf88SSatish Balay InsertMode addv; 460bc5ccf88SSatish Balay 461bc5ccf88SSatish Balay PetscFunctionBegin; 4624cb17eb5SBarry Smith if (aij->donotstash || mat->nooffprocentries) { 463bc5ccf88SSatish Balay PetscFunctionReturn(0); 464bc5ccf88SSatish Balay } 465bc5ccf88SSatish Balay 466bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 4677adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr); 468e7e72b3dSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 469bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 470bc5ccf88SSatish Balay 471d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 4728798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 473ae15b995SBarry Smith ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 474bc5ccf88SSatish Balay PetscFunctionReturn(0); 475bc5ccf88SSatish Balay } 476bc5ccf88SSatish Balay 4774a2ae208SSatish Balay #undef __FUNCT__ 4784a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 479dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 480bc5ccf88SSatish Balay { 481bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 48291c97fd4SSatish Balay Mat_SeqAIJ *a=(Mat_SeqAIJ *)aij->A->data; 4836849ba73SBarry Smith PetscErrorCode ierr; 484b1d57f15SBarry Smith PetscMPIInt n; 485b1d57f15SBarry Smith PetscInt i,j,rstart,ncols,flg; 486e44c0bd4SBarry Smith PetscInt *row,*col; 487ace3abfcSBarry Smith PetscBool other_disassembled; 48887828ca2SBarry Smith PetscScalar *val; 489bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 490bc5ccf88SSatish Balay 49191c97fd4SSatish Balay /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */ 492bc5ccf88SSatish Balay PetscFunctionBegin; 4934cb17eb5SBarry Smith if (!aij->donotstash && !mat->nooffprocentries) { 494a2d1c673SSatish Balay while (1) { 4958798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 496a2d1c673SSatish Balay if (!flg) break; 497a2d1c673SSatish Balay 498bc5ccf88SSatish Balay for (i=0; i<n;) { 499bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 500bc5ccf88SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 501bc5ccf88SSatish Balay if (j < n) ncols = j-i; 502bc5ccf88SSatish Balay else ncols = n-i; 503bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 504bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 505bc5ccf88SSatish Balay i = j; 506bc5ccf88SSatish Balay } 507bc5ccf88SSatish Balay } 5088798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 509bc5ccf88SSatish Balay } 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); 529*4e35b6f3SSatish Balay ierr = MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr); 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" 5602b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 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; 57397b48c8fSBarry Smith const PetscScalar *xx; 57497b48c8fSBarry Smith PetscScalar *bb; 5756543fbbaSBarry Smith #if defined(PETSC_DEBUG) 576ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 5776543fbbaSBarry Smith #endif 5781eb62cbbSBarry Smith 5793a40ed3dSBarry Smith PetscFunctionBegin; 5801eb62cbbSBarry Smith /* first count number of contributors to each processor */ 581b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 582b1d57f15SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 583b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 5846543fbbaSBarry Smith j = 0; 5851eb62cbbSBarry Smith for (i=0; i<N; i++) { 5866543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 5876543fbbaSBarry Smith lastidx = idx; 5886543fbbaSBarry Smith for (; j<size; j++) { 5891eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 5906543fbbaSBarry Smith nprocs[2*j]++; 5916543fbbaSBarry Smith nprocs[2*j+1] = 1; 5926543fbbaSBarry Smith owner[i] = j; 5936543fbbaSBarry Smith #if defined(PETSC_DEBUG) 5946543fbbaSBarry Smith found = PETSC_TRUE; 5956543fbbaSBarry Smith #endif 5966543fbbaSBarry Smith break; 5971eb62cbbSBarry Smith } 5981eb62cbbSBarry Smith } 5996543fbbaSBarry Smith #if defined(PETSC_DEBUG) 600e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 6016543fbbaSBarry Smith found = PETSC_FALSE; 6026543fbbaSBarry Smith #endif 6031eb62cbbSBarry Smith } 604c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 6051eb62cbbSBarry Smith 6067367270fSBarry Smith if (A->nooffproczerorows) { 6077367270fSBarry Smith if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row"); 6087367270fSBarry Smith nrecvs = nsends; 6097367270fSBarry Smith nmax = N; 6107367270fSBarry Smith } else { 6111eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 612c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 6137367270fSBarry Smith } 6141eb62cbbSBarry Smith 6151eb62cbbSBarry Smith /* post receives: */ 616b1d57f15SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 617b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 6181eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 619b1d57f15SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 6201eb62cbbSBarry Smith } 6211eb62cbbSBarry Smith 6221eb62cbbSBarry Smith /* do sends: 6231eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 6241eb62cbbSBarry Smith the ith processor 6251eb62cbbSBarry Smith */ 626b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 627b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 628b1d57f15SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 6291eb62cbbSBarry Smith starts[0] = 0; 630c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6311eb62cbbSBarry Smith for (i=0; i<N; i++) { 6321eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 6331eb62cbbSBarry Smith } 6341eb62cbbSBarry Smith 6351eb62cbbSBarry Smith starts[0] = 0; 636c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6371eb62cbbSBarry Smith count = 0; 63817699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 639c1dc657dSBarry Smith if (nprocs[2*i+1]) { 640b1d57f15SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 6411eb62cbbSBarry Smith } 6421eb62cbbSBarry Smith } 643606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 6441eb62cbbSBarry Smith 64517699dbbSLois Curfman McInnes base = owners[rank]; 6461eb62cbbSBarry Smith 6471eb62cbbSBarry Smith /* wait on receives */ 6481d79065fSBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 6491eb62cbbSBarry Smith count = nrecvs; slen = 0; 6501eb62cbbSBarry Smith while (count) { 651ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 6521eb62cbbSBarry Smith /* unpack receives into our local space */ 653b1d57f15SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 654d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 655d6dfbf8fSBarry Smith lens[imdex] = n; 6561eb62cbbSBarry Smith slen += n; 6571eb62cbbSBarry Smith count--; 6581eb62cbbSBarry Smith } 659606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 6601eb62cbbSBarry Smith 6611eb62cbbSBarry Smith /* move the data into the send scatter */ 662b1d57f15SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 6631eb62cbbSBarry Smith count = 0; 6641eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 6651eb62cbbSBarry Smith values = rvalues + i*nmax; 6661eb62cbbSBarry Smith for (j=0; j<lens[i]; j++) { 6671eb62cbbSBarry Smith lrows[count++] = values[j] - base; 6681eb62cbbSBarry Smith } 6691eb62cbbSBarry Smith } 670606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 6711d79065fSBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 672606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 673606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 6741eb62cbbSBarry Smith 67597b48c8fSBarry Smith /* fix right hand side if needed */ 67697b48c8fSBarry Smith if (x && b) { 67797b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 67897b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 679564f14d6SBarry Smith for (i=0; i<slen; i++) { 68097b48c8fSBarry Smith bb[lrows[i]] = diag*xx[lrows[i]]; 68197b48c8fSBarry Smith } 68297b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 68397b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 68497b48c8fSBarry Smith } 6856eb55b6aSBarry Smith /* 6866eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 687a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 6886eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 6896eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 6906eb55b6aSBarry Smith 6916eb55b6aSBarry Smith */ 692e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 6932b40b63fSBarry Smith ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr); 694d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 6952b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr); 696f4df32b1SMatthew Knepley } else if (diag != 0.0) { 6972b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 698fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 699e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 700512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 7016525c446SSatish Balay } 702e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 703e2d53e46SBarry Smith row = lrows[i] + rstart; 704f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 705e2d53e46SBarry Smith } 706e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 707e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7086eb55b6aSBarry Smith } else { 7092b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 7106eb55b6aSBarry Smith } 711606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 71272dacd9aSBarry Smith 7131eb62cbbSBarry Smith /* wait on sends */ 7141eb62cbbSBarry Smith if (nsends) { 715b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 716ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 717606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 7181eb62cbbSBarry Smith } 719606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 720606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 7211eb62cbbSBarry Smith 7223a40ed3dSBarry Smith PetscFunctionReturn(0); 7231eb62cbbSBarry Smith } 7241eb62cbbSBarry Smith 7254a2ae208SSatish Balay #undef __FUNCT__ 7269c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 7279c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7289c7c4993SBarry Smith { 7299c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 7309c7c4993SBarry Smith PetscErrorCode ierr; 7319c7c4993SBarry Smith PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 7329c7c4993SBarry Smith PetscInt i,*owners = A->rmap->range; 733564f14d6SBarry Smith PetscInt *nprocs,j,idx,nsends; 7349c7c4993SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 7359c7c4993SBarry Smith PetscInt *rvalues,count,base,slen,*source; 736564f14d6SBarry Smith PetscInt *lens,*lrows,*values,m; 7379c7c4993SBarry Smith MPI_Comm comm = ((PetscObject)A)->comm; 7389c7c4993SBarry Smith MPI_Request *send_waits,*recv_waits; 7399c7c4993SBarry Smith MPI_Status recv_status,*send_status; 7409c7c4993SBarry Smith const PetscScalar *xx; 741564f14d6SBarry Smith PetscScalar *bb,*mask; 742564f14d6SBarry Smith Vec xmask,lmask; 743564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 744564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 745564f14d6SBarry Smith PetscScalar *aa; 7469c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7479c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 7489c7c4993SBarry Smith #endif 7499c7c4993SBarry Smith 7509c7c4993SBarry Smith PetscFunctionBegin; 7519c7c4993SBarry Smith /* first count number of contributors to each processor */ 7529c7c4993SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 7539c7c4993SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 7549c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 7559c7c4993SBarry Smith j = 0; 7569c7c4993SBarry Smith for (i=0; i<N; i++) { 7579c7c4993SBarry Smith if (lastidx > (idx = rows[i])) j = 0; 7589c7c4993SBarry Smith lastidx = idx; 7599c7c4993SBarry Smith for (; j<size; j++) { 7609c7c4993SBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 7619c7c4993SBarry Smith nprocs[2*j]++; 7629c7c4993SBarry Smith nprocs[2*j+1] = 1; 7639c7c4993SBarry Smith owner[i] = j; 7649c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7659c7c4993SBarry Smith found = PETSC_TRUE; 7669c7c4993SBarry Smith #endif 7679c7c4993SBarry Smith break; 7689c7c4993SBarry Smith } 7699c7c4993SBarry Smith } 7709c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7719c7c4993SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 7729c7c4993SBarry Smith found = PETSC_FALSE; 7739c7c4993SBarry Smith #endif 7749c7c4993SBarry Smith } 7759c7c4993SBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 7769c7c4993SBarry Smith 7779c7c4993SBarry Smith /* inform other processors of number of messages and max length*/ 7789c7c4993SBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 7799c7c4993SBarry Smith 7809c7c4993SBarry Smith /* post receives: */ 7819c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 7829c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 7839c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 7849c7c4993SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 7859c7c4993SBarry Smith } 7869c7c4993SBarry Smith 7879c7c4993SBarry Smith /* do sends: 7889c7c4993SBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 7899c7c4993SBarry Smith the ith processor 7909c7c4993SBarry Smith */ 7919c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 7929c7c4993SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 7939c7c4993SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 7949c7c4993SBarry Smith starts[0] = 0; 7959c7c4993SBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 7969c7c4993SBarry Smith for (i=0; i<N; i++) { 7979c7c4993SBarry Smith svalues[starts[owner[i]]++] = rows[i]; 7989c7c4993SBarry Smith } 7999c7c4993SBarry Smith 8009c7c4993SBarry Smith starts[0] = 0; 8019c7c4993SBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 8029c7c4993SBarry Smith count = 0; 8039c7c4993SBarry Smith for (i=0; i<size; i++) { 8049c7c4993SBarry Smith if (nprocs[2*i+1]) { 8059c7c4993SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 8069c7c4993SBarry Smith } 8079c7c4993SBarry Smith } 8089c7c4993SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 8099c7c4993SBarry Smith 8109c7c4993SBarry Smith base = owners[rank]; 8119c7c4993SBarry Smith 8129c7c4993SBarry Smith /* wait on receives */ 8139c7c4993SBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 8149c7c4993SBarry Smith count = nrecvs; slen = 0; 8159c7c4993SBarry Smith while (count) { 8169c7c4993SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 8179c7c4993SBarry Smith /* unpack receives into our local space */ 8189c7c4993SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 8199c7c4993SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 8209c7c4993SBarry Smith lens[imdex] = n; 8219c7c4993SBarry Smith slen += n; 8229c7c4993SBarry Smith count--; 8239c7c4993SBarry Smith } 8249c7c4993SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 8259c7c4993SBarry Smith 8269c7c4993SBarry Smith /* move the data into the send scatter */ 8279c7c4993SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 8289c7c4993SBarry Smith count = 0; 8299c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 8309c7c4993SBarry Smith values = rvalues + i*nmax; 8319c7c4993SBarry Smith for (j=0; j<lens[i]; j++) { 8329c7c4993SBarry Smith lrows[count++] = values[j] - base; 8339c7c4993SBarry Smith } 8349c7c4993SBarry Smith } 8359c7c4993SBarry Smith ierr = PetscFree(rvalues);CHKERRQ(ierr); 8369c7c4993SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 8379c7c4993SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 8389c7c4993SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 839564f14d6SBarry Smith /* lrows are the local rows to be zeroed, slen is the number of local rows */ 8409c7c4993SBarry Smith 841564f14d6SBarry Smith /* zero diagonal part of matrix */ 842564f14d6SBarry Smith ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr); 8439c7c4993SBarry Smith 844564f14d6SBarry Smith /* handle off diagonal part of matrix */ 845564f14d6SBarry Smith ierr = MatGetVecs(A,&xmask,PETSC_NULL);CHKERRQ(ierr); 846564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 847564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 8489c7c4993SBarry Smith for (i=0; i<slen; i++) { 849564f14d6SBarry Smith bb[lrows[i]] = 1; 8509c7c4993SBarry Smith } 851564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 852564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 853564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 854564f14d6SBarry Smith ierr = VecDestroy(xmask);CHKERRQ(ierr); 855564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 856564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 857564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 858564f14d6SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 859564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 860564f14d6SBarry Smith 861564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 862564f14d6SBarry Smith ii = aij->i; 863564f14d6SBarry Smith for (i=0; i<slen; i++) { 864564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 8659c7c4993SBarry Smith } 866564f14d6SBarry Smith 867564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 868564f14d6SBarry Smith if (aij->compressedrow.use){ 869564f14d6SBarry Smith m = aij->compressedrow.nrows; 870564f14d6SBarry Smith ii = aij->compressedrow.i; 871564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 872564f14d6SBarry Smith for (i=0; i<m; i++){ 873564f14d6SBarry Smith n = ii[i+1] - ii[i]; 874564f14d6SBarry Smith aj = aij->j + ii[i]; 875564f14d6SBarry Smith aa = aij->a + ii[i]; 876564f14d6SBarry Smith 877564f14d6SBarry Smith for (j=0; j<n; j++) { 87825266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 879564f14d6SBarry Smith bb[*ridx] -= *aa*xx[*aj]; 880564f14d6SBarry Smith *aa = 0.0; 881564f14d6SBarry Smith } 882564f14d6SBarry Smith aa++; 883564f14d6SBarry Smith aj++; 884564f14d6SBarry Smith } 885564f14d6SBarry Smith ridx++; 886564f14d6SBarry Smith } 887564f14d6SBarry Smith } else { /* do not use compressed row format */ 888564f14d6SBarry Smith m = l->B->rmap->n; 889564f14d6SBarry Smith for (i=0; i<m; i++) { 890564f14d6SBarry Smith n = ii[i+1] - ii[i]; 891564f14d6SBarry Smith aj = aij->j + ii[i]; 892564f14d6SBarry Smith aa = aij->a + ii[i]; 893564f14d6SBarry Smith for (j=0; j<n; j++) { 89425266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 895564f14d6SBarry Smith bb[i] -= *aa*xx[*aj]; 896564f14d6SBarry Smith *aa = 0.0; 897564f14d6SBarry Smith } 898564f14d6SBarry Smith aa++; 899564f14d6SBarry Smith aj++; 900564f14d6SBarry Smith } 901564f14d6SBarry Smith } 902564f14d6SBarry Smith } 903564f14d6SBarry Smith 904564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 905564f14d6SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 906564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 907564f14d6SBarry Smith ierr = VecDestroy(lmask);CHKERRQ(ierr); 9089c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 9099c7c4993SBarry Smith 9109c7c4993SBarry Smith /* wait on sends */ 9119c7c4993SBarry Smith if (nsends) { 9129c7c4993SBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 9139c7c4993SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 9149c7c4993SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 9159c7c4993SBarry Smith } 9169c7c4993SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 9179c7c4993SBarry Smith ierr = PetscFree(svalues);CHKERRQ(ierr); 9189c7c4993SBarry Smith 9199c7c4993SBarry Smith PetscFunctionReturn(0); 9209c7c4993SBarry Smith } 9219c7c4993SBarry Smith 9229c7c4993SBarry Smith #undef __FUNCT__ 9234a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 924dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 9251eb62cbbSBarry Smith { 926416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 927dfbe8321SBarry Smith PetscErrorCode ierr; 928b1d57f15SBarry Smith PetscInt nt; 929416022c9SBarry Smith 9303a40ed3dSBarry Smith PetscFunctionBegin; 931a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 93265e19b50SBarry 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); 933ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 934f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 935ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 936f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 9373a40ed3dSBarry Smith PetscFunctionReturn(0); 9381eb62cbbSBarry Smith } 9391eb62cbbSBarry Smith 9404a2ae208SSatish Balay #undef __FUNCT__ 941bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 942bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 943bd0c2dcbSBarry Smith { 944bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 945bd0c2dcbSBarry Smith PetscErrorCode ierr; 946bd0c2dcbSBarry Smith 947bd0c2dcbSBarry Smith PetscFunctionBegin; 948bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 949bd0c2dcbSBarry Smith PetscFunctionReturn(0); 950bd0c2dcbSBarry Smith } 951bd0c2dcbSBarry Smith 952bd0c2dcbSBarry Smith #undef __FUNCT__ 9534a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 954dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 955da3a660dSBarry Smith { 956416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 957dfbe8321SBarry Smith PetscErrorCode ierr; 9583a40ed3dSBarry Smith 9593a40ed3dSBarry Smith PetscFunctionBegin; 960ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 961f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 962ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 963f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 9643a40ed3dSBarry Smith PetscFunctionReturn(0); 965da3a660dSBarry Smith } 966da3a660dSBarry Smith 9674a2ae208SSatish Balay #undef __FUNCT__ 9684a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 969dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 970da3a660dSBarry Smith { 971416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 972dfbe8321SBarry Smith PetscErrorCode ierr; 973ace3abfcSBarry Smith PetscBool merged; 974da3a660dSBarry Smith 9753a40ed3dSBarry Smith PetscFunctionBegin; 976a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 977da3a660dSBarry Smith /* do nondiagonal part */ 9787c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 979a5ff213dSBarry Smith if (!merged) { 980da3a660dSBarry Smith /* send it on its way */ 981ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 982da3a660dSBarry Smith /* do local part */ 9837c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 984da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 985a5ff213dSBarry Smith /* added in yy until the next line, */ 986ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 987a5ff213dSBarry Smith } else { 988a5ff213dSBarry Smith /* do local part */ 989a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 990a5ff213dSBarry Smith /* send it on its way */ 991ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 992a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 993ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 994a5ff213dSBarry Smith } 9953a40ed3dSBarry Smith PetscFunctionReturn(0); 996da3a660dSBarry Smith } 997da3a660dSBarry Smith 998cd0d46ebSvictorle EXTERN_C_BEGIN 999cd0d46ebSvictorle #undef __FUNCT__ 10005fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 10017087cfbeSBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1002cd0d46ebSvictorle { 10034f423910Svictorle MPI_Comm comm; 1004cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 100566501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1006cd0d46ebSvictorle IS Me,Notme; 10076849ba73SBarry Smith PetscErrorCode ierr; 1008b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1009b1d57f15SBarry Smith PetscMPIInt size; 1010cd0d46ebSvictorle 1011cd0d46ebSvictorle PetscFunctionBegin; 101242e5f5b4Svictorle 101342e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 101466501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 10155485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1016cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 10174f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1018b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1019b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 102042e5f5b4Svictorle 102142e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1022cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1023cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1024b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 1025cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1026cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 102770b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1028268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1029268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 103066501d38Svictorle Aoff = Aoffs[0]; 1031268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 103266501d38Svictorle Boff = Boffs[0]; 10335485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 103466501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 103566501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 103642e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 103742e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 103842e5f5b4Svictorle 1039cd0d46ebSvictorle PetscFunctionReturn(0); 1040cd0d46ebSvictorle } 1041cd0d46ebSvictorle EXTERN_C_END 1042cd0d46ebSvictorle 10434a2ae208SSatish Balay #undef __FUNCT__ 10444a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1045dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1046da3a660dSBarry Smith { 1047416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1048dfbe8321SBarry Smith PetscErrorCode ierr; 1049da3a660dSBarry Smith 10503a40ed3dSBarry Smith PetscFunctionBegin; 1051da3a660dSBarry Smith /* do nondiagonal part */ 10527c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1053da3a660dSBarry Smith /* send it on its way */ 1054ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1055da3a660dSBarry Smith /* do local part */ 10567c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1057a5ff213dSBarry Smith /* receive remote parts */ 1058ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 10593a40ed3dSBarry Smith PetscFunctionReturn(0); 1060da3a660dSBarry Smith } 1061da3a660dSBarry Smith 10621eb62cbbSBarry Smith /* 10631eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 10641eb62cbbSBarry Smith diagonal block 10651eb62cbbSBarry Smith */ 10664a2ae208SSatish Balay #undef __FUNCT__ 10674a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1068dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 10691eb62cbbSBarry Smith { 1070dfbe8321SBarry Smith PetscErrorCode ierr; 1071416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 10723a40ed3dSBarry Smith 10733a40ed3dSBarry Smith PetscFunctionBegin; 1074e7e72b3dSBarry 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"); 1075e7e72b3dSBarry 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"); 10763a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 10773a40ed3dSBarry Smith PetscFunctionReturn(0); 10781eb62cbbSBarry Smith } 10791eb62cbbSBarry Smith 10804a2ae208SSatish Balay #undef __FUNCT__ 10814a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1082f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1083052efed2SBarry Smith { 1084052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1085dfbe8321SBarry Smith PetscErrorCode ierr; 10863a40ed3dSBarry Smith 10873a40ed3dSBarry Smith PetscFunctionBegin; 1088f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1089f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 10903a40ed3dSBarry Smith PetscFunctionReturn(0); 1091052efed2SBarry Smith } 1092052efed2SBarry Smith 10934a2ae208SSatish Balay #undef __FUNCT__ 10944a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1095dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 10961eb62cbbSBarry Smith { 109744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1098dfbe8321SBarry Smith PetscErrorCode ierr; 109983e2fdc7SBarry Smith 11003a40ed3dSBarry Smith PetscFunctionBegin; 1101aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1102d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1103a5a9c739SBarry Smith #endif 11048798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 1105a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);} 1106d88c0aacSHong Zhang if (aij->A){ierr = MatDestroy(aij->A);CHKERRQ(ierr);} 1107d88c0aacSHong Zhang if (aij->B){ierr = MatDestroy(aij->B);CHKERRQ(ierr);} 1108aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 11099c666560SBarry Smith if (aij->colmap) {ierr = PetscTableDestroy(aij->colmap);CHKERRQ(ierr);} 1110b1fc9764SSatish Balay #else 111105b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1112b1fc9764SSatish Balay #endif 111305b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 11147c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 11157c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 111603095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 11178aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1118606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 1119901853e0SKris Buschelman 1120dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1121901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 1122901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 1123901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 1124901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 1125901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 1126ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 1127901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 1128471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 11293a40ed3dSBarry Smith PetscFunctionReturn(0); 11301eb62cbbSBarry Smith } 1131ee50ffe9SBarry Smith 11324a2ae208SSatish Balay #undef __FUNCT__ 11338e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1134dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 11358e2fed03SBarry Smith { 11368e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 11378e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 11388e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 11396849ba73SBarry Smith PetscErrorCode ierr; 114032dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 11416f69ff64SBarry Smith int fd; 1142a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1143d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 11448e2fed03SBarry Smith PetscScalar *column_values; 114585ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 11468e2fed03SBarry Smith 11478e2fed03SBarry Smith PetscFunctionBegin; 11487adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 11497adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 11508e2fed03SBarry Smith nz = A->nz + B->nz; 1151958c9bccSBarry Smith if (!rank) { 11520700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1153d0f46423SBarry Smith header[1] = mat->rmap->N; 1154d0f46423SBarry Smith header[2] = mat->cmap->N; 11557adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 11568e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 11576f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11588e2fed03SBarry Smith /* get largest number of rows any processor has */ 1159d0f46423SBarry Smith rlen = mat->rmap->n; 1160d0f46423SBarry Smith range = mat->rmap->range; 11618e2fed03SBarry Smith for (i=1; i<size; i++) { 11628e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 11638e2fed03SBarry Smith } 11648e2fed03SBarry Smith } else { 11657adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1166d0f46423SBarry Smith rlen = mat->rmap->n; 11678e2fed03SBarry Smith } 11688e2fed03SBarry Smith 11698e2fed03SBarry Smith /* load up the local row counts */ 1170b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 1171d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 11728e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 11738e2fed03SBarry Smith } 11748e2fed03SBarry Smith 11758e2fed03SBarry Smith /* store the row lengths to the file */ 117685ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1177958c9bccSBarry Smith if (!rank) { 11788e2fed03SBarry Smith MPI_Status status; 1179d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11808e2fed03SBarry Smith for (i=1; i<size; i++) { 118185ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 11828e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1183a1319256SJed Brown ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 11846f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11858e2fed03SBarry Smith } 118685ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 11878e2fed03SBarry Smith } else { 118885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1189d0f46423SBarry Smith ierr = MPI_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 119085ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 11918e2fed03SBarry Smith } 11928e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 11938e2fed03SBarry Smith 11948e2fed03SBarry Smith /* load up the local column indices */ 11958e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 11967adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1197b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 11988e2fed03SBarry Smith cnt = 0; 1199d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12008e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12018e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 12028e2fed03SBarry Smith column_indices[cnt++] = col; 12038e2fed03SBarry Smith } 12048e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 12058e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 12068e2fed03SBarry Smith } 12078e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 12088e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 12098e2fed03SBarry Smith } 12108e2fed03SBarry Smith } 1211e32f2f54SBarry 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); 12128e2fed03SBarry Smith 12138e2fed03SBarry Smith /* store the column indices to the file */ 121485ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1215958c9bccSBarry Smith if (!rank) { 12168e2fed03SBarry Smith MPI_Status status; 12176f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12188e2fed03SBarry Smith for (i=1; i<size; i++) { 121985ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 12207adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1221e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 12227adad957SLisandro Dalcin ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 12236f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12248e2fed03SBarry Smith } 122585ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 12268e2fed03SBarry Smith } else { 122785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 12287adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12297adad957SLisandro Dalcin ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 123085ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12318e2fed03SBarry Smith } 12328e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 12338e2fed03SBarry Smith 12348e2fed03SBarry Smith /* load up the local column values */ 12358e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 12368e2fed03SBarry Smith cnt = 0; 1237d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12388e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12398e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 12408e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12418e2fed03SBarry Smith } 12428e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 12438e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 12448e2fed03SBarry Smith } 12458e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 12468e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12478e2fed03SBarry Smith } 12488e2fed03SBarry Smith } 1249e32f2f54SBarry 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); 12508e2fed03SBarry Smith 12518e2fed03SBarry Smith /* store the column values to the file */ 125285ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1253958c9bccSBarry Smith if (!rank) { 12548e2fed03SBarry Smith MPI_Status status; 12556f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12568e2fed03SBarry Smith for (i=1; i<size; i++) { 125785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 12587adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1259e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 12607adad957SLisandro Dalcin ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 12616f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12628e2fed03SBarry Smith } 126385ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 12648e2fed03SBarry Smith } else { 126585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 12667adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12677adad957SLisandro Dalcin ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 126885ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12698e2fed03SBarry Smith } 12708e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 12718e2fed03SBarry Smith PetscFunctionReturn(0); 12728e2fed03SBarry Smith } 12738e2fed03SBarry Smith 12748e2fed03SBarry Smith #undef __FUNCT__ 12754a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1276dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1277416022c9SBarry Smith { 127844a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1279dfbe8321SBarry Smith PetscErrorCode ierr; 128032dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1281ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1282b0a32e0cSBarry Smith PetscViewer sviewer; 1283f3ef73ceSBarry Smith PetscViewerFormat format; 1284416022c9SBarry Smith 12853a40ed3dSBarry Smith PetscFunctionBegin; 12862692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 12872692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 12882692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 128932077d6dSBarry Smith if (iascii) { 1290b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1291456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 12924e220ebcSLois Curfman McInnes MatInfo info; 1293ace3abfcSBarry Smith PetscBool inodes; 1294923f20ffSKris Buschelman 12957adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1296888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1297923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 1298923f20ffSKris Buschelman if (!inodes) { 129977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1300d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13016831982aSBarry Smith } else { 130277431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1303d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13046831982aSBarry Smith } 1305888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 130677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1307888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 130877431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1309b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 131007d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1311a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 13123a40ed3dSBarry Smith PetscFunctionReturn(0); 1313fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1314923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1315923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1316923f20ffSKris Buschelman if (inodes) { 1317923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1318d38fa0fbSBarry Smith } else { 1319d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1320d38fa0fbSBarry Smith } 13213a40ed3dSBarry Smith PetscFunctionReturn(0); 13224aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 13234aedb280SBarry Smith PetscFunctionReturn(0); 132408480c60SBarry Smith } 13258e2fed03SBarry Smith } else if (isbinary) { 13268e2fed03SBarry Smith if (size == 1) { 13277adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 13288e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13298e2fed03SBarry Smith } else { 13308e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 13318e2fed03SBarry Smith } 13328e2fed03SBarry Smith PetscFunctionReturn(0); 13330f5bd95cSBarry Smith } else if (isdraw) { 1334b0a32e0cSBarry Smith PetscDraw draw; 1335ace3abfcSBarry Smith PetscBool isnull; 1336b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1337b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 133819bcc07fSBarry Smith } 133919bcc07fSBarry Smith 134017699dbbSLois Curfman McInnes if (size == 1) { 13417adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 134278b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13433a40ed3dSBarry Smith } else { 134495373324SBarry Smith /* assemble the entire matrix onto first processor. */ 134595373324SBarry Smith Mat A; 1346ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1347d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1348dd6ea824SBarry Smith MatScalar *a; 13492ee70a88SLois Curfman McInnes 135032a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1351ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 135232a366e4SMatthew Knepley 1353acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 135432a366e4SMatthew Knepley if (!flg) { 1355e7e72b3dSBarry 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."); 135632a366e4SMatthew Knepley } 135732a366e4SMatthew Knepley } 13580805154bSBarry Smith 13597adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 136017699dbbSLois Curfman McInnes if (!rank) { 1361f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 13623a40ed3dSBarry Smith } else { 1363f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 136495373324SBarry Smith } 1365f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1366f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1367f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 136852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1369416022c9SBarry Smith 137095373324SBarry Smith /* copy over the A part */ 1371ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1372d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1373d0f46423SBarry Smith row = mat->rmap->rstart; 1374d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 137595373324SBarry Smith for (i=0; i<m; i++) { 1376416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 137795373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 137895373324SBarry Smith } 13792ee70a88SLois Curfman McInnes aj = Aloc->j; 1380d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 138195373324SBarry Smith 138295373324SBarry Smith /* copy over the B part */ 1383ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1384d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1385d0f46423SBarry Smith row = mat->rmap->rstart; 1386b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1387b0a32e0cSBarry Smith ct = cols; 1388bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 138995373324SBarry Smith for (i=0; i<m; i++) { 1390416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 139195373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 139295373324SBarry Smith } 1393606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 13946d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13956d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 139655843e3eSBarry Smith /* 139755843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1398b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 139955843e3eSBarry Smith */ 1400b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1401e03a110bSBarry Smith if (!rank) { 14027adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 14037566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 14047566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 14056831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 140695373324SBarry Smith } 1407b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 140878b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 140995373324SBarry Smith } 14103a40ed3dSBarry Smith PetscFunctionReturn(0); 14111eb62cbbSBarry Smith } 14121eb62cbbSBarry Smith 14134a2ae208SSatish Balay #undef __FUNCT__ 14144a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1415dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1416416022c9SBarry Smith { 1417dfbe8321SBarry Smith PetscErrorCode ierr; 1418ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1419416022c9SBarry Smith 14203a40ed3dSBarry Smith PetscFunctionBegin; 14212692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 14222692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 14232692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14242692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 142532077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 14267b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 14275cd90555SBarry Smith } else { 1428e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1429416022c9SBarry Smith } 14303a40ed3dSBarry Smith PetscFunctionReturn(0); 1431416022c9SBarry Smith } 1432416022c9SBarry Smith 14334a2ae208SSatish Balay #undef __FUNCT__ 143441f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 143541f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 14368a729477SBarry Smith { 143744a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1438dfbe8321SBarry Smith PetscErrorCode ierr; 14396987fefcSBarry Smith Vec bb1 = 0; 1440ace3abfcSBarry Smith PetscBool hasop; 14418a729477SBarry Smith 14423a40ed3dSBarry Smith PetscFunctionBegin; 144385911e72SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 144485911e72SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 144585911e72SJed Brown } 14462798e883SHong Zhang 1447a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 144841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1449a2b30743SBarry Smith PetscFunctionReturn(0); 1450a2b30743SBarry Smith } 1451a2b30743SBarry Smith 1452c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1453da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 145441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14552798e883SHong Zhang its--; 1456da3a660dSBarry Smith } 14572798e883SHong Zhang 14582798e883SHong Zhang while (its--) { 1459ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1460ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14612798e883SHong Zhang 1462c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1463efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1464c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14652798e883SHong Zhang 1466c14dc6b6SHong Zhang /* local sweep */ 146741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14682798e883SHong Zhang } 14693a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1470da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 147141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14722798e883SHong Zhang its--; 1473da3a660dSBarry Smith } 14742798e883SHong Zhang while (its--) { 1475ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1476ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14772798e883SHong Zhang 1478c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1479efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1480c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1481c14dc6b6SHong Zhang 1482c14dc6b6SHong Zhang /* local sweep */ 148341f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14842798e883SHong Zhang } 14853a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1486da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 148741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14882798e883SHong Zhang its--; 1489da3a660dSBarry Smith } 14902798e883SHong Zhang while (its--) { 1491ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1492ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14932798e883SHong Zhang 1494c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1495efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1496c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14972798e883SHong Zhang 1498c14dc6b6SHong Zhang /* local sweep */ 149941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15002798e883SHong Zhang } 1501a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1502a7420bb7SBarry Smith Vec xx1; 1503a7420bb7SBarry Smith 1504a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 150541f059aeSBarry 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); 1506a7420bb7SBarry Smith 1507a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1508a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1509a7420bb7SBarry Smith if (!mat->diag) { 1510a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1511a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1512a7420bb7SBarry Smith } 1513bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1514bd0c2dcbSBarry Smith if (hasop) { 1515bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1516bd0c2dcbSBarry Smith } else { 1517a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1518bd0c2dcbSBarry Smith } 1519887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1520887ee2caSBarry Smith 1521a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1522a7420bb7SBarry Smith 1523a7420bb7SBarry Smith /* local sweep */ 152441f059aeSBarry 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); 1525a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 1526a7420bb7SBarry Smith ierr = VecDestroy(xx1);CHKERRQ(ierr); 15273a40ed3dSBarry Smith } else { 1528e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1529c16cb8f2SBarry Smith } 1530c14dc6b6SHong Zhang 15316987fefcSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 15323a40ed3dSBarry Smith PetscFunctionReturn(0); 15338a729477SBarry Smith } 1534a66be287SLois Curfman McInnes 15354a2ae208SSatish Balay #undef __FUNCT__ 153642e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 153742e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 153842e855d1Svictor { 153942e855d1Svictor MPI_Comm comm,pcomm; 15405d0c19d7SBarry Smith PetscInt first,local_size,nrows; 15415d0c19d7SBarry Smith const PetscInt *rows; 1542dbf0e21dSBarry Smith PetscMPIInt size; 154342e855d1Svictor IS crowp,growp,irowp,lrowp,lcolp,icolp; 154442e855d1Svictor PetscErrorCode ierr; 154542e855d1Svictor 154642e855d1Svictor PetscFunctionBegin; 154742e855d1Svictor ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 154842e855d1Svictor /* make a collective version of 'rowp' */ 154942e855d1Svictor ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 155042e855d1Svictor if (pcomm==comm) { 155142e855d1Svictor crowp = rowp; 155242e855d1Svictor } else { 155342e855d1Svictor ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 155442e855d1Svictor ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 155570b3c8c7SBarry Smith ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr); 155642e855d1Svictor ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 155742e855d1Svictor } 155842e855d1Svictor /* collect the global row permutation and invert it */ 155942e855d1Svictor ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 156042e855d1Svictor ierr = ISSetPermutation(growp);CHKERRQ(ierr); 156142e855d1Svictor if (pcomm!=comm) { 156242e855d1Svictor ierr = ISDestroy(crowp);CHKERRQ(ierr); 156342e855d1Svictor } 156442e855d1Svictor ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 156542e855d1Svictor /* get the local target indices */ 156642e855d1Svictor ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 156742e855d1Svictor ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 156842e855d1Svictor ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 156970b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr); 157042e855d1Svictor ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 157142e855d1Svictor ierr = ISDestroy(irowp);CHKERRQ(ierr); 157242e855d1Svictor /* the column permutation is so much easier; 157342e855d1Svictor make a local version of 'colp' and invert it */ 157442e855d1Svictor ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 1575dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 1576dbf0e21dSBarry Smith if (size==1) { 157742e855d1Svictor lcolp = colp; 157842e855d1Svictor } else { 157942e855d1Svictor ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 158042e855d1Svictor ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 158170b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr); 158242e855d1Svictor } 1583dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 158442e855d1Svictor ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 15854aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 1586dbf0e21dSBarry Smith if (size>1) { 158742e855d1Svictor ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 158842e855d1Svictor ierr = ISDestroy(lcolp);CHKERRQ(ierr); 158942e855d1Svictor } 159042e855d1Svictor /* now we just get the submatrix */ 15914aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 159242e855d1Svictor /* clean up */ 159342e855d1Svictor ierr = ISDestroy(lrowp);CHKERRQ(ierr); 159442e855d1Svictor ierr = ISDestroy(icolp);CHKERRQ(ierr); 159542e855d1Svictor PetscFunctionReturn(0); 159642e855d1Svictor } 159742e855d1Svictor 159842e855d1Svictor #undef __FUNCT__ 15994a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1600dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1601a66be287SLois Curfman McInnes { 1602a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1603a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1604dfbe8321SBarry Smith PetscErrorCode ierr; 1605329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1606a66be287SLois Curfman McInnes 16073a40ed3dSBarry Smith PetscFunctionBegin; 16084e220ebcSLois Curfman McInnes info->block_size = 1.0; 16094e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 16104e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 16114e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 16124e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 16134e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 16144e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1615a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 16164e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 16174e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 16184e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 16194e220ebcSLois Curfman McInnes info->memory = isend[3]; 16204e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1621a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 16227adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 16234e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16244e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16254e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16264e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16274e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1628a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 16297adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 16304e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16314e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16324e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16334e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16344e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1635a66be287SLois Curfman McInnes } 16364e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 16374e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 16384e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 16394e220ebcSLois Curfman McInnes 16403a40ed3dSBarry Smith PetscFunctionReturn(0); 1641a66be287SLois Curfman McInnes } 1642a66be287SLois Curfman McInnes 16434a2ae208SSatish Balay #undef __FUNCT__ 16444a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1645ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1646c74985f6SBarry Smith { 1647c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1648dfbe8321SBarry Smith PetscErrorCode ierr; 1649c74985f6SBarry Smith 16503a40ed3dSBarry Smith PetscFunctionBegin; 165112c028f9SKris Buschelman switch (op) { 1652512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 165312c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 165428b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1655a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 165612c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 165712c028f9SKris Buschelman case MAT_USE_INODES: 165812c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 16594e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16604e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 166112c028f9SKris Buschelman break; 166212c028f9SKris Buschelman case MAT_ROW_ORIENTED: 16634e0d8c25SBarry Smith a->roworiented = flg; 16644e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16654e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 166612c028f9SKris Buschelman break; 16674e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1668290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 166912c028f9SKris Buschelman break; 167012c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 16717c922b88SBarry Smith a->donotstash = PETSC_TRUE; 167212c028f9SKris Buschelman break; 1673ffa07934SHong Zhang case MAT_SPD: 1674ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1675ffa07934SHong Zhang A->spd = flg; 1676ffa07934SHong Zhang if (flg) { 1677ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1678ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1679ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1680ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1681ffa07934SHong Zhang } 1682ffa07934SHong Zhang break; 168377e54ba9SKris Buschelman case MAT_SYMMETRIC: 16844e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 168525f421beSHong Zhang break; 168677e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1687eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1688eeffb40dSHong Zhang break; 1689bf108f30SBarry Smith case MAT_HERMITIAN: 1690eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1691eeffb40dSHong Zhang break; 1692bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 16934e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 169477e54ba9SKris Buschelman break; 169512c028f9SKris Buschelman default: 1696e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 16973a40ed3dSBarry Smith } 16983a40ed3dSBarry Smith PetscFunctionReturn(0); 1699c74985f6SBarry Smith } 1700c74985f6SBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 17024a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1703b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 170439e00950SLois Curfman McInnes { 1705154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 170687828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 17076849ba73SBarry Smith PetscErrorCode ierr; 1708d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1709d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1710b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 171139e00950SLois Curfman McInnes 17123a40ed3dSBarry Smith PetscFunctionBegin; 1713e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 17147a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 17157a0afa10SBarry Smith 171670f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 17177a0afa10SBarry Smith /* 17187a0afa10SBarry Smith allocate enough space to hold information from the longest row. 17197a0afa10SBarry Smith */ 17207a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1721b1d57f15SBarry Smith PetscInt max = 1,tmp; 1722d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 17237a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 17247a0afa10SBarry Smith if (max < tmp) { max = tmp; } 17257a0afa10SBarry Smith } 17261d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 17277a0afa10SBarry Smith } 17287a0afa10SBarry Smith 1729e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1730abc0e9e4SLois Curfman McInnes lrow = row - rstart; 173139e00950SLois Curfman McInnes 1732154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1733154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1734154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1735f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1736f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1737154123eaSLois Curfman McInnes nztot = nzA + nzB; 1738154123eaSLois Curfman McInnes 173970f0671dSBarry Smith cmap = mat->garray; 1740154123eaSLois Curfman McInnes if (v || idx) { 1741154123eaSLois Curfman McInnes if (nztot) { 1742154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1743b1d57f15SBarry Smith PetscInt imark = -1; 1744154123eaSLois Curfman McInnes if (v) { 174570f0671dSBarry Smith *v = v_p = mat->rowvalues; 174639e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 174770f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1748154123eaSLois Curfman McInnes else break; 1749154123eaSLois Curfman McInnes } 1750154123eaSLois Curfman McInnes imark = i; 175170f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 175270f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1753154123eaSLois Curfman McInnes } 1754154123eaSLois Curfman McInnes if (idx) { 175570f0671dSBarry Smith *idx = idx_p = mat->rowindices; 175670f0671dSBarry Smith if (imark > -1) { 175770f0671dSBarry Smith for (i=0; i<imark; i++) { 175870f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 175970f0671dSBarry Smith } 176070f0671dSBarry Smith } else { 1761154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 176270f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1763154123eaSLois Curfman McInnes else break; 1764154123eaSLois Curfman McInnes } 1765154123eaSLois Curfman McInnes imark = i; 176670f0671dSBarry Smith } 176770f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 176870f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 176939e00950SLois Curfman McInnes } 17703f97c4b0SBarry Smith } else { 17711ca473b0SSatish Balay if (idx) *idx = 0; 17721ca473b0SSatish Balay if (v) *v = 0; 17731ca473b0SSatish Balay } 1774154123eaSLois Curfman McInnes } 177539e00950SLois Curfman McInnes *nz = nztot; 1776f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1777f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 17783a40ed3dSBarry Smith PetscFunctionReturn(0); 177939e00950SLois Curfman McInnes } 178039e00950SLois Curfman McInnes 17814a2ae208SSatish Balay #undef __FUNCT__ 17824a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1783b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 178439e00950SLois Curfman McInnes { 17857a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17863a40ed3dSBarry Smith 17873a40ed3dSBarry Smith PetscFunctionBegin; 1788e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 17897a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 17903a40ed3dSBarry Smith PetscFunctionReturn(0); 179139e00950SLois Curfman McInnes } 179239e00950SLois Curfman McInnes 17934a2ae208SSatish Balay #undef __FUNCT__ 17944a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1795dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1796855ac2c5SLois Curfman McInnes { 1797855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1798ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1799dfbe8321SBarry Smith PetscErrorCode ierr; 1800d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1801329f5518SBarry Smith PetscReal sum = 0.0; 1802a77337e4SBarry Smith MatScalar *v; 180304ca555eSLois Curfman McInnes 18043a40ed3dSBarry Smith PetscFunctionBegin; 180517699dbbSLois Curfman McInnes if (aij->size == 1) { 180614183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 180737fa93a5SLois Curfman McInnes } else { 180804ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 180904ca555eSLois Curfman McInnes v = amat->a; 181004ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1811aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1812329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 181304ca555eSLois Curfman McInnes #else 181404ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 181504ca555eSLois Curfman McInnes #endif 181604ca555eSLois Curfman McInnes } 181704ca555eSLois Curfman McInnes v = bmat->a; 181804ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1819aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1820329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 182104ca555eSLois Curfman McInnes #else 182204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 182304ca555eSLois Curfman McInnes #endif 182404ca555eSLois Curfman McInnes } 18257adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 182604ca555eSLois Curfman McInnes *norm = sqrt(*norm); 18273a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1828329f5518SBarry Smith PetscReal *tmp,*tmp2; 1829b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 1830d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1831d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1832d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 183304ca555eSLois Curfman McInnes *norm = 0.0; 183404ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 183504ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1836bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 183704ca555eSLois Curfman McInnes } 183804ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 183904ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1840bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 184104ca555eSLois Curfman McInnes } 1842d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 1843d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 184404ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 184504ca555eSLois Curfman McInnes } 1846606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1847606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 18483a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1849329f5518SBarry Smith PetscReal ntemp = 0.0; 1850d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1851bfec09a0SHong Zhang v = amat->a + amat->i[j]; 185204ca555eSLois Curfman McInnes sum = 0.0; 185304ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1854cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 185504ca555eSLois Curfman McInnes } 1856bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 185704ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1858cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 185904ca555eSLois Curfman McInnes } 1860515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 186104ca555eSLois Curfman McInnes } 18627adad957SLisandro Dalcin ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 1863ca161407SBarry Smith } else { 1864e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 186504ca555eSLois Curfman McInnes } 186637fa93a5SLois Curfman McInnes } 18673a40ed3dSBarry Smith PetscFunctionReturn(0); 1868855ac2c5SLois Curfman McInnes } 1869855ac2c5SLois Curfman McInnes 18704a2ae208SSatish Balay #undef __FUNCT__ 18714a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1872fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1873b7c46309SBarry Smith { 1874b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1875da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1876dfbe8321SBarry Smith PetscErrorCode ierr; 1877d0f46423SBarry Smith PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz; 1878d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 18793a40ed3dSBarry Smith Mat B; 1880a77337e4SBarry Smith MatScalar *array; 1881b7c46309SBarry Smith 18823a40ed3dSBarry Smith PetscFunctionBegin; 1883e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1884da668accSHong Zhang 1885d0f46423SBarry Smith ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; 1886da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1887da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1888fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 1889fc73b1b3SBarry Smith /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */ 1890fc73b1b3SBarry Smith ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr); 1891da668accSHong Zhang ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr); 1892da668accSHong Zhang for (i=0; i<ai[ma]; i++){ 1893da668accSHong Zhang d_nnz[aj[i]] ++; 1894da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1895d4bb536fSBarry Smith } 1896d4bb536fSBarry Smith 18977adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1898d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 18997adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1900da668accSHong Zhang ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr); 1901fc73b1b3SBarry Smith ierr = PetscFree(d_nnz);CHKERRQ(ierr); 1902fc4dec0aSBarry Smith } else { 1903fc4dec0aSBarry Smith B = *matout; 1904fc4dec0aSBarry Smith } 1905b7c46309SBarry Smith 1906b7c46309SBarry Smith /* copy over the A part */ 1907da668accSHong Zhang array = Aloc->a; 1908d0f46423SBarry Smith row = A->rmap->rstart; 1909da668accSHong Zhang for (i=0; i<ma; i++) { 1910da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1911da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1912da668accSHong Zhang row++; array += ncol; aj += ncol; 1913b7c46309SBarry Smith } 1914b7c46309SBarry Smith aj = Aloc->j; 1915da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1916b7c46309SBarry Smith 1917b7c46309SBarry Smith /* copy over the B part */ 1918fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1919fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 1920da668accSHong Zhang array = Bloc->a; 1921d0f46423SBarry Smith row = A->rmap->rstart; 1922da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 192361a2fbbaSHong Zhang cols_tmp = cols; 1924da668accSHong Zhang for (i=0; i<mb; i++) { 1925da668accSHong Zhang ncol = bi[i+1]-bi[i]; 192661a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 192761a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 1928b7c46309SBarry Smith } 1929fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1930fc73b1b3SBarry Smith 19316d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19326d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1933815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 19340de55854SLois Curfman McInnes *matout = B; 19350de55854SLois Curfman McInnes } else { 1936eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 19370de55854SLois Curfman McInnes } 19383a40ed3dSBarry Smith PetscFunctionReturn(0); 1939b7c46309SBarry Smith } 1940b7c46309SBarry Smith 19414a2ae208SSatish Balay #undef __FUNCT__ 19424a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1943dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1944a008b906SSatish Balay { 19454b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 19464b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1947dfbe8321SBarry Smith PetscErrorCode ierr; 1948b1d57f15SBarry Smith PetscInt s1,s2,s3; 1949a008b906SSatish Balay 19503a40ed3dSBarry Smith PetscFunctionBegin; 19514b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 19524b967eb1SSatish Balay if (rr) { 1953e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1954e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 19554b967eb1SSatish Balay /* Overlap communication with computation. */ 1956ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1957a008b906SSatish Balay } 19584b967eb1SSatish Balay if (ll) { 1959e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1960e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1961f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 19624b967eb1SSatish Balay } 19634b967eb1SSatish Balay /* scale the diagonal block */ 1964f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 19654b967eb1SSatish Balay 19664b967eb1SSatish Balay if (rr) { 19674b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1968ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1969f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 19704b967eb1SSatish Balay } 19714b967eb1SSatish Balay 19723a40ed3dSBarry Smith PetscFunctionReturn(0); 1973a008b906SSatish Balay } 1974a008b906SSatish Balay 19754a2ae208SSatish Balay #undef __FUNCT__ 1976521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ" 1977521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs) 19785a838052SSatish Balay { 1979521d7252SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1980521d7252SBarry Smith PetscErrorCode ierr; 1981521d7252SBarry Smith 19823a40ed3dSBarry Smith PetscFunctionBegin; 1983521d7252SBarry Smith ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 1984521d7252SBarry Smith ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1985829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 1986829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 19873a40ed3dSBarry Smith PetscFunctionReturn(0); 19885a838052SSatish Balay } 19894a2ae208SSatish Balay #undef __FUNCT__ 19904a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 1991dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 1992bb5a7306SBarry Smith { 1993bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1994dfbe8321SBarry Smith PetscErrorCode ierr; 19953a40ed3dSBarry Smith 19963a40ed3dSBarry Smith PetscFunctionBegin; 1997bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 19983a40ed3dSBarry Smith PetscFunctionReturn(0); 1999bb5a7306SBarry Smith } 2000bb5a7306SBarry Smith 20014a2ae208SSatish Balay #undef __FUNCT__ 20024a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2003ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2004d4bb536fSBarry Smith { 2005d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2006d4bb536fSBarry Smith Mat a,b,c,d; 2007ace3abfcSBarry Smith PetscBool flg; 2008dfbe8321SBarry Smith PetscErrorCode ierr; 2009d4bb536fSBarry Smith 20103a40ed3dSBarry Smith PetscFunctionBegin; 2011d4bb536fSBarry Smith a = matA->A; b = matA->B; 2012d4bb536fSBarry Smith c = matB->A; d = matB->B; 2013d4bb536fSBarry Smith 2014d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2015abc0a331SBarry Smith if (flg) { 2016d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2017d4bb536fSBarry Smith } 20187adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 20193a40ed3dSBarry Smith PetscFunctionReturn(0); 2020d4bb536fSBarry Smith } 2021d4bb536fSBarry Smith 20224a2ae208SSatish Balay #undef __FUNCT__ 20234a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2024dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2025cb5b572fSBarry Smith { 2026dfbe8321SBarry Smith PetscErrorCode ierr; 2027cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 2028cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 2029cb5b572fSBarry Smith 2030cb5b572fSBarry Smith PetscFunctionBegin; 203133f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 203233f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2033cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2034cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2035cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2036cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2037cb5b572fSBarry Smith then copying the submatrices */ 2038cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2039cb5b572fSBarry Smith } else { 2040cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2041cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2042cb5b572fSBarry Smith } 2043cb5b572fSBarry Smith PetscFunctionReturn(0); 2044cb5b572fSBarry Smith } 2045cb5b572fSBarry Smith 20464a2ae208SSatish Balay #undef __FUNCT__ 20474a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 2048dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A) 2049273d9f13SBarry Smith { 2050dfbe8321SBarry Smith PetscErrorCode ierr; 2051273d9f13SBarry Smith 2052273d9f13SBarry Smith PetscFunctionBegin; 2053273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2054273d9f13SBarry Smith PetscFunctionReturn(0); 2055273d9f13SBarry Smith } 2056273d9f13SBarry Smith 2057ac90fabeSBarry Smith #undef __FUNCT__ 2058ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2059f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2060ac90fabeSBarry Smith { 2061dfbe8321SBarry Smith PetscErrorCode ierr; 2062b1d57f15SBarry Smith PetscInt i; 2063ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 20644ce68768SBarry Smith PetscBLASInt bnz,one=1; 2065ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2066ac90fabeSBarry Smith 2067ac90fabeSBarry Smith PetscFunctionBegin; 2068ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2069f4df32b1SMatthew Knepley PetscScalar alpha = a; 2070ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 2071ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 20720805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2073f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2074ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 2075ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 20760805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2077f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2078a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2079f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2080c537a176SHong Zhang 2081c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 2082a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 2083a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2084a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2085a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2086c537a176SHong Zhang } 2087a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2088d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2089a30b2313SHong Zhang y->XtoY = xx->B; 2090407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2091c537a176SHong Zhang } 2092f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2093ac90fabeSBarry Smith } else { 20949f5f6813SShri Abhyankar Mat B; 20959f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 20969f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 20979f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 20989f5f6813SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 2099bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 21009f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 21019f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 21029f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 21039f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->B,xx->B,nnz_o);CHKERRQ(ierr); 21049f5f6813SShri Abhyankar ierr = MatMPIAIJSetPreallocation(B,PETSC_NULL,nnz_d,PETSC_NULL,nnz_o);CHKERRQ(ierr); 21059f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 21069f5f6813SShri Abhyankar ierr = MatHeaderReplace(Y,B); 21079f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 21089f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2109ac90fabeSBarry Smith } 2110ac90fabeSBarry Smith PetscFunctionReturn(0); 2111ac90fabeSBarry Smith } 2112ac90fabeSBarry Smith 21137087cfbeSBarry Smith extern PetscErrorCode MatConjugate_SeqAIJ(Mat); 2114354c94deSBarry Smith 2115354c94deSBarry Smith #undef __FUNCT__ 2116354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 21177087cfbeSBarry Smith PetscErrorCode MatConjugate_MPIAIJ(Mat mat) 2118354c94deSBarry Smith { 2119354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2120354c94deSBarry Smith PetscErrorCode ierr; 2121354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2122354c94deSBarry Smith 2123354c94deSBarry Smith PetscFunctionBegin; 2124354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2125354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2126354c94deSBarry Smith #else 2127354c94deSBarry Smith PetscFunctionBegin; 2128354c94deSBarry Smith #endif 2129354c94deSBarry Smith PetscFunctionReturn(0); 2130354c94deSBarry Smith } 2131354c94deSBarry Smith 213299cafbc1SBarry Smith #undef __FUNCT__ 213399cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 213499cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 213599cafbc1SBarry Smith { 213699cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 213799cafbc1SBarry Smith PetscErrorCode ierr; 213899cafbc1SBarry Smith 213999cafbc1SBarry Smith PetscFunctionBegin; 214099cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 214199cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 214299cafbc1SBarry Smith PetscFunctionReturn(0); 214399cafbc1SBarry Smith } 214499cafbc1SBarry Smith 214599cafbc1SBarry Smith #undef __FUNCT__ 214699cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 214799cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 214899cafbc1SBarry Smith { 214999cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 215099cafbc1SBarry Smith PetscErrorCode ierr; 215199cafbc1SBarry Smith 215299cafbc1SBarry Smith PetscFunctionBegin; 215399cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 215499cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 215599cafbc1SBarry Smith PetscFunctionReturn(0); 215699cafbc1SBarry Smith } 215799cafbc1SBarry Smith 2158103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2159103bf8bdSMatthew Knepley 2160103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2161a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2162a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2163a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2164103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2165a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2166d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2167103bf8bdSMatthew Knepley 2168103bf8bdSMatthew Knepley #undef __FUNCT__ 2169103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2170103bf8bdSMatthew Knepley /* 2171103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2172103bf8bdSMatthew Knepley */ 21730481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2174103bf8bdSMatthew Knepley { 2175a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2176a2c909beSMatthew Knepley 2177a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2178a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2179a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2180a2c909beSMatthew Knepley 2181ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2182776b82aeSLisandro Dalcin PetscContainer c; 2183103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2184103bf8bdSMatthew Knepley PetscErrorCode ierr; 2185103bf8bdSMatthew Knepley 2186103bf8bdSMatthew Knepley PetscFunctionBegin; 2187e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2188103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2189103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2190103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 2191e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2192103bf8bdSMatthew Knepley } 2193103bf8bdSMatthew Knepley 2194103bf8bdSMatthew Knepley process_group_type pg; 2195a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2196a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2197a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2198a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2199a2c909beSMatthew Knepley 2200103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2201a2c909beSMatthew Knepley ilu_permuted(level_graph); 2202103bf8bdSMatthew Knepley 2203103bf8bdSMatthew Knepley /* put together the new matrix */ 22047adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 2205103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2206103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2207719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2208719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2209719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2210719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2211103bf8bdSMatthew Knepley 22127adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 2213776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2214719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2215103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2216103bf8bdSMatthew Knepley } 2217103bf8bdSMatthew Knepley 2218103bf8bdSMatthew Knepley #undef __FUNCT__ 2219103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 22200481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2221103bf8bdSMatthew Knepley { 2222103bf8bdSMatthew Knepley PetscFunctionBegin; 2223103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2224103bf8bdSMatthew Knepley } 2225103bf8bdSMatthew Knepley 2226103bf8bdSMatthew Knepley #undef __FUNCT__ 2227103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2228103bf8bdSMatthew Knepley /* 2229103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2230103bf8bdSMatthew Knepley */ 2231103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2232103bf8bdSMatthew Knepley { 2233a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2234a2c909beSMatthew Knepley 2235a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2236a2c909beSMatthew Knepley lgraph_type* lgraph_p; 2237776b82aeSLisandro Dalcin PetscContainer c; 2238103bf8bdSMatthew Knepley PetscErrorCode ierr; 2239103bf8bdSMatthew Knepley 2240103bf8bdSMatthew Knepley PetscFunctionBegin; 2241103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 2242776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 2243103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2244a2c909beSMatthew Knepley 2245a2c909beSMatthew Knepley PetscScalar* array_x; 2246a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2247a2c909beSMatthew Knepley PetscInt sx; 2248a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2249a2c909beSMatthew Knepley 2250a2c909beSMatthew Knepley PetscScalar* array_b; 2251a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2252a2c909beSMatthew Knepley PetscInt sb; 2253a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2254a2c909beSMatthew Knepley 2255a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2256a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2257a2c909beSMatthew Knepley 2258a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2259a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2260a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2261a2c909beSMatthew Knepley 2262a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2263a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2264a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2265a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2266a2c909beSMatthew Knepley 2267a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2268a2c909beSMatthew Knepley 2269103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2270103bf8bdSMatthew Knepley } 2271103bf8bdSMatthew Knepley #endif 2272103bf8bdSMatthew Knepley 227369db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 227469db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 22751d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 22761d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 227769db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 227869db28dcSHong Zhang PetscErrorCode (*MatDestroy)(Mat); 227969db28dcSHong Zhang } Mat_Redundant; 228069db28dcSHong Zhang 228169db28dcSHong Zhang #undef __FUNCT__ 228269db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 228369db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 228469db28dcSHong Zhang { 228569db28dcSHong Zhang PetscErrorCode ierr; 228669db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 228769db28dcSHong Zhang PetscInt i; 228869db28dcSHong Zhang 228969db28dcSHong Zhang PetscFunctionBegin; 22901d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 229169db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 229269db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 229369db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 229469db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 229569db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 229669db28dcSHong Zhang } 22971d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 229869db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 229969db28dcSHong Zhang PetscFunctionReturn(0); 230069db28dcSHong Zhang } 230169db28dcSHong Zhang 230269db28dcSHong Zhang #undef __FUNCT__ 230369db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 230469db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 230569db28dcSHong Zhang { 230669db28dcSHong Zhang PetscErrorCode ierr; 230769db28dcSHong Zhang PetscContainer container; 230869db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 230969db28dcSHong Zhang 231069db28dcSHong Zhang PetscFunctionBegin; 231169db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 231269db28dcSHong Zhang if (container) { 231369db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 231469db28dcSHong Zhang } else { 2315e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 231669db28dcSHong Zhang } 231769db28dcSHong Zhang A->ops->destroy = redund->MatDestroy; 231869db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 231969db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 232069db28dcSHong Zhang ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 232169db28dcSHong Zhang PetscFunctionReturn(0); 232269db28dcSHong Zhang } 232369db28dcSHong Zhang 232469db28dcSHong Zhang #undef __FUNCT__ 232569db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 232669db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 232769db28dcSHong Zhang { 232869db28dcSHong Zhang PetscMPIInt rank,size; 23297adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 233069db28dcSHong Zhang PetscErrorCode ierr; 233169db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 233269db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2333d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 233469db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 233569db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 233669db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 233769db28dcSHong Zhang PetscScalar *sbuf_a; 233869db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2339d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2340d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 234169db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2342a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2343a77337e4SBarry Smith PetscScalar *vals; 234469db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 234569db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 234669db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 234769db28dcSHong Zhang MPI_Status recv_status,*send_status; 234869db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 234969db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 235069db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 235169db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 235269db28dcSHong Zhang PetscContainer container; 235369db28dcSHong Zhang 235469db28dcSHong Zhang PetscFunctionBegin; 235569db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 235669db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 235769db28dcSHong Zhang 235869db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 235969db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2360e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 236169db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2362e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 236369db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 236469db28dcSHong Zhang if (container) { 236569db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 236669db28dcSHong Zhang } else { 2367e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 236869db28dcSHong Zhang } 2369e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 237069db28dcSHong Zhang 237169db28dcSHong Zhang nsends = redund->nsends; 237269db28dcSHong Zhang nrecvs = redund->nrecvs; 23731d79065fSBarry Smith send_rank = redund->send_rank; 23741d79065fSBarry Smith recv_rank = redund->recv_rank; 23751d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 23761d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 237769db28dcSHong Zhang sbuf_j = redund->sbuf_j; 237869db28dcSHong Zhang sbuf_a = redund->sbuf_a; 237969db28dcSHong Zhang rbuf_j = redund->rbuf_j; 238069db28dcSHong Zhang rbuf_a = redund->rbuf_a; 238169db28dcSHong Zhang } 238269db28dcSHong Zhang 238369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 238469db28dcSHong Zhang PetscMPIInt subrank,subsize; 238569db28dcSHong Zhang PetscInt nleftover,np_subcomm; 238669db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 238769db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 238869db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 23891d79065fSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank); 239069db28dcSHong Zhang np_subcomm = size/nsubcomm; 239169db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 239269db28dcSHong Zhang nsends = 0; nrecvs = 0; 239369db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 239469db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 239569db28dcSHong Zhang send_rank[nsends] = i; nsends++; 239669db28dcSHong Zhang recv_rank[nrecvs++] = i; 239769db28dcSHong Zhang } 239869db28dcSHong Zhang } 239969db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 240069db28dcSHong Zhang i = size-nleftover-1; 240169db28dcSHong Zhang j = 0; 240269db28dcSHong Zhang while (j < nsubcomm - nleftover){ 240369db28dcSHong Zhang send_rank[nsends++] = i; 240469db28dcSHong Zhang i--; j++; 240569db28dcSHong Zhang } 240669db28dcSHong Zhang } 240769db28dcSHong Zhang 240869db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 240969db28dcSHong Zhang for (i=0; i<nleftover; i++){ 241069db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 241169db28dcSHong Zhang } 241269db28dcSHong Zhang } 241369db28dcSHong Zhang 241469db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 241569db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 241669db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 241769db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 241869db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 241969db28dcSHong Zhang 242069db28dcSHong Zhang /* copy mat's local entries into the buffers */ 242169db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 242269db28dcSHong Zhang rownz_max = 0; 242369db28dcSHong Zhang rptr = sbuf_j; 242469db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 242569db28dcSHong Zhang vals = sbuf_a; 242669db28dcSHong Zhang rptr[0] = 0; 242769db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 242869db28dcSHong Zhang row = i + rstart; 242969db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 243069db28dcSHong Zhang ncols = nzA + nzB; 243169db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 243269db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 243369db28dcSHong Zhang /* load the column indices for this row into cols */ 243469db28dcSHong Zhang lwrite = 0; 243569db28dcSHong Zhang for (l=0; l<nzB; l++) { 243669db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 243769db28dcSHong Zhang vals[lwrite] = aworkB[l]; 243869db28dcSHong Zhang cols[lwrite++] = ctmp; 243969db28dcSHong Zhang } 244069db28dcSHong Zhang } 244169db28dcSHong Zhang for (l=0; l<nzA; l++){ 244269db28dcSHong Zhang vals[lwrite] = aworkA[l]; 244369db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 244469db28dcSHong Zhang } 244569db28dcSHong Zhang for (l=0; l<nzB; l++) { 244669db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 244769db28dcSHong Zhang vals[lwrite] = aworkB[l]; 244869db28dcSHong Zhang cols[lwrite++] = ctmp; 244969db28dcSHong Zhang } 245069db28dcSHong Zhang } 245169db28dcSHong Zhang vals += ncols; 245269db28dcSHong Zhang cols += ncols; 245369db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 245469db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 245569db28dcSHong Zhang } 2456e32f2f54SBarry 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); 245769db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 245869db28dcSHong Zhang rptr = sbuf_j; 245969db28dcSHong Zhang vals = sbuf_a; 246069db28dcSHong Zhang rptr[0] = 0; 246169db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 246269db28dcSHong Zhang row = i + rstart; 246369db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 246469db28dcSHong Zhang ncols = nzA + nzB; 246569db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 246669db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 246769db28dcSHong Zhang lwrite = 0; 246869db28dcSHong Zhang for (l=0; l<nzB; l++) { 246969db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 247069db28dcSHong Zhang } 247169db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 247269db28dcSHong Zhang for (l=0; l<nzB; l++) { 247369db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 247469db28dcSHong Zhang } 247569db28dcSHong Zhang vals += ncols; 247669db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 247769db28dcSHong Zhang } 247869db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 247969db28dcSHong Zhang 248069db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 248169db28dcSHong Zhang /*--------------------------------------------------*/ 248269db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 248369db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 248469db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 248569db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 248669db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 248769db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 248869db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 248969db28dcSHong Zhang } else { 249069db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 249169db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 249269db28dcSHong Zhang } 249369db28dcSHong Zhang 249469db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 249569db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 249669db28dcSHong Zhang /* get new tags to keep the communication clean */ 249769db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 249869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 24991d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 250069db28dcSHong Zhang 250169db28dcSHong Zhang /* post receives of other's nzlocal */ 250269db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 250369db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 250469db28dcSHong Zhang } 250569db28dcSHong Zhang /* send nzlocal to others */ 250669db28dcSHong Zhang for (i=0; i<nsends; i++){ 250769db28dcSHong Zhang sbuf_nz[i] = nzlocal; 250869db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 250969db28dcSHong Zhang } 251069db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 251169db28dcSHong Zhang count = nrecvs; 251269db28dcSHong Zhang while (count) { 251369db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 251469db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 251569db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 251669db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 251769db28dcSHong Zhang 251869db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 251969db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 252069db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 252169db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 252269db28dcSHong Zhang count--; 252369db28dcSHong Zhang } 252469db28dcSHong Zhang /* wait on sends of nzlocal */ 252569db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 252669db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 252769db28dcSHong Zhang /*------------------------------------------------*/ 252869db28dcSHong Zhang for (i=0; i<nsends; i++){ 252969db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 253069db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 253169db28dcSHong Zhang } 253269db28dcSHong Zhang /* wait on receives of mat->i,j */ 253369db28dcSHong Zhang /*------------------------------*/ 253469db28dcSHong Zhang count = nrecvs; 253569db28dcSHong Zhang while (count) { 253669db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2537e32f2f54SBarry 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); 253869db28dcSHong Zhang count--; 253969db28dcSHong Zhang } 254069db28dcSHong Zhang /* wait on sends of mat->i,j */ 254169db28dcSHong Zhang /*---------------------------*/ 254269db28dcSHong Zhang if (nsends) { 254369db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 254469db28dcSHong Zhang } 254569db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 254669db28dcSHong Zhang 254769db28dcSHong Zhang /* post receives, send and receive mat->a */ 254869db28dcSHong Zhang /*----------------------------------------*/ 254969db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 255069db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 255169db28dcSHong Zhang } 255269db28dcSHong Zhang for (i=0; i<nsends; i++){ 255369db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 255469db28dcSHong Zhang } 255569db28dcSHong Zhang count = nrecvs; 255669db28dcSHong Zhang while (count) { 255769db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2558e32f2f54SBarry 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); 255969db28dcSHong Zhang count--; 256069db28dcSHong Zhang } 256169db28dcSHong Zhang if (nsends) { 256269db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 256369db28dcSHong Zhang } 256469db28dcSHong Zhang 256569db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 256669db28dcSHong Zhang 256769db28dcSHong Zhang /* create redundant matrix */ 256869db28dcSHong Zhang /*-------------------------*/ 256969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 257069db28dcSHong Zhang /* compute rownz_max for preallocation */ 257169db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 257269db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 257369db28dcSHong Zhang rptr = rbuf_j[imdex]; 257469db28dcSHong Zhang for (i=0; i<j; i++){ 257569db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 257669db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 257769db28dcSHong Zhang } 257869db28dcSHong Zhang } 257969db28dcSHong Zhang 258069db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 258169db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 258269db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 258369db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 258469db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 258569db28dcSHong Zhang } else { 258669db28dcSHong Zhang C = *matredundant; 258769db28dcSHong Zhang } 258869db28dcSHong Zhang 258969db28dcSHong Zhang /* insert local matrix entries */ 259069db28dcSHong Zhang rptr = sbuf_j; 259169db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 259269db28dcSHong Zhang vals = sbuf_a; 259369db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 259469db28dcSHong Zhang row = i + rstart; 259569db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 259669db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 259769db28dcSHong Zhang vals += ncols; 259869db28dcSHong Zhang cols += ncols; 259969db28dcSHong Zhang } 260069db28dcSHong Zhang /* insert received matrix entries */ 260169db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 260269db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 260369db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 260469db28dcSHong Zhang rptr = rbuf_j[imdex]; 260569db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 260669db28dcSHong Zhang vals = rbuf_a[imdex]; 260769db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 260869db28dcSHong Zhang row = i + rstart; 260969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 261069db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 261169db28dcSHong Zhang vals += ncols; 261269db28dcSHong Zhang cols += ncols; 261369db28dcSHong Zhang } 261469db28dcSHong Zhang } 261569db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 261669db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 261769db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2618e32f2f54SBarry 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); 261969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 262069db28dcSHong Zhang PetscContainer container; 262169db28dcSHong Zhang *matredundant = C; 262269db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 262338f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 262469db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 262569db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 262669db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 262769db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 262869db28dcSHong Zhang 262969db28dcSHong Zhang redund->nzlocal = nzlocal; 263069db28dcSHong Zhang redund->nsends = nsends; 263169db28dcSHong Zhang redund->nrecvs = nrecvs; 263269db28dcSHong Zhang redund->send_rank = send_rank; 26331d79065fSBarry Smith redund->recv_rank = recv_rank; 263469db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 26351d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 263669db28dcSHong Zhang redund->sbuf_j = sbuf_j; 263769db28dcSHong Zhang redund->sbuf_a = sbuf_a; 263869db28dcSHong Zhang redund->rbuf_j = rbuf_j; 263969db28dcSHong Zhang redund->rbuf_a = rbuf_a; 264069db28dcSHong Zhang 264169db28dcSHong Zhang redund->MatDestroy = C->ops->destroy; 264269db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 264369db28dcSHong Zhang } 264469db28dcSHong Zhang PetscFunctionReturn(0); 264569db28dcSHong Zhang } 264669db28dcSHong Zhang 264703bc72f1SMatthew Knepley #undef __FUNCT__ 2648c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2649c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2650c91732d9SHong Zhang { 2651c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2652c91732d9SHong Zhang PetscErrorCode ierr; 2653c91732d9SHong Zhang PetscInt i,*idxb = 0; 2654c91732d9SHong Zhang PetscScalar *va,*vb; 2655c91732d9SHong Zhang Vec vtmp; 2656c91732d9SHong Zhang 2657c91732d9SHong Zhang PetscFunctionBegin; 2658c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2659c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2660c91732d9SHong Zhang if (idx) { 2661192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2662d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2663c91732d9SHong Zhang } 2664c91732d9SHong Zhang } 2665c91732d9SHong Zhang 2666d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2667c91732d9SHong Zhang if (idx) { 2668d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2669c91732d9SHong Zhang } 2670c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2671c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2672c91732d9SHong Zhang 2673d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2674c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2675c91732d9SHong Zhang va[i] = vb[i]; 2676c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2677c91732d9SHong Zhang } 2678c91732d9SHong Zhang } 2679c91732d9SHong Zhang 2680c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2681c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2682c91732d9SHong Zhang if (idxb) { 2683c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 2684c91732d9SHong Zhang } 2685c91732d9SHong Zhang ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2686c91732d9SHong Zhang PetscFunctionReturn(0); 2687c91732d9SHong Zhang } 2688c91732d9SHong Zhang 2689c91732d9SHong Zhang #undef __FUNCT__ 2690c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2691c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2692c87e5d42SMatthew Knepley { 2693c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2694c87e5d42SMatthew Knepley PetscErrorCode ierr; 2695c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2696c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2697c87e5d42SMatthew Knepley Vec vtmp; 2698c87e5d42SMatthew Knepley 2699c87e5d42SMatthew Knepley PetscFunctionBegin; 2700c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2701c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2702c87e5d42SMatthew Knepley if (idx) { 2703c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2704c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2705c87e5d42SMatthew Knepley } 2706c87e5d42SMatthew Knepley } 2707c87e5d42SMatthew Knepley 2708c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2709c87e5d42SMatthew Knepley if (idx) { 2710c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2711c87e5d42SMatthew Knepley } 2712c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2713c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2714c87e5d42SMatthew Knepley 2715c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2716c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2717c87e5d42SMatthew Knepley va[i] = vb[i]; 2718c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2719c87e5d42SMatthew Knepley } 2720c87e5d42SMatthew Knepley } 2721c87e5d42SMatthew Knepley 2722c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2723c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2724c87e5d42SMatthew Knepley if (idxb) { 2725c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 2726c87e5d42SMatthew Knepley } 2727c87e5d42SMatthew Knepley ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2728c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2729c87e5d42SMatthew Knepley } 2730c87e5d42SMatthew Knepley 2731c87e5d42SMatthew Knepley #undef __FUNCT__ 273203bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 273303bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 273403bc72f1SMatthew Knepley { 273503bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2736d0f46423SBarry Smith PetscInt n = A->rmap->n; 2737d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 273803bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 273903bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 274003bc72f1SMatthew Knepley Vec diagV, offdiagV; 274103bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 274203bc72f1SMatthew Knepley PetscInt r; 274303bc72f1SMatthew Knepley PetscErrorCode ierr; 274403bc72f1SMatthew Knepley 274503bc72f1SMatthew Knepley PetscFunctionBegin; 274603bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2747e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2748e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 274903bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 275003bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 275103bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 275203bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 275303bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 275403bc72f1SMatthew Knepley for(r = 0; r < n; ++r) { 2755028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 275603bc72f1SMatthew Knepley a[r] = diagA[r]; 275703bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 275803bc72f1SMatthew Knepley } else { 275903bc72f1SMatthew Knepley a[r] = offdiagA[r]; 276003bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 276103bc72f1SMatthew Knepley } 276203bc72f1SMatthew Knepley } 276303bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 276403bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 276503bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 276603bc72f1SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 276703bc72f1SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 276803bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 276903bc72f1SMatthew Knepley PetscFunctionReturn(0); 277003bc72f1SMatthew Knepley } 277103bc72f1SMatthew Knepley 27725494a064SHong Zhang #undef __FUNCT__ 2773c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2774c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2775c87e5d42SMatthew Knepley { 2776c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2777c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2778c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2779c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2780c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2781c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2782c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2783c87e5d42SMatthew Knepley PetscInt r; 2784c87e5d42SMatthew Knepley PetscErrorCode ierr; 2785c87e5d42SMatthew Knepley 2786c87e5d42SMatthew Knepley PetscFunctionBegin; 2787c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2788c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2789c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 2790c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2791c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2792c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2793c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2794c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2795c87e5d42SMatthew Knepley for(r = 0; r < n; ++r) { 2796c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2797c87e5d42SMatthew Knepley a[r] = diagA[r]; 2798c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2799c87e5d42SMatthew Knepley } else { 2800c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2801c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2802c87e5d42SMatthew Knepley } 2803c87e5d42SMatthew Knepley } 2804c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2805c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2806c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2807c87e5d42SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 2808c87e5d42SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 2809c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 2810c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2811c87e5d42SMatthew Knepley } 2812c87e5d42SMatthew Knepley 2813c87e5d42SMatthew Knepley #undef __FUNCT__ 2814829201f2SHong Zhang #define __FUNCT__ "MatGetSeqNonzerostructure_MPIAIJ" 2815f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIAIJ(Mat mat,Mat *newmat) 28165494a064SHong Zhang { 28175494a064SHong Zhang PetscErrorCode ierr; 2818f6d58c54SBarry Smith Mat *dummy; 28195494a064SHong Zhang 28205494a064SHong Zhang PetscFunctionBegin; 2821f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 2822f6d58c54SBarry Smith *newmat = *dummy; 2823f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 28245494a064SHong Zhang PetscFunctionReturn(0); 28255494a064SHong Zhang } 28265494a064SHong Zhang 28277087cfbeSBarry Smith extern PetscErrorCode MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 28288a729477SBarry Smith /* -------------------------------------------------------------------*/ 2829cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 2830cda55fadSBarry Smith MatGetRow_MPIAIJ, 2831cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 2832cda55fadSBarry Smith MatMult_MPIAIJ, 283397304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 28347c922b88SBarry Smith MatMultTranspose_MPIAIJ, 28357c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 2836103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2837103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 2838103bf8bdSMatthew Knepley #else 2839cda55fadSBarry Smith 0, 2840103bf8bdSMatthew Knepley #endif 2841cda55fadSBarry Smith 0, 2842cda55fadSBarry Smith 0, 284397304618SKris Buschelman /*10*/ 0, 2844cda55fadSBarry Smith 0, 2845cda55fadSBarry Smith 0, 284641f059aeSBarry Smith MatSOR_MPIAIJ, 2847b7c46309SBarry Smith MatTranspose_MPIAIJ, 284897304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 2849cda55fadSBarry Smith MatEqual_MPIAIJ, 2850cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 2851cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 2852cda55fadSBarry Smith MatNorm_MPIAIJ, 285397304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 2854cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 2855cda55fadSBarry Smith MatSetOption_MPIAIJ, 2856cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 2857d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 2858cda55fadSBarry Smith 0, 2859103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2860719d5645SBarry Smith 0, 2861103bf8bdSMatthew Knepley #else 2862cda55fadSBarry Smith 0, 2863103bf8bdSMatthew Knepley #endif 2864cda55fadSBarry Smith 0, 2865cda55fadSBarry Smith 0, 2866d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIAIJ, 2867103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2868719d5645SBarry Smith 0, 2869103bf8bdSMatthew Knepley #else 2870cda55fadSBarry Smith 0, 2871103bf8bdSMatthew Knepley #endif 2872cda55fadSBarry Smith 0, 2873cda55fadSBarry Smith 0, 2874cda55fadSBarry Smith 0, 2875d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 2876cda55fadSBarry Smith 0, 2877cda55fadSBarry Smith 0, 2878cda55fadSBarry Smith 0, 2879cda55fadSBarry Smith 0, 2880d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 2881cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 2882cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 2883cda55fadSBarry Smith MatGetValues_MPIAIJ, 2884cb5b572fSBarry Smith MatCopy_MPIAIJ, 2885d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 2886cda55fadSBarry Smith MatScale_MPIAIJ, 2887cda55fadSBarry Smith 0, 2888cda55fadSBarry Smith 0, 2889564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 2890d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_MPIAIJ, 2891cda55fadSBarry Smith 0, 2892cda55fadSBarry Smith 0, 2893cda55fadSBarry Smith 0, 2894cda55fadSBarry Smith 0, 2895d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 2896cda55fadSBarry Smith 0, 2897cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 289842e855d1Svictor MatPermute_MPIAIJ, 2899cda55fadSBarry Smith 0, 2900d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 2901e03a110bSBarry Smith MatDestroy_MPIAIJ, 2902e03a110bSBarry Smith MatView_MPIAIJ, 2903357abbc8SBarry Smith 0, 2904a2243be0SBarry Smith 0, 2905d519adbfSMatthew Knepley /*64*/ 0, 2906a2243be0SBarry Smith 0, 2907a2243be0SBarry Smith 0, 2908a2243be0SBarry Smith 0, 2909a2243be0SBarry Smith 0, 2910d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 2911c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 2912a2243be0SBarry Smith 0, 2913a2243be0SBarry Smith MatSetColoring_MPIAIJ, 2914dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2915779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 2916dcf5cc72SBarry Smith #else 2917dcf5cc72SBarry Smith 0, 2918dcf5cc72SBarry Smith #endif 291997304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 29203acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 292197304618SKris Buschelman 0, 292297304618SKris Buschelman 0, 292397304618SKris Buschelman 0, 292497304618SKris Buschelman 0, 292597304618SKris Buschelman /*80*/ 0, 292697304618SKris Buschelman 0, 292797304618SKris Buschelman 0, 29285bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 29296284ec50SHong Zhang 0, 29306284ec50SHong Zhang 0, 29316284ec50SHong Zhang 0, 29326284ec50SHong Zhang 0, 2933865e5f61SKris Buschelman 0, 2934d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 293526be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 293626be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 29377a7894deSKris Buschelman MatPtAP_Basic, 29387a7894deSKris Buschelman MatPtAPSymbolic_MPIAIJ, 2939d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ, 29407a7894deSKris Buschelman 0, 29417a7894deSKris Buschelman 0, 29427a7894deSKris Buschelman 0, 29437a7894deSKris Buschelman 0, 2944d519adbfSMatthew Knepley /*99*/ 0, 2945865e5f61SKris Buschelman MatPtAPSymbolic_MPIAIJ_MPIAIJ, 29467a7894deSKris Buschelman MatPtAPNumeric_MPIAIJ_MPIAIJ, 29472fd7e33dSBarry Smith MatConjugate_MPIAIJ, 29482fd7e33dSBarry Smith 0, 2949d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 295099cafbc1SBarry Smith MatRealPart_MPIAIJ, 295169db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 295269db28dcSHong Zhang 0, 295369db28dcSHong Zhang 0, 2954d519adbfSMatthew Knepley /*109*/0, 295503bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 29565494a064SHong Zhang MatGetRowMin_MPIAIJ, 29575494a064SHong Zhang 0, 29585494a064SHong Zhang 0, 2959bd0c2dcbSBarry Smith /*114*/MatGetSeqNonzerostructure_MPIAIJ, 2960bd0c2dcbSBarry Smith 0, 2961bd0c2dcbSBarry Smith 0, 2962bd0c2dcbSBarry Smith 0, 2963bd0c2dcbSBarry Smith 0, 29648fb81238SShri Abhyankar /*119*/0, 29658fb81238SShri Abhyankar 0, 29668fb81238SShri Abhyankar 0, 2967d6037b41SHong Zhang 0, 2968b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 2969b9614d88SDmitry Karpeev /*124*/0, 2970b9614d88SDmitry Karpeev 0, 2971b9614d88SDmitry Karpeev 0, 2972b9614d88SDmitry Karpeev 0, 2973b9614d88SDmitry Karpeev MatGetSubMatricesParallel_MPIAIJ 2974bd0c2dcbSBarry Smith }; 297536ce4990SBarry Smith 29762e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 29772e8a6d31SBarry Smith 2978fb2e594dSBarry Smith EXTERN_C_BEGIN 29794a2ae208SSatish Balay #undef __FUNCT__ 29804a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 29817087cfbeSBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat) 29822e8a6d31SBarry Smith { 29832e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2984dfbe8321SBarry Smith PetscErrorCode ierr; 29852e8a6d31SBarry Smith 29862e8a6d31SBarry Smith PetscFunctionBegin; 29872e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 29882e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 29892e8a6d31SBarry Smith PetscFunctionReturn(0); 29902e8a6d31SBarry Smith } 2991fb2e594dSBarry Smith EXTERN_C_END 29922e8a6d31SBarry Smith 2993fb2e594dSBarry Smith EXTERN_C_BEGIN 29944a2ae208SSatish Balay #undef __FUNCT__ 29954a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 29967087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat) 29972e8a6d31SBarry Smith { 29982e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2999dfbe8321SBarry Smith PetscErrorCode ierr; 30002e8a6d31SBarry Smith 30012e8a6d31SBarry Smith PetscFunctionBegin; 30022e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 30032e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 30042e8a6d31SBarry Smith PetscFunctionReturn(0); 30052e8a6d31SBarry Smith } 3006fb2e594dSBarry Smith EXTERN_C_END 30078a729477SBarry Smith 300827508adbSBarry Smith EXTERN_C_BEGIN 30094a2ae208SSatish Balay #undef __FUNCT__ 3010a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 30117087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3012a23d5eceSKris Buschelman { 3013a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3014dfbe8321SBarry Smith PetscErrorCode ierr; 3015b1d57f15SBarry Smith PetscInt i; 3016a23d5eceSKris Buschelman 3017a23d5eceSKris Buschelman PetscFunctionBegin; 3018a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 3019a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 3020e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 3021e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 3022899cda47SBarry Smith 302326283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 302426283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 302526283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 302626283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3027a23d5eceSKris Buschelman if (d_nnz) { 3028d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3029e32f2f54SBarry 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]); 3030a23d5eceSKris Buschelman } 3031a23d5eceSKris Buschelman } 3032a23d5eceSKris Buschelman if (o_nnz) { 3033d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3034e32f2f54SBarry 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]); 3035a23d5eceSKris Buschelman } 3036a23d5eceSKris Buschelman } 3037a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3038899cda47SBarry Smith 3039526dfc15SBarry Smith if (!B->preallocated) { 3040899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3041899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3042d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3043899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 3044899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3045899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3046d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3047899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 3048899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 3049526dfc15SBarry Smith } 3050899cda47SBarry Smith 3051c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3052c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 3053526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3054a23d5eceSKris Buschelman PetscFunctionReturn(0); 3055a23d5eceSKris Buschelman } 3056a23d5eceSKris Buschelman EXTERN_C_END 3057a23d5eceSKris Buschelman 30584a2ae208SSatish Balay #undef __FUNCT__ 30594a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3060dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3061d6dfbf8fSBarry Smith { 3062d6dfbf8fSBarry Smith Mat mat; 3063416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3064dfbe8321SBarry Smith PetscErrorCode ierr; 3065d6dfbf8fSBarry Smith 30663a40ed3dSBarry Smith PetscFunctionBegin; 3067416022c9SBarry Smith *newmat = 0; 30687adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 3069d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 30707adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 30711d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3072273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3073e1b6402fSHong Zhang 3074d5f3da31SBarry Smith mat->factortype = matin->factortype; 3075d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3076c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3077e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3078273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3079d6dfbf8fSBarry Smith 308017699dbbSLois Curfman McInnes a->size = oldmat->size; 308117699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3082e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3083e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3084e7641de0SSatish Balay a->rowindices = 0; 3085bcd2baecSBarry Smith a->rowvalues = 0; 3086bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3087d6dfbf8fSBarry Smith 308826283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 308926283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3090899cda47SBarry Smith 30912ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3092aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 30930f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3094b1fc9764SSatish Balay #else 3095d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 3096d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3097d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3098b1fc9764SSatish Balay #endif 3099416022c9SBarry Smith } else a->colmap = 0; 31003f41c07dSBarry Smith if (oldmat->garray) { 3101b1d57f15SBarry Smith PetscInt len; 3102d0f46423SBarry Smith len = oldmat->B->cmap->n; 3103b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 310452e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3105b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3106416022c9SBarry Smith } else a->garray = 0; 3107d6dfbf8fSBarry Smith 3108416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 310952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 3110a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 311152e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 31122e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 311352e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 31142e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 311552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 31167adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 31178a729477SBarry Smith *newmat = mat; 31183a40ed3dSBarry Smith PetscFunctionReturn(0); 31198a729477SBarry Smith } 3120416022c9SBarry Smith 31211a4ee126SBarry Smith /* 31221a4ee126SBarry Smith Allows sending/receiving larger messages then 2 gigabytes in a single call 31231a4ee126SBarry Smith */ 31241a4ee126SBarry Smith static int MPILong_Send(void *mess,PetscInt cnt, MPI_Datatype type,int to, int tag, MPI_Comm comm) 31251a4ee126SBarry Smith { 31261a4ee126SBarry Smith int ierr; 31271a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 31281a4ee126SBarry Smith PetscInt i,numchunks; 31291a4ee126SBarry Smith PetscMPIInt icnt; 31301a4ee126SBarry Smith 31311a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 31321a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 31331a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31341a4ee126SBarry Smith ierr = MPI_Send(mess,icnt,type,to,tag,comm); 31351a4ee126SBarry Smith if (type == MPIU_INT) { 31361a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31371a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31381a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31391a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31401a4ee126SBarry Smith } 31411a4ee126SBarry Smith return 0; 31421a4ee126SBarry Smith } 31431a4ee126SBarry Smith static int MPILong_Recv(void *mess,PetscInt cnt, MPI_Datatype type,int from, int tag, MPI_Comm comm) 31441a4ee126SBarry Smith { 31451a4ee126SBarry Smith int ierr; 31461a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 31471a4ee126SBarry Smith MPI_Status status; 31481a4ee126SBarry Smith PetscInt i,numchunks; 31491a4ee126SBarry Smith PetscMPIInt icnt; 31501a4ee126SBarry Smith 31511a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 31521a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 31531a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31541a4ee126SBarry Smith ierr = MPI_Recv(mess,icnt,type,from,tag,comm,&status); 31551a4ee126SBarry Smith if (type == MPIU_INT) { 31561a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31571a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31581a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31591a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31601a4ee126SBarry Smith } 31611a4ee126SBarry Smith return 0; 31621a4ee126SBarry Smith } 31631a4ee126SBarry Smith 31644a2ae208SSatish Balay #undef __FUNCT__ 31655bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3166112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 31678fb81238SShri Abhyankar { 31688fb81238SShri Abhyankar PetscScalar *vals,*svals; 31698fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 31708fb81238SShri Abhyankar PetscErrorCode ierr; 31711a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 31728fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 31738fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 31748fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 31758fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 31768fb81238SShri Abhyankar int fd; 31778fb81238SShri Abhyankar 31788fb81238SShri Abhyankar PetscFunctionBegin; 31798fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 31808fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 31818fb81238SShri Abhyankar if (!rank) { 31828fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 31838fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 31848fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 31858fb81238SShri Abhyankar } 31868fb81238SShri Abhyankar 31878fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 31888fb81238SShri Abhyankar 31898fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 31908fb81238SShri Abhyankar M = header[1]; N = header[2]; 31918fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 31928fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 31938fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 31948fb81238SShri Abhyankar 31958fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 31968fb81238SShri Abhyankar if (sizesset) { 31978fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 31988fb81238SShri Abhyankar } 3199abd38a8fSBarry Smith if (sizesset && newMat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows); 3200abd38a8fSBarry Smith if (sizesset && newMat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols); 32018fb81238SShri Abhyankar 32028fb81238SShri Abhyankar /* determine ownership of all rows */ 32038fb81238SShri Abhyankar if (newMat->rmap->n < 0 ) m = M/size + ((M % size) > rank); /* PETSC_DECIDE */ 32044683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 32058fb81238SShri Abhyankar 32068fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 32078fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 32088fb81238SShri Abhyankar 32098fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 32108fb81238SShri Abhyankar if (!rank) { 32118fb81238SShri Abhyankar mmax = rowners[1]; 32128fb81238SShri Abhyankar for (i=2; i<size; i++) { 32138fb81238SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 32148fb81238SShri Abhyankar } 32158fb81238SShri Abhyankar } else mmax = m; 32168fb81238SShri Abhyankar 32178fb81238SShri Abhyankar rowners[0] = 0; 32188fb81238SShri Abhyankar for (i=2; i<=size; i++) { 32198fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 32208fb81238SShri Abhyankar } 32218fb81238SShri Abhyankar rstart = rowners[rank]; 32228fb81238SShri Abhyankar rend = rowners[rank+1]; 32238fb81238SShri Abhyankar 32248fb81238SShri Abhyankar /* distribute row lengths to all processors */ 32258fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 32268fb81238SShri Abhyankar if (!rank) { 32278fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 32288fb81238SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 32298fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 32308fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 32318fb81238SShri Abhyankar for (j=0; j<m; j++) { 32328fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 32338fb81238SShri Abhyankar } 32348fb81238SShri Abhyankar for (i=1; i<size; i++) { 32358fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 32368fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 32378fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 32388fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 32398fb81238SShri Abhyankar } 32401a4ee126SBarry Smith ierr = MPILong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32418fb81238SShri Abhyankar } 32428fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 32438fb81238SShri Abhyankar } else { 32441a4ee126SBarry Smith ierr = MPILong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32458fb81238SShri Abhyankar } 32468fb81238SShri Abhyankar 32478fb81238SShri Abhyankar if (!rank) { 32488fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 32498fb81238SShri Abhyankar maxnz = 0; 32508fb81238SShri Abhyankar for (i=0; i<size; i++) { 32518fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 32528fb81238SShri Abhyankar } 32538fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 32548fb81238SShri Abhyankar 32558fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 32568fb81238SShri Abhyankar nz = procsnz[0]; 32578fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32588fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 32598fb81238SShri Abhyankar 32608fb81238SShri Abhyankar /* read in every one elses and ship off */ 32618fb81238SShri Abhyankar for (i=1; i<size; i++) { 32628fb81238SShri Abhyankar nz = procsnz[i]; 32638fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 32641a4ee126SBarry Smith ierr = MPILong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32658fb81238SShri Abhyankar } 32668fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 32678fb81238SShri Abhyankar } else { 32688fb81238SShri Abhyankar /* determine buffer space needed for message */ 32698fb81238SShri Abhyankar nz = 0; 32708fb81238SShri Abhyankar for (i=0; i<m; i++) { 32718fb81238SShri Abhyankar nz += ourlens[i]; 32728fb81238SShri Abhyankar } 32738fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32748fb81238SShri Abhyankar 32758fb81238SShri Abhyankar /* receive message of column indices*/ 32761a4ee126SBarry Smith ierr = MPILong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32778fb81238SShri Abhyankar } 32788fb81238SShri Abhyankar 32798fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 32808fb81238SShri Abhyankar if (N != M) { 32818fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 32828fb81238SShri Abhyankar else n = newMat->cmap->n; 32838fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 32848fb81238SShri Abhyankar cstart = cend - n; 32858fb81238SShri Abhyankar } else { 32868fb81238SShri Abhyankar cstart = rstart; 32878fb81238SShri Abhyankar cend = rend; 32888fb81238SShri Abhyankar n = cend - cstart; 32898fb81238SShri Abhyankar } 32908fb81238SShri Abhyankar 32918fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 32928fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 32938fb81238SShri Abhyankar jj = 0; 32948fb81238SShri Abhyankar for (i=0; i<m; i++) { 32958fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 32968fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 32978fb81238SShri Abhyankar jj++; 32988fb81238SShri Abhyankar } 32998fb81238SShri Abhyankar } 33008fb81238SShri Abhyankar 33018fb81238SShri Abhyankar for (i=0; i<m; i++) { 33028fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 33038fb81238SShri Abhyankar } 33048fb81238SShri Abhyankar if (!sizesset) { 33058fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 33068fb81238SShri Abhyankar } 33078fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 33088fb81238SShri Abhyankar 33098fb81238SShri Abhyankar for (i=0; i<m; i++) { 33108fb81238SShri Abhyankar ourlens[i] += offlens[i]; 33118fb81238SShri Abhyankar } 33128fb81238SShri Abhyankar 33138fb81238SShri Abhyankar if (!rank) { 33148fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 33158fb81238SShri Abhyankar 33168fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 33178fb81238SShri Abhyankar nz = procsnz[0]; 33188fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 33198fb81238SShri Abhyankar 33208fb81238SShri Abhyankar /* insert into matrix */ 33218fb81238SShri Abhyankar jj = rstart; 33228fb81238SShri Abhyankar smycols = mycols; 33238fb81238SShri Abhyankar svals = vals; 33248fb81238SShri Abhyankar for (i=0; i<m; i++) { 33258fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 33268fb81238SShri Abhyankar smycols += ourlens[i]; 33278fb81238SShri Abhyankar svals += ourlens[i]; 33288fb81238SShri Abhyankar jj++; 33298fb81238SShri Abhyankar } 33308fb81238SShri Abhyankar 33318fb81238SShri Abhyankar /* read in other processors and ship out */ 33328fb81238SShri Abhyankar for (i=1; i<size; i++) { 33338fb81238SShri Abhyankar nz = procsnz[i]; 33348fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 33351a4ee126SBarry Smith ierr = MPILong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33368fb81238SShri Abhyankar } 33378fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 33388fb81238SShri Abhyankar } else { 33398fb81238SShri Abhyankar /* receive numeric values */ 33408fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 33418fb81238SShri Abhyankar 33428fb81238SShri Abhyankar /* receive message of values*/ 33431a4ee126SBarry Smith ierr = MPILong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33448fb81238SShri Abhyankar 33458fb81238SShri Abhyankar /* insert into matrix */ 33468fb81238SShri Abhyankar jj = rstart; 33478fb81238SShri Abhyankar smycols = mycols; 33488fb81238SShri Abhyankar svals = vals; 33498fb81238SShri Abhyankar for (i=0; i<m; i++) { 33508fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 33518fb81238SShri Abhyankar smycols += ourlens[i]; 33528fb81238SShri Abhyankar svals += ourlens[i]; 33538fb81238SShri Abhyankar jj++; 33548fb81238SShri Abhyankar } 33558fb81238SShri Abhyankar } 33568fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 33578fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 33588fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 33598fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 33608fb81238SShri Abhyankar 33618fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33628fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33638fb81238SShri Abhyankar PetscFunctionReturn(0); 33648fb81238SShri Abhyankar } 33658fb81238SShri Abhyankar 33668fb81238SShri Abhyankar #undef __FUNCT__ 33674a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 33684aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 33694aa3045dSJed Brown { 33704aa3045dSJed Brown PetscErrorCode ierr; 33714aa3045dSJed Brown IS iscol_local; 33724aa3045dSJed Brown PetscInt csize; 33734aa3045dSJed Brown 33744aa3045dSJed Brown PetscFunctionBegin; 33754aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3376b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3377b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3378e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3379b79d0421SJed Brown } else { 33804aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3381b79d0421SJed Brown } 33824aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3383b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3384b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 33854aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 3386b79d0421SJed Brown } 33874aa3045dSJed Brown PetscFunctionReturn(0); 33884aa3045dSJed Brown } 33894aa3045dSJed Brown 33904aa3045dSJed Brown #undef __FUNCT__ 33914aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3392a0ff6018SBarry Smith /* 339329da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 339429da9460SBarry Smith in local and then by concatenating the local matrices the end result. 339529da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 33964aa3045dSJed Brown 33974aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3398a0ff6018SBarry Smith */ 33994aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3400a0ff6018SBarry Smith { 3401dfbe8321SBarry Smith PetscErrorCode ierr; 340232dcc486SBarry Smith PetscMPIInt rank,size; 3403b1d57f15SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j; 3404b1d57f15SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 3405fee21e36SBarry Smith Mat *local,M,Mreuse; 3406a77337e4SBarry Smith MatScalar *vwork,*aa; 34077adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 340800e6dbe6SBarry Smith Mat_SeqAIJ *aij; 34097e2c5f70SBarry Smith 3410a0ff6018SBarry Smith 3411a0ff6018SBarry Smith PetscFunctionBegin; 34121dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 34131dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 341400e6dbe6SBarry Smith 3415fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3416fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3417e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3418fee21e36SBarry Smith local = &Mreuse; 3419fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 3420fee21e36SBarry Smith } else { 3421a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3422fee21e36SBarry Smith Mreuse = *local; 3423606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 3424fee21e36SBarry Smith } 3425a0ff6018SBarry Smith 3426a0ff6018SBarry Smith /* 3427a0ff6018SBarry Smith m - number of local rows 3428a0ff6018SBarry Smith n - number of columns (same on all processors) 3429a0ff6018SBarry Smith rstart - first row in new global matrix generated 3430a0ff6018SBarry Smith */ 3431fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3432a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3433fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 343400e6dbe6SBarry Smith ii = aij->i; 343500e6dbe6SBarry Smith jj = aij->j; 343600e6dbe6SBarry Smith 3437a0ff6018SBarry Smith /* 343800e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 343900e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3440a0ff6018SBarry Smith */ 344100e6dbe6SBarry Smith 344200e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 34436a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3444ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3445ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3446e2c4fddaSBarry Smith nlocal = m; 34476a6a5d1dSBarry Smith } else { 3448ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3449ab50ec6bSBarry Smith } 3450ab50ec6bSBarry Smith } else { 34516a6a5d1dSBarry Smith nlocal = csize; 34526a6a5d1dSBarry Smith } 3453b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 345400e6dbe6SBarry Smith rstart = rend - nlocal; 345565e19b50SBarry 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); 345600e6dbe6SBarry Smith 345700e6dbe6SBarry Smith /* next, compute all the lengths */ 3458b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 345900e6dbe6SBarry Smith olens = dlens + m; 346000e6dbe6SBarry Smith for (i=0; i<m; i++) { 346100e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 346200e6dbe6SBarry Smith olen = 0; 346300e6dbe6SBarry Smith dlen = 0; 346400e6dbe6SBarry Smith for (j=0; j<jend; j++) { 346500e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 346600e6dbe6SBarry Smith else dlen++; 346700e6dbe6SBarry Smith jj++; 346800e6dbe6SBarry Smith } 346900e6dbe6SBarry Smith olens[i] = olen; 347000e6dbe6SBarry Smith dlens[i] = dlen; 347100e6dbe6SBarry Smith } 3472f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3473f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 34747adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3475e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3476606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3477a0ff6018SBarry Smith } else { 3478b1d57f15SBarry Smith PetscInt ml,nl; 3479a0ff6018SBarry Smith 3480a0ff6018SBarry Smith M = *newmat; 3481a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3482e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3483a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3484c48de900SBarry Smith /* 3485c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3486c48de900SBarry Smith rather than the slower MatSetValues(). 3487c48de900SBarry Smith */ 3488c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3489c48de900SBarry Smith M->assembled = PETSC_FALSE; 3490a0ff6018SBarry Smith } 3491a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3492fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 349300e6dbe6SBarry Smith ii = aij->i; 349400e6dbe6SBarry Smith jj = aij->j; 349500e6dbe6SBarry Smith aa = aij->a; 3496a0ff6018SBarry Smith for (i=0; i<m; i++) { 3497a0ff6018SBarry Smith row = rstart + i; 349800e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 349900e6dbe6SBarry Smith cwork = jj; jj += nz; 350000e6dbe6SBarry Smith vwork = aa; aa += nz; 35018c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3502a0ff6018SBarry Smith } 3503a0ff6018SBarry Smith 3504a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3505a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3506a0ff6018SBarry Smith *newmat = M; 3507fee21e36SBarry Smith 3508fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3509fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3510fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3511fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 3512fee21e36SBarry Smith } 3513fee21e36SBarry Smith 3514a0ff6018SBarry Smith PetscFunctionReturn(0); 3515a0ff6018SBarry Smith } 3516273d9f13SBarry Smith 3517e2e86b8fSSatish Balay EXTERN_C_BEGIN 35184a2ae208SSatish Balay #undef __FUNCT__ 3519ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 35207087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3521ccd8e176SBarry Smith { 3522899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3523899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3524ccd8e176SBarry Smith const PetscInt *JJ; 3525ccd8e176SBarry Smith PetscScalar *values; 3526ccd8e176SBarry Smith PetscErrorCode ierr; 3527ccd8e176SBarry Smith 3528ccd8e176SBarry Smith PetscFunctionBegin; 3529e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3530899cda47SBarry Smith 353126283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 353226283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 353326283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 353426283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3535d0f46423SBarry Smith m = B->rmap->n; 3536d0f46423SBarry Smith cstart = B->cmap->rstart; 3537d0f46423SBarry Smith cend = B->cmap->rend; 3538d0f46423SBarry Smith rstart = B->rmap->rstart; 3539899cda47SBarry Smith 35401d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3541ccd8e176SBarry Smith 3542ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3543ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3544ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3545ecc77c7aSBarry Smith JJ = J + Ii[i]; 3546e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3547ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3548d0f46423SBarry 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); 3549ecc77c7aSBarry Smith } 3550ecc77c7aSBarry Smith #endif 3551ecc77c7aSBarry Smith 3552ccd8e176SBarry Smith for (i=0; i<m; i++) { 3553b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3554b7940d39SSatish Balay JJ = J + Ii[i]; 3555ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3556ccd8e176SBarry Smith d = 0; 35570daa03b5SJed Brown for (j=0; j<nnz; j++) { 35580daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3559ccd8e176SBarry Smith } 3560ccd8e176SBarry Smith d_nnz[i] = d; 3561ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3562ccd8e176SBarry Smith } 3563ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 35641d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3565ccd8e176SBarry Smith 3566ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3567ccd8e176SBarry Smith else { 3568ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3569ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3570ccd8e176SBarry Smith } 3571ccd8e176SBarry Smith 3572ccd8e176SBarry Smith for (i=0; i<m; i++) { 3573ccd8e176SBarry Smith ii = i + rstart; 3574b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3575b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3576ccd8e176SBarry Smith } 3577ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3578ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3579ccd8e176SBarry Smith 3580ccd8e176SBarry Smith if (!v) { 3581ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3582ccd8e176SBarry Smith } 3583ccd8e176SBarry Smith PetscFunctionReturn(0); 3584ccd8e176SBarry Smith } 3585e2e86b8fSSatish Balay EXTERN_C_END 3586ccd8e176SBarry Smith 3587ccd8e176SBarry Smith #undef __FUNCT__ 3588ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 35891eea217eSSatish Balay /*@ 3590ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3591ccd8e176SBarry Smith (the default parallel PETSc format). 3592ccd8e176SBarry Smith 3593ccd8e176SBarry Smith Collective on MPI_Comm 3594ccd8e176SBarry Smith 3595ccd8e176SBarry Smith Input Parameters: 3596a1661176SMatthew Knepley + B - the matrix 3597ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 35980daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3599ccd8e176SBarry Smith - v - optional values in the matrix 3600ccd8e176SBarry Smith 3601ccd8e176SBarry Smith Level: developer 3602ccd8e176SBarry Smith 360312251496SSatish Balay Notes: 360412251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 360512251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 360612251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 360712251496SSatish Balay 360812251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 360912251496SSatish Balay 361012251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 361112251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 361212251496SSatish Balay as shown: 361312251496SSatish Balay 361412251496SSatish Balay 1 0 0 361512251496SSatish Balay 2 0 3 P0 361612251496SSatish Balay ------- 361712251496SSatish Balay 4 5 6 P1 361812251496SSatish Balay 361912251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 362012251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 362112251496SSatish Balay j = {0,0,2} [size = nz = 6] 362212251496SSatish Balay v = {1,2,3} [size = nz = 6] 362312251496SSatish Balay 362412251496SSatish Balay Process1 [P1]: rows_owned=[2] 362512251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 362612251496SSatish Balay j = {0,1,2} [size = nz = 6] 362712251496SSatish Balay v = {4,5,6} [size = nz = 6] 362812251496SSatish Balay 3629ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3630ccd8e176SBarry Smith 36312fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ, 36328d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3633ccd8e176SBarry Smith @*/ 36347087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3635ccd8e176SBarry Smith { 36364ac538c5SBarry Smith PetscErrorCode ierr; 3637ccd8e176SBarry Smith 3638ccd8e176SBarry Smith PetscFunctionBegin; 36394ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3640ccd8e176SBarry Smith PetscFunctionReturn(0); 3641ccd8e176SBarry Smith } 3642ccd8e176SBarry Smith 3643ccd8e176SBarry Smith #undef __FUNCT__ 36444a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3645273d9f13SBarry Smith /*@C 3646ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3647273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3648273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3649273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3650273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3651273d9f13SBarry Smith 3652273d9f13SBarry Smith Collective on MPI_Comm 3653273d9f13SBarry Smith 3654273d9f13SBarry Smith Input Parameters: 3655273d9f13SBarry Smith + A - the matrix 3656273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3657273d9f13SBarry Smith (same value is used for all local rows) 3658273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3659273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3660273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3661273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3662273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3663273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3664273d9f13SBarry Smith submatrix (same value is used for all local rows). 3665273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3666273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3667273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3668273d9f13SBarry Smith structure. The size of this array is equal to the number 3669273d9f13SBarry Smith of local rows, i.e 'm'. 3670273d9f13SBarry Smith 367149a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 367249a6f317SBarry Smith 3673273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3674ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 36750598bfebSBarry Smith storage. The stored row and column indices begin with zero. 36760598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3677273d9f13SBarry Smith 3678273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3679273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3680273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3681273d9f13SBarry Smith 3682273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3683a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3684a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3685a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3686a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3687a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3688a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3689a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3690a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3691273d9f13SBarry Smith 3692273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3693273d9f13SBarry Smith 3694aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3695aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3696aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3697aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3698aa95bbe8SBarry Smith 3699273d9f13SBarry Smith Example usage: 3700273d9f13SBarry Smith 3701273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3702273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3703273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3704273d9f13SBarry Smith as follows: 3705273d9f13SBarry Smith 3706273d9f13SBarry Smith .vb 3707273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3708273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3709273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3710273d9f13SBarry Smith ------------------------------------- 3711273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3712273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3713273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3714273d9f13SBarry Smith ------------------------------------- 3715273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3716273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3717273d9f13SBarry Smith .ve 3718273d9f13SBarry Smith 3719273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3720273d9f13SBarry Smith 3721273d9f13SBarry Smith .vb 3722273d9f13SBarry Smith A B C 3723273d9f13SBarry Smith D E F 3724273d9f13SBarry Smith G H I 3725273d9f13SBarry Smith .ve 3726273d9f13SBarry Smith 3727273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3728273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3729273d9f13SBarry Smith 3730273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3731273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3732273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3733273d9f13SBarry Smith 3734273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3735273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3736273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3737273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3738273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3739273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3740273d9f13SBarry Smith 3741273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3742273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3743273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3744273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3745273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3746273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3747273d9f13SBarry Smith .vb 3748273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3749273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3750273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3751273d9f13SBarry Smith .ve 3752273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3753273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3754273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3755273d9f13SBarry Smith 34 values. 3756273d9f13SBarry Smith 3757273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3758273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3759273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3760273d9f13SBarry Smith .vb 3761273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3762273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3763273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3764273d9f13SBarry Smith .ve 3765273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3766273d9f13SBarry Smith hence pre-allocation is perfect. 3767273d9f13SBarry Smith 3768273d9f13SBarry Smith Level: intermediate 3769273d9f13SBarry Smith 3770273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3771273d9f13SBarry Smith 3772ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(), 3773aa95bbe8SBarry Smith MPIAIJ, MatGetInfo() 3774273d9f13SBarry Smith @*/ 37757087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3776273d9f13SBarry Smith { 37774ac538c5SBarry Smith PetscErrorCode ierr; 3778273d9f13SBarry Smith 3779273d9f13SBarry Smith PetscFunctionBegin; 37804ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 3781273d9f13SBarry Smith PetscFunctionReturn(0); 3782273d9f13SBarry Smith } 3783273d9f13SBarry Smith 37844a2ae208SSatish Balay #undef __FUNCT__ 37852fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 378658d36128SBarry Smith /*@ 37872fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 37882fb0ec9aSBarry Smith CSR format the local rows. 37892fb0ec9aSBarry Smith 37902fb0ec9aSBarry Smith Collective on MPI_Comm 37912fb0ec9aSBarry Smith 37922fb0ec9aSBarry Smith Input Parameters: 37932fb0ec9aSBarry Smith + comm - MPI communicator 37942fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 37952fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 37962fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 37972fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 37982fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 37992fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 38002fb0ec9aSBarry Smith . i - row indices 38012fb0ec9aSBarry Smith . j - column indices 38022fb0ec9aSBarry Smith - a - matrix values 38032fb0ec9aSBarry Smith 38042fb0ec9aSBarry Smith Output Parameter: 38052fb0ec9aSBarry Smith . mat - the matrix 380603bfb495SBarry Smith 38072fb0ec9aSBarry Smith Level: intermediate 38082fb0ec9aSBarry Smith 38092fb0ec9aSBarry Smith Notes: 38102fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 38112fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 38128d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 38132fb0ec9aSBarry Smith 381412251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 381512251496SSatish Balay 381612251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 381712251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 381812251496SSatish Balay as shown: 381912251496SSatish Balay 382012251496SSatish Balay 1 0 0 382112251496SSatish Balay 2 0 3 P0 382212251496SSatish Balay ------- 382312251496SSatish Balay 4 5 6 P1 382412251496SSatish Balay 382512251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 382612251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 382712251496SSatish Balay j = {0,0,2} [size = nz = 6] 382812251496SSatish Balay v = {1,2,3} [size = nz = 6] 382912251496SSatish Balay 383012251496SSatish Balay Process1 [P1]: rows_owned=[2] 383112251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 383212251496SSatish Balay j = {0,1,2} [size = nz = 6] 383312251496SSatish Balay v = {4,5,6} [size = nz = 6] 38342fb0ec9aSBarry Smith 38352fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 38362fb0ec9aSBarry Smith 38372fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 38388d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 38392fb0ec9aSBarry Smith @*/ 38407087cfbeSBarry Smith PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat) 38412fb0ec9aSBarry Smith { 38422fb0ec9aSBarry Smith PetscErrorCode ierr; 38432fb0ec9aSBarry Smith 38442fb0ec9aSBarry Smith PetscFunctionBegin; 38452fb0ec9aSBarry Smith if (i[0]) { 3846e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 38472fb0ec9aSBarry Smith } 3848e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 38492fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3850d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 38512fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 38522fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 38532fb0ec9aSBarry Smith PetscFunctionReturn(0); 38542fb0ec9aSBarry Smith } 38552fb0ec9aSBarry Smith 38562fb0ec9aSBarry Smith #undef __FUNCT__ 38574a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 3858273d9f13SBarry Smith /*@C 3859273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 3860273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3861273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3862273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3863273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3864273d9f13SBarry Smith 3865273d9f13SBarry Smith Collective on MPI_Comm 3866273d9f13SBarry Smith 3867273d9f13SBarry Smith Input Parameters: 3868273d9f13SBarry Smith + comm - MPI communicator 3869273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 3870273d9f13SBarry Smith This value should be the same as the local size used in creating the 3871273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 3872273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 3873273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 3874273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 3875273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3876273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 3877273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3878273d9f13SBarry Smith (same value is used for all local rows) 3879273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3880273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3881273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3882273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3883273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3884273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3885273d9f13SBarry Smith submatrix (same value is used for all local rows). 3886273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3887273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3888273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3889273d9f13SBarry Smith structure. The size of this array is equal to the number 3890273d9f13SBarry Smith of local rows, i.e 'm'. 3891273d9f13SBarry Smith 3892273d9f13SBarry Smith Output Parameter: 3893273d9f13SBarry Smith . A - the matrix 3894273d9f13SBarry Smith 3895175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3896ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3897175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3898175b88e8SBarry Smith 3899273d9f13SBarry Smith Notes: 390049a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 390149a6f317SBarry Smith 3902273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 3903273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 3904273d9f13SBarry Smith storage requirements for this matrix. 3905273d9f13SBarry Smith 3906273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 3907273d9f13SBarry Smith processor than it must be used on all processors that share the object for 3908273d9f13SBarry Smith that argument. 3909273d9f13SBarry Smith 3910273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 3911273d9f13SBarry Smith (possibly both). 3912273d9f13SBarry Smith 391333a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 391433a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 391533a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 391633a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 391733a7c187SSatish Balay values corresponding to [m x N] submatrix. 3918273d9f13SBarry Smith 391933a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 392033a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 392133a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 392233a7c187SSatish Balay 392333a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 392433a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 392533a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 392633a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 392733a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 392833a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 392933a7c187SSatish Balay illustrates this concept. 393033a7c187SSatish Balay 393133a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 393233a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 393333a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 393433a7c187SSatish Balay local matrix (a rectangular submatrix). 3935273d9f13SBarry Smith 3936273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3937273d9f13SBarry Smith 393897d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 393997d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 394097d05335SKris Buschelman type of communicator, use the construction mechanism: 394178102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 394297d05335SKris Buschelman 3943273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 3944273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 3945273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3946273d9f13SBarry Smith 3947273d9f13SBarry Smith Options Database Keys: 3948923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 3949923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3950273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3951273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3952273d9f13SBarry Smith the user still MUST index entries starting at 0! 3953273d9f13SBarry Smith 3954273d9f13SBarry Smith 3955273d9f13SBarry Smith Example usage: 3956273d9f13SBarry Smith 3957273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3958273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3959273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3960273d9f13SBarry Smith as follows: 3961273d9f13SBarry Smith 3962273d9f13SBarry Smith .vb 3963273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3964273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3965273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3966273d9f13SBarry Smith ------------------------------------- 3967273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3968273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3969273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3970273d9f13SBarry Smith ------------------------------------- 3971273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3972273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3973273d9f13SBarry Smith .ve 3974273d9f13SBarry Smith 3975273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3976273d9f13SBarry Smith 3977273d9f13SBarry Smith .vb 3978273d9f13SBarry Smith A B C 3979273d9f13SBarry Smith D E F 3980273d9f13SBarry Smith G H I 3981273d9f13SBarry Smith .ve 3982273d9f13SBarry Smith 3983273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3984273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3985273d9f13SBarry Smith 3986273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3987273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3988273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3989273d9f13SBarry Smith 3990273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3991273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3992273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3993273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3994273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3995273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3996273d9f13SBarry Smith 3997273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3998273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3999273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4000273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4001273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4002273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4003273d9f13SBarry Smith .vb 4004273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4005273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4006273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4007273d9f13SBarry Smith .ve 4008273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4009273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4010273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4011273d9f13SBarry Smith 34 values. 4012273d9f13SBarry Smith 4013273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4014273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4015273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4016273d9f13SBarry Smith .vb 4017273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4018273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4019273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4020273d9f13SBarry Smith .ve 4021273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4022273d9f13SBarry Smith hence pre-allocation is perfect. 4023273d9f13SBarry Smith 4024273d9f13SBarry Smith Level: intermediate 4025273d9f13SBarry Smith 4026273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4027273d9f13SBarry Smith 4028ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 40292fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4030273d9f13SBarry Smith @*/ 40317087cfbeSBarry Smith PetscErrorCode 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) 4032273d9f13SBarry Smith { 40336849ba73SBarry Smith PetscErrorCode ierr; 4034b1d57f15SBarry Smith PetscMPIInt size; 4035273d9f13SBarry Smith 4036273d9f13SBarry Smith PetscFunctionBegin; 4037f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4038f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4039273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4040273d9f13SBarry Smith if (size > 1) { 4041273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4042273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4043273d9f13SBarry Smith } else { 4044273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4045273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4046273d9f13SBarry Smith } 4047273d9f13SBarry Smith PetscFunctionReturn(0); 4048273d9f13SBarry Smith } 4049195d93cdSBarry Smith 40504a2ae208SSatish Balay #undef __FUNCT__ 40514a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 40527087cfbeSBarry Smith PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 4053195d93cdSBarry Smith { 4054195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 4055b1d57f15SBarry Smith 4056195d93cdSBarry Smith PetscFunctionBegin; 4057195d93cdSBarry Smith *Ad = a->A; 4058195d93cdSBarry Smith *Ao = a->B; 4059195d93cdSBarry Smith *colmap = a->garray; 4060195d93cdSBarry Smith PetscFunctionReturn(0); 4061195d93cdSBarry Smith } 4062a2243be0SBarry Smith 4063a2243be0SBarry Smith #undef __FUNCT__ 4064a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4065dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4066a2243be0SBarry Smith { 4067dfbe8321SBarry Smith PetscErrorCode ierr; 4068b1d57f15SBarry Smith PetscInt i; 4069a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4070a2243be0SBarry Smith 4071a2243be0SBarry Smith PetscFunctionBegin; 40728ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 407308b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4074a2243be0SBarry Smith ISColoring ocoloring; 4075a2243be0SBarry Smith 4076a2243be0SBarry Smith /* set coloring for diagonal portion */ 4077a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4078a2243be0SBarry Smith 4079a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 40807adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 4081d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4082d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4083a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4084a2243be0SBarry Smith } 4085a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4086d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4087a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4088a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4089a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 409008b6dcc0SBarry Smith ISColoringValue *colors; 4091b1d57f15SBarry Smith PetscInt *larray; 4092a2243be0SBarry Smith ISColoring ocoloring; 4093a2243be0SBarry Smith 4094a2243be0SBarry Smith /* set coloring for diagonal portion */ 4095d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4096d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4097d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4098a2243be0SBarry Smith } 4099784ac674SJed Brown ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 4100d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4101d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4102a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4103a2243be0SBarry Smith } 4104a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4105d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4106a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 4107a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4108a2243be0SBarry Smith 4109a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4110d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4111784ac674SJed Brown ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 4112d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4113d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4114a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4115a2243be0SBarry Smith } 4116a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4117d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4118a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4119a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4120a2243be0SBarry Smith } else { 4121e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4122a2243be0SBarry Smith } 4123a2243be0SBarry Smith 4124a2243be0SBarry Smith PetscFunctionReturn(0); 4125a2243be0SBarry Smith } 4126a2243be0SBarry Smith 4127dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 4128a2243be0SBarry Smith #undef __FUNCT__ 4129779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 4130dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 4131a2243be0SBarry Smith { 4132a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4133dfbe8321SBarry Smith PetscErrorCode ierr; 4134a2243be0SBarry Smith 4135a2243be0SBarry Smith PetscFunctionBegin; 4136779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 4137779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 4138779c1a83SBarry Smith PetscFunctionReturn(0); 4139779c1a83SBarry Smith } 4140dcf5cc72SBarry Smith #endif 4141779c1a83SBarry Smith 4142779c1a83SBarry Smith #undef __FUNCT__ 4143779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4144b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4145779c1a83SBarry Smith { 4146779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4147dfbe8321SBarry Smith PetscErrorCode ierr; 4148779c1a83SBarry Smith 4149779c1a83SBarry Smith PetscFunctionBegin; 4150779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4151779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4152a2243be0SBarry Smith PetscFunctionReturn(0); 4153a2243be0SBarry Smith } 4154c5d6d63eSBarry Smith 4155c5d6d63eSBarry Smith #undef __FUNCT__ 415651dd7536SBarry Smith #define __FUNCT__ "MatMerge" 4157bc08b0f1SBarry Smith /*@ 415851dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 415951dd7536SBarry Smith matrices from each processor 4160c5d6d63eSBarry Smith 4161c5d6d63eSBarry Smith Collective on MPI_Comm 4162c5d6d63eSBarry Smith 4163c5d6d63eSBarry Smith Input Parameters: 416451dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4165d6bb3c2dSHong Zhang . inmat - the input sequential matrices 41660e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4167d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 416851dd7536SBarry Smith 416951dd7536SBarry Smith Output Parameter: 417051dd7536SBarry Smith . outmat - the parallel matrix generated 4171c5d6d63eSBarry Smith 41727e25d530SSatish Balay Level: advanced 41737e25d530SSatish Balay 4174f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4175c5d6d63eSBarry Smith 4176c5d6d63eSBarry Smith @*/ 41777087cfbeSBarry Smith PetscErrorCode MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4178c5d6d63eSBarry Smith { 4179dfbe8321SBarry Smith PetscErrorCode ierr; 4180b7940d39SSatish Balay PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz; 4181ba8c8a56SBarry Smith PetscInt *indx; 4182ba8c8a56SBarry Smith PetscScalar *values; 4183c5d6d63eSBarry Smith 4184c5d6d63eSBarry Smith PetscFunctionBegin; 41850e36024fSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4186d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4187d6bb3c2dSHong Zhang /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 41880e36024fSHong Zhang if (n == PETSC_DECIDE){ 4189357abbc8SBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 41900e36024fSHong Zhang } 4191357abbc8SBarry Smith ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4192357abbc8SBarry Smith rstart -= m; 4193d6bb3c2dSHong Zhang 4194d6bb3c2dSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4195d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4196ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4197d6bb3c2dSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 4198ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4199d6bb3c2dSHong Zhang } 4200d6bb3c2dSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 4201f69a0ea3SMatthew Knepley ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 4202f69a0ea3SMatthew Knepley ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4203d6bb3c2dSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 4204d6bb3c2dSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 4205d6bb3c2dSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 4206d6bb3c2dSHong Zhang 4207d6bb3c2dSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 4208d6bb3c2dSHong Zhang ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 4209d6bb3c2dSHong Zhang } else { 4210e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 4211d6bb3c2dSHong Zhang } 4212d6bb3c2dSHong Zhang 4213d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4214ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4215b7940d39SSatish Balay Ii = i + rstart; 4216b7940d39SSatish Balay ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4217ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4218d6bb3c2dSHong Zhang } 4219d6bb3c2dSHong Zhang ierr = MatDestroy(inmat);CHKERRQ(ierr); 4220d6bb3c2dSHong Zhang ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4221d6bb3c2dSHong Zhang ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 422251dd7536SBarry Smith 4223c5d6d63eSBarry Smith PetscFunctionReturn(0); 4224c5d6d63eSBarry Smith } 4225c5d6d63eSBarry Smith 4226c5d6d63eSBarry Smith #undef __FUNCT__ 4227c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4228dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4229c5d6d63eSBarry Smith { 4230dfbe8321SBarry Smith PetscErrorCode ierr; 423132dcc486SBarry Smith PetscMPIInt rank; 4232b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4233de4209c5SBarry Smith size_t len; 4234b1d57f15SBarry Smith const PetscInt *indx; 4235c5d6d63eSBarry Smith PetscViewer out; 4236c5d6d63eSBarry Smith char *name; 4237c5d6d63eSBarry Smith Mat B; 4238b3cc6726SBarry Smith const PetscScalar *values; 4239c5d6d63eSBarry Smith 4240c5d6d63eSBarry Smith PetscFunctionBegin; 4241c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4242c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4243f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4244f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4245f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4246f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4247f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4248c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4249c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4250c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4251c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4252c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4253c5d6d63eSBarry Smith } 4254c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4255c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4256c5d6d63eSBarry Smith 42577adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4258c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4259c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4260c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4261852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4262c5d6d63eSBarry Smith ierr = PetscFree(name); 4263c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 4264c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 4265c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 4266c5d6d63eSBarry Smith PetscFunctionReturn(0); 4267c5d6d63eSBarry Smith } 4268e5f2cdd8SHong Zhang 426909573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 427051a7d1a8SHong Zhang #undef __FUNCT__ 427151a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 42727087cfbeSBarry Smith PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 427351a7d1a8SHong Zhang { 427451a7d1a8SHong Zhang PetscErrorCode ierr; 4275671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4276776b82aeSLisandro Dalcin PetscContainer container; 427751a7d1a8SHong Zhang 427851a7d1a8SHong Zhang PetscFunctionBegin; 4279671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4280671beff6SHong Zhang if (container) { 4281776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 428251a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 42833e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 42843e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 428551a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 428651a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4287533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 428802c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4289533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 429002c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 429105b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 429205b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 429305b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 429426283091SBarry Smith ierr = PetscLayoutDestroy(merge->rowmap);CHKERRQ(ierr); 4295671beff6SHong Zhang 4296776b82aeSLisandro Dalcin ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 4297671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4298671beff6SHong Zhang } 429951a7d1a8SHong Zhang ierr = PetscFree(merge);CHKERRQ(ierr); 430051a7d1a8SHong Zhang 430151a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 430251a7d1a8SHong Zhang PetscFunctionReturn(0); 430351a7d1a8SHong Zhang } 430451a7d1a8SHong Zhang 43057c4f633dSBarry Smith #include "../src/mat/utils/freespace.h" 4306be0fcf8dSHong Zhang #include "petscbt.h" 43074ebed01fSBarry Smith 4308e5f2cdd8SHong Zhang #undef __FUNCT__ 430938f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric" 4310e5f2cdd8SHong Zhang /*@C 4311f08fae4eSHong Zhang MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential 4312e5f2cdd8SHong Zhang matrices from each processor 4313e5f2cdd8SHong Zhang 4314e5f2cdd8SHong Zhang Collective on MPI_Comm 4315e5f2cdd8SHong Zhang 4316e5f2cdd8SHong Zhang Input Parameters: 4317e5f2cdd8SHong Zhang + comm - the communicators the parallel matrix will live on 4318f08fae4eSHong Zhang . seqmat - the input sequential matrices 43190e36024fSHong Zhang . m - number of local rows (or PETSC_DECIDE) 43200e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4321e5f2cdd8SHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4322e5f2cdd8SHong Zhang 4323e5f2cdd8SHong Zhang Output Parameter: 4324f08fae4eSHong Zhang . mpimat - the parallel matrix generated 4325e5f2cdd8SHong Zhang 4326e5f2cdd8SHong Zhang Level: advanced 4327e5f2cdd8SHong Zhang 4328affca5deSHong Zhang Notes: 4329affca5deSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4330affca5deSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4331affca5deSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4332e5f2cdd8SHong Zhang @*/ 43337087cfbeSBarry Smith PetscErrorCode MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat) 433455d1abb9SHong Zhang { 433555d1abb9SHong Zhang PetscErrorCode ierr; 43367adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 433755d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4338b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4339d0f46423SBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j; 4340b1d57f15SBarry Smith PetscInt proc,m; 4341b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4342b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4343b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 434455d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 434555d1abb9SHong Zhang MPI_Status *status; 4346a77337e4SBarry Smith MatScalar *aa=a->a; 4347dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 434855d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4349776b82aeSLisandro Dalcin PetscContainer container; 435055d1abb9SHong Zhang 435155d1abb9SHong Zhang PetscFunctionBegin; 43524ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 43533c2c1871SHong Zhang 435455d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 435555d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 435655d1abb9SHong Zhang 435755d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 435855d1abb9SHong Zhang if (container) { 4359776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 436055d1abb9SHong Zhang } 436155d1abb9SHong Zhang bi = merge->bi; 436255d1abb9SHong Zhang bj = merge->bj; 436355d1abb9SHong Zhang buf_ri = merge->buf_ri; 436455d1abb9SHong Zhang buf_rj = merge->buf_rj; 436555d1abb9SHong Zhang 436655d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 43677a2fc3feSBarry Smith owners = merge->rowmap->range; 436855d1abb9SHong Zhang len_s = merge->len_s; 436955d1abb9SHong Zhang 437055d1abb9SHong Zhang /* send and recv matrix values */ 437155d1abb9SHong Zhang /*-----------------------------*/ 4372357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 437355d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 437455d1abb9SHong Zhang 437555d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 437655d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 437755d1abb9SHong Zhang if (!len_s[proc]) continue; 437855d1abb9SHong Zhang i = owners[proc]; 437955d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 438055d1abb9SHong Zhang k++; 438155d1abb9SHong Zhang } 438255d1abb9SHong Zhang 43830c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 43840c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 438555d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 438655d1abb9SHong Zhang 438755d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 438855d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 438955d1abb9SHong Zhang 439055d1abb9SHong Zhang /* insert mat values of mpimat */ 439155d1abb9SHong Zhang /*----------------------------*/ 4392a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 43930572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 439455d1abb9SHong Zhang 439555d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 439655d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 439755d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 439855d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 439955d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 440055d1abb9SHong Zhang } 440155d1abb9SHong Zhang 440255d1abb9SHong Zhang /* set values of ba */ 44037a2fc3feSBarry Smith m = merge->rowmap->n; 440455d1abb9SHong Zhang for (i=0; i<m; i++) { 440555d1abb9SHong Zhang arow = owners[rank] + i; 440655d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 440755d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4408a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 440955d1abb9SHong Zhang 441055d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 441155d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 441255d1abb9SHong Zhang aj = a->j + ai[arow]; 441355d1abb9SHong Zhang aa = a->a + ai[arow]; 441455d1abb9SHong Zhang nextaj = 0; 441555d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 441655d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 441755d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 441855d1abb9SHong Zhang } 441955d1abb9SHong Zhang } 442055d1abb9SHong Zhang 442155d1abb9SHong Zhang /* add received vals into ba */ 442255d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 442355d1abb9SHong Zhang /* i-th row */ 442455d1abb9SHong Zhang if (i == *nextrow[k]) { 442555d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 442655d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 442755d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 442855d1abb9SHong Zhang nextaj = 0; 442955d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 443055d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 443155d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 443255d1abb9SHong Zhang } 443355d1abb9SHong Zhang } 443455d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 443555d1abb9SHong Zhang } 443655d1abb9SHong Zhang } 443755d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 443855d1abb9SHong Zhang } 443955d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 444055d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 444155d1abb9SHong Zhang 4442533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 444355d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 444455d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 44451d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 44464ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 444755d1abb9SHong Zhang PetscFunctionReturn(0); 444855d1abb9SHong Zhang } 444938f152feSBarry Smith 445038f152feSBarry Smith #undef __FUNCT__ 445138f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic" 44527087cfbeSBarry Smith PetscErrorCode MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4453e5f2cdd8SHong Zhang { 4454f08fae4eSHong Zhang PetscErrorCode ierr; 445555a3bba9SHong Zhang Mat B_mpi; 4456c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4457b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4458b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4459d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4460b1d57f15SBarry Smith PetscInt len,proc,*dnz,*onz; 4461b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4462b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 446355d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 446458cb9c82SHong Zhang MPI_Status *status; 4465a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4466be0fcf8dSHong Zhang PetscBT lnkbt; 446751a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4468776b82aeSLisandro Dalcin PetscContainer container; 446902c68681SHong Zhang 4470e5f2cdd8SHong Zhang PetscFunctionBegin; 44714ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 44723c2c1871SHong Zhang 447338f152feSBarry Smith /* make sure it is a PETSc comm */ 447438f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4475e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4476e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 447755d1abb9SHong Zhang 447851a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4479c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4480e5f2cdd8SHong Zhang 44816abd8857SHong Zhang /* determine row ownership */ 4482f08fae4eSHong Zhang /*---------------------------------------------------------*/ 448326283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 448426283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 448526283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 448626283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 448726283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4488b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4489b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 449055d1abb9SHong Zhang 44917a2fc3feSBarry Smith m = merge->rowmap->n; 44927a2fc3feSBarry Smith M = merge->rowmap->N; 44937a2fc3feSBarry Smith owners = merge->rowmap->range; 44946abd8857SHong Zhang 44956abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 44966abd8857SHong Zhang /*---------------------------------------------------------*/ 44973e06a4e6SHong Zhang len_s = merge->len_s; 449851a7d1a8SHong Zhang 44992257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4500c2234fe3SHong Zhang merge->nsend = 0; 4501409913e3SHong Zhang for (proc=0; proc<size; proc++){ 45022257cef7SHong Zhang len_si[proc] = 0; 45033e06a4e6SHong Zhang if (proc == rank){ 45046abd8857SHong Zhang len_s[proc] = 0; 45053e06a4e6SHong Zhang } else { 450602c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 45073e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 45083e06a4e6SHong Zhang } 45093e06a4e6SHong Zhang if (len_s[proc]) { 4510c2234fe3SHong Zhang merge->nsend++; 45112257cef7SHong Zhang nrows = 0; 45122257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 45132257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 45142257cef7SHong Zhang } 45152257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 45162257cef7SHong Zhang len += len_si[proc]; 4517409913e3SHong Zhang } 451858cb9c82SHong Zhang } 4519409913e3SHong Zhang 45202257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 45212257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 452251a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 452355d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4524671beff6SHong Zhang 45253e06a4e6SHong Zhang /* post the Irecv of j-structure */ 45263e06a4e6SHong Zhang /*-------------------------------*/ 45272c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 45283e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 452902c68681SHong Zhang 45303e06a4e6SHong Zhang /* post the Isend of j-structure */ 4531affca5deSHong Zhang /*--------------------------------*/ 45321d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 45333e06a4e6SHong Zhang 45342257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4535409913e3SHong Zhang if (!len_s[proc]) continue; 453602c68681SHong Zhang i = owners[proc]; 4537b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 453851a7d1a8SHong Zhang k++; 453951a7d1a8SHong Zhang } 454051a7d1a8SHong Zhang 45413e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 45423e06a4e6SHong Zhang /*------------------------------------------------*/ 45430c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 45440c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 454502c68681SHong Zhang 454602c68681SHong Zhang /* send and recv i-structure */ 454702c68681SHong Zhang /*---------------------------*/ 45482c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 454902c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 455002c68681SHong Zhang 4551b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 45523e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 45532257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 455402c68681SHong Zhang if (!len_s[proc]) continue; 45553e06a4e6SHong Zhang /* form outgoing message for i-structure: 45563e06a4e6SHong Zhang buf_si[0]: nrows to be sent 45573e06a4e6SHong Zhang [1:nrows]: row index (global) 45583e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 45593e06a4e6SHong Zhang */ 45603e06a4e6SHong Zhang /*-------------------------------------------*/ 45612257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 45623e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 45633e06a4e6SHong Zhang buf_si[0] = nrows; 45643e06a4e6SHong Zhang buf_si_i[0] = 0; 45653e06a4e6SHong Zhang nrows = 0; 45663e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 45673e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 45683e06a4e6SHong Zhang if (anzi) { 45693e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 45703e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 45713e06a4e6SHong Zhang nrows++; 45723e06a4e6SHong Zhang } 45733e06a4e6SHong Zhang } 4574b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 457502c68681SHong Zhang k++; 45762257cef7SHong Zhang buf_si += len_si[proc]; 457702c68681SHong Zhang } 45782257cef7SHong Zhang 45790c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 45800c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 458102c68681SHong Zhang 4582ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 45833e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4584ae15b995SBarry 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); 45853e06a4e6SHong Zhang } 45863e06a4e6SHong Zhang 45873e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 458802c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 458902c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 45901d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 45912257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 45923e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4593bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 459458cb9c82SHong Zhang 4595bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4596bcc1bcd5SHong Zhang /*----------------------------------------------*/ 459758cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4598b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 459958cb9c82SHong Zhang bi[0] = 0; 460058cb9c82SHong Zhang 4601be0fcf8dSHong Zhang /* create and initialize a linked list */ 4602be0fcf8dSHong Zhang nlnk = N+1; 4603be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 460458cb9c82SHong Zhang 4605bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 460658cb9c82SHong Zhang len = 0; 4607bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4608a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 460958cb9c82SHong Zhang current_space = free_space; 461058cb9c82SHong Zhang 4611bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 46120572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 46131d79065fSBarry Smith 46143e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 46152257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 46163e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 46173e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 46182257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 46193e06a4e6SHong Zhang } 46202257cef7SHong Zhang 4621bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4622bcc1bcd5SHong Zhang len = 0; 462358cb9c82SHong Zhang for (i=0;i<m;i++) { 462458cb9c82SHong Zhang bnzi = 0; 462558cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 462658cb9c82SHong Zhang arow = owners[rank] + i; 462758cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 462858cb9c82SHong Zhang aj = a->j + ai[arow]; 4629be0fcf8dSHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 463058cb9c82SHong Zhang bnzi += nlnk; 463158cb9c82SHong Zhang /* add received col data into lnk */ 463251a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 463355d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 46343e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 46353e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 46363e06a4e6SHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 46373e06a4e6SHong Zhang bnzi += nlnk; 46383e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 46393e06a4e6SHong Zhang } 464058cb9c82SHong Zhang } 4641bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 464258cb9c82SHong Zhang 464358cb9c82SHong Zhang /* if free space is not available, make more free space */ 464458cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 46454238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 464658cb9c82SHong Zhang nspacedouble++; 464758cb9c82SHong Zhang } 464858cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4649be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4650bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4651bcc1bcd5SHong Zhang 465258cb9c82SHong Zhang current_space->array += bnzi; 465358cb9c82SHong Zhang current_space->local_used += bnzi; 465458cb9c82SHong Zhang current_space->local_remaining -= bnzi; 465558cb9c82SHong Zhang 465658cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 465758cb9c82SHong Zhang } 4658bcc1bcd5SHong Zhang 46591d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4660bcc1bcd5SHong Zhang 4661b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4662a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4663be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4664409913e3SHong Zhang 4665bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4666bcc1bcd5SHong Zhang /*---------------------------------------*/ 4667f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 466854b84b50SHong Zhang if (n==PETSC_DECIDE) { 4669f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 467054b84b50SHong Zhang } else { 4671f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 467254b84b50SHong Zhang } 4673bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4674bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4675bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 467658cb9c82SHong Zhang 46776abd8857SHong Zhang /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */ 46786abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4679affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4680affca5deSHong Zhang merge->bi = bi; 4681affca5deSHong Zhang merge->bj = bj; 468202c68681SHong Zhang merge->buf_ri = buf_ri; 468302c68681SHong Zhang merge->buf_rj = buf_rj; 4684de0260b3SHong Zhang merge->coi = PETSC_NULL; 4685de0260b3SHong Zhang merge->coj = PETSC_NULL; 4686de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4687affca5deSHong Zhang 4688affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4689776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4690776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4691affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4692affca5deSHong Zhang *mpimat = B_mpi; 469338f152feSBarry Smith 469438f152feSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 46954ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4696e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4697e5f2cdd8SHong Zhang } 469825616d81SHong Zhang 469938f152feSBarry Smith #undef __FUNCT__ 470038f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI" 47017087cfbeSBarry Smith PetscErrorCode MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 470255d1abb9SHong Zhang { 470355d1abb9SHong Zhang PetscErrorCode ierr; 470455d1abb9SHong Zhang 470555d1abb9SHong Zhang PetscFunctionBegin; 47064ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 470755d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 470855d1abb9SHong Zhang ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 470955d1abb9SHong Zhang } 471055d1abb9SHong Zhang ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr); 47114ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 471255d1abb9SHong Zhang PetscFunctionReturn(0); 471355d1abb9SHong Zhang } 47144ebed01fSBarry Smith 471525616d81SHong Zhang #undef __FUNCT__ 471625616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat" 4717bc08b0f1SBarry Smith /*@ 471832fba14fSHong Zhang MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows 471925616d81SHong Zhang 472032fba14fSHong Zhang Not Collective 472125616d81SHong Zhang 472225616d81SHong Zhang Input Parameters: 472325616d81SHong Zhang + A - the matrix 472425616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 472525616d81SHong Zhang 472625616d81SHong Zhang Output Parameter: 472725616d81SHong Zhang . A_loc - the local sequential matrix generated 472825616d81SHong Zhang 472925616d81SHong Zhang Level: developer 473025616d81SHong Zhang 473125616d81SHong Zhang @*/ 47327087cfbeSBarry Smith PetscErrorCode MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 473325616d81SHong Zhang { 473425616d81SHong Zhang PetscErrorCode ierr; 473501b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 473601b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 473701b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 4738a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 4739a77337e4SBarry Smith PetscScalar *ca; 4740d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 47415a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 474225616d81SHong Zhang 474325616d81SHong Zhang PetscFunctionBegin; 47444ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 474501b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4746dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 4747dea91ad1SHong Zhang ci[0] = 0; 474801b7ae99SHong Zhang for (i=0; i<am; i++){ 4749dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 475001b7ae99SHong Zhang } 4751dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 4752dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 4753dea91ad1SHong Zhang k = 0; 475401b7ae99SHong Zhang for (i=0; i<am; i++) { 47555a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47565a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 475701b7ae99SHong Zhang /* off-diagonal portion of A */ 47585a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47595a7d977cSHong Zhang col = cmap[*bj]; 47605a7d977cSHong Zhang if (col >= cstart) break; 47615a7d977cSHong Zhang cj[k] = col; bj++; 47625a7d977cSHong Zhang ca[k++] = *ba++; 47635a7d977cSHong Zhang } 47645a7d977cSHong Zhang /* diagonal portion of A */ 47655a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 47665a7d977cSHong Zhang cj[k] = cstart + *aj++; 47675a7d977cSHong Zhang ca[k++] = *aa++; 47685a7d977cSHong Zhang } 47695a7d977cSHong Zhang /* off-diagonal portion of A */ 47705a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 47715a7d977cSHong Zhang cj[k] = cmap[*bj++]; 47725a7d977cSHong Zhang ca[k++] = *ba++; 47735a7d977cSHong Zhang } 477425616d81SHong Zhang } 4775dea91ad1SHong Zhang /* put together the new matrix */ 4776d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 4777dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4778dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 4779dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 4780e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 4781e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 4782dea91ad1SHong Zhang mat->nonew = 0; 47835a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 47845a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 4785a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 47865a7d977cSHong Zhang for (i=0; i<am; i++) { 47875a7d977cSHong Zhang /* off-diagonal portion of A */ 47885a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47895a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47905a7d977cSHong Zhang col = cmap[*bj]; 47915a7d977cSHong Zhang if (col >= cstart) break; 4792a77337e4SBarry Smith *cam++ = *ba++; bj++; 47935a7d977cSHong Zhang } 47945a7d977cSHong Zhang /* diagonal portion of A */ 4795ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 4796a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 47975a7d977cSHong Zhang /* off-diagonal portion of A */ 4798f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 4799a77337e4SBarry Smith *cam++ = *ba++; bj++; 4800f33d1a9aSHong Zhang } 48015a7d977cSHong Zhang } 48025a7d977cSHong Zhang } else { 4803e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 480425616d81SHong Zhang } 480501b7ae99SHong Zhang 48064ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 480725616d81SHong Zhang PetscFunctionReturn(0); 480825616d81SHong Zhang } 480925616d81SHong Zhang 481032fba14fSHong Zhang #undef __FUNCT__ 481132fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed" 481232fba14fSHong Zhang /*@C 481332fba14fSHong Zhang MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns 481432fba14fSHong Zhang 481532fba14fSHong Zhang Not Collective 481632fba14fSHong Zhang 481732fba14fSHong Zhang Input Parameters: 481832fba14fSHong Zhang + A - the matrix 481932fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 482032fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 482132fba14fSHong Zhang 482232fba14fSHong Zhang Output Parameter: 482332fba14fSHong Zhang . A_loc - the local sequential matrix generated 482432fba14fSHong Zhang 482532fba14fSHong Zhang Level: developer 482632fba14fSHong Zhang 482732fba14fSHong Zhang @*/ 48287087cfbeSBarry Smith PetscErrorCode MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 482932fba14fSHong Zhang { 483032fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 483132fba14fSHong Zhang PetscErrorCode ierr; 483232fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 483332fba14fSHong Zhang IS isrowa,iscola; 483432fba14fSHong Zhang Mat *aloc; 483532fba14fSHong Zhang 483632fba14fSHong Zhang PetscFunctionBegin; 48374ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 483832fba14fSHong Zhang if (!row){ 4839d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 484032fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 484132fba14fSHong Zhang } else { 484232fba14fSHong Zhang isrowa = *row; 484332fba14fSHong Zhang } 484432fba14fSHong Zhang if (!col){ 4845d0f46423SBarry Smith start = A->cmap->rstart; 484632fba14fSHong Zhang cmap = a->garray; 4847d0f46423SBarry Smith nzA = a->A->cmap->n; 4848d0f46423SBarry Smith nzB = a->B->cmap->n; 484932fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 485032fba14fSHong Zhang ncols = 0; 485132fba14fSHong Zhang for (i=0; i<nzB; i++) { 485232fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 485332fba14fSHong Zhang else break; 485432fba14fSHong Zhang } 485532fba14fSHong Zhang imark = i; 485632fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 485732fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 4858d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 485932fba14fSHong Zhang } else { 486032fba14fSHong Zhang iscola = *col; 486132fba14fSHong Zhang } 486232fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 486332fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 486432fba14fSHong Zhang aloc[0] = *A_loc; 486532fba14fSHong Zhang } 486632fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 486732fba14fSHong Zhang *A_loc = aloc[0]; 486832fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 486932fba14fSHong Zhang if (!row){ 487032fba14fSHong Zhang ierr = ISDestroy(isrowa);CHKERRQ(ierr); 487132fba14fSHong Zhang } 487232fba14fSHong Zhang if (!col){ 487332fba14fSHong Zhang ierr = ISDestroy(iscola);CHKERRQ(ierr); 487432fba14fSHong Zhang } 48754ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 487632fba14fSHong Zhang PetscFunctionReturn(0); 487732fba14fSHong Zhang } 487832fba14fSHong Zhang 487925616d81SHong Zhang #undef __FUNCT__ 488025616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 488125616d81SHong Zhang /*@C 488232fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 488325616d81SHong Zhang 488425616d81SHong Zhang Collective on Mat 488525616d81SHong Zhang 488625616d81SHong Zhang Input Parameters: 4887e240928fSHong Zhang + A,B - the matrices in mpiaij format 488825616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 488925616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 489025616d81SHong Zhang 489125616d81SHong Zhang Output Parameter: 489225616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 4893d0f46423SBarry Smith . brstart - row index of B_seq from which next B->rmap->n rows are taken from B's local rows 489425616d81SHong Zhang - B_seq - the sequential matrix generated 489525616d81SHong Zhang 489625616d81SHong Zhang Level: developer 489725616d81SHong Zhang 489825616d81SHong Zhang @*/ 48997087cfbeSBarry Smith PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq) 490025616d81SHong Zhang { 4901899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 490225616d81SHong Zhang PetscErrorCode ierr; 4903b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 490425616d81SHong Zhang IS isrowb,iscolb; 490525616d81SHong Zhang Mat *bseq; 490625616d81SHong Zhang 490725616d81SHong Zhang PetscFunctionBegin; 4908d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4909e32f2f54SBarry 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); 491025616d81SHong Zhang } 49114ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 491225616d81SHong Zhang 491325616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4914d0f46423SBarry Smith start = A->cmap->rstart; 491525616d81SHong Zhang cmap = a->garray; 4916d0f46423SBarry Smith nzA = a->A->cmap->n; 4917d0f46423SBarry Smith nzB = a->B->cmap->n; 4918b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 491925616d81SHong Zhang ncols = 0; 49200390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 492125616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 492225616d81SHong Zhang else break; 492325616d81SHong Zhang } 492425616d81SHong Zhang imark = i; 49250390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 49260390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 4927d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 492825616d81SHong Zhang *brstart = imark; 4929d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 493025616d81SHong Zhang } else { 4931e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 493225616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 493325616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 493425616d81SHong Zhang bseq[0] = *B_seq; 493525616d81SHong Zhang } 493625616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 493725616d81SHong Zhang *B_seq = bseq[0]; 493825616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 493925616d81SHong Zhang if (!rowb){ 494025616d81SHong Zhang ierr = ISDestroy(isrowb);CHKERRQ(ierr); 494125616d81SHong Zhang } else { 494225616d81SHong Zhang *rowb = isrowb; 494325616d81SHong Zhang } 494425616d81SHong Zhang if (!colb){ 494525616d81SHong Zhang ierr = ISDestroy(iscolb);CHKERRQ(ierr); 494625616d81SHong Zhang } else { 494725616d81SHong Zhang *colb = iscolb; 494825616d81SHong Zhang } 49494ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 495025616d81SHong Zhang PetscFunctionReturn(0); 495125616d81SHong Zhang } 4952429d309bSHong Zhang 4953a61c8c0fSHong Zhang #undef __FUNCT__ 4954a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols" 4955429d309bSHong Zhang /*@C 4956429d309bSHong Zhang MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 495701b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 4958429d309bSHong Zhang 4959429d309bSHong Zhang Collective on Mat 4960429d309bSHong Zhang 4961429d309bSHong Zhang Input Parameters: 4962429d309bSHong Zhang + A,B - the matrices in mpiaij format 496387025532SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 496487025532SHong Zhang . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 49651d79065fSBarry Smith . startsj_r - similar to startsj for receives 496687025532SHong Zhang - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 4967429d309bSHong Zhang 4968429d309bSHong Zhang Output Parameter: 496987025532SHong Zhang + B_oth - the sequential matrix generated 4970429d309bSHong Zhang 4971429d309bSHong Zhang Level: developer 4972429d309bSHong Zhang 4973429d309bSHong Zhang @*/ 49747087cfbeSBarry Smith PetscErrorCode MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 4975429d309bSHong Zhang { 4976a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 4977429d309bSHong Zhang PetscErrorCode ierr; 4978899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 497987025532SHong Zhang Mat_SeqAIJ *b_oth; 4980a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 49817adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 49827adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 4983d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 4984dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 4985dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 4986e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 4987910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 498887025532SHong Zhang MPI_Status *sstatus,rstatus; 4989aa5bb8c0SSatish Balay PetscMPIInt jj; 4990e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 4991ba8c8a56SBarry Smith PetscScalar *vals; 4992429d309bSHong Zhang 4993429d309bSHong Zhang PetscFunctionBegin; 4994d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4995e32f2f54SBarry 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); 4996429d309bSHong Zhang } 49974ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4998a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 4999a6b2eed2SHong Zhang 5000a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5001a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5002e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5003e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5004a6b2eed2SHong Zhang nrecvs = gen_from->n; 5005a6b2eed2SHong Zhang nsends = gen_to->n; 5006d7ee0231SBarry Smith 5007d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 5008a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5009a6b2eed2SHong Zhang sstarts = gen_to->starts; 5010a6b2eed2SHong Zhang sprocs = gen_to->procs; 5011a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5012e42f35eeSHong Zhang sbs = gen_to->bs; 5013e42f35eeSHong Zhang rstarts = gen_from->starts; 5014e42f35eeSHong Zhang rprocs = gen_from->procs; 5015e42f35eeSHong Zhang rbs = gen_from->bs; 5016429d309bSHong Zhang 5017dea91ad1SHong Zhang if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5018429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5019a6b2eed2SHong Zhang /* i-array */ 5020a6b2eed2SHong Zhang /*---------*/ 5021a6b2eed2SHong Zhang /* post receives */ 5022a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5023e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5024e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 502587025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5026429d309bSHong Zhang } 5027a6b2eed2SHong Zhang 5028a6b2eed2SHong Zhang /* pack the outgoing message */ 50291d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 5030a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 5031a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5032a6b2eed2SHong Zhang k = 0; 5033a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5034e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5035e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 503687025532SHong Zhang for (j=0; j<nrows; j++) { 5037d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5038e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 5039e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 5040e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 5041e42f35eeSHong Zhang len += ncols; 5042e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5043e42f35eeSHong Zhang } 5044a6b2eed2SHong Zhang k++; 5045429d309bSHong Zhang } 5046e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5047dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5048429d309bSHong Zhang } 504987025532SHong Zhang /* recvs and sends of i-array are completed */ 505087025532SHong Zhang i = nrecvs; 505187025532SHong Zhang while (i--) { 5052aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 505387025532SHong Zhang } 50540c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5055e42f35eeSHong Zhang 5056a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5057a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 5058a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 5059a6b2eed2SHong Zhang 506087025532SHong Zhang /* create i-array of B_oth */ 506187025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 506287025532SHong Zhang b_othi[0] = 0; 5063a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5064a6b2eed2SHong Zhang k = 0; 5065a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5066fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5067e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 506887025532SHong Zhang for (j=0; j<nrows; j++) { 506987025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5070a6b2eed2SHong Zhang len += rowlen[j]; k++; 5071a6b2eed2SHong Zhang } 5072dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5073a6b2eed2SHong Zhang } 5074a6b2eed2SHong Zhang 507587025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 507687025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 5077dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 5078a6b2eed2SHong Zhang 507987025532SHong Zhang /* j-array */ 508087025532SHong Zhang /*---------*/ 5081a6b2eed2SHong Zhang /* post receives of j-array */ 5082a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 508387025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 508487025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5085a6b2eed2SHong Zhang } 5086e42f35eeSHong Zhang 5087e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5088a6b2eed2SHong Zhang k = 0; 5089a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5090e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5091a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 509287025532SHong Zhang for (j=0; j<nrows; j++) { 5093d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5094e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5095e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5096a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 5097a6b2eed2SHong Zhang *bufJ++ = cols[l]; 509887025532SHong Zhang } 5099e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5100e42f35eeSHong Zhang } 510187025532SHong Zhang } 510287025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 510387025532SHong Zhang } 510487025532SHong Zhang 510587025532SHong Zhang /* recvs and sends of j-array are completed */ 510687025532SHong Zhang i = nrecvs; 510787025532SHong Zhang while (i--) { 5108aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 510987025532SHong Zhang } 51100c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 511187025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 511287025532SHong Zhang sstartsj = *startsj; 51131d79065fSBarry Smith rstartsj = *startsj_r; 511487025532SHong Zhang bufa = *bufa_ptr; 511587025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 511687025532SHong Zhang b_otha = b_oth->a; 511787025532SHong Zhang } else { 5118e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 511987025532SHong Zhang } 512087025532SHong Zhang 512187025532SHong Zhang /* a-array */ 512287025532SHong Zhang /*---------*/ 512387025532SHong Zhang /* post receives of a-array */ 512487025532SHong Zhang for (i=0; i<nrecvs; i++){ 512587025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 512687025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 512787025532SHong Zhang } 5128e42f35eeSHong Zhang 5129e42f35eeSHong Zhang /* pack the outgoing message a-array */ 513087025532SHong Zhang k = 0; 513187025532SHong Zhang for (i=0; i<nsends; i++){ 5132e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 513387025532SHong Zhang bufA = bufa+sstartsj[i]; 513487025532SHong Zhang for (j=0; j<nrows; j++) { 5135d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5136e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5137e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 513887025532SHong Zhang for (l=0; l<ncols; l++){ 5139a6b2eed2SHong Zhang *bufA++ = vals[l]; 5140a6b2eed2SHong Zhang } 5141e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 5142e42f35eeSHong Zhang } 5143a6b2eed2SHong Zhang } 514487025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5145a6b2eed2SHong Zhang } 514687025532SHong Zhang /* recvs and sends of a-array are completed */ 514787025532SHong Zhang i = nrecvs; 514887025532SHong Zhang while (i--) { 5149aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 515087025532SHong Zhang } 51510c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5152d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5153a6b2eed2SHong Zhang 515487025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5155a6b2eed2SHong Zhang /* put together the new matrix */ 5156d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5157a6b2eed2SHong Zhang 5158a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5159a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 516087025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 5161e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5162e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 516387025532SHong Zhang b_oth->nonew = 0; 5164a6b2eed2SHong Zhang 5165a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5166dea91ad1SHong Zhang if (!startsj || !bufa_ptr){ 51671d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5168dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5169dea91ad1SHong Zhang } else { 517087025532SHong Zhang *startsj = sstartsj; 51711d79065fSBarry Smith *startsj_r = rstartsj; 517287025532SHong Zhang *bufa_ptr = bufa; 517387025532SHong Zhang } 5174dea91ad1SHong Zhang } 51754ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5176429d309bSHong Zhang PetscFunctionReturn(0); 5177429d309bSHong Zhang } 5178ccd8e176SBarry Smith 517943eb5e2fSMatthew Knepley #undef __FUNCT__ 518043eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 518143eb5e2fSMatthew Knepley /*@C 518243eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 518343eb5e2fSMatthew Knepley 518443eb5e2fSMatthew Knepley Not Collective 518543eb5e2fSMatthew Knepley 518643eb5e2fSMatthew Knepley Input Parameters: 518743eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 518843eb5e2fSMatthew Knepley 518943eb5e2fSMatthew Knepley Output Parameter: 519043eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 519143eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 519243eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 519343eb5e2fSMatthew Knepley 519443eb5e2fSMatthew Knepley Level: developer 519543eb5e2fSMatthew Knepley 519643eb5e2fSMatthew Knepley @*/ 519743eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 51987087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 519943eb5e2fSMatthew Knepley #else 52007087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 520143eb5e2fSMatthew Knepley #endif 520243eb5e2fSMatthew Knepley { 520343eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 520443eb5e2fSMatthew Knepley 520543eb5e2fSMatthew Knepley PetscFunctionBegin; 52060700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5207e414b56bSJed Brown PetscValidPointer(lvec, 2); 5208e414b56bSJed Brown PetscValidPointer(colmap, 3); 5209e414b56bSJed Brown PetscValidPointer(multScatter, 4); 521043eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 521143eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 521243eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 521343eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 521443eb5e2fSMatthew Knepley PetscFunctionReturn(0); 521543eb5e2fSMatthew Knepley } 521643eb5e2fSMatthew Knepley 521717667f90SBarry Smith EXTERN_C_BEGIN 52187087cfbeSBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*); 52197087cfbeSBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*); 52207087cfbeSBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*); 522117667f90SBarry Smith EXTERN_C_END 522217667f90SBarry Smith 5223fc4dec0aSBarry Smith #undef __FUNCT__ 5224fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5225fc4dec0aSBarry Smith /* 5226fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5227fc4dec0aSBarry Smith 5228fc4dec0aSBarry Smith n p p 5229fc4dec0aSBarry Smith ( ) ( ) ( ) 5230fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5231fc4dec0aSBarry Smith ( ) ( ) ( ) 5232fc4dec0aSBarry Smith 5233fc4dec0aSBarry Smith */ 5234fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5235fc4dec0aSBarry Smith { 5236fc4dec0aSBarry Smith PetscErrorCode ierr; 5237fc4dec0aSBarry Smith Mat At,Bt,Ct; 5238fc4dec0aSBarry Smith 5239fc4dec0aSBarry Smith PetscFunctionBegin; 5240fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5241fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5242fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 5243fc4dec0aSBarry Smith ierr = MatDestroy(At);CHKERRQ(ierr); 5244fc4dec0aSBarry Smith ierr = MatDestroy(Bt);CHKERRQ(ierr); 5245fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 5246e5e4356aSBarry Smith ierr = MatDestroy(Ct);CHKERRQ(ierr); 5247fc4dec0aSBarry Smith PetscFunctionReturn(0); 5248fc4dec0aSBarry Smith } 5249fc4dec0aSBarry Smith 5250fc4dec0aSBarry Smith #undef __FUNCT__ 5251fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5252fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5253fc4dec0aSBarry Smith { 5254fc4dec0aSBarry Smith PetscErrorCode ierr; 5255d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5256fc4dec0aSBarry Smith Mat Cmat; 5257fc4dec0aSBarry Smith 5258fc4dec0aSBarry Smith PetscFunctionBegin; 5259e32f2f54SBarry 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); 526039804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5261fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5262fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5263fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 526438556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 526538556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5266fc4dec0aSBarry Smith *C = Cmat; 5267fc4dec0aSBarry Smith PetscFunctionReturn(0); 5268fc4dec0aSBarry Smith } 5269fc4dec0aSBarry Smith 5270fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5271fc4dec0aSBarry Smith #undef __FUNCT__ 5272fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5273fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5274fc4dec0aSBarry Smith { 5275fc4dec0aSBarry Smith PetscErrorCode ierr; 5276fc4dec0aSBarry Smith 5277fc4dec0aSBarry Smith PetscFunctionBegin; 5278fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5279fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5280fc4dec0aSBarry Smith } 5281fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5282fc4dec0aSBarry Smith PetscFunctionReturn(0); 5283fc4dec0aSBarry Smith } 5284fc4dec0aSBarry Smith 52855c9eb25fSBarry Smith EXTERN_C_BEGIN 5286611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5287bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5288611f576cSBarry Smith #endif 52893bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 52903bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 52913bf14a46SMatthew Knepley #endif 5292611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 52935c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5294611f576cSBarry Smith #endif 5295611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 52965c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*); 5297611f576cSBarry Smith #endif 52985c9eb25fSBarry Smith EXTERN_C_END 52995c9eb25fSBarry Smith 5300ccd8e176SBarry Smith /*MC 5301ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5302ccd8e176SBarry Smith 5303ccd8e176SBarry Smith Options Database Keys: 5304ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5305ccd8e176SBarry Smith 5306ccd8e176SBarry Smith Level: beginner 5307ccd8e176SBarry Smith 5308175b88e8SBarry Smith .seealso: MatCreateMPIAIJ() 5309ccd8e176SBarry Smith M*/ 5310ccd8e176SBarry Smith 5311ccd8e176SBarry Smith EXTERN_C_BEGIN 5312ccd8e176SBarry Smith #undef __FUNCT__ 5313ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 53147087cfbeSBarry Smith PetscErrorCode MatCreate_MPIAIJ(Mat B) 5315ccd8e176SBarry Smith { 5316ccd8e176SBarry Smith Mat_MPIAIJ *b; 5317ccd8e176SBarry Smith PetscErrorCode ierr; 5318ccd8e176SBarry Smith PetscMPIInt size; 5319ccd8e176SBarry Smith 5320ccd8e176SBarry Smith PetscFunctionBegin; 53217adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 5322ccd8e176SBarry Smith 532338f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5324ccd8e176SBarry Smith B->data = (void*)b; 5325ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5326d0f46423SBarry Smith B->rmap->bs = 1; 5327ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5328ccd8e176SBarry Smith 5329ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5330ccd8e176SBarry Smith b->size = size; 53317adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5332ccd8e176SBarry Smith 5333ccd8e176SBarry Smith /* build cache for off array entries formed */ 53347adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5335ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5336ccd8e176SBarry Smith b->colmap = 0; 5337ccd8e176SBarry Smith b->garray = 0; 5338ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5339ccd8e176SBarry Smith 5340ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5341ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5342ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5343ccd8e176SBarry Smith 5344ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5345ccd8e176SBarry Smith b->rowindices = 0; 5346ccd8e176SBarry Smith b->rowvalues = 0; 5347ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5348ccd8e176SBarry Smith 5349611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 5350ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 53515c9eb25fSBarry Smith "MatGetFactor_mpiaij_spooles", 53525c9eb25fSBarry Smith MatGetFactor_mpiaij_spooles);CHKERRQ(ierr); 5353611f576cSBarry Smith #endif 5354611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5355ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5356bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5357bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5358611f576cSBarry Smith #endif 53593bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5360ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 53613bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 53623bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 53633bf14a46SMatthew Knepley #endif 5364611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5365ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 53665c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 53675c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5368611f576cSBarry Smith #endif 5369ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5370ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5371ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5372ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5373ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5374ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5375ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5376ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5377ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5378ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5379ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5380ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5381ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5382ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5383ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5384ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5385ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5386ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5387ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5388ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5389ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 53905a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C", 53915a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJPERM", 53925a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 53935a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C", 53945a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJCRL", 53955a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5396471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5397471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5398471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5399fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5400fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5401fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5402fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5403fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5404fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5405fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5406fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5407fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 540817667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5409ccd8e176SBarry Smith PetscFunctionReturn(0); 5410ccd8e176SBarry Smith } 5411ccd8e176SBarry Smith EXTERN_C_END 541281824310SBarry Smith 541303bfb495SBarry Smith #undef __FUNCT__ 541403bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 541558d36128SBarry Smith /*@ 541603bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 541703bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 541803bfb495SBarry Smith 541903bfb495SBarry Smith Collective on MPI_Comm 542003bfb495SBarry Smith 542103bfb495SBarry Smith Input Parameters: 542203bfb495SBarry Smith + comm - MPI communicator 542303bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 542403bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 542503bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 542603bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 542703bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 542803bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 542903bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 543003bfb495SBarry Smith . j - column indices 543103bfb495SBarry Smith . a - matrix values 543203bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 543303bfb495SBarry Smith . oj - column indices 543403bfb495SBarry Smith - oa - matrix values 543503bfb495SBarry Smith 543603bfb495SBarry Smith Output Parameter: 543703bfb495SBarry Smith . mat - the matrix 543803bfb495SBarry Smith 543903bfb495SBarry Smith Level: advanced 544003bfb495SBarry Smith 544103bfb495SBarry Smith Notes: 544203bfb495SBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. 544303bfb495SBarry Smith 544403bfb495SBarry Smith The i and j indices are 0 based 544503bfb495SBarry Smith 544603bfb495SBarry Smith See MatCreateMPIAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 544703bfb495SBarry Smith 54487b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 54497b55108eSBarry Smith 54507b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 545103bfb495SBarry Smith 545203bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 545303bfb495SBarry Smith 545403bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 54558d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithArrays() 545603bfb495SBarry Smith @*/ 54577087cfbeSBarry Smith PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 545803bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 545903bfb495SBarry Smith { 546003bfb495SBarry Smith PetscErrorCode ierr; 546103bfb495SBarry Smith Mat_MPIAIJ *maij; 546203bfb495SBarry Smith 546303bfb495SBarry Smith PetscFunctionBegin; 5464e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 546503bfb495SBarry Smith if (i[0]) { 5466e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 546703bfb495SBarry Smith } 546803bfb495SBarry Smith if (oi[0]) { 5469e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 547003bfb495SBarry Smith } 547103bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 547203bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 547303bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 547403bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 54758d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 54768d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 547703bfb495SBarry Smith 547826283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->rmap,1);CHKERRQ(ierr); 547926283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->cmap,1);CHKERRQ(ierr); 548026283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 548126283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 548203bfb495SBarry Smith 548303bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5484d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 548503bfb495SBarry Smith 54868d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54878d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54888d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54898d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54908d7a6e47SBarry Smith 549103bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549203bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549303bfb495SBarry Smith PetscFunctionReturn(0); 549403bfb495SBarry Smith } 549503bfb495SBarry Smith 549681824310SBarry Smith /* 549781824310SBarry Smith Special version for direct calls from Fortran 549881824310SBarry Smith */ 54997087cfbeSBarry Smith #include "private/fortranimpl.h" 55007087cfbeSBarry Smith 550181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 550281824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 550381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 550481824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 550581824310SBarry Smith #endif 550681824310SBarry Smith 550781824310SBarry Smith /* Change these macros so can be used in void function */ 550881824310SBarry Smith #undef CHKERRQ 5509e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 551081824310SBarry Smith #undef SETERRQ2 5511e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 551281824310SBarry Smith #undef SETERRQ 5513e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 551481824310SBarry Smith 551581824310SBarry Smith EXTERN_C_BEGIN 551681824310SBarry Smith #undef __FUNCT__ 551781824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 55181f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 551981824310SBarry Smith { 552081824310SBarry Smith Mat mat = *mmat; 552181824310SBarry Smith PetscInt m = *mm, n = *mn; 552281824310SBarry Smith InsertMode addv = *maddv; 552381824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 552481824310SBarry Smith PetscScalar value; 552581824310SBarry Smith PetscErrorCode ierr; 5526899cda47SBarry Smith 5527d9e2c085SLisandro Dalcin ierr = MatPreallocated(mat);CHKERRQ(ierr); 552881824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 552981824310SBarry Smith mat->insertmode = addv; 553081824310SBarry Smith } 553181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 553281824310SBarry Smith else if (mat->insertmode != addv) { 5533e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 553481824310SBarry Smith } 553581824310SBarry Smith #endif 553681824310SBarry Smith { 5537d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5538d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5539ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 554081824310SBarry Smith 554181824310SBarry Smith /* Some Variables required in the macro */ 554281824310SBarry Smith Mat A = aij->A; 554381824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 554481824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5545dd6ea824SBarry Smith MatScalar *aa = a->a; 5546ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 554781824310SBarry Smith Mat B = aij->B; 554881824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5549d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5550dd6ea824SBarry Smith MatScalar *ba = b->a; 555181824310SBarry Smith 555281824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 555381824310SBarry Smith PetscInt nonew = a->nonew; 5554dd6ea824SBarry Smith MatScalar *ap1,*ap2; 555581824310SBarry Smith 555681824310SBarry Smith PetscFunctionBegin; 555781824310SBarry Smith for (i=0; i<m; i++) { 555881824310SBarry Smith if (im[i] < 0) continue; 555981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5560e32f2f54SBarry 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); 556181824310SBarry Smith #endif 556281824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 556381824310SBarry Smith row = im[i] - rstart; 556481824310SBarry Smith lastcol1 = -1; 556581824310SBarry Smith rp1 = aj + ai[row]; 556681824310SBarry Smith ap1 = aa + ai[row]; 556781824310SBarry Smith rmax1 = aimax[row]; 556881824310SBarry Smith nrow1 = ailen[row]; 556981824310SBarry Smith low1 = 0; 557081824310SBarry Smith high1 = nrow1; 557181824310SBarry Smith lastcol2 = -1; 557281824310SBarry Smith rp2 = bj + bi[row]; 557381824310SBarry Smith ap2 = ba + bi[row]; 557481824310SBarry Smith rmax2 = bimax[row]; 557581824310SBarry Smith nrow2 = bilen[row]; 557681824310SBarry Smith low2 = 0; 557781824310SBarry Smith high2 = nrow2; 557881824310SBarry Smith 557981824310SBarry Smith for (j=0; j<n; j++) { 558081824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 558181824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 558281824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 558381824310SBarry Smith col = in[j] - cstart; 558481824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 558581824310SBarry Smith } else if (in[j] < 0) continue; 558681824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5587cb9801acSJed 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); 558881824310SBarry Smith #endif 558981824310SBarry Smith else { 559081824310SBarry Smith if (mat->was_assembled) { 559181824310SBarry Smith if (!aij->colmap) { 559281824310SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 559381824310SBarry Smith } 559481824310SBarry Smith #if defined (PETSC_USE_CTABLE) 559581824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 559681824310SBarry Smith col--; 559781824310SBarry Smith #else 559881824310SBarry Smith col = aij->colmap[in[j]] - 1; 559981824310SBarry Smith #endif 560081824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 560181824310SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 560281824310SBarry Smith col = in[j]; 560381824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 560481824310SBarry Smith B = aij->B; 560581824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 560681824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 560781824310SBarry Smith rp2 = bj + bi[row]; 560881824310SBarry Smith ap2 = ba + bi[row]; 560981824310SBarry Smith rmax2 = bimax[row]; 561081824310SBarry Smith nrow2 = bilen[row]; 561181824310SBarry Smith low2 = 0; 561281824310SBarry Smith high2 = nrow2; 5613d0f46423SBarry Smith bm = aij->B->rmap->n; 561481824310SBarry Smith ba = b->a; 561581824310SBarry Smith } 561681824310SBarry Smith } else col = in[j]; 561781824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 561881824310SBarry Smith } 561981824310SBarry Smith } 562081824310SBarry Smith } else { 562181824310SBarry Smith if (!aij->donotstash) { 562281824310SBarry Smith if (roworiented) { 5623ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 562481824310SBarry Smith } else { 5625ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 562681824310SBarry Smith } 562781824310SBarry Smith } 562881824310SBarry Smith } 562981824310SBarry Smith }} 563081824310SBarry Smith PetscFunctionReturnVoid(); 563181824310SBarry Smith } 563281824310SBarry Smith EXTERN_C_END 563303bfb495SBarry Smith 5634