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 } 5102f53aa61SHong Zhang a->compressedrow.use = PETSC_FALSE; 511bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 512bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 513bc5ccf88SSatish Balay 514bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 515bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 516bc5ccf88SSatish Balay /* 517bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 518bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 519bc5ccf88SSatish Balay */ 520bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 5217adad957SLisandro Dalcin ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr); 522bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 523bc5ccf88SSatish Balay ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 524ad59fb31SSatish Balay } 525ad59fb31SSatish Balay } 526bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 527bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 528bc5ccf88SSatish Balay } 5294e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 53091c97fd4SSatish Balay ((Mat_SeqAIJ *)aij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */ 531bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 532bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 533bc5ccf88SSatish Balay 5341d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 535606d414cSSatish Balay aij->rowvalues = 0; 536a30b2313SHong Zhang 537a30b2313SHong Zhang /* used by MatAXPY() */ 53891c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 53991c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 540a30b2313SHong Zhang 541a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);aij->diag = 0;} 542bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 543bc5ccf88SSatish Balay PetscFunctionReturn(0); 544bc5ccf88SSatish Balay } 545bc5ccf88SSatish Balay 5464a2ae208SSatish Balay #undef __FUNCT__ 5474a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 548dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 5491eb62cbbSBarry Smith { 55044a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 551dfbe8321SBarry Smith PetscErrorCode ierr; 5523a40ed3dSBarry Smith 5533a40ed3dSBarry Smith PetscFunctionBegin; 55478b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 55578b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 5563a40ed3dSBarry Smith PetscFunctionReturn(0); 5571eb62cbbSBarry Smith } 5581eb62cbbSBarry Smith 5594a2ae208SSatish Balay #undef __FUNCT__ 5604a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 5612b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 5621eb62cbbSBarry Smith { 56344a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 5646849ba73SBarry Smith PetscErrorCode ierr; 5657adad957SLisandro Dalcin PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 566d0f46423SBarry Smith PetscInt i,*owners = A->rmap->range; 567b1d57f15SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 568b1d57f15SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 569b1d57f15SBarry Smith PetscInt *rvalues,count,base,slen,*source; 570d0f46423SBarry Smith PetscInt *lens,*lrows,*values,rstart=A->rmap->rstart; 5717adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)A)->comm; 5721eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 5731eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 57497b48c8fSBarry Smith const PetscScalar *xx; 57597b48c8fSBarry Smith PetscScalar *bb; 5766543fbbaSBarry Smith #if defined(PETSC_DEBUG) 577ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 5786543fbbaSBarry Smith #endif 5791eb62cbbSBarry Smith 5803a40ed3dSBarry Smith PetscFunctionBegin; 5811eb62cbbSBarry Smith /* first count number of contributors to each processor */ 582b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 583b1d57f15SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 584b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 5856543fbbaSBarry Smith j = 0; 5861eb62cbbSBarry Smith for (i=0; i<N; i++) { 5876543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 5886543fbbaSBarry Smith lastidx = idx; 5896543fbbaSBarry Smith for (; j<size; j++) { 5901eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 5916543fbbaSBarry Smith nprocs[2*j]++; 5926543fbbaSBarry Smith nprocs[2*j+1] = 1; 5936543fbbaSBarry Smith owner[i] = j; 5946543fbbaSBarry Smith #if defined(PETSC_DEBUG) 5956543fbbaSBarry Smith found = PETSC_TRUE; 5966543fbbaSBarry Smith #endif 5976543fbbaSBarry Smith break; 5981eb62cbbSBarry Smith } 5991eb62cbbSBarry Smith } 6006543fbbaSBarry Smith #if defined(PETSC_DEBUG) 601e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 6026543fbbaSBarry Smith found = PETSC_FALSE; 6036543fbbaSBarry Smith #endif 6041eb62cbbSBarry Smith } 605c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 6061eb62cbbSBarry Smith 6077367270fSBarry Smith if (A->nooffproczerorows) { 6087367270fSBarry 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"); 6097367270fSBarry Smith nrecvs = nsends; 6107367270fSBarry Smith nmax = N; 6117367270fSBarry Smith } else { 6121eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 613c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 6147367270fSBarry Smith } 6151eb62cbbSBarry Smith 6161eb62cbbSBarry Smith /* post receives: */ 617b1d57f15SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 618b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 6191eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 620b1d57f15SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 6211eb62cbbSBarry Smith } 6221eb62cbbSBarry Smith 6231eb62cbbSBarry Smith /* do sends: 6241eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 6251eb62cbbSBarry Smith the ith processor 6261eb62cbbSBarry Smith */ 627b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 628b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 629b1d57f15SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 6301eb62cbbSBarry Smith starts[0] = 0; 631c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6321eb62cbbSBarry Smith for (i=0; i<N; i++) { 6331eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 6341eb62cbbSBarry Smith } 6351eb62cbbSBarry Smith 6361eb62cbbSBarry Smith starts[0] = 0; 637c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 6381eb62cbbSBarry Smith count = 0; 63917699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 640c1dc657dSBarry Smith if (nprocs[2*i+1]) { 641b1d57f15SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 6421eb62cbbSBarry Smith } 6431eb62cbbSBarry Smith } 644606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 6451eb62cbbSBarry Smith 64617699dbbSLois Curfman McInnes base = owners[rank]; 6471eb62cbbSBarry Smith 6481eb62cbbSBarry Smith /* wait on receives */ 6491d79065fSBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 6501eb62cbbSBarry Smith count = nrecvs; slen = 0; 6511eb62cbbSBarry Smith while (count) { 652ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 6531eb62cbbSBarry Smith /* unpack receives into our local space */ 654b1d57f15SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 655d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 656d6dfbf8fSBarry Smith lens[imdex] = n; 6571eb62cbbSBarry Smith slen += n; 6581eb62cbbSBarry Smith count--; 6591eb62cbbSBarry Smith } 660606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 6611eb62cbbSBarry Smith 6621eb62cbbSBarry Smith /* move the data into the send scatter */ 663b1d57f15SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 6641eb62cbbSBarry Smith count = 0; 6651eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 6661eb62cbbSBarry Smith values = rvalues + i*nmax; 6671eb62cbbSBarry Smith for (j=0; j<lens[i]; j++) { 6681eb62cbbSBarry Smith lrows[count++] = values[j] - base; 6691eb62cbbSBarry Smith } 6701eb62cbbSBarry Smith } 671606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 6721d79065fSBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 673606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 674606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 6751eb62cbbSBarry Smith 67697b48c8fSBarry Smith /* fix right hand side if needed */ 67797b48c8fSBarry Smith if (x && b) { 67897b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 67997b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 68097b48c8fSBarry Smith for (i=0; i<N; i++) { 68197b48c8fSBarry Smith bb[lrows[i]] = diag*xx[lrows[i]]; 68297b48c8fSBarry Smith } 68397b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 68497b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 68597b48c8fSBarry Smith } 6866eb55b6aSBarry Smith /* 6876eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 688a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 6896eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 6906eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 6916eb55b6aSBarry Smith 6926eb55b6aSBarry Smith */ 693e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 6942b40b63fSBarry Smith ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr); 695d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 6962b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr); 697f4df32b1SMatthew Knepley } else if (diag != 0.0) { 6982b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 699fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 700e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 701512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 7026525c446SSatish Balay } 703e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 704e2d53e46SBarry Smith row = lrows[i] + rstart; 705f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 706e2d53e46SBarry Smith } 707e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 708e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 7096eb55b6aSBarry Smith } else { 7102b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 7116eb55b6aSBarry Smith } 712606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 71372dacd9aSBarry Smith 7141eb62cbbSBarry Smith /* wait on sends */ 7151eb62cbbSBarry Smith if (nsends) { 716b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 717ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 718606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 7191eb62cbbSBarry Smith } 720606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 721606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 7221eb62cbbSBarry Smith 7233a40ed3dSBarry Smith PetscFunctionReturn(0); 7241eb62cbbSBarry Smith } 7251eb62cbbSBarry Smith 7264a2ae208SSatish Balay #undef __FUNCT__ 727*9c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 728*9c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 729*9c7c4993SBarry Smith { 730*9c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 731*9c7c4993SBarry Smith PetscErrorCode ierr; 732*9c7c4993SBarry Smith PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 733*9c7c4993SBarry Smith PetscInt i,*owners = A->rmap->range; 734*9c7c4993SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 735*9c7c4993SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 736*9c7c4993SBarry Smith PetscInt *rvalues,count,base,slen,*source; 737*9c7c4993SBarry Smith PetscInt *lens,*lrows,*values,rstart=A->rmap->rstart; 738*9c7c4993SBarry Smith MPI_Comm comm = ((PetscObject)A)->comm; 739*9c7c4993SBarry Smith MPI_Request *send_waits,*recv_waits; 740*9c7c4993SBarry Smith MPI_Status recv_status,*send_status; 741*9c7c4993SBarry Smith const PetscScalar *xx; 742*9c7c4993SBarry Smith PetscScalar *bb; 743*9c7c4993SBarry Smith #if defined(PETSC_DEBUG) 744*9c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 745*9c7c4993SBarry Smith #endif 746*9c7c4993SBarry Smith 747*9c7c4993SBarry Smith PetscFunctionBegin; 748*9c7c4993SBarry Smith /* first count number of contributors to each processor */ 749*9c7c4993SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 750*9c7c4993SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 751*9c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 752*9c7c4993SBarry Smith j = 0; 753*9c7c4993SBarry Smith for (i=0; i<N; i++) { 754*9c7c4993SBarry Smith if (lastidx > (idx = rows[i])) j = 0; 755*9c7c4993SBarry Smith lastidx = idx; 756*9c7c4993SBarry Smith for (; j<size; j++) { 757*9c7c4993SBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 758*9c7c4993SBarry Smith nprocs[2*j]++; 759*9c7c4993SBarry Smith nprocs[2*j+1] = 1; 760*9c7c4993SBarry Smith owner[i] = j; 761*9c7c4993SBarry Smith #if defined(PETSC_DEBUG) 762*9c7c4993SBarry Smith found = PETSC_TRUE; 763*9c7c4993SBarry Smith #endif 764*9c7c4993SBarry Smith break; 765*9c7c4993SBarry Smith } 766*9c7c4993SBarry Smith } 767*9c7c4993SBarry Smith #if defined(PETSC_DEBUG) 768*9c7c4993SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 769*9c7c4993SBarry Smith found = PETSC_FALSE; 770*9c7c4993SBarry Smith #endif 771*9c7c4993SBarry Smith } 772*9c7c4993SBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 773*9c7c4993SBarry Smith 774*9c7c4993SBarry Smith if (A->nooffproczerorows) { 775*9c7c4993SBarry 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"); 776*9c7c4993SBarry Smith nrecvs = nsends; 777*9c7c4993SBarry Smith nmax = N; 778*9c7c4993SBarry Smith } else { 779*9c7c4993SBarry Smith /* inform other processors of number of messages and max length*/ 780*9c7c4993SBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 781*9c7c4993SBarry Smith } 782*9c7c4993SBarry Smith 783*9c7c4993SBarry Smith /* post receives: */ 784*9c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 785*9c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 786*9c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 787*9c7c4993SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 788*9c7c4993SBarry Smith } 789*9c7c4993SBarry Smith 790*9c7c4993SBarry Smith /* do sends: 791*9c7c4993SBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 792*9c7c4993SBarry Smith the ith processor 793*9c7c4993SBarry Smith */ 794*9c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 795*9c7c4993SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 796*9c7c4993SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 797*9c7c4993SBarry Smith starts[0] = 0; 798*9c7c4993SBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 799*9c7c4993SBarry Smith for (i=0; i<N; i++) { 800*9c7c4993SBarry Smith svalues[starts[owner[i]]++] = rows[i]; 801*9c7c4993SBarry Smith } 802*9c7c4993SBarry Smith 803*9c7c4993SBarry Smith starts[0] = 0; 804*9c7c4993SBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 805*9c7c4993SBarry Smith count = 0; 806*9c7c4993SBarry Smith for (i=0; i<size; i++) { 807*9c7c4993SBarry Smith if (nprocs[2*i+1]) { 808*9c7c4993SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 809*9c7c4993SBarry Smith } 810*9c7c4993SBarry Smith } 811*9c7c4993SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 812*9c7c4993SBarry Smith 813*9c7c4993SBarry Smith base = owners[rank]; 814*9c7c4993SBarry Smith 815*9c7c4993SBarry Smith /* wait on receives */ 816*9c7c4993SBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 817*9c7c4993SBarry Smith count = nrecvs; slen = 0; 818*9c7c4993SBarry Smith while (count) { 819*9c7c4993SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 820*9c7c4993SBarry Smith /* unpack receives into our local space */ 821*9c7c4993SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 822*9c7c4993SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 823*9c7c4993SBarry Smith lens[imdex] = n; 824*9c7c4993SBarry Smith slen += n; 825*9c7c4993SBarry Smith count--; 826*9c7c4993SBarry Smith } 827*9c7c4993SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 828*9c7c4993SBarry Smith 829*9c7c4993SBarry Smith /* move the data into the send scatter */ 830*9c7c4993SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 831*9c7c4993SBarry Smith count = 0; 832*9c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 833*9c7c4993SBarry Smith values = rvalues + i*nmax; 834*9c7c4993SBarry Smith for (j=0; j<lens[i]; j++) { 835*9c7c4993SBarry Smith lrows[count++] = values[j] - base; 836*9c7c4993SBarry Smith } 837*9c7c4993SBarry Smith } 838*9c7c4993SBarry Smith ierr = PetscFree(rvalues);CHKERRQ(ierr); 839*9c7c4993SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 840*9c7c4993SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 841*9c7c4993SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 842*9c7c4993SBarry Smith 843*9c7c4993SBarry Smith /* fix right hand side if needed */ 844*9c7c4993SBarry Smith if (x && b) { 845*9c7c4993SBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 846*9c7c4993SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 847*9c7c4993SBarry Smith for (i=0; i<N; i++) { 848*9c7c4993SBarry Smith bb[lrows[i]] = diag*xx[lrows[i]]; 849*9c7c4993SBarry Smith } 850*9c7c4993SBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 851*9c7c4993SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 852*9c7c4993SBarry Smith } 853*9c7c4993SBarry Smith /* 854*9c7c4993SBarry Smith Zero the required rows. If the "diagonal block" of the matrix 855*9c7c4993SBarry Smith is square and the user wishes to set the diagonal we use separate 856*9c7c4993SBarry Smith code so that MatSetValues() is not called for each diagonal allocating 857*9c7c4993SBarry Smith new memory, thus calling lots of mallocs and slowing things down. 858*9c7c4993SBarry Smith 859*9c7c4993SBarry Smith */ 860*9c7c4993SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 861*9c7c4993SBarry Smith ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr); 862*9c7c4993SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 863*9c7c4993SBarry Smith ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr); 864*9c7c4993SBarry Smith } else if (diag != 0.0) { 865*9c7c4993SBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 866*9c7c4993SBarry Smith if (((Mat_SeqAIJ*)l->A->data)->nonew) { 867*9c7c4993SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 868*9c7c4993SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 869*9c7c4993SBarry Smith } 870*9c7c4993SBarry Smith for (i = 0; i < slen; i++) { 871*9c7c4993SBarry Smith row = lrows[i] + rstart; 872*9c7c4993SBarry Smith ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 873*9c7c4993SBarry Smith } 874*9c7c4993SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 875*9c7c4993SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 876*9c7c4993SBarry Smith } else { 877*9c7c4993SBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 878*9c7c4993SBarry Smith } 879*9c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 880*9c7c4993SBarry Smith 881*9c7c4993SBarry Smith /* wait on sends */ 882*9c7c4993SBarry Smith if (nsends) { 883*9c7c4993SBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 884*9c7c4993SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 885*9c7c4993SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 886*9c7c4993SBarry Smith } 887*9c7c4993SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 888*9c7c4993SBarry Smith ierr = PetscFree(svalues);CHKERRQ(ierr); 889*9c7c4993SBarry Smith 890*9c7c4993SBarry Smith PetscFunctionReturn(0); 891*9c7c4993SBarry Smith } 892*9c7c4993SBarry Smith 893*9c7c4993SBarry Smith #undef __FUNCT__ 8944a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 895dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 8961eb62cbbSBarry Smith { 897416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 898dfbe8321SBarry Smith PetscErrorCode ierr; 899b1d57f15SBarry Smith PetscInt nt; 900416022c9SBarry Smith 9013a40ed3dSBarry Smith PetscFunctionBegin; 902a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 90365e19b50SBarry 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); 904ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 905f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 906ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 907f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 9083a40ed3dSBarry Smith PetscFunctionReturn(0); 9091eb62cbbSBarry Smith } 9101eb62cbbSBarry Smith 9114a2ae208SSatish Balay #undef __FUNCT__ 912bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 913bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 914bd0c2dcbSBarry Smith { 915bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 916bd0c2dcbSBarry Smith PetscErrorCode ierr; 917bd0c2dcbSBarry Smith 918bd0c2dcbSBarry Smith PetscFunctionBegin; 919bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 920bd0c2dcbSBarry Smith PetscFunctionReturn(0); 921bd0c2dcbSBarry Smith } 922bd0c2dcbSBarry Smith 923bd0c2dcbSBarry Smith #undef __FUNCT__ 9244a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 925dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 926da3a660dSBarry Smith { 927416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 928dfbe8321SBarry Smith PetscErrorCode ierr; 9293a40ed3dSBarry Smith 9303a40ed3dSBarry Smith PetscFunctionBegin; 931ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 932f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 933ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 934f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 9353a40ed3dSBarry Smith PetscFunctionReturn(0); 936da3a660dSBarry Smith } 937da3a660dSBarry Smith 9384a2ae208SSatish Balay #undef __FUNCT__ 9394a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 940dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 941da3a660dSBarry Smith { 942416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 943dfbe8321SBarry Smith PetscErrorCode ierr; 944ace3abfcSBarry Smith PetscBool merged; 945da3a660dSBarry Smith 9463a40ed3dSBarry Smith PetscFunctionBegin; 947a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 948da3a660dSBarry Smith /* do nondiagonal part */ 9497c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 950a5ff213dSBarry Smith if (!merged) { 951da3a660dSBarry Smith /* send it on its way */ 952ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 953da3a660dSBarry Smith /* do local part */ 9547c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 955da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 956a5ff213dSBarry Smith /* added in yy until the next line, */ 957ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 958a5ff213dSBarry Smith } else { 959a5ff213dSBarry Smith /* do local part */ 960a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 961a5ff213dSBarry Smith /* send it on its way */ 962ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 963a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 964ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 965a5ff213dSBarry Smith } 9663a40ed3dSBarry Smith PetscFunctionReturn(0); 967da3a660dSBarry Smith } 968da3a660dSBarry Smith 969cd0d46ebSvictorle EXTERN_C_BEGIN 970cd0d46ebSvictorle #undef __FUNCT__ 9715fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 972ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 973cd0d46ebSvictorle { 9744f423910Svictorle MPI_Comm comm; 975cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 97666501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 977cd0d46ebSvictorle IS Me,Notme; 9786849ba73SBarry Smith PetscErrorCode ierr; 979b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 980b1d57f15SBarry Smith PetscMPIInt size; 981cd0d46ebSvictorle 982cd0d46ebSvictorle PetscFunctionBegin; 98342e5f5b4Svictorle 98442e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 98566501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 9865485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 987cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 9884f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 989b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 990b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 99142e5f5b4Svictorle 99242e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 993cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 994cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 995b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 996cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 997cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 99870b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 999268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1000268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 100166501d38Svictorle Aoff = Aoffs[0]; 1002268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 100366501d38Svictorle Boff = Boffs[0]; 10045485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 100566501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 100666501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 100742e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 100842e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 100942e5f5b4Svictorle 1010cd0d46ebSvictorle PetscFunctionReturn(0); 1011cd0d46ebSvictorle } 1012cd0d46ebSvictorle EXTERN_C_END 1013cd0d46ebSvictorle 10144a2ae208SSatish Balay #undef __FUNCT__ 10154a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1016dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1017da3a660dSBarry Smith { 1018416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1019dfbe8321SBarry Smith PetscErrorCode ierr; 1020da3a660dSBarry Smith 10213a40ed3dSBarry Smith PetscFunctionBegin; 1022da3a660dSBarry Smith /* do nondiagonal part */ 10237c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1024da3a660dSBarry Smith /* send it on its way */ 1025ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1026da3a660dSBarry Smith /* do local part */ 10277c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1028a5ff213dSBarry Smith /* receive remote parts */ 1029ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 10303a40ed3dSBarry Smith PetscFunctionReturn(0); 1031da3a660dSBarry Smith } 1032da3a660dSBarry Smith 10331eb62cbbSBarry Smith /* 10341eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 10351eb62cbbSBarry Smith diagonal block 10361eb62cbbSBarry Smith */ 10374a2ae208SSatish Balay #undef __FUNCT__ 10384a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1039dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 10401eb62cbbSBarry Smith { 1041dfbe8321SBarry Smith PetscErrorCode ierr; 1042416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 10433a40ed3dSBarry Smith 10443a40ed3dSBarry Smith PetscFunctionBegin; 1045e7e72b3dSBarry 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"); 1046e7e72b3dSBarry 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"); 10473a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 10483a40ed3dSBarry Smith PetscFunctionReturn(0); 10491eb62cbbSBarry Smith } 10501eb62cbbSBarry Smith 10514a2ae208SSatish Balay #undef __FUNCT__ 10524a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1053f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1054052efed2SBarry Smith { 1055052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1056dfbe8321SBarry Smith PetscErrorCode ierr; 10573a40ed3dSBarry Smith 10583a40ed3dSBarry Smith PetscFunctionBegin; 1059f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1060f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 10613a40ed3dSBarry Smith PetscFunctionReturn(0); 1062052efed2SBarry Smith } 1063052efed2SBarry Smith 10644a2ae208SSatish Balay #undef __FUNCT__ 10654a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1066dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 10671eb62cbbSBarry Smith { 106844a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1069dfbe8321SBarry Smith PetscErrorCode ierr; 107083e2fdc7SBarry Smith 10713a40ed3dSBarry Smith PetscFunctionBegin; 1072aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1073d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1074a5a9c739SBarry Smith #endif 10758798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 1076a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);} 1077d88c0aacSHong Zhang if (aij->A){ierr = MatDestroy(aij->A);CHKERRQ(ierr);} 1078d88c0aacSHong Zhang if (aij->B){ierr = MatDestroy(aij->B);CHKERRQ(ierr);} 1079aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 10809c666560SBarry Smith if (aij->colmap) {ierr = PetscTableDestroy(aij->colmap);CHKERRQ(ierr);} 1081b1fc9764SSatish Balay #else 108205b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1083b1fc9764SSatish Balay #endif 108405b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 10857c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 10867c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 108703095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 10888aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1089606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 1090901853e0SKris Buschelman 1091dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1092901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 1093901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 1094901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 1095901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 1096901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 1097ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 1098901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 1099471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 11003a40ed3dSBarry Smith PetscFunctionReturn(0); 11011eb62cbbSBarry Smith } 1102ee50ffe9SBarry Smith 11034a2ae208SSatish Balay #undef __FUNCT__ 11048e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1105dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 11068e2fed03SBarry Smith { 11078e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 11088e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 11098e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 11106849ba73SBarry Smith PetscErrorCode ierr; 111132dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 11126f69ff64SBarry Smith int fd; 1113a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1114d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 11158e2fed03SBarry Smith PetscScalar *column_values; 111685ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 11178e2fed03SBarry Smith 11188e2fed03SBarry Smith PetscFunctionBegin; 11197adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 11207adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 11218e2fed03SBarry Smith nz = A->nz + B->nz; 1122958c9bccSBarry Smith if (!rank) { 11230700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1124d0f46423SBarry Smith header[1] = mat->rmap->N; 1125d0f46423SBarry Smith header[2] = mat->cmap->N; 11267adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 11278e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 11286f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11298e2fed03SBarry Smith /* get largest number of rows any processor has */ 1130d0f46423SBarry Smith rlen = mat->rmap->n; 1131d0f46423SBarry Smith range = mat->rmap->range; 11328e2fed03SBarry Smith for (i=1; i<size; i++) { 11338e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 11348e2fed03SBarry Smith } 11358e2fed03SBarry Smith } else { 11367adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1137d0f46423SBarry Smith rlen = mat->rmap->n; 11388e2fed03SBarry Smith } 11398e2fed03SBarry Smith 11408e2fed03SBarry Smith /* load up the local row counts */ 1141b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 1142d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 11438e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 11448e2fed03SBarry Smith } 11458e2fed03SBarry Smith 11468e2fed03SBarry Smith /* store the row lengths to the file */ 114785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1148958c9bccSBarry Smith if (!rank) { 11498e2fed03SBarry Smith MPI_Status status; 1150d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11518e2fed03SBarry Smith for (i=1; i<size; i++) { 115285ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 11538e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1154a1319256SJed Brown ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 11556f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11568e2fed03SBarry Smith } 115785ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 11588e2fed03SBarry Smith } else { 115985ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1160d0f46423SBarry Smith ierr = MPI_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 116185ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 11628e2fed03SBarry Smith } 11638e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 11648e2fed03SBarry Smith 11658e2fed03SBarry Smith /* load up the local column indices */ 11668e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 11677adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1168b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 11698e2fed03SBarry Smith cnt = 0; 1170d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 11718e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 11728e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 11738e2fed03SBarry Smith column_indices[cnt++] = col; 11748e2fed03SBarry Smith } 11758e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 11768e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 11778e2fed03SBarry Smith } 11788e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 11798e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 11808e2fed03SBarry Smith } 11818e2fed03SBarry Smith } 1182e32f2f54SBarry 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); 11838e2fed03SBarry Smith 11848e2fed03SBarry Smith /* store the column indices to the file */ 118585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1186958c9bccSBarry Smith if (!rank) { 11878e2fed03SBarry Smith MPI_Status status; 11886f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11898e2fed03SBarry Smith for (i=1; i<size; i++) { 119085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 11917adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1192e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 11937adad957SLisandro Dalcin ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 11946f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11958e2fed03SBarry Smith } 119685ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 11978e2fed03SBarry Smith } else { 119885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 11997adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12007adad957SLisandro Dalcin ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 120185ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12028e2fed03SBarry Smith } 12038e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 12048e2fed03SBarry Smith 12058e2fed03SBarry Smith /* load up the local column values */ 12068e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 12078e2fed03SBarry Smith cnt = 0; 1208d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12098e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12108e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 12118e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12128e2fed03SBarry Smith } 12138e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 12148e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 12158e2fed03SBarry Smith } 12168e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 12178e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12188e2fed03SBarry Smith } 12198e2fed03SBarry Smith } 1220e32f2f54SBarry 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); 12218e2fed03SBarry Smith 12228e2fed03SBarry Smith /* store the column values to the file */ 122385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1224958c9bccSBarry Smith if (!rank) { 12258e2fed03SBarry Smith MPI_Status status; 12266f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12278e2fed03SBarry Smith for (i=1; i<size; i++) { 122885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 12297adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1230e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 12317adad957SLisandro Dalcin ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 12326f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12338e2fed03SBarry Smith } 123485ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 12358e2fed03SBarry Smith } else { 123685ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 12377adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12387adad957SLisandro Dalcin ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 123985ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12408e2fed03SBarry Smith } 12418e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 12428e2fed03SBarry Smith PetscFunctionReturn(0); 12438e2fed03SBarry Smith } 12448e2fed03SBarry Smith 12458e2fed03SBarry Smith #undef __FUNCT__ 12464a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1247dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1248416022c9SBarry Smith { 124944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1250dfbe8321SBarry Smith PetscErrorCode ierr; 125132dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1252ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1253b0a32e0cSBarry Smith PetscViewer sviewer; 1254f3ef73ceSBarry Smith PetscViewerFormat format; 1255416022c9SBarry Smith 12563a40ed3dSBarry Smith PetscFunctionBegin; 12572692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 12582692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 12592692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 126032077d6dSBarry Smith if (iascii) { 1261b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1262456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 12634e220ebcSLois Curfman McInnes MatInfo info; 1264ace3abfcSBarry Smith PetscBool inodes; 1265923f20ffSKris Buschelman 12667adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1267888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1268923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 1269923f20ffSKris Buschelman if (!inodes) { 127077431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1271d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 12726831982aSBarry Smith } else { 127377431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1274d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 12756831982aSBarry Smith } 1276888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 127777431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1278888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 127977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1280b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 128107d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1282a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 12833a40ed3dSBarry Smith PetscFunctionReturn(0); 1284fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1285923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1286923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1287923f20ffSKris Buschelman if (inodes) { 1288923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1289d38fa0fbSBarry Smith } else { 1290d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1291d38fa0fbSBarry Smith } 12923a40ed3dSBarry Smith PetscFunctionReturn(0); 12934aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 12944aedb280SBarry Smith PetscFunctionReturn(0); 129508480c60SBarry Smith } 12968e2fed03SBarry Smith } else if (isbinary) { 12978e2fed03SBarry Smith if (size == 1) { 12987adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 12998e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13008e2fed03SBarry Smith } else { 13018e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 13028e2fed03SBarry Smith } 13038e2fed03SBarry Smith PetscFunctionReturn(0); 13040f5bd95cSBarry Smith } else if (isdraw) { 1305b0a32e0cSBarry Smith PetscDraw draw; 1306ace3abfcSBarry Smith PetscBool isnull; 1307b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1308b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 130919bcc07fSBarry Smith } 131019bcc07fSBarry Smith 131117699dbbSLois Curfman McInnes if (size == 1) { 13127adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 131378b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13143a40ed3dSBarry Smith } else { 131595373324SBarry Smith /* assemble the entire matrix onto first processor. */ 131695373324SBarry Smith Mat A; 1317ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1318d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1319dd6ea824SBarry Smith MatScalar *a; 13202ee70a88SLois Curfman McInnes 132132a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1322ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 132332a366e4SMatthew Knepley 1324acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 132532a366e4SMatthew Knepley if (!flg) { 1326e7e72b3dSBarry 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."); 132732a366e4SMatthew Knepley } 132832a366e4SMatthew Knepley } 13290805154bSBarry Smith 13307adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 133117699dbbSLois Curfman McInnes if (!rank) { 1332f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 13333a40ed3dSBarry Smith } else { 1334f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 133595373324SBarry Smith } 1336f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1337f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1338f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 133952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1340416022c9SBarry Smith 134195373324SBarry Smith /* copy over the A part */ 1342ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1343d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1344d0f46423SBarry Smith row = mat->rmap->rstart; 1345d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 134695373324SBarry Smith for (i=0; i<m; i++) { 1347416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 134895373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 134995373324SBarry Smith } 13502ee70a88SLois Curfman McInnes aj = Aloc->j; 1351d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 135295373324SBarry Smith 135395373324SBarry Smith /* copy over the B part */ 1354ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1355d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1356d0f46423SBarry Smith row = mat->rmap->rstart; 1357b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1358b0a32e0cSBarry Smith ct = cols; 1359bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 136095373324SBarry Smith for (i=0; i<m; i++) { 1361416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 136295373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 136395373324SBarry Smith } 1364606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 13656d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13666d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 136755843e3eSBarry Smith /* 136855843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1369b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 137055843e3eSBarry Smith */ 1371b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1372e03a110bSBarry Smith if (!rank) { 13737adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 13747566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 13757566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 13766831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 137795373324SBarry Smith } 1378b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 137978b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 138095373324SBarry Smith } 13813a40ed3dSBarry Smith PetscFunctionReturn(0); 13821eb62cbbSBarry Smith } 13831eb62cbbSBarry Smith 13844a2ae208SSatish Balay #undef __FUNCT__ 13854a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1386dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1387416022c9SBarry Smith { 1388dfbe8321SBarry Smith PetscErrorCode ierr; 1389ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1390416022c9SBarry Smith 13913a40ed3dSBarry Smith PetscFunctionBegin; 13922692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 13932692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 13942692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 13952692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 139632077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 13977b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 13985cd90555SBarry Smith } else { 1399e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1400416022c9SBarry Smith } 14013a40ed3dSBarry Smith PetscFunctionReturn(0); 1402416022c9SBarry Smith } 1403416022c9SBarry Smith 14044a2ae208SSatish Balay #undef __FUNCT__ 140541f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 140641f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 14078a729477SBarry Smith { 140844a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1409dfbe8321SBarry Smith PetscErrorCode ierr; 14106987fefcSBarry Smith Vec bb1 = 0; 1411ace3abfcSBarry Smith PetscBool hasop; 14128a729477SBarry Smith 14133a40ed3dSBarry Smith PetscFunctionBegin; 141485911e72SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 141585911e72SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 141685911e72SJed Brown } 14172798e883SHong Zhang 1418a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 141941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1420a2b30743SBarry Smith PetscFunctionReturn(0); 1421a2b30743SBarry Smith } 1422a2b30743SBarry Smith 1423c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1424da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 142541f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14262798e883SHong Zhang its--; 1427da3a660dSBarry Smith } 14282798e883SHong Zhang 14292798e883SHong Zhang while (its--) { 1430ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1431ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14322798e883SHong Zhang 1433c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1434efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1435c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14362798e883SHong Zhang 1437c14dc6b6SHong Zhang /* local sweep */ 143841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14392798e883SHong Zhang } 14403a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1441da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 144241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14432798e883SHong Zhang its--; 1444da3a660dSBarry Smith } 14452798e883SHong Zhang while (its--) { 1446ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1447ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14482798e883SHong Zhang 1449c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1450efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1451c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1452c14dc6b6SHong Zhang 1453c14dc6b6SHong Zhang /* local sweep */ 145441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14552798e883SHong Zhang } 14563a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1457da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 145841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14592798e883SHong Zhang its--; 1460da3a660dSBarry Smith } 14612798e883SHong Zhang while (its--) { 1462ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1463ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14642798e883SHong Zhang 1465c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1466efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1467c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14682798e883SHong Zhang 1469c14dc6b6SHong Zhang /* local sweep */ 147041f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14712798e883SHong Zhang } 1472a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1473a7420bb7SBarry Smith Vec xx1; 1474a7420bb7SBarry Smith 1475a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 147641f059aeSBarry 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); 1477a7420bb7SBarry Smith 1478a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1479a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1480a7420bb7SBarry Smith if (!mat->diag) { 1481a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1482a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1483a7420bb7SBarry Smith } 1484bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1485bd0c2dcbSBarry Smith if (hasop) { 1486bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1487bd0c2dcbSBarry Smith } else { 1488a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1489bd0c2dcbSBarry Smith } 1490887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1491887ee2caSBarry Smith 1492a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1493a7420bb7SBarry Smith 1494a7420bb7SBarry Smith /* local sweep */ 149541f059aeSBarry 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); 1496a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 1497a7420bb7SBarry Smith ierr = VecDestroy(xx1);CHKERRQ(ierr); 14983a40ed3dSBarry Smith } else { 1499e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1500c16cb8f2SBarry Smith } 1501c14dc6b6SHong Zhang 15026987fefcSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 15033a40ed3dSBarry Smith PetscFunctionReturn(0); 15048a729477SBarry Smith } 1505a66be287SLois Curfman McInnes 15064a2ae208SSatish Balay #undef __FUNCT__ 150742e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 150842e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 150942e855d1Svictor { 151042e855d1Svictor MPI_Comm comm,pcomm; 15115d0c19d7SBarry Smith PetscInt first,local_size,nrows; 15125d0c19d7SBarry Smith const PetscInt *rows; 1513dbf0e21dSBarry Smith PetscMPIInt size; 151442e855d1Svictor IS crowp,growp,irowp,lrowp,lcolp,icolp; 151542e855d1Svictor PetscErrorCode ierr; 151642e855d1Svictor 151742e855d1Svictor PetscFunctionBegin; 151842e855d1Svictor ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 151942e855d1Svictor /* make a collective version of 'rowp' */ 152042e855d1Svictor ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 152142e855d1Svictor if (pcomm==comm) { 152242e855d1Svictor crowp = rowp; 152342e855d1Svictor } else { 152442e855d1Svictor ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 152542e855d1Svictor ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 152670b3c8c7SBarry Smith ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr); 152742e855d1Svictor ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 152842e855d1Svictor } 152942e855d1Svictor /* collect the global row permutation and invert it */ 153042e855d1Svictor ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 153142e855d1Svictor ierr = ISSetPermutation(growp);CHKERRQ(ierr); 153242e855d1Svictor if (pcomm!=comm) { 153342e855d1Svictor ierr = ISDestroy(crowp);CHKERRQ(ierr); 153442e855d1Svictor } 153542e855d1Svictor ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 153642e855d1Svictor /* get the local target indices */ 153742e855d1Svictor ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 153842e855d1Svictor ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 153942e855d1Svictor ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 154070b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr); 154142e855d1Svictor ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 154242e855d1Svictor ierr = ISDestroy(irowp);CHKERRQ(ierr); 154342e855d1Svictor /* the column permutation is so much easier; 154442e855d1Svictor make a local version of 'colp' and invert it */ 154542e855d1Svictor ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 1546dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 1547dbf0e21dSBarry Smith if (size==1) { 154842e855d1Svictor lcolp = colp; 154942e855d1Svictor } else { 155042e855d1Svictor ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 155142e855d1Svictor ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 155270b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr); 155342e855d1Svictor } 1554dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 155542e855d1Svictor ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 15564aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 1557dbf0e21dSBarry Smith if (size>1) { 155842e855d1Svictor ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 155942e855d1Svictor ierr = ISDestroy(lcolp);CHKERRQ(ierr); 156042e855d1Svictor } 156142e855d1Svictor /* now we just get the submatrix */ 15624aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 156342e855d1Svictor /* clean up */ 156442e855d1Svictor ierr = ISDestroy(lrowp);CHKERRQ(ierr); 156542e855d1Svictor ierr = ISDestroy(icolp);CHKERRQ(ierr); 156642e855d1Svictor PetscFunctionReturn(0); 156742e855d1Svictor } 156842e855d1Svictor 156942e855d1Svictor #undef __FUNCT__ 15704a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1571dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1572a66be287SLois Curfman McInnes { 1573a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1574a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1575dfbe8321SBarry Smith PetscErrorCode ierr; 1576329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1577a66be287SLois Curfman McInnes 15783a40ed3dSBarry Smith PetscFunctionBegin; 15794e220ebcSLois Curfman McInnes info->block_size = 1.0; 15804e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 15814e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 15824e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 15834e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 15844e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 15854e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1586a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 15874e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 15884e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 15894e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 15904e220ebcSLois Curfman McInnes info->memory = isend[3]; 15914e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1592a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 15937adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 15944e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 15954e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 15964e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 15974e220ebcSLois Curfman McInnes info->memory = irecv[3]; 15984e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1599a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 16007adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 16014e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16024e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16034e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16044e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16054e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1606a66be287SLois Curfman McInnes } 16074e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 16084e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 16094e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 16104e220ebcSLois Curfman McInnes 16113a40ed3dSBarry Smith PetscFunctionReturn(0); 1612a66be287SLois Curfman McInnes } 1613a66be287SLois Curfman McInnes 16144a2ae208SSatish Balay #undef __FUNCT__ 16154a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1616ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1617c74985f6SBarry Smith { 1618c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1619dfbe8321SBarry Smith PetscErrorCode ierr; 1620c74985f6SBarry Smith 16213a40ed3dSBarry Smith PetscFunctionBegin; 162212c028f9SKris Buschelman switch (op) { 1623512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 162412c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 162528b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1626a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 162712c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 162812c028f9SKris Buschelman case MAT_USE_INODES: 162912c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 16304e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16314e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 163212c028f9SKris Buschelman break; 163312c028f9SKris Buschelman case MAT_ROW_ORIENTED: 16344e0d8c25SBarry Smith a->roworiented = flg; 16354e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16364e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 163712c028f9SKris Buschelman break; 16384e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1639290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 164012c028f9SKris Buschelman break; 164112c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 16427c922b88SBarry Smith a->donotstash = PETSC_TRUE; 164312c028f9SKris Buschelman break; 1644ffa07934SHong Zhang case MAT_SPD: 1645ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1646ffa07934SHong Zhang A->spd = flg; 1647ffa07934SHong Zhang if (flg) { 1648ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1649ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1650ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1651ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1652ffa07934SHong Zhang } 1653ffa07934SHong Zhang break; 165477e54ba9SKris Buschelman case MAT_SYMMETRIC: 16554e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 165625f421beSHong Zhang break; 165777e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1658eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1659eeffb40dSHong Zhang break; 1660bf108f30SBarry Smith case MAT_HERMITIAN: 1661eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1662eeffb40dSHong Zhang break; 1663bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 16644e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 166577e54ba9SKris Buschelman break; 166612c028f9SKris Buschelman default: 1667e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 16683a40ed3dSBarry Smith } 16693a40ed3dSBarry Smith PetscFunctionReturn(0); 1670c74985f6SBarry Smith } 1671c74985f6SBarry Smith 16724a2ae208SSatish Balay #undef __FUNCT__ 16734a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1674b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 167539e00950SLois Curfman McInnes { 1676154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 167787828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 16786849ba73SBarry Smith PetscErrorCode ierr; 1679d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1680d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1681b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 168239e00950SLois Curfman McInnes 16833a40ed3dSBarry Smith PetscFunctionBegin; 1684e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 16857a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 16867a0afa10SBarry Smith 168770f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 16887a0afa10SBarry Smith /* 16897a0afa10SBarry Smith allocate enough space to hold information from the longest row. 16907a0afa10SBarry Smith */ 16917a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1692b1d57f15SBarry Smith PetscInt max = 1,tmp; 1693d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 16947a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 16957a0afa10SBarry Smith if (max < tmp) { max = tmp; } 16967a0afa10SBarry Smith } 16971d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 16987a0afa10SBarry Smith } 16997a0afa10SBarry Smith 1700e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1701abc0e9e4SLois Curfman McInnes lrow = row - rstart; 170239e00950SLois Curfman McInnes 1703154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1704154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1705154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1706f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1707f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1708154123eaSLois Curfman McInnes nztot = nzA + nzB; 1709154123eaSLois Curfman McInnes 171070f0671dSBarry Smith cmap = mat->garray; 1711154123eaSLois Curfman McInnes if (v || idx) { 1712154123eaSLois Curfman McInnes if (nztot) { 1713154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1714b1d57f15SBarry Smith PetscInt imark = -1; 1715154123eaSLois Curfman McInnes if (v) { 171670f0671dSBarry Smith *v = v_p = mat->rowvalues; 171739e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 171870f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1719154123eaSLois Curfman McInnes else break; 1720154123eaSLois Curfman McInnes } 1721154123eaSLois Curfman McInnes imark = i; 172270f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 172370f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1724154123eaSLois Curfman McInnes } 1725154123eaSLois Curfman McInnes if (idx) { 172670f0671dSBarry Smith *idx = idx_p = mat->rowindices; 172770f0671dSBarry Smith if (imark > -1) { 172870f0671dSBarry Smith for (i=0; i<imark; i++) { 172970f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 173070f0671dSBarry Smith } 173170f0671dSBarry Smith } else { 1732154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 173370f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1734154123eaSLois Curfman McInnes else break; 1735154123eaSLois Curfman McInnes } 1736154123eaSLois Curfman McInnes imark = i; 173770f0671dSBarry Smith } 173870f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 173970f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 174039e00950SLois Curfman McInnes } 17413f97c4b0SBarry Smith } else { 17421ca473b0SSatish Balay if (idx) *idx = 0; 17431ca473b0SSatish Balay if (v) *v = 0; 17441ca473b0SSatish Balay } 1745154123eaSLois Curfman McInnes } 174639e00950SLois Curfman McInnes *nz = nztot; 1747f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1748f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 17493a40ed3dSBarry Smith PetscFunctionReturn(0); 175039e00950SLois Curfman McInnes } 175139e00950SLois Curfman McInnes 17524a2ae208SSatish Balay #undef __FUNCT__ 17534a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1754b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 175539e00950SLois Curfman McInnes { 17567a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17573a40ed3dSBarry Smith 17583a40ed3dSBarry Smith PetscFunctionBegin; 1759e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 17607a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 17613a40ed3dSBarry Smith PetscFunctionReturn(0); 176239e00950SLois Curfman McInnes } 176339e00950SLois Curfman McInnes 17644a2ae208SSatish Balay #undef __FUNCT__ 17654a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1766dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1767855ac2c5SLois Curfman McInnes { 1768855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1769ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1770dfbe8321SBarry Smith PetscErrorCode ierr; 1771d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1772329f5518SBarry Smith PetscReal sum = 0.0; 1773a77337e4SBarry Smith MatScalar *v; 177404ca555eSLois Curfman McInnes 17753a40ed3dSBarry Smith PetscFunctionBegin; 177617699dbbSLois Curfman McInnes if (aij->size == 1) { 177714183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 177837fa93a5SLois Curfman McInnes } else { 177904ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 178004ca555eSLois Curfman McInnes v = amat->a; 178104ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1782aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1783329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 178404ca555eSLois Curfman McInnes #else 178504ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 178604ca555eSLois Curfman McInnes #endif 178704ca555eSLois Curfman McInnes } 178804ca555eSLois Curfman McInnes v = bmat->a; 178904ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1790aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1791329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 179204ca555eSLois Curfman McInnes #else 179304ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 179404ca555eSLois Curfman McInnes #endif 179504ca555eSLois Curfman McInnes } 17967adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 179704ca555eSLois Curfman McInnes *norm = sqrt(*norm); 17983a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1799329f5518SBarry Smith PetscReal *tmp,*tmp2; 1800b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 1801d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1802d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1803d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 180404ca555eSLois Curfman McInnes *norm = 0.0; 180504ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 180604ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1807bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 180804ca555eSLois Curfman McInnes } 180904ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 181004ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1811bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 181204ca555eSLois Curfman McInnes } 1813d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 1814d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 181504ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 181604ca555eSLois Curfman McInnes } 1817606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1818606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 18193a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1820329f5518SBarry Smith PetscReal ntemp = 0.0; 1821d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1822bfec09a0SHong Zhang v = amat->a + amat->i[j]; 182304ca555eSLois Curfman McInnes sum = 0.0; 182404ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1825cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 182604ca555eSLois Curfman McInnes } 1827bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 182804ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1829cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 183004ca555eSLois Curfman McInnes } 1831515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 183204ca555eSLois Curfman McInnes } 18337adad957SLisandro Dalcin ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 1834ca161407SBarry Smith } else { 1835e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 183604ca555eSLois Curfman McInnes } 183737fa93a5SLois Curfman McInnes } 18383a40ed3dSBarry Smith PetscFunctionReturn(0); 1839855ac2c5SLois Curfman McInnes } 1840855ac2c5SLois Curfman McInnes 18414a2ae208SSatish Balay #undef __FUNCT__ 18424a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1843fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1844b7c46309SBarry Smith { 1845b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1846da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1847dfbe8321SBarry Smith PetscErrorCode ierr; 1848d0f46423SBarry Smith PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz; 1849d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 18503a40ed3dSBarry Smith Mat B; 1851a77337e4SBarry Smith MatScalar *array; 1852b7c46309SBarry Smith 18533a40ed3dSBarry Smith PetscFunctionBegin; 1854e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1855da668accSHong Zhang 1856d0f46423SBarry Smith ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; 1857da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1858da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1859fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 1860fc73b1b3SBarry Smith /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */ 1861fc73b1b3SBarry Smith ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr); 1862da668accSHong Zhang ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr); 1863da668accSHong Zhang for (i=0; i<ai[ma]; i++){ 1864da668accSHong Zhang d_nnz[aj[i]] ++; 1865da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1866d4bb536fSBarry Smith } 1867d4bb536fSBarry Smith 18687adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1869d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 18707adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1871da668accSHong Zhang ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr); 1872fc73b1b3SBarry Smith ierr = PetscFree(d_nnz);CHKERRQ(ierr); 1873fc4dec0aSBarry Smith } else { 1874fc4dec0aSBarry Smith B = *matout; 1875fc4dec0aSBarry Smith } 1876b7c46309SBarry Smith 1877b7c46309SBarry Smith /* copy over the A part */ 1878da668accSHong Zhang array = Aloc->a; 1879d0f46423SBarry Smith row = A->rmap->rstart; 1880da668accSHong Zhang for (i=0; i<ma; i++) { 1881da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1882da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1883da668accSHong Zhang row++; array += ncol; aj += ncol; 1884b7c46309SBarry Smith } 1885b7c46309SBarry Smith aj = Aloc->j; 1886da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1887b7c46309SBarry Smith 1888b7c46309SBarry Smith /* copy over the B part */ 1889fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1890fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 1891da668accSHong Zhang array = Bloc->a; 1892d0f46423SBarry Smith row = A->rmap->rstart; 1893da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 189461a2fbbaSHong Zhang cols_tmp = cols; 1895da668accSHong Zhang for (i=0; i<mb; i++) { 1896da668accSHong Zhang ncol = bi[i+1]-bi[i]; 189761a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 189861a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 1899b7c46309SBarry Smith } 1900fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1901fc73b1b3SBarry Smith 19026d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19036d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1904815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 19050de55854SLois Curfman McInnes *matout = B; 19060de55854SLois Curfman McInnes } else { 1907eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 19080de55854SLois Curfman McInnes } 19093a40ed3dSBarry Smith PetscFunctionReturn(0); 1910b7c46309SBarry Smith } 1911b7c46309SBarry Smith 19124a2ae208SSatish Balay #undef __FUNCT__ 19134a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1914dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1915a008b906SSatish Balay { 19164b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 19174b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1918dfbe8321SBarry Smith PetscErrorCode ierr; 1919b1d57f15SBarry Smith PetscInt s1,s2,s3; 1920a008b906SSatish Balay 19213a40ed3dSBarry Smith PetscFunctionBegin; 19224b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 19234b967eb1SSatish Balay if (rr) { 1924e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1925e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 19264b967eb1SSatish Balay /* Overlap communication with computation. */ 1927ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1928a008b906SSatish Balay } 19294b967eb1SSatish Balay if (ll) { 1930e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1931e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1932f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 19334b967eb1SSatish Balay } 19344b967eb1SSatish Balay /* scale the diagonal block */ 1935f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 19364b967eb1SSatish Balay 19374b967eb1SSatish Balay if (rr) { 19384b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1939ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1940f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 19414b967eb1SSatish Balay } 19424b967eb1SSatish Balay 19433a40ed3dSBarry Smith PetscFunctionReturn(0); 1944a008b906SSatish Balay } 1945a008b906SSatish Balay 19464a2ae208SSatish Balay #undef __FUNCT__ 1947521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ" 1948521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs) 19495a838052SSatish Balay { 1950521d7252SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1951521d7252SBarry Smith PetscErrorCode ierr; 1952521d7252SBarry Smith 19533a40ed3dSBarry Smith PetscFunctionBegin; 1954521d7252SBarry Smith ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 1955521d7252SBarry Smith ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1956829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 1957829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 19583a40ed3dSBarry Smith PetscFunctionReturn(0); 19595a838052SSatish Balay } 19604a2ae208SSatish Balay #undef __FUNCT__ 19614a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 1962dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 1963bb5a7306SBarry Smith { 1964bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1965dfbe8321SBarry Smith PetscErrorCode ierr; 19663a40ed3dSBarry Smith 19673a40ed3dSBarry Smith PetscFunctionBegin; 1968bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 19693a40ed3dSBarry Smith PetscFunctionReturn(0); 1970bb5a7306SBarry Smith } 1971bb5a7306SBarry Smith 19724a2ae208SSatish Balay #undef __FUNCT__ 19734a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 1974ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 1975d4bb536fSBarry Smith { 1976d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 1977d4bb536fSBarry Smith Mat a,b,c,d; 1978ace3abfcSBarry Smith PetscBool flg; 1979dfbe8321SBarry Smith PetscErrorCode ierr; 1980d4bb536fSBarry Smith 19813a40ed3dSBarry Smith PetscFunctionBegin; 1982d4bb536fSBarry Smith a = matA->A; b = matA->B; 1983d4bb536fSBarry Smith c = matB->A; d = matB->B; 1984d4bb536fSBarry Smith 1985d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 1986abc0a331SBarry Smith if (flg) { 1987d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 1988d4bb536fSBarry Smith } 19897adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 19903a40ed3dSBarry Smith PetscFunctionReturn(0); 1991d4bb536fSBarry Smith } 1992d4bb536fSBarry Smith 19934a2ae208SSatish Balay #undef __FUNCT__ 19944a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 1995dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1996cb5b572fSBarry Smith { 1997dfbe8321SBarry Smith PetscErrorCode ierr; 1998cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1999cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 2000cb5b572fSBarry Smith 2001cb5b572fSBarry Smith PetscFunctionBegin; 200233f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 200333f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2004cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2005cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2006cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2007cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2008cb5b572fSBarry Smith then copying the submatrices */ 2009cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2010cb5b572fSBarry Smith } else { 2011cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2012cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2013cb5b572fSBarry Smith } 2014cb5b572fSBarry Smith PetscFunctionReturn(0); 2015cb5b572fSBarry Smith } 2016cb5b572fSBarry Smith 20174a2ae208SSatish Balay #undef __FUNCT__ 20184a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 2019dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A) 2020273d9f13SBarry Smith { 2021dfbe8321SBarry Smith PetscErrorCode ierr; 2022273d9f13SBarry Smith 2023273d9f13SBarry Smith PetscFunctionBegin; 2024273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2025273d9f13SBarry Smith PetscFunctionReturn(0); 2026273d9f13SBarry Smith } 2027273d9f13SBarry Smith 2028ac90fabeSBarry Smith #undef __FUNCT__ 2029ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2030f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2031ac90fabeSBarry Smith { 2032dfbe8321SBarry Smith PetscErrorCode ierr; 2033b1d57f15SBarry Smith PetscInt i; 2034ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 20354ce68768SBarry Smith PetscBLASInt bnz,one=1; 2036ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2037ac90fabeSBarry Smith 2038ac90fabeSBarry Smith PetscFunctionBegin; 2039ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2040f4df32b1SMatthew Knepley PetscScalar alpha = a; 2041ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 2042ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 20430805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2044f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2045ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 2046ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 20470805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2048f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2049a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2050f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2051c537a176SHong Zhang 2052c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 2053a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 2054a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2055a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2056a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2057c537a176SHong Zhang } 2058a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2059d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2060a30b2313SHong Zhang y->XtoY = xx->B; 2061407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2062c537a176SHong Zhang } 2063f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2064ac90fabeSBarry Smith } else { 20659f5f6813SShri Abhyankar Mat B; 20669f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 20679f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 20689f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 20699f5f6813SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 2070bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 20719f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 20729f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 20739f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 20749f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->B,xx->B,nnz_o);CHKERRQ(ierr); 20759f5f6813SShri Abhyankar ierr = MatMPIAIJSetPreallocation(B,PETSC_NULL,nnz_d,PETSC_NULL,nnz_o);CHKERRQ(ierr); 20769f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 20779f5f6813SShri Abhyankar ierr = MatHeaderReplace(Y,B); 20789f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 20799f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2080ac90fabeSBarry Smith } 2081ac90fabeSBarry Smith PetscFunctionReturn(0); 2082ac90fabeSBarry Smith } 2083ac90fabeSBarry Smith 2084354c94deSBarry Smith EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat); 2085354c94deSBarry Smith 2086354c94deSBarry Smith #undef __FUNCT__ 2087354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 2088354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat) 2089354c94deSBarry Smith { 2090354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2091354c94deSBarry Smith PetscErrorCode ierr; 2092354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2093354c94deSBarry Smith 2094354c94deSBarry Smith PetscFunctionBegin; 2095354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2096354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2097354c94deSBarry Smith #else 2098354c94deSBarry Smith PetscFunctionBegin; 2099354c94deSBarry Smith #endif 2100354c94deSBarry Smith PetscFunctionReturn(0); 2101354c94deSBarry Smith } 2102354c94deSBarry Smith 210399cafbc1SBarry Smith #undef __FUNCT__ 210499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 210599cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 210699cafbc1SBarry Smith { 210799cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 210899cafbc1SBarry Smith PetscErrorCode ierr; 210999cafbc1SBarry Smith 211099cafbc1SBarry Smith PetscFunctionBegin; 211199cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 211299cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 211399cafbc1SBarry Smith PetscFunctionReturn(0); 211499cafbc1SBarry Smith } 211599cafbc1SBarry Smith 211699cafbc1SBarry Smith #undef __FUNCT__ 211799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 211899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 211999cafbc1SBarry Smith { 212099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 212199cafbc1SBarry Smith PetscErrorCode ierr; 212299cafbc1SBarry Smith 212399cafbc1SBarry Smith PetscFunctionBegin; 212499cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 212599cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 212699cafbc1SBarry Smith PetscFunctionReturn(0); 212799cafbc1SBarry Smith } 212899cafbc1SBarry Smith 2129103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2130103bf8bdSMatthew Knepley 2131103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2132a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2133a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2134a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2135103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2136a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2137d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2138103bf8bdSMatthew Knepley 2139103bf8bdSMatthew Knepley #undef __FUNCT__ 2140103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2141103bf8bdSMatthew Knepley /* 2142103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2143103bf8bdSMatthew Knepley */ 21440481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2145103bf8bdSMatthew Knepley { 2146a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2147a2c909beSMatthew Knepley 2148a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2149a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2150a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2151a2c909beSMatthew Knepley 2152ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2153776b82aeSLisandro Dalcin PetscContainer c; 2154103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2155103bf8bdSMatthew Knepley PetscErrorCode ierr; 2156103bf8bdSMatthew Knepley 2157103bf8bdSMatthew Knepley PetscFunctionBegin; 2158e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2159103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2160103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2161103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 2162e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2163103bf8bdSMatthew Knepley } 2164103bf8bdSMatthew Knepley 2165103bf8bdSMatthew Knepley process_group_type pg; 2166a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2167a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2168a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2169a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2170a2c909beSMatthew Knepley 2171103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2172a2c909beSMatthew Knepley ilu_permuted(level_graph); 2173103bf8bdSMatthew Knepley 2174103bf8bdSMatthew Knepley /* put together the new matrix */ 21757adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 2176103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2177103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2178719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2179719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2180719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2181719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2182103bf8bdSMatthew Knepley 21837adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 2184776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2185719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2186103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2187103bf8bdSMatthew Knepley } 2188103bf8bdSMatthew Knepley 2189103bf8bdSMatthew Knepley #undef __FUNCT__ 2190103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 21910481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2192103bf8bdSMatthew Knepley { 2193103bf8bdSMatthew Knepley PetscFunctionBegin; 2194103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2195103bf8bdSMatthew Knepley } 2196103bf8bdSMatthew Knepley 2197103bf8bdSMatthew Knepley #undef __FUNCT__ 2198103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2199103bf8bdSMatthew Knepley /* 2200103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2201103bf8bdSMatthew Knepley */ 2202103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2203103bf8bdSMatthew Knepley { 2204a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2205a2c909beSMatthew Knepley 2206a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2207a2c909beSMatthew Knepley lgraph_type* lgraph_p; 2208776b82aeSLisandro Dalcin PetscContainer c; 2209103bf8bdSMatthew Knepley PetscErrorCode ierr; 2210103bf8bdSMatthew Knepley 2211103bf8bdSMatthew Knepley PetscFunctionBegin; 2212103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 2213776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 2214103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2215a2c909beSMatthew Knepley 2216a2c909beSMatthew Knepley PetscScalar* array_x; 2217a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2218a2c909beSMatthew Knepley PetscInt sx; 2219a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2220a2c909beSMatthew Knepley 2221a2c909beSMatthew Knepley PetscScalar* array_b; 2222a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2223a2c909beSMatthew Knepley PetscInt sb; 2224a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2225a2c909beSMatthew Knepley 2226a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2227a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2228a2c909beSMatthew Knepley 2229a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2230a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2231a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2232a2c909beSMatthew Knepley 2233a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2234a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2235a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2236a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2237a2c909beSMatthew Knepley 2238a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2239a2c909beSMatthew Knepley 2240103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2241103bf8bdSMatthew Knepley } 2242103bf8bdSMatthew Knepley #endif 2243103bf8bdSMatthew Knepley 224469db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 224569db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 22461d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 22471d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 224869db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 224969db28dcSHong Zhang PetscErrorCode (*MatDestroy)(Mat); 225069db28dcSHong Zhang } Mat_Redundant; 225169db28dcSHong Zhang 225269db28dcSHong Zhang #undef __FUNCT__ 225369db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 225469db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 225569db28dcSHong Zhang { 225669db28dcSHong Zhang PetscErrorCode ierr; 225769db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 225869db28dcSHong Zhang PetscInt i; 225969db28dcSHong Zhang 226069db28dcSHong Zhang PetscFunctionBegin; 22611d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 226269db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 226369db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 226469db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 226569db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 226669db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 226769db28dcSHong Zhang } 22681d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 226969db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 227069db28dcSHong Zhang PetscFunctionReturn(0); 227169db28dcSHong Zhang } 227269db28dcSHong Zhang 227369db28dcSHong Zhang #undef __FUNCT__ 227469db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 227569db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 227669db28dcSHong Zhang { 227769db28dcSHong Zhang PetscErrorCode ierr; 227869db28dcSHong Zhang PetscContainer container; 227969db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 228069db28dcSHong Zhang 228169db28dcSHong Zhang PetscFunctionBegin; 228269db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 228369db28dcSHong Zhang if (container) { 228469db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 228569db28dcSHong Zhang } else { 2286e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 228769db28dcSHong Zhang } 228869db28dcSHong Zhang A->ops->destroy = redund->MatDestroy; 228969db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 229069db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 229169db28dcSHong Zhang ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 229269db28dcSHong Zhang PetscFunctionReturn(0); 229369db28dcSHong Zhang } 229469db28dcSHong Zhang 229569db28dcSHong Zhang #undef __FUNCT__ 229669db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 229769db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 229869db28dcSHong Zhang { 229969db28dcSHong Zhang PetscMPIInt rank,size; 23007adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 230169db28dcSHong Zhang PetscErrorCode ierr; 230269db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 230369db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2304d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 230569db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 230669db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 230769db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 230869db28dcSHong Zhang PetscScalar *sbuf_a; 230969db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2310d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2311d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 231269db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2313a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2314a77337e4SBarry Smith PetscScalar *vals; 231569db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 231669db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 231769db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 231869db28dcSHong Zhang MPI_Status recv_status,*send_status; 231969db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 232069db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 232169db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 232269db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 232369db28dcSHong Zhang PetscContainer container; 232469db28dcSHong Zhang 232569db28dcSHong Zhang PetscFunctionBegin; 232669db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 232769db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 232869db28dcSHong Zhang 232969db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 233069db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2331e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 233269db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2333e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 233469db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 233569db28dcSHong Zhang if (container) { 233669db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 233769db28dcSHong Zhang } else { 2338e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 233969db28dcSHong Zhang } 2340e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 234169db28dcSHong Zhang 234269db28dcSHong Zhang nsends = redund->nsends; 234369db28dcSHong Zhang nrecvs = redund->nrecvs; 23441d79065fSBarry Smith send_rank = redund->send_rank; 23451d79065fSBarry Smith recv_rank = redund->recv_rank; 23461d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 23471d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 234869db28dcSHong Zhang sbuf_j = redund->sbuf_j; 234969db28dcSHong Zhang sbuf_a = redund->sbuf_a; 235069db28dcSHong Zhang rbuf_j = redund->rbuf_j; 235169db28dcSHong Zhang rbuf_a = redund->rbuf_a; 235269db28dcSHong Zhang } 235369db28dcSHong Zhang 235469db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 235569db28dcSHong Zhang PetscMPIInt subrank,subsize; 235669db28dcSHong Zhang PetscInt nleftover,np_subcomm; 235769db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 235869db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 235969db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 23601d79065fSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank); 236169db28dcSHong Zhang np_subcomm = size/nsubcomm; 236269db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 236369db28dcSHong Zhang nsends = 0; nrecvs = 0; 236469db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 236569db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 236669db28dcSHong Zhang send_rank[nsends] = i; nsends++; 236769db28dcSHong Zhang recv_rank[nrecvs++] = i; 236869db28dcSHong Zhang } 236969db28dcSHong Zhang } 237069db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 237169db28dcSHong Zhang i = size-nleftover-1; 237269db28dcSHong Zhang j = 0; 237369db28dcSHong Zhang while (j < nsubcomm - nleftover){ 237469db28dcSHong Zhang send_rank[nsends++] = i; 237569db28dcSHong Zhang i--; j++; 237669db28dcSHong Zhang } 237769db28dcSHong Zhang } 237869db28dcSHong Zhang 237969db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 238069db28dcSHong Zhang for (i=0; i<nleftover; i++){ 238169db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 238269db28dcSHong Zhang } 238369db28dcSHong Zhang } 238469db28dcSHong Zhang 238569db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 238669db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 238769db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 238869db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 238969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 239069db28dcSHong Zhang 239169db28dcSHong Zhang /* copy mat's local entries into the buffers */ 239269db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 239369db28dcSHong Zhang rownz_max = 0; 239469db28dcSHong Zhang rptr = sbuf_j; 239569db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 239669db28dcSHong Zhang vals = sbuf_a; 239769db28dcSHong Zhang rptr[0] = 0; 239869db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 239969db28dcSHong Zhang row = i + rstart; 240069db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 240169db28dcSHong Zhang ncols = nzA + nzB; 240269db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 240369db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 240469db28dcSHong Zhang /* load the column indices for this row into cols */ 240569db28dcSHong Zhang lwrite = 0; 240669db28dcSHong Zhang for (l=0; l<nzB; l++) { 240769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 240869db28dcSHong Zhang vals[lwrite] = aworkB[l]; 240969db28dcSHong Zhang cols[lwrite++] = ctmp; 241069db28dcSHong Zhang } 241169db28dcSHong Zhang } 241269db28dcSHong Zhang for (l=0; l<nzA; l++){ 241369db28dcSHong Zhang vals[lwrite] = aworkA[l]; 241469db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 241569db28dcSHong Zhang } 241669db28dcSHong Zhang for (l=0; l<nzB; l++) { 241769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 241869db28dcSHong Zhang vals[lwrite] = aworkB[l]; 241969db28dcSHong Zhang cols[lwrite++] = ctmp; 242069db28dcSHong Zhang } 242169db28dcSHong Zhang } 242269db28dcSHong Zhang vals += ncols; 242369db28dcSHong Zhang cols += ncols; 242469db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 242569db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 242669db28dcSHong Zhang } 2427e32f2f54SBarry 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); 242869db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 242969db28dcSHong Zhang rptr = sbuf_j; 243069db28dcSHong Zhang vals = sbuf_a; 243169db28dcSHong Zhang rptr[0] = 0; 243269db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 243369db28dcSHong Zhang row = i + rstart; 243469db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 243569db28dcSHong Zhang ncols = nzA + nzB; 243669db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 243769db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 243869db28dcSHong Zhang lwrite = 0; 243969db28dcSHong Zhang for (l=0; l<nzB; l++) { 244069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 244169db28dcSHong Zhang } 244269db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 244369db28dcSHong Zhang for (l=0; l<nzB; l++) { 244469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 244569db28dcSHong Zhang } 244669db28dcSHong Zhang vals += ncols; 244769db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 244869db28dcSHong Zhang } 244969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 245069db28dcSHong Zhang 245169db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 245269db28dcSHong Zhang /*--------------------------------------------------*/ 245369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 245469db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 245569db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 245669db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 245769db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 245869db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 245969db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 246069db28dcSHong Zhang } else { 246169db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 246269db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 246369db28dcSHong Zhang } 246469db28dcSHong Zhang 246569db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 246669db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 246769db28dcSHong Zhang /* get new tags to keep the communication clean */ 246869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 246969db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 24701d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 247169db28dcSHong Zhang 247269db28dcSHong Zhang /* post receives of other's nzlocal */ 247369db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 247469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 247569db28dcSHong Zhang } 247669db28dcSHong Zhang /* send nzlocal to others */ 247769db28dcSHong Zhang for (i=0; i<nsends; i++){ 247869db28dcSHong Zhang sbuf_nz[i] = nzlocal; 247969db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 248069db28dcSHong Zhang } 248169db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 248269db28dcSHong Zhang count = nrecvs; 248369db28dcSHong Zhang while (count) { 248469db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 248569db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 248669db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 248769db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 248869db28dcSHong Zhang 248969db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 249069db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 249169db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 249269db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 249369db28dcSHong Zhang count--; 249469db28dcSHong Zhang } 249569db28dcSHong Zhang /* wait on sends of nzlocal */ 249669db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 249769db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 249869db28dcSHong Zhang /*------------------------------------------------*/ 249969db28dcSHong Zhang for (i=0; i<nsends; i++){ 250069db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 250169db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 250269db28dcSHong Zhang } 250369db28dcSHong Zhang /* wait on receives of mat->i,j */ 250469db28dcSHong Zhang /*------------------------------*/ 250569db28dcSHong Zhang count = nrecvs; 250669db28dcSHong Zhang while (count) { 250769db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2508e32f2f54SBarry 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); 250969db28dcSHong Zhang count--; 251069db28dcSHong Zhang } 251169db28dcSHong Zhang /* wait on sends of mat->i,j */ 251269db28dcSHong Zhang /*---------------------------*/ 251369db28dcSHong Zhang if (nsends) { 251469db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 251569db28dcSHong Zhang } 251669db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 251769db28dcSHong Zhang 251869db28dcSHong Zhang /* post receives, send and receive mat->a */ 251969db28dcSHong Zhang /*----------------------------------------*/ 252069db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 252169db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 252269db28dcSHong Zhang } 252369db28dcSHong Zhang for (i=0; i<nsends; i++){ 252469db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 252569db28dcSHong Zhang } 252669db28dcSHong Zhang count = nrecvs; 252769db28dcSHong Zhang while (count) { 252869db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2529e32f2f54SBarry 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); 253069db28dcSHong Zhang count--; 253169db28dcSHong Zhang } 253269db28dcSHong Zhang if (nsends) { 253369db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 253469db28dcSHong Zhang } 253569db28dcSHong Zhang 253669db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 253769db28dcSHong Zhang 253869db28dcSHong Zhang /* create redundant matrix */ 253969db28dcSHong Zhang /*-------------------------*/ 254069db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 254169db28dcSHong Zhang /* compute rownz_max for preallocation */ 254269db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 254369db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 254469db28dcSHong Zhang rptr = rbuf_j[imdex]; 254569db28dcSHong Zhang for (i=0; i<j; i++){ 254669db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 254769db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 254869db28dcSHong Zhang } 254969db28dcSHong Zhang } 255069db28dcSHong Zhang 255169db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 255269db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 255369db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 255469db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 255569db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 255669db28dcSHong Zhang } else { 255769db28dcSHong Zhang C = *matredundant; 255869db28dcSHong Zhang } 255969db28dcSHong Zhang 256069db28dcSHong Zhang /* insert local matrix entries */ 256169db28dcSHong Zhang rptr = sbuf_j; 256269db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 256369db28dcSHong Zhang vals = sbuf_a; 256469db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 256569db28dcSHong Zhang row = i + rstart; 256669db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 256769db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 256869db28dcSHong Zhang vals += ncols; 256969db28dcSHong Zhang cols += ncols; 257069db28dcSHong Zhang } 257169db28dcSHong Zhang /* insert received matrix entries */ 257269db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 257369db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 257469db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 257569db28dcSHong Zhang rptr = rbuf_j[imdex]; 257669db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 257769db28dcSHong Zhang vals = rbuf_a[imdex]; 257869db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 257969db28dcSHong Zhang row = i + rstart; 258069db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 258169db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 258269db28dcSHong Zhang vals += ncols; 258369db28dcSHong Zhang cols += ncols; 258469db28dcSHong Zhang } 258569db28dcSHong Zhang } 258669db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 258769db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 258869db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2589e32f2f54SBarry 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); 259069db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 259169db28dcSHong Zhang PetscContainer container; 259269db28dcSHong Zhang *matredundant = C; 259369db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 259438f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 259569db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 259669db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 259769db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 259869db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 259969db28dcSHong Zhang 260069db28dcSHong Zhang redund->nzlocal = nzlocal; 260169db28dcSHong Zhang redund->nsends = nsends; 260269db28dcSHong Zhang redund->nrecvs = nrecvs; 260369db28dcSHong Zhang redund->send_rank = send_rank; 26041d79065fSBarry Smith redund->recv_rank = recv_rank; 260569db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 26061d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 260769db28dcSHong Zhang redund->sbuf_j = sbuf_j; 260869db28dcSHong Zhang redund->sbuf_a = sbuf_a; 260969db28dcSHong Zhang redund->rbuf_j = rbuf_j; 261069db28dcSHong Zhang redund->rbuf_a = rbuf_a; 261169db28dcSHong Zhang 261269db28dcSHong Zhang redund->MatDestroy = C->ops->destroy; 261369db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 261469db28dcSHong Zhang } 261569db28dcSHong Zhang PetscFunctionReturn(0); 261669db28dcSHong Zhang } 261769db28dcSHong Zhang 261803bc72f1SMatthew Knepley #undef __FUNCT__ 2619c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2620c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2621c91732d9SHong Zhang { 2622c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2623c91732d9SHong Zhang PetscErrorCode ierr; 2624c91732d9SHong Zhang PetscInt i,*idxb = 0; 2625c91732d9SHong Zhang PetscScalar *va,*vb; 2626c91732d9SHong Zhang Vec vtmp; 2627c91732d9SHong Zhang 2628c91732d9SHong Zhang PetscFunctionBegin; 2629c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2630c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2631c91732d9SHong Zhang if (idx) { 2632192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2633d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2634c91732d9SHong Zhang } 2635c91732d9SHong Zhang } 2636c91732d9SHong Zhang 2637d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2638c91732d9SHong Zhang if (idx) { 2639d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2640c91732d9SHong Zhang } 2641c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2642c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2643c91732d9SHong Zhang 2644d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2645c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2646c91732d9SHong Zhang va[i] = vb[i]; 2647c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2648c91732d9SHong Zhang } 2649c91732d9SHong Zhang } 2650c91732d9SHong Zhang 2651c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2652c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2653c91732d9SHong Zhang if (idxb) { 2654c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 2655c91732d9SHong Zhang } 2656c91732d9SHong Zhang ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2657c91732d9SHong Zhang PetscFunctionReturn(0); 2658c91732d9SHong Zhang } 2659c91732d9SHong Zhang 2660c91732d9SHong Zhang #undef __FUNCT__ 2661c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2662c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2663c87e5d42SMatthew Knepley { 2664c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2665c87e5d42SMatthew Knepley PetscErrorCode ierr; 2666c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2667c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2668c87e5d42SMatthew Knepley Vec vtmp; 2669c87e5d42SMatthew Knepley 2670c87e5d42SMatthew Knepley PetscFunctionBegin; 2671c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2672c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2673c87e5d42SMatthew Knepley if (idx) { 2674c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2675c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2676c87e5d42SMatthew Knepley } 2677c87e5d42SMatthew Knepley } 2678c87e5d42SMatthew Knepley 2679c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2680c87e5d42SMatthew Knepley if (idx) { 2681c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2682c87e5d42SMatthew Knepley } 2683c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2684c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2685c87e5d42SMatthew Knepley 2686c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2687c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2688c87e5d42SMatthew Knepley va[i] = vb[i]; 2689c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2690c87e5d42SMatthew Knepley } 2691c87e5d42SMatthew Knepley } 2692c87e5d42SMatthew Knepley 2693c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2694c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2695c87e5d42SMatthew Knepley if (idxb) { 2696c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 2697c87e5d42SMatthew Knepley } 2698c87e5d42SMatthew Knepley ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2699c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2700c87e5d42SMatthew Knepley } 2701c87e5d42SMatthew Knepley 2702c87e5d42SMatthew Knepley #undef __FUNCT__ 270303bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 270403bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 270503bc72f1SMatthew Knepley { 270603bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2707d0f46423SBarry Smith PetscInt n = A->rmap->n; 2708d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 270903bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 271003bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 271103bc72f1SMatthew Knepley Vec diagV, offdiagV; 271203bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 271303bc72f1SMatthew Knepley PetscInt r; 271403bc72f1SMatthew Knepley PetscErrorCode ierr; 271503bc72f1SMatthew Knepley 271603bc72f1SMatthew Knepley PetscFunctionBegin; 271703bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2718e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2719e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 272003bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 272103bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 272203bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 272303bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 272403bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 272503bc72f1SMatthew Knepley for(r = 0; r < n; ++r) { 2726028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 272703bc72f1SMatthew Knepley a[r] = diagA[r]; 272803bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 272903bc72f1SMatthew Knepley } else { 273003bc72f1SMatthew Knepley a[r] = offdiagA[r]; 273103bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 273203bc72f1SMatthew Knepley } 273303bc72f1SMatthew Knepley } 273403bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 273503bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 273603bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 273703bc72f1SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 273803bc72f1SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 273903bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 274003bc72f1SMatthew Knepley PetscFunctionReturn(0); 274103bc72f1SMatthew Knepley } 274203bc72f1SMatthew Knepley 27435494a064SHong Zhang #undef __FUNCT__ 2744c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2745c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2746c87e5d42SMatthew Knepley { 2747c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2748c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2749c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2750c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2751c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2752c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2753c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2754c87e5d42SMatthew Knepley PetscInt r; 2755c87e5d42SMatthew Knepley PetscErrorCode ierr; 2756c87e5d42SMatthew Knepley 2757c87e5d42SMatthew Knepley PetscFunctionBegin; 2758c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2759c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2760c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 2761c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2762c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2763c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2764c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2765c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2766c87e5d42SMatthew Knepley for(r = 0; r < n; ++r) { 2767c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2768c87e5d42SMatthew Knepley a[r] = diagA[r]; 2769c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2770c87e5d42SMatthew Knepley } else { 2771c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2772c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2773c87e5d42SMatthew Knepley } 2774c87e5d42SMatthew Knepley } 2775c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2776c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2777c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2778c87e5d42SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 2779c87e5d42SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 2780c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 2781c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2782c87e5d42SMatthew Knepley } 2783c87e5d42SMatthew Knepley 2784c87e5d42SMatthew Knepley #undef __FUNCT__ 2785829201f2SHong Zhang #define __FUNCT__ "MatGetSeqNonzerostructure_MPIAIJ" 2786f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIAIJ(Mat mat,Mat *newmat) 27875494a064SHong Zhang { 27885494a064SHong Zhang PetscErrorCode ierr; 2789f6d58c54SBarry Smith Mat *dummy; 27905494a064SHong Zhang 27915494a064SHong Zhang PetscFunctionBegin; 2792f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 2793f6d58c54SBarry Smith *newmat = *dummy; 2794f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 27955494a064SHong Zhang PetscFunctionReturn(0); 27965494a064SHong Zhang } 27975494a064SHong Zhang 27983acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 27998a729477SBarry Smith /* -------------------------------------------------------------------*/ 2800cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 2801cda55fadSBarry Smith MatGetRow_MPIAIJ, 2802cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 2803cda55fadSBarry Smith MatMult_MPIAIJ, 280497304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 28057c922b88SBarry Smith MatMultTranspose_MPIAIJ, 28067c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 2807103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2808103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 2809103bf8bdSMatthew Knepley #else 2810cda55fadSBarry Smith 0, 2811103bf8bdSMatthew Knepley #endif 2812cda55fadSBarry Smith 0, 2813cda55fadSBarry Smith 0, 281497304618SKris Buschelman /*10*/ 0, 2815cda55fadSBarry Smith 0, 2816cda55fadSBarry Smith 0, 281741f059aeSBarry Smith MatSOR_MPIAIJ, 2818b7c46309SBarry Smith MatTranspose_MPIAIJ, 281997304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 2820cda55fadSBarry Smith MatEqual_MPIAIJ, 2821cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 2822cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 2823cda55fadSBarry Smith MatNorm_MPIAIJ, 282497304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 2825cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 2826cda55fadSBarry Smith MatSetOption_MPIAIJ, 2827cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 2828d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 2829cda55fadSBarry Smith 0, 2830103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2831719d5645SBarry Smith 0, 2832103bf8bdSMatthew Knepley #else 2833cda55fadSBarry Smith 0, 2834103bf8bdSMatthew Knepley #endif 2835cda55fadSBarry Smith 0, 2836cda55fadSBarry Smith 0, 2837d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIAIJ, 2838103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2839719d5645SBarry Smith 0, 2840103bf8bdSMatthew Knepley #else 2841cda55fadSBarry Smith 0, 2842103bf8bdSMatthew Knepley #endif 2843cda55fadSBarry Smith 0, 2844cda55fadSBarry Smith 0, 2845cda55fadSBarry Smith 0, 2846d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 2847cda55fadSBarry Smith 0, 2848cda55fadSBarry Smith 0, 2849cda55fadSBarry Smith 0, 2850cda55fadSBarry Smith 0, 2851d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 2852cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 2853cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 2854cda55fadSBarry Smith MatGetValues_MPIAIJ, 2855cb5b572fSBarry Smith MatCopy_MPIAIJ, 2856d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 2857cda55fadSBarry Smith MatScale_MPIAIJ, 2858cda55fadSBarry Smith 0, 2859cda55fadSBarry Smith 0, 2860cda55fadSBarry Smith 0, 2861d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_MPIAIJ, 2862cda55fadSBarry Smith 0, 2863cda55fadSBarry Smith 0, 2864cda55fadSBarry Smith 0, 2865cda55fadSBarry Smith 0, 2866d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 2867cda55fadSBarry Smith 0, 2868cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 286942e855d1Svictor MatPermute_MPIAIJ, 2870cda55fadSBarry Smith 0, 2871d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 2872e03a110bSBarry Smith MatDestroy_MPIAIJ, 2873e03a110bSBarry Smith MatView_MPIAIJ, 2874357abbc8SBarry Smith 0, 2875a2243be0SBarry Smith 0, 2876d519adbfSMatthew Knepley /*64*/ 0, 2877a2243be0SBarry Smith 0, 2878a2243be0SBarry Smith 0, 2879a2243be0SBarry Smith 0, 2880a2243be0SBarry Smith 0, 2881d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 2882c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 2883a2243be0SBarry Smith 0, 2884a2243be0SBarry Smith MatSetColoring_MPIAIJ, 2885dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2886779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 2887dcf5cc72SBarry Smith #else 2888dcf5cc72SBarry Smith 0, 2889dcf5cc72SBarry Smith #endif 289097304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 28913acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 289297304618SKris Buschelman 0, 289397304618SKris Buschelman 0, 289497304618SKris Buschelman 0, 289597304618SKris Buschelman 0, 289697304618SKris Buschelman /*80*/ 0, 289797304618SKris Buschelman 0, 289897304618SKris Buschelman 0, 28995bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 29006284ec50SHong Zhang 0, 29016284ec50SHong Zhang 0, 29026284ec50SHong Zhang 0, 29036284ec50SHong Zhang 0, 2904865e5f61SKris Buschelman 0, 2905d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 290626be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 290726be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 29087a7894deSKris Buschelman MatPtAP_Basic, 29097a7894deSKris Buschelman MatPtAPSymbolic_MPIAIJ, 2910d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ, 29117a7894deSKris Buschelman 0, 29127a7894deSKris Buschelman 0, 29137a7894deSKris Buschelman 0, 29147a7894deSKris Buschelman 0, 2915d519adbfSMatthew Knepley /*99*/ 0, 2916865e5f61SKris Buschelman MatPtAPSymbolic_MPIAIJ_MPIAIJ, 29177a7894deSKris Buschelman MatPtAPNumeric_MPIAIJ_MPIAIJ, 29182fd7e33dSBarry Smith MatConjugate_MPIAIJ, 29192fd7e33dSBarry Smith 0, 2920d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 292199cafbc1SBarry Smith MatRealPart_MPIAIJ, 292269db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 292369db28dcSHong Zhang 0, 292469db28dcSHong Zhang 0, 2925d519adbfSMatthew Knepley /*109*/0, 292603bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 29275494a064SHong Zhang MatGetRowMin_MPIAIJ, 29285494a064SHong Zhang 0, 29295494a064SHong Zhang 0, 2930bd0c2dcbSBarry Smith /*114*/MatGetSeqNonzerostructure_MPIAIJ, 2931bd0c2dcbSBarry Smith 0, 2932bd0c2dcbSBarry Smith 0, 2933bd0c2dcbSBarry Smith 0, 2934bd0c2dcbSBarry Smith 0, 29358fb81238SShri Abhyankar /*119*/0, 29368fb81238SShri Abhyankar 0, 29378fb81238SShri Abhyankar 0, 2938d6037b41SHong Zhang 0, 2939d6037b41SHong Zhang MatGetMultiProcBlock_MPIAIJ 2940bd0c2dcbSBarry Smith }; 294136ce4990SBarry Smith 29422e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 29432e8a6d31SBarry Smith 2944fb2e594dSBarry Smith EXTERN_C_BEGIN 29454a2ae208SSatish Balay #undef __FUNCT__ 29464a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 2947be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat) 29482e8a6d31SBarry Smith { 29492e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2950dfbe8321SBarry Smith PetscErrorCode ierr; 29512e8a6d31SBarry Smith 29522e8a6d31SBarry Smith PetscFunctionBegin; 29532e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 29542e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 29552e8a6d31SBarry Smith PetscFunctionReturn(0); 29562e8a6d31SBarry Smith } 2957fb2e594dSBarry Smith EXTERN_C_END 29582e8a6d31SBarry Smith 2959fb2e594dSBarry Smith EXTERN_C_BEGIN 29604a2ae208SSatish Balay #undef __FUNCT__ 29614a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 2962be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat) 29632e8a6d31SBarry Smith { 29642e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2965dfbe8321SBarry Smith PetscErrorCode ierr; 29662e8a6d31SBarry Smith 29672e8a6d31SBarry Smith PetscFunctionBegin; 29682e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 29692e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 29702e8a6d31SBarry Smith PetscFunctionReturn(0); 29712e8a6d31SBarry Smith } 2972fb2e594dSBarry Smith EXTERN_C_END 29738a729477SBarry Smith 297427508adbSBarry Smith EXTERN_C_BEGIN 29754a2ae208SSatish Balay #undef __FUNCT__ 2976a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 2977be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 2978a23d5eceSKris Buschelman { 2979a23d5eceSKris Buschelman Mat_MPIAIJ *b; 2980dfbe8321SBarry Smith PetscErrorCode ierr; 2981b1d57f15SBarry Smith PetscInt i; 2982a23d5eceSKris Buschelman 2983a23d5eceSKris Buschelman PetscFunctionBegin; 2984a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 2985a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 2986e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 2987e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 2988899cda47SBarry Smith 298926283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 299026283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 299126283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 299226283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 2993a23d5eceSKris Buschelman if (d_nnz) { 2994d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 2995e32f2f54SBarry 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]); 2996a23d5eceSKris Buschelman } 2997a23d5eceSKris Buschelman } 2998a23d5eceSKris Buschelman if (o_nnz) { 2999d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3000e32f2f54SBarry 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]); 3001a23d5eceSKris Buschelman } 3002a23d5eceSKris Buschelman } 3003a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3004899cda47SBarry Smith 3005526dfc15SBarry Smith if (!B->preallocated) { 3006899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3007899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3008d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3009899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 3010899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3011899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3012d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3013899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 3014899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 3015526dfc15SBarry Smith } 3016899cda47SBarry Smith 3017c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3018c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 3019526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3020a23d5eceSKris Buschelman PetscFunctionReturn(0); 3021a23d5eceSKris Buschelman } 3022a23d5eceSKris Buschelman EXTERN_C_END 3023a23d5eceSKris Buschelman 30244a2ae208SSatish Balay #undef __FUNCT__ 30254a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3026dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3027d6dfbf8fSBarry Smith { 3028d6dfbf8fSBarry Smith Mat mat; 3029416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3030dfbe8321SBarry Smith PetscErrorCode ierr; 3031d6dfbf8fSBarry Smith 30323a40ed3dSBarry Smith PetscFunctionBegin; 3033416022c9SBarry Smith *newmat = 0; 30347adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 3035d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 30367adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 30371d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3038273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3039e1b6402fSHong Zhang 3040d5f3da31SBarry Smith mat->factortype = matin->factortype; 3041d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3042c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3043e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3044273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3045d6dfbf8fSBarry Smith 304617699dbbSLois Curfman McInnes a->size = oldmat->size; 304717699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3048e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3049e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3050e7641de0SSatish Balay a->rowindices = 0; 3051bcd2baecSBarry Smith a->rowvalues = 0; 3052bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3053d6dfbf8fSBarry Smith 305426283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 305526283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3056899cda47SBarry Smith 30572ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3058aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 30590f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3060b1fc9764SSatish Balay #else 3061d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 3062d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3063d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3064b1fc9764SSatish Balay #endif 3065416022c9SBarry Smith } else a->colmap = 0; 30663f41c07dSBarry Smith if (oldmat->garray) { 3067b1d57f15SBarry Smith PetscInt len; 3068d0f46423SBarry Smith len = oldmat->B->cmap->n; 3069b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 307052e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3071b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3072416022c9SBarry Smith } else a->garray = 0; 3073d6dfbf8fSBarry Smith 3074416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 307552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 3076a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 307752e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 30782e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 307952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 30802e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 308152e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 30827adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 30838a729477SBarry Smith *newmat = mat; 30843a40ed3dSBarry Smith PetscFunctionReturn(0); 30858a729477SBarry Smith } 3086416022c9SBarry Smith 30871a4ee126SBarry Smith /* 30881a4ee126SBarry Smith Allows sending/receiving larger messages then 2 gigabytes in a single call 30891a4ee126SBarry Smith */ 30901a4ee126SBarry Smith static int MPILong_Send(void *mess,PetscInt cnt, MPI_Datatype type,int to, int tag, MPI_Comm comm) 30911a4ee126SBarry Smith { 30921a4ee126SBarry Smith int ierr; 30931a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 30941a4ee126SBarry Smith PetscInt i,numchunks; 30951a4ee126SBarry Smith PetscMPIInt icnt; 30961a4ee126SBarry Smith 30971a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 30981a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 30991a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31001a4ee126SBarry Smith ierr = MPI_Send(mess,icnt,type,to,tag,comm); 31011a4ee126SBarry Smith if (type == MPIU_INT) { 31021a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31031a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31041a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31051a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31061a4ee126SBarry Smith } 31071a4ee126SBarry Smith return 0; 31081a4ee126SBarry Smith } 31091a4ee126SBarry Smith static int MPILong_Recv(void *mess,PetscInt cnt, MPI_Datatype type,int from, int tag, MPI_Comm comm) 31101a4ee126SBarry Smith { 31111a4ee126SBarry Smith int ierr; 31121a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 31131a4ee126SBarry Smith MPI_Status status; 31141a4ee126SBarry Smith PetscInt i,numchunks; 31151a4ee126SBarry Smith PetscMPIInt icnt; 31161a4ee126SBarry Smith 31171a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 31181a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 31191a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31201a4ee126SBarry Smith ierr = MPI_Recv(mess,icnt,type,from,tag,comm,&status); 31211a4ee126SBarry Smith if (type == MPIU_INT) { 31221a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31231a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31241a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31251a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31261a4ee126SBarry Smith } 31271a4ee126SBarry Smith return 0; 31281a4ee126SBarry Smith } 31291a4ee126SBarry Smith 31304a2ae208SSatish Balay #undef __FUNCT__ 31315bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3132112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 31338fb81238SShri Abhyankar { 31348fb81238SShri Abhyankar PetscScalar *vals,*svals; 31358fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 31368fb81238SShri Abhyankar PetscErrorCode ierr; 31371a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 31388fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 31398fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 31408fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 31418fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 31428fb81238SShri Abhyankar int fd; 31438fb81238SShri Abhyankar 31448fb81238SShri Abhyankar PetscFunctionBegin; 31458fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 31468fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 31478fb81238SShri Abhyankar if (!rank) { 31488fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 31498fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 31508fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 31518fb81238SShri Abhyankar } 31528fb81238SShri Abhyankar 31538fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 31548fb81238SShri Abhyankar 31558fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 31568fb81238SShri Abhyankar M = header[1]; N = header[2]; 31578fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 31588fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 31598fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 31608fb81238SShri Abhyankar 31618fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 31628fb81238SShri Abhyankar if (sizesset) { 31638fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 31648fb81238SShri Abhyankar } 3165abd38a8fSBarry 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); 3166abd38a8fSBarry 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); 31678fb81238SShri Abhyankar 31688fb81238SShri Abhyankar /* determine ownership of all rows */ 31698fb81238SShri Abhyankar if (newMat->rmap->n < 0 ) m = M/size + ((M % size) > rank); /* PETSC_DECIDE */ 31704683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 31718fb81238SShri Abhyankar 31728fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 31738fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 31748fb81238SShri Abhyankar 31758fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 31768fb81238SShri Abhyankar if (!rank) { 31778fb81238SShri Abhyankar mmax = rowners[1]; 31788fb81238SShri Abhyankar for (i=2; i<size; i++) { 31798fb81238SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 31808fb81238SShri Abhyankar } 31818fb81238SShri Abhyankar } else mmax = m; 31828fb81238SShri Abhyankar 31838fb81238SShri Abhyankar rowners[0] = 0; 31848fb81238SShri Abhyankar for (i=2; i<=size; i++) { 31858fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 31868fb81238SShri Abhyankar } 31878fb81238SShri Abhyankar rstart = rowners[rank]; 31888fb81238SShri Abhyankar rend = rowners[rank+1]; 31898fb81238SShri Abhyankar 31908fb81238SShri Abhyankar /* distribute row lengths to all processors */ 31918fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 31928fb81238SShri Abhyankar if (!rank) { 31938fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 31948fb81238SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 31958fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 31968fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 31978fb81238SShri Abhyankar for (j=0; j<m; j++) { 31988fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 31998fb81238SShri Abhyankar } 32008fb81238SShri Abhyankar for (i=1; i<size; i++) { 32018fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 32028fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 32038fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 32048fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 32058fb81238SShri Abhyankar } 32061a4ee126SBarry Smith ierr = MPILong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32078fb81238SShri Abhyankar } 32088fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 32098fb81238SShri Abhyankar } else { 32101a4ee126SBarry Smith ierr = MPILong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32118fb81238SShri Abhyankar } 32128fb81238SShri Abhyankar 32138fb81238SShri Abhyankar if (!rank) { 32148fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 32158fb81238SShri Abhyankar maxnz = 0; 32168fb81238SShri Abhyankar for (i=0; i<size; i++) { 32178fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 32188fb81238SShri Abhyankar } 32198fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 32208fb81238SShri Abhyankar 32218fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 32228fb81238SShri Abhyankar nz = procsnz[0]; 32238fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32248fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 32258fb81238SShri Abhyankar 32268fb81238SShri Abhyankar /* read in every one elses and ship off */ 32278fb81238SShri Abhyankar for (i=1; i<size; i++) { 32288fb81238SShri Abhyankar nz = procsnz[i]; 32298fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 32301a4ee126SBarry Smith ierr = MPILong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32318fb81238SShri Abhyankar } 32328fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 32338fb81238SShri Abhyankar } else { 32348fb81238SShri Abhyankar /* determine buffer space needed for message */ 32358fb81238SShri Abhyankar nz = 0; 32368fb81238SShri Abhyankar for (i=0; i<m; i++) { 32378fb81238SShri Abhyankar nz += ourlens[i]; 32388fb81238SShri Abhyankar } 32398fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32408fb81238SShri Abhyankar 32418fb81238SShri Abhyankar /* receive message of column indices*/ 32421a4ee126SBarry Smith ierr = MPILong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32438fb81238SShri Abhyankar } 32448fb81238SShri Abhyankar 32458fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 32468fb81238SShri Abhyankar if (N != M) { 32478fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 32488fb81238SShri Abhyankar else n = newMat->cmap->n; 32498fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 32508fb81238SShri Abhyankar cstart = cend - n; 32518fb81238SShri Abhyankar } else { 32528fb81238SShri Abhyankar cstart = rstart; 32538fb81238SShri Abhyankar cend = rend; 32548fb81238SShri Abhyankar n = cend - cstart; 32558fb81238SShri Abhyankar } 32568fb81238SShri Abhyankar 32578fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 32588fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 32598fb81238SShri Abhyankar jj = 0; 32608fb81238SShri Abhyankar for (i=0; i<m; i++) { 32618fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 32628fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 32638fb81238SShri Abhyankar jj++; 32648fb81238SShri Abhyankar } 32658fb81238SShri Abhyankar } 32668fb81238SShri Abhyankar 32678fb81238SShri Abhyankar for (i=0; i<m; i++) { 32688fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 32698fb81238SShri Abhyankar } 32708fb81238SShri Abhyankar if (!sizesset) { 32718fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 32728fb81238SShri Abhyankar } 32738fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 32748fb81238SShri Abhyankar 32758fb81238SShri Abhyankar for (i=0; i<m; i++) { 32768fb81238SShri Abhyankar ourlens[i] += offlens[i]; 32778fb81238SShri Abhyankar } 32788fb81238SShri Abhyankar 32798fb81238SShri Abhyankar if (!rank) { 32808fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 32818fb81238SShri Abhyankar 32828fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 32838fb81238SShri Abhyankar nz = procsnz[0]; 32848fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 32858fb81238SShri Abhyankar 32868fb81238SShri Abhyankar /* insert into matrix */ 32878fb81238SShri Abhyankar jj = rstart; 32888fb81238SShri Abhyankar smycols = mycols; 32898fb81238SShri Abhyankar svals = vals; 32908fb81238SShri Abhyankar for (i=0; i<m; i++) { 32918fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 32928fb81238SShri Abhyankar smycols += ourlens[i]; 32938fb81238SShri Abhyankar svals += ourlens[i]; 32948fb81238SShri Abhyankar jj++; 32958fb81238SShri Abhyankar } 32968fb81238SShri Abhyankar 32978fb81238SShri Abhyankar /* read in other processors and ship out */ 32988fb81238SShri Abhyankar for (i=1; i<size; i++) { 32998fb81238SShri Abhyankar nz = procsnz[i]; 33008fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 33011a4ee126SBarry Smith ierr = MPILong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33028fb81238SShri Abhyankar } 33038fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 33048fb81238SShri Abhyankar } else { 33058fb81238SShri Abhyankar /* receive numeric values */ 33068fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 33078fb81238SShri Abhyankar 33088fb81238SShri Abhyankar /* receive message of values*/ 33091a4ee126SBarry Smith ierr = MPILong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33108fb81238SShri Abhyankar 33118fb81238SShri Abhyankar /* insert into matrix */ 33128fb81238SShri Abhyankar jj = rstart; 33138fb81238SShri Abhyankar smycols = mycols; 33148fb81238SShri Abhyankar svals = vals; 33158fb81238SShri Abhyankar for (i=0; i<m; i++) { 33168fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 33178fb81238SShri Abhyankar smycols += ourlens[i]; 33188fb81238SShri Abhyankar svals += ourlens[i]; 33198fb81238SShri Abhyankar jj++; 33208fb81238SShri Abhyankar } 33218fb81238SShri Abhyankar } 33228fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 33238fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 33248fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 33258fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 33268fb81238SShri Abhyankar 33278fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33288fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33298fb81238SShri Abhyankar PetscFunctionReturn(0); 33308fb81238SShri Abhyankar } 33318fb81238SShri Abhyankar 33328fb81238SShri Abhyankar #undef __FUNCT__ 33334a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 33344aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 33354aa3045dSJed Brown { 33364aa3045dSJed Brown PetscErrorCode ierr; 33374aa3045dSJed Brown IS iscol_local; 33384aa3045dSJed Brown PetscInt csize; 33394aa3045dSJed Brown 33404aa3045dSJed Brown PetscFunctionBegin; 33414aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3342b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3343b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3344e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3345b79d0421SJed Brown } else { 33464aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3347b79d0421SJed Brown } 33484aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3349b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3350b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 33514aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 3352b79d0421SJed Brown } 33534aa3045dSJed Brown PetscFunctionReturn(0); 33544aa3045dSJed Brown } 33554aa3045dSJed Brown 33564aa3045dSJed Brown #undef __FUNCT__ 33574aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3358a0ff6018SBarry Smith /* 335929da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 336029da9460SBarry Smith in local and then by concatenating the local matrices the end result. 336129da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 33624aa3045dSJed Brown 33634aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3364a0ff6018SBarry Smith */ 33654aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3366a0ff6018SBarry Smith { 3367dfbe8321SBarry Smith PetscErrorCode ierr; 336832dcc486SBarry Smith PetscMPIInt rank,size; 3369b1d57f15SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j; 3370b1d57f15SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 3371fee21e36SBarry Smith Mat *local,M,Mreuse; 3372a77337e4SBarry Smith MatScalar *vwork,*aa; 33737adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 337400e6dbe6SBarry Smith Mat_SeqAIJ *aij; 33757e2c5f70SBarry Smith 3376a0ff6018SBarry Smith 3377a0ff6018SBarry Smith PetscFunctionBegin; 33781dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 33791dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 338000e6dbe6SBarry Smith 3381fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3382fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3383e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3384fee21e36SBarry Smith local = &Mreuse; 3385fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 3386fee21e36SBarry Smith } else { 3387a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3388fee21e36SBarry Smith Mreuse = *local; 3389606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 3390fee21e36SBarry Smith } 3391a0ff6018SBarry Smith 3392a0ff6018SBarry Smith /* 3393a0ff6018SBarry Smith m - number of local rows 3394a0ff6018SBarry Smith n - number of columns (same on all processors) 3395a0ff6018SBarry Smith rstart - first row in new global matrix generated 3396a0ff6018SBarry Smith */ 3397fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3398a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3399fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 340000e6dbe6SBarry Smith ii = aij->i; 340100e6dbe6SBarry Smith jj = aij->j; 340200e6dbe6SBarry Smith 3403a0ff6018SBarry Smith /* 340400e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 340500e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3406a0ff6018SBarry Smith */ 340700e6dbe6SBarry Smith 340800e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 34096a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3410ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3411ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3412e2c4fddaSBarry Smith nlocal = m; 34136a6a5d1dSBarry Smith } else { 3414ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3415ab50ec6bSBarry Smith } 3416ab50ec6bSBarry Smith } else { 34176a6a5d1dSBarry Smith nlocal = csize; 34186a6a5d1dSBarry Smith } 3419b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 342000e6dbe6SBarry Smith rstart = rend - nlocal; 342165e19b50SBarry 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); 342200e6dbe6SBarry Smith 342300e6dbe6SBarry Smith /* next, compute all the lengths */ 3424b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 342500e6dbe6SBarry Smith olens = dlens + m; 342600e6dbe6SBarry Smith for (i=0; i<m; i++) { 342700e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 342800e6dbe6SBarry Smith olen = 0; 342900e6dbe6SBarry Smith dlen = 0; 343000e6dbe6SBarry Smith for (j=0; j<jend; j++) { 343100e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 343200e6dbe6SBarry Smith else dlen++; 343300e6dbe6SBarry Smith jj++; 343400e6dbe6SBarry Smith } 343500e6dbe6SBarry Smith olens[i] = olen; 343600e6dbe6SBarry Smith dlens[i] = dlen; 343700e6dbe6SBarry Smith } 3438f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3439f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 34407adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3441e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3442606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3443a0ff6018SBarry Smith } else { 3444b1d57f15SBarry Smith PetscInt ml,nl; 3445a0ff6018SBarry Smith 3446a0ff6018SBarry Smith M = *newmat; 3447a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3448e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3449a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3450c48de900SBarry Smith /* 3451c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3452c48de900SBarry Smith rather than the slower MatSetValues(). 3453c48de900SBarry Smith */ 3454c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3455c48de900SBarry Smith M->assembled = PETSC_FALSE; 3456a0ff6018SBarry Smith } 3457a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3458fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 345900e6dbe6SBarry Smith ii = aij->i; 346000e6dbe6SBarry Smith jj = aij->j; 346100e6dbe6SBarry Smith aa = aij->a; 3462a0ff6018SBarry Smith for (i=0; i<m; i++) { 3463a0ff6018SBarry Smith row = rstart + i; 346400e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 346500e6dbe6SBarry Smith cwork = jj; jj += nz; 346600e6dbe6SBarry Smith vwork = aa; aa += nz; 34678c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3468a0ff6018SBarry Smith } 3469a0ff6018SBarry Smith 3470a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3471a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3472a0ff6018SBarry Smith *newmat = M; 3473fee21e36SBarry Smith 3474fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3475fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3476fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3477fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 3478fee21e36SBarry Smith } 3479fee21e36SBarry Smith 3480a0ff6018SBarry Smith PetscFunctionReturn(0); 3481a0ff6018SBarry Smith } 3482273d9f13SBarry Smith 3483e2e86b8fSSatish Balay EXTERN_C_BEGIN 34844a2ae208SSatish Balay #undef __FUNCT__ 3485ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 3486b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3487ccd8e176SBarry Smith { 3488899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3489899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3490ccd8e176SBarry Smith const PetscInt *JJ; 3491ccd8e176SBarry Smith PetscScalar *values; 3492ccd8e176SBarry Smith PetscErrorCode ierr; 3493ccd8e176SBarry Smith 3494ccd8e176SBarry Smith PetscFunctionBegin; 3495e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3496899cda47SBarry Smith 349726283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 349826283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 349926283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 350026283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3501d0f46423SBarry Smith m = B->rmap->n; 3502d0f46423SBarry Smith cstart = B->cmap->rstart; 3503d0f46423SBarry Smith cend = B->cmap->rend; 3504d0f46423SBarry Smith rstart = B->rmap->rstart; 3505899cda47SBarry Smith 35061d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3507ccd8e176SBarry Smith 3508ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3509ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3510ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3511ecc77c7aSBarry Smith JJ = J + Ii[i]; 3512e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3513ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3514d0f46423SBarry 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); 3515ecc77c7aSBarry Smith } 3516ecc77c7aSBarry Smith #endif 3517ecc77c7aSBarry Smith 3518ccd8e176SBarry Smith for (i=0; i<m; i++) { 3519b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3520b7940d39SSatish Balay JJ = J + Ii[i]; 3521ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3522ccd8e176SBarry Smith d = 0; 35230daa03b5SJed Brown for (j=0; j<nnz; j++) { 35240daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3525ccd8e176SBarry Smith } 3526ccd8e176SBarry Smith d_nnz[i] = d; 3527ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3528ccd8e176SBarry Smith } 3529ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 35301d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3531ccd8e176SBarry Smith 3532ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3533ccd8e176SBarry Smith else { 3534ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3535ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3536ccd8e176SBarry Smith } 3537ccd8e176SBarry Smith 3538ccd8e176SBarry Smith for (i=0; i<m; i++) { 3539ccd8e176SBarry Smith ii = i + rstart; 3540b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3541b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3542ccd8e176SBarry Smith } 3543ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3544ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3545ccd8e176SBarry Smith 3546ccd8e176SBarry Smith if (!v) { 3547ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3548ccd8e176SBarry Smith } 3549ccd8e176SBarry Smith PetscFunctionReturn(0); 3550ccd8e176SBarry Smith } 3551e2e86b8fSSatish Balay EXTERN_C_END 3552ccd8e176SBarry Smith 3553ccd8e176SBarry Smith #undef __FUNCT__ 3554ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 35551eea217eSSatish Balay /*@ 3556ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3557ccd8e176SBarry Smith (the default parallel PETSc format). 3558ccd8e176SBarry Smith 3559ccd8e176SBarry Smith Collective on MPI_Comm 3560ccd8e176SBarry Smith 3561ccd8e176SBarry Smith Input Parameters: 3562a1661176SMatthew Knepley + B - the matrix 3563ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 35640daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3565ccd8e176SBarry Smith - v - optional values in the matrix 3566ccd8e176SBarry Smith 3567ccd8e176SBarry Smith Level: developer 3568ccd8e176SBarry Smith 356912251496SSatish Balay Notes: 357012251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 357112251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 357212251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 357312251496SSatish Balay 357412251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 357512251496SSatish Balay 357612251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 357712251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 357812251496SSatish Balay as shown: 357912251496SSatish Balay 358012251496SSatish Balay 1 0 0 358112251496SSatish Balay 2 0 3 P0 358212251496SSatish Balay ------- 358312251496SSatish Balay 4 5 6 P1 358412251496SSatish Balay 358512251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 358612251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 358712251496SSatish Balay j = {0,0,2} [size = nz = 6] 358812251496SSatish Balay v = {1,2,3} [size = nz = 6] 358912251496SSatish Balay 359012251496SSatish Balay Process1 [P1]: rows_owned=[2] 359112251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 359212251496SSatish Balay j = {0,1,2} [size = nz = 6] 359312251496SSatish Balay v = {4,5,6} [size = nz = 6] 359412251496SSatish Balay 3595ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3596ccd8e176SBarry Smith 35972fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ, 35988d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3599ccd8e176SBarry Smith @*/ 3600be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3601ccd8e176SBarry Smith { 36024ac538c5SBarry Smith PetscErrorCode ierr; 3603ccd8e176SBarry Smith 3604ccd8e176SBarry Smith PetscFunctionBegin; 36054ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3606ccd8e176SBarry Smith PetscFunctionReturn(0); 3607ccd8e176SBarry Smith } 3608ccd8e176SBarry Smith 3609ccd8e176SBarry Smith #undef __FUNCT__ 36104a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3611273d9f13SBarry Smith /*@C 3612ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3613273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3614273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3615273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3616273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3617273d9f13SBarry Smith 3618273d9f13SBarry Smith Collective on MPI_Comm 3619273d9f13SBarry Smith 3620273d9f13SBarry Smith Input Parameters: 3621273d9f13SBarry Smith + A - the matrix 3622273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3623273d9f13SBarry Smith (same value is used for all local rows) 3624273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3625273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3626273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3627273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3628273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3629273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3630273d9f13SBarry Smith submatrix (same value is used for all local rows). 3631273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3632273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3633273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3634273d9f13SBarry Smith structure. The size of this array is equal to the number 3635273d9f13SBarry Smith of local rows, i.e 'm'. 3636273d9f13SBarry Smith 363749a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 363849a6f317SBarry Smith 3639273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3640ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 36410598bfebSBarry Smith storage. The stored row and column indices begin with zero. 36420598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3643273d9f13SBarry Smith 3644273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3645273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3646273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3647273d9f13SBarry Smith 3648273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3649a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3650a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3651a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3652a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3653a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3654a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3655a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3656a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3657273d9f13SBarry Smith 3658273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3659273d9f13SBarry Smith 3660aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3661aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3662aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3663aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3664aa95bbe8SBarry Smith 3665273d9f13SBarry Smith Example usage: 3666273d9f13SBarry Smith 3667273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3668273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3669273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3670273d9f13SBarry Smith as follows: 3671273d9f13SBarry Smith 3672273d9f13SBarry Smith .vb 3673273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3674273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3675273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3676273d9f13SBarry Smith ------------------------------------- 3677273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3678273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3679273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3680273d9f13SBarry Smith ------------------------------------- 3681273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3682273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3683273d9f13SBarry Smith .ve 3684273d9f13SBarry Smith 3685273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3686273d9f13SBarry Smith 3687273d9f13SBarry Smith .vb 3688273d9f13SBarry Smith A B C 3689273d9f13SBarry Smith D E F 3690273d9f13SBarry Smith G H I 3691273d9f13SBarry Smith .ve 3692273d9f13SBarry Smith 3693273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3694273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3695273d9f13SBarry Smith 3696273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3697273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3698273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3699273d9f13SBarry Smith 3700273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3701273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3702273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3703273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3704273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3705273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3706273d9f13SBarry Smith 3707273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3708273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3709273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3710273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3711273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3712273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3713273d9f13SBarry Smith .vb 3714273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3715273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3716273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3717273d9f13SBarry Smith .ve 3718273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3719273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3720273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3721273d9f13SBarry Smith 34 values. 3722273d9f13SBarry Smith 3723273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3724273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3725273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3726273d9f13SBarry Smith .vb 3727273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3728273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3729273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3730273d9f13SBarry Smith .ve 3731273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3732273d9f13SBarry Smith hence pre-allocation is perfect. 3733273d9f13SBarry Smith 3734273d9f13SBarry Smith Level: intermediate 3735273d9f13SBarry Smith 3736273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3737273d9f13SBarry Smith 3738ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(), 3739aa95bbe8SBarry Smith MPIAIJ, MatGetInfo() 3740273d9f13SBarry Smith @*/ 3741be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3742273d9f13SBarry Smith { 37434ac538c5SBarry Smith PetscErrorCode ierr; 3744273d9f13SBarry Smith 3745273d9f13SBarry Smith PetscFunctionBegin; 37464ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 3747273d9f13SBarry Smith PetscFunctionReturn(0); 3748273d9f13SBarry Smith } 3749273d9f13SBarry Smith 37504a2ae208SSatish Balay #undef __FUNCT__ 37512fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 375258d36128SBarry Smith /*@ 37532fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 37542fb0ec9aSBarry Smith CSR format the local rows. 37552fb0ec9aSBarry Smith 37562fb0ec9aSBarry Smith Collective on MPI_Comm 37572fb0ec9aSBarry Smith 37582fb0ec9aSBarry Smith Input Parameters: 37592fb0ec9aSBarry Smith + comm - MPI communicator 37602fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 37612fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 37622fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 37632fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 37642fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 37652fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 37662fb0ec9aSBarry Smith . i - row indices 37672fb0ec9aSBarry Smith . j - column indices 37682fb0ec9aSBarry Smith - a - matrix values 37692fb0ec9aSBarry Smith 37702fb0ec9aSBarry Smith Output Parameter: 37712fb0ec9aSBarry Smith . mat - the matrix 377203bfb495SBarry Smith 37732fb0ec9aSBarry Smith Level: intermediate 37742fb0ec9aSBarry Smith 37752fb0ec9aSBarry Smith Notes: 37762fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 37772fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 37788d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 37792fb0ec9aSBarry Smith 378012251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 378112251496SSatish Balay 378212251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 378312251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 378412251496SSatish Balay as shown: 378512251496SSatish Balay 378612251496SSatish Balay 1 0 0 378712251496SSatish Balay 2 0 3 P0 378812251496SSatish Balay ------- 378912251496SSatish Balay 4 5 6 P1 379012251496SSatish Balay 379112251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 379212251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 379312251496SSatish Balay j = {0,0,2} [size = nz = 6] 379412251496SSatish Balay v = {1,2,3} [size = nz = 6] 379512251496SSatish Balay 379612251496SSatish Balay Process1 [P1]: rows_owned=[2] 379712251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 379812251496SSatish Balay j = {0,1,2} [size = nz = 6] 379912251496SSatish Balay v = {4,5,6} [size = nz = 6] 38002fb0ec9aSBarry Smith 38012fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 38022fb0ec9aSBarry Smith 38032fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 38048d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 38052fb0ec9aSBarry Smith @*/ 380682b90586SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat) 38072fb0ec9aSBarry Smith { 38082fb0ec9aSBarry Smith PetscErrorCode ierr; 38092fb0ec9aSBarry Smith 38102fb0ec9aSBarry Smith PetscFunctionBegin; 38112fb0ec9aSBarry Smith if (i[0]) { 3812e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 38132fb0ec9aSBarry Smith } 3814e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 38152fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3816d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 38172fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 38182fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 38192fb0ec9aSBarry Smith PetscFunctionReturn(0); 38202fb0ec9aSBarry Smith } 38212fb0ec9aSBarry Smith 38222fb0ec9aSBarry Smith #undef __FUNCT__ 38234a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 3824273d9f13SBarry Smith /*@C 3825273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 3826273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3827273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3828273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3829273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3830273d9f13SBarry Smith 3831273d9f13SBarry Smith Collective on MPI_Comm 3832273d9f13SBarry Smith 3833273d9f13SBarry Smith Input Parameters: 3834273d9f13SBarry Smith + comm - MPI communicator 3835273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 3836273d9f13SBarry Smith This value should be the same as the local size used in creating the 3837273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 3838273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 3839273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 3840273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 3841273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3842273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 3843273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3844273d9f13SBarry Smith (same value is used for all local rows) 3845273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3846273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3847273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3848273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3849273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3850273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3851273d9f13SBarry Smith submatrix (same value is used for all local rows). 3852273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3853273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3854273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3855273d9f13SBarry Smith structure. The size of this array is equal to the number 3856273d9f13SBarry Smith of local rows, i.e 'm'. 3857273d9f13SBarry Smith 3858273d9f13SBarry Smith Output Parameter: 3859273d9f13SBarry Smith . A - the matrix 3860273d9f13SBarry Smith 3861175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3862ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3863175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3864175b88e8SBarry Smith 3865273d9f13SBarry Smith Notes: 386649a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 386749a6f317SBarry Smith 3868273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 3869273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 3870273d9f13SBarry Smith storage requirements for this matrix. 3871273d9f13SBarry Smith 3872273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 3873273d9f13SBarry Smith processor than it must be used on all processors that share the object for 3874273d9f13SBarry Smith that argument. 3875273d9f13SBarry Smith 3876273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 3877273d9f13SBarry Smith (possibly both). 3878273d9f13SBarry Smith 387933a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 388033a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 388133a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 388233a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 388333a7c187SSatish Balay values corresponding to [m x N] submatrix. 3884273d9f13SBarry Smith 388533a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 388633a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 388733a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 388833a7c187SSatish Balay 388933a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 389033a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 389133a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 389233a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 389333a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 389433a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 389533a7c187SSatish Balay illustrates this concept. 389633a7c187SSatish Balay 389733a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 389833a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 389933a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 390033a7c187SSatish Balay local matrix (a rectangular submatrix). 3901273d9f13SBarry Smith 3902273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3903273d9f13SBarry Smith 390497d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 390597d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 390697d05335SKris Buschelman type of communicator, use the construction mechanism: 390778102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 390897d05335SKris Buschelman 3909273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 3910273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 3911273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3912273d9f13SBarry Smith 3913273d9f13SBarry Smith Options Database Keys: 3914923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 3915923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3916273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3917273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3918273d9f13SBarry Smith the user still MUST index entries starting at 0! 3919273d9f13SBarry Smith 3920273d9f13SBarry Smith 3921273d9f13SBarry Smith Example usage: 3922273d9f13SBarry Smith 3923273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3924273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3925273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3926273d9f13SBarry Smith as follows: 3927273d9f13SBarry Smith 3928273d9f13SBarry Smith .vb 3929273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3930273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3931273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3932273d9f13SBarry Smith ------------------------------------- 3933273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3934273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3935273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3936273d9f13SBarry Smith ------------------------------------- 3937273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3938273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3939273d9f13SBarry Smith .ve 3940273d9f13SBarry Smith 3941273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3942273d9f13SBarry Smith 3943273d9f13SBarry Smith .vb 3944273d9f13SBarry Smith A B C 3945273d9f13SBarry Smith D E F 3946273d9f13SBarry Smith G H I 3947273d9f13SBarry Smith .ve 3948273d9f13SBarry Smith 3949273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3950273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3951273d9f13SBarry Smith 3952273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3953273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3954273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3955273d9f13SBarry Smith 3956273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3957273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3958273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3959273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3960273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3961273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3962273d9f13SBarry Smith 3963273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3964273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3965273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3966273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3967273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3968273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3969273d9f13SBarry Smith .vb 3970273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3971273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3972273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3973273d9f13SBarry Smith .ve 3974273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3975273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3976273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3977273d9f13SBarry Smith 34 values. 3978273d9f13SBarry Smith 3979273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3980273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3981273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3982273d9f13SBarry Smith .vb 3983273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3984273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3985273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3986273d9f13SBarry Smith .ve 3987273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3988273d9f13SBarry Smith hence pre-allocation is perfect. 3989273d9f13SBarry Smith 3990273d9f13SBarry Smith Level: intermediate 3991273d9f13SBarry Smith 3992273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3993273d9f13SBarry Smith 3994ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 39952fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 3996273d9f13SBarry Smith @*/ 3997be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A) 3998273d9f13SBarry Smith { 39996849ba73SBarry Smith PetscErrorCode ierr; 4000b1d57f15SBarry Smith PetscMPIInt size; 4001273d9f13SBarry Smith 4002273d9f13SBarry Smith PetscFunctionBegin; 4003f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4004f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4005273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4006273d9f13SBarry Smith if (size > 1) { 4007273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4008273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4009273d9f13SBarry Smith } else { 4010273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4011273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4012273d9f13SBarry Smith } 4013273d9f13SBarry Smith PetscFunctionReturn(0); 4014273d9f13SBarry Smith } 4015195d93cdSBarry Smith 40164a2ae208SSatish Balay #undef __FUNCT__ 40174a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 4018be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 4019195d93cdSBarry Smith { 4020195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 4021b1d57f15SBarry Smith 4022195d93cdSBarry Smith PetscFunctionBegin; 4023195d93cdSBarry Smith *Ad = a->A; 4024195d93cdSBarry Smith *Ao = a->B; 4025195d93cdSBarry Smith *colmap = a->garray; 4026195d93cdSBarry Smith PetscFunctionReturn(0); 4027195d93cdSBarry Smith } 4028a2243be0SBarry Smith 4029a2243be0SBarry Smith #undef __FUNCT__ 4030a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4031dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4032a2243be0SBarry Smith { 4033dfbe8321SBarry Smith PetscErrorCode ierr; 4034b1d57f15SBarry Smith PetscInt i; 4035a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4036a2243be0SBarry Smith 4037a2243be0SBarry Smith PetscFunctionBegin; 40388ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 403908b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4040a2243be0SBarry Smith ISColoring ocoloring; 4041a2243be0SBarry Smith 4042a2243be0SBarry Smith /* set coloring for diagonal portion */ 4043a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4044a2243be0SBarry Smith 4045a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 40467adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 4047d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4048d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4049a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4050a2243be0SBarry Smith } 4051a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4052d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4053a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4054a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4055a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 405608b6dcc0SBarry Smith ISColoringValue *colors; 4057b1d57f15SBarry Smith PetscInt *larray; 4058a2243be0SBarry Smith ISColoring ocoloring; 4059a2243be0SBarry Smith 4060a2243be0SBarry Smith /* set coloring for diagonal portion */ 4061d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4062d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4063d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4064a2243be0SBarry Smith } 4065d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 4066d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4067d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4068a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4069a2243be0SBarry Smith } 4070a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4071d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4072a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 4073a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4074a2243be0SBarry Smith 4075a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4076d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4077d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 4078d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4079d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4080a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4081a2243be0SBarry Smith } 4082a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4083d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4084a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4085a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4086a2243be0SBarry Smith } else { 4087e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4088a2243be0SBarry Smith } 4089a2243be0SBarry Smith 4090a2243be0SBarry Smith PetscFunctionReturn(0); 4091a2243be0SBarry Smith } 4092a2243be0SBarry Smith 4093dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 4094a2243be0SBarry Smith #undef __FUNCT__ 4095779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 4096dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 4097a2243be0SBarry Smith { 4098a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4099dfbe8321SBarry Smith PetscErrorCode ierr; 4100a2243be0SBarry Smith 4101a2243be0SBarry Smith PetscFunctionBegin; 4102779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 4103779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 4104779c1a83SBarry Smith PetscFunctionReturn(0); 4105779c1a83SBarry Smith } 4106dcf5cc72SBarry Smith #endif 4107779c1a83SBarry Smith 4108779c1a83SBarry Smith #undef __FUNCT__ 4109779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4110b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4111779c1a83SBarry Smith { 4112779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4113dfbe8321SBarry Smith PetscErrorCode ierr; 4114779c1a83SBarry Smith 4115779c1a83SBarry Smith PetscFunctionBegin; 4116779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4117779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4118a2243be0SBarry Smith PetscFunctionReturn(0); 4119a2243be0SBarry Smith } 4120c5d6d63eSBarry Smith 4121c5d6d63eSBarry Smith #undef __FUNCT__ 412251dd7536SBarry Smith #define __FUNCT__ "MatMerge" 4123bc08b0f1SBarry Smith /*@ 412451dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 412551dd7536SBarry Smith matrices from each processor 4126c5d6d63eSBarry Smith 4127c5d6d63eSBarry Smith Collective on MPI_Comm 4128c5d6d63eSBarry Smith 4129c5d6d63eSBarry Smith Input Parameters: 413051dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4131d6bb3c2dSHong Zhang . inmat - the input sequential matrices 41320e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4133d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 413451dd7536SBarry Smith 413551dd7536SBarry Smith Output Parameter: 413651dd7536SBarry Smith . outmat - the parallel matrix generated 4137c5d6d63eSBarry Smith 41387e25d530SSatish Balay Level: advanced 41397e25d530SSatish Balay 4140f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4141c5d6d63eSBarry Smith 4142c5d6d63eSBarry Smith @*/ 4143be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4144c5d6d63eSBarry Smith { 4145dfbe8321SBarry Smith PetscErrorCode ierr; 4146b7940d39SSatish Balay PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz; 4147ba8c8a56SBarry Smith PetscInt *indx; 4148ba8c8a56SBarry Smith PetscScalar *values; 4149c5d6d63eSBarry Smith 4150c5d6d63eSBarry Smith PetscFunctionBegin; 41510e36024fSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4152d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4153d6bb3c2dSHong Zhang /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 41540e36024fSHong Zhang if (n == PETSC_DECIDE){ 4155357abbc8SBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 41560e36024fSHong Zhang } 4157357abbc8SBarry Smith ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4158357abbc8SBarry Smith rstart -= m; 4159d6bb3c2dSHong Zhang 4160d6bb3c2dSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4161d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4162ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4163d6bb3c2dSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 4164ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4165d6bb3c2dSHong Zhang } 4166d6bb3c2dSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 4167f69a0ea3SMatthew Knepley ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 4168f69a0ea3SMatthew Knepley ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4169d6bb3c2dSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 4170d6bb3c2dSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 4171d6bb3c2dSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 4172d6bb3c2dSHong Zhang 4173d6bb3c2dSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 4174d6bb3c2dSHong Zhang ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 4175d6bb3c2dSHong Zhang } else { 4176e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 4177d6bb3c2dSHong Zhang } 4178d6bb3c2dSHong Zhang 4179d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4180ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4181b7940d39SSatish Balay Ii = i + rstart; 4182b7940d39SSatish Balay ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4183ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4184d6bb3c2dSHong Zhang } 4185d6bb3c2dSHong Zhang ierr = MatDestroy(inmat);CHKERRQ(ierr); 4186d6bb3c2dSHong Zhang ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4187d6bb3c2dSHong Zhang ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 418851dd7536SBarry Smith 4189c5d6d63eSBarry Smith PetscFunctionReturn(0); 4190c5d6d63eSBarry Smith } 4191c5d6d63eSBarry Smith 4192c5d6d63eSBarry Smith #undef __FUNCT__ 4193c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4194dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4195c5d6d63eSBarry Smith { 4196dfbe8321SBarry Smith PetscErrorCode ierr; 419732dcc486SBarry Smith PetscMPIInt rank; 4198b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4199de4209c5SBarry Smith size_t len; 4200b1d57f15SBarry Smith const PetscInt *indx; 4201c5d6d63eSBarry Smith PetscViewer out; 4202c5d6d63eSBarry Smith char *name; 4203c5d6d63eSBarry Smith Mat B; 4204b3cc6726SBarry Smith const PetscScalar *values; 4205c5d6d63eSBarry Smith 4206c5d6d63eSBarry Smith PetscFunctionBegin; 4207c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4208c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4209f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4210f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4211f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4212f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4213f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4214c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4215c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4216c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4217c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4218c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4219c5d6d63eSBarry Smith } 4220c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4221c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4222c5d6d63eSBarry Smith 42237adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4224c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4225c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4226c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4227852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4228c5d6d63eSBarry Smith ierr = PetscFree(name); 4229c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 4230c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 4231c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 4232c5d6d63eSBarry Smith PetscFunctionReturn(0); 4233c5d6d63eSBarry Smith } 4234e5f2cdd8SHong Zhang 423551a7d1a8SHong Zhang EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat); 423651a7d1a8SHong Zhang #undef __FUNCT__ 423751a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 4238be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 423951a7d1a8SHong Zhang { 424051a7d1a8SHong Zhang PetscErrorCode ierr; 4241671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4242776b82aeSLisandro Dalcin PetscContainer container; 424351a7d1a8SHong Zhang 424451a7d1a8SHong Zhang PetscFunctionBegin; 4245671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4246671beff6SHong Zhang if (container) { 4247776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 424851a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 42493e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 42503e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 425151a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 425251a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4253533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 425402c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4255533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 425602c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 425705b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 425805b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 425905b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 426026283091SBarry Smith ierr = PetscLayoutDestroy(merge->rowmap);CHKERRQ(ierr); 4261671beff6SHong Zhang 4262776b82aeSLisandro Dalcin ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 4263671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4264671beff6SHong Zhang } 426551a7d1a8SHong Zhang ierr = PetscFree(merge);CHKERRQ(ierr); 426651a7d1a8SHong Zhang 426751a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 426851a7d1a8SHong Zhang PetscFunctionReturn(0); 426951a7d1a8SHong Zhang } 427051a7d1a8SHong Zhang 42717c4f633dSBarry Smith #include "../src/mat/utils/freespace.h" 4272be0fcf8dSHong Zhang #include "petscbt.h" 42734ebed01fSBarry Smith 4274e5f2cdd8SHong Zhang #undef __FUNCT__ 427538f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric" 4276e5f2cdd8SHong Zhang /*@C 4277f08fae4eSHong Zhang MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential 4278e5f2cdd8SHong Zhang matrices from each processor 4279e5f2cdd8SHong Zhang 4280e5f2cdd8SHong Zhang Collective on MPI_Comm 4281e5f2cdd8SHong Zhang 4282e5f2cdd8SHong Zhang Input Parameters: 4283e5f2cdd8SHong Zhang + comm - the communicators the parallel matrix will live on 4284f08fae4eSHong Zhang . seqmat - the input sequential matrices 42850e36024fSHong Zhang . m - number of local rows (or PETSC_DECIDE) 42860e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4287e5f2cdd8SHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4288e5f2cdd8SHong Zhang 4289e5f2cdd8SHong Zhang Output Parameter: 4290f08fae4eSHong Zhang . mpimat - the parallel matrix generated 4291e5f2cdd8SHong Zhang 4292e5f2cdd8SHong Zhang Level: advanced 4293e5f2cdd8SHong Zhang 4294affca5deSHong Zhang Notes: 4295affca5deSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4296affca5deSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4297affca5deSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4298e5f2cdd8SHong Zhang @*/ 4299be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat) 430055d1abb9SHong Zhang { 430155d1abb9SHong Zhang PetscErrorCode ierr; 43027adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 430355d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4304b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4305d0f46423SBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j; 4306b1d57f15SBarry Smith PetscInt proc,m; 4307b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4308b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4309b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 431055d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 431155d1abb9SHong Zhang MPI_Status *status; 4312a77337e4SBarry Smith MatScalar *aa=a->a; 4313dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 431455d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4315776b82aeSLisandro Dalcin PetscContainer container; 431655d1abb9SHong Zhang 431755d1abb9SHong Zhang PetscFunctionBegin; 43184ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 43193c2c1871SHong Zhang 432055d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 432155d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 432255d1abb9SHong Zhang 432355d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 432455d1abb9SHong Zhang if (container) { 4325776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 432655d1abb9SHong Zhang } 432755d1abb9SHong Zhang bi = merge->bi; 432855d1abb9SHong Zhang bj = merge->bj; 432955d1abb9SHong Zhang buf_ri = merge->buf_ri; 433055d1abb9SHong Zhang buf_rj = merge->buf_rj; 433155d1abb9SHong Zhang 433255d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 43337a2fc3feSBarry Smith owners = merge->rowmap->range; 433455d1abb9SHong Zhang len_s = merge->len_s; 433555d1abb9SHong Zhang 433655d1abb9SHong Zhang /* send and recv matrix values */ 433755d1abb9SHong Zhang /*-----------------------------*/ 4338357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 433955d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 434055d1abb9SHong Zhang 434155d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 434255d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 434355d1abb9SHong Zhang if (!len_s[proc]) continue; 434455d1abb9SHong Zhang i = owners[proc]; 434555d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 434655d1abb9SHong Zhang k++; 434755d1abb9SHong Zhang } 434855d1abb9SHong Zhang 43490c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 43500c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 435155d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 435255d1abb9SHong Zhang 435355d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 435455d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 435555d1abb9SHong Zhang 435655d1abb9SHong Zhang /* insert mat values of mpimat */ 435755d1abb9SHong Zhang /*----------------------------*/ 4358a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 43590572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 436055d1abb9SHong Zhang 436155d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 436255d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 436355d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 436455d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 436555d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 436655d1abb9SHong Zhang } 436755d1abb9SHong Zhang 436855d1abb9SHong Zhang /* set values of ba */ 43697a2fc3feSBarry Smith m = merge->rowmap->n; 437055d1abb9SHong Zhang for (i=0; i<m; i++) { 437155d1abb9SHong Zhang arow = owners[rank] + i; 437255d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 437355d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4374a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 437555d1abb9SHong Zhang 437655d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 437755d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 437855d1abb9SHong Zhang aj = a->j + ai[arow]; 437955d1abb9SHong Zhang aa = a->a + ai[arow]; 438055d1abb9SHong Zhang nextaj = 0; 438155d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 438255d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 438355d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 438455d1abb9SHong Zhang } 438555d1abb9SHong Zhang } 438655d1abb9SHong Zhang 438755d1abb9SHong Zhang /* add received vals into ba */ 438855d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 438955d1abb9SHong Zhang /* i-th row */ 439055d1abb9SHong Zhang if (i == *nextrow[k]) { 439155d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 439255d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 439355d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 439455d1abb9SHong Zhang nextaj = 0; 439555d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 439655d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 439755d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 439855d1abb9SHong Zhang } 439955d1abb9SHong Zhang } 440055d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 440155d1abb9SHong Zhang } 440255d1abb9SHong Zhang } 440355d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 440455d1abb9SHong Zhang } 440555d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 440655d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 440755d1abb9SHong Zhang 4408533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 440955d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 441055d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 44111d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 44124ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 441355d1abb9SHong Zhang PetscFunctionReturn(0); 441455d1abb9SHong Zhang } 441538f152feSBarry Smith 441638f152feSBarry Smith #undef __FUNCT__ 441738f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic" 4418be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4419e5f2cdd8SHong Zhang { 4420f08fae4eSHong Zhang PetscErrorCode ierr; 442155a3bba9SHong Zhang Mat B_mpi; 4422c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4423b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4424b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4425d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4426b1d57f15SBarry Smith PetscInt len,proc,*dnz,*onz; 4427b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4428b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 442955d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 443058cb9c82SHong Zhang MPI_Status *status; 4431a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4432be0fcf8dSHong Zhang PetscBT lnkbt; 443351a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4434776b82aeSLisandro Dalcin PetscContainer container; 443502c68681SHong Zhang 4436e5f2cdd8SHong Zhang PetscFunctionBegin; 44374ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 44383c2c1871SHong Zhang 443938f152feSBarry Smith /* make sure it is a PETSc comm */ 444038f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4441e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4442e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 444355d1abb9SHong Zhang 444451a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4445c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4446e5f2cdd8SHong Zhang 44476abd8857SHong Zhang /* determine row ownership */ 4448f08fae4eSHong Zhang /*---------------------------------------------------------*/ 444926283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 445026283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 445126283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 445226283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 445326283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4454b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4455b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 445655d1abb9SHong Zhang 44577a2fc3feSBarry Smith m = merge->rowmap->n; 44587a2fc3feSBarry Smith M = merge->rowmap->N; 44597a2fc3feSBarry Smith owners = merge->rowmap->range; 44606abd8857SHong Zhang 44616abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 44626abd8857SHong Zhang /*---------------------------------------------------------*/ 44633e06a4e6SHong Zhang len_s = merge->len_s; 446451a7d1a8SHong Zhang 44652257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4466c2234fe3SHong Zhang merge->nsend = 0; 4467409913e3SHong Zhang for (proc=0; proc<size; proc++){ 44682257cef7SHong Zhang len_si[proc] = 0; 44693e06a4e6SHong Zhang if (proc == rank){ 44706abd8857SHong Zhang len_s[proc] = 0; 44713e06a4e6SHong Zhang } else { 447202c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 44733e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 44743e06a4e6SHong Zhang } 44753e06a4e6SHong Zhang if (len_s[proc]) { 4476c2234fe3SHong Zhang merge->nsend++; 44772257cef7SHong Zhang nrows = 0; 44782257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 44792257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 44802257cef7SHong Zhang } 44812257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 44822257cef7SHong Zhang len += len_si[proc]; 4483409913e3SHong Zhang } 448458cb9c82SHong Zhang } 4485409913e3SHong Zhang 44862257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 44872257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 448851a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 448955d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4490671beff6SHong Zhang 44913e06a4e6SHong Zhang /* post the Irecv of j-structure */ 44923e06a4e6SHong Zhang /*-------------------------------*/ 44932c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 44943e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 449502c68681SHong Zhang 44963e06a4e6SHong Zhang /* post the Isend of j-structure */ 4497affca5deSHong Zhang /*--------------------------------*/ 44981d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 44993e06a4e6SHong Zhang 45002257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4501409913e3SHong Zhang if (!len_s[proc]) continue; 450202c68681SHong Zhang i = owners[proc]; 4503b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 450451a7d1a8SHong Zhang k++; 450551a7d1a8SHong Zhang } 450651a7d1a8SHong Zhang 45073e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 45083e06a4e6SHong Zhang /*------------------------------------------------*/ 45090c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 45100c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 451102c68681SHong Zhang 451202c68681SHong Zhang /* send and recv i-structure */ 451302c68681SHong Zhang /*---------------------------*/ 45142c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 451502c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 451602c68681SHong Zhang 4517b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 45183e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 45192257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 452002c68681SHong Zhang if (!len_s[proc]) continue; 45213e06a4e6SHong Zhang /* form outgoing message for i-structure: 45223e06a4e6SHong Zhang buf_si[0]: nrows to be sent 45233e06a4e6SHong Zhang [1:nrows]: row index (global) 45243e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 45253e06a4e6SHong Zhang */ 45263e06a4e6SHong Zhang /*-------------------------------------------*/ 45272257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 45283e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 45293e06a4e6SHong Zhang buf_si[0] = nrows; 45303e06a4e6SHong Zhang buf_si_i[0] = 0; 45313e06a4e6SHong Zhang nrows = 0; 45323e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 45333e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 45343e06a4e6SHong Zhang if (anzi) { 45353e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 45363e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 45373e06a4e6SHong Zhang nrows++; 45383e06a4e6SHong Zhang } 45393e06a4e6SHong Zhang } 4540b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 454102c68681SHong Zhang k++; 45422257cef7SHong Zhang buf_si += len_si[proc]; 454302c68681SHong Zhang } 45442257cef7SHong Zhang 45450c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 45460c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 454702c68681SHong Zhang 4548ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 45493e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4550ae15b995SBarry 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); 45513e06a4e6SHong Zhang } 45523e06a4e6SHong Zhang 45533e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 455402c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 455502c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 45561d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 45572257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 45583e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4559bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 456058cb9c82SHong Zhang 4561bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4562bcc1bcd5SHong Zhang /*----------------------------------------------*/ 456358cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4564b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 456558cb9c82SHong Zhang bi[0] = 0; 456658cb9c82SHong Zhang 4567be0fcf8dSHong Zhang /* create and initialize a linked list */ 4568be0fcf8dSHong Zhang nlnk = N+1; 4569be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 457058cb9c82SHong Zhang 4571bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 457258cb9c82SHong Zhang len = 0; 4573bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4574a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 457558cb9c82SHong Zhang current_space = free_space; 457658cb9c82SHong Zhang 4577bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 45780572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 45791d79065fSBarry Smith 45803e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 45812257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 45823e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 45833e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 45842257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 45853e06a4e6SHong Zhang } 45862257cef7SHong Zhang 4587bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4588bcc1bcd5SHong Zhang len = 0; 458958cb9c82SHong Zhang for (i=0;i<m;i++) { 459058cb9c82SHong Zhang bnzi = 0; 459158cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 459258cb9c82SHong Zhang arow = owners[rank] + i; 459358cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 459458cb9c82SHong Zhang aj = a->j + ai[arow]; 4595be0fcf8dSHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 459658cb9c82SHong Zhang bnzi += nlnk; 459758cb9c82SHong Zhang /* add received col data into lnk */ 459851a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 459955d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 46003e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 46013e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 46023e06a4e6SHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 46033e06a4e6SHong Zhang bnzi += nlnk; 46043e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 46053e06a4e6SHong Zhang } 460658cb9c82SHong Zhang } 4607bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 460858cb9c82SHong Zhang 460958cb9c82SHong Zhang /* if free space is not available, make more free space */ 461058cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 46114238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 461258cb9c82SHong Zhang nspacedouble++; 461358cb9c82SHong Zhang } 461458cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4615be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4616bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4617bcc1bcd5SHong Zhang 461858cb9c82SHong Zhang current_space->array += bnzi; 461958cb9c82SHong Zhang current_space->local_used += bnzi; 462058cb9c82SHong Zhang current_space->local_remaining -= bnzi; 462158cb9c82SHong Zhang 462258cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 462358cb9c82SHong Zhang } 4624bcc1bcd5SHong Zhang 46251d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4626bcc1bcd5SHong Zhang 4627b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4628a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4629be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4630409913e3SHong Zhang 4631bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4632bcc1bcd5SHong Zhang /*---------------------------------------*/ 4633f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 463454b84b50SHong Zhang if (n==PETSC_DECIDE) { 4635f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 463654b84b50SHong Zhang } else { 4637f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 463854b84b50SHong Zhang } 4639bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4640bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4641bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 464258cb9c82SHong Zhang 46436abd8857SHong Zhang /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */ 46446abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4645affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4646affca5deSHong Zhang merge->bi = bi; 4647affca5deSHong Zhang merge->bj = bj; 464802c68681SHong Zhang merge->buf_ri = buf_ri; 464902c68681SHong Zhang merge->buf_rj = buf_rj; 4650de0260b3SHong Zhang merge->coi = PETSC_NULL; 4651de0260b3SHong Zhang merge->coj = PETSC_NULL; 4652de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4653affca5deSHong Zhang 4654affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4655776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4656776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4657affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4658affca5deSHong Zhang *mpimat = B_mpi; 465938f152feSBarry Smith 466038f152feSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 46614ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4662e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4663e5f2cdd8SHong Zhang } 466425616d81SHong Zhang 466538f152feSBarry Smith #undef __FUNCT__ 466638f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI" 4667be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 466855d1abb9SHong Zhang { 466955d1abb9SHong Zhang PetscErrorCode ierr; 467055d1abb9SHong Zhang 467155d1abb9SHong Zhang PetscFunctionBegin; 46724ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 467355d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 467455d1abb9SHong Zhang ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 467555d1abb9SHong Zhang } 467655d1abb9SHong Zhang ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr); 46774ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 467855d1abb9SHong Zhang PetscFunctionReturn(0); 467955d1abb9SHong Zhang } 46804ebed01fSBarry Smith 468125616d81SHong Zhang #undef __FUNCT__ 468225616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat" 4683bc08b0f1SBarry Smith /*@ 468432fba14fSHong Zhang MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows 468525616d81SHong Zhang 468632fba14fSHong Zhang Not Collective 468725616d81SHong Zhang 468825616d81SHong Zhang Input Parameters: 468925616d81SHong Zhang + A - the matrix 469025616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 469125616d81SHong Zhang 469225616d81SHong Zhang Output Parameter: 469325616d81SHong Zhang . A_loc - the local sequential matrix generated 469425616d81SHong Zhang 469525616d81SHong Zhang Level: developer 469625616d81SHong Zhang 469725616d81SHong Zhang @*/ 4698be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 469925616d81SHong Zhang { 470025616d81SHong Zhang PetscErrorCode ierr; 470101b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 470201b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 470301b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 4704a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 4705a77337e4SBarry Smith PetscScalar *ca; 4706d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 47075a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 470825616d81SHong Zhang 470925616d81SHong Zhang PetscFunctionBegin; 47104ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 471101b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4712dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 4713dea91ad1SHong Zhang ci[0] = 0; 471401b7ae99SHong Zhang for (i=0; i<am; i++){ 4715dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 471601b7ae99SHong Zhang } 4717dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 4718dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 4719dea91ad1SHong Zhang k = 0; 472001b7ae99SHong Zhang for (i=0; i<am; i++) { 47215a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47225a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 472301b7ae99SHong Zhang /* off-diagonal portion of A */ 47245a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47255a7d977cSHong Zhang col = cmap[*bj]; 47265a7d977cSHong Zhang if (col >= cstart) break; 47275a7d977cSHong Zhang cj[k] = col; bj++; 47285a7d977cSHong Zhang ca[k++] = *ba++; 47295a7d977cSHong Zhang } 47305a7d977cSHong Zhang /* diagonal portion of A */ 47315a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 47325a7d977cSHong Zhang cj[k] = cstart + *aj++; 47335a7d977cSHong Zhang ca[k++] = *aa++; 47345a7d977cSHong Zhang } 47355a7d977cSHong Zhang /* off-diagonal portion of A */ 47365a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 47375a7d977cSHong Zhang cj[k] = cmap[*bj++]; 47385a7d977cSHong Zhang ca[k++] = *ba++; 47395a7d977cSHong Zhang } 474025616d81SHong Zhang } 4741dea91ad1SHong Zhang /* put together the new matrix */ 4742d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 4743dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4744dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 4745dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 4746e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 4747e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 4748dea91ad1SHong Zhang mat->nonew = 0; 47495a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 47505a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 4751a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 47525a7d977cSHong Zhang for (i=0; i<am; i++) { 47535a7d977cSHong Zhang /* off-diagonal portion of A */ 47545a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47555a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47565a7d977cSHong Zhang col = cmap[*bj]; 47575a7d977cSHong Zhang if (col >= cstart) break; 4758a77337e4SBarry Smith *cam++ = *ba++; bj++; 47595a7d977cSHong Zhang } 47605a7d977cSHong Zhang /* diagonal portion of A */ 4761ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 4762a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 47635a7d977cSHong Zhang /* off-diagonal portion of A */ 4764f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 4765a77337e4SBarry Smith *cam++ = *ba++; bj++; 4766f33d1a9aSHong Zhang } 47675a7d977cSHong Zhang } 47685a7d977cSHong Zhang } else { 4769e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 477025616d81SHong Zhang } 477101b7ae99SHong Zhang 47724ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 477325616d81SHong Zhang PetscFunctionReturn(0); 477425616d81SHong Zhang } 477525616d81SHong Zhang 477632fba14fSHong Zhang #undef __FUNCT__ 477732fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed" 477832fba14fSHong Zhang /*@C 477932fba14fSHong Zhang MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns 478032fba14fSHong Zhang 478132fba14fSHong Zhang Not Collective 478232fba14fSHong Zhang 478332fba14fSHong Zhang Input Parameters: 478432fba14fSHong Zhang + A - the matrix 478532fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 478632fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 478732fba14fSHong Zhang 478832fba14fSHong Zhang Output Parameter: 478932fba14fSHong Zhang . A_loc - the local sequential matrix generated 479032fba14fSHong Zhang 479132fba14fSHong Zhang Level: developer 479232fba14fSHong Zhang 479332fba14fSHong Zhang @*/ 4794be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 479532fba14fSHong Zhang { 479632fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 479732fba14fSHong Zhang PetscErrorCode ierr; 479832fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 479932fba14fSHong Zhang IS isrowa,iscola; 480032fba14fSHong Zhang Mat *aloc; 480132fba14fSHong Zhang 480232fba14fSHong Zhang PetscFunctionBegin; 48034ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 480432fba14fSHong Zhang if (!row){ 4805d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 480632fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 480732fba14fSHong Zhang } else { 480832fba14fSHong Zhang isrowa = *row; 480932fba14fSHong Zhang } 481032fba14fSHong Zhang if (!col){ 4811d0f46423SBarry Smith start = A->cmap->rstart; 481232fba14fSHong Zhang cmap = a->garray; 4813d0f46423SBarry Smith nzA = a->A->cmap->n; 4814d0f46423SBarry Smith nzB = a->B->cmap->n; 481532fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 481632fba14fSHong Zhang ncols = 0; 481732fba14fSHong Zhang for (i=0; i<nzB; i++) { 481832fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 481932fba14fSHong Zhang else break; 482032fba14fSHong Zhang } 482132fba14fSHong Zhang imark = i; 482232fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 482332fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 4824d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 482532fba14fSHong Zhang } else { 482632fba14fSHong Zhang iscola = *col; 482732fba14fSHong Zhang } 482832fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 482932fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 483032fba14fSHong Zhang aloc[0] = *A_loc; 483132fba14fSHong Zhang } 483232fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 483332fba14fSHong Zhang *A_loc = aloc[0]; 483432fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 483532fba14fSHong Zhang if (!row){ 483632fba14fSHong Zhang ierr = ISDestroy(isrowa);CHKERRQ(ierr); 483732fba14fSHong Zhang } 483832fba14fSHong Zhang if (!col){ 483932fba14fSHong Zhang ierr = ISDestroy(iscola);CHKERRQ(ierr); 484032fba14fSHong Zhang } 48414ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 484232fba14fSHong Zhang PetscFunctionReturn(0); 484332fba14fSHong Zhang } 484432fba14fSHong Zhang 484525616d81SHong Zhang #undef __FUNCT__ 484625616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 484725616d81SHong Zhang /*@C 484832fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 484925616d81SHong Zhang 485025616d81SHong Zhang Collective on Mat 485125616d81SHong Zhang 485225616d81SHong Zhang Input Parameters: 4853e240928fSHong Zhang + A,B - the matrices in mpiaij format 485425616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 485525616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 485625616d81SHong Zhang 485725616d81SHong Zhang Output Parameter: 485825616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 4859d0f46423SBarry Smith . brstart - row index of B_seq from which next B->rmap->n rows are taken from B's local rows 486025616d81SHong Zhang - B_seq - the sequential matrix generated 486125616d81SHong Zhang 486225616d81SHong Zhang Level: developer 486325616d81SHong Zhang 486425616d81SHong Zhang @*/ 4865be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq) 486625616d81SHong Zhang { 4867899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 486825616d81SHong Zhang PetscErrorCode ierr; 4869b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 487025616d81SHong Zhang IS isrowb,iscolb; 487125616d81SHong Zhang Mat *bseq; 487225616d81SHong Zhang 487325616d81SHong Zhang PetscFunctionBegin; 4874d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4875e32f2f54SBarry 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); 487625616d81SHong Zhang } 48774ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 487825616d81SHong Zhang 487925616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4880d0f46423SBarry Smith start = A->cmap->rstart; 488125616d81SHong Zhang cmap = a->garray; 4882d0f46423SBarry Smith nzA = a->A->cmap->n; 4883d0f46423SBarry Smith nzB = a->B->cmap->n; 4884b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 488525616d81SHong Zhang ncols = 0; 48860390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 488725616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 488825616d81SHong Zhang else break; 488925616d81SHong Zhang } 489025616d81SHong Zhang imark = i; 48910390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 48920390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 4893d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 489425616d81SHong Zhang *brstart = imark; 4895d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 489625616d81SHong Zhang } else { 4897e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 489825616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 489925616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 490025616d81SHong Zhang bseq[0] = *B_seq; 490125616d81SHong Zhang } 490225616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 490325616d81SHong Zhang *B_seq = bseq[0]; 490425616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 490525616d81SHong Zhang if (!rowb){ 490625616d81SHong Zhang ierr = ISDestroy(isrowb);CHKERRQ(ierr); 490725616d81SHong Zhang } else { 490825616d81SHong Zhang *rowb = isrowb; 490925616d81SHong Zhang } 491025616d81SHong Zhang if (!colb){ 491125616d81SHong Zhang ierr = ISDestroy(iscolb);CHKERRQ(ierr); 491225616d81SHong Zhang } else { 491325616d81SHong Zhang *colb = iscolb; 491425616d81SHong Zhang } 49154ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 491625616d81SHong Zhang PetscFunctionReturn(0); 491725616d81SHong Zhang } 4918429d309bSHong Zhang 4919a61c8c0fSHong Zhang #undef __FUNCT__ 4920a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols" 4921429d309bSHong Zhang /*@C 4922429d309bSHong Zhang MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 492301b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 4924429d309bSHong Zhang 4925429d309bSHong Zhang Collective on Mat 4926429d309bSHong Zhang 4927429d309bSHong Zhang Input Parameters: 4928429d309bSHong Zhang + A,B - the matrices in mpiaij format 492987025532SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 493087025532SHong Zhang . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 49311d79065fSBarry Smith . startsj_r - similar to startsj for receives 493287025532SHong Zhang - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 4933429d309bSHong Zhang 4934429d309bSHong Zhang Output Parameter: 493587025532SHong Zhang + B_oth - the sequential matrix generated 4936429d309bSHong Zhang 4937429d309bSHong Zhang Level: developer 4938429d309bSHong Zhang 4939429d309bSHong Zhang @*/ 49401d79065fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 4941429d309bSHong Zhang { 4942a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 4943429d309bSHong Zhang PetscErrorCode ierr; 4944899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 494587025532SHong Zhang Mat_SeqAIJ *b_oth; 4946a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 49477adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 49487adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 4949d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 4950dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 4951dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 4952e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 4953910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 495487025532SHong Zhang MPI_Status *sstatus,rstatus; 4955aa5bb8c0SSatish Balay PetscMPIInt jj; 4956e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 4957ba8c8a56SBarry Smith PetscScalar *vals; 4958429d309bSHong Zhang 4959429d309bSHong Zhang PetscFunctionBegin; 4960d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4961e32f2f54SBarry 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); 4962429d309bSHong Zhang } 49634ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4964a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 4965a6b2eed2SHong Zhang 4966a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 4967a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 4968e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 4969e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 4970a6b2eed2SHong Zhang nrecvs = gen_from->n; 4971a6b2eed2SHong Zhang nsends = gen_to->n; 4972d7ee0231SBarry Smith 4973d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 4974a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 4975a6b2eed2SHong Zhang sstarts = gen_to->starts; 4976a6b2eed2SHong Zhang sprocs = gen_to->procs; 4977a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 4978e42f35eeSHong Zhang sbs = gen_to->bs; 4979e42f35eeSHong Zhang rstarts = gen_from->starts; 4980e42f35eeSHong Zhang rprocs = gen_from->procs; 4981e42f35eeSHong Zhang rbs = gen_from->bs; 4982429d309bSHong Zhang 4983dea91ad1SHong Zhang if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 4984429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4985a6b2eed2SHong Zhang /* i-array */ 4986a6b2eed2SHong Zhang /*---------*/ 4987a6b2eed2SHong Zhang /* post receives */ 4988a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 4989e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 4990e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 499187025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 4992429d309bSHong Zhang } 4993a6b2eed2SHong Zhang 4994a6b2eed2SHong Zhang /* pack the outgoing message */ 49951d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 4996a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 4997a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 4998a6b2eed2SHong Zhang k = 0; 4999a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5000e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5001e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 500287025532SHong Zhang for (j=0; j<nrows; j++) { 5003d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5004e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 5005e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 5006e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 5007e42f35eeSHong Zhang len += ncols; 5008e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5009e42f35eeSHong Zhang } 5010a6b2eed2SHong Zhang k++; 5011429d309bSHong Zhang } 5012e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5013dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5014429d309bSHong Zhang } 501587025532SHong Zhang /* recvs and sends of i-array are completed */ 501687025532SHong Zhang i = nrecvs; 501787025532SHong Zhang while (i--) { 5018aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 501987025532SHong Zhang } 50200c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5021e42f35eeSHong Zhang 5022a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5023a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 5024a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 5025a6b2eed2SHong Zhang 502687025532SHong Zhang /* create i-array of B_oth */ 502787025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 502887025532SHong Zhang b_othi[0] = 0; 5029a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5030a6b2eed2SHong Zhang k = 0; 5031a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5032fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5033e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 503487025532SHong Zhang for (j=0; j<nrows; j++) { 503587025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5036a6b2eed2SHong Zhang len += rowlen[j]; k++; 5037a6b2eed2SHong Zhang } 5038dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5039a6b2eed2SHong Zhang } 5040a6b2eed2SHong Zhang 504187025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 504287025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 5043dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 5044a6b2eed2SHong Zhang 504587025532SHong Zhang /* j-array */ 504687025532SHong Zhang /*---------*/ 5047a6b2eed2SHong Zhang /* post receives of j-array */ 5048a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 504987025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 505087025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5051a6b2eed2SHong Zhang } 5052e42f35eeSHong Zhang 5053e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5054a6b2eed2SHong Zhang k = 0; 5055a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5056e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5057a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 505887025532SHong Zhang for (j=0; j<nrows; j++) { 5059d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5060e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5061e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5062a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 5063a6b2eed2SHong Zhang *bufJ++ = cols[l]; 506487025532SHong Zhang } 5065e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5066e42f35eeSHong Zhang } 506787025532SHong Zhang } 506887025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 506987025532SHong Zhang } 507087025532SHong Zhang 507187025532SHong Zhang /* recvs and sends of j-array are completed */ 507287025532SHong Zhang i = nrecvs; 507387025532SHong Zhang while (i--) { 5074aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 507587025532SHong Zhang } 50760c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 507787025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 507887025532SHong Zhang sstartsj = *startsj; 50791d79065fSBarry Smith rstartsj = *startsj_r; 508087025532SHong Zhang bufa = *bufa_ptr; 508187025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 508287025532SHong Zhang b_otha = b_oth->a; 508387025532SHong Zhang } else { 5084e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 508587025532SHong Zhang } 508687025532SHong Zhang 508787025532SHong Zhang /* a-array */ 508887025532SHong Zhang /*---------*/ 508987025532SHong Zhang /* post receives of a-array */ 509087025532SHong Zhang for (i=0; i<nrecvs; i++){ 509187025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 509287025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 509387025532SHong Zhang } 5094e42f35eeSHong Zhang 5095e42f35eeSHong Zhang /* pack the outgoing message a-array */ 509687025532SHong Zhang k = 0; 509787025532SHong Zhang for (i=0; i<nsends; i++){ 5098e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 509987025532SHong Zhang bufA = bufa+sstartsj[i]; 510087025532SHong Zhang for (j=0; j<nrows; j++) { 5101d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5102e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5103e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 510487025532SHong Zhang for (l=0; l<ncols; l++){ 5105a6b2eed2SHong Zhang *bufA++ = vals[l]; 5106a6b2eed2SHong Zhang } 5107e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 5108e42f35eeSHong Zhang } 5109a6b2eed2SHong Zhang } 511087025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5111a6b2eed2SHong Zhang } 511287025532SHong Zhang /* recvs and sends of a-array are completed */ 511387025532SHong Zhang i = nrecvs; 511487025532SHong Zhang while (i--) { 5115aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 511687025532SHong Zhang } 51170c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5118d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5119a6b2eed2SHong Zhang 512087025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5121a6b2eed2SHong Zhang /* put together the new matrix */ 5122d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5123a6b2eed2SHong Zhang 5124a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5125a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 512687025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 5127e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5128e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 512987025532SHong Zhang b_oth->nonew = 0; 5130a6b2eed2SHong Zhang 5131a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5132dea91ad1SHong Zhang if (!startsj || !bufa_ptr){ 51331d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5134dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5135dea91ad1SHong Zhang } else { 513687025532SHong Zhang *startsj = sstartsj; 51371d79065fSBarry Smith *startsj_r = rstartsj; 513887025532SHong Zhang *bufa_ptr = bufa; 513987025532SHong Zhang } 5140dea91ad1SHong Zhang } 51414ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5142429d309bSHong Zhang PetscFunctionReturn(0); 5143429d309bSHong Zhang } 5144ccd8e176SBarry Smith 514543eb5e2fSMatthew Knepley #undef __FUNCT__ 514643eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 514743eb5e2fSMatthew Knepley /*@C 514843eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 514943eb5e2fSMatthew Knepley 515043eb5e2fSMatthew Knepley Not Collective 515143eb5e2fSMatthew Knepley 515243eb5e2fSMatthew Knepley Input Parameters: 515343eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 515443eb5e2fSMatthew Knepley 515543eb5e2fSMatthew Knepley Output Parameter: 515643eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 515743eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 515843eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 515943eb5e2fSMatthew Knepley 516043eb5e2fSMatthew Knepley Level: developer 516143eb5e2fSMatthew Knepley 516243eb5e2fSMatthew Knepley @*/ 516343eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 516443eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 516543eb5e2fSMatthew Knepley #else 516643eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 516743eb5e2fSMatthew Knepley #endif 516843eb5e2fSMatthew Knepley { 516943eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 517043eb5e2fSMatthew Knepley 517143eb5e2fSMatthew Knepley PetscFunctionBegin; 51720700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5173e414b56bSJed Brown PetscValidPointer(lvec, 2); 5174e414b56bSJed Brown PetscValidPointer(colmap, 3); 5175e414b56bSJed Brown PetscValidPointer(multScatter, 4); 517643eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 517743eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 517843eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 517943eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 518043eb5e2fSMatthew Knepley PetscFunctionReturn(0); 518143eb5e2fSMatthew Knepley } 518243eb5e2fSMatthew Knepley 518317667f90SBarry Smith EXTERN_C_BEGIN 51845a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*); 51855a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*); 5186c4688eafSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*); 518717667f90SBarry Smith EXTERN_C_END 518817667f90SBarry Smith 5189fc4dec0aSBarry Smith #undef __FUNCT__ 5190fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5191fc4dec0aSBarry Smith /* 5192fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5193fc4dec0aSBarry Smith 5194fc4dec0aSBarry Smith n p p 5195fc4dec0aSBarry Smith ( ) ( ) ( ) 5196fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5197fc4dec0aSBarry Smith ( ) ( ) ( ) 5198fc4dec0aSBarry Smith 5199fc4dec0aSBarry Smith */ 5200fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5201fc4dec0aSBarry Smith { 5202fc4dec0aSBarry Smith PetscErrorCode ierr; 5203fc4dec0aSBarry Smith Mat At,Bt,Ct; 5204fc4dec0aSBarry Smith 5205fc4dec0aSBarry Smith PetscFunctionBegin; 5206fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5207fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5208fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 5209fc4dec0aSBarry Smith ierr = MatDestroy(At);CHKERRQ(ierr); 5210fc4dec0aSBarry Smith ierr = MatDestroy(Bt);CHKERRQ(ierr); 5211fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 5212e5e4356aSBarry Smith ierr = MatDestroy(Ct);CHKERRQ(ierr); 5213fc4dec0aSBarry Smith PetscFunctionReturn(0); 5214fc4dec0aSBarry Smith } 5215fc4dec0aSBarry Smith 5216fc4dec0aSBarry Smith #undef __FUNCT__ 5217fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5218fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5219fc4dec0aSBarry Smith { 5220fc4dec0aSBarry Smith PetscErrorCode ierr; 5221d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5222fc4dec0aSBarry Smith Mat Cmat; 5223fc4dec0aSBarry Smith 5224fc4dec0aSBarry Smith PetscFunctionBegin; 5225e32f2f54SBarry 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); 522639804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5227fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5228fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5229fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 523038556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 523138556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5232fc4dec0aSBarry Smith *C = Cmat; 5233fc4dec0aSBarry Smith PetscFunctionReturn(0); 5234fc4dec0aSBarry Smith } 5235fc4dec0aSBarry Smith 5236fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5237fc4dec0aSBarry Smith #undef __FUNCT__ 5238fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5239fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5240fc4dec0aSBarry Smith { 5241fc4dec0aSBarry Smith PetscErrorCode ierr; 5242fc4dec0aSBarry Smith 5243fc4dec0aSBarry Smith PetscFunctionBegin; 5244fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5245fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5246fc4dec0aSBarry Smith } 5247fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5248fc4dec0aSBarry Smith PetscFunctionReturn(0); 5249fc4dec0aSBarry Smith } 5250fc4dec0aSBarry Smith 52515c9eb25fSBarry Smith EXTERN_C_BEGIN 5252611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5253bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5254611f576cSBarry Smith #endif 52553bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 52563bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 52573bf14a46SMatthew Knepley #endif 5258611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 52595c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5260611f576cSBarry Smith #endif 5261611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 52625c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*); 5263611f576cSBarry Smith #endif 52645c9eb25fSBarry Smith EXTERN_C_END 52655c9eb25fSBarry Smith 5266ccd8e176SBarry Smith /*MC 5267ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5268ccd8e176SBarry Smith 5269ccd8e176SBarry Smith Options Database Keys: 5270ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5271ccd8e176SBarry Smith 5272ccd8e176SBarry Smith Level: beginner 5273ccd8e176SBarry Smith 5274175b88e8SBarry Smith .seealso: MatCreateMPIAIJ() 5275ccd8e176SBarry Smith M*/ 5276ccd8e176SBarry Smith 5277ccd8e176SBarry Smith EXTERN_C_BEGIN 5278ccd8e176SBarry Smith #undef __FUNCT__ 5279ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 5280be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B) 5281ccd8e176SBarry Smith { 5282ccd8e176SBarry Smith Mat_MPIAIJ *b; 5283ccd8e176SBarry Smith PetscErrorCode ierr; 5284ccd8e176SBarry Smith PetscMPIInt size; 5285ccd8e176SBarry Smith 5286ccd8e176SBarry Smith PetscFunctionBegin; 52877adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 5288ccd8e176SBarry Smith 528938f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5290ccd8e176SBarry Smith B->data = (void*)b; 5291ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5292d0f46423SBarry Smith B->rmap->bs = 1; 5293ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5294ccd8e176SBarry Smith B->mapping = 0; 5295ccd8e176SBarry Smith 5296ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5297ccd8e176SBarry Smith b->size = size; 52987adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5299ccd8e176SBarry Smith 5300ccd8e176SBarry Smith /* build cache for off array entries formed */ 53017adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5302ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5303ccd8e176SBarry Smith b->colmap = 0; 5304ccd8e176SBarry Smith b->garray = 0; 5305ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5306ccd8e176SBarry Smith 5307ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5308ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5309ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5310ccd8e176SBarry Smith 5311ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5312ccd8e176SBarry Smith b->rowindices = 0; 5313ccd8e176SBarry Smith b->rowvalues = 0; 5314ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5315ccd8e176SBarry Smith 5316611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 5317ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 53185c9eb25fSBarry Smith "MatGetFactor_mpiaij_spooles", 53195c9eb25fSBarry Smith MatGetFactor_mpiaij_spooles);CHKERRQ(ierr); 5320611f576cSBarry Smith #endif 5321611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5322ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5323bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5324bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5325611f576cSBarry Smith #endif 53263bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5327ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 53283bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 53293bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 53303bf14a46SMatthew Knepley #endif 5331611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5332ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 53335c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 53345c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5335611f576cSBarry Smith #endif 5336ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5337ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5338ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5339ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5340ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5341ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5342ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5343ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5344ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5345ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5346ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5347ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5348ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5349ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5350ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5351ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5352ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5353ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5354ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5355ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5356ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 53575a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C", 53585a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJPERM", 53595a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 53605a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C", 53615a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJCRL", 53625a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5363471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5364471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5365471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5366fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5367fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5368fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5369fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5370fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5371fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5372fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5373fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5374fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 537517667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5376ccd8e176SBarry Smith PetscFunctionReturn(0); 5377ccd8e176SBarry Smith } 5378ccd8e176SBarry Smith EXTERN_C_END 537981824310SBarry Smith 538003bfb495SBarry Smith #undef __FUNCT__ 538103bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 538258d36128SBarry Smith /*@ 538303bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 538403bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 538503bfb495SBarry Smith 538603bfb495SBarry Smith Collective on MPI_Comm 538703bfb495SBarry Smith 538803bfb495SBarry Smith Input Parameters: 538903bfb495SBarry Smith + comm - MPI communicator 539003bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 539103bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 539203bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 539303bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 539403bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 539503bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 539603bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 539703bfb495SBarry Smith . j - column indices 539803bfb495SBarry Smith . a - matrix values 539903bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 540003bfb495SBarry Smith . oj - column indices 540103bfb495SBarry Smith - oa - matrix values 540203bfb495SBarry Smith 540303bfb495SBarry Smith Output Parameter: 540403bfb495SBarry Smith . mat - the matrix 540503bfb495SBarry Smith 540603bfb495SBarry Smith Level: advanced 540703bfb495SBarry Smith 540803bfb495SBarry Smith Notes: 540903bfb495SBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. 541003bfb495SBarry Smith 541103bfb495SBarry Smith The i and j indices are 0 based 541203bfb495SBarry Smith 541303bfb495SBarry Smith See MatCreateMPIAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 541403bfb495SBarry Smith 54157b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 54167b55108eSBarry Smith 54177b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 541803bfb495SBarry Smith 541903bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 542003bfb495SBarry Smith 542103bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 54228d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithArrays() 542303bfb495SBarry Smith @*/ 54248d7a6e47SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 542503bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 542603bfb495SBarry Smith { 542703bfb495SBarry Smith PetscErrorCode ierr; 542803bfb495SBarry Smith Mat_MPIAIJ *maij; 542903bfb495SBarry Smith 543003bfb495SBarry Smith PetscFunctionBegin; 5431e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 543203bfb495SBarry Smith if (i[0]) { 5433e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 543403bfb495SBarry Smith } 543503bfb495SBarry Smith if (oi[0]) { 5436e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 543703bfb495SBarry Smith } 543803bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 543903bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 544003bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 544103bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 54428d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 54438d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 544403bfb495SBarry Smith 544526283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->rmap,1);CHKERRQ(ierr); 544626283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->cmap,1);CHKERRQ(ierr); 544726283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 544826283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 544903bfb495SBarry Smith 545003bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5451d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 545203bfb495SBarry Smith 54538d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54548d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54558d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54568d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54578d7a6e47SBarry Smith 545803bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 545903bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 546003bfb495SBarry Smith PetscFunctionReturn(0); 546103bfb495SBarry Smith } 546203bfb495SBarry Smith 546381824310SBarry Smith /* 546481824310SBarry Smith Special version for direct calls from Fortran 546581824310SBarry Smith */ 546681824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 546781824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 546881824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 546981824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 547081824310SBarry Smith #endif 547181824310SBarry Smith 547281824310SBarry Smith /* Change these macros so can be used in void function */ 547381824310SBarry Smith #undef CHKERRQ 5474e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 547581824310SBarry Smith #undef SETERRQ2 5476e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 547781824310SBarry Smith #undef SETERRQ 5478e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 547981824310SBarry Smith 548081824310SBarry Smith EXTERN_C_BEGIN 548181824310SBarry Smith #undef __FUNCT__ 548281824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 54831f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 548481824310SBarry Smith { 548581824310SBarry Smith Mat mat = *mmat; 548681824310SBarry Smith PetscInt m = *mm, n = *mn; 548781824310SBarry Smith InsertMode addv = *maddv; 548881824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 548981824310SBarry Smith PetscScalar value; 549081824310SBarry Smith PetscErrorCode ierr; 5491899cda47SBarry Smith 5492d9e2c085SLisandro Dalcin ierr = MatPreallocated(mat);CHKERRQ(ierr); 549381824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 549481824310SBarry Smith mat->insertmode = addv; 549581824310SBarry Smith } 549681824310SBarry Smith #if defined(PETSC_USE_DEBUG) 549781824310SBarry Smith else if (mat->insertmode != addv) { 5498e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 549981824310SBarry Smith } 550081824310SBarry Smith #endif 550181824310SBarry Smith { 5502d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5503d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5504ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 550581824310SBarry Smith 550681824310SBarry Smith /* Some Variables required in the macro */ 550781824310SBarry Smith Mat A = aij->A; 550881824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 550981824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5510dd6ea824SBarry Smith MatScalar *aa = a->a; 5511ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 551281824310SBarry Smith Mat B = aij->B; 551381824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5514d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5515dd6ea824SBarry Smith MatScalar *ba = b->a; 551681824310SBarry Smith 551781824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 551881824310SBarry Smith PetscInt nonew = a->nonew; 5519dd6ea824SBarry Smith MatScalar *ap1,*ap2; 552081824310SBarry Smith 552181824310SBarry Smith PetscFunctionBegin; 552281824310SBarry Smith for (i=0; i<m; i++) { 552381824310SBarry Smith if (im[i] < 0) continue; 552481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5525e32f2f54SBarry 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); 552681824310SBarry Smith #endif 552781824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 552881824310SBarry Smith row = im[i] - rstart; 552981824310SBarry Smith lastcol1 = -1; 553081824310SBarry Smith rp1 = aj + ai[row]; 553181824310SBarry Smith ap1 = aa + ai[row]; 553281824310SBarry Smith rmax1 = aimax[row]; 553381824310SBarry Smith nrow1 = ailen[row]; 553481824310SBarry Smith low1 = 0; 553581824310SBarry Smith high1 = nrow1; 553681824310SBarry Smith lastcol2 = -1; 553781824310SBarry Smith rp2 = bj + bi[row]; 553881824310SBarry Smith ap2 = ba + bi[row]; 553981824310SBarry Smith rmax2 = bimax[row]; 554081824310SBarry Smith nrow2 = bilen[row]; 554181824310SBarry Smith low2 = 0; 554281824310SBarry Smith high2 = nrow2; 554381824310SBarry Smith 554481824310SBarry Smith for (j=0; j<n; j++) { 554581824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 554681824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 554781824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 554881824310SBarry Smith col = in[j] - cstart; 554981824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 555081824310SBarry Smith } else if (in[j] < 0) continue; 555181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5552cb9801acSJed 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); 555381824310SBarry Smith #endif 555481824310SBarry Smith else { 555581824310SBarry Smith if (mat->was_assembled) { 555681824310SBarry Smith if (!aij->colmap) { 555781824310SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 555881824310SBarry Smith } 555981824310SBarry Smith #if defined (PETSC_USE_CTABLE) 556081824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 556181824310SBarry Smith col--; 556281824310SBarry Smith #else 556381824310SBarry Smith col = aij->colmap[in[j]] - 1; 556481824310SBarry Smith #endif 556581824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 556681824310SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 556781824310SBarry Smith col = in[j]; 556881824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 556981824310SBarry Smith B = aij->B; 557081824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 557181824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 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; 5578d0f46423SBarry Smith bm = aij->B->rmap->n; 557981824310SBarry Smith ba = b->a; 558081824310SBarry Smith } 558181824310SBarry Smith } else col = in[j]; 558281824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 558381824310SBarry Smith } 558481824310SBarry Smith } 558581824310SBarry Smith } else { 558681824310SBarry Smith if (!aij->donotstash) { 558781824310SBarry Smith if (roworiented) { 5588ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 558981824310SBarry Smith } else { 5590ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 559181824310SBarry Smith } 559281824310SBarry Smith } 559381824310SBarry Smith } 559481824310SBarry Smith }} 559581824310SBarry Smith PetscFunctionReturnVoid(); 559681824310SBarry Smith } 559781824310SBarry Smith EXTERN_C_END 559803bfb495SBarry Smith 5599