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> 50c312b8eSJed Brown #include <petscsf.h> 68a729477SBarry Smith 701bebe75SBarry Smith /*MC 801bebe75SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 901bebe75SBarry Smith 1001bebe75SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 1101bebe75SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 1201bebe75SBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 1301bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 1401bebe75SBarry Smith the above preallocation routines for simplicity. 1501bebe75SBarry Smith 1601bebe75SBarry Smith Options Database Keys: 1701bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 1801bebe75SBarry Smith 199ae82921SPaul Mullowney Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when 2001bebe75SBarry Smith enough exist. 2101bebe75SBarry Smith 2201bebe75SBarry Smith Level: beginner 2301bebe75SBarry Smith 2469b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 2501bebe75SBarry Smith M*/ 2601bebe75SBarry Smith 2701bebe75SBarry Smith /*MC 2801bebe75SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 2901bebe75SBarry Smith 3001bebe75SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 3101bebe75SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 3201bebe75SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 3301bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3401bebe75SBarry Smith the above preallocation routines for simplicity. 3501bebe75SBarry Smith 3601bebe75SBarry Smith Options Database Keys: 3701bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 3801bebe75SBarry Smith 3901bebe75SBarry Smith Level: beginner 4001bebe75SBarry Smith 4101bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4201bebe75SBarry Smith M*/ 4301bebe75SBarry Smith 44dd6ea824SBarry Smith #undef __FUNCT__ 45f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ" 46f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows) 4727d4218bSShri Abhyankar { 4827d4218bSShri Abhyankar PetscErrorCode ierr; 4927d4218bSShri Abhyankar Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data; 5027d4218bSShri Abhyankar Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data; 5127d4218bSShri Abhyankar Mat_SeqAIJ *b = (Mat_SeqAIJ*)mat->B->data; 5227d4218bSShri Abhyankar const PetscInt *ia,*ib; 5327d4218bSShri Abhyankar const MatScalar *aa,*bb; 5427d4218bSShri Abhyankar PetscInt na,nb,i,j,*rows,cnt=0,n0rows; 5527d4218bSShri Abhyankar PetscInt m = M->rmap->n,rstart = M->rmap->rstart; 5627d4218bSShri Abhyankar 5727d4218bSShri Abhyankar PetscFunctionBegin; 5827d4218bSShri Abhyankar *keptrows = 0; 5927d4218bSShri Abhyankar ia = a->i; 6027d4218bSShri Abhyankar ib = b->i; 6127d4218bSShri Abhyankar for (i=0; i<m; i++) { 6227d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 6327d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 6427d4218bSShri Abhyankar if (!na && !nb) { 6527d4218bSShri Abhyankar cnt++; 6627d4218bSShri Abhyankar goto ok1; 6727d4218bSShri Abhyankar } 6827d4218bSShri Abhyankar aa = a->a + ia[i]; 6927d4218bSShri Abhyankar for (j=0; j<na; j++) { 7027d4218bSShri Abhyankar if (aa[j] != 0.0) goto ok1; 7127d4218bSShri Abhyankar } 7227d4218bSShri Abhyankar bb = b->a + ib[i]; 7327d4218bSShri Abhyankar for (j=0; j <nb; j++) { 7427d4218bSShri Abhyankar if (bb[j] != 0.0) goto ok1; 7527d4218bSShri Abhyankar } 7627d4218bSShri Abhyankar cnt++; 7727d4218bSShri Abhyankar ok1:; 7827d4218bSShri Abhyankar } 79e56f5c9eSBarry Smith ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPIU_SUM,PetscObjectComm((PetscObject)M));CHKERRQ(ierr); 8027d4218bSShri Abhyankar if (!n0rows) PetscFunctionReturn(0); 81785e854fSJed Brown ierr = PetscMalloc1((M->rmap->n-cnt),&rows);CHKERRQ(ierr); 8227d4218bSShri Abhyankar cnt = 0; 8327d4218bSShri Abhyankar for (i=0; i<m; i++) { 8427d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 8527d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 8627d4218bSShri Abhyankar if (!na && !nb) continue; 8727d4218bSShri Abhyankar aa = a->a + ia[i]; 8827d4218bSShri Abhyankar for (j=0; j<na;j++) { 8927d4218bSShri Abhyankar if (aa[j] != 0.0) { 9027d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9127d4218bSShri Abhyankar goto ok2; 9227d4218bSShri Abhyankar } 9327d4218bSShri Abhyankar } 9427d4218bSShri Abhyankar bb = b->a + ib[i]; 9527d4218bSShri Abhyankar for (j=0; j<nb; j++) { 9627d4218bSShri Abhyankar if (bb[j] != 0.0) { 9727d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9827d4218bSShri Abhyankar goto ok2; 9927d4218bSShri Abhyankar } 10027d4218bSShri Abhyankar } 10127d4218bSShri Abhyankar ok2:; 10227d4218bSShri Abhyankar } 103ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 10427d4218bSShri Abhyankar PetscFunctionReturn(0); 10527d4218bSShri Abhyankar } 10627d4218bSShri Abhyankar 10727d4218bSShri Abhyankar #undef __FUNCT__ 108f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ" 109f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows) 110f1f41ecbSJed Brown { 111f1f41ecbSJed Brown Mat_MPIAIJ *aij = (Mat_MPIAIJ*)M->data; 112f1f41ecbSJed Brown PetscErrorCode ierr; 113f1f41ecbSJed Brown PetscInt i,rstart,nrows,*rows; 114f1f41ecbSJed Brown 115f1f41ecbSJed Brown PetscFunctionBegin; 1160298fd71SBarry Smith *zrows = NULL; 117f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr); 1180298fd71SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,NULL);CHKERRQ(ierr); 119f1f41ecbSJed Brown for (i=0; i<nrows; i++) rows[i] += rstart; 120ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 121f1f41ecbSJed Brown PetscFunctionReturn(0); 122f1f41ecbSJed Brown } 123f1f41ecbSJed Brown 124f1f41ecbSJed Brown #undef __FUNCT__ 1250716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ" 1260716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms) 1270716a85fSBarry Smith { 1280716a85fSBarry Smith PetscErrorCode ierr; 1290716a85fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data; 1300716a85fSBarry Smith PetscInt i,n,*garray = aij->garray; 1310716a85fSBarry Smith Mat_SeqAIJ *a_aij = (Mat_SeqAIJ*) aij->A->data; 1320716a85fSBarry Smith Mat_SeqAIJ *b_aij = (Mat_SeqAIJ*) aij->B->data; 1330716a85fSBarry Smith PetscReal *work; 1340716a85fSBarry Smith 1350716a85fSBarry Smith PetscFunctionBegin; 1360298fd71SBarry Smith ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr); 1371795a4d1SJed Brown ierr = PetscCalloc1(n,&work);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) { 1627ec4195eSRémi Lacroix ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 1630716a85fSBarry Smith } else { 1647ec4195eSRémi Lacroix ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));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__ 174*e52d2c62SBarry Smith #define __FUNCT__ "MatFindOffBlockDiagonalEntries_MPIAIJ" 175*e52d2c62SBarry Smith PetscErrorCode MatFindOffBlockDiagonalEntries_MPIAIJ(Mat A,IS *is) 176*e52d2c62SBarry Smith { 177*e52d2c62SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 178*e52d2c62SBarry Smith IS sis,gis; 179*e52d2c62SBarry Smith PetscErrorCode ierr; 180*e52d2c62SBarry Smith const PetscInt *isis,*igis; 181*e52d2c62SBarry Smith PetscInt n,*iis,nsis,ngis,rstart,i; 182*e52d2c62SBarry Smith 183*e52d2c62SBarry Smith PetscFunctionBegin; 184*e52d2c62SBarry Smith ierr = MatFindOffBlockDiagonalEntries(a->A,&sis);CHKERRQ(ierr); 185*e52d2c62SBarry Smith ierr = MatFindNonzeroRows(a->B,&gis);CHKERRQ(ierr); 186*e52d2c62SBarry Smith ierr = ISGetSize(gis,&ngis);CHKERRQ(ierr); 187*e52d2c62SBarry Smith ierr = ISGetSize(sis,&nsis);CHKERRQ(ierr); 188*e52d2c62SBarry Smith ierr = ISGetIndices(sis,&isis);CHKERRQ(ierr); 189*e52d2c62SBarry Smith ierr = ISGetIndices(gis,&igis);CHKERRQ(ierr); 190*e52d2c62SBarry Smith 191*e52d2c62SBarry Smith ierr = PetscMalloc1(ngis+nsis,&iis);CHKERRQ(ierr); 192*e52d2c62SBarry Smith ierr = PetscMemcpy(iis,igis,ngis*sizeof(PetscInt));CHKERRQ(ierr); 193*e52d2c62SBarry Smith ierr = PetscMemcpy(iis+ngis,isis,nsis*sizeof(PetscInt));CHKERRQ(ierr); 194*e52d2c62SBarry Smith n = ngis + nsis; 195*e52d2c62SBarry Smith ierr = PetscSortRemoveDupsInt(&n,iis);CHKERRQ(ierr); 196*e52d2c62SBarry Smith ierr = MatGetOwnershipRange(A,&rstart,NULL);CHKERRQ(ierr); 197*e52d2c62SBarry Smith for (i=0; i<n; i++) iis[i] += rstart; 198*e52d2c62SBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)A),n,iis,PETSC_OWN_POINTER,is);CHKERRQ(ierr); 199*e52d2c62SBarry Smith 200*e52d2c62SBarry Smith ierr = ISRestoreIndices(sis,&isis);CHKERRQ(ierr); 201*e52d2c62SBarry Smith ierr = ISRestoreIndices(gis,&igis);CHKERRQ(ierr); 202*e52d2c62SBarry Smith ierr = ISDestroy(&sis);CHKERRQ(ierr); 203*e52d2c62SBarry Smith ierr = ISDestroy(&gis);CHKERRQ(ierr); 204*e52d2c62SBarry Smith PetscFunctionReturn(0); 205*e52d2c62SBarry Smith } 206*e52d2c62SBarry Smith 207*e52d2c62SBarry Smith #undef __FUNCT__ 208dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ" 209dd6ea824SBarry Smith /* 210dd6ea824SBarry Smith Distributes a SeqAIJ matrix across a set of processes. Code stolen from 211dd6ea824SBarry Smith MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type. 212dd6ea824SBarry Smith 213dd6ea824SBarry Smith Only for square matrices 214b30237c6SBarry Smith 215b30237c6SBarry Smith Used by a preconditioner, hence PETSC_EXTERN 216dd6ea824SBarry Smith */ 2175a576424SJed Brown PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat) 218dd6ea824SBarry Smith { 219dd6ea824SBarry Smith PetscMPIInt rank,size; 220d892089bSMatthew G. Knepley PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2]; 221dd6ea824SBarry Smith PetscErrorCode ierr; 222dd6ea824SBarry Smith Mat mat; 223dd6ea824SBarry Smith Mat_SeqAIJ *gmata; 224dd6ea824SBarry Smith PetscMPIInt tag; 225dd6ea824SBarry Smith MPI_Status status; 226ace3abfcSBarry Smith PetscBool aij; 227dd6ea824SBarry Smith MatScalar *gmataa,*ao,*ad,*gmataarestore=0; 228dd6ea824SBarry Smith 229dd6ea824SBarry Smith PetscFunctionBegin; 230dd6ea824SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 231dd6ea824SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 232dd6ea824SBarry Smith if (!rank) { 233251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr); 234ce94432eSBarry Smith if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name); 235dd6ea824SBarry Smith } 236dd6ea824SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 237dd6ea824SBarry Smith ierr = MatCreate(comm,&mat);CHKERRQ(ierr); 238dd6ea824SBarry Smith ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 23933d57670SJed Brown ierr = MatGetBlockSizes(gmat,&bses[0],&bses[1]);CHKERRQ(ierr); 240efcf75d5SBarry Smith ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr); 241efcf75d5SBarry Smith ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr); 242dd6ea824SBarry Smith ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 243785e854fSJed Brown ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr); 244dcca6d9dSJed Brown ierr = PetscMalloc2(m,&dlens,m,&olens);CHKERRQ(ierr); 245dd6ea824SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 2462205254eSKarl Rupp 247dd6ea824SBarry Smith rowners[0] = 0; 2482205254eSKarl Rupp for (i=2; i<=size; i++) rowners[i] += rowners[i-1]; 249dd6ea824SBarry Smith rstart = rowners[rank]; 250dd6ea824SBarry Smith rend = rowners[rank+1]; 251dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 252dd6ea824SBarry Smith if (!rank) { 253dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 254dd6ea824SBarry Smith /* send row lengths to all processors */ 255dd6ea824SBarry Smith for (i=0; i<m; i++) dlens[i] = gmata->ilen[i]; 256dd6ea824SBarry Smith for (i=1; i<size; i++) { 257dd6ea824SBarry Smith ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 258dd6ea824SBarry Smith } 259dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 260dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 2611795a4d1SJed Brown ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr); 262dd6ea824SBarry Smith jj = 0; 263dd6ea824SBarry Smith for (i=0; i<m; i++) { 264dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 265dd6ea824SBarry Smith if (gmata->j[jj] < rstart) ld[i]++; 266dd6ea824SBarry Smith if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++; 267dd6ea824SBarry Smith jj++; 268dd6ea824SBarry Smith } 269dd6ea824SBarry Smith } 270dd6ea824SBarry Smith /* send column indices to other processes */ 271dd6ea824SBarry Smith for (i=1; i<size; i++) { 272dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 273dd6ea824SBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 274dd6ea824SBarry Smith ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 275dd6ea824SBarry Smith } 276dd6ea824SBarry Smith 277dd6ea824SBarry Smith /* send numerical values to other processes */ 278dd6ea824SBarry Smith for (i=1; i<size; i++) { 279dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 280dd6ea824SBarry Smith ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 281dd6ea824SBarry Smith } 282dd6ea824SBarry Smith gmataa = gmata->a; 283dd6ea824SBarry Smith gmataj = gmata->j; 284dd6ea824SBarry Smith 285dd6ea824SBarry Smith } else { 286dd6ea824SBarry Smith /* receive row lengths */ 287dd6ea824SBarry Smith ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 288dd6ea824SBarry Smith /* receive column indices */ 289dd6ea824SBarry Smith ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 290dcca6d9dSJed Brown ierr = PetscMalloc2(nz,&gmataa,nz,&gmataj);CHKERRQ(ierr); 291dd6ea824SBarry Smith ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 292dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 293dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 2941795a4d1SJed Brown ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr); 295dd6ea824SBarry Smith jj = 0; 296dd6ea824SBarry Smith for (i=0; i<m; i++) { 297dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 298dd6ea824SBarry Smith if (gmataj[jj] < rstart) ld[i]++; 299dd6ea824SBarry Smith if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++; 300dd6ea824SBarry Smith jj++; 301dd6ea824SBarry Smith } 302dd6ea824SBarry Smith } 303dd6ea824SBarry Smith /* receive numerical values */ 304dd6ea824SBarry Smith ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 305dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 306dd6ea824SBarry Smith } 307dd6ea824SBarry Smith /* set preallocation */ 308dd6ea824SBarry Smith for (i=0; i<m; i++) { 309dd6ea824SBarry Smith dlens[i] -= olens[i]; 310dd6ea824SBarry Smith } 311dd6ea824SBarry Smith ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr); 312dd6ea824SBarry Smith ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr); 313dd6ea824SBarry Smith 314dd6ea824SBarry Smith for (i=0; i<m; i++) { 315dd6ea824SBarry Smith dlens[i] += olens[i]; 316dd6ea824SBarry Smith } 317dd6ea824SBarry Smith cnt = 0; 318dd6ea824SBarry Smith for (i=0; i<m; i++) { 319dd6ea824SBarry Smith row = rstart + i; 320dd6ea824SBarry Smith ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr); 321dd6ea824SBarry Smith cnt += dlens[i]; 322dd6ea824SBarry Smith } 323dd6ea824SBarry Smith if (rank) { 324dd6ea824SBarry Smith ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr); 325dd6ea824SBarry Smith } 326dd6ea824SBarry Smith ierr = PetscFree2(dlens,olens);CHKERRQ(ierr); 327dd6ea824SBarry Smith ierr = PetscFree(rowners);CHKERRQ(ierr); 3282205254eSKarl Rupp 329dd6ea824SBarry Smith ((Mat_MPIAIJ*)(mat->data))->ld = ld; 3302205254eSKarl Rupp 331dd6ea824SBarry Smith *inmat = mat; 332dd6ea824SBarry Smith } else { /* column indices are already set; only need to move over numerical values from process 0 */ 333dd6ea824SBarry Smith Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data; 334dd6ea824SBarry Smith Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data; 335dd6ea824SBarry Smith mat = *inmat; 336dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 337dd6ea824SBarry Smith if (!rank) { 338dd6ea824SBarry Smith /* send numerical values to other processes */ 339dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 340dd6ea824SBarry Smith ierr = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr); 341dd6ea824SBarry Smith gmataa = gmata->a; 342dd6ea824SBarry Smith for (i=1; i<size; i++) { 343dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 344dd6ea824SBarry Smith ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 345dd6ea824SBarry Smith } 346dd6ea824SBarry Smith nz = gmata->i[rowners[1]]-gmata->i[rowners[0]]; 347dd6ea824SBarry Smith } else { 348dd6ea824SBarry Smith /* receive numerical values from process 0*/ 349dd6ea824SBarry Smith nz = Ad->nz + Ao->nz; 350785e854fSJed Brown ierr = PetscMalloc1(nz,&gmataa);CHKERRQ(ierr); gmataarestore = gmataa; 351dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 352dd6ea824SBarry Smith } 353dd6ea824SBarry Smith /* transfer numerical values into the diagonal A and off diagonal B parts of mat */ 354dd6ea824SBarry Smith ld = ((Mat_MPIAIJ*)(mat->data))->ld; 355dd6ea824SBarry Smith ad = Ad->a; 356dd6ea824SBarry Smith ao = Ao->a; 357d0f46423SBarry Smith if (mat->rmap->n) { 358dd6ea824SBarry Smith i = 0; 359dd6ea824SBarry Smith nz = ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 360dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 361dd6ea824SBarry Smith } 362d0f46423SBarry Smith for (i=1; i<mat->rmap->n; i++) { 363dd6ea824SBarry 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; 364dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 365dd6ea824SBarry Smith } 366dd6ea824SBarry Smith i--; 367d0f46423SBarry Smith if (mat->rmap->n) { 36822d28d08SBarry Smith nz = Ao->i[i+1] - Ao->i[i] - ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 369dd6ea824SBarry Smith } 370dd6ea824SBarry Smith if (rank) { 371dd6ea824SBarry Smith ierr = PetscFree(gmataarestore);CHKERRQ(ierr); 372dd6ea824SBarry Smith } 373dd6ea824SBarry Smith } 374dd6ea824SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 375dd6ea824SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 376dd6ea824SBarry Smith PetscFunctionReturn(0); 377dd6ea824SBarry Smith } 378dd6ea824SBarry Smith 3790f5bd95cSBarry Smith /* 3800f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 3819e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 3820f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 3830f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 3840f5bd95cSBarry Smith has an order N integer array but is fast to acess. 3859e25ed09SBarry Smith */ 3864a2ae208SSatish Balay #undef __FUNCT__ 387ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private" 388ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat) 3899e25ed09SBarry Smith { 39044a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3916849ba73SBarry Smith PetscErrorCode ierr; 392d0f46423SBarry Smith PetscInt n = aij->B->cmap->n,i; 393dbb450caSBarry Smith 3943a40ed3dSBarry Smith PetscFunctionBegin; 3955e1f6667SBarry Smith if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray"); 396aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 397e23dfa41SBarry Smith ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr); 398b1fc9764SSatish Balay for (i=0; i<n; i++) { 3993861aac3SJed Brown ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr); 400b1fc9764SSatish Balay } 401b1fc9764SSatish Balay #else 4021795a4d1SJed Brown ierr = PetscCalloc1((mat->cmap->N+1),&aij->colmap);CHKERRQ(ierr); 4031795a4d1SJed Brown ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));CHKERRQ(ierr); 404905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 405b1fc9764SSatish Balay #endif 4063a40ed3dSBarry Smith PetscFunctionReturn(0); 4079e25ed09SBarry Smith } 4089e25ed09SBarry Smith 40930770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 4100520107fSSatish Balay { \ 411db4deed7SKarl Rupp if (col <= lastcol1) low1 = 0; \ 412db4deed7SKarl Rupp else high1 = nrow1; \ 413fd3458f5SBarry Smith lastcol1 = col;\ 414fd3458f5SBarry Smith while (high1-low1 > 5) { \ 415fd3458f5SBarry Smith t = (low1+high1)/2; \ 416fd3458f5SBarry Smith if (rp1[t] > col) high1 = t; \ 417fd3458f5SBarry Smith else low1 = t; \ 418ba4e3ef2SSatish Balay } \ 419fd3458f5SBarry Smith for (_i=low1; _i<high1; _i++) { \ 420fd3458f5SBarry Smith if (rp1[_i] > col) break; \ 421fd3458f5SBarry Smith if (rp1[_i] == col) { \ 422fd3458f5SBarry Smith if (addv == ADD_VALUES) ap1[_i] += value; \ 423fd3458f5SBarry Smith else ap1[_i] = value; \ 42430770e4dSSatish Balay goto a_noinsert; \ 4250520107fSSatish Balay } \ 4260520107fSSatish Balay } \ 427e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \ 428e44c0bd4SBarry Smith if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;} \ 429e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 430fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \ 431669a8dbcSSatish Balay N = nrow1++ - 1; a->nz++; high1++; \ 4320520107fSSatish Balay /* shift up all the later entries in this row */ \ 4330520107fSSatish Balay for (ii=N; ii>=_i; ii--) { \ 434fd3458f5SBarry Smith rp1[ii+1] = rp1[ii]; \ 435fd3458f5SBarry Smith ap1[ii+1] = ap1[ii]; \ 4360520107fSSatish Balay } \ 437fd3458f5SBarry Smith rp1[_i] = col; \ 438fd3458f5SBarry Smith ap1[_i] = value; \ 439e56f5c9eSBarry Smith A->nonzerostate++;\ 44030770e4dSSatish Balay a_noinsert: ; \ 441fd3458f5SBarry Smith ailen[row] = nrow1; \ 4420520107fSSatish Balay } 4430a198c4cSBarry Smith 444085a36d4SBarry Smith 44530770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 44630770e4dSSatish Balay { \ 447db4deed7SKarl Rupp if (col <= lastcol2) low2 = 0; \ 448db4deed7SKarl Rupp else high2 = nrow2; \ 449fd3458f5SBarry Smith lastcol2 = col; \ 450fd3458f5SBarry Smith while (high2-low2 > 5) { \ 451fd3458f5SBarry Smith t = (low2+high2)/2; \ 452fd3458f5SBarry Smith if (rp2[t] > col) high2 = t; \ 453fd3458f5SBarry Smith else low2 = t; \ 454ba4e3ef2SSatish Balay } \ 455fd3458f5SBarry Smith for (_i=low2; _i<high2; _i++) { \ 456fd3458f5SBarry Smith if (rp2[_i] > col) break; \ 457fd3458f5SBarry Smith if (rp2[_i] == col) { \ 458fd3458f5SBarry Smith if (addv == ADD_VALUES) ap2[_i] += value; \ 459fd3458f5SBarry Smith else ap2[_i] = value; \ 46030770e4dSSatish Balay goto b_noinsert; \ 46130770e4dSSatish Balay } \ 46230770e4dSSatish Balay } \ 463e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 464e44c0bd4SBarry Smith if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 465e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 466fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \ 467669a8dbcSSatish Balay N = nrow2++ - 1; b->nz++; high2++; \ 46830770e4dSSatish Balay /* shift up all the later entries in this row */ \ 46930770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 470fd3458f5SBarry Smith rp2[ii+1] = rp2[ii]; \ 471fd3458f5SBarry Smith ap2[ii+1] = ap2[ii]; \ 47230770e4dSSatish Balay } \ 473fd3458f5SBarry Smith rp2[_i] = col; \ 474fd3458f5SBarry Smith ap2[_i] = value; \ 475e56f5c9eSBarry Smith B->nonzerostate++; \ 47630770e4dSSatish Balay b_noinsert: ; \ 477fd3458f5SBarry Smith bilen[row] = nrow2; \ 47830770e4dSSatish Balay } 47930770e4dSSatish Balay 4804a2ae208SSatish Balay #undef __FUNCT__ 4812fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ" 4822fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[]) 4832fd7e33dSBarry Smith { 4842fd7e33dSBarry Smith Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 4852fd7e33dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data; 4862fd7e33dSBarry Smith PetscErrorCode ierr; 4872fd7e33dSBarry Smith PetscInt l,*garray = mat->garray,diag; 4882fd7e33dSBarry Smith 4892fd7e33dSBarry Smith PetscFunctionBegin; 4902fd7e33dSBarry Smith /* code only works for square matrices A */ 4912fd7e33dSBarry Smith 4922fd7e33dSBarry Smith /* find size of row to the left of the diagonal part */ 4932fd7e33dSBarry Smith ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr); 4942fd7e33dSBarry Smith row = row - diag; 4952fd7e33dSBarry Smith for (l=0; l<b->i[row+1]-b->i[row]; l++) { 4962fd7e33dSBarry Smith if (garray[b->j[b->i[row]+l]] > diag) break; 4972fd7e33dSBarry Smith } 4982fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr); 4992fd7e33dSBarry Smith 5002fd7e33dSBarry Smith /* diagonal part */ 5012fd7e33dSBarry Smith ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr); 5022fd7e33dSBarry Smith 5032fd7e33dSBarry Smith /* right of diagonal part */ 5042fd7e33dSBarry 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); 5052fd7e33dSBarry Smith PetscFunctionReturn(0); 5062fd7e33dSBarry Smith } 5072fd7e33dSBarry Smith 5082fd7e33dSBarry Smith #undef __FUNCT__ 5094a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 510b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 5118a729477SBarry Smith { 51244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 51387828ca2SBarry Smith PetscScalar value; 514dfbe8321SBarry Smith PetscErrorCode ierr; 515d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 516d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 517ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 5188a729477SBarry Smith 5190520107fSSatish Balay /* Some Variables required in the macro */ 5204ee7247eSSatish Balay Mat A = aij->A; 5214ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 52257809a77SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 523a77337e4SBarry Smith MatScalar *aa = a->a; 524ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 52530770e4dSSatish Balay Mat B = aij->B; 52630770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 527d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 528a77337e4SBarry Smith MatScalar *ba = b->a; 52930770e4dSSatish Balay 530fd3458f5SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 5318d76821aSHong Zhang PetscInt nonew; 532a77337e4SBarry Smith MatScalar *ap1,*ap2; 5334ee7247eSSatish Balay 5343a40ed3dSBarry Smith PetscFunctionBegin; 5358a729477SBarry Smith for (i=0; i<m; i++) { 5365ef9f2a5SBarry Smith if (im[i] < 0) continue; 5372515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 538e32f2f54SBarry 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); 5390a198c4cSBarry Smith #endif 5404b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 5414b0e389bSBarry Smith row = im[i] - rstart; 542fd3458f5SBarry Smith lastcol1 = -1; 543fd3458f5SBarry Smith rp1 = aj + ai[row]; 544fd3458f5SBarry Smith ap1 = aa + ai[row]; 545fd3458f5SBarry Smith rmax1 = aimax[row]; 546fd3458f5SBarry Smith nrow1 = ailen[row]; 547fd3458f5SBarry Smith low1 = 0; 548fd3458f5SBarry Smith high1 = nrow1; 549fd3458f5SBarry Smith lastcol2 = -1; 550fd3458f5SBarry Smith rp2 = bj + bi[row]; 551d498b1e9SBarry Smith ap2 = ba + bi[row]; 552fd3458f5SBarry Smith rmax2 = bimax[row]; 553d498b1e9SBarry Smith nrow2 = bilen[row]; 554fd3458f5SBarry Smith low2 = 0; 555fd3458f5SBarry Smith high2 = nrow2; 556fd3458f5SBarry Smith 5571eb62cbbSBarry Smith for (j=0; j<n; j++) { 558db4deed7SKarl Rupp if (roworiented) value = v[i*n+j]; 559db4deed7SKarl Rupp else value = v[i+j*m]; 560abc0a331SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 561fd3458f5SBarry Smith if (in[j] >= cstart && in[j] < cend) { 562fd3458f5SBarry Smith col = in[j] - cstart; 5638d76821aSHong Zhang nonew = a->nonew; 56430770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 565273d9f13SBarry Smith } else if (in[j] < 0) continue; 5662515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 567cb9801acSJed 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); 5680a198c4cSBarry Smith #endif 5691eb62cbbSBarry Smith else { 570227d817aSBarry Smith if (mat->was_assembled) { 571905e6a2fSBarry Smith if (!aij->colmap) { 572ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 573905e6a2fSBarry Smith } 574aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 5750f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 576fa46199cSSatish Balay col--; 577b1fc9764SSatish Balay #else 578905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 579b1fc9764SSatish Balay #endif 5800e9bae81SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) { 581ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 5824b0e389bSBarry Smith col = in[j]; 5839bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 584f9508a3cSSatish Balay B = aij->B; 585f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 586e44c0bd4SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a; 587d498b1e9SBarry Smith rp2 = bj + bi[row]; 588d498b1e9SBarry Smith ap2 = ba + bi[row]; 589d498b1e9SBarry Smith rmax2 = bimax[row]; 590d498b1e9SBarry Smith nrow2 = bilen[row]; 591d498b1e9SBarry Smith low2 = 0; 592d498b1e9SBarry Smith high2 = nrow2; 593d0f46423SBarry Smith bm = aij->B->rmap->n; 594f9508a3cSSatish Balay ba = b->a; 5950e9bae81SBarry 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]); 596c48de900SBarry Smith } else col = in[j]; 5978d76821aSHong Zhang nonew = b->nonew; 59830770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 5991eb62cbbSBarry Smith } 6001eb62cbbSBarry Smith } 6015ef9f2a5SBarry Smith } else { 6024cb17eb5SBarry 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]); 60390f02eecSBarry Smith if (!aij->donotstash) { 6045080c13bSMatthew G Knepley mat->assembled = PETSC_FALSE; 605d36fbae8SSatish Balay if (roworiented) { 606ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 607d36fbae8SSatish Balay } else { 608ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 6094b0e389bSBarry Smith } 6101eb62cbbSBarry Smith } 6118a729477SBarry Smith } 61290f02eecSBarry Smith } 6133a40ed3dSBarry Smith PetscFunctionReturn(0); 6148a729477SBarry Smith } 6158a729477SBarry Smith 6164a2ae208SSatish Balay #undef __FUNCT__ 6174a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 618b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 619b49de8d1SLois Curfman McInnes { 620b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 621dfbe8321SBarry Smith PetscErrorCode ierr; 622d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 623d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 624b49de8d1SLois Curfman McInnes 6253a40ed3dSBarry Smith PetscFunctionBegin; 626b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 627e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 628e32f2f54SBarry 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); 629b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 630b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 631b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 632e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 633e32f2f54SBarry 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); 634b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend) { 635b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 636b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 637fa852ad4SSatish Balay } else { 638905e6a2fSBarry Smith if (!aij->colmap) { 639ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 640905e6a2fSBarry Smith } 641aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 6420f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 643fa46199cSSatish Balay col--; 644b1fc9764SSatish Balay #else 645905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 646b1fc9764SSatish Balay #endif 647e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 648d9d09a02SSatish Balay else { 649b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 650b49de8d1SLois Curfman McInnes } 651b49de8d1SLois Curfman McInnes } 652b49de8d1SLois Curfman McInnes } 653f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 654b49de8d1SLois Curfman McInnes } 6553a40ed3dSBarry Smith PetscFunctionReturn(0); 656b49de8d1SLois Curfman McInnes } 657bc5ccf88SSatish Balay 658bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 659bd0c2dcbSBarry Smith 6604a2ae208SSatish Balay #undef __FUNCT__ 6614a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 662dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 663bc5ccf88SSatish Balay { 664bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 665dfbe8321SBarry Smith PetscErrorCode ierr; 666b1d57f15SBarry Smith PetscInt nstash,reallocs; 667bc5ccf88SSatish Balay InsertMode addv; 668bc5ccf88SSatish Balay 669bc5ccf88SSatish Balay PetscFunctionBegin; 6702205254eSKarl Rupp if (aij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0); 671bc5ccf88SSatish Balay 672bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 673ce94432eSBarry Smith ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 674ce94432eSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 675bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 676bc5ccf88SSatish Balay 677d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 6788798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 679ae15b995SBarry Smith ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 680bc5ccf88SSatish Balay PetscFunctionReturn(0); 681bc5ccf88SSatish Balay } 682bc5ccf88SSatish Balay 6834a2ae208SSatish Balay #undef __FUNCT__ 6844a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 685dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 686bc5ccf88SSatish Balay { 687bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 68891c97fd4SSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)aij->A->data; 6896849ba73SBarry Smith PetscErrorCode ierr; 690b1d57f15SBarry Smith PetscMPIInt n; 691b1d57f15SBarry Smith PetscInt i,j,rstart,ncols,flg; 692e44c0bd4SBarry Smith PetscInt *row,*col; 693ace3abfcSBarry Smith PetscBool other_disassembled; 69487828ca2SBarry Smith PetscScalar *val; 695bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 696bc5ccf88SSatish Balay 69791c97fd4SSatish Balay /* do not use 'b = (Mat_SeqAIJ*)aij->B->data' as B can be reset in disassembly */ 6986e111a19SKarl Rupp 699bc5ccf88SSatish Balay PetscFunctionBegin; 7004cb17eb5SBarry Smith if (!aij->donotstash && !mat->nooffprocentries) { 701a2d1c673SSatish Balay while (1) { 7028798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 703a2d1c673SSatish Balay if (!flg) break; 704a2d1c673SSatish Balay 705bc5ccf88SSatish Balay for (i=0; i<n; ) { 706bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 7072205254eSKarl Rupp for (j=i,rstart=row[j]; j<n; j++) { 7082205254eSKarl Rupp if (row[j] != rstart) break; 7092205254eSKarl Rupp } 710bc5ccf88SSatish Balay if (j < n) ncols = j-i; 711bc5ccf88SSatish Balay else ncols = n-i; 712bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 713bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 7142205254eSKarl Rupp 715bc5ccf88SSatish Balay i = j; 716bc5ccf88SSatish Balay } 717bc5ccf88SSatish Balay } 7188798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 719bc5ccf88SSatish Balay } 720bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 721bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 722bc5ccf88SSatish Balay 723bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 724bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 725bc5ccf88SSatish Balay /* 726bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 727bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 728bc5ccf88SSatish Balay */ 729bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 730ce94432eSBarry Smith ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 731bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 732ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 733ad59fb31SSatish Balay } 734ad59fb31SSatish Balay } 735bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 736bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 737bc5ccf88SSatish Balay } 7384e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 739bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 740bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 741bc5ccf88SSatish Balay 7421d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 7432205254eSKarl Rupp 744606d414cSSatish Balay aij->rowvalues = 0; 745a30b2313SHong Zhang 746a30b2313SHong Zhang /* used by MatAXPY() */ 74791c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 74891c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 749a30b2313SHong Zhang 7506bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 751bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 752e56f5c9eSBarry Smith 7534f9cfa9eSBarry Smith /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */ 7544f9cfa9eSBarry Smith if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 755e56f5c9eSBarry Smith PetscObjectState state = aij->A->nonzerostate + aij->B->nonzerostate; 75609e82e2bSBarry Smith ierr = MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 757e56f5c9eSBarry Smith } 758bc5ccf88SSatish Balay PetscFunctionReturn(0); 759bc5ccf88SSatish Balay } 760bc5ccf88SSatish Balay 7614a2ae208SSatish Balay #undef __FUNCT__ 7624a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 763dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 7641eb62cbbSBarry Smith { 76544a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 766dfbe8321SBarry Smith PetscErrorCode ierr; 7673a40ed3dSBarry Smith 7683a40ed3dSBarry Smith PetscFunctionBegin; 76978b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 77078b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 7713a40ed3dSBarry Smith PetscFunctionReturn(0); 7721eb62cbbSBarry Smith } 7731eb62cbbSBarry Smith 7744a2ae208SSatish Balay #undef __FUNCT__ 7754a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 7762b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7771eb62cbbSBarry Smith { 7781b1dd7adSMatthew G. Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 7791b1dd7adSMatthew G. Knepley PetscInt *owners = A->rmap->range; 7801b1dd7adSMatthew G. Knepley PetscInt n = A->rmap->n; 7811b1dd7adSMatthew G. Knepley PetscMPIInt size = mat->size; 7821b1dd7adSMatthew G. Knepley PetscSF sf; 7831b1dd7adSMatthew G. Knepley PetscInt *lrows; 7841b1dd7adSMatthew G. Knepley PetscSFNode *rrows; 7851b1dd7adSMatthew G. Knepley PetscInt lastidx = -1, r, p = 0, len = 0; 7866849ba73SBarry Smith PetscErrorCode ierr; 7871eb62cbbSBarry Smith 7883a40ed3dSBarry Smith PetscFunctionBegin; 7891b1dd7adSMatthew G. Knepley /* Create SF where leaves are input rows and roots are owned rows */ 790785e854fSJed Brown ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr); 7919d80f4afSMatthew G. Knepley for (r = 0; r < n; ++r) lrows[r] = -1; 792785e854fSJed Brown ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr); 7931b1dd7adSMatthew G. Knepley for (r = 0; r < N; ++r) { 7941b1dd7adSMatthew G. Knepley const PetscInt idx = rows[r]; 7951b1dd7adSMatthew G. Knepley PetscBool found = PETSC_FALSE; 7961b1dd7adSMatthew G. Knepley /* Trick for efficient searching for sorted rows */ 7971b1dd7adSMatthew G. Knepley if (lastidx > idx) p = 0; 7986543fbbaSBarry Smith lastidx = idx; 7991b1dd7adSMatthew G. Knepley for (; p < size; ++p) { 8001b1dd7adSMatthew G. Knepley if (idx >= owners[p] && idx < owners[p+1]) { 8011b1dd7adSMatthew G. Knepley rrows[r].rank = p; 8021b1dd7adSMatthew G. Knepley rrows[r].index = rows[r] - owners[p]; 8036543fbbaSBarry Smith found = PETSC_TRUE; 8046543fbbaSBarry Smith break; 8051eb62cbbSBarry Smith } 8061eb62cbbSBarry Smith } 8071b1dd7adSMatthew G. Knepley if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx); 8081eb62cbbSBarry Smith } 8091b1dd7adSMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr); 8101b1dd7adSMatthew G. Knepley ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr); 8111b1dd7adSMatthew G. Knepley /* Collect flags for rows to be zeroed */ 81258c26cb0SMatthew G. Knepley ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 81358c26cb0SMatthew G. Knepley ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 8141b1dd7adSMatthew G. Knepley ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 8151b1dd7adSMatthew G. Knepley /* Compress and put in row numbers */ 8169d80f4afSMatthew G. Knepley for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r; 81797b48c8fSBarry Smith /* fix right hand side if needed */ 81897b48c8fSBarry Smith if (x && b) { 8191b1dd7adSMatthew G. Knepley const PetscScalar *xx; 8201b1dd7adSMatthew G. Knepley PetscScalar *bb; 8211b1dd7adSMatthew G. Knepley 82297b48c8fSBarry Smith ierr = VecGetArrayRead(x, &xx);CHKERRQ(ierr); 82397b48c8fSBarry Smith ierr = VecGetArray(b, &bb);CHKERRQ(ierr); 8241b1dd7adSMatthew G. Knepley for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]]; 82597b48c8fSBarry Smith ierr = VecRestoreArrayRead(x, &xx);CHKERRQ(ierr); 82697b48c8fSBarry Smith ierr = VecRestoreArray(b, &bb);CHKERRQ(ierr); 82797b48c8fSBarry Smith } 8281b1dd7adSMatthew G. Knepley /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/ 8291b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->B, len, lrows, 0.0, 0,0);CHKERRQ(ierr); 8301b1dd7adSMatthew G. Knepley if ((diag != 0.0) && (mat->A->rmap->N == mat->A->cmap->N)) { 8311b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);CHKERRQ(ierr); 832f4df32b1SMatthew Knepley } else if (diag != 0.0) { 8331b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr); 8341b1dd7adSMatthew G. Knepley if (((Mat_SeqAIJ *) mat->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"); 8351b1dd7adSMatthew G. Knepley for (r = 0; r < len; ++r) { 8361b1dd7adSMatthew G. Knepley const PetscInt row = lrows[r] + A->rmap->rstart; 837f4df32b1SMatthew Knepley ierr = MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);CHKERRQ(ierr); 838e2d53e46SBarry Smith } 839e2d53e46SBarry Smith ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 840e2d53e46SBarry Smith ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 8416eb55b6aSBarry Smith } else { 8421b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr); 8436eb55b6aSBarry Smith } 844606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 8454f9cfa9eSBarry Smith 8464f9cfa9eSBarry Smith /* only change matrix nonzero state if pattern was allowed to be changed */ 8474f9cfa9eSBarry Smith if (!((Mat_SeqAIJ*)(mat->A->data))->keepnonzeropattern) { 848e56f5c9eSBarry Smith PetscObjectState state = mat->A->nonzerostate + mat->B->nonzerostate; 84909e82e2bSBarry Smith ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 850e56f5c9eSBarry Smith } 8513a40ed3dSBarry Smith PetscFunctionReturn(0); 8521eb62cbbSBarry Smith } 8531eb62cbbSBarry Smith 8544a2ae208SSatish Balay #undef __FUNCT__ 8559c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 8569c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 8579c7c4993SBarry Smith { 8589c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 8599c7c4993SBarry Smith PetscErrorCode ierr; 86054bd4135SMatthew G. Knepley PetscMPIInt size = l->size,n = A->rmap->n,lastidx = -1; 86178fab17bSMatthew G. Knepley PetscInt i,j,r,m,p = 0,len = 0; 86254bd4135SMatthew G. Knepley PetscInt *lrows,*owners = A->rmap->range; 86354bd4135SMatthew G. Knepley PetscSFNode *rrows; 86454bd4135SMatthew G. Knepley PetscSF sf; 8659c7c4993SBarry Smith const PetscScalar *xx; 866564f14d6SBarry Smith PetscScalar *bb,*mask; 867564f14d6SBarry Smith Vec xmask,lmask; 868564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 869564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 870564f14d6SBarry Smith PetscScalar *aa; 8719c7c4993SBarry Smith #if defined(PETSC_DEBUG) 8729c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 8739c7c4993SBarry Smith #endif 8749c7c4993SBarry Smith 8759c7c4993SBarry Smith PetscFunctionBegin; 87654bd4135SMatthew G. Knepley /* Create SF where leaves are input rows and roots are owned rows */ 87754bd4135SMatthew G. Knepley ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr); 87854bd4135SMatthew G. Knepley for (r = 0; r < n; ++r) lrows[r] = -1; 87954bd4135SMatthew G. Knepley ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr); 88054bd4135SMatthew G. Knepley for (r = 0; r < N; ++r) { 88154bd4135SMatthew G. Knepley const PetscInt idx = rows[r]; 88254bd4135SMatthew G. Knepley PetscBool found = PETSC_FALSE; 88354bd4135SMatthew G. Knepley /* Trick for efficient searching for sorted rows */ 88454bd4135SMatthew G. Knepley if (lastidx > idx) p = 0; 8859c7c4993SBarry Smith lastidx = idx; 88654bd4135SMatthew G. Knepley for (; p < size; ++p) { 88754bd4135SMatthew G. Knepley if (idx >= owners[p] && idx < owners[p+1]) { 88854bd4135SMatthew G. Knepley rrows[r].rank = p; 88954bd4135SMatthew G. Knepley rrows[r].index = rows[r] - owners[p]; 8909c7c4993SBarry Smith found = PETSC_TRUE; 8919c7c4993SBarry Smith break; 8929c7c4993SBarry Smith } 8939c7c4993SBarry Smith } 89454bd4135SMatthew G. Knepley if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx); 8959c7c4993SBarry Smith } 89654bd4135SMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr); 89754bd4135SMatthew G. Knepley ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr); 89854bd4135SMatthew G. Knepley /* Collect flags for rows to be zeroed */ 89954bd4135SMatthew G. Knepley ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 90054bd4135SMatthew G. Knepley ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 90154bd4135SMatthew G. Knepley ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 90254bd4135SMatthew G. Knepley /* Compress and put in row numbers */ 90354bd4135SMatthew G. Knepley for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r; 904564f14d6SBarry Smith /* zero diagonal part of matrix */ 90554bd4135SMatthew G. Knepley ierr = MatZeroRowsColumns(l->A,len,lrows,diag,x,b);CHKERRQ(ierr); 906564f14d6SBarry Smith /* handle off diagonal part of matrix */ 9070298fd71SBarry Smith ierr = MatGetVecs(A,&xmask,NULL);CHKERRQ(ierr); 908564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 909564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 91054bd4135SMatthew G. Knepley for (i=0; i<len; i++) bb[lrows[i]] = 1; 911564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 912564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 913564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 9146bf464f9SBarry Smith ierr = VecDestroy(&xmask);CHKERRQ(ierr); 915377aa5a1SBarry Smith if (x) { 91667caceb0SMatthew G. Knepley ierr = VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 91767caceb0SMatthew G. Knepley ierr = VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 918564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 919564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 920377aa5a1SBarry Smith } 921377aa5a1SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 922564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 923564f14d6SBarry Smith ii = aij->i; 92454bd4135SMatthew G. Knepley for (i=0; i<len; i++) { 925564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 9269c7c4993SBarry Smith } 927564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 928564f14d6SBarry Smith if (aij->compressedrow.use) { 929564f14d6SBarry Smith m = aij->compressedrow.nrows; 930564f14d6SBarry Smith ii = aij->compressedrow.i; 931564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 932564f14d6SBarry Smith for (i=0; i<m; i++) { 933564f14d6SBarry Smith n = ii[i+1] - ii[i]; 934564f14d6SBarry Smith aj = aij->j + ii[i]; 935564f14d6SBarry Smith aa = aij->a + ii[i]; 936564f14d6SBarry Smith 937564f14d6SBarry Smith for (j=0; j<n; j++) { 93825266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 939377aa5a1SBarry Smith if (b) bb[*ridx] -= *aa*xx[*aj]; 940564f14d6SBarry Smith *aa = 0.0; 941564f14d6SBarry Smith } 942564f14d6SBarry Smith aa++; 943564f14d6SBarry Smith aj++; 944564f14d6SBarry Smith } 945564f14d6SBarry Smith ridx++; 946564f14d6SBarry Smith } 947564f14d6SBarry Smith } else { /* do not use compressed row format */ 948564f14d6SBarry Smith m = l->B->rmap->n; 949564f14d6SBarry Smith for (i=0; i<m; i++) { 950564f14d6SBarry Smith n = ii[i+1] - ii[i]; 951564f14d6SBarry Smith aj = aij->j + ii[i]; 952564f14d6SBarry Smith aa = aij->a + ii[i]; 953564f14d6SBarry Smith for (j=0; j<n; j++) { 95425266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 955377aa5a1SBarry Smith if (b) bb[i] -= *aa*xx[*aj]; 956564f14d6SBarry Smith *aa = 0.0; 957564f14d6SBarry Smith } 958564f14d6SBarry Smith aa++; 959564f14d6SBarry Smith aj++; 960564f14d6SBarry Smith } 961564f14d6SBarry Smith } 962564f14d6SBarry Smith } 963377aa5a1SBarry Smith if (x) { 964564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 965564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 966377aa5a1SBarry Smith } 967377aa5a1SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 9686bf464f9SBarry Smith ierr = VecDestroy(&lmask);CHKERRQ(ierr); 9699c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 9704f9cfa9eSBarry Smith 9714f9cfa9eSBarry Smith /* only change matrix nonzero state if pattern was allowed to be changed */ 9724f9cfa9eSBarry Smith if (!((Mat_SeqAIJ*)(l->A->data))->keepnonzeropattern) { 9734f9cfa9eSBarry Smith PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate; 9744f9cfa9eSBarry Smith ierr = MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 9754f9cfa9eSBarry Smith } 9769c7c4993SBarry Smith PetscFunctionReturn(0); 9779c7c4993SBarry Smith } 9789c7c4993SBarry Smith 9799c7c4993SBarry Smith #undef __FUNCT__ 9804a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 981dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 9821eb62cbbSBarry Smith { 983416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 984dfbe8321SBarry Smith PetscErrorCode ierr; 985b1d57f15SBarry Smith PetscInt nt; 986416022c9SBarry Smith 9873a40ed3dSBarry Smith PetscFunctionBegin; 988a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 98965e19b50SBarry 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); 990ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 991f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 992ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 993f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 9943a40ed3dSBarry Smith PetscFunctionReturn(0); 9951eb62cbbSBarry Smith } 9961eb62cbbSBarry Smith 9974a2ae208SSatish Balay #undef __FUNCT__ 998bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 999bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 1000bd0c2dcbSBarry Smith { 1001bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1002bd0c2dcbSBarry Smith PetscErrorCode ierr; 1003bd0c2dcbSBarry Smith 1004bd0c2dcbSBarry Smith PetscFunctionBegin; 1005bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 1006bd0c2dcbSBarry Smith PetscFunctionReturn(0); 1007bd0c2dcbSBarry Smith } 1008bd0c2dcbSBarry Smith 1009bd0c2dcbSBarry Smith #undef __FUNCT__ 10104a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 1011dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1012da3a660dSBarry Smith { 1013416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1014dfbe8321SBarry Smith PetscErrorCode ierr; 10153a40ed3dSBarry Smith 10163a40ed3dSBarry Smith PetscFunctionBegin; 1017ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1018f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1019ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1020f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 10213a40ed3dSBarry Smith PetscFunctionReturn(0); 1022da3a660dSBarry Smith } 1023da3a660dSBarry Smith 10244a2ae208SSatish Balay #undef __FUNCT__ 10254a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 1026dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 1027da3a660dSBarry Smith { 1028416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1029dfbe8321SBarry Smith PetscErrorCode ierr; 1030ace3abfcSBarry Smith PetscBool merged; 1031da3a660dSBarry Smith 10323a40ed3dSBarry Smith PetscFunctionBegin; 1033a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 1034da3a660dSBarry Smith /* do nondiagonal part */ 10357c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1036a5ff213dSBarry Smith if (!merged) { 1037da3a660dSBarry Smith /* send it on its way */ 1038ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1039da3a660dSBarry Smith /* do local part */ 10407c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1041da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 1042a5ff213dSBarry Smith /* added in yy until the next line, */ 1043ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1044a5ff213dSBarry Smith } else { 1045a5ff213dSBarry Smith /* do local part */ 1046a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1047a5ff213dSBarry Smith /* send it on its way */ 1048ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1049a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 1050ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1051a5ff213dSBarry Smith } 10523a40ed3dSBarry Smith PetscFunctionReturn(0); 1053da3a660dSBarry Smith } 1054da3a660dSBarry Smith 1055cd0d46ebSvictorle #undef __FUNCT__ 10565fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 10577087cfbeSBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1058cd0d46ebSvictorle { 10594f423910Svictorle MPI_Comm comm; 1060cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ*) Amat->data, *Bij; 106166501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1062cd0d46ebSvictorle IS Me,Notme; 10636849ba73SBarry Smith PetscErrorCode ierr; 1064b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1065b1d57f15SBarry Smith PetscMPIInt size; 1066cd0d46ebSvictorle 1067cd0d46ebSvictorle PetscFunctionBegin; 106842e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 106966501d38Svictorle Bij = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A; 10705485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1071cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 10724f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1073b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1074b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 107542e5f5b4Svictorle 107642e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1077cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1078cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1079785e854fSJed Brown ierr = PetscMalloc1((N-last+first),¬me);CHKERRQ(ierr); 1080cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1081cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 108270b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1083268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1084268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 108566501d38Svictorle Aoff = Aoffs[0]; 1086268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 108766501d38Svictorle Boff = Boffs[0]; 10885485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 108966501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 109066501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 10916bf464f9SBarry Smith ierr = ISDestroy(&Me);CHKERRQ(ierr); 10926bf464f9SBarry Smith ierr = ISDestroy(&Notme);CHKERRQ(ierr); 10933e0d0d19SHong Zhang ierr = PetscFree(notme);CHKERRQ(ierr); 1094cd0d46ebSvictorle PetscFunctionReturn(0); 1095cd0d46ebSvictorle } 1096cd0d46ebSvictorle 10974a2ae208SSatish Balay #undef __FUNCT__ 10984a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1099dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1100da3a660dSBarry Smith { 1101416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1102dfbe8321SBarry Smith PetscErrorCode ierr; 1103da3a660dSBarry Smith 11043a40ed3dSBarry Smith PetscFunctionBegin; 1105da3a660dSBarry Smith /* do nondiagonal part */ 11067c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1107da3a660dSBarry Smith /* send it on its way */ 1108ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1109da3a660dSBarry Smith /* do local part */ 11107c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1111a5ff213dSBarry Smith /* receive remote parts */ 1112ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 11133a40ed3dSBarry Smith PetscFunctionReturn(0); 1114da3a660dSBarry Smith } 1115da3a660dSBarry Smith 11161eb62cbbSBarry Smith /* 11171eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 11181eb62cbbSBarry Smith diagonal block 11191eb62cbbSBarry Smith */ 11204a2ae208SSatish Balay #undef __FUNCT__ 11214a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1122dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 11231eb62cbbSBarry Smith { 1124dfbe8321SBarry Smith PetscErrorCode ierr; 1125416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 11263a40ed3dSBarry Smith 11273a40ed3dSBarry Smith PetscFunctionBegin; 1128ce94432eSBarry 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"); 1129e7e72b3dSBarry 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"); 11303a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 11313a40ed3dSBarry Smith PetscFunctionReturn(0); 11321eb62cbbSBarry Smith } 11331eb62cbbSBarry Smith 11344a2ae208SSatish Balay #undef __FUNCT__ 11354a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1136f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1137052efed2SBarry Smith { 1138052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1139dfbe8321SBarry Smith PetscErrorCode ierr; 11403a40ed3dSBarry Smith 11413a40ed3dSBarry Smith PetscFunctionBegin; 1142f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1143f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 11443a40ed3dSBarry Smith PetscFunctionReturn(0); 1145052efed2SBarry Smith } 1146052efed2SBarry Smith 11474a2ae208SSatish Balay #undef __FUNCT__ 11484a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1149dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 11501eb62cbbSBarry Smith { 115144a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1152dfbe8321SBarry Smith PetscErrorCode ierr; 115383e2fdc7SBarry Smith 11543a40ed3dSBarry Smith PetscFunctionBegin; 1155aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1156d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1157a5a9c739SBarry Smith #endif 11588798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 11596bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 11606bf464f9SBarry Smith ierr = MatDestroy(&aij->A);CHKERRQ(ierr); 11616bf464f9SBarry Smith ierr = MatDestroy(&aij->B);CHKERRQ(ierr); 1162aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 11636bc0bbbfSBarry Smith ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr); 1164b1fc9764SSatish Balay #else 116505b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1166b1fc9764SSatish Balay #endif 116705b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 11686bf464f9SBarry Smith ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr); 11696bf464f9SBarry Smith ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr); 117003095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 11718aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1172bf0cc555SLisandro Dalcin ierr = PetscFree(mat->data);CHKERRQ(ierr); 1173901853e0SKris Buschelman 1174dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1175bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);CHKERRQ(ierr); 1176bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);CHKERRQ(ierr); 1177bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);CHKERRQ(ierr); 1178bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);CHKERRQ(ierr); 1179bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);CHKERRQ(ierr); 1180bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr); 1181bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);CHKERRQ(ierr); 1182bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);CHKERRQ(ierr); 11833a40ed3dSBarry Smith PetscFunctionReturn(0); 11841eb62cbbSBarry Smith } 1185ee50ffe9SBarry Smith 11864a2ae208SSatish Balay #undef __FUNCT__ 11878e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1188dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 11898e2fed03SBarry Smith { 11908e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 11918e2fed03SBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ*)aij->A->data; 11928e2fed03SBarry Smith Mat_SeqAIJ *B = (Mat_SeqAIJ*)aij->B->data; 11936849ba73SBarry Smith PetscErrorCode ierr; 119432dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 11956f69ff64SBarry Smith int fd; 1196a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1197d892089bSMatthew G. Knepley PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0; 11988e2fed03SBarry Smith PetscScalar *column_values; 119985ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 1200b37d52dbSMark F. Adams FILE *file; 12018e2fed03SBarry Smith 12028e2fed03SBarry Smith PetscFunctionBegin; 1203ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1204ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 12058e2fed03SBarry Smith nz = A->nz + B->nz; 1206958c9bccSBarry Smith if (!rank) { 12070700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1208d0f46423SBarry Smith header[1] = mat->rmap->N; 1209d0f46423SBarry Smith header[2] = mat->cmap->N; 12102205254eSKarl Rupp 1211ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 12128e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 12136f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12148e2fed03SBarry Smith /* get largest number of rows any processor has */ 1215d0f46423SBarry Smith rlen = mat->rmap->n; 1216d0f46423SBarry Smith range = mat->rmap->range; 12172205254eSKarl Rupp for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]); 12188e2fed03SBarry Smith } else { 1219ce94432eSBarry Smith ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1220d0f46423SBarry Smith rlen = mat->rmap->n; 12218e2fed03SBarry Smith } 12228e2fed03SBarry Smith 12238e2fed03SBarry Smith /* load up the local row counts */ 1224785e854fSJed Brown ierr = PetscMalloc1((rlen+1),&row_lengths);CHKERRQ(ierr); 12252205254eSKarl 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]; 12268e2fed03SBarry Smith 12278e2fed03SBarry Smith /* store the row lengths to the file */ 122885ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1229958c9bccSBarry Smith if (!rank) { 1230d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12318e2fed03SBarry Smith for (i=1; i<size; i++) { 1232639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 12338e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1234ce94432eSBarry Smith ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 12356f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12368e2fed03SBarry Smith } 1237639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 12388e2fed03SBarry Smith } else { 1239639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1240ce94432eSBarry Smith ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1241639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 12428e2fed03SBarry Smith } 12438e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 12448e2fed03SBarry Smith 12458e2fed03SBarry Smith /* load up the local column indices */ 12461147fc2aSKarl Rupp nzmax = nz; /* th processor needs space a largest processor needs */ 1247ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1248785e854fSJed Brown ierr = PetscMalloc1((nzmax+1),&column_indices);CHKERRQ(ierr); 12498e2fed03SBarry Smith cnt = 0; 1250d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12518e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12528e2fed03SBarry Smith if ((col = garray[B->j[j]]) > cstart) break; 12538e2fed03SBarry Smith column_indices[cnt++] = col; 12548e2fed03SBarry Smith } 12552205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart; 12562205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]]; 12578e2fed03SBarry Smith } 1258e32f2f54SBarry 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); 12598e2fed03SBarry Smith 12608e2fed03SBarry Smith /* store the column indices to the file */ 126185ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1262958c9bccSBarry Smith if (!rank) { 12638e2fed03SBarry Smith MPI_Status status; 12646f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12658e2fed03SBarry Smith for (i=1; i<size; i++) { 1266639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1267ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1268e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1269ce94432eSBarry Smith ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 12706f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12718e2fed03SBarry Smith } 1272639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 12738e2fed03SBarry Smith } else { 1274639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1275ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1276ce94432eSBarry Smith ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1277639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 12788e2fed03SBarry Smith } 12798e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 12808e2fed03SBarry Smith 12818e2fed03SBarry Smith /* load up the local column values */ 1282785e854fSJed Brown ierr = PetscMalloc1((nzmax+1),&column_values);CHKERRQ(ierr); 12838e2fed03SBarry Smith cnt = 0; 1284d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12858e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12868e2fed03SBarry Smith if (garray[B->j[j]] > cstart) break; 12878e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12888e2fed03SBarry Smith } 12892205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k]; 12902205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j]; 12918e2fed03SBarry Smith } 1292e32f2f54SBarry 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); 12938e2fed03SBarry Smith 12948e2fed03SBarry Smith /* store the column values to the file */ 129585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1296958c9bccSBarry Smith if (!rank) { 12978e2fed03SBarry Smith MPI_Status status; 12986f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12998e2fed03SBarry Smith for (i=1; i<size; i++) { 1300639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1301ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1302e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1303ce94432eSBarry Smith ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 13046f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 13058e2fed03SBarry Smith } 1306639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 13078e2fed03SBarry Smith } else { 1308639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1309ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1310ce94432eSBarry Smith ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1311639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 13128e2fed03SBarry Smith } 13138e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 1314b37d52dbSMark F. Adams 1315b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 131633d57670SJed Brown if (file) fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(mat->rmap->bs)); 13178e2fed03SBarry Smith PetscFunctionReturn(0); 13188e2fed03SBarry Smith } 13198e2fed03SBarry Smith 13209804daf3SBarry Smith #include <petscdraw.h> 13218e2fed03SBarry Smith #undef __FUNCT__ 13224a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1323dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1324416022c9SBarry Smith { 132544a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1326dfbe8321SBarry Smith PetscErrorCode ierr; 132732dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1328ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1329b0a32e0cSBarry Smith PetscViewer sviewer; 1330f3ef73ceSBarry Smith PetscViewerFormat format; 1331416022c9SBarry Smith 13323a40ed3dSBarry Smith PetscFunctionBegin; 1333251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1334251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1335251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 133632077d6dSBarry Smith if (iascii) { 1337b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1338456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 13394e220ebcSLois Curfman McInnes MatInfo info; 1340ace3abfcSBarry Smith PetscBool inodes; 1341923f20ffSKris Buschelman 1342ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1343888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 13440298fd71SBarry Smith ierr = MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);CHKERRQ(ierr); 13457b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); 1346923f20ffSKris Buschelman if (!inodes) { 134777431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1348d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13496831982aSBarry Smith } else { 135077431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1351d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13526831982aSBarry Smith } 1353888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 135477431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1355888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 135677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1357b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 13587b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); 135907d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1360a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 13613a40ed3dSBarry Smith PetscFunctionReturn(0); 1362fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1363923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1364923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1365923f20ffSKris Buschelman if (inodes) { 1366923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1367d38fa0fbSBarry Smith } else { 1368d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1369d38fa0fbSBarry Smith } 13703a40ed3dSBarry Smith PetscFunctionReturn(0); 13714aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 13724aedb280SBarry Smith PetscFunctionReturn(0); 137308480c60SBarry Smith } 13748e2fed03SBarry Smith } else if (isbinary) { 13758e2fed03SBarry Smith if (size == 1) { 13767adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 13778e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13788e2fed03SBarry Smith } else { 13798e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 13808e2fed03SBarry Smith } 13818e2fed03SBarry Smith PetscFunctionReturn(0); 13820f5bd95cSBarry Smith } else if (isdraw) { 1383b0a32e0cSBarry Smith PetscDraw draw; 1384ace3abfcSBarry Smith PetscBool isnull; 1385b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1386b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 138719bcc07fSBarry Smith } 138819bcc07fSBarry Smith 13897da1fb6eSBarry Smith { 139095373324SBarry Smith /* assemble the entire matrix onto first processor. */ 139195373324SBarry Smith Mat A; 1392ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1393d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1394dd6ea824SBarry Smith MatScalar *a; 13952ee70a88SLois Curfman McInnes 1396ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr); 139717699dbbSLois Curfman McInnes if (!rank) { 1398f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 13993a40ed3dSBarry Smith } else { 1400f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 140195373324SBarry Smith } 1402f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1403f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 14040298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);CHKERRQ(ierr); 14052b82e772SSatish Balay ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 14063bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)A);CHKERRQ(ierr); 1407416022c9SBarry Smith 140895373324SBarry Smith /* copy over the A part */ 1409ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1410d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1411d0f46423SBarry Smith row = mat->rmap->rstart; 14122205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart; 141395373324SBarry Smith for (i=0; i<m; i++) { 1414416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 141526fbe8dcSKarl Rupp row++; 141626fbe8dcSKarl Rupp a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 141795373324SBarry Smith } 14182ee70a88SLois Curfman McInnes aj = Aloc->j; 14192205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart; 142095373324SBarry Smith 142195373324SBarry Smith /* copy over the B part */ 1422ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1423d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1424d0f46423SBarry Smith row = mat->rmap->rstart; 1425785e854fSJed Brown ierr = PetscMalloc1((ai[m]+1),&cols);CHKERRQ(ierr); 1426b0a32e0cSBarry Smith ct = cols; 14272205254eSKarl Rupp for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]]; 142895373324SBarry Smith for (i=0; i<m; i++) { 1429416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 14302205254eSKarl Rupp row++; 14312205254eSKarl Rupp a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 143295373324SBarry Smith } 1433606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 14346d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 14356d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 143655843e3eSBarry Smith /* 143755843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1438b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 143955843e3eSBarry Smith */ 1440b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1441e03a110bSBarry Smith if (!rank) { 14427da1fb6eSBarry Smith ierr = MatView_SeqAIJ(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 144395373324SBarry Smith } 1444b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 14456bf464f9SBarry Smith ierr = MatDestroy(&A);CHKERRQ(ierr); 144695373324SBarry Smith } 14473a40ed3dSBarry Smith PetscFunctionReturn(0); 14481eb62cbbSBarry Smith } 14491eb62cbbSBarry Smith 14504a2ae208SSatish Balay #undef __FUNCT__ 14514a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1452dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1453416022c9SBarry Smith { 1454dfbe8321SBarry Smith PetscErrorCode ierr; 1455ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1456416022c9SBarry Smith 14573a40ed3dSBarry Smith PetscFunctionBegin; 1458251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1459251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1460251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1461251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 146232077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 14637b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 1464416022c9SBarry Smith } 14653a40ed3dSBarry Smith PetscFunctionReturn(0); 1466416022c9SBarry Smith } 1467416022c9SBarry Smith 14684a2ae208SSatish Balay #undef __FUNCT__ 146941f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 147041f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 14718a729477SBarry Smith { 147244a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1473dfbe8321SBarry Smith PetscErrorCode ierr; 14746987fefcSBarry Smith Vec bb1 = 0; 1475ace3abfcSBarry Smith PetscBool hasop; 14768a729477SBarry Smith 14773a40ed3dSBarry Smith PetscFunctionBegin; 1478a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 147941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1480a2b30743SBarry Smith PetscFunctionReturn(0); 1481a2b30743SBarry Smith } 1482a2b30743SBarry Smith 14834e980039SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 14844e980039SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 14854e980039SJed Brown } 14864e980039SJed Brown 1487c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) { 1488da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 148941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14902798e883SHong Zhang its--; 1491da3a660dSBarry Smith } 14922798e883SHong Zhang 14932798e883SHong Zhang while (its--) { 1494ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1495ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14962798e883SHong Zhang 1497c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1498efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1499c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 15002798e883SHong Zhang 1501c14dc6b6SHong Zhang /* local sweep */ 150241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15032798e883SHong Zhang } 15043a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP) { 1505da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 150641f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 15072798e883SHong Zhang its--; 1508da3a660dSBarry Smith } 15092798e883SHong Zhang while (its--) { 1510ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1511ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 15122798e883SHong Zhang 1513c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1514efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1515c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1516c14dc6b6SHong Zhang 1517c14dc6b6SHong Zhang /* local sweep */ 151841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15192798e883SHong Zhang } 15203a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) { 1521da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 152241f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 15232798e883SHong Zhang its--; 1524da3a660dSBarry Smith } 15252798e883SHong Zhang while (its--) { 1526ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1527ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 15282798e883SHong Zhang 1529c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1530efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1531c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 15322798e883SHong Zhang 1533c14dc6b6SHong Zhang /* local sweep */ 153441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15352798e883SHong Zhang } 1536a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1537a7420bb7SBarry Smith Vec xx1; 1538a7420bb7SBarry Smith 1539a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 154041f059aeSBarry 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); 1541a7420bb7SBarry Smith 1542a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1543a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1544a7420bb7SBarry Smith if (!mat->diag) { 15450298fd71SBarry Smith ierr = MatGetVecs(matin,&mat->diag,NULL);CHKERRQ(ierr); 1546a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1547a7420bb7SBarry Smith } 1548bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1549bd0c2dcbSBarry Smith if (hasop) { 1550bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1551bd0c2dcbSBarry Smith } else { 1552a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1553bd0c2dcbSBarry Smith } 1554887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1555887ee2caSBarry Smith 1556a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1557a7420bb7SBarry Smith 1558a7420bb7SBarry Smith /* local sweep */ 155941f059aeSBarry 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); 1560a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 15616bf464f9SBarry Smith ierr = VecDestroy(&xx1);CHKERRQ(ierr); 1562ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported"); 1563c14dc6b6SHong Zhang 15646bf464f9SBarry Smith ierr = VecDestroy(&bb1);CHKERRQ(ierr); 15653a40ed3dSBarry Smith PetscFunctionReturn(0); 15668a729477SBarry Smith } 1567a66be287SLois Curfman McInnes 15684a2ae208SSatish Balay #undef __FUNCT__ 156942e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 157042e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 157142e855d1Svictor { 157272e6a0cfSJed Brown Mat aA,aB,Aperm; 157372e6a0cfSJed Brown const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj; 157472e6a0cfSJed Brown PetscScalar *aa,*ba; 157572e6a0cfSJed Brown PetscInt i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest; 157672e6a0cfSJed Brown PetscSF rowsf,sf; 15770298fd71SBarry Smith IS parcolp = NULL; 157872e6a0cfSJed Brown PetscBool done; 157942e855d1Svictor PetscErrorCode ierr; 158042e855d1Svictor 158142e855d1Svictor PetscFunctionBegin; 158272e6a0cfSJed Brown ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 158372e6a0cfSJed Brown ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr); 158472e6a0cfSJed Brown ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr); 1585dcca6d9dSJed Brown ierr = PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);CHKERRQ(ierr); 158672e6a0cfSJed Brown 158772e6a0cfSJed Brown /* Invert row permutation to find out where my rows should go */ 1588ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);CHKERRQ(ierr); 15890298fd71SBarry Smith ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr); 1590e9e74f11SJed Brown ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr); 159172e6a0cfSJed Brown for (i=0; i<m; i++) work[i] = A->rmap->rstart + i; 15928bfbc91cSJed Brown ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr); 15938bfbc91cSJed Brown ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr); 159472e6a0cfSJed Brown 159572e6a0cfSJed Brown /* Invert column permutation to find out where my columns should go */ 1596ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 15970298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr); 1598e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 159972e6a0cfSJed Brown for (i=0; i<n; i++) work[i] = A->cmap->rstart + i; 16008bfbc91cSJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr); 16018bfbc91cSJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr); 160272e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 160372e6a0cfSJed Brown 160472e6a0cfSJed Brown ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr); 160572e6a0cfSJed Brown ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr); 160672e6a0cfSJed Brown ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr); 160772e6a0cfSJed Brown 160872e6a0cfSJed Brown /* Find out where my gcols should go */ 16090298fd71SBarry Smith ierr = MatGetSize(aB,NULL,&ng);CHKERRQ(ierr); 1610785e854fSJed Brown ierr = PetscMalloc1(ng,&gcdest);CHKERRQ(ierr); 1611ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 16120298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr); 1613e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 161472e6a0cfSJed Brown ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 161572e6a0cfSJed Brown ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 161672e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 161772e6a0cfSJed Brown 16181795a4d1SJed Brown ierr = PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);CHKERRQ(ierr); 161972e6a0cfSJed Brown ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 162072e6a0cfSJed Brown ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 162172e6a0cfSJed Brown for (i=0; i<m; i++) { 162272e6a0cfSJed Brown PetscInt row = rdest[i],rowner; 162372e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr); 162472e6a0cfSJed Brown for (j=ai[i]; j<ai[i+1]; j++) { 162572e6a0cfSJed Brown PetscInt cowner,col = cdest[aj[j]]; 162672e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */ 162772e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 162872e6a0cfSJed Brown else onnz[i]++; 162972e6a0cfSJed Brown } 163072e6a0cfSJed Brown for (j=bi[i]; j<bi[i+1]; j++) { 163172e6a0cfSJed Brown PetscInt cowner,col = gcdest[bj[j]]; 163272e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); 163372e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 163472e6a0cfSJed Brown else onnz[i]++; 163572e6a0cfSJed Brown } 163672e6a0cfSJed Brown } 163772e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 163872e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 163972e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 164072e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 164172e6a0cfSJed Brown ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr); 164272e6a0cfSJed Brown 1643ce94432eSBarry 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); 164472e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr); 164572e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr); 164672e6a0cfSJed Brown for (i=0; i<m; i++) { 164772e6a0cfSJed Brown PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */ 1648970468b0SJed Brown PetscInt j0,rowlen; 164972e6a0cfSJed Brown rowlen = ai[i+1] - ai[i]; 1650970468b0SJed Brown for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */ 1651970468b0SJed Brown for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]]; 1652970468b0SJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);CHKERRQ(ierr); 1653970468b0SJed Brown } 165472e6a0cfSJed Brown rowlen = bi[i+1] - bi[i]; 1655970468b0SJed Brown for (j0=j=0; j<rowlen; j0=j) { 1656970468b0SJed Brown for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]]; 1657970468b0SJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);CHKERRQ(ierr); 1658970468b0SJed Brown } 165972e6a0cfSJed Brown } 166072e6a0cfSJed Brown ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 166172e6a0cfSJed Brown ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 166272e6a0cfSJed Brown ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 166372e6a0cfSJed Brown ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 166472e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr); 166572e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr); 166672e6a0cfSJed Brown ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr); 166772e6a0cfSJed Brown ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr); 166872e6a0cfSJed Brown ierr = PetscFree(gcdest);CHKERRQ(ierr); 166972e6a0cfSJed Brown if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);} 167072e6a0cfSJed Brown *B = Aperm; 167142e855d1Svictor PetscFunctionReturn(0); 167242e855d1Svictor } 167342e855d1Svictor 167442e855d1Svictor #undef __FUNCT__ 16754a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1676dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1677a66be287SLois Curfman McInnes { 1678a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1679a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1680dfbe8321SBarry Smith PetscErrorCode ierr; 1681329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1682a66be287SLois Curfman McInnes 16833a40ed3dSBarry Smith PetscFunctionBegin; 16844e220ebcSLois Curfman McInnes info->block_size = 1.0; 16854e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 16862205254eSKarl Rupp 16874e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 16884e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 16892205254eSKarl Rupp 16904e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 16912205254eSKarl Rupp 16924e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 16934e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1694a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 16954e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 16964e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 16974e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 16984e220ebcSLois Curfman McInnes info->memory = isend[3]; 16994e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1700a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1701ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 17022205254eSKarl Rupp 17034e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 17044e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 17054e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 17064e220ebcSLois Curfman McInnes info->memory = irecv[3]; 17074e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1708a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1709ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 17102205254eSKarl Rupp 17114e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 17124e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 17134e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 17144e220ebcSLois Curfman McInnes info->memory = irecv[3]; 17154e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1716a66be287SLois Curfman McInnes } 17174e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 17184e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 17194e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 17203a40ed3dSBarry Smith PetscFunctionReturn(0); 1721a66be287SLois Curfman McInnes } 1722a66be287SLois Curfman McInnes 17234a2ae208SSatish Balay #undef __FUNCT__ 17244a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1725ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1726c74985f6SBarry Smith { 1727c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1728dfbe8321SBarry Smith PetscErrorCode ierr; 1729c74985f6SBarry Smith 17303a40ed3dSBarry Smith PetscFunctionBegin; 173112c028f9SKris Buschelman switch (op) { 1732512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 173312c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 173428b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1735a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 173612c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 173712c028f9SKris Buschelman case MAT_USE_INODES: 173812c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 1739fa1f0d2cSMatthew G Knepley MatCheckPreallocated(A,1); 17404e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 17414e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 174212c028f9SKris Buschelman break; 174312c028f9SKris Buschelman case MAT_ROW_ORIENTED: 17444e0d8c25SBarry Smith a->roworiented = flg; 17452205254eSKarl Rupp 17464e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 17474e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 174812c028f9SKris Buschelman break; 17494e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1750290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 175112c028f9SKris Buschelman break; 175212c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 17535c0f0b64SBarry Smith a->donotstash = flg; 175412c028f9SKris Buschelman break; 1755ffa07934SHong Zhang case MAT_SPD: 1756ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1757ffa07934SHong Zhang A->spd = flg; 1758ffa07934SHong Zhang if (flg) { 1759ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1760ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1761ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1762ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1763ffa07934SHong Zhang } 1764ffa07934SHong Zhang break; 176577e54ba9SKris Buschelman case MAT_SYMMETRIC: 17664e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 176725f421beSHong Zhang break; 176877e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1769eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1770eeffb40dSHong Zhang break; 1771bf108f30SBarry Smith case MAT_HERMITIAN: 1772eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1773eeffb40dSHong Zhang break; 1774bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 17754e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 177677e54ba9SKris Buschelman break; 177712c028f9SKris Buschelman default: 1778e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 17793a40ed3dSBarry Smith } 17803a40ed3dSBarry Smith PetscFunctionReturn(0); 1781c74985f6SBarry Smith } 1782c74985f6SBarry Smith 17834a2ae208SSatish Balay #undef __FUNCT__ 17844a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1785b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 178639e00950SLois Curfman McInnes { 1787154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 178887828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 17896849ba73SBarry Smith PetscErrorCode ierr; 1790d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1791d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1792b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 179339e00950SLois Curfman McInnes 17943a40ed3dSBarry Smith PetscFunctionBegin; 1795e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 17967a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 17977a0afa10SBarry Smith 179870f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 17997a0afa10SBarry Smith /* 18007a0afa10SBarry Smith allocate enough space to hold information from the longest row. 18017a0afa10SBarry Smith */ 18027a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1803b1d57f15SBarry Smith PetscInt max = 1,tmp; 1804d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 18057a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 18062205254eSKarl Rupp if (max < tmp) max = tmp; 18077a0afa10SBarry Smith } 1808dcca6d9dSJed Brown ierr = PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);CHKERRQ(ierr); 18097a0afa10SBarry Smith } 18107a0afa10SBarry Smith 1811e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1812abc0e9e4SLois Curfman McInnes lrow = row - rstart; 181339e00950SLois Curfman McInnes 1814154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1815154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1816154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1817f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1818f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1819154123eaSLois Curfman McInnes nztot = nzA + nzB; 1820154123eaSLois Curfman McInnes 182170f0671dSBarry Smith cmap = mat->garray; 1822154123eaSLois Curfman McInnes if (v || idx) { 1823154123eaSLois Curfman McInnes if (nztot) { 1824154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1825b1d57f15SBarry Smith PetscInt imark = -1; 1826154123eaSLois Curfman McInnes if (v) { 182770f0671dSBarry Smith *v = v_p = mat->rowvalues; 182839e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 182970f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1830154123eaSLois Curfman McInnes else break; 1831154123eaSLois Curfman McInnes } 1832154123eaSLois Curfman McInnes imark = i; 183370f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 183470f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1835154123eaSLois Curfman McInnes } 1836154123eaSLois Curfman McInnes if (idx) { 183770f0671dSBarry Smith *idx = idx_p = mat->rowindices; 183870f0671dSBarry Smith if (imark > -1) { 183970f0671dSBarry Smith for (i=0; i<imark; i++) { 184070f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 184170f0671dSBarry Smith } 184270f0671dSBarry Smith } else { 1843154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 184470f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1845154123eaSLois Curfman McInnes else break; 1846154123eaSLois Curfman McInnes } 1847154123eaSLois Curfman McInnes imark = i; 184870f0671dSBarry Smith } 184970f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 185070f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 185139e00950SLois Curfman McInnes } 18523f97c4b0SBarry Smith } else { 18531ca473b0SSatish Balay if (idx) *idx = 0; 18541ca473b0SSatish Balay if (v) *v = 0; 18551ca473b0SSatish Balay } 1856154123eaSLois Curfman McInnes } 185739e00950SLois Curfman McInnes *nz = nztot; 1858f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1859f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 18603a40ed3dSBarry Smith PetscFunctionReturn(0); 186139e00950SLois Curfman McInnes } 186239e00950SLois Curfman McInnes 18634a2ae208SSatish Balay #undef __FUNCT__ 18644a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1865b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 186639e00950SLois Curfman McInnes { 18677a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 18683a40ed3dSBarry Smith 18693a40ed3dSBarry Smith PetscFunctionBegin; 1870e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 18717a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 18723a40ed3dSBarry Smith PetscFunctionReturn(0); 187339e00950SLois Curfman McInnes } 187439e00950SLois Curfman McInnes 18754a2ae208SSatish Balay #undef __FUNCT__ 18764a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1877dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1878855ac2c5SLois Curfman McInnes { 1879855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1880ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1881dfbe8321SBarry Smith PetscErrorCode ierr; 1882d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1883329f5518SBarry Smith PetscReal sum = 0.0; 1884a77337e4SBarry Smith MatScalar *v; 188504ca555eSLois Curfman McInnes 18863a40ed3dSBarry Smith PetscFunctionBegin; 188717699dbbSLois Curfman McInnes if (aij->size == 1) { 188814183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 188937fa93a5SLois Curfman McInnes } else { 189004ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 189104ca555eSLois Curfman McInnes v = amat->a; 189204ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1893329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 189404ca555eSLois Curfman McInnes } 189504ca555eSLois Curfman McInnes v = bmat->a; 189604ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1897329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 189804ca555eSLois Curfman McInnes } 1899ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 19008f1a2a5eSBarry Smith *norm = PetscSqrtReal(*norm); 19013a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1902329f5518SBarry Smith PetscReal *tmp,*tmp2; 1903b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 19041795a4d1SJed Brown ierr = PetscCalloc1((mat->cmap->N+1),&tmp);CHKERRQ(ierr); 1905785e854fSJed Brown ierr = PetscMalloc1((mat->cmap->N+1),&tmp2);CHKERRQ(ierr); 190604ca555eSLois Curfman McInnes *norm = 0.0; 190704ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 190804ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1909bfec09a0SHong Zhang tmp[cstart + *jj++] += PetscAbsScalar(*v); v++; 191004ca555eSLois Curfman McInnes } 191104ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 191204ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1913bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 191404ca555eSLois Curfman McInnes } 1915ce94432eSBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1916d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 191704ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 191804ca555eSLois Curfman McInnes } 1919606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1920606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 19213a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1922329f5518SBarry Smith PetscReal ntemp = 0.0; 1923d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1924bfec09a0SHong Zhang v = amat->a + amat->i[j]; 192504ca555eSLois Curfman McInnes sum = 0.0; 192604ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1927cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 192804ca555eSLois Curfman McInnes } 1929bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 193004ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1931cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 193204ca555eSLois Curfman McInnes } 1933515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 193404ca555eSLois Curfman McInnes } 1935ce94432eSBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1936ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm"); 193737fa93a5SLois Curfman McInnes } 19383a40ed3dSBarry Smith PetscFunctionReturn(0); 1939855ac2c5SLois Curfman McInnes } 1940855ac2c5SLois Curfman McInnes 19414a2ae208SSatish Balay #undef __FUNCT__ 19424a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1943fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1944b7c46309SBarry Smith { 1945b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1946da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1947dfbe8321SBarry Smith PetscErrorCode ierr; 194880bcc5a1SJed Brown PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i; 1949d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart,ncol; 19503a40ed3dSBarry Smith Mat B; 1951a77337e4SBarry Smith MatScalar *array; 1952b7c46309SBarry Smith 19533a40ed3dSBarry Smith PetscFunctionBegin; 1954ce94432eSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1955da668accSHong Zhang 195680bcc5a1SJed Brown ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n; 1957da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1958da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1959fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 196080bcc5a1SJed Brown PetscInt *d_nnz,*g_nnz,*o_nnz; 196180bcc5a1SJed Brown PetscSFNode *oloc; 1962713c93b4SJed Brown PETSC_UNUSED PetscSF sf; 196380bcc5a1SJed Brown 1964dcca6d9dSJed Brown ierr = PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);CHKERRQ(ierr); 196580bcc5a1SJed Brown /* compute d_nnz for preallocation */ 196680bcc5a1SJed Brown ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 1967da668accSHong Zhang for (i=0; i<ai[ma]; i++) { 1968da668accSHong Zhang d_nnz[aj[i]]++; 1969da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1970d4bb536fSBarry Smith } 197180bcc5a1SJed Brown /* compute local off-diagonal contributions */ 19720beca09bSJed Brown ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr); 197380bcc5a1SJed Brown for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++; 197480bcc5a1SJed Brown /* map those to global */ 1975ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 19760298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr); 1977e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 197880bcc5a1SJed Brown ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 197980bcc5a1SJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 198080bcc5a1SJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 198180bcc5a1SJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 1982d4bb536fSBarry Smith 1983ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr); 1984d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 198533d57670SJed Brown ierr = MatSetBlockSizes(B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr); 19867adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 198780bcc5a1SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 198880bcc5a1SJed Brown ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr); 1989fc4dec0aSBarry Smith } else { 1990fc4dec0aSBarry Smith B = *matout; 19916ffab4bbSHong Zhang ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 19922205254eSKarl Rupp for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1993fc4dec0aSBarry Smith } 1994b7c46309SBarry Smith 1995b7c46309SBarry Smith /* copy over the A part */ 1996da668accSHong Zhang array = Aloc->a; 1997d0f46423SBarry Smith row = A->rmap->rstart; 1998da668accSHong Zhang for (i=0; i<ma; i++) { 1999da668accSHong Zhang ncol = ai[i+1]-ai[i]; 2000da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 20012205254eSKarl Rupp row++; 20022205254eSKarl Rupp array += ncol; aj += ncol; 2003b7c46309SBarry Smith } 2004b7c46309SBarry Smith aj = Aloc->j; 2005da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 2006b7c46309SBarry Smith 2007b7c46309SBarry Smith /* copy over the B part */ 20081795a4d1SJed Brown ierr = PetscCalloc1(bi[mb],&cols);CHKERRQ(ierr); 2009da668accSHong Zhang array = Bloc->a; 2010d0f46423SBarry Smith row = A->rmap->rstart; 20112205254eSKarl Rupp for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]]; 201261a2fbbaSHong Zhang cols_tmp = cols; 2013da668accSHong Zhang for (i=0; i<mb; i++) { 2014da668accSHong Zhang ncol = bi[i+1]-bi[i]; 201561a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 20162205254eSKarl Rupp row++; 20172205254eSKarl Rupp array += ncol; cols_tmp += ncol; 2018b7c46309SBarry Smith } 2019fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 2020fc73b1b3SBarry Smith 20216d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 20226d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2023815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 20240de55854SLois Curfman McInnes *matout = B; 20250de55854SLois Curfman McInnes } else { 2026eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 20270de55854SLois Curfman McInnes } 20283a40ed3dSBarry Smith PetscFunctionReturn(0); 2029b7c46309SBarry Smith } 2030b7c46309SBarry Smith 20314a2ae208SSatish Balay #undef __FUNCT__ 20324a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 2033dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 2034a008b906SSatish Balay { 20354b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 20364b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 2037dfbe8321SBarry Smith PetscErrorCode ierr; 2038b1d57f15SBarry Smith PetscInt s1,s2,s3; 2039a008b906SSatish Balay 20403a40ed3dSBarry Smith PetscFunctionBegin; 20414b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 20424b967eb1SSatish Balay if (rr) { 2043e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 2044e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 20454b967eb1SSatish Balay /* Overlap communication with computation. */ 2046ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2047a008b906SSatish Balay } 20484b967eb1SSatish Balay if (ll) { 2049e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 2050e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 2051f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 20524b967eb1SSatish Balay } 20534b967eb1SSatish Balay /* scale the diagonal block */ 2054f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 20554b967eb1SSatish Balay 20564b967eb1SSatish Balay if (rr) { 20574b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 2058ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2059f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 20604b967eb1SSatish Balay } 20613a40ed3dSBarry Smith PetscFunctionReturn(0); 2062a008b906SSatish Balay } 2063a008b906SSatish Balay 20644a2ae208SSatish Balay #undef __FUNCT__ 20654a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 2066dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 2067bb5a7306SBarry Smith { 2068bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2069dfbe8321SBarry Smith PetscErrorCode ierr; 20703a40ed3dSBarry Smith 20713a40ed3dSBarry Smith PetscFunctionBegin; 2072bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 20733a40ed3dSBarry Smith PetscFunctionReturn(0); 2074bb5a7306SBarry Smith } 2075bb5a7306SBarry Smith 20764a2ae208SSatish Balay #undef __FUNCT__ 20774a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2078ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2079d4bb536fSBarry Smith { 2080d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2081d4bb536fSBarry Smith Mat a,b,c,d; 2082ace3abfcSBarry Smith PetscBool flg; 2083dfbe8321SBarry Smith PetscErrorCode ierr; 2084d4bb536fSBarry Smith 20853a40ed3dSBarry Smith PetscFunctionBegin; 2086d4bb536fSBarry Smith a = matA->A; b = matA->B; 2087d4bb536fSBarry Smith c = matB->A; d = matB->B; 2088d4bb536fSBarry Smith 2089d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2090abc0a331SBarry Smith if (flg) { 2091d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2092d4bb536fSBarry Smith } 2093ce94432eSBarry Smith ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 20943a40ed3dSBarry Smith PetscFunctionReturn(0); 2095d4bb536fSBarry Smith } 2096d4bb536fSBarry Smith 20974a2ae208SSatish Balay #undef __FUNCT__ 20984a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2099dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2100cb5b572fSBarry Smith { 2101dfbe8321SBarry Smith PetscErrorCode ierr; 2102cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2103cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ*)B->data; 2104cb5b572fSBarry Smith 2105cb5b572fSBarry Smith PetscFunctionBegin; 210633f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 210733f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2108cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2109cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2110cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2111cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2112cb5b572fSBarry Smith then copying the submatrices */ 2113cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2114cb5b572fSBarry Smith } else { 2115cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2116cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2117cb5b572fSBarry Smith } 2118cb5b572fSBarry Smith PetscFunctionReturn(0); 2119cb5b572fSBarry Smith } 2120cb5b572fSBarry Smith 21214a2ae208SSatish Balay #undef __FUNCT__ 21224994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ" 21234994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A) 2124273d9f13SBarry Smith { 2125dfbe8321SBarry Smith PetscErrorCode ierr; 2126273d9f13SBarry Smith 2127273d9f13SBarry Smith PetscFunctionBegin; 2128273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2129273d9f13SBarry Smith PetscFunctionReturn(0); 2130273d9f13SBarry Smith } 2131273d9f13SBarry Smith 2132ac90fabeSBarry Smith #undef __FUNCT__ 213395b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ" 213495b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */ 213595b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz) 213695b7e79eSJed Brown { 213795b7e79eSJed Brown PetscInt i,m=Y->rmap->N; 213895b7e79eSJed Brown Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 213995b7e79eSJed Brown Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 214095b7e79eSJed Brown const PetscInt *xi = x->i,*yi = y->i; 214195b7e79eSJed Brown 214295b7e79eSJed Brown PetscFunctionBegin; 214395b7e79eSJed Brown /* Set the number of nonzeros in the new matrix */ 214495b7e79eSJed Brown for (i=0; i<m; i++) { 214595b7e79eSJed Brown PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i]; 214695b7e79eSJed Brown const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i]; 214795b7e79eSJed Brown nnz[i] = 0; 214895b7e79eSJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 214995b7e79eSJed Brown for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */ 215095b7e79eSJed Brown if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++; /* Skip duplicate */ 215195b7e79eSJed Brown nnz[i]++; 215295b7e79eSJed Brown } 215395b7e79eSJed Brown for (; k<nzy; k++) nnz[i]++; 215495b7e79eSJed Brown } 215595b7e79eSJed Brown PetscFunctionReturn(0); 215695b7e79eSJed Brown } 215795b7e79eSJed Brown 215895b7e79eSJed Brown #undef __FUNCT__ 2159ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2160f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2161ac90fabeSBarry Smith { 2162dfbe8321SBarry Smith PetscErrorCode ierr; 2163b1d57f15SBarry Smith PetscInt i; 2164ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data; 21654ce68768SBarry Smith PetscBLASInt bnz,one=1; 2166ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2167ac90fabeSBarry Smith 2168ac90fabeSBarry Smith PetscFunctionBegin; 2169ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2170f4df32b1SMatthew Knepley PetscScalar alpha = a; 2171ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->A->data; 2172c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2173ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->A->data; 21748b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2175ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->B->data; 2176ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->B->data; 2177c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 21788b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2179a3fa217bSJose E. Roman ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); 2180a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2181f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2182c537a176SHong Zhang 2183c537a176SHong Zhang x = (Mat_SeqAIJ*)xx->B->data; 2184a30b2313SHong Zhang y = (Mat_SeqAIJ*)yy->B->data; 2185a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2186a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 21876bf464f9SBarry Smith ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr); 2188c537a176SHong Zhang } 2189a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2190d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2191a30b2313SHong Zhang y->XtoY = xx->B; 2192407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2193c537a176SHong Zhang } 2194f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2195a3fa217bSJose E. Roman ierr = PetscObjectStateIncrease((PetscObject)Y);CHKERRQ(ierr); 2196ac90fabeSBarry Smith } else { 21979f5f6813SShri Abhyankar Mat B; 21989f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 2199785e854fSJed Brown ierr = PetscMalloc1(yy->A->rmap->N,&nnz_d);CHKERRQ(ierr); 2200785e854fSJed Brown ierr = PetscMalloc1(yy->B->rmap->N,&nnz_o);CHKERRQ(ierr); 2201ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 2202bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 22039f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 220433d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,Y,Y);CHKERRQ(ierr); 22059f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 22069f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 220795b7e79eSJed Brown ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr); 2208ecd8bba6SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr); 22099f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 2210a2ea699eSBarry Smith ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr); 22119f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 22129f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2213ac90fabeSBarry Smith } 2214ac90fabeSBarry Smith PetscFunctionReturn(0); 2215ac90fabeSBarry Smith } 2216ac90fabeSBarry Smith 22177087cfbeSBarry Smith extern PetscErrorCode MatConjugate_SeqAIJ(Mat); 2218354c94deSBarry Smith 2219354c94deSBarry Smith #undef __FUNCT__ 2220354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 22217087cfbeSBarry Smith PetscErrorCode MatConjugate_MPIAIJ(Mat mat) 2222354c94deSBarry Smith { 2223354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2224354c94deSBarry Smith PetscErrorCode ierr; 2225354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2226354c94deSBarry Smith 2227354c94deSBarry Smith PetscFunctionBegin; 2228354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2229354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2230354c94deSBarry Smith #else 2231354c94deSBarry Smith PetscFunctionBegin; 2232354c94deSBarry Smith #endif 2233354c94deSBarry Smith PetscFunctionReturn(0); 2234354c94deSBarry Smith } 2235354c94deSBarry Smith 223699cafbc1SBarry Smith #undef __FUNCT__ 223799cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 223899cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 223999cafbc1SBarry Smith { 224099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 224199cafbc1SBarry Smith PetscErrorCode ierr; 224299cafbc1SBarry Smith 224399cafbc1SBarry Smith PetscFunctionBegin; 224499cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 224599cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 224699cafbc1SBarry Smith PetscFunctionReturn(0); 224799cafbc1SBarry Smith } 224899cafbc1SBarry Smith 224999cafbc1SBarry Smith #undef __FUNCT__ 225099cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 225199cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 225299cafbc1SBarry Smith { 225399cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 225499cafbc1SBarry Smith PetscErrorCode ierr; 225599cafbc1SBarry Smith 225699cafbc1SBarry Smith PetscFunctionBegin; 225799cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 225899cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 225999cafbc1SBarry Smith PetscFunctionReturn(0); 226099cafbc1SBarry Smith } 226199cafbc1SBarry Smith 2262519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 2263103bf8bdSMatthew Knepley 2264103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2265a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2266a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2267a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2268103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2269a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2270d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2271103bf8bdSMatthew Knepley 2272103bf8bdSMatthew Knepley #undef __FUNCT__ 2273103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2274103bf8bdSMatthew Knepley /* 2275103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2276103bf8bdSMatthew Knepley */ 22770481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2278103bf8bdSMatthew Knepley { 2279a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2280a2c909beSMatthew Knepley 2281a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2282a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2283a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2284a2c909beSMatthew Knepley 2285ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2286776b82aeSLisandro Dalcin PetscContainer c; 2287103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2288103bf8bdSMatthew Knepley PetscErrorCode ierr; 2289103bf8bdSMatthew Knepley 2290103bf8bdSMatthew Knepley PetscFunctionBegin; 2291e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2292103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2293103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2294f23aa3ddSBarry Smith if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2295103bf8bdSMatthew Knepley 2296103bf8bdSMatthew Knepley process_group_type pg; 2297a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2298a2c909beSMatthew Knepley lgraph_type *lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2299a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2300a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2301a2c909beSMatthew Knepley 2302103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2303a2c909beSMatthew Knepley ilu_permuted(level_graph); 2304103bf8bdSMatthew Knepley 2305103bf8bdSMatthew Knepley /* put together the new matrix */ 2306ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A), fact);CHKERRQ(ierr); 2307103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2308103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2309719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 231033d57670SJed Brown ierr = MatSetBlockSizesFromMats(fact,A,A);CHKERRQ(ierr); 2311719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2312719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2313719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2314103bf8bdSMatthew Knepley 2315ce94432eSBarry Smith ierr = PetscContainerCreate(PetscObjectComm((PetscObject)A), &c); 2316776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2317719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2318bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&c); 2319103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2320103bf8bdSMatthew Knepley } 2321103bf8bdSMatthew Knepley 2322103bf8bdSMatthew Knepley #undef __FUNCT__ 2323103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 23240481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2325103bf8bdSMatthew Knepley { 2326103bf8bdSMatthew Knepley PetscFunctionBegin; 2327103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2328103bf8bdSMatthew Knepley } 2329103bf8bdSMatthew Knepley 2330103bf8bdSMatthew Knepley #undef __FUNCT__ 2331103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2332103bf8bdSMatthew Knepley /* 2333103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2334103bf8bdSMatthew Knepley */ 2335103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2336103bf8bdSMatthew Knepley { 2337a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2338a2c909beSMatthew Knepley 2339a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2340a2c909beSMatthew Knepley lgraph_type *lgraph_p; 2341776b82aeSLisandro Dalcin PetscContainer c; 2342103bf8bdSMatthew Knepley PetscErrorCode ierr; 2343103bf8bdSMatthew Knepley 2344103bf8bdSMatthew Knepley PetscFunctionBegin; 2345103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);CHKERRQ(ierr); 2346776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void**) &lgraph_p);CHKERRQ(ierr); 2347103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2348a2c909beSMatthew Knepley 2349a2c909beSMatthew Knepley PetscScalar *array_x; 2350a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2351a2c909beSMatthew Knepley PetscInt sx; 2352a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2353a2c909beSMatthew Knepley 2354a2c909beSMatthew Knepley PetscScalar *array_b; 2355a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2356a2c909beSMatthew Knepley PetscInt sb; 2357a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2358a2c909beSMatthew Knepley 2359a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2360a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2361a2c909beSMatthew Knepley 2362a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 23632205254eSKarl Rupp array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]); 23642205254eSKarl Rupp array_ref_type ref_x(array_x, boost::extents[num_vertices(graph)]); 2365a2c909beSMatthew Knepley 2366a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2367a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 23682205254eSKarl Rupp gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)); 23692205254eSKarl Rupp gvector_type vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2370a2c909beSMatthew Knepley 2371a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2372103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2373103bf8bdSMatthew Knepley } 2374103bf8bdSMatthew Knepley #endif 2375103bf8bdSMatthew Knepley 237669db28dcSHong Zhang #undef __FUNCT__ 23775cc03489SHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 23785cc03489SHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 237969db28dcSHong Zhang { 238069db28dcSHong Zhang PetscErrorCode ierr; 23815cc03489SHong Zhang Mat_Redundant *redund; 238269db28dcSHong Zhang PetscInt i; 23835cc03489SHong Zhang PetscMPIInt size; 238469db28dcSHong Zhang 238569db28dcSHong Zhang PetscFunctionBegin; 23865cc03489SHong Zhang ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr); 23875cc03489SHong Zhang if (size == 1) { 23885cc03489SHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 23895cc03489SHong Zhang redund = a->redundant; 23905cc03489SHong Zhang } else { 23915cc03489SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 23925cc03489SHong Zhang redund = a->redundant; 23935cc03489SHong Zhang } 23945cc03489SHong Zhang if (redund){ 2395c79c5527SHong Zhang if (redund->matseq) { /* via MatGetSubMatrices() */ 23964388c78fSHong Zhang ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr); 23974388c78fSHong Zhang ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr); 23984388c78fSHong Zhang ierr = MatDestroy(&redund->matseq[0]);CHKERRQ(ierr); 23994388c78fSHong Zhang ierr = PetscFree(redund->matseq);CHKERRQ(ierr); 24004388c78fSHong Zhang } else { 24011d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 240269db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 240369db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 240469db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++) { 240569db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 240669db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 240769db28dcSHong Zhang } 24081d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 2409c79c5527SHong Zhang } 24100b291e46SHong Zhang 24110b291e46SHong Zhang if (redund->psubcomm) { 24120b291e46SHong Zhang ierr = PetscSubcommDestroy(&redund->psubcomm);CHKERRQ(ierr); 24130b291e46SHong Zhang } 24145cc03489SHong Zhang ierr = redund->Destroy(A);CHKERRQ(ierr); 241569db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 2416bf0cc555SLisandro Dalcin } 241769db28dcSHong Zhang PetscFunctionReturn(0); 241869db28dcSHong Zhang } 241969db28dcSHong Zhang 242069db28dcSHong Zhang #undef __FUNCT__ 242122559b1cSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ_interlaced" 24227cb6ea77SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ_interlaced(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 2423b4617e5dSHong Zhang { 2424b4617e5dSHong Zhang PetscMPIInt rank,size; 24257cb6ea77SHong Zhang MPI_Comm comm; 2426b4617e5dSHong Zhang PetscErrorCode ierr; 242734d19554SHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0,M=mat->rmap->N,N=mat->cmap->N; 24285cc03489SHong Zhang PetscMPIInt *send_rank= NULL,*recv_rank=NULL,subrank,subsize; 2429b4617e5dSHong Zhang PetscInt *rowrange = mat->rmap->range; 2430b4617e5dSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2431b4617e5dSHong Zhang Mat A = aij->A,B=aij->B,C=*matredundant; 2432b4617e5dSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 2433b4617e5dSHong Zhang PetscScalar *sbuf_a; 2434b4617e5dSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2435b4617e5dSHong Zhang PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 243634d19554SHong Zhang PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray; 2437b4617e5dSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2438b4617e5dSHong Zhang MatScalar *aworkA,*aworkB; 2439b4617e5dSHong Zhang PetscScalar *vals; 2440b4617e5dSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 2441b4617e5dSHong Zhang MPI_Request *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL; 2442b4617e5dSHong Zhang MPI_Request *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL; 2443b4617e5dSHong Zhang MPI_Status recv_status,*send_status; 2444b4617e5dSHong Zhang PetscInt *sbuf_nz=NULL,*rbuf_nz=NULL,count; 2445b4617e5dSHong Zhang PetscInt **rbuf_j=NULL; 2446b4617e5dSHong Zhang PetscScalar **rbuf_a=NULL; 2447b4617e5dSHong Zhang Mat_Redundant *redund =NULL; 2448b4617e5dSHong Zhang 2449b4617e5dSHong Zhang PetscFunctionBegin; 2450b4617e5dSHong Zhang ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 2451b4617e5dSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 2452b4617e5dSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 24535cc03489SHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 24545cc03489SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2455d3b23db5SHong Zhang 2456b4617e5dSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 2457b4617e5dSHong Zhang if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 24585cc03489SHong Zhang if (subsize == 1) { 24595cc03489SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 24605cc03489SHong Zhang redund = c->redundant; 24615cc03489SHong Zhang } else { 24625cc03489SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 24635cc03489SHong Zhang redund = c->redundant; 24645cc03489SHong Zhang } 2465b4617e5dSHong Zhang if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 2466b4617e5dSHong Zhang 2467b4617e5dSHong Zhang nsends = redund->nsends; 2468b4617e5dSHong Zhang nrecvs = redund->nrecvs; 2469b4617e5dSHong Zhang send_rank = redund->send_rank; 2470b4617e5dSHong Zhang recv_rank = redund->recv_rank; 2471b4617e5dSHong Zhang sbuf_nz = redund->sbuf_nz; 2472b4617e5dSHong Zhang rbuf_nz = redund->rbuf_nz; 2473b4617e5dSHong Zhang sbuf_j = redund->sbuf_j; 2474b4617e5dSHong Zhang sbuf_a = redund->sbuf_a; 2475b4617e5dSHong Zhang rbuf_j = redund->rbuf_j; 2476b4617e5dSHong Zhang rbuf_a = redund->rbuf_a; 2477b4617e5dSHong Zhang } 2478b4617e5dSHong Zhang 2479b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2480b4617e5dSHong Zhang PetscInt nleftover,np_subcomm; 2481b4617e5dSHong Zhang 2482b4617e5dSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 2483dcca6d9dSJed Brown ierr = PetscMalloc2(size,&send_rank,size,&recv_rank);CHKERRQ(ierr); 2484b4617e5dSHong Zhang 2485b4617e5dSHong Zhang np_subcomm = size/nsubcomm; 2486b4617e5dSHong Zhang nleftover = size - nsubcomm*np_subcomm; 2487b4617e5dSHong Zhang 248822559b1cSHong Zhang /* block of codes below is specific for INTERLACED */ 248922559b1cSHong Zhang /* ------------------------------------------------*/ 2490b4617e5dSHong Zhang nsends = 0; nrecvs = 0; 2491b4617e5dSHong Zhang for (i=0; i<size; i++) { 2492b4617e5dSHong Zhang if (subrank == i/nsubcomm && i != rank) { /* my_subrank == other's subrank */ 249322559b1cSHong Zhang send_rank[nsends++] = i; 2494b4617e5dSHong Zhang recv_rank[nrecvs++] = i; 2495b4617e5dSHong Zhang } 2496b4617e5dSHong Zhang } 2497b4617e5dSHong Zhang if (rank >= size - nleftover) { /* this proc is a leftover processor */ 2498b4617e5dSHong Zhang i = size-nleftover-1; 2499b4617e5dSHong Zhang j = 0; 2500b4617e5dSHong Zhang while (j < nsubcomm - nleftover) { 2501b4617e5dSHong Zhang send_rank[nsends++] = i; 2502b4617e5dSHong Zhang i--; j++; 2503b4617e5dSHong Zhang } 2504b4617e5dSHong Zhang } 2505b4617e5dSHong Zhang 2506b4617e5dSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */ 2507b4617e5dSHong Zhang for (i=0; i<nleftover; i++) { 2508b4617e5dSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 2509b4617e5dSHong Zhang } 2510b4617e5dSHong Zhang } 251122559b1cSHong Zhang /*----------------------------------------------*/ 2512b4617e5dSHong Zhang 2513b4617e5dSHong Zhang /* allocate sbuf_j, sbuf_a */ 2514b4617e5dSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 2515785e854fSJed Brown ierr = PetscMalloc1(i,&sbuf_j);CHKERRQ(ierr); 2516785e854fSJed Brown ierr = PetscMalloc1((nzlocal+1),&sbuf_a);CHKERRQ(ierr); 2517e37c6257SHong Zhang /* 2518e37c6257SHong Zhang ierr = PetscSynchronizedPrintf(comm,"[%d] nsends %d, nrecvs %d\n",rank,nsends,nrecvs);CHKERRQ(ierr); 25190ec8b6e3SBarry Smith ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr); 2520e37c6257SHong Zhang */ 2521b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2522b4617e5dSHong Zhang 2523b4617e5dSHong Zhang /* copy mat's local entries into the buffers */ 2524b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2525b4617e5dSHong Zhang rownz_max = 0; 2526b4617e5dSHong Zhang rptr = sbuf_j; 2527b4617e5dSHong Zhang cols = sbuf_j + rend-rstart + 1; 2528b4617e5dSHong Zhang vals = sbuf_a; 2529b4617e5dSHong Zhang rptr[0] = 0; 2530b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2531b4617e5dSHong Zhang row = i + rstart; 2532b4617e5dSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 2533b4617e5dSHong Zhang ncols = nzA + nzB; 2534b4617e5dSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 2535b4617e5dSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 2536b4617e5dSHong Zhang /* load the column indices for this row into cols */ 2537b4617e5dSHong Zhang lwrite = 0; 2538b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2539b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) { 2540b4617e5dSHong Zhang vals[lwrite] = aworkB[l]; 2541b4617e5dSHong Zhang cols[lwrite++] = ctmp; 2542b4617e5dSHong Zhang } 2543b4617e5dSHong Zhang } 2544b4617e5dSHong Zhang for (l=0; l<nzA; l++) { 2545b4617e5dSHong Zhang vals[lwrite] = aworkA[l]; 2546b4617e5dSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 2547b4617e5dSHong Zhang } 2548b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2549b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) { 2550b4617e5dSHong Zhang vals[lwrite] = aworkB[l]; 2551b4617e5dSHong Zhang cols[lwrite++] = ctmp; 2552b4617e5dSHong Zhang } 2553b4617e5dSHong Zhang } 2554b4617e5dSHong Zhang vals += ncols; 2555b4617e5dSHong Zhang cols += ncols; 2556b4617e5dSHong Zhang rptr[i+1] = rptr[i] + ncols; 2557b4617e5dSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 2558b4617e5dSHong Zhang } 2559b4617e5dSHong Zhang 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); 2560b4617e5dSHong Zhang } else { /* only copy matrix values into sbuf_a */ 2561b4617e5dSHong Zhang rptr = sbuf_j; 2562b4617e5dSHong Zhang vals = sbuf_a; 2563b4617e5dSHong Zhang rptr[0] = 0; 2564b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2565b4617e5dSHong Zhang row = i + rstart; 2566b4617e5dSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 2567b4617e5dSHong Zhang ncols = nzA + nzB; 2568b4617e5dSHong Zhang cworkB = b->j + b->i[i]; 2569b4617e5dSHong Zhang aworkA = a->a + a->i[i]; 2570b4617e5dSHong Zhang aworkB = b->a + b->i[i]; 2571b4617e5dSHong Zhang lwrite = 0; 2572b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2573b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 2574b4617e5dSHong Zhang } 2575b4617e5dSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 2576b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2577b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 2578b4617e5dSHong Zhang } 2579b4617e5dSHong Zhang vals += ncols; 2580b4617e5dSHong Zhang rptr[i+1] = rptr[i] + ncols; 2581b4617e5dSHong Zhang } 2582b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2583b4617e5dSHong Zhang 2584b4617e5dSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 2585b4617e5dSHong Zhang /*--------------------------------------------------*/ 2586b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2587dcca6d9dSJed Brown ierr = PetscMalloc2(3*(nsends + nrecvs)+1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr); 2588b4617e5dSHong Zhang 2589b4617e5dSHong Zhang s_waits2 = s_waits3 + nsends; 2590b4617e5dSHong Zhang s_waits1 = s_waits2 + nsends; 2591b4617e5dSHong Zhang r_waits1 = s_waits1 + nsends; 2592b4617e5dSHong Zhang r_waits2 = r_waits1 + nrecvs; 2593b4617e5dSHong Zhang r_waits3 = r_waits2 + nrecvs; 2594b4617e5dSHong Zhang } else { 2595dcca6d9dSJed Brown ierr = PetscMalloc2(nsends + nrecvs +1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr); 2596b4617e5dSHong Zhang 2597b4617e5dSHong Zhang r_waits3 = s_waits3 + nsends; 2598b4617e5dSHong Zhang } 2599b4617e5dSHong Zhang 2600b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 2601b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2602b4617e5dSHong Zhang /* get new tags to keep the communication clean */ 2603b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 2604b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 2605dcca6d9dSJed Brown ierr = PetscMalloc4(nsends,&sbuf_nz,nrecvs,&rbuf_nz,nrecvs,&rbuf_j,nrecvs,&rbuf_a);CHKERRQ(ierr); 2606b4617e5dSHong Zhang 2607b4617e5dSHong Zhang /* post receives of other's nzlocal */ 2608b4617e5dSHong Zhang for (i=0; i<nrecvs; i++) { 2609b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 2610b4617e5dSHong Zhang } 2611b4617e5dSHong Zhang /* send nzlocal to others */ 2612b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2613b4617e5dSHong Zhang sbuf_nz[i] = nzlocal; 2614b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 2615b4617e5dSHong Zhang } 2616b4617e5dSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 2617b4617e5dSHong Zhang count = nrecvs; 2618b4617e5dSHong Zhang while (count) { 2619b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 2620b4617e5dSHong Zhang 2621b4617e5dSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 2622b4617e5dSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 2623785e854fSJed Brown ierr = PetscMalloc1((rbuf_nz[imdex]+1),&rbuf_a[imdex]);CHKERRQ(ierr); 2624b4617e5dSHong Zhang 2625b4617e5dSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 2626b4617e5dSHong Zhang 2627b4617e5dSHong Zhang rbuf_nz[imdex] += i + 2; 2628b4617e5dSHong Zhang 2629785e854fSJed Brown ierr = PetscMalloc1(rbuf_nz[imdex],&rbuf_j[imdex]);CHKERRQ(ierr); 2630b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 2631b4617e5dSHong Zhang count--; 2632b4617e5dSHong Zhang } 2633b4617e5dSHong Zhang /* wait on sends of nzlocal */ 2634b4617e5dSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 2635b4617e5dSHong Zhang /* send mat->i,j to others, and recv from other's */ 2636b4617e5dSHong Zhang /*------------------------------------------------*/ 2637b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2638b4617e5dSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 2639b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 2640b4617e5dSHong Zhang } 2641b4617e5dSHong Zhang /* wait on receives of mat->i,j */ 2642b4617e5dSHong Zhang /*------------------------------*/ 2643b4617e5dSHong Zhang count = nrecvs; 2644b4617e5dSHong Zhang while (count) { 2645b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2646b4617e5dSHong Zhang 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); 2647b4617e5dSHong Zhang count--; 2648b4617e5dSHong Zhang } 2649b4617e5dSHong Zhang /* wait on sends of mat->i,j */ 2650b4617e5dSHong Zhang /*---------------------------*/ 2651b4617e5dSHong Zhang if (nsends) { 2652b4617e5dSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 2653b4617e5dSHong Zhang } 2654b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2655b4617e5dSHong Zhang 2656b4617e5dSHong Zhang /* post receives, send and receive mat->a */ 2657b4617e5dSHong Zhang /*----------------------------------------*/ 2658b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2659b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 2660b4617e5dSHong Zhang } 2661b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2662b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 2663b4617e5dSHong Zhang } 2664b4617e5dSHong Zhang count = nrecvs; 2665b4617e5dSHong Zhang while (count) { 2666b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2667b4617e5dSHong Zhang 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); 2668b4617e5dSHong Zhang count--; 2669b4617e5dSHong Zhang } 2670b4617e5dSHong Zhang if (nsends) { 2671b4617e5dSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 2672b4617e5dSHong Zhang } 2673b4617e5dSHong Zhang 2674b4617e5dSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 2675b4617e5dSHong Zhang 2676b4617e5dSHong Zhang /* create redundant matrix */ 2677b4617e5dSHong Zhang /*-------------------------*/ 2678b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 267919171117SHong Zhang const PetscInt *range; 268019171117SHong Zhang PetscInt rstart_sub,rend_sub,mloc_sub; 268119171117SHong Zhang 2682b4617e5dSHong Zhang /* compute rownz_max for preallocation */ 2683b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2684b4617e5dSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 2685b4617e5dSHong Zhang rptr = rbuf_j[imdex]; 2686b4617e5dSHong Zhang for (i=0; i<j; i++) { 2687b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2688b4617e5dSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 2689b4617e5dSHong Zhang } 2690b4617e5dSHong Zhang } 2691b4617e5dSHong Zhang 2692b4617e5dSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 269319171117SHong Zhang 269419171117SHong Zhang /* get local size of redundant matrix 269519171117SHong Zhang - mloc_sub is chosen for PETSC_SUBCOMM_INTERLACED, works for other types, but may not efficient! */ 269619171117SHong Zhang ierr = MatGetOwnershipRanges(mat,&range);CHKERRQ(ierr); 269719171117SHong Zhang rstart_sub = range[nsubcomm*subrank]; 269819171117SHong Zhang if (subrank+1 < subsize) { /* not the last proc in subcomm */ 269919171117SHong Zhang rend_sub = range[nsubcomm*(subrank+1)]; 270019171117SHong Zhang } else { 270119171117SHong Zhang rend_sub = mat->rmap->N; 270219171117SHong Zhang } 270319171117SHong Zhang mloc_sub = rend_sub - rstart_sub; 270419171117SHong Zhang 270534d19554SHong Zhang if (M == N) { 2706b4617e5dSHong Zhang ierr = MatSetSizes(C,mloc_sub,mloc_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 270734d19554SHong Zhang } else { /* non-square matrix */ 270834d19554SHong Zhang ierr = MatSetSizes(C,mloc_sub,PETSC_DECIDE,PETSC_DECIDE,mat->cmap->N);CHKERRQ(ierr); 270934d19554SHong Zhang } 271033d57670SJed Brown ierr = MatSetBlockSizesFromMats(C,mat,mat);CHKERRQ(ierr); 2711b4617e5dSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 2712b4617e5dSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,NULL);CHKERRQ(ierr); 2713b4617e5dSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);CHKERRQ(ierr); 2714b4617e5dSHong Zhang } else { 2715b4617e5dSHong Zhang C = *matredundant; 2716b4617e5dSHong Zhang } 2717b4617e5dSHong Zhang 2718b4617e5dSHong Zhang /* insert local matrix entries */ 2719b4617e5dSHong Zhang rptr = sbuf_j; 2720b4617e5dSHong Zhang cols = sbuf_j + rend-rstart + 1; 2721b4617e5dSHong Zhang vals = sbuf_a; 2722b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2723b4617e5dSHong Zhang row = i + rstart; 2724b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2725b4617e5dSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 2726b4617e5dSHong Zhang vals += ncols; 2727b4617e5dSHong Zhang cols += ncols; 2728b4617e5dSHong Zhang } 2729b4617e5dSHong Zhang /* insert received matrix entries */ 2730b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2731b4617e5dSHong Zhang rstart = rowrange[recv_rank[imdex]]; 2732b4617e5dSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 2733e37c6257SHong Zhang /* printf("[%d] insert rows %d - %d\n",rank,rstart,rend-1); */ 2734b4617e5dSHong Zhang rptr = rbuf_j[imdex]; 2735b4617e5dSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 2736b4617e5dSHong Zhang vals = rbuf_a[imdex]; 2737b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2738b4617e5dSHong Zhang row = i + rstart; 2739b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2740b4617e5dSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 2741b4617e5dSHong Zhang vals += ncols; 2742b4617e5dSHong Zhang cols += ncols; 2743b4617e5dSHong Zhang } 2744b4617e5dSHong Zhang } 2745b4617e5dSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2746b4617e5dSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2747b4617e5dSHong Zhang 2748b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2749b4617e5dSHong Zhang *matredundant = C; 27505cc03489SHong Zhang 2751b4617e5dSHong Zhang /* create a supporting struct and attach it to C for reuse */ 2752b00a9115SJed Brown ierr = PetscNewLog(C,&redund);CHKERRQ(ierr); 27535cc03489SHong Zhang if (subsize == 1) { 27545cc03489SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 27555cc03489SHong Zhang c->redundant = redund; 27565cc03489SHong Zhang } else { 27575cc03489SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 27585cc03489SHong Zhang c->redundant = redund; 27595cc03489SHong Zhang } 2760b4617e5dSHong Zhang 2761b4617e5dSHong Zhang redund->nzlocal = nzlocal; 2762b4617e5dSHong Zhang redund->nsends = nsends; 2763b4617e5dSHong Zhang redund->nrecvs = nrecvs; 2764b4617e5dSHong Zhang redund->send_rank = send_rank; 2765b4617e5dSHong Zhang redund->recv_rank = recv_rank; 2766b4617e5dSHong Zhang redund->sbuf_nz = sbuf_nz; 2767b4617e5dSHong Zhang redund->rbuf_nz = rbuf_nz; 2768b4617e5dSHong Zhang redund->sbuf_j = sbuf_j; 2769b4617e5dSHong Zhang redund->sbuf_a = sbuf_a; 2770b4617e5dSHong Zhang redund->rbuf_j = rbuf_j; 2771b4617e5dSHong Zhang redund->rbuf_a = rbuf_a; 27720b291e46SHong Zhang redund->psubcomm = NULL; 2773b4617e5dSHong Zhang 2774b4617e5dSHong Zhang redund->Destroy = C->ops->destroy; 2775b4617e5dSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 2776b4617e5dSHong Zhang } 2777b4617e5dSHong Zhang PetscFunctionReturn(0); 2778b4617e5dSHong Zhang } 2779b4617e5dSHong Zhang 2780b4617e5dSHong Zhang #undef __FUNCT__ 278169db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 2782b2bf6370SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 278369db28dcSHong Zhang { 2784f38d543fSHong Zhang PetscErrorCode ierr; 2785c79c5527SHong Zhang MPI_Comm comm; 2786c79c5527SHong Zhang PetscMPIInt size,subsize; 2787c79c5527SHong Zhang PetscInt mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N; 2788c79c5527SHong Zhang Mat_Redundant *redund=NULL; 27891f2d8ef4SHong Zhang PetscSubcomm psubcomm=NULL; 2790473f7991SHong Zhang MPI_Comm subcomm_in=subcomm; 27911f2d8ef4SHong Zhang Mat *matseq; 27921f2d8ef4SHong Zhang IS isrow,iscol; 279369db28dcSHong Zhang 279469db28dcSHong Zhang PetscFunctionBegin; 27951f2d8ef4SHong Zhang if (subcomm_in == MPI_COMM_NULL) { /* user does not provide subcomm */ 2796c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 27971f2d8ef4SHong Zhang /* create psubcomm, then get subcomm */ 2798ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 279969db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 28007cb6ea77SHong Zhang if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size); 28017cb6ea77SHong Zhang 2802d3b23db5SHong Zhang ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 2803d3b23db5SHong Zhang ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 2804c79c5527SHong Zhang ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 280519171117SHong Zhang ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 2806c79c5527SHong Zhang subcomm = psubcomm->comm; 28077cb6ea77SHong Zhang } else { /* retrieve psubcomm and subcomm */ 2808c79c5527SHong Zhang ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 2809c79c5527SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2810c79c5527SHong Zhang if (subsize == 1) { 2811c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 28127cb6ea77SHong Zhang redund = c->redundant; 2813c79c5527SHong Zhang } else { 2814c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 28157cb6ea77SHong Zhang redund = c->redundant; 2816c79c5527SHong Zhang } 28177cb6ea77SHong Zhang psubcomm = redund->psubcomm; 2818fd7037dcSHong Zhang } 28191f2d8ef4SHong Zhang if (psubcomm->type == PETSC_SUBCOMM_INTERLACED) { 28207cb6ea77SHong Zhang ierr = MatGetRedundantMatrix_MPIAIJ_interlaced(mat,nsubcomm,subcomm,reuse,matredundant);CHKERRQ(ierr); 28211f2d8ef4SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { /* psubcomm is created in this routine, free it in MatDestroy_MatRedundant() */ 28221f2d8ef4SHong Zhang ierr = MPI_Comm_size(psubcomm->comm,&subsize);CHKERRQ(ierr); 28231f2d8ef4SHong Zhang if (subsize == 1) { 28241f2d8ef4SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 28251f2d8ef4SHong Zhang c->redundant->psubcomm = psubcomm; 28261f2d8ef4SHong Zhang } else { 28271f2d8ef4SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 28281f2d8ef4SHong Zhang c->redundant->psubcomm = psubcomm ; 28291f2d8ef4SHong Zhang } 28301f2d8ef4SHong Zhang } 28311f2d8ef4SHong Zhang PetscFunctionReturn(0); 2832c79c5527SHong Zhang } 2833c79c5527SHong Zhang } 2834e37c6257SHong Zhang 28351f2d8ef4SHong Zhang /* use MPI subcomm via MatGetSubMatrices(); use subcomm_in or psubcomm->comm (psubcomm->type != INTERLACED) */ 28367cb6ea77SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2837c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2838c79c5527SHong Zhang /* create a local sequential matrix matseq[0] */ 2839c79c5527SHong Zhang mloc_sub = PETSC_DECIDE; 2840c79c5527SHong Zhang ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 2841c79c5527SHong Zhang ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr); 2842c79c5527SHong Zhang rstart = rend - mloc_sub; 2843c79c5527SHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 2844c79c5527SHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 2845c79c5527SHong Zhang } else { /* reuse == MAT_REUSE_MATRIX */ 2846c79c5527SHong Zhang if (subsize == 1) { 2847c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 2848c79c5527SHong Zhang redund = c->redundant; 2849c79c5527SHong Zhang } else { 2850c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 2851c79c5527SHong Zhang redund = c->redundant; 2852c79c5527SHong Zhang } 2853c79c5527SHong Zhang 2854c79c5527SHong Zhang isrow = redund->isrow; 2855c79c5527SHong Zhang iscol = redund->iscol; 2856c79c5527SHong Zhang matseq = redund->matseq; 2857c79c5527SHong Zhang } 2858c79c5527SHong Zhang ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 2859c79c5527SHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJ(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 2860c79c5527SHong Zhang 2861c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2862c79c5527SHong Zhang /* create a supporting struct and attach it to C for reuse */ 2863b00a9115SJed Brown ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 2864c79c5527SHong Zhang if (subsize == 1) { 2865c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 2866c79c5527SHong Zhang c->redundant = redund; 2867c79c5527SHong Zhang } else { 2868c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 2869c79c5527SHong Zhang c->redundant = redund; 2870c79c5527SHong Zhang } 2871c79c5527SHong Zhang redund->isrow = isrow; 2872c79c5527SHong Zhang redund->iscol = iscol; 2873c79c5527SHong Zhang redund->matseq = matseq; 28741f2d8ef4SHong Zhang redund->psubcomm = psubcomm; 2875c79c5527SHong Zhang redund->Destroy = (*matredundant)->ops->destroy; 2876c79c5527SHong Zhang (*matredundant)->ops->destroy = MatDestroy_MatRedundant; 2877c79c5527SHong Zhang } 287869db28dcSHong Zhang PetscFunctionReturn(0); 287969db28dcSHong Zhang } 288069db28dcSHong Zhang 288103bc72f1SMatthew Knepley #undef __FUNCT__ 2882c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2883c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2884c91732d9SHong Zhang { 2885c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2886c91732d9SHong Zhang PetscErrorCode ierr; 2887c91732d9SHong Zhang PetscInt i,*idxb = 0; 2888c91732d9SHong Zhang PetscScalar *va,*vb; 2889c91732d9SHong Zhang Vec vtmp; 2890c91732d9SHong Zhang 2891c91732d9SHong Zhang PetscFunctionBegin; 2892c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2893c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2894c91732d9SHong Zhang if (idx) { 2895192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2896d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2897c91732d9SHong Zhang } 2898c91732d9SHong Zhang } 2899c91732d9SHong Zhang 2900d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2901c91732d9SHong Zhang if (idx) { 2902785e854fSJed Brown ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr); 2903c91732d9SHong Zhang } 2904c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2905c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2906c91732d9SHong Zhang 2907d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 2908c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2909c91732d9SHong Zhang va[i] = vb[i]; 2910c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2911c91732d9SHong Zhang } 2912c91732d9SHong Zhang } 2913c91732d9SHong Zhang 2914c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2915c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2916c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 29176bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2918c91732d9SHong Zhang PetscFunctionReturn(0); 2919c91732d9SHong Zhang } 2920c91732d9SHong Zhang 2921c91732d9SHong Zhang #undef __FUNCT__ 2922c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2923c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2924c87e5d42SMatthew Knepley { 2925c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2926c87e5d42SMatthew Knepley PetscErrorCode ierr; 2927c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2928c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2929c87e5d42SMatthew Knepley Vec vtmp; 2930c87e5d42SMatthew Knepley 2931c87e5d42SMatthew Knepley PetscFunctionBegin; 2932c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2933c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2934c87e5d42SMatthew Knepley if (idx) { 2935c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2936c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2937c87e5d42SMatthew Knepley } 2938c87e5d42SMatthew Knepley } 2939c87e5d42SMatthew Knepley 2940c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2941c87e5d42SMatthew Knepley if (idx) { 2942785e854fSJed Brown ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr); 2943c87e5d42SMatthew Knepley } 2944c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2945c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2946c87e5d42SMatthew Knepley 2947c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++) { 2948c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2949c87e5d42SMatthew Knepley va[i] = vb[i]; 2950c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2951c87e5d42SMatthew Knepley } 2952c87e5d42SMatthew Knepley } 2953c87e5d42SMatthew Knepley 2954c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2955c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2956c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 29576bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2958c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2959c87e5d42SMatthew Knepley } 2960c87e5d42SMatthew Knepley 2961c87e5d42SMatthew Knepley #undef __FUNCT__ 296203bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 296303bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 296403bc72f1SMatthew Knepley { 296503bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 2966d0f46423SBarry Smith PetscInt n = A->rmap->n; 2967d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 296803bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 296903bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 297003bc72f1SMatthew Knepley Vec diagV, offdiagV; 297103bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 297203bc72f1SMatthew Knepley PetscInt r; 297303bc72f1SMatthew Knepley PetscErrorCode ierr; 297403bc72f1SMatthew Knepley 297503bc72f1SMatthew Knepley PetscFunctionBegin; 2976dcca6d9dSJed Brown ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr); 2977ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);CHKERRQ(ierr); 2978ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);CHKERRQ(ierr); 297903bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 298003bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 298103bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 298203bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 298303bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 298403bc72f1SMatthew Knepley for (r = 0; r < n; ++r) { 2985028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 298603bc72f1SMatthew Knepley a[r] = diagA[r]; 298703bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 298803bc72f1SMatthew Knepley } else { 298903bc72f1SMatthew Knepley a[r] = offdiagA[r]; 299003bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 299103bc72f1SMatthew Knepley } 299203bc72f1SMatthew Knepley } 299303bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 299403bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 299503bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 29966bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 29976bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 299803bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 299903bc72f1SMatthew Knepley PetscFunctionReturn(0); 300003bc72f1SMatthew Knepley } 300103bc72f1SMatthew Knepley 30025494a064SHong Zhang #undef __FUNCT__ 3003c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 3004c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 3005c87e5d42SMatthew Knepley { 3006c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 3007c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 3008c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 3009c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 3010c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 3011c87e5d42SMatthew Knepley Vec diagV, offdiagV; 3012c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 3013c87e5d42SMatthew Knepley PetscInt r; 3014c87e5d42SMatthew Knepley PetscErrorCode ierr; 3015c87e5d42SMatthew Knepley 3016c87e5d42SMatthew Knepley PetscFunctionBegin; 3017dcca6d9dSJed Brown ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr); 3018d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr); 3019d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr); 3020c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 3021c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 3022c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 3023c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 3024c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 3025c87e5d42SMatthew Knepley for (r = 0; r < n; ++r) { 3026c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 3027c87e5d42SMatthew Knepley a[r] = diagA[r]; 3028c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 3029c87e5d42SMatthew Knepley } else { 3030c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 3031c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 3032c87e5d42SMatthew Knepley } 3033c87e5d42SMatthew Knepley } 3034c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 3035c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 3036c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30376bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30386bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 3039c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 3040c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3041c87e5d42SMatthew Knepley } 3042c87e5d42SMatthew Knepley 3043c87e5d42SMatthew Knepley #undef __FUNCT__ 3044d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ" 3045d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat) 30465494a064SHong Zhang { 30475494a064SHong Zhang PetscErrorCode ierr; 3048f6d58c54SBarry Smith Mat *dummy; 30495494a064SHong Zhang 30505494a064SHong Zhang PetscFunctionBegin; 3051f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 3052f6d58c54SBarry Smith *newmat = *dummy; 3053f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 30545494a064SHong Zhang PetscFunctionReturn(0); 30555494a064SHong Zhang } 30565494a064SHong Zhang 3057bbead8a2SBarry Smith #undef __FUNCT__ 3058bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ" 3059713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values) 3060bbead8a2SBarry Smith { 3061bbead8a2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 3062bbead8a2SBarry Smith PetscErrorCode ierr; 3063bbead8a2SBarry Smith 3064bbead8a2SBarry Smith PetscFunctionBegin; 3065bbead8a2SBarry Smith ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr); 3066bbead8a2SBarry Smith PetscFunctionReturn(0); 3067bbead8a2SBarry Smith } 3068bbead8a2SBarry Smith 306973a71a0fSBarry Smith #undef __FUNCT__ 307073a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ" 307173a71a0fSBarry Smith static PetscErrorCode MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx) 307273a71a0fSBarry Smith { 307373a71a0fSBarry Smith PetscErrorCode ierr; 307473a71a0fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)x->data; 307573a71a0fSBarry Smith 307673a71a0fSBarry Smith PetscFunctionBegin; 307773a71a0fSBarry Smith ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr); 307873a71a0fSBarry Smith ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr); 307973a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 308073a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 308173a71a0fSBarry Smith PetscFunctionReturn(0); 308273a71a0fSBarry Smith } 3083bbead8a2SBarry Smith 30848a729477SBarry Smith /* -------------------------------------------------------------------*/ 3085cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 3086cda55fadSBarry Smith MatGetRow_MPIAIJ, 3087cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 3088cda55fadSBarry Smith MatMult_MPIAIJ, 308997304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 30907c922b88SBarry Smith MatMultTranspose_MPIAIJ, 30917c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 3092519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3093103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 3094103bf8bdSMatthew Knepley #else 3095cda55fadSBarry Smith 0, 3096103bf8bdSMatthew Knepley #endif 3097cda55fadSBarry Smith 0, 3098cda55fadSBarry Smith 0, 309997304618SKris Buschelman /*10*/ 0, 3100cda55fadSBarry Smith 0, 3101cda55fadSBarry Smith 0, 310241f059aeSBarry Smith MatSOR_MPIAIJ, 3103b7c46309SBarry Smith MatTranspose_MPIAIJ, 310497304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 3105cda55fadSBarry Smith MatEqual_MPIAIJ, 3106cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 3107cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 3108cda55fadSBarry Smith MatNorm_MPIAIJ, 310997304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 3110cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 3111cda55fadSBarry Smith MatSetOption_MPIAIJ, 3112cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 3113d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 3114cda55fadSBarry Smith 0, 3115519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3116719d5645SBarry Smith 0, 3117103bf8bdSMatthew Knepley #else 3118cda55fadSBarry Smith 0, 3119103bf8bdSMatthew Knepley #endif 3120cda55fadSBarry Smith 0, 3121cda55fadSBarry Smith 0, 31224994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ, 3123519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3124719d5645SBarry Smith 0, 3125103bf8bdSMatthew Knepley #else 3126cda55fadSBarry Smith 0, 3127103bf8bdSMatthew Knepley #endif 3128cda55fadSBarry Smith 0, 3129cda55fadSBarry Smith 0, 3130cda55fadSBarry Smith 0, 3131d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 3132cda55fadSBarry Smith 0, 3133cda55fadSBarry Smith 0, 3134cda55fadSBarry Smith 0, 3135cda55fadSBarry Smith 0, 3136d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 3137cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 3138cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 3139cda55fadSBarry Smith MatGetValues_MPIAIJ, 3140cb5b572fSBarry Smith MatCopy_MPIAIJ, 3141d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 3142cda55fadSBarry Smith MatScale_MPIAIJ, 3143cda55fadSBarry Smith 0, 3144cda55fadSBarry Smith 0, 3145564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 314673a71a0fSBarry Smith /*49*/ MatSetRandom_MPIAIJ, 3147cda55fadSBarry Smith 0, 3148cda55fadSBarry Smith 0, 3149cda55fadSBarry Smith 0, 3150cda55fadSBarry Smith 0, 315193dfae19SHong Zhang /*54*/ MatFDColoringCreate_MPIXAIJ, 3152cda55fadSBarry Smith 0, 3153cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 315472e6a0cfSJed Brown MatPermute_MPIAIJ, 3155cda55fadSBarry Smith 0, 3156d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 3157e03a110bSBarry Smith MatDestroy_MPIAIJ, 3158e03a110bSBarry Smith MatView_MPIAIJ, 3159357abbc8SBarry Smith 0, 3160f996eeb8SHong Zhang MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ, 3161f996eeb8SHong Zhang /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ, 3162f996eeb8SHong Zhang MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ, 3163a2243be0SBarry Smith 0, 3164a2243be0SBarry Smith 0, 3165a2243be0SBarry Smith 0, 3166d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 3167c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 3168a2243be0SBarry Smith 0, 3169a2243be0SBarry Smith MatSetColoring_MPIAIJ, 3170dcf5cc72SBarry Smith 0, 317197304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 31723acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 317397304618SKris Buschelman 0, 317497304618SKris Buschelman 0, 317597304618SKris Buschelman 0, 3176f1f41ecbSJed Brown MatFindZeroDiagonals_MPIAIJ, 317797304618SKris Buschelman /*80*/ 0, 317897304618SKris Buschelman 0, 317997304618SKris Buschelman 0, 31805bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 31816284ec50SHong Zhang 0, 31826284ec50SHong Zhang 0, 31836284ec50SHong Zhang 0, 31846284ec50SHong Zhang 0, 3185865e5f61SKris Buschelman 0, 3186d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 318726be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 318826be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 3189cf3ca8ceSHong Zhang MatPtAP_MPIAIJ_MPIAIJ, 3190cf3ca8ceSHong Zhang MatPtAPSymbolic_MPIAIJ_MPIAIJ, 3191cf3ca8ceSHong Zhang /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ, 31927a7894deSKris Buschelman 0, 31937a7894deSKris Buschelman 0, 31947a7894deSKris Buschelman 0, 31957a7894deSKris Buschelman 0, 3196d519adbfSMatthew Knepley /*99*/ 0, 3197d2b207f1SPeter Brune 0, 3198d2b207f1SPeter Brune 0, 31992fd7e33dSBarry Smith MatConjugate_MPIAIJ, 32002fd7e33dSBarry Smith 0, 3201d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 320299cafbc1SBarry Smith MatRealPart_MPIAIJ, 320369db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 320469db28dcSHong Zhang 0, 320569db28dcSHong Zhang 0, 3206d519adbfSMatthew Knepley /*109*/0, 320703bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 32085494a064SHong Zhang MatGetRowMin_MPIAIJ, 32095494a064SHong Zhang 0, 32105494a064SHong Zhang 0, 3211d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ, 3212bd0c2dcbSBarry Smith 0, 3213bd0c2dcbSBarry Smith 0, 3214bd0c2dcbSBarry Smith 0, 3215bd0c2dcbSBarry Smith 0, 32168fb81238SShri Abhyankar /*119*/0, 32178fb81238SShri Abhyankar 0, 32188fb81238SShri Abhyankar 0, 3219d6037b41SHong Zhang 0, 3220b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 3221f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ, 32220716a85fSBarry Smith MatGetColumnNorms_MPIAIJ, 3223bbead8a2SBarry Smith MatInvertBlockDiagonal_MPIAIJ, 3224b9614d88SDmitry Karpeev 0, 322537868618SMatthew G Knepley MatGetSubMatricesParallel_MPIAIJ, 3226187b3c17SHong Zhang /*129*/0, 3227187b3c17SHong Zhang MatTransposeMatMult_MPIAIJ_MPIAIJ, 3228187b3c17SHong Zhang MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ, 3229187b3c17SHong Zhang MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ, 3230187b3c17SHong Zhang 0, 3231187b3c17SHong Zhang /*134*/0, 3232187b3c17SHong Zhang 0, 3233187b3c17SHong Zhang 0, 3234187b3c17SHong Zhang 0, 32353964eb88SJed Brown 0, 32363964eb88SJed Brown /*139*/0, 3237f9426fe0SMark Adams 0, 3238f86b9fbaSHong Zhang 0, 3239*e52d2c62SBarry Smith MatFDColoringSetUp_MPIXAIJ, 3240*e52d2c62SBarry Smith MatFindOffBlockDiagonalEntries_MPIAIJ 3241bd0c2dcbSBarry Smith }; 324236ce4990SBarry Smith 32432e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 32442e8a6d31SBarry Smith 32454a2ae208SSatish Balay #undef __FUNCT__ 32464a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 32477087cfbeSBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat) 32482e8a6d31SBarry Smith { 32492e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3250dfbe8321SBarry Smith PetscErrorCode ierr; 32512e8a6d31SBarry Smith 32522e8a6d31SBarry Smith PetscFunctionBegin; 32532e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 32542e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 32552e8a6d31SBarry Smith PetscFunctionReturn(0); 32562e8a6d31SBarry Smith } 32572e8a6d31SBarry Smith 32584a2ae208SSatish Balay #undef __FUNCT__ 32594a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 32607087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat) 32612e8a6d31SBarry Smith { 32622e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3263dfbe8321SBarry Smith PetscErrorCode ierr; 32642e8a6d31SBarry Smith 32652e8a6d31SBarry Smith PetscFunctionBegin; 32662e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 32672e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 32682e8a6d31SBarry Smith PetscFunctionReturn(0); 32692e8a6d31SBarry Smith } 32708a729477SBarry Smith 32714a2ae208SSatish Balay #undef __FUNCT__ 3272a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 32737087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3274a23d5eceSKris Buschelman { 3275a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3276dfbe8321SBarry Smith PetscErrorCode ierr; 3277a23d5eceSKris Buschelman 3278a23d5eceSKris Buschelman PetscFunctionBegin; 327926283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 328026283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3281a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3282899cda47SBarry Smith 3283526dfc15SBarry Smith if (!B->preallocated) { 3284899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3285899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3286d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 328733d57670SJed Brown ierr = MatSetBlockSizesFromMats(b->A,B,B);CHKERRQ(ierr); 3288899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 32893bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);CHKERRQ(ierr); 3290899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3291d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 329233d57670SJed Brown ierr = MatSetBlockSizesFromMats(b->B,B,B);CHKERRQ(ierr); 3293899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 32943bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);CHKERRQ(ierr); 3295526dfc15SBarry Smith } 3296899cda47SBarry Smith 3297c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3298c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 3299526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3300a23d5eceSKris Buschelman PetscFunctionReturn(0); 3301a23d5eceSKris Buschelman } 3302a23d5eceSKris Buschelman 33034a2ae208SSatish Balay #undef __FUNCT__ 33044a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3305dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3306d6dfbf8fSBarry Smith { 3307d6dfbf8fSBarry Smith Mat mat; 3308416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3309dfbe8321SBarry Smith PetscErrorCode ierr; 3310d6dfbf8fSBarry Smith 33113a40ed3dSBarry Smith PetscFunctionBegin; 3312416022c9SBarry Smith *newmat = 0; 3313ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr); 3314d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 331533d57670SJed Brown ierr = MatSetBlockSizesFromMats(mat,matin,matin);CHKERRQ(ierr); 33167adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 33171d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3318273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3319e1b6402fSHong Zhang 3320d5f3da31SBarry Smith mat->factortype = matin->factortype; 3321c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3322e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3323273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3324d6dfbf8fSBarry Smith 332517699dbbSLois Curfman McInnes a->size = oldmat->size; 332617699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3327e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3328e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3329e7641de0SSatish Balay a->rowindices = 0; 3330bcd2baecSBarry Smith a->rowvalues = 0; 3331bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3332d6dfbf8fSBarry Smith 33331e1e43feSBarry Smith ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr); 33341e1e43feSBarry Smith ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3335899cda47SBarry Smith 33362ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3337aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 33380f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3339b1fc9764SSatish Balay #else 3340785e854fSJed Brown ierr = PetscMalloc1((mat->cmap->N),&a->colmap);CHKERRQ(ierr); 33413bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3342d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3343b1fc9764SSatish Balay #endif 3344416022c9SBarry Smith } else a->colmap = 0; 33453f41c07dSBarry Smith if (oldmat->garray) { 3346b1d57f15SBarry Smith PetscInt len; 3347d0f46423SBarry Smith len = oldmat->B->cmap->n; 3348785e854fSJed Brown ierr = PetscMalloc1((len+1),&a->garray);CHKERRQ(ierr); 33493bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3350b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3351416022c9SBarry Smith } else a->garray = 0; 3352d6dfbf8fSBarry Smith 3353416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 33543bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);CHKERRQ(ierr); 3355a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 33563bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);CHKERRQ(ierr); 33572e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 33583bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);CHKERRQ(ierr); 33592e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 33603bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);CHKERRQ(ierr); 3361140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 33628a729477SBarry Smith *newmat = mat; 33633a40ed3dSBarry Smith PetscFunctionReturn(0); 33648a729477SBarry Smith } 3365416022c9SBarry Smith 33661a4ee126SBarry Smith 33671a4ee126SBarry Smith 33684a2ae208SSatish Balay #undef __FUNCT__ 33695bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3370112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 33718fb81238SShri Abhyankar { 33728fb81238SShri Abhyankar PetscScalar *vals,*svals; 3373ce94432eSBarry Smith MPI_Comm comm; 33748fb81238SShri Abhyankar PetscErrorCode ierr; 33751a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 33768fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 33778fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 33780298fd71SBarry Smith PetscInt *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols; 33798fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 33808fb81238SShri Abhyankar int fd; 338108ea439dSMark F. Adams PetscInt bs = 1; 33828fb81238SShri Abhyankar 33838fb81238SShri Abhyankar PetscFunctionBegin; 3384ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 33858fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 33868fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 33878fb81238SShri Abhyankar if (!rank) { 33888fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 33898fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr); 33908fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 33918fb81238SShri Abhyankar } 33928fb81238SShri Abhyankar 33930298fd71SBarry Smith ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 33940298fd71SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr); 339508ea439dSMark F. Adams ierr = PetscOptionsEnd();CHKERRQ(ierr); 339608ea439dSMark F. Adams 33978fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 33988fb81238SShri Abhyankar 33998fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 34008fb81238SShri Abhyankar M = header[1]; N = header[2]; 34018fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 34028fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 34038fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 34048fb81238SShri Abhyankar 34058fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 34068fb81238SShri Abhyankar if (sizesset) { 34078fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 34088fb81238SShri Abhyankar } 3409abd38a8fSBarry 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); 3410abd38a8fSBarry 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); 34118fb81238SShri Abhyankar 341208ea439dSMark F. Adams /* determine ownership of all (block) rows */ 341308ea439dSMark F. Adams if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs); 341408ea439dSMark F. Adams if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank)); /* PETSC_DECIDE */ 34154683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 34168fb81238SShri Abhyankar 3417785e854fSJed Brown ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr); 34188fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 34198fb81238SShri Abhyankar 34208fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 34218fb81238SShri Abhyankar if (!rank) { 34228fb81238SShri Abhyankar mmax = rowners[1]; 34235c4ea359SMatthew G Knepley for (i=2; i<=size; i++) { 34248fb81238SShri Abhyankar mmax = PetscMax(mmax, rowners[i]); 34258fb81238SShri Abhyankar } 34263964eb88SJed Brown } else mmax = -1; /* unused, but compilers complain */ 34278fb81238SShri Abhyankar 34288fb81238SShri Abhyankar rowners[0] = 0; 34298fb81238SShri Abhyankar for (i=2; i<=size; i++) { 34308fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 34318fb81238SShri Abhyankar } 34328fb81238SShri Abhyankar rstart = rowners[rank]; 34338fb81238SShri Abhyankar rend = rowners[rank+1]; 34348fb81238SShri Abhyankar 34358fb81238SShri Abhyankar /* distribute row lengths to all processors */ 3436dcca6d9dSJed Brown ierr = PetscMalloc2(m,&ourlens,m,&offlens);CHKERRQ(ierr); 34378fb81238SShri Abhyankar if (!rank) { 34388fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 3439785e854fSJed Brown ierr = PetscMalloc1(mmax,&rowlengths);CHKERRQ(ierr); 34401795a4d1SJed Brown ierr = PetscCalloc1(size,&procsnz);CHKERRQ(ierr); 34418fb81238SShri Abhyankar for (j=0; j<m; j++) { 34428fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 34438fb81238SShri Abhyankar } 34448fb81238SShri Abhyankar for (i=1; i<size; i++) { 34458fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 34468fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 34478fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 34488fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 34498fb81238SShri Abhyankar } 3450a25532f0SBarry Smith ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 34518fb81238SShri Abhyankar } 34528fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 34538fb81238SShri Abhyankar } else { 3454a25532f0SBarry Smith ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 34558fb81238SShri Abhyankar } 34568fb81238SShri Abhyankar 34578fb81238SShri Abhyankar if (!rank) { 34588fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 34598fb81238SShri Abhyankar maxnz = 0; 34608fb81238SShri Abhyankar for (i=0; i<size; i++) { 34618fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 34628fb81238SShri Abhyankar } 3463785e854fSJed Brown ierr = PetscMalloc1(maxnz,&cols);CHKERRQ(ierr); 34648fb81238SShri Abhyankar 34658fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 34668fb81238SShri Abhyankar nz = procsnz[0]; 3467785e854fSJed Brown ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr); 34688fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 34698fb81238SShri Abhyankar 34708fb81238SShri Abhyankar /* read in every one elses and ship off */ 34718fb81238SShri Abhyankar for (i=1; i<size; i++) { 34728fb81238SShri Abhyankar nz = procsnz[i]; 34738fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 3474a25532f0SBarry Smith ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 34758fb81238SShri Abhyankar } 34768fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 34778fb81238SShri Abhyankar } else { 34788fb81238SShri Abhyankar /* determine buffer space needed for message */ 34798fb81238SShri Abhyankar nz = 0; 34808fb81238SShri Abhyankar for (i=0; i<m; i++) { 34818fb81238SShri Abhyankar nz += ourlens[i]; 34828fb81238SShri Abhyankar } 3483785e854fSJed Brown ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr); 34848fb81238SShri Abhyankar 34858fb81238SShri Abhyankar /* receive message of column indices*/ 3486a25532f0SBarry Smith ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 34878fb81238SShri Abhyankar } 34888fb81238SShri Abhyankar 34898fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 34908fb81238SShri Abhyankar if (N != M) { 34918fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 34928fb81238SShri Abhyankar else n = newMat->cmap->n; 34938fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 34948fb81238SShri Abhyankar cstart = cend - n; 34958fb81238SShri Abhyankar } else { 34968fb81238SShri Abhyankar cstart = rstart; 34978fb81238SShri Abhyankar cend = rend; 34988fb81238SShri Abhyankar n = cend - cstart; 34998fb81238SShri Abhyankar } 35008fb81238SShri Abhyankar 35018fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 35028fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 35038fb81238SShri Abhyankar jj = 0; 35048fb81238SShri Abhyankar for (i=0; i<m; i++) { 35058fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 35068fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 35078fb81238SShri Abhyankar jj++; 35088fb81238SShri Abhyankar } 35098fb81238SShri Abhyankar } 35108fb81238SShri Abhyankar 35118fb81238SShri Abhyankar for (i=0; i<m; i++) { 35128fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 35138fb81238SShri Abhyankar } 35148fb81238SShri Abhyankar if (!sizesset) { 35158fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 35168fb81238SShri Abhyankar } 351708ea439dSMark F. Adams 351808ea439dSMark F. Adams if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);} 351908ea439dSMark F. Adams 35208fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 35218fb81238SShri Abhyankar 35228fb81238SShri Abhyankar for (i=0; i<m; i++) { 35238fb81238SShri Abhyankar ourlens[i] += offlens[i]; 35248fb81238SShri Abhyankar } 35258fb81238SShri Abhyankar 35268fb81238SShri Abhyankar if (!rank) { 3527785e854fSJed Brown ierr = PetscMalloc1((maxnz+1),&vals);CHKERRQ(ierr); 35288fb81238SShri Abhyankar 35298fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 35308fb81238SShri Abhyankar nz = procsnz[0]; 35318fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 35328fb81238SShri Abhyankar 35338fb81238SShri Abhyankar /* insert into matrix */ 35348fb81238SShri Abhyankar jj = rstart; 35358fb81238SShri Abhyankar smycols = mycols; 35368fb81238SShri Abhyankar svals = vals; 35378fb81238SShri Abhyankar for (i=0; i<m; i++) { 35388fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35398fb81238SShri Abhyankar smycols += ourlens[i]; 35408fb81238SShri Abhyankar svals += ourlens[i]; 35418fb81238SShri Abhyankar jj++; 35428fb81238SShri Abhyankar } 35438fb81238SShri Abhyankar 35448fb81238SShri Abhyankar /* read in other processors and ship out */ 35458fb81238SShri Abhyankar for (i=1; i<size; i++) { 35468fb81238SShri Abhyankar nz = procsnz[i]; 35478fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3548a25532f0SBarry Smith ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35498fb81238SShri Abhyankar } 35508fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 35518fb81238SShri Abhyankar } else { 35528fb81238SShri Abhyankar /* receive numeric values */ 3553785e854fSJed Brown ierr = PetscMalloc1((nz+1),&vals);CHKERRQ(ierr); 35548fb81238SShri Abhyankar 35558fb81238SShri Abhyankar /* receive message of values*/ 3556a25532f0SBarry Smith ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35578fb81238SShri Abhyankar 35588fb81238SShri Abhyankar /* insert into matrix */ 35598fb81238SShri Abhyankar jj = rstart; 35608fb81238SShri Abhyankar smycols = mycols; 35618fb81238SShri Abhyankar svals = vals; 35628fb81238SShri Abhyankar for (i=0; i<m; i++) { 35638fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35648fb81238SShri Abhyankar smycols += ourlens[i]; 35658fb81238SShri Abhyankar svals += ourlens[i]; 35668fb81238SShri Abhyankar jj++; 35678fb81238SShri Abhyankar } 35688fb81238SShri Abhyankar } 35698fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 35708fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 35718fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 35728fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 35738fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 35748fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 35758fb81238SShri Abhyankar PetscFunctionReturn(0); 35768fb81238SShri Abhyankar } 35778fb81238SShri Abhyankar 35788fb81238SShri Abhyankar #undef __FUNCT__ 35794a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 35804aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 35814aa3045dSJed Brown { 35824aa3045dSJed Brown PetscErrorCode ierr; 35834aa3045dSJed Brown IS iscol_local; 35844aa3045dSJed Brown PetscInt csize; 35854aa3045dSJed Brown 35864aa3045dSJed Brown PetscFunctionBegin; 35874aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3588b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3589b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3590e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3591b79d0421SJed Brown } else { 3592c5bfad50SMark F. Adams PetscInt cbs; 3593c5bfad50SMark F. Adams ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 35944aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3595c5bfad50SMark F. Adams ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr); 3596b79d0421SJed Brown } 35974aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3598b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3599b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 36006bf464f9SBarry Smith ierr = ISDestroy(&iscol_local);CHKERRQ(ierr); 3601b79d0421SJed Brown } 36024aa3045dSJed Brown PetscFunctionReturn(0); 36034aa3045dSJed Brown } 36044aa3045dSJed Brown 360529dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*); 36064aa3045dSJed Brown #undef __FUNCT__ 36074aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3608a0ff6018SBarry Smith /* 360929da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 361029da9460SBarry Smith in local and then by concatenating the local matrices the end result. 361129da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 36124aa3045dSJed Brown 36134aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3614a0ff6018SBarry Smith */ 36154aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3616a0ff6018SBarry Smith { 3617dfbe8321SBarry Smith PetscErrorCode ierr; 361832dcc486SBarry Smith PetscMPIInt rank,size; 3619a2f3521dSMark F. Adams PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs; 362029dcf524SDmitry Karpeev PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol; 362129dcf524SDmitry Karpeev PetscBool allcolumns, colflag; 362229dcf524SDmitry Karpeev Mat M,Mreuse; 3623a77337e4SBarry Smith MatScalar *vwork,*aa; 3624ce94432eSBarry Smith MPI_Comm comm; 362500e6dbe6SBarry Smith Mat_SeqAIJ *aij; 36267e2c5f70SBarry Smith 3627a0ff6018SBarry Smith PetscFunctionBegin; 3628ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 36291dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 36301dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 363100e6dbe6SBarry Smith 363229dcf524SDmitry Karpeev ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr); 363329dcf524SDmitry Karpeev ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr); 363429dcf524SDmitry Karpeev if (colflag && ncol == mat->cmap->N) { 363529dcf524SDmitry Karpeev allcolumns = PETSC_TRUE; 363629dcf524SDmitry Karpeev } else { 363729dcf524SDmitry Karpeev allcolumns = PETSC_FALSE; 363829dcf524SDmitry Karpeev } 3639fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3640fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr); 3641e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 364229dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3643fee21e36SBarry Smith } else { 364429dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3645fee21e36SBarry Smith } 3646a0ff6018SBarry Smith 3647a0ff6018SBarry Smith /* 3648a0ff6018SBarry Smith m - number of local rows 3649a0ff6018SBarry Smith n - number of columns (same on all processors) 3650a0ff6018SBarry Smith rstart - first row in new global matrix generated 3651a0ff6018SBarry Smith */ 3652fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3653a2f3521dSMark F. Adams ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr); 3654a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3655fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 365600e6dbe6SBarry Smith ii = aij->i; 365700e6dbe6SBarry Smith jj = aij->j; 365800e6dbe6SBarry Smith 3659a0ff6018SBarry Smith /* 366000e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 366100e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3662a0ff6018SBarry Smith */ 366300e6dbe6SBarry Smith 366400e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 36656a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3666ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3667ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3668e2c4fddaSBarry Smith nlocal = m; 36696a6a5d1dSBarry Smith } else { 3670ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3671ab50ec6bSBarry Smith } 3672ab50ec6bSBarry Smith } else { 36736a6a5d1dSBarry Smith nlocal = csize; 36746a6a5d1dSBarry Smith } 3675b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 367600e6dbe6SBarry Smith rstart = rend - nlocal; 367765e19b50SBarry 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); 367800e6dbe6SBarry Smith 367900e6dbe6SBarry Smith /* next, compute all the lengths */ 3680785e854fSJed Brown ierr = PetscMalloc1((2*m+1),&dlens);CHKERRQ(ierr); 368100e6dbe6SBarry Smith olens = dlens + m; 368200e6dbe6SBarry Smith for (i=0; i<m; i++) { 368300e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 368400e6dbe6SBarry Smith olen = 0; 368500e6dbe6SBarry Smith dlen = 0; 368600e6dbe6SBarry Smith for (j=0; j<jend; j++) { 368700e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 368800e6dbe6SBarry Smith else dlen++; 368900e6dbe6SBarry Smith jj++; 369000e6dbe6SBarry Smith } 369100e6dbe6SBarry Smith olens[i] = olen; 369200e6dbe6SBarry Smith dlens[i] = dlen; 369300e6dbe6SBarry Smith } 3694f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3695f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 3696a2f3521dSMark F. Adams ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); 36977adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3698e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3699606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3700a0ff6018SBarry Smith } else { 3701b1d57f15SBarry Smith PetscInt ml,nl; 3702a0ff6018SBarry Smith 3703a0ff6018SBarry Smith M = *newmat; 3704a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3705e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3706a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3707c48de900SBarry Smith /* 3708c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3709c48de900SBarry Smith rather than the slower MatSetValues(). 3710c48de900SBarry Smith */ 3711c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3712c48de900SBarry Smith M->assembled = PETSC_FALSE; 3713a0ff6018SBarry Smith } 3714a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3715fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 371600e6dbe6SBarry Smith ii = aij->i; 371700e6dbe6SBarry Smith jj = aij->j; 371800e6dbe6SBarry Smith aa = aij->a; 3719a0ff6018SBarry Smith for (i=0; i<m; i++) { 3720a0ff6018SBarry Smith row = rstart + i; 372100e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 372200e6dbe6SBarry Smith cwork = jj; jj += nz; 372300e6dbe6SBarry Smith vwork = aa; aa += nz; 37248c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3725a0ff6018SBarry Smith } 3726a0ff6018SBarry Smith 3727a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3728a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3729a0ff6018SBarry Smith *newmat = M; 3730fee21e36SBarry Smith 3731fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3732fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3733fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3734bf0cc555SLisandro Dalcin ierr = MatDestroy(&Mreuse);CHKERRQ(ierr); 3735fee21e36SBarry Smith } 3736a0ff6018SBarry Smith PetscFunctionReturn(0); 3737a0ff6018SBarry Smith } 3738273d9f13SBarry Smith 37394a2ae208SSatish Balay #undef __FUNCT__ 3740ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 37417087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3742ccd8e176SBarry Smith { 3743899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3744899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3745ccd8e176SBarry Smith const PetscInt *JJ; 3746ccd8e176SBarry Smith PetscScalar *values; 3747ccd8e176SBarry Smith PetscErrorCode ierr; 3748ccd8e176SBarry Smith 3749ccd8e176SBarry Smith PetscFunctionBegin; 3750e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3751899cda47SBarry Smith 375226283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 375326283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3754d0f46423SBarry Smith m = B->rmap->n; 3755d0f46423SBarry Smith cstart = B->cmap->rstart; 3756d0f46423SBarry Smith cend = B->cmap->rend; 3757d0f46423SBarry Smith rstart = B->rmap->rstart; 3758899cda47SBarry Smith 3759dcca6d9dSJed Brown ierr = PetscMalloc2(m,&d_nnz,m,&o_nnz);CHKERRQ(ierr); 3760ccd8e176SBarry Smith 3761ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3762ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3763ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3764ecc77c7aSBarry Smith JJ = J + Ii[i]; 3765e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3766ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3767d0f46423SBarry 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); 3768ecc77c7aSBarry Smith } 3769ecc77c7aSBarry Smith #endif 3770ecc77c7aSBarry Smith 3771ccd8e176SBarry Smith for (i=0; i<m; i++) { 3772b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3773b7940d39SSatish Balay JJ = J + Ii[i]; 3774ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3775ccd8e176SBarry Smith d = 0; 37760daa03b5SJed Brown for (j=0; j<nnz; j++) { 37770daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3778ccd8e176SBarry Smith } 3779ccd8e176SBarry Smith d_nnz[i] = d; 3780ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3781ccd8e176SBarry Smith } 3782ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 37831d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3784ccd8e176SBarry Smith 3785ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3786ccd8e176SBarry Smith else { 37871795a4d1SJed Brown ierr = PetscCalloc1((nnz_max+1),&values);CHKERRQ(ierr); 3788ccd8e176SBarry Smith } 3789ccd8e176SBarry Smith 3790ccd8e176SBarry Smith for (i=0; i<m; i++) { 3791ccd8e176SBarry Smith ii = i + rstart; 3792b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3793b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3794ccd8e176SBarry Smith } 3795ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3796ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3797ccd8e176SBarry Smith 3798ccd8e176SBarry Smith if (!v) { 3799ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3800ccd8e176SBarry Smith } 38017827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3802ccd8e176SBarry Smith PetscFunctionReturn(0); 3803ccd8e176SBarry Smith } 3804ccd8e176SBarry Smith 3805ccd8e176SBarry Smith #undef __FUNCT__ 3806ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 38071eea217eSSatish Balay /*@ 3808ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3809ccd8e176SBarry Smith (the default parallel PETSc format). 3810ccd8e176SBarry Smith 3811ccd8e176SBarry Smith Collective on MPI_Comm 3812ccd8e176SBarry Smith 3813ccd8e176SBarry Smith Input Parameters: 3814a1661176SMatthew Knepley + B - the matrix 3815ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 38160daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3817ccd8e176SBarry Smith - v - optional values in the matrix 3818ccd8e176SBarry Smith 3819ccd8e176SBarry Smith Level: developer 3820ccd8e176SBarry Smith 382112251496SSatish Balay Notes: 382212251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 382312251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 382412251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 382512251496SSatish Balay 382612251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 382712251496SSatish Balay 382812251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 382912251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 383012251496SSatish Balay as shown: 383112251496SSatish Balay 383212251496SSatish Balay 1 0 0 383312251496SSatish Balay 2 0 3 P0 383412251496SSatish Balay ------- 383512251496SSatish Balay 4 5 6 P1 383612251496SSatish Balay 383712251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 383812251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 383912251496SSatish Balay j = {0,0,2} [size = nz = 6] 384012251496SSatish Balay v = {1,2,3} [size = nz = 6] 384112251496SSatish Balay 384212251496SSatish Balay Process1 [P1]: rows_owned=[2] 384312251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 384412251496SSatish Balay j = {0,1,2} [size = nz = 6] 384512251496SSatish Balay v = {4,5,6} [size = nz = 6] 384612251496SSatish Balay 3847ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3848ccd8e176SBarry Smith 384969b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, 38508d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3851ccd8e176SBarry Smith @*/ 38527087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3853ccd8e176SBarry Smith { 38544ac538c5SBarry Smith PetscErrorCode ierr; 3855ccd8e176SBarry Smith 3856ccd8e176SBarry Smith PetscFunctionBegin; 38574ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3858ccd8e176SBarry Smith PetscFunctionReturn(0); 3859ccd8e176SBarry Smith } 3860ccd8e176SBarry Smith 3861ccd8e176SBarry Smith #undef __FUNCT__ 38624a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3863273d9f13SBarry Smith /*@C 3864ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3865273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3866273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3867273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3868273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3869273d9f13SBarry Smith 3870273d9f13SBarry Smith Collective on MPI_Comm 3871273d9f13SBarry Smith 3872273d9f13SBarry Smith Input Parameters: 3873273d9f13SBarry Smith + A - the matrix 3874273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3875273d9f13SBarry Smith (same value is used for all local rows) 3876273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3877273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 38780298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 3879273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 38803287b5eaSJed Brown For matrices that will be factored, you must leave room for (and set) 38813287b5eaSJed Brown the diagonal entry even if it is zero. 3882273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3883273d9f13SBarry Smith submatrix (same value is used for all local rows). 3884273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3885273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 38860298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 3887273d9f13SBarry Smith structure. The size of this array is equal to the number 3888273d9f13SBarry Smith of local rows, i.e 'm'. 3889273d9f13SBarry Smith 389049a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 389149a6f317SBarry Smith 3892273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3893ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 38940598bfebSBarry Smith storage. The stored row and column indices begin with zero. 3895a7f22e61SSatish Balay See Users-Manual: ch_mat for details. 3896273d9f13SBarry Smith 3897273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3898273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3899273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3900273d9f13SBarry Smith 3901273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3902a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3903a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3904a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3905a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3906a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3907a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3908a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3909a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3910273d9f13SBarry Smith 3911273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3912273d9f13SBarry Smith 3913aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3914aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3915aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3916aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3917aa95bbe8SBarry Smith 3918273d9f13SBarry Smith Example usage: 3919273d9f13SBarry Smith 3920273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3921273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3922273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3923273d9f13SBarry Smith as follows: 3924273d9f13SBarry Smith 3925273d9f13SBarry Smith .vb 3926273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3927273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3928273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3929273d9f13SBarry Smith ------------------------------------- 3930273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3931273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3932273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3933273d9f13SBarry Smith ------------------------------------- 3934273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3935273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3936273d9f13SBarry Smith .ve 3937273d9f13SBarry Smith 3938273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3939273d9f13SBarry Smith 3940273d9f13SBarry Smith .vb 3941273d9f13SBarry Smith A B C 3942273d9f13SBarry Smith D E F 3943273d9f13SBarry Smith G H I 3944273d9f13SBarry Smith .ve 3945273d9f13SBarry Smith 3946273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3947273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3948273d9f13SBarry Smith 3949273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3950273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3951273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3952273d9f13SBarry Smith 3953273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3954273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3955273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3956273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3957273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3958273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3959273d9f13SBarry Smith 3960273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3961273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3962273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3963273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3964273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3965273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3966273d9f13SBarry Smith .vb 3967273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3968273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3969273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3970273d9f13SBarry Smith .ve 3971273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3972273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3973273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3974273d9f13SBarry Smith 34 values. 3975273d9f13SBarry Smith 3976273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3977273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3978273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3979273d9f13SBarry Smith .vb 3980273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3981273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3982273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3983273d9f13SBarry Smith .ve 3984273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3985273d9f13SBarry Smith hence pre-allocation is perfect. 3986273d9f13SBarry Smith 3987273d9f13SBarry Smith Level: intermediate 3988273d9f13SBarry Smith 3989273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3990273d9f13SBarry Smith 399169b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(), 3992ab978733SBarry Smith MPIAIJ, MatGetInfo(), PetscSplitOwnership() 3993273d9f13SBarry Smith @*/ 39947087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3995273d9f13SBarry Smith { 39964ac538c5SBarry Smith PetscErrorCode ierr; 3997273d9f13SBarry Smith 3998273d9f13SBarry Smith PetscFunctionBegin; 39996ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 40006ba663aaSJed Brown PetscValidType(B,1); 40014ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 4002273d9f13SBarry Smith PetscFunctionReturn(0); 4003273d9f13SBarry Smith } 4004273d9f13SBarry Smith 40054a2ae208SSatish Balay #undef __FUNCT__ 40062fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 400758d36128SBarry Smith /*@ 40082fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 40092fb0ec9aSBarry Smith CSR format the local rows. 40102fb0ec9aSBarry Smith 40112fb0ec9aSBarry Smith Collective on MPI_Comm 40122fb0ec9aSBarry Smith 40132fb0ec9aSBarry Smith Input Parameters: 40142fb0ec9aSBarry Smith + comm - MPI communicator 40152fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 40162fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 40172fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 40182fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 40192fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 40202fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 40212fb0ec9aSBarry Smith . i - row indices 40222fb0ec9aSBarry Smith . j - column indices 40232fb0ec9aSBarry Smith - a - matrix values 40242fb0ec9aSBarry Smith 40252fb0ec9aSBarry Smith Output Parameter: 40262fb0ec9aSBarry Smith . mat - the matrix 402703bfb495SBarry Smith 40282fb0ec9aSBarry Smith Level: intermediate 40292fb0ec9aSBarry Smith 40302fb0ec9aSBarry Smith Notes: 40312fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 40322fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 40338d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 40342fb0ec9aSBarry Smith 403512251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 403612251496SSatish Balay 403712251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 403812251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 403912251496SSatish Balay as shown: 404012251496SSatish Balay 404112251496SSatish Balay 1 0 0 404212251496SSatish Balay 2 0 3 P0 404312251496SSatish Balay ------- 404412251496SSatish Balay 4 5 6 P1 404512251496SSatish Balay 404612251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 404712251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 404812251496SSatish Balay j = {0,0,2} [size = nz = 6] 404912251496SSatish Balay v = {1,2,3} [size = nz = 6] 405012251496SSatish Balay 405112251496SSatish Balay Process1 [P1]: rows_owned=[2] 405212251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 405312251496SSatish Balay j = {0,1,2} [size = nz = 6] 405412251496SSatish Balay v = {4,5,6} [size = nz = 6] 40552fb0ec9aSBarry Smith 40562fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 40572fb0ec9aSBarry Smith 40582fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 405969b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays() 40602fb0ec9aSBarry Smith @*/ 40617087cfbeSBarry 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) 40622fb0ec9aSBarry Smith { 40632fb0ec9aSBarry Smith PetscErrorCode ierr; 40642fb0ec9aSBarry Smith 40652fb0ec9aSBarry Smith PetscFunctionBegin; 406669b1f4b7SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4067e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 40682fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4069d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 4070a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */ 40712fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 40722fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 40732fb0ec9aSBarry Smith PetscFunctionReturn(0); 40742fb0ec9aSBarry Smith } 40752fb0ec9aSBarry Smith 40762fb0ec9aSBarry Smith #undef __FUNCT__ 407769b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ" 4078273d9f13SBarry Smith /*@C 407969b1f4b7SBarry Smith MatCreateAIJ - Creates a sparse parallel matrix in AIJ format 4080273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 4081273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 4082273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 4083273d9f13SBarry Smith performance can be increased by more than a factor of 50. 4084273d9f13SBarry Smith 4085273d9f13SBarry Smith Collective on MPI_Comm 4086273d9f13SBarry Smith 4087273d9f13SBarry Smith Input Parameters: 4088273d9f13SBarry Smith + comm - MPI communicator 4089273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 4090273d9f13SBarry Smith This value should be the same as the local size used in creating the 4091273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 4092273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 4093273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 4094273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 4095273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 4096273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 4097273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 4098273d9f13SBarry Smith (same value is used for all local rows) 4099273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 4100273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 41010298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 4102273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 4103273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 4104273d9f13SBarry Smith submatrix (same value is used for all local rows). 4105273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 4106273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 41070298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 4108273d9f13SBarry Smith structure. The size of this array is equal to the number 4109273d9f13SBarry Smith of local rows, i.e 'm'. 4110273d9f13SBarry Smith 4111273d9f13SBarry Smith Output Parameter: 4112273d9f13SBarry Smith . A - the matrix 4113273d9f13SBarry Smith 4114175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 4115ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 4116175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 4117175b88e8SBarry Smith 4118273d9f13SBarry Smith Notes: 411949a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 412049a6f317SBarry Smith 4121273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 4122273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 4123273d9f13SBarry Smith storage requirements for this matrix. 4124273d9f13SBarry Smith 4125273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 4126273d9f13SBarry Smith processor than it must be used on all processors that share the object for 4127273d9f13SBarry Smith that argument. 4128273d9f13SBarry Smith 4129273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 4130273d9f13SBarry Smith (possibly both). 4131273d9f13SBarry Smith 413233a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 413333a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 413433a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 413533a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 413633a7c187SSatish Balay values corresponding to [m x N] submatrix. 4137273d9f13SBarry Smith 413833a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 413933a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 414033a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 414133a7c187SSatish Balay 414233a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 414333a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 414433a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 414533a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 414633a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 414733a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 414833a7c187SSatish Balay illustrates this concept. 414933a7c187SSatish Balay 415033a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 415133a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 415233a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 415333a7c187SSatish Balay local matrix (a rectangular submatrix). 4154273d9f13SBarry Smith 4155273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 4156273d9f13SBarry Smith 415797d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 415897d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 415997d05335SKris Buschelman type of communicator, use the construction mechanism: 416078102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 416197d05335SKris Buschelman 4162273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 4163273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 4164273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 4165273d9f13SBarry Smith 4166273d9f13SBarry Smith Options Database Keys: 4167923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 4168923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 4169273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 4170273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 4171273d9f13SBarry Smith the user still MUST index entries starting at 0! 4172273d9f13SBarry Smith 4173273d9f13SBarry Smith 4174273d9f13SBarry Smith Example usage: 4175273d9f13SBarry Smith 4176273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 4177273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 4178273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 4179273d9f13SBarry Smith as follows: 4180273d9f13SBarry Smith 4181273d9f13SBarry Smith .vb 4182273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 4183273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 4184273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 4185273d9f13SBarry Smith ------------------------------------- 4186273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 4187273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 4188273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 4189273d9f13SBarry Smith ------------------------------------- 4190273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 4191273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 4192273d9f13SBarry Smith .ve 4193273d9f13SBarry Smith 4194273d9f13SBarry Smith This can be represented as a collection of submatrices as: 4195273d9f13SBarry Smith 4196273d9f13SBarry Smith .vb 4197273d9f13SBarry Smith A B C 4198273d9f13SBarry Smith D E F 4199273d9f13SBarry Smith G H I 4200273d9f13SBarry Smith .ve 4201273d9f13SBarry Smith 4202273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 4203273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 4204273d9f13SBarry Smith 4205273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4206273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4207273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 4208273d9f13SBarry Smith 4209273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 4210273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 4211273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 4212273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4213273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4214273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4215273d9f13SBarry Smith 4216273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4217273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4218273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4219273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4220273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4221273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4222273d9f13SBarry Smith .vb 4223273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4224273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4225273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4226273d9f13SBarry Smith .ve 4227273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4228273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4229273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4230273d9f13SBarry Smith 34 values. 4231273d9f13SBarry Smith 4232273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4233273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4234273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4235273d9f13SBarry Smith .vb 4236273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4237273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4238273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4239273d9f13SBarry Smith .ve 4240273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4241273d9f13SBarry Smith hence pre-allocation is perfect. 4242273d9f13SBarry Smith 4243273d9f13SBarry Smith Level: intermediate 4244273d9f13SBarry Smith 4245273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4246273d9f13SBarry Smith 4247ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 42482fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4249273d9f13SBarry Smith @*/ 425069b1f4b7SBarry 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) 4251273d9f13SBarry Smith { 42526849ba73SBarry Smith PetscErrorCode ierr; 4253b1d57f15SBarry Smith PetscMPIInt size; 4254273d9f13SBarry Smith 4255273d9f13SBarry Smith PetscFunctionBegin; 4256f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4257f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4258273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4259273d9f13SBarry Smith if (size > 1) { 4260273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4261273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4262273d9f13SBarry Smith } else { 4263273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4264273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4265273d9f13SBarry Smith } 4266273d9f13SBarry Smith PetscFunctionReturn(0); 4267273d9f13SBarry Smith } 4268195d93cdSBarry Smith 42694a2ae208SSatish Balay #undef __FUNCT__ 42704a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 42719230625dSJed Brown PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[]) 4272195d93cdSBarry Smith { 4273195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4274b1d57f15SBarry Smith 4275195d93cdSBarry Smith PetscFunctionBegin; 4276195d93cdSBarry Smith *Ad = a->A; 4277195d93cdSBarry Smith *Ao = a->B; 4278195d93cdSBarry Smith *colmap = a->garray; 4279195d93cdSBarry Smith PetscFunctionReturn(0); 4280195d93cdSBarry Smith } 4281a2243be0SBarry Smith 4282a2243be0SBarry Smith #undef __FUNCT__ 4283a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4284dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4285a2243be0SBarry Smith { 4286dfbe8321SBarry Smith PetscErrorCode ierr; 4287b1d57f15SBarry Smith PetscInt i; 4288a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4289a2243be0SBarry Smith 4290a2243be0SBarry Smith PetscFunctionBegin; 42918ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 429208b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4293a2243be0SBarry Smith ISColoring ocoloring; 4294a2243be0SBarry Smith 4295a2243be0SBarry Smith /* set coloring for diagonal portion */ 4296a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4297a2243be0SBarry Smith 4298a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4299ce94432eSBarry Smith ierr = ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);CHKERRQ(ierr); 4300785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr); 4301d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4302a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4303a2243be0SBarry Smith } 4304a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4305d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4306a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43076bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4308a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 430908b6dcc0SBarry Smith ISColoringValue *colors; 4310b1d57f15SBarry Smith PetscInt *larray; 4311a2243be0SBarry Smith ISColoring ocoloring; 4312a2243be0SBarry Smith 4313a2243be0SBarry Smith /* set coloring for diagonal portion */ 4314785e854fSJed Brown ierr = PetscMalloc1((a->A->cmap->n+1),&larray);CHKERRQ(ierr); 4315d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4316d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4317a2243be0SBarry Smith } 43180298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);CHKERRQ(ierr); 4319785e854fSJed Brown ierr = PetscMalloc1((a->A->cmap->n+1),&colors);CHKERRQ(ierr); 4320d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4321a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4322a2243be0SBarry Smith } 4323a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4324d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4325a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 43266bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4327a2243be0SBarry Smith 4328a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4329785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&larray);CHKERRQ(ierr); 43300298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);CHKERRQ(ierr); 4331785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr); 4332d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4333a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4334a2243be0SBarry Smith } 4335a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4336d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4337a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43386bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 43396bf464f9SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4340a2243be0SBarry Smith PetscFunctionReturn(0); 4341a2243be0SBarry Smith } 4342a2243be0SBarry Smith 4343779c1a83SBarry Smith #undef __FUNCT__ 4344779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4345b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4346779c1a83SBarry Smith { 4347779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4348dfbe8321SBarry Smith PetscErrorCode ierr; 4349779c1a83SBarry Smith 4350779c1a83SBarry Smith PetscFunctionBegin; 4351779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4352779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4353a2243be0SBarry Smith PetscFunctionReturn(0); 4354a2243be0SBarry Smith } 4355c5d6d63eSBarry Smith 4356c5d6d63eSBarry Smith #undef __FUNCT__ 435790431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic" 435890431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat) 43599b8102ccSHong Zhang { 43609b8102ccSHong Zhang PetscErrorCode ierr; 4361a2f3521dSMark F. Adams PetscInt m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs; 43629b8102ccSHong Zhang PetscInt *indx; 43639b8102ccSHong Zhang 43649b8102ccSHong Zhang PetscFunctionBegin; 43659b8102ccSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 43669b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4367a2f3521dSMark F. Adams ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr); 43689b8102ccSHong Zhang if (n == PETSC_DECIDE) { 43699b8102ccSHong Zhang ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 43709b8102ccSHong Zhang } 4371a22543b6SHong Zhang /* Check sum(n) = N */ 4372a95133b1SBarry Smith ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4373a22543b6SHong Zhang if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N); 4374a22543b6SHong Zhang 43759b8102ccSHong Zhang ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 43769b8102ccSHong Zhang rstart -= m; 43779b8102ccSHong Zhang 43789b8102ccSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 43799b8102ccSHong Zhang for (i=0; i<m; i++) { 43800298fd71SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 43819b8102ccSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 43820298fd71SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 43839b8102ccSHong Zhang } 43849b8102ccSHong Zhang 43859b8102ccSHong Zhang ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 43869b8102ccSHong Zhang ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4387a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr); 43889b8102ccSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 43899b8102ccSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 43909b8102ccSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 43919b8102ccSHong Zhang PetscFunctionReturn(0); 43929b8102ccSHong Zhang } 43939b8102ccSHong Zhang 43949b8102ccSHong Zhang #undef __FUNCT__ 439590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric" 439690431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat) 43979b8102ccSHong Zhang { 43989b8102ccSHong Zhang PetscErrorCode ierr; 43999b8102ccSHong Zhang PetscInt m,N,i,rstart,nnz,Ii; 44009b8102ccSHong Zhang PetscInt *indx; 44019b8102ccSHong Zhang PetscScalar *values; 44029b8102ccSHong Zhang 44039b8102ccSHong Zhang PetscFunctionBegin; 44049b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 44050298fd71SBarry Smith ierr = MatGetOwnershipRange(outmat,&rstart,NULL);CHKERRQ(ierr); 44069b8102ccSHong Zhang for (i=0; i<m; i++) { 44079b8102ccSHong Zhang ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44089b8102ccSHong Zhang Ii = i + rstart; 44093c79b8e7SHong Zhang ierr = MatSetValues(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 44109b8102ccSHong Zhang ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44119b8102ccSHong Zhang } 44129b8102ccSHong Zhang ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44139b8102ccSHong Zhang ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44149b8102ccSHong Zhang PetscFunctionReturn(0); 44159b8102ccSHong Zhang } 44169b8102ccSHong Zhang 44179b8102ccSHong Zhang #undef __FUNCT__ 441890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ" 4419bc08b0f1SBarry Smith /*@ 442090431a8fSHong Zhang MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential 442151dd7536SBarry Smith matrices from each processor 4422c5d6d63eSBarry Smith 4423c5d6d63eSBarry Smith Collective on MPI_Comm 4424c5d6d63eSBarry Smith 4425c5d6d63eSBarry Smith Input Parameters: 442651dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4427d6bb3c2dSHong Zhang . inmat - the input sequential matrices 44280e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4429d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 443051dd7536SBarry Smith 443151dd7536SBarry Smith Output Parameter: 443251dd7536SBarry Smith . outmat - the parallel matrix generated 4433c5d6d63eSBarry Smith 44347e25d530SSatish Balay Level: advanced 44357e25d530SSatish Balay 4436f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4437c5d6d63eSBarry Smith 4438c5d6d63eSBarry Smith @*/ 443990431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4440c5d6d63eSBarry Smith { 4441dfbe8321SBarry Smith PetscErrorCode ierr; 4442f4703a44SHong Zhang PetscMPIInt size; 4443c5d6d63eSBarry Smith 4444c5d6d63eSBarry Smith PetscFunctionBegin; 4445f4703a44SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 44469b8102ccSHong Zhang ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4447f4703a44SHong Zhang if (size == 1) { 4448f4703a44SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 4449f4703a44SHong Zhang ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr); 4450f4703a44SHong Zhang } else { 4451f4703a44SHong Zhang ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 4452f4703a44SHong Zhang } 4453f4703a44SHong Zhang } else { 4454d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 445590431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr); 44560e36024fSHong Zhang } 445790431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr); 4458f4703a44SHong Zhang } 44599b8102ccSHong Zhang ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4460c5d6d63eSBarry Smith PetscFunctionReturn(0); 4461c5d6d63eSBarry Smith } 4462c5d6d63eSBarry Smith 4463c5d6d63eSBarry Smith #undef __FUNCT__ 4464c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4465dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4466c5d6d63eSBarry Smith { 4467dfbe8321SBarry Smith PetscErrorCode ierr; 446832dcc486SBarry Smith PetscMPIInt rank; 4469b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4470de4209c5SBarry Smith size_t len; 4471b1d57f15SBarry Smith const PetscInt *indx; 4472c5d6d63eSBarry Smith PetscViewer out; 4473c5d6d63eSBarry Smith char *name; 4474c5d6d63eSBarry Smith Mat B; 4475b3cc6726SBarry Smith const PetscScalar *values; 4476c5d6d63eSBarry Smith 4477c5d6d63eSBarry Smith PetscFunctionBegin; 4478c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4479c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4480f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4481f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4482f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 448333d57670SJed Brown ierr = MatSetBlockSizesFromMats(B,A,A);CHKERRQ(ierr); 4484f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 44850298fd71SBarry Smith ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr); 4486c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4487c5d6d63eSBarry Smith for (i=0; i<m; i++) { 4488c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4489c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4490c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4491c5d6d63eSBarry Smith } 4492c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4493c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4494c5d6d63eSBarry Smith 4495ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr); 4496c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4497785e854fSJed Brown ierr = PetscMalloc1((len+5),&name);CHKERRQ(ierr); 4498c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4499852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4500a2ea699eSBarry Smith ierr = PetscFree(name);CHKERRQ(ierr); 4501c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 45026bf464f9SBarry Smith ierr = PetscViewerDestroy(&out);CHKERRQ(ierr); 45036bf464f9SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 4504c5d6d63eSBarry Smith PetscFunctionReturn(0); 4505c5d6d63eSBarry Smith } 4506e5f2cdd8SHong Zhang 450709573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 450851a7d1a8SHong Zhang #undef __FUNCT__ 450951a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 45107087cfbeSBarry Smith PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 451151a7d1a8SHong Zhang { 451251a7d1a8SHong Zhang PetscErrorCode ierr; 4513671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4514776b82aeSLisandro Dalcin PetscContainer container; 451551a7d1a8SHong Zhang 451651a7d1a8SHong Zhang PetscFunctionBegin; 4517671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4518671beff6SHong Zhang if (container) { 4519776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 452051a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 45213e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 45223e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 452351a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 452451a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4525533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 452602c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4527533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 452802c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 452905b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 453005b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 453105b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 45326bf464f9SBarry Smith ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr); 4533bf0cc555SLisandro Dalcin ierr = PetscFree(merge);CHKERRQ(ierr); 4534671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4535671beff6SHong Zhang } 453651a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 453751a7d1a8SHong Zhang PetscFunctionReturn(0); 453851a7d1a8SHong Zhang } 453951a7d1a8SHong Zhang 4540c6db04a5SJed Brown #include <../src/mat/utils/freespace.h> 4541c6db04a5SJed Brown #include <petscbt.h> 45424ebed01fSBarry Smith 4543e5f2cdd8SHong Zhang #undef __FUNCT__ 454490431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric" 454590431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat) 454655d1abb9SHong Zhang { 454755d1abb9SHong Zhang PetscErrorCode ierr; 4548ce94432eSBarry Smith MPI_Comm comm; 454955d1abb9SHong Zhang Mat_SeqAIJ *a =(Mat_SeqAIJ*)seqmat->data; 4550b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4551a2ea699eSBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj; 4552b1d57f15SBarry Smith PetscInt proc,m; 4553b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4554b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4555b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 455655d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 455755d1abb9SHong Zhang MPI_Status *status; 4558a77337e4SBarry Smith MatScalar *aa=a->a; 4559dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 456055d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4561776b82aeSLisandro Dalcin PetscContainer container; 456255d1abb9SHong Zhang 456355d1abb9SHong Zhang PetscFunctionBegin; 4564bedda5b1SHong Zhang ierr = PetscObjectGetComm((PetscObject)mpimat,&comm);CHKERRQ(ierr); 45654ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 45663c2c1871SHong Zhang 456755d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 456855d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 456955d1abb9SHong Zhang 457055d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4571776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 4572bf0cc555SLisandro Dalcin 457355d1abb9SHong Zhang bi = merge->bi; 457455d1abb9SHong Zhang bj = merge->bj; 457555d1abb9SHong Zhang buf_ri = merge->buf_ri; 457655d1abb9SHong Zhang buf_rj = merge->buf_rj; 457755d1abb9SHong Zhang 4578785e854fSJed Brown ierr = PetscMalloc1(size,&status);CHKERRQ(ierr); 45797a2fc3feSBarry Smith owners = merge->rowmap->range; 458055d1abb9SHong Zhang len_s = merge->len_s; 458155d1abb9SHong Zhang 458255d1abb9SHong Zhang /* send and recv matrix values */ 458355d1abb9SHong Zhang /*-----------------------------*/ 4584357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 458555d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 458655d1abb9SHong Zhang 4587785e854fSJed Brown ierr = PetscMalloc1((merge->nsend+1),&s_waits);CHKERRQ(ierr); 458855d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++) { 458955d1abb9SHong Zhang if (!len_s[proc]) continue; 459055d1abb9SHong Zhang i = owners[proc]; 459155d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 459255d1abb9SHong Zhang k++; 459355d1abb9SHong Zhang } 459455d1abb9SHong Zhang 45950c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 45960c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 459755d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 459855d1abb9SHong Zhang 459955d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 460055d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 460155d1abb9SHong Zhang 460255d1abb9SHong Zhang /* insert mat values of mpimat */ 460355d1abb9SHong Zhang /*----------------------------*/ 4604785e854fSJed Brown ierr = PetscMalloc1(N,&ba_i);CHKERRQ(ierr); 4605dcca6d9dSJed Brown ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr); 460655d1abb9SHong Zhang 460755d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { 460855d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 460955d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 461055d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 461155d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 461255d1abb9SHong Zhang } 461355d1abb9SHong Zhang 461455d1abb9SHong Zhang /* set values of ba */ 46157a2fc3feSBarry Smith m = merge->rowmap->n; 461655d1abb9SHong Zhang for (i=0; i<m; i++) { 461755d1abb9SHong Zhang arow = owners[rank] + i; 461855d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 461955d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4620a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 462155d1abb9SHong Zhang 462255d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 462355d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 462455d1abb9SHong Zhang aj = a->j + ai[arow]; 462555d1abb9SHong Zhang aa = a->a + ai[arow]; 462655d1abb9SHong Zhang nextaj = 0; 462755d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 462855d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 462955d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 463055d1abb9SHong Zhang } 463155d1abb9SHong Zhang } 463255d1abb9SHong Zhang 463355d1abb9SHong Zhang /* add received vals into ba */ 463455d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 463555d1abb9SHong Zhang /* i-th row */ 463655d1abb9SHong Zhang if (i == *nextrow[k]) { 463755d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 463855d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 463955d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 464055d1abb9SHong Zhang nextaj = 0; 464155d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 464255d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 464355d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 464455d1abb9SHong Zhang } 464555d1abb9SHong Zhang } 464655d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 464755d1abb9SHong Zhang } 464855d1abb9SHong Zhang } 464955d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 465055d1abb9SHong Zhang } 465155d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 465255d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 465355d1abb9SHong Zhang 4654533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 465555d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 465655d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 46571d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 46584ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 465955d1abb9SHong Zhang PetscFunctionReturn(0); 466055d1abb9SHong Zhang } 466138f152feSBarry Smith 46626bc0bbbfSBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat); 46636bc0bbbfSBarry Smith 466438f152feSBarry Smith #undef __FUNCT__ 466590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic" 466690431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4667e5f2cdd8SHong Zhang { 4668f08fae4eSHong Zhang PetscErrorCode ierr; 466955a3bba9SHong Zhang Mat B_mpi; 4670c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4671b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4672b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4673d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4674a2f3521dSMark F. Adams PetscInt len,proc,*dnz,*onz,bs,cbs; 4675b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4676b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 467755d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 467858cb9c82SHong Zhang MPI_Status *status; 46790298fd71SBarry Smith PetscFreeSpaceList free_space=NULL,current_space=NULL; 4680be0fcf8dSHong Zhang PetscBT lnkbt; 468151a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4682776b82aeSLisandro Dalcin PetscContainer container; 468302c68681SHong Zhang 4684e5f2cdd8SHong Zhang PetscFunctionBegin; 46854ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 46863c2c1871SHong Zhang 468738f152feSBarry Smith /* make sure it is a PETSc comm */ 46880298fd71SBarry Smith ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr); 4689e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4690e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 469155d1abb9SHong Zhang 4692b00a9115SJed Brown ierr = PetscNew(&merge);CHKERRQ(ierr); 4693785e854fSJed Brown ierr = PetscMalloc1(size,&status);CHKERRQ(ierr); 4694e5f2cdd8SHong Zhang 46956abd8857SHong Zhang /* determine row ownership */ 4696f08fae4eSHong Zhang /*---------------------------------------------------------*/ 469726283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 469826283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 469926283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 470026283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 470126283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4702785e854fSJed Brown ierr = PetscMalloc1(size,&len_si);CHKERRQ(ierr); 4703785e854fSJed Brown ierr = PetscMalloc1(size,&merge->len_s);CHKERRQ(ierr); 470455d1abb9SHong Zhang 47057a2fc3feSBarry Smith m = merge->rowmap->n; 47067a2fc3feSBarry Smith owners = merge->rowmap->range; 47076abd8857SHong Zhang 47086abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 47096abd8857SHong Zhang /*---------------------------------------------------------*/ 47103e06a4e6SHong Zhang len_s = merge->len_s; 471151a7d1a8SHong Zhang 47122257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4713c2234fe3SHong Zhang merge->nsend = 0; 4714409913e3SHong Zhang for (proc=0; proc<size; proc++) { 47152257cef7SHong Zhang len_si[proc] = 0; 47163e06a4e6SHong Zhang if (proc == rank) { 47176abd8857SHong Zhang len_s[proc] = 0; 47183e06a4e6SHong Zhang } else { 471902c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 47203e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 47213e06a4e6SHong Zhang } 47223e06a4e6SHong Zhang if (len_s[proc]) { 4723c2234fe3SHong Zhang merge->nsend++; 47242257cef7SHong Zhang nrows = 0; 47252257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 47262257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 47272257cef7SHong Zhang } 47282257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 47292257cef7SHong Zhang len += len_si[proc]; 4730409913e3SHong Zhang } 473158cb9c82SHong Zhang } 4732409913e3SHong Zhang 47332257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 47342257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 47350298fd71SBarry Smith ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 473655d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4737671beff6SHong Zhang 47383e06a4e6SHong Zhang /* post the Irecv of j-structure */ 47393e06a4e6SHong Zhang /*-------------------------------*/ 47402c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 47413e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 474202c68681SHong Zhang 47433e06a4e6SHong Zhang /* post the Isend of j-structure */ 4744affca5deSHong Zhang /*--------------------------------*/ 4745dcca6d9dSJed Brown ierr = PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);CHKERRQ(ierr); 47463e06a4e6SHong Zhang 47472257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++) { 4748409913e3SHong Zhang if (!len_s[proc]) continue; 474902c68681SHong Zhang i = owners[proc]; 4750b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 475151a7d1a8SHong Zhang k++; 475251a7d1a8SHong Zhang } 475351a7d1a8SHong Zhang 47543e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 47553e06a4e6SHong Zhang /*------------------------------------------------*/ 47560c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 47570c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 475802c68681SHong Zhang 475902c68681SHong Zhang /* send and recv i-structure */ 476002c68681SHong Zhang /*---------------------------*/ 47612c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 476202c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 476302c68681SHong Zhang 4764785e854fSJed Brown ierr = PetscMalloc1((len+1),&buf_s);CHKERRQ(ierr); 47653e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 47662257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++) { 476702c68681SHong Zhang if (!len_s[proc]) continue; 47683e06a4e6SHong Zhang /* form outgoing message for i-structure: 47693e06a4e6SHong Zhang buf_si[0]: nrows to be sent 47703e06a4e6SHong Zhang [1:nrows]: row index (global) 47713e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 47723e06a4e6SHong Zhang */ 47733e06a4e6SHong Zhang /*-------------------------------------------*/ 47742257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 47753e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 47763e06a4e6SHong Zhang buf_si[0] = nrows; 47773e06a4e6SHong Zhang buf_si_i[0] = 0; 47783e06a4e6SHong Zhang nrows = 0; 47793e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 47803e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 47813e06a4e6SHong Zhang if (anzi) { 47823e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 47833e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 47843e06a4e6SHong Zhang nrows++; 47853e06a4e6SHong Zhang } 47863e06a4e6SHong Zhang } 4787b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 478802c68681SHong Zhang k++; 47892257cef7SHong Zhang buf_si += len_si[proc]; 479002c68681SHong Zhang } 47912257cef7SHong Zhang 47920c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 47930c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 479402c68681SHong Zhang 4795ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 47963e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++) { 4797ae15b995SBarry 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); 47983e06a4e6SHong Zhang } 47993e06a4e6SHong Zhang 48003e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 480102c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 480202c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 48031d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 48042257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 48053e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4806bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 480758cb9c82SHong Zhang 4808bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4809bcc1bcd5SHong Zhang /*----------------------------------------------*/ 481058cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4811785e854fSJed Brown ierr = PetscMalloc1((m+1),&bi);CHKERRQ(ierr); 481258cb9c82SHong Zhang bi[0] = 0; 481358cb9c82SHong Zhang 4814be0fcf8dSHong Zhang /* create and initialize a linked list */ 4815be0fcf8dSHong Zhang nlnk = N+1; 4816be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 481758cb9c82SHong Zhang 4818bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 4819bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4820a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 48212205254eSKarl Rupp 482258cb9c82SHong Zhang current_space = free_space; 482358cb9c82SHong Zhang 4824bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 4825dcca6d9dSJed Brown ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr); 48261d79065fSBarry Smith 48273e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++) { 48282257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 48293e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 48303e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 48312257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 48323e06a4e6SHong Zhang } 48332257cef7SHong Zhang 4834bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4835bcc1bcd5SHong Zhang len = 0; 483658cb9c82SHong Zhang for (i=0; i<m; i++) { 483758cb9c82SHong Zhang bnzi = 0; 483858cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 483958cb9c82SHong Zhang arow = owners[rank] + i; 484058cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 484158cb9c82SHong Zhang aj = a->j + ai[arow]; 4842dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 484358cb9c82SHong Zhang bnzi += nlnk; 484458cb9c82SHong Zhang /* add received col data into lnk */ 484551a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 484655d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 48473e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 48483e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 4849dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 48503e06a4e6SHong Zhang bnzi += nlnk; 48513e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 48523e06a4e6SHong Zhang } 485358cb9c82SHong Zhang } 4854bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 485558cb9c82SHong Zhang 485658cb9c82SHong Zhang /* if free space is not available, make more free space */ 485758cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 48584238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 485958cb9c82SHong Zhang nspacedouble++; 486058cb9c82SHong Zhang } 486158cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4862be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4863bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4864bcc1bcd5SHong Zhang 486558cb9c82SHong Zhang current_space->array += bnzi; 486658cb9c82SHong Zhang current_space->local_used += bnzi; 486758cb9c82SHong Zhang current_space->local_remaining -= bnzi; 486858cb9c82SHong Zhang 486958cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 487058cb9c82SHong Zhang } 4871bcc1bcd5SHong Zhang 48721d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4873bcc1bcd5SHong Zhang 4874785e854fSJed Brown ierr = PetscMalloc1((bi[m]+1),&bj);CHKERRQ(ierr); 4875a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4876be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4877409913e3SHong Zhang 4878bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4879bcc1bcd5SHong Zhang /*---------------------------------------*/ 4880a2f3521dSMark F. Adams ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr); 4881f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 488254b84b50SHong Zhang if (n==PETSC_DECIDE) { 4883f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 488454b84b50SHong Zhang } else { 4885f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 488654b84b50SHong Zhang } 4887a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr); 4888bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4889bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4890bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 48917e63b356SHong Zhang ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 489258cb9c82SHong Zhang 489390431a8fSHong Zhang /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */ 48946abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4895affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4896affca5deSHong Zhang merge->bi = bi; 4897affca5deSHong Zhang merge->bj = bj; 489802c68681SHong Zhang merge->buf_ri = buf_ri; 489902c68681SHong Zhang merge->buf_rj = buf_rj; 49000298fd71SBarry Smith merge->coi = NULL; 49010298fd71SBarry Smith merge->coj = NULL; 49020298fd71SBarry Smith merge->owners_co = NULL; 4903affca5deSHong Zhang 4904bf0cc555SLisandro Dalcin ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 4905bf0cc555SLisandro Dalcin 4906affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4907776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4908776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4909affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4910bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 4911affca5deSHong Zhang *mpimat = B_mpi; 491238f152feSBarry Smith 49134ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4914e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4915e5f2cdd8SHong Zhang } 491625616d81SHong Zhang 491738f152feSBarry Smith #undef __FUNCT__ 491890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ" 4919d4036a1aSHong Zhang /*@C 492090431a8fSHong Zhang MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential 4921d4036a1aSHong Zhang matrices from each processor 4922d4036a1aSHong Zhang 4923d4036a1aSHong Zhang Collective on MPI_Comm 4924d4036a1aSHong Zhang 4925d4036a1aSHong Zhang Input Parameters: 4926d4036a1aSHong Zhang + comm - the communicators the parallel matrix will live on 4927d4036a1aSHong Zhang . seqmat - the input sequential matrices 4928d4036a1aSHong Zhang . m - number of local rows (or PETSC_DECIDE) 4929d4036a1aSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4930d4036a1aSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4931d4036a1aSHong Zhang 4932d4036a1aSHong Zhang Output Parameter: 4933d4036a1aSHong Zhang . mpimat - the parallel matrix generated 4934d4036a1aSHong Zhang 4935d4036a1aSHong Zhang Level: advanced 4936d4036a1aSHong Zhang 4937d4036a1aSHong Zhang Notes: 4938d4036a1aSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4939d4036a1aSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4940d4036a1aSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4941d4036a1aSHong Zhang @*/ 494290431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 494355d1abb9SHong Zhang { 494455d1abb9SHong Zhang PetscErrorCode ierr; 49457e63b356SHong Zhang PetscMPIInt size; 494655d1abb9SHong Zhang 494755d1abb9SHong Zhang PetscFunctionBegin; 49487e63b356SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 49497e63b356SHong Zhang if (size == 1) { 49507e63b356SHong Zhang ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49517e63b356SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 49527e63b356SHong Zhang ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr); 49537e63b356SHong Zhang } else { 49547e63b356SHong Zhang ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 49557e63b356SHong Zhang } 49567e63b356SHong Zhang ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49577e63b356SHong Zhang PetscFunctionReturn(0); 49587e63b356SHong Zhang } 49594ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 496055d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 496190431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 496255d1abb9SHong Zhang } 496390431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr); 49644ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 496555d1abb9SHong Zhang PetscFunctionReturn(0); 496655d1abb9SHong Zhang } 49674ebed01fSBarry Smith 496825616d81SHong Zhang #undef __FUNCT__ 49694a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat" 4970bc08b0f1SBarry Smith /*@ 49714a2b5492SBarry Smith MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with 49728661ff28SBarry Smith mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained 49738661ff28SBarry Smith with MatGetSize() 497425616d81SHong Zhang 497532fba14fSHong Zhang Not Collective 497625616d81SHong Zhang 497725616d81SHong Zhang Input Parameters: 497825616d81SHong Zhang + A - the matrix 497925616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 498025616d81SHong Zhang 498125616d81SHong Zhang Output Parameter: 498225616d81SHong Zhang . A_loc - the local sequential matrix generated 498325616d81SHong Zhang 498425616d81SHong Zhang Level: developer 498525616d81SHong Zhang 4986ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed() 49878661ff28SBarry Smith 498825616d81SHong Zhang @*/ 49894a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 499025616d81SHong Zhang { 499125616d81SHong Zhang PetscErrorCode ierr; 499201b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 4993b78526a6SJose E. Roman Mat_SeqAIJ *mat,*a,*b; 4994b78526a6SJose E. Roman PetscInt *ai,*aj,*bi,*bj,*cmap=mpimat->garray; 4995b78526a6SJose E. Roman MatScalar *aa,*ba,*cam; 4996a77337e4SBarry Smith PetscScalar *ca; 4997d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 49985a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 49998661ff28SBarry Smith PetscBool match; 500025616d81SHong Zhang 500125616d81SHong Zhang PetscFunctionBegin; 5002251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 5003ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 50044ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 5005b78526a6SJose E. Roman a = (Mat_SeqAIJ*)(mpimat->A)->data; 5006b78526a6SJose E. Roman b = (Mat_SeqAIJ*)(mpimat->B)->data; 5007b78526a6SJose E. Roman ai = a->i; aj = a->j; bi = b->i; bj = b->j; 5008b78526a6SJose E. Roman aa = a->a; ba = b->a; 500901b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5010785e854fSJed Brown ierr = PetscMalloc1((1+am),&ci);CHKERRQ(ierr); 5011dea91ad1SHong Zhang ci[0] = 0; 501201b7ae99SHong Zhang for (i=0; i<am; i++) { 5013dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 501401b7ae99SHong Zhang } 5015785e854fSJed Brown ierr = PetscMalloc1((1+ci[am]),&cj);CHKERRQ(ierr); 5016785e854fSJed Brown ierr = PetscMalloc1((1+ci[am]),&ca);CHKERRQ(ierr); 5017dea91ad1SHong Zhang k = 0; 501801b7ae99SHong Zhang for (i=0; i<am; i++) { 50195a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50205a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 502101b7ae99SHong Zhang /* off-diagonal portion of A */ 50225a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50235a7d977cSHong Zhang col = cmap[*bj]; 50245a7d977cSHong Zhang if (col >= cstart) break; 50255a7d977cSHong Zhang cj[k] = col; bj++; 50265a7d977cSHong Zhang ca[k++] = *ba++; 50275a7d977cSHong Zhang } 50285a7d977cSHong Zhang /* diagonal portion of A */ 50295a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 50305a7d977cSHong Zhang cj[k] = cstart + *aj++; 50315a7d977cSHong Zhang ca[k++] = *aa++; 50325a7d977cSHong Zhang } 50335a7d977cSHong Zhang /* off-diagonal portion of A */ 50345a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 50355a7d977cSHong Zhang cj[k] = cmap[*bj++]; 50365a7d977cSHong Zhang ca[k++] = *ba++; 50375a7d977cSHong Zhang } 503825616d81SHong Zhang } 5039dea91ad1SHong Zhang /* put together the new matrix */ 5040d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 5041dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5042dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 5043dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 5044e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 5045e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 5046dea91ad1SHong Zhang mat->nonew = 0; 50475a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 50485a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 5049a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 50505a7d977cSHong Zhang for (i=0; i<am; i++) { 50515a7d977cSHong Zhang /* off-diagonal portion of A */ 50525a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50535a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50545a7d977cSHong Zhang col = cmap[*bj]; 50555a7d977cSHong Zhang if (col >= cstart) break; 5056a77337e4SBarry Smith *cam++ = *ba++; bj++; 50575a7d977cSHong Zhang } 50585a7d977cSHong Zhang /* diagonal portion of A */ 5059ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 5060a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 50615a7d977cSHong Zhang /* off-diagonal portion of A */ 5062f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 5063a77337e4SBarry Smith *cam++ = *ba++; bj++; 5064f33d1a9aSHong Zhang } 50655a7d977cSHong Zhang } 50668661ff28SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 50674ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 506825616d81SHong Zhang PetscFunctionReturn(0); 506925616d81SHong Zhang } 507025616d81SHong Zhang 507132fba14fSHong Zhang #undef __FUNCT__ 50724a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed" 507332fba14fSHong Zhang /*@C 5074ba264940SBarry Smith MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns 507532fba14fSHong Zhang 507632fba14fSHong Zhang Not Collective 507732fba14fSHong Zhang 507832fba14fSHong Zhang Input Parameters: 507932fba14fSHong Zhang + A - the matrix 508032fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 50810298fd71SBarry Smith - row, col - index sets of rows and columns to extract (or NULL) 508232fba14fSHong Zhang 508332fba14fSHong Zhang Output Parameter: 508432fba14fSHong Zhang . A_loc - the local sequential matrix generated 508532fba14fSHong Zhang 508632fba14fSHong Zhang Level: developer 508732fba14fSHong Zhang 5088ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat() 5089ba264940SBarry Smith 509032fba14fSHong Zhang @*/ 50914a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 509232fba14fSHong Zhang { 509332fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 509432fba14fSHong Zhang PetscErrorCode ierr; 509532fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 509632fba14fSHong Zhang IS isrowa,iscola; 509732fba14fSHong Zhang Mat *aloc; 50984a2b5492SBarry Smith PetscBool match; 509932fba14fSHong Zhang 510032fba14fSHong Zhang PetscFunctionBegin; 5101251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 5102ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 51034ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 510432fba14fSHong Zhang if (!row) { 5105d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 510632fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 510732fba14fSHong Zhang } else { 510832fba14fSHong Zhang isrowa = *row; 510932fba14fSHong Zhang } 511032fba14fSHong Zhang if (!col) { 5111d0f46423SBarry Smith start = A->cmap->rstart; 511232fba14fSHong Zhang cmap = a->garray; 5113d0f46423SBarry Smith nzA = a->A->cmap->n; 5114d0f46423SBarry Smith nzB = a->B->cmap->n; 5115785e854fSJed Brown ierr = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr); 511632fba14fSHong Zhang ncols = 0; 511732fba14fSHong Zhang for (i=0; i<nzB; i++) { 511832fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 511932fba14fSHong Zhang else break; 512032fba14fSHong Zhang } 512132fba14fSHong Zhang imark = i; 512232fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 512332fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 5124d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 512532fba14fSHong Zhang } else { 512632fba14fSHong Zhang iscola = *col; 512732fba14fSHong Zhang } 512832fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX) { 512932fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 513032fba14fSHong Zhang aloc[0] = *A_loc; 513132fba14fSHong Zhang } 513232fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 513332fba14fSHong Zhang *A_loc = aloc[0]; 513432fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 513532fba14fSHong Zhang if (!row) { 51366bf464f9SBarry Smith ierr = ISDestroy(&isrowa);CHKERRQ(ierr); 513732fba14fSHong Zhang } 513832fba14fSHong Zhang if (!col) { 51396bf464f9SBarry Smith ierr = ISDestroy(&iscola);CHKERRQ(ierr); 514032fba14fSHong Zhang } 51414ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 514232fba14fSHong Zhang PetscFunctionReturn(0); 514332fba14fSHong Zhang } 514432fba14fSHong Zhang 514525616d81SHong Zhang #undef __FUNCT__ 514625616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 514725616d81SHong Zhang /*@C 514832fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 514925616d81SHong Zhang 515025616d81SHong Zhang Collective on Mat 515125616d81SHong Zhang 515225616d81SHong Zhang Input Parameters: 5153e240928fSHong Zhang + A,B - the matrices in mpiaij format 515425616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 51550298fd71SBarry Smith - rowb, colb - index sets of rows and columns of B to extract (or NULL) 515625616d81SHong Zhang 515725616d81SHong Zhang Output Parameter: 515825616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 515925616d81SHong Zhang - B_seq - the sequential matrix generated 516025616d81SHong Zhang 516125616d81SHong Zhang Level: developer 516225616d81SHong Zhang 516325616d81SHong Zhang @*/ 516466bfb163SHong Zhang PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq) 516525616d81SHong Zhang { 5166899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 516725616d81SHong Zhang PetscErrorCode ierr; 5168b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 516925616d81SHong Zhang IS isrowb,iscolb; 51700298fd71SBarry Smith Mat *bseq=NULL; 517125616d81SHong Zhang 517225616d81SHong Zhang PetscFunctionBegin; 5173d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5174e32f2f54SBarry 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); 517525616d81SHong Zhang } 51764ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 517725616d81SHong Zhang 517825616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5179d0f46423SBarry Smith start = A->cmap->rstart; 518025616d81SHong Zhang cmap = a->garray; 5181d0f46423SBarry Smith nzA = a->A->cmap->n; 5182d0f46423SBarry Smith nzB = a->B->cmap->n; 5183785e854fSJed Brown ierr = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr); 518425616d81SHong Zhang ncols = 0; 51850390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 518625616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 518725616d81SHong Zhang else break; 518825616d81SHong Zhang } 518925616d81SHong Zhang imark = i; 51900390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 51910390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 5192d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 5193d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 519425616d81SHong Zhang } else { 5195e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 519625616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 519725616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 519825616d81SHong Zhang bseq[0] = *B_seq; 519925616d81SHong Zhang } 520025616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 520125616d81SHong Zhang *B_seq = bseq[0]; 520225616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 520325616d81SHong Zhang if (!rowb) { 52046bf464f9SBarry Smith ierr = ISDestroy(&isrowb);CHKERRQ(ierr); 520525616d81SHong Zhang } else { 520625616d81SHong Zhang *rowb = isrowb; 520725616d81SHong Zhang } 520825616d81SHong Zhang if (!colb) { 52096bf464f9SBarry Smith ierr = ISDestroy(&iscolb);CHKERRQ(ierr); 521025616d81SHong Zhang } else { 521125616d81SHong Zhang *colb = iscolb; 521225616d81SHong Zhang } 52134ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 521425616d81SHong Zhang PetscFunctionReturn(0); 521525616d81SHong Zhang } 5216429d309bSHong Zhang 5217a61c8c0fSHong Zhang #undef __FUNCT__ 5218f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ" 5219f8487c73SHong Zhang /* 5220f8487c73SHong Zhang MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 522101b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 5222429d309bSHong Zhang 5223429d309bSHong Zhang Collective on Mat 5224429d309bSHong Zhang 5225429d309bSHong Zhang Input Parameters: 5226429d309bSHong Zhang + A,B - the matrices in mpiaij format 5227598bc09dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 5228429d309bSHong Zhang 5229429d309bSHong Zhang Output Parameter: 52300298fd71SBarry Smith + startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL) 52310298fd71SBarry Smith . startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL) 52320298fd71SBarry Smith . bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL) 5233598bc09dSHong Zhang - B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N 5234429d309bSHong Zhang 5235429d309bSHong Zhang Level: developer 5236429d309bSHong Zhang 5237f8487c73SHong Zhang */ 5238b7f45c76SHong Zhang PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 5239429d309bSHong Zhang { 5240a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 5241429d309bSHong Zhang PetscErrorCode ierr; 5242899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 524387025532SHong Zhang Mat_SeqAIJ *b_oth; 5244a6b2eed2SHong Zhang VecScatter ctx =a->Mvctx; 5245ce94432eSBarry Smith MPI_Comm comm; 52467adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 5247d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 5248dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 5249dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 5250e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 52510298fd71SBarry Smith MPI_Request *rwaits = NULL,*swaits = NULL; 525287025532SHong Zhang MPI_Status *sstatus,rstatus; 5253aa5bb8c0SSatish Balay PetscMPIInt jj; 5254e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 5255ba8c8a56SBarry Smith PetscScalar *vals; 5256429d309bSHong Zhang 5257429d309bSHong Zhang PetscFunctionBegin; 5258ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 5259d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5260e32f2f54SBarry 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); 5261429d309bSHong Zhang } 52624ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5263a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5264a6b2eed2SHong Zhang 5265a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5266a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5267e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5268e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5269a6b2eed2SHong Zhang nrecvs = gen_from->n; 5270a6b2eed2SHong Zhang nsends = gen_to->n; 5271d7ee0231SBarry Smith 5272dcca6d9dSJed Brown ierr = PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);CHKERRQ(ierr); 5273a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5274a6b2eed2SHong Zhang sstarts = gen_to->starts; 5275a6b2eed2SHong Zhang sprocs = gen_to->procs; 5276a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5277e42f35eeSHong Zhang sbs = gen_to->bs; 5278e42f35eeSHong Zhang rstarts = gen_from->starts; 5279e42f35eeSHong Zhang rprocs = gen_from->procs; 5280e42f35eeSHong Zhang rbs = gen_from->bs; 5281429d309bSHong Zhang 5282b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5283429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5284a6b2eed2SHong Zhang /* i-array */ 5285a6b2eed2SHong Zhang /*---------*/ 5286a6b2eed2SHong Zhang /* post receives */ 5287a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5288e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5289e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 529087025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5291429d309bSHong Zhang } 5292a6b2eed2SHong Zhang 5293a6b2eed2SHong Zhang /* pack the outgoing message */ 5294dcca6d9dSJed Brown ierr = PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);CHKERRQ(ierr); 52952205254eSKarl Rupp 52962205254eSKarl Rupp sstartsj[0] = 0; 52972205254eSKarl Rupp rstartsj[0] = 0; 5298a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5299a6b2eed2SHong Zhang k = 0; 5300a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5301e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5302e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 530387025532SHong Zhang for (j=0; j<nrows; j++) { 5304d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5305e42f35eeSHong Zhang for (l=0; l<sbs; l++) { 53060298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); /* rowlength */ 53072205254eSKarl Rupp 5308e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 53092205254eSKarl Rupp 5310e42f35eeSHong Zhang len += ncols; 53110298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); 5312e42f35eeSHong Zhang } 5313a6b2eed2SHong Zhang k++; 5314429d309bSHong Zhang } 5315e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 53162205254eSKarl Rupp 5317dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5318429d309bSHong Zhang } 531987025532SHong Zhang /* recvs and sends of i-array are completed */ 532087025532SHong Zhang i = nrecvs; 532187025532SHong Zhang while (i--) { 5322aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 532387025532SHong Zhang } 53240c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5325e42f35eeSHong Zhang 5326a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5327785e854fSJed Brown ierr = PetscMalloc1((len+1),&bufj);CHKERRQ(ierr); 5328785e854fSJed Brown ierr = PetscMalloc1((len+1),&bufa);CHKERRQ(ierr); 5329a6b2eed2SHong Zhang 533087025532SHong Zhang /* create i-array of B_oth */ 5331785e854fSJed Brown ierr = PetscMalloc1((aBn+2),&b_othi);CHKERRQ(ierr); 53322205254eSKarl Rupp 533387025532SHong Zhang b_othi[0] = 0; 5334a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5335a6b2eed2SHong Zhang k = 0; 5336a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5337fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5338e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 533987025532SHong Zhang for (j=0; j<nrows; j++) { 534087025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5341a6b2eed2SHong Zhang len += rowlen[j]; k++; 5342a6b2eed2SHong Zhang } 5343dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5344a6b2eed2SHong Zhang } 5345a6b2eed2SHong Zhang 534687025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 5347785e854fSJed Brown ierr = PetscMalloc1((b_othi[aBn]+1),&b_othj);CHKERRQ(ierr); 5348785e854fSJed Brown ierr = PetscMalloc1((b_othi[aBn]+1),&b_otha);CHKERRQ(ierr); 5349a6b2eed2SHong Zhang 535087025532SHong Zhang /* j-array */ 535187025532SHong Zhang /*---------*/ 5352a6b2eed2SHong Zhang /* post receives of j-array */ 5353a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 535487025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 535587025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5356a6b2eed2SHong Zhang } 5357e42f35eeSHong Zhang 5358e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5359a6b2eed2SHong Zhang k = 0; 5360a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5361e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5362a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 536387025532SHong Zhang for (j=0; j<nrows; j++) { 5364d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5365e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 53660298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5367a6b2eed2SHong Zhang for (l=0; l<ncols; l++) { 5368a6b2eed2SHong Zhang *bufJ++ = cols[l]; 536987025532SHong Zhang } 53700298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5371e42f35eeSHong Zhang } 537287025532SHong Zhang } 537387025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 537487025532SHong Zhang } 537587025532SHong Zhang 537687025532SHong Zhang /* recvs and sends of j-array are completed */ 537787025532SHong Zhang i = nrecvs; 537887025532SHong Zhang while (i--) { 5379aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 538087025532SHong Zhang } 53810c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 538287025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 5383b7f45c76SHong Zhang sstartsj = *startsj_s; 53841d79065fSBarry Smith rstartsj = *startsj_r; 538587025532SHong Zhang bufa = *bufa_ptr; 538687025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 538787025532SHong Zhang b_otha = b_oth->a; 5388f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 538987025532SHong Zhang 539087025532SHong Zhang /* a-array */ 539187025532SHong Zhang /*---------*/ 539287025532SHong Zhang /* post receives of a-array */ 539387025532SHong Zhang for (i=0; i<nrecvs; i++) { 539487025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 539587025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 539687025532SHong Zhang } 5397e42f35eeSHong Zhang 5398e42f35eeSHong Zhang /* pack the outgoing message a-array */ 539987025532SHong Zhang k = 0; 540087025532SHong Zhang for (i=0; i<nsends; i++) { 5401e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 540287025532SHong Zhang bufA = bufa+sstartsj[i]; 540387025532SHong Zhang for (j=0; j<nrows; j++) { 5404d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5405e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 54060298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 540787025532SHong Zhang for (l=0; l<ncols; l++) { 5408a6b2eed2SHong Zhang *bufA++ = vals[l]; 5409a6b2eed2SHong Zhang } 54100298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 5411e42f35eeSHong Zhang } 5412a6b2eed2SHong Zhang } 541387025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5414a6b2eed2SHong Zhang } 541587025532SHong Zhang /* recvs and sends of a-array are completed */ 541687025532SHong Zhang i = nrecvs; 541787025532SHong Zhang while (i--) { 5418aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 541987025532SHong Zhang } 54200c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5421d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5422a6b2eed2SHong Zhang 542387025532SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5424a6b2eed2SHong Zhang /* put together the new matrix */ 5425d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5426a6b2eed2SHong Zhang 5427a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5428a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 542987025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 5430e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5431e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 543287025532SHong Zhang b_oth->nonew = 0; 5433a6b2eed2SHong Zhang 5434a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5435b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) { 54361d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5437dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5438dea91ad1SHong Zhang } else { 5439b7f45c76SHong Zhang *startsj_s = sstartsj; 54401d79065fSBarry Smith *startsj_r = rstartsj; 544187025532SHong Zhang *bufa_ptr = bufa; 544287025532SHong Zhang } 5443dea91ad1SHong Zhang } 54444ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5445429d309bSHong Zhang PetscFunctionReturn(0); 5446429d309bSHong Zhang } 5447ccd8e176SBarry Smith 544843eb5e2fSMatthew Knepley #undef __FUNCT__ 544943eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 545043eb5e2fSMatthew Knepley /*@C 545143eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 545243eb5e2fSMatthew Knepley 545343eb5e2fSMatthew Knepley Not Collective 545443eb5e2fSMatthew Knepley 545543eb5e2fSMatthew Knepley Input Parameters: 545643eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 545743eb5e2fSMatthew Knepley 545843eb5e2fSMatthew Knepley Output Parameter: 545943eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 546043eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 546143eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 546243eb5e2fSMatthew Knepley 546343eb5e2fSMatthew Knepley Level: developer 546443eb5e2fSMatthew Knepley 546543eb5e2fSMatthew Knepley @*/ 546643eb5e2fSMatthew Knepley #if defined(PETSC_USE_CTABLE) 54677087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 546843eb5e2fSMatthew Knepley #else 54697087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 547043eb5e2fSMatthew Knepley #endif 547143eb5e2fSMatthew Knepley { 547243eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 547343eb5e2fSMatthew Knepley 547443eb5e2fSMatthew Knepley PetscFunctionBegin; 54750700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5476e414b56bSJed Brown PetscValidPointer(lvec, 2); 5477e414b56bSJed Brown PetscValidPointer(colmap, 3); 5478e414b56bSJed Brown PetscValidPointer(multScatter, 4); 547943eb5e2fSMatthew Knepley a = (Mat_MPIAIJ*) A->data; 548043eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 548143eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 548243eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 548343eb5e2fSMatthew Knepley PetscFunctionReturn(0); 548443eb5e2fSMatthew Knepley } 548543eb5e2fSMatthew Knepley 54868cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*); 54878cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*); 54888cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*); 548917667f90SBarry Smith 5490fc4dec0aSBarry Smith #undef __FUNCT__ 5491fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5492fc4dec0aSBarry Smith /* 5493fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5494fc4dec0aSBarry Smith 5495fc4dec0aSBarry Smith n p p 5496fc4dec0aSBarry Smith ( ) ( ) ( ) 5497fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5498fc4dec0aSBarry Smith ( ) ( ) ( ) 5499fc4dec0aSBarry Smith 5500fc4dec0aSBarry Smith */ 5501fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5502fc4dec0aSBarry Smith { 5503fc4dec0aSBarry Smith PetscErrorCode ierr; 5504fc4dec0aSBarry Smith Mat At,Bt,Ct; 5505fc4dec0aSBarry Smith 5506fc4dec0aSBarry Smith PetscFunctionBegin; 5507fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5508fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5509fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 55106bf464f9SBarry Smith ierr = MatDestroy(&At);CHKERRQ(ierr); 55116bf464f9SBarry Smith ierr = MatDestroy(&Bt);CHKERRQ(ierr); 5512fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 55136bf464f9SBarry Smith ierr = MatDestroy(&Ct);CHKERRQ(ierr); 5514fc4dec0aSBarry Smith PetscFunctionReturn(0); 5515fc4dec0aSBarry Smith } 5516fc4dec0aSBarry Smith 5517fc4dec0aSBarry Smith #undef __FUNCT__ 5518fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5519fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5520fc4dec0aSBarry Smith { 5521fc4dec0aSBarry Smith PetscErrorCode ierr; 5522d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5523fc4dec0aSBarry Smith Mat Cmat; 5524fc4dec0aSBarry Smith 5525fc4dec0aSBarry Smith PetscFunctionBegin; 5526e32f2f54SBarry 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); 5527ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr); 5528fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 552933d57670SJed Brown ierr = MatSetBlockSizesFromMats(Cmat,A,B);CHKERRQ(ierr); 5530fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 55310298fd71SBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr); 553238556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 553338556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5534f75ecaa4SHong Zhang 5535f75ecaa4SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ; 55362205254eSKarl Rupp 5537fc4dec0aSBarry Smith *C = Cmat; 5538fc4dec0aSBarry Smith PetscFunctionReturn(0); 5539fc4dec0aSBarry Smith } 5540fc4dec0aSBarry Smith 5541fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5542fc4dec0aSBarry Smith #undef __FUNCT__ 5543fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5544fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5545fc4dec0aSBarry Smith { 5546fc4dec0aSBarry Smith PetscErrorCode ierr; 5547fc4dec0aSBarry Smith 5548fc4dec0aSBarry Smith PetscFunctionBegin; 5549fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX) { 55503ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 5551fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 55523ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 5553fc4dec0aSBarry Smith } 55543ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 5555fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 55563ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 5557fc4dec0aSBarry Smith PetscFunctionReturn(0); 5558fc4dec0aSBarry Smith } 5559fc4dec0aSBarry Smith 5560611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 55618cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5562611f576cSBarry Smith #endif 55633bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 55648cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 55653bf14a46SMatthew Knepley #endif 5566611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 55678cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5568611f576cSBarry Smith #endif 556917f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 55708cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*); 557117f1a0eaSHong Zhang #endif 55725c9eb25fSBarry Smith 5573ccd8e176SBarry Smith /*MC 5574ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5575ccd8e176SBarry Smith 5576ccd8e176SBarry Smith Options Database Keys: 5577ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5578ccd8e176SBarry Smith 5579ccd8e176SBarry Smith Level: beginner 5580ccd8e176SBarry Smith 558169b1f4b7SBarry Smith .seealso: MatCreateAIJ() 5582ccd8e176SBarry Smith M*/ 5583ccd8e176SBarry Smith 5584ccd8e176SBarry Smith #undef __FUNCT__ 5585ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 55868cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B) 5587ccd8e176SBarry Smith { 5588ccd8e176SBarry Smith Mat_MPIAIJ *b; 5589ccd8e176SBarry Smith PetscErrorCode ierr; 5590ccd8e176SBarry Smith PetscMPIInt size; 5591ccd8e176SBarry Smith 5592ccd8e176SBarry Smith PetscFunctionBegin; 5593ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 55942205254eSKarl Rupp 5595b00a9115SJed Brown ierr = PetscNewLog(B,&b);CHKERRQ(ierr); 5596ccd8e176SBarry Smith B->data = (void*)b; 5597ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5598ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5599ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5600ccd8e176SBarry Smith b->size = size; 56012205254eSKarl Rupp 5602ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr); 5603ccd8e176SBarry Smith 5604ccd8e176SBarry Smith /* build cache for off array entries formed */ 5605ce94432eSBarry Smith ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr); 56062205254eSKarl Rupp 5607ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5608ccd8e176SBarry Smith b->colmap = 0; 5609ccd8e176SBarry Smith b->garray = 0; 5610ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5611ccd8e176SBarry Smith 5612ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 56130298fd71SBarry Smith b->lvec = NULL; 56140298fd71SBarry Smith b->Mvctx = NULL; 5615ccd8e176SBarry Smith 5616ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5617ccd8e176SBarry Smith b->rowindices = 0; 5618ccd8e176SBarry Smith b->rowvalues = 0; 5619ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5620ccd8e176SBarry Smith 5621bbf3fe20SPaul Mullowney /* flexible pointer used in CUSP/CUSPARSE classes */ 56220298fd71SBarry Smith b->spptr = NULL; 5623f60c3dc2SHong Zhang 5624611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5625bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr); 5626611f576cSBarry Smith #endif 56273bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5628bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 56293bf14a46SMatthew Knepley #endif 5630611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5631bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5632611f576cSBarry Smith #endif 563317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 5634bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr); 563517f1a0eaSHong Zhang #endif 5636bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5637bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5638bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5639bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5640bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5641bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5642bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 5643bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 5644bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5645bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5646bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5647bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5648bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 564917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5650ccd8e176SBarry Smith PetscFunctionReturn(0); 5651ccd8e176SBarry Smith } 565281824310SBarry Smith 565303bfb495SBarry Smith #undef __FUNCT__ 565403bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 565558d36128SBarry Smith /*@ 565603bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 565703bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 565803bfb495SBarry Smith 565903bfb495SBarry Smith Collective on MPI_Comm 566003bfb495SBarry Smith 566103bfb495SBarry Smith Input Parameters: 566203bfb495SBarry Smith + comm - MPI communicator 566303bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 566403bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 566503bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 566603bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 566703bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 566803bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 566903bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 567003bfb495SBarry Smith . j - column indices 567103bfb495SBarry Smith . a - matrix values 567203bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 567303bfb495SBarry Smith . oj - column indices 567403bfb495SBarry Smith - oa - matrix values 567503bfb495SBarry Smith 567603bfb495SBarry Smith Output Parameter: 567703bfb495SBarry Smith . mat - the matrix 567803bfb495SBarry Smith 567903bfb495SBarry Smith Level: advanced 568003bfb495SBarry Smith 568103bfb495SBarry Smith Notes: 5682292fb18eSBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user 5683292fb18eSBarry Smith must free the arrays once the matrix has been destroyed and not before. 568403bfb495SBarry Smith 568503bfb495SBarry Smith The i and j indices are 0 based 568603bfb495SBarry Smith 568769b1f4b7SBarry Smith See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 568803bfb495SBarry Smith 56897b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 56907b55108eSBarry Smith 5691dca341c0SJed Brown Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a 5692dca341c0SJed Brown legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does 5693dca341c0SJed Brown not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because 5694dca341c0SJed Brown the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to 5695dca341c0SJed Brown keep track of the underlying array. Use MatSetOption(A,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all 5696dca341c0SJed Brown communication if it is known that only local entries will be set. 569703bfb495SBarry Smith 569803bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 569903bfb495SBarry Smith 570003bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 570169b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays() 570203bfb495SBarry Smith @*/ 57032205254eSKarl 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) 570403bfb495SBarry Smith { 570503bfb495SBarry Smith PetscErrorCode ierr; 570603bfb495SBarry Smith Mat_MPIAIJ *maij; 570703bfb495SBarry Smith 570803bfb495SBarry Smith PetscFunctionBegin; 5709e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 5710ea345e14SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 5711ea345e14SBarry Smith if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 571203bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 571303bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 571403bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 571503bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 57162205254eSKarl Rupp 57178d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 571803bfb495SBarry Smith 571926283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 572026283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 572103bfb495SBarry Smith 572203bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5723d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 572403bfb495SBarry Smith 57258d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57268d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57278d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57288d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57298d7a6e47SBarry Smith 573003bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 573103bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5732dca341c0SJed Brown ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 573303bfb495SBarry Smith PetscFunctionReturn(0); 573403bfb495SBarry Smith } 573503bfb495SBarry Smith 573681824310SBarry Smith /* 573781824310SBarry Smith Special version for direct calls from Fortran 573881824310SBarry Smith */ 5739b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h> 57407087cfbeSBarry Smith 574181824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 574281824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 574381824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 574481824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 574581824310SBarry Smith #endif 574681824310SBarry Smith 574781824310SBarry Smith /* Change these macros so can be used in void function */ 574881824310SBarry Smith #undef CHKERRQ 5749e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 575081824310SBarry Smith #undef SETERRQ2 5751e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 57524994cf47SJed Brown #undef SETERRQ3 57534994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 575481824310SBarry Smith #undef SETERRQ 5755e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 575681824310SBarry Smith 575781824310SBarry Smith #undef __FUNCT__ 575881824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 57598cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 576081824310SBarry Smith { 576181824310SBarry Smith Mat mat = *mmat; 576281824310SBarry Smith PetscInt m = *mm, n = *mn; 576381824310SBarry Smith InsertMode addv = *maddv; 576481824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 576581824310SBarry Smith PetscScalar value; 576681824310SBarry Smith PetscErrorCode ierr; 5767899cda47SBarry Smith 57684994cf47SJed Brown MatCheckPreallocated(mat,1); 57692205254eSKarl Rupp if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 57702205254eSKarl Rupp 577181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5772f23aa3ddSBarry Smith else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 577381824310SBarry Smith #endif 577481824310SBarry Smith { 5775d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5776d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5777ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 577881824310SBarry Smith 577981824310SBarry Smith /* Some Variables required in the macro */ 578081824310SBarry Smith Mat A = aij->A; 578181824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 578281824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5783dd6ea824SBarry Smith MatScalar *aa = a->a; 5784ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE); 578581824310SBarry Smith Mat B = aij->B; 578681824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5787d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5788dd6ea824SBarry Smith MatScalar *ba = b->a; 578981824310SBarry Smith 579081824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 579181824310SBarry Smith PetscInt nonew = a->nonew; 5792dd6ea824SBarry Smith MatScalar *ap1,*ap2; 579381824310SBarry Smith 579481824310SBarry Smith PetscFunctionBegin; 579581824310SBarry Smith for (i=0; i<m; i++) { 579681824310SBarry Smith if (im[i] < 0) continue; 579781824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5798e32f2f54SBarry 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); 579981824310SBarry Smith #endif 580081824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 580181824310SBarry Smith row = im[i] - rstart; 580281824310SBarry Smith lastcol1 = -1; 580381824310SBarry Smith rp1 = aj + ai[row]; 580481824310SBarry Smith ap1 = aa + ai[row]; 580581824310SBarry Smith rmax1 = aimax[row]; 580681824310SBarry Smith nrow1 = ailen[row]; 580781824310SBarry Smith low1 = 0; 580881824310SBarry Smith high1 = nrow1; 580981824310SBarry Smith lastcol2 = -1; 581081824310SBarry Smith rp2 = bj + bi[row]; 581181824310SBarry Smith ap2 = ba + bi[row]; 581281824310SBarry Smith rmax2 = bimax[row]; 581381824310SBarry Smith nrow2 = bilen[row]; 581481824310SBarry Smith low2 = 0; 581581824310SBarry Smith high2 = nrow2; 581681824310SBarry Smith 581781824310SBarry Smith for (j=0; j<n; j++) { 58182205254eSKarl Rupp if (roworiented) value = v[i*n+j]; 58192205254eSKarl Rupp else value = v[i+j*m]; 582081824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 582181824310SBarry Smith if (in[j] >= cstart && in[j] < cend) { 582281824310SBarry Smith col = in[j] - cstart; 582381824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 582481824310SBarry Smith } else if (in[j] < 0) continue; 582581824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5826cb9801acSJed 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); 582781824310SBarry Smith #endif 582881824310SBarry Smith else { 582981824310SBarry Smith if (mat->was_assembled) { 583081824310SBarry Smith if (!aij->colmap) { 5831ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 583281824310SBarry Smith } 583381824310SBarry Smith #if defined(PETSC_USE_CTABLE) 583481824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 583581824310SBarry Smith col--; 583681824310SBarry Smith #else 583781824310SBarry Smith col = aij->colmap[in[j]] - 1; 583881824310SBarry Smith #endif 583981824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 5840ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 584181824310SBarry Smith col = in[j]; 584281824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 584381824310SBarry Smith B = aij->B; 584481824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 584581824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 584681824310SBarry Smith rp2 = bj + bi[row]; 584781824310SBarry Smith ap2 = ba + bi[row]; 584881824310SBarry Smith rmax2 = bimax[row]; 584981824310SBarry Smith nrow2 = bilen[row]; 585081824310SBarry Smith low2 = 0; 585181824310SBarry Smith high2 = nrow2; 5852d0f46423SBarry Smith bm = aij->B->rmap->n; 585381824310SBarry Smith ba = b->a; 585481824310SBarry Smith } 585581824310SBarry Smith } else col = in[j]; 585681824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 585781824310SBarry Smith } 585881824310SBarry Smith } 58592205254eSKarl Rupp } else if (!aij->donotstash) { 586081824310SBarry Smith if (roworiented) { 5861ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 586281824310SBarry Smith } else { 5863ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 586481824310SBarry Smith } 586581824310SBarry Smith } 586681824310SBarry Smith } 58672205254eSKarl Rupp } 586881824310SBarry Smith PetscFunctionReturnVoid(); 586981824310SBarry Smith } 587003bfb495SBarry Smith 5871