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); 680564f14d6SBarry Smith for (i=0; i<slen; 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__ 7279c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 7289c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7299c7c4993SBarry Smith { 7309c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 7319c7c4993SBarry Smith PetscErrorCode ierr; 7329c7c4993SBarry Smith PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 7339c7c4993SBarry Smith PetscInt i,*owners = A->rmap->range; 734564f14d6SBarry Smith PetscInt *nprocs,j,idx,nsends; 7359c7c4993SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 7369c7c4993SBarry Smith PetscInt *rvalues,count,base,slen,*source; 737564f14d6SBarry Smith PetscInt *lens,*lrows,*values,m; 7389c7c4993SBarry Smith MPI_Comm comm = ((PetscObject)A)->comm; 7399c7c4993SBarry Smith MPI_Request *send_waits,*recv_waits; 7409c7c4993SBarry Smith MPI_Status recv_status,*send_status; 7419c7c4993SBarry Smith const PetscScalar *xx; 742564f14d6SBarry Smith PetscScalar *bb,*mask; 743564f14d6SBarry Smith Vec xmask,lmask; 744564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 745564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 746564f14d6SBarry Smith PetscScalar *aa; 7479c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7489c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 7499c7c4993SBarry Smith #endif 7509c7c4993SBarry Smith 7519c7c4993SBarry Smith PetscFunctionBegin; 7529c7c4993SBarry Smith /* first count number of contributors to each processor */ 7539c7c4993SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 7549c7c4993SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 7559c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 7569c7c4993SBarry Smith j = 0; 7579c7c4993SBarry Smith for (i=0; i<N; i++) { 7589c7c4993SBarry Smith if (lastidx > (idx = rows[i])) j = 0; 7599c7c4993SBarry Smith lastidx = idx; 7609c7c4993SBarry Smith for (; j<size; j++) { 7619c7c4993SBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 7629c7c4993SBarry Smith nprocs[2*j]++; 7639c7c4993SBarry Smith nprocs[2*j+1] = 1; 7649c7c4993SBarry Smith owner[i] = j; 7659c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7669c7c4993SBarry Smith found = PETSC_TRUE; 7679c7c4993SBarry Smith #endif 7689c7c4993SBarry Smith break; 7699c7c4993SBarry Smith } 7709c7c4993SBarry Smith } 7719c7c4993SBarry Smith #if defined(PETSC_DEBUG) 7729c7c4993SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 7739c7c4993SBarry Smith found = PETSC_FALSE; 7749c7c4993SBarry Smith #endif 7759c7c4993SBarry Smith } 7769c7c4993SBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 7779c7c4993SBarry Smith 7789c7c4993SBarry Smith /* inform other processors of number of messages and max length*/ 7799c7c4993SBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 7809c7c4993SBarry Smith 7819c7c4993SBarry Smith /* post receives: */ 7829c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 7839c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 7849c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 7859c7c4993SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 7869c7c4993SBarry Smith } 7879c7c4993SBarry Smith 7889c7c4993SBarry Smith /* do sends: 7899c7c4993SBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 7909c7c4993SBarry Smith the ith processor 7919c7c4993SBarry Smith */ 7929c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 7939c7c4993SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 7949c7c4993SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 7959c7c4993SBarry Smith starts[0] = 0; 7969c7c4993SBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 7979c7c4993SBarry Smith for (i=0; i<N; i++) { 7989c7c4993SBarry Smith svalues[starts[owner[i]]++] = rows[i]; 7999c7c4993SBarry Smith } 8009c7c4993SBarry Smith 8019c7c4993SBarry Smith starts[0] = 0; 8029c7c4993SBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 8039c7c4993SBarry Smith count = 0; 8049c7c4993SBarry Smith for (i=0; i<size; i++) { 8059c7c4993SBarry Smith if (nprocs[2*i+1]) { 8069c7c4993SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 8079c7c4993SBarry Smith } 8089c7c4993SBarry Smith } 8099c7c4993SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 8109c7c4993SBarry Smith 8119c7c4993SBarry Smith base = owners[rank]; 8129c7c4993SBarry Smith 8139c7c4993SBarry Smith /* wait on receives */ 8149c7c4993SBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 8159c7c4993SBarry Smith count = nrecvs; slen = 0; 8169c7c4993SBarry Smith while (count) { 8179c7c4993SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 8189c7c4993SBarry Smith /* unpack receives into our local space */ 8199c7c4993SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 8209c7c4993SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 8219c7c4993SBarry Smith lens[imdex] = n; 8229c7c4993SBarry Smith slen += n; 8239c7c4993SBarry Smith count--; 8249c7c4993SBarry Smith } 8259c7c4993SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 8269c7c4993SBarry Smith 8279c7c4993SBarry Smith /* move the data into the send scatter */ 8289c7c4993SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 8299c7c4993SBarry Smith count = 0; 8309c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 8319c7c4993SBarry Smith values = rvalues + i*nmax; 8329c7c4993SBarry Smith for (j=0; j<lens[i]; j++) { 8339c7c4993SBarry Smith lrows[count++] = values[j] - base; 8349c7c4993SBarry Smith } 8359c7c4993SBarry Smith } 8369c7c4993SBarry Smith ierr = PetscFree(rvalues);CHKERRQ(ierr); 8379c7c4993SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 8389c7c4993SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 8399c7c4993SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 840564f14d6SBarry Smith /* lrows are the local rows to be zeroed, slen is the number of local rows */ 8419c7c4993SBarry Smith 842564f14d6SBarry Smith /* zero diagonal part of matrix */ 843564f14d6SBarry Smith ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr); 8449c7c4993SBarry Smith 845564f14d6SBarry Smith /* handle off diagonal part of matrix */ 846564f14d6SBarry Smith ierr = MatGetVecs(A,&xmask,PETSC_NULL);CHKERRQ(ierr); 847564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 848564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 8499c7c4993SBarry Smith for (i=0; i<slen; i++) { 850564f14d6SBarry Smith bb[lrows[i]] = 1; 8519c7c4993SBarry Smith } 852564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 853564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 854564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 855564f14d6SBarry Smith ierr = VecDestroy(xmask);CHKERRQ(ierr); 856564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 857564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 858564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 859564f14d6SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 860564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 861564f14d6SBarry Smith 862564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 863564f14d6SBarry Smith ii = aij->i; 864564f14d6SBarry Smith for (i=0; i<slen; i++) { 865564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 8669c7c4993SBarry Smith } 867564f14d6SBarry Smith 868564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 869564f14d6SBarry Smith if (aij->compressedrow.use){ 870564f14d6SBarry Smith m = aij->compressedrow.nrows; 871564f14d6SBarry Smith ii = aij->compressedrow.i; 872564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 873564f14d6SBarry Smith for (i=0; i<m; i++){ 874564f14d6SBarry Smith n = ii[i+1] - ii[i]; 875564f14d6SBarry Smith aj = aij->j + ii[i]; 876564f14d6SBarry Smith aa = aij->a + ii[i]; 877564f14d6SBarry Smith 878564f14d6SBarry Smith for (j=0; j<n; j++) { 87925266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 880564f14d6SBarry Smith bb[*ridx] -= *aa*xx[*aj]; 881564f14d6SBarry Smith *aa = 0.0; 882564f14d6SBarry Smith } 883564f14d6SBarry Smith aa++; 884564f14d6SBarry Smith aj++; 885564f14d6SBarry Smith } 886564f14d6SBarry Smith ridx++; 887564f14d6SBarry Smith } 888564f14d6SBarry Smith } else { /* do not use compressed row format */ 889564f14d6SBarry Smith m = l->B->rmap->n; 890564f14d6SBarry Smith for (i=0; i<m; i++) { 891564f14d6SBarry Smith n = ii[i+1] - ii[i]; 892564f14d6SBarry Smith aj = aij->j + ii[i]; 893564f14d6SBarry Smith aa = aij->a + ii[i]; 894564f14d6SBarry Smith for (j=0; j<n; j++) { 89525266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 896564f14d6SBarry Smith bb[i] -= *aa*xx[*aj]; 897564f14d6SBarry Smith *aa = 0.0; 898564f14d6SBarry Smith } 899564f14d6SBarry Smith aa++; 900564f14d6SBarry Smith aj++; 901564f14d6SBarry Smith } 902564f14d6SBarry Smith } 903564f14d6SBarry Smith } 904564f14d6SBarry Smith 905564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 906564f14d6SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 907564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 908564f14d6SBarry Smith ierr = VecDestroy(lmask);CHKERRQ(ierr); 9099c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 9109c7c4993SBarry Smith 9119c7c4993SBarry Smith /* wait on sends */ 9129c7c4993SBarry Smith if (nsends) { 9139c7c4993SBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 9149c7c4993SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 9159c7c4993SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 9169c7c4993SBarry Smith } 9179c7c4993SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 9189c7c4993SBarry Smith ierr = PetscFree(svalues);CHKERRQ(ierr); 9199c7c4993SBarry Smith 9209c7c4993SBarry Smith PetscFunctionReturn(0); 9219c7c4993SBarry Smith } 9229c7c4993SBarry Smith 9239c7c4993SBarry Smith #undef __FUNCT__ 9244a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 925dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 9261eb62cbbSBarry Smith { 927416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 928dfbe8321SBarry Smith PetscErrorCode ierr; 929b1d57f15SBarry Smith PetscInt nt; 930416022c9SBarry Smith 9313a40ed3dSBarry Smith PetscFunctionBegin; 932a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 93365e19b50SBarry 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); 934ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 935f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 936ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 937f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 9383a40ed3dSBarry Smith PetscFunctionReturn(0); 9391eb62cbbSBarry Smith } 9401eb62cbbSBarry Smith 9414a2ae208SSatish Balay #undef __FUNCT__ 942bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 943bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 944bd0c2dcbSBarry Smith { 945bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 946bd0c2dcbSBarry Smith PetscErrorCode ierr; 947bd0c2dcbSBarry Smith 948bd0c2dcbSBarry Smith PetscFunctionBegin; 949bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 950bd0c2dcbSBarry Smith PetscFunctionReturn(0); 951bd0c2dcbSBarry Smith } 952bd0c2dcbSBarry Smith 953bd0c2dcbSBarry Smith #undef __FUNCT__ 9544a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 955dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 956da3a660dSBarry Smith { 957416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 958dfbe8321SBarry Smith PetscErrorCode ierr; 9593a40ed3dSBarry Smith 9603a40ed3dSBarry Smith PetscFunctionBegin; 961ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 962f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 963ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 964f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 9653a40ed3dSBarry Smith PetscFunctionReturn(0); 966da3a660dSBarry Smith } 967da3a660dSBarry Smith 9684a2ae208SSatish Balay #undef __FUNCT__ 9694a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 970dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 971da3a660dSBarry Smith { 972416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 973dfbe8321SBarry Smith PetscErrorCode ierr; 974ace3abfcSBarry Smith PetscBool merged; 975da3a660dSBarry Smith 9763a40ed3dSBarry Smith PetscFunctionBegin; 977a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 978da3a660dSBarry Smith /* do nondiagonal part */ 9797c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 980a5ff213dSBarry Smith if (!merged) { 981da3a660dSBarry Smith /* send it on its way */ 982ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 983da3a660dSBarry Smith /* do local part */ 9847c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 985da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 986a5ff213dSBarry Smith /* added in yy until the next line, */ 987ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 988a5ff213dSBarry Smith } else { 989a5ff213dSBarry Smith /* do local part */ 990a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 991a5ff213dSBarry Smith /* send it on its way */ 992ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 993a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 994ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 995a5ff213dSBarry Smith } 9963a40ed3dSBarry Smith PetscFunctionReturn(0); 997da3a660dSBarry Smith } 998da3a660dSBarry Smith 999cd0d46ebSvictorle EXTERN_C_BEGIN 1000cd0d46ebSvictorle #undef __FUNCT__ 10015fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 1002ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1003cd0d46ebSvictorle { 10044f423910Svictorle MPI_Comm comm; 1005cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 100666501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1007cd0d46ebSvictorle IS Me,Notme; 10086849ba73SBarry Smith PetscErrorCode ierr; 1009b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1010b1d57f15SBarry Smith PetscMPIInt size; 1011cd0d46ebSvictorle 1012cd0d46ebSvictorle PetscFunctionBegin; 101342e5f5b4Svictorle 101442e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 101566501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 10165485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1017cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 10184f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1019b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1020b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 102142e5f5b4Svictorle 102242e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1023cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1024cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1025b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 1026cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1027cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 102870b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1029268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1030268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 103166501d38Svictorle Aoff = Aoffs[0]; 1032268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 103366501d38Svictorle Boff = Boffs[0]; 10345485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 103566501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 103666501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 103742e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 103842e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 103942e5f5b4Svictorle 1040cd0d46ebSvictorle PetscFunctionReturn(0); 1041cd0d46ebSvictorle } 1042cd0d46ebSvictorle EXTERN_C_END 1043cd0d46ebSvictorle 10444a2ae208SSatish Balay #undef __FUNCT__ 10454a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1046dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1047da3a660dSBarry Smith { 1048416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1049dfbe8321SBarry Smith PetscErrorCode ierr; 1050da3a660dSBarry Smith 10513a40ed3dSBarry Smith PetscFunctionBegin; 1052da3a660dSBarry Smith /* do nondiagonal part */ 10537c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1054da3a660dSBarry Smith /* send it on its way */ 1055ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1056da3a660dSBarry Smith /* do local part */ 10577c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1058a5ff213dSBarry Smith /* receive remote parts */ 1059ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 10603a40ed3dSBarry Smith PetscFunctionReturn(0); 1061da3a660dSBarry Smith } 1062da3a660dSBarry Smith 10631eb62cbbSBarry Smith /* 10641eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 10651eb62cbbSBarry Smith diagonal block 10661eb62cbbSBarry Smith */ 10674a2ae208SSatish Balay #undef __FUNCT__ 10684a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1069dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 10701eb62cbbSBarry Smith { 1071dfbe8321SBarry Smith PetscErrorCode ierr; 1072416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 10733a40ed3dSBarry Smith 10743a40ed3dSBarry Smith PetscFunctionBegin; 1075e7e72b3dSBarry 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"); 1076e7e72b3dSBarry 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"); 10773a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 10783a40ed3dSBarry Smith PetscFunctionReturn(0); 10791eb62cbbSBarry Smith } 10801eb62cbbSBarry Smith 10814a2ae208SSatish Balay #undef __FUNCT__ 10824a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1083f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1084052efed2SBarry Smith { 1085052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1086dfbe8321SBarry Smith PetscErrorCode ierr; 10873a40ed3dSBarry Smith 10883a40ed3dSBarry Smith PetscFunctionBegin; 1089f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1090f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 10913a40ed3dSBarry Smith PetscFunctionReturn(0); 1092052efed2SBarry Smith } 1093052efed2SBarry Smith 10944a2ae208SSatish Balay #undef __FUNCT__ 10954a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1096dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 10971eb62cbbSBarry Smith { 109844a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1099dfbe8321SBarry Smith PetscErrorCode ierr; 110083e2fdc7SBarry Smith 11013a40ed3dSBarry Smith PetscFunctionBegin; 1102aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1103d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1104a5a9c739SBarry Smith #endif 11058798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 1106a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);} 1107d88c0aacSHong Zhang if (aij->A){ierr = MatDestroy(aij->A);CHKERRQ(ierr);} 1108d88c0aacSHong Zhang if (aij->B){ierr = MatDestroy(aij->B);CHKERRQ(ierr);} 1109aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 11109c666560SBarry Smith if (aij->colmap) {ierr = PetscTableDestroy(aij->colmap);CHKERRQ(ierr);} 1111b1fc9764SSatish Balay #else 111205b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1113b1fc9764SSatish Balay #endif 111405b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 11157c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 11167c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 111703095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 11188aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1119606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 1120901853e0SKris Buschelman 1121dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1122901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 1123901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 1124901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 1125901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 1126901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 1127ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 1128901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 1129471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 11303a40ed3dSBarry Smith PetscFunctionReturn(0); 11311eb62cbbSBarry Smith } 1132ee50ffe9SBarry Smith 11334a2ae208SSatish Balay #undef __FUNCT__ 11348e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1135dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 11368e2fed03SBarry Smith { 11378e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 11388e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 11398e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 11406849ba73SBarry Smith PetscErrorCode ierr; 114132dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 11426f69ff64SBarry Smith int fd; 1143a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1144d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 11458e2fed03SBarry Smith PetscScalar *column_values; 114685ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 11478e2fed03SBarry Smith 11488e2fed03SBarry Smith PetscFunctionBegin; 11497adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 11507adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 11518e2fed03SBarry Smith nz = A->nz + B->nz; 1152958c9bccSBarry Smith if (!rank) { 11530700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1154d0f46423SBarry Smith header[1] = mat->rmap->N; 1155d0f46423SBarry Smith header[2] = mat->cmap->N; 11567adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 11578e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 11586f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11598e2fed03SBarry Smith /* get largest number of rows any processor has */ 1160d0f46423SBarry Smith rlen = mat->rmap->n; 1161d0f46423SBarry Smith range = mat->rmap->range; 11628e2fed03SBarry Smith for (i=1; i<size; i++) { 11638e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 11648e2fed03SBarry Smith } 11658e2fed03SBarry Smith } else { 11667adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1167d0f46423SBarry Smith rlen = mat->rmap->n; 11688e2fed03SBarry Smith } 11698e2fed03SBarry Smith 11708e2fed03SBarry Smith /* load up the local row counts */ 1171b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 1172d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 11738e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 11748e2fed03SBarry Smith } 11758e2fed03SBarry Smith 11768e2fed03SBarry Smith /* store the row lengths to the file */ 117785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1178958c9bccSBarry Smith if (!rank) { 11798e2fed03SBarry Smith MPI_Status status; 1180d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11818e2fed03SBarry Smith for (i=1; i<size; i++) { 118285ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 11838e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1184a1319256SJed Brown ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 11856f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11868e2fed03SBarry Smith } 118785ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 11888e2fed03SBarry Smith } else { 118985ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1190d0f46423SBarry Smith ierr = MPI_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 119185ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 11928e2fed03SBarry Smith } 11938e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 11948e2fed03SBarry Smith 11958e2fed03SBarry Smith /* load up the local column indices */ 11968e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 11977adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1198b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 11998e2fed03SBarry Smith cnt = 0; 1200d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12018e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12028e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 12038e2fed03SBarry Smith column_indices[cnt++] = col; 12048e2fed03SBarry Smith } 12058e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 12068e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 12078e2fed03SBarry Smith } 12088e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 12098e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 12108e2fed03SBarry Smith } 12118e2fed03SBarry Smith } 1212e32f2f54SBarry 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); 12138e2fed03SBarry Smith 12148e2fed03SBarry Smith /* store the column indices to the file */ 121585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1216958c9bccSBarry Smith if (!rank) { 12178e2fed03SBarry Smith MPI_Status status; 12186f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12198e2fed03SBarry Smith for (i=1; i<size; i++) { 122085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 12217adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1222e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 12237adad957SLisandro Dalcin ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 12246f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12258e2fed03SBarry Smith } 122685ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 12278e2fed03SBarry Smith } else { 122885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 12297adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12307adad957SLisandro Dalcin ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 123185ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12328e2fed03SBarry Smith } 12338e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 12348e2fed03SBarry Smith 12358e2fed03SBarry Smith /* load up the local column values */ 12368e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 12378e2fed03SBarry Smith cnt = 0; 1238d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12398e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12408e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 12418e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12428e2fed03SBarry Smith } 12438e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 12448e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 12458e2fed03SBarry Smith } 12468e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 12478e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12488e2fed03SBarry Smith } 12498e2fed03SBarry Smith } 1250e32f2f54SBarry 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); 12518e2fed03SBarry Smith 12528e2fed03SBarry Smith /* store the column values to the file */ 125385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1254958c9bccSBarry Smith if (!rank) { 12558e2fed03SBarry Smith MPI_Status status; 12566f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12578e2fed03SBarry Smith for (i=1; i<size; i++) { 125885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 12597adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1260e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 12617adad957SLisandro Dalcin ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 12626f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12638e2fed03SBarry Smith } 126485ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 12658e2fed03SBarry Smith } else { 126685ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 12677adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 12687adad957SLisandro Dalcin ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 126985ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 12708e2fed03SBarry Smith } 12718e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 12728e2fed03SBarry Smith PetscFunctionReturn(0); 12738e2fed03SBarry Smith } 12748e2fed03SBarry Smith 12758e2fed03SBarry Smith #undef __FUNCT__ 12764a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1277dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1278416022c9SBarry Smith { 127944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1280dfbe8321SBarry Smith PetscErrorCode ierr; 128132dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1282ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1283b0a32e0cSBarry Smith PetscViewer sviewer; 1284f3ef73ceSBarry Smith PetscViewerFormat format; 1285416022c9SBarry Smith 12863a40ed3dSBarry Smith PetscFunctionBegin; 12872692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 12882692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 12892692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 129032077d6dSBarry Smith if (iascii) { 1291b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1292456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 12934e220ebcSLois Curfman McInnes MatInfo info; 1294ace3abfcSBarry Smith PetscBool inodes; 1295923f20ffSKris Buschelman 12967adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1297888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1298923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 1299923f20ffSKris Buschelman if (!inodes) { 130077431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1301d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13026831982aSBarry Smith } else { 130377431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1304d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13056831982aSBarry Smith } 1306888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 130777431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1308888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 130977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1310b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 131107d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1312a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 13133a40ed3dSBarry Smith PetscFunctionReturn(0); 1314fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1315923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1316923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1317923f20ffSKris Buschelman if (inodes) { 1318923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1319d38fa0fbSBarry Smith } else { 1320d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1321d38fa0fbSBarry Smith } 13223a40ed3dSBarry Smith PetscFunctionReturn(0); 13234aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 13244aedb280SBarry Smith PetscFunctionReturn(0); 132508480c60SBarry Smith } 13268e2fed03SBarry Smith } else if (isbinary) { 13278e2fed03SBarry Smith if (size == 1) { 13287adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 13298e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13308e2fed03SBarry Smith } else { 13318e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 13328e2fed03SBarry Smith } 13338e2fed03SBarry Smith PetscFunctionReturn(0); 13340f5bd95cSBarry Smith } else if (isdraw) { 1335b0a32e0cSBarry Smith PetscDraw draw; 1336ace3abfcSBarry Smith PetscBool isnull; 1337b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1338b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 133919bcc07fSBarry Smith } 134019bcc07fSBarry Smith 134117699dbbSLois Curfman McInnes if (size == 1) { 13427adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 134378b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13443a40ed3dSBarry Smith } else { 134595373324SBarry Smith /* assemble the entire matrix onto first processor. */ 134695373324SBarry Smith Mat A; 1347ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1348d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1349dd6ea824SBarry Smith MatScalar *a; 13502ee70a88SLois Curfman McInnes 135132a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1352ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 135332a366e4SMatthew Knepley 1354acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 135532a366e4SMatthew Knepley if (!flg) { 1356e7e72b3dSBarry 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."); 135732a366e4SMatthew Knepley } 135832a366e4SMatthew Knepley } 13590805154bSBarry Smith 13607adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 136117699dbbSLois Curfman McInnes if (!rank) { 1362f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 13633a40ed3dSBarry Smith } else { 1364f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 136595373324SBarry Smith } 1366f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1367f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1368f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 136952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1370416022c9SBarry Smith 137195373324SBarry Smith /* copy over the A part */ 1372ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1373d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1374d0f46423SBarry Smith row = mat->rmap->rstart; 1375d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 137695373324SBarry Smith for (i=0; i<m; i++) { 1377416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 137895373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 137995373324SBarry Smith } 13802ee70a88SLois Curfman McInnes aj = Aloc->j; 1381d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 138295373324SBarry Smith 138395373324SBarry Smith /* copy over the B part */ 1384ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1385d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1386d0f46423SBarry Smith row = mat->rmap->rstart; 1387b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1388b0a32e0cSBarry Smith ct = cols; 1389bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 139095373324SBarry Smith for (i=0; i<m; i++) { 1391416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 139295373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 139395373324SBarry Smith } 1394606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 13956d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13966d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 139755843e3eSBarry Smith /* 139855843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1399b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 140055843e3eSBarry Smith */ 1401b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1402e03a110bSBarry Smith if (!rank) { 14037adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 14047566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 14057566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 14066831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 140795373324SBarry Smith } 1408b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 140978b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 141095373324SBarry Smith } 14113a40ed3dSBarry Smith PetscFunctionReturn(0); 14121eb62cbbSBarry Smith } 14131eb62cbbSBarry Smith 14144a2ae208SSatish Balay #undef __FUNCT__ 14154a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1416dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1417416022c9SBarry Smith { 1418dfbe8321SBarry Smith PetscErrorCode ierr; 1419ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1420416022c9SBarry Smith 14213a40ed3dSBarry Smith PetscFunctionBegin; 14222692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 14232692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 14242692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 14252692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 142632077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 14277b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 14285cd90555SBarry Smith } else { 1429e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1430416022c9SBarry Smith } 14313a40ed3dSBarry Smith PetscFunctionReturn(0); 1432416022c9SBarry Smith } 1433416022c9SBarry Smith 14344a2ae208SSatish Balay #undef __FUNCT__ 143541f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 143641f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 14378a729477SBarry Smith { 143844a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1439dfbe8321SBarry Smith PetscErrorCode ierr; 14406987fefcSBarry Smith Vec bb1 = 0; 1441ace3abfcSBarry Smith PetscBool hasop; 14428a729477SBarry Smith 14433a40ed3dSBarry Smith PetscFunctionBegin; 144485911e72SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 144585911e72SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 144685911e72SJed Brown } 14472798e883SHong Zhang 1448a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 144941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1450a2b30743SBarry Smith PetscFunctionReturn(0); 1451a2b30743SBarry Smith } 1452a2b30743SBarry Smith 1453c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1454da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 145541f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14562798e883SHong Zhang its--; 1457da3a660dSBarry Smith } 14582798e883SHong Zhang 14592798e883SHong Zhang while (its--) { 1460ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1461ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14622798e883SHong Zhang 1463c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1464efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1465c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14662798e883SHong Zhang 1467c14dc6b6SHong Zhang /* local sweep */ 146841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14692798e883SHong Zhang } 14703a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1471da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 147241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14732798e883SHong Zhang its--; 1474da3a660dSBarry Smith } 14752798e883SHong Zhang while (its--) { 1476ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1477ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14782798e883SHong Zhang 1479c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1480efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1481c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1482c14dc6b6SHong Zhang 1483c14dc6b6SHong Zhang /* local sweep */ 148441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14852798e883SHong Zhang } 14863a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1487da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 148841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14892798e883SHong Zhang its--; 1490da3a660dSBarry Smith } 14912798e883SHong Zhang while (its--) { 1492ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1493ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14942798e883SHong Zhang 1495c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1496efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1497c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14982798e883SHong Zhang 1499c14dc6b6SHong Zhang /* local sweep */ 150041f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15012798e883SHong Zhang } 1502a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1503a7420bb7SBarry Smith Vec xx1; 1504a7420bb7SBarry Smith 1505a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 150641f059aeSBarry 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); 1507a7420bb7SBarry Smith 1508a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1509a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1510a7420bb7SBarry Smith if (!mat->diag) { 1511a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1512a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1513a7420bb7SBarry Smith } 1514bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1515bd0c2dcbSBarry Smith if (hasop) { 1516bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1517bd0c2dcbSBarry Smith } else { 1518a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1519bd0c2dcbSBarry Smith } 1520887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1521887ee2caSBarry Smith 1522a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1523a7420bb7SBarry Smith 1524a7420bb7SBarry Smith /* local sweep */ 152541f059aeSBarry 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); 1526a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 1527a7420bb7SBarry Smith ierr = VecDestroy(xx1);CHKERRQ(ierr); 15283a40ed3dSBarry Smith } else { 1529e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1530c16cb8f2SBarry Smith } 1531c14dc6b6SHong Zhang 15326987fefcSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 15333a40ed3dSBarry Smith PetscFunctionReturn(0); 15348a729477SBarry Smith } 1535a66be287SLois Curfman McInnes 15364a2ae208SSatish Balay #undef __FUNCT__ 153742e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 153842e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 153942e855d1Svictor { 154042e855d1Svictor MPI_Comm comm,pcomm; 15415d0c19d7SBarry Smith PetscInt first,local_size,nrows; 15425d0c19d7SBarry Smith const PetscInt *rows; 1543dbf0e21dSBarry Smith PetscMPIInt size; 154442e855d1Svictor IS crowp,growp,irowp,lrowp,lcolp,icolp; 154542e855d1Svictor PetscErrorCode ierr; 154642e855d1Svictor 154742e855d1Svictor PetscFunctionBegin; 154842e855d1Svictor ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 154942e855d1Svictor /* make a collective version of 'rowp' */ 155042e855d1Svictor ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 155142e855d1Svictor if (pcomm==comm) { 155242e855d1Svictor crowp = rowp; 155342e855d1Svictor } else { 155442e855d1Svictor ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 155542e855d1Svictor ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 155670b3c8c7SBarry Smith ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr); 155742e855d1Svictor ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 155842e855d1Svictor } 155942e855d1Svictor /* collect the global row permutation and invert it */ 156042e855d1Svictor ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 156142e855d1Svictor ierr = ISSetPermutation(growp);CHKERRQ(ierr); 156242e855d1Svictor if (pcomm!=comm) { 156342e855d1Svictor ierr = ISDestroy(crowp);CHKERRQ(ierr); 156442e855d1Svictor } 156542e855d1Svictor ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 156642e855d1Svictor /* get the local target indices */ 156742e855d1Svictor ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 156842e855d1Svictor ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 156942e855d1Svictor ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 157070b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr); 157142e855d1Svictor ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 157242e855d1Svictor ierr = ISDestroy(irowp);CHKERRQ(ierr); 157342e855d1Svictor /* the column permutation is so much easier; 157442e855d1Svictor make a local version of 'colp' and invert it */ 157542e855d1Svictor ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 1576dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 1577dbf0e21dSBarry Smith if (size==1) { 157842e855d1Svictor lcolp = colp; 157942e855d1Svictor } else { 158042e855d1Svictor ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 158142e855d1Svictor ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 158270b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr); 158342e855d1Svictor } 1584dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 158542e855d1Svictor ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 15864aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 1587dbf0e21dSBarry Smith if (size>1) { 158842e855d1Svictor ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 158942e855d1Svictor ierr = ISDestroy(lcolp);CHKERRQ(ierr); 159042e855d1Svictor } 159142e855d1Svictor /* now we just get the submatrix */ 15924aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 159342e855d1Svictor /* clean up */ 159442e855d1Svictor ierr = ISDestroy(lrowp);CHKERRQ(ierr); 159542e855d1Svictor ierr = ISDestroy(icolp);CHKERRQ(ierr); 159642e855d1Svictor PetscFunctionReturn(0); 159742e855d1Svictor } 159842e855d1Svictor 159942e855d1Svictor #undef __FUNCT__ 16004a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1601dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1602a66be287SLois Curfman McInnes { 1603a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1604a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1605dfbe8321SBarry Smith PetscErrorCode ierr; 1606329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1607a66be287SLois Curfman McInnes 16083a40ed3dSBarry Smith PetscFunctionBegin; 16094e220ebcSLois Curfman McInnes info->block_size = 1.0; 16104e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 16114e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 16124e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 16134e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 16144e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 16154e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1616a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 16174e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 16184e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 16194e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 16204e220ebcSLois Curfman McInnes info->memory = isend[3]; 16214e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1622a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 16237adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 16244e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16254e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16264e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16274e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16284e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1629a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 16307adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 16314e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16324e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16334e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16344e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16354e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1636a66be287SLois Curfman McInnes } 16374e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 16384e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 16394e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 16404e220ebcSLois Curfman McInnes 16413a40ed3dSBarry Smith PetscFunctionReturn(0); 1642a66be287SLois Curfman McInnes } 1643a66be287SLois Curfman McInnes 16444a2ae208SSatish Balay #undef __FUNCT__ 16454a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1646ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1647c74985f6SBarry Smith { 1648c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1649dfbe8321SBarry Smith PetscErrorCode ierr; 1650c74985f6SBarry Smith 16513a40ed3dSBarry Smith PetscFunctionBegin; 165212c028f9SKris Buschelman switch (op) { 1653512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 165412c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 165528b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1656a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 165712c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 165812c028f9SKris Buschelman case MAT_USE_INODES: 165912c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 16604e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16614e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 166212c028f9SKris Buschelman break; 166312c028f9SKris Buschelman case MAT_ROW_ORIENTED: 16644e0d8c25SBarry Smith a->roworiented = flg; 16654e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 16664e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 166712c028f9SKris Buschelman break; 16684e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1669290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 167012c028f9SKris Buschelman break; 167112c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 16727c922b88SBarry Smith a->donotstash = PETSC_TRUE; 167312c028f9SKris Buschelman break; 1674ffa07934SHong Zhang case MAT_SPD: 1675ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1676ffa07934SHong Zhang A->spd = flg; 1677ffa07934SHong Zhang if (flg) { 1678ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1679ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1680ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1681ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1682ffa07934SHong Zhang } 1683ffa07934SHong Zhang break; 168477e54ba9SKris Buschelman case MAT_SYMMETRIC: 16854e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 168625f421beSHong Zhang break; 168777e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1688eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1689eeffb40dSHong Zhang break; 1690bf108f30SBarry Smith case MAT_HERMITIAN: 1691eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1692eeffb40dSHong Zhang break; 1693bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 16944e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 169577e54ba9SKris Buschelman break; 169612c028f9SKris Buschelman default: 1697e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 16983a40ed3dSBarry Smith } 16993a40ed3dSBarry Smith PetscFunctionReturn(0); 1700c74985f6SBarry Smith } 1701c74985f6SBarry Smith 17024a2ae208SSatish Balay #undef __FUNCT__ 17034a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1704b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 170539e00950SLois Curfman McInnes { 1706154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 170787828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 17086849ba73SBarry Smith PetscErrorCode ierr; 1709d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1710d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1711b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 171239e00950SLois Curfman McInnes 17133a40ed3dSBarry Smith PetscFunctionBegin; 1714e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 17157a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 17167a0afa10SBarry Smith 171770f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 17187a0afa10SBarry Smith /* 17197a0afa10SBarry Smith allocate enough space to hold information from the longest row. 17207a0afa10SBarry Smith */ 17217a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1722b1d57f15SBarry Smith PetscInt max = 1,tmp; 1723d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 17247a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 17257a0afa10SBarry Smith if (max < tmp) { max = tmp; } 17267a0afa10SBarry Smith } 17271d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 17287a0afa10SBarry Smith } 17297a0afa10SBarry Smith 1730e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1731abc0e9e4SLois Curfman McInnes lrow = row - rstart; 173239e00950SLois Curfman McInnes 1733154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1734154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1735154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1736f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1737f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1738154123eaSLois Curfman McInnes nztot = nzA + nzB; 1739154123eaSLois Curfman McInnes 174070f0671dSBarry Smith cmap = mat->garray; 1741154123eaSLois Curfman McInnes if (v || idx) { 1742154123eaSLois Curfman McInnes if (nztot) { 1743154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1744b1d57f15SBarry Smith PetscInt imark = -1; 1745154123eaSLois Curfman McInnes if (v) { 174670f0671dSBarry Smith *v = v_p = mat->rowvalues; 174739e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 174870f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1749154123eaSLois Curfman McInnes else break; 1750154123eaSLois Curfman McInnes } 1751154123eaSLois Curfman McInnes imark = i; 175270f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 175370f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1754154123eaSLois Curfman McInnes } 1755154123eaSLois Curfman McInnes if (idx) { 175670f0671dSBarry Smith *idx = idx_p = mat->rowindices; 175770f0671dSBarry Smith if (imark > -1) { 175870f0671dSBarry Smith for (i=0; i<imark; i++) { 175970f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 176070f0671dSBarry Smith } 176170f0671dSBarry Smith } else { 1762154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 176370f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1764154123eaSLois Curfman McInnes else break; 1765154123eaSLois Curfman McInnes } 1766154123eaSLois Curfman McInnes imark = i; 176770f0671dSBarry Smith } 176870f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 176970f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 177039e00950SLois Curfman McInnes } 17713f97c4b0SBarry Smith } else { 17721ca473b0SSatish Balay if (idx) *idx = 0; 17731ca473b0SSatish Balay if (v) *v = 0; 17741ca473b0SSatish Balay } 1775154123eaSLois Curfman McInnes } 177639e00950SLois Curfman McInnes *nz = nztot; 1777f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1778f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 17793a40ed3dSBarry Smith PetscFunctionReturn(0); 178039e00950SLois Curfman McInnes } 178139e00950SLois Curfman McInnes 17824a2ae208SSatish Balay #undef __FUNCT__ 17834a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1784b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 178539e00950SLois Curfman McInnes { 17867a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17873a40ed3dSBarry Smith 17883a40ed3dSBarry Smith PetscFunctionBegin; 1789e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 17907a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 17913a40ed3dSBarry Smith PetscFunctionReturn(0); 179239e00950SLois Curfman McInnes } 179339e00950SLois Curfman McInnes 17944a2ae208SSatish Balay #undef __FUNCT__ 17954a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1796dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1797855ac2c5SLois Curfman McInnes { 1798855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1799ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1800dfbe8321SBarry Smith PetscErrorCode ierr; 1801d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1802329f5518SBarry Smith PetscReal sum = 0.0; 1803a77337e4SBarry Smith MatScalar *v; 180404ca555eSLois Curfman McInnes 18053a40ed3dSBarry Smith PetscFunctionBegin; 180617699dbbSLois Curfman McInnes if (aij->size == 1) { 180714183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 180837fa93a5SLois Curfman McInnes } else { 180904ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 181004ca555eSLois Curfman McInnes v = amat->a; 181104ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1812aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1813329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 181404ca555eSLois Curfman McInnes #else 181504ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 181604ca555eSLois Curfman McInnes #endif 181704ca555eSLois Curfman McInnes } 181804ca555eSLois Curfman McInnes v = bmat->a; 181904ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1820aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1821329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 182204ca555eSLois Curfman McInnes #else 182304ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 182404ca555eSLois Curfman McInnes #endif 182504ca555eSLois Curfman McInnes } 18267adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 182704ca555eSLois Curfman McInnes *norm = sqrt(*norm); 18283a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1829329f5518SBarry Smith PetscReal *tmp,*tmp2; 1830b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 1831d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1832d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1833d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 183404ca555eSLois Curfman McInnes *norm = 0.0; 183504ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 183604ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1837bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 183804ca555eSLois Curfman McInnes } 183904ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 184004ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1841bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 184204ca555eSLois Curfman McInnes } 1843d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 1844d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 184504ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 184604ca555eSLois Curfman McInnes } 1847606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1848606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 18493a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1850329f5518SBarry Smith PetscReal ntemp = 0.0; 1851d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1852bfec09a0SHong Zhang v = amat->a + amat->i[j]; 185304ca555eSLois Curfman McInnes sum = 0.0; 185404ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1855cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 185604ca555eSLois Curfman McInnes } 1857bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 185804ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1859cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 186004ca555eSLois Curfman McInnes } 1861515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 186204ca555eSLois Curfman McInnes } 18637adad957SLisandro Dalcin ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 1864ca161407SBarry Smith } else { 1865e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 186604ca555eSLois Curfman McInnes } 186737fa93a5SLois Curfman McInnes } 18683a40ed3dSBarry Smith PetscFunctionReturn(0); 1869855ac2c5SLois Curfman McInnes } 1870855ac2c5SLois Curfman McInnes 18714a2ae208SSatish Balay #undef __FUNCT__ 18724a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1873fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1874b7c46309SBarry Smith { 1875b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1876da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1877dfbe8321SBarry Smith PetscErrorCode ierr; 1878d0f46423SBarry Smith PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz; 1879d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 18803a40ed3dSBarry Smith Mat B; 1881a77337e4SBarry Smith MatScalar *array; 1882b7c46309SBarry Smith 18833a40ed3dSBarry Smith PetscFunctionBegin; 1884e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1885da668accSHong Zhang 1886d0f46423SBarry Smith ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; 1887da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1888da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1889fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 1890fc73b1b3SBarry Smith /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */ 1891fc73b1b3SBarry Smith ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr); 1892da668accSHong Zhang ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr); 1893da668accSHong Zhang for (i=0; i<ai[ma]; i++){ 1894da668accSHong Zhang d_nnz[aj[i]] ++; 1895da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1896d4bb536fSBarry Smith } 1897d4bb536fSBarry Smith 18987adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1899d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 19007adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1901da668accSHong Zhang ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr); 1902fc73b1b3SBarry Smith ierr = PetscFree(d_nnz);CHKERRQ(ierr); 1903fc4dec0aSBarry Smith } else { 1904fc4dec0aSBarry Smith B = *matout; 1905fc4dec0aSBarry Smith } 1906b7c46309SBarry Smith 1907b7c46309SBarry Smith /* copy over the A part */ 1908da668accSHong Zhang array = Aloc->a; 1909d0f46423SBarry Smith row = A->rmap->rstart; 1910da668accSHong Zhang for (i=0; i<ma; i++) { 1911da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1912da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1913da668accSHong Zhang row++; array += ncol; aj += ncol; 1914b7c46309SBarry Smith } 1915b7c46309SBarry Smith aj = Aloc->j; 1916da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1917b7c46309SBarry Smith 1918b7c46309SBarry Smith /* copy over the B part */ 1919fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1920fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 1921da668accSHong Zhang array = Bloc->a; 1922d0f46423SBarry Smith row = A->rmap->rstart; 1923da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 192461a2fbbaSHong Zhang cols_tmp = cols; 1925da668accSHong Zhang for (i=0; i<mb; i++) { 1926da668accSHong Zhang ncol = bi[i+1]-bi[i]; 192761a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 192861a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 1929b7c46309SBarry Smith } 1930fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1931fc73b1b3SBarry Smith 19326d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19336d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1934815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 19350de55854SLois Curfman McInnes *matout = B; 19360de55854SLois Curfman McInnes } else { 1937eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 19380de55854SLois Curfman McInnes } 19393a40ed3dSBarry Smith PetscFunctionReturn(0); 1940b7c46309SBarry Smith } 1941b7c46309SBarry Smith 19424a2ae208SSatish Balay #undef __FUNCT__ 19434a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1944dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1945a008b906SSatish Balay { 19464b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 19474b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1948dfbe8321SBarry Smith PetscErrorCode ierr; 1949b1d57f15SBarry Smith PetscInt s1,s2,s3; 1950a008b906SSatish Balay 19513a40ed3dSBarry Smith PetscFunctionBegin; 19524b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 19534b967eb1SSatish Balay if (rr) { 1954e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1955e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 19564b967eb1SSatish Balay /* Overlap communication with computation. */ 1957ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1958a008b906SSatish Balay } 19594b967eb1SSatish Balay if (ll) { 1960e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1961e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1962f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 19634b967eb1SSatish Balay } 19644b967eb1SSatish Balay /* scale the diagonal block */ 1965f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 19664b967eb1SSatish Balay 19674b967eb1SSatish Balay if (rr) { 19684b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1969ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1970f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 19714b967eb1SSatish Balay } 19724b967eb1SSatish Balay 19733a40ed3dSBarry Smith PetscFunctionReturn(0); 1974a008b906SSatish Balay } 1975a008b906SSatish Balay 19764a2ae208SSatish Balay #undef __FUNCT__ 1977521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ" 1978521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs) 19795a838052SSatish Balay { 1980521d7252SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1981521d7252SBarry Smith PetscErrorCode ierr; 1982521d7252SBarry Smith 19833a40ed3dSBarry Smith PetscFunctionBegin; 1984521d7252SBarry Smith ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 1985521d7252SBarry Smith ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1986829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 1987829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 19883a40ed3dSBarry Smith PetscFunctionReturn(0); 19895a838052SSatish Balay } 19904a2ae208SSatish Balay #undef __FUNCT__ 19914a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 1992dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 1993bb5a7306SBarry Smith { 1994bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1995dfbe8321SBarry Smith PetscErrorCode ierr; 19963a40ed3dSBarry Smith 19973a40ed3dSBarry Smith PetscFunctionBegin; 1998bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 19993a40ed3dSBarry Smith PetscFunctionReturn(0); 2000bb5a7306SBarry Smith } 2001bb5a7306SBarry Smith 20024a2ae208SSatish Balay #undef __FUNCT__ 20034a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2004ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2005d4bb536fSBarry Smith { 2006d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2007d4bb536fSBarry Smith Mat a,b,c,d; 2008ace3abfcSBarry Smith PetscBool flg; 2009dfbe8321SBarry Smith PetscErrorCode ierr; 2010d4bb536fSBarry Smith 20113a40ed3dSBarry Smith PetscFunctionBegin; 2012d4bb536fSBarry Smith a = matA->A; b = matA->B; 2013d4bb536fSBarry Smith c = matB->A; d = matB->B; 2014d4bb536fSBarry Smith 2015d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2016abc0a331SBarry Smith if (flg) { 2017d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2018d4bb536fSBarry Smith } 20197adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 20203a40ed3dSBarry Smith PetscFunctionReturn(0); 2021d4bb536fSBarry Smith } 2022d4bb536fSBarry Smith 20234a2ae208SSatish Balay #undef __FUNCT__ 20244a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2025dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2026cb5b572fSBarry Smith { 2027dfbe8321SBarry Smith PetscErrorCode ierr; 2028cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 2029cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 2030cb5b572fSBarry Smith 2031cb5b572fSBarry Smith PetscFunctionBegin; 203233f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 203333f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2034cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2035cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2036cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2037cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2038cb5b572fSBarry Smith then copying the submatrices */ 2039cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2040cb5b572fSBarry Smith } else { 2041cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2042cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2043cb5b572fSBarry Smith } 2044cb5b572fSBarry Smith PetscFunctionReturn(0); 2045cb5b572fSBarry Smith } 2046cb5b572fSBarry Smith 20474a2ae208SSatish Balay #undef __FUNCT__ 20484a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 2049dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A) 2050273d9f13SBarry Smith { 2051dfbe8321SBarry Smith PetscErrorCode ierr; 2052273d9f13SBarry Smith 2053273d9f13SBarry Smith PetscFunctionBegin; 2054273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2055273d9f13SBarry Smith PetscFunctionReturn(0); 2056273d9f13SBarry Smith } 2057273d9f13SBarry Smith 2058ac90fabeSBarry Smith #undef __FUNCT__ 2059ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2060f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2061ac90fabeSBarry Smith { 2062dfbe8321SBarry Smith PetscErrorCode ierr; 2063b1d57f15SBarry Smith PetscInt i; 2064ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 20654ce68768SBarry Smith PetscBLASInt bnz,one=1; 2066ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2067ac90fabeSBarry Smith 2068ac90fabeSBarry Smith PetscFunctionBegin; 2069ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2070f4df32b1SMatthew Knepley PetscScalar alpha = a; 2071ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 2072ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 20730805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2074f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2075ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 2076ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 20770805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2078f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2079a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2080f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2081c537a176SHong Zhang 2082c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 2083a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 2084a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2085a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 2086a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 2087c537a176SHong Zhang } 2088a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2089d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2090a30b2313SHong Zhang y->XtoY = xx->B; 2091407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2092c537a176SHong Zhang } 2093f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2094ac90fabeSBarry Smith } else { 20959f5f6813SShri Abhyankar Mat B; 20969f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 20979f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 20989f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 20999f5f6813SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 2100bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 21019f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 21029f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 21039f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 21049f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->B,xx->B,nnz_o);CHKERRQ(ierr); 21059f5f6813SShri Abhyankar ierr = MatMPIAIJSetPreallocation(B,PETSC_NULL,nnz_d,PETSC_NULL,nnz_o);CHKERRQ(ierr); 21069f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 21079f5f6813SShri Abhyankar ierr = MatHeaderReplace(Y,B); 21089f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 21099f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2110ac90fabeSBarry Smith } 2111ac90fabeSBarry Smith PetscFunctionReturn(0); 2112ac90fabeSBarry Smith } 2113ac90fabeSBarry Smith 2114*09573ac7SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat); 2115354c94deSBarry Smith 2116354c94deSBarry Smith #undef __FUNCT__ 2117354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 2118354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat) 2119354c94deSBarry Smith { 2120354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2121354c94deSBarry Smith PetscErrorCode ierr; 2122354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2123354c94deSBarry Smith 2124354c94deSBarry Smith PetscFunctionBegin; 2125354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2126354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2127354c94deSBarry Smith #else 2128354c94deSBarry Smith PetscFunctionBegin; 2129354c94deSBarry Smith #endif 2130354c94deSBarry Smith PetscFunctionReturn(0); 2131354c94deSBarry Smith } 2132354c94deSBarry Smith 213399cafbc1SBarry Smith #undef __FUNCT__ 213499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 213599cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 213699cafbc1SBarry Smith { 213799cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 213899cafbc1SBarry Smith PetscErrorCode ierr; 213999cafbc1SBarry Smith 214099cafbc1SBarry Smith PetscFunctionBegin; 214199cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 214299cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 214399cafbc1SBarry Smith PetscFunctionReturn(0); 214499cafbc1SBarry Smith } 214599cafbc1SBarry Smith 214699cafbc1SBarry Smith #undef __FUNCT__ 214799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 214899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 214999cafbc1SBarry Smith { 215099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 215199cafbc1SBarry Smith PetscErrorCode ierr; 215299cafbc1SBarry Smith 215399cafbc1SBarry Smith PetscFunctionBegin; 215499cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 215599cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 215699cafbc1SBarry Smith PetscFunctionReturn(0); 215799cafbc1SBarry Smith } 215899cafbc1SBarry Smith 2159103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2160103bf8bdSMatthew Knepley 2161103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2162a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2163a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2164a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2165103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2166a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2167d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2168103bf8bdSMatthew Knepley 2169103bf8bdSMatthew Knepley #undef __FUNCT__ 2170103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2171103bf8bdSMatthew Knepley /* 2172103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2173103bf8bdSMatthew Knepley */ 21740481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2175103bf8bdSMatthew Knepley { 2176a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2177a2c909beSMatthew Knepley 2178a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2179a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2180a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2181a2c909beSMatthew Knepley 2182ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2183776b82aeSLisandro Dalcin PetscContainer c; 2184103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2185103bf8bdSMatthew Knepley PetscErrorCode ierr; 2186103bf8bdSMatthew Knepley 2187103bf8bdSMatthew Knepley PetscFunctionBegin; 2188e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2189103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2190103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2191103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 2192e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2193103bf8bdSMatthew Knepley } 2194103bf8bdSMatthew Knepley 2195103bf8bdSMatthew Knepley process_group_type pg; 2196a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2197a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2198a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2199a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2200a2c909beSMatthew Knepley 2201103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2202a2c909beSMatthew Knepley ilu_permuted(level_graph); 2203103bf8bdSMatthew Knepley 2204103bf8bdSMatthew Knepley /* put together the new matrix */ 22057adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 2206103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2207103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2208719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2209719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2210719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2211719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2212103bf8bdSMatthew Knepley 22137adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 2214776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2215719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2216103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2217103bf8bdSMatthew Knepley } 2218103bf8bdSMatthew Knepley 2219103bf8bdSMatthew Knepley #undef __FUNCT__ 2220103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 22210481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2222103bf8bdSMatthew Knepley { 2223103bf8bdSMatthew Knepley PetscFunctionBegin; 2224103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2225103bf8bdSMatthew Knepley } 2226103bf8bdSMatthew Knepley 2227103bf8bdSMatthew Knepley #undef __FUNCT__ 2228103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2229103bf8bdSMatthew Knepley /* 2230103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2231103bf8bdSMatthew Knepley */ 2232103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2233103bf8bdSMatthew Knepley { 2234a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2235a2c909beSMatthew Knepley 2236a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2237a2c909beSMatthew Knepley lgraph_type* lgraph_p; 2238776b82aeSLisandro Dalcin PetscContainer c; 2239103bf8bdSMatthew Knepley PetscErrorCode ierr; 2240103bf8bdSMatthew Knepley 2241103bf8bdSMatthew Knepley PetscFunctionBegin; 2242103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 2243776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 2244103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2245a2c909beSMatthew Knepley 2246a2c909beSMatthew Knepley PetscScalar* array_x; 2247a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2248a2c909beSMatthew Knepley PetscInt sx; 2249a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2250a2c909beSMatthew Knepley 2251a2c909beSMatthew Knepley PetscScalar* array_b; 2252a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2253a2c909beSMatthew Knepley PetscInt sb; 2254a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2255a2c909beSMatthew Knepley 2256a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2257a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2258a2c909beSMatthew Knepley 2259a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2260a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2261a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2262a2c909beSMatthew Knepley 2263a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2264a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2265a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2266a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2267a2c909beSMatthew Knepley 2268a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2269a2c909beSMatthew Knepley 2270103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2271103bf8bdSMatthew Knepley } 2272103bf8bdSMatthew Knepley #endif 2273103bf8bdSMatthew Knepley 227469db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 227569db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 22761d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 22771d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 227869db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 227969db28dcSHong Zhang PetscErrorCode (*MatDestroy)(Mat); 228069db28dcSHong Zhang } Mat_Redundant; 228169db28dcSHong Zhang 228269db28dcSHong Zhang #undef __FUNCT__ 228369db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 228469db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 228569db28dcSHong Zhang { 228669db28dcSHong Zhang PetscErrorCode ierr; 228769db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 228869db28dcSHong Zhang PetscInt i; 228969db28dcSHong Zhang 229069db28dcSHong Zhang PetscFunctionBegin; 22911d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 229269db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 229369db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 229469db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 229569db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 229669db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 229769db28dcSHong Zhang } 22981d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 229969db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 230069db28dcSHong Zhang PetscFunctionReturn(0); 230169db28dcSHong Zhang } 230269db28dcSHong Zhang 230369db28dcSHong Zhang #undef __FUNCT__ 230469db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 230569db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 230669db28dcSHong Zhang { 230769db28dcSHong Zhang PetscErrorCode ierr; 230869db28dcSHong Zhang PetscContainer container; 230969db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 231069db28dcSHong Zhang 231169db28dcSHong Zhang PetscFunctionBegin; 231269db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 231369db28dcSHong Zhang if (container) { 231469db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 231569db28dcSHong Zhang } else { 2316e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 231769db28dcSHong Zhang } 231869db28dcSHong Zhang A->ops->destroy = redund->MatDestroy; 231969db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 232069db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 232169db28dcSHong Zhang ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 232269db28dcSHong Zhang PetscFunctionReturn(0); 232369db28dcSHong Zhang } 232469db28dcSHong Zhang 232569db28dcSHong Zhang #undef __FUNCT__ 232669db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 232769db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 232869db28dcSHong Zhang { 232969db28dcSHong Zhang PetscMPIInt rank,size; 23307adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 233169db28dcSHong Zhang PetscErrorCode ierr; 233269db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 233369db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2334d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 233569db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 233669db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 233769db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 233869db28dcSHong Zhang PetscScalar *sbuf_a; 233969db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2340d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2341d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 234269db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2343a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2344a77337e4SBarry Smith PetscScalar *vals; 234569db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 234669db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 234769db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 234869db28dcSHong Zhang MPI_Status recv_status,*send_status; 234969db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 235069db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 235169db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 235269db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 235369db28dcSHong Zhang PetscContainer container; 235469db28dcSHong Zhang 235569db28dcSHong Zhang PetscFunctionBegin; 235669db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 235769db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 235869db28dcSHong Zhang 235969db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 236069db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2361e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 236269db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2363e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 236469db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 236569db28dcSHong Zhang if (container) { 236669db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 236769db28dcSHong Zhang } else { 2368e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 236969db28dcSHong Zhang } 2370e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 237169db28dcSHong Zhang 237269db28dcSHong Zhang nsends = redund->nsends; 237369db28dcSHong Zhang nrecvs = redund->nrecvs; 23741d79065fSBarry Smith send_rank = redund->send_rank; 23751d79065fSBarry Smith recv_rank = redund->recv_rank; 23761d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 23771d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 237869db28dcSHong Zhang sbuf_j = redund->sbuf_j; 237969db28dcSHong Zhang sbuf_a = redund->sbuf_a; 238069db28dcSHong Zhang rbuf_j = redund->rbuf_j; 238169db28dcSHong Zhang rbuf_a = redund->rbuf_a; 238269db28dcSHong Zhang } 238369db28dcSHong Zhang 238469db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 238569db28dcSHong Zhang PetscMPIInt subrank,subsize; 238669db28dcSHong Zhang PetscInt nleftover,np_subcomm; 238769db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 238869db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 238969db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 23901d79065fSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank); 239169db28dcSHong Zhang np_subcomm = size/nsubcomm; 239269db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 239369db28dcSHong Zhang nsends = 0; nrecvs = 0; 239469db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 239569db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 239669db28dcSHong Zhang send_rank[nsends] = i; nsends++; 239769db28dcSHong Zhang recv_rank[nrecvs++] = i; 239869db28dcSHong Zhang } 239969db28dcSHong Zhang } 240069db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 240169db28dcSHong Zhang i = size-nleftover-1; 240269db28dcSHong Zhang j = 0; 240369db28dcSHong Zhang while (j < nsubcomm - nleftover){ 240469db28dcSHong Zhang send_rank[nsends++] = i; 240569db28dcSHong Zhang i--; j++; 240669db28dcSHong Zhang } 240769db28dcSHong Zhang } 240869db28dcSHong Zhang 240969db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 241069db28dcSHong Zhang for (i=0; i<nleftover; i++){ 241169db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 241269db28dcSHong Zhang } 241369db28dcSHong Zhang } 241469db28dcSHong Zhang 241569db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 241669db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 241769db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 241869db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 241969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 242069db28dcSHong Zhang 242169db28dcSHong Zhang /* copy mat's local entries into the buffers */ 242269db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 242369db28dcSHong Zhang rownz_max = 0; 242469db28dcSHong Zhang rptr = sbuf_j; 242569db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 242669db28dcSHong Zhang vals = sbuf_a; 242769db28dcSHong Zhang rptr[0] = 0; 242869db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 242969db28dcSHong Zhang row = i + rstart; 243069db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 243169db28dcSHong Zhang ncols = nzA + nzB; 243269db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 243369db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 243469db28dcSHong Zhang /* load the column indices for this row into cols */ 243569db28dcSHong Zhang lwrite = 0; 243669db28dcSHong Zhang for (l=0; l<nzB; l++) { 243769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 243869db28dcSHong Zhang vals[lwrite] = aworkB[l]; 243969db28dcSHong Zhang cols[lwrite++] = ctmp; 244069db28dcSHong Zhang } 244169db28dcSHong Zhang } 244269db28dcSHong Zhang for (l=0; l<nzA; l++){ 244369db28dcSHong Zhang vals[lwrite] = aworkA[l]; 244469db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 244569db28dcSHong Zhang } 244669db28dcSHong Zhang for (l=0; l<nzB; l++) { 244769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 244869db28dcSHong Zhang vals[lwrite] = aworkB[l]; 244969db28dcSHong Zhang cols[lwrite++] = ctmp; 245069db28dcSHong Zhang } 245169db28dcSHong Zhang } 245269db28dcSHong Zhang vals += ncols; 245369db28dcSHong Zhang cols += ncols; 245469db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 245569db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 245669db28dcSHong Zhang } 2457e32f2f54SBarry 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); 245869db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 245969db28dcSHong Zhang rptr = sbuf_j; 246069db28dcSHong Zhang vals = sbuf_a; 246169db28dcSHong Zhang rptr[0] = 0; 246269db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 246369db28dcSHong Zhang row = i + rstart; 246469db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 246569db28dcSHong Zhang ncols = nzA + nzB; 246669db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 246769db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 246869db28dcSHong Zhang lwrite = 0; 246969db28dcSHong Zhang for (l=0; l<nzB; l++) { 247069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 247169db28dcSHong Zhang } 247269db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 247369db28dcSHong Zhang for (l=0; l<nzB; l++) { 247469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 247569db28dcSHong Zhang } 247669db28dcSHong Zhang vals += ncols; 247769db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 247869db28dcSHong Zhang } 247969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 248069db28dcSHong Zhang 248169db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 248269db28dcSHong Zhang /*--------------------------------------------------*/ 248369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 248469db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 248569db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 248669db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 248769db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 248869db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 248969db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 249069db28dcSHong Zhang } else { 249169db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 249269db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 249369db28dcSHong Zhang } 249469db28dcSHong Zhang 249569db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 249669db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 249769db28dcSHong Zhang /* get new tags to keep the communication clean */ 249869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 249969db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 25001d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 250169db28dcSHong Zhang 250269db28dcSHong Zhang /* post receives of other's nzlocal */ 250369db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 250469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 250569db28dcSHong Zhang } 250669db28dcSHong Zhang /* send nzlocal to others */ 250769db28dcSHong Zhang for (i=0; i<nsends; i++){ 250869db28dcSHong Zhang sbuf_nz[i] = nzlocal; 250969db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 251069db28dcSHong Zhang } 251169db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 251269db28dcSHong Zhang count = nrecvs; 251369db28dcSHong Zhang while (count) { 251469db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 251569db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 251669db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 251769db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 251869db28dcSHong Zhang 251969db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 252069db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 252169db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 252269db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 252369db28dcSHong Zhang count--; 252469db28dcSHong Zhang } 252569db28dcSHong Zhang /* wait on sends of nzlocal */ 252669db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 252769db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 252869db28dcSHong Zhang /*------------------------------------------------*/ 252969db28dcSHong Zhang for (i=0; i<nsends; i++){ 253069db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 253169db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 253269db28dcSHong Zhang } 253369db28dcSHong Zhang /* wait on receives of mat->i,j */ 253469db28dcSHong Zhang /*------------------------------*/ 253569db28dcSHong Zhang count = nrecvs; 253669db28dcSHong Zhang while (count) { 253769db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2538e32f2f54SBarry 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); 253969db28dcSHong Zhang count--; 254069db28dcSHong Zhang } 254169db28dcSHong Zhang /* wait on sends of mat->i,j */ 254269db28dcSHong Zhang /*---------------------------*/ 254369db28dcSHong Zhang if (nsends) { 254469db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 254569db28dcSHong Zhang } 254669db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 254769db28dcSHong Zhang 254869db28dcSHong Zhang /* post receives, send and receive mat->a */ 254969db28dcSHong Zhang /*----------------------------------------*/ 255069db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 255169db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 255269db28dcSHong Zhang } 255369db28dcSHong Zhang for (i=0; i<nsends; i++){ 255469db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 255569db28dcSHong Zhang } 255669db28dcSHong Zhang count = nrecvs; 255769db28dcSHong Zhang while (count) { 255869db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2559e32f2f54SBarry 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); 256069db28dcSHong Zhang count--; 256169db28dcSHong Zhang } 256269db28dcSHong Zhang if (nsends) { 256369db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 256469db28dcSHong Zhang } 256569db28dcSHong Zhang 256669db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 256769db28dcSHong Zhang 256869db28dcSHong Zhang /* create redundant matrix */ 256969db28dcSHong Zhang /*-------------------------*/ 257069db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 257169db28dcSHong Zhang /* compute rownz_max for preallocation */ 257269db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 257369db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 257469db28dcSHong Zhang rptr = rbuf_j[imdex]; 257569db28dcSHong Zhang for (i=0; i<j; i++){ 257669db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 257769db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 257869db28dcSHong Zhang } 257969db28dcSHong Zhang } 258069db28dcSHong Zhang 258169db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 258269db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 258369db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 258469db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 258569db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 258669db28dcSHong Zhang } else { 258769db28dcSHong Zhang C = *matredundant; 258869db28dcSHong Zhang } 258969db28dcSHong Zhang 259069db28dcSHong Zhang /* insert local matrix entries */ 259169db28dcSHong Zhang rptr = sbuf_j; 259269db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 259369db28dcSHong Zhang vals = sbuf_a; 259469db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 259569db28dcSHong Zhang row = i + rstart; 259669db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 259769db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 259869db28dcSHong Zhang vals += ncols; 259969db28dcSHong Zhang cols += ncols; 260069db28dcSHong Zhang } 260169db28dcSHong Zhang /* insert received matrix entries */ 260269db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 260369db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 260469db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 260569db28dcSHong Zhang rptr = rbuf_j[imdex]; 260669db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 260769db28dcSHong Zhang vals = rbuf_a[imdex]; 260869db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 260969db28dcSHong Zhang row = i + rstart; 261069db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 261169db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 261269db28dcSHong Zhang vals += ncols; 261369db28dcSHong Zhang cols += ncols; 261469db28dcSHong Zhang } 261569db28dcSHong Zhang } 261669db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 261769db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 261869db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2619e32f2f54SBarry 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); 262069db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 262169db28dcSHong Zhang PetscContainer container; 262269db28dcSHong Zhang *matredundant = C; 262369db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 262438f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 262569db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 262669db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 262769db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 262869db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 262969db28dcSHong Zhang 263069db28dcSHong Zhang redund->nzlocal = nzlocal; 263169db28dcSHong Zhang redund->nsends = nsends; 263269db28dcSHong Zhang redund->nrecvs = nrecvs; 263369db28dcSHong Zhang redund->send_rank = send_rank; 26341d79065fSBarry Smith redund->recv_rank = recv_rank; 263569db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 26361d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 263769db28dcSHong Zhang redund->sbuf_j = sbuf_j; 263869db28dcSHong Zhang redund->sbuf_a = sbuf_a; 263969db28dcSHong Zhang redund->rbuf_j = rbuf_j; 264069db28dcSHong Zhang redund->rbuf_a = rbuf_a; 264169db28dcSHong Zhang 264269db28dcSHong Zhang redund->MatDestroy = C->ops->destroy; 264369db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 264469db28dcSHong Zhang } 264569db28dcSHong Zhang PetscFunctionReturn(0); 264669db28dcSHong Zhang } 264769db28dcSHong Zhang 264803bc72f1SMatthew Knepley #undef __FUNCT__ 2649c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2650c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2651c91732d9SHong Zhang { 2652c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2653c91732d9SHong Zhang PetscErrorCode ierr; 2654c91732d9SHong Zhang PetscInt i,*idxb = 0; 2655c91732d9SHong Zhang PetscScalar *va,*vb; 2656c91732d9SHong Zhang Vec vtmp; 2657c91732d9SHong Zhang 2658c91732d9SHong Zhang PetscFunctionBegin; 2659c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2660c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2661c91732d9SHong Zhang if (idx) { 2662192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2663d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2664c91732d9SHong Zhang } 2665c91732d9SHong Zhang } 2666c91732d9SHong Zhang 2667d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2668c91732d9SHong Zhang if (idx) { 2669d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2670c91732d9SHong Zhang } 2671c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2672c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2673c91732d9SHong Zhang 2674d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2675c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2676c91732d9SHong Zhang va[i] = vb[i]; 2677c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2678c91732d9SHong Zhang } 2679c91732d9SHong Zhang } 2680c91732d9SHong Zhang 2681c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2682c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2683c91732d9SHong Zhang if (idxb) { 2684c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 2685c91732d9SHong Zhang } 2686c91732d9SHong Zhang ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2687c91732d9SHong Zhang PetscFunctionReturn(0); 2688c91732d9SHong Zhang } 2689c91732d9SHong Zhang 2690c91732d9SHong Zhang #undef __FUNCT__ 2691c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2692c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2693c87e5d42SMatthew Knepley { 2694c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2695c87e5d42SMatthew Knepley PetscErrorCode ierr; 2696c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2697c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2698c87e5d42SMatthew Knepley Vec vtmp; 2699c87e5d42SMatthew Knepley 2700c87e5d42SMatthew Knepley PetscFunctionBegin; 2701c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2702c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2703c87e5d42SMatthew Knepley if (idx) { 2704c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2705c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2706c87e5d42SMatthew Knepley } 2707c87e5d42SMatthew Knepley } 2708c87e5d42SMatthew Knepley 2709c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2710c87e5d42SMatthew Knepley if (idx) { 2711c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2712c87e5d42SMatthew Knepley } 2713c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2714c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2715c87e5d42SMatthew Knepley 2716c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2717c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2718c87e5d42SMatthew Knepley va[i] = vb[i]; 2719c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2720c87e5d42SMatthew Knepley } 2721c87e5d42SMatthew Knepley } 2722c87e5d42SMatthew Knepley 2723c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2724c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2725c87e5d42SMatthew Knepley if (idxb) { 2726c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 2727c87e5d42SMatthew Knepley } 2728c87e5d42SMatthew Knepley ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2729c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2730c87e5d42SMatthew Knepley } 2731c87e5d42SMatthew Knepley 2732c87e5d42SMatthew Knepley #undef __FUNCT__ 273303bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 273403bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 273503bc72f1SMatthew Knepley { 273603bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2737d0f46423SBarry Smith PetscInt n = A->rmap->n; 2738d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 273903bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 274003bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 274103bc72f1SMatthew Knepley Vec diagV, offdiagV; 274203bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 274303bc72f1SMatthew Knepley PetscInt r; 274403bc72f1SMatthew Knepley PetscErrorCode ierr; 274503bc72f1SMatthew Knepley 274603bc72f1SMatthew Knepley PetscFunctionBegin; 274703bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2748e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2749e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 275003bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 275103bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 275203bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 275303bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 275403bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 275503bc72f1SMatthew Knepley for(r = 0; r < n; ++r) { 2756028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 275703bc72f1SMatthew Knepley a[r] = diagA[r]; 275803bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 275903bc72f1SMatthew Knepley } else { 276003bc72f1SMatthew Knepley a[r] = offdiagA[r]; 276103bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 276203bc72f1SMatthew Knepley } 276303bc72f1SMatthew Knepley } 276403bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 276503bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 276603bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 276703bc72f1SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 276803bc72f1SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 276903bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 277003bc72f1SMatthew Knepley PetscFunctionReturn(0); 277103bc72f1SMatthew Knepley } 277203bc72f1SMatthew Knepley 27735494a064SHong Zhang #undef __FUNCT__ 2774c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2775c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2776c87e5d42SMatthew Knepley { 2777c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2778c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2779c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2780c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2781c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2782c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2783c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2784c87e5d42SMatthew Knepley PetscInt r; 2785c87e5d42SMatthew Knepley PetscErrorCode ierr; 2786c87e5d42SMatthew Knepley 2787c87e5d42SMatthew Knepley PetscFunctionBegin; 2788c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2789c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2790c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 2791c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2792c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2793c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2794c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2795c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2796c87e5d42SMatthew Knepley for(r = 0; r < n; ++r) { 2797c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2798c87e5d42SMatthew Knepley a[r] = diagA[r]; 2799c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2800c87e5d42SMatthew Knepley } else { 2801c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2802c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2803c87e5d42SMatthew Knepley } 2804c87e5d42SMatthew Knepley } 2805c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2806c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2807c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2808c87e5d42SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 2809c87e5d42SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 2810c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 2811c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2812c87e5d42SMatthew Knepley } 2813c87e5d42SMatthew Knepley 2814c87e5d42SMatthew Knepley #undef __FUNCT__ 2815829201f2SHong Zhang #define __FUNCT__ "MatGetSeqNonzerostructure_MPIAIJ" 2816f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIAIJ(Mat mat,Mat *newmat) 28175494a064SHong Zhang { 28185494a064SHong Zhang PetscErrorCode ierr; 2819f6d58c54SBarry Smith Mat *dummy; 28205494a064SHong Zhang 28215494a064SHong Zhang PetscFunctionBegin; 2822f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 2823f6d58c54SBarry Smith *newmat = *dummy; 2824f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 28255494a064SHong Zhang PetscFunctionReturn(0); 28265494a064SHong Zhang } 28275494a064SHong Zhang 28283acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 28298a729477SBarry Smith /* -------------------------------------------------------------------*/ 2830cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 2831cda55fadSBarry Smith MatGetRow_MPIAIJ, 2832cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 2833cda55fadSBarry Smith MatMult_MPIAIJ, 283497304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 28357c922b88SBarry Smith MatMultTranspose_MPIAIJ, 28367c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 2837103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2838103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 2839103bf8bdSMatthew Knepley #else 2840cda55fadSBarry Smith 0, 2841103bf8bdSMatthew Knepley #endif 2842cda55fadSBarry Smith 0, 2843cda55fadSBarry Smith 0, 284497304618SKris Buschelman /*10*/ 0, 2845cda55fadSBarry Smith 0, 2846cda55fadSBarry Smith 0, 284741f059aeSBarry Smith MatSOR_MPIAIJ, 2848b7c46309SBarry Smith MatTranspose_MPIAIJ, 284997304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 2850cda55fadSBarry Smith MatEqual_MPIAIJ, 2851cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 2852cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 2853cda55fadSBarry Smith MatNorm_MPIAIJ, 285497304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 2855cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 2856cda55fadSBarry Smith MatSetOption_MPIAIJ, 2857cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 2858d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 2859cda55fadSBarry Smith 0, 2860103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2861719d5645SBarry Smith 0, 2862103bf8bdSMatthew Knepley #else 2863cda55fadSBarry Smith 0, 2864103bf8bdSMatthew Knepley #endif 2865cda55fadSBarry Smith 0, 2866cda55fadSBarry Smith 0, 2867d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIAIJ, 2868103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2869719d5645SBarry Smith 0, 2870103bf8bdSMatthew Knepley #else 2871cda55fadSBarry Smith 0, 2872103bf8bdSMatthew Knepley #endif 2873cda55fadSBarry Smith 0, 2874cda55fadSBarry Smith 0, 2875cda55fadSBarry Smith 0, 2876d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 2877cda55fadSBarry Smith 0, 2878cda55fadSBarry Smith 0, 2879cda55fadSBarry Smith 0, 2880cda55fadSBarry Smith 0, 2881d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 2882cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 2883cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 2884cda55fadSBarry Smith MatGetValues_MPIAIJ, 2885cb5b572fSBarry Smith MatCopy_MPIAIJ, 2886d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 2887cda55fadSBarry Smith MatScale_MPIAIJ, 2888cda55fadSBarry Smith 0, 2889cda55fadSBarry Smith 0, 2890564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 2891d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_MPIAIJ, 2892cda55fadSBarry Smith 0, 2893cda55fadSBarry Smith 0, 2894cda55fadSBarry Smith 0, 2895cda55fadSBarry Smith 0, 2896d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 2897cda55fadSBarry Smith 0, 2898cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 289942e855d1Svictor MatPermute_MPIAIJ, 2900cda55fadSBarry Smith 0, 2901d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 2902e03a110bSBarry Smith MatDestroy_MPIAIJ, 2903e03a110bSBarry Smith MatView_MPIAIJ, 2904357abbc8SBarry Smith 0, 2905a2243be0SBarry Smith 0, 2906d519adbfSMatthew Knepley /*64*/ 0, 2907a2243be0SBarry Smith 0, 2908a2243be0SBarry Smith 0, 2909a2243be0SBarry Smith 0, 2910a2243be0SBarry Smith 0, 2911d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 2912c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 2913a2243be0SBarry Smith 0, 2914a2243be0SBarry Smith MatSetColoring_MPIAIJ, 2915dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2916779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 2917dcf5cc72SBarry Smith #else 2918dcf5cc72SBarry Smith 0, 2919dcf5cc72SBarry Smith #endif 292097304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 29213acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 292297304618SKris Buschelman 0, 292397304618SKris Buschelman 0, 292497304618SKris Buschelman 0, 292597304618SKris Buschelman 0, 292697304618SKris Buschelman /*80*/ 0, 292797304618SKris Buschelman 0, 292897304618SKris Buschelman 0, 29295bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 29306284ec50SHong Zhang 0, 29316284ec50SHong Zhang 0, 29326284ec50SHong Zhang 0, 29336284ec50SHong Zhang 0, 2934865e5f61SKris Buschelman 0, 2935d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 293626be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 293726be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 29387a7894deSKris Buschelman MatPtAP_Basic, 29397a7894deSKris Buschelman MatPtAPSymbolic_MPIAIJ, 2940d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ, 29417a7894deSKris Buschelman 0, 29427a7894deSKris Buschelman 0, 29437a7894deSKris Buschelman 0, 29447a7894deSKris Buschelman 0, 2945d519adbfSMatthew Knepley /*99*/ 0, 2946865e5f61SKris Buschelman MatPtAPSymbolic_MPIAIJ_MPIAIJ, 29477a7894deSKris Buschelman MatPtAPNumeric_MPIAIJ_MPIAIJ, 29482fd7e33dSBarry Smith MatConjugate_MPIAIJ, 29492fd7e33dSBarry Smith 0, 2950d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 295199cafbc1SBarry Smith MatRealPart_MPIAIJ, 295269db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 295369db28dcSHong Zhang 0, 295469db28dcSHong Zhang 0, 2955d519adbfSMatthew Knepley /*109*/0, 295603bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 29575494a064SHong Zhang MatGetRowMin_MPIAIJ, 29585494a064SHong Zhang 0, 29595494a064SHong Zhang 0, 2960bd0c2dcbSBarry Smith /*114*/MatGetSeqNonzerostructure_MPIAIJ, 2961bd0c2dcbSBarry Smith 0, 2962bd0c2dcbSBarry Smith 0, 2963bd0c2dcbSBarry Smith 0, 2964bd0c2dcbSBarry Smith 0, 29658fb81238SShri Abhyankar /*119*/0, 29668fb81238SShri Abhyankar 0, 29678fb81238SShri Abhyankar 0, 2968d6037b41SHong Zhang 0, 2969b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 2970b9614d88SDmitry Karpeev /*124*/0, 2971b9614d88SDmitry Karpeev 0, 2972b9614d88SDmitry Karpeev 0, 2973b9614d88SDmitry Karpeev 0, 2974b9614d88SDmitry Karpeev MatGetSubMatricesParallel_MPIAIJ 2975bd0c2dcbSBarry Smith }; 297636ce4990SBarry Smith 29772e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 29782e8a6d31SBarry Smith 2979fb2e594dSBarry Smith EXTERN_C_BEGIN 29804a2ae208SSatish Balay #undef __FUNCT__ 29814a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 2982be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat) 29832e8a6d31SBarry Smith { 29842e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2985dfbe8321SBarry Smith PetscErrorCode ierr; 29862e8a6d31SBarry Smith 29872e8a6d31SBarry Smith PetscFunctionBegin; 29882e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 29892e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 29902e8a6d31SBarry Smith PetscFunctionReturn(0); 29912e8a6d31SBarry Smith } 2992fb2e594dSBarry Smith EXTERN_C_END 29932e8a6d31SBarry Smith 2994fb2e594dSBarry Smith EXTERN_C_BEGIN 29954a2ae208SSatish Balay #undef __FUNCT__ 29964a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 2997be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat) 29982e8a6d31SBarry Smith { 29992e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 3000dfbe8321SBarry Smith PetscErrorCode ierr; 30012e8a6d31SBarry Smith 30022e8a6d31SBarry Smith PetscFunctionBegin; 30032e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 30042e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 30052e8a6d31SBarry Smith PetscFunctionReturn(0); 30062e8a6d31SBarry Smith } 3007fb2e594dSBarry Smith EXTERN_C_END 30088a729477SBarry Smith 300927508adbSBarry Smith EXTERN_C_BEGIN 30104a2ae208SSatish Balay #undef __FUNCT__ 3011a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 3012be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3013a23d5eceSKris Buschelman { 3014a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3015dfbe8321SBarry Smith PetscErrorCode ierr; 3016b1d57f15SBarry Smith PetscInt i; 3017a23d5eceSKris Buschelman 3018a23d5eceSKris Buschelman PetscFunctionBegin; 3019a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 3020a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 3021e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 3022e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 3023899cda47SBarry Smith 302426283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 302526283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 302626283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 302726283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3028a23d5eceSKris Buschelman if (d_nnz) { 3029d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3030e32f2f54SBarry 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]); 3031a23d5eceSKris Buschelman } 3032a23d5eceSKris Buschelman } 3033a23d5eceSKris Buschelman if (o_nnz) { 3034d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3035e32f2f54SBarry 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]); 3036a23d5eceSKris Buschelman } 3037a23d5eceSKris Buschelman } 3038a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3039899cda47SBarry Smith 3040526dfc15SBarry Smith if (!B->preallocated) { 3041899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3042899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3043d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3044899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 3045899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3046899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3047d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3048899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 3049899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 3050526dfc15SBarry Smith } 3051899cda47SBarry Smith 3052c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3053c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 3054526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3055a23d5eceSKris Buschelman PetscFunctionReturn(0); 3056a23d5eceSKris Buschelman } 3057a23d5eceSKris Buschelman EXTERN_C_END 3058a23d5eceSKris Buschelman 30594a2ae208SSatish Balay #undef __FUNCT__ 30604a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3061dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3062d6dfbf8fSBarry Smith { 3063d6dfbf8fSBarry Smith Mat mat; 3064416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3065dfbe8321SBarry Smith PetscErrorCode ierr; 3066d6dfbf8fSBarry Smith 30673a40ed3dSBarry Smith PetscFunctionBegin; 3068416022c9SBarry Smith *newmat = 0; 30697adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 3070d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 30717adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 30721d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3073273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3074e1b6402fSHong Zhang 3075d5f3da31SBarry Smith mat->factortype = matin->factortype; 3076d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3077c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3078e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3079273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3080d6dfbf8fSBarry Smith 308117699dbbSLois Curfman McInnes a->size = oldmat->size; 308217699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3083e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3084e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3085e7641de0SSatish Balay a->rowindices = 0; 3086bcd2baecSBarry Smith a->rowvalues = 0; 3087bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3088d6dfbf8fSBarry Smith 308926283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 309026283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3091899cda47SBarry Smith 30922ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3093aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 30940f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3095b1fc9764SSatish Balay #else 3096d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 3097d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3098d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3099b1fc9764SSatish Balay #endif 3100416022c9SBarry Smith } else a->colmap = 0; 31013f41c07dSBarry Smith if (oldmat->garray) { 3102b1d57f15SBarry Smith PetscInt len; 3103d0f46423SBarry Smith len = oldmat->B->cmap->n; 3104b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 310552e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3106b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3107416022c9SBarry Smith } else a->garray = 0; 3108d6dfbf8fSBarry Smith 3109416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 311052e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 3111a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 311252e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 31132e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 311452e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 31152e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 311652e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 31177adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 31188a729477SBarry Smith *newmat = mat; 31193a40ed3dSBarry Smith PetscFunctionReturn(0); 31208a729477SBarry Smith } 3121416022c9SBarry Smith 31221a4ee126SBarry Smith /* 31231a4ee126SBarry Smith Allows sending/receiving larger messages then 2 gigabytes in a single call 31241a4ee126SBarry Smith */ 31251a4ee126SBarry Smith static int MPILong_Send(void *mess,PetscInt cnt, MPI_Datatype type,int to, int tag, MPI_Comm comm) 31261a4ee126SBarry Smith { 31271a4ee126SBarry Smith int ierr; 31281a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 31291a4ee126SBarry Smith PetscInt i,numchunks; 31301a4ee126SBarry Smith PetscMPIInt icnt; 31311a4ee126SBarry Smith 31321a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 31331a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 31341a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31351a4ee126SBarry Smith ierr = MPI_Send(mess,icnt,type,to,tag,comm); 31361a4ee126SBarry Smith if (type == MPIU_INT) { 31371a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31381a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31391a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31401a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31411a4ee126SBarry Smith } 31421a4ee126SBarry Smith return 0; 31431a4ee126SBarry Smith } 31441a4ee126SBarry Smith static int MPILong_Recv(void *mess,PetscInt cnt, MPI_Datatype type,int from, int tag, MPI_Comm comm) 31451a4ee126SBarry Smith { 31461a4ee126SBarry Smith int ierr; 31471a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 31481a4ee126SBarry Smith MPI_Status status; 31491a4ee126SBarry Smith PetscInt i,numchunks; 31501a4ee126SBarry Smith PetscMPIInt icnt; 31511a4ee126SBarry Smith 31521a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 31531a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 31541a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 31551a4ee126SBarry Smith ierr = MPI_Recv(mess,icnt,type,from,tag,comm,&status); 31561a4ee126SBarry Smith if (type == MPIU_INT) { 31571a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 31581a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 31591a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 31601a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 31611a4ee126SBarry Smith } 31621a4ee126SBarry Smith return 0; 31631a4ee126SBarry Smith } 31641a4ee126SBarry Smith 31654a2ae208SSatish Balay #undef __FUNCT__ 31665bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3167112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 31688fb81238SShri Abhyankar { 31698fb81238SShri Abhyankar PetscScalar *vals,*svals; 31708fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 31718fb81238SShri Abhyankar PetscErrorCode ierr; 31721a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 31738fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 31748fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 31758fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 31768fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 31778fb81238SShri Abhyankar int fd; 31788fb81238SShri Abhyankar 31798fb81238SShri Abhyankar PetscFunctionBegin; 31808fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 31818fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 31828fb81238SShri Abhyankar if (!rank) { 31838fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 31848fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 31858fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 31868fb81238SShri Abhyankar } 31878fb81238SShri Abhyankar 31888fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 31898fb81238SShri Abhyankar 31908fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 31918fb81238SShri Abhyankar M = header[1]; N = header[2]; 31928fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 31938fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 31948fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 31958fb81238SShri Abhyankar 31968fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 31978fb81238SShri Abhyankar if (sizesset) { 31988fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 31998fb81238SShri Abhyankar } 3200abd38a8fSBarry 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); 3201abd38a8fSBarry 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); 32028fb81238SShri Abhyankar 32038fb81238SShri Abhyankar /* determine ownership of all rows */ 32048fb81238SShri Abhyankar if (newMat->rmap->n < 0 ) m = M/size + ((M % size) > rank); /* PETSC_DECIDE */ 32054683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 32068fb81238SShri Abhyankar 32078fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 32088fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 32098fb81238SShri Abhyankar 32108fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 32118fb81238SShri Abhyankar if (!rank) { 32128fb81238SShri Abhyankar mmax = rowners[1]; 32138fb81238SShri Abhyankar for (i=2; i<size; i++) { 32148fb81238SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 32158fb81238SShri Abhyankar } 32168fb81238SShri Abhyankar } else mmax = m; 32178fb81238SShri Abhyankar 32188fb81238SShri Abhyankar rowners[0] = 0; 32198fb81238SShri Abhyankar for (i=2; i<=size; i++) { 32208fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 32218fb81238SShri Abhyankar } 32228fb81238SShri Abhyankar rstart = rowners[rank]; 32238fb81238SShri Abhyankar rend = rowners[rank+1]; 32248fb81238SShri Abhyankar 32258fb81238SShri Abhyankar /* distribute row lengths to all processors */ 32268fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 32278fb81238SShri Abhyankar if (!rank) { 32288fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 32298fb81238SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 32308fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 32318fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 32328fb81238SShri Abhyankar for (j=0; j<m; j++) { 32338fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 32348fb81238SShri Abhyankar } 32358fb81238SShri Abhyankar for (i=1; i<size; i++) { 32368fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 32378fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 32388fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 32398fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 32408fb81238SShri Abhyankar } 32411a4ee126SBarry Smith ierr = MPILong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32428fb81238SShri Abhyankar } 32438fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 32448fb81238SShri Abhyankar } else { 32451a4ee126SBarry Smith ierr = MPILong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32468fb81238SShri Abhyankar } 32478fb81238SShri Abhyankar 32488fb81238SShri Abhyankar if (!rank) { 32498fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 32508fb81238SShri Abhyankar maxnz = 0; 32518fb81238SShri Abhyankar for (i=0; i<size; i++) { 32528fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 32538fb81238SShri Abhyankar } 32548fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 32558fb81238SShri Abhyankar 32568fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 32578fb81238SShri Abhyankar nz = procsnz[0]; 32588fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32598fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 32608fb81238SShri Abhyankar 32618fb81238SShri Abhyankar /* read in every one elses and ship off */ 32628fb81238SShri Abhyankar for (i=1; i<size; i++) { 32638fb81238SShri Abhyankar nz = procsnz[i]; 32648fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 32651a4ee126SBarry Smith ierr = MPILong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 32668fb81238SShri Abhyankar } 32678fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 32688fb81238SShri Abhyankar } else { 32698fb81238SShri Abhyankar /* determine buffer space needed for message */ 32708fb81238SShri Abhyankar nz = 0; 32718fb81238SShri Abhyankar for (i=0; i<m; i++) { 32728fb81238SShri Abhyankar nz += ourlens[i]; 32738fb81238SShri Abhyankar } 32748fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 32758fb81238SShri Abhyankar 32768fb81238SShri Abhyankar /* receive message of column indices*/ 32771a4ee126SBarry Smith ierr = MPILong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 32788fb81238SShri Abhyankar } 32798fb81238SShri Abhyankar 32808fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 32818fb81238SShri Abhyankar if (N != M) { 32828fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 32838fb81238SShri Abhyankar else n = newMat->cmap->n; 32848fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 32858fb81238SShri Abhyankar cstart = cend - n; 32868fb81238SShri Abhyankar } else { 32878fb81238SShri Abhyankar cstart = rstart; 32888fb81238SShri Abhyankar cend = rend; 32898fb81238SShri Abhyankar n = cend - cstart; 32908fb81238SShri Abhyankar } 32918fb81238SShri Abhyankar 32928fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 32938fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 32948fb81238SShri Abhyankar jj = 0; 32958fb81238SShri Abhyankar for (i=0; i<m; i++) { 32968fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 32978fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 32988fb81238SShri Abhyankar jj++; 32998fb81238SShri Abhyankar } 33008fb81238SShri Abhyankar } 33018fb81238SShri Abhyankar 33028fb81238SShri Abhyankar for (i=0; i<m; i++) { 33038fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 33048fb81238SShri Abhyankar } 33058fb81238SShri Abhyankar if (!sizesset) { 33068fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 33078fb81238SShri Abhyankar } 33088fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 33098fb81238SShri Abhyankar 33108fb81238SShri Abhyankar for (i=0; i<m; i++) { 33118fb81238SShri Abhyankar ourlens[i] += offlens[i]; 33128fb81238SShri Abhyankar } 33138fb81238SShri Abhyankar 33148fb81238SShri Abhyankar if (!rank) { 33158fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 33168fb81238SShri Abhyankar 33178fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 33188fb81238SShri Abhyankar nz = procsnz[0]; 33198fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 33208fb81238SShri Abhyankar 33218fb81238SShri Abhyankar /* insert into matrix */ 33228fb81238SShri Abhyankar jj = rstart; 33238fb81238SShri Abhyankar smycols = mycols; 33248fb81238SShri Abhyankar svals = vals; 33258fb81238SShri Abhyankar for (i=0; i<m; i++) { 33268fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 33278fb81238SShri Abhyankar smycols += ourlens[i]; 33288fb81238SShri Abhyankar svals += ourlens[i]; 33298fb81238SShri Abhyankar jj++; 33308fb81238SShri Abhyankar } 33318fb81238SShri Abhyankar 33328fb81238SShri Abhyankar /* read in other processors and ship out */ 33338fb81238SShri Abhyankar for (i=1; i<size; i++) { 33348fb81238SShri Abhyankar nz = procsnz[i]; 33358fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 33361a4ee126SBarry Smith ierr = MPILong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33378fb81238SShri Abhyankar } 33388fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 33398fb81238SShri Abhyankar } else { 33408fb81238SShri Abhyankar /* receive numeric values */ 33418fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 33428fb81238SShri Abhyankar 33438fb81238SShri Abhyankar /* receive message of values*/ 33441a4ee126SBarry Smith ierr = MPILong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 33458fb81238SShri Abhyankar 33468fb81238SShri Abhyankar /* insert into matrix */ 33478fb81238SShri Abhyankar jj = rstart; 33488fb81238SShri Abhyankar smycols = mycols; 33498fb81238SShri Abhyankar svals = vals; 33508fb81238SShri Abhyankar for (i=0; i<m; i++) { 33518fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 33528fb81238SShri Abhyankar smycols += ourlens[i]; 33538fb81238SShri Abhyankar svals += ourlens[i]; 33548fb81238SShri Abhyankar jj++; 33558fb81238SShri Abhyankar } 33568fb81238SShri Abhyankar } 33578fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 33588fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 33598fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 33608fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 33618fb81238SShri Abhyankar 33628fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33638fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 33648fb81238SShri Abhyankar PetscFunctionReturn(0); 33658fb81238SShri Abhyankar } 33668fb81238SShri Abhyankar 33678fb81238SShri Abhyankar #undef __FUNCT__ 33684a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 33694aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 33704aa3045dSJed Brown { 33714aa3045dSJed Brown PetscErrorCode ierr; 33724aa3045dSJed Brown IS iscol_local; 33734aa3045dSJed Brown PetscInt csize; 33744aa3045dSJed Brown 33754aa3045dSJed Brown PetscFunctionBegin; 33764aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3377b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3378b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3379e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3380b79d0421SJed Brown } else { 33814aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3382b79d0421SJed Brown } 33834aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3384b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3385b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 33864aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 3387b79d0421SJed Brown } 33884aa3045dSJed Brown PetscFunctionReturn(0); 33894aa3045dSJed Brown } 33904aa3045dSJed Brown 33914aa3045dSJed Brown #undef __FUNCT__ 33924aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3393a0ff6018SBarry Smith /* 339429da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 339529da9460SBarry Smith in local and then by concatenating the local matrices the end result. 339629da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 33974aa3045dSJed Brown 33984aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3399a0ff6018SBarry Smith */ 34004aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3401a0ff6018SBarry Smith { 3402dfbe8321SBarry Smith PetscErrorCode ierr; 340332dcc486SBarry Smith PetscMPIInt rank,size; 3404b1d57f15SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j; 3405b1d57f15SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 3406fee21e36SBarry Smith Mat *local,M,Mreuse; 3407a77337e4SBarry Smith MatScalar *vwork,*aa; 34087adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 340900e6dbe6SBarry Smith Mat_SeqAIJ *aij; 34107e2c5f70SBarry Smith 3411a0ff6018SBarry Smith 3412a0ff6018SBarry Smith PetscFunctionBegin; 34131dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 34141dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 341500e6dbe6SBarry Smith 3416fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3417fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3418e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3419fee21e36SBarry Smith local = &Mreuse; 3420fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 3421fee21e36SBarry Smith } else { 3422a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3423fee21e36SBarry Smith Mreuse = *local; 3424606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 3425fee21e36SBarry Smith } 3426a0ff6018SBarry Smith 3427a0ff6018SBarry Smith /* 3428a0ff6018SBarry Smith m - number of local rows 3429a0ff6018SBarry Smith n - number of columns (same on all processors) 3430a0ff6018SBarry Smith rstart - first row in new global matrix generated 3431a0ff6018SBarry Smith */ 3432fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3433a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3434fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 343500e6dbe6SBarry Smith ii = aij->i; 343600e6dbe6SBarry Smith jj = aij->j; 343700e6dbe6SBarry Smith 3438a0ff6018SBarry Smith /* 343900e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 344000e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3441a0ff6018SBarry Smith */ 344200e6dbe6SBarry Smith 344300e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 34446a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3445ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3446ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3447e2c4fddaSBarry Smith nlocal = m; 34486a6a5d1dSBarry Smith } else { 3449ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3450ab50ec6bSBarry Smith } 3451ab50ec6bSBarry Smith } else { 34526a6a5d1dSBarry Smith nlocal = csize; 34536a6a5d1dSBarry Smith } 3454b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 345500e6dbe6SBarry Smith rstart = rend - nlocal; 345665e19b50SBarry 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); 345700e6dbe6SBarry Smith 345800e6dbe6SBarry Smith /* next, compute all the lengths */ 3459b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 346000e6dbe6SBarry Smith olens = dlens + m; 346100e6dbe6SBarry Smith for (i=0; i<m; i++) { 346200e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 346300e6dbe6SBarry Smith olen = 0; 346400e6dbe6SBarry Smith dlen = 0; 346500e6dbe6SBarry Smith for (j=0; j<jend; j++) { 346600e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 346700e6dbe6SBarry Smith else dlen++; 346800e6dbe6SBarry Smith jj++; 346900e6dbe6SBarry Smith } 347000e6dbe6SBarry Smith olens[i] = olen; 347100e6dbe6SBarry Smith dlens[i] = dlen; 347200e6dbe6SBarry Smith } 3473f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3474f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 34757adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3476e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3477606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3478a0ff6018SBarry Smith } else { 3479b1d57f15SBarry Smith PetscInt ml,nl; 3480a0ff6018SBarry Smith 3481a0ff6018SBarry Smith M = *newmat; 3482a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3483e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3484a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3485c48de900SBarry Smith /* 3486c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3487c48de900SBarry Smith rather than the slower MatSetValues(). 3488c48de900SBarry Smith */ 3489c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3490c48de900SBarry Smith M->assembled = PETSC_FALSE; 3491a0ff6018SBarry Smith } 3492a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3493fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 349400e6dbe6SBarry Smith ii = aij->i; 349500e6dbe6SBarry Smith jj = aij->j; 349600e6dbe6SBarry Smith aa = aij->a; 3497a0ff6018SBarry Smith for (i=0; i<m; i++) { 3498a0ff6018SBarry Smith row = rstart + i; 349900e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 350000e6dbe6SBarry Smith cwork = jj; jj += nz; 350100e6dbe6SBarry Smith vwork = aa; aa += nz; 35028c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3503a0ff6018SBarry Smith } 3504a0ff6018SBarry Smith 3505a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3506a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3507a0ff6018SBarry Smith *newmat = M; 3508fee21e36SBarry Smith 3509fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3510fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3511fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3512fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 3513fee21e36SBarry Smith } 3514fee21e36SBarry Smith 3515a0ff6018SBarry Smith PetscFunctionReturn(0); 3516a0ff6018SBarry Smith } 3517273d9f13SBarry Smith 3518e2e86b8fSSatish Balay EXTERN_C_BEGIN 35194a2ae208SSatish Balay #undef __FUNCT__ 3520ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 3521b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3522ccd8e176SBarry Smith { 3523899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3524899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3525ccd8e176SBarry Smith const PetscInt *JJ; 3526ccd8e176SBarry Smith PetscScalar *values; 3527ccd8e176SBarry Smith PetscErrorCode ierr; 3528ccd8e176SBarry Smith 3529ccd8e176SBarry Smith PetscFunctionBegin; 3530e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3531899cda47SBarry Smith 353226283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 353326283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 353426283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 353526283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3536d0f46423SBarry Smith m = B->rmap->n; 3537d0f46423SBarry Smith cstart = B->cmap->rstart; 3538d0f46423SBarry Smith cend = B->cmap->rend; 3539d0f46423SBarry Smith rstart = B->rmap->rstart; 3540899cda47SBarry Smith 35411d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3542ccd8e176SBarry Smith 3543ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3544ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3545ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3546ecc77c7aSBarry Smith JJ = J + Ii[i]; 3547e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3548ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3549d0f46423SBarry 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); 3550ecc77c7aSBarry Smith } 3551ecc77c7aSBarry Smith #endif 3552ecc77c7aSBarry Smith 3553ccd8e176SBarry Smith for (i=0; i<m; i++) { 3554b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3555b7940d39SSatish Balay JJ = J + Ii[i]; 3556ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3557ccd8e176SBarry Smith d = 0; 35580daa03b5SJed Brown for (j=0; j<nnz; j++) { 35590daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3560ccd8e176SBarry Smith } 3561ccd8e176SBarry Smith d_nnz[i] = d; 3562ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3563ccd8e176SBarry Smith } 3564ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 35651d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3566ccd8e176SBarry Smith 3567ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3568ccd8e176SBarry Smith else { 3569ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3570ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3571ccd8e176SBarry Smith } 3572ccd8e176SBarry Smith 3573ccd8e176SBarry Smith for (i=0; i<m; i++) { 3574ccd8e176SBarry Smith ii = i + rstart; 3575b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3576b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3577ccd8e176SBarry Smith } 3578ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3579ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3580ccd8e176SBarry Smith 3581ccd8e176SBarry Smith if (!v) { 3582ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3583ccd8e176SBarry Smith } 3584ccd8e176SBarry Smith PetscFunctionReturn(0); 3585ccd8e176SBarry Smith } 3586e2e86b8fSSatish Balay EXTERN_C_END 3587ccd8e176SBarry Smith 3588ccd8e176SBarry Smith #undef __FUNCT__ 3589ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 35901eea217eSSatish Balay /*@ 3591ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3592ccd8e176SBarry Smith (the default parallel PETSc format). 3593ccd8e176SBarry Smith 3594ccd8e176SBarry Smith Collective on MPI_Comm 3595ccd8e176SBarry Smith 3596ccd8e176SBarry Smith Input Parameters: 3597a1661176SMatthew Knepley + B - the matrix 3598ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 35990daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3600ccd8e176SBarry Smith - v - optional values in the matrix 3601ccd8e176SBarry Smith 3602ccd8e176SBarry Smith Level: developer 3603ccd8e176SBarry Smith 360412251496SSatish Balay Notes: 360512251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 360612251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 360712251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 360812251496SSatish Balay 360912251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 361012251496SSatish Balay 361112251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 361212251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 361312251496SSatish Balay as shown: 361412251496SSatish Balay 361512251496SSatish Balay 1 0 0 361612251496SSatish Balay 2 0 3 P0 361712251496SSatish Balay ------- 361812251496SSatish Balay 4 5 6 P1 361912251496SSatish Balay 362012251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 362112251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 362212251496SSatish Balay j = {0,0,2} [size = nz = 6] 362312251496SSatish Balay v = {1,2,3} [size = nz = 6] 362412251496SSatish Balay 362512251496SSatish Balay Process1 [P1]: rows_owned=[2] 362612251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 362712251496SSatish Balay j = {0,1,2} [size = nz = 6] 362812251496SSatish Balay v = {4,5,6} [size = nz = 6] 362912251496SSatish Balay 3630ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3631ccd8e176SBarry Smith 36322fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ, 36338d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3634ccd8e176SBarry Smith @*/ 3635be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3636ccd8e176SBarry Smith { 36374ac538c5SBarry Smith PetscErrorCode ierr; 3638ccd8e176SBarry Smith 3639ccd8e176SBarry Smith PetscFunctionBegin; 36404ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3641ccd8e176SBarry Smith PetscFunctionReturn(0); 3642ccd8e176SBarry Smith } 3643ccd8e176SBarry Smith 3644ccd8e176SBarry Smith #undef __FUNCT__ 36454a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3646273d9f13SBarry Smith /*@C 3647ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3648273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3649273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3650273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3651273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3652273d9f13SBarry Smith 3653273d9f13SBarry Smith Collective on MPI_Comm 3654273d9f13SBarry Smith 3655273d9f13SBarry Smith Input Parameters: 3656273d9f13SBarry Smith + A - the matrix 3657273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3658273d9f13SBarry Smith (same value is used for all local rows) 3659273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3660273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3661273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3662273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3663273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3664273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3665273d9f13SBarry Smith submatrix (same value is used for all local rows). 3666273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3667273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3668273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3669273d9f13SBarry Smith structure. The size of this array is equal to the number 3670273d9f13SBarry Smith of local rows, i.e 'm'. 3671273d9f13SBarry Smith 367249a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 367349a6f317SBarry Smith 3674273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3675ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 36760598bfebSBarry Smith storage. The stored row and column indices begin with zero. 36770598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3678273d9f13SBarry Smith 3679273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3680273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3681273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3682273d9f13SBarry Smith 3683273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3684a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3685a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3686a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3687a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3688a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3689a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3690a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3691a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3692273d9f13SBarry Smith 3693273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3694273d9f13SBarry Smith 3695aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3696aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3697aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3698aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3699aa95bbe8SBarry Smith 3700273d9f13SBarry Smith Example usage: 3701273d9f13SBarry Smith 3702273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3703273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3704273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3705273d9f13SBarry Smith as follows: 3706273d9f13SBarry Smith 3707273d9f13SBarry Smith .vb 3708273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3709273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3710273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3711273d9f13SBarry Smith ------------------------------------- 3712273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3713273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3714273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3715273d9f13SBarry Smith ------------------------------------- 3716273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3717273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3718273d9f13SBarry Smith .ve 3719273d9f13SBarry Smith 3720273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3721273d9f13SBarry Smith 3722273d9f13SBarry Smith .vb 3723273d9f13SBarry Smith A B C 3724273d9f13SBarry Smith D E F 3725273d9f13SBarry Smith G H I 3726273d9f13SBarry Smith .ve 3727273d9f13SBarry Smith 3728273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3729273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3730273d9f13SBarry Smith 3731273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3732273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3733273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3734273d9f13SBarry Smith 3735273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3736273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3737273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3738273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3739273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3740273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3741273d9f13SBarry Smith 3742273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3743273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3744273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3745273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3746273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3747273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3748273d9f13SBarry Smith .vb 3749273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3750273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3751273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3752273d9f13SBarry Smith .ve 3753273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3754273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3755273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3756273d9f13SBarry Smith 34 values. 3757273d9f13SBarry Smith 3758273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3759273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3760273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3761273d9f13SBarry Smith .vb 3762273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3763273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3764273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3765273d9f13SBarry Smith .ve 3766273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3767273d9f13SBarry Smith hence pre-allocation is perfect. 3768273d9f13SBarry Smith 3769273d9f13SBarry Smith Level: intermediate 3770273d9f13SBarry Smith 3771273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3772273d9f13SBarry Smith 3773ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(), 3774aa95bbe8SBarry Smith MPIAIJ, MatGetInfo() 3775273d9f13SBarry Smith @*/ 3776be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3777273d9f13SBarry Smith { 37784ac538c5SBarry Smith PetscErrorCode ierr; 3779273d9f13SBarry Smith 3780273d9f13SBarry Smith PetscFunctionBegin; 37814ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 3782273d9f13SBarry Smith PetscFunctionReturn(0); 3783273d9f13SBarry Smith } 3784273d9f13SBarry Smith 37854a2ae208SSatish Balay #undef __FUNCT__ 37862fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 378758d36128SBarry Smith /*@ 37882fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 37892fb0ec9aSBarry Smith CSR format the local rows. 37902fb0ec9aSBarry Smith 37912fb0ec9aSBarry Smith Collective on MPI_Comm 37922fb0ec9aSBarry Smith 37932fb0ec9aSBarry Smith Input Parameters: 37942fb0ec9aSBarry Smith + comm - MPI communicator 37952fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 37962fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 37972fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 37982fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 37992fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 38002fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 38012fb0ec9aSBarry Smith . i - row indices 38022fb0ec9aSBarry Smith . j - column indices 38032fb0ec9aSBarry Smith - a - matrix values 38042fb0ec9aSBarry Smith 38052fb0ec9aSBarry Smith Output Parameter: 38062fb0ec9aSBarry Smith . mat - the matrix 380703bfb495SBarry Smith 38082fb0ec9aSBarry Smith Level: intermediate 38092fb0ec9aSBarry Smith 38102fb0ec9aSBarry Smith Notes: 38112fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 38122fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 38138d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 38142fb0ec9aSBarry Smith 381512251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 381612251496SSatish Balay 381712251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 381812251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 381912251496SSatish Balay as shown: 382012251496SSatish Balay 382112251496SSatish Balay 1 0 0 382212251496SSatish Balay 2 0 3 P0 382312251496SSatish Balay ------- 382412251496SSatish Balay 4 5 6 P1 382512251496SSatish Balay 382612251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 382712251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 382812251496SSatish Balay j = {0,0,2} [size = nz = 6] 382912251496SSatish Balay v = {1,2,3} [size = nz = 6] 383012251496SSatish Balay 383112251496SSatish Balay Process1 [P1]: rows_owned=[2] 383212251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 383312251496SSatish Balay j = {0,1,2} [size = nz = 6] 383412251496SSatish Balay v = {4,5,6} [size = nz = 6] 38352fb0ec9aSBarry Smith 38362fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 38372fb0ec9aSBarry Smith 38382fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 38398d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 38402fb0ec9aSBarry Smith @*/ 384182b90586SSatish 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) 38422fb0ec9aSBarry Smith { 38432fb0ec9aSBarry Smith PetscErrorCode ierr; 38442fb0ec9aSBarry Smith 38452fb0ec9aSBarry Smith PetscFunctionBegin; 38462fb0ec9aSBarry Smith if (i[0]) { 3847e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 38482fb0ec9aSBarry Smith } 3849e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 38502fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3851d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 38522fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 38532fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 38542fb0ec9aSBarry Smith PetscFunctionReturn(0); 38552fb0ec9aSBarry Smith } 38562fb0ec9aSBarry Smith 38572fb0ec9aSBarry Smith #undef __FUNCT__ 38584a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 3859273d9f13SBarry Smith /*@C 3860273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 3861273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3862273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3863273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3864273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3865273d9f13SBarry Smith 3866273d9f13SBarry Smith Collective on MPI_Comm 3867273d9f13SBarry Smith 3868273d9f13SBarry Smith Input Parameters: 3869273d9f13SBarry Smith + comm - MPI communicator 3870273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 3871273d9f13SBarry Smith This value should be the same as the local size used in creating the 3872273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 3873273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 3874273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 3875273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 3876273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3877273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 3878273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3879273d9f13SBarry Smith (same value is used for all local rows) 3880273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3881273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3882273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3883273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3884273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3885273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3886273d9f13SBarry Smith submatrix (same value is used for all local rows). 3887273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3888273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3889273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3890273d9f13SBarry Smith structure. The size of this array is equal to the number 3891273d9f13SBarry Smith of local rows, i.e 'm'. 3892273d9f13SBarry Smith 3893273d9f13SBarry Smith Output Parameter: 3894273d9f13SBarry Smith . A - the matrix 3895273d9f13SBarry Smith 3896175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3897ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3898175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3899175b88e8SBarry Smith 3900273d9f13SBarry Smith Notes: 390149a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 390249a6f317SBarry Smith 3903273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 3904273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 3905273d9f13SBarry Smith storage requirements for this matrix. 3906273d9f13SBarry Smith 3907273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 3908273d9f13SBarry Smith processor than it must be used on all processors that share the object for 3909273d9f13SBarry Smith that argument. 3910273d9f13SBarry Smith 3911273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 3912273d9f13SBarry Smith (possibly both). 3913273d9f13SBarry Smith 391433a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 391533a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 391633a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 391733a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 391833a7c187SSatish Balay values corresponding to [m x N] submatrix. 3919273d9f13SBarry Smith 392033a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 392133a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 392233a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 392333a7c187SSatish Balay 392433a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 392533a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 392633a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 392733a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 392833a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 392933a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 393033a7c187SSatish Balay illustrates this concept. 393133a7c187SSatish Balay 393233a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 393333a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 393433a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 393533a7c187SSatish Balay local matrix (a rectangular submatrix). 3936273d9f13SBarry Smith 3937273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3938273d9f13SBarry Smith 393997d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 394097d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 394197d05335SKris Buschelman type of communicator, use the construction mechanism: 394278102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 394397d05335SKris Buschelman 3944273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 3945273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 3946273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3947273d9f13SBarry Smith 3948273d9f13SBarry Smith Options Database Keys: 3949923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 3950923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3951273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3952273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3953273d9f13SBarry Smith the user still MUST index entries starting at 0! 3954273d9f13SBarry Smith 3955273d9f13SBarry Smith 3956273d9f13SBarry Smith Example usage: 3957273d9f13SBarry Smith 3958273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3959273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3960273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3961273d9f13SBarry Smith as follows: 3962273d9f13SBarry Smith 3963273d9f13SBarry Smith .vb 3964273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3965273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3966273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3967273d9f13SBarry Smith ------------------------------------- 3968273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3969273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3970273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3971273d9f13SBarry Smith ------------------------------------- 3972273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3973273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3974273d9f13SBarry Smith .ve 3975273d9f13SBarry Smith 3976273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3977273d9f13SBarry Smith 3978273d9f13SBarry Smith .vb 3979273d9f13SBarry Smith A B C 3980273d9f13SBarry Smith D E F 3981273d9f13SBarry Smith G H I 3982273d9f13SBarry Smith .ve 3983273d9f13SBarry Smith 3984273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3985273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3986273d9f13SBarry Smith 3987273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3988273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3989273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3990273d9f13SBarry Smith 3991273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3992273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3993273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3994273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3995273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3996273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3997273d9f13SBarry Smith 3998273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3999273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4000273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4001273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4002273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4003273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4004273d9f13SBarry Smith .vb 4005273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4006273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4007273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4008273d9f13SBarry Smith .ve 4009273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4010273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4011273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4012273d9f13SBarry Smith 34 values. 4013273d9f13SBarry Smith 4014273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4015273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4016273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4017273d9f13SBarry Smith .vb 4018273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4019273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4020273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4021273d9f13SBarry Smith .ve 4022273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4023273d9f13SBarry Smith hence pre-allocation is perfect. 4024273d9f13SBarry Smith 4025273d9f13SBarry Smith Level: intermediate 4026273d9f13SBarry Smith 4027273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4028273d9f13SBarry Smith 4029ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 40302fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4031273d9f13SBarry Smith @*/ 4032be1d678aSKris 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) 4033273d9f13SBarry Smith { 40346849ba73SBarry Smith PetscErrorCode ierr; 4035b1d57f15SBarry Smith PetscMPIInt size; 4036273d9f13SBarry Smith 4037273d9f13SBarry Smith PetscFunctionBegin; 4038f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4039f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4040273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4041273d9f13SBarry Smith if (size > 1) { 4042273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4043273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4044273d9f13SBarry Smith } else { 4045273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4046273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4047273d9f13SBarry Smith } 4048273d9f13SBarry Smith PetscFunctionReturn(0); 4049273d9f13SBarry Smith } 4050195d93cdSBarry Smith 40514a2ae208SSatish Balay #undef __FUNCT__ 40524a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 4053be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 4054195d93cdSBarry Smith { 4055195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 4056b1d57f15SBarry Smith 4057195d93cdSBarry Smith PetscFunctionBegin; 4058195d93cdSBarry Smith *Ad = a->A; 4059195d93cdSBarry Smith *Ao = a->B; 4060195d93cdSBarry Smith *colmap = a->garray; 4061195d93cdSBarry Smith PetscFunctionReturn(0); 4062195d93cdSBarry Smith } 4063a2243be0SBarry Smith 4064a2243be0SBarry Smith #undef __FUNCT__ 4065a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4066dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4067a2243be0SBarry Smith { 4068dfbe8321SBarry Smith PetscErrorCode ierr; 4069b1d57f15SBarry Smith PetscInt i; 4070a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4071a2243be0SBarry Smith 4072a2243be0SBarry Smith PetscFunctionBegin; 40738ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 407408b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4075a2243be0SBarry Smith ISColoring ocoloring; 4076a2243be0SBarry Smith 4077a2243be0SBarry Smith /* set coloring for diagonal portion */ 4078a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4079a2243be0SBarry Smith 4080a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 40817adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 4082d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4083d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4084a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4085a2243be0SBarry Smith } 4086a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4087d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4088a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4089a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4090a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 409108b6dcc0SBarry Smith ISColoringValue *colors; 4092b1d57f15SBarry Smith PetscInt *larray; 4093a2243be0SBarry Smith ISColoring ocoloring; 4094a2243be0SBarry Smith 4095a2243be0SBarry Smith /* set coloring for diagonal portion */ 4096d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4097d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4098d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4099a2243be0SBarry Smith } 4100784ac674SJed Brown ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 4101d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4102d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4103a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4104a2243be0SBarry Smith } 4105a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4106d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4107a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 4108a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4109a2243be0SBarry Smith 4110a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4111d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4112784ac674SJed Brown ierr = ISGlobalToLocalMappingApply(A->cmapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 4113d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4114d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4115a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4116a2243be0SBarry Smith } 4117a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4118d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4119a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 4120a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 4121a2243be0SBarry Smith } else { 4122e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4123a2243be0SBarry Smith } 4124a2243be0SBarry Smith 4125a2243be0SBarry Smith PetscFunctionReturn(0); 4126a2243be0SBarry Smith } 4127a2243be0SBarry Smith 4128dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 4129a2243be0SBarry Smith #undef __FUNCT__ 4130779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 4131dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 4132a2243be0SBarry Smith { 4133a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4134dfbe8321SBarry Smith PetscErrorCode ierr; 4135a2243be0SBarry Smith 4136a2243be0SBarry Smith PetscFunctionBegin; 4137779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 4138779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 4139779c1a83SBarry Smith PetscFunctionReturn(0); 4140779c1a83SBarry Smith } 4141dcf5cc72SBarry Smith #endif 4142779c1a83SBarry Smith 4143779c1a83SBarry Smith #undef __FUNCT__ 4144779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4145b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4146779c1a83SBarry Smith { 4147779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4148dfbe8321SBarry Smith PetscErrorCode ierr; 4149779c1a83SBarry Smith 4150779c1a83SBarry Smith PetscFunctionBegin; 4151779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4152779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4153a2243be0SBarry Smith PetscFunctionReturn(0); 4154a2243be0SBarry Smith } 4155c5d6d63eSBarry Smith 4156c5d6d63eSBarry Smith #undef __FUNCT__ 415751dd7536SBarry Smith #define __FUNCT__ "MatMerge" 4158bc08b0f1SBarry Smith /*@ 415951dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 416051dd7536SBarry Smith matrices from each processor 4161c5d6d63eSBarry Smith 4162c5d6d63eSBarry Smith Collective on MPI_Comm 4163c5d6d63eSBarry Smith 4164c5d6d63eSBarry Smith Input Parameters: 416551dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4166d6bb3c2dSHong Zhang . inmat - the input sequential matrices 41670e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4168d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 416951dd7536SBarry Smith 417051dd7536SBarry Smith Output Parameter: 417151dd7536SBarry Smith . outmat - the parallel matrix generated 4172c5d6d63eSBarry Smith 41737e25d530SSatish Balay Level: advanced 41747e25d530SSatish Balay 4175f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4176c5d6d63eSBarry Smith 4177c5d6d63eSBarry Smith @*/ 4178be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4179c5d6d63eSBarry Smith { 4180dfbe8321SBarry Smith PetscErrorCode ierr; 4181b7940d39SSatish Balay PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz; 4182ba8c8a56SBarry Smith PetscInt *indx; 4183ba8c8a56SBarry Smith PetscScalar *values; 4184c5d6d63eSBarry Smith 4185c5d6d63eSBarry Smith PetscFunctionBegin; 41860e36024fSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4187d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4188d6bb3c2dSHong Zhang /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 41890e36024fSHong Zhang if (n == PETSC_DECIDE){ 4190357abbc8SBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 41910e36024fSHong Zhang } 4192357abbc8SBarry Smith ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4193357abbc8SBarry Smith rstart -= m; 4194d6bb3c2dSHong Zhang 4195d6bb3c2dSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4196d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4197ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4198d6bb3c2dSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 4199ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 4200d6bb3c2dSHong Zhang } 4201d6bb3c2dSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 4202f69a0ea3SMatthew Knepley ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 4203f69a0ea3SMatthew Knepley ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4204d6bb3c2dSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 4205d6bb3c2dSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 4206d6bb3c2dSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 4207d6bb3c2dSHong Zhang 4208d6bb3c2dSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 4209d6bb3c2dSHong Zhang ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 4210d6bb3c2dSHong Zhang } else { 4211e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 4212d6bb3c2dSHong Zhang } 4213d6bb3c2dSHong Zhang 4214d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4215ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4216b7940d39SSatish Balay Ii = i + rstart; 4217b7940d39SSatish Balay ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4218ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4219d6bb3c2dSHong Zhang } 4220d6bb3c2dSHong Zhang ierr = MatDestroy(inmat);CHKERRQ(ierr); 4221d6bb3c2dSHong Zhang ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4222d6bb3c2dSHong Zhang ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 422351dd7536SBarry Smith 4224c5d6d63eSBarry Smith PetscFunctionReturn(0); 4225c5d6d63eSBarry Smith } 4226c5d6d63eSBarry Smith 4227c5d6d63eSBarry Smith #undef __FUNCT__ 4228c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4229dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4230c5d6d63eSBarry Smith { 4231dfbe8321SBarry Smith PetscErrorCode ierr; 423232dcc486SBarry Smith PetscMPIInt rank; 4233b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4234de4209c5SBarry Smith size_t len; 4235b1d57f15SBarry Smith const PetscInt *indx; 4236c5d6d63eSBarry Smith PetscViewer out; 4237c5d6d63eSBarry Smith char *name; 4238c5d6d63eSBarry Smith Mat B; 4239b3cc6726SBarry Smith const PetscScalar *values; 4240c5d6d63eSBarry Smith 4241c5d6d63eSBarry Smith PetscFunctionBegin; 4242c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4243c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4244f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4245f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4246f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4247f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4248f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4249c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4250c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4251c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4252c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4253c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4254c5d6d63eSBarry Smith } 4255c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4256c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4257c5d6d63eSBarry Smith 42587adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4259c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4260c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4261c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4262852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4263c5d6d63eSBarry Smith ierr = PetscFree(name); 4264c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 4265c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 4266c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 4267c5d6d63eSBarry Smith PetscFunctionReturn(0); 4268c5d6d63eSBarry Smith } 4269e5f2cdd8SHong Zhang 4270*09573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 427151a7d1a8SHong Zhang #undef __FUNCT__ 427251a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 4273be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 427451a7d1a8SHong Zhang { 427551a7d1a8SHong Zhang PetscErrorCode ierr; 4276671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4277776b82aeSLisandro Dalcin PetscContainer container; 427851a7d1a8SHong Zhang 427951a7d1a8SHong Zhang PetscFunctionBegin; 4280671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4281671beff6SHong Zhang if (container) { 4282776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 428351a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 42843e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 42853e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 428651a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 428751a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4288533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 428902c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4290533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 429102c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 429205b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 429305b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 429405b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 429526283091SBarry Smith ierr = PetscLayoutDestroy(merge->rowmap);CHKERRQ(ierr); 4296671beff6SHong Zhang 4297776b82aeSLisandro Dalcin ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 4298671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4299671beff6SHong Zhang } 430051a7d1a8SHong Zhang ierr = PetscFree(merge);CHKERRQ(ierr); 430151a7d1a8SHong Zhang 430251a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 430351a7d1a8SHong Zhang PetscFunctionReturn(0); 430451a7d1a8SHong Zhang } 430551a7d1a8SHong Zhang 43067c4f633dSBarry Smith #include "../src/mat/utils/freespace.h" 4307be0fcf8dSHong Zhang #include "petscbt.h" 43084ebed01fSBarry Smith 4309e5f2cdd8SHong Zhang #undef __FUNCT__ 431038f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric" 4311e5f2cdd8SHong Zhang /*@C 4312f08fae4eSHong Zhang MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential 4313e5f2cdd8SHong Zhang matrices from each processor 4314e5f2cdd8SHong Zhang 4315e5f2cdd8SHong Zhang Collective on MPI_Comm 4316e5f2cdd8SHong Zhang 4317e5f2cdd8SHong Zhang Input Parameters: 4318e5f2cdd8SHong Zhang + comm - the communicators the parallel matrix will live on 4319f08fae4eSHong Zhang . seqmat - the input sequential matrices 43200e36024fSHong Zhang . m - number of local rows (or PETSC_DECIDE) 43210e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4322e5f2cdd8SHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4323e5f2cdd8SHong Zhang 4324e5f2cdd8SHong Zhang Output Parameter: 4325f08fae4eSHong Zhang . mpimat - the parallel matrix generated 4326e5f2cdd8SHong Zhang 4327e5f2cdd8SHong Zhang Level: advanced 4328e5f2cdd8SHong Zhang 4329affca5deSHong Zhang Notes: 4330affca5deSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4331affca5deSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4332affca5deSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4333e5f2cdd8SHong Zhang @*/ 4334be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat) 433555d1abb9SHong Zhang { 433655d1abb9SHong Zhang PetscErrorCode ierr; 43377adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 433855d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4339b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4340d0f46423SBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j; 4341b1d57f15SBarry Smith PetscInt proc,m; 4342b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4343b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4344b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 434555d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 434655d1abb9SHong Zhang MPI_Status *status; 4347a77337e4SBarry Smith MatScalar *aa=a->a; 4348dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 434955d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4350776b82aeSLisandro Dalcin PetscContainer container; 435155d1abb9SHong Zhang 435255d1abb9SHong Zhang PetscFunctionBegin; 43534ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 43543c2c1871SHong Zhang 435555d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 435655d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 435755d1abb9SHong Zhang 435855d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 435955d1abb9SHong Zhang if (container) { 4360776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 436155d1abb9SHong Zhang } 436255d1abb9SHong Zhang bi = merge->bi; 436355d1abb9SHong Zhang bj = merge->bj; 436455d1abb9SHong Zhang buf_ri = merge->buf_ri; 436555d1abb9SHong Zhang buf_rj = merge->buf_rj; 436655d1abb9SHong Zhang 436755d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 43687a2fc3feSBarry Smith owners = merge->rowmap->range; 436955d1abb9SHong Zhang len_s = merge->len_s; 437055d1abb9SHong Zhang 437155d1abb9SHong Zhang /* send and recv matrix values */ 437255d1abb9SHong Zhang /*-----------------------------*/ 4373357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 437455d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 437555d1abb9SHong Zhang 437655d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 437755d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 437855d1abb9SHong Zhang if (!len_s[proc]) continue; 437955d1abb9SHong Zhang i = owners[proc]; 438055d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 438155d1abb9SHong Zhang k++; 438255d1abb9SHong Zhang } 438355d1abb9SHong Zhang 43840c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 43850c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 438655d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 438755d1abb9SHong Zhang 438855d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 438955d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 439055d1abb9SHong Zhang 439155d1abb9SHong Zhang /* insert mat values of mpimat */ 439255d1abb9SHong Zhang /*----------------------------*/ 4393a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 43940572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 439555d1abb9SHong Zhang 439655d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 439755d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 439855d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 439955d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 440055d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 440155d1abb9SHong Zhang } 440255d1abb9SHong Zhang 440355d1abb9SHong Zhang /* set values of ba */ 44047a2fc3feSBarry Smith m = merge->rowmap->n; 440555d1abb9SHong Zhang for (i=0; i<m; i++) { 440655d1abb9SHong Zhang arow = owners[rank] + i; 440755d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 440855d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4409a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 441055d1abb9SHong Zhang 441155d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 441255d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 441355d1abb9SHong Zhang aj = a->j + ai[arow]; 441455d1abb9SHong Zhang aa = a->a + ai[arow]; 441555d1abb9SHong Zhang nextaj = 0; 441655d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 441755d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 441855d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 441955d1abb9SHong Zhang } 442055d1abb9SHong Zhang } 442155d1abb9SHong Zhang 442255d1abb9SHong Zhang /* add received vals into ba */ 442355d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 442455d1abb9SHong Zhang /* i-th row */ 442555d1abb9SHong Zhang if (i == *nextrow[k]) { 442655d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 442755d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 442855d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 442955d1abb9SHong Zhang nextaj = 0; 443055d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 443155d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 443255d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 443355d1abb9SHong Zhang } 443455d1abb9SHong Zhang } 443555d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 443655d1abb9SHong Zhang } 443755d1abb9SHong Zhang } 443855d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 443955d1abb9SHong Zhang } 444055d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 444155d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 444255d1abb9SHong Zhang 4443533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 444455d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 444555d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 44461d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 44474ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 444855d1abb9SHong Zhang PetscFunctionReturn(0); 444955d1abb9SHong Zhang } 445038f152feSBarry Smith 445138f152feSBarry Smith #undef __FUNCT__ 445238f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic" 4453be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4454e5f2cdd8SHong Zhang { 4455f08fae4eSHong Zhang PetscErrorCode ierr; 445655a3bba9SHong Zhang Mat B_mpi; 4457c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4458b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4459b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4460d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4461b1d57f15SBarry Smith PetscInt len,proc,*dnz,*onz; 4462b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4463b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 446455d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 446558cb9c82SHong Zhang MPI_Status *status; 4466a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4467be0fcf8dSHong Zhang PetscBT lnkbt; 446851a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4469776b82aeSLisandro Dalcin PetscContainer container; 447002c68681SHong Zhang 4471e5f2cdd8SHong Zhang PetscFunctionBegin; 44724ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 44733c2c1871SHong Zhang 447438f152feSBarry Smith /* make sure it is a PETSc comm */ 447538f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4476e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4477e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 447855d1abb9SHong Zhang 447951a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4480c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4481e5f2cdd8SHong Zhang 44826abd8857SHong Zhang /* determine row ownership */ 4483f08fae4eSHong Zhang /*---------------------------------------------------------*/ 448426283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 448526283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 448626283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 448726283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 448826283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4489b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4490b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 449155d1abb9SHong Zhang 44927a2fc3feSBarry Smith m = merge->rowmap->n; 44937a2fc3feSBarry Smith M = merge->rowmap->N; 44947a2fc3feSBarry Smith owners = merge->rowmap->range; 44956abd8857SHong Zhang 44966abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 44976abd8857SHong Zhang /*---------------------------------------------------------*/ 44983e06a4e6SHong Zhang len_s = merge->len_s; 449951a7d1a8SHong Zhang 45002257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4501c2234fe3SHong Zhang merge->nsend = 0; 4502409913e3SHong Zhang for (proc=0; proc<size; proc++){ 45032257cef7SHong Zhang len_si[proc] = 0; 45043e06a4e6SHong Zhang if (proc == rank){ 45056abd8857SHong Zhang len_s[proc] = 0; 45063e06a4e6SHong Zhang } else { 450702c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 45083e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 45093e06a4e6SHong Zhang } 45103e06a4e6SHong Zhang if (len_s[proc]) { 4511c2234fe3SHong Zhang merge->nsend++; 45122257cef7SHong Zhang nrows = 0; 45132257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 45142257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 45152257cef7SHong Zhang } 45162257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 45172257cef7SHong Zhang len += len_si[proc]; 4518409913e3SHong Zhang } 451958cb9c82SHong Zhang } 4520409913e3SHong Zhang 45212257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 45222257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 452351a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 452455d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4525671beff6SHong Zhang 45263e06a4e6SHong Zhang /* post the Irecv of j-structure */ 45273e06a4e6SHong Zhang /*-------------------------------*/ 45282c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 45293e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 453002c68681SHong Zhang 45313e06a4e6SHong Zhang /* post the Isend of j-structure */ 4532affca5deSHong Zhang /*--------------------------------*/ 45331d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 45343e06a4e6SHong Zhang 45352257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4536409913e3SHong Zhang if (!len_s[proc]) continue; 453702c68681SHong Zhang i = owners[proc]; 4538b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 453951a7d1a8SHong Zhang k++; 454051a7d1a8SHong Zhang } 454151a7d1a8SHong Zhang 45423e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 45433e06a4e6SHong Zhang /*------------------------------------------------*/ 45440c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 45450c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 454602c68681SHong Zhang 454702c68681SHong Zhang /* send and recv i-structure */ 454802c68681SHong Zhang /*---------------------------*/ 45492c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 455002c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 455102c68681SHong Zhang 4552b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 45533e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 45542257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 455502c68681SHong Zhang if (!len_s[proc]) continue; 45563e06a4e6SHong Zhang /* form outgoing message for i-structure: 45573e06a4e6SHong Zhang buf_si[0]: nrows to be sent 45583e06a4e6SHong Zhang [1:nrows]: row index (global) 45593e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 45603e06a4e6SHong Zhang */ 45613e06a4e6SHong Zhang /*-------------------------------------------*/ 45622257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 45633e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 45643e06a4e6SHong Zhang buf_si[0] = nrows; 45653e06a4e6SHong Zhang buf_si_i[0] = 0; 45663e06a4e6SHong Zhang nrows = 0; 45673e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 45683e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 45693e06a4e6SHong Zhang if (anzi) { 45703e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 45713e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 45723e06a4e6SHong Zhang nrows++; 45733e06a4e6SHong Zhang } 45743e06a4e6SHong Zhang } 4575b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 457602c68681SHong Zhang k++; 45772257cef7SHong Zhang buf_si += len_si[proc]; 457802c68681SHong Zhang } 45792257cef7SHong Zhang 45800c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 45810c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 458202c68681SHong Zhang 4583ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 45843e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4585ae15b995SBarry 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); 45863e06a4e6SHong Zhang } 45873e06a4e6SHong Zhang 45883e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 458902c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 459002c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 45911d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 45922257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 45933e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4594bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 459558cb9c82SHong Zhang 4596bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4597bcc1bcd5SHong Zhang /*----------------------------------------------*/ 459858cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4599b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 460058cb9c82SHong Zhang bi[0] = 0; 460158cb9c82SHong Zhang 4602be0fcf8dSHong Zhang /* create and initialize a linked list */ 4603be0fcf8dSHong Zhang nlnk = N+1; 4604be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 460558cb9c82SHong Zhang 4606bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 460758cb9c82SHong Zhang len = 0; 4608bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4609a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 461058cb9c82SHong Zhang current_space = free_space; 461158cb9c82SHong Zhang 4612bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 46130572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 46141d79065fSBarry Smith 46153e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 46162257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 46173e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 46183e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 46192257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 46203e06a4e6SHong Zhang } 46212257cef7SHong Zhang 4622bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4623bcc1bcd5SHong Zhang len = 0; 462458cb9c82SHong Zhang for (i=0;i<m;i++) { 462558cb9c82SHong Zhang bnzi = 0; 462658cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 462758cb9c82SHong Zhang arow = owners[rank] + i; 462858cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 462958cb9c82SHong Zhang aj = a->j + ai[arow]; 4630be0fcf8dSHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 463158cb9c82SHong Zhang bnzi += nlnk; 463258cb9c82SHong Zhang /* add received col data into lnk */ 463351a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 463455d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 46353e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 46363e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 46373e06a4e6SHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 46383e06a4e6SHong Zhang bnzi += nlnk; 46393e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 46403e06a4e6SHong Zhang } 464158cb9c82SHong Zhang } 4642bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 464358cb9c82SHong Zhang 464458cb9c82SHong Zhang /* if free space is not available, make more free space */ 464558cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 46464238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 464758cb9c82SHong Zhang nspacedouble++; 464858cb9c82SHong Zhang } 464958cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4650be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4651bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4652bcc1bcd5SHong Zhang 465358cb9c82SHong Zhang current_space->array += bnzi; 465458cb9c82SHong Zhang current_space->local_used += bnzi; 465558cb9c82SHong Zhang current_space->local_remaining -= bnzi; 465658cb9c82SHong Zhang 465758cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 465858cb9c82SHong Zhang } 4659bcc1bcd5SHong Zhang 46601d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4661bcc1bcd5SHong Zhang 4662b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4663a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4664be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4665409913e3SHong Zhang 4666bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4667bcc1bcd5SHong Zhang /*---------------------------------------*/ 4668f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 466954b84b50SHong Zhang if (n==PETSC_DECIDE) { 4670f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 467154b84b50SHong Zhang } else { 4672f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 467354b84b50SHong Zhang } 4674bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4675bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4676bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 467758cb9c82SHong Zhang 46786abd8857SHong Zhang /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */ 46796abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4680affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4681affca5deSHong Zhang merge->bi = bi; 4682affca5deSHong Zhang merge->bj = bj; 468302c68681SHong Zhang merge->buf_ri = buf_ri; 468402c68681SHong Zhang merge->buf_rj = buf_rj; 4685de0260b3SHong Zhang merge->coi = PETSC_NULL; 4686de0260b3SHong Zhang merge->coj = PETSC_NULL; 4687de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4688affca5deSHong Zhang 4689affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4690776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4691776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4692affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4693affca5deSHong Zhang *mpimat = B_mpi; 469438f152feSBarry Smith 469538f152feSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 46964ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4697e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4698e5f2cdd8SHong Zhang } 469925616d81SHong Zhang 470038f152feSBarry Smith #undef __FUNCT__ 470138f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI" 4702be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 470355d1abb9SHong Zhang { 470455d1abb9SHong Zhang PetscErrorCode ierr; 470555d1abb9SHong Zhang 470655d1abb9SHong Zhang PetscFunctionBegin; 47074ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 470855d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 470955d1abb9SHong Zhang ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 471055d1abb9SHong Zhang } 471155d1abb9SHong Zhang ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr); 47124ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 471355d1abb9SHong Zhang PetscFunctionReturn(0); 471455d1abb9SHong Zhang } 47154ebed01fSBarry Smith 471625616d81SHong Zhang #undef __FUNCT__ 471725616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat" 4718bc08b0f1SBarry Smith /*@ 471932fba14fSHong Zhang MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows 472025616d81SHong Zhang 472132fba14fSHong Zhang Not Collective 472225616d81SHong Zhang 472325616d81SHong Zhang Input Parameters: 472425616d81SHong Zhang + A - the matrix 472525616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 472625616d81SHong Zhang 472725616d81SHong Zhang Output Parameter: 472825616d81SHong Zhang . A_loc - the local sequential matrix generated 472925616d81SHong Zhang 473025616d81SHong Zhang Level: developer 473125616d81SHong Zhang 473225616d81SHong Zhang @*/ 4733be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 473425616d81SHong Zhang { 473525616d81SHong Zhang PetscErrorCode ierr; 473601b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 473701b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 473801b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 4739a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 4740a77337e4SBarry Smith PetscScalar *ca; 4741d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 47425a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 474325616d81SHong Zhang 474425616d81SHong Zhang PetscFunctionBegin; 47454ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 474601b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4747dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 4748dea91ad1SHong Zhang ci[0] = 0; 474901b7ae99SHong Zhang for (i=0; i<am; i++){ 4750dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 475101b7ae99SHong Zhang } 4752dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 4753dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 4754dea91ad1SHong Zhang k = 0; 475501b7ae99SHong Zhang for (i=0; i<am; i++) { 47565a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47575a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 475801b7ae99SHong Zhang /* off-diagonal portion of A */ 47595a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47605a7d977cSHong Zhang col = cmap[*bj]; 47615a7d977cSHong Zhang if (col >= cstart) break; 47625a7d977cSHong Zhang cj[k] = col; bj++; 47635a7d977cSHong Zhang ca[k++] = *ba++; 47645a7d977cSHong Zhang } 47655a7d977cSHong Zhang /* diagonal portion of A */ 47665a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 47675a7d977cSHong Zhang cj[k] = cstart + *aj++; 47685a7d977cSHong Zhang ca[k++] = *aa++; 47695a7d977cSHong Zhang } 47705a7d977cSHong Zhang /* off-diagonal portion of A */ 47715a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 47725a7d977cSHong Zhang cj[k] = cmap[*bj++]; 47735a7d977cSHong Zhang ca[k++] = *ba++; 47745a7d977cSHong Zhang } 477525616d81SHong Zhang } 4776dea91ad1SHong Zhang /* put together the new matrix */ 4777d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 4778dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4779dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 4780dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 4781e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 4782e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 4783dea91ad1SHong Zhang mat->nonew = 0; 47845a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 47855a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 4786a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 47875a7d977cSHong Zhang for (i=0; i<am; i++) { 47885a7d977cSHong Zhang /* off-diagonal portion of A */ 47895a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 47905a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 47915a7d977cSHong Zhang col = cmap[*bj]; 47925a7d977cSHong Zhang if (col >= cstart) break; 4793a77337e4SBarry Smith *cam++ = *ba++; bj++; 47945a7d977cSHong Zhang } 47955a7d977cSHong Zhang /* diagonal portion of A */ 4796ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 4797a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 47985a7d977cSHong Zhang /* off-diagonal portion of A */ 4799f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 4800a77337e4SBarry Smith *cam++ = *ba++; bj++; 4801f33d1a9aSHong Zhang } 48025a7d977cSHong Zhang } 48035a7d977cSHong Zhang } else { 4804e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 480525616d81SHong Zhang } 480601b7ae99SHong Zhang 48074ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 480825616d81SHong Zhang PetscFunctionReturn(0); 480925616d81SHong Zhang } 481025616d81SHong Zhang 481132fba14fSHong Zhang #undef __FUNCT__ 481232fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed" 481332fba14fSHong Zhang /*@C 481432fba14fSHong Zhang MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns 481532fba14fSHong Zhang 481632fba14fSHong Zhang Not Collective 481732fba14fSHong Zhang 481832fba14fSHong Zhang Input Parameters: 481932fba14fSHong Zhang + A - the matrix 482032fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 482132fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 482232fba14fSHong Zhang 482332fba14fSHong Zhang Output Parameter: 482432fba14fSHong Zhang . A_loc - the local sequential matrix generated 482532fba14fSHong Zhang 482632fba14fSHong Zhang Level: developer 482732fba14fSHong Zhang 482832fba14fSHong Zhang @*/ 4829be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 483032fba14fSHong Zhang { 483132fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 483232fba14fSHong Zhang PetscErrorCode ierr; 483332fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 483432fba14fSHong Zhang IS isrowa,iscola; 483532fba14fSHong Zhang Mat *aloc; 483632fba14fSHong Zhang 483732fba14fSHong Zhang PetscFunctionBegin; 48384ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 483932fba14fSHong Zhang if (!row){ 4840d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 484132fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 484232fba14fSHong Zhang } else { 484332fba14fSHong Zhang isrowa = *row; 484432fba14fSHong Zhang } 484532fba14fSHong Zhang if (!col){ 4846d0f46423SBarry Smith start = A->cmap->rstart; 484732fba14fSHong Zhang cmap = a->garray; 4848d0f46423SBarry Smith nzA = a->A->cmap->n; 4849d0f46423SBarry Smith nzB = a->B->cmap->n; 485032fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 485132fba14fSHong Zhang ncols = 0; 485232fba14fSHong Zhang for (i=0; i<nzB; i++) { 485332fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 485432fba14fSHong Zhang else break; 485532fba14fSHong Zhang } 485632fba14fSHong Zhang imark = i; 485732fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 485832fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 4859d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 486032fba14fSHong Zhang } else { 486132fba14fSHong Zhang iscola = *col; 486232fba14fSHong Zhang } 486332fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 486432fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 486532fba14fSHong Zhang aloc[0] = *A_loc; 486632fba14fSHong Zhang } 486732fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 486832fba14fSHong Zhang *A_loc = aloc[0]; 486932fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 487032fba14fSHong Zhang if (!row){ 487132fba14fSHong Zhang ierr = ISDestroy(isrowa);CHKERRQ(ierr); 487232fba14fSHong Zhang } 487332fba14fSHong Zhang if (!col){ 487432fba14fSHong Zhang ierr = ISDestroy(iscola);CHKERRQ(ierr); 487532fba14fSHong Zhang } 48764ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 487732fba14fSHong Zhang PetscFunctionReturn(0); 487832fba14fSHong Zhang } 487932fba14fSHong Zhang 488025616d81SHong Zhang #undef __FUNCT__ 488125616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 488225616d81SHong Zhang /*@C 488332fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 488425616d81SHong Zhang 488525616d81SHong Zhang Collective on Mat 488625616d81SHong Zhang 488725616d81SHong Zhang Input Parameters: 4888e240928fSHong Zhang + A,B - the matrices in mpiaij format 488925616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 489025616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 489125616d81SHong Zhang 489225616d81SHong Zhang Output Parameter: 489325616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 4894d0f46423SBarry Smith . brstart - row index of B_seq from which next B->rmap->n rows are taken from B's local rows 489525616d81SHong Zhang - B_seq - the sequential matrix generated 489625616d81SHong Zhang 489725616d81SHong Zhang Level: developer 489825616d81SHong Zhang 489925616d81SHong Zhang @*/ 4900be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq) 490125616d81SHong Zhang { 4902899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 490325616d81SHong Zhang PetscErrorCode ierr; 4904b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 490525616d81SHong Zhang IS isrowb,iscolb; 490625616d81SHong Zhang Mat *bseq; 490725616d81SHong Zhang 490825616d81SHong Zhang PetscFunctionBegin; 4909d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4910e32f2f54SBarry 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); 491125616d81SHong Zhang } 49124ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 491325616d81SHong Zhang 491425616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4915d0f46423SBarry Smith start = A->cmap->rstart; 491625616d81SHong Zhang cmap = a->garray; 4917d0f46423SBarry Smith nzA = a->A->cmap->n; 4918d0f46423SBarry Smith nzB = a->B->cmap->n; 4919b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 492025616d81SHong Zhang ncols = 0; 49210390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 492225616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 492325616d81SHong Zhang else break; 492425616d81SHong Zhang } 492525616d81SHong Zhang imark = i; 49260390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 49270390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 4928d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 492925616d81SHong Zhang *brstart = imark; 4930d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 493125616d81SHong Zhang } else { 4932e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 493325616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 493425616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 493525616d81SHong Zhang bseq[0] = *B_seq; 493625616d81SHong Zhang } 493725616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 493825616d81SHong Zhang *B_seq = bseq[0]; 493925616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 494025616d81SHong Zhang if (!rowb){ 494125616d81SHong Zhang ierr = ISDestroy(isrowb);CHKERRQ(ierr); 494225616d81SHong Zhang } else { 494325616d81SHong Zhang *rowb = isrowb; 494425616d81SHong Zhang } 494525616d81SHong Zhang if (!colb){ 494625616d81SHong Zhang ierr = ISDestroy(iscolb);CHKERRQ(ierr); 494725616d81SHong Zhang } else { 494825616d81SHong Zhang *colb = iscolb; 494925616d81SHong Zhang } 49504ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 495125616d81SHong Zhang PetscFunctionReturn(0); 495225616d81SHong Zhang } 4953429d309bSHong Zhang 4954a61c8c0fSHong Zhang #undef __FUNCT__ 4955a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols" 4956429d309bSHong Zhang /*@C 4957429d309bSHong Zhang MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 495801b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 4959429d309bSHong Zhang 4960429d309bSHong Zhang Collective on Mat 4961429d309bSHong Zhang 4962429d309bSHong Zhang Input Parameters: 4963429d309bSHong Zhang + A,B - the matrices in mpiaij format 496487025532SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 496587025532SHong Zhang . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 49661d79065fSBarry Smith . startsj_r - similar to startsj for receives 496787025532SHong Zhang - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 4968429d309bSHong Zhang 4969429d309bSHong Zhang Output Parameter: 497087025532SHong Zhang + B_oth - the sequential matrix generated 4971429d309bSHong Zhang 4972429d309bSHong Zhang Level: developer 4973429d309bSHong Zhang 4974429d309bSHong Zhang @*/ 49751d79065fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 4976429d309bSHong Zhang { 4977a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 4978429d309bSHong Zhang PetscErrorCode ierr; 4979899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 498087025532SHong Zhang Mat_SeqAIJ *b_oth; 4981a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 49827adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 49837adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 4984d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 4985dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 4986dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 4987e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 4988910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 498987025532SHong Zhang MPI_Status *sstatus,rstatus; 4990aa5bb8c0SSatish Balay PetscMPIInt jj; 4991e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 4992ba8c8a56SBarry Smith PetscScalar *vals; 4993429d309bSHong Zhang 4994429d309bSHong Zhang PetscFunctionBegin; 4995d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4996e32f2f54SBarry 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); 4997429d309bSHong Zhang } 49984ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4999a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5000a6b2eed2SHong Zhang 5001a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5002a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5003e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5004e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5005a6b2eed2SHong Zhang nrecvs = gen_from->n; 5006a6b2eed2SHong Zhang nsends = gen_to->n; 5007d7ee0231SBarry Smith 5008d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 5009a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5010a6b2eed2SHong Zhang sstarts = gen_to->starts; 5011a6b2eed2SHong Zhang sprocs = gen_to->procs; 5012a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5013e42f35eeSHong Zhang sbs = gen_to->bs; 5014e42f35eeSHong Zhang rstarts = gen_from->starts; 5015e42f35eeSHong Zhang rprocs = gen_from->procs; 5016e42f35eeSHong Zhang rbs = gen_from->bs; 5017429d309bSHong Zhang 5018dea91ad1SHong Zhang if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5019429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5020a6b2eed2SHong Zhang /* i-array */ 5021a6b2eed2SHong Zhang /*---------*/ 5022a6b2eed2SHong Zhang /* post receives */ 5023a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5024e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5025e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 502687025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5027429d309bSHong Zhang } 5028a6b2eed2SHong Zhang 5029a6b2eed2SHong Zhang /* pack the outgoing message */ 50301d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 5031a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 5032a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5033a6b2eed2SHong Zhang k = 0; 5034a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5035e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5036e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 503787025532SHong Zhang for (j=0; j<nrows; j++) { 5038d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5039e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 5040e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 5041e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 5042e42f35eeSHong Zhang len += ncols; 5043e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5044e42f35eeSHong Zhang } 5045a6b2eed2SHong Zhang k++; 5046429d309bSHong Zhang } 5047e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5048dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5049429d309bSHong Zhang } 505087025532SHong Zhang /* recvs and sends of i-array are completed */ 505187025532SHong Zhang i = nrecvs; 505287025532SHong Zhang while (i--) { 5053aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 505487025532SHong Zhang } 50550c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5056e42f35eeSHong Zhang 5057a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5058a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 5059a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 5060a6b2eed2SHong Zhang 506187025532SHong Zhang /* create i-array of B_oth */ 506287025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 506387025532SHong Zhang b_othi[0] = 0; 5064a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5065a6b2eed2SHong Zhang k = 0; 5066a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5067fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5068e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 506987025532SHong Zhang for (j=0; j<nrows; j++) { 507087025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5071a6b2eed2SHong Zhang len += rowlen[j]; k++; 5072a6b2eed2SHong Zhang } 5073dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5074a6b2eed2SHong Zhang } 5075a6b2eed2SHong Zhang 507687025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 507787025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 5078dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 5079a6b2eed2SHong Zhang 508087025532SHong Zhang /* j-array */ 508187025532SHong Zhang /*---------*/ 5082a6b2eed2SHong Zhang /* post receives of j-array */ 5083a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 508487025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 508587025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5086a6b2eed2SHong Zhang } 5087e42f35eeSHong Zhang 5088e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5089a6b2eed2SHong Zhang k = 0; 5090a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5091e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5092a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 509387025532SHong Zhang for (j=0; j<nrows; j++) { 5094d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5095e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5096e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5097a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 5098a6b2eed2SHong Zhang *bufJ++ = cols[l]; 509987025532SHong Zhang } 5100e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5101e42f35eeSHong Zhang } 510287025532SHong Zhang } 510387025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 510487025532SHong Zhang } 510587025532SHong Zhang 510687025532SHong Zhang /* recvs and sends of j-array are completed */ 510787025532SHong Zhang i = nrecvs; 510887025532SHong Zhang while (i--) { 5109aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 511087025532SHong Zhang } 51110c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 511287025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 511387025532SHong Zhang sstartsj = *startsj; 51141d79065fSBarry Smith rstartsj = *startsj_r; 511587025532SHong Zhang bufa = *bufa_ptr; 511687025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 511787025532SHong Zhang b_otha = b_oth->a; 511887025532SHong Zhang } else { 5119e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 512087025532SHong Zhang } 512187025532SHong Zhang 512287025532SHong Zhang /* a-array */ 512387025532SHong Zhang /*---------*/ 512487025532SHong Zhang /* post receives of a-array */ 512587025532SHong Zhang for (i=0; i<nrecvs; i++){ 512687025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 512787025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 512887025532SHong Zhang } 5129e42f35eeSHong Zhang 5130e42f35eeSHong Zhang /* pack the outgoing message a-array */ 513187025532SHong Zhang k = 0; 513287025532SHong Zhang for (i=0; i<nsends; i++){ 5133e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 513487025532SHong Zhang bufA = bufa+sstartsj[i]; 513587025532SHong Zhang for (j=0; j<nrows; j++) { 5136d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5137e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5138e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 513987025532SHong Zhang for (l=0; l<ncols; l++){ 5140a6b2eed2SHong Zhang *bufA++ = vals[l]; 5141a6b2eed2SHong Zhang } 5142e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 5143e42f35eeSHong Zhang } 5144a6b2eed2SHong Zhang } 514587025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5146a6b2eed2SHong Zhang } 514787025532SHong Zhang /* recvs and sends of a-array are completed */ 514887025532SHong Zhang i = nrecvs; 514987025532SHong Zhang while (i--) { 5150aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 515187025532SHong Zhang } 51520c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5153d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5154a6b2eed2SHong Zhang 515587025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5156a6b2eed2SHong Zhang /* put together the new matrix */ 5157d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5158a6b2eed2SHong Zhang 5159a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5160a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 516187025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 5162e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5163e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 516487025532SHong Zhang b_oth->nonew = 0; 5165a6b2eed2SHong Zhang 5166a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5167dea91ad1SHong Zhang if (!startsj || !bufa_ptr){ 51681d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5169dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5170dea91ad1SHong Zhang } else { 517187025532SHong Zhang *startsj = sstartsj; 51721d79065fSBarry Smith *startsj_r = rstartsj; 517387025532SHong Zhang *bufa_ptr = bufa; 517487025532SHong Zhang } 5175dea91ad1SHong Zhang } 51764ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5177429d309bSHong Zhang PetscFunctionReturn(0); 5178429d309bSHong Zhang } 5179ccd8e176SBarry Smith 518043eb5e2fSMatthew Knepley #undef __FUNCT__ 518143eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 518243eb5e2fSMatthew Knepley /*@C 518343eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 518443eb5e2fSMatthew Knepley 518543eb5e2fSMatthew Knepley Not Collective 518643eb5e2fSMatthew Knepley 518743eb5e2fSMatthew Knepley Input Parameters: 518843eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 518943eb5e2fSMatthew Knepley 519043eb5e2fSMatthew Knepley Output Parameter: 519143eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 519243eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 519343eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 519443eb5e2fSMatthew Knepley 519543eb5e2fSMatthew Knepley Level: developer 519643eb5e2fSMatthew Knepley 519743eb5e2fSMatthew Knepley @*/ 519843eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 519943eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 520043eb5e2fSMatthew Knepley #else 520143eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 520243eb5e2fSMatthew Knepley #endif 520343eb5e2fSMatthew Knepley { 520443eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 520543eb5e2fSMatthew Knepley 520643eb5e2fSMatthew Knepley PetscFunctionBegin; 52070700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5208e414b56bSJed Brown PetscValidPointer(lvec, 2); 5209e414b56bSJed Brown PetscValidPointer(colmap, 3); 5210e414b56bSJed Brown PetscValidPointer(multScatter, 4); 521143eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 521243eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 521343eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 521443eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 521543eb5e2fSMatthew Knepley PetscFunctionReturn(0); 521643eb5e2fSMatthew Knepley } 521743eb5e2fSMatthew Knepley 521817667f90SBarry Smith EXTERN_C_BEGIN 52195a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*); 52205a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*); 5221c4688eafSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*); 522217667f90SBarry Smith EXTERN_C_END 522317667f90SBarry Smith 5224fc4dec0aSBarry Smith #undef __FUNCT__ 5225fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5226fc4dec0aSBarry Smith /* 5227fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5228fc4dec0aSBarry Smith 5229fc4dec0aSBarry Smith n p p 5230fc4dec0aSBarry Smith ( ) ( ) ( ) 5231fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5232fc4dec0aSBarry Smith ( ) ( ) ( ) 5233fc4dec0aSBarry Smith 5234fc4dec0aSBarry Smith */ 5235fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5236fc4dec0aSBarry Smith { 5237fc4dec0aSBarry Smith PetscErrorCode ierr; 5238fc4dec0aSBarry Smith Mat At,Bt,Ct; 5239fc4dec0aSBarry Smith 5240fc4dec0aSBarry Smith PetscFunctionBegin; 5241fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5242fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5243fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 5244fc4dec0aSBarry Smith ierr = MatDestroy(At);CHKERRQ(ierr); 5245fc4dec0aSBarry Smith ierr = MatDestroy(Bt);CHKERRQ(ierr); 5246fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 5247e5e4356aSBarry Smith ierr = MatDestroy(Ct);CHKERRQ(ierr); 5248fc4dec0aSBarry Smith PetscFunctionReturn(0); 5249fc4dec0aSBarry Smith } 5250fc4dec0aSBarry Smith 5251fc4dec0aSBarry Smith #undef __FUNCT__ 5252fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5253fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5254fc4dec0aSBarry Smith { 5255fc4dec0aSBarry Smith PetscErrorCode ierr; 5256d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5257fc4dec0aSBarry Smith Mat Cmat; 5258fc4dec0aSBarry Smith 5259fc4dec0aSBarry Smith PetscFunctionBegin; 5260e32f2f54SBarry 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); 526139804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5262fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5263fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5264fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 526538556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 526638556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5267fc4dec0aSBarry Smith *C = Cmat; 5268fc4dec0aSBarry Smith PetscFunctionReturn(0); 5269fc4dec0aSBarry Smith } 5270fc4dec0aSBarry Smith 5271fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5272fc4dec0aSBarry Smith #undef __FUNCT__ 5273fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5274fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5275fc4dec0aSBarry Smith { 5276fc4dec0aSBarry Smith PetscErrorCode ierr; 5277fc4dec0aSBarry Smith 5278fc4dec0aSBarry Smith PetscFunctionBegin; 5279fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5280fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5281fc4dec0aSBarry Smith } 5282fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5283fc4dec0aSBarry Smith PetscFunctionReturn(0); 5284fc4dec0aSBarry Smith } 5285fc4dec0aSBarry Smith 52865c9eb25fSBarry Smith EXTERN_C_BEGIN 5287611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5288bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5289611f576cSBarry Smith #endif 52903bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 52913bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 52923bf14a46SMatthew Knepley #endif 5293611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 52945c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5295611f576cSBarry Smith #endif 5296611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 52975c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*); 5298611f576cSBarry Smith #endif 52995c9eb25fSBarry Smith EXTERN_C_END 53005c9eb25fSBarry Smith 5301ccd8e176SBarry Smith /*MC 5302ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5303ccd8e176SBarry Smith 5304ccd8e176SBarry Smith Options Database Keys: 5305ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5306ccd8e176SBarry Smith 5307ccd8e176SBarry Smith Level: beginner 5308ccd8e176SBarry Smith 5309175b88e8SBarry Smith .seealso: MatCreateMPIAIJ() 5310ccd8e176SBarry Smith M*/ 5311ccd8e176SBarry Smith 5312ccd8e176SBarry Smith EXTERN_C_BEGIN 5313ccd8e176SBarry Smith #undef __FUNCT__ 5314ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 5315be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B) 5316ccd8e176SBarry Smith { 5317ccd8e176SBarry Smith Mat_MPIAIJ *b; 5318ccd8e176SBarry Smith PetscErrorCode ierr; 5319ccd8e176SBarry Smith PetscMPIInt size; 5320ccd8e176SBarry Smith 5321ccd8e176SBarry Smith PetscFunctionBegin; 53227adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 5323ccd8e176SBarry Smith 532438f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5325ccd8e176SBarry Smith B->data = (void*)b; 5326ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5327d0f46423SBarry Smith B->rmap->bs = 1; 5328ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5329ccd8e176SBarry Smith 5330ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5331ccd8e176SBarry Smith b->size = size; 53327adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5333ccd8e176SBarry Smith 5334ccd8e176SBarry Smith /* build cache for off array entries formed */ 53357adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5336ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5337ccd8e176SBarry Smith b->colmap = 0; 5338ccd8e176SBarry Smith b->garray = 0; 5339ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5340ccd8e176SBarry Smith 5341ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5342ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5343ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5344ccd8e176SBarry Smith 5345ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5346ccd8e176SBarry Smith b->rowindices = 0; 5347ccd8e176SBarry Smith b->rowvalues = 0; 5348ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5349ccd8e176SBarry Smith 5350611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 5351ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 53525c9eb25fSBarry Smith "MatGetFactor_mpiaij_spooles", 53535c9eb25fSBarry Smith MatGetFactor_mpiaij_spooles);CHKERRQ(ierr); 5354611f576cSBarry Smith #endif 5355611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5356ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5357bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5358bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5359611f576cSBarry Smith #endif 53603bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5361ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 53623bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 53633bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 53643bf14a46SMatthew Knepley #endif 5365611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5366ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 53675c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 53685c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5369611f576cSBarry Smith #endif 5370ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5371ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5372ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5373ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5374ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5375ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5376ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5377ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5378ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5379ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5380ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5381ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5382ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5383ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5384ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5385ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5386ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5387ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5388ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5389ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5390ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 53915a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C", 53925a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJPERM", 53935a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 53945a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C", 53955a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJCRL", 53965a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5397471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5398471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5399471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5400fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5401fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5402fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5403fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5404fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5405fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5406fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5407fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5408fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 540917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5410ccd8e176SBarry Smith PetscFunctionReturn(0); 5411ccd8e176SBarry Smith } 5412ccd8e176SBarry Smith EXTERN_C_END 541381824310SBarry Smith 541403bfb495SBarry Smith #undef __FUNCT__ 541503bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 541658d36128SBarry Smith /*@ 541703bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 541803bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 541903bfb495SBarry Smith 542003bfb495SBarry Smith Collective on MPI_Comm 542103bfb495SBarry Smith 542203bfb495SBarry Smith Input Parameters: 542303bfb495SBarry Smith + comm - MPI communicator 542403bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 542503bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 542603bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 542703bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 542803bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 542903bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 543003bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 543103bfb495SBarry Smith . j - column indices 543203bfb495SBarry Smith . a - matrix values 543303bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 543403bfb495SBarry Smith . oj - column indices 543503bfb495SBarry Smith - oa - matrix values 543603bfb495SBarry Smith 543703bfb495SBarry Smith Output Parameter: 543803bfb495SBarry Smith . mat - the matrix 543903bfb495SBarry Smith 544003bfb495SBarry Smith Level: advanced 544103bfb495SBarry Smith 544203bfb495SBarry Smith Notes: 544303bfb495SBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. 544403bfb495SBarry Smith 544503bfb495SBarry Smith The i and j indices are 0 based 544603bfb495SBarry Smith 544703bfb495SBarry Smith See MatCreateMPIAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 544803bfb495SBarry Smith 54497b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 54507b55108eSBarry Smith 54517b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 545203bfb495SBarry Smith 545303bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 545403bfb495SBarry Smith 545503bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 54568d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithArrays() 545703bfb495SBarry Smith @*/ 54588d7a6e47SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 545903bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 546003bfb495SBarry Smith { 546103bfb495SBarry Smith PetscErrorCode ierr; 546203bfb495SBarry Smith Mat_MPIAIJ *maij; 546303bfb495SBarry Smith 546403bfb495SBarry Smith PetscFunctionBegin; 5465e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 546603bfb495SBarry Smith if (i[0]) { 5467e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 546803bfb495SBarry Smith } 546903bfb495SBarry Smith if (oi[0]) { 5470e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 547103bfb495SBarry Smith } 547203bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 547303bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 547403bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 547503bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 54768d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 54778d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 547803bfb495SBarry Smith 547926283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->rmap,1);CHKERRQ(ierr); 548026283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->cmap,1);CHKERRQ(ierr); 548126283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 548226283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 548303bfb495SBarry Smith 548403bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5485d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 548603bfb495SBarry Smith 54878d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54888d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54898d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54908d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 54918d7a6e47SBarry Smith 549203bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549303bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549403bfb495SBarry Smith PetscFunctionReturn(0); 549503bfb495SBarry Smith } 549603bfb495SBarry Smith 549781824310SBarry Smith /* 549881824310SBarry Smith Special version for direct calls from Fortran 549981824310SBarry Smith */ 550081824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 550181824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 550281824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 550381824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 550481824310SBarry Smith #endif 550581824310SBarry Smith 550681824310SBarry Smith /* Change these macros so can be used in void function */ 550781824310SBarry Smith #undef CHKERRQ 5508e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 550981824310SBarry Smith #undef SETERRQ2 5510e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 551181824310SBarry Smith #undef SETERRQ 5512e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 551381824310SBarry Smith 551481824310SBarry Smith EXTERN_C_BEGIN 551581824310SBarry Smith #undef __FUNCT__ 551681824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 55171f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 551881824310SBarry Smith { 551981824310SBarry Smith Mat mat = *mmat; 552081824310SBarry Smith PetscInt m = *mm, n = *mn; 552181824310SBarry Smith InsertMode addv = *maddv; 552281824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 552381824310SBarry Smith PetscScalar value; 552481824310SBarry Smith PetscErrorCode ierr; 5525899cda47SBarry Smith 5526d9e2c085SLisandro Dalcin ierr = MatPreallocated(mat);CHKERRQ(ierr); 552781824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 552881824310SBarry Smith mat->insertmode = addv; 552981824310SBarry Smith } 553081824310SBarry Smith #if defined(PETSC_USE_DEBUG) 553181824310SBarry Smith else if (mat->insertmode != addv) { 5532e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 553381824310SBarry Smith } 553481824310SBarry Smith #endif 553581824310SBarry Smith { 5536d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5537d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5538ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 553981824310SBarry Smith 554081824310SBarry Smith /* Some Variables required in the macro */ 554181824310SBarry Smith Mat A = aij->A; 554281824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 554381824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5544dd6ea824SBarry Smith MatScalar *aa = a->a; 5545ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 554681824310SBarry Smith Mat B = aij->B; 554781824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5548d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5549dd6ea824SBarry Smith MatScalar *ba = b->a; 555081824310SBarry Smith 555181824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 555281824310SBarry Smith PetscInt nonew = a->nonew; 5553dd6ea824SBarry Smith MatScalar *ap1,*ap2; 555481824310SBarry Smith 555581824310SBarry Smith PetscFunctionBegin; 555681824310SBarry Smith for (i=0; i<m; i++) { 555781824310SBarry Smith if (im[i] < 0) continue; 555881824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5559e32f2f54SBarry 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); 556081824310SBarry Smith #endif 556181824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 556281824310SBarry Smith row = im[i] - rstart; 556381824310SBarry Smith lastcol1 = -1; 556481824310SBarry Smith rp1 = aj + ai[row]; 556581824310SBarry Smith ap1 = aa + ai[row]; 556681824310SBarry Smith rmax1 = aimax[row]; 556781824310SBarry Smith nrow1 = ailen[row]; 556881824310SBarry Smith low1 = 0; 556981824310SBarry Smith high1 = nrow1; 557081824310SBarry Smith lastcol2 = -1; 557181824310SBarry Smith rp2 = bj + bi[row]; 557281824310SBarry Smith ap2 = ba + bi[row]; 557381824310SBarry Smith rmax2 = bimax[row]; 557481824310SBarry Smith nrow2 = bilen[row]; 557581824310SBarry Smith low2 = 0; 557681824310SBarry Smith high2 = nrow2; 557781824310SBarry Smith 557881824310SBarry Smith for (j=0; j<n; j++) { 557981824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 558081824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 558181824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 558281824310SBarry Smith col = in[j] - cstart; 558381824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 558481824310SBarry Smith } else if (in[j] < 0) continue; 558581824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5586cb9801acSJed 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); 558781824310SBarry Smith #endif 558881824310SBarry Smith else { 558981824310SBarry Smith if (mat->was_assembled) { 559081824310SBarry Smith if (!aij->colmap) { 559181824310SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 559281824310SBarry Smith } 559381824310SBarry Smith #if defined (PETSC_USE_CTABLE) 559481824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 559581824310SBarry Smith col--; 559681824310SBarry Smith #else 559781824310SBarry Smith col = aij->colmap[in[j]] - 1; 559881824310SBarry Smith #endif 559981824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 560081824310SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 560181824310SBarry Smith col = in[j]; 560281824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 560381824310SBarry Smith B = aij->B; 560481824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 560581824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 560681824310SBarry Smith rp2 = bj + bi[row]; 560781824310SBarry Smith ap2 = ba + bi[row]; 560881824310SBarry Smith rmax2 = bimax[row]; 560981824310SBarry Smith nrow2 = bilen[row]; 561081824310SBarry Smith low2 = 0; 561181824310SBarry Smith high2 = nrow2; 5612d0f46423SBarry Smith bm = aij->B->rmap->n; 561381824310SBarry Smith ba = b->a; 561481824310SBarry Smith } 561581824310SBarry Smith } else col = in[j]; 561681824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 561781824310SBarry Smith } 561881824310SBarry Smith } 561981824310SBarry Smith } else { 562081824310SBarry Smith if (!aij->donotstash) { 562181824310SBarry Smith if (roworiented) { 5622ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 562381824310SBarry Smith } else { 5624ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 562581824310SBarry Smith } 562681824310SBarry Smith } 562781824310SBarry Smith } 562881824310SBarry Smith }} 562981824310SBarry Smith PetscFunctionReturnVoid(); 563081824310SBarry Smith } 563181824310SBarry Smith EXTERN_C_END 563203bfb495SBarry Smith 5633