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; 574*97b48c8fSBarry Smith const PetscScalar *xx; 575*97b48c8fSBarry 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 676*97b48c8fSBarry Smith /* fix right hand side if needed */ 677*97b48c8fSBarry Smith if (x && b) { 678*97b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 679*97b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 680*97b48c8fSBarry Smith for (i=0; i<N; i++) { 681*97b48c8fSBarry Smith bb[lrows[i]] = diag*xx[lrows[i]]; 682*97b48c8fSBarry Smith } 683*97b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 684*97b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 685*97b48c8fSBarry 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__ 7274a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 728dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 7291eb62cbbSBarry Smith { 730416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 731dfbe8321SBarry Smith PetscErrorCode ierr; 732b1d57f15SBarry Smith PetscInt nt; 733416022c9SBarry Smith 7343a40ed3dSBarry Smith PetscFunctionBegin; 735a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 73665e19b50SBarry 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); 737ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 738f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 739ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 740f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 7413a40ed3dSBarry Smith PetscFunctionReturn(0); 7421eb62cbbSBarry Smith } 7431eb62cbbSBarry Smith 7444a2ae208SSatish Balay #undef __FUNCT__ 745bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 746bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 747bd0c2dcbSBarry Smith { 748bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 749bd0c2dcbSBarry Smith PetscErrorCode ierr; 750bd0c2dcbSBarry Smith 751bd0c2dcbSBarry Smith PetscFunctionBegin; 752bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 753bd0c2dcbSBarry Smith PetscFunctionReturn(0); 754bd0c2dcbSBarry Smith } 755bd0c2dcbSBarry Smith 756bd0c2dcbSBarry Smith #undef __FUNCT__ 7574a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 758dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 759da3a660dSBarry Smith { 760416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 761dfbe8321SBarry Smith PetscErrorCode ierr; 7623a40ed3dSBarry Smith 7633a40ed3dSBarry Smith PetscFunctionBegin; 764ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 765f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 766ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 767f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 7683a40ed3dSBarry Smith PetscFunctionReturn(0); 769da3a660dSBarry Smith } 770da3a660dSBarry Smith 7714a2ae208SSatish Balay #undef __FUNCT__ 7724a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 773dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 774da3a660dSBarry Smith { 775416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 776dfbe8321SBarry Smith PetscErrorCode ierr; 777ace3abfcSBarry Smith PetscBool merged; 778da3a660dSBarry Smith 7793a40ed3dSBarry Smith PetscFunctionBegin; 780a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 781da3a660dSBarry Smith /* do nondiagonal part */ 7827c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 783a5ff213dSBarry Smith if (!merged) { 784da3a660dSBarry Smith /* send it on its way */ 785ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 786da3a660dSBarry Smith /* do local part */ 7877c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 788da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 789a5ff213dSBarry Smith /* added in yy until the next line, */ 790ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 791a5ff213dSBarry Smith } else { 792a5ff213dSBarry Smith /* do local part */ 793a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 794a5ff213dSBarry Smith /* send it on its way */ 795ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 796a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 797ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 798a5ff213dSBarry Smith } 7993a40ed3dSBarry Smith PetscFunctionReturn(0); 800da3a660dSBarry Smith } 801da3a660dSBarry Smith 802cd0d46ebSvictorle EXTERN_C_BEGIN 803cd0d46ebSvictorle #undef __FUNCT__ 8045fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 805ace3abfcSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 806cd0d46ebSvictorle { 8074f423910Svictorle MPI_Comm comm; 808cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 80966501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 810cd0d46ebSvictorle IS Me,Notme; 8116849ba73SBarry Smith PetscErrorCode ierr; 812b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 813b1d57f15SBarry Smith PetscMPIInt size; 814cd0d46ebSvictorle 815cd0d46ebSvictorle PetscFunctionBegin; 81642e5f5b4Svictorle 81742e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 81866501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 8195485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 820cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 8214f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 822b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 823b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 82442e5f5b4Svictorle 82542e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 826cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 827cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 828b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 829cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 830cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 83170b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 832268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 833268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 83466501d38Svictorle Aoff = Aoffs[0]; 835268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 83666501d38Svictorle Boff = Boffs[0]; 8375485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 83866501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 83966501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 84042e5f5b4Svictorle ierr = ISDestroy(Me);CHKERRQ(ierr); 84142e5f5b4Svictorle ierr = ISDestroy(Notme);CHKERRQ(ierr); 84242e5f5b4Svictorle 843cd0d46ebSvictorle PetscFunctionReturn(0); 844cd0d46ebSvictorle } 845cd0d46ebSvictorle EXTERN_C_END 846cd0d46ebSvictorle 8474a2ae208SSatish Balay #undef __FUNCT__ 8484a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 849dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 850da3a660dSBarry Smith { 851416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 852dfbe8321SBarry Smith PetscErrorCode ierr; 853da3a660dSBarry Smith 8543a40ed3dSBarry Smith PetscFunctionBegin; 855da3a660dSBarry Smith /* do nondiagonal part */ 8567c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 857da3a660dSBarry Smith /* send it on its way */ 858ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 859da3a660dSBarry Smith /* do local part */ 8607c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 861a5ff213dSBarry Smith /* receive remote parts */ 862ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 8633a40ed3dSBarry Smith PetscFunctionReturn(0); 864da3a660dSBarry Smith } 865da3a660dSBarry Smith 8661eb62cbbSBarry Smith /* 8671eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 8681eb62cbbSBarry Smith diagonal block 8691eb62cbbSBarry Smith */ 8704a2ae208SSatish Balay #undef __FUNCT__ 8714a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 872dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 8731eb62cbbSBarry Smith { 874dfbe8321SBarry Smith PetscErrorCode ierr; 875416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 8763a40ed3dSBarry Smith 8773a40ed3dSBarry Smith PetscFunctionBegin; 878e7e72b3dSBarry 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"); 879e7e72b3dSBarry 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"); 8803a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 8813a40ed3dSBarry Smith PetscFunctionReturn(0); 8821eb62cbbSBarry Smith } 8831eb62cbbSBarry Smith 8844a2ae208SSatish Balay #undef __FUNCT__ 8854a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 886f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 887052efed2SBarry Smith { 888052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 889dfbe8321SBarry Smith PetscErrorCode ierr; 8903a40ed3dSBarry Smith 8913a40ed3dSBarry Smith PetscFunctionBegin; 892f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 893f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 8943a40ed3dSBarry Smith PetscFunctionReturn(0); 895052efed2SBarry Smith } 896052efed2SBarry Smith 8974a2ae208SSatish Balay #undef __FUNCT__ 8984a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 899dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 9001eb62cbbSBarry Smith { 90144a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 902dfbe8321SBarry Smith PetscErrorCode ierr; 90383e2fdc7SBarry Smith 9043a40ed3dSBarry Smith PetscFunctionBegin; 905aa482453SBarry Smith #if defined(PETSC_USE_LOG) 906d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 907a5a9c739SBarry Smith #endif 9088798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 909a7420bb7SBarry Smith if (aij->diag) {ierr = VecDestroy(aij->diag);CHKERRQ(ierr);} 910d88c0aacSHong Zhang if (aij->A){ierr = MatDestroy(aij->A);CHKERRQ(ierr);} 911d88c0aacSHong Zhang if (aij->B){ierr = MatDestroy(aij->B);CHKERRQ(ierr);} 912aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 9139c666560SBarry Smith if (aij->colmap) {ierr = PetscTableDestroy(aij->colmap);CHKERRQ(ierr);} 914b1fc9764SSatish Balay #else 91505b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 916b1fc9764SSatish Balay #endif 91705b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 9187c922b88SBarry Smith if (aij->lvec) {ierr = VecDestroy(aij->lvec);CHKERRQ(ierr);} 9197c922b88SBarry Smith if (aij->Mvctx) {ierr = VecScatterDestroy(aij->Mvctx);CHKERRQ(ierr);} 92003095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 9218aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 922606d414cSSatish Balay ierr = PetscFree(aij);CHKERRQ(ierr); 923901853e0SKris Buschelman 924dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 925901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 926901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 927901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 928901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 929901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 930ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 931901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 932471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 9333a40ed3dSBarry Smith PetscFunctionReturn(0); 9341eb62cbbSBarry Smith } 935ee50ffe9SBarry Smith 9364a2ae208SSatish Balay #undef __FUNCT__ 9378e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 938dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 9398e2fed03SBarry Smith { 9408e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 9418e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 9428e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 9436849ba73SBarry Smith PetscErrorCode ierr; 94432dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 9456f69ff64SBarry Smith int fd; 946a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 947d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 9488e2fed03SBarry Smith PetscScalar *column_values; 94985ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 9508e2fed03SBarry Smith 9518e2fed03SBarry Smith PetscFunctionBegin; 9527adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 9537adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 9548e2fed03SBarry Smith nz = A->nz + B->nz; 955958c9bccSBarry Smith if (!rank) { 9560700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 957d0f46423SBarry Smith header[1] = mat->rmap->N; 958d0f46423SBarry Smith header[2] = mat->cmap->N; 9597adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 9608e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 9616f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9628e2fed03SBarry Smith /* get largest number of rows any processor has */ 963d0f46423SBarry Smith rlen = mat->rmap->n; 964d0f46423SBarry Smith range = mat->rmap->range; 9658e2fed03SBarry Smith for (i=1; i<size; i++) { 9668e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 9678e2fed03SBarry Smith } 9688e2fed03SBarry Smith } else { 9697adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 970d0f46423SBarry Smith rlen = mat->rmap->n; 9718e2fed03SBarry Smith } 9728e2fed03SBarry Smith 9738e2fed03SBarry Smith /* load up the local row counts */ 974b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 975d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 9768e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 9778e2fed03SBarry Smith } 9788e2fed03SBarry Smith 9798e2fed03SBarry Smith /* store the row lengths to the file */ 98085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 981958c9bccSBarry Smith if (!rank) { 9828e2fed03SBarry Smith MPI_Status status; 983d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9848e2fed03SBarry Smith for (i=1; i<size; i++) { 98585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 9868e2fed03SBarry Smith rlen = range[i+1] - range[i]; 987a1319256SJed Brown ierr = MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 9886f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 9898e2fed03SBarry Smith } 99085ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 9918e2fed03SBarry Smith } else { 99285ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 993d0f46423SBarry Smith ierr = MPI_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 99485ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 9958e2fed03SBarry Smith } 9968e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 9978e2fed03SBarry Smith 9988e2fed03SBarry Smith /* load up the local column indices */ 9998e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 10007adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1001b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 10028e2fed03SBarry Smith cnt = 0; 1003d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 10048e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 10058e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 10068e2fed03SBarry Smith column_indices[cnt++] = col; 10078e2fed03SBarry Smith } 10088e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 10098e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 10108e2fed03SBarry Smith } 10118e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 10128e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 10138e2fed03SBarry Smith } 10148e2fed03SBarry Smith } 1015e32f2f54SBarry 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); 10168e2fed03SBarry Smith 10178e2fed03SBarry Smith /* store the column indices to the file */ 101885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1019958c9bccSBarry Smith if (!rank) { 10208e2fed03SBarry Smith MPI_Status status; 10216f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 10228e2fed03SBarry Smith for (i=1; i<size; i++) { 102385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 10247adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1025e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 10267adad957SLisandro Dalcin ierr = MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 10276f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 10288e2fed03SBarry Smith } 102985ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 10308e2fed03SBarry Smith } else { 103185ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 10327adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10337adad957SLisandro Dalcin ierr = MPI_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 103485ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 10358e2fed03SBarry Smith } 10368e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 10378e2fed03SBarry Smith 10388e2fed03SBarry Smith /* load up the local column values */ 10398e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 10408e2fed03SBarry Smith cnt = 0; 1041d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 10428e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 10438e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 10448e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 10458e2fed03SBarry Smith } 10468e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 10478e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 10488e2fed03SBarry Smith } 10498e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 10508e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 10518e2fed03SBarry Smith } 10528e2fed03SBarry Smith } 1053e32f2f54SBarry 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); 10548e2fed03SBarry Smith 10558e2fed03SBarry Smith /* store the column values to the file */ 105685ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1057958c9bccSBarry Smith if (!rank) { 10588e2fed03SBarry Smith MPI_Status status; 10596f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 10608e2fed03SBarry Smith for (i=1; i<size; i++) { 106185ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 10627adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1063e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 10647adad957SLisandro Dalcin ierr = MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 10656f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 10668e2fed03SBarry Smith } 106785ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 10688e2fed03SBarry Smith } else { 106985ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 10707adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 10717adad957SLisandro Dalcin ierr = MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 107285ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 10738e2fed03SBarry Smith } 10748e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 10758e2fed03SBarry Smith PetscFunctionReturn(0); 10768e2fed03SBarry Smith } 10778e2fed03SBarry Smith 10788e2fed03SBarry Smith #undef __FUNCT__ 10794a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1080dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1081416022c9SBarry Smith { 108244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1083dfbe8321SBarry Smith PetscErrorCode ierr; 108432dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1085ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1086b0a32e0cSBarry Smith PetscViewer sviewer; 1087f3ef73ceSBarry Smith PetscViewerFormat format; 1088416022c9SBarry Smith 10893a40ed3dSBarry Smith PetscFunctionBegin; 10902692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 10912692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 10922692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 109332077d6dSBarry Smith if (iascii) { 1094b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1095456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 10964e220ebcSLois Curfman McInnes MatInfo info; 1097ace3abfcSBarry Smith PetscBool inodes; 1098923f20ffSKris Buschelman 10997adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1100888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1101923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 1102923f20ffSKris Buschelman if (!inodes) { 110377431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1104d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 11056831982aSBarry Smith } else { 110677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1107d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 11086831982aSBarry Smith } 1109888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 111077431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1111888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 111277431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1113b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 111407d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1115a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 11163a40ed3dSBarry Smith PetscFunctionReturn(0); 1117fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1118923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1119923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1120923f20ffSKris Buschelman if (inodes) { 1121923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1122d38fa0fbSBarry Smith } else { 1123d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1124d38fa0fbSBarry Smith } 11253a40ed3dSBarry Smith PetscFunctionReturn(0); 11264aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 11274aedb280SBarry Smith PetscFunctionReturn(0); 112808480c60SBarry Smith } 11298e2fed03SBarry Smith } else if (isbinary) { 11308e2fed03SBarry Smith if (size == 1) { 11317adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 11328e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 11338e2fed03SBarry Smith } else { 11348e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 11358e2fed03SBarry Smith } 11368e2fed03SBarry Smith PetscFunctionReturn(0); 11370f5bd95cSBarry Smith } else if (isdraw) { 1138b0a32e0cSBarry Smith PetscDraw draw; 1139ace3abfcSBarry Smith PetscBool isnull; 1140b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1141b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 114219bcc07fSBarry Smith } 114319bcc07fSBarry Smith 114417699dbbSLois Curfman McInnes if (size == 1) { 11457adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 114678b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 11473a40ed3dSBarry Smith } else { 114895373324SBarry Smith /* assemble the entire matrix onto first processor. */ 114995373324SBarry Smith Mat A; 1150ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1151d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1152dd6ea824SBarry Smith MatScalar *a; 11532ee70a88SLois Curfman McInnes 115432a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1155ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 115632a366e4SMatthew Knepley 1157acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 115832a366e4SMatthew Knepley if (!flg) { 1159e7e72b3dSBarry 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."); 116032a366e4SMatthew Knepley } 116132a366e4SMatthew Knepley } 11620805154bSBarry Smith 11637adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 116417699dbbSLois Curfman McInnes if (!rank) { 1165f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 11663a40ed3dSBarry Smith } else { 1167f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 116895373324SBarry Smith } 1169f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1170f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1171f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 117252e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1173416022c9SBarry Smith 117495373324SBarry Smith /* copy over the A part */ 1175ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1176d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1177d0f46423SBarry Smith row = mat->rmap->rstart; 1178d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 117995373324SBarry Smith for (i=0; i<m; i++) { 1180416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 118195373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 118295373324SBarry Smith } 11832ee70a88SLois Curfman McInnes aj = Aloc->j; 1184d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 118595373324SBarry Smith 118695373324SBarry Smith /* copy over the B part */ 1187ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1188d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1189d0f46423SBarry Smith row = mat->rmap->rstart; 1190b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1191b0a32e0cSBarry Smith ct = cols; 1192bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 119395373324SBarry Smith for (i=0; i<m; i++) { 1194416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 119595373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 119695373324SBarry Smith } 1197606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 11986d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 11996d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 120055843e3eSBarry Smith /* 120155843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1202b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 120355843e3eSBarry Smith */ 1204b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1205e03a110bSBarry Smith if (!rank) { 12067adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 12077566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 12087566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 12096831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 121095373324SBarry Smith } 1211b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 121278b31e54SBarry Smith ierr = MatDestroy(A);CHKERRQ(ierr); 121395373324SBarry Smith } 12143a40ed3dSBarry Smith PetscFunctionReturn(0); 12151eb62cbbSBarry Smith } 12161eb62cbbSBarry Smith 12174a2ae208SSatish Balay #undef __FUNCT__ 12184a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1219dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1220416022c9SBarry Smith { 1221dfbe8321SBarry Smith PetscErrorCode ierr; 1222ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1223416022c9SBarry Smith 12243a40ed3dSBarry Smith PetscFunctionBegin; 12252692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 12262692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 12272692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 12282692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 122932077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 12307b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 12315cd90555SBarry Smith } else { 1232e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1233416022c9SBarry Smith } 12343a40ed3dSBarry Smith PetscFunctionReturn(0); 1235416022c9SBarry Smith } 1236416022c9SBarry Smith 12374a2ae208SSatish Balay #undef __FUNCT__ 123841f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 123941f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 12408a729477SBarry Smith { 124144a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1242dfbe8321SBarry Smith PetscErrorCode ierr; 12436987fefcSBarry Smith Vec bb1 = 0; 1244ace3abfcSBarry Smith PetscBool hasop; 12458a729477SBarry Smith 12463a40ed3dSBarry Smith PetscFunctionBegin; 124785911e72SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 124885911e72SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 124985911e72SJed Brown } 12502798e883SHong Zhang 1251a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 125241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1253a2b30743SBarry Smith PetscFunctionReturn(0); 1254a2b30743SBarry Smith } 1255a2b30743SBarry Smith 1256c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1257da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 125841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12592798e883SHong Zhang its--; 1260da3a660dSBarry Smith } 12612798e883SHong Zhang 12622798e883SHong Zhang while (its--) { 1263ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1264ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12652798e883SHong Zhang 1266c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1267efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1268c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 12692798e883SHong Zhang 1270c14dc6b6SHong Zhang /* local sweep */ 127141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 12722798e883SHong Zhang } 12733a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1274da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 127541f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12762798e883SHong Zhang its--; 1277da3a660dSBarry Smith } 12782798e883SHong Zhang while (its--) { 1279ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1280ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12812798e883SHong Zhang 1282c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1283efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1284c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1285c14dc6b6SHong Zhang 1286c14dc6b6SHong Zhang /* local sweep */ 128741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 12882798e883SHong Zhang } 12893a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1290da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 129141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 12922798e883SHong Zhang its--; 1293da3a660dSBarry Smith } 12942798e883SHong Zhang while (its--) { 1295ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1296ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 12972798e883SHong Zhang 1298c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1299efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1300c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 13012798e883SHong Zhang 1302c14dc6b6SHong Zhang /* local sweep */ 130341f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 13042798e883SHong Zhang } 1305a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1306a7420bb7SBarry Smith Vec xx1; 1307a7420bb7SBarry Smith 1308a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 130941f059aeSBarry 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); 1310a7420bb7SBarry Smith 1311a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1312a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1313a7420bb7SBarry Smith if (!mat->diag) { 1314a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1315a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1316a7420bb7SBarry Smith } 1317bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1318bd0c2dcbSBarry Smith if (hasop) { 1319bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1320bd0c2dcbSBarry Smith } else { 1321a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1322bd0c2dcbSBarry Smith } 1323887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1324887ee2caSBarry Smith 1325a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1326a7420bb7SBarry Smith 1327a7420bb7SBarry Smith /* local sweep */ 132841f059aeSBarry 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); 1329a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 1330a7420bb7SBarry Smith ierr = VecDestroy(xx1);CHKERRQ(ierr); 13313a40ed3dSBarry Smith } else { 1332e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1333c16cb8f2SBarry Smith } 1334c14dc6b6SHong Zhang 13356987fefcSBarry Smith if (bb1) {ierr = VecDestroy(bb1);CHKERRQ(ierr);} 13363a40ed3dSBarry Smith PetscFunctionReturn(0); 13378a729477SBarry Smith } 1338a66be287SLois Curfman McInnes 13394a2ae208SSatish Balay #undef __FUNCT__ 134042e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 134142e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 134242e855d1Svictor { 134342e855d1Svictor MPI_Comm comm,pcomm; 13445d0c19d7SBarry Smith PetscInt first,local_size,nrows; 13455d0c19d7SBarry Smith const PetscInt *rows; 1346dbf0e21dSBarry Smith PetscMPIInt size; 134742e855d1Svictor IS crowp,growp,irowp,lrowp,lcolp,icolp; 134842e855d1Svictor PetscErrorCode ierr; 134942e855d1Svictor 135042e855d1Svictor PetscFunctionBegin; 135142e855d1Svictor ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 135242e855d1Svictor /* make a collective version of 'rowp' */ 135342e855d1Svictor ierr = PetscObjectGetComm((PetscObject)rowp,&pcomm);CHKERRQ(ierr); 135442e855d1Svictor if (pcomm==comm) { 135542e855d1Svictor crowp = rowp; 135642e855d1Svictor } else { 135742e855d1Svictor ierr = ISGetSize(rowp,&nrows);CHKERRQ(ierr); 135842e855d1Svictor ierr = ISGetIndices(rowp,&rows);CHKERRQ(ierr); 135970b3c8c7SBarry Smith ierr = ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);CHKERRQ(ierr); 136042e855d1Svictor ierr = ISRestoreIndices(rowp,&rows);CHKERRQ(ierr); 136142e855d1Svictor } 136242e855d1Svictor /* collect the global row permutation and invert it */ 136342e855d1Svictor ierr = ISAllGather(crowp,&growp);CHKERRQ(ierr); 136442e855d1Svictor ierr = ISSetPermutation(growp);CHKERRQ(ierr); 136542e855d1Svictor if (pcomm!=comm) { 136642e855d1Svictor ierr = ISDestroy(crowp);CHKERRQ(ierr); 136742e855d1Svictor } 136842e855d1Svictor ierr = ISInvertPermutation(growp,PETSC_DECIDE,&irowp);CHKERRQ(ierr); 136942e855d1Svictor /* get the local target indices */ 137042e855d1Svictor ierr = MatGetOwnershipRange(A,&first,PETSC_NULL);CHKERRQ(ierr); 137142e855d1Svictor ierr = MatGetLocalSize(A,&local_size,PETSC_NULL);CHKERRQ(ierr); 137242e855d1Svictor ierr = ISGetIndices(irowp,&rows);CHKERRQ(ierr); 137370b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,PETSC_COPY_VALUES,&lrowp);CHKERRQ(ierr); 137442e855d1Svictor ierr = ISRestoreIndices(irowp,&rows);CHKERRQ(ierr); 137542e855d1Svictor ierr = ISDestroy(irowp);CHKERRQ(ierr); 137642e855d1Svictor /* the column permutation is so much easier; 137742e855d1Svictor make a local version of 'colp' and invert it */ 137842e855d1Svictor ierr = PetscObjectGetComm((PetscObject)colp,&pcomm);CHKERRQ(ierr); 1379dbf0e21dSBarry Smith ierr = MPI_Comm_size(pcomm,&size);CHKERRQ(ierr); 1380dbf0e21dSBarry Smith if (size==1) { 138142e855d1Svictor lcolp = colp; 138242e855d1Svictor } else { 138342e855d1Svictor ierr = ISGetSize(colp,&nrows);CHKERRQ(ierr); 138442e855d1Svictor ierr = ISGetIndices(colp,&rows);CHKERRQ(ierr); 138570b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,nrows,rows,PETSC_COPY_VALUES,&lcolp);CHKERRQ(ierr); 138642e855d1Svictor } 1387dbf0e21dSBarry Smith ierr = ISSetPermutation(lcolp);CHKERRQ(ierr); 138842e855d1Svictor ierr = ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);CHKERRQ(ierr); 13894aa3045dSJed Brown ierr = ISSetPermutation(icolp);CHKERRQ(ierr); 1390dbf0e21dSBarry Smith if (size>1) { 139142e855d1Svictor ierr = ISRestoreIndices(colp,&rows);CHKERRQ(ierr); 139242e855d1Svictor ierr = ISDestroy(lcolp);CHKERRQ(ierr); 139342e855d1Svictor } 139442e855d1Svictor /* now we just get the submatrix */ 13954aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);CHKERRQ(ierr); 139642e855d1Svictor /* clean up */ 139742e855d1Svictor ierr = ISDestroy(lrowp);CHKERRQ(ierr); 139842e855d1Svictor ierr = ISDestroy(icolp);CHKERRQ(ierr); 139942e855d1Svictor PetscFunctionReturn(0); 140042e855d1Svictor } 140142e855d1Svictor 140242e855d1Svictor #undef __FUNCT__ 14034a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1404dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1405a66be287SLois Curfman McInnes { 1406a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1407a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1408dfbe8321SBarry Smith PetscErrorCode ierr; 1409329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1410a66be287SLois Curfman McInnes 14113a40ed3dSBarry Smith PetscFunctionBegin; 14124e220ebcSLois Curfman McInnes info->block_size = 1.0; 14134e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 14144e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 14154e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 14164e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 14174e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 14184e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1419a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 14204e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 14214e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 14224e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 14234e220ebcSLois Curfman McInnes info->memory = isend[3]; 14244e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1425a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 14267adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 14274e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 14284e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 14294e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 14304e220ebcSLois Curfman McInnes info->memory = irecv[3]; 14314e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1432a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 14337adad957SLisandro Dalcin ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 14344e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 14354e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 14364e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 14374e220ebcSLois Curfman McInnes info->memory = irecv[3]; 14384e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1439a66be287SLois Curfman McInnes } 14404e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 14414e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 14424e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 14434e220ebcSLois Curfman McInnes 14443a40ed3dSBarry Smith PetscFunctionReturn(0); 1445a66be287SLois Curfman McInnes } 1446a66be287SLois Curfman McInnes 14474a2ae208SSatish Balay #undef __FUNCT__ 14484a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1449ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1450c74985f6SBarry Smith { 1451c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1452dfbe8321SBarry Smith PetscErrorCode ierr; 1453c74985f6SBarry Smith 14543a40ed3dSBarry Smith PetscFunctionBegin; 145512c028f9SKris Buschelman switch (op) { 1456512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 145712c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 145828b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1459a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 146012c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 146112c028f9SKris Buschelman case MAT_USE_INODES: 146212c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 14634e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 14644e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 146512c028f9SKris Buschelman break; 146612c028f9SKris Buschelman case MAT_ROW_ORIENTED: 14674e0d8c25SBarry Smith a->roworiented = flg; 14684e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 14694e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 147012c028f9SKris Buschelman break; 14714e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1472290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 147312c028f9SKris Buschelman break; 147412c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 14757c922b88SBarry Smith a->donotstash = PETSC_TRUE; 147612c028f9SKris Buschelman break; 1477ffa07934SHong Zhang case MAT_SPD: 1478ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1479ffa07934SHong Zhang A->spd = flg; 1480ffa07934SHong Zhang if (flg) { 1481ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1482ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1483ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1484ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1485ffa07934SHong Zhang } 1486ffa07934SHong Zhang break; 148777e54ba9SKris Buschelman case MAT_SYMMETRIC: 14884e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 148925f421beSHong Zhang break; 149077e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1491eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1492eeffb40dSHong Zhang break; 1493bf108f30SBarry Smith case MAT_HERMITIAN: 1494eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1495eeffb40dSHong Zhang break; 1496bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 14974e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 149877e54ba9SKris Buschelman break; 149912c028f9SKris Buschelman default: 1500e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 15013a40ed3dSBarry Smith } 15023a40ed3dSBarry Smith PetscFunctionReturn(0); 1503c74985f6SBarry Smith } 1504c74985f6SBarry Smith 15054a2ae208SSatish Balay #undef __FUNCT__ 15064a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1507b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 150839e00950SLois Curfman McInnes { 1509154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 151087828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 15116849ba73SBarry Smith PetscErrorCode ierr; 1512d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1513d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1514b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 151539e00950SLois Curfman McInnes 15163a40ed3dSBarry Smith PetscFunctionBegin; 1517e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 15187a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 15197a0afa10SBarry Smith 152070f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 15217a0afa10SBarry Smith /* 15227a0afa10SBarry Smith allocate enough space to hold information from the longest row. 15237a0afa10SBarry Smith */ 15247a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1525b1d57f15SBarry Smith PetscInt max = 1,tmp; 1526d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 15277a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 15287a0afa10SBarry Smith if (max < tmp) { max = tmp; } 15297a0afa10SBarry Smith } 15301d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 15317a0afa10SBarry Smith } 15327a0afa10SBarry Smith 1533e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1534abc0e9e4SLois Curfman McInnes lrow = row - rstart; 153539e00950SLois Curfman McInnes 1536154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1537154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1538154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1539f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1540f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1541154123eaSLois Curfman McInnes nztot = nzA + nzB; 1542154123eaSLois Curfman McInnes 154370f0671dSBarry Smith cmap = mat->garray; 1544154123eaSLois Curfman McInnes if (v || idx) { 1545154123eaSLois Curfman McInnes if (nztot) { 1546154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1547b1d57f15SBarry Smith PetscInt imark = -1; 1548154123eaSLois Curfman McInnes if (v) { 154970f0671dSBarry Smith *v = v_p = mat->rowvalues; 155039e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 155170f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1552154123eaSLois Curfman McInnes else break; 1553154123eaSLois Curfman McInnes } 1554154123eaSLois Curfman McInnes imark = i; 155570f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 155670f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1557154123eaSLois Curfman McInnes } 1558154123eaSLois Curfman McInnes if (idx) { 155970f0671dSBarry Smith *idx = idx_p = mat->rowindices; 156070f0671dSBarry Smith if (imark > -1) { 156170f0671dSBarry Smith for (i=0; i<imark; i++) { 156270f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 156370f0671dSBarry Smith } 156470f0671dSBarry Smith } else { 1565154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 156670f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1567154123eaSLois Curfman McInnes else break; 1568154123eaSLois Curfman McInnes } 1569154123eaSLois Curfman McInnes imark = i; 157070f0671dSBarry Smith } 157170f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 157270f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 157339e00950SLois Curfman McInnes } 15743f97c4b0SBarry Smith } else { 15751ca473b0SSatish Balay if (idx) *idx = 0; 15761ca473b0SSatish Balay if (v) *v = 0; 15771ca473b0SSatish Balay } 1578154123eaSLois Curfman McInnes } 157939e00950SLois Curfman McInnes *nz = nztot; 1580f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1581f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 15823a40ed3dSBarry Smith PetscFunctionReturn(0); 158339e00950SLois Curfman McInnes } 158439e00950SLois Curfman McInnes 15854a2ae208SSatish Balay #undef __FUNCT__ 15864a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1587b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 158839e00950SLois Curfman McInnes { 15897a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 15903a40ed3dSBarry Smith 15913a40ed3dSBarry Smith PetscFunctionBegin; 1592e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 15937a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 15943a40ed3dSBarry Smith PetscFunctionReturn(0); 159539e00950SLois Curfman McInnes } 159639e00950SLois Curfman McInnes 15974a2ae208SSatish Balay #undef __FUNCT__ 15984a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1599dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1600855ac2c5SLois Curfman McInnes { 1601855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1602ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1603dfbe8321SBarry Smith PetscErrorCode ierr; 1604d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1605329f5518SBarry Smith PetscReal sum = 0.0; 1606a77337e4SBarry Smith MatScalar *v; 160704ca555eSLois Curfman McInnes 16083a40ed3dSBarry Smith PetscFunctionBegin; 160917699dbbSLois Curfman McInnes if (aij->size == 1) { 161014183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 161137fa93a5SLois Curfman McInnes } else { 161204ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 161304ca555eSLois Curfman McInnes v = amat->a; 161404ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1615aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1616329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 161704ca555eSLois Curfman McInnes #else 161804ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 161904ca555eSLois Curfman McInnes #endif 162004ca555eSLois Curfman McInnes } 162104ca555eSLois Curfman McInnes v = bmat->a; 162204ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1623aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 1624329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 162504ca555eSLois Curfman McInnes #else 162604ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 162704ca555eSLois Curfman McInnes #endif 162804ca555eSLois Curfman McInnes } 16297adad957SLisandro Dalcin ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 163004ca555eSLois Curfman McInnes *norm = sqrt(*norm); 16313a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1632329f5518SBarry Smith PetscReal *tmp,*tmp2; 1633b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 1634d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 1635d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 1636d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 163704ca555eSLois Curfman McInnes *norm = 0.0; 163804ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 163904ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1640bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 164104ca555eSLois Curfman McInnes } 164204ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 164304ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1644bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 164504ca555eSLois Curfman McInnes } 1646d0f46423SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPI_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 1647d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 164804ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 164904ca555eSLois Curfman McInnes } 1650606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1651606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 16523a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1653329f5518SBarry Smith PetscReal ntemp = 0.0; 1654d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1655bfec09a0SHong Zhang v = amat->a + amat->i[j]; 165604ca555eSLois Curfman McInnes sum = 0.0; 165704ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1658cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 165904ca555eSLois Curfman McInnes } 1660bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 166104ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1662cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 166304ca555eSLois Curfman McInnes } 1664515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 166504ca555eSLois Curfman McInnes } 16667adad957SLisandro Dalcin ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 1667ca161407SBarry Smith } else { 1668e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 166904ca555eSLois Curfman McInnes } 167037fa93a5SLois Curfman McInnes } 16713a40ed3dSBarry Smith PetscFunctionReturn(0); 1672855ac2c5SLois Curfman McInnes } 1673855ac2c5SLois Curfman McInnes 16744a2ae208SSatish Balay #undef __FUNCT__ 16754a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1676fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1677b7c46309SBarry Smith { 1678b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1679da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1680dfbe8321SBarry Smith PetscErrorCode ierr; 1681d0f46423SBarry Smith PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i,*d_nnz; 1682d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 16833a40ed3dSBarry Smith Mat B; 1684a77337e4SBarry Smith MatScalar *array; 1685b7c46309SBarry Smith 16863a40ed3dSBarry Smith PetscFunctionBegin; 1687e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1688da668accSHong Zhang 1689d0f46423SBarry Smith ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; 1690da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1691da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1692fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 1693fc73b1b3SBarry Smith /* compute d_nnz for preallocation; o_nnz is approximated by d_nnz to avoid communication */ 1694fc73b1b3SBarry Smith ierr = PetscMalloc((1+na)*sizeof(PetscInt),&d_nnz);CHKERRQ(ierr); 1695da668accSHong Zhang ierr = PetscMemzero(d_nnz,(1+na)*sizeof(PetscInt));CHKERRQ(ierr); 1696da668accSHong Zhang for (i=0; i<ai[ma]; i++){ 1697da668accSHong Zhang d_nnz[aj[i]] ++; 1698da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1699d4bb536fSBarry Smith } 1700d4bb536fSBarry Smith 17017adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 1702d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 17037adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 1704da668accSHong Zhang ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,d_nnz);CHKERRQ(ierr); 1705fc73b1b3SBarry Smith ierr = PetscFree(d_nnz);CHKERRQ(ierr); 1706fc4dec0aSBarry Smith } else { 1707fc4dec0aSBarry Smith B = *matout; 1708fc4dec0aSBarry Smith } 1709b7c46309SBarry Smith 1710b7c46309SBarry Smith /* copy over the A part */ 1711da668accSHong Zhang array = Aloc->a; 1712d0f46423SBarry Smith row = A->rmap->rstart; 1713da668accSHong Zhang for (i=0; i<ma; i++) { 1714da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1715da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 1716da668accSHong Zhang row++; array += ncol; aj += ncol; 1717b7c46309SBarry Smith } 1718b7c46309SBarry Smith aj = Aloc->j; 1719da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1720b7c46309SBarry Smith 1721b7c46309SBarry Smith /* copy over the B part */ 1722fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1723fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 1724da668accSHong Zhang array = Bloc->a; 1725d0f46423SBarry Smith row = A->rmap->rstart; 1726da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 172761a2fbbaSHong Zhang cols_tmp = cols; 1728da668accSHong Zhang for (i=0; i<mb; i++) { 1729da668accSHong Zhang ncol = bi[i+1]-bi[i]; 173061a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 173161a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 1732b7c46309SBarry Smith } 1733fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1734fc73b1b3SBarry Smith 17356d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 17366d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1737815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 17380de55854SLois Curfman McInnes *matout = B; 17390de55854SLois Curfman McInnes } else { 1740eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 17410de55854SLois Curfman McInnes } 17423a40ed3dSBarry Smith PetscFunctionReturn(0); 1743b7c46309SBarry Smith } 1744b7c46309SBarry Smith 17454a2ae208SSatish Balay #undef __FUNCT__ 17464a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1747dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1748a008b906SSatish Balay { 17494b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 17504b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 1751dfbe8321SBarry Smith PetscErrorCode ierr; 1752b1d57f15SBarry Smith PetscInt s1,s2,s3; 1753a008b906SSatish Balay 17543a40ed3dSBarry Smith PetscFunctionBegin; 17554b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 17564b967eb1SSatish Balay if (rr) { 1757e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 1758e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 17594b967eb1SSatish Balay /* Overlap communication with computation. */ 1760ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1761a008b906SSatish Balay } 17624b967eb1SSatish Balay if (ll) { 1763e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 1764e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 1765f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 17664b967eb1SSatish Balay } 17674b967eb1SSatish Balay /* scale the diagonal block */ 1768f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 17694b967eb1SSatish Balay 17704b967eb1SSatish Balay if (rr) { 17714b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 1772ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1773f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 17744b967eb1SSatish Balay } 17754b967eb1SSatish Balay 17763a40ed3dSBarry Smith PetscFunctionReturn(0); 1777a008b906SSatish Balay } 1778a008b906SSatish Balay 17794a2ae208SSatish Balay #undef __FUNCT__ 1780521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_MPIAIJ" 1781521d7252SBarry Smith PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs) 17825a838052SSatish Balay { 1783521d7252SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1784521d7252SBarry Smith PetscErrorCode ierr; 1785521d7252SBarry Smith 17863a40ed3dSBarry Smith PetscFunctionBegin; 1787521d7252SBarry Smith ierr = MatSetBlockSize(a->A,bs);CHKERRQ(ierr); 1788521d7252SBarry Smith ierr = MatSetBlockSize(a->B,bs);CHKERRQ(ierr); 1789829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr); 1790829b6ff0SJed Brown ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr); 17913a40ed3dSBarry Smith PetscFunctionReturn(0); 17925a838052SSatish Balay } 17934a2ae208SSatish Balay #undef __FUNCT__ 17944a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 1795dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 1796bb5a7306SBarry Smith { 1797bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1798dfbe8321SBarry Smith PetscErrorCode ierr; 17993a40ed3dSBarry Smith 18003a40ed3dSBarry Smith PetscFunctionBegin; 1801bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 18023a40ed3dSBarry Smith PetscFunctionReturn(0); 1803bb5a7306SBarry Smith } 1804bb5a7306SBarry Smith 18054a2ae208SSatish Balay #undef __FUNCT__ 18064a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 1807ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 1808d4bb536fSBarry Smith { 1809d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 1810d4bb536fSBarry Smith Mat a,b,c,d; 1811ace3abfcSBarry Smith PetscBool flg; 1812dfbe8321SBarry Smith PetscErrorCode ierr; 1813d4bb536fSBarry Smith 18143a40ed3dSBarry Smith PetscFunctionBegin; 1815d4bb536fSBarry Smith a = matA->A; b = matA->B; 1816d4bb536fSBarry Smith c = matB->A; d = matB->B; 1817d4bb536fSBarry Smith 1818d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 1819abc0a331SBarry Smith if (flg) { 1820d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 1821d4bb536fSBarry Smith } 18227adad957SLisandro Dalcin ierr = MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 18233a40ed3dSBarry Smith PetscFunctionReturn(0); 1824d4bb536fSBarry Smith } 1825d4bb536fSBarry Smith 18264a2ae208SSatish Balay #undef __FUNCT__ 18274a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 1828dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 1829cb5b572fSBarry Smith { 1830dfbe8321SBarry Smith PetscErrorCode ierr; 1831cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 1832cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 1833cb5b572fSBarry Smith 1834cb5b572fSBarry Smith PetscFunctionBegin; 183533f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 183633f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 1837cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 1838cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 1839cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 1840cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 1841cb5b572fSBarry Smith then copying the submatrices */ 1842cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 1843cb5b572fSBarry Smith } else { 1844cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 1845cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 1846cb5b572fSBarry Smith } 1847cb5b572fSBarry Smith PetscFunctionReturn(0); 1848cb5b572fSBarry Smith } 1849cb5b572fSBarry Smith 18504a2ae208SSatish Balay #undef __FUNCT__ 18514a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_MPIAIJ" 1852dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A) 1853273d9f13SBarry Smith { 1854dfbe8321SBarry Smith PetscErrorCode ierr; 1855273d9f13SBarry Smith 1856273d9f13SBarry Smith PetscFunctionBegin; 1857273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 1858273d9f13SBarry Smith PetscFunctionReturn(0); 1859273d9f13SBarry Smith } 1860273d9f13SBarry Smith 1861ac90fabeSBarry Smith #undef __FUNCT__ 1862ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 1863f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 1864ac90fabeSBarry Smith { 1865dfbe8321SBarry Smith PetscErrorCode ierr; 1866b1d57f15SBarry Smith PetscInt i; 1867ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 18684ce68768SBarry Smith PetscBLASInt bnz,one=1; 1869ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 1870ac90fabeSBarry Smith 1871ac90fabeSBarry Smith PetscFunctionBegin; 1872ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 1873f4df32b1SMatthew Knepley PetscScalar alpha = a; 1874ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 1875ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 18760805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 1877f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 1878ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 1879ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 18800805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 1881f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 1882a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 1883f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 1884c537a176SHong Zhang 1885c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 1886a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 1887a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 1888a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 1889a30b2313SHong Zhang ierr = MatDestroy(y->XtoY);CHKERRQ(ierr); 1890c537a176SHong Zhang } 1891a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 1892d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 1893a30b2313SHong Zhang y->XtoY = xx->B; 1894407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 1895c537a176SHong Zhang } 1896f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 1897ac90fabeSBarry Smith } else { 18989f5f6813SShri Abhyankar Mat B; 18999f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 19009f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 19019f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 19029f5f6813SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 1903bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 19049f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 19059f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 19069f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 19079f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->B,xx->B,nnz_o);CHKERRQ(ierr); 19089f5f6813SShri Abhyankar ierr = MatMPIAIJSetPreallocation(B,PETSC_NULL,nnz_d,PETSC_NULL,nnz_o);CHKERRQ(ierr); 19099f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 19109f5f6813SShri Abhyankar ierr = MatHeaderReplace(Y,B); 19119f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 19129f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 1913ac90fabeSBarry Smith } 1914ac90fabeSBarry Smith PetscFunctionReturn(0); 1915ac90fabeSBarry Smith } 1916ac90fabeSBarry Smith 1917354c94deSBarry Smith EXTERN PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat); 1918354c94deSBarry Smith 1919354c94deSBarry Smith #undef __FUNCT__ 1920354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 1921354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_MPIAIJ(Mat mat) 1922354c94deSBarry Smith { 1923354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 1924354c94deSBarry Smith PetscErrorCode ierr; 1925354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 1926354c94deSBarry Smith 1927354c94deSBarry Smith PetscFunctionBegin; 1928354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 1929354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 1930354c94deSBarry Smith #else 1931354c94deSBarry Smith PetscFunctionBegin; 1932354c94deSBarry Smith #endif 1933354c94deSBarry Smith PetscFunctionReturn(0); 1934354c94deSBarry Smith } 1935354c94deSBarry Smith 193699cafbc1SBarry Smith #undef __FUNCT__ 193799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 193899cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 193999cafbc1SBarry Smith { 194099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 194199cafbc1SBarry Smith PetscErrorCode ierr; 194299cafbc1SBarry Smith 194399cafbc1SBarry Smith PetscFunctionBegin; 194499cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 194599cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 194699cafbc1SBarry Smith PetscFunctionReturn(0); 194799cafbc1SBarry Smith } 194899cafbc1SBarry Smith 194999cafbc1SBarry Smith #undef __FUNCT__ 195099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 195199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 195299cafbc1SBarry Smith { 195399cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 195499cafbc1SBarry Smith PetscErrorCode ierr; 195599cafbc1SBarry Smith 195699cafbc1SBarry Smith PetscFunctionBegin; 195799cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 195899cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 195999cafbc1SBarry Smith PetscFunctionReturn(0); 196099cafbc1SBarry Smith } 196199cafbc1SBarry Smith 1962103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 1963103bf8bdSMatthew Knepley 1964103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 1965a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 1966a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 1967a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 1968103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 1969a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 1970d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 1971103bf8bdSMatthew Knepley 1972103bf8bdSMatthew Knepley #undef __FUNCT__ 1973103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 1974103bf8bdSMatthew Knepley /* 1975103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 1976103bf8bdSMatthew Knepley */ 19770481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 1978103bf8bdSMatthew Knepley { 1979a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 1980a2c909beSMatthew Knepley 1981a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 1982a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 1983a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 1984a2c909beSMatthew Knepley 1985ace3abfcSBarry Smith PetscBool row_identity, col_identity; 1986776b82aeSLisandro Dalcin PetscContainer c; 1987103bf8bdSMatthew Knepley PetscInt m, n, M, N; 1988103bf8bdSMatthew Knepley PetscErrorCode ierr; 1989103bf8bdSMatthew Knepley 1990103bf8bdSMatthew Knepley PetscFunctionBegin; 1991e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 1992103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 1993103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 1994103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 1995e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 1996103bf8bdSMatthew Knepley } 1997103bf8bdSMatthew Knepley 1998103bf8bdSMatthew Knepley process_group_type pg; 1999a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2000a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2001a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2002a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2003a2c909beSMatthew Knepley 2004103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2005a2c909beSMatthew Knepley ilu_permuted(level_graph); 2006103bf8bdSMatthew Knepley 2007103bf8bdSMatthew Knepley /* put together the new matrix */ 20087adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 2009103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2010103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2011719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2012719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2013719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2014719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2015103bf8bdSMatthew Knepley 20167adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 2017776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2018719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2019103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2020103bf8bdSMatthew Knepley } 2021103bf8bdSMatthew Knepley 2022103bf8bdSMatthew Knepley #undef __FUNCT__ 2023103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 20240481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2025103bf8bdSMatthew Knepley { 2026103bf8bdSMatthew Knepley PetscFunctionBegin; 2027103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2028103bf8bdSMatthew Knepley } 2029103bf8bdSMatthew Knepley 2030103bf8bdSMatthew Knepley #undef __FUNCT__ 2031103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2032103bf8bdSMatthew Knepley /* 2033103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2034103bf8bdSMatthew Knepley */ 2035103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2036103bf8bdSMatthew Knepley { 2037a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2038a2c909beSMatthew Knepley 2039a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2040a2c909beSMatthew Knepley lgraph_type* lgraph_p; 2041776b82aeSLisandro Dalcin PetscContainer c; 2042103bf8bdSMatthew Knepley PetscErrorCode ierr; 2043103bf8bdSMatthew Knepley 2044103bf8bdSMatthew Knepley PetscFunctionBegin; 2045103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 2046776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 2047103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2048a2c909beSMatthew Knepley 2049a2c909beSMatthew Knepley PetscScalar* array_x; 2050a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2051a2c909beSMatthew Knepley PetscInt sx; 2052a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2053a2c909beSMatthew Knepley 2054a2c909beSMatthew Knepley PetscScalar* array_b; 2055a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2056a2c909beSMatthew Knepley PetscInt sb; 2057a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2058a2c909beSMatthew Knepley 2059a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2060a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2061a2c909beSMatthew Knepley 2062a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2063a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2064a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2065a2c909beSMatthew Knepley 2066a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2067a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2068a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2069a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2070a2c909beSMatthew Knepley 2071a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2072a2c909beSMatthew Knepley 2073103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2074103bf8bdSMatthew Knepley } 2075103bf8bdSMatthew Knepley #endif 2076103bf8bdSMatthew Knepley 207769db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 207869db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 20791d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 20801d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 208169db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 208269db28dcSHong Zhang PetscErrorCode (*MatDestroy)(Mat); 208369db28dcSHong Zhang } Mat_Redundant; 208469db28dcSHong Zhang 208569db28dcSHong Zhang #undef __FUNCT__ 208669db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 208769db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 208869db28dcSHong Zhang { 208969db28dcSHong Zhang PetscErrorCode ierr; 209069db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 209169db28dcSHong Zhang PetscInt i; 209269db28dcSHong Zhang 209369db28dcSHong Zhang PetscFunctionBegin; 20941d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 209569db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 209669db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 209769db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 209869db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 209969db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 210069db28dcSHong Zhang } 21011d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 210269db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 210369db28dcSHong Zhang PetscFunctionReturn(0); 210469db28dcSHong Zhang } 210569db28dcSHong Zhang 210669db28dcSHong Zhang #undef __FUNCT__ 210769db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 210869db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 210969db28dcSHong Zhang { 211069db28dcSHong Zhang PetscErrorCode ierr; 211169db28dcSHong Zhang PetscContainer container; 211269db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 211369db28dcSHong Zhang 211469db28dcSHong Zhang PetscFunctionBegin; 211569db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 211669db28dcSHong Zhang if (container) { 211769db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 211869db28dcSHong Zhang } else { 2119e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 212069db28dcSHong Zhang } 212169db28dcSHong Zhang A->ops->destroy = redund->MatDestroy; 212269db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 212369db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 212469db28dcSHong Zhang ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 212569db28dcSHong Zhang PetscFunctionReturn(0); 212669db28dcSHong Zhang } 212769db28dcSHong Zhang 212869db28dcSHong Zhang #undef __FUNCT__ 212969db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 213069db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 213169db28dcSHong Zhang { 213269db28dcSHong Zhang PetscMPIInt rank,size; 21337adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 213469db28dcSHong Zhang PetscErrorCode ierr; 213569db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 213669db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2137d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 213869db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 213969db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 214069db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 214169db28dcSHong Zhang PetscScalar *sbuf_a; 214269db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2143d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2144d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 214569db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2146a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2147a77337e4SBarry Smith PetscScalar *vals; 214869db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 214969db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 215069db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 215169db28dcSHong Zhang MPI_Status recv_status,*send_status; 215269db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 215369db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 215469db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 215569db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 215669db28dcSHong Zhang PetscContainer container; 215769db28dcSHong Zhang 215869db28dcSHong Zhang PetscFunctionBegin; 215969db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 216069db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 216169db28dcSHong Zhang 216269db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 216369db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2164e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 216569db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2166e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 216769db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 216869db28dcSHong Zhang if (container) { 216969db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 217069db28dcSHong Zhang } else { 2171e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 217269db28dcSHong Zhang } 2173e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 217469db28dcSHong Zhang 217569db28dcSHong Zhang nsends = redund->nsends; 217669db28dcSHong Zhang nrecvs = redund->nrecvs; 21771d79065fSBarry Smith send_rank = redund->send_rank; 21781d79065fSBarry Smith recv_rank = redund->recv_rank; 21791d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 21801d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 218169db28dcSHong Zhang sbuf_j = redund->sbuf_j; 218269db28dcSHong Zhang sbuf_a = redund->sbuf_a; 218369db28dcSHong Zhang rbuf_j = redund->rbuf_j; 218469db28dcSHong Zhang rbuf_a = redund->rbuf_a; 218569db28dcSHong Zhang } 218669db28dcSHong Zhang 218769db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 218869db28dcSHong Zhang PetscMPIInt subrank,subsize; 218969db28dcSHong Zhang PetscInt nleftover,np_subcomm; 219069db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 219169db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 219269db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 21931d79065fSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank); 219469db28dcSHong Zhang np_subcomm = size/nsubcomm; 219569db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 219669db28dcSHong Zhang nsends = 0; nrecvs = 0; 219769db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 219869db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 219969db28dcSHong Zhang send_rank[nsends] = i; nsends++; 220069db28dcSHong Zhang recv_rank[nrecvs++] = i; 220169db28dcSHong Zhang } 220269db28dcSHong Zhang } 220369db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 220469db28dcSHong Zhang i = size-nleftover-1; 220569db28dcSHong Zhang j = 0; 220669db28dcSHong Zhang while (j < nsubcomm - nleftover){ 220769db28dcSHong Zhang send_rank[nsends++] = i; 220869db28dcSHong Zhang i--; j++; 220969db28dcSHong Zhang } 221069db28dcSHong Zhang } 221169db28dcSHong Zhang 221269db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 221369db28dcSHong Zhang for (i=0; i<nleftover; i++){ 221469db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 221569db28dcSHong Zhang } 221669db28dcSHong Zhang } 221769db28dcSHong Zhang 221869db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 221969db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 222069db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 222169db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 222269db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 222369db28dcSHong Zhang 222469db28dcSHong Zhang /* copy mat's local entries into the buffers */ 222569db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 222669db28dcSHong Zhang rownz_max = 0; 222769db28dcSHong Zhang rptr = sbuf_j; 222869db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 222969db28dcSHong Zhang vals = sbuf_a; 223069db28dcSHong Zhang rptr[0] = 0; 223169db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 223269db28dcSHong Zhang row = i + rstart; 223369db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 223469db28dcSHong Zhang ncols = nzA + nzB; 223569db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 223669db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 223769db28dcSHong Zhang /* load the column indices for this row into cols */ 223869db28dcSHong Zhang lwrite = 0; 223969db28dcSHong Zhang for (l=0; l<nzB; l++) { 224069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 224169db28dcSHong Zhang vals[lwrite] = aworkB[l]; 224269db28dcSHong Zhang cols[lwrite++] = ctmp; 224369db28dcSHong Zhang } 224469db28dcSHong Zhang } 224569db28dcSHong Zhang for (l=0; l<nzA; l++){ 224669db28dcSHong Zhang vals[lwrite] = aworkA[l]; 224769db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 224869db28dcSHong Zhang } 224969db28dcSHong Zhang for (l=0; l<nzB; l++) { 225069db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 225169db28dcSHong Zhang vals[lwrite] = aworkB[l]; 225269db28dcSHong Zhang cols[lwrite++] = ctmp; 225369db28dcSHong Zhang } 225469db28dcSHong Zhang } 225569db28dcSHong Zhang vals += ncols; 225669db28dcSHong Zhang cols += ncols; 225769db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 225869db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 225969db28dcSHong Zhang } 2260e32f2f54SBarry 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); 226169db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 226269db28dcSHong Zhang rptr = sbuf_j; 226369db28dcSHong Zhang vals = sbuf_a; 226469db28dcSHong Zhang rptr[0] = 0; 226569db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 226669db28dcSHong Zhang row = i + rstart; 226769db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 226869db28dcSHong Zhang ncols = nzA + nzB; 226969db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 227069db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 227169db28dcSHong Zhang lwrite = 0; 227269db28dcSHong Zhang for (l=0; l<nzB; l++) { 227369db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 227469db28dcSHong Zhang } 227569db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 227669db28dcSHong Zhang for (l=0; l<nzB; l++) { 227769db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 227869db28dcSHong Zhang } 227969db28dcSHong Zhang vals += ncols; 228069db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 228169db28dcSHong Zhang } 228269db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 228369db28dcSHong Zhang 228469db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 228569db28dcSHong Zhang /*--------------------------------------------------*/ 228669db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 228769db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 228869db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 228969db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 229069db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 229169db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 229269db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 229369db28dcSHong Zhang } else { 229469db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 229569db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 229669db28dcSHong Zhang } 229769db28dcSHong Zhang 229869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 229969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 230069db28dcSHong Zhang /* get new tags to keep the communication clean */ 230169db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 230269db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 23031d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 230469db28dcSHong Zhang 230569db28dcSHong Zhang /* post receives of other's nzlocal */ 230669db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 230769db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 230869db28dcSHong Zhang } 230969db28dcSHong Zhang /* send nzlocal to others */ 231069db28dcSHong Zhang for (i=0; i<nsends; i++){ 231169db28dcSHong Zhang sbuf_nz[i] = nzlocal; 231269db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 231369db28dcSHong Zhang } 231469db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 231569db28dcSHong Zhang count = nrecvs; 231669db28dcSHong Zhang while (count) { 231769db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 231869db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 231969db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 232069db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 232169db28dcSHong Zhang 232269db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 232369db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 232469db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 232569db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 232669db28dcSHong Zhang count--; 232769db28dcSHong Zhang } 232869db28dcSHong Zhang /* wait on sends of nzlocal */ 232969db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 233069db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 233169db28dcSHong Zhang /*------------------------------------------------*/ 233269db28dcSHong Zhang for (i=0; i<nsends; i++){ 233369db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 233469db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 233569db28dcSHong Zhang } 233669db28dcSHong Zhang /* wait on receives of mat->i,j */ 233769db28dcSHong Zhang /*------------------------------*/ 233869db28dcSHong Zhang count = nrecvs; 233969db28dcSHong Zhang while (count) { 234069db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2341e32f2f54SBarry 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); 234269db28dcSHong Zhang count--; 234369db28dcSHong Zhang } 234469db28dcSHong Zhang /* wait on sends of mat->i,j */ 234569db28dcSHong Zhang /*---------------------------*/ 234669db28dcSHong Zhang if (nsends) { 234769db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 234869db28dcSHong Zhang } 234969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 235069db28dcSHong Zhang 235169db28dcSHong Zhang /* post receives, send and receive mat->a */ 235269db28dcSHong Zhang /*----------------------------------------*/ 235369db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 235469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 235569db28dcSHong Zhang } 235669db28dcSHong Zhang for (i=0; i<nsends; i++){ 235769db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 235869db28dcSHong Zhang } 235969db28dcSHong Zhang count = nrecvs; 236069db28dcSHong Zhang while (count) { 236169db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2362e32f2f54SBarry 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); 236369db28dcSHong Zhang count--; 236469db28dcSHong Zhang } 236569db28dcSHong Zhang if (nsends) { 236669db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 236769db28dcSHong Zhang } 236869db28dcSHong Zhang 236969db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 237069db28dcSHong Zhang 237169db28dcSHong Zhang /* create redundant matrix */ 237269db28dcSHong Zhang /*-------------------------*/ 237369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 237469db28dcSHong Zhang /* compute rownz_max for preallocation */ 237569db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 237669db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 237769db28dcSHong Zhang rptr = rbuf_j[imdex]; 237869db28dcSHong Zhang for (i=0; i<j; i++){ 237969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 238069db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 238169db28dcSHong Zhang } 238269db28dcSHong Zhang } 238369db28dcSHong Zhang 238469db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 238569db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 238669db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 238769db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 238869db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 238969db28dcSHong Zhang } else { 239069db28dcSHong Zhang C = *matredundant; 239169db28dcSHong Zhang } 239269db28dcSHong Zhang 239369db28dcSHong Zhang /* insert local matrix entries */ 239469db28dcSHong Zhang rptr = sbuf_j; 239569db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 239669db28dcSHong Zhang vals = sbuf_a; 239769db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 239869db28dcSHong Zhang row = i + rstart; 239969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 240069db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 240169db28dcSHong Zhang vals += ncols; 240269db28dcSHong Zhang cols += ncols; 240369db28dcSHong Zhang } 240469db28dcSHong Zhang /* insert received matrix entries */ 240569db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 240669db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 240769db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 240869db28dcSHong Zhang rptr = rbuf_j[imdex]; 240969db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 241069db28dcSHong Zhang vals = rbuf_a[imdex]; 241169db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 241269db28dcSHong Zhang row = i + rstart; 241369db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 241469db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 241569db28dcSHong Zhang vals += ncols; 241669db28dcSHong Zhang cols += ncols; 241769db28dcSHong Zhang } 241869db28dcSHong Zhang } 241969db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 242069db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 242169db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2422e32f2f54SBarry 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); 242369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 242469db28dcSHong Zhang PetscContainer container; 242569db28dcSHong Zhang *matredundant = C; 242669db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 242738f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 242869db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 242969db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 243069db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 243169db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 243269db28dcSHong Zhang 243369db28dcSHong Zhang redund->nzlocal = nzlocal; 243469db28dcSHong Zhang redund->nsends = nsends; 243569db28dcSHong Zhang redund->nrecvs = nrecvs; 243669db28dcSHong Zhang redund->send_rank = send_rank; 24371d79065fSBarry Smith redund->recv_rank = recv_rank; 243869db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 24391d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 244069db28dcSHong Zhang redund->sbuf_j = sbuf_j; 244169db28dcSHong Zhang redund->sbuf_a = sbuf_a; 244269db28dcSHong Zhang redund->rbuf_j = rbuf_j; 244369db28dcSHong Zhang redund->rbuf_a = rbuf_a; 244469db28dcSHong Zhang 244569db28dcSHong Zhang redund->MatDestroy = C->ops->destroy; 244669db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 244769db28dcSHong Zhang } 244869db28dcSHong Zhang PetscFunctionReturn(0); 244969db28dcSHong Zhang } 245069db28dcSHong Zhang 245103bc72f1SMatthew Knepley #undef __FUNCT__ 2452c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2453c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2454c91732d9SHong Zhang { 2455c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2456c91732d9SHong Zhang PetscErrorCode ierr; 2457c91732d9SHong Zhang PetscInt i,*idxb = 0; 2458c91732d9SHong Zhang PetscScalar *va,*vb; 2459c91732d9SHong Zhang Vec vtmp; 2460c91732d9SHong Zhang 2461c91732d9SHong Zhang PetscFunctionBegin; 2462c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2463c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2464c91732d9SHong Zhang if (idx) { 2465192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2466d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2467c91732d9SHong Zhang } 2468c91732d9SHong Zhang } 2469c91732d9SHong Zhang 2470d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2471c91732d9SHong Zhang if (idx) { 2472d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2473c91732d9SHong Zhang } 2474c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2475c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2476c91732d9SHong Zhang 2477d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2478c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2479c91732d9SHong Zhang va[i] = vb[i]; 2480c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2481c91732d9SHong Zhang } 2482c91732d9SHong Zhang } 2483c91732d9SHong Zhang 2484c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2485c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2486c91732d9SHong Zhang if (idxb) { 2487c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 2488c91732d9SHong Zhang } 2489c91732d9SHong Zhang ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2490c91732d9SHong Zhang PetscFunctionReturn(0); 2491c91732d9SHong Zhang } 2492c91732d9SHong Zhang 2493c91732d9SHong Zhang #undef __FUNCT__ 2494c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2495c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2496c87e5d42SMatthew Knepley { 2497c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2498c87e5d42SMatthew Knepley PetscErrorCode ierr; 2499c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2500c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2501c87e5d42SMatthew Knepley Vec vtmp; 2502c87e5d42SMatthew Knepley 2503c87e5d42SMatthew Knepley PetscFunctionBegin; 2504c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2505c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2506c87e5d42SMatthew Knepley if (idx) { 2507c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2508c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2509c87e5d42SMatthew Knepley } 2510c87e5d42SMatthew Knepley } 2511c87e5d42SMatthew Knepley 2512c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2513c87e5d42SMatthew Knepley if (idx) { 2514c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2515c87e5d42SMatthew Knepley } 2516c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2517c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2518c87e5d42SMatthew Knepley 2519c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2520c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2521c87e5d42SMatthew Knepley va[i] = vb[i]; 2522c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2523c87e5d42SMatthew Knepley } 2524c87e5d42SMatthew Knepley } 2525c87e5d42SMatthew Knepley 2526c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2527c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2528c87e5d42SMatthew Knepley if (idxb) { 2529c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 2530c87e5d42SMatthew Knepley } 2531c87e5d42SMatthew Knepley ierr = VecDestroy(vtmp);CHKERRQ(ierr); 2532c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2533c87e5d42SMatthew Knepley } 2534c87e5d42SMatthew Knepley 2535c87e5d42SMatthew Knepley #undef __FUNCT__ 253603bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 253703bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 253803bc72f1SMatthew Knepley { 253903bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2540d0f46423SBarry Smith PetscInt n = A->rmap->n; 2541d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 254203bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 254303bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 254403bc72f1SMatthew Knepley Vec diagV, offdiagV; 254503bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 254603bc72f1SMatthew Knepley PetscInt r; 254703bc72f1SMatthew Knepley PetscErrorCode ierr; 254803bc72f1SMatthew Knepley 254903bc72f1SMatthew Knepley PetscFunctionBegin; 255003bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2551e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2552e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 255303bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 255403bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 255503bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 255603bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 255703bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 255803bc72f1SMatthew Knepley for(r = 0; r < n; ++r) { 2559028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 256003bc72f1SMatthew Knepley a[r] = diagA[r]; 256103bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 256203bc72f1SMatthew Knepley } else { 256303bc72f1SMatthew Knepley a[r] = offdiagA[r]; 256403bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 256503bc72f1SMatthew Knepley } 256603bc72f1SMatthew Knepley } 256703bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 256803bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 256903bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 257003bc72f1SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 257103bc72f1SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 257203bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 257303bc72f1SMatthew Knepley PetscFunctionReturn(0); 257403bc72f1SMatthew Knepley } 257503bc72f1SMatthew Knepley 25765494a064SHong Zhang #undef __FUNCT__ 2577c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2578c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2579c87e5d42SMatthew Knepley { 2580c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2581c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2582c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2583c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2584c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2585c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2586c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2587c87e5d42SMatthew Knepley PetscInt r; 2588c87e5d42SMatthew Knepley PetscErrorCode ierr; 2589c87e5d42SMatthew Knepley 2590c87e5d42SMatthew Knepley PetscFunctionBegin; 2591c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2592c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 2593c87e5d42SMatthew Knepley ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 2594c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2595c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2596c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2597c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2598c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2599c87e5d42SMatthew Knepley for(r = 0; r < n; ++r) { 2600c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2601c87e5d42SMatthew Knepley a[r] = diagA[r]; 2602c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2603c87e5d42SMatthew Knepley } else { 2604c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2605c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2606c87e5d42SMatthew Knepley } 2607c87e5d42SMatthew Knepley } 2608c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2609c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2610c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2611c87e5d42SMatthew Knepley ierr = VecDestroy(diagV);CHKERRQ(ierr); 2612c87e5d42SMatthew Knepley ierr = VecDestroy(offdiagV);CHKERRQ(ierr); 2613c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 2614c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2615c87e5d42SMatthew Knepley } 2616c87e5d42SMatthew Knepley 2617c87e5d42SMatthew Knepley #undef __FUNCT__ 2618829201f2SHong Zhang #define __FUNCT__ "MatGetSeqNonzerostructure_MPIAIJ" 2619f6d58c54SBarry Smith PetscErrorCode MatGetSeqNonzerostructure_MPIAIJ(Mat mat,Mat *newmat) 26205494a064SHong Zhang { 26215494a064SHong Zhang PetscErrorCode ierr; 2622f6d58c54SBarry Smith Mat *dummy; 26235494a064SHong Zhang 26245494a064SHong Zhang PetscFunctionBegin; 2625f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 2626f6d58c54SBarry Smith *newmat = *dummy; 2627f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 26285494a064SHong Zhang PetscFunctionReturn(0); 26295494a064SHong Zhang } 26305494a064SHong Zhang 26313acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 26328a729477SBarry Smith /* -------------------------------------------------------------------*/ 2633cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 2634cda55fadSBarry Smith MatGetRow_MPIAIJ, 2635cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 2636cda55fadSBarry Smith MatMult_MPIAIJ, 263797304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 26387c922b88SBarry Smith MatMultTranspose_MPIAIJ, 26397c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 2640103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2641103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 2642103bf8bdSMatthew Knepley #else 2643cda55fadSBarry Smith 0, 2644103bf8bdSMatthew Knepley #endif 2645cda55fadSBarry Smith 0, 2646cda55fadSBarry Smith 0, 264797304618SKris Buschelman /*10*/ 0, 2648cda55fadSBarry Smith 0, 2649cda55fadSBarry Smith 0, 265041f059aeSBarry Smith MatSOR_MPIAIJ, 2651b7c46309SBarry Smith MatTranspose_MPIAIJ, 265297304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 2653cda55fadSBarry Smith MatEqual_MPIAIJ, 2654cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 2655cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 2656cda55fadSBarry Smith MatNorm_MPIAIJ, 265797304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 2658cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 2659cda55fadSBarry Smith MatSetOption_MPIAIJ, 2660cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 2661d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 2662cda55fadSBarry Smith 0, 2663103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2664719d5645SBarry Smith 0, 2665103bf8bdSMatthew Knepley #else 2666cda55fadSBarry Smith 0, 2667103bf8bdSMatthew Knepley #endif 2668cda55fadSBarry Smith 0, 2669cda55fadSBarry Smith 0, 2670d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_MPIAIJ, 2671103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2672719d5645SBarry Smith 0, 2673103bf8bdSMatthew Knepley #else 2674cda55fadSBarry Smith 0, 2675103bf8bdSMatthew Knepley #endif 2676cda55fadSBarry Smith 0, 2677cda55fadSBarry Smith 0, 2678cda55fadSBarry Smith 0, 2679d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 2680cda55fadSBarry Smith 0, 2681cda55fadSBarry Smith 0, 2682cda55fadSBarry Smith 0, 2683cda55fadSBarry Smith 0, 2684d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 2685cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 2686cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 2687cda55fadSBarry Smith MatGetValues_MPIAIJ, 2688cb5b572fSBarry Smith MatCopy_MPIAIJ, 2689d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 2690cda55fadSBarry Smith MatScale_MPIAIJ, 2691cda55fadSBarry Smith 0, 2692cda55fadSBarry Smith 0, 2693cda55fadSBarry Smith 0, 2694d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_MPIAIJ, 2695cda55fadSBarry Smith 0, 2696cda55fadSBarry Smith 0, 2697cda55fadSBarry Smith 0, 2698cda55fadSBarry Smith 0, 2699d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 2700cda55fadSBarry Smith 0, 2701cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 270242e855d1Svictor MatPermute_MPIAIJ, 2703cda55fadSBarry Smith 0, 2704d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 2705e03a110bSBarry Smith MatDestroy_MPIAIJ, 2706e03a110bSBarry Smith MatView_MPIAIJ, 2707357abbc8SBarry Smith 0, 2708a2243be0SBarry Smith 0, 2709d519adbfSMatthew Knepley /*64*/ 0, 2710a2243be0SBarry Smith 0, 2711a2243be0SBarry Smith 0, 2712a2243be0SBarry Smith 0, 2713a2243be0SBarry Smith 0, 2714d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 2715c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 2716a2243be0SBarry Smith 0, 2717a2243be0SBarry Smith MatSetColoring_MPIAIJ, 2718dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 2719779c1a83SBarry Smith MatSetValuesAdic_MPIAIJ, 2720dcf5cc72SBarry Smith #else 2721dcf5cc72SBarry Smith 0, 2722dcf5cc72SBarry Smith #endif 272397304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 27243acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 272597304618SKris Buschelman 0, 272697304618SKris Buschelman 0, 272797304618SKris Buschelman 0, 272897304618SKris Buschelman 0, 272997304618SKris Buschelman /*80*/ 0, 273097304618SKris Buschelman 0, 273197304618SKris Buschelman 0, 27325bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 27336284ec50SHong Zhang 0, 27346284ec50SHong Zhang 0, 27356284ec50SHong Zhang 0, 27366284ec50SHong Zhang 0, 2737865e5f61SKris Buschelman 0, 2738d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 273926be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 274026be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 27417a7894deSKris Buschelman MatPtAP_Basic, 27427a7894deSKris Buschelman MatPtAPSymbolic_MPIAIJ, 2743d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_MPIAIJ, 27447a7894deSKris Buschelman 0, 27457a7894deSKris Buschelman 0, 27467a7894deSKris Buschelman 0, 27477a7894deSKris Buschelman 0, 2748d519adbfSMatthew Knepley /*99*/ 0, 2749865e5f61SKris Buschelman MatPtAPSymbolic_MPIAIJ_MPIAIJ, 27507a7894deSKris Buschelman MatPtAPNumeric_MPIAIJ_MPIAIJ, 27512fd7e33dSBarry Smith MatConjugate_MPIAIJ, 27522fd7e33dSBarry Smith 0, 2753d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 275499cafbc1SBarry Smith MatRealPart_MPIAIJ, 275569db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 275669db28dcSHong Zhang 0, 275769db28dcSHong Zhang 0, 2758d519adbfSMatthew Knepley /*109*/0, 275903bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 27605494a064SHong Zhang MatGetRowMin_MPIAIJ, 27615494a064SHong Zhang 0, 27625494a064SHong Zhang 0, 2763bd0c2dcbSBarry Smith /*114*/MatGetSeqNonzerostructure_MPIAIJ, 2764bd0c2dcbSBarry Smith 0, 2765bd0c2dcbSBarry Smith 0, 2766bd0c2dcbSBarry Smith 0, 2767bd0c2dcbSBarry Smith 0, 27688fb81238SShri Abhyankar /*119*/0, 27698fb81238SShri Abhyankar 0, 27708fb81238SShri Abhyankar 0, 2771d6037b41SHong Zhang 0, 2772d6037b41SHong Zhang MatGetMultiProcBlock_MPIAIJ 2773bd0c2dcbSBarry Smith }; 277436ce4990SBarry Smith 27752e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 27762e8a6d31SBarry Smith 2777fb2e594dSBarry Smith EXTERN_C_BEGIN 27784a2ae208SSatish Balay #undef __FUNCT__ 27794a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 2780be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_MPIAIJ(Mat mat) 27812e8a6d31SBarry Smith { 27822e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2783dfbe8321SBarry Smith PetscErrorCode ierr; 27842e8a6d31SBarry Smith 27852e8a6d31SBarry Smith PetscFunctionBegin; 27862e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 27872e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 27882e8a6d31SBarry Smith PetscFunctionReturn(0); 27892e8a6d31SBarry Smith } 2790fb2e594dSBarry Smith EXTERN_C_END 27912e8a6d31SBarry Smith 2792fb2e594dSBarry Smith EXTERN_C_BEGIN 27934a2ae208SSatish Balay #undef __FUNCT__ 27944a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 2795be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_MPIAIJ(Mat mat) 27962e8a6d31SBarry Smith { 27972e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2798dfbe8321SBarry Smith PetscErrorCode ierr; 27992e8a6d31SBarry Smith 28002e8a6d31SBarry Smith PetscFunctionBegin; 28012e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 28022e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 28032e8a6d31SBarry Smith PetscFunctionReturn(0); 28042e8a6d31SBarry Smith } 2805fb2e594dSBarry Smith EXTERN_C_END 28068a729477SBarry Smith 280727508adbSBarry Smith EXTERN_C_BEGIN 28084a2ae208SSatish Balay #undef __FUNCT__ 2809a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 2810be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 2811a23d5eceSKris Buschelman { 2812a23d5eceSKris Buschelman Mat_MPIAIJ *b; 2813dfbe8321SBarry Smith PetscErrorCode ierr; 2814b1d57f15SBarry Smith PetscInt i; 2815a23d5eceSKris Buschelman 2816a23d5eceSKris Buschelman PetscFunctionBegin; 2817a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 2818a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 2819e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 2820e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 2821899cda47SBarry Smith 282226283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 282326283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 282426283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 282526283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 2826a23d5eceSKris Buschelman if (d_nnz) { 2827d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 2828e32f2f54SBarry 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]); 2829a23d5eceSKris Buschelman } 2830a23d5eceSKris Buschelman } 2831a23d5eceSKris Buschelman if (o_nnz) { 2832d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 2833e32f2f54SBarry 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]); 2834a23d5eceSKris Buschelman } 2835a23d5eceSKris Buschelman } 2836a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 2837899cda47SBarry Smith 2838526dfc15SBarry Smith if (!B->preallocated) { 2839899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 2840899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 2841d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 2842899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 2843899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 2844899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 2845d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 2846899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 2847899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 2848526dfc15SBarry Smith } 2849899cda47SBarry Smith 2850c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 2851c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 2852526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 2853a23d5eceSKris Buschelman PetscFunctionReturn(0); 2854a23d5eceSKris Buschelman } 2855a23d5eceSKris Buschelman EXTERN_C_END 2856a23d5eceSKris Buschelman 28574a2ae208SSatish Balay #undef __FUNCT__ 28584a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 2859dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 2860d6dfbf8fSBarry Smith { 2861d6dfbf8fSBarry Smith Mat mat; 2862416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 2863dfbe8321SBarry Smith PetscErrorCode ierr; 2864d6dfbf8fSBarry Smith 28653a40ed3dSBarry Smith PetscFunctionBegin; 2866416022c9SBarry Smith *newmat = 0; 28677adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 2868d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 28697adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 28701d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 2871273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 2872e1b6402fSHong Zhang 2873d5f3da31SBarry Smith mat->factortype = matin->factortype; 2874d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 2875c456f294SBarry Smith mat->assembled = PETSC_TRUE; 2876e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 2877273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 2878d6dfbf8fSBarry Smith 287917699dbbSLois Curfman McInnes a->size = oldmat->size; 288017699dbbSLois Curfman McInnes a->rank = oldmat->rank; 2881e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 2882e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 2883e7641de0SSatish Balay a->rowindices = 0; 2884bcd2baecSBarry Smith a->rowvalues = 0; 2885bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 2886d6dfbf8fSBarry Smith 288726283091SBarry Smith ierr = PetscLayoutCopy(matin->rmap,&mat->rmap);CHKERRQ(ierr); 288826283091SBarry Smith ierr = PetscLayoutCopy(matin->cmap,&mat->cmap);CHKERRQ(ierr); 2889899cda47SBarry Smith 28902ee70a88SLois Curfman McInnes if (oldmat->colmap) { 2891aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 28920f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 2893b1fc9764SSatish Balay #else 2894d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 2895d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 2896d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 2897b1fc9764SSatish Balay #endif 2898416022c9SBarry Smith } else a->colmap = 0; 28993f41c07dSBarry Smith if (oldmat->garray) { 2900b1d57f15SBarry Smith PetscInt len; 2901d0f46423SBarry Smith len = oldmat->B->cmap->n; 2902b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 290352e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 2904b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 2905416022c9SBarry Smith } else a->garray = 0; 2906d6dfbf8fSBarry Smith 2907416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 290852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 2909a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 291052e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 29112e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 291252e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 29132e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 291452e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 29157adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 29168a729477SBarry Smith *newmat = mat; 29173a40ed3dSBarry Smith PetscFunctionReturn(0); 29188a729477SBarry Smith } 2919416022c9SBarry Smith 29201a4ee126SBarry Smith /* 29211a4ee126SBarry Smith Allows sending/receiving larger messages then 2 gigabytes in a single call 29221a4ee126SBarry Smith */ 29231a4ee126SBarry Smith static int MPILong_Send(void *mess,PetscInt cnt, MPI_Datatype type,int to, int tag, MPI_Comm comm) 29241a4ee126SBarry Smith { 29251a4ee126SBarry Smith int ierr; 29261a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 29271a4ee126SBarry Smith PetscInt i,numchunks; 29281a4ee126SBarry Smith PetscMPIInt icnt; 29291a4ee126SBarry Smith 29301a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 29311a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 29321a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 29331a4ee126SBarry Smith ierr = MPI_Send(mess,icnt,type,to,tag,comm); 29341a4ee126SBarry Smith if (type == MPIU_INT) { 29351a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 29361a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 29371a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 29381a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 29391a4ee126SBarry Smith } 29401a4ee126SBarry Smith return 0; 29411a4ee126SBarry Smith } 29421a4ee126SBarry Smith static int MPILong_Recv(void *mess,PetscInt cnt, MPI_Datatype type,int from, int tag, MPI_Comm comm) 29431a4ee126SBarry Smith { 29441a4ee126SBarry Smith int ierr; 29451a4ee126SBarry Smith static PetscInt CHUNKSIZE = 250000000; /* 250,000,000 */ 29461a4ee126SBarry Smith MPI_Status status; 29471a4ee126SBarry Smith PetscInt i,numchunks; 29481a4ee126SBarry Smith PetscMPIInt icnt; 29491a4ee126SBarry Smith 29501a4ee126SBarry Smith numchunks = cnt/CHUNKSIZE + 1; 29511a4ee126SBarry Smith for (i=0; i<numchunks; i++) { 29521a4ee126SBarry Smith icnt = PetscMPIIntCast((i < numchunks-1) ? CHUNKSIZE : cnt - (numchunks-1)*CHUNKSIZE); 29531a4ee126SBarry Smith ierr = MPI_Recv(mess,icnt,type,from,tag,comm,&status); 29541a4ee126SBarry Smith if (type == MPIU_INT) { 29551a4ee126SBarry Smith mess = (void*) (((PetscInt*)mess) + CHUNKSIZE); 29561a4ee126SBarry Smith } else if (type == MPIU_SCALAR) { 29571a4ee126SBarry Smith mess = (void*) (((PetscScalar*)mess) + CHUNKSIZE); 29581a4ee126SBarry Smith } else SETERRQ(comm,PETSC_ERR_SUP,"No support for this datatype"); 29591a4ee126SBarry Smith } 29601a4ee126SBarry Smith return 0; 29611a4ee126SBarry Smith } 29621a4ee126SBarry Smith 29634a2ae208SSatish Balay #undef __FUNCT__ 29645bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 2965112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 29668fb81238SShri Abhyankar { 29678fb81238SShri Abhyankar PetscScalar *vals,*svals; 29688fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 29698fb81238SShri Abhyankar PetscErrorCode ierr; 29701a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 29718fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 29728fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 29738fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 29748fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 29758fb81238SShri Abhyankar int fd; 29768fb81238SShri Abhyankar 29778fb81238SShri Abhyankar PetscFunctionBegin; 29788fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 29798fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 29808fb81238SShri Abhyankar if (!rank) { 29818fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 29828fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 29838fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 29848fb81238SShri Abhyankar } 29858fb81238SShri Abhyankar 29868fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 29878fb81238SShri Abhyankar 29888fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 29898fb81238SShri Abhyankar M = header[1]; N = header[2]; 29908fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 29918fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 29928fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 29938fb81238SShri Abhyankar 29948fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 29958fb81238SShri Abhyankar if (sizesset) { 29968fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 29978fb81238SShri Abhyankar } 2998abd38a8fSBarry 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); 2999abd38a8fSBarry 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); 30008fb81238SShri Abhyankar 30018fb81238SShri Abhyankar /* determine ownership of all rows */ 30028fb81238SShri Abhyankar if (newMat->rmap->n < 0 ) m = M/size + ((M % size) > rank); /* PETSC_DECIDE */ 30034683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 30048fb81238SShri Abhyankar 30058fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 30068fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 30078fb81238SShri Abhyankar 30088fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 30098fb81238SShri Abhyankar if (!rank) { 30108fb81238SShri Abhyankar mmax = rowners[1]; 30118fb81238SShri Abhyankar for (i=2; i<size; i++) { 30128fb81238SShri Abhyankar mmax = PetscMax(mmax,rowners[i]); 30138fb81238SShri Abhyankar } 30148fb81238SShri Abhyankar } else mmax = m; 30158fb81238SShri Abhyankar 30168fb81238SShri Abhyankar rowners[0] = 0; 30178fb81238SShri Abhyankar for (i=2; i<=size; i++) { 30188fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 30198fb81238SShri Abhyankar } 30208fb81238SShri Abhyankar rstart = rowners[rank]; 30218fb81238SShri Abhyankar rend = rowners[rank+1]; 30228fb81238SShri Abhyankar 30238fb81238SShri Abhyankar /* distribute row lengths to all processors */ 30248fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 30258fb81238SShri Abhyankar if (!rank) { 30268fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 30278fb81238SShri Abhyankar ierr = PetscMalloc(m*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 30288fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 30298fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 30308fb81238SShri Abhyankar for (j=0; j<m; j++) { 30318fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 30328fb81238SShri Abhyankar } 30338fb81238SShri Abhyankar for (i=1; i<size; i++) { 30348fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 30358fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 30368fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 30378fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 30388fb81238SShri Abhyankar } 30391a4ee126SBarry Smith ierr = MPILong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 30408fb81238SShri Abhyankar } 30418fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 30428fb81238SShri Abhyankar } else { 30431a4ee126SBarry Smith ierr = MPILong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 30448fb81238SShri Abhyankar } 30458fb81238SShri Abhyankar 30468fb81238SShri Abhyankar if (!rank) { 30478fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 30488fb81238SShri Abhyankar maxnz = 0; 30498fb81238SShri Abhyankar for (i=0; i<size; i++) { 30508fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 30518fb81238SShri Abhyankar } 30528fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 30538fb81238SShri Abhyankar 30548fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 30558fb81238SShri Abhyankar nz = procsnz[0]; 30568fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 30578fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 30588fb81238SShri Abhyankar 30598fb81238SShri Abhyankar /* read in every one elses and ship off */ 30608fb81238SShri Abhyankar for (i=1; i<size; i++) { 30618fb81238SShri Abhyankar nz = procsnz[i]; 30628fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 30631a4ee126SBarry Smith ierr = MPILong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 30648fb81238SShri Abhyankar } 30658fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 30668fb81238SShri Abhyankar } else { 30678fb81238SShri Abhyankar /* determine buffer space needed for message */ 30688fb81238SShri Abhyankar nz = 0; 30698fb81238SShri Abhyankar for (i=0; i<m; i++) { 30708fb81238SShri Abhyankar nz += ourlens[i]; 30718fb81238SShri Abhyankar } 30728fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 30738fb81238SShri Abhyankar 30748fb81238SShri Abhyankar /* receive message of column indices*/ 30751a4ee126SBarry Smith ierr = MPILong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 30768fb81238SShri Abhyankar } 30778fb81238SShri Abhyankar 30788fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 30798fb81238SShri Abhyankar if (N != M) { 30808fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 30818fb81238SShri Abhyankar else n = newMat->cmap->n; 30828fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 30838fb81238SShri Abhyankar cstart = cend - n; 30848fb81238SShri Abhyankar } else { 30858fb81238SShri Abhyankar cstart = rstart; 30868fb81238SShri Abhyankar cend = rend; 30878fb81238SShri Abhyankar n = cend - cstart; 30888fb81238SShri Abhyankar } 30898fb81238SShri Abhyankar 30908fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 30918fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 30928fb81238SShri Abhyankar jj = 0; 30938fb81238SShri Abhyankar for (i=0; i<m; i++) { 30948fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 30958fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 30968fb81238SShri Abhyankar jj++; 30978fb81238SShri Abhyankar } 30988fb81238SShri Abhyankar } 30998fb81238SShri Abhyankar 31008fb81238SShri Abhyankar for (i=0; i<m; i++) { 31018fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 31028fb81238SShri Abhyankar } 31038fb81238SShri Abhyankar if (!sizesset) { 31048fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 31058fb81238SShri Abhyankar } 31068fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 31078fb81238SShri Abhyankar 31088fb81238SShri Abhyankar for (i=0; i<m; i++) { 31098fb81238SShri Abhyankar ourlens[i] += offlens[i]; 31108fb81238SShri Abhyankar } 31118fb81238SShri Abhyankar 31128fb81238SShri Abhyankar if (!rank) { 31138fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 31148fb81238SShri Abhyankar 31158fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 31168fb81238SShri Abhyankar nz = procsnz[0]; 31178fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 31188fb81238SShri Abhyankar 31198fb81238SShri Abhyankar /* insert into matrix */ 31208fb81238SShri Abhyankar jj = rstart; 31218fb81238SShri Abhyankar smycols = mycols; 31228fb81238SShri Abhyankar svals = vals; 31238fb81238SShri Abhyankar for (i=0; i<m; i++) { 31248fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 31258fb81238SShri Abhyankar smycols += ourlens[i]; 31268fb81238SShri Abhyankar svals += ourlens[i]; 31278fb81238SShri Abhyankar jj++; 31288fb81238SShri Abhyankar } 31298fb81238SShri Abhyankar 31308fb81238SShri Abhyankar /* read in other processors and ship out */ 31318fb81238SShri Abhyankar for (i=1; i<size; i++) { 31328fb81238SShri Abhyankar nz = procsnz[i]; 31338fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 31341a4ee126SBarry Smith ierr = MPILong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 31358fb81238SShri Abhyankar } 31368fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 31378fb81238SShri Abhyankar } else { 31388fb81238SShri Abhyankar /* receive numeric values */ 31398fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 31408fb81238SShri Abhyankar 31418fb81238SShri Abhyankar /* receive message of values*/ 31421a4ee126SBarry Smith ierr = MPILong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 31438fb81238SShri Abhyankar 31448fb81238SShri Abhyankar /* insert into matrix */ 31458fb81238SShri Abhyankar jj = rstart; 31468fb81238SShri Abhyankar smycols = mycols; 31478fb81238SShri Abhyankar svals = vals; 31488fb81238SShri Abhyankar for (i=0; i<m; i++) { 31498fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 31508fb81238SShri Abhyankar smycols += ourlens[i]; 31518fb81238SShri Abhyankar svals += ourlens[i]; 31528fb81238SShri Abhyankar jj++; 31538fb81238SShri Abhyankar } 31548fb81238SShri Abhyankar } 31558fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 31568fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 31578fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 31588fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 31598fb81238SShri Abhyankar 31608fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 31618fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 31628fb81238SShri Abhyankar PetscFunctionReturn(0); 31638fb81238SShri Abhyankar } 31648fb81238SShri Abhyankar 31658fb81238SShri Abhyankar #undef __FUNCT__ 31664a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 31674aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 31684aa3045dSJed Brown { 31694aa3045dSJed Brown PetscErrorCode ierr; 31704aa3045dSJed Brown IS iscol_local; 31714aa3045dSJed Brown PetscInt csize; 31724aa3045dSJed Brown 31734aa3045dSJed Brown PetscFunctionBegin; 31744aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3175b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3176b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3177e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3178b79d0421SJed Brown } else { 31794aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3180b79d0421SJed Brown } 31814aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3182b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3183b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 31844aa3045dSJed Brown ierr = ISDestroy(iscol_local);CHKERRQ(ierr); 3185b79d0421SJed Brown } 31864aa3045dSJed Brown PetscFunctionReturn(0); 31874aa3045dSJed Brown } 31884aa3045dSJed Brown 31894aa3045dSJed Brown #undef __FUNCT__ 31904aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3191a0ff6018SBarry Smith /* 319229da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 319329da9460SBarry Smith in local and then by concatenating the local matrices the end result. 319429da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 31954aa3045dSJed Brown 31964aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3197a0ff6018SBarry Smith */ 31984aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3199a0ff6018SBarry Smith { 3200dfbe8321SBarry Smith PetscErrorCode ierr; 320132dcc486SBarry Smith PetscMPIInt rank,size; 3202b1d57f15SBarry Smith PetscInt i,m,n,rstart,row,rend,nz,*cwork,j; 3203b1d57f15SBarry Smith PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal; 3204fee21e36SBarry Smith Mat *local,M,Mreuse; 3205a77337e4SBarry Smith MatScalar *vwork,*aa; 32067adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 320700e6dbe6SBarry Smith Mat_SeqAIJ *aij; 32087e2c5f70SBarry Smith 3209a0ff6018SBarry Smith 3210a0ff6018SBarry Smith PetscFunctionBegin; 32111dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 32121dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 321300e6dbe6SBarry Smith 3214fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3215fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3216e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3217fee21e36SBarry Smith local = &Mreuse; 3218fee21e36SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);CHKERRQ(ierr); 3219fee21e36SBarry Smith } else { 3220a0ff6018SBarry Smith ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3221fee21e36SBarry Smith Mreuse = *local; 3222606d414cSSatish Balay ierr = PetscFree(local);CHKERRQ(ierr); 3223fee21e36SBarry Smith } 3224a0ff6018SBarry Smith 3225a0ff6018SBarry Smith /* 3226a0ff6018SBarry Smith m - number of local rows 3227a0ff6018SBarry Smith n - number of columns (same on all processors) 3228a0ff6018SBarry Smith rstart - first row in new global matrix generated 3229a0ff6018SBarry Smith */ 3230fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3231a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3232fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 323300e6dbe6SBarry Smith ii = aij->i; 323400e6dbe6SBarry Smith jj = aij->j; 323500e6dbe6SBarry Smith 3236a0ff6018SBarry Smith /* 323700e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 323800e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3239a0ff6018SBarry Smith */ 324000e6dbe6SBarry Smith 324100e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 32426a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3243ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3244ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3245e2c4fddaSBarry Smith nlocal = m; 32466a6a5d1dSBarry Smith } else { 3247ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3248ab50ec6bSBarry Smith } 3249ab50ec6bSBarry Smith } else { 32506a6a5d1dSBarry Smith nlocal = csize; 32516a6a5d1dSBarry Smith } 3252b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 325300e6dbe6SBarry Smith rstart = rend - nlocal; 325465e19b50SBarry 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); 325500e6dbe6SBarry Smith 325600e6dbe6SBarry Smith /* next, compute all the lengths */ 3257b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 325800e6dbe6SBarry Smith olens = dlens + m; 325900e6dbe6SBarry Smith for (i=0; i<m; i++) { 326000e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 326100e6dbe6SBarry Smith olen = 0; 326200e6dbe6SBarry Smith dlen = 0; 326300e6dbe6SBarry Smith for (j=0; j<jend; j++) { 326400e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 326500e6dbe6SBarry Smith else dlen++; 326600e6dbe6SBarry Smith jj++; 326700e6dbe6SBarry Smith } 326800e6dbe6SBarry Smith olens[i] = olen; 326900e6dbe6SBarry Smith dlens[i] = dlen; 327000e6dbe6SBarry Smith } 3271f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3272f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 32737adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3274e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3275606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3276a0ff6018SBarry Smith } else { 3277b1d57f15SBarry Smith PetscInt ml,nl; 3278a0ff6018SBarry Smith 3279a0ff6018SBarry Smith M = *newmat; 3280a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3281e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3282a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3283c48de900SBarry Smith /* 3284c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3285c48de900SBarry Smith rather than the slower MatSetValues(). 3286c48de900SBarry Smith */ 3287c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3288c48de900SBarry Smith M->assembled = PETSC_FALSE; 3289a0ff6018SBarry Smith } 3290a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3291fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 329200e6dbe6SBarry Smith ii = aij->i; 329300e6dbe6SBarry Smith jj = aij->j; 329400e6dbe6SBarry Smith aa = aij->a; 3295a0ff6018SBarry Smith for (i=0; i<m; i++) { 3296a0ff6018SBarry Smith row = rstart + i; 329700e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 329800e6dbe6SBarry Smith cwork = jj; jj += nz; 329900e6dbe6SBarry Smith vwork = aa; aa += nz; 33008c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3301a0ff6018SBarry Smith } 3302a0ff6018SBarry Smith 3303a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3304a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3305a0ff6018SBarry Smith *newmat = M; 3306fee21e36SBarry Smith 3307fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3308fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3309fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3310fee21e36SBarry Smith ierr = PetscObjectDereference((PetscObject)Mreuse);CHKERRQ(ierr); 3311fee21e36SBarry Smith } 3312fee21e36SBarry Smith 3313a0ff6018SBarry Smith PetscFunctionReturn(0); 3314a0ff6018SBarry Smith } 3315273d9f13SBarry Smith 3316e2e86b8fSSatish Balay EXTERN_C_BEGIN 33174a2ae208SSatish Balay #undef __FUNCT__ 3318ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 3319b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3320ccd8e176SBarry Smith { 3321899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3322899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3323ccd8e176SBarry Smith const PetscInt *JJ; 3324ccd8e176SBarry Smith PetscScalar *values; 3325ccd8e176SBarry Smith PetscErrorCode ierr; 3326ccd8e176SBarry Smith 3327ccd8e176SBarry Smith PetscFunctionBegin; 3328e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3329899cda47SBarry Smith 333026283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr); 333126283091SBarry Smith ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr); 333226283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 333326283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3334d0f46423SBarry Smith m = B->rmap->n; 3335d0f46423SBarry Smith cstart = B->cmap->rstart; 3336d0f46423SBarry Smith cend = B->cmap->rend; 3337d0f46423SBarry Smith rstart = B->rmap->rstart; 3338899cda47SBarry Smith 33391d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3340ccd8e176SBarry Smith 3341ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3342ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3343ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3344ecc77c7aSBarry Smith JJ = J + Ii[i]; 3345e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3346ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3347d0f46423SBarry 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); 3348ecc77c7aSBarry Smith } 3349ecc77c7aSBarry Smith #endif 3350ecc77c7aSBarry Smith 3351ccd8e176SBarry Smith for (i=0; i<m; i++) { 3352b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3353b7940d39SSatish Balay JJ = J + Ii[i]; 3354ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3355ccd8e176SBarry Smith d = 0; 33560daa03b5SJed Brown for (j=0; j<nnz; j++) { 33570daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3358ccd8e176SBarry Smith } 3359ccd8e176SBarry Smith d_nnz[i] = d; 3360ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3361ccd8e176SBarry Smith } 3362ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 33631d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3364ccd8e176SBarry Smith 3365ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3366ccd8e176SBarry Smith else { 3367ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3368ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3369ccd8e176SBarry Smith } 3370ccd8e176SBarry Smith 3371ccd8e176SBarry Smith for (i=0; i<m; i++) { 3372ccd8e176SBarry Smith ii = i + rstart; 3373b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3374b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3375ccd8e176SBarry Smith } 3376ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3377ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3378ccd8e176SBarry Smith 3379ccd8e176SBarry Smith if (!v) { 3380ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3381ccd8e176SBarry Smith } 3382ccd8e176SBarry Smith PetscFunctionReturn(0); 3383ccd8e176SBarry Smith } 3384e2e86b8fSSatish Balay EXTERN_C_END 3385ccd8e176SBarry Smith 3386ccd8e176SBarry Smith #undef __FUNCT__ 3387ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 33881eea217eSSatish Balay /*@ 3389ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3390ccd8e176SBarry Smith (the default parallel PETSc format). 3391ccd8e176SBarry Smith 3392ccd8e176SBarry Smith Collective on MPI_Comm 3393ccd8e176SBarry Smith 3394ccd8e176SBarry Smith Input Parameters: 3395a1661176SMatthew Knepley + B - the matrix 3396ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 33970daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3398ccd8e176SBarry Smith - v - optional values in the matrix 3399ccd8e176SBarry Smith 3400ccd8e176SBarry Smith Level: developer 3401ccd8e176SBarry Smith 340212251496SSatish Balay Notes: 340312251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 340412251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 340512251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 340612251496SSatish Balay 340712251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 340812251496SSatish Balay 340912251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 341012251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 341112251496SSatish Balay as shown: 341212251496SSatish Balay 341312251496SSatish Balay 1 0 0 341412251496SSatish Balay 2 0 3 P0 341512251496SSatish Balay ------- 341612251496SSatish Balay 4 5 6 P1 341712251496SSatish Balay 341812251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 341912251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 342012251496SSatish Balay j = {0,0,2} [size = nz = 6] 342112251496SSatish Balay v = {1,2,3} [size = nz = 6] 342212251496SSatish Balay 342312251496SSatish Balay Process1 [P1]: rows_owned=[2] 342412251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 342512251496SSatish Balay j = {0,1,2} [size = nz = 6] 342612251496SSatish Balay v = {4,5,6} [size = nz = 6] 342712251496SSatish Balay 3428ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3429ccd8e176SBarry Smith 34302fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ, 34318d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3432ccd8e176SBarry Smith @*/ 3433be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3434ccd8e176SBarry Smith { 34354ac538c5SBarry Smith PetscErrorCode ierr; 3436ccd8e176SBarry Smith 3437ccd8e176SBarry Smith PetscFunctionBegin; 34384ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3439ccd8e176SBarry Smith PetscFunctionReturn(0); 3440ccd8e176SBarry Smith } 3441ccd8e176SBarry Smith 3442ccd8e176SBarry Smith #undef __FUNCT__ 34434a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3444273d9f13SBarry Smith /*@C 3445ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3446273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3447273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3448273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3449273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3450273d9f13SBarry Smith 3451273d9f13SBarry Smith Collective on MPI_Comm 3452273d9f13SBarry Smith 3453273d9f13SBarry Smith Input Parameters: 3454273d9f13SBarry Smith + A - the matrix 3455273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3456273d9f13SBarry Smith (same value is used for all local rows) 3457273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3458273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3459273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3460273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3461273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3462273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3463273d9f13SBarry Smith submatrix (same value is used for all local rows). 3464273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3465273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3466273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3467273d9f13SBarry Smith structure. The size of this array is equal to the number 3468273d9f13SBarry Smith of local rows, i.e 'm'. 3469273d9f13SBarry Smith 347049a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 347149a6f317SBarry Smith 3472273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3473ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 34740598bfebSBarry Smith storage. The stored row and column indices begin with zero. 34750598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3476273d9f13SBarry Smith 3477273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3478273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3479273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3480273d9f13SBarry Smith 3481273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3482a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3483a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3484a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3485a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3486a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3487a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3488a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3489a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3490273d9f13SBarry Smith 3491273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3492273d9f13SBarry Smith 3493aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3494aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3495aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3496aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3497aa95bbe8SBarry Smith 3498273d9f13SBarry Smith Example usage: 3499273d9f13SBarry Smith 3500273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3501273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3502273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3503273d9f13SBarry Smith as follows: 3504273d9f13SBarry Smith 3505273d9f13SBarry Smith .vb 3506273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3507273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3508273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3509273d9f13SBarry Smith ------------------------------------- 3510273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3511273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3512273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3513273d9f13SBarry Smith ------------------------------------- 3514273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3515273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3516273d9f13SBarry Smith .ve 3517273d9f13SBarry Smith 3518273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3519273d9f13SBarry Smith 3520273d9f13SBarry Smith .vb 3521273d9f13SBarry Smith A B C 3522273d9f13SBarry Smith D E F 3523273d9f13SBarry Smith G H I 3524273d9f13SBarry Smith .ve 3525273d9f13SBarry Smith 3526273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3527273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3528273d9f13SBarry Smith 3529273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3530273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3531273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3532273d9f13SBarry Smith 3533273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3534273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3535273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3536273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3537273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3538273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3539273d9f13SBarry Smith 3540273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3541273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3542273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3543273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3544273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3545273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3546273d9f13SBarry Smith .vb 3547273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3548273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3549273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3550273d9f13SBarry Smith .ve 3551273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3552273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3553273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3554273d9f13SBarry Smith 34 values. 3555273d9f13SBarry Smith 3556273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3557273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3558273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3559273d9f13SBarry Smith .vb 3560273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3561273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3562273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3563273d9f13SBarry Smith .ve 3564273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3565273d9f13SBarry Smith hence pre-allocation is perfect. 3566273d9f13SBarry Smith 3567273d9f13SBarry Smith Level: intermediate 3568273d9f13SBarry Smith 3569273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3570273d9f13SBarry Smith 3571ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(), 3572aa95bbe8SBarry Smith MPIAIJ, MatGetInfo() 3573273d9f13SBarry Smith @*/ 3574be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3575273d9f13SBarry Smith { 35764ac538c5SBarry Smith PetscErrorCode ierr; 3577273d9f13SBarry Smith 3578273d9f13SBarry Smith PetscFunctionBegin; 35794ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 3580273d9f13SBarry Smith PetscFunctionReturn(0); 3581273d9f13SBarry Smith } 3582273d9f13SBarry Smith 35834a2ae208SSatish Balay #undef __FUNCT__ 35842fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 358558d36128SBarry Smith /*@ 35862fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 35872fb0ec9aSBarry Smith CSR format the local rows. 35882fb0ec9aSBarry Smith 35892fb0ec9aSBarry Smith Collective on MPI_Comm 35902fb0ec9aSBarry Smith 35912fb0ec9aSBarry Smith Input Parameters: 35922fb0ec9aSBarry Smith + comm - MPI communicator 35932fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 35942fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 35952fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 35962fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 35972fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 35982fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 35992fb0ec9aSBarry Smith . i - row indices 36002fb0ec9aSBarry Smith . j - column indices 36012fb0ec9aSBarry Smith - a - matrix values 36022fb0ec9aSBarry Smith 36032fb0ec9aSBarry Smith Output Parameter: 36042fb0ec9aSBarry Smith . mat - the matrix 360503bfb495SBarry Smith 36062fb0ec9aSBarry Smith Level: intermediate 36072fb0ec9aSBarry Smith 36082fb0ec9aSBarry Smith Notes: 36092fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 36102fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 36118d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 36122fb0ec9aSBarry Smith 361312251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 361412251496SSatish Balay 361512251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 361612251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 361712251496SSatish Balay as shown: 361812251496SSatish Balay 361912251496SSatish Balay 1 0 0 362012251496SSatish Balay 2 0 3 P0 362112251496SSatish Balay ------- 362212251496SSatish Balay 4 5 6 P1 362312251496SSatish Balay 362412251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 362512251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 362612251496SSatish Balay j = {0,0,2} [size = nz = 6] 362712251496SSatish Balay v = {1,2,3} [size = nz = 6] 362812251496SSatish Balay 362912251496SSatish Balay Process1 [P1]: rows_owned=[2] 363012251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 363112251496SSatish Balay j = {0,1,2} [size = nz = 6] 363212251496SSatish Balay v = {4,5,6} [size = nz = 6] 36332fb0ec9aSBarry Smith 36342fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 36352fb0ec9aSBarry Smith 36362fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 36378d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithSplitArrays() 36382fb0ec9aSBarry Smith @*/ 363982b90586SSatish 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) 36402fb0ec9aSBarry Smith { 36412fb0ec9aSBarry Smith PetscErrorCode ierr; 36422fb0ec9aSBarry Smith 36432fb0ec9aSBarry Smith PetscFunctionBegin; 36442fb0ec9aSBarry Smith if (i[0]) { 3645e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 36462fb0ec9aSBarry Smith } 3647e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 36482fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 3649d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 36502fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 36512fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 36522fb0ec9aSBarry Smith PetscFunctionReturn(0); 36532fb0ec9aSBarry Smith } 36542fb0ec9aSBarry Smith 36552fb0ec9aSBarry Smith #undef __FUNCT__ 36564a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAIJ" 3657273d9f13SBarry Smith /*@C 3658273d9f13SBarry Smith MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format 3659273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3660273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3661273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3662273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3663273d9f13SBarry Smith 3664273d9f13SBarry Smith Collective on MPI_Comm 3665273d9f13SBarry Smith 3666273d9f13SBarry Smith Input Parameters: 3667273d9f13SBarry Smith + comm - MPI communicator 3668273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 3669273d9f13SBarry Smith This value should be the same as the local size used in creating the 3670273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 3671273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 3672273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 3673273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 3674273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 3675273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 3676273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3677273d9f13SBarry Smith (same value is used for all local rows) 3678273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3679273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3680273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3681273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 3682273d9f13SBarry Smith You must leave room for the diagonal entry even if it is zero. 3683273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3684273d9f13SBarry Smith submatrix (same value is used for all local rows). 3685273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3686273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3687273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3688273d9f13SBarry Smith structure. The size of this array is equal to the number 3689273d9f13SBarry Smith of local rows, i.e 'm'. 3690273d9f13SBarry Smith 3691273d9f13SBarry Smith Output Parameter: 3692273d9f13SBarry Smith . A - the matrix 3693273d9f13SBarry Smith 3694175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 3695ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 3696175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 3697175b88e8SBarry Smith 3698273d9f13SBarry Smith Notes: 369949a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 370049a6f317SBarry Smith 3701273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 3702273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 3703273d9f13SBarry Smith storage requirements for this matrix. 3704273d9f13SBarry Smith 3705273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 3706273d9f13SBarry Smith processor than it must be used on all processors that share the object for 3707273d9f13SBarry Smith that argument. 3708273d9f13SBarry Smith 3709273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 3710273d9f13SBarry Smith (possibly both). 3711273d9f13SBarry Smith 371233a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 371333a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 371433a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 371533a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 371633a7c187SSatish Balay values corresponding to [m x N] submatrix. 3717273d9f13SBarry Smith 371833a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 371933a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 372033a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 372133a7c187SSatish Balay 372233a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 372333a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 372433a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 372533a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 372633a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 372733a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 372833a7c187SSatish Balay illustrates this concept. 372933a7c187SSatish Balay 373033a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 373133a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 373233a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 373333a7c187SSatish Balay local matrix (a rectangular submatrix). 3734273d9f13SBarry Smith 3735273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3736273d9f13SBarry Smith 373797d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 373897d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 373997d05335SKris Buschelman type of communicator, use the construction mechanism: 374078102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 374197d05335SKris Buschelman 3742273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 3743273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 3744273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 3745273d9f13SBarry Smith 3746273d9f13SBarry Smith Options Database Keys: 3747923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 3748923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 3749273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 3750273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 3751273d9f13SBarry Smith the user still MUST index entries starting at 0! 3752273d9f13SBarry Smith 3753273d9f13SBarry Smith 3754273d9f13SBarry Smith Example usage: 3755273d9f13SBarry Smith 3756273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3757273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3758273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3759273d9f13SBarry Smith as follows: 3760273d9f13SBarry Smith 3761273d9f13SBarry Smith .vb 3762273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3763273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3764273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3765273d9f13SBarry Smith ------------------------------------- 3766273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3767273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3768273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3769273d9f13SBarry Smith ------------------------------------- 3770273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3771273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3772273d9f13SBarry Smith .ve 3773273d9f13SBarry Smith 3774273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3775273d9f13SBarry Smith 3776273d9f13SBarry Smith .vb 3777273d9f13SBarry Smith A B C 3778273d9f13SBarry Smith D E F 3779273d9f13SBarry Smith G H I 3780273d9f13SBarry Smith .ve 3781273d9f13SBarry Smith 3782273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3783273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3784273d9f13SBarry Smith 3785273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3786273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3787273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3788273d9f13SBarry Smith 3789273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3790273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3791273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3792273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3793273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3794273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3795273d9f13SBarry Smith 3796273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3797273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3798273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3799273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3800273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3801273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3802273d9f13SBarry Smith .vb 3803273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3804273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3805273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3806273d9f13SBarry Smith .ve 3807273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3808273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3809273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3810273d9f13SBarry Smith 34 values. 3811273d9f13SBarry Smith 3812273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3813273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3814273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3815273d9f13SBarry Smith .vb 3816273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3817273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3818273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3819273d9f13SBarry Smith .ve 3820273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3821273d9f13SBarry Smith hence pre-allocation is perfect. 3822273d9f13SBarry Smith 3823273d9f13SBarry Smith Level: intermediate 3824273d9f13SBarry Smith 3825273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3826273d9f13SBarry Smith 3827ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 38282fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 3829273d9f13SBarry Smith @*/ 3830be1d678aSKris 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) 3831273d9f13SBarry Smith { 38326849ba73SBarry Smith PetscErrorCode ierr; 3833b1d57f15SBarry Smith PetscMPIInt size; 3834273d9f13SBarry Smith 3835273d9f13SBarry Smith PetscFunctionBegin; 3836f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 3837f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 3838273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 3839273d9f13SBarry Smith if (size > 1) { 3840273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 3841273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 3842273d9f13SBarry Smith } else { 3843273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 3844273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 3845273d9f13SBarry Smith } 3846273d9f13SBarry Smith PetscFunctionReturn(0); 3847273d9f13SBarry Smith } 3848195d93cdSBarry Smith 38494a2ae208SSatish Balay #undef __FUNCT__ 38504a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 3851be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[]) 3852195d93cdSBarry Smith { 3853195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 3854b1d57f15SBarry Smith 3855195d93cdSBarry Smith PetscFunctionBegin; 3856195d93cdSBarry Smith *Ad = a->A; 3857195d93cdSBarry Smith *Ao = a->B; 3858195d93cdSBarry Smith *colmap = a->garray; 3859195d93cdSBarry Smith PetscFunctionReturn(0); 3860195d93cdSBarry Smith } 3861a2243be0SBarry Smith 3862a2243be0SBarry Smith #undef __FUNCT__ 3863a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 3864dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 3865a2243be0SBarry Smith { 3866dfbe8321SBarry Smith PetscErrorCode ierr; 3867b1d57f15SBarry Smith PetscInt i; 3868a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 3869a2243be0SBarry Smith 3870a2243be0SBarry Smith PetscFunctionBegin; 38718ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 387208b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 3873a2243be0SBarry Smith ISColoring ocoloring; 3874a2243be0SBarry Smith 3875a2243be0SBarry Smith /* set coloring for diagonal portion */ 3876a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 3877a2243be0SBarry Smith 3878a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 38797adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 3880d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3881d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 3882a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 3883a2243be0SBarry Smith } 3884a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 3885d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 3886a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 3887a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 3888a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 388908b6dcc0SBarry Smith ISColoringValue *colors; 3890b1d57f15SBarry Smith PetscInt *larray; 3891a2243be0SBarry Smith ISColoring ocoloring; 3892a2243be0SBarry Smith 3893a2243be0SBarry Smith /* set coloring for diagonal portion */ 3894d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 3895d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 3896d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 3897a2243be0SBarry Smith } 3898d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 3899d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3900d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 3901a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 3902a2243be0SBarry Smith } 3903a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 3904d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 3905a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 3906a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 3907a2243be0SBarry Smith 3908a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 3909d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 3910d0f46423SBarry Smith ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 3911d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 3912d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 3913a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 3914a2243be0SBarry Smith } 3915a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 3916d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 3917a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 3918a2243be0SBarry Smith ierr = ISColoringDestroy(ocoloring);CHKERRQ(ierr); 3919a2243be0SBarry Smith } else { 3920e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 3921a2243be0SBarry Smith } 3922a2243be0SBarry Smith 3923a2243be0SBarry Smith PetscFunctionReturn(0); 3924a2243be0SBarry Smith } 3925a2243be0SBarry Smith 3926dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC) 3927a2243be0SBarry Smith #undef __FUNCT__ 3928779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdic_MPIAIJ" 3929dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues) 3930a2243be0SBarry Smith { 3931a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 3932dfbe8321SBarry Smith PetscErrorCode ierr; 3933a2243be0SBarry Smith 3934a2243be0SBarry Smith PetscFunctionBegin; 3935779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->A,advalues);CHKERRQ(ierr); 3936779c1a83SBarry Smith ierr = MatSetValuesAdic_SeqAIJ(a->B,advalues);CHKERRQ(ierr); 3937779c1a83SBarry Smith PetscFunctionReturn(0); 3938779c1a83SBarry Smith } 3939dcf5cc72SBarry Smith #endif 3940779c1a83SBarry Smith 3941779c1a83SBarry Smith #undef __FUNCT__ 3942779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 3943b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 3944779c1a83SBarry Smith { 3945779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 3946dfbe8321SBarry Smith PetscErrorCode ierr; 3947779c1a83SBarry Smith 3948779c1a83SBarry Smith PetscFunctionBegin; 3949779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 3950779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 3951a2243be0SBarry Smith PetscFunctionReturn(0); 3952a2243be0SBarry Smith } 3953c5d6d63eSBarry Smith 3954c5d6d63eSBarry Smith #undef __FUNCT__ 395551dd7536SBarry Smith #define __FUNCT__ "MatMerge" 3956bc08b0f1SBarry Smith /*@ 395751dd7536SBarry Smith MatMerge - Creates a single large PETSc matrix by concatinating sequential 395851dd7536SBarry Smith matrices from each processor 3959c5d6d63eSBarry Smith 3960c5d6d63eSBarry Smith Collective on MPI_Comm 3961c5d6d63eSBarry Smith 3962c5d6d63eSBarry Smith Input Parameters: 396351dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 3964d6bb3c2dSHong Zhang . inmat - the input sequential matrices 39650e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 3966d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 396751dd7536SBarry Smith 396851dd7536SBarry Smith Output Parameter: 396951dd7536SBarry Smith . outmat - the parallel matrix generated 3970c5d6d63eSBarry Smith 39717e25d530SSatish Balay Level: advanced 39727e25d530SSatish Balay 3973f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 3974c5d6d63eSBarry Smith 3975c5d6d63eSBarry Smith @*/ 3976be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 3977c5d6d63eSBarry Smith { 3978dfbe8321SBarry Smith PetscErrorCode ierr; 3979b7940d39SSatish Balay PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz; 3980ba8c8a56SBarry Smith PetscInt *indx; 3981ba8c8a56SBarry Smith PetscScalar *values; 3982c5d6d63eSBarry Smith 3983c5d6d63eSBarry Smith PetscFunctionBegin; 39840e36024fSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 3985d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 3986d6bb3c2dSHong Zhang /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */ 39870e36024fSHong Zhang if (n == PETSC_DECIDE){ 3988357abbc8SBarry Smith ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 39890e36024fSHong Zhang } 3990357abbc8SBarry Smith ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 3991357abbc8SBarry Smith rstart -= m; 3992d6bb3c2dSHong Zhang 3993d6bb3c2dSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 3994d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 3995ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 3996d6bb3c2dSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 3997ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 3998d6bb3c2dSHong Zhang } 3999d6bb3c2dSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 4000f69a0ea3SMatthew Knepley ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 4001f69a0ea3SMatthew Knepley ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4002d6bb3c2dSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 4003d6bb3c2dSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 4004d6bb3c2dSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 4005d6bb3c2dSHong Zhang 4006d6bb3c2dSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 4007d6bb3c2dSHong Zhang ierr = MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 4008d6bb3c2dSHong Zhang } else { 4009e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 4010d6bb3c2dSHong Zhang } 4011d6bb3c2dSHong Zhang 4012d6bb3c2dSHong Zhang for (i=0;i<m;i++) { 4013ba8c8a56SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4014b7940d39SSatish Balay Ii = i + rstart; 4015b7940d39SSatish Balay ierr = MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4016ba8c8a56SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 4017d6bb3c2dSHong Zhang } 4018d6bb3c2dSHong Zhang ierr = MatDestroy(inmat);CHKERRQ(ierr); 4019d6bb3c2dSHong Zhang ierr = MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4020d6bb3c2dSHong Zhang ierr = MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 402151dd7536SBarry Smith 4022c5d6d63eSBarry Smith PetscFunctionReturn(0); 4023c5d6d63eSBarry Smith } 4024c5d6d63eSBarry Smith 4025c5d6d63eSBarry Smith #undef __FUNCT__ 4026c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4027dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4028c5d6d63eSBarry Smith { 4029dfbe8321SBarry Smith PetscErrorCode ierr; 403032dcc486SBarry Smith PetscMPIInt rank; 4031b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4032de4209c5SBarry Smith size_t len; 4033b1d57f15SBarry Smith const PetscInt *indx; 4034c5d6d63eSBarry Smith PetscViewer out; 4035c5d6d63eSBarry Smith char *name; 4036c5d6d63eSBarry Smith Mat B; 4037b3cc6726SBarry Smith const PetscScalar *values; 4038c5d6d63eSBarry Smith 4039c5d6d63eSBarry Smith PetscFunctionBegin; 4040c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4041c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4042f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4043f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4044f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4045f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4046f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4047c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4048c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4049c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4050c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4051c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4052c5d6d63eSBarry Smith } 4053c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4054c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4055c5d6d63eSBarry Smith 40567adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4057c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4058c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4059c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4060852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4061c5d6d63eSBarry Smith ierr = PetscFree(name); 4062c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 4063c5d6d63eSBarry Smith ierr = PetscViewerDestroy(out);CHKERRQ(ierr); 4064c5d6d63eSBarry Smith ierr = MatDestroy(B);CHKERRQ(ierr); 4065c5d6d63eSBarry Smith PetscFunctionReturn(0); 4066c5d6d63eSBarry Smith } 4067e5f2cdd8SHong Zhang 406851a7d1a8SHong Zhang EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat); 406951a7d1a8SHong Zhang #undef __FUNCT__ 407051a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 4071be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 407251a7d1a8SHong Zhang { 407351a7d1a8SHong Zhang PetscErrorCode ierr; 4074671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4075776b82aeSLisandro Dalcin PetscContainer container; 407651a7d1a8SHong Zhang 407751a7d1a8SHong Zhang PetscFunctionBegin; 4078671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4079671beff6SHong Zhang if (container) { 4080776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 408151a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 40823e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 40833e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 408451a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 408551a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4086533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 408702c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4088533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 408902c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 409005b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 409105b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 409205b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 409326283091SBarry Smith ierr = PetscLayoutDestroy(merge->rowmap);CHKERRQ(ierr); 4094671beff6SHong Zhang 4095776b82aeSLisandro Dalcin ierr = PetscContainerDestroy(container);CHKERRQ(ierr); 4096671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4097671beff6SHong Zhang } 409851a7d1a8SHong Zhang ierr = PetscFree(merge);CHKERRQ(ierr); 409951a7d1a8SHong Zhang 410051a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 410151a7d1a8SHong Zhang PetscFunctionReturn(0); 410251a7d1a8SHong Zhang } 410351a7d1a8SHong Zhang 41047c4f633dSBarry Smith #include "../src/mat/utils/freespace.h" 4105be0fcf8dSHong Zhang #include "petscbt.h" 41064ebed01fSBarry Smith 4107e5f2cdd8SHong Zhang #undef __FUNCT__ 410838f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPINumeric" 4109e5f2cdd8SHong Zhang /*@C 4110f08fae4eSHong Zhang MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential 4111e5f2cdd8SHong Zhang matrices from each processor 4112e5f2cdd8SHong Zhang 4113e5f2cdd8SHong Zhang Collective on MPI_Comm 4114e5f2cdd8SHong Zhang 4115e5f2cdd8SHong Zhang Input Parameters: 4116e5f2cdd8SHong Zhang + comm - the communicators the parallel matrix will live on 4117f08fae4eSHong Zhang . seqmat - the input sequential matrices 41180e36024fSHong Zhang . m - number of local rows (or PETSC_DECIDE) 41190e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4120e5f2cdd8SHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4121e5f2cdd8SHong Zhang 4122e5f2cdd8SHong Zhang Output Parameter: 4123f08fae4eSHong Zhang . mpimat - the parallel matrix generated 4124e5f2cdd8SHong Zhang 4125e5f2cdd8SHong Zhang Level: advanced 4126e5f2cdd8SHong Zhang 4127affca5deSHong Zhang Notes: 4128affca5deSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4129affca5deSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4130affca5deSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4131e5f2cdd8SHong Zhang @*/ 4132be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat) 413355d1abb9SHong Zhang { 413455d1abb9SHong Zhang PetscErrorCode ierr; 41357adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 413655d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4137b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4138d0f46423SBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj=a->j; 4139b1d57f15SBarry Smith PetscInt proc,m; 4140b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4141b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4142b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 414355d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 414455d1abb9SHong Zhang MPI_Status *status; 4145a77337e4SBarry Smith MatScalar *aa=a->a; 4146dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 414755d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4148776b82aeSLisandro Dalcin PetscContainer container; 414955d1abb9SHong Zhang 415055d1abb9SHong Zhang PetscFunctionBegin; 41514ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 41523c2c1871SHong Zhang 415355d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 415455d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 415555d1abb9SHong Zhang 415655d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 415755d1abb9SHong Zhang if (container) { 4158776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 415955d1abb9SHong Zhang } 416055d1abb9SHong Zhang bi = merge->bi; 416155d1abb9SHong Zhang bj = merge->bj; 416255d1abb9SHong Zhang buf_ri = merge->buf_ri; 416355d1abb9SHong Zhang buf_rj = merge->buf_rj; 416455d1abb9SHong Zhang 416555d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 41667a2fc3feSBarry Smith owners = merge->rowmap->range; 416755d1abb9SHong Zhang len_s = merge->len_s; 416855d1abb9SHong Zhang 416955d1abb9SHong Zhang /* send and recv matrix values */ 417055d1abb9SHong Zhang /*-----------------------------*/ 4171357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 417255d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 417355d1abb9SHong Zhang 417455d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 417555d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 417655d1abb9SHong Zhang if (!len_s[proc]) continue; 417755d1abb9SHong Zhang i = owners[proc]; 417855d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 417955d1abb9SHong Zhang k++; 418055d1abb9SHong Zhang } 418155d1abb9SHong Zhang 41820c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 41830c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 418455d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 418555d1abb9SHong Zhang 418655d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 418755d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 418855d1abb9SHong Zhang 418955d1abb9SHong Zhang /* insert mat values of mpimat */ 419055d1abb9SHong Zhang /*----------------------------*/ 4191a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 41920572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 419355d1abb9SHong Zhang 419455d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 419555d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 419655d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 419755d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 419855d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 419955d1abb9SHong Zhang } 420055d1abb9SHong Zhang 420155d1abb9SHong Zhang /* set values of ba */ 42027a2fc3feSBarry Smith m = merge->rowmap->n; 420355d1abb9SHong Zhang for (i=0; i<m; i++) { 420455d1abb9SHong Zhang arow = owners[rank] + i; 420555d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 420655d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4207a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 420855d1abb9SHong Zhang 420955d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 421055d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 421155d1abb9SHong Zhang aj = a->j + ai[arow]; 421255d1abb9SHong Zhang aa = a->a + ai[arow]; 421355d1abb9SHong Zhang nextaj = 0; 421455d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 421555d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 421655d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 421755d1abb9SHong Zhang } 421855d1abb9SHong Zhang } 421955d1abb9SHong Zhang 422055d1abb9SHong Zhang /* add received vals into ba */ 422155d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 422255d1abb9SHong Zhang /* i-th row */ 422355d1abb9SHong Zhang if (i == *nextrow[k]) { 422455d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 422555d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 422655d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 422755d1abb9SHong Zhang nextaj = 0; 422855d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 422955d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 423055d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 423155d1abb9SHong Zhang } 423255d1abb9SHong Zhang } 423355d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 423455d1abb9SHong Zhang } 423555d1abb9SHong Zhang } 423655d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 423755d1abb9SHong Zhang } 423855d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 423955d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 424055d1abb9SHong Zhang 4241533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 424255d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 424355d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 42441d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 42454ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 424655d1abb9SHong Zhang PetscFunctionReturn(0); 424755d1abb9SHong Zhang } 424838f152feSBarry Smith 424938f152feSBarry Smith #undef __FUNCT__ 425038f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPISymbolic" 4251be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4252e5f2cdd8SHong Zhang { 4253f08fae4eSHong Zhang PetscErrorCode ierr; 425455a3bba9SHong Zhang Mat B_mpi; 4255c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4256b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4257b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4258d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4259b1d57f15SBarry Smith PetscInt len,proc,*dnz,*onz; 4260b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4261b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 426255d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 426358cb9c82SHong Zhang MPI_Status *status; 4264a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4265be0fcf8dSHong Zhang PetscBT lnkbt; 426651a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4267776b82aeSLisandro Dalcin PetscContainer container; 426802c68681SHong Zhang 4269e5f2cdd8SHong Zhang PetscFunctionBegin; 42704ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 42713c2c1871SHong Zhang 427238f152feSBarry Smith /* make sure it is a PETSc comm */ 427338f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4274e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4275e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 427655d1abb9SHong Zhang 427751a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4278c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4279e5f2cdd8SHong Zhang 42806abd8857SHong Zhang /* determine row ownership */ 4281f08fae4eSHong Zhang /*---------------------------------------------------------*/ 428226283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 428326283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 428426283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 428526283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 428626283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4287b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4288b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 428955d1abb9SHong Zhang 42907a2fc3feSBarry Smith m = merge->rowmap->n; 42917a2fc3feSBarry Smith M = merge->rowmap->N; 42927a2fc3feSBarry Smith owners = merge->rowmap->range; 42936abd8857SHong Zhang 42946abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 42956abd8857SHong Zhang /*---------------------------------------------------------*/ 42963e06a4e6SHong Zhang len_s = merge->len_s; 429751a7d1a8SHong Zhang 42982257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4299c2234fe3SHong Zhang merge->nsend = 0; 4300409913e3SHong Zhang for (proc=0; proc<size; proc++){ 43012257cef7SHong Zhang len_si[proc] = 0; 43023e06a4e6SHong Zhang if (proc == rank){ 43036abd8857SHong Zhang len_s[proc] = 0; 43043e06a4e6SHong Zhang } else { 430502c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 43063e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 43073e06a4e6SHong Zhang } 43083e06a4e6SHong Zhang if (len_s[proc]) { 4309c2234fe3SHong Zhang merge->nsend++; 43102257cef7SHong Zhang nrows = 0; 43112257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 43122257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 43132257cef7SHong Zhang } 43142257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 43152257cef7SHong Zhang len += len_si[proc]; 4316409913e3SHong Zhang } 431758cb9c82SHong Zhang } 4318409913e3SHong Zhang 43192257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 43202257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 432151a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 432255d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4323671beff6SHong Zhang 43243e06a4e6SHong Zhang /* post the Irecv of j-structure */ 43253e06a4e6SHong Zhang /*-------------------------------*/ 43262c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 43273e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 432802c68681SHong Zhang 43293e06a4e6SHong Zhang /* post the Isend of j-structure */ 4330affca5deSHong Zhang /*--------------------------------*/ 43311d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 43323e06a4e6SHong Zhang 43332257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4334409913e3SHong Zhang if (!len_s[proc]) continue; 433502c68681SHong Zhang i = owners[proc]; 4336b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 433751a7d1a8SHong Zhang k++; 433851a7d1a8SHong Zhang } 433951a7d1a8SHong Zhang 43403e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 43413e06a4e6SHong Zhang /*------------------------------------------------*/ 43420c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 43430c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 434402c68681SHong Zhang 434502c68681SHong Zhang /* send and recv i-structure */ 434602c68681SHong Zhang /*---------------------------*/ 43472c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 434802c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 434902c68681SHong Zhang 4350b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 43513e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 43522257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 435302c68681SHong Zhang if (!len_s[proc]) continue; 43543e06a4e6SHong Zhang /* form outgoing message for i-structure: 43553e06a4e6SHong Zhang buf_si[0]: nrows to be sent 43563e06a4e6SHong Zhang [1:nrows]: row index (global) 43573e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 43583e06a4e6SHong Zhang */ 43593e06a4e6SHong Zhang /*-------------------------------------------*/ 43602257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 43613e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 43623e06a4e6SHong Zhang buf_si[0] = nrows; 43633e06a4e6SHong Zhang buf_si_i[0] = 0; 43643e06a4e6SHong Zhang nrows = 0; 43653e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 43663e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 43673e06a4e6SHong Zhang if (anzi) { 43683e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 43693e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 43703e06a4e6SHong Zhang nrows++; 43713e06a4e6SHong Zhang } 43723e06a4e6SHong Zhang } 4373b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 437402c68681SHong Zhang k++; 43752257cef7SHong Zhang buf_si += len_si[proc]; 437602c68681SHong Zhang } 43772257cef7SHong Zhang 43780c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 43790c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 438002c68681SHong Zhang 4381ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 43823e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4383ae15b995SBarry 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); 43843e06a4e6SHong Zhang } 43853e06a4e6SHong Zhang 43863e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 438702c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 438802c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 43891d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 43902257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 43913e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4392bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 439358cb9c82SHong Zhang 4394bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4395bcc1bcd5SHong Zhang /*----------------------------------------------*/ 439658cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4397b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 439858cb9c82SHong Zhang bi[0] = 0; 439958cb9c82SHong Zhang 4400be0fcf8dSHong Zhang /* create and initialize a linked list */ 4401be0fcf8dSHong Zhang nlnk = N+1; 4402be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 440358cb9c82SHong Zhang 4404bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 440558cb9c82SHong Zhang len = 0; 4406bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4407a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 440858cb9c82SHong Zhang current_space = free_space; 440958cb9c82SHong Zhang 4410bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 44110572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 44121d79065fSBarry Smith 44133e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 44142257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 44153e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 44163e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 44172257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 44183e06a4e6SHong Zhang } 44192257cef7SHong Zhang 4420bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4421bcc1bcd5SHong Zhang len = 0; 442258cb9c82SHong Zhang for (i=0;i<m;i++) { 442358cb9c82SHong Zhang bnzi = 0; 442458cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 442558cb9c82SHong Zhang arow = owners[rank] + i; 442658cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 442758cb9c82SHong Zhang aj = a->j + ai[arow]; 4428be0fcf8dSHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 442958cb9c82SHong Zhang bnzi += nlnk; 443058cb9c82SHong Zhang /* add received col data into lnk */ 443151a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 443255d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 44333e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 44343e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 44353e06a4e6SHong Zhang ierr = PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 44363e06a4e6SHong Zhang bnzi += nlnk; 44373e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 44383e06a4e6SHong Zhang } 443958cb9c82SHong Zhang } 4440bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 444158cb9c82SHong Zhang 444258cb9c82SHong Zhang /* if free space is not available, make more free space */ 444358cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 44444238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 444558cb9c82SHong Zhang nspacedouble++; 444658cb9c82SHong Zhang } 444758cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4448be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4449bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4450bcc1bcd5SHong Zhang 445158cb9c82SHong Zhang current_space->array += bnzi; 445258cb9c82SHong Zhang current_space->local_used += bnzi; 445358cb9c82SHong Zhang current_space->local_remaining -= bnzi; 445458cb9c82SHong Zhang 445558cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 445658cb9c82SHong Zhang } 4457bcc1bcd5SHong Zhang 44581d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4459bcc1bcd5SHong Zhang 4460b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4461a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4462be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4463409913e3SHong Zhang 4464bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4465bcc1bcd5SHong Zhang /*---------------------------------------*/ 4466f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 446754b84b50SHong Zhang if (n==PETSC_DECIDE) { 4468f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 446954b84b50SHong Zhang } else { 4470f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 447154b84b50SHong Zhang } 4472bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4473bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4474bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 447558cb9c82SHong Zhang 44766abd8857SHong Zhang /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */ 44776abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4478affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4479affca5deSHong Zhang merge->bi = bi; 4480affca5deSHong Zhang merge->bj = bj; 448102c68681SHong Zhang merge->buf_ri = buf_ri; 448202c68681SHong Zhang merge->buf_rj = buf_rj; 4483de0260b3SHong Zhang merge->coi = PETSC_NULL; 4484de0260b3SHong Zhang merge->coj = PETSC_NULL; 4485de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4486affca5deSHong Zhang 4487affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4488776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4489776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4490affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4491affca5deSHong Zhang *mpimat = B_mpi; 449238f152feSBarry Smith 449338f152feSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 44944ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4495e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4496e5f2cdd8SHong Zhang } 449725616d81SHong Zhang 449838f152feSBarry Smith #undef __FUNCT__ 449938f152feSBarry Smith #define __FUNCT__ "MatMerge_SeqsToMPI" 4500be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 450155d1abb9SHong Zhang { 450255d1abb9SHong Zhang PetscErrorCode ierr; 450355d1abb9SHong Zhang 450455d1abb9SHong Zhang PetscFunctionBegin; 45054ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 450655d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 450755d1abb9SHong Zhang ierr = MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 450855d1abb9SHong Zhang } 450955d1abb9SHong Zhang ierr = MatMerge_SeqsToMPINumeric(seqmat,*mpimat);CHKERRQ(ierr); 45104ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 451155d1abb9SHong Zhang PetscFunctionReturn(0); 451255d1abb9SHong Zhang } 45134ebed01fSBarry Smith 451425616d81SHong Zhang #undef __FUNCT__ 451525616d81SHong Zhang #define __FUNCT__ "MatGetLocalMat" 4516bc08b0f1SBarry Smith /*@ 451732fba14fSHong Zhang MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows 451825616d81SHong Zhang 451932fba14fSHong Zhang Not Collective 452025616d81SHong Zhang 452125616d81SHong Zhang Input Parameters: 452225616d81SHong Zhang + A - the matrix 452325616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 452425616d81SHong Zhang 452525616d81SHong Zhang Output Parameter: 452625616d81SHong Zhang . A_loc - the local sequential matrix generated 452725616d81SHong Zhang 452825616d81SHong Zhang Level: developer 452925616d81SHong Zhang 453025616d81SHong Zhang @*/ 4531be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 453225616d81SHong Zhang { 453325616d81SHong Zhang PetscErrorCode ierr; 453401b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 453501b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 453601b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 4537a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 4538a77337e4SBarry Smith PetscScalar *ca; 4539d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 45405a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 454125616d81SHong Zhang 454225616d81SHong Zhang PetscFunctionBegin; 45434ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 454401b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4545dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 4546dea91ad1SHong Zhang ci[0] = 0; 454701b7ae99SHong Zhang for (i=0; i<am; i++){ 4548dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 454901b7ae99SHong Zhang } 4550dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 4551dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 4552dea91ad1SHong Zhang k = 0; 455301b7ae99SHong Zhang for (i=0; i<am; i++) { 45545a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 45555a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 455601b7ae99SHong Zhang /* off-diagonal portion of A */ 45575a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 45585a7d977cSHong Zhang col = cmap[*bj]; 45595a7d977cSHong Zhang if (col >= cstart) break; 45605a7d977cSHong Zhang cj[k] = col; bj++; 45615a7d977cSHong Zhang ca[k++] = *ba++; 45625a7d977cSHong Zhang } 45635a7d977cSHong Zhang /* diagonal portion of A */ 45645a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 45655a7d977cSHong Zhang cj[k] = cstart + *aj++; 45665a7d977cSHong Zhang ca[k++] = *aa++; 45675a7d977cSHong Zhang } 45685a7d977cSHong Zhang /* off-diagonal portion of A */ 45695a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 45705a7d977cSHong Zhang cj[k] = cmap[*bj++]; 45715a7d977cSHong Zhang ca[k++] = *ba++; 45725a7d977cSHong Zhang } 457325616d81SHong Zhang } 4574dea91ad1SHong Zhang /* put together the new matrix */ 4575d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 4576dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4577dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 4578dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 4579e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 4580e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 4581dea91ad1SHong Zhang mat->nonew = 0; 45825a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 45835a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 4584a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 45855a7d977cSHong Zhang for (i=0; i<am; i++) { 45865a7d977cSHong Zhang /* off-diagonal portion of A */ 45875a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 45885a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 45895a7d977cSHong Zhang col = cmap[*bj]; 45905a7d977cSHong Zhang if (col >= cstart) break; 4591a77337e4SBarry Smith *cam++ = *ba++; bj++; 45925a7d977cSHong Zhang } 45935a7d977cSHong Zhang /* diagonal portion of A */ 4594ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 4595a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 45965a7d977cSHong Zhang /* off-diagonal portion of A */ 4597f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 4598a77337e4SBarry Smith *cam++ = *ba++; bj++; 4599f33d1a9aSHong Zhang } 46005a7d977cSHong Zhang } 46015a7d977cSHong Zhang } else { 4602e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 460325616d81SHong Zhang } 460401b7ae99SHong Zhang 46054ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 460625616d81SHong Zhang PetscFunctionReturn(0); 460725616d81SHong Zhang } 460825616d81SHong Zhang 460932fba14fSHong Zhang #undef __FUNCT__ 461032fba14fSHong Zhang #define __FUNCT__ "MatGetLocalMatCondensed" 461132fba14fSHong Zhang /*@C 461232fba14fSHong Zhang MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns 461332fba14fSHong Zhang 461432fba14fSHong Zhang Not Collective 461532fba14fSHong Zhang 461632fba14fSHong Zhang Input Parameters: 461732fba14fSHong Zhang + A - the matrix 461832fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 461932fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 462032fba14fSHong Zhang 462132fba14fSHong Zhang Output Parameter: 462232fba14fSHong Zhang . A_loc - the local sequential matrix generated 462332fba14fSHong Zhang 462432fba14fSHong Zhang Level: developer 462532fba14fSHong Zhang 462632fba14fSHong Zhang @*/ 4627be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 462832fba14fSHong Zhang { 462932fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 463032fba14fSHong Zhang PetscErrorCode ierr; 463132fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 463232fba14fSHong Zhang IS isrowa,iscola; 463332fba14fSHong Zhang Mat *aloc; 463432fba14fSHong Zhang 463532fba14fSHong Zhang PetscFunctionBegin; 46364ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 463732fba14fSHong Zhang if (!row){ 4638d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 463932fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 464032fba14fSHong Zhang } else { 464132fba14fSHong Zhang isrowa = *row; 464232fba14fSHong Zhang } 464332fba14fSHong Zhang if (!col){ 4644d0f46423SBarry Smith start = A->cmap->rstart; 464532fba14fSHong Zhang cmap = a->garray; 4646d0f46423SBarry Smith nzA = a->A->cmap->n; 4647d0f46423SBarry Smith nzB = a->B->cmap->n; 464832fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 464932fba14fSHong Zhang ncols = 0; 465032fba14fSHong Zhang for (i=0; i<nzB; i++) { 465132fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 465232fba14fSHong Zhang else break; 465332fba14fSHong Zhang } 465432fba14fSHong Zhang imark = i; 465532fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 465632fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 4657d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 465832fba14fSHong Zhang } else { 465932fba14fSHong Zhang iscola = *col; 466032fba14fSHong Zhang } 466132fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 466232fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 466332fba14fSHong Zhang aloc[0] = *A_loc; 466432fba14fSHong Zhang } 466532fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 466632fba14fSHong Zhang *A_loc = aloc[0]; 466732fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 466832fba14fSHong Zhang if (!row){ 466932fba14fSHong Zhang ierr = ISDestroy(isrowa);CHKERRQ(ierr); 467032fba14fSHong Zhang } 467132fba14fSHong Zhang if (!col){ 467232fba14fSHong Zhang ierr = ISDestroy(iscola);CHKERRQ(ierr); 467332fba14fSHong Zhang } 46744ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 467532fba14fSHong Zhang PetscFunctionReturn(0); 467632fba14fSHong Zhang } 467732fba14fSHong Zhang 467825616d81SHong Zhang #undef __FUNCT__ 467925616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 468025616d81SHong Zhang /*@C 468132fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 468225616d81SHong Zhang 468325616d81SHong Zhang Collective on Mat 468425616d81SHong Zhang 468525616d81SHong Zhang Input Parameters: 4686e240928fSHong Zhang + A,B - the matrices in mpiaij format 468725616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 468825616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 468925616d81SHong Zhang 469025616d81SHong Zhang Output Parameter: 469125616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 4692d0f46423SBarry Smith . brstart - row index of B_seq from which next B->rmap->n rows are taken from B's local rows 469325616d81SHong Zhang - B_seq - the sequential matrix generated 469425616d81SHong Zhang 469525616d81SHong Zhang Level: developer 469625616d81SHong Zhang 469725616d81SHong Zhang @*/ 4698be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq) 469925616d81SHong Zhang { 4700899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 470125616d81SHong Zhang PetscErrorCode ierr; 4702b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 470325616d81SHong Zhang IS isrowb,iscolb; 470425616d81SHong Zhang Mat *bseq; 470525616d81SHong Zhang 470625616d81SHong Zhang PetscFunctionBegin; 4707d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4708e32f2f54SBarry 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); 470925616d81SHong Zhang } 47104ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 471125616d81SHong Zhang 471225616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4713d0f46423SBarry Smith start = A->cmap->rstart; 471425616d81SHong Zhang cmap = a->garray; 4715d0f46423SBarry Smith nzA = a->A->cmap->n; 4716d0f46423SBarry Smith nzB = a->B->cmap->n; 4717b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 471825616d81SHong Zhang ncols = 0; 47190390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 472025616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 472125616d81SHong Zhang else break; 472225616d81SHong Zhang } 472325616d81SHong Zhang imark = i; 47240390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 47250390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 4726d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 472725616d81SHong Zhang *brstart = imark; 4728d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 472925616d81SHong Zhang } else { 4730e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 473125616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 473225616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 473325616d81SHong Zhang bseq[0] = *B_seq; 473425616d81SHong Zhang } 473525616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 473625616d81SHong Zhang *B_seq = bseq[0]; 473725616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 473825616d81SHong Zhang if (!rowb){ 473925616d81SHong Zhang ierr = ISDestroy(isrowb);CHKERRQ(ierr); 474025616d81SHong Zhang } else { 474125616d81SHong Zhang *rowb = isrowb; 474225616d81SHong Zhang } 474325616d81SHong Zhang if (!colb){ 474425616d81SHong Zhang ierr = ISDestroy(iscolb);CHKERRQ(ierr); 474525616d81SHong Zhang } else { 474625616d81SHong Zhang *colb = iscolb; 474725616d81SHong Zhang } 47484ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 474925616d81SHong Zhang PetscFunctionReturn(0); 475025616d81SHong Zhang } 4751429d309bSHong Zhang 4752a61c8c0fSHong Zhang #undef __FUNCT__ 4753a61c8c0fSHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols" 4754429d309bSHong Zhang /*@C 4755429d309bSHong Zhang MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 475601b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 4757429d309bSHong Zhang 4758429d309bSHong Zhang Collective on Mat 4759429d309bSHong Zhang 4760429d309bSHong Zhang Input Parameters: 4761429d309bSHong Zhang + A,B - the matrices in mpiaij format 476287025532SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 476387025532SHong Zhang . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 47641d79065fSBarry Smith . startsj_r - similar to startsj for receives 476587025532SHong Zhang - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 4766429d309bSHong Zhang 4767429d309bSHong Zhang Output Parameter: 476887025532SHong Zhang + B_oth - the sequential matrix generated 4769429d309bSHong Zhang 4770429d309bSHong Zhang Level: developer 4771429d309bSHong Zhang 4772429d309bSHong Zhang @*/ 47731d79065fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 4774429d309bSHong Zhang { 4775a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 4776429d309bSHong Zhang PetscErrorCode ierr; 4777899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 477887025532SHong Zhang Mat_SeqAIJ *b_oth; 4779a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 47807adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 47817adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 4782d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 4783dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 4784dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 4785e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 4786910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 478787025532SHong Zhang MPI_Status *sstatus,rstatus; 4788aa5bb8c0SSatish Balay PetscMPIInt jj; 4789e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 4790ba8c8a56SBarry Smith PetscScalar *vals; 4791429d309bSHong Zhang 4792429d309bSHong Zhang PetscFunctionBegin; 4793d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 4794e32f2f54SBarry 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); 4795429d309bSHong Zhang } 47964ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4797a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 4798a6b2eed2SHong Zhang 4799a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 4800a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 4801e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 4802e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 4803a6b2eed2SHong Zhang nrecvs = gen_from->n; 4804a6b2eed2SHong Zhang nsends = gen_to->n; 4805d7ee0231SBarry Smith 4806d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 4807a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 4808a6b2eed2SHong Zhang sstarts = gen_to->starts; 4809a6b2eed2SHong Zhang sprocs = gen_to->procs; 4810a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 4811e42f35eeSHong Zhang sbs = gen_to->bs; 4812e42f35eeSHong Zhang rstarts = gen_from->starts; 4813e42f35eeSHong Zhang rprocs = gen_from->procs; 4814e42f35eeSHong Zhang rbs = gen_from->bs; 4815429d309bSHong Zhang 4816dea91ad1SHong Zhang if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 4817429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4818a6b2eed2SHong Zhang /* i-array */ 4819a6b2eed2SHong Zhang /*---------*/ 4820a6b2eed2SHong Zhang /* post receives */ 4821a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 4822e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 4823e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 482487025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 4825429d309bSHong Zhang } 4826a6b2eed2SHong Zhang 4827a6b2eed2SHong Zhang /* pack the outgoing message */ 48281d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 4829a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 4830a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 4831a6b2eed2SHong Zhang k = 0; 4832a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 4833e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 4834e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 483587025532SHong Zhang for (j=0; j<nrows; j++) { 4836d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 4837e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 4838e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 4839e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 4840e42f35eeSHong Zhang len += ncols; 4841e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 4842e42f35eeSHong Zhang } 4843a6b2eed2SHong Zhang k++; 4844429d309bSHong Zhang } 4845e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 4846dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 4847429d309bSHong Zhang } 484887025532SHong Zhang /* recvs and sends of i-array are completed */ 484987025532SHong Zhang i = nrecvs; 485087025532SHong Zhang while (i--) { 4851aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 485287025532SHong Zhang } 48530c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 4854e42f35eeSHong Zhang 4855a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 4856a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 4857a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 4858a6b2eed2SHong Zhang 485987025532SHong Zhang /* create i-array of B_oth */ 486087025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 486187025532SHong Zhang b_othi[0] = 0; 4862a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 4863a6b2eed2SHong Zhang k = 0; 4864a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 4865fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 4866e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 486787025532SHong Zhang for (j=0; j<nrows; j++) { 486887025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 4869a6b2eed2SHong Zhang len += rowlen[j]; k++; 4870a6b2eed2SHong Zhang } 4871dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 4872a6b2eed2SHong Zhang } 4873a6b2eed2SHong Zhang 487487025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 487587025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 4876dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 4877a6b2eed2SHong Zhang 487887025532SHong Zhang /* j-array */ 487987025532SHong Zhang /*---------*/ 4880a6b2eed2SHong Zhang /* post receives of j-array */ 4881a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 488287025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 488387025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 4884a6b2eed2SHong Zhang } 4885e42f35eeSHong Zhang 4886e42f35eeSHong Zhang /* pack the outgoing message j-array */ 4887a6b2eed2SHong Zhang k = 0; 4888a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 4889e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 4890a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 489187025532SHong Zhang for (j=0; j<nrows; j++) { 4892d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 4893e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 4894e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 4895a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 4896a6b2eed2SHong Zhang *bufJ++ = cols[l]; 489787025532SHong Zhang } 4898e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 4899e42f35eeSHong Zhang } 490087025532SHong Zhang } 490187025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 490287025532SHong Zhang } 490387025532SHong Zhang 490487025532SHong Zhang /* recvs and sends of j-array are completed */ 490587025532SHong Zhang i = nrecvs; 490687025532SHong Zhang while (i--) { 4907aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 490887025532SHong Zhang } 49090c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 491087025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 491187025532SHong Zhang sstartsj = *startsj; 49121d79065fSBarry Smith rstartsj = *startsj_r; 491387025532SHong Zhang bufa = *bufa_ptr; 491487025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 491587025532SHong Zhang b_otha = b_oth->a; 491687025532SHong Zhang } else { 4917e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 491887025532SHong Zhang } 491987025532SHong Zhang 492087025532SHong Zhang /* a-array */ 492187025532SHong Zhang /*---------*/ 492287025532SHong Zhang /* post receives of a-array */ 492387025532SHong Zhang for (i=0; i<nrecvs; i++){ 492487025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 492587025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 492687025532SHong Zhang } 4927e42f35eeSHong Zhang 4928e42f35eeSHong Zhang /* pack the outgoing message a-array */ 492987025532SHong Zhang k = 0; 493087025532SHong Zhang for (i=0; i<nsends; i++){ 4931e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 493287025532SHong Zhang bufA = bufa+sstartsj[i]; 493387025532SHong Zhang for (j=0; j<nrows; j++) { 4934d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 4935e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 4936e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 493787025532SHong Zhang for (l=0; l<ncols; l++){ 4938a6b2eed2SHong Zhang *bufA++ = vals[l]; 4939a6b2eed2SHong Zhang } 4940e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 4941e42f35eeSHong Zhang } 4942a6b2eed2SHong Zhang } 494387025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 4944a6b2eed2SHong Zhang } 494587025532SHong Zhang /* recvs and sends of a-array are completed */ 494687025532SHong Zhang i = nrecvs; 494787025532SHong Zhang while (i--) { 4948aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 494987025532SHong Zhang } 49500c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 4951d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 4952a6b2eed2SHong Zhang 495387025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 4954a6b2eed2SHong Zhang /* put together the new matrix */ 4955d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 4956a6b2eed2SHong Zhang 4957a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 4958a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 495987025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 4960e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 4961e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 496287025532SHong Zhang b_oth->nonew = 0; 4963a6b2eed2SHong Zhang 4964a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 4965dea91ad1SHong Zhang if (!startsj || !bufa_ptr){ 49661d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 4967dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 4968dea91ad1SHong Zhang } else { 496987025532SHong Zhang *startsj = sstartsj; 49701d79065fSBarry Smith *startsj_r = rstartsj; 497187025532SHong Zhang *bufa_ptr = bufa; 497287025532SHong Zhang } 4973dea91ad1SHong Zhang } 49744ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 4975429d309bSHong Zhang PetscFunctionReturn(0); 4976429d309bSHong Zhang } 4977ccd8e176SBarry Smith 497843eb5e2fSMatthew Knepley #undef __FUNCT__ 497943eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 498043eb5e2fSMatthew Knepley /*@C 498143eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 498243eb5e2fSMatthew Knepley 498343eb5e2fSMatthew Knepley Not Collective 498443eb5e2fSMatthew Knepley 498543eb5e2fSMatthew Knepley Input Parameters: 498643eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 498743eb5e2fSMatthew Knepley 498843eb5e2fSMatthew Knepley Output Parameter: 498943eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 499043eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 499143eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 499243eb5e2fSMatthew Knepley 499343eb5e2fSMatthew Knepley Level: developer 499443eb5e2fSMatthew Knepley 499543eb5e2fSMatthew Knepley @*/ 499643eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 499743eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 499843eb5e2fSMatthew Knepley #else 499943eb5e2fSMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 500043eb5e2fSMatthew Knepley #endif 500143eb5e2fSMatthew Knepley { 500243eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 500343eb5e2fSMatthew Knepley 500443eb5e2fSMatthew Knepley PetscFunctionBegin; 50050700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5006e414b56bSJed Brown PetscValidPointer(lvec, 2); 5007e414b56bSJed Brown PetscValidPointer(colmap, 3); 5008e414b56bSJed Brown PetscValidPointer(multScatter, 4); 500943eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 501043eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 501143eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 501243eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 501343eb5e2fSMatthew Knepley PetscFunctionReturn(0); 501443eb5e2fSMatthew Knepley } 501543eb5e2fSMatthew Knepley 501617667f90SBarry Smith EXTERN_C_BEGIN 50175a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJCRL(Mat,const MatType,MatReuse,Mat*); 50185a11e1b2SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPIAIJPERM(Mat,const MatType,MatReuse,Mat*); 5019c4688eafSJed Brown extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_MPIAIJ_MPISBAIJ(Mat,const MatType,MatReuse,Mat*); 502017667f90SBarry Smith EXTERN_C_END 502117667f90SBarry Smith 5022fc4dec0aSBarry Smith #undef __FUNCT__ 5023fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5024fc4dec0aSBarry Smith /* 5025fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5026fc4dec0aSBarry Smith 5027fc4dec0aSBarry Smith n p p 5028fc4dec0aSBarry Smith ( ) ( ) ( ) 5029fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5030fc4dec0aSBarry Smith ( ) ( ) ( ) 5031fc4dec0aSBarry Smith 5032fc4dec0aSBarry Smith */ 5033fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5034fc4dec0aSBarry Smith { 5035fc4dec0aSBarry Smith PetscErrorCode ierr; 5036fc4dec0aSBarry Smith Mat At,Bt,Ct; 5037fc4dec0aSBarry Smith 5038fc4dec0aSBarry Smith PetscFunctionBegin; 5039fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5040fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5041fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 5042fc4dec0aSBarry Smith ierr = MatDestroy(At);CHKERRQ(ierr); 5043fc4dec0aSBarry Smith ierr = MatDestroy(Bt);CHKERRQ(ierr); 5044fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 5045e5e4356aSBarry Smith ierr = MatDestroy(Ct);CHKERRQ(ierr); 5046fc4dec0aSBarry Smith PetscFunctionReturn(0); 5047fc4dec0aSBarry Smith } 5048fc4dec0aSBarry Smith 5049fc4dec0aSBarry Smith #undef __FUNCT__ 5050fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5051fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5052fc4dec0aSBarry Smith { 5053fc4dec0aSBarry Smith PetscErrorCode ierr; 5054d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5055fc4dec0aSBarry Smith Mat Cmat; 5056fc4dec0aSBarry Smith 5057fc4dec0aSBarry Smith PetscFunctionBegin; 5058e32f2f54SBarry 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); 505939804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5060fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5061fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5062fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 506338556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 506438556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5065fc4dec0aSBarry Smith *C = Cmat; 5066fc4dec0aSBarry Smith PetscFunctionReturn(0); 5067fc4dec0aSBarry Smith } 5068fc4dec0aSBarry Smith 5069fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5070fc4dec0aSBarry Smith #undef __FUNCT__ 5071fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5072fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5073fc4dec0aSBarry Smith { 5074fc4dec0aSBarry Smith PetscErrorCode ierr; 5075fc4dec0aSBarry Smith 5076fc4dec0aSBarry Smith PetscFunctionBegin; 5077fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5078fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5079fc4dec0aSBarry Smith } 5080fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5081fc4dec0aSBarry Smith PetscFunctionReturn(0); 5082fc4dec0aSBarry Smith } 5083fc4dec0aSBarry Smith 50845c9eb25fSBarry Smith EXTERN_C_BEGIN 5085611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5086bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5087611f576cSBarry Smith #endif 50883bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 50893bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 50903bf14a46SMatthew Knepley #endif 5091611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 50925c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5093611f576cSBarry Smith #endif 5094611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 50955c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_spooles(Mat,MatFactorType,Mat*); 5096611f576cSBarry Smith #endif 50975c9eb25fSBarry Smith EXTERN_C_END 50985c9eb25fSBarry Smith 5099ccd8e176SBarry Smith /*MC 5100ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5101ccd8e176SBarry Smith 5102ccd8e176SBarry Smith Options Database Keys: 5103ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5104ccd8e176SBarry Smith 5105ccd8e176SBarry Smith Level: beginner 5106ccd8e176SBarry Smith 5107175b88e8SBarry Smith .seealso: MatCreateMPIAIJ() 5108ccd8e176SBarry Smith M*/ 5109ccd8e176SBarry Smith 5110ccd8e176SBarry Smith EXTERN_C_BEGIN 5111ccd8e176SBarry Smith #undef __FUNCT__ 5112ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 5113be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAIJ(Mat B) 5114ccd8e176SBarry Smith { 5115ccd8e176SBarry Smith Mat_MPIAIJ *b; 5116ccd8e176SBarry Smith PetscErrorCode ierr; 5117ccd8e176SBarry Smith PetscMPIInt size; 5118ccd8e176SBarry Smith 5119ccd8e176SBarry Smith PetscFunctionBegin; 51207adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 5121ccd8e176SBarry Smith 512238f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5123ccd8e176SBarry Smith B->data = (void*)b; 5124ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5125d0f46423SBarry Smith B->rmap->bs = 1; 5126ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5127ccd8e176SBarry Smith B->mapping = 0; 5128ccd8e176SBarry Smith 5129ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5130ccd8e176SBarry Smith b->size = size; 51317adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5132ccd8e176SBarry Smith 5133ccd8e176SBarry Smith /* build cache for off array entries formed */ 51347adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5135ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5136ccd8e176SBarry Smith b->colmap = 0; 5137ccd8e176SBarry Smith b->garray = 0; 5138ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5139ccd8e176SBarry Smith 5140ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5141ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5142ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5143ccd8e176SBarry Smith 5144ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5145ccd8e176SBarry Smith b->rowindices = 0; 5146ccd8e176SBarry Smith b->rowvalues = 0; 5147ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5148ccd8e176SBarry Smith 5149611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES) 5150ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C", 51515c9eb25fSBarry Smith "MatGetFactor_mpiaij_spooles", 51525c9eb25fSBarry Smith MatGetFactor_mpiaij_spooles);CHKERRQ(ierr); 5153611f576cSBarry Smith #endif 5154611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5155ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5156bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5157bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5158611f576cSBarry Smith #endif 51593bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5160ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 51613bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 51623bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 51633bf14a46SMatthew Knepley #endif 5164611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5165ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 51665c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 51675c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5168611f576cSBarry Smith #endif 5169ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5170ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5171ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5172ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5173ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5174ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5175ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5176ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5177ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5178ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5179ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5180ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5181ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5182ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5183ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5184ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5185ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5186ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5187ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5188ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5189ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 51905a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C", 51915a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJPERM", 51925a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 51935a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C", 51945a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJCRL", 51955a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5196471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5197471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5198471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5199fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5200fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5201fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5202fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5203fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5204fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5205fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5206fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5207fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 520817667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5209ccd8e176SBarry Smith PetscFunctionReturn(0); 5210ccd8e176SBarry Smith } 5211ccd8e176SBarry Smith EXTERN_C_END 521281824310SBarry Smith 521303bfb495SBarry Smith #undef __FUNCT__ 521403bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 521558d36128SBarry Smith /*@ 521603bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 521703bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 521803bfb495SBarry Smith 521903bfb495SBarry Smith Collective on MPI_Comm 522003bfb495SBarry Smith 522103bfb495SBarry Smith Input Parameters: 522203bfb495SBarry Smith + comm - MPI communicator 522303bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 522403bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 522503bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 522603bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 522703bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 522803bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 522903bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 523003bfb495SBarry Smith . j - column indices 523103bfb495SBarry Smith . a - matrix values 523203bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 523303bfb495SBarry Smith . oj - column indices 523403bfb495SBarry Smith - oa - matrix values 523503bfb495SBarry Smith 523603bfb495SBarry Smith Output Parameter: 523703bfb495SBarry Smith . mat - the matrix 523803bfb495SBarry Smith 523903bfb495SBarry Smith Level: advanced 524003bfb495SBarry Smith 524103bfb495SBarry Smith Notes: 524203bfb495SBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. 524303bfb495SBarry Smith 524403bfb495SBarry Smith The i and j indices are 0 based 524503bfb495SBarry Smith 524603bfb495SBarry Smith See MatCreateMPIAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 524703bfb495SBarry Smith 52487b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 52497b55108eSBarry Smith 52507b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 525103bfb495SBarry Smith 525203bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 525303bfb495SBarry Smith 525403bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 52558d7a6e47SBarry Smith MPIAIJ, MatCreateMPIAIJ(), MatCreateMPIAIJWithArrays() 525603bfb495SBarry Smith @*/ 52578d7a6e47SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 525803bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 525903bfb495SBarry Smith { 526003bfb495SBarry Smith PetscErrorCode ierr; 526103bfb495SBarry Smith Mat_MPIAIJ *maij; 526203bfb495SBarry Smith 526303bfb495SBarry Smith PetscFunctionBegin; 5264e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 526503bfb495SBarry Smith if (i[0]) { 5266e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 526703bfb495SBarry Smith } 526803bfb495SBarry Smith if (oi[0]) { 5269e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 527003bfb495SBarry Smith } 527103bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 527203bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 527303bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 527403bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 52758d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 52768d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 527703bfb495SBarry Smith 527826283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->rmap,1);CHKERRQ(ierr); 527926283091SBarry Smith ierr = PetscLayoutSetBlockSize((*mat)->cmap,1);CHKERRQ(ierr); 528026283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 528126283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 528203bfb495SBarry Smith 528303bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5284d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 528503bfb495SBarry Smith 52868d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 52878d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 52888d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 52898d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 52908d7a6e47SBarry Smith 529103bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 529203bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 529303bfb495SBarry Smith PetscFunctionReturn(0); 529403bfb495SBarry Smith } 529503bfb495SBarry Smith 529681824310SBarry Smith /* 529781824310SBarry Smith Special version for direct calls from Fortran 529881824310SBarry Smith */ 529981824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 530081824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 530181824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 530281824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 530381824310SBarry Smith #endif 530481824310SBarry Smith 530581824310SBarry Smith /* Change these macros so can be used in void function */ 530681824310SBarry Smith #undef CHKERRQ 5307e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 530881824310SBarry Smith #undef SETERRQ2 5309e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 531081824310SBarry Smith #undef SETERRQ 5311e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 531281824310SBarry Smith 531381824310SBarry Smith EXTERN_C_BEGIN 531481824310SBarry Smith #undef __FUNCT__ 531581824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 53161f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 531781824310SBarry Smith { 531881824310SBarry Smith Mat mat = *mmat; 531981824310SBarry Smith PetscInt m = *mm, n = *mn; 532081824310SBarry Smith InsertMode addv = *maddv; 532181824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 532281824310SBarry Smith PetscScalar value; 532381824310SBarry Smith PetscErrorCode ierr; 5324899cda47SBarry Smith 5325d9e2c085SLisandro Dalcin ierr = MatPreallocated(mat);CHKERRQ(ierr); 532681824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 532781824310SBarry Smith mat->insertmode = addv; 532881824310SBarry Smith } 532981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 533081824310SBarry Smith else if (mat->insertmode != addv) { 5331e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 533281824310SBarry Smith } 533381824310SBarry Smith #endif 533481824310SBarry Smith { 5335d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5336d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5337ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 533881824310SBarry Smith 533981824310SBarry Smith /* Some Variables required in the macro */ 534081824310SBarry Smith Mat A = aij->A; 534181824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 534281824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5343dd6ea824SBarry Smith MatScalar *aa = a->a; 5344ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 534581824310SBarry Smith Mat B = aij->B; 534681824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5347d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5348dd6ea824SBarry Smith MatScalar *ba = b->a; 534981824310SBarry Smith 535081824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 535181824310SBarry Smith PetscInt nonew = a->nonew; 5352dd6ea824SBarry Smith MatScalar *ap1,*ap2; 535381824310SBarry Smith 535481824310SBarry Smith PetscFunctionBegin; 535581824310SBarry Smith for (i=0; i<m; i++) { 535681824310SBarry Smith if (im[i] < 0) continue; 535781824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5358e32f2f54SBarry 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); 535981824310SBarry Smith #endif 536081824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 536181824310SBarry Smith row = im[i] - rstart; 536281824310SBarry Smith lastcol1 = -1; 536381824310SBarry Smith rp1 = aj + ai[row]; 536481824310SBarry Smith ap1 = aa + ai[row]; 536581824310SBarry Smith rmax1 = aimax[row]; 536681824310SBarry Smith nrow1 = ailen[row]; 536781824310SBarry Smith low1 = 0; 536881824310SBarry Smith high1 = nrow1; 536981824310SBarry Smith lastcol2 = -1; 537081824310SBarry Smith rp2 = bj + bi[row]; 537181824310SBarry Smith ap2 = ba + bi[row]; 537281824310SBarry Smith rmax2 = bimax[row]; 537381824310SBarry Smith nrow2 = bilen[row]; 537481824310SBarry Smith low2 = 0; 537581824310SBarry Smith high2 = nrow2; 537681824310SBarry Smith 537781824310SBarry Smith for (j=0; j<n; j++) { 537881824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 537981824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 538081824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 538181824310SBarry Smith col = in[j] - cstart; 538281824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 538381824310SBarry Smith } else if (in[j] < 0) continue; 538481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5385cb9801acSJed 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); 538681824310SBarry Smith #endif 538781824310SBarry Smith else { 538881824310SBarry Smith if (mat->was_assembled) { 538981824310SBarry Smith if (!aij->colmap) { 539081824310SBarry Smith ierr = CreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 539181824310SBarry Smith } 539281824310SBarry Smith #if defined (PETSC_USE_CTABLE) 539381824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 539481824310SBarry Smith col--; 539581824310SBarry Smith #else 539681824310SBarry Smith col = aij->colmap[in[j]] - 1; 539781824310SBarry Smith #endif 539881824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 539981824310SBarry Smith ierr = DisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 540081824310SBarry Smith col = in[j]; 540181824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 540281824310SBarry Smith B = aij->B; 540381824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 540481824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 540581824310SBarry Smith rp2 = bj + bi[row]; 540681824310SBarry Smith ap2 = ba + bi[row]; 540781824310SBarry Smith rmax2 = bimax[row]; 540881824310SBarry Smith nrow2 = bilen[row]; 540981824310SBarry Smith low2 = 0; 541081824310SBarry Smith high2 = nrow2; 5411d0f46423SBarry Smith bm = aij->B->rmap->n; 541281824310SBarry Smith ba = b->a; 541381824310SBarry Smith } 541481824310SBarry Smith } else col = in[j]; 541581824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 541681824310SBarry Smith } 541781824310SBarry Smith } 541881824310SBarry Smith } else { 541981824310SBarry Smith if (!aij->donotstash) { 542081824310SBarry Smith if (roworiented) { 5421ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 542281824310SBarry Smith } else { 5423ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 542481824310SBarry Smith } 542581824310SBarry Smith } 542681824310SBarry Smith } 542781824310SBarry Smith }} 542881824310SBarry Smith PetscFunctionReturnVoid(); 542981824310SBarry Smith } 543081824310SBarry Smith EXTERN_C_END 543103bfb495SBarry Smith 5432