18a729477SBarry Smith 2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h> /*I "petscmat.h" I*/ 39a6d0b0bSJed Brown #include <petsc-private/vecimpl.h> 4c6db04a5SJed Brown #include <petscblaslapack.h> 58a729477SBarry Smith 601bebe75SBarry Smith /*MC 701bebe75SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 801bebe75SBarry Smith 901bebe75SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 1001bebe75SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 1101bebe75SBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 1201bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 1301bebe75SBarry Smith the above preallocation routines for simplicity. 1401bebe75SBarry Smith 1501bebe75SBarry Smith Options Database Keys: 1601bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 1701bebe75SBarry Smith 189ae82921SPaul Mullowney Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when 1901bebe75SBarry Smith enough exist. 2001bebe75SBarry Smith 2101bebe75SBarry Smith Level: beginner 2201bebe75SBarry Smith 2369b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 2401bebe75SBarry Smith M*/ 2501bebe75SBarry Smith 2601bebe75SBarry Smith /*MC 2701bebe75SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 2801bebe75SBarry Smith 2901bebe75SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 3001bebe75SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 3101bebe75SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 3201bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3301bebe75SBarry Smith the above preallocation routines for simplicity. 3401bebe75SBarry Smith 3501bebe75SBarry Smith Options Database Keys: 3601bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 3701bebe75SBarry Smith 3801bebe75SBarry Smith Level: beginner 3901bebe75SBarry Smith 4001bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4101bebe75SBarry Smith M*/ 4201bebe75SBarry Smith 43dd6ea824SBarry Smith #undef __FUNCT__ 44f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ" 45f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows) 4627d4218bSShri Abhyankar { 4727d4218bSShri Abhyankar PetscErrorCode ierr; 4827d4218bSShri Abhyankar Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data; 4927d4218bSShri Abhyankar Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data; 5027d4218bSShri Abhyankar Mat_SeqAIJ *b = (Mat_SeqAIJ*)mat->B->data; 5127d4218bSShri Abhyankar const PetscInt *ia,*ib; 5227d4218bSShri Abhyankar const MatScalar *aa,*bb; 5327d4218bSShri Abhyankar PetscInt na,nb,i,j,*rows,cnt=0,n0rows; 5427d4218bSShri Abhyankar PetscInt m = M->rmap->n,rstart = M->rmap->rstart; 5527d4218bSShri Abhyankar 5627d4218bSShri Abhyankar PetscFunctionBegin; 5727d4218bSShri Abhyankar *keptrows = 0; 5827d4218bSShri Abhyankar ia = a->i; 5927d4218bSShri Abhyankar ib = b->i; 6027d4218bSShri Abhyankar for (i=0; i<m; i++) { 6127d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 6227d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 6327d4218bSShri Abhyankar if (!na && !nb) { 6427d4218bSShri Abhyankar cnt++; 6527d4218bSShri Abhyankar goto ok1; 6627d4218bSShri Abhyankar } 6727d4218bSShri Abhyankar aa = a->a + ia[i]; 6827d4218bSShri Abhyankar for (j=0; j<na; j++) { 6927d4218bSShri Abhyankar if (aa[j] != 0.0) goto ok1; 7027d4218bSShri Abhyankar } 7127d4218bSShri Abhyankar bb = b->a + ib[i]; 7227d4218bSShri Abhyankar for (j=0; j <nb; j++) { 7327d4218bSShri Abhyankar if (bb[j] != 0.0) goto ok1; 7427d4218bSShri Abhyankar } 7527d4218bSShri Abhyankar cnt++; 7627d4218bSShri Abhyankar ok1:; 7727d4218bSShri Abhyankar } 78ce94432eSBarry Smith ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)M));CHKERRQ(ierr); 7927d4218bSShri Abhyankar if (!n0rows) PetscFunctionReturn(0); 8027d4218bSShri Abhyankar ierr = PetscMalloc((M->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr); 8127d4218bSShri Abhyankar cnt = 0; 8227d4218bSShri Abhyankar for (i=0; i<m; i++) { 8327d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 8427d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 8527d4218bSShri Abhyankar if (!na && !nb) continue; 8627d4218bSShri Abhyankar aa = a->a + ia[i]; 8727d4218bSShri Abhyankar for (j=0; j<na;j++) { 8827d4218bSShri Abhyankar if (aa[j] != 0.0) { 8927d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9027d4218bSShri Abhyankar goto ok2; 9127d4218bSShri Abhyankar } 9227d4218bSShri Abhyankar } 9327d4218bSShri Abhyankar bb = b->a + ib[i]; 9427d4218bSShri Abhyankar for (j=0; j<nb; j++) { 9527d4218bSShri Abhyankar if (bb[j] != 0.0) { 9627d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9727d4218bSShri Abhyankar goto ok2; 9827d4218bSShri Abhyankar } 9927d4218bSShri Abhyankar } 10027d4218bSShri Abhyankar ok2:; 10127d4218bSShri Abhyankar } 102ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 10327d4218bSShri Abhyankar PetscFunctionReturn(0); 10427d4218bSShri Abhyankar } 10527d4218bSShri Abhyankar 10627d4218bSShri Abhyankar #undef __FUNCT__ 107f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ" 108f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows) 109f1f41ecbSJed Brown { 110f1f41ecbSJed Brown Mat_MPIAIJ *aij = (Mat_MPIAIJ*)M->data; 111f1f41ecbSJed Brown PetscErrorCode ierr; 112f1f41ecbSJed Brown PetscInt i,rstart,nrows,*rows; 113f1f41ecbSJed Brown 114f1f41ecbSJed Brown PetscFunctionBegin; 1150298fd71SBarry Smith *zrows = NULL; 116f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr); 1170298fd71SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,NULL);CHKERRQ(ierr); 118f1f41ecbSJed Brown for (i=0; i<nrows; i++) rows[i] += rstart; 119ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 120f1f41ecbSJed Brown PetscFunctionReturn(0); 121f1f41ecbSJed Brown } 122f1f41ecbSJed Brown 123f1f41ecbSJed Brown #undef __FUNCT__ 1240716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ" 1250716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms) 1260716a85fSBarry Smith { 1270716a85fSBarry Smith PetscErrorCode ierr; 1280716a85fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data; 1290716a85fSBarry Smith PetscInt i,n,*garray = aij->garray; 1300716a85fSBarry Smith Mat_SeqAIJ *a_aij = (Mat_SeqAIJ*) aij->A->data; 1310716a85fSBarry Smith Mat_SeqAIJ *b_aij = (Mat_SeqAIJ*) aij->B->data; 1320716a85fSBarry Smith PetscReal *work; 1330716a85fSBarry Smith 1340716a85fSBarry Smith PetscFunctionBegin; 1350298fd71SBarry Smith ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr); 1360716a85fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscReal),&work);CHKERRQ(ierr); 1370716a85fSBarry Smith ierr = PetscMemzero(work,n*sizeof(PetscReal));CHKERRQ(ierr); 1380716a85fSBarry Smith if (type == NORM_2) { 1390716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1400716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]); 1410716a85fSBarry Smith } 1420716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1430716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]); 1440716a85fSBarry Smith } 1450716a85fSBarry Smith } else if (type == NORM_1) { 1460716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1470716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]); 1480716a85fSBarry Smith } 1490716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1500716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]); 1510716a85fSBarry Smith } 1520716a85fSBarry Smith } else if (type == NORM_INFINITY) { 1530716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1540716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]); 1550716a85fSBarry Smith } 1560716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1570716a85fSBarry Smith work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]); 1580716a85fSBarry Smith } 1590716a85fSBarry Smith 160ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType"); 1610716a85fSBarry Smith if (type == NORM_INFINITY) { 1620716a85fSBarry Smith ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);CHKERRQ(ierr); 1630716a85fSBarry Smith } else { 1640716a85fSBarry Smith ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);CHKERRQ(ierr); 1650716a85fSBarry Smith } 1660716a85fSBarry Smith ierr = PetscFree(work);CHKERRQ(ierr); 1670716a85fSBarry Smith if (type == NORM_2) { 1688f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 1690716a85fSBarry Smith } 1700716a85fSBarry Smith PetscFunctionReturn(0); 1710716a85fSBarry Smith } 1720716a85fSBarry Smith 1730716a85fSBarry Smith #undef __FUNCT__ 174dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ" 175dd6ea824SBarry Smith /* 176dd6ea824SBarry Smith Distributes a SeqAIJ matrix across a set of processes. Code stolen from 177dd6ea824SBarry Smith MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type. 178dd6ea824SBarry Smith 179dd6ea824SBarry Smith Only for square matrices 180dd6ea824SBarry Smith */ 181*5a576424SJed Brown PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat) 182dd6ea824SBarry Smith { 183dd6ea824SBarry Smith PetscMPIInt rank,size; 184efcf75d5SBarry Smith PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld,bses[2]; 185dd6ea824SBarry Smith PetscErrorCode ierr; 186dd6ea824SBarry Smith Mat mat; 187dd6ea824SBarry Smith Mat_SeqAIJ *gmata; 188dd6ea824SBarry Smith PetscMPIInt tag; 189dd6ea824SBarry Smith MPI_Status status; 190ace3abfcSBarry Smith PetscBool aij; 191dd6ea824SBarry Smith MatScalar *gmataa,*ao,*ad,*gmataarestore=0; 192dd6ea824SBarry Smith 193dd6ea824SBarry Smith PetscFunctionBegin; 194dd6ea824SBarry Smith CHKMEMQ; 195dd6ea824SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 196dd6ea824SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 197dd6ea824SBarry Smith if (!rank) { 198251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr); 199ce94432eSBarry Smith if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name); 200dd6ea824SBarry Smith } 201dd6ea824SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 202dd6ea824SBarry Smith ierr = MatCreate(comm,&mat);CHKERRQ(ierr); 203dd6ea824SBarry Smith ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 204efcf75d5SBarry Smith if (!rank) { 205efcf75d5SBarry Smith bses[0] = gmat->rmap->bs; 206efcf75d5SBarry Smith bses[1] = gmat->cmap->bs; 207efcf75d5SBarry Smith } 208efcf75d5SBarry Smith ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr); 209efcf75d5SBarry Smith ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr); 210dd6ea824SBarry Smith ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 211dd6ea824SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 212dd6ea824SBarry Smith ierr = PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);CHKERRQ(ierr); 213dd6ea824SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 2142205254eSKarl Rupp 215dd6ea824SBarry Smith rowners[0] = 0; 2162205254eSKarl Rupp for (i=2; i<=size; i++) rowners[i] += rowners[i-1]; 217dd6ea824SBarry Smith rstart = rowners[rank]; 218dd6ea824SBarry Smith rend = rowners[rank+1]; 219dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 220dd6ea824SBarry Smith if (!rank) { 221dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 222dd6ea824SBarry Smith /* send row lengths to all processors */ 223dd6ea824SBarry Smith for (i=0; i<m; i++) dlens[i] = gmata->ilen[i]; 224dd6ea824SBarry Smith for (i=1; i<size; i++) { 225dd6ea824SBarry Smith ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 226dd6ea824SBarry Smith } 227dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 228dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 229dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 230dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 231dd6ea824SBarry Smith jj = 0; 232dd6ea824SBarry Smith for (i=0; i<m; i++) { 233dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 234dd6ea824SBarry Smith if (gmata->j[jj] < rstart) ld[i]++; 235dd6ea824SBarry Smith if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++; 236dd6ea824SBarry Smith jj++; 237dd6ea824SBarry Smith } 238dd6ea824SBarry Smith } 239dd6ea824SBarry Smith /* send column indices to other processes */ 240dd6ea824SBarry Smith for (i=1; i<size; i++) { 241dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 242dd6ea824SBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 243dd6ea824SBarry Smith ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 244dd6ea824SBarry Smith } 245dd6ea824SBarry Smith 246dd6ea824SBarry Smith /* send numerical values to other processes */ 247dd6ea824SBarry Smith for (i=1; i<size; i++) { 248dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 249dd6ea824SBarry Smith ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 250dd6ea824SBarry Smith } 251dd6ea824SBarry Smith gmataa = gmata->a; 252dd6ea824SBarry Smith gmataj = gmata->j; 253dd6ea824SBarry Smith 254dd6ea824SBarry Smith } else { 255dd6ea824SBarry Smith /* receive row lengths */ 256dd6ea824SBarry Smith ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 257dd6ea824SBarry Smith /* receive column indices */ 258dd6ea824SBarry Smith ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 259dd6ea824SBarry Smith ierr = PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);CHKERRQ(ierr); 260dd6ea824SBarry Smith ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 261dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 262dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 263dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 264dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 265dd6ea824SBarry Smith jj = 0; 266dd6ea824SBarry Smith for (i=0; i<m; i++) { 267dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 268dd6ea824SBarry Smith if (gmataj[jj] < rstart) ld[i]++; 269dd6ea824SBarry Smith if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++; 270dd6ea824SBarry Smith jj++; 271dd6ea824SBarry Smith } 272dd6ea824SBarry Smith } 273dd6ea824SBarry Smith /* receive numerical values */ 274dd6ea824SBarry Smith ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 275dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 276dd6ea824SBarry Smith } 277dd6ea824SBarry Smith /* set preallocation */ 278dd6ea824SBarry Smith for (i=0; i<m; i++) { 279dd6ea824SBarry Smith dlens[i] -= olens[i]; 280dd6ea824SBarry Smith } 281dd6ea824SBarry Smith ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr); 282dd6ea824SBarry Smith ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr); 283dd6ea824SBarry Smith 284dd6ea824SBarry Smith for (i=0; i<m; i++) { 285dd6ea824SBarry Smith dlens[i] += olens[i]; 286dd6ea824SBarry Smith } 287dd6ea824SBarry Smith cnt = 0; 288dd6ea824SBarry Smith for (i=0; i<m; i++) { 289dd6ea824SBarry Smith row = rstart + i; 290dd6ea824SBarry Smith ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr); 291dd6ea824SBarry Smith cnt += dlens[i]; 292dd6ea824SBarry Smith } 293dd6ea824SBarry Smith if (rank) { 294dd6ea824SBarry Smith ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr); 295dd6ea824SBarry Smith } 296dd6ea824SBarry Smith ierr = PetscFree2(dlens,olens);CHKERRQ(ierr); 297dd6ea824SBarry Smith ierr = PetscFree(rowners);CHKERRQ(ierr); 2982205254eSKarl Rupp 299dd6ea824SBarry Smith ((Mat_MPIAIJ*)(mat->data))->ld = ld; 3002205254eSKarl Rupp 301dd6ea824SBarry Smith *inmat = mat; 302dd6ea824SBarry Smith } else { /* column indices are already set; only need to move over numerical values from process 0 */ 303dd6ea824SBarry Smith Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data; 304dd6ea824SBarry Smith Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data; 305dd6ea824SBarry Smith mat = *inmat; 306dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 307dd6ea824SBarry Smith if (!rank) { 308dd6ea824SBarry Smith /* send numerical values to other processes */ 309dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 310dd6ea824SBarry Smith ierr = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr); 311dd6ea824SBarry Smith gmataa = gmata->a; 312dd6ea824SBarry Smith for (i=1; i<size; i++) { 313dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 314dd6ea824SBarry Smith ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 315dd6ea824SBarry Smith } 316dd6ea824SBarry Smith nz = gmata->i[rowners[1]]-gmata->i[rowners[0]]; 317dd6ea824SBarry Smith } else { 318dd6ea824SBarry Smith /* receive numerical values from process 0*/ 319dd6ea824SBarry Smith nz = Ad->nz + Ao->nz; 320dd6ea824SBarry Smith ierr = PetscMalloc(nz*sizeof(PetscScalar),&gmataa);CHKERRQ(ierr); gmataarestore = gmataa; 321dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 322dd6ea824SBarry Smith } 323dd6ea824SBarry Smith /* transfer numerical values into the diagonal A and off diagonal B parts of mat */ 324dd6ea824SBarry Smith ld = ((Mat_MPIAIJ*)(mat->data))->ld; 325dd6ea824SBarry Smith ad = Ad->a; 326dd6ea824SBarry Smith ao = Ao->a; 327d0f46423SBarry Smith if (mat->rmap->n) { 328dd6ea824SBarry Smith i = 0; 329dd6ea824SBarry Smith nz = ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 330dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 331dd6ea824SBarry Smith } 332d0f46423SBarry Smith for (i=1; i<mat->rmap->n; i++) { 333dd6ea824SBarry 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; 334dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 335dd6ea824SBarry Smith } 336dd6ea824SBarry Smith i--; 337d0f46423SBarry Smith if (mat->rmap->n) { 33822d28d08SBarry Smith nz = Ao->i[i+1] - Ao->i[i] - ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 339dd6ea824SBarry Smith } 340dd6ea824SBarry Smith if (rank) { 341dd6ea824SBarry Smith ierr = PetscFree(gmataarestore);CHKERRQ(ierr); 342dd6ea824SBarry Smith } 343dd6ea824SBarry Smith } 344dd6ea824SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 345dd6ea824SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 346dd6ea824SBarry Smith CHKMEMQ; 347dd6ea824SBarry Smith PetscFunctionReturn(0); 348dd6ea824SBarry Smith } 349dd6ea824SBarry Smith 3500f5bd95cSBarry Smith /* 3510f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 3529e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 3530f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 3540f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 3550f5bd95cSBarry Smith has an order N integer array but is fast to acess. 3569e25ed09SBarry Smith */ 3574a2ae208SSatish Balay #undef __FUNCT__ 358ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private" 359ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat) 3609e25ed09SBarry Smith { 36144a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3626849ba73SBarry Smith PetscErrorCode ierr; 363d0f46423SBarry Smith PetscInt n = aij->B->cmap->n,i; 364dbb450caSBarry Smith 3653a40ed3dSBarry Smith PetscFunctionBegin; 3665e1f6667SBarry Smith if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray"); 367aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 368e23dfa41SBarry Smith ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr); 369b1fc9764SSatish Balay for (i=0; i<n; i++) { 3703861aac3SJed Brown ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr); 371b1fc9764SSatish Balay } 372b1fc9764SSatish Balay #else 373d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr); 374d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 375d0f46423SBarry Smith ierr = PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 376905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 377b1fc9764SSatish Balay #endif 3783a40ed3dSBarry Smith PetscFunctionReturn(0); 3799e25ed09SBarry Smith } 3809e25ed09SBarry Smith 38130770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 3820520107fSSatish Balay { \ 383db4deed7SKarl Rupp if (col <= lastcol1) low1 = 0; \ 384db4deed7SKarl Rupp else high1 = nrow1; \ 385fd3458f5SBarry Smith lastcol1 = col;\ 386fd3458f5SBarry Smith while (high1-low1 > 5) { \ 387fd3458f5SBarry Smith t = (low1+high1)/2; \ 388fd3458f5SBarry Smith if (rp1[t] > col) high1 = t; \ 389fd3458f5SBarry Smith else low1 = t; \ 390ba4e3ef2SSatish Balay } \ 391fd3458f5SBarry Smith for (_i=low1; _i<high1; _i++) { \ 392fd3458f5SBarry Smith if (rp1[_i] > col) break; \ 393fd3458f5SBarry Smith if (rp1[_i] == col) { \ 394fd3458f5SBarry Smith if (addv == ADD_VALUES) ap1[_i] += value; \ 395fd3458f5SBarry Smith else ap1[_i] = value; \ 39630770e4dSSatish Balay goto a_noinsert; \ 3970520107fSSatish Balay } \ 3980520107fSSatish Balay } \ 399e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \ 400e44c0bd4SBarry Smith if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;} \ 401e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 402fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \ 403669a8dbcSSatish Balay N = nrow1++ - 1; a->nz++; high1++; \ 4040520107fSSatish Balay /* shift up all the later entries in this row */ \ 4050520107fSSatish Balay for (ii=N; ii>=_i; ii--) { \ 406fd3458f5SBarry Smith rp1[ii+1] = rp1[ii]; \ 407fd3458f5SBarry Smith ap1[ii+1] = ap1[ii]; \ 4080520107fSSatish Balay } \ 409fd3458f5SBarry Smith rp1[_i] = col; \ 410fd3458f5SBarry Smith ap1[_i] = value; \ 41130770e4dSSatish Balay a_noinsert: ; \ 412fd3458f5SBarry Smith ailen[row] = nrow1; \ 4130520107fSSatish Balay } 4140a198c4cSBarry Smith 415085a36d4SBarry Smith 41630770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 41730770e4dSSatish Balay { \ 418db4deed7SKarl Rupp if (col <= lastcol2) low2 = 0; \ 419db4deed7SKarl Rupp else high2 = nrow2; \ 420fd3458f5SBarry Smith lastcol2 = col; \ 421fd3458f5SBarry Smith while (high2-low2 > 5) { \ 422fd3458f5SBarry Smith t = (low2+high2)/2; \ 423fd3458f5SBarry Smith if (rp2[t] > col) high2 = t; \ 424fd3458f5SBarry Smith else low2 = t; \ 425ba4e3ef2SSatish Balay } \ 426fd3458f5SBarry Smith for (_i=low2; _i<high2; _i++) { \ 427fd3458f5SBarry Smith if (rp2[_i] > col) break; \ 428fd3458f5SBarry Smith if (rp2[_i] == col) { \ 429fd3458f5SBarry Smith if (addv == ADD_VALUES) ap2[_i] += value; \ 430fd3458f5SBarry Smith else ap2[_i] = value; \ 43130770e4dSSatish Balay goto b_noinsert; \ 43230770e4dSSatish Balay } \ 43330770e4dSSatish Balay } \ 434e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 435e44c0bd4SBarry Smith if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 436e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 437fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \ 438669a8dbcSSatish Balay N = nrow2++ - 1; b->nz++; high2++; \ 43930770e4dSSatish Balay /* shift up all the later entries in this row */ \ 44030770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 441fd3458f5SBarry Smith rp2[ii+1] = rp2[ii]; \ 442fd3458f5SBarry Smith ap2[ii+1] = ap2[ii]; \ 44330770e4dSSatish Balay } \ 444fd3458f5SBarry Smith rp2[_i] = col; \ 445fd3458f5SBarry Smith ap2[_i] = value; \ 44630770e4dSSatish Balay b_noinsert: ; \ 447fd3458f5SBarry Smith bilen[row] = nrow2; \ 44830770e4dSSatish Balay } 44930770e4dSSatish Balay 4504a2ae208SSatish Balay #undef __FUNCT__ 4512fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ" 4522fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[]) 4532fd7e33dSBarry Smith { 4542fd7e33dSBarry Smith Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 4552fd7e33dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data; 4562fd7e33dSBarry Smith PetscErrorCode ierr; 4572fd7e33dSBarry Smith PetscInt l,*garray = mat->garray,diag; 4582fd7e33dSBarry Smith 4592fd7e33dSBarry Smith PetscFunctionBegin; 4602fd7e33dSBarry Smith /* code only works for square matrices A */ 4612fd7e33dSBarry Smith 4622fd7e33dSBarry Smith /* find size of row to the left of the diagonal part */ 4632fd7e33dSBarry Smith ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr); 4642fd7e33dSBarry Smith row = row - diag; 4652fd7e33dSBarry Smith for (l=0; l<b->i[row+1]-b->i[row]; l++) { 4662fd7e33dSBarry Smith if (garray[b->j[b->i[row]+l]] > diag) break; 4672fd7e33dSBarry Smith } 4682fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr); 4692fd7e33dSBarry Smith 4702fd7e33dSBarry Smith /* diagonal part */ 4712fd7e33dSBarry Smith ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr); 4722fd7e33dSBarry Smith 4732fd7e33dSBarry Smith /* right of diagonal part */ 4742fd7e33dSBarry 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); 4752fd7e33dSBarry Smith PetscFunctionReturn(0); 4762fd7e33dSBarry Smith } 4772fd7e33dSBarry Smith 4782fd7e33dSBarry Smith #undef __FUNCT__ 4794a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 480b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 4818a729477SBarry Smith { 48244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 48387828ca2SBarry Smith PetscScalar value; 484dfbe8321SBarry Smith PetscErrorCode ierr; 485d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 486d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 487ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 4888a729477SBarry Smith 4890520107fSSatish Balay /* Some Variables required in the macro */ 4904ee7247eSSatish Balay Mat A = aij->A; 4914ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 49257809a77SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 493a77337e4SBarry Smith MatScalar *aa = a->a; 494ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 49530770e4dSSatish Balay Mat B = aij->B; 49630770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 497d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 498a77337e4SBarry Smith MatScalar *ba = b->a; 49930770e4dSSatish Balay 500fd3458f5SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 5018d76821aSHong Zhang PetscInt nonew; 502a77337e4SBarry Smith MatScalar *ap1,*ap2; 5034ee7247eSSatish Balay 5043a40ed3dSBarry Smith PetscFunctionBegin; 50571fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 5068a729477SBarry Smith for (i=0; i<m; i++) { 5075ef9f2a5SBarry Smith if (im[i] < 0) continue; 5082515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 509e32f2f54SBarry 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); 5100a198c4cSBarry Smith #endif 5114b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 5124b0e389bSBarry Smith row = im[i] - rstart; 513fd3458f5SBarry Smith lastcol1 = -1; 514fd3458f5SBarry Smith rp1 = aj + ai[row]; 515fd3458f5SBarry Smith ap1 = aa + ai[row]; 516fd3458f5SBarry Smith rmax1 = aimax[row]; 517fd3458f5SBarry Smith nrow1 = ailen[row]; 518fd3458f5SBarry Smith low1 = 0; 519fd3458f5SBarry Smith high1 = nrow1; 520fd3458f5SBarry Smith lastcol2 = -1; 521fd3458f5SBarry Smith rp2 = bj + bi[row]; 522d498b1e9SBarry Smith ap2 = ba + bi[row]; 523fd3458f5SBarry Smith rmax2 = bimax[row]; 524d498b1e9SBarry Smith nrow2 = bilen[row]; 525fd3458f5SBarry Smith low2 = 0; 526fd3458f5SBarry Smith high2 = nrow2; 527fd3458f5SBarry Smith 5281eb62cbbSBarry Smith for (j=0; j<n; j++) { 529db4deed7SKarl Rupp if (v) { 530db4deed7SKarl Rupp if (roworiented) value = v[i*n+j]; 531db4deed7SKarl Rupp else value = v[i+j*m]; 532db4deed7SKarl Rupp } else value = 0.0; 533abc0a331SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 534fd3458f5SBarry Smith if (in[j] >= cstart && in[j] < cend) { 535fd3458f5SBarry Smith col = in[j] - cstart; 5368d76821aSHong Zhang nonew = a->nonew; 53730770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 538273d9f13SBarry Smith } else if (in[j] < 0) continue; 5392515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 540cb9801acSJed 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); 5410a198c4cSBarry Smith #endif 5421eb62cbbSBarry Smith else { 543227d817aSBarry Smith if (mat->was_assembled) { 544905e6a2fSBarry Smith if (!aij->colmap) { 545ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 546905e6a2fSBarry Smith } 547aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 5480f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 549fa46199cSSatish Balay col--; 550b1fc9764SSatish Balay #else 551905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 552b1fc9764SSatish Balay #endif 5530e9bae81SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) { 554ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 5554b0e389bSBarry Smith col = in[j]; 5569bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 557f9508a3cSSatish Balay B = aij->B; 558f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 559e44c0bd4SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a; 560d498b1e9SBarry Smith rp2 = bj + bi[row]; 561d498b1e9SBarry Smith ap2 = ba + bi[row]; 562d498b1e9SBarry Smith rmax2 = bimax[row]; 563d498b1e9SBarry Smith nrow2 = bilen[row]; 564d498b1e9SBarry Smith low2 = 0; 565d498b1e9SBarry Smith high2 = nrow2; 566d0f46423SBarry Smith bm = aij->B->rmap->n; 567f9508a3cSSatish Balay ba = b->a; 5680e9bae81SBarry Smith } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]); 569c48de900SBarry Smith } else col = in[j]; 5708d76821aSHong Zhang nonew = b->nonew; 57130770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 5721eb62cbbSBarry Smith } 5731eb62cbbSBarry Smith } 5745ef9f2a5SBarry Smith } else { 5754cb17eb5SBarry 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]); 57690f02eecSBarry Smith if (!aij->donotstash) { 5775080c13bSMatthew G Knepley mat->assembled = PETSC_FALSE; 578d36fbae8SSatish Balay if (roworiented) { 579ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 580d36fbae8SSatish Balay } else { 581ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 5824b0e389bSBarry Smith } 5831eb62cbbSBarry Smith } 5848a729477SBarry Smith } 58590f02eecSBarry Smith } 5863a40ed3dSBarry Smith PetscFunctionReturn(0); 5878a729477SBarry Smith } 5888a729477SBarry Smith 5894a2ae208SSatish Balay #undef __FUNCT__ 5904a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 591b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 592b49de8d1SLois Curfman McInnes { 593b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 594dfbe8321SBarry Smith PetscErrorCode ierr; 595d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 596d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 597b49de8d1SLois Curfman McInnes 5983a40ed3dSBarry Smith PetscFunctionBegin; 599b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 600e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 601e32f2f54SBarry 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); 602b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 603b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 604b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 605e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 606e32f2f54SBarry 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); 607b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend) { 608b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 609b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 610fa852ad4SSatish Balay } else { 611905e6a2fSBarry Smith if (!aij->colmap) { 612ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 613905e6a2fSBarry Smith } 614aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 6150f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 616fa46199cSSatish Balay col--; 617b1fc9764SSatish Balay #else 618905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 619b1fc9764SSatish Balay #endif 620e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 621d9d09a02SSatish Balay else { 622b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 623b49de8d1SLois Curfman McInnes } 624b49de8d1SLois Curfman McInnes } 625b49de8d1SLois Curfman McInnes } 626f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 627b49de8d1SLois Curfman McInnes } 6283a40ed3dSBarry Smith PetscFunctionReturn(0); 629b49de8d1SLois Curfman McInnes } 630bc5ccf88SSatish Balay 631bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 632bd0c2dcbSBarry Smith 6334a2ae208SSatish Balay #undef __FUNCT__ 6344a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 635dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 636bc5ccf88SSatish Balay { 637bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 638dfbe8321SBarry Smith PetscErrorCode ierr; 639b1d57f15SBarry Smith PetscInt nstash,reallocs; 640bc5ccf88SSatish Balay InsertMode addv; 641bc5ccf88SSatish Balay 642bc5ccf88SSatish Balay PetscFunctionBegin; 6432205254eSKarl Rupp if (aij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0); 644bc5ccf88SSatish Balay 645bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 646ce94432eSBarry Smith ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 647ce94432eSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 648bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 649bc5ccf88SSatish Balay 650d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 6518798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 652ae15b995SBarry Smith ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 653bc5ccf88SSatish Balay PetscFunctionReturn(0); 654bc5ccf88SSatish Balay } 655bc5ccf88SSatish Balay 6564a2ae208SSatish Balay #undef __FUNCT__ 6574a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 658dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 659bc5ccf88SSatish Balay { 660bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 66191c97fd4SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)aij->A->data; 6626849ba73SBarry Smith PetscErrorCode ierr; 663b1d57f15SBarry Smith PetscMPIInt n; 664b1d57f15SBarry Smith PetscInt i,j,rstart,ncols,flg; 665e44c0bd4SBarry Smith PetscInt *row,*col; 666ace3abfcSBarry Smith PetscBool other_disassembled; 66787828ca2SBarry Smith PetscScalar *val; 668bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 669bc5ccf88SSatish Balay 67091c97fd4SSatish Balay /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */ 6716e111a19SKarl Rupp 672bc5ccf88SSatish Balay PetscFunctionBegin; 6734cb17eb5SBarry Smith if (!aij->donotstash && !mat->nooffprocentries) { 674a2d1c673SSatish Balay while (1) { 6758798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 676a2d1c673SSatish Balay if (!flg) break; 677a2d1c673SSatish Balay 678bc5ccf88SSatish Balay for (i=0; i<n; ) { 679bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 6802205254eSKarl Rupp for (j=i,rstart=row[j]; j<n; j++) { 6812205254eSKarl Rupp if (row[j] != rstart) break; 6822205254eSKarl Rupp } 683bc5ccf88SSatish Balay if (j < n) ncols = j-i; 684bc5ccf88SSatish Balay else ncols = n-i; 685bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 686bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 6872205254eSKarl Rupp 688bc5ccf88SSatish Balay i = j; 689bc5ccf88SSatish Balay } 690bc5ccf88SSatish Balay } 6918798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 692bc5ccf88SSatish Balay } 693bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 694bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 695bc5ccf88SSatish Balay 696bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 697bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 698bc5ccf88SSatish Balay /* 699bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 700bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 701bc5ccf88SSatish Balay */ 702bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 703ce94432eSBarry Smith ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 704bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 705ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 706ad59fb31SSatish Balay } 707ad59fb31SSatish Balay } 708bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 709bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 710bc5ccf88SSatish Balay } 7114e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 7124e35b6f3SSatish Balay ierr = MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr); 713bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 714bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 715bc5ccf88SSatish Balay 7161d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 7172205254eSKarl Rupp 718606d414cSSatish Balay aij->rowvalues = 0; 719a30b2313SHong Zhang 720a30b2313SHong Zhang /* used by MatAXPY() */ 72191c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 72291c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 723a30b2313SHong Zhang 7246bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 725bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 726bc5ccf88SSatish Balay PetscFunctionReturn(0); 727bc5ccf88SSatish Balay } 728bc5ccf88SSatish Balay 7294a2ae208SSatish Balay #undef __FUNCT__ 7304a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 731dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 7321eb62cbbSBarry Smith { 73344a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 734dfbe8321SBarry Smith PetscErrorCode ierr; 7353a40ed3dSBarry Smith 7363a40ed3dSBarry Smith PetscFunctionBegin; 73778b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 73878b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 7393a40ed3dSBarry Smith PetscFunctionReturn(0); 7401eb62cbbSBarry Smith } 7411eb62cbbSBarry Smith 7424a2ae208SSatish Balay #undef __FUNCT__ 7434a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 7442b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7451eb62cbbSBarry Smith { 74644a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 7476849ba73SBarry Smith PetscErrorCode ierr; 7487adad957SLisandro Dalcin PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 749d0f46423SBarry Smith PetscInt i,*owners = A->rmap->range; 750b1d57f15SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 751b1d57f15SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 752b1d57f15SBarry Smith PetscInt *rvalues,count,base,slen,*source; 753d0f46423SBarry Smith PetscInt *lens,*lrows,*values,rstart=A->rmap->rstart; 754ce94432eSBarry Smith MPI_Comm comm; 7551eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 7561eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 75797b48c8fSBarry Smith const PetscScalar *xx; 75897b48c8fSBarry Smith PetscScalar *bb; 7596543fbbaSBarry Smith #if defined(PETSC_DEBUG) 760ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 7616543fbbaSBarry Smith #endif 7621eb62cbbSBarry Smith 7633a40ed3dSBarry Smith PetscFunctionBegin; 764ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 7651eb62cbbSBarry Smith /* first count number of contributors to each processor */ 766b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 767b1d57f15SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 768b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 7696543fbbaSBarry Smith j = 0; 7701eb62cbbSBarry Smith for (i=0; i<N; i++) { 7716543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 7726543fbbaSBarry Smith lastidx = idx; 7736543fbbaSBarry Smith for (; j<size; j++) { 7741eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 7756543fbbaSBarry Smith nprocs[2*j]++; 7766543fbbaSBarry Smith nprocs[2*j+1] = 1; 7776543fbbaSBarry Smith owner[i] = j; 7786543fbbaSBarry Smith #if defined(PETSC_DEBUG) 7796543fbbaSBarry Smith found = PETSC_TRUE; 7806543fbbaSBarry Smith #endif 7816543fbbaSBarry Smith break; 7821eb62cbbSBarry Smith } 7831eb62cbbSBarry Smith } 7846543fbbaSBarry Smith #if defined(PETSC_DEBUG) 785e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 7866543fbbaSBarry Smith found = PETSC_FALSE; 7876543fbbaSBarry Smith #endif 7881eb62cbbSBarry Smith } 7892205254eSKarl Rupp nsends = 0; 7902205254eSKarl Rupp for (i=0; i<size; i++) nsends += nprocs[2*i+1]; 7911eb62cbbSBarry Smith 7927367270fSBarry Smith if (A->nooffproczerorows) { 7937367270fSBarry 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"); 7947367270fSBarry Smith nrecvs = nsends; 7957367270fSBarry Smith nmax = N; 7967367270fSBarry Smith } else { 7971eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 798c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 7997367270fSBarry Smith } 8001eb62cbbSBarry Smith 8011eb62cbbSBarry Smith /* post receives: */ 802b1d57f15SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 803b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 8041eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 805b1d57f15SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 8061eb62cbbSBarry Smith } 8071eb62cbbSBarry Smith 8081eb62cbbSBarry Smith /* do sends: 8091eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 8101eb62cbbSBarry Smith the ith processor 8111eb62cbbSBarry Smith */ 812b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 813b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 814b1d57f15SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 8151eb62cbbSBarry Smith 8161eb62cbbSBarry Smith starts[0] = 0; 8172205254eSKarl Rupp for (i=1; i<size; i++) starts[i] = starts[i-1] + nprocs[2*i-2]; 8182205254eSKarl Rupp for (i=0; i<N; i++) svalues[starts[owner[i]]++] = rows[i]; 8192205254eSKarl Rupp 8202205254eSKarl Rupp starts[0] = 0; 8212205254eSKarl Rupp for (i=1; i<size+1; i++) starts[i] = starts[i-1] + nprocs[2*i-2]; 8221eb62cbbSBarry Smith count = 0; 82317699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 824c1dc657dSBarry Smith if (nprocs[2*i+1]) { 825b1d57f15SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 8261eb62cbbSBarry Smith } 8271eb62cbbSBarry Smith } 828606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 8291eb62cbbSBarry Smith 83017699dbbSLois Curfman McInnes base = owners[rank]; 8311eb62cbbSBarry Smith 8321eb62cbbSBarry Smith /* wait on receives */ 8331d79065fSBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 8341eb62cbbSBarry Smith count = nrecvs; slen = 0; 8351eb62cbbSBarry Smith while (count) { 836ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 8371eb62cbbSBarry Smith /* unpack receives into our local space */ 838b1d57f15SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 8392205254eSKarl Rupp 840d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 841d6dfbf8fSBarry Smith lens[imdex] = n; 8421eb62cbbSBarry Smith slen += n; 8431eb62cbbSBarry Smith count--; 8441eb62cbbSBarry Smith } 845606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 8461eb62cbbSBarry Smith 8471eb62cbbSBarry Smith /* move the data into the send scatter */ 848b1d57f15SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 8491eb62cbbSBarry Smith count = 0; 8501eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 8511eb62cbbSBarry Smith values = rvalues + i*nmax; 8522205254eSKarl Rupp for (j=0; j<lens[i]; j++) lrows[count++] = values[j] - base; 8531eb62cbbSBarry Smith } 854606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 8551d79065fSBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 856606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 857606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 8581eb62cbbSBarry Smith 85997b48c8fSBarry Smith /* fix right hand side if needed */ 86097b48c8fSBarry Smith if (x && b) { 86197b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 86297b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 8632205254eSKarl Rupp for (i=0; i<slen; i++) bb[lrows[i]] = diag*xx[lrows[i]]; 86497b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 86597b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 86697b48c8fSBarry Smith } 8676eb55b6aSBarry Smith /* 8686eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 869a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 8706eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 8716eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 8726eb55b6aSBarry Smith 8736eb55b6aSBarry Smith */ 874e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 8752b40b63fSBarry Smith ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr); 876d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 8772b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr); 878f4df32b1SMatthew Knepley } else if (diag != 0.0) { 8792b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 8802205254eSKarl Rupp if (((Mat_SeqAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\nMAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 881e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 882e2d53e46SBarry Smith row = lrows[i] + rstart; 883f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 884e2d53e46SBarry Smith } 885e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 886e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 8876eb55b6aSBarry Smith } else { 8882b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 8896eb55b6aSBarry Smith } 890606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 89172dacd9aSBarry Smith 8921eb62cbbSBarry Smith /* wait on sends */ 8931eb62cbbSBarry Smith if (nsends) { 894b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 895ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 896606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 8971eb62cbbSBarry Smith } 898606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 899606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 9003a40ed3dSBarry Smith PetscFunctionReturn(0); 9011eb62cbbSBarry Smith } 9021eb62cbbSBarry Smith 9034a2ae208SSatish Balay #undef __FUNCT__ 9049c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 9059c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 9069c7c4993SBarry Smith { 9079c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 9089c7c4993SBarry Smith PetscErrorCode ierr; 9099c7c4993SBarry Smith PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 9109c7c4993SBarry Smith PetscInt i,*owners = A->rmap->range; 911564f14d6SBarry Smith PetscInt *nprocs,j,idx,nsends; 9129c7c4993SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 9139c7c4993SBarry Smith PetscInt *rvalues,count,base,slen,*source; 914564f14d6SBarry Smith PetscInt *lens,*lrows,*values,m; 915ce94432eSBarry Smith MPI_Comm comm; 9169c7c4993SBarry Smith MPI_Request *send_waits,*recv_waits; 9179c7c4993SBarry Smith MPI_Status recv_status,*send_status; 9189c7c4993SBarry Smith const PetscScalar *xx; 919564f14d6SBarry Smith PetscScalar *bb,*mask; 920564f14d6SBarry Smith Vec xmask,lmask; 921564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 922564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 923564f14d6SBarry Smith PetscScalar *aa; 9249c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9259c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 9269c7c4993SBarry Smith #endif 9279c7c4993SBarry Smith 9289c7c4993SBarry Smith PetscFunctionBegin; 929ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 9309c7c4993SBarry Smith /* first count number of contributors to each processor */ 9319c7c4993SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 9329c7c4993SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 9339c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 9349c7c4993SBarry Smith j = 0; 9359c7c4993SBarry Smith for (i=0; i<N; i++) { 9369c7c4993SBarry Smith if (lastidx > (idx = rows[i])) j = 0; 9379c7c4993SBarry Smith lastidx = idx; 9389c7c4993SBarry Smith for (; j<size; j++) { 9399c7c4993SBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 9409c7c4993SBarry Smith nprocs[2*j]++; 9419c7c4993SBarry Smith nprocs[2*j+1] = 1; 9429c7c4993SBarry Smith owner[i] = j; 9439c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9449c7c4993SBarry Smith found = PETSC_TRUE; 9459c7c4993SBarry Smith #endif 9469c7c4993SBarry Smith break; 9479c7c4993SBarry Smith } 9489c7c4993SBarry Smith } 9499c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9509c7c4993SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 9519c7c4993SBarry Smith found = PETSC_FALSE; 9529c7c4993SBarry Smith #endif 9539c7c4993SBarry Smith } 9542205254eSKarl Rupp nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1]; 9559c7c4993SBarry Smith 9569c7c4993SBarry Smith /* inform other processors of number of messages and max length*/ 9579c7c4993SBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 9589c7c4993SBarry Smith 9599c7c4993SBarry Smith /* post receives: */ 9609c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 9619c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 9629c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 9639c7c4993SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 9649c7c4993SBarry Smith } 9659c7c4993SBarry Smith 9669c7c4993SBarry Smith /* do sends: 9679c7c4993SBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 9689c7c4993SBarry Smith the ith processor 9699c7c4993SBarry Smith */ 9709c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 9719c7c4993SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 9729c7c4993SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 9739c7c4993SBarry Smith 9749c7c4993SBarry Smith starts[0] = 0; 9752205254eSKarl Rupp for (i=1; i<size; i++) starts[i] = starts[i-1] + nprocs[2*i-2]; 9762205254eSKarl Rupp for (i=0; i<N; i++) svalues[starts[owner[i]]++] = rows[i]; 9772205254eSKarl Rupp 9782205254eSKarl Rupp starts[0] = 0; 9792205254eSKarl Rupp for (i=1; i<size+1; i++) starts[i] = starts[i-1] + nprocs[2*i-2]; 9809c7c4993SBarry Smith count = 0; 9819c7c4993SBarry Smith for (i=0; i<size; i++) { 9829c7c4993SBarry Smith if (nprocs[2*i+1]) { 9839c7c4993SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 9849c7c4993SBarry Smith } 9859c7c4993SBarry Smith } 9869c7c4993SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 9879c7c4993SBarry Smith 9889c7c4993SBarry Smith base = owners[rank]; 9899c7c4993SBarry Smith 9909c7c4993SBarry Smith /* wait on receives */ 9919c7c4993SBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 9929c7c4993SBarry Smith count = nrecvs; slen = 0; 9939c7c4993SBarry Smith while (count) { 9949c7c4993SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 9959c7c4993SBarry Smith /* unpack receives into our local space */ 9969c7c4993SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 9972205254eSKarl Rupp 9989c7c4993SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 9999c7c4993SBarry Smith lens[imdex] = n; 10009c7c4993SBarry Smith slen += n; 10019c7c4993SBarry Smith count--; 10029c7c4993SBarry Smith } 10039c7c4993SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 10049c7c4993SBarry Smith 10059c7c4993SBarry Smith /* move the data into the send scatter */ 10069c7c4993SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 10079c7c4993SBarry Smith count = 0; 10089c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 10099c7c4993SBarry Smith values = rvalues + i*nmax; 10102205254eSKarl Rupp for (j=0; j<lens[i]; j++) lrows[count++] = values[j] - base; 10119c7c4993SBarry Smith } 10129c7c4993SBarry Smith ierr = PetscFree(rvalues);CHKERRQ(ierr); 10139c7c4993SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 10149c7c4993SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 10159c7c4993SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 1016564f14d6SBarry Smith /* lrows are the local rows to be zeroed, slen is the number of local rows */ 10179c7c4993SBarry Smith 1018564f14d6SBarry Smith /* zero diagonal part of matrix */ 1019564f14d6SBarry Smith ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr); 10209c7c4993SBarry Smith 1021564f14d6SBarry Smith /* handle off diagonal part of matrix */ 10220298fd71SBarry Smith ierr = MatGetVecs(A,&xmask,NULL);CHKERRQ(ierr); 1023564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 1024564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 10252205254eSKarl Rupp for (i=0; i<slen; i++) bb[lrows[i]] = 1; 1026564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 1027564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1028564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 10296bf464f9SBarry Smith ierr = VecDestroy(&xmask);CHKERRQ(ierr); 1030377aa5a1SBarry Smith if (x) { 1031564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1032564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1033564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 1034564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 1035377aa5a1SBarry Smith } 1036377aa5a1SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 1037564f14d6SBarry Smith 1038564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 1039564f14d6SBarry Smith ii = aij->i; 1040564f14d6SBarry Smith for (i=0; i<slen; i++) { 1041564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 10429c7c4993SBarry Smith } 1043564f14d6SBarry Smith 1044564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 1045564f14d6SBarry Smith if (aij->compressedrow.use) { 1046564f14d6SBarry Smith m = aij->compressedrow.nrows; 1047564f14d6SBarry Smith ii = aij->compressedrow.i; 1048564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 1049564f14d6SBarry Smith for (i=0; i<m; i++) { 1050564f14d6SBarry Smith n = ii[i+1] - ii[i]; 1051564f14d6SBarry Smith aj = aij->j + ii[i]; 1052564f14d6SBarry Smith aa = aij->a + ii[i]; 1053564f14d6SBarry Smith 1054564f14d6SBarry Smith for (j=0; j<n; j++) { 105525266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 1056377aa5a1SBarry Smith if (b) bb[*ridx] -= *aa*xx[*aj]; 1057564f14d6SBarry Smith *aa = 0.0; 1058564f14d6SBarry Smith } 1059564f14d6SBarry Smith aa++; 1060564f14d6SBarry Smith aj++; 1061564f14d6SBarry Smith } 1062564f14d6SBarry Smith ridx++; 1063564f14d6SBarry Smith } 1064564f14d6SBarry Smith } else { /* do not use compressed row format */ 1065564f14d6SBarry Smith m = l->B->rmap->n; 1066564f14d6SBarry Smith for (i=0; i<m; i++) { 1067564f14d6SBarry Smith n = ii[i+1] - ii[i]; 1068564f14d6SBarry Smith aj = aij->j + ii[i]; 1069564f14d6SBarry Smith aa = aij->a + ii[i]; 1070564f14d6SBarry Smith for (j=0; j<n; j++) { 107125266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 1072377aa5a1SBarry Smith if (b) bb[i] -= *aa*xx[*aj]; 1073564f14d6SBarry Smith *aa = 0.0; 1074564f14d6SBarry Smith } 1075564f14d6SBarry Smith aa++; 1076564f14d6SBarry Smith aj++; 1077564f14d6SBarry Smith } 1078564f14d6SBarry Smith } 1079564f14d6SBarry Smith } 1080377aa5a1SBarry Smith if (x) { 1081564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 1082564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 1083377aa5a1SBarry Smith } 1084377aa5a1SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 10856bf464f9SBarry Smith ierr = VecDestroy(&lmask);CHKERRQ(ierr); 10869c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 10879c7c4993SBarry Smith 10889c7c4993SBarry Smith /* wait on sends */ 10899c7c4993SBarry Smith if (nsends) { 10909c7c4993SBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 10919c7c4993SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 10929c7c4993SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 10939c7c4993SBarry Smith } 10949c7c4993SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 10959c7c4993SBarry Smith ierr = PetscFree(svalues);CHKERRQ(ierr); 10969c7c4993SBarry Smith PetscFunctionReturn(0); 10979c7c4993SBarry Smith } 10989c7c4993SBarry Smith 10999c7c4993SBarry Smith #undef __FUNCT__ 11004a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 1101dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 11021eb62cbbSBarry Smith { 1103416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1104dfbe8321SBarry Smith PetscErrorCode ierr; 1105b1d57f15SBarry Smith PetscInt nt; 1106416022c9SBarry Smith 11073a40ed3dSBarry Smith PetscFunctionBegin; 1108a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 110965e19b50SBarry 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); 1110ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1111f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 1112ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1113f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 11143a40ed3dSBarry Smith PetscFunctionReturn(0); 11151eb62cbbSBarry Smith } 11161eb62cbbSBarry Smith 11174a2ae208SSatish Balay #undef __FUNCT__ 1118bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 1119bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 1120bd0c2dcbSBarry Smith { 1121bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1122bd0c2dcbSBarry Smith PetscErrorCode ierr; 1123bd0c2dcbSBarry Smith 1124bd0c2dcbSBarry Smith PetscFunctionBegin; 1125bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 1126bd0c2dcbSBarry Smith PetscFunctionReturn(0); 1127bd0c2dcbSBarry Smith } 1128bd0c2dcbSBarry Smith 1129bd0c2dcbSBarry Smith #undef __FUNCT__ 11304a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 1131dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1132da3a660dSBarry Smith { 1133416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1134dfbe8321SBarry Smith PetscErrorCode ierr; 11353a40ed3dSBarry Smith 11363a40ed3dSBarry Smith PetscFunctionBegin; 1137ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1138f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1139ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1140f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 11413a40ed3dSBarry Smith PetscFunctionReturn(0); 1142da3a660dSBarry Smith } 1143da3a660dSBarry Smith 11444a2ae208SSatish Balay #undef __FUNCT__ 11454a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 1146dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 1147da3a660dSBarry Smith { 1148416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1149dfbe8321SBarry Smith PetscErrorCode ierr; 1150ace3abfcSBarry Smith PetscBool merged; 1151da3a660dSBarry Smith 11523a40ed3dSBarry Smith PetscFunctionBegin; 1153a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 1154da3a660dSBarry Smith /* do nondiagonal part */ 11557c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1156a5ff213dSBarry Smith if (!merged) { 1157da3a660dSBarry Smith /* send it on its way */ 1158ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1159da3a660dSBarry Smith /* do local part */ 11607c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1161da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 1162a5ff213dSBarry Smith /* added in yy until the next line, */ 1163ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1164a5ff213dSBarry Smith } else { 1165a5ff213dSBarry Smith /* do local part */ 1166a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1167a5ff213dSBarry Smith /* send it on its way */ 1168ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1169a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 1170ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1171a5ff213dSBarry Smith } 11723a40ed3dSBarry Smith PetscFunctionReturn(0); 1173da3a660dSBarry Smith } 1174da3a660dSBarry Smith 1175cd0d46ebSvictorle EXTERN_C_BEGIN 1176cd0d46ebSvictorle #undef __FUNCT__ 11775fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 11787087cfbeSBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1179cd0d46ebSvictorle { 11804f423910Svictorle MPI_Comm comm; 1181cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ*) Amat->data, *Bij; 118266501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1183cd0d46ebSvictorle IS Me,Notme; 11846849ba73SBarry Smith PetscErrorCode ierr; 1185b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1186b1d57f15SBarry Smith PetscMPIInt size; 1187cd0d46ebSvictorle 1188cd0d46ebSvictorle PetscFunctionBegin; 118942e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 119066501d38Svictorle Bij = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A; 11915485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1192cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 11934f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1194b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1195b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 119642e5f5b4Svictorle 119742e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1198cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1199cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1200b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 1201cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1202cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 120370b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1204268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1205268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 120666501d38Svictorle Aoff = Aoffs[0]; 1207268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 120866501d38Svictorle Boff = Boffs[0]; 12095485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 121066501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 121166501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 12126bf464f9SBarry Smith ierr = ISDestroy(&Me);CHKERRQ(ierr); 12136bf464f9SBarry Smith ierr = ISDestroy(&Notme);CHKERRQ(ierr); 12143e0d0d19SHong Zhang ierr = PetscFree(notme);CHKERRQ(ierr); 1215cd0d46ebSvictorle PetscFunctionReturn(0); 1216cd0d46ebSvictorle } 1217cd0d46ebSvictorle EXTERN_C_END 1218cd0d46ebSvictorle 12194a2ae208SSatish Balay #undef __FUNCT__ 12204a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1221dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1222da3a660dSBarry Smith { 1223416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1224dfbe8321SBarry Smith PetscErrorCode ierr; 1225da3a660dSBarry Smith 12263a40ed3dSBarry Smith PetscFunctionBegin; 1227da3a660dSBarry Smith /* do nondiagonal part */ 12287c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1229da3a660dSBarry Smith /* send it on its way */ 1230ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1231da3a660dSBarry Smith /* do local part */ 12327c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1233a5ff213dSBarry Smith /* receive remote parts */ 1234ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 12353a40ed3dSBarry Smith PetscFunctionReturn(0); 1236da3a660dSBarry Smith } 1237da3a660dSBarry Smith 12381eb62cbbSBarry Smith /* 12391eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 12401eb62cbbSBarry Smith diagonal block 12411eb62cbbSBarry Smith */ 12424a2ae208SSatish Balay #undef __FUNCT__ 12434a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1244dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 12451eb62cbbSBarry Smith { 1246dfbe8321SBarry Smith PetscErrorCode ierr; 1247416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 12483a40ed3dSBarry Smith 12493a40ed3dSBarry Smith PetscFunctionBegin; 1250ce94432eSBarry Smith if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); 1251e7e72b3dSBarry 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"); 12523a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 12533a40ed3dSBarry Smith PetscFunctionReturn(0); 12541eb62cbbSBarry Smith } 12551eb62cbbSBarry Smith 12564a2ae208SSatish Balay #undef __FUNCT__ 12574a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1258f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1259052efed2SBarry Smith { 1260052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1261dfbe8321SBarry Smith PetscErrorCode ierr; 12623a40ed3dSBarry Smith 12633a40ed3dSBarry Smith PetscFunctionBegin; 1264f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1265f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 12663a40ed3dSBarry Smith PetscFunctionReturn(0); 1267052efed2SBarry Smith } 1268052efed2SBarry Smith 12694a2ae208SSatish Balay #undef __FUNCT__ 12704a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1271dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 12721eb62cbbSBarry Smith { 127344a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1274dfbe8321SBarry Smith PetscErrorCode ierr; 127583e2fdc7SBarry Smith 12763a40ed3dSBarry Smith PetscFunctionBegin; 1277aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1278d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1279a5a9c739SBarry Smith #endif 12808798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 12816bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 12826bf464f9SBarry Smith ierr = MatDestroy(&aij->A);CHKERRQ(ierr); 12836bf464f9SBarry Smith ierr = MatDestroy(&aij->B);CHKERRQ(ierr); 1284aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 12856bc0bbbfSBarry Smith ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr); 1286b1fc9764SSatish Balay #else 128705b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1288b1fc9764SSatish Balay #endif 128905b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 12906bf464f9SBarry Smith ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr); 12916bf464f9SBarry Smith ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr); 129203095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 12938aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1294bf0cc555SLisandro Dalcin ierr = PetscFree(mat->data);CHKERRQ(ierr); 1295901853e0SKris Buschelman 1296dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 12970298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",NULL);CHKERRQ(ierr); 12980298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",NULL);CHKERRQ(ierr); 12990298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",NULL);CHKERRQ(ierr); 13000298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",NULL);CHKERRQ(ierr); 13010298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",NULL);CHKERRQ(ierr); 13020298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",NULL);CHKERRQ(ierr); 13030298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",NULL);CHKERRQ(ierr); 13040298fd71SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",NULL);CHKERRQ(ierr); 13053a40ed3dSBarry Smith PetscFunctionReturn(0); 13061eb62cbbSBarry Smith } 1307ee50ffe9SBarry Smith 13084a2ae208SSatish Balay #undef __FUNCT__ 13098e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1310dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 13118e2fed03SBarry Smith { 13128e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 13138e2fed03SBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ*)aij->A->data; 13148e2fed03SBarry Smith Mat_SeqAIJ *B = (Mat_SeqAIJ*)aij->B->data; 13156849ba73SBarry Smith PetscErrorCode ierr; 131632dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 13176f69ff64SBarry Smith int fd; 1318a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1319d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 13208e2fed03SBarry Smith PetscScalar *column_values; 132185ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 1322b37d52dbSMark F. Adams FILE *file; 13238e2fed03SBarry Smith 13248e2fed03SBarry Smith PetscFunctionBegin; 1325ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1326ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 13278e2fed03SBarry Smith nz = A->nz + B->nz; 1328958c9bccSBarry Smith if (!rank) { 13290700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1330d0f46423SBarry Smith header[1] = mat->rmap->N; 1331d0f46423SBarry Smith header[2] = mat->cmap->N; 13322205254eSKarl Rupp 1333ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 13348e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 13356f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13368e2fed03SBarry Smith /* get largest number of rows any processor has */ 1337d0f46423SBarry Smith rlen = mat->rmap->n; 1338d0f46423SBarry Smith range = mat->rmap->range; 13392205254eSKarl Rupp for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]); 13408e2fed03SBarry Smith } else { 1341ce94432eSBarry Smith ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1342d0f46423SBarry Smith rlen = mat->rmap->n; 13438e2fed03SBarry Smith } 13448e2fed03SBarry Smith 13458e2fed03SBarry Smith /* load up the local row counts */ 1346b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 13472205254eSKarl Rupp for (i=0; i<mat->rmap->n; i++) row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 13488e2fed03SBarry Smith 13498e2fed03SBarry Smith /* store the row lengths to the file */ 135085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1351958c9bccSBarry Smith if (!rank) { 1352d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13538e2fed03SBarry Smith for (i=1; i<size; i++) { 1354639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 13558e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1356ce94432eSBarry Smith ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 13576f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13588e2fed03SBarry Smith } 1359639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 13608e2fed03SBarry Smith } else { 1361639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1362ce94432eSBarry Smith ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1363639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 13648e2fed03SBarry Smith } 13658e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 13668e2fed03SBarry Smith 13678e2fed03SBarry Smith /* load up the local column indices */ 13681147fc2aSKarl Rupp nzmax = nz; /* th processor needs space a largest processor needs */ 1369ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1370b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 13718e2fed03SBarry Smith cnt = 0; 1372d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 13738e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 13748e2fed03SBarry Smith if ((col = garray[B->j[j]]) > cstart) break; 13758e2fed03SBarry Smith column_indices[cnt++] = col; 13768e2fed03SBarry Smith } 13772205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart; 13782205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]]; 13798e2fed03SBarry Smith } 1380e32f2f54SBarry 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); 13818e2fed03SBarry Smith 13828e2fed03SBarry Smith /* store the column indices to the file */ 138385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1384958c9bccSBarry Smith if (!rank) { 13858e2fed03SBarry Smith MPI_Status status; 13866f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13878e2fed03SBarry Smith for (i=1; i<size; i++) { 1388639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1389ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1390e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1391ce94432eSBarry Smith ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 13926f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13938e2fed03SBarry Smith } 1394639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 13958e2fed03SBarry Smith } else { 1396639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1397ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1398ce94432eSBarry Smith ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1399639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 14008e2fed03SBarry Smith } 14018e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 14028e2fed03SBarry Smith 14038e2fed03SBarry Smith /* load up the local column values */ 14048e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 14058e2fed03SBarry Smith cnt = 0; 1406d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 14078e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 14088e2fed03SBarry Smith if (garray[B->j[j]] > cstart) break; 14098e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 14108e2fed03SBarry Smith } 14112205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k]; 14122205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j]; 14138e2fed03SBarry Smith } 1414e32f2f54SBarry 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); 14158e2fed03SBarry Smith 14168e2fed03SBarry Smith /* store the column values to the file */ 141785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1418958c9bccSBarry Smith if (!rank) { 14198e2fed03SBarry Smith MPI_Status status; 14206f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 14218e2fed03SBarry Smith for (i=1; i<size; i++) { 1422639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1423ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1424e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1425ce94432eSBarry Smith ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 14266f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 14278e2fed03SBarry Smith } 1428639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 14298e2fed03SBarry Smith } else { 1430639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1431ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1432ce94432eSBarry Smith ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1433639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 14348e2fed03SBarry Smith } 14358e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 1436b37d52dbSMark F. Adams 1437b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 14382205254eSKarl Rupp if (file) fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs); 14398e2fed03SBarry Smith PetscFunctionReturn(0); 14408e2fed03SBarry Smith } 14418e2fed03SBarry Smith 14429804daf3SBarry Smith #include <petscdraw.h> 14438e2fed03SBarry Smith #undef __FUNCT__ 14444a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1445dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1446416022c9SBarry Smith { 144744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1448dfbe8321SBarry Smith PetscErrorCode ierr; 144932dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1450ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1451b0a32e0cSBarry Smith PetscViewer sviewer; 1452f3ef73ceSBarry Smith PetscViewerFormat format; 1453416022c9SBarry Smith 14543a40ed3dSBarry Smith PetscFunctionBegin; 1455251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1456251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1457251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 145832077d6dSBarry Smith if (iascii) { 1459b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1460456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 14614e220ebcSLois Curfman McInnes MatInfo info; 1462ace3abfcSBarry Smith PetscBool inodes; 1463923f20ffSKris Buschelman 1464ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1465888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 14660298fd71SBarry Smith ierr = MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);CHKERRQ(ierr); 14677b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); 1468923f20ffSKris Buschelman if (!inodes) { 146977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1470d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 14716831982aSBarry Smith } else { 147277431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1473d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 14746831982aSBarry Smith } 1475888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 147677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1477888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 147877431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1479b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 14807b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); 148107d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1482a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 14833a40ed3dSBarry Smith PetscFunctionReturn(0); 1484fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1485923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1486923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1487923f20ffSKris Buschelman if (inodes) { 1488923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1489d38fa0fbSBarry Smith } else { 1490d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1491d38fa0fbSBarry Smith } 14923a40ed3dSBarry Smith PetscFunctionReturn(0); 14934aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 14944aedb280SBarry Smith PetscFunctionReturn(0); 149508480c60SBarry Smith } 14968e2fed03SBarry Smith } else if (isbinary) { 14978e2fed03SBarry Smith if (size == 1) { 14987adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 14998e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 15008e2fed03SBarry Smith } else { 15018e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 15028e2fed03SBarry Smith } 15038e2fed03SBarry Smith PetscFunctionReturn(0); 15040f5bd95cSBarry Smith } else if (isdraw) { 1505b0a32e0cSBarry Smith PetscDraw draw; 1506ace3abfcSBarry Smith PetscBool isnull; 1507b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1508b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 150919bcc07fSBarry Smith } 151019bcc07fSBarry Smith 151117699dbbSLois Curfman McInnes if (size == 1) { 15127adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 151378b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 15143a40ed3dSBarry Smith } else { 151595373324SBarry Smith /* assemble the entire matrix onto first processor. */ 151695373324SBarry Smith Mat A; 1517ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1518d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1519dd6ea824SBarry Smith MatScalar *a; 15202ee70a88SLois Curfman McInnes 152132a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1522ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 152332a366e4SMatthew Knepley 15240298fd71SBarry Smith ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,NULL);CHKERRQ(ierr); 1525ce94432eSBarry Smith if (!flg) SETERRQ(PetscObjectComm((PetscObject)mat),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."); 152632a366e4SMatthew Knepley } 15270805154bSBarry Smith 1528ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr); 152917699dbbSLois Curfman McInnes if (!rank) { 1530f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 15313a40ed3dSBarry Smith } else { 1532f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 153395373324SBarry Smith } 1534f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1535f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 15360298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);CHKERRQ(ierr); 15372b82e772SSatish Balay ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 153852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1539416022c9SBarry Smith 154095373324SBarry Smith /* copy over the A part */ 1541ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1542d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1543d0f46423SBarry Smith row = mat->rmap->rstart; 15442205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart; 154595373324SBarry Smith for (i=0; i<m; i++) { 1546416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 154726fbe8dcSKarl Rupp row++; 154826fbe8dcSKarl Rupp a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 154995373324SBarry Smith } 15502ee70a88SLois Curfman McInnes aj = Aloc->j; 15512205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart; 155295373324SBarry Smith 155395373324SBarry Smith /* copy over the B part */ 1554ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1555d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1556d0f46423SBarry Smith row = mat->rmap->rstart; 1557b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1558b0a32e0cSBarry Smith ct = cols; 15592205254eSKarl Rupp for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]]; 156095373324SBarry Smith for (i=0; i<m; i++) { 1561416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 15622205254eSKarl Rupp row++; 15632205254eSKarl Rupp a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 156495373324SBarry Smith } 1565606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 15666d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 15676d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 156855843e3eSBarry Smith /* 156955843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1570b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 157155843e3eSBarry Smith */ 1572b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1573e03a110bSBarry Smith if (!rank) { 15747adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 15757566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 15767566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 15776831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 157895373324SBarry Smith } 1579b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 15806bf464f9SBarry Smith ierr = MatDestroy(&A);CHKERRQ(ierr); 158195373324SBarry Smith } 15823a40ed3dSBarry Smith PetscFunctionReturn(0); 15831eb62cbbSBarry Smith } 15841eb62cbbSBarry Smith 15854a2ae208SSatish Balay #undef __FUNCT__ 15864a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1587dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1588416022c9SBarry Smith { 1589dfbe8321SBarry Smith PetscErrorCode ierr; 1590ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1591416022c9SBarry Smith 15923a40ed3dSBarry Smith PetscFunctionBegin; 1593251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1594251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1595251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1596251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 159732077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 15987b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 1599416022c9SBarry Smith } 16003a40ed3dSBarry Smith PetscFunctionReturn(0); 1601416022c9SBarry Smith } 1602416022c9SBarry Smith 16034a2ae208SSatish Balay #undef __FUNCT__ 160441f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 160541f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 16068a729477SBarry Smith { 160744a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1608dfbe8321SBarry Smith PetscErrorCode ierr; 16096987fefcSBarry Smith Vec bb1 = 0; 1610ace3abfcSBarry Smith PetscBool hasop; 16118a729477SBarry Smith 16123a40ed3dSBarry Smith PetscFunctionBegin; 1613a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 161441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1615a2b30743SBarry Smith PetscFunctionReturn(0); 1616a2b30743SBarry Smith } 1617a2b30743SBarry Smith 16184e980039SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 16194e980039SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 16204e980039SJed Brown } 16214e980039SJed Brown 1622c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) { 1623da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 162441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16252798e883SHong Zhang its--; 1626da3a660dSBarry Smith } 16272798e883SHong Zhang 16282798e883SHong Zhang while (its--) { 1629ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1630ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16312798e883SHong Zhang 1632c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1633efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1634c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 16352798e883SHong Zhang 1636c14dc6b6SHong Zhang /* local sweep */ 163741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16382798e883SHong Zhang } 16393a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP) { 1640da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 164141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16422798e883SHong Zhang its--; 1643da3a660dSBarry Smith } 16442798e883SHong Zhang while (its--) { 1645ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1646ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16472798e883SHong Zhang 1648c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1649efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1650c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1651c14dc6b6SHong Zhang 1652c14dc6b6SHong Zhang /* local sweep */ 165341f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16542798e883SHong Zhang } 16553a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) { 1656da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 165741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16582798e883SHong Zhang its--; 1659da3a660dSBarry Smith } 16602798e883SHong Zhang while (its--) { 1661ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1662ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16632798e883SHong Zhang 1664c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1665efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1666c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 16672798e883SHong Zhang 1668c14dc6b6SHong Zhang /* local sweep */ 166941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16702798e883SHong Zhang } 1671a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1672a7420bb7SBarry Smith Vec xx1; 1673a7420bb7SBarry Smith 1674a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 167541f059aeSBarry 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); 1676a7420bb7SBarry Smith 1677a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1678a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1679a7420bb7SBarry Smith if (!mat->diag) { 16800298fd71SBarry Smith ierr = MatGetVecs(matin,&mat->diag,NULL);CHKERRQ(ierr); 1681a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1682a7420bb7SBarry Smith } 1683bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1684bd0c2dcbSBarry Smith if (hasop) { 1685bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1686bd0c2dcbSBarry Smith } else { 1687a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1688bd0c2dcbSBarry Smith } 1689887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1690887ee2caSBarry Smith 1691a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1692a7420bb7SBarry Smith 1693a7420bb7SBarry Smith /* local sweep */ 169441f059aeSBarry 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); 1695a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 16966bf464f9SBarry Smith ierr = VecDestroy(&xx1);CHKERRQ(ierr); 1697ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported"); 1698c14dc6b6SHong Zhang 16996bf464f9SBarry Smith ierr = VecDestroy(&bb1);CHKERRQ(ierr); 17003a40ed3dSBarry Smith PetscFunctionReturn(0); 17018a729477SBarry Smith } 1702a66be287SLois Curfman McInnes 17034a2ae208SSatish Balay #undef __FUNCT__ 170442e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 170542e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 170642e855d1Svictor { 170772e6a0cfSJed Brown Mat aA,aB,Aperm; 170872e6a0cfSJed Brown const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj; 170972e6a0cfSJed Brown PetscScalar *aa,*ba; 171072e6a0cfSJed Brown PetscInt i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest; 171172e6a0cfSJed Brown PetscSF rowsf,sf; 17120298fd71SBarry Smith IS parcolp = NULL; 171372e6a0cfSJed Brown PetscBool done; 171442e855d1Svictor PetscErrorCode ierr; 171542e855d1Svictor 171642e855d1Svictor PetscFunctionBegin; 171772e6a0cfSJed Brown ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 171872e6a0cfSJed Brown ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr); 171972e6a0cfSJed Brown ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr); 172072e6a0cfSJed Brown ierr = PetscMalloc3(PetscMax(m,n),PetscInt,&work,m,PetscInt,&rdest,n,PetscInt,&cdest);CHKERRQ(ierr); 172172e6a0cfSJed Brown 172272e6a0cfSJed Brown /* Invert row permutation to find out where my rows should go */ 1723ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);CHKERRQ(ierr); 17240298fd71SBarry Smith ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr); 1725e9e74f11SJed Brown ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr); 172672e6a0cfSJed Brown for (i=0; i<m; i++) work[i] = A->rmap->rstart + i; 172772e6a0cfSJed Brown ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr); 172872e6a0cfSJed Brown ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr); 172972e6a0cfSJed Brown 173072e6a0cfSJed Brown /* Invert column permutation to find out where my columns should go */ 1731ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 17320298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr); 1733e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 173472e6a0cfSJed Brown for (i=0; i<n; i++) work[i] = A->cmap->rstart + i; 173572e6a0cfSJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr); 173672e6a0cfSJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr); 173772e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 173872e6a0cfSJed Brown 173972e6a0cfSJed Brown ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr); 174072e6a0cfSJed Brown ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr); 174172e6a0cfSJed Brown ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr); 174272e6a0cfSJed Brown 174372e6a0cfSJed Brown /* Find out where my gcols should go */ 17440298fd71SBarry Smith ierr = MatGetSize(aB,NULL,&ng);CHKERRQ(ierr); 174572e6a0cfSJed Brown ierr = PetscMalloc(ng*sizeof(PetscInt),&gcdest);CHKERRQ(ierr); 1746ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 17470298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr); 1748e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 174972e6a0cfSJed Brown ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 175072e6a0cfSJed Brown ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 175172e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 175272e6a0cfSJed Brown 175372e6a0cfSJed Brown ierr = PetscMalloc4(m,PetscInt,&dnnz,m,PetscInt,&onnz,m,PetscInt,&tdnnz,m,PetscInt,&tonnz);CHKERRQ(ierr); 175472e6a0cfSJed Brown ierr = PetscMemzero(dnnz,m*sizeof(PetscInt));CHKERRQ(ierr); 175572e6a0cfSJed Brown ierr = PetscMemzero(onnz,m*sizeof(PetscInt));CHKERRQ(ierr); 175672e6a0cfSJed Brown ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 175772e6a0cfSJed Brown ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 175872e6a0cfSJed Brown for (i=0; i<m; i++) { 175972e6a0cfSJed Brown PetscInt row = rdest[i],rowner; 176072e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr); 176172e6a0cfSJed Brown for (j=ai[i]; j<ai[i+1]; j++) { 176272e6a0cfSJed Brown PetscInt cowner,col = cdest[aj[j]]; 176372e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */ 176472e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 176572e6a0cfSJed Brown else onnz[i]++; 176672e6a0cfSJed Brown } 176772e6a0cfSJed Brown for (j=bi[i]; j<bi[i+1]; j++) { 176872e6a0cfSJed Brown PetscInt cowner,col = gcdest[bj[j]]; 176972e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); 177072e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 177172e6a0cfSJed Brown else onnz[i]++; 177272e6a0cfSJed Brown } 177372e6a0cfSJed Brown } 177472e6a0cfSJed Brown ierr = PetscMemzero(tdnnz,m*sizeof(PetscInt));CHKERRQ(ierr); 177572e6a0cfSJed Brown ierr = PetscMemzero(tonnz,m*sizeof(PetscInt));CHKERRQ(ierr); 177672e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 177772e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 177872e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 177972e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 178072e6a0cfSJed Brown ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr); 178172e6a0cfSJed Brown 1782ce94432eSBarry Smith ierr = MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);CHKERRQ(ierr); 178372e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr); 178472e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr); 178572e6a0cfSJed Brown for (i=0; i<m; i++) { 178672e6a0cfSJed Brown PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */ 178772e6a0cfSJed Brown PetscInt rowlen; 178872e6a0cfSJed Brown rowlen = ai[i+1] - ai[i]; 178972e6a0cfSJed Brown for (j=0; j<rowlen; j++) acols[j] = cdest[aj[ai[i]+j]]; 179072e6a0cfSJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,acols,aa+ai[i],INSERT_VALUES);CHKERRQ(ierr); 179172e6a0cfSJed Brown rowlen = bi[i+1] - bi[i]; 179272e6a0cfSJed Brown for (j=0; j<rowlen; j++) bcols[j] = gcdest[bj[bi[i]+j]]; 179372e6a0cfSJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,bcols,ba+bi[i],INSERT_VALUES);CHKERRQ(ierr); 179472e6a0cfSJed Brown } 179572e6a0cfSJed Brown ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 179672e6a0cfSJed Brown ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 179772e6a0cfSJed Brown ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 179872e6a0cfSJed Brown ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 179972e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr); 180072e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr); 180172e6a0cfSJed Brown ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr); 180272e6a0cfSJed Brown ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr); 180372e6a0cfSJed Brown ierr = PetscFree(gcdest);CHKERRQ(ierr); 180472e6a0cfSJed Brown if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);} 180572e6a0cfSJed Brown *B = Aperm; 180642e855d1Svictor PetscFunctionReturn(0); 180742e855d1Svictor } 180842e855d1Svictor 180942e855d1Svictor #undef __FUNCT__ 18104a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1811dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1812a66be287SLois Curfman McInnes { 1813a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1814a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1815dfbe8321SBarry Smith PetscErrorCode ierr; 1816329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1817a66be287SLois Curfman McInnes 18183a40ed3dSBarry Smith PetscFunctionBegin; 18194e220ebcSLois Curfman McInnes info->block_size = 1.0; 18204e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 18212205254eSKarl Rupp 18224e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 18234e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 18242205254eSKarl Rupp 18254e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 18262205254eSKarl Rupp 18274e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 18284e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1829a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 18304e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 18314e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 18324e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 18334e220ebcSLois Curfman McInnes info->memory = isend[3]; 18344e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1835a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1836ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 18372205254eSKarl Rupp 18384e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 18394e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 18404e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 18414e220ebcSLois Curfman McInnes info->memory = irecv[3]; 18424e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1843a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1844ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 18452205254eSKarl Rupp 18464e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 18474e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 18484e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 18494e220ebcSLois Curfman McInnes info->memory = irecv[3]; 18504e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1851a66be287SLois Curfman McInnes } 18524e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 18534e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 18544e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 18553a40ed3dSBarry Smith PetscFunctionReturn(0); 1856a66be287SLois Curfman McInnes } 1857a66be287SLois Curfman McInnes 18584a2ae208SSatish Balay #undef __FUNCT__ 18594a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1860ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1861c74985f6SBarry Smith { 1862c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1863dfbe8321SBarry Smith PetscErrorCode ierr; 1864c74985f6SBarry Smith 18653a40ed3dSBarry Smith PetscFunctionBegin; 186612c028f9SKris Buschelman switch (op) { 1867512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 186812c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 186928b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1870a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 187112c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 187212c028f9SKris Buschelman case MAT_USE_INODES: 187312c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 1874fa1f0d2cSMatthew G Knepley MatCheckPreallocated(A,1); 18754e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 18764e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 187712c028f9SKris Buschelman break; 187812c028f9SKris Buschelman case MAT_ROW_ORIENTED: 18794e0d8c25SBarry Smith a->roworiented = flg; 18802205254eSKarl Rupp 18814e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 18824e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 188312c028f9SKris Buschelman break; 18844e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1885290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 188612c028f9SKris Buschelman break; 188712c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 18885c0f0b64SBarry Smith a->donotstash = flg; 188912c028f9SKris Buschelman break; 1890ffa07934SHong Zhang case MAT_SPD: 1891ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1892ffa07934SHong Zhang A->spd = flg; 1893ffa07934SHong Zhang if (flg) { 1894ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1895ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1896ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1897ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1898ffa07934SHong Zhang } 1899ffa07934SHong Zhang break; 190077e54ba9SKris Buschelman case MAT_SYMMETRIC: 19014e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 190225f421beSHong Zhang break; 190377e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1904eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1905eeffb40dSHong Zhang break; 1906bf108f30SBarry Smith case MAT_HERMITIAN: 1907eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1908eeffb40dSHong Zhang break; 1909bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 19104e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 191177e54ba9SKris Buschelman break; 191212c028f9SKris Buschelman default: 1913e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 19143a40ed3dSBarry Smith } 19153a40ed3dSBarry Smith PetscFunctionReturn(0); 1916c74985f6SBarry Smith } 1917c74985f6SBarry Smith 19184a2ae208SSatish Balay #undef __FUNCT__ 19194a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1920b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 192139e00950SLois Curfman McInnes { 1922154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 192387828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 19246849ba73SBarry Smith PetscErrorCode ierr; 1925d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1926d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1927b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 192839e00950SLois Curfman McInnes 19293a40ed3dSBarry Smith PetscFunctionBegin; 1930e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 19317a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 19327a0afa10SBarry Smith 193370f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 19347a0afa10SBarry Smith /* 19357a0afa10SBarry Smith allocate enough space to hold information from the longest row. 19367a0afa10SBarry Smith */ 19377a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1938b1d57f15SBarry Smith PetscInt max = 1,tmp; 1939d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 19407a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 19412205254eSKarl Rupp if (max < tmp) max = tmp; 19427a0afa10SBarry Smith } 19431d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 19447a0afa10SBarry Smith } 19457a0afa10SBarry Smith 1946e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1947abc0e9e4SLois Curfman McInnes lrow = row - rstart; 194839e00950SLois Curfman McInnes 1949154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1950154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1951154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1952f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1953f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1954154123eaSLois Curfman McInnes nztot = nzA + nzB; 1955154123eaSLois Curfman McInnes 195670f0671dSBarry Smith cmap = mat->garray; 1957154123eaSLois Curfman McInnes if (v || idx) { 1958154123eaSLois Curfman McInnes if (nztot) { 1959154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1960b1d57f15SBarry Smith PetscInt imark = -1; 1961154123eaSLois Curfman McInnes if (v) { 196270f0671dSBarry Smith *v = v_p = mat->rowvalues; 196339e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 196470f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1965154123eaSLois Curfman McInnes else break; 1966154123eaSLois Curfman McInnes } 1967154123eaSLois Curfman McInnes imark = i; 196870f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 196970f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1970154123eaSLois Curfman McInnes } 1971154123eaSLois Curfman McInnes if (idx) { 197270f0671dSBarry Smith *idx = idx_p = mat->rowindices; 197370f0671dSBarry Smith if (imark > -1) { 197470f0671dSBarry Smith for (i=0; i<imark; i++) { 197570f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 197670f0671dSBarry Smith } 197770f0671dSBarry Smith } else { 1978154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 197970f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1980154123eaSLois Curfman McInnes else break; 1981154123eaSLois Curfman McInnes } 1982154123eaSLois Curfman McInnes imark = i; 198370f0671dSBarry Smith } 198470f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 198570f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 198639e00950SLois Curfman McInnes } 19873f97c4b0SBarry Smith } else { 19881ca473b0SSatish Balay if (idx) *idx = 0; 19891ca473b0SSatish Balay if (v) *v = 0; 19901ca473b0SSatish Balay } 1991154123eaSLois Curfman McInnes } 199239e00950SLois Curfman McInnes *nz = nztot; 1993f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1994f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 19953a40ed3dSBarry Smith PetscFunctionReturn(0); 199639e00950SLois Curfman McInnes } 199739e00950SLois Curfman McInnes 19984a2ae208SSatish Balay #undef __FUNCT__ 19994a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 2000b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 200139e00950SLois Curfman McInnes { 20027a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 20033a40ed3dSBarry Smith 20043a40ed3dSBarry Smith PetscFunctionBegin; 2005e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 20067a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 20073a40ed3dSBarry Smith PetscFunctionReturn(0); 200839e00950SLois Curfman McInnes } 200939e00950SLois Curfman McInnes 20104a2ae208SSatish Balay #undef __FUNCT__ 20114a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 2012dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 2013855ac2c5SLois Curfman McInnes { 2014855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2015ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 2016dfbe8321SBarry Smith PetscErrorCode ierr; 2017d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 2018329f5518SBarry Smith PetscReal sum = 0.0; 2019a77337e4SBarry Smith MatScalar *v; 202004ca555eSLois Curfman McInnes 20213a40ed3dSBarry Smith PetscFunctionBegin; 202217699dbbSLois Curfman McInnes if (aij->size == 1) { 202314183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 202437fa93a5SLois Curfman McInnes } else { 202504ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 202604ca555eSLois Curfman McInnes v = amat->a; 202704ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 2028329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 202904ca555eSLois Curfman McInnes } 203004ca555eSLois Curfman McInnes v = bmat->a; 203104ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 2032329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 203304ca555eSLois Curfman McInnes } 2034ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 20358f1a2a5eSBarry Smith *norm = PetscSqrtReal(*norm); 20363a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 2037329f5518SBarry Smith PetscReal *tmp,*tmp2; 2038b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 2039d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 2040d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 2041d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 204204ca555eSLois Curfman McInnes *norm = 0.0; 204304ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 204404ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 2045bfec09a0SHong Zhang tmp[cstart + *jj++] += PetscAbsScalar(*v); v++; 204604ca555eSLois Curfman McInnes } 204704ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 204804ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 2049bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 205004ca555eSLois Curfman McInnes } 2051ce94432eSBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 2052d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 205304ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 205404ca555eSLois Curfman McInnes } 2055606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 2056606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 20573a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 2058329f5518SBarry Smith PetscReal ntemp = 0.0; 2059d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 2060bfec09a0SHong Zhang v = amat->a + amat->i[j]; 206104ca555eSLois Curfman McInnes sum = 0.0; 206204ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 2063cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 206404ca555eSLois Curfman McInnes } 2065bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 206604ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 2067cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 206804ca555eSLois Curfman McInnes } 2069515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 207004ca555eSLois Curfman McInnes } 2071ce94432eSBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 2072ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm"); 207337fa93a5SLois Curfman McInnes } 20743a40ed3dSBarry Smith PetscFunctionReturn(0); 2075855ac2c5SLois Curfman McInnes } 2076855ac2c5SLois Curfman McInnes 20774a2ae208SSatish Balay #undef __FUNCT__ 20784a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 2079fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 2080b7c46309SBarry Smith { 2081b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2082da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 2083dfbe8321SBarry Smith PetscErrorCode ierr; 208480bcc5a1SJed Brown PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i; 2085d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart,ncol; 20863a40ed3dSBarry Smith Mat B; 2087a77337e4SBarry Smith MatScalar *array; 2088b7c46309SBarry Smith 20893a40ed3dSBarry Smith PetscFunctionBegin; 2090ce94432eSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 2091da668accSHong Zhang 209280bcc5a1SJed Brown ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n; 2093da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 2094da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 2095fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 209680bcc5a1SJed Brown PetscInt *d_nnz,*g_nnz,*o_nnz; 209780bcc5a1SJed Brown PetscSFNode *oloc; 2098713c93b4SJed Brown PETSC_UNUSED PetscSF sf; 209980bcc5a1SJed Brown 210080bcc5a1SJed Brown ierr = PetscMalloc4(na,PetscInt,&d_nnz,na,PetscInt,&o_nnz,nb,PetscInt,&g_nnz,nb,PetscSFNode,&oloc);CHKERRQ(ierr); 210180bcc5a1SJed Brown /* compute d_nnz for preallocation */ 210280bcc5a1SJed Brown ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 2103da668accSHong Zhang for (i=0; i<ai[ma]; i++) { 2104da668accSHong Zhang d_nnz[aj[i]]++; 2105da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 2106d4bb536fSBarry Smith } 210780bcc5a1SJed Brown /* compute local off-diagonal contributions */ 21080beca09bSJed Brown ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr); 210980bcc5a1SJed Brown for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++; 211080bcc5a1SJed Brown /* map those to global */ 2111ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 21120298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr); 2113e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 211480bcc5a1SJed Brown ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 211580bcc5a1SJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 211680bcc5a1SJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 211780bcc5a1SJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 2118d4bb536fSBarry Smith 2119ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr); 2120d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 2121a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr); 21227adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 212380bcc5a1SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 212480bcc5a1SJed Brown ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr); 2125fc4dec0aSBarry Smith } else { 2126fc4dec0aSBarry Smith B = *matout; 21276ffab4bbSHong Zhang ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 21282205254eSKarl Rupp for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */ 2129fc4dec0aSBarry Smith } 2130b7c46309SBarry Smith 2131b7c46309SBarry Smith /* copy over the A part */ 2132da668accSHong Zhang array = Aloc->a; 2133d0f46423SBarry Smith row = A->rmap->rstart; 2134da668accSHong Zhang for (i=0; i<ma; i++) { 2135da668accSHong Zhang ncol = ai[i+1]-ai[i]; 2136da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 21372205254eSKarl Rupp row++; 21382205254eSKarl Rupp array += ncol; aj += ncol; 2139b7c46309SBarry Smith } 2140b7c46309SBarry Smith aj = Aloc->j; 2141da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 2142b7c46309SBarry Smith 2143b7c46309SBarry Smith /* copy over the B part */ 2144fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 2145fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 2146da668accSHong Zhang array = Bloc->a; 2147d0f46423SBarry Smith row = A->rmap->rstart; 21482205254eSKarl Rupp for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]]; 214961a2fbbaSHong Zhang cols_tmp = cols; 2150da668accSHong Zhang for (i=0; i<mb; i++) { 2151da668accSHong Zhang ncol = bi[i+1]-bi[i]; 215261a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 21532205254eSKarl Rupp row++; 21542205254eSKarl Rupp array += ncol; cols_tmp += ncol; 2155b7c46309SBarry Smith } 2156fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 2157fc73b1b3SBarry Smith 21586d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21596d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2160815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 21610de55854SLois Curfman McInnes *matout = B; 21620de55854SLois Curfman McInnes } else { 2163eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 21640de55854SLois Curfman McInnes } 21653a40ed3dSBarry Smith PetscFunctionReturn(0); 2166b7c46309SBarry Smith } 2167b7c46309SBarry Smith 21684a2ae208SSatish Balay #undef __FUNCT__ 21694a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 2170dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 2171a008b906SSatish Balay { 21724b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 21734b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 2174dfbe8321SBarry Smith PetscErrorCode ierr; 2175b1d57f15SBarry Smith PetscInt s1,s2,s3; 2176a008b906SSatish Balay 21773a40ed3dSBarry Smith PetscFunctionBegin; 21784b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 21794b967eb1SSatish Balay if (rr) { 2180e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 2181e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 21824b967eb1SSatish Balay /* Overlap communication with computation. */ 2183ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2184a008b906SSatish Balay } 21854b967eb1SSatish Balay if (ll) { 2186e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 2187e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 2188f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 21894b967eb1SSatish Balay } 21904b967eb1SSatish Balay /* scale the diagonal block */ 2191f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 21924b967eb1SSatish Balay 21934b967eb1SSatish Balay if (rr) { 21944b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 2195ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2196f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 21974b967eb1SSatish Balay } 21983a40ed3dSBarry Smith PetscFunctionReturn(0); 2199a008b906SSatish Balay } 2200a008b906SSatish Balay 22014a2ae208SSatish Balay #undef __FUNCT__ 22024a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 2203dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 2204bb5a7306SBarry Smith { 2205bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2206dfbe8321SBarry Smith PetscErrorCode ierr; 22073a40ed3dSBarry Smith 22083a40ed3dSBarry Smith PetscFunctionBegin; 2209bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 22103a40ed3dSBarry Smith PetscFunctionReturn(0); 2211bb5a7306SBarry Smith } 2212bb5a7306SBarry Smith 22134a2ae208SSatish Balay #undef __FUNCT__ 22144a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2215ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2216d4bb536fSBarry Smith { 2217d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2218d4bb536fSBarry Smith Mat a,b,c,d; 2219ace3abfcSBarry Smith PetscBool flg; 2220dfbe8321SBarry Smith PetscErrorCode ierr; 2221d4bb536fSBarry Smith 22223a40ed3dSBarry Smith PetscFunctionBegin; 2223d4bb536fSBarry Smith a = matA->A; b = matA->B; 2224d4bb536fSBarry Smith c = matB->A; d = matB->B; 2225d4bb536fSBarry Smith 2226d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2227abc0a331SBarry Smith if (flg) { 2228d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2229d4bb536fSBarry Smith } 2230ce94432eSBarry Smith ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 22313a40ed3dSBarry Smith PetscFunctionReturn(0); 2232d4bb536fSBarry Smith } 2233d4bb536fSBarry Smith 22344a2ae208SSatish Balay #undef __FUNCT__ 22354a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2236dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2237cb5b572fSBarry Smith { 2238dfbe8321SBarry Smith PetscErrorCode ierr; 2239cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2240cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ*)B->data; 2241cb5b572fSBarry Smith 2242cb5b572fSBarry Smith PetscFunctionBegin; 224333f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 224433f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2245cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2246cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2247cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2248cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2249cb5b572fSBarry Smith then copying the submatrices */ 2250cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2251cb5b572fSBarry Smith } else { 2252cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2253cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2254cb5b572fSBarry Smith } 2255cb5b572fSBarry Smith PetscFunctionReturn(0); 2256cb5b572fSBarry Smith } 2257cb5b572fSBarry Smith 22584a2ae208SSatish Balay #undef __FUNCT__ 22594994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ" 22604994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A) 2261273d9f13SBarry Smith { 2262dfbe8321SBarry Smith PetscErrorCode ierr; 2263273d9f13SBarry Smith 2264273d9f13SBarry Smith PetscFunctionBegin; 2265273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2266273d9f13SBarry Smith PetscFunctionReturn(0); 2267273d9f13SBarry Smith } 2268273d9f13SBarry Smith 2269ac90fabeSBarry Smith #undef __FUNCT__ 227095b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ" 227195b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */ 227295b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz) 227395b7e79eSJed Brown { 227495b7e79eSJed Brown PetscInt i,m=Y->rmap->N; 227595b7e79eSJed Brown Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 227695b7e79eSJed Brown Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 227795b7e79eSJed Brown const PetscInt *xi = x->i,*yi = y->i; 227895b7e79eSJed Brown 227995b7e79eSJed Brown PetscFunctionBegin; 228095b7e79eSJed Brown /* Set the number of nonzeros in the new matrix */ 228195b7e79eSJed Brown for (i=0; i<m; i++) { 228295b7e79eSJed Brown PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i]; 228395b7e79eSJed Brown const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i]; 228495b7e79eSJed Brown nnz[i] = 0; 228595b7e79eSJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 228695b7e79eSJed Brown for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */ 228795b7e79eSJed Brown if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++; /* Skip duplicate */ 228895b7e79eSJed Brown nnz[i]++; 228995b7e79eSJed Brown } 229095b7e79eSJed Brown for (; k<nzy; k++) nnz[i]++; 229195b7e79eSJed Brown } 229295b7e79eSJed Brown PetscFunctionReturn(0); 229395b7e79eSJed Brown } 229495b7e79eSJed Brown 229595b7e79eSJed Brown #undef __FUNCT__ 2296ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2297f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2298ac90fabeSBarry Smith { 2299dfbe8321SBarry Smith PetscErrorCode ierr; 2300b1d57f15SBarry Smith PetscInt i; 2301ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data; 23024ce68768SBarry Smith PetscBLASInt bnz,one=1; 2303ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2304ac90fabeSBarry Smith 2305ac90fabeSBarry Smith PetscFunctionBegin; 2306ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2307f4df32b1SMatthew Knepley PetscScalar alpha = a; 2308ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->A->data; 2309c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2310ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->A->data; 2311a83cb05cSBarry Smith PetscStackCall("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2312ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->B->data; 2313ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->B->data; 2314c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2315a83cb05cSBarry Smith PetscStackCall("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2316a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2317f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2318c537a176SHong Zhang 2319c537a176SHong Zhang x = (Mat_SeqAIJ*)xx->B->data; 2320a30b2313SHong Zhang y = (Mat_SeqAIJ*)yy->B->data; 2321a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2322a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 23236bf464f9SBarry Smith ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr); 2324c537a176SHong Zhang } 2325a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2326d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2327a30b2313SHong Zhang y->XtoY = xx->B; 2328407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2329c537a176SHong Zhang } 2330f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2331ac90fabeSBarry Smith } else { 23329f5f6813SShri Abhyankar Mat B; 23339f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 23349f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 23359f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 2336ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 2337bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 23389f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 2339a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr); 23409f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 23419f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 234295b7e79eSJed Brown ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr); 2343ecd8bba6SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr); 23449f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 2345a2ea699eSBarry Smith ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr); 23469f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 23479f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2348ac90fabeSBarry Smith } 2349ac90fabeSBarry Smith PetscFunctionReturn(0); 2350ac90fabeSBarry Smith } 2351ac90fabeSBarry Smith 23527087cfbeSBarry Smith extern PetscErrorCode MatConjugate_SeqAIJ(Mat); 2353354c94deSBarry Smith 2354354c94deSBarry Smith #undef __FUNCT__ 2355354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 23567087cfbeSBarry Smith PetscErrorCode MatConjugate_MPIAIJ(Mat mat) 2357354c94deSBarry Smith { 2358354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2359354c94deSBarry Smith PetscErrorCode ierr; 2360354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2361354c94deSBarry Smith 2362354c94deSBarry Smith PetscFunctionBegin; 2363354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2364354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2365354c94deSBarry Smith #else 2366354c94deSBarry Smith PetscFunctionBegin; 2367354c94deSBarry Smith #endif 2368354c94deSBarry Smith PetscFunctionReturn(0); 2369354c94deSBarry Smith } 2370354c94deSBarry Smith 237199cafbc1SBarry Smith #undef __FUNCT__ 237299cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 237399cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 237499cafbc1SBarry Smith { 237599cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 237699cafbc1SBarry Smith PetscErrorCode ierr; 237799cafbc1SBarry Smith 237899cafbc1SBarry Smith PetscFunctionBegin; 237999cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 238099cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 238199cafbc1SBarry Smith PetscFunctionReturn(0); 238299cafbc1SBarry Smith } 238399cafbc1SBarry Smith 238499cafbc1SBarry Smith #undef __FUNCT__ 238599cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 238699cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 238799cafbc1SBarry Smith { 238899cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 238999cafbc1SBarry Smith PetscErrorCode ierr; 239099cafbc1SBarry Smith 239199cafbc1SBarry Smith PetscFunctionBegin; 239299cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 239399cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 239499cafbc1SBarry Smith PetscFunctionReturn(0); 239599cafbc1SBarry Smith } 239699cafbc1SBarry Smith 2397519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 2398103bf8bdSMatthew Knepley 2399103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2400a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2401a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2402a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2403103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2404a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2405d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2406103bf8bdSMatthew Knepley 2407103bf8bdSMatthew Knepley #undef __FUNCT__ 2408103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2409103bf8bdSMatthew Knepley /* 2410103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2411103bf8bdSMatthew Knepley */ 24120481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2413103bf8bdSMatthew Knepley { 2414a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2415a2c909beSMatthew Knepley 2416a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2417a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2418a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2419a2c909beSMatthew Knepley 2420ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2421776b82aeSLisandro Dalcin PetscContainer c; 2422103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2423103bf8bdSMatthew Knepley PetscErrorCode ierr; 2424103bf8bdSMatthew Knepley 2425103bf8bdSMatthew Knepley PetscFunctionBegin; 2426e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2427103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2428103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2429f23aa3ddSBarry Smith if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2430103bf8bdSMatthew Knepley 2431103bf8bdSMatthew Knepley process_group_type pg; 2432a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2433a2c909beSMatthew Knepley lgraph_type *lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2434a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2435a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2436a2c909beSMatthew Knepley 2437103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2438a2c909beSMatthew Knepley ilu_permuted(level_graph); 2439103bf8bdSMatthew Knepley 2440103bf8bdSMatthew Knepley /* put together the new matrix */ 2441ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A), fact);CHKERRQ(ierr); 2442103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2443103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2444719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2445a2f3521dSMark F. Adams ierr = MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 2446719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2447719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2448719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2449103bf8bdSMatthew Knepley 2450ce94432eSBarry Smith ierr = PetscContainerCreate(PetscObjectComm((PetscObject)A), &c); 2451776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2452719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2453bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&c); 2454103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2455103bf8bdSMatthew Knepley } 2456103bf8bdSMatthew Knepley 2457103bf8bdSMatthew Knepley #undef __FUNCT__ 2458103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 24590481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2460103bf8bdSMatthew Knepley { 2461103bf8bdSMatthew Knepley PetscFunctionBegin; 2462103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2463103bf8bdSMatthew Knepley } 2464103bf8bdSMatthew Knepley 2465103bf8bdSMatthew Knepley #undef __FUNCT__ 2466103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2467103bf8bdSMatthew Knepley /* 2468103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2469103bf8bdSMatthew Knepley */ 2470103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2471103bf8bdSMatthew Knepley { 2472a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2473a2c909beSMatthew Knepley 2474a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2475a2c909beSMatthew Knepley lgraph_type *lgraph_p; 2476776b82aeSLisandro Dalcin PetscContainer c; 2477103bf8bdSMatthew Knepley PetscErrorCode ierr; 2478103bf8bdSMatthew Knepley 2479103bf8bdSMatthew Knepley PetscFunctionBegin; 2480103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);CHKERRQ(ierr); 2481776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void**) &lgraph_p);CHKERRQ(ierr); 2482103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2483a2c909beSMatthew Knepley 2484a2c909beSMatthew Knepley PetscScalar *array_x; 2485a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2486a2c909beSMatthew Knepley PetscInt sx; 2487a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2488a2c909beSMatthew Knepley 2489a2c909beSMatthew Knepley PetscScalar *array_b; 2490a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2491a2c909beSMatthew Knepley PetscInt sb; 2492a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2493a2c909beSMatthew Knepley 2494a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2495a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2496a2c909beSMatthew Knepley 2497a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 24982205254eSKarl Rupp array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]); 24992205254eSKarl Rupp array_ref_type ref_x(array_x, boost::extents[num_vertices(graph)]); 2500a2c909beSMatthew Knepley 2501a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2502a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 25032205254eSKarl Rupp gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)); 25042205254eSKarl Rupp gvector_type vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2505a2c909beSMatthew Knepley 2506a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2507103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2508103bf8bdSMatthew Knepley } 2509103bf8bdSMatthew Knepley #endif 2510103bf8bdSMatthew Knepley 251169db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 251269db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 25131d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 25141d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 251569db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 2516bf0cc555SLisandro Dalcin PetscErrorCode (*Destroy)(Mat); 251769db28dcSHong Zhang } Mat_Redundant; 251869db28dcSHong Zhang 251969db28dcSHong Zhang #undef __FUNCT__ 252069db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 252169db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 252269db28dcSHong Zhang { 252369db28dcSHong Zhang PetscErrorCode ierr; 252469db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 252569db28dcSHong Zhang PetscInt i; 252669db28dcSHong Zhang 252769db28dcSHong Zhang PetscFunctionBegin; 25281d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 252969db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 253069db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 253169db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++) { 253269db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 253369db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 253469db28dcSHong Zhang } 25351d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 253669db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 253769db28dcSHong Zhang PetscFunctionReturn(0); 253869db28dcSHong Zhang } 253969db28dcSHong Zhang 254069db28dcSHong Zhang #undef __FUNCT__ 254169db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 254269db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 254369db28dcSHong Zhang { 254469db28dcSHong Zhang PetscErrorCode ierr; 254569db28dcSHong Zhang PetscContainer container; 25460298fd71SBarry Smith Mat_Redundant *redund=NULL; 254769db28dcSHong Zhang 254869db28dcSHong Zhang PetscFunctionBegin; 254969db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject*)&container);CHKERRQ(ierr); 2550bf0cc555SLisandro Dalcin if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 255169db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void**)&redund);CHKERRQ(ierr); 25522205254eSKarl Rupp 2553bf0cc555SLisandro Dalcin A->ops->destroy = redund->Destroy; 25542205254eSKarl Rupp 255569db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 2556bf0cc555SLisandro Dalcin if (A->ops->destroy) { 255769db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 2558bf0cc555SLisandro Dalcin } 255969db28dcSHong Zhang PetscFunctionReturn(0); 256069db28dcSHong Zhang } 256169db28dcSHong Zhang 256269db28dcSHong Zhang #undef __FUNCT__ 256369db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 256469db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 256569db28dcSHong Zhang { 256669db28dcSHong Zhang PetscMPIInt rank,size; 2567ce94432eSBarry Smith MPI_Comm comm; 256869db28dcSHong Zhang PetscErrorCode ierr; 256969db28dcSHong Zhang PetscInt nsends = 0,nrecvs=0,i,rownz_max=0; 25700298fd71SBarry Smith PetscMPIInt *send_rank= NULL,*recv_rank=NULL; 2571d0f46423SBarry Smith PetscInt *rowrange = mat->rmap->range; 257269db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 257369db28dcSHong Zhang Mat A = aij->A,B=aij->B,C=*matredundant; 257469db28dcSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 257569db28dcSHong Zhang PetscScalar *sbuf_a; 257669db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2577d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2578d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 257969db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2580a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2581a77337e4SBarry Smith PetscScalar *vals; 258269db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 25830298fd71SBarry Smith MPI_Request *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL; 25840298fd71SBarry Smith MPI_Request *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL; 258569db28dcSHong Zhang MPI_Status recv_status,*send_status; 25860298fd71SBarry Smith PetscInt *sbuf_nz=NULL,*rbuf_nz=NULL,count; 25870298fd71SBarry Smith PetscInt **rbuf_j=NULL; 25880298fd71SBarry Smith PetscScalar **rbuf_a=NULL; 25890298fd71SBarry Smith Mat_Redundant *redund =NULL; 259069db28dcSHong Zhang PetscContainer container; 259169db28dcSHong Zhang 259269db28dcSHong Zhang PetscFunctionBegin; 2593ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 259469db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 259569db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 259669db28dcSHong Zhang 259769db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 259869db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2599e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 260069db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2601e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 260269db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject*)&container);CHKERRQ(ierr); 2603bf0cc555SLisandro Dalcin if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 260469db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void**)&redund);CHKERRQ(ierr); 2605e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 260669db28dcSHong Zhang 260769db28dcSHong Zhang nsends = redund->nsends; 260869db28dcSHong Zhang nrecvs = redund->nrecvs; 26091d79065fSBarry Smith send_rank = redund->send_rank; 26101d79065fSBarry Smith recv_rank = redund->recv_rank; 26111d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 26121d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 261369db28dcSHong Zhang sbuf_j = redund->sbuf_j; 261469db28dcSHong Zhang sbuf_a = redund->sbuf_a; 261569db28dcSHong Zhang rbuf_j = redund->rbuf_j; 261669db28dcSHong Zhang rbuf_a = redund->rbuf_a; 261769db28dcSHong Zhang } 261869db28dcSHong Zhang 261969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 262069db28dcSHong Zhang PetscMPIInt subrank,subsize; 262169db28dcSHong Zhang PetscInt nleftover,np_subcomm; 262269db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 262369db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 262469db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2625a2ea699eSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank);CHKERRQ(ierr); 26262205254eSKarl Rupp 262769db28dcSHong Zhang np_subcomm = size/nsubcomm; 262869db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 26292205254eSKarl Rupp 263069db28dcSHong Zhang nsends = 0; nrecvs = 0; 263169db28dcSHong Zhang for (i=0; i<size; i++) { /* i=rank*/ 263269db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i) { /* my_subrank == other's subrank */ 263369db28dcSHong Zhang send_rank[nsends] = i; nsends++; 263469db28dcSHong Zhang recv_rank[nrecvs++] = i; 263569db28dcSHong Zhang } 263669db28dcSHong Zhang } 263769db28dcSHong Zhang if (rank >= size - nleftover) { /* this proc is a leftover processor */ 263869db28dcSHong Zhang i = size-nleftover-1; 263969db28dcSHong Zhang j = 0; 264069db28dcSHong Zhang while (j < nsubcomm - nleftover) { 264169db28dcSHong Zhang send_rank[nsends++] = i; 264269db28dcSHong Zhang i--; j++; 264369db28dcSHong Zhang } 264469db28dcSHong Zhang } 264569db28dcSHong Zhang 264669db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */ 264769db28dcSHong Zhang for (i=0; i<nleftover; i++) { 264869db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 264969db28dcSHong Zhang } 265069db28dcSHong Zhang } 265169db28dcSHong Zhang 265269db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 265369db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 265469db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 265569db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 265669db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 265769db28dcSHong Zhang 265869db28dcSHong Zhang /* copy mat's local entries into the buffers */ 265969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 266069db28dcSHong Zhang rownz_max = 0; 266169db28dcSHong Zhang rptr = sbuf_j; 266269db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 266369db28dcSHong Zhang vals = sbuf_a; 266469db28dcSHong Zhang rptr[0] = 0; 266569db28dcSHong Zhang for (i=0; i<rend-rstart; i++) { 266669db28dcSHong Zhang row = i + rstart; 266769db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 266869db28dcSHong Zhang ncols = nzA + nzB; 266969db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 267069db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 267169db28dcSHong Zhang /* load the column indices for this row into cols */ 267269db28dcSHong Zhang lwrite = 0; 267369db28dcSHong Zhang for (l=0; l<nzB; l++) { 267469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) { 267569db28dcSHong Zhang vals[lwrite] = aworkB[l]; 267669db28dcSHong Zhang cols[lwrite++] = ctmp; 267769db28dcSHong Zhang } 267869db28dcSHong Zhang } 267969db28dcSHong Zhang for (l=0; l<nzA; l++) { 268069db28dcSHong Zhang vals[lwrite] = aworkA[l]; 268169db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 268269db28dcSHong Zhang } 268369db28dcSHong Zhang for (l=0; l<nzB; l++) { 268469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) { 268569db28dcSHong Zhang vals[lwrite] = aworkB[l]; 268669db28dcSHong Zhang cols[lwrite++] = ctmp; 268769db28dcSHong Zhang } 268869db28dcSHong Zhang } 268969db28dcSHong Zhang vals += ncols; 269069db28dcSHong Zhang cols += ncols; 269169db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 269269db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 269369db28dcSHong Zhang } 2694e32f2f54SBarry 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); 269569db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 269669db28dcSHong Zhang rptr = sbuf_j; 269769db28dcSHong Zhang vals = sbuf_a; 269869db28dcSHong Zhang rptr[0] = 0; 269969db28dcSHong Zhang for (i=0; i<rend-rstart; i++) { 270069db28dcSHong Zhang row = i + rstart; 270169db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 270269db28dcSHong Zhang ncols = nzA + nzB; 2703a2ea699eSBarry Smith cworkB = b->j + b->i[i]; 2704a2ea699eSBarry Smith aworkA = a->a + a->i[i]; 2705a2ea699eSBarry Smith aworkB = b->a + b->i[i]; 270669db28dcSHong Zhang lwrite = 0; 270769db28dcSHong Zhang for (l=0; l<nzB; l++) { 270869db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 270969db28dcSHong Zhang } 271069db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 271169db28dcSHong Zhang for (l=0; l<nzB; l++) { 271269db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 271369db28dcSHong Zhang } 271469db28dcSHong Zhang vals += ncols; 271569db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 271669db28dcSHong Zhang } 271769db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 271869db28dcSHong Zhang 271969db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 272069db28dcSHong Zhang /*--------------------------------------------------*/ 272169db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 272269db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 27232205254eSKarl Rupp 272469db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 272569db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 272669db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 272769db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 272869db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 272969db28dcSHong Zhang } else { 273069db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 27312205254eSKarl Rupp 273269db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 273369db28dcSHong Zhang } 273469db28dcSHong Zhang 273569db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 273669db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 273769db28dcSHong Zhang /* get new tags to keep the communication clean */ 273869db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 273969db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 27401d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 274169db28dcSHong Zhang 274269db28dcSHong Zhang /* post receives of other's nzlocal */ 274369db28dcSHong Zhang for (i=0; i<nrecvs; i++) { 274469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 274569db28dcSHong Zhang } 274669db28dcSHong Zhang /* send nzlocal to others */ 274769db28dcSHong Zhang for (i=0; i<nsends; i++) { 274869db28dcSHong Zhang sbuf_nz[i] = nzlocal; 274969db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 275069db28dcSHong Zhang } 275169db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 275269db28dcSHong Zhang count = nrecvs; 275369db28dcSHong Zhang while (count) { 275469db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 275526fbe8dcSKarl Rupp 275669db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 275769db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 275869db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 275969db28dcSHong Zhang 276069db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 27612205254eSKarl Rupp 276269db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 27632205254eSKarl Rupp 276469db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 276569db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 276669db28dcSHong Zhang count--; 276769db28dcSHong Zhang } 276869db28dcSHong Zhang /* wait on sends of nzlocal */ 276969db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 277069db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 277169db28dcSHong Zhang /*------------------------------------------------*/ 277269db28dcSHong Zhang for (i=0; i<nsends; i++) { 277369db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 277469db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 277569db28dcSHong Zhang } 277669db28dcSHong Zhang /* wait on receives of mat->i,j */ 277769db28dcSHong Zhang /*------------------------------*/ 277869db28dcSHong Zhang count = nrecvs; 277969db28dcSHong Zhang while (count) { 278069db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2781e32f2f54SBarry 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); 278269db28dcSHong Zhang count--; 278369db28dcSHong Zhang } 278469db28dcSHong Zhang /* wait on sends of mat->i,j */ 278569db28dcSHong Zhang /*---------------------------*/ 278669db28dcSHong Zhang if (nsends) { 278769db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 278869db28dcSHong Zhang } 278969db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 279069db28dcSHong Zhang 279169db28dcSHong Zhang /* post receives, send and receive mat->a */ 279269db28dcSHong Zhang /*----------------------------------------*/ 279369db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 279469db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 279569db28dcSHong Zhang } 279669db28dcSHong Zhang for (i=0; i<nsends; i++) { 279769db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 279869db28dcSHong Zhang } 279969db28dcSHong Zhang count = nrecvs; 280069db28dcSHong Zhang while (count) { 280169db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2802e32f2f54SBarry 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); 280369db28dcSHong Zhang count--; 280469db28dcSHong Zhang } 280569db28dcSHong Zhang if (nsends) { 280669db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 280769db28dcSHong Zhang } 280869db28dcSHong Zhang 280969db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 281069db28dcSHong Zhang 281169db28dcSHong Zhang /* create redundant matrix */ 281269db28dcSHong Zhang /*-------------------------*/ 281369db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 281469db28dcSHong Zhang /* compute rownz_max for preallocation */ 281569db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 281669db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 281769db28dcSHong Zhang rptr = rbuf_j[imdex]; 281869db28dcSHong Zhang for (i=0; i<j; i++) { 281969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 282069db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 282169db28dcSHong Zhang } 282269db28dcSHong Zhang } 282369db28dcSHong Zhang 282469db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 282569db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 2826a2f3521dSMark F. Adams ierr = MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr); 282769db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 28280298fd71SBarry Smith ierr = MatSeqAIJSetPreallocation(C,rownz_max,NULL);CHKERRQ(ierr); 28290298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);CHKERRQ(ierr); 283069db28dcSHong Zhang } else { 283169db28dcSHong Zhang C = *matredundant; 283269db28dcSHong Zhang } 283369db28dcSHong Zhang 283469db28dcSHong Zhang /* insert local matrix entries */ 283569db28dcSHong Zhang rptr = sbuf_j; 283669db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 283769db28dcSHong Zhang vals = sbuf_a; 283869db28dcSHong Zhang for (i=0; i<rend-rstart; i++) { 283969db28dcSHong Zhang row = i + rstart; 284069db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 284169db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 284269db28dcSHong Zhang vals += ncols; 284369db28dcSHong Zhang cols += ncols; 284469db28dcSHong Zhang } 284569db28dcSHong Zhang /* insert received matrix entries */ 284669db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 284769db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 284869db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 284969db28dcSHong Zhang rptr = rbuf_j[imdex]; 285069db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 285169db28dcSHong Zhang vals = rbuf_a[imdex]; 285269db28dcSHong Zhang for (i=0; i<rend-rstart; i++) { 285369db28dcSHong Zhang row = i + rstart; 285469db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 285569db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 285669db28dcSHong Zhang vals += ncols; 285769db28dcSHong Zhang cols += ncols; 285869db28dcSHong Zhang } 285969db28dcSHong Zhang } 286069db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 286169db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 286269db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2863e32f2f54SBarry 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); 286469db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 286569db28dcSHong Zhang PetscContainer container; 286669db28dcSHong Zhang *matredundant = C; 286769db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 286838f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 286969db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 287069db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 287169db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 2872bf0cc555SLisandro Dalcin ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 2873bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 287469db28dcSHong Zhang 287569db28dcSHong Zhang redund->nzlocal = nzlocal; 287669db28dcSHong Zhang redund->nsends = nsends; 287769db28dcSHong Zhang redund->nrecvs = nrecvs; 287869db28dcSHong Zhang redund->send_rank = send_rank; 28791d79065fSBarry Smith redund->recv_rank = recv_rank; 288069db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 28811d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 288269db28dcSHong Zhang redund->sbuf_j = sbuf_j; 288369db28dcSHong Zhang redund->sbuf_a = sbuf_a; 288469db28dcSHong Zhang redund->rbuf_j = rbuf_j; 288569db28dcSHong Zhang redund->rbuf_a = rbuf_a; 288669db28dcSHong Zhang 2887bf0cc555SLisandro Dalcin redund->Destroy = C->ops->destroy; 288869db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 288969db28dcSHong Zhang } 289069db28dcSHong Zhang PetscFunctionReturn(0); 289169db28dcSHong Zhang } 289269db28dcSHong Zhang 289303bc72f1SMatthew Knepley #undef __FUNCT__ 2894c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2895c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2896c91732d9SHong Zhang { 2897c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2898c91732d9SHong Zhang PetscErrorCode ierr; 2899c91732d9SHong Zhang PetscInt i,*idxb = 0; 2900c91732d9SHong Zhang PetscScalar *va,*vb; 2901c91732d9SHong Zhang Vec vtmp; 2902c91732d9SHong Zhang 2903c91732d9SHong Zhang PetscFunctionBegin; 2904c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2905c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2906c91732d9SHong Zhang if (idx) { 2907192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2908d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2909c91732d9SHong Zhang } 2910c91732d9SHong Zhang } 2911c91732d9SHong Zhang 2912d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2913c91732d9SHong Zhang if (idx) { 2914d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2915c91732d9SHong Zhang } 2916c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2917c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2918c91732d9SHong Zhang 2919d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 2920c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2921c91732d9SHong Zhang va[i] = vb[i]; 2922c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2923c91732d9SHong Zhang } 2924c91732d9SHong Zhang } 2925c91732d9SHong Zhang 2926c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2927c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2928c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 29296bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2930c91732d9SHong Zhang PetscFunctionReturn(0); 2931c91732d9SHong Zhang } 2932c91732d9SHong Zhang 2933c91732d9SHong Zhang #undef __FUNCT__ 2934c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2935c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2936c87e5d42SMatthew Knepley { 2937c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2938c87e5d42SMatthew Knepley PetscErrorCode ierr; 2939c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2940c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2941c87e5d42SMatthew Knepley Vec vtmp; 2942c87e5d42SMatthew Knepley 2943c87e5d42SMatthew Knepley PetscFunctionBegin; 2944c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2945c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2946c87e5d42SMatthew Knepley if (idx) { 2947c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2948c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2949c87e5d42SMatthew Knepley } 2950c87e5d42SMatthew Knepley } 2951c87e5d42SMatthew Knepley 2952c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2953c87e5d42SMatthew Knepley if (idx) { 2954c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2955c87e5d42SMatthew Knepley } 2956c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2957c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2958c87e5d42SMatthew Knepley 2959c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++) { 2960c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2961c87e5d42SMatthew Knepley va[i] = vb[i]; 2962c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2963c87e5d42SMatthew Knepley } 2964c87e5d42SMatthew Knepley } 2965c87e5d42SMatthew Knepley 2966c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2967c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2968c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 29696bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2970c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2971c87e5d42SMatthew Knepley } 2972c87e5d42SMatthew Knepley 2973c87e5d42SMatthew Knepley #undef __FUNCT__ 297403bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 297503bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 297603bc72f1SMatthew Knepley { 297703bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 2978d0f46423SBarry Smith PetscInt n = A->rmap->n; 2979d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 298003bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 298103bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 298203bc72f1SMatthew Knepley Vec diagV, offdiagV; 298303bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 298403bc72f1SMatthew Knepley PetscInt r; 298503bc72f1SMatthew Knepley PetscErrorCode ierr; 298603bc72f1SMatthew Knepley 298703bc72f1SMatthew Knepley PetscFunctionBegin; 298803bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 2989ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);CHKERRQ(ierr); 2990ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);CHKERRQ(ierr); 299103bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 299203bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 299303bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 299403bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 299503bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 299603bc72f1SMatthew Knepley for (r = 0; r < n; ++r) { 2997028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 299803bc72f1SMatthew Knepley a[r] = diagA[r]; 299903bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 300003bc72f1SMatthew Knepley } else { 300103bc72f1SMatthew Knepley a[r] = offdiagA[r]; 300203bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 300303bc72f1SMatthew Knepley } 300403bc72f1SMatthew Knepley } 300503bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 300603bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 300703bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30086bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30096bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 301003bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 301103bc72f1SMatthew Knepley PetscFunctionReturn(0); 301203bc72f1SMatthew Knepley } 301303bc72f1SMatthew Knepley 30145494a064SHong Zhang #undef __FUNCT__ 3015c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 3016c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 3017c87e5d42SMatthew Knepley { 3018c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 3019c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 3020c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 3021c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 3022c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 3023c87e5d42SMatthew Knepley Vec diagV, offdiagV; 3024c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 3025c87e5d42SMatthew Knepley PetscInt r; 3026c87e5d42SMatthew Knepley PetscErrorCode ierr; 3027c87e5d42SMatthew Knepley 3028c87e5d42SMatthew Knepley PetscFunctionBegin; 3029c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 3030d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr); 3031d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr); 3032c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 3033c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 3034c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 3035c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 3036c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 3037c87e5d42SMatthew Knepley for (r = 0; r < n; ++r) { 3038c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 3039c87e5d42SMatthew Knepley a[r] = diagA[r]; 3040c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 3041c87e5d42SMatthew Knepley } else { 3042c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 3043c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 3044c87e5d42SMatthew Knepley } 3045c87e5d42SMatthew Knepley } 3046c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 3047c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 3048c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30496bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30506bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 3051c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 3052c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3053c87e5d42SMatthew Knepley } 3054c87e5d42SMatthew Knepley 3055c87e5d42SMatthew Knepley #undef __FUNCT__ 3056d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ" 3057d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat) 30585494a064SHong Zhang { 30595494a064SHong Zhang PetscErrorCode ierr; 3060f6d58c54SBarry Smith Mat *dummy; 30615494a064SHong Zhang 30625494a064SHong Zhang PetscFunctionBegin; 3063f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 3064f6d58c54SBarry Smith *newmat = *dummy; 3065f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 30665494a064SHong Zhang PetscFunctionReturn(0); 30675494a064SHong Zhang } 30685494a064SHong Zhang 30697087cfbeSBarry Smith extern PetscErrorCode MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 3070bbead8a2SBarry Smith 3071bbead8a2SBarry Smith #undef __FUNCT__ 3072bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ" 3073713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values) 3074bbead8a2SBarry Smith { 3075bbead8a2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 3076bbead8a2SBarry Smith PetscErrorCode ierr; 3077bbead8a2SBarry Smith 3078bbead8a2SBarry Smith PetscFunctionBegin; 3079bbead8a2SBarry Smith ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr); 3080bbead8a2SBarry Smith PetscFunctionReturn(0); 3081bbead8a2SBarry Smith } 3082bbead8a2SBarry Smith 308373a71a0fSBarry Smith #undef __FUNCT__ 308473a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ" 308573a71a0fSBarry Smith static PetscErrorCode MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx) 308673a71a0fSBarry Smith { 308773a71a0fSBarry Smith PetscErrorCode ierr; 308873a71a0fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)x->data; 308973a71a0fSBarry Smith 309073a71a0fSBarry Smith PetscFunctionBegin; 309173a71a0fSBarry Smith ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr); 309273a71a0fSBarry Smith ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr); 309373a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 309473a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 309573a71a0fSBarry Smith PetscFunctionReturn(0); 309673a71a0fSBarry Smith } 3097bbead8a2SBarry Smith 30988a729477SBarry Smith /* -------------------------------------------------------------------*/ 3099cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 3100cda55fadSBarry Smith MatGetRow_MPIAIJ, 3101cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 3102cda55fadSBarry Smith MatMult_MPIAIJ, 310397304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 31047c922b88SBarry Smith MatMultTranspose_MPIAIJ, 31057c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 3106519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3107103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 3108103bf8bdSMatthew Knepley #else 3109cda55fadSBarry Smith 0, 3110103bf8bdSMatthew Knepley #endif 3111cda55fadSBarry Smith 0, 3112cda55fadSBarry Smith 0, 311397304618SKris Buschelman /*10*/ 0, 3114cda55fadSBarry Smith 0, 3115cda55fadSBarry Smith 0, 311641f059aeSBarry Smith MatSOR_MPIAIJ, 3117b7c46309SBarry Smith MatTranspose_MPIAIJ, 311897304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 3119cda55fadSBarry Smith MatEqual_MPIAIJ, 3120cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 3121cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 3122cda55fadSBarry Smith MatNorm_MPIAIJ, 312397304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 3124cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 3125cda55fadSBarry Smith MatSetOption_MPIAIJ, 3126cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 3127d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 3128cda55fadSBarry Smith 0, 3129519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3130719d5645SBarry Smith 0, 3131103bf8bdSMatthew Knepley #else 3132cda55fadSBarry Smith 0, 3133103bf8bdSMatthew Knepley #endif 3134cda55fadSBarry Smith 0, 3135cda55fadSBarry Smith 0, 31364994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ, 3137519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3138719d5645SBarry Smith 0, 3139103bf8bdSMatthew Knepley #else 3140cda55fadSBarry Smith 0, 3141103bf8bdSMatthew Knepley #endif 3142cda55fadSBarry Smith 0, 3143cda55fadSBarry Smith 0, 3144cda55fadSBarry Smith 0, 3145d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 3146cda55fadSBarry Smith 0, 3147cda55fadSBarry Smith 0, 3148cda55fadSBarry Smith 0, 3149cda55fadSBarry Smith 0, 3150d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 3151cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 3152cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 3153cda55fadSBarry Smith MatGetValues_MPIAIJ, 3154cb5b572fSBarry Smith MatCopy_MPIAIJ, 3155d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 3156cda55fadSBarry Smith MatScale_MPIAIJ, 3157cda55fadSBarry Smith 0, 3158cda55fadSBarry Smith 0, 3159564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 316073a71a0fSBarry Smith /*49*/ MatSetRandom_MPIAIJ, 3161cda55fadSBarry Smith 0, 3162cda55fadSBarry Smith 0, 3163cda55fadSBarry Smith 0, 3164cda55fadSBarry Smith 0, 3165d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 3166cda55fadSBarry Smith 0, 3167cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 316872e6a0cfSJed Brown MatPermute_MPIAIJ, 3169cda55fadSBarry Smith 0, 3170d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 3171e03a110bSBarry Smith MatDestroy_MPIAIJ, 3172e03a110bSBarry Smith MatView_MPIAIJ, 3173357abbc8SBarry Smith 0, 3174f996eeb8SHong Zhang MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ, 3175f996eeb8SHong Zhang /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ, 3176f996eeb8SHong Zhang MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ, 3177a2243be0SBarry Smith 0, 3178a2243be0SBarry Smith 0, 3179a2243be0SBarry Smith 0, 3180d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 3181c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 3182a2243be0SBarry Smith 0, 3183a2243be0SBarry Smith MatSetColoring_MPIAIJ, 3184dcf5cc72SBarry Smith 0, 318597304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 31863acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 318797304618SKris Buschelman 0, 318897304618SKris Buschelman 0, 318997304618SKris Buschelman 0, 3190f1f41ecbSJed Brown MatFindZeroDiagonals_MPIAIJ, 319197304618SKris Buschelman /*80*/ 0, 319297304618SKris Buschelman 0, 319397304618SKris Buschelman 0, 31945bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 31956284ec50SHong Zhang 0, 31966284ec50SHong Zhang 0, 31976284ec50SHong Zhang 0, 31986284ec50SHong Zhang 0, 3199865e5f61SKris Buschelman 0, 3200d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 320126be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 320226be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 3203cf3ca8ceSHong Zhang MatPtAP_MPIAIJ_MPIAIJ, 3204cf3ca8ceSHong Zhang MatPtAPSymbolic_MPIAIJ_MPIAIJ, 3205cf3ca8ceSHong Zhang /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ, 32067a7894deSKris Buschelman 0, 32077a7894deSKris Buschelman 0, 32087a7894deSKris Buschelman 0, 32097a7894deSKris Buschelman 0, 3210d519adbfSMatthew Knepley /*99*/ 0, 3211d2b207f1SPeter Brune 0, 3212d2b207f1SPeter Brune 0, 32132fd7e33dSBarry Smith MatConjugate_MPIAIJ, 32142fd7e33dSBarry Smith 0, 3215d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 321699cafbc1SBarry Smith MatRealPart_MPIAIJ, 321769db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 321869db28dcSHong Zhang 0, 321969db28dcSHong Zhang 0, 3220d519adbfSMatthew Knepley /*109*/0, 322103bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 32225494a064SHong Zhang MatGetRowMin_MPIAIJ, 32235494a064SHong Zhang 0, 32245494a064SHong Zhang 0, 3225d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ, 3226bd0c2dcbSBarry Smith 0, 3227bd0c2dcbSBarry Smith 0, 3228bd0c2dcbSBarry Smith 0, 3229bd0c2dcbSBarry Smith 0, 32308fb81238SShri Abhyankar /*119*/0, 32318fb81238SShri Abhyankar 0, 32328fb81238SShri Abhyankar 0, 3233d6037b41SHong Zhang 0, 3234b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 3235f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ, 32360716a85fSBarry Smith MatGetColumnNorms_MPIAIJ, 3237bbead8a2SBarry Smith MatInvertBlockDiagonal_MPIAIJ, 3238b9614d88SDmitry Karpeev 0, 323937868618SMatthew G Knepley MatGetSubMatricesParallel_MPIAIJ, 3240187b3c17SHong Zhang /*129*/0, 3241187b3c17SHong Zhang MatTransposeMatMult_MPIAIJ_MPIAIJ, 3242187b3c17SHong Zhang MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ, 3243187b3c17SHong Zhang MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ, 3244187b3c17SHong Zhang 0, 3245187b3c17SHong Zhang /*134*/0, 3246187b3c17SHong Zhang 0, 3247187b3c17SHong Zhang 0, 3248187b3c17SHong Zhang 0, 3249187b3c17SHong Zhang 0 3250bd0c2dcbSBarry Smith }; 325136ce4990SBarry Smith 32522e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 32532e8a6d31SBarry Smith 3254fb2e594dSBarry Smith EXTERN_C_BEGIN 32554a2ae208SSatish Balay #undef __FUNCT__ 32564a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 32577087cfbeSBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat) 32582e8a6d31SBarry Smith { 32592e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3260dfbe8321SBarry Smith PetscErrorCode ierr; 32612e8a6d31SBarry Smith 32622e8a6d31SBarry Smith PetscFunctionBegin; 32632e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 32642e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 32652e8a6d31SBarry Smith PetscFunctionReturn(0); 32662e8a6d31SBarry Smith } 3267fb2e594dSBarry Smith EXTERN_C_END 32682e8a6d31SBarry Smith 3269fb2e594dSBarry Smith EXTERN_C_BEGIN 32704a2ae208SSatish Balay #undef __FUNCT__ 32714a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 32727087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat) 32732e8a6d31SBarry Smith { 32742e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3275dfbe8321SBarry Smith PetscErrorCode ierr; 32762e8a6d31SBarry Smith 32772e8a6d31SBarry Smith PetscFunctionBegin; 32782e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 32792e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 32802e8a6d31SBarry Smith PetscFunctionReturn(0); 32812e8a6d31SBarry Smith } 3282fb2e594dSBarry Smith EXTERN_C_END 32838a729477SBarry Smith 328427508adbSBarry Smith EXTERN_C_BEGIN 32854a2ae208SSatish Balay #undef __FUNCT__ 3286a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 32877087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3288a23d5eceSKris Buschelman { 3289a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3290dfbe8321SBarry Smith PetscErrorCode ierr; 3291b1d57f15SBarry Smith PetscInt i; 32922576faa2SJed Brown PetscBool d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE; 3293a23d5eceSKris Buschelman 3294a23d5eceSKris Buschelman PetscFunctionBegin; 32952576faa2SJed Brown if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE; 32962576faa2SJed Brown if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE; 3297a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 3298a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 3299e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 3300e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 3301899cda47SBarry Smith 330226283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 330326283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3304a23d5eceSKris Buschelman if (d_nnz) { 3305d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3306e32f2f54SBarry 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]); 3307a23d5eceSKris Buschelman } 3308a23d5eceSKris Buschelman } 3309a23d5eceSKris Buschelman if (o_nnz) { 3310d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3311e32f2f54SBarry 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]); 3312a23d5eceSKris Buschelman } 3313a23d5eceSKris Buschelman } 3314a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3315899cda47SBarry Smith 3316526dfc15SBarry Smith if (!B->preallocated) { 3317899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3318899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3319d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3320f9e9af59SJed Brown ierr = MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3321899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 3322899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3323899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3324d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3325f9e9af59SJed Brown ierr = MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3326899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 3327899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 3328526dfc15SBarry Smith } 3329899cda47SBarry Smith 3330c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3331c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 33322576faa2SJed Brown /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */ 33332576faa2SJed Brown if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);} 33342576faa2SJed Brown if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);} 3335526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3336a23d5eceSKris Buschelman PetscFunctionReturn(0); 3337a23d5eceSKris Buschelman } 3338a23d5eceSKris Buschelman EXTERN_C_END 3339a23d5eceSKris Buschelman 33404a2ae208SSatish Balay #undef __FUNCT__ 33414a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3342dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3343d6dfbf8fSBarry Smith { 3344d6dfbf8fSBarry Smith Mat mat; 3345416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3346dfbe8321SBarry Smith PetscErrorCode ierr; 3347d6dfbf8fSBarry Smith 33483a40ed3dSBarry Smith PetscFunctionBegin; 3349416022c9SBarry Smith *newmat = 0; 3350ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr); 3351d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 3352a2f3521dSMark F. Adams ierr = MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);CHKERRQ(ierr); 33537adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 33541d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3355273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3356e1b6402fSHong Zhang 3357d5f3da31SBarry Smith mat->factortype = matin->factortype; 3358d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3359a2f3521dSMark F. Adams mat->cmap->bs = matin->cmap->bs; 3360c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3361e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3362273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3363d6dfbf8fSBarry Smith 336417699dbbSLois Curfman McInnes a->size = oldmat->size; 336517699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3366e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3367e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3368e7641de0SSatish Balay a->rowindices = 0; 3369bcd2baecSBarry Smith a->rowvalues = 0; 3370bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3371d6dfbf8fSBarry Smith 33721e1e43feSBarry Smith ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr); 33731e1e43feSBarry Smith ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3374899cda47SBarry Smith 33752ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3376aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 33770f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3378b1fc9764SSatish Balay #else 3379d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 3380d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3381d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3382b1fc9764SSatish Balay #endif 3383416022c9SBarry Smith } else a->colmap = 0; 33843f41c07dSBarry Smith if (oldmat->garray) { 3385b1d57f15SBarry Smith PetscInt len; 3386d0f46423SBarry Smith len = oldmat->B->cmap->n; 3387b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 338852e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3389b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3390416022c9SBarry Smith } else a->garray = 0; 3391d6dfbf8fSBarry Smith 3392416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 339352e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 3394a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 339552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 33962e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 339752e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 33982e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 339952e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 3400140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 34018a729477SBarry Smith *newmat = mat; 34023a40ed3dSBarry Smith PetscFunctionReturn(0); 34038a729477SBarry Smith } 3404416022c9SBarry Smith 34051a4ee126SBarry Smith 34061a4ee126SBarry Smith 34074a2ae208SSatish Balay #undef __FUNCT__ 34085bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3409112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 34108fb81238SShri Abhyankar { 34118fb81238SShri Abhyankar PetscScalar *vals,*svals; 3412ce94432eSBarry Smith MPI_Comm comm; 34138fb81238SShri Abhyankar PetscErrorCode ierr; 34141a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 34158fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 34168fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 34170298fd71SBarry Smith PetscInt *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols; 34188fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 34198fb81238SShri Abhyankar int fd; 342008ea439dSMark F. Adams PetscInt bs = 1; 34218fb81238SShri Abhyankar 34228fb81238SShri Abhyankar PetscFunctionBegin; 3423ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 34248fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 34258fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 34268fb81238SShri Abhyankar if (!rank) { 34278fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 34288fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr); 34298fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 34308fb81238SShri Abhyankar } 34318fb81238SShri Abhyankar 34320298fd71SBarry Smith ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 34330298fd71SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr); 343408ea439dSMark F. Adams ierr = PetscOptionsEnd();CHKERRQ(ierr); 343508ea439dSMark F. Adams 34368fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 34378fb81238SShri Abhyankar 34388fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 34398fb81238SShri Abhyankar M = header[1]; N = header[2]; 34408fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 34418fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 34428fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 34438fb81238SShri Abhyankar 34448fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 34458fb81238SShri Abhyankar if (sizesset) { 34468fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 34478fb81238SShri Abhyankar } 3448abd38a8fSBarry 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); 3449abd38a8fSBarry 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); 34508fb81238SShri Abhyankar 345108ea439dSMark F. Adams /* determine ownership of all (block) rows */ 345208ea439dSMark F. Adams if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs); 345308ea439dSMark F. Adams if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank)); /* PETSC_DECIDE */ 34544683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 34558fb81238SShri Abhyankar 34568fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 34578fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 34588fb81238SShri Abhyankar 34598fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 34608fb81238SShri Abhyankar if (!rank) { 34618fb81238SShri Abhyankar mmax = rowners[1]; 34625c4ea359SMatthew G Knepley for (i=2; i<=size; i++) { 34638fb81238SShri Abhyankar mmax = PetscMax(mmax, rowners[i]); 34648fb81238SShri Abhyankar } 34658fb81238SShri Abhyankar } else mmax = m; 34668fb81238SShri Abhyankar 34678fb81238SShri Abhyankar rowners[0] = 0; 34688fb81238SShri Abhyankar for (i=2; i<=size; i++) { 34698fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 34708fb81238SShri Abhyankar } 34718fb81238SShri Abhyankar rstart = rowners[rank]; 34728fb81238SShri Abhyankar rend = rowners[rank+1]; 34738fb81238SShri Abhyankar 34748fb81238SShri Abhyankar /* distribute row lengths to all processors */ 34758fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 34768fb81238SShri Abhyankar if (!rank) { 34778fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 34785c4ea359SMatthew G Knepley ierr = PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 34798fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 34808fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 34818fb81238SShri Abhyankar for (j=0; j<m; j++) { 34828fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 34838fb81238SShri Abhyankar } 34848fb81238SShri Abhyankar for (i=1; i<size; i++) { 34858fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 34868fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 34878fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 34888fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 34898fb81238SShri Abhyankar } 3490a25532f0SBarry Smith ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 34918fb81238SShri Abhyankar } 34928fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 34938fb81238SShri Abhyankar } else { 3494a25532f0SBarry Smith ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 34958fb81238SShri Abhyankar } 34968fb81238SShri Abhyankar 34978fb81238SShri Abhyankar if (!rank) { 34988fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 34998fb81238SShri Abhyankar maxnz = 0; 35008fb81238SShri Abhyankar for (i=0; i<size; i++) { 35018fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 35028fb81238SShri Abhyankar } 35038fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 35048fb81238SShri Abhyankar 35058fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 35068fb81238SShri Abhyankar nz = procsnz[0]; 35078fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 35088fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 35098fb81238SShri Abhyankar 35108fb81238SShri Abhyankar /* read in every one elses and ship off */ 35118fb81238SShri Abhyankar for (i=1; i<size; i++) { 35128fb81238SShri Abhyankar nz = procsnz[i]; 35138fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 3514a25532f0SBarry Smith ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 35158fb81238SShri Abhyankar } 35168fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 35178fb81238SShri Abhyankar } else { 35188fb81238SShri Abhyankar /* determine buffer space needed for message */ 35198fb81238SShri Abhyankar nz = 0; 35208fb81238SShri Abhyankar for (i=0; i<m; i++) { 35218fb81238SShri Abhyankar nz += ourlens[i]; 35228fb81238SShri Abhyankar } 35238fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 35248fb81238SShri Abhyankar 35258fb81238SShri Abhyankar /* receive message of column indices*/ 3526a25532f0SBarry Smith ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 35278fb81238SShri Abhyankar } 35288fb81238SShri Abhyankar 35298fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 35308fb81238SShri Abhyankar if (N != M) { 35318fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 35328fb81238SShri Abhyankar else n = newMat->cmap->n; 35338fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 35348fb81238SShri Abhyankar cstart = cend - n; 35358fb81238SShri Abhyankar } else { 35368fb81238SShri Abhyankar cstart = rstart; 35378fb81238SShri Abhyankar cend = rend; 35388fb81238SShri Abhyankar n = cend - cstart; 35398fb81238SShri Abhyankar } 35408fb81238SShri Abhyankar 35418fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 35428fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 35438fb81238SShri Abhyankar jj = 0; 35448fb81238SShri Abhyankar for (i=0; i<m; i++) { 35458fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 35468fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 35478fb81238SShri Abhyankar jj++; 35488fb81238SShri Abhyankar } 35498fb81238SShri Abhyankar } 35508fb81238SShri Abhyankar 35518fb81238SShri Abhyankar for (i=0; i<m; i++) { 35528fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 35538fb81238SShri Abhyankar } 35548fb81238SShri Abhyankar if (!sizesset) { 35558fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 35568fb81238SShri Abhyankar } 355708ea439dSMark F. Adams 355808ea439dSMark F. Adams if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);} 355908ea439dSMark F. Adams 35608fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 35618fb81238SShri Abhyankar 35628fb81238SShri Abhyankar for (i=0; i<m; i++) { 35638fb81238SShri Abhyankar ourlens[i] += offlens[i]; 35648fb81238SShri Abhyankar } 35658fb81238SShri Abhyankar 35668fb81238SShri Abhyankar if (!rank) { 35678fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 35688fb81238SShri Abhyankar 35698fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 35708fb81238SShri Abhyankar nz = procsnz[0]; 35718fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 35728fb81238SShri Abhyankar 35738fb81238SShri Abhyankar /* insert into matrix */ 35748fb81238SShri Abhyankar jj = rstart; 35758fb81238SShri Abhyankar smycols = mycols; 35768fb81238SShri Abhyankar svals = vals; 35778fb81238SShri Abhyankar for (i=0; i<m; i++) { 35788fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35798fb81238SShri Abhyankar smycols += ourlens[i]; 35808fb81238SShri Abhyankar svals += ourlens[i]; 35818fb81238SShri Abhyankar jj++; 35828fb81238SShri Abhyankar } 35838fb81238SShri Abhyankar 35848fb81238SShri Abhyankar /* read in other processors and ship out */ 35858fb81238SShri Abhyankar for (i=1; i<size; i++) { 35868fb81238SShri Abhyankar nz = procsnz[i]; 35878fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3588a25532f0SBarry Smith ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35898fb81238SShri Abhyankar } 35908fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 35918fb81238SShri Abhyankar } else { 35928fb81238SShri Abhyankar /* receive numeric values */ 35938fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 35948fb81238SShri Abhyankar 35958fb81238SShri Abhyankar /* receive message of values*/ 3596a25532f0SBarry Smith ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35978fb81238SShri Abhyankar 35988fb81238SShri Abhyankar /* insert into matrix */ 35998fb81238SShri Abhyankar jj = rstart; 36008fb81238SShri Abhyankar smycols = mycols; 36018fb81238SShri Abhyankar svals = vals; 36028fb81238SShri Abhyankar for (i=0; i<m; i++) { 36038fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 36048fb81238SShri Abhyankar smycols += ourlens[i]; 36058fb81238SShri Abhyankar svals += ourlens[i]; 36068fb81238SShri Abhyankar jj++; 36078fb81238SShri Abhyankar } 36088fb81238SShri Abhyankar } 36098fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 36108fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 36118fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 36128fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 36138fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 36148fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 36158fb81238SShri Abhyankar PetscFunctionReturn(0); 36168fb81238SShri Abhyankar } 36178fb81238SShri Abhyankar 36188fb81238SShri Abhyankar #undef __FUNCT__ 36194a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 36204aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 36214aa3045dSJed Brown { 36224aa3045dSJed Brown PetscErrorCode ierr; 36234aa3045dSJed Brown IS iscol_local; 36244aa3045dSJed Brown PetscInt csize; 36254aa3045dSJed Brown 36264aa3045dSJed Brown PetscFunctionBegin; 36274aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3628b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3629b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3630e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3631b79d0421SJed Brown } else { 3632c5bfad50SMark F. Adams PetscInt cbs; 3633c5bfad50SMark F. Adams ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 36344aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3635c5bfad50SMark F. Adams ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr); 3636b79d0421SJed Brown } 36374aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3638b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3639b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 36406bf464f9SBarry Smith ierr = ISDestroy(&iscol_local);CHKERRQ(ierr); 3641b79d0421SJed Brown } 36424aa3045dSJed Brown PetscFunctionReturn(0); 36434aa3045dSJed Brown } 36444aa3045dSJed Brown 364529dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*); 36464aa3045dSJed Brown #undef __FUNCT__ 36474aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3648a0ff6018SBarry Smith /* 364929da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 365029da9460SBarry Smith in local and then by concatenating the local matrices the end result. 365129da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 36524aa3045dSJed Brown 36534aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3654a0ff6018SBarry Smith */ 36554aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3656a0ff6018SBarry Smith { 3657dfbe8321SBarry Smith PetscErrorCode ierr; 365832dcc486SBarry Smith PetscMPIInt rank,size; 3659a2f3521dSMark F. Adams PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs; 366029dcf524SDmitry Karpeev PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol; 366129dcf524SDmitry Karpeev PetscBool allcolumns, colflag; 366229dcf524SDmitry Karpeev Mat M,Mreuse; 3663a77337e4SBarry Smith MatScalar *vwork,*aa; 3664ce94432eSBarry Smith MPI_Comm comm; 366500e6dbe6SBarry Smith Mat_SeqAIJ *aij; 36667e2c5f70SBarry Smith 3667a0ff6018SBarry Smith PetscFunctionBegin; 3668ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 36691dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 36701dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 367100e6dbe6SBarry Smith 367229dcf524SDmitry Karpeev ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr); 367329dcf524SDmitry Karpeev ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr); 367429dcf524SDmitry Karpeev if (colflag && ncol == mat->cmap->N) { 367529dcf524SDmitry Karpeev allcolumns = PETSC_TRUE; 367629dcf524SDmitry Karpeev } else { 367729dcf524SDmitry Karpeev allcolumns = PETSC_FALSE; 367829dcf524SDmitry Karpeev } 3679fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3680fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr); 3681e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 368229dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3683fee21e36SBarry Smith } else { 368429dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3685fee21e36SBarry Smith } 3686a0ff6018SBarry Smith 3687a0ff6018SBarry Smith /* 3688a0ff6018SBarry Smith m - number of local rows 3689a0ff6018SBarry Smith n - number of columns (same on all processors) 3690a0ff6018SBarry Smith rstart - first row in new global matrix generated 3691a0ff6018SBarry Smith */ 3692fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3693a2f3521dSMark F. Adams ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr); 3694a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3695fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 369600e6dbe6SBarry Smith ii = aij->i; 369700e6dbe6SBarry Smith jj = aij->j; 369800e6dbe6SBarry Smith 3699a0ff6018SBarry Smith /* 370000e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 370100e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3702a0ff6018SBarry Smith */ 370300e6dbe6SBarry Smith 370400e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 37056a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3706ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3707ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3708e2c4fddaSBarry Smith nlocal = m; 37096a6a5d1dSBarry Smith } else { 3710ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3711ab50ec6bSBarry Smith } 3712ab50ec6bSBarry Smith } else { 37136a6a5d1dSBarry Smith nlocal = csize; 37146a6a5d1dSBarry Smith } 3715b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 371600e6dbe6SBarry Smith rstart = rend - nlocal; 371765e19b50SBarry 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); 371800e6dbe6SBarry Smith 371900e6dbe6SBarry Smith /* next, compute all the lengths */ 3720b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 372100e6dbe6SBarry Smith olens = dlens + m; 372200e6dbe6SBarry Smith for (i=0; i<m; i++) { 372300e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 372400e6dbe6SBarry Smith olen = 0; 372500e6dbe6SBarry Smith dlen = 0; 372600e6dbe6SBarry Smith for (j=0; j<jend; j++) { 372700e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 372800e6dbe6SBarry Smith else dlen++; 372900e6dbe6SBarry Smith jj++; 373000e6dbe6SBarry Smith } 373100e6dbe6SBarry Smith olens[i] = olen; 373200e6dbe6SBarry Smith dlens[i] = dlen; 373300e6dbe6SBarry Smith } 3734f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3735f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 3736a2f3521dSMark F. Adams ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); 37377adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3738e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3739606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3740a0ff6018SBarry Smith } else { 3741b1d57f15SBarry Smith PetscInt ml,nl; 3742a0ff6018SBarry Smith 3743a0ff6018SBarry Smith M = *newmat; 3744a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3745e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3746a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3747c48de900SBarry Smith /* 3748c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3749c48de900SBarry Smith rather than the slower MatSetValues(). 3750c48de900SBarry Smith */ 3751c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3752c48de900SBarry Smith M->assembled = PETSC_FALSE; 3753a0ff6018SBarry Smith } 3754a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3755fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 375600e6dbe6SBarry Smith ii = aij->i; 375700e6dbe6SBarry Smith jj = aij->j; 375800e6dbe6SBarry Smith aa = aij->a; 3759a0ff6018SBarry Smith for (i=0; i<m; i++) { 3760a0ff6018SBarry Smith row = rstart + i; 376100e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 376200e6dbe6SBarry Smith cwork = jj; jj += nz; 376300e6dbe6SBarry Smith vwork = aa; aa += nz; 37648c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3765a0ff6018SBarry Smith } 3766a0ff6018SBarry Smith 3767a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3768a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3769a0ff6018SBarry Smith *newmat = M; 3770fee21e36SBarry Smith 3771fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3772fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3773fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3774bf0cc555SLisandro Dalcin ierr = MatDestroy(&Mreuse);CHKERRQ(ierr); 3775fee21e36SBarry Smith } 3776a0ff6018SBarry Smith PetscFunctionReturn(0); 3777a0ff6018SBarry Smith } 3778273d9f13SBarry Smith 3779e2e86b8fSSatish Balay EXTERN_C_BEGIN 37804a2ae208SSatish Balay #undef __FUNCT__ 3781ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 37827087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3783ccd8e176SBarry Smith { 3784899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3785899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3786ccd8e176SBarry Smith const PetscInt *JJ; 3787ccd8e176SBarry Smith PetscScalar *values; 3788ccd8e176SBarry Smith PetscErrorCode ierr; 3789ccd8e176SBarry Smith 3790ccd8e176SBarry Smith PetscFunctionBegin; 3791e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3792899cda47SBarry Smith 379326283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 379426283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3795d0f46423SBarry Smith m = B->rmap->n; 3796d0f46423SBarry Smith cstart = B->cmap->rstart; 3797d0f46423SBarry Smith cend = B->cmap->rend; 3798d0f46423SBarry Smith rstart = B->rmap->rstart; 3799899cda47SBarry Smith 38001d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3801ccd8e176SBarry Smith 3802ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3803ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3804ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3805ecc77c7aSBarry Smith JJ = J + Ii[i]; 3806e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3807ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3808d0f46423SBarry 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); 3809ecc77c7aSBarry Smith } 3810ecc77c7aSBarry Smith #endif 3811ecc77c7aSBarry Smith 3812ccd8e176SBarry Smith for (i=0; i<m; i++) { 3813b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3814b7940d39SSatish Balay JJ = J + Ii[i]; 3815ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3816ccd8e176SBarry Smith d = 0; 38170daa03b5SJed Brown for (j=0; j<nnz; j++) { 38180daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3819ccd8e176SBarry Smith } 3820ccd8e176SBarry Smith d_nnz[i] = d; 3821ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3822ccd8e176SBarry Smith } 3823ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 38241d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3825ccd8e176SBarry Smith 3826ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3827ccd8e176SBarry Smith else { 3828ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3829ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3830ccd8e176SBarry Smith } 3831ccd8e176SBarry Smith 3832ccd8e176SBarry Smith for (i=0; i<m; i++) { 3833ccd8e176SBarry Smith ii = i + rstart; 3834b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3835b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3836ccd8e176SBarry Smith } 3837ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3838ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3839ccd8e176SBarry Smith 3840ccd8e176SBarry Smith if (!v) { 3841ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3842ccd8e176SBarry Smith } 38437827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3844ccd8e176SBarry Smith PetscFunctionReturn(0); 3845ccd8e176SBarry Smith } 3846e2e86b8fSSatish Balay EXTERN_C_END 3847ccd8e176SBarry Smith 3848ccd8e176SBarry Smith #undef __FUNCT__ 3849ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 38501eea217eSSatish Balay /*@ 3851ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3852ccd8e176SBarry Smith (the default parallel PETSc format). 3853ccd8e176SBarry Smith 3854ccd8e176SBarry Smith Collective on MPI_Comm 3855ccd8e176SBarry Smith 3856ccd8e176SBarry Smith Input Parameters: 3857a1661176SMatthew Knepley + B - the matrix 3858ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 38590daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3860ccd8e176SBarry Smith - v - optional values in the matrix 3861ccd8e176SBarry Smith 3862ccd8e176SBarry Smith Level: developer 3863ccd8e176SBarry Smith 386412251496SSatish Balay Notes: 386512251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 386612251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 386712251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 386812251496SSatish Balay 386912251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 387012251496SSatish Balay 387112251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 387212251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 387312251496SSatish Balay as shown: 387412251496SSatish Balay 387512251496SSatish Balay 1 0 0 387612251496SSatish Balay 2 0 3 P0 387712251496SSatish Balay ------- 387812251496SSatish Balay 4 5 6 P1 387912251496SSatish Balay 388012251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 388112251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 388212251496SSatish Balay j = {0,0,2} [size = nz = 6] 388312251496SSatish Balay v = {1,2,3} [size = nz = 6] 388412251496SSatish Balay 388512251496SSatish Balay Process1 [P1]: rows_owned=[2] 388612251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 388712251496SSatish Balay j = {0,1,2} [size = nz = 6] 388812251496SSatish Balay v = {4,5,6} [size = nz = 6] 388912251496SSatish Balay 3890ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3891ccd8e176SBarry Smith 389269b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, 38938d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3894ccd8e176SBarry Smith @*/ 38957087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3896ccd8e176SBarry Smith { 38974ac538c5SBarry Smith PetscErrorCode ierr; 3898ccd8e176SBarry Smith 3899ccd8e176SBarry Smith PetscFunctionBegin; 39004ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3901ccd8e176SBarry Smith PetscFunctionReturn(0); 3902ccd8e176SBarry Smith } 3903ccd8e176SBarry Smith 3904ccd8e176SBarry Smith #undef __FUNCT__ 39054a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3906273d9f13SBarry Smith /*@C 3907ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3908273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3909273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3910273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3911273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3912273d9f13SBarry Smith 3913273d9f13SBarry Smith Collective on MPI_Comm 3914273d9f13SBarry Smith 3915273d9f13SBarry Smith Input Parameters: 3916273d9f13SBarry Smith + A - the matrix 3917273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3918273d9f13SBarry Smith (same value is used for all local rows) 3919273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3920273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 39210298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 3922273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 39233287b5eaSJed Brown For matrices that will be factored, you must leave room for (and set) 39243287b5eaSJed Brown the diagonal entry even if it is zero. 3925273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3926273d9f13SBarry Smith submatrix (same value is used for all local rows). 3927273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3928273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 39290298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 3930273d9f13SBarry Smith structure. The size of this array is equal to the number 3931273d9f13SBarry Smith of local rows, i.e 'm'. 3932273d9f13SBarry Smith 393349a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 393449a6f317SBarry Smith 3935273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3936ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 39370598bfebSBarry Smith storage. The stored row and column indices begin with zero. 39380598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3939273d9f13SBarry Smith 3940273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3941273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3942273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3943273d9f13SBarry Smith 3944273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3945a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3946a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3947a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3948a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3949a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3950a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3951a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3952a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3953273d9f13SBarry Smith 3954273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3955273d9f13SBarry Smith 3956aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3957aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3958aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3959aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3960aa95bbe8SBarry Smith 3961273d9f13SBarry Smith Example usage: 3962273d9f13SBarry Smith 3963273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3964273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3965273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3966273d9f13SBarry Smith as follows: 3967273d9f13SBarry Smith 3968273d9f13SBarry Smith .vb 3969273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3970273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3971273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3972273d9f13SBarry Smith ------------------------------------- 3973273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3974273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3975273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3976273d9f13SBarry Smith ------------------------------------- 3977273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3978273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3979273d9f13SBarry Smith .ve 3980273d9f13SBarry Smith 3981273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3982273d9f13SBarry Smith 3983273d9f13SBarry Smith .vb 3984273d9f13SBarry Smith A B C 3985273d9f13SBarry Smith D E F 3986273d9f13SBarry Smith G H I 3987273d9f13SBarry Smith .ve 3988273d9f13SBarry Smith 3989273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3990273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3991273d9f13SBarry Smith 3992273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3993273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3994273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3995273d9f13SBarry Smith 3996273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3997273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3998273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3999273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4000273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4001273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4002273d9f13SBarry Smith 4003273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4004273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4005273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4006273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4007273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4008273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4009273d9f13SBarry Smith .vb 4010273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4011273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4012273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4013273d9f13SBarry Smith .ve 4014273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4015273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4016273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4017273d9f13SBarry Smith 34 values. 4018273d9f13SBarry Smith 4019273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4020273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4021273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4022273d9f13SBarry Smith .vb 4023273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4024273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4025273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4026273d9f13SBarry Smith .ve 4027273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4028273d9f13SBarry Smith hence pre-allocation is perfect. 4029273d9f13SBarry Smith 4030273d9f13SBarry Smith Level: intermediate 4031273d9f13SBarry Smith 4032273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4033273d9f13SBarry Smith 403469b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(), 4035ab978733SBarry Smith MPIAIJ, MatGetInfo(), PetscSplitOwnership() 4036273d9f13SBarry Smith @*/ 40377087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 4038273d9f13SBarry Smith { 40394ac538c5SBarry Smith PetscErrorCode ierr; 4040273d9f13SBarry Smith 4041273d9f13SBarry Smith PetscFunctionBegin; 40426ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 40436ba663aaSJed Brown PetscValidType(B,1); 40444ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 4045273d9f13SBarry Smith PetscFunctionReturn(0); 4046273d9f13SBarry Smith } 4047273d9f13SBarry Smith 40484a2ae208SSatish Balay #undef __FUNCT__ 40492fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 405058d36128SBarry Smith /*@ 40512fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 40522fb0ec9aSBarry Smith CSR format the local rows. 40532fb0ec9aSBarry Smith 40542fb0ec9aSBarry Smith Collective on MPI_Comm 40552fb0ec9aSBarry Smith 40562fb0ec9aSBarry Smith Input Parameters: 40572fb0ec9aSBarry Smith + comm - MPI communicator 40582fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 40592fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 40602fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 40612fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 40622fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 40632fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 40642fb0ec9aSBarry Smith . i - row indices 40652fb0ec9aSBarry Smith . j - column indices 40662fb0ec9aSBarry Smith - a - matrix values 40672fb0ec9aSBarry Smith 40682fb0ec9aSBarry Smith Output Parameter: 40692fb0ec9aSBarry Smith . mat - the matrix 407003bfb495SBarry Smith 40712fb0ec9aSBarry Smith Level: intermediate 40722fb0ec9aSBarry Smith 40732fb0ec9aSBarry Smith Notes: 40742fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 40752fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 40768d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 40772fb0ec9aSBarry Smith 407812251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 407912251496SSatish Balay 408012251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 408112251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 408212251496SSatish Balay as shown: 408312251496SSatish Balay 408412251496SSatish Balay 1 0 0 408512251496SSatish Balay 2 0 3 P0 408612251496SSatish Balay ------- 408712251496SSatish Balay 4 5 6 P1 408812251496SSatish Balay 408912251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 409012251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 409112251496SSatish Balay j = {0,0,2} [size = nz = 6] 409212251496SSatish Balay v = {1,2,3} [size = nz = 6] 409312251496SSatish Balay 409412251496SSatish Balay Process1 [P1]: rows_owned=[2] 409512251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 409612251496SSatish Balay j = {0,1,2} [size = nz = 6] 409712251496SSatish Balay v = {4,5,6} [size = nz = 6] 40982fb0ec9aSBarry Smith 40992fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 41002fb0ec9aSBarry Smith 41012fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 410269b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays() 41032fb0ec9aSBarry Smith @*/ 41047087cfbeSBarry Smith PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat) 41052fb0ec9aSBarry Smith { 41062fb0ec9aSBarry Smith PetscErrorCode ierr; 41072fb0ec9aSBarry Smith 41082fb0ec9aSBarry Smith PetscFunctionBegin; 410969b1f4b7SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4110e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 41112fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4112d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 4113a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */ 41142fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 41152fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 41162fb0ec9aSBarry Smith PetscFunctionReturn(0); 41172fb0ec9aSBarry Smith } 41182fb0ec9aSBarry Smith 41192fb0ec9aSBarry Smith #undef __FUNCT__ 412069b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ" 4121273d9f13SBarry Smith /*@C 412269b1f4b7SBarry Smith MatCreateAIJ - Creates a sparse parallel matrix in AIJ format 4123273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 4124273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 4125273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 4126273d9f13SBarry Smith performance can be increased by more than a factor of 50. 4127273d9f13SBarry Smith 4128273d9f13SBarry Smith Collective on MPI_Comm 4129273d9f13SBarry Smith 4130273d9f13SBarry Smith Input Parameters: 4131273d9f13SBarry Smith + comm - MPI communicator 4132273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 4133273d9f13SBarry Smith This value should be the same as the local size used in creating the 4134273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 4135273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 4136273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 4137273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 4138273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 4139273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 4140273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 4141273d9f13SBarry Smith (same value is used for all local rows) 4142273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 4143273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 41440298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 4145273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 4146273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 4147273d9f13SBarry Smith submatrix (same value is used for all local rows). 4148273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 4149273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 41500298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 4151273d9f13SBarry Smith structure. The size of this array is equal to the number 4152273d9f13SBarry Smith of local rows, i.e 'm'. 4153273d9f13SBarry Smith 4154273d9f13SBarry Smith Output Parameter: 4155273d9f13SBarry Smith . A - the matrix 4156273d9f13SBarry Smith 4157175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 4158ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 4159175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 4160175b88e8SBarry Smith 4161273d9f13SBarry Smith Notes: 416249a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 416349a6f317SBarry Smith 4164273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 4165273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 4166273d9f13SBarry Smith storage requirements for this matrix. 4167273d9f13SBarry Smith 4168273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 4169273d9f13SBarry Smith processor than it must be used on all processors that share the object for 4170273d9f13SBarry Smith that argument. 4171273d9f13SBarry Smith 4172273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 4173273d9f13SBarry Smith (possibly both). 4174273d9f13SBarry Smith 417533a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 417633a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 417733a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 417833a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 417933a7c187SSatish Balay values corresponding to [m x N] submatrix. 4180273d9f13SBarry Smith 418133a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 418233a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 418333a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 418433a7c187SSatish Balay 418533a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 418633a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 418733a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 418833a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 418933a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 419033a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 419133a7c187SSatish Balay illustrates this concept. 419233a7c187SSatish Balay 419333a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 419433a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 419533a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 419633a7c187SSatish Balay local matrix (a rectangular submatrix). 4197273d9f13SBarry Smith 4198273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 4199273d9f13SBarry Smith 420097d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 420197d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 420297d05335SKris Buschelman type of communicator, use the construction mechanism: 420378102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 420497d05335SKris Buschelman 4205273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 4206273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 4207273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 4208273d9f13SBarry Smith 4209273d9f13SBarry Smith Options Database Keys: 4210923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 4211923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 4212273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 4213273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 4214273d9f13SBarry Smith the user still MUST index entries starting at 0! 4215273d9f13SBarry Smith 4216273d9f13SBarry Smith 4217273d9f13SBarry Smith Example usage: 4218273d9f13SBarry Smith 4219273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 4220273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 4221273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 4222273d9f13SBarry Smith as follows: 4223273d9f13SBarry Smith 4224273d9f13SBarry Smith .vb 4225273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 4226273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 4227273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 4228273d9f13SBarry Smith ------------------------------------- 4229273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 4230273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 4231273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 4232273d9f13SBarry Smith ------------------------------------- 4233273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 4234273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 4235273d9f13SBarry Smith .ve 4236273d9f13SBarry Smith 4237273d9f13SBarry Smith This can be represented as a collection of submatrices as: 4238273d9f13SBarry Smith 4239273d9f13SBarry Smith .vb 4240273d9f13SBarry Smith A B C 4241273d9f13SBarry Smith D E F 4242273d9f13SBarry Smith G H I 4243273d9f13SBarry Smith .ve 4244273d9f13SBarry Smith 4245273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 4246273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 4247273d9f13SBarry Smith 4248273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4249273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4250273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 4251273d9f13SBarry Smith 4252273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 4253273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 4254273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 4255273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4256273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4257273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4258273d9f13SBarry Smith 4259273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4260273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4261273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4262273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4263273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4264273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4265273d9f13SBarry Smith .vb 4266273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4267273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4268273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4269273d9f13SBarry Smith .ve 4270273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4271273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4272273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4273273d9f13SBarry Smith 34 values. 4274273d9f13SBarry Smith 4275273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4276273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4277273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4278273d9f13SBarry Smith .vb 4279273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4280273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4281273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4282273d9f13SBarry Smith .ve 4283273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4284273d9f13SBarry Smith hence pre-allocation is perfect. 4285273d9f13SBarry Smith 4286273d9f13SBarry Smith Level: intermediate 4287273d9f13SBarry Smith 4288273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4289273d9f13SBarry Smith 4290ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 42912fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4292273d9f13SBarry Smith @*/ 429369b1f4b7SBarry Smith PetscErrorCode MatCreateAIJ(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) 4294273d9f13SBarry Smith { 42956849ba73SBarry Smith PetscErrorCode ierr; 4296b1d57f15SBarry Smith PetscMPIInt size; 4297273d9f13SBarry Smith 4298273d9f13SBarry Smith PetscFunctionBegin; 4299f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4300f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4301273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4302273d9f13SBarry Smith if (size > 1) { 4303273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4304273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4305273d9f13SBarry Smith } else { 4306273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4307273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4308273d9f13SBarry Smith } 4309273d9f13SBarry Smith PetscFunctionReturn(0); 4310273d9f13SBarry Smith } 4311195d93cdSBarry Smith 43124a2ae208SSatish Balay #undef __FUNCT__ 43134a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 43149230625dSJed Brown PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[]) 4315195d93cdSBarry Smith { 4316195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4317b1d57f15SBarry Smith 4318195d93cdSBarry Smith PetscFunctionBegin; 4319195d93cdSBarry Smith *Ad = a->A; 4320195d93cdSBarry Smith *Ao = a->B; 4321195d93cdSBarry Smith *colmap = a->garray; 4322195d93cdSBarry Smith PetscFunctionReturn(0); 4323195d93cdSBarry Smith } 4324a2243be0SBarry Smith 4325a2243be0SBarry Smith #undef __FUNCT__ 4326a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4327dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4328a2243be0SBarry Smith { 4329dfbe8321SBarry Smith PetscErrorCode ierr; 4330b1d57f15SBarry Smith PetscInt i; 4331a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4332a2243be0SBarry Smith 4333a2243be0SBarry Smith PetscFunctionBegin; 43348ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 433508b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4336a2243be0SBarry Smith ISColoring ocoloring; 4337a2243be0SBarry Smith 4338a2243be0SBarry Smith /* set coloring for diagonal portion */ 4339a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4340a2243be0SBarry Smith 4341a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4342ce94432eSBarry Smith ierr = ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);CHKERRQ(ierr); 4343d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4344d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4345a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4346a2243be0SBarry Smith } 4347a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4348d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4349a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43506bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4351a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 435208b6dcc0SBarry Smith ISColoringValue *colors; 4353b1d57f15SBarry Smith PetscInt *larray; 4354a2243be0SBarry Smith ISColoring ocoloring; 4355a2243be0SBarry Smith 4356a2243be0SBarry Smith /* set coloring for diagonal portion */ 4357d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4358d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4359d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4360a2243be0SBarry Smith } 43610298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);CHKERRQ(ierr); 4362d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4363d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4364a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4365a2243be0SBarry Smith } 4366a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4367d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4368a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 43696bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4370a2243be0SBarry Smith 4371a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4372d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 43730298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);CHKERRQ(ierr); 4374d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4375d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4376a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4377a2243be0SBarry Smith } 4378a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4379d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4380a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43816bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 43826bf464f9SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4383a2243be0SBarry Smith PetscFunctionReturn(0); 4384a2243be0SBarry Smith } 4385a2243be0SBarry Smith 4386779c1a83SBarry Smith #undef __FUNCT__ 4387779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4388b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4389779c1a83SBarry Smith { 4390779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4391dfbe8321SBarry Smith PetscErrorCode ierr; 4392779c1a83SBarry Smith 4393779c1a83SBarry Smith PetscFunctionBegin; 4394779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4395779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4396a2243be0SBarry Smith PetscFunctionReturn(0); 4397a2243be0SBarry Smith } 4398c5d6d63eSBarry Smith 4399c5d6d63eSBarry Smith #undef __FUNCT__ 440090431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic" 440190431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat) 44029b8102ccSHong Zhang { 44039b8102ccSHong Zhang PetscErrorCode ierr; 4404a2f3521dSMark F. Adams PetscInt m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs; 44059b8102ccSHong Zhang PetscInt *indx; 44069b8102ccSHong Zhang 44079b8102ccSHong Zhang PetscFunctionBegin; 44089b8102ccSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 44099b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4410a2f3521dSMark F. Adams ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr); 44119b8102ccSHong Zhang if (n == PETSC_DECIDE) { 44129b8102ccSHong Zhang ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 44139b8102ccSHong Zhang } 4414a22543b6SHong Zhang /* Check sum(n) = N */ 4415a95133b1SBarry Smith ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4416a22543b6SHong Zhang if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N); 4417a22543b6SHong Zhang 44189b8102ccSHong Zhang ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 44199b8102ccSHong Zhang rstart -= m; 44209b8102ccSHong Zhang 44219b8102ccSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 44229b8102ccSHong Zhang for (i=0; i<m; i++) { 44230298fd71SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 44249b8102ccSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 44250298fd71SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 44269b8102ccSHong Zhang } 44279b8102ccSHong Zhang 44289b8102ccSHong Zhang ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 44299b8102ccSHong Zhang ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4430a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr); 44319b8102ccSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 44329b8102ccSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 44339b8102ccSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 44349b8102ccSHong Zhang PetscFunctionReturn(0); 44359b8102ccSHong Zhang } 44369b8102ccSHong Zhang 44379b8102ccSHong Zhang #undef __FUNCT__ 443890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric" 443990431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat) 44409b8102ccSHong Zhang { 44419b8102ccSHong Zhang PetscErrorCode ierr; 44429b8102ccSHong Zhang PetscInt m,N,i,rstart,nnz,Ii; 44439b8102ccSHong Zhang PetscInt *indx; 44449b8102ccSHong Zhang PetscScalar *values; 44459b8102ccSHong Zhang 44469b8102ccSHong Zhang PetscFunctionBegin; 44479b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 44480298fd71SBarry Smith ierr = MatGetOwnershipRange(outmat,&rstart,NULL);CHKERRQ(ierr); 44499b8102ccSHong Zhang for (i=0; i<m; i++) { 44509b8102ccSHong Zhang ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44519b8102ccSHong Zhang Ii = i + rstart; 4452a22543b6SHong Zhang ierr = MatSetValues_MPIAIJ(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 44539b8102ccSHong Zhang ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44549b8102ccSHong Zhang } 44559b8102ccSHong Zhang ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44569b8102ccSHong Zhang ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44579b8102ccSHong Zhang PetscFunctionReturn(0); 44589b8102ccSHong Zhang } 44599b8102ccSHong Zhang 44609b8102ccSHong Zhang #undef __FUNCT__ 446190431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ" 4462bc08b0f1SBarry Smith /*@ 446390431a8fSHong Zhang MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential 446451dd7536SBarry Smith matrices from each processor 4465c5d6d63eSBarry Smith 4466c5d6d63eSBarry Smith Collective on MPI_Comm 4467c5d6d63eSBarry Smith 4468c5d6d63eSBarry Smith Input Parameters: 446951dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4470d6bb3c2dSHong Zhang . inmat - the input sequential matrices 44710e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4472d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 447351dd7536SBarry Smith 447451dd7536SBarry Smith Output Parameter: 447551dd7536SBarry Smith . outmat - the parallel matrix generated 4476c5d6d63eSBarry Smith 44777e25d530SSatish Balay Level: advanced 44787e25d530SSatish Balay 4479f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4480c5d6d63eSBarry Smith 4481c5d6d63eSBarry Smith @*/ 448290431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4483c5d6d63eSBarry Smith { 4484dfbe8321SBarry Smith PetscErrorCode ierr; 4485c5d6d63eSBarry Smith 4486c5d6d63eSBarry Smith PetscFunctionBegin; 44879b8102ccSHong Zhang ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4488d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 448990431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr); 44900e36024fSHong Zhang } 449190431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr); 44929b8102ccSHong Zhang ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4493c5d6d63eSBarry Smith PetscFunctionReturn(0); 4494c5d6d63eSBarry Smith } 4495c5d6d63eSBarry Smith 4496c5d6d63eSBarry Smith #undef __FUNCT__ 4497c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4498dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4499c5d6d63eSBarry Smith { 4500dfbe8321SBarry Smith PetscErrorCode ierr; 450132dcc486SBarry Smith PetscMPIInt rank; 4502b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4503de4209c5SBarry Smith size_t len; 4504b1d57f15SBarry Smith const PetscInt *indx; 4505c5d6d63eSBarry Smith PetscViewer out; 4506c5d6d63eSBarry Smith char *name; 4507c5d6d63eSBarry Smith Mat B; 4508b3cc6726SBarry Smith const PetscScalar *values; 4509c5d6d63eSBarry Smith 4510c5d6d63eSBarry Smith PetscFunctionBegin; 4511c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4512c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4513f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4514f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4515f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4516a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 4517f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 45180298fd71SBarry Smith ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr); 4519c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4520c5d6d63eSBarry Smith for (i=0; i<m; i++) { 4521c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4522c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4523c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4524c5d6d63eSBarry Smith } 4525c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4526c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4527c5d6d63eSBarry Smith 4528ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr); 4529c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4530c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4531c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4532852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4533a2ea699eSBarry Smith ierr = PetscFree(name);CHKERRQ(ierr); 4534c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 45356bf464f9SBarry Smith ierr = PetscViewerDestroy(&out);CHKERRQ(ierr); 45366bf464f9SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 4537c5d6d63eSBarry Smith PetscFunctionReturn(0); 4538c5d6d63eSBarry Smith } 4539e5f2cdd8SHong Zhang 454009573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 454151a7d1a8SHong Zhang #undef __FUNCT__ 454251a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 45437087cfbeSBarry Smith PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 454451a7d1a8SHong Zhang { 454551a7d1a8SHong Zhang PetscErrorCode ierr; 4546671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4547776b82aeSLisandro Dalcin PetscContainer container; 454851a7d1a8SHong Zhang 454951a7d1a8SHong Zhang PetscFunctionBegin; 4550671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4551671beff6SHong Zhang if (container) { 4552776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 455351a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 45543e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 45553e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 455651a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 455751a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4558533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 455902c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4560533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 456102c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 456205b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 456305b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 456405b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 45656bf464f9SBarry Smith ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr); 4566bf0cc555SLisandro Dalcin ierr = PetscFree(merge);CHKERRQ(ierr); 4567671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4568671beff6SHong Zhang } 456951a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 457051a7d1a8SHong Zhang PetscFunctionReturn(0); 457151a7d1a8SHong Zhang } 457251a7d1a8SHong Zhang 4573c6db04a5SJed Brown #include <../src/mat/utils/freespace.h> 4574c6db04a5SJed Brown #include <petscbt.h> 45754ebed01fSBarry Smith 4576e5f2cdd8SHong Zhang #undef __FUNCT__ 457790431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric" 457890431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat) 457955d1abb9SHong Zhang { 458055d1abb9SHong Zhang PetscErrorCode ierr; 4581ce94432eSBarry Smith MPI_Comm comm; 458255d1abb9SHong Zhang Mat_SeqAIJ *a =(Mat_SeqAIJ*)seqmat->data; 4583b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4584a2ea699eSBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj; 4585b1d57f15SBarry Smith PetscInt proc,m; 4586b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4587b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4588b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 458955d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 459055d1abb9SHong Zhang MPI_Status *status; 4591a77337e4SBarry Smith MatScalar *aa=a->a; 4592dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 459355d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4594776b82aeSLisandro Dalcin PetscContainer container; 459555d1abb9SHong Zhang 459655d1abb9SHong Zhang PetscFunctionBegin; 4597bedda5b1SHong Zhang ierr = PetscObjectGetComm((PetscObject)mpimat,&comm);CHKERRQ(ierr); 45984ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 45993c2c1871SHong Zhang 460055d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 460155d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 460255d1abb9SHong Zhang 460355d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4604776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 4605bf0cc555SLisandro Dalcin 460655d1abb9SHong Zhang bi = merge->bi; 460755d1abb9SHong Zhang bj = merge->bj; 460855d1abb9SHong Zhang buf_ri = merge->buf_ri; 460955d1abb9SHong Zhang buf_rj = merge->buf_rj; 461055d1abb9SHong Zhang 461155d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 46127a2fc3feSBarry Smith owners = merge->rowmap->range; 461355d1abb9SHong Zhang len_s = merge->len_s; 461455d1abb9SHong Zhang 461555d1abb9SHong Zhang /* send and recv matrix values */ 461655d1abb9SHong Zhang /*-----------------------------*/ 4617357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 461855d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 461955d1abb9SHong Zhang 462055d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 462155d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++) { 462255d1abb9SHong Zhang if (!len_s[proc]) continue; 462355d1abb9SHong Zhang i = owners[proc]; 462455d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 462555d1abb9SHong Zhang k++; 462655d1abb9SHong Zhang } 462755d1abb9SHong Zhang 46280c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 46290c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 463055d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 463155d1abb9SHong Zhang 463255d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 463355d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 463455d1abb9SHong Zhang 463555d1abb9SHong Zhang /* insert mat values of mpimat */ 463655d1abb9SHong Zhang /*----------------------------*/ 4637a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 46380572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 463955d1abb9SHong Zhang 464055d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { 464155d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 464255d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 464355d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 464455d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 464555d1abb9SHong Zhang } 464655d1abb9SHong Zhang 464755d1abb9SHong Zhang /* set values of ba */ 46487a2fc3feSBarry Smith m = merge->rowmap->n; 464955d1abb9SHong Zhang for (i=0; i<m; i++) { 465055d1abb9SHong Zhang arow = owners[rank] + i; 465155d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 465255d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4653a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 465455d1abb9SHong Zhang 465555d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 465655d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 465755d1abb9SHong Zhang aj = a->j + ai[arow]; 465855d1abb9SHong Zhang aa = a->a + ai[arow]; 465955d1abb9SHong Zhang nextaj = 0; 466055d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 466155d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 466255d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 466355d1abb9SHong Zhang } 466455d1abb9SHong Zhang } 466555d1abb9SHong Zhang 466655d1abb9SHong Zhang /* add received vals into ba */ 466755d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 466855d1abb9SHong Zhang /* i-th row */ 466955d1abb9SHong Zhang if (i == *nextrow[k]) { 467055d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 467155d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 467255d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 467355d1abb9SHong Zhang nextaj = 0; 467455d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 467555d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 467655d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 467755d1abb9SHong Zhang } 467855d1abb9SHong Zhang } 467955d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 468055d1abb9SHong Zhang } 468155d1abb9SHong Zhang } 468255d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 468355d1abb9SHong Zhang } 468455d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 468555d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 468655d1abb9SHong Zhang 4687533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 468855d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 468955d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 46901d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 46914ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 469255d1abb9SHong Zhang PetscFunctionReturn(0); 469355d1abb9SHong Zhang } 469438f152feSBarry Smith 46956bc0bbbfSBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat); 46966bc0bbbfSBarry Smith 469738f152feSBarry Smith #undef __FUNCT__ 469890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic" 469990431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4700e5f2cdd8SHong Zhang { 4701f08fae4eSHong Zhang PetscErrorCode ierr; 470255a3bba9SHong Zhang Mat B_mpi; 4703c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4704b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4705b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4706d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4707a2f3521dSMark F. Adams PetscInt len,proc,*dnz,*onz,bs,cbs; 4708b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4709b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 471055d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 471158cb9c82SHong Zhang MPI_Status *status; 47120298fd71SBarry Smith PetscFreeSpaceList free_space=NULL,current_space=NULL; 4713be0fcf8dSHong Zhang PetscBT lnkbt; 471451a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4715776b82aeSLisandro Dalcin PetscContainer container; 471602c68681SHong Zhang 4717e5f2cdd8SHong Zhang PetscFunctionBegin; 47184ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 47193c2c1871SHong Zhang 472038f152feSBarry Smith /* make sure it is a PETSc comm */ 47210298fd71SBarry Smith ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr); 4722e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4723e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 472455d1abb9SHong Zhang 472551a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4726c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4727e5f2cdd8SHong Zhang 47286abd8857SHong Zhang /* determine row ownership */ 4729f08fae4eSHong Zhang /*---------------------------------------------------------*/ 473026283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 473126283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 473226283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 473326283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 473426283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4735b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4736b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 473755d1abb9SHong Zhang 47387a2fc3feSBarry Smith m = merge->rowmap->n; 47397a2fc3feSBarry Smith owners = merge->rowmap->range; 47406abd8857SHong Zhang 47416abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 47426abd8857SHong Zhang /*---------------------------------------------------------*/ 47433e06a4e6SHong Zhang len_s = merge->len_s; 474451a7d1a8SHong Zhang 47452257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4746c2234fe3SHong Zhang merge->nsend = 0; 4747409913e3SHong Zhang for (proc=0; proc<size; proc++) { 47482257cef7SHong Zhang len_si[proc] = 0; 47493e06a4e6SHong Zhang if (proc == rank) { 47506abd8857SHong Zhang len_s[proc] = 0; 47513e06a4e6SHong Zhang } else { 475202c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 47533e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 47543e06a4e6SHong Zhang } 47553e06a4e6SHong Zhang if (len_s[proc]) { 4756c2234fe3SHong Zhang merge->nsend++; 47572257cef7SHong Zhang nrows = 0; 47582257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 47592257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 47602257cef7SHong Zhang } 47612257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 47622257cef7SHong Zhang len += len_si[proc]; 4763409913e3SHong Zhang } 476458cb9c82SHong Zhang } 4765409913e3SHong Zhang 47662257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 47672257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 47680298fd71SBarry Smith ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 476955d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4770671beff6SHong Zhang 47713e06a4e6SHong Zhang /* post the Irecv of j-structure */ 47723e06a4e6SHong Zhang /*-------------------------------*/ 47732c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 47743e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 477502c68681SHong Zhang 47763e06a4e6SHong Zhang /* post the Isend of j-structure */ 4777affca5deSHong Zhang /*--------------------------------*/ 47781d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 47793e06a4e6SHong Zhang 47802257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++) { 4781409913e3SHong Zhang if (!len_s[proc]) continue; 478202c68681SHong Zhang i = owners[proc]; 4783b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 478451a7d1a8SHong Zhang k++; 478551a7d1a8SHong Zhang } 478651a7d1a8SHong Zhang 47873e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 47883e06a4e6SHong Zhang /*------------------------------------------------*/ 47890c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 47900c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 479102c68681SHong Zhang 479202c68681SHong Zhang /* send and recv i-structure */ 479302c68681SHong Zhang /*---------------------------*/ 47942c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 479502c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 479602c68681SHong Zhang 4797b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 47983e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 47992257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++) { 480002c68681SHong Zhang if (!len_s[proc]) continue; 48013e06a4e6SHong Zhang /* form outgoing message for i-structure: 48023e06a4e6SHong Zhang buf_si[0]: nrows to be sent 48033e06a4e6SHong Zhang [1:nrows]: row index (global) 48043e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 48053e06a4e6SHong Zhang */ 48063e06a4e6SHong Zhang /*-------------------------------------------*/ 48072257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 48083e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 48093e06a4e6SHong Zhang buf_si[0] = nrows; 48103e06a4e6SHong Zhang buf_si_i[0] = 0; 48113e06a4e6SHong Zhang nrows = 0; 48123e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 48133e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 48143e06a4e6SHong Zhang if (anzi) { 48153e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 48163e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 48173e06a4e6SHong Zhang nrows++; 48183e06a4e6SHong Zhang } 48193e06a4e6SHong Zhang } 4820b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 482102c68681SHong Zhang k++; 48222257cef7SHong Zhang buf_si += len_si[proc]; 482302c68681SHong Zhang } 48242257cef7SHong Zhang 48250c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 48260c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 482702c68681SHong Zhang 4828ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 48293e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++) { 4830ae15b995SBarry 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); 48313e06a4e6SHong Zhang } 48323e06a4e6SHong Zhang 48333e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 483402c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 483502c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 48361d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 48372257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 48383e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4839bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 484058cb9c82SHong Zhang 4841bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4842bcc1bcd5SHong Zhang /*----------------------------------------------*/ 484358cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4844b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 484558cb9c82SHong Zhang bi[0] = 0; 484658cb9c82SHong Zhang 4847be0fcf8dSHong Zhang /* create and initialize a linked list */ 4848be0fcf8dSHong Zhang nlnk = N+1; 4849be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 485058cb9c82SHong Zhang 4851bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 4852bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4853a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 48542205254eSKarl Rupp 485558cb9c82SHong Zhang current_space = free_space; 485658cb9c82SHong Zhang 4857bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 48580572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 48591d79065fSBarry Smith 48603e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++) { 48612257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 48623e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 48633e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 48642257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 48653e06a4e6SHong Zhang } 48662257cef7SHong Zhang 4867bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4868bcc1bcd5SHong Zhang len = 0; 486958cb9c82SHong Zhang for (i=0; i<m; i++) { 487058cb9c82SHong Zhang bnzi = 0; 487158cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 487258cb9c82SHong Zhang arow = owners[rank] + i; 487358cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 487458cb9c82SHong Zhang aj = a->j + ai[arow]; 4875dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 487658cb9c82SHong Zhang bnzi += nlnk; 487758cb9c82SHong Zhang /* add received col data into lnk */ 487851a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 487955d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 48803e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 48813e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 4882dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 48833e06a4e6SHong Zhang bnzi += nlnk; 48843e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 48853e06a4e6SHong Zhang } 488658cb9c82SHong Zhang } 4887bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 488858cb9c82SHong Zhang 488958cb9c82SHong Zhang /* if free space is not available, make more free space */ 489058cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 48914238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 489258cb9c82SHong Zhang nspacedouble++; 489358cb9c82SHong Zhang } 489458cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4895be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4896bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4897bcc1bcd5SHong Zhang 489858cb9c82SHong Zhang current_space->array += bnzi; 489958cb9c82SHong Zhang current_space->local_used += bnzi; 490058cb9c82SHong Zhang current_space->local_remaining -= bnzi; 490158cb9c82SHong Zhang 490258cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 490358cb9c82SHong Zhang } 4904bcc1bcd5SHong Zhang 49051d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4906bcc1bcd5SHong Zhang 4907b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4908a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4909be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4910409913e3SHong Zhang 4911bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4912bcc1bcd5SHong Zhang /*---------------------------------------*/ 4913a2f3521dSMark F. Adams ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr); 4914f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 491554b84b50SHong Zhang if (n==PETSC_DECIDE) { 4916f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 491754b84b50SHong Zhang } else { 4918f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 491954b84b50SHong Zhang } 4920a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr); 4921bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4922bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4923bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 49247e63b356SHong Zhang ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 492558cb9c82SHong Zhang 492690431a8fSHong Zhang /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */ 49276abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4928affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4929affca5deSHong Zhang merge->bi = bi; 4930affca5deSHong Zhang merge->bj = bj; 493102c68681SHong Zhang merge->buf_ri = buf_ri; 493202c68681SHong Zhang merge->buf_rj = buf_rj; 49330298fd71SBarry Smith merge->coi = NULL; 49340298fd71SBarry Smith merge->coj = NULL; 49350298fd71SBarry Smith merge->owners_co = NULL; 4936affca5deSHong Zhang 4937bf0cc555SLisandro Dalcin ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 4938bf0cc555SLisandro Dalcin 4939affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4940776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4941776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4942affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4943bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 4944affca5deSHong Zhang *mpimat = B_mpi; 494538f152feSBarry Smith 49464ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4947e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4948e5f2cdd8SHong Zhang } 494925616d81SHong Zhang 495038f152feSBarry Smith #undef __FUNCT__ 495190431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ" 4952d4036a1aSHong Zhang /*@C 495390431a8fSHong Zhang MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential 4954d4036a1aSHong Zhang matrices from each processor 4955d4036a1aSHong Zhang 4956d4036a1aSHong Zhang Collective on MPI_Comm 4957d4036a1aSHong Zhang 4958d4036a1aSHong Zhang Input Parameters: 4959d4036a1aSHong Zhang + comm - the communicators the parallel matrix will live on 4960d4036a1aSHong Zhang . seqmat - the input sequential matrices 4961d4036a1aSHong Zhang . m - number of local rows (or PETSC_DECIDE) 4962d4036a1aSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4963d4036a1aSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4964d4036a1aSHong Zhang 4965d4036a1aSHong Zhang Output Parameter: 4966d4036a1aSHong Zhang . mpimat - the parallel matrix generated 4967d4036a1aSHong Zhang 4968d4036a1aSHong Zhang Level: advanced 4969d4036a1aSHong Zhang 4970d4036a1aSHong Zhang Notes: 4971d4036a1aSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4972d4036a1aSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4973d4036a1aSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4974d4036a1aSHong Zhang @*/ 497590431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 497655d1abb9SHong Zhang { 497755d1abb9SHong Zhang PetscErrorCode ierr; 49787e63b356SHong Zhang PetscMPIInt size; 497955d1abb9SHong Zhang 498055d1abb9SHong Zhang PetscFunctionBegin; 49817e63b356SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 49827e63b356SHong Zhang if (size == 1) { 49837e63b356SHong Zhang ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49847e63b356SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 49857e63b356SHong Zhang ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr); 49867e63b356SHong Zhang } else { 49877e63b356SHong Zhang ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 49887e63b356SHong Zhang } 49897e63b356SHong Zhang ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49907e63b356SHong Zhang PetscFunctionReturn(0); 49917e63b356SHong Zhang } 49924ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 499355d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 499490431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 499555d1abb9SHong Zhang } 499690431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr); 49974ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 499855d1abb9SHong Zhang PetscFunctionReturn(0); 499955d1abb9SHong Zhang } 50004ebed01fSBarry Smith 500125616d81SHong Zhang #undef __FUNCT__ 50024a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat" 5003bc08b0f1SBarry Smith /*@ 50044a2b5492SBarry Smith MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with 50058661ff28SBarry Smith mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained 50068661ff28SBarry Smith with MatGetSize() 500725616d81SHong Zhang 500832fba14fSHong Zhang Not Collective 500925616d81SHong Zhang 501025616d81SHong Zhang Input Parameters: 501125616d81SHong Zhang + A - the matrix 501225616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 501325616d81SHong Zhang 501425616d81SHong Zhang Output Parameter: 501525616d81SHong Zhang . A_loc - the local sequential matrix generated 501625616d81SHong Zhang 501725616d81SHong Zhang Level: developer 501825616d81SHong Zhang 5019ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed() 50208661ff28SBarry Smith 502125616d81SHong Zhang @*/ 50224a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 502325616d81SHong Zhang { 502425616d81SHong Zhang PetscErrorCode ierr; 502501b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 502601b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 502701b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 5028a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 5029a77337e4SBarry Smith PetscScalar *ca; 5030d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 50315a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 50328661ff28SBarry Smith PetscBool match; 503325616d81SHong Zhang 503425616d81SHong Zhang PetscFunctionBegin; 5035251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 5036ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 50374ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 503801b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5039dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 5040dea91ad1SHong Zhang ci[0] = 0; 504101b7ae99SHong Zhang for (i=0; i<am; i++) { 5042dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 504301b7ae99SHong Zhang } 5044dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 5045dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 5046dea91ad1SHong Zhang k = 0; 504701b7ae99SHong Zhang for (i=0; i<am; i++) { 50485a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50495a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 505001b7ae99SHong Zhang /* off-diagonal portion of A */ 50515a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50525a7d977cSHong Zhang col = cmap[*bj]; 50535a7d977cSHong Zhang if (col >= cstart) break; 50545a7d977cSHong Zhang cj[k] = col; bj++; 50555a7d977cSHong Zhang ca[k++] = *ba++; 50565a7d977cSHong Zhang } 50575a7d977cSHong Zhang /* diagonal portion of A */ 50585a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 50595a7d977cSHong Zhang cj[k] = cstart + *aj++; 50605a7d977cSHong Zhang ca[k++] = *aa++; 50615a7d977cSHong Zhang } 50625a7d977cSHong Zhang /* off-diagonal portion of A */ 50635a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 50645a7d977cSHong Zhang cj[k] = cmap[*bj++]; 50655a7d977cSHong Zhang ca[k++] = *ba++; 50665a7d977cSHong Zhang } 506725616d81SHong Zhang } 5068dea91ad1SHong Zhang /* put together the new matrix */ 5069d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 5070dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5071dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 5072dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 5073e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 5074e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 5075dea91ad1SHong Zhang mat->nonew = 0; 50765a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 50775a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 5078a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 50795a7d977cSHong Zhang for (i=0; i<am; i++) { 50805a7d977cSHong Zhang /* off-diagonal portion of A */ 50815a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50825a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50835a7d977cSHong Zhang col = cmap[*bj]; 50845a7d977cSHong Zhang if (col >= cstart) break; 5085a77337e4SBarry Smith *cam++ = *ba++; bj++; 50865a7d977cSHong Zhang } 50875a7d977cSHong Zhang /* diagonal portion of A */ 5088ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 5089a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 50905a7d977cSHong Zhang /* off-diagonal portion of A */ 5091f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 5092a77337e4SBarry Smith *cam++ = *ba++; bj++; 5093f33d1a9aSHong Zhang } 50945a7d977cSHong Zhang } 50958661ff28SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 50964ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 509725616d81SHong Zhang PetscFunctionReturn(0); 509825616d81SHong Zhang } 509925616d81SHong Zhang 510032fba14fSHong Zhang #undef __FUNCT__ 51014a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed" 510232fba14fSHong Zhang /*@C 5103ba264940SBarry Smith MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns 510432fba14fSHong Zhang 510532fba14fSHong Zhang Not Collective 510632fba14fSHong Zhang 510732fba14fSHong Zhang Input Parameters: 510832fba14fSHong Zhang + A - the matrix 510932fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 51100298fd71SBarry Smith - row, col - index sets of rows and columns to extract (or NULL) 511132fba14fSHong Zhang 511232fba14fSHong Zhang Output Parameter: 511332fba14fSHong Zhang . A_loc - the local sequential matrix generated 511432fba14fSHong Zhang 511532fba14fSHong Zhang Level: developer 511632fba14fSHong Zhang 5117ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat() 5118ba264940SBarry Smith 511932fba14fSHong Zhang @*/ 51204a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 512132fba14fSHong Zhang { 512232fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 512332fba14fSHong Zhang PetscErrorCode ierr; 512432fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 512532fba14fSHong Zhang IS isrowa,iscola; 512632fba14fSHong Zhang Mat *aloc; 51274a2b5492SBarry Smith PetscBool match; 512832fba14fSHong Zhang 512932fba14fSHong Zhang PetscFunctionBegin; 5130251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 5131ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 51324ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 513332fba14fSHong Zhang if (!row) { 5134d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 513532fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 513632fba14fSHong Zhang } else { 513732fba14fSHong Zhang isrowa = *row; 513832fba14fSHong Zhang } 513932fba14fSHong Zhang if (!col) { 5140d0f46423SBarry Smith start = A->cmap->rstart; 514132fba14fSHong Zhang cmap = a->garray; 5142d0f46423SBarry Smith nzA = a->A->cmap->n; 5143d0f46423SBarry Smith nzB = a->B->cmap->n; 514432fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 514532fba14fSHong Zhang ncols = 0; 514632fba14fSHong Zhang for (i=0; i<nzB; i++) { 514732fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 514832fba14fSHong Zhang else break; 514932fba14fSHong Zhang } 515032fba14fSHong Zhang imark = i; 515132fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 515232fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 5153d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 515432fba14fSHong Zhang } else { 515532fba14fSHong Zhang iscola = *col; 515632fba14fSHong Zhang } 515732fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX) { 515832fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 515932fba14fSHong Zhang aloc[0] = *A_loc; 516032fba14fSHong Zhang } 516132fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 516232fba14fSHong Zhang *A_loc = aloc[0]; 516332fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 516432fba14fSHong Zhang if (!row) { 51656bf464f9SBarry Smith ierr = ISDestroy(&isrowa);CHKERRQ(ierr); 516632fba14fSHong Zhang } 516732fba14fSHong Zhang if (!col) { 51686bf464f9SBarry Smith ierr = ISDestroy(&iscola);CHKERRQ(ierr); 516932fba14fSHong Zhang } 51704ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 517132fba14fSHong Zhang PetscFunctionReturn(0); 517232fba14fSHong Zhang } 517332fba14fSHong Zhang 517425616d81SHong Zhang #undef __FUNCT__ 517525616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 517625616d81SHong Zhang /*@C 517732fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 517825616d81SHong Zhang 517925616d81SHong Zhang Collective on Mat 518025616d81SHong Zhang 518125616d81SHong Zhang Input Parameters: 5182e240928fSHong Zhang + A,B - the matrices in mpiaij format 518325616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 51840298fd71SBarry Smith - rowb, colb - index sets of rows and columns of B to extract (or NULL) 518525616d81SHong Zhang 518625616d81SHong Zhang Output Parameter: 518725616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 518825616d81SHong Zhang - B_seq - the sequential matrix generated 518925616d81SHong Zhang 519025616d81SHong Zhang Level: developer 519125616d81SHong Zhang 519225616d81SHong Zhang @*/ 519366bfb163SHong Zhang PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq) 519425616d81SHong Zhang { 5195899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 519625616d81SHong Zhang PetscErrorCode ierr; 5197b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 519825616d81SHong Zhang IS isrowb,iscolb; 51990298fd71SBarry Smith Mat *bseq=NULL; 520025616d81SHong Zhang 520125616d81SHong Zhang PetscFunctionBegin; 5202d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5203e32f2f54SBarry 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); 520425616d81SHong Zhang } 52054ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 520625616d81SHong Zhang 520725616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5208d0f46423SBarry Smith start = A->cmap->rstart; 520925616d81SHong Zhang cmap = a->garray; 5210d0f46423SBarry Smith nzA = a->A->cmap->n; 5211d0f46423SBarry Smith nzB = a->B->cmap->n; 5212b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 521325616d81SHong Zhang ncols = 0; 52140390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 521525616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 521625616d81SHong Zhang else break; 521725616d81SHong Zhang } 521825616d81SHong Zhang imark = i; 52190390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 52200390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 5221d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 5222d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 522325616d81SHong Zhang } else { 5224e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 522525616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 522625616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 522725616d81SHong Zhang bseq[0] = *B_seq; 522825616d81SHong Zhang } 522925616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 523025616d81SHong Zhang *B_seq = bseq[0]; 523125616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 523225616d81SHong Zhang if (!rowb) { 52336bf464f9SBarry Smith ierr = ISDestroy(&isrowb);CHKERRQ(ierr); 523425616d81SHong Zhang } else { 523525616d81SHong Zhang *rowb = isrowb; 523625616d81SHong Zhang } 523725616d81SHong Zhang if (!colb) { 52386bf464f9SBarry Smith ierr = ISDestroy(&iscolb);CHKERRQ(ierr); 523925616d81SHong Zhang } else { 524025616d81SHong Zhang *colb = iscolb; 524125616d81SHong Zhang } 52424ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 524325616d81SHong Zhang PetscFunctionReturn(0); 524425616d81SHong Zhang } 5245429d309bSHong Zhang 5246a61c8c0fSHong Zhang #undef __FUNCT__ 5247f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ" 5248f8487c73SHong Zhang /* 5249f8487c73SHong Zhang MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 525001b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 5251429d309bSHong Zhang 5252429d309bSHong Zhang Collective on Mat 5253429d309bSHong Zhang 5254429d309bSHong Zhang Input Parameters: 5255429d309bSHong Zhang + A,B - the matrices in mpiaij format 5256598bc09dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 5257429d309bSHong Zhang 5258429d309bSHong Zhang Output Parameter: 52590298fd71SBarry Smith + startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL) 52600298fd71SBarry Smith . startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL) 52610298fd71SBarry Smith . bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL) 5262598bc09dSHong Zhang - B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N 5263429d309bSHong Zhang 5264429d309bSHong Zhang Level: developer 5265429d309bSHong Zhang 5266f8487c73SHong Zhang */ 5267b7f45c76SHong Zhang PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 5268429d309bSHong Zhang { 5269a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 5270429d309bSHong Zhang PetscErrorCode ierr; 5271899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 527287025532SHong Zhang Mat_SeqAIJ *b_oth; 5273a6b2eed2SHong Zhang VecScatter ctx =a->Mvctx; 5274ce94432eSBarry Smith MPI_Comm comm; 52757adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 5276d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 5277dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 5278dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 5279e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 52800298fd71SBarry Smith MPI_Request *rwaits = NULL,*swaits = NULL; 528187025532SHong Zhang MPI_Status *sstatus,rstatus; 5282aa5bb8c0SSatish Balay PetscMPIInt jj; 5283e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 5284ba8c8a56SBarry Smith PetscScalar *vals; 5285429d309bSHong Zhang 5286429d309bSHong Zhang PetscFunctionBegin; 5287ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 5288d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5289e32f2f54SBarry 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); 5290429d309bSHong Zhang } 52914ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5292a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5293a6b2eed2SHong Zhang 5294a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5295a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5296e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5297e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5298a6b2eed2SHong Zhang nrecvs = gen_from->n; 5299a6b2eed2SHong Zhang nsends = gen_to->n; 5300d7ee0231SBarry Smith 5301d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 5302a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5303a6b2eed2SHong Zhang sstarts = gen_to->starts; 5304a6b2eed2SHong Zhang sprocs = gen_to->procs; 5305a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5306e42f35eeSHong Zhang sbs = gen_to->bs; 5307e42f35eeSHong Zhang rstarts = gen_from->starts; 5308e42f35eeSHong Zhang rprocs = gen_from->procs; 5309e42f35eeSHong Zhang rbs = gen_from->bs; 5310429d309bSHong Zhang 5311b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5312429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5313a6b2eed2SHong Zhang /* i-array */ 5314a6b2eed2SHong Zhang /*---------*/ 5315a6b2eed2SHong Zhang /* post receives */ 5316a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5317e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5318e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 531987025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5320429d309bSHong Zhang } 5321a6b2eed2SHong Zhang 5322a6b2eed2SHong Zhang /* pack the outgoing message */ 53231d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 53242205254eSKarl Rupp 53252205254eSKarl Rupp sstartsj[0] = 0; 53262205254eSKarl Rupp rstartsj[0] = 0; 5327a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5328a6b2eed2SHong Zhang k = 0; 5329a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5330e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5331e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 533287025532SHong Zhang for (j=0; j<nrows; j++) { 5333d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5334e42f35eeSHong Zhang for (l=0; l<sbs; l++) { 53350298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); /* rowlength */ 53362205254eSKarl Rupp 5337e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 53382205254eSKarl Rupp 5339e42f35eeSHong Zhang len += ncols; 53400298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); 5341e42f35eeSHong Zhang } 5342a6b2eed2SHong Zhang k++; 5343429d309bSHong Zhang } 5344e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 53452205254eSKarl Rupp 5346dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5347429d309bSHong Zhang } 534887025532SHong Zhang /* recvs and sends of i-array are completed */ 534987025532SHong Zhang i = nrecvs; 535087025532SHong Zhang while (i--) { 5351aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 535287025532SHong Zhang } 53530c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5354e42f35eeSHong Zhang 5355a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5356a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 5357a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 5358a6b2eed2SHong Zhang 535987025532SHong Zhang /* create i-array of B_oth */ 536087025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 53612205254eSKarl Rupp 536287025532SHong Zhang b_othi[0] = 0; 5363a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5364a6b2eed2SHong Zhang k = 0; 5365a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5366fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5367e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 536887025532SHong Zhang for (j=0; j<nrows; j++) { 536987025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5370a6b2eed2SHong Zhang len += rowlen[j]; k++; 5371a6b2eed2SHong Zhang } 5372dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5373a6b2eed2SHong Zhang } 5374a6b2eed2SHong Zhang 537587025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 537687025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 5377dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 5378a6b2eed2SHong Zhang 537987025532SHong Zhang /* j-array */ 538087025532SHong Zhang /*---------*/ 5381a6b2eed2SHong Zhang /* post receives of j-array */ 5382a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 538387025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 538487025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5385a6b2eed2SHong Zhang } 5386e42f35eeSHong Zhang 5387e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5388a6b2eed2SHong Zhang k = 0; 5389a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5390e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5391a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 539287025532SHong Zhang for (j=0; j<nrows; j++) { 5393d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5394e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 53950298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5396a6b2eed2SHong Zhang for (l=0; l<ncols; l++) { 5397a6b2eed2SHong Zhang *bufJ++ = cols[l]; 539887025532SHong Zhang } 53990298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5400e42f35eeSHong Zhang } 540187025532SHong Zhang } 540287025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 540387025532SHong Zhang } 540487025532SHong Zhang 540587025532SHong Zhang /* recvs and sends of j-array are completed */ 540687025532SHong Zhang i = nrecvs; 540787025532SHong Zhang while (i--) { 5408aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 540987025532SHong Zhang } 54100c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 541187025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 5412b7f45c76SHong Zhang sstartsj = *startsj_s; 54131d79065fSBarry Smith rstartsj = *startsj_r; 541487025532SHong Zhang bufa = *bufa_ptr; 541587025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 541687025532SHong Zhang b_otha = b_oth->a; 5417f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 541887025532SHong Zhang 541987025532SHong Zhang /* a-array */ 542087025532SHong Zhang /*---------*/ 542187025532SHong Zhang /* post receives of a-array */ 542287025532SHong Zhang for (i=0; i<nrecvs; i++) { 542387025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 542487025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 542587025532SHong Zhang } 5426e42f35eeSHong Zhang 5427e42f35eeSHong Zhang /* pack the outgoing message a-array */ 542887025532SHong Zhang k = 0; 542987025532SHong Zhang for (i=0; i<nsends; i++) { 5430e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 543187025532SHong Zhang bufA = bufa+sstartsj[i]; 543287025532SHong Zhang for (j=0; j<nrows; j++) { 5433d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5434e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 54350298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 543687025532SHong Zhang for (l=0; l<ncols; l++) { 5437a6b2eed2SHong Zhang *bufA++ = vals[l]; 5438a6b2eed2SHong Zhang } 54390298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 5440e42f35eeSHong Zhang } 5441a6b2eed2SHong Zhang } 544287025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5443a6b2eed2SHong Zhang } 544487025532SHong Zhang /* recvs and sends of a-array are completed */ 544587025532SHong Zhang i = nrecvs; 544687025532SHong Zhang while (i--) { 5447aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 544887025532SHong Zhang } 54490c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5450d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5451a6b2eed2SHong Zhang 545287025532SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5453a6b2eed2SHong Zhang /* put together the new matrix */ 5454d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5455a6b2eed2SHong Zhang 5456a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5457a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 545887025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 5459e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5460e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 546187025532SHong Zhang b_oth->nonew = 0; 5462a6b2eed2SHong Zhang 5463a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5464b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) { 54651d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5466dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5467dea91ad1SHong Zhang } else { 5468b7f45c76SHong Zhang *startsj_s = sstartsj; 54691d79065fSBarry Smith *startsj_r = rstartsj; 547087025532SHong Zhang *bufa_ptr = bufa; 547187025532SHong Zhang } 5472dea91ad1SHong Zhang } 54734ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5474429d309bSHong Zhang PetscFunctionReturn(0); 5475429d309bSHong Zhang } 5476ccd8e176SBarry Smith 547743eb5e2fSMatthew Knepley #undef __FUNCT__ 547843eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 547943eb5e2fSMatthew Knepley /*@C 548043eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 548143eb5e2fSMatthew Knepley 548243eb5e2fSMatthew Knepley Not Collective 548343eb5e2fSMatthew Knepley 548443eb5e2fSMatthew Knepley Input Parameters: 548543eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 548643eb5e2fSMatthew Knepley 548743eb5e2fSMatthew Knepley Output Parameter: 548843eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 548943eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 549043eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 549143eb5e2fSMatthew Knepley 549243eb5e2fSMatthew Knepley Level: developer 549343eb5e2fSMatthew Knepley 549443eb5e2fSMatthew Knepley @*/ 549543eb5e2fSMatthew Knepley #if defined(PETSC_USE_CTABLE) 54967087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 549743eb5e2fSMatthew Knepley #else 54987087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 549943eb5e2fSMatthew Knepley #endif 550043eb5e2fSMatthew Knepley { 550143eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 550243eb5e2fSMatthew Knepley 550343eb5e2fSMatthew Knepley PetscFunctionBegin; 55040700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5505e414b56bSJed Brown PetscValidPointer(lvec, 2); 5506e414b56bSJed Brown PetscValidPointer(colmap, 3); 5507e414b56bSJed Brown PetscValidPointer(multScatter, 4); 550843eb5e2fSMatthew Knepley a = (Mat_MPIAIJ*) A->data; 550943eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 551043eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 551143eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 551243eb5e2fSMatthew Knepley PetscFunctionReturn(0); 551343eb5e2fSMatthew Knepley } 551443eb5e2fSMatthew Knepley 551517667f90SBarry Smith EXTERN_C_BEGIN 551619fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*); 551719fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*); 551819fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*); 551917667f90SBarry Smith EXTERN_C_END 552017667f90SBarry Smith 5521fc4dec0aSBarry Smith #undef __FUNCT__ 5522fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5523fc4dec0aSBarry Smith /* 5524fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5525fc4dec0aSBarry Smith 5526fc4dec0aSBarry Smith n p p 5527fc4dec0aSBarry Smith ( ) ( ) ( ) 5528fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5529fc4dec0aSBarry Smith ( ) ( ) ( ) 5530fc4dec0aSBarry Smith 5531fc4dec0aSBarry Smith */ 5532fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5533fc4dec0aSBarry Smith { 5534fc4dec0aSBarry Smith PetscErrorCode ierr; 5535fc4dec0aSBarry Smith Mat At,Bt,Ct; 5536fc4dec0aSBarry Smith 5537fc4dec0aSBarry Smith PetscFunctionBegin; 5538fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5539fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5540fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 55416bf464f9SBarry Smith ierr = MatDestroy(&At);CHKERRQ(ierr); 55426bf464f9SBarry Smith ierr = MatDestroy(&Bt);CHKERRQ(ierr); 5543fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 55446bf464f9SBarry Smith ierr = MatDestroy(&Ct);CHKERRQ(ierr); 5545fc4dec0aSBarry Smith PetscFunctionReturn(0); 5546fc4dec0aSBarry Smith } 5547fc4dec0aSBarry Smith 5548fc4dec0aSBarry Smith #undef __FUNCT__ 5549fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5550fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5551fc4dec0aSBarry Smith { 5552fc4dec0aSBarry Smith PetscErrorCode ierr; 5553d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5554fc4dec0aSBarry Smith Mat Cmat; 5555fc4dec0aSBarry Smith 5556fc4dec0aSBarry Smith PetscFunctionBegin; 5557e32f2f54SBarry 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); 5558ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr); 5559fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5560a2f3521dSMark F. Adams ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 5561fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 55620298fd71SBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr); 556338556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 556438556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5565f75ecaa4SHong Zhang 5566f75ecaa4SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ; 55672205254eSKarl Rupp 5568fc4dec0aSBarry Smith *C = Cmat; 5569fc4dec0aSBarry Smith PetscFunctionReturn(0); 5570fc4dec0aSBarry Smith } 5571fc4dec0aSBarry Smith 5572fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5573fc4dec0aSBarry Smith #undef __FUNCT__ 5574fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5575fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5576fc4dec0aSBarry Smith { 5577fc4dec0aSBarry Smith PetscErrorCode ierr; 5578fc4dec0aSBarry Smith 5579fc4dec0aSBarry Smith PetscFunctionBegin; 5580fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX) { 5581fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5582fc4dec0aSBarry Smith } 5583fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5584fc4dec0aSBarry Smith PetscFunctionReturn(0); 5585fc4dec0aSBarry Smith } 5586fc4dec0aSBarry Smith 55875c9eb25fSBarry Smith EXTERN_C_BEGIN 5588611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5589bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5590611f576cSBarry Smith #endif 55913bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 55923bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 55933bf14a46SMatthew Knepley #endif 5594611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 55955c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5596611f576cSBarry Smith #endif 559717f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 559817f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*); 559917f1a0eaSHong Zhang #endif 56005c9eb25fSBarry Smith EXTERN_C_END 56015c9eb25fSBarry Smith 5602ccd8e176SBarry Smith /*MC 5603ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5604ccd8e176SBarry Smith 5605ccd8e176SBarry Smith Options Database Keys: 5606ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5607ccd8e176SBarry Smith 5608ccd8e176SBarry Smith Level: beginner 5609ccd8e176SBarry Smith 561069b1f4b7SBarry Smith .seealso: MatCreateAIJ() 5611ccd8e176SBarry Smith M*/ 5612ccd8e176SBarry Smith 5613ccd8e176SBarry Smith EXTERN_C_BEGIN 5614ccd8e176SBarry Smith #undef __FUNCT__ 5615ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 56167087cfbeSBarry Smith PetscErrorCode MatCreate_MPIAIJ(Mat B) 5617ccd8e176SBarry Smith { 5618ccd8e176SBarry Smith Mat_MPIAIJ *b; 5619ccd8e176SBarry Smith PetscErrorCode ierr; 5620ccd8e176SBarry Smith PetscMPIInt size; 5621ccd8e176SBarry Smith 5622ccd8e176SBarry Smith PetscFunctionBegin; 5623ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 56242205254eSKarl Rupp 562538f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5626ccd8e176SBarry Smith B->data = (void*)b; 5627ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5628ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5629ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5630ccd8e176SBarry Smith b->size = size; 56312205254eSKarl Rupp 5632ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr); 5633ccd8e176SBarry Smith 5634ccd8e176SBarry Smith /* build cache for off array entries formed */ 5635ce94432eSBarry Smith ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr); 56362205254eSKarl Rupp 5637ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5638ccd8e176SBarry Smith b->colmap = 0; 5639ccd8e176SBarry Smith b->garray = 0; 5640ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5641ccd8e176SBarry Smith 5642ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 56430298fd71SBarry Smith b->lvec = NULL; 56440298fd71SBarry Smith b->Mvctx = NULL; 5645ccd8e176SBarry Smith 5646ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5647ccd8e176SBarry Smith b->rowindices = 0; 5648ccd8e176SBarry Smith b->rowvalues = 0; 5649ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5650ccd8e176SBarry Smith 5651bbf3fe20SPaul Mullowney /* flexible pointer used in CUSP/CUSPARSE classes */ 56520298fd71SBarry Smith b->spptr = NULL; 5653f60c3dc2SHong Zhang 5654611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 565500de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C","MatGetFactor_aij_mumps",MatGetFactor_aij_mumps);CHKERRQ(ierr); 5656611f576cSBarry Smith #endif 56573bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 565800de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C","MatGetFactor_mpiaij_pastix",MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 56593bf14a46SMatthew Knepley #endif 5660611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 566100de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C","MatGetFactor_mpiaij_superlu_dist",MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5662611f576cSBarry Smith #endif 566317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 566400de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C","MatGetFactor_aij_clique",MatGetFactor_aij_clique);CHKERRQ(ierr); 566517f1a0eaSHong Zhang #endif 566600de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C","MatStoreValues_MPIAIJ",MatStoreValues_MPIAIJ);CHKERRQ(ierr); 566700de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C","MatRetrieveValues_MPIAIJ",MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 566800de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C","MatGetDiagonalBlock_MPIAIJ",MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 566900de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C","MatIsTranspose_MPIAIJ",MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 567000de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C","MatMPIAIJSetPreallocation_MPIAIJ",MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 567100de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C","MatMPIAIJSetPreallocationCSR_MPIAIJ",MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 567200de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C","MatDiagonalScaleLocal_MPIAIJ",MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 567300de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C","MatConvert_MPIAIJ_MPIAIJPERM",MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 567400de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C","MatConvert_MPIAIJ_MPIAIJCRL",MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 567500de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C","MatConvert_MPIAIJ_MPISBAIJ",MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 567600de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C","MatMatMult_MPIDense_MPIAIJ",MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 567700de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C","MatMatMultSymbolic_MPIDense_MPIAIJ",MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 567800de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C","MatMatMultNumeric_MPIDense_MPIAIJ",MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 567917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5680ccd8e176SBarry Smith PetscFunctionReturn(0); 5681ccd8e176SBarry Smith } 5682ccd8e176SBarry Smith EXTERN_C_END 568381824310SBarry Smith 568403bfb495SBarry Smith #undef __FUNCT__ 568503bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 568658d36128SBarry Smith /*@ 568703bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 568803bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 568903bfb495SBarry Smith 569003bfb495SBarry Smith Collective on MPI_Comm 569103bfb495SBarry Smith 569203bfb495SBarry Smith Input Parameters: 569303bfb495SBarry Smith + comm - MPI communicator 569403bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 569503bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 569603bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 569703bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 569803bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 569903bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 570003bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 570103bfb495SBarry Smith . j - column indices 570203bfb495SBarry Smith . a - matrix values 570303bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 570403bfb495SBarry Smith . oj - column indices 570503bfb495SBarry Smith - oa - matrix values 570603bfb495SBarry Smith 570703bfb495SBarry Smith Output Parameter: 570803bfb495SBarry Smith . mat - the matrix 570903bfb495SBarry Smith 571003bfb495SBarry Smith Level: advanced 571103bfb495SBarry Smith 571203bfb495SBarry Smith Notes: 5713292fb18eSBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user 5714292fb18eSBarry Smith must free the arrays once the matrix has been destroyed and not before. 571503bfb495SBarry Smith 571603bfb495SBarry Smith The i and j indices are 0 based 571703bfb495SBarry Smith 571869b1f4b7SBarry Smith See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 571903bfb495SBarry Smith 57207b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 57217b55108eSBarry Smith 5722dca341c0SJed Brown Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a 5723dca341c0SJed Brown legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does 5724dca341c0SJed Brown not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because 5725dca341c0SJed Brown the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to 5726dca341c0SJed Brown keep track of the underlying array. Use MatSetOption(A,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all 5727dca341c0SJed Brown communication if it is known that only local entries will be set. 572803bfb495SBarry Smith 572903bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 573003bfb495SBarry Smith 573103bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 573269b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays() 573303bfb495SBarry Smith @*/ 57342205254eSKarl Rupp PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 573503bfb495SBarry Smith { 573603bfb495SBarry Smith PetscErrorCode ierr; 573703bfb495SBarry Smith Mat_MPIAIJ *maij; 573803bfb495SBarry Smith 573903bfb495SBarry Smith PetscFunctionBegin; 5740e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 5741ea345e14SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 5742ea345e14SBarry Smith if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 574303bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 574403bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 574503bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 574603bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 57472205254eSKarl Rupp 57488d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 574903bfb495SBarry Smith 575026283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 575126283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 575203bfb495SBarry Smith 575303bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5754d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 575503bfb495SBarry Smith 57568d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57578d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57588d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57598d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57608d7a6e47SBarry Smith 576103bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 576203bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5763dca341c0SJed Brown ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 576403bfb495SBarry Smith PetscFunctionReturn(0); 576503bfb495SBarry Smith } 576603bfb495SBarry Smith 576781824310SBarry Smith /* 576881824310SBarry Smith Special version for direct calls from Fortran 576981824310SBarry Smith */ 5770b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h> 57717087cfbeSBarry Smith 577281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 577381824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 577481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 577581824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 577681824310SBarry Smith #endif 577781824310SBarry Smith 577881824310SBarry Smith /* Change these macros so can be used in void function */ 577981824310SBarry Smith #undef CHKERRQ 5780e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 578181824310SBarry Smith #undef SETERRQ2 5782e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 57834994cf47SJed Brown #undef SETERRQ3 57844994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 578581824310SBarry Smith #undef SETERRQ 5786e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 578781824310SBarry Smith 578881824310SBarry Smith EXTERN_C_BEGIN 578981824310SBarry Smith #undef __FUNCT__ 579081824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 57911f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 579281824310SBarry Smith { 579381824310SBarry Smith Mat mat = *mmat; 579481824310SBarry Smith PetscInt m = *mm, n = *mn; 579581824310SBarry Smith InsertMode addv = *maddv; 579681824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 579781824310SBarry Smith PetscScalar value; 579881824310SBarry Smith PetscErrorCode ierr; 5799899cda47SBarry Smith 58004994cf47SJed Brown MatCheckPreallocated(mat,1); 58012205254eSKarl Rupp if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 58022205254eSKarl Rupp 580381824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5804f23aa3ddSBarry Smith else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 580581824310SBarry Smith #endif 580681824310SBarry Smith { 5807d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5808d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5809ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 581081824310SBarry Smith 581181824310SBarry Smith /* Some Variables required in the macro */ 581281824310SBarry Smith Mat A = aij->A; 581381824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 581481824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5815dd6ea824SBarry Smith MatScalar *aa = a->a; 5816ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE); 581781824310SBarry Smith Mat B = aij->B; 581881824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5819d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5820dd6ea824SBarry Smith MatScalar *ba = b->a; 582181824310SBarry Smith 582281824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 582381824310SBarry Smith PetscInt nonew = a->nonew; 5824dd6ea824SBarry Smith MatScalar *ap1,*ap2; 582581824310SBarry Smith 582681824310SBarry Smith PetscFunctionBegin; 582781824310SBarry Smith for (i=0; i<m; i++) { 582881824310SBarry Smith if (im[i] < 0) continue; 582981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5830e32f2f54SBarry 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); 583181824310SBarry Smith #endif 583281824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 583381824310SBarry Smith row = im[i] - rstart; 583481824310SBarry Smith lastcol1 = -1; 583581824310SBarry Smith rp1 = aj + ai[row]; 583681824310SBarry Smith ap1 = aa + ai[row]; 583781824310SBarry Smith rmax1 = aimax[row]; 583881824310SBarry Smith nrow1 = ailen[row]; 583981824310SBarry Smith low1 = 0; 584081824310SBarry Smith high1 = nrow1; 584181824310SBarry Smith lastcol2 = -1; 584281824310SBarry Smith rp2 = bj + bi[row]; 584381824310SBarry Smith ap2 = ba + bi[row]; 584481824310SBarry Smith rmax2 = bimax[row]; 584581824310SBarry Smith nrow2 = bilen[row]; 584681824310SBarry Smith low2 = 0; 584781824310SBarry Smith high2 = nrow2; 584881824310SBarry Smith 584981824310SBarry Smith for (j=0; j<n; j++) { 58502205254eSKarl Rupp if (roworiented) value = v[i*n+j]; 58512205254eSKarl Rupp else value = v[i+j*m]; 585281824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 585381824310SBarry Smith if (in[j] >= cstart && in[j] < cend) { 585481824310SBarry Smith col = in[j] - cstart; 585581824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 585681824310SBarry Smith } else if (in[j] < 0) continue; 585781824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5858cb9801acSJed 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); 585981824310SBarry Smith #endif 586081824310SBarry Smith else { 586181824310SBarry Smith if (mat->was_assembled) { 586281824310SBarry Smith if (!aij->colmap) { 5863ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 586481824310SBarry Smith } 586581824310SBarry Smith #if defined(PETSC_USE_CTABLE) 586681824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 586781824310SBarry Smith col--; 586881824310SBarry Smith #else 586981824310SBarry Smith col = aij->colmap[in[j]] - 1; 587081824310SBarry Smith #endif 587181824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 5872ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 587381824310SBarry Smith col = in[j]; 587481824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 587581824310SBarry Smith B = aij->B; 587681824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 587781824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 587881824310SBarry Smith rp2 = bj + bi[row]; 587981824310SBarry Smith ap2 = ba + bi[row]; 588081824310SBarry Smith rmax2 = bimax[row]; 588181824310SBarry Smith nrow2 = bilen[row]; 588281824310SBarry Smith low2 = 0; 588381824310SBarry Smith high2 = nrow2; 5884d0f46423SBarry Smith bm = aij->B->rmap->n; 588581824310SBarry Smith ba = b->a; 588681824310SBarry Smith } 588781824310SBarry Smith } else col = in[j]; 588881824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 588981824310SBarry Smith } 589081824310SBarry Smith } 58912205254eSKarl Rupp } else if (!aij->donotstash) { 589281824310SBarry Smith if (roworiented) { 5893ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 589481824310SBarry Smith } else { 5895ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 589681824310SBarry Smith } 589781824310SBarry Smith } 589881824310SBarry Smith } 58992205254eSKarl Rupp } 590081824310SBarry Smith PetscFunctionReturnVoid(); 590181824310SBarry Smith } 590281824310SBarry Smith EXTERN_C_END 590303bfb495SBarry Smith 5904