18a729477SBarry Smith 2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h> /*I "petscmat.h" I*/ 3c6db04a5SJed Brown #include <petscblaslapack.h> 48a729477SBarry Smith 501bebe75SBarry Smith /*MC 601bebe75SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 701bebe75SBarry Smith 801bebe75SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 901bebe75SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 1001bebe75SBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 1101bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 1201bebe75SBarry Smith the above preallocation routines for simplicity. 1301bebe75SBarry Smith 1401bebe75SBarry Smith Options Database Keys: 1501bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 1601bebe75SBarry Smith 179ae82921SPaul Mullowney Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when 1801bebe75SBarry Smith enough exist. 1901bebe75SBarry Smith 2001bebe75SBarry Smith Level: beginner 2101bebe75SBarry Smith 2269b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 2301bebe75SBarry Smith M*/ 2401bebe75SBarry Smith 2501bebe75SBarry Smith /*MC 2601bebe75SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 2701bebe75SBarry Smith 2801bebe75SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 2901bebe75SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 3001bebe75SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 3101bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3201bebe75SBarry Smith the above preallocation routines for simplicity. 3301bebe75SBarry Smith 3401bebe75SBarry Smith Options Database Keys: 3501bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 3601bebe75SBarry Smith 3701bebe75SBarry Smith Level: beginner 3801bebe75SBarry Smith 3901bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4001bebe75SBarry Smith M*/ 4101bebe75SBarry Smith 42dd6ea824SBarry Smith #undef __FUNCT__ 43f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ" 44f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows) 4527d4218bSShri Abhyankar { 4627d4218bSShri Abhyankar PetscErrorCode ierr; 4727d4218bSShri Abhyankar Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data; 4827d4218bSShri Abhyankar Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data; 4927d4218bSShri Abhyankar Mat_SeqAIJ *b = (Mat_SeqAIJ*)mat->B->data; 5027d4218bSShri Abhyankar const PetscInt *ia,*ib; 5127d4218bSShri Abhyankar const MatScalar *aa,*bb; 5227d4218bSShri Abhyankar PetscInt na,nb,i,j,*rows,cnt=0,n0rows; 5327d4218bSShri Abhyankar PetscInt m = M->rmap->n,rstart = M->rmap->rstart; 5427d4218bSShri Abhyankar 5527d4218bSShri Abhyankar PetscFunctionBegin; 5627d4218bSShri Abhyankar *keptrows = 0; 5727d4218bSShri Abhyankar ia = a->i; 5827d4218bSShri Abhyankar ib = b->i; 5927d4218bSShri Abhyankar for (i=0; i<m; i++) { 6027d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 6127d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 6227d4218bSShri Abhyankar if (!na && !nb) { 6327d4218bSShri Abhyankar cnt++; 6427d4218bSShri Abhyankar goto ok1; 6527d4218bSShri Abhyankar } 6627d4218bSShri Abhyankar aa = a->a + ia[i]; 6727d4218bSShri Abhyankar for (j=0; j<na; j++) { 6827d4218bSShri Abhyankar if (aa[j] != 0.0) goto ok1; 6927d4218bSShri Abhyankar } 7027d4218bSShri Abhyankar bb = b->a + ib[i]; 7127d4218bSShri Abhyankar for (j=0; j <nb; j++) { 7227d4218bSShri Abhyankar if (bb[j] != 0.0) goto ok1; 7327d4218bSShri Abhyankar } 7427d4218bSShri Abhyankar cnt++; 7527d4218bSShri Abhyankar ok1:; 7627d4218bSShri Abhyankar } 7727d4218bSShri Abhyankar ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,((PetscObject)M)->comm);CHKERRQ(ierr); 7827d4218bSShri Abhyankar if (!n0rows) PetscFunctionReturn(0); 7927d4218bSShri Abhyankar ierr = PetscMalloc((M->rmap->n-cnt)*sizeof(PetscInt),&rows);CHKERRQ(ierr); 8027d4218bSShri Abhyankar cnt = 0; 8127d4218bSShri Abhyankar for (i=0; i<m; i++) { 8227d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 8327d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 8427d4218bSShri Abhyankar if (!na && !nb) continue; 8527d4218bSShri Abhyankar aa = a->a + ia[i]; 8627d4218bSShri Abhyankar for (j=0; j<na;j++) { 8727d4218bSShri Abhyankar if (aa[j] != 0.0) { 8827d4218bSShri Abhyankar rows[cnt++] = rstart + i; 8927d4218bSShri Abhyankar goto ok2; 9027d4218bSShri Abhyankar } 9127d4218bSShri Abhyankar } 9227d4218bSShri Abhyankar bb = b->a + ib[i]; 9327d4218bSShri Abhyankar for (j=0; j<nb; j++) { 9427d4218bSShri Abhyankar if (bb[j] != 0.0) { 9527d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9627d4218bSShri Abhyankar goto ok2; 9727d4218bSShri Abhyankar } 9827d4218bSShri Abhyankar } 9927d4218bSShri Abhyankar ok2:; 10027d4218bSShri Abhyankar } 10177c8aea5SJed Brown ierr = ISCreateGeneral(((PetscObject)M)->comm,cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 10227d4218bSShri Abhyankar PetscFunctionReturn(0); 10327d4218bSShri Abhyankar } 10427d4218bSShri Abhyankar 10527d4218bSShri Abhyankar #undef __FUNCT__ 106f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ" 107f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows) 108f1f41ecbSJed Brown { 109f1f41ecbSJed Brown Mat_MPIAIJ *aij = (Mat_MPIAIJ*)M->data; 110f1f41ecbSJed Brown PetscErrorCode ierr; 111f1f41ecbSJed Brown PetscInt i,rstart,nrows,*rows; 112f1f41ecbSJed Brown 113f1f41ecbSJed Brown PetscFunctionBegin; 114f1f41ecbSJed Brown *zrows = PETSC_NULL; 115f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr); 116f1f41ecbSJed Brown ierr = MatGetOwnershipRange(M,&rstart,PETSC_NULL);CHKERRQ(ierr); 117f1f41ecbSJed Brown for (i=0; i<nrows; i++) rows[i] += rstart; 118f1f41ecbSJed Brown ierr = ISCreateGeneral(((PetscObject)M)->comm,nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 119f1f41ecbSJed Brown PetscFunctionReturn(0); 120f1f41ecbSJed Brown } 121f1f41ecbSJed Brown 122f1f41ecbSJed Brown #undef __FUNCT__ 1230716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ" 1240716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms) 1250716a85fSBarry Smith { 1260716a85fSBarry Smith PetscErrorCode ierr; 1270716a85fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data; 1280716a85fSBarry Smith PetscInt i,n,*garray = aij->garray; 1290716a85fSBarry Smith Mat_SeqAIJ *a_aij = (Mat_SeqAIJ*) aij->A->data; 1300716a85fSBarry Smith Mat_SeqAIJ *b_aij = (Mat_SeqAIJ*) aij->B->data; 1310716a85fSBarry Smith PetscReal *work; 1320716a85fSBarry Smith 1330716a85fSBarry Smith PetscFunctionBegin; 1340716a85fSBarry Smith ierr = MatGetSize(A,PETSC_NULL,&n);CHKERRQ(ierr); 1350716a85fSBarry Smith ierr = PetscMalloc(n*sizeof(PetscReal),&work);CHKERRQ(ierr); 1360716a85fSBarry Smith ierr = PetscMemzero(work,n*sizeof(PetscReal));CHKERRQ(ierr); 1370716a85fSBarry Smith if (type == NORM_2) { 1380716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1390716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]); 1400716a85fSBarry Smith } 1410716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1420716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]); 1430716a85fSBarry Smith } 1440716a85fSBarry Smith } else if (type == NORM_1) { 1450716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1460716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]); 1470716a85fSBarry Smith } 1480716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1490716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]); 1500716a85fSBarry Smith } 1510716a85fSBarry Smith } else if (type == NORM_INFINITY) { 1520716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1530716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]); 1540716a85fSBarry Smith } 1550716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1560716a85fSBarry Smith work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]); 1570716a85fSBarry Smith } 1580716a85fSBarry Smith 1590716a85fSBarry Smith } else SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_WRONG,"Unknown NormType"); 1600716a85fSBarry Smith if (type == NORM_INFINITY) { 1610716a85fSBarry Smith ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,A->hdr.comm);CHKERRQ(ierr); 1620716a85fSBarry Smith } else { 1630716a85fSBarry Smith ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,A->hdr.comm);CHKERRQ(ierr); 1640716a85fSBarry Smith } 1650716a85fSBarry Smith ierr = PetscFree(work);CHKERRQ(ierr); 1660716a85fSBarry Smith if (type == NORM_2) { 1678f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 1680716a85fSBarry Smith } 1690716a85fSBarry Smith PetscFunctionReturn(0); 1700716a85fSBarry Smith } 1710716a85fSBarry Smith 1720716a85fSBarry Smith #undef __FUNCT__ 173dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ" 174dd6ea824SBarry Smith /* 175dd6ea824SBarry Smith Distributes a SeqAIJ matrix across a set of processes. Code stolen from 176dd6ea824SBarry Smith MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type. 177dd6ea824SBarry Smith 178dd6ea824SBarry Smith Only for square matrices 179dd6ea824SBarry Smith */ 180dd6ea824SBarry Smith PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat) 181dd6ea824SBarry Smith { 182dd6ea824SBarry Smith PetscMPIInt rank,size; 183efcf75d5SBarry Smith PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz,*gmataj,cnt,row,*ld,bses[2]; 184dd6ea824SBarry Smith PetscErrorCode ierr; 185dd6ea824SBarry Smith Mat mat; 186dd6ea824SBarry Smith Mat_SeqAIJ *gmata; 187dd6ea824SBarry Smith PetscMPIInt tag; 188dd6ea824SBarry Smith MPI_Status status; 189ace3abfcSBarry Smith PetscBool aij; 190dd6ea824SBarry Smith MatScalar *gmataa,*ao,*ad,*gmataarestore=0; 191dd6ea824SBarry Smith 192dd6ea824SBarry Smith PetscFunctionBegin; 193dd6ea824SBarry Smith CHKMEMQ; 194dd6ea824SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 195dd6ea824SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 196dd6ea824SBarry Smith if (!rank) { 197251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr); 19865e19b50SBarry Smith if (!aij) SETERRQ1(((PetscObject)gmat)->comm,PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name); 199dd6ea824SBarry Smith } 200dd6ea824SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 201dd6ea824SBarry Smith ierr = MatCreate(comm,&mat);CHKERRQ(ierr); 202dd6ea824SBarry Smith ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 203efcf75d5SBarry Smith if (!rank) { 204efcf75d5SBarry Smith bses[0] = gmat->rmap->bs; 205efcf75d5SBarry Smith bses[1] = gmat->cmap->bs; 206efcf75d5SBarry Smith } 207efcf75d5SBarry Smith ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr); 208efcf75d5SBarry Smith ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr); 209dd6ea824SBarry Smith ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 210dd6ea824SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 211dd6ea824SBarry Smith ierr = PetscMalloc2(m,PetscInt,&dlens,m,PetscInt,&olens);CHKERRQ(ierr); 212dd6ea824SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 213dd6ea824SBarry Smith rowners[0] = 0; 214dd6ea824SBarry Smith for (i=2; i<=size; i++) { 215dd6ea824SBarry Smith rowners[i] += rowners[i-1]; 216dd6ea824SBarry Smith } 217dd6ea824SBarry Smith rstart = rowners[rank]; 218dd6ea824SBarry Smith rend = rowners[rank+1]; 219dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 220dd6ea824SBarry Smith if (!rank) { 221dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 222dd6ea824SBarry Smith /* send row lengths to all processors */ 223dd6ea824SBarry Smith for (i=0; i<m; i++) dlens[i] = gmata->ilen[i]; 224dd6ea824SBarry Smith for (i=1; i<size; i++) { 225dd6ea824SBarry Smith ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 226dd6ea824SBarry Smith } 227dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 228dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 229dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 230dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 231dd6ea824SBarry Smith jj = 0; 232dd6ea824SBarry Smith for (i=0; i<m; i++) { 233dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 234dd6ea824SBarry Smith if (gmata->j[jj] < rstart) ld[i]++; 235dd6ea824SBarry Smith if (gmata->j[jj] < rstart || gmata->j[jj] >= rend) olens[i]++; 236dd6ea824SBarry Smith jj++; 237dd6ea824SBarry Smith } 238dd6ea824SBarry Smith } 239dd6ea824SBarry Smith /* send column indices to other processes */ 240dd6ea824SBarry Smith for (i=1; i<size; i++) { 241dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 242dd6ea824SBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 243dd6ea824SBarry Smith ierr = MPI_Send(gmata->j + gmata->i[rowners[i]],nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 244dd6ea824SBarry Smith } 245dd6ea824SBarry Smith 246dd6ea824SBarry Smith /* send numerical values to other processes */ 247dd6ea824SBarry Smith for (i=1; i<size; i++) { 248dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 249dd6ea824SBarry Smith ierr = MPI_Send(gmata->a + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 250dd6ea824SBarry Smith } 251dd6ea824SBarry Smith gmataa = gmata->a; 252dd6ea824SBarry Smith gmataj = gmata->j; 253dd6ea824SBarry Smith 254dd6ea824SBarry Smith } else { 255dd6ea824SBarry Smith /* receive row lengths */ 256dd6ea824SBarry Smith ierr = MPI_Recv(dlens,m,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 257dd6ea824SBarry Smith /* receive column indices */ 258dd6ea824SBarry Smith ierr = MPI_Recv(&nz,1,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 259dd6ea824SBarry Smith ierr = PetscMalloc2(nz,PetscScalar,&gmataa,nz,PetscInt,&gmataj);CHKERRQ(ierr); 260dd6ea824SBarry Smith ierr = MPI_Recv(gmataj,nz,MPIU_INT,0,tag,comm,&status);CHKERRQ(ierr); 261dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 262dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 263dd6ea824SBarry Smith ierr = PetscMalloc(m*sizeof(PetscInt),&ld);CHKERRQ(ierr); 264dd6ea824SBarry Smith ierr = PetscMemzero(ld,m*sizeof(PetscInt));CHKERRQ(ierr); 265dd6ea824SBarry Smith jj = 0; 266dd6ea824SBarry Smith for (i=0; i<m; i++) { 267dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 268dd6ea824SBarry Smith if (gmataj[jj] < rstart) ld[i]++; 269dd6ea824SBarry Smith if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++; 270dd6ea824SBarry Smith jj++; 271dd6ea824SBarry Smith } 272dd6ea824SBarry Smith } 273dd6ea824SBarry Smith /* receive numerical values */ 274dd6ea824SBarry Smith ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 275dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 276dd6ea824SBarry Smith } 277dd6ea824SBarry Smith /* set preallocation */ 278dd6ea824SBarry Smith for (i=0; i<m; i++) { 279dd6ea824SBarry Smith dlens[i] -= olens[i]; 280dd6ea824SBarry Smith } 281dd6ea824SBarry Smith ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr); 282dd6ea824SBarry Smith ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr); 283dd6ea824SBarry Smith 284dd6ea824SBarry Smith for (i=0; i<m; i++) { 285dd6ea824SBarry Smith dlens[i] += olens[i]; 286dd6ea824SBarry Smith } 287dd6ea824SBarry Smith cnt = 0; 288dd6ea824SBarry Smith for (i=0; i<m; i++) { 289dd6ea824SBarry Smith row = rstart + i; 290dd6ea824SBarry Smith ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr); 291dd6ea824SBarry Smith cnt += dlens[i]; 292dd6ea824SBarry Smith } 293dd6ea824SBarry Smith if (rank) { 294dd6ea824SBarry Smith ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr); 295dd6ea824SBarry Smith } 296dd6ea824SBarry Smith ierr = PetscFree2(dlens,olens);CHKERRQ(ierr); 297dd6ea824SBarry Smith ierr = PetscFree(rowners);CHKERRQ(ierr); 298dd6ea824SBarry Smith ((Mat_MPIAIJ*)(mat->data))->ld = ld; 299dd6ea824SBarry Smith *inmat = mat; 300dd6ea824SBarry Smith } else { /* column indices are already set; only need to move over numerical values from process 0 */ 301dd6ea824SBarry Smith Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data; 302dd6ea824SBarry Smith Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data; 303dd6ea824SBarry Smith mat = *inmat; 304dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 305dd6ea824SBarry Smith if (!rank) { 306dd6ea824SBarry Smith /* send numerical values to other processes */ 307dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 308dd6ea824SBarry Smith ierr = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr); 309dd6ea824SBarry Smith gmataa = gmata->a; 310dd6ea824SBarry Smith for (i=1; i<size; i++) { 311dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 312dd6ea824SBarry Smith ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 313dd6ea824SBarry Smith } 314dd6ea824SBarry Smith nz = gmata->i[rowners[1]]-gmata->i[rowners[0]]; 315dd6ea824SBarry Smith } else { 316dd6ea824SBarry Smith /* receive numerical values from process 0*/ 317dd6ea824SBarry Smith nz = Ad->nz + Ao->nz; 318dd6ea824SBarry Smith ierr = PetscMalloc(nz*sizeof(PetscScalar),&gmataa);CHKERRQ(ierr); gmataarestore = gmataa; 319dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 320dd6ea824SBarry Smith } 321dd6ea824SBarry Smith /* transfer numerical values into the diagonal A and off diagonal B parts of mat */ 322dd6ea824SBarry Smith ld = ((Mat_MPIAIJ*)(mat->data))->ld; 323dd6ea824SBarry Smith ad = Ad->a; 324dd6ea824SBarry Smith ao = Ao->a; 325d0f46423SBarry Smith if (mat->rmap->n) { 326dd6ea824SBarry Smith i = 0; 327dd6ea824SBarry Smith nz = ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 328dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 329dd6ea824SBarry Smith } 330d0f46423SBarry Smith for (i=1; i<mat->rmap->n; i++) { 331dd6ea824SBarry 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; 332dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 333dd6ea824SBarry Smith } 334dd6ea824SBarry Smith i--; 335d0f46423SBarry Smith if (mat->rmap->n) { 336dd6ea824SBarry Smith nz = Ao->i[i+1] - Ao->i[i] - ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 337dd6ea824SBarry Smith } 338dd6ea824SBarry Smith if (rank) { 339dd6ea824SBarry Smith ierr = PetscFree(gmataarestore);CHKERRQ(ierr); 340dd6ea824SBarry Smith } 341dd6ea824SBarry Smith } 342dd6ea824SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 343dd6ea824SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 344dd6ea824SBarry Smith CHKMEMQ; 345dd6ea824SBarry Smith PetscFunctionReturn(0); 346dd6ea824SBarry Smith } 347dd6ea824SBarry Smith 3480f5bd95cSBarry Smith /* 3490f5bd95cSBarry Smith Local utility routine that creates a mapping from the global column 3509e25ed09SBarry Smith number to the local number in the off-diagonal part of the local 3510f5bd95cSBarry Smith storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at 3520f5bd95cSBarry Smith a slightly higher hash table cost; without it it is not scalable (each processor 3530f5bd95cSBarry Smith has an order N integer array but is fast to acess. 3549e25ed09SBarry Smith */ 3554a2ae208SSatish Balay #undef __FUNCT__ 356ab9863d7SBarry Smith #define __FUNCT__ "MatCreateColmap_MPIAIJ_Private" 357ab9863d7SBarry Smith PetscErrorCode MatCreateColmap_MPIAIJ_Private(Mat mat) 3589e25ed09SBarry Smith { 35944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3606849ba73SBarry Smith PetscErrorCode ierr; 361d0f46423SBarry Smith PetscInt n = aij->B->cmap->n,i; 362dbb450caSBarry Smith 3633a40ed3dSBarry Smith PetscFunctionBegin; 3645e1f6667SBarry Smith if (!aij->garray) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPIAIJ Matrix was assembled but is missing garray"); 365aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 366e23dfa41SBarry Smith ierr = PetscTableCreate(n,mat->cmap->N+1,&aij->colmap);CHKERRQ(ierr); 367b1fc9764SSatish Balay for (i=0; i<n; i++){ 3683861aac3SJed Brown ierr = PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr); 369b1fc9764SSatish Balay } 370b1fc9764SSatish Balay #else 371d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscInt),&aij->colmap);CHKERRQ(ierr); 372d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 373d0f46423SBarry Smith ierr = PetscMemzero(aij->colmap,mat->cmap->N*sizeof(PetscInt));CHKERRQ(ierr); 374905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 375b1fc9764SSatish Balay #endif 3763a40ed3dSBarry Smith PetscFunctionReturn(0); 3779e25ed09SBarry Smith } 3789e25ed09SBarry Smith 37930770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 3800520107fSSatish Balay { \ 3817cd84e04SBarry Smith if (col <= lastcol1) low1 = 0; else high1 = nrow1; \ 382fd3458f5SBarry Smith lastcol1 = col;\ 383fd3458f5SBarry Smith while (high1-low1 > 5) { \ 384fd3458f5SBarry Smith t = (low1+high1)/2; \ 385fd3458f5SBarry Smith if (rp1[t] > col) high1 = t; \ 386fd3458f5SBarry Smith else low1 = t; \ 387ba4e3ef2SSatish Balay } \ 388fd3458f5SBarry Smith for (_i=low1; _i<high1; _i++) { \ 389fd3458f5SBarry Smith if (rp1[_i] > col) break; \ 390fd3458f5SBarry Smith if (rp1[_i] == col) { \ 391fd3458f5SBarry Smith if (addv == ADD_VALUES) ap1[_i] += value; \ 392fd3458f5SBarry Smith else ap1[_i] = value; \ 39330770e4dSSatish Balay goto a_noinsert; \ 3940520107fSSatish Balay } \ 3950520107fSSatish Balay } \ 396e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low1 = 0; high1 = nrow1;goto a_noinsert;} \ 397e44c0bd4SBarry Smith if (nonew == 1) {low1 = 0; high1 = nrow1; goto a_noinsert;} \ 398e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 399fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew,MatScalar); \ 400669a8dbcSSatish Balay N = nrow1++ - 1; a->nz++; high1++; \ 4010520107fSSatish Balay /* shift up all the later entries in this row */ \ 4020520107fSSatish Balay for (ii=N; ii>=_i; ii--) { \ 403fd3458f5SBarry Smith rp1[ii+1] = rp1[ii]; \ 404fd3458f5SBarry Smith ap1[ii+1] = ap1[ii]; \ 4050520107fSSatish Balay } \ 406fd3458f5SBarry Smith rp1[_i] = col; \ 407fd3458f5SBarry Smith ap1[_i] = value; \ 40830770e4dSSatish Balay a_noinsert: ; \ 409fd3458f5SBarry Smith ailen[row] = nrow1; \ 4100520107fSSatish Balay } 4110a198c4cSBarry Smith 412085a36d4SBarry Smith 41330770e4dSSatish Balay #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \ 41430770e4dSSatish Balay { \ 4157cd84e04SBarry Smith if (col <= lastcol2) low2 = 0; else high2 = nrow2; \ 416fd3458f5SBarry Smith lastcol2 = col;\ 417fd3458f5SBarry Smith while (high2-low2 > 5) { \ 418fd3458f5SBarry Smith t = (low2+high2)/2; \ 419fd3458f5SBarry Smith if (rp2[t] > col) high2 = t; \ 420fd3458f5SBarry Smith else low2 = t; \ 421ba4e3ef2SSatish Balay } \ 422fd3458f5SBarry Smith for (_i=low2; _i<high2; _i++) { \ 423fd3458f5SBarry Smith if (rp2[_i] > col) break; \ 424fd3458f5SBarry Smith if (rp2[_i] == col) { \ 425fd3458f5SBarry Smith if (addv == ADD_VALUES) ap2[_i] += value; \ 426fd3458f5SBarry Smith else ap2[_i] = value; \ 42730770e4dSSatish Balay goto b_noinsert; \ 42830770e4dSSatish Balay } \ 42930770e4dSSatish Balay } \ 430e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 431e44c0bd4SBarry Smith if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 432e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 433fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \ 434669a8dbcSSatish Balay N = nrow2++ - 1; b->nz++; high2++; \ 43530770e4dSSatish Balay /* shift up all the later entries in this row */ \ 43630770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 437fd3458f5SBarry Smith rp2[ii+1] = rp2[ii]; \ 438fd3458f5SBarry Smith ap2[ii+1] = ap2[ii]; \ 43930770e4dSSatish Balay } \ 440fd3458f5SBarry Smith rp2[_i] = col; \ 441fd3458f5SBarry Smith ap2[_i] = value; \ 44230770e4dSSatish Balay b_noinsert: ; \ 443fd3458f5SBarry Smith bilen[row] = nrow2; \ 44430770e4dSSatish Balay } 44530770e4dSSatish Balay 4464a2ae208SSatish Balay #undef __FUNCT__ 4472fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ" 4482fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[]) 4492fd7e33dSBarry Smith { 4502fd7e33dSBarry Smith Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 4512fd7e33dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data; 4522fd7e33dSBarry Smith PetscErrorCode ierr; 4532fd7e33dSBarry Smith PetscInt l,*garray = mat->garray,diag; 4542fd7e33dSBarry Smith 4552fd7e33dSBarry Smith PetscFunctionBegin; 4562fd7e33dSBarry Smith /* code only works for square matrices A */ 4572fd7e33dSBarry Smith 4582fd7e33dSBarry Smith /* find size of row to the left of the diagonal part */ 4592fd7e33dSBarry Smith ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr); 4602fd7e33dSBarry Smith row = row - diag; 4612fd7e33dSBarry Smith for (l=0; l<b->i[row+1]-b->i[row]; l++) { 4622fd7e33dSBarry Smith if (garray[b->j[b->i[row]+l]] > diag) break; 4632fd7e33dSBarry Smith } 4642fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr); 4652fd7e33dSBarry Smith 4662fd7e33dSBarry Smith /* diagonal part */ 4672fd7e33dSBarry Smith ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr); 4682fd7e33dSBarry Smith 4692fd7e33dSBarry Smith /* right of diagonal part */ 4702fd7e33dSBarry 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); 4712fd7e33dSBarry Smith PetscFunctionReturn(0); 4722fd7e33dSBarry Smith } 4732fd7e33dSBarry Smith 4742fd7e33dSBarry Smith #undef __FUNCT__ 4754a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 476b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 4778a729477SBarry Smith { 47844a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 47987828ca2SBarry Smith PetscScalar value; 480dfbe8321SBarry Smith PetscErrorCode ierr; 481d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 482d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 483ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 4848a729477SBarry Smith 4850520107fSSatish Balay /* Some Variables required in the macro */ 4864ee7247eSSatish Balay Mat A = aij->A; 4874ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 48857809a77SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 489a77337e4SBarry Smith MatScalar *aa = a->a; 490ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 49130770e4dSSatish Balay Mat B = aij->B; 49230770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 493d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 494a77337e4SBarry Smith MatScalar *ba = b->a; 49530770e4dSSatish Balay 496fd3458f5SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 4978d76821aSHong Zhang PetscInt nonew; 498a77337e4SBarry Smith MatScalar *ap1,*ap2; 4994ee7247eSSatish Balay 5003a40ed3dSBarry Smith PetscFunctionBegin; 50171fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 5028a729477SBarry Smith for (i=0; i<m; i++) { 5035ef9f2a5SBarry Smith if (im[i] < 0) continue; 5042515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 505e32f2f54SBarry 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); 5060a198c4cSBarry Smith #endif 5074b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 5084b0e389bSBarry Smith row = im[i] - rstart; 509fd3458f5SBarry Smith lastcol1 = -1; 510fd3458f5SBarry Smith rp1 = aj + ai[row]; 511fd3458f5SBarry Smith ap1 = aa + ai[row]; 512fd3458f5SBarry Smith rmax1 = aimax[row]; 513fd3458f5SBarry Smith nrow1 = ailen[row]; 514fd3458f5SBarry Smith low1 = 0; 515fd3458f5SBarry Smith high1 = nrow1; 516fd3458f5SBarry Smith lastcol2 = -1; 517fd3458f5SBarry Smith rp2 = bj + bi[row]; 518d498b1e9SBarry Smith ap2 = ba + bi[row]; 519fd3458f5SBarry Smith rmax2 = bimax[row]; 520d498b1e9SBarry Smith nrow2 = bilen[row]; 521fd3458f5SBarry Smith low2 = 0; 522fd3458f5SBarry Smith high2 = nrow2; 523fd3458f5SBarry Smith 5241eb62cbbSBarry Smith for (j=0; j<n; j++) { 52516371a99SBarry Smith if (v) {if (roworiented) value = v[i*n+j]; else value = v[i+j*m];} else value = 0.0; 526abc0a331SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 527fd3458f5SBarry Smith if (in[j] >= cstart && in[j] < cend){ 528fd3458f5SBarry Smith col = in[j] - cstart; 5298d76821aSHong Zhang nonew = a->nonew; 53030770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 531273d9f13SBarry Smith } else if (in[j] < 0) continue; 5322515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 533cb9801acSJed 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); 5340a198c4cSBarry Smith #endif 5351eb62cbbSBarry Smith else { 536227d817aSBarry Smith if (mat->was_assembled) { 537905e6a2fSBarry Smith if (!aij->colmap) { 538ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 539905e6a2fSBarry Smith } 540aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 5410f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 542fa46199cSSatish Balay col--; 543b1fc9764SSatish Balay #else 544905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 545b1fc9764SSatish Balay #endif 5460e9bae81SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) { 547ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 5484b0e389bSBarry Smith col = in[j]; 5499bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 550f9508a3cSSatish Balay B = aij->B; 551f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 552e44c0bd4SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a; 553d498b1e9SBarry Smith rp2 = bj + bi[row]; 554d498b1e9SBarry Smith ap2 = ba + bi[row]; 555d498b1e9SBarry Smith rmax2 = bimax[row]; 556d498b1e9SBarry Smith nrow2 = bilen[row]; 557d498b1e9SBarry Smith low2 = 0; 558d498b1e9SBarry Smith high2 = nrow2; 559d0f46423SBarry Smith bm = aij->B->rmap->n; 560f9508a3cSSatish Balay ba = b->a; 5610e9bae81SBarry 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]); 562c48de900SBarry Smith } else col = in[j]; 5638d76821aSHong Zhang nonew = b->nonew; 56430770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 5651eb62cbbSBarry Smith } 5661eb62cbbSBarry Smith } 5675ef9f2a5SBarry Smith } else { 5684cb17eb5SBarry 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]); 56990f02eecSBarry Smith if (!aij->donotstash) { 5705080c13bSMatthew G Knepley mat->assembled = PETSC_FALSE; 571d36fbae8SSatish Balay if (roworiented) { 572ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 573d36fbae8SSatish Balay } else { 574ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 5754b0e389bSBarry Smith } 5761eb62cbbSBarry Smith } 5778a729477SBarry Smith } 57890f02eecSBarry Smith } 5793a40ed3dSBarry Smith PetscFunctionReturn(0); 5808a729477SBarry Smith } 5818a729477SBarry Smith 5824a2ae208SSatish Balay #undef __FUNCT__ 5834a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 584b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 585b49de8d1SLois Curfman McInnes { 586b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 587dfbe8321SBarry Smith PetscErrorCode ierr; 588d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 589d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 590b49de8d1SLois Curfman McInnes 5913a40ed3dSBarry Smith PetscFunctionBegin; 592b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 593e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 594e32f2f54SBarry 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); 595b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 596b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 597b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 598e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 599e32f2f54SBarry 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); 600b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend){ 601b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 602b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 603fa852ad4SSatish Balay } else { 604905e6a2fSBarry Smith if (!aij->colmap) { 605ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 606905e6a2fSBarry Smith } 607aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 6080f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 609fa46199cSSatish Balay col --; 610b1fc9764SSatish Balay #else 611905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 612b1fc9764SSatish Balay #endif 613e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 614d9d09a02SSatish Balay else { 615b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 616b49de8d1SLois Curfman McInnes } 617b49de8d1SLois Curfman McInnes } 618b49de8d1SLois Curfman McInnes } 619a8c6a408SBarry Smith } else { 620e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 621b49de8d1SLois Curfman McInnes } 622b49de8d1SLois Curfman McInnes } 6233a40ed3dSBarry Smith PetscFunctionReturn(0); 624b49de8d1SLois Curfman McInnes } 625bc5ccf88SSatish Balay 626bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 627bd0c2dcbSBarry Smith 6284a2ae208SSatish Balay #undef __FUNCT__ 6294a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 630dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 631bc5ccf88SSatish Balay { 632bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 633dfbe8321SBarry Smith PetscErrorCode ierr; 634b1d57f15SBarry Smith PetscInt nstash,reallocs; 635bc5ccf88SSatish Balay InsertMode addv; 636bc5ccf88SSatish Balay 637bc5ccf88SSatish Balay PetscFunctionBegin; 6384cb17eb5SBarry Smith if (aij->donotstash || mat->nooffprocentries) { 639bc5ccf88SSatish Balay PetscFunctionReturn(0); 640bc5ccf88SSatish Balay } 641bc5ccf88SSatish Balay 642bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 643*c3aae356SJed Brown ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,((PetscObject)mat)->comm);CHKERRQ(ierr); 644e7e72b3dSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added"); 645bc5ccf88SSatish Balay mat->insertmode = addv; /* in case this processor had no cache */ 646bc5ccf88SSatish Balay 647d0f46423SBarry Smith ierr = MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);CHKERRQ(ierr); 6488798bf22SSatish Balay ierr = MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);CHKERRQ(ierr); 649ae15b995SBarry Smith ierr = PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);CHKERRQ(ierr); 650bc5ccf88SSatish Balay PetscFunctionReturn(0); 651bc5ccf88SSatish Balay } 652bc5ccf88SSatish Balay 6534a2ae208SSatish Balay #undef __FUNCT__ 6544a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_MPIAIJ" 655dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode) 656bc5ccf88SSatish Balay { 657bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 65891c97fd4SSatish Balay Mat_SeqAIJ *a=(Mat_SeqAIJ *)aij->A->data; 6596849ba73SBarry Smith PetscErrorCode ierr; 660b1d57f15SBarry Smith PetscMPIInt n; 661b1d57f15SBarry Smith PetscInt i,j,rstart,ncols,flg; 662e44c0bd4SBarry Smith PetscInt *row,*col; 663ace3abfcSBarry Smith PetscBool other_disassembled; 66487828ca2SBarry Smith PetscScalar *val; 665bc5ccf88SSatish Balay InsertMode addv = mat->insertmode; 666bc5ccf88SSatish Balay 66791c97fd4SSatish Balay /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */ 668bc5ccf88SSatish Balay PetscFunctionBegin; 6694cb17eb5SBarry Smith if (!aij->donotstash && !mat->nooffprocentries) { 670a2d1c673SSatish Balay while (1) { 6718798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 672a2d1c673SSatish Balay if (!flg) break; 673a2d1c673SSatish Balay 674bc5ccf88SSatish Balay for (i=0; i<n;) { 675bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 676bc5ccf88SSatish Balay for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; } 677bc5ccf88SSatish Balay if (j < n) ncols = j-i; 678bc5ccf88SSatish Balay else ncols = n-i; 679bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 680bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 681bc5ccf88SSatish Balay i = j; 682bc5ccf88SSatish Balay } 683bc5ccf88SSatish Balay } 6848798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 685bc5ccf88SSatish Balay } 686bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 687bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 688bc5ccf88SSatish Balay 689bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 690bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 691bc5ccf88SSatish Balay /* 692bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 693bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 694bc5ccf88SSatish Balay */ 695bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 696*c3aae356SJed Brown ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,((PetscObject)mat)->comm);CHKERRQ(ierr); 697bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 698ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 699ad59fb31SSatish Balay } 700ad59fb31SSatish Balay } 701bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 702bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 703bc5ccf88SSatish Balay } 7044e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 7054e35b6f3SSatish Balay ierr = MatSetOption(aij->B,MAT_CHECK_COMPRESSED_ROW,PETSC_FALSE);CHKERRQ(ierr); 706bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 707bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 708bc5ccf88SSatish Balay 7091d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 710606d414cSSatish Balay aij->rowvalues = 0; 711a30b2313SHong Zhang 712a30b2313SHong Zhang /* used by MatAXPY() */ 71391c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 71491c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 715a30b2313SHong Zhang 7166bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 717bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 718bc5ccf88SSatish Balay PetscFunctionReturn(0); 719bc5ccf88SSatish Balay } 720bc5ccf88SSatish Balay 7214a2ae208SSatish Balay #undef __FUNCT__ 7224a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 723dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 7241eb62cbbSBarry Smith { 72544a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 726dfbe8321SBarry Smith PetscErrorCode ierr; 7273a40ed3dSBarry Smith 7283a40ed3dSBarry Smith PetscFunctionBegin; 72978b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 73078b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 7313a40ed3dSBarry Smith PetscFunctionReturn(0); 7321eb62cbbSBarry Smith } 7331eb62cbbSBarry Smith 7344a2ae208SSatish Balay #undef __FUNCT__ 7354a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 7362b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7371eb62cbbSBarry Smith { 73844a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 7396849ba73SBarry Smith PetscErrorCode ierr; 7407adad957SLisandro Dalcin PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 741d0f46423SBarry Smith PetscInt i,*owners = A->rmap->range; 742b1d57f15SBarry Smith PetscInt *nprocs,j,idx,nsends,row; 743b1d57f15SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 744b1d57f15SBarry Smith PetscInt *rvalues,count,base,slen,*source; 745d0f46423SBarry Smith PetscInt *lens,*lrows,*values,rstart=A->rmap->rstart; 7467adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)A)->comm; 7471eb62cbbSBarry Smith MPI_Request *send_waits,*recv_waits; 7481eb62cbbSBarry Smith MPI_Status recv_status,*send_status; 74997b48c8fSBarry Smith const PetscScalar *xx; 75097b48c8fSBarry Smith PetscScalar *bb; 7516543fbbaSBarry Smith #if defined(PETSC_DEBUG) 752ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 7536543fbbaSBarry Smith #endif 7541eb62cbbSBarry Smith 7553a40ed3dSBarry Smith PetscFunctionBegin; 7561eb62cbbSBarry Smith /* first count number of contributors to each processor */ 757b1d57f15SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 758b1d57f15SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 759b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 7606543fbbaSBarry Smith j = 0; 7611eb62cbbSBarry Smith for (i=0; i<N; i++) { 7626543fbbaSBarry Smith if (lastidx > (idx = rows[i])) j = 0; 7636543fbbaSBarry Smith lastidx = idx; 7646543fbbaSBarry Smith for (; j<size; j++) { 7651eb62cbbSBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 7666543fbbaSBarry Smith nprocs[2*j]++; 7676543fbbaSBarry Smith nprocs[2*j+1] = 1; 7686543fbbaSBarry Smith owner[i] = j; 7696543fbbaSBarry Smith #if defined(PETSC_DEBUG) 7706543fbbaSBarry Smith found = PETSC_TRUE; 7716543fbbaSBarry Smith #endif 7726543fbbaSBarry Smith break; 7731eb62cbbSBarry Smith } 7741eb62cbbSBarry Smith } 7756543fbbaSBarry Smith #if defined(PETSC_DEBUG) 776e32f2f54SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 7776543fbbaSBarry Smith found = PETSC_FALSE; 7786543fbbaSBarry Smith #endif 7791eb62cbbSBarry Smith } 780c1dc657dSBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 7811eb62cbbSBarry Smith 7827367270fSBarry Smith if (A->nooffproczerorows) { 7837367270fSBarry Smith if (nsends > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You called MatSetOption(,MAT_NO_OFF_PROC_ZERO_ROWS,PETSC_TRUE) but set an off process zero row"); 7847367270fSBarry Smith nrecvs = nsends; 7857367270fSBarry Smith nmax = N; 7867367270fSBarry Smith } else { 7871eb62cbbSBarry Smith /* inform other processors of number of messages and max length*/ 788c1dc657dSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 7897367270fSBarry Smith } 7901eb62cbbSBarry Smith 7911eb62cbbSBarry Smith /* post receives: */ 792b1d57f15SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 793b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 7941eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 795b1d57f15SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 7961eb62cbbSBarry Smith } 7971eb62cbbSBarry Smith 7981eb62cbbSBarry Smith /* do sends: 7991eb62cbbSBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 8001eb62cbbSBarry Smith the ith processor 8011eb62cbbSBarry Smith */ 802b1d57f15SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 803b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 804b1d57f15SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 8051eb62cbbSBarry Smith starts[0] = 0; 806c1dc657dSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 8071eb62cbbSBarry Smith for (i=0; i<N; i++) { 8081eb62cbbSBarry Smith svalues[starts[owner[i]]++] = rows[i]; 8091eb62cbbSBarry Smith } 8101eb62cbbSBarry Smith 8111eb62cbbSBarry Smith starts[0] = 0; 812c1dc657dSBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 8131eb62cbbSBarry Smith count = 0; 81417699dbbSLois Curfman McInnes for (i=0; i<size; i++) { 815c1dc657dSBarry Smith if (nprocs[2*i+1]) { 816b1d57f15SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 8171eb62cbbSBarry Smith } 8181eb62cbbSBarry Smith } 819606d414cSSatish Balay ierr = PetscFree(starts);CHKERRQ(ierr); 8201eb62cbbSBarry Smith 82117699dbbSLois Curfman McInnes base = owners[rank]; 8221eb62cbbSBarry Smith 8231eb62cbbSBarry Smith /* wait on receives */ 8241d79065fSBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 8251eb62cbbSBarry Smith count = nrecvs; slen = 0; 8261eb62cbbSBarry Smith while (count) { 827ca161407SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 8281eb62cbbSBarry Smith /* unpack receives into our local space */ 829b1d57f15SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 830d6dfbf8fSBarry Smith source[imdex] = recv_status.MPI_SOURCE; 831d6dfbf8fSBarry Smith lens[imdex] = n; 8321eb62cbbSBarry Smith slen += n; 8331eb62cbbSBarry Smith count--; 8341eb62cbbSBarry Smith } 835606d414cSSatish Balay ierr = PetscFree(recv_waits);CHKERRQ(ierr); 8361eb62cbbSBarry Smith 8371eb62cbbSBarry Smith /* move the data into the send scatter */ 838b1d57f15SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 8391eb62cbbSBarry Smith count = 0; 8401eb62cbbSBarry Smith for (i=0; i<nrecvs; i++) { 8411eb62cbbSBarry Smith values = rvalues + i*nmax; 8421eb62cbbSBarry Smith for (j=0; j<lens[i]; j++) { 8431eb62cbbSBarry Smith lrows[count++] = values[j] - base; 8441eb62cbbSBarry Smith } 8451eb62cbbSBarry Smith } 846606d414cSSatish Balay ierr = PetscFree(rvalues);CHKERRQ(ierr); 8471d79065fSBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 848606d414cSSatish Balay ierr = PetscFree(owner);CHKERRQ(ierr); 849606d414cSSatish Balay ierr = PetscFree(nprocs);CHKERRQ(ierr); 8501eb62cbbSBarry Smith 85197b48c8fSBarry Smith /* fix right hand side if needed */ 85297b48c8fSBarry Smith if (x && b) { 85397b48c8fSBarry Smith ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); 85497b48c8fSBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 855564f14d6SBarry Smith for (i=0; i<slen; i++) { 85697b48c8fSBarry Smith bb[lrows[i]] = diag*xx[lrows[i]]; 85797b48c8fSBarry Smith } 85897b48c8fSBarry Smith ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); 85997b48c8fSBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 86097b48c8fSBarry Smith } 8616eb55b6aSBarry Smith /* 8626eb55b6aSBarry Smith Zero the required rows. If the "diagonal block" of the matrix 863a8c7a070SBarry Smith is square and the user wishes to set the diagonal we use separate 8646eb55b6aSBarry Smith code so that MatSetValues() is not called for each diagonal allocating 8656eb55b6aSBarry Smith new memory, thus calling lots of mallocs and slowing things down. 8666eb55b6aSBarry Smith 8676eb55b6aSBarry Smith */ 868e2d53e46SBarry Smith /* must zero l->B before l->A because the (diag) case below may put values into l->B*/ 8692b40b63fSBarry Smith ierr = MatZeroRows(l->B,slen,lrows,0.0,0,0);CHKERRQ(ierr); 870d0f46423SBarry Smith if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) { 8712b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,diag,0,0);CHKERRQ(ierr); 872f4df32b1SMatthew Knepley } else if (diag != 0.0) { 8732b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 874fa46199cSSatish Balay if (((Mat_SeqAIJ*)l->A->data)->nonew) { 875e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\ 876512a5fc5SBarry Smith MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 8776525c446SSatish Balay } 878e2d53e46SBarry Smith for (i = 0; i < slen; i++) { 879e2d53e46SBarry Smith row = lrows[i] + rstart; 880f4df32b1SMatthew Knepley ierr = MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);CHKERRQ(ierr); 881e2d53e46SBarry Smith } 882e2d53e46SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 883e2d53e46SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 8846eb55b6aSBarry Smith } else { 8852b40b63fSBarry Smith ierr = MatZeroRows(l->A,slen,lrows,0.0,0,0);CHKERRQ(ierr); 8866eb55b6aSBarry Smith } 887606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 88872dacd9aSBarry Smith 8891eb62cbbSBarry Smith /* wait on sends */ 8901eb62cbbSBarry Smith if (nsends) { 891b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 892ca161407SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 893606d414cSSatish Balay ierr = PetscFree(send_status);CHKERRQ(ierr); 8941eb62cbbSBarry Smith } 895606d414cSSatish Balay ierr = PetscFree(send_waits);CHKERRQ(ierr); 896606d414cSSatish Balay ierr = PetscFree(svalues);CHKERRQ(ierr); 8973a40ed3dSBarry Smith PetscFunctionReturn(0); 8981eb62cbbSBarry Smith } 8991eb62cbbSBarry Smith 9004a2ae208SSatish Balay #undef __FUNCT__ 9019c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 9029c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 9039c7c4993SBarry Smith { 9049c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 9059c7c4993SBarry Smith PetscErrorCode ierr; 9069c7c4993SBarry Smith PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = ((PetscObject)A)->tag,lastidx = -1; 9079c7c4993SBarry Smith PetscInt i,*owners = A->rmap->range; 908564f14d6SBarry Smith PetscInt *nprocs,j,idx,nsends; 9099c7c4993SBarry Smith PetscInt nmax,*svalues,*starts,*owner,nrecvs; 9109c7c4993SBarry Smith PetscInt *rvalues,count,base,slen,*source; 911564f14d6SBarry Smith PetscInt *lens,*lrows,*values,m; 9129c7c4993SBarry Smith MPI_Comm comm = ((PetscObject)A)->comm; 9139c7c4993SBarry Smith MPI_Request *send_waits,*recv_waits; 9149c7c4993SBarry Smith MPI_Status recv_status,*send_status; 9159c7c4993SBarry Smith const PetscScalar *xx; 916564f14d6SBarry Smith PetscScalar *bb,*mask; 917564f14d6SBarry Smith Vec xmask,lmask; 918564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 919564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 920564f14d6SBarry Smith PetscScalar *aa; 9219c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9229c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 9239c7c4993SBarry Smith #endif 9249c7c4993SBarry Smith 9259c7c4993SBarry Smith PetscFunctionBegin; 9269c7c4993SBarry Smith /* first count number of contributors to each processor */ 9279c7c4993SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 9289c7c4993SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 9299c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&owner);CHKERRQ(ierr); /* see note*/ 9309c7c4993SBarry Smith j = 0; 9319c7c4993SBarry Smith for (i=0; i<N; i++) { 9329c7c4993SBarry Smith if (lastidx > (idx = rows[i])) j = 0; 9339c7c4993SBarry Smith lastidx = idx; 9349c7c4993SBarry Smith for (; j<size; j++) { 9359c7c4993SBarry Smith if (idx >= owners[j] && idx < owners[j+1]) { 9369c7c4993SBarry Smith nprocs[2*j]++; 9379c7c4993SBarry Smith nprocs[2*j+1] = 1; 9389c7c4993SBarry Smith owner[i] = j; 9399c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9409c7c4993SBarry Smith found = PETSC_TRUE; 9419c7c4993SBarry Smith #endif 9429c7c4993SBarry Smith break; 9439c7c4993SBarry Smith } 9449c7c4993SBarry Smith } 9459c7c4993SBarry Smith #if defined(PETSC_DEBUG) 9469c7c4993SBarry Smith if (!found) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Index out of range"); 9479c7c4993SBarry Smith found = PETSC_FALSE; 9489c7c4993SBarry Smith #endif 9499c7c4993SBarry Smith } 9509c7c4993SBarry Smith nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];} 9519c7c4993SBarry Smith 9529c7c4993SBarry Smith /* inform other processors of number of messages and max length*/ 9539c7c4993SBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 9549c7c4993SBarry Smith 9559c7c4993SBarry Smith /* post receives: */ 9569c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);CHKERRQ(ierr); 9579c7c4993SBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 9589c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 9599c7c4993SBarry Smith ierr = MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);CHKERRQ(ierr); 9609c7c4993SBarry Smith } 9619c7c4993SBarry Smith 9629c7c4993SBarry Smith /* do sends: 9639c7c4993SBarry Smith 1) starts[i] gives the starting index in svalues for stuff going to 9649c7c4993SBarry Smith the ith processor 9659c7c4993SBarry Smith */ 9669c7c4993SBarry Smith ierr = PetscMalloc((N+1)*sizeof(PetscInt),&svalues);CHKERRQ(ierr); 9679c7c4993SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 9689c7c4993SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 9699c7c4993SBarry Smith starts[0] = 0; 9709c7c4993SBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 9719c7c4993SBarry Smith for (i=0; i<N; i++) { 9729c7c4993SBarry Smith svalues[starts[owner[i]]++] = rows[i]; 9739c7c4993SBarry Smith } 9749c7c4993SBarry Smith 9759c7c4993SBarry Smith starts[0] = 0; 9769c7c4993SBarry Smith for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];} 9779c7c4993SBarry Smith count = 0; 9789c7c4993SBarry Smith for (i=0; i<size; i++) { 9799c7c4993SBarry Smith if (nprocs[2*i+1]) { 9809c7c4993SBarry Smith ierr = MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 9819c7c4993SBarry Smith } 9829c7c4993SBarry Smith } 9839c7c4993SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 9849c7c4993SBarry Smith 9859c7c4993SBarry Smith base = owners[rank]; 9869c7c4993SBarry Smith 9879c7c4993SBarry Smith /* wait on receives */ 9889c7c4993SBarry Smith ierr = PetscMalloc2(nrecvs,PetscInt,&lens,nrecvs,PetscInt,&source);CHKERRQ(ierr); 9899c7c4993SBarry Smith count = nrecvs; slen = 0; 9909c7c4993SBarry Smith while (count) { 9919c7c4993SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 9929c7c4993SBarry Smith /* unpack receives into our local space */ 9939c7c4993SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 9949c7c4993SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 9959c7c4993SBarry Smith lens[imdex] = n; 9969c7c4993SBarry Smith slen += n; 9979c7c4993SBarry Smith count--; 9989c7c4993SBarry Smith } 9999c7c4993SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 10009c7c4993SBarry Smith 10019c7c4993SBarry Smith /* move the data into the send scatter */ 10029c7c4993SBarry Smith ierr = PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);CHKERRQ(ierr); 10039c7c4993SBarry Smith count = 0; 10049c7c4993SBarry Smith for (i=0; i<nrecvs; i++) { 10059c7c4993SBarry Smith values = rvalues + i*nmax; 10069c7c4993SBarry Smith for (j=0; j<lens[i]; j++) { 10079c7c4993SBarry Smith lrows[count++] = values[j] - base; 10089c7c4993SBarry Smith } 10099c7c4993SBarry Smith } 10109c7c4993SBarry Smith ierr = PetscFree(rvalues);CHKERRQ(ierr); 10119c7c4993SBarry Smith ierr = PetscFree2(lens,source);CHKERRQ(ierr); 10129c7c4993SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 10139c7c4993SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 1014564f14d6SBarry Smith /* lrows are the local rows to be zeroed, slen is the number of local rows */ 10159c7c4993SBarry Smith 1016564f14d6SBarry Smith /* zero diagonal part of matrix */ 1017564f14d6SBarry Smith ierr = MatZeroRowsColumns(l->A,slen,lrows,diag,x,b);CHKERRQ(ierr); 10189c7c4993SBarry Smith 1019564f14d6SBarry Smith /* handle off diagonal part of matrix */ 1020564f14d6SBarry Smith ierr = MatGetVecs(A,&xmask,PETSC_NULL);CHKERRQ(ierr); 1021564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 1022564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 10239c7c4993SBarry Smith for (i=0; i<slen; i++) { 1024564f14d6SBarry Smith bb[lrows[i]] = 1; 10259c7c4993SBarry Smith } 1026564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 1027564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1028564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 10296bf464f9SBarry Smith ierr = VecDestroy(&xmask);CHKERRQ(ierr); 1030377aa5a1SBarry Smith if (x) { 1031564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1032564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,x,l->lvec,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1033564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 1034564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 1035377aa5a1SBarry Smith } 1036377aa5a1SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 1037564f14d6SBarry Smith 1038564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 1039564f14d6SBarry Smith ii = aij->i; 1040564f14d6SBarry Smith for (i=0; i<slen; i++) { 1041564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 10429c7c4993SBarry Smith } 1043564f14d6SBarry Smith 1044564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 1045564f14d6SBarry Smith if (aij->compressedrow.use){ 1046564f14d6SBarry Smith m = aij->compressedrow.nrows; 1047564f14d6SBarry Smith ii = aij->compressedrow.i; 1048564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 1049564f14d6SBarry Smith for (i=0; i<m; i++){ 1050564f14d6SBarry Smith n = ii[i+1] - ii[i]; 1051564f14d6SBarry Smith aj = aij->j + ii[i]; 1052564f14d6SBarry Smith aa = aij->a + ii[i]; 1053564f14d6SBarry Smith 1054564f14d6SBarry Smith for (j=0; j<n; j++) { 105525266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 1056377aa5a1SBarry Smith if (b) bb[*ridx] -= *aa*xx[*aj]; 1057564f14d6SBarry Smith *aa = 0.0; 1058564f14d6SBarry Smith } 1059564f14d6SBarry Smith aa++; 1060564f14d6SBarry Smith aj++; 1061564f14d6SBarry Smith } 1062564f14d6SBarry Smith ridx++; 1063564f14d6SBarry Smith } 1064564f14d6SBarry Smith } else { /* do not use compressed row format */ 1065564f14d6SBarry Smith m = l->B->rmap->n; 1066564f14d6SBarry Smith for (i=0; i<m; i++) { 1067564f14d6SBarry Smith n = ii[i+1] - ii[i]; 1068564f14d6SBarry Smith aj = aij->j + ii[i]; 1069564f14d6SBarry Smith aa = aij->a + ii[i]; 1070564f14d6SBarry Smith for (j=0; j<n; j++) { 107125266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 1072377aa5a1SBarry Smith if (b) bb[i] -= *aa*xx[*aj]; 1073564f14d6SBarry Smith *aa = 0.0; 1074564f14d6SBarry Smith } 1075564f14d6SBarry Smith aa++; 1076564f14d6SBarry Smith aj++; 1077564f14d6SBarry Smith } 1078564f14d6SBarry Smith } 1079564f14d6SBarry Smith } 1080377aa5a1SBarry Smith if (x) { 1081564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 1082564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 1083377aa5a1SBarry Smith } 1084377aa5a1SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 10856bf464f9SBarry Smith ierr = VecDestroy(&lmask);CHKERRQ(ierr); 10869c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 10879c7c4993SBarry Smith 10889c7c4993SBarry Smith /* wait on sends */ 10899c7c4993SBarry Smith if (nsends) { 10909c7c4993SBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 10919c7c4993SBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 10929c7c4993SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 10939c7c4993SBarry Smith } 10949c7c4993SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 10959c7c4993SBarry Smith ierr = PetscFree(svalues);CHKERRQ(ierr); 10969c7c4993SBarry Smith 10979c7c4993SBarry Smith PetscFunctionReturn(0); 10989c7c4993SBarry Smith } 10999c7c4993SBarry Smith 11009c7c4993SBarry Smith #undef __FUNCT__ 11014a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 1102dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 11031eb62cbbSBarry Smith { 1104416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1105dfbe8321SBarry Smith PetscErrorCode ierr; 1106b1d57f15SBarry Smith PetscInt nt; 1107416022c9SBarry Smith 11083a40ed3dSBarry Smith PetscFunctionBegin; 1109a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 111065e19b50SBarry 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); 1111ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1112f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 1113ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1114f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 11153a40ed3dSBarry Smith PetscFunctionReturn(0); 11161eb62cbbSBarry Smith } 11171eb62cbbSBarry Smith 11184a2ae208SSatish Balay #undef __FUNCT__ 1119bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 1120bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 1121bd0c2dcbSBarry Smith { 1122bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1123bd0c2dcbSBarry Smith PetscErrorCode ierr; 1124bd0c2dcbSBarry Smith 1125bd0c2dcbSBarry Smith PetscFunctionBegin; 1126bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 1127bd0c2dcbSBarry Smith PetscFunctionReturn(0); 1128bd0c2dcbSBarry Smith } 1129bd0c2dcbSBarry Smith 1130bd0c2dcbSBarry Smith #undef __FUNCT__ 11314a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 1132dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1133da3a660dSBarry Smith { 1134416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1135dfbe8321SBarry Smith PetscErrorCode ierr; 11363a40ed3dSBarry Smith 11373a40ed3dSBarry Smith PetscFunctionBegin; 1138ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1139f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1140ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1141f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 11423a40ed3dSBarry Smith PetscFunctionReturn(0); 1143da3a660dSBarry Smith } 1144da3a660dSBarry Smith 11454a2ae208SSatish Balay #undef __FUNCT__ 11464a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 1147dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 1148da3a660dSBarry Smith { 1149416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1150dfbe8321SBarry Smith PetscErrorCode ierr; 1151ace3abfcSBarry Smith PetscBool merged; 1152da3a660dSBarry Smith 11533a40ed3dSBarry Smith PetscFunctionBegin; 1154a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 1155da3a660dSBarry Smith /* do nondiagonal part */ 11567c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1157a5ff213dSBarry Smith if (!merged) { 1158da3a660dSBarry Smith /* send it on its way */ 1159ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1160da3a660dSBarry Smith /* do local part */ 11617c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1162da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 1163a5ff213dSBarry Smith /* added in yy until the next line, */ 1164ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1165a5ff213dSBarry Smith } else { 1166a5ff213dSBarry Smith /* do local part */ 1167a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 1168a5ff213dSBarry Smith /* send it on its way */ 1169ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1170a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 1171ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1172a5ff213dSBarry Smith } 11733a40ed3dSBarry Smith PetscFunctionReturn(0); 1174da3a660dSBarry Smith } 1175da3a660dSBarry Smith 1176cd0d46ebSvictorle EXTERN_C_BEGIN 1177cd0d46ebSvictorle #undef __FUNCT__ 11785fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 11797087cfbeSBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1180cd0d46ebSvictorle { 11814f423910Svictorle MPI_Comm comm; 1182cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij; 118366501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1184cd0d46ebSvictorle IS Me,Notme; 11856849ba73SBarry Smith PetscErrorCode ierr; 1186b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1187b1d57f15SBarry Smith PetscMPIInt size; 1188cd0d46ebSvictorle 1189cd0d46ebSvictorle PetscFunctionBegin; 119042e5f5b4Svictorle 119142e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 119266501d38Svictorle Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A; 11935485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1194cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 11954f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1196b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1197b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 119842e5f5b4Svictorle 119942e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1200cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1201cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1202b1d57f15SBarry Smith ierr = PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);CHKERRQ(ierr); 1203cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1204cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 120570b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1206268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1207268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 120866501d38Svictorle Aoff = Aoffs[0]; 1209268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 121066501d38Svictorle Boff = Boffs[0]; 12115485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 121266501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 121366501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 12146bf464f9SBarry Smith ierr = ISDestroy(&Me);CHKERRQ(ierr); 12156bf464f9SBarry Smith ierr = ISDestroy(&Notme);CHKERRQ(ierr); 12163e0d0d19SHong Zhang ierr = PetscFree(notme);CHKERRQ(ierr); 1217cd0d46ebSvictorle PetscFunctionReturn(0); 1218cd0d46ebSvictorle } 1219cd0d46ebSvictorle EXTERN_C_END 1220cd0d46ebSvictorle 12214a2ae208SSatish Balay #undef __FUNCT__ 12224a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1223dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1224da3a660dSBarry Smith { 1225416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1226dfbe8321SBarry Smith PetscErrorCode ierr; 1227da3a660dSBarry Smith 12283a40ed3dSBarry Smith PetscFunctionBegin; 1229da3a660dSBarry Smith /* do nondiagonal part */ 12307c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1231da3a660dSBarry Smith /* send it on its way */ 1232ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1233da3a660dSBarry Smith /* do local part */ 12347c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1235a5ff213dSBarry Smith /* receive remote parts */ 1236ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 12373a40ed3dSBarry Smith PetscFunctionReturn(0); 1238da3a660dSBarry Smith } 1239da3a660dSBarry Smith 12401eb62cbbSBarry Smith /* 12411eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 12421eb62cbbSBarry Smith diagonal block 12431eb62cbbSBarry Smith */ 12444a2ae208SSatish Balay #undef __FUNCT__ 12454a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1246dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 12471eb62cbbSBarry Smith { 1248dfbe8321SBarry Smith PetscErrorCode ierr; 1249416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 12503a40ed3dSBarry Smith 12513a40ed3dSBarry Smith PetscFunctionBegin; 1252e7e72b3dSBarry Smith if (A->rmap->N != A->cmap->N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); 1253e7e72b3dSBarry 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"); 12543a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 12553a40ed3dSBarry Smith PetscFunctionReturn(0); 12561eb62cbbSBarry Smith } 12571eb62cbbSBarry Smith 12584a2ae208SSatish Balay #undef __FUNCT__ 12594a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1260f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1261052efed2SBarry Smith { 1262052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1263dfbe8321SBarry Smith PetscErrorCode ierr; 12643a40ed3dSBarry Smith 12653a40ed3dSBarry Smith PetscFunctionBegin; 1266f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1267f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 12683a40ed3dSBarry Smith PetscFunctionReturn(0); 1269052efed2SBarry Smith } 1270052efed2SBarry Smith 12714a2ae208SSatish Balay #undef __FUNCT__ 12724a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1273dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 12741eb62cbbSBarry Smith { 127544a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1276dfbe8321SBarry Smith PetscErrorCode ierr; 127783e2fdc7SBarry Smith 12783a40ed3dSBarry Smith PetscFunctionBegin; 1279aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1280d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1281a5a9c739SBarry Smith #endif 12828798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 12836bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 12846bf464f9SBarry Smith ierr = MatDestroy(&aij->A);CHKERRQ(ierr); 12856bf464f9SBarry Smith ierr = MatDestroy(&aij->B);CHKERRQ(ierr); 1286aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 12876bc0bbbfSBarry Smith ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr); 1288b1fc9764SSatish Balay #else 128905b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1290b1fc9764SSatish Balay #endif 129105b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 12926bf464f9SBarry Smith ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr); 12936bf464f9SBarry Smith ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr); 129403095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 12958aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1296bf0cc555SLisandro Dalcin ierr = PetscFree(mat->data);CHKERRQ(ierr); 1297901853e0SKris Buschelman 1298dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1299901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr); 1300901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr); 1301901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);CHKERRQ(ierr); 1302901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr); 1303901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr); 1304ff69c46cSKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr); 1305901853e0SKris Buschelman ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);CHKERRQ(ierr); 1306471cc821SHong Zhang ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C","",PETSC_NULL);CHKERRQ(ierr); 13073a40ed3dSBarry Smith PetscFunctionReturn(0); 13081eb62cbbSBarry Smith } 1309ee50ffe9SBarry Smith 13104a2ae208SSatish Balay #undef __FUNCT__ 13118e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1312dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 13138e2fed03SBarry Smith { 13148e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 13158e2fed03SBarry Smith Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data; 13168e2fed03SBarry Smith Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data; 13176849ba73SBarry Smith PetscErrorCode ierr; 131832dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 13196f69ff64SBarry Smith int fd; 1320a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1321d0f46423SBarry Smith PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz; 13228e2fed03SBarry Smith PetscScalar *column_values; 132385ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 1324b37d52dbSMark F. Adams FILE *file; 13258e2fed03SBarry Smith 13268e2fed03SBarry Smith PetscFunctionBegin; 13277adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 13287adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)mat)->comm,&size);CHKERRQ(ierr); 13298e2fed03SBarry Smith nz = A->nz + B->nz; 1330958c9bccSBarry Smith if (!rank) { 13310700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1332d0f46423SBarry Smith header[1] = mat->rmap->N; 1333d0f46423SBarry Smith header[2] = mat->cmap->N; 13347adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 13358e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 13366f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13378e2fed03SBarry Smith /* get largest number of rows any processor has */ 1338d0f46423SBarry Smith rlen = mat->rmap->n; 1339d0f46423SBarry Smith range = mat->rmap->range; 13408e2fed03SBarry Smith for (i=1; i<size; i++) { 13418e2fed03SBarry Smith rlen = PetscMax(rlen,range[i+1] - range[i]); 13428e2fed03SBarry Smith } 13438e2fed03SBarry Smith } else { 13447adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1345d0f46423SBarry Smith rlen = mat->rmap->n; 13468e2fed03SBarry Smith } 13478e2fed03SBarry Smith 13488e2fed03SBarry Smith /* load up the local row counts */ 1349b1d57f15SBarry Smith ierr = PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);CHKERRQ(ierr); 1350d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 13518e2fed03SBarry Smith row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 13528e2fed03SBarry Smith } 13538e2fed03SBarry Smith 13548e2fed03SBarry Smith /* store the row lengths to the file */ 135585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1356958c9bccSBarry Smith if (!rank) { 1357d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13588e2fed03SBarry Smith for (i=1; i<size; i++) { 135985ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 13608e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1361a25532f0SBarry Smith ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 13626f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13638e2fed03SBarry Smith } 136485ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 13658e2fed03SBarry Smith } else { 136685ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 1367a25532f0SBarry Smith ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 136885ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 13698e2fed03SBarry Smith } 13708e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 13718e2fed03SBarry Smith 13728e2fed03SBarry Smith /* load up the local column indices */ 13738e2fed03SBarry Smith nzmax = nz; /* )th processor needs space a largest processor needs */ 13747adad957SLisandro Dalcin ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,((PetscObject)mat)->comm);CHKERRQ(ierr); 1375b1d57f15SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);CHKERRQ(ierr); 13768e2fed03SBarry Smith cnt = 0; 1377d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 13788e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 13798e2fed03SBarry Smith if ( (col = garray[B->j[j]]) > cstart) break; 13808e2fed03SBarry Smith column_indices[cnt++] = col; 13818e2fed03SBarry Smith } 13828e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 13838e2fed03SBarry Smith column_indices[cnt++] = A->j[k] + cstart; 13848e2fed03SBarry Smith } 13858e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 13868e2fed03SBarry Smith column_indices[cnt++] = garray[B->j[j]]; 13878e2fed03SBarry Smith } 13888e2fed03SBarry Smith } 1389e32f2f54SBarry 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); 13908e2fed03SBarry Smith 13918e2fed03SBarry Smith /* store the column indices to the file */ 139285ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1393958c9bccSBarry Smith if (!rank) { 13948e2fed03SBarry Smith MPI_Status status; 13956f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 13968e2fed03SBarry Smith for (i=1; i<size; i++) { 139785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 13987adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1399e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1400a25532f0SBarry Smith ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 14016f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 14028e2fed03SBarry Smith } 140385ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 14048e2fed03SBarry Smith } else { 140585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 14067adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1407a25532f0SBarry Smith ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 140885ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 14098e2fed03SBarry Smith } 14108e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 14118e2fed03SBarry Smith 14128e2fed03SBarry Smith /* load up the local column values */ 14138e2fed03SBarry Smith ierr = PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);CHKERRQ(ierr); 14148e2fed03SBarry Smith cnt = 0; 1415d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 14168e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 14178e2fed03SBarry Smith if ( garray[B->j[j]] > cstart) break; 14188e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 14198e2fed03SBarry Smith } 14208e2fed03SBarry Smith for (k=A->i[i]; k<A->i[i+1]; k++) { 14218e2fed03SBarry Smith column_values[cnt++] = A->a[k]; 14228e2fed03SBarry Smith } 14238e2fed03SBarry Smith for (; j<B->i[i+1]; j++) { 14248e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 14258e2fed03SBarry Smith } 14268e2fed03SBarry Smith } 1427e32f2f54SBarry 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); 14288e2fed03SBarry Smith 14298e2fed03SBarry Smith /* store the column values to the file */ 143085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1431958c9bccSBarry Smith if (!rank) { 14328e2fed03SBarry Smith MPI_Status status; 14336f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 14348e2fed03SBarry Smith for (i=1; i<size; i++) { 143585ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,message_count,flowcontrolcount);CHKERRQ(ierr); 14367adad957SLisandro Dalcin ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,((PetscObject)mat)->comm,&status);CHKERRQ(ierr); 1437e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1438a25532f0SBarry Smith ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 14396f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 14408e2fed03SBarry Smith } 144185ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,message_count);CHKERRQ(ierr); 14428e2fed03SBarry Smith } else { 144385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,message_count);CHKERRQ(ierr); 14447adad957SLisandro Dalcin ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 1445a25532f0SBarry Smith ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,((PetscObject)mat)->comm);CHKERRQ(ierr); 144685ebf7a4SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,message_count);CHKERRQ(ierr); 14478e2fed03SBarry Smith } 14488e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 1449b37d52dbSMark F. Adams 1450b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 1451b37d52dbSMark F. Adams if (file) { 1452b37d52dbSMark F. Adams fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs); 1453b37d52dbSMark F. Adams } 1454b37d52dbSMark F. Adams 14558e2fed03SBarry Smith PetscFunctionReturn(0); 14568e2fed03SBarry Smith } 14578e2fed03SBarry Smith 14588e2fed03SBarry Smith #undef __FUNCT__ 14594a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1460dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1461416022c9SBarry Smith { 146244a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1463dfbe8321SBarry Smith PetscErrorCode ierr; 146432dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1465ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1466b0a32e0cSBarry Smith PetscViewer sviewer; 1467f3ef73ceSBarry Smith PetscViewerFormat format; 1468416022c9SBarry Smith 14693a40ed3dSBarry Smith PetscFunctionBegin; 1470251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1471251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1472251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 147332077d6dSBarry Smith if (iascii) { 1474b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1475456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 14764e220ebcSLois Curfman McInnes MatInfo info; 1477ace3abfcSBarry Smith PetscBool inodes; 1478923f20ffSKris Buschelman 14797adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)mat)->comm,&rank);CHKERRQ(ierr); 1480888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 1481923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);CHKERRQ(ierr); 14827b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); 1483923f20ffSKris Buschelman if (!inodes) { 148477431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1485d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 14866831982aSBarry Smith } else { 148777431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1488d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 14896831982aSBarry Smith } 1490888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 149177431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1492888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 149377431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1494b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 14957b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); 149607d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1497a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 14983a40ed3dSBarry Smith PetscFunctionReturn(0); 1499fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1500923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1501923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1502923f20ffSKris Buschelman if (inodes) { 1503923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1504d38fa0fbSBarry Smith } else { 1505d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1506d38fa0fbSBarry Smith } 15073a40ed3dSBarry Smith PetscFunctionReturn(0); 15084aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 15094aedb280SBarry Smith PetscFunctionReturn(0); 151008480c60SBarry Smith } 15118e2fed03SBarry Smith } else if (isbinary) { 15128e2fed03SBarry Smith if (size == 1) { 15137adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 15148e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 15158e2fed03SBarry Smith } else { 15168e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 15178e2fed03SBarry Smith } 15188e2fed03SBarry Smith PetscFunctionReturn(0); 15190f5bd95cSBarry Smith } else if (isdraw) { 1520b0a32e0cSBarry Smith PetscDraw draw; 1521ace3abfcSBarry Smith PetscBool isnull; 1522b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1523b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 152419bcc07fSBarry Smith } 152519bcc07fSBarry Smith 152617699dbbSLois Curfman McInnes if (size == 1) { 15277adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 152878b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 15293a40ed3dSBarry Smith } else { 153095373324SBarry Smith /* assemble the entire matrix onto first processor. */ 153195373324SBarry Smith Mat A; 1532ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1533d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1534dd6ea824SBarry Smith MatScalar *a; 15352ee70a88SLois Curfman McInnes 153632a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1537ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 153832a366e4SMatthew Knepley 1539acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,PETSC_NULL);CHKERRQ(ierr); 154032a366e4SMatthew Knepley if (!flg) { 1541e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large."); 154232a366e4SMatthew Knepley } 154332a366e4SMatthew Knepley } 15440805154bSBarry Smith 15457adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)mat)->comm,&A);CHKERRQ(ierr); 154617699dbbSLois Curfman McInnes if (!rank) { 1547f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 15483a40ed3dSBarry Smith } else { 1549f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 155095373324SBarry Smith } 1551f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1552f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 1553f204ca49SKris Buschelman ierr = MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);CHKERRQ(ierr); 15542b82e772SSatish Balay ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 155552e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,A);CHKERRQ(ierr); 1556416022c9SBarry Smith 155795373324SBarry Smith /* copy over the A part */ 1558ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1559d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1560d0f46423SBarry Smith row = mat->rmap->rstart; 1561d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap->rstart ;} 156295373324SBarry Smith for (i=0; i<m; i++) { 1563416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 156495373324SBarry Smith row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 156595373324SBarry Smith } 15662ee70a88SLois Curfman McInnes aj = Aloc->j; 1567d0f46423SBarry Smith for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap->rstart;} 156895373324SBarry Smith 156995373324SBarry Smith /* copy over the B part */ 1570ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1571d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1572d0f46423SBarry Smith row = mat->rmap->rstart; 1573b1d57f15SBarry Smith ierr = PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);CHKERRQ(ierr); 1574b0a32e0cSBarry Smith ct = cols; 1575bfec09a0SHong Zhang for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];} 157695373324SBarry Smith for (i=0; i<m; i++) { 1577416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 157895373324SBarry Smith row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 157995373324SBarry Smith } 1580606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 15816d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 15826d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 158355843e3eSBarry Smith /* 158455843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1585b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 158655843e3eSBarry Smith */ 1587b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1588e03a110bSBarry Smith if (!rank) { 15897adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 15907566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 15917566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 15926831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 159395373324SBarry Smith } 1594b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 15956bf464f9SBarry Smith ierr = MatDestroy(&A);CHKERRQ(ierr); 159695373324SBarry Smith } 15973a40ed3dSBarry Smith PetscFunctionReturn(0); 15981eb62cbbSBarry Smith } 15991eb62cbbSBarry Smith 16004a2ae208SSatish Balay #undef __FUNCT__ 16014a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1602dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1603416022c9SBarry Smith { 1604dfbe8321SBarry Smith PetscErrorCode ierr; 1605ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1606416022c9SBarry Smith 16073a40ed3dSBarry Smith PetscFunctionBegin; 1608251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1609251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1610251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1611251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 161232077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 16137b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 16145cd90555SBarry Smith } else { 1615e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name); 1616416022c9SBarry Smith } 16173a40ed3dSBarry Smith PetscFunctionReturn(0); 1618416022c9SBarry Smith } 1619416022c9SBarry Smith 16204a2ae208SSatish Balay #undef __FUNCT__ 162141f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 162241f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 16238a729477SBarry Smith { 162444a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1625dfbe8321SBarry Smith PetscErrorCode ierr; 16266987fefcSBarry Smith Vec bb1 = 0; 1627ace3abfcSBarry Smith PetscBool hasop; 16288a729477SBarry Smith 16293a40ed3dSBarry Smith PetscFunctionBegin; 1630a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 163141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1632a2b30743SBarry Smith PetscFunctionReturn(0); 1633a2b30743SBarry Smith } 1634a2b30743SBarry Smith 16354e980039SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 16364e980039SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 16374e980039SJed Brown } 16384e980039SJed Brown 1639c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){ 1640da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 164141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16422798e883SHong Zhang its--; 1643da3a660dSBarry Smith } 16442798e883SHong Zhang 16452798e883SHong Zhang while (its--) { 1646ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1647ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16482798e883SHong Zhang 1649c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1650efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1651c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 16522798e883SHong Zhang 1653c14dc6b6SHong Zhang /* local sweep */ 165441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16552798e883SHong Zhang } 16563a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP){ 1657da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 165841f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16592798e883SHong Zhang its--; 1660da3a660dSBarry Smith } 16612798e883SHong Zhang while (its--) { 1662ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1663ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16642798e883SHong Zhang 1665c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1666efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1667c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1668c14dc6b6SHong Zhang 1669c14dc6b6SHong Zhang /* local sweep */ 167041f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16712798e883SHong Zhang } 16723a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){ 1673da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 167441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 16752798e883SHong Zhang its--; 1676da3a660dSBarry Smith } 16772798e883SHong Zhang while (its--) { 1678ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1679ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 16802798e883SHong Zhang 1681c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1682efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1683c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 16842798e883SHong Zhang 1685c14dc6b6SHong Zhang /* local sweep */ 168641f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 16872798e883SHong Zhang } 1688a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1689a7420bb7SBarry Smith Vec xx1; 1690a7420bb7SBarry Smith 1691a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 169241f059aeSBarry 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); 1693a7420bb7SBarry Smith 1694a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1695a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1696a7420bb7SBarry Smith if (!mat->diag) { 1697a7420bb7SBarry Smith ierr = MatGetVecs(matin,&mat->diag,PETSC_NULL);CHKERRQ(ierr); 1698a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1699a7420bb7SBarry Smith } 1700bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1701bd0c2dcbSBarry Smith if (hasop) { 1702bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1703bd0c2dcbSBarry Smith } else { 1704a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1705bd0c2dcbSBarry Smith } 1706887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1707887ee2caSBarry Smith 1708a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1709a7420bb7SBarry Smith 1710a7420bb7SBarry Smith /* local sweep */ 171141f059aeSBarry 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); 1712a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 17136bf464f9SBarry Smith ierr = VecDestroy(&xx1);CHKERRQ(ierr); 171444b1af1bSBarry Smith } else SETERRQ(((PetscObject)matin)->comm,PETSC_ERR_SUP,"Parallel SOR not supported"); 1715c14dc6b6SHong Zhang 17166bf464f9SBarry Smith ierr = VecDestroy(&bb1);CHKERRQ(ierr); 17173a40ed3dSBarry Smith PetscFunctionReturn(0); 17188a729477SBarry Smith } 1719a66be287SLois Curfman McInnes 17204a2ae208SSatish Balay #undef __FUNCT__ 172142e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 172242e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 172342e855d1Svictor { 172472e6a0cfSJed Brown Mat aA,aB,Aperm; 172572e6a0cfSJed Brown const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj; 172672e6a0cfSJed Brown PetscScalar *aa,*ba; 172772e6a0cfSJed Brown PetscInt i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest; 172872e6a0cfSJed Brown PetscSF rowsf,sf; 172972e6a0cfSJed Brown IS parcolp = PETSC_NULL; 173072e6a0cfSJed Brown PetscBool done; 173142e855d1Svictor PetscErrorCode ierr; 173242e855d1Svictor 173342e855d1Svictor PetscFunctionBegin; 173472e6a0cfSJed Brown ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 173572e6a0cfSJed Brown ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr); 173672e6a0cfSJed Brown ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr); 173772e6a0cfSJed Brown ierr = PetscMalloc3(PetscMax(m,n),PetscInt,&work,m,PetscInt,&rdest,n,PetscInt,&cdest);CHKERRQ(ierr); 173872e6a0cfSJed Brown 173972e6a0cfSJed Brown /* Invert row permutation to find out where my rows should go */ 174072e6a0cfSJed Brown ierr = PetscSFCreate(((PetscObject)A)->comm,&rowsf);CHKERRQ(ierr); 174172e6a0cfSJed Brown ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,PETSC_NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr); 1742e9e74f11SJed Brown ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr); 174372e6a0cfSJed Brown for (i=0; i<m; i++) work[i] = A->rmap->rstart + i; 174472e6a0cfSJed Brown ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr); 174572e6a0cfSJed Brown ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPI_REPLACE);CHKERRQ(ierr); 174672e6a0cfSJed Brown 174772e6a0cfSJed Brown /* Invert column permutation to find out where my columns should go */ 174872e6a0cfSJed Brown ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr); 174972e6a0cfSJed Brown ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,PETSC_NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr); 1750e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 175172e6a0cfSJed Brown for (i=0; i<n; i++) work[i] = A->cmap->rstart + i; 175272e6a0cfSJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr); 175372e6a0cfSJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPI_REPLACE);CHKERRQ(ierr); 175472e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 175572e6a0cfSJed Brown 175672e6a0cfSJed Brown ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr); 175772e6a0cfSJed Brown ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr); 175872e6a0cfSJed Brown ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr); 175972e6a0cfSJed Brown 176072e6a0cfSJed Brown /* Find out where my gcols should go */ 176172e6a0cfSJed Brown ierr = MatGetSize(aB,PETSC_NULL,&ng);CHKERRQ(ierr); 176272e6a0cfSJed Brown ierr = PetscMalloc(ng*sizeof(PetscInt),&gcdest);CHKERRQ(ierr); 176372e6a0cfSJed Brown ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr); 176472e6a0cfSJed Brown ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,PETSC_NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr); 1765e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 176672e6a0cfSJed Brown ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 176772e6a0cfSJed Brown ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 176872e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 176972e6a0cfSJed Brown 177072e6a0cfSJed Brown ierr = PetscMalloc4(m,PetscInt,&dnnz,m,PetscInt,&onnz,m,PetscInt,&tdnnz,m,PetscInt,&tonnz);CHKERRQ(ierr); 177172e6a0cfSJed Brown ierr = PetscMemzero(dnnz,m*sizeof(PetscInt));CHKERRQ(ierr); 177272e6a0cfSJed Brown ierr = PetscMemzero(onnz,m*sizeof(PetscInt));CHKERRQ(ierr); 177372e6a0cfSJed Brown ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 177472e6a0cfSJed Brown ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 177572e6a0cfSJed Brown for (i=0; i<m; i++) { 177672e6a0cfSJed Brown PetscInt row = rdest[i],rowner; 177772e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr); 177872e6a0cfSJed Brown for (j=ai[i]; j<ai[i+1]; j++) { 177972e6a0cfSJed Brown PetscInt cowner,col = cdest[aj[j]]; 178072e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */ 178172e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 178272e6a0cfSJed Brown else onnz[i]++; 178372e6a0cfSJed Brown } 178472e6a0cfSJed Brown for (j=bi[i]; j<bi[i+1]; j++) { 178572e6a0cfSJed Brown PetscInt cowner,col = gcdest[bj[j]]; 178672e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); 178772e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 178872e6a0cfSJed Brown else onnz[i]++; 178972e6a0cfSJed Brown } 179072e6a0cfSJed Brown } 179172e6a0cfSJed Brown ierr = PetscMemzero(tdnnz,m*sizeof(PetscInt));CHKERRQ(ierr); 179272e6a0cfSJed Brown ierr = PetscMemzero(tonnz,m*sizeof(PetscInt));CHKERRQ(ierr); 179372e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 179472e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 179572e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 179672e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 179772e6a0cfSJed Brown ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr); 179872e6a0cfSJed Brown 1799568b9367SJed Brown ierr = MatCreateAIJ(((PetscObject)A)->comm,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);CHKERRQ(ierr); 180072e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr); 180172e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr); 180272e6a0cfSJed Brown for (i=0; i<m; i++) { 180372e6a0cfSJed Brown PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */ 180472e6a0cfSJed Brown PetscInt rowlen; 180572e6a0cfSJed Brown rowlen = ai[i+1] - ai[i]; 180672e6a0cfSJed Brown for (j=0; j<rowlen; j++) acols[j] = cdest[aj[ai[i]+j]]; 180772e6a0cfSJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,acols,aa+ai[i],INSERT_VALUES);CHKERRQ(ierr); 180872e6a0cfSJed Brown rowlen = bi[i+1] - bi[i]; 180972e6a0cfSJed Brown for (j=0; j<rowlen; j++) bcols[j] = gcdest[bj[bi[i]+j]]; 181072e6a0cfSJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],rowlen,bcols,ba+bi[i],INSERT_VALUES);CHKERRQ(ierr); 181172e6a0cfSJed Brown } 181272e6a0cfSJed Brown ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 181372e6a0cfSJed Brown ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 181472e6a0cfSJed Brown ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 181572e6a0cfSJed Brown ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 181672e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr); 181772e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr); 181872e6a0cfSJed Brown ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr); 181972e6a0cfSJed Brown ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr); 182072e6a0cfSJed Brown ierr = PetscFree(gcdest);CHKERRQ(ierr); 182172e6a0cfSJed Brown if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);} 182272e6a0cfSJed Brown *B = Aperm; 182342e855d1Svictor PetscFunctionReturn(0); 182442e855d1Svictor } 182542e855d1Svictor 182642e855d1Svictor #undef __FUNCT__ 18274a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1828dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1829a66be287SLois Curfman McInnes { 1830a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1831a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1832dfbe8321SBarry Smith PetscErrorCode ierr; 1833329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1834a66be287SLois Curfman McInnes 18353a40ed3dSBarry Smith PetscFunctionBegin; 18364e220ebcSLois Curfman McInnes info->block_size = 1.0; 18374e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 18384e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 18394e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 18404e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 18414e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 18424e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1843a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 18444e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 18454e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 18464e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 18474e220ebcSLois Curfman McInnes info->memory = isend[3]; 18484e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1849a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1850d9822059SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,((PetscObject)matin)->comm);CHKERRQ(ierr); 18514e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 18524e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 18534e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 18544e220ebcSLois Curfman McInnes info->memory = irecv[3]; 18554e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1856a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1857d9822059SBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,((PetscObject)matin)->comm);CHKERRQ(ierr); 18584e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 18594e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 18604e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 18614e220ebcSLois Curfman McInnes info->memory = irecv[3]; 18624e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1863a66be287SLois Curfman McInnes } 18644e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 18654e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 18664e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 18673a40ed3dSBarry Smith PetscFunctionReturn(0); 1868a66be287SLois Curfman McInnes } 1869a66be287SLois Curfman McInnes 18704a2ae208SSatish Balay #undef __FUNCT__ 18714a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1872ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1873c74985f6SBarry Smith { 1874c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1875dfbe8321SBarry Smith PetscErrorCode ierr; 1876c74985f6SBarry Smith 18773a40ed3dSBarry Smith PetscFunctionBegin; 187812c028f9SKris Buschelman switch (op) { 1879512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 188012c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 188128b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1882a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 188312c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 188412c028f9SKris Buschelman case MAT_USE_INODES: 188512c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 1886fa1f0d2cSMatthew G Knepley MatCheckPreallocated(A,1); 18874e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 18884e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 188912c028f9SKris Buschelman break; 189012c028f9SKris Buschelman case MAT_ROW_ORIENTED: 18914e0d8c25SBarry Smith a->roworiented = flg; 18924e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 18934e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 189412c028f9SKris Buschelman break; 18954e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1896290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 189712c028f9SKris Buschelman break; 189812c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 18995c0f0b64SBarry Smith a->donotstash = flg; 190012c028f9SKris Buschelman break; 1901ffa07934SHong Zhang case MAT_SPD: 1902ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1903ffa07934SHong Zhang A->spd = flg; 1904ffa07934SHong Zhang if (flg) { 1905ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1906ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1907ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1908ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1909ffa07934SHong Zhang } 1910ffa07934SHong Zhang break; 191177e54ba9SKris Buschelman case MAT_SYMMETRIC: 19124e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 191325f421beSHong Zhang break; 191477e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1915eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1916eeffb40dSHong Zhang break; 1917bf108f30SBarry Smith case MAT_HERMITIAN: 1918eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1919eeffb40dSHong Zhang break; 1920bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 19214e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 192277e54ba9SKris Buschelman break; 192312c028f9SKris Buschelman default: 1924e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 19253a40ed3dSBarry Smith } 19263a40ed3dSBarry Smith PetscFunctionReturn(0); 1927c74985f6SBarry Smith } 1928c74985f6SBarry Smith 19294a2ae208SSatish Balay #undef __FUNCT__ 19304a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1931b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 193239e00950SLois Curfman McInnes { 1933154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 193487828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 19356849ba73SBarry Smith PetscErrorCode ierr; 1936d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1937d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1938b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 193939e00950SLois Curfman McInnes 19403a40ed3dSBarry Smith PetscFunctionBegin; 1941e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 19427a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 19437a0afa10SBarry Smith 194470f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 19457a0afa10SBarry Smith /* 19467a0afa10SBarry Smith allocate enough space to hold information from the longest row. 19477a0afa10SBarry Smith */ 19487a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1949b1d57f15SBarry Smith PetscInt max = 1,tmp; 1950d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 19517a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 19527a0afa10SBarry Smith if (max < tmp) { max = tmp; } 19537a0afa10SBarry Smith } 19541d79065fSBarry Smith ierr = PetscMalloc2(max,PetscScalar,&mat->rowvalues,max,PetscInt,&mat->rowindices);CHKERRQ(ierr); 19557a0afa10SBarry Smith } 19567a0afa10SBarry Smith 1957e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1958abc0e9e4SLois Curfman McInnes lrow = row - rstart; 195939e00950SLois Curfman McInnes 1960154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1961154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1962154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1963f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1964f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1965154123eaSLois Curfman McInnes nztot = nzA + nzB; 1966154123eaSLois Curfman McInnes 196770f0671dSBarry Smith cmap = mat->garray; 1968154123eaSLois Curfman McInnes if (v || idx) { 1969154123eaSLois Curfman McInnes if (nztot) { 1970154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1971b1d57f15SBarry Smith PetscInt imark = -1; 1972154123eaSLois Curfman McInnes if (v) { 197370f0671dSBarry Smith *v = v_p = mat->rowvalues; 197439e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 197570f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1976154123eaSLois Curfman McInnes else break; 1977154123eaSLois Curfman McInnes } 1978154123eaSLois Curfman McInnes imark = i; 197970f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 198070f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1981154123eaSLois Curfman McInnes } 1982154123eaSLois Curfman McInnes if (idx) { 198370f0671dSBarry Smith *idx = idx_p = mat->rowindices; 198470f0671dSBarry Smith if (imark > -1) { 198570f0671dSBarry Smith for (i=0; i<imark; i++) { 198670f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 198770f0671dSBarry Smith } 198870f0671dSBarry Smith } else { 1989154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 199070f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1991154123eaSLois Curfman McInnes else break; 1992154123eaSLois Curfman McInnes } 1993154123eaSLois Curfman McInnes imark = i; 199470f0671dSBarry Smith } 199570f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 199670f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 199739e00950SLois Curfman McInnes } 19983f97c4b0SBarry Smith } else { 19991ca473b0SSatish Balay if (idx) *idx = 0; 20001ca473b0SSatish Balay if (v) *v = 0; 20011ca473b0SSatish Balay } 2002154123eaSLois Curfman McInnes } 200339e00950SLois Curfman McInnes *nz = nztot; 2004f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 2005f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 20063a40ed3dSBarry Smith PetscFunctionReturn(0); 200739e00950SLois Curfman McInnes } 200839e00950SLois Curfman McInnes 20094a2ae208SSatish Balay #undef __FUNCT__ 20104a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 2011b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 201239e00950SLois Curfman McInnes { 20137a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 20143a40ed3dSBarry Smith 20153a40ed3dSBarry Smith PetscFunctionBegin; 2016e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 20177a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 20183a40ed3dSBarry Smith PetscFunctionReturn(0); 201939e00950SLois Curfman McInnes } 202039e00950SLois Curfman McInnes 20214a2ae208SSatish Balay #undef __FUNCT__ 20224a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 2023dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 2024855ac2c5SLois Curfman McInnes { 2025855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2026ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 2027dfbe8321SBarry Smith PetscErrorCode ierr; 2028d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 2029329f5518SBarry Smith PetscReal sum = 0.0; 2030a77337e4SBarry Smith MatScalar *v; 203104ca555eSLois Curfman McInnes 20323a40ed3dSBarry Smith PetscFunctionBegin; 203317699dbbSLois Curfman McInnes if (aij->size == 1) { 203414183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 203537fa93a5SLois Curfman McInnes } else { 203604ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 203704ca555eSLois Curfman McInnes v = amat->a; 203804ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 2039aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 2040329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 204104ca555eSLois Curfman McInnes #else 204204ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 204304ca555eSLois Curfman McInnes #endif 204404ca555eSLois Curfman McInnes } 204504ca555eSLois Curfman McInnes v = bmat->a; 204604ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 2047aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 2048329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 204904ca555eSLois Curfman McInnes #else 205004ca555eSLois Curfman McInnes sum += (*v)*(*v); v++; 205104ca555eSLois Curfman McInnes #endif 205204ca555eSLois Curfman McInnes } 2053d9822059SBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 20548f1a2a5eSBarry Smith *norm = PetscSqrtReal(*norm); 20553a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 2056329f5518SBarry Smith PetscReal *tmp,*tmp2; 2057b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 2058d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr); 2059d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N+1)*sizeof(PetscReal),&tmp2);CHKERRQ(ierr); 2060d0f46423SBarry Smith ierr = PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));CHKERRQ(ierr); 206104ca555eSLois Curfman McInnes *norm = 0.0; 206204ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 206304ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 2064bfec09a0SHong Zhang tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++; 206504ca555eSLois Curfman McInnes } 206604ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 206704ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 2068bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 206904ca555eSLois Curfman McInnes } 2070d9822059SBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,((PetscObject)mat)->comm);CHKERRQ(ierr); 2071d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 207204ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 207304ca555eSLois Curfman McInnes } 2074606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 2075606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 20763a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 2077329f5518SBarry Smith PetscReal ntemp = 0.0; 2078d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 2079bfec09a0SHong Zhang v = amat->a + amat->i[j]; 208004ca555eSLois Curfman McInnes sum = 0.0; 208104ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 2082cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 208304ca555eSLois Curfman McInnes } 2084bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 208504ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 2086cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 208704ca555eSLois Curfman McInnes } 2088515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 208904ca555eSLois Curfman McInnes } 2090d9822059SBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,((PetscObject)mat)->comm);CHKERRQ(ierr); 2091ca161407SBarry Smith } else { 2092e7e72b3dSBarry Smith SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"No support for two norm"); 209304ca555eSLois Curfman McInnes } 209437fa93a5SLois Curfman McInnes } 20953a40ed3dSBarry Smith PetscFunctionReturn(0); 2096855ac2c5SLois Curfman McInnes } 2097855ac2c5SLois Curfman McInnes 20984a2ae208SSatish Balay #undef __FUNCT__ 20994a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 2100fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 2101b7c46309SBarry Smith { 2102b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2103da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 2104dfbe8321SBarry Smith PetscErrorCode ierr; 210580bcc5a1SJed Brown PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i; 2106d0f46423SBarry Smith PetscInt cstart=A->cmap->rstart,ncol; 21073a40ed3dSBarry Smith Mat B; 2108a77337e4SBarry Smith MatScalar *array; 2109b7c46309SBarry Smith 21103a40ed3dSBarry Smith PetscFunctionBegin; 2111e7e72b3dSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 2112da668accSHong Zhang 211380bcc5a1SJed Brown ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n; 2114da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 2115da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 2116fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 211780bcc5a1SJed Brown PetscInt *d_nnz,*g_nnz,*o_nnz; 211880bcc5a1SJed Brown PetscSFNode *oloc; 2119713c93b4SJed Brown PETSC_UNUSED PetscSF sf; 212080bcc5a1SJed Brown 212180bcc5a1SJed Brown ierr = PetscMalloc4(na,PetscInt,&d_nnz,na,PetscInt,&o_nnz,nb,PetscInt,&g_nnz,nb,PetscSFNode,&oloc);CHKERRQ(ierr); 212280bcc5a1SJed Brown /* compute d_nnz for preallocation */ 212380bcc5a1SJed Brown ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 2124da668accSHong Zhang for (i=0; i<ai[ma]; i++) { 2125da668accSHong Zhang d_nnz[aj[i]] ++; 2126da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 2127d4bb536fSBarry Smith } 212880bcc5a1SJed Brown /* compute local off-diagonal contributions */ 21290beca09bSJed Brown ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr); 213080bcc5a1SJed Brown for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++; 213180bcc5a1SJed Brown /* map those to global */ 213280bcc5a1SJed Brown ierr = PetscSFCreate(((PetscObject)A)->comm,&sf);CHKERRQ(ierr); 213363f4a732SJed Brown ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,PETSC_NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr); 2134e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 213580bcc5a1SJed Brown ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 213680bcc5a1SJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 213780bcc5a1SJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 213880bcc5a1SJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 2139d4bb536fSBarry Smith 21407adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm,&B);CHKERRQ(ierr); 2141d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 2142a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr); 21437adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 214480bcc5a1SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 214580bcc5a1SJed Brown ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr); 2146fc4dec0aSBarry Smith } else { 2147fc4dec0aSBarry Smith B = *matout; 21486ffab4bbSHong Zhang ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 21496ffab4bbSHong Zhang for (i=0; i<ai[ma]; i++){ 21506ffab4bbSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 21516ffab4bbSHong Zhang } 2152fc4dec0aSBarry Smith } 2153b7c46309SBarry Smith 2154b7c46309SBarry Smith /* copy over the A part */ 2155da668accSHong Zhang array = Aloc->a; 2156d0f46423SBarry Smith row = A->rmap->rstart; 2157da668accSHong Zhang for (i=0; i<ma; i++) { 2158da668accSHong Zhang ncol = ai[i+1]-ai[i]; 2159da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 2160da668accSHong Zhang row++; array += ncol; aj += ncol; 2161b7c46309SBarry Smith } 2162b7c46309SBarry Smith aj = Aloc->j; 2163da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 2164b7c46309SBarry Smith 2165b7c46309SBarry Smith /* copy over the B part */ 2166fc73b1b3SBarry Smith ierr = PetscMalloc(bi[mb]*sizeof(PetscInt),&cols);CHKERRQ(ierr); 2167fc73b1b3SBarry Smith ierr = PetscMemzero(cols,bi[mb]*sizeof(PetscInt));CHKERRQ(ierr); 2168da668accSHong Zhang array = Bloc->a; 2169d0f46423SBarry Smith row = A->rmap->rstart; 2170da668accSHong Zhang for (i=0; i<bi[mb]; i++) {cols[i] = a->garray[bj[i]];} 217161a2fbbaSHong Zhang cols_tmp = cols; 2172da668accSHong Zhang for (i=0; i<mb; i++) { 2173da668accSHong Zhang ncol = bi[i+1]-bi[i]; 217461a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 217561a2fbbaSHong Zhang row++; array += ncol; cols_tmp += ncol; 2176b7c46309SBarry Smith } 2177fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 2178fc73b1b3SBarry Smith 21796d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 21806d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2181815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 21820de55854SLois Curfman McInnes *matout = B; 21830de55854SLois Curfman McInnes } else { 2184eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 21850de55854SLois Curfman McInnes } 21863a40ed3dSBarry Smith PetscFunctionReturn(0); 2187b7c46309SBarry Smith } 2188b7c46309SBarry Smith 21894a2ae208SSatish Balay #undef __FUNCT__ 21904a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 2191dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 2192a008b906SSatish Balay { 21934b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 21944b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 2195dfbe8321SBarry Smith PetscErrorCode ierr; 2196b1d57f15SBarry Smith PetscInt s1,s2,s3; 2197a008b906SSatish Balay 21983a40ed3dSBarry Smith PetscFunctionBegin; 21994b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 22004b967eb1SSatish Balay if (rr) { 2201e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 2202e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 22034b967eb1SSatish Balay /* Overlap communication with computation. */ 2204ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2205a008b906SSatish Balay } 22064b967eb1SSatish Balay if (ll) { 2207e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 2208e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 2209f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 22104b967eb1SSatish Balay } 22114b967eb1SSatish Balay /* scale the diagonal block */ 2212f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 22134b967eb1SSatish Balay 22144b967eb1SSatish Balay if (rr) { 22154b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 2216ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2217f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 22184b967eb1SSatish Balay } 22194b967eb1SSatish Balay 22203a40ed3dSBarry Smith PetscFunctionReturn(0); 2221a008b906SSatish Balay } 2222a008b906SSatish Balay 22234a2ae208SSatish Balay #undef __FUNCT__ 22244a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 2225dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 2226bb5a7306SBarry Smith { 2227bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2228dfbe8321SBarry Smith PetscErrorCode ierr; 22293a40ed3dSBarry Smith 22303a40ed3dSBarry Smith PetscFunctionBegin; 2231bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 22323a40ed3dSBarry Smith PetscFunctionReturn(0); 2233bb5a7306SBarry Smith } 2234bb5a7306SBarry Smith 22354a2ae208SSatish Balay #undef __FUNCT__ 22364a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2237ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2238d4bb536fSBarry Smith { 2239d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2240d4bb536fSBarry Smith Mat a,b,c,d; 2241ace3abfcSBarry Smith PetscBool flg; 2242dfbe8321SBarry Smith PetscErrorCode ierr; 2243d4bb536fSBarry Smith 22443a40ed3dSBarry Smith PetscFunctionBegin; 2245d4bb536fSBarry Smith a = matA->A; b = matA->B; 2246d4bb536fSBarry Smith c = matB->A; d = matB->B; 2247d4bb536fSBarry Smith 2248d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2249abc0a331SBarry Smith if (flg) { 2250d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2251d4bb536fSBarry Smith } 2252*c3aae356SJed Brown ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr); 22533a40ed3dSBarry Smith PetscFunctionReturn(0); 2254d4bb536fSBarry Smith } 2255d4bb536fSBarry Smith 22564a2ae208SSatish Balay #undef __FUNCT__ 22574a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2258dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2259cb5b572fSBarry Smith { 2260dfbe8321SBarry Smith PetscErrorCode ierr; 2261cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 2262cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data; 2263cb5b572fSBarry Smith 2264cb5b572fSBarry Smith PetscFunctionBegin; 226533f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 226633f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2267cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2268cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2269cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2270cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2271cb5b572fSBarry Smith then copying the submatrices */ 2272cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2273cb5b572fSBarry Smith } else { 2274cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2275cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2276cb5b572fSBarry Smith } 2277cb5b572fSBarry Smith PetscFunctionReturn(0); 2278cb5b572fSBarry Smith } 2279cb5b572fSBarry Smith 22804a2ae208SSatish Balay #undef __FUNCT__ 22814994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ" 22824994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A) 2283273d9f13SBarry Smith { 2284dfbe8321SBarry Smith PetscErrorCode ierr; 2285273d9f13SBarry Smith 2286273d9f13SBarry Smith PetscFunctionBegin; 2287273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2288273d9f13SBarry Smith PetscFunctionReturn(0); 2289273d9f13SBarry Smith } 2290273d9f13SBarry Smith 2291ac90fabeSBarry Smith #undef __FUNCT__ 229295b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ" 229395b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */ 229495b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt* nnz) 229595b7e79eSJed Brown { 229695b7e79eSJed Brown PetscInt i,m=Y->rmap->N; 229795b7e79eSJed Brown Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 229895b7e79eSJed Brown Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 229995b7e79eSJed Brown const PetscInt *xi = x->i,*yi = y->i; 230095b7e79eSJed Brown 230195b7e79eSJed Brown PetscFunctionBegin; 230295b7e79eSJed Brown /* Set the number of nonzeros in the new matrix */ 230395b7e79eSJed Brown for (i=0; i<m; i++) { 230495b7e79eSJed Brown PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i]; 230595b7e79eSJed Brown const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i]; 230695b7e79eSJed Brown nnz[i] = 0; 230795b7e79eSJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 230895b7e79eSJed Brown for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */ 230995b7e79eSJed Brown if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++; /* Skip duplicate */ 231095b7e79eSJed Brown nnz[i]++; 231195b7e79eSJed Brown } 231295b7e79eSJed Brown for (; k<nzy; k++) nnz[i]++; 231395b7e79eSJed Brown } 231495b7e79eSJed Brown PetscFunctionReturn(0); 231595b7e79eSJed Brown } 231695b7e79eSJed Brown 231795b7e79eSJed Brown #undef __FUNCT__ 2318ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2319f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2320ac90fabeSBarry Smith { 2321dfbe8321SBarry Smith PetscErrorCode ierr; 2322b1d57f15SBarry Smith PetscInt i; 2323ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data; 23244ce68768SBarry Smith PetscBLASInt bnz,one=1; 2325ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2326ac90fabeSBarry Smith 2327ac90fabeSBarry Smith PetscFunctionBegin; 2328ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2329f4df32b1SMatthew Knepley PetscScalar alpha = a; 2330ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->A->data; 2331ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->A->data; 23320805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2333f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2334ac90fabeSBarry Smith x = (Mat_SeqAIJ *)xx->B->data; 2335ac90fabeSBarry Smith y = (Mat_SeqAIJ *)yy->B->data; 23360805154bSBarry Smith bnz = PetscBLASIntCast(x->nz); 2337f4df32b1SMatthew Knepley BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one); 2338a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2339f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2340c537a176SHong Zhang 2341c537a176SHong Zhang x = (Mat_SeqAIJ *)xx->B->data; 2342a30b2313SHong Zhang y = (Mat_SeqAIJ *)yy->B->data; 2343a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2344a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 23456bf464f9SBarry Smith ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr); 2346c537a176SHong Zhang } 2347a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2348d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2349a30b2313SHong Zhang y->XtoY = xx->B; 2350407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2351c537a176SHong Zhang } 2352f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2353ac90fabeSBarry Smith } else { 23549f5f6813SShri Abhyankar Mat B; 23559f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 23569f5f6813SShri Abhyankar ierr = PetscMalloc(yy->A->rmap->N*sizeof(PetscInt),&nnz_d);CHKERRQ(ierr); 23579f5f6813SShri Abhyankar ierr = PetscMalloc(yy->B->rmap->N*sizeof(PetscInt),&nnz_o);CHKERRQ(ierr); 23589f5f6813SShri Abhyankar ierr = MatCreate(((PetscObject)Y)->comm,&B);CHKERRQ(ierr); 2359bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 23609f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 2361a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr); 23629f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 23639f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 236495b7e79eSJed Brown ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr); 2365ecd8bba6SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr); 23669f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 2367a2ea699eSBarry Smith ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr); 23689f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 23699f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2370ac90fabeSBarry Smith } 2371ac90fabeSBarry Smith PetscFunctionReturn(0); 2372ac90fabeSBarry Smith } 2373ac90fabeSBarry Smith 23747087cfbeSBarry Smith extern PetscErrorCode MatConjugate_SeqAIJ(Mat); 2375354c94deSBarry Smith 2376354c94deSBarry Smith #undef __FUNCT__ 2377354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 23787087cfbeSBarry Smith PetscErrorCode MatConjugate_MPIAIJ(Mat mat) 2379354c94deSBarry Smith { 2380354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2381354c94deSBarry Smith PetscErrorCode ierr; 2382354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 2383354c94deSBarry Smith 2384354c94deSBarry Smith PetscFunctionBegin; 2385354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2386354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2387354c94deSBarry Smith #else 2388354c94deSBarry Smith PetscFunctionBegin; 2389354c94deSBarry Smith #endif 2390354c94deSBarry Smith PetscFunctionReturn(0); 2391354c94deSBarry Smith } 2392354c94deSBarry Smith 239399cafbc1SBarry Smith #undef __FUNCT__ 239499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 239599cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 239699cafbc1SBarry Smith { 239799cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 239899cafbc1SBarry Smith PetscErrorCode ierr; 239999cafbc1SBarry Smith 240099cafbc1SBarry Smith PetscFunctionBegin; 240199cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 240299cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 240399cafbc1SBarry Smith PetscFunctionReturn(0); 240499cafbc1SBarry Smith } 240599cafbc1SBarry Smith 240699cafbc1SBarry Smith #undef __FUNCT__ 240799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 240899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 240999cafbc1SBarry Smith { 241099cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 241199cafbc1SBarry Smith PetscErrorCode ierr; 241299cafbc1SBarry Smith 241399cafbc1SBarry Smith PetscFunctionBegin; 241499cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 241599cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 241699cafbc1SBarry Smith PetscFunctionReturn(0); 241799cafbc1SBarry Smith } 241899cafbc1SBarry Smith 2419103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 2420103bf8bdSMatthew Knepley 2421103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2422a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2423a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2424a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2425103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2426a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2427d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2428103bf8bdSMatthew Knepley 2429103bf8bdSMatthew Knepley #undef __FUNCT__ 2430103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2431103bf8bdSMatthew Knepley /* 2432103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2433103bf8bdSMatthew Knepley */ 24340481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2435103bf8bdSMatthew Knepley { 2436a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2437a2c909beSMatthew Knepley 2438a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2439a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2440a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2441a2c909beSMatthew Knepley 2442ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2443776b82aeSLisandro Dalcin PetscContainer c; 2444103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2445103bf8bdSMatthew Knepley PetscErrorCode ierr; 2446103bf8bdSMatthew Knepley 2447103bf8bdSMatthew Knepley PetscFunctionBegin; 2448e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2449103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2450103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2451103bf8bdSMatthew Knepley if (!row_identity || !col_identity) { 2452e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2453103bf8bdSMatthew Knepley } 2454103bf8bdSMatthew Knepley 2455103bf8bdSMatthew Knepley process_group_type pg; 2456a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2457a2c909beSMatthew Knepley lgraph_type* lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2458a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2459a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2460a2c909beSMatthew Knepley 2461103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2462a2c909beSMatthew Knepley ilu_permuted(level_graph); 2463103bf8bdSMatthew Knepley 2464103bf8bdSMatthew Knepley /* put together the new matrix */ 24657adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)A)->comm, fact);CHKERRQ(ierr); 2466103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2467103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2468719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2469a2f3521dSMark F. Adams ierr = MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 2470719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2471719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2472719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2473103bf8bdSMatthew Knepley 24747adad957SLisandro Dalcin ierr = PetscContainerCreate(((PetscObject)A)->comm, &c); 2475776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2476719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2477bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&c); 2478103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2479103bf8bdSMatthew Knepley } 2480103bf8bdSMatthew Knepley 2481103bf8bdSMatthew Knepley #undef __FUNCT__ 2482103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 24830481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2484103bf8bdSMatthew Knepley { 2485103bf8bdSMatthew Knepley PetscFunctionBegin; 2486103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2487103bf8bdSMatthew Knepley } 2488103bf8bdSMatthew Knepley 2489103bf8bdSMatthew Knepley #undef __FUNCT__ 2490103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2491103bf8bdSMatthew Knepley /* 2492103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2493103bf8bdSMatthew Knepley */ 2494103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2495103bf8bdSMatthew Knepley { 2496a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2497a2c909beSMatthew Knepley 2498a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2499a2c909beSMatthew Knepley lgraph_type* lgraph_p; 2500776b82aeSLisandro Dalcin PetscContainer c; 2501103bf8bdSMatthew Knepley PetscErrorCode ierr; 2502103bf8bdSMatthew Knepley 2503103bf8bdSMatthew Knepley PetscFunctionBegin; 2504103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);CHKERRQ(ierr); 2505776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void **) &lgraph_p);CHKERRQ(ierr); 2506103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2507a2c909beSMatthew Knepley 2508a2c909beSMatthew Knepley PetscScalar* array_x; 2509a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2510a2c909beSMatthew Knepley PetscInt sx; 2511a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2512a2c909beSMatthew Knepley 2513a2c909beSMatthew Knepley PetscScalar* array_b; 2514a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2515a2c909beSMatthew Knepley PetscInt sb; 2516a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2517a2c909beSMatthew Knepley 2518a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2519a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2520a2c909beSMatthew Knepley 2521a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 2522a2c909beSMatthew Knepley array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]), 2523a2c909beSMatthew Knepley ref_x(array_x, boost::extents[num_vertices(graph)]); 2524a2c909beSMatthew Knepley 2525a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2526a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 2527a2c909beSMatthew Knepley gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)), 2528a2c909beSMatthew Knepley vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2529a2c909beSMatthew Knepley 2530a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2531a2c909beSMatthew Knepley 2532103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2533103bf8bdSMatthew Knepley } 2534103bf8bdSMatthew Knepley #endif 2535103bf8bdSMatthew Knepley 253669db28dcSHong Zhang typedef struct { /* used by MatGetRedundantMatrix() for reusing matredundant */ 253769db28dcSHong Zhang PetscInt nzlocal,nsends,nrecvs; 25381d79065fSBarry Smith PetscMPIInt *send_rank,*recv_rank; 25391d79065fSBarry Smith PetscInt *sbuf_nz,*rbuf_nz,*sbuf_j,**rbuf_j; 254069db28dcSHong Zhang PetscScalar *sbuf_a,**rbuf_a; 2541bf0cc555SLisandro Dalcin PetscErrorCode (*Destroy)(Mat); 254269db28dcSHong Zhang } Mat_Redundant; 254369db28dcSHong Zhang 254469db28dcSHong Zhang #undef __FUNCT__ 254569db28dcSHong Zhang #define __FUNCT__ "PetscContainerDestroy_MatRedundant" 254669db28dcSHong Zhang PetscErrorCode PetscContainerDestroy_MatRedundant(void *ptr) 254769db28dcSHong Zhang { 254869db28dcSHong Zhang PetscErrorCode ierr; 254969db28dcSHong Zhang Mat_Redundant *redund=(Mat_Redundant*)ptr; 255069db28dcSHong Zhang PetscInt i; 255169db28dcSHong Zhang 255269db28dcSHong Zhang PetscFunctionBegin; 25531d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 255469db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 255569db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 255669db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++){ 255769db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 255869db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 255969db28dcSHong Zhang } 25601d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 256169db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 256269db28dcSHong Zhang PetscFunctionReturn(0); 256369db28dcSHong Zhang } 256469db28dcSHong Zhang 256569db28dcSHong Zhang #undef __FUNCT__ 256669db28dcSHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 256769db28dcSHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 256869db28dcSHong Zhang { 256969db28dcSHong Zhang PetscErrorCode ierr; 257069db28dcSHong Zhang PetscContainer container; 257169db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 257269db28dcSHong Zhang 257369db28dcSHong Zhang PetscFunctionBegin; 257469db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)A,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 2575bf0cc555SLisandro Dalcin if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 257669db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 2577bf0cc555SLisandro Dalcin A->ops->destroy = redund->Destroy; 257869db28dcSHong Zhang ierr = PetscObjectCompose((PetscObject)A,"Mat_Redundant",0);CHKERRQ(ierr); 2579bf0cc555SLisandro Dalcin if (A->ops->destroy) { 258069db28dcSHong Zhang ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 2581bf0cc555SLisandro Dalcin } 258269db28dcSHong Zhang PetscFunctionReturn(0); 258369db28dcSHong Zhang } 258469db28dcSHong Zhang 258569db28dcSHong Zhang #undef __FUNCT__ 258669db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 258769db28dcSHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,PetscInt mlocal_sub,MatReuse reuse,Mat *matredundant) 258869db28dcSHong Zhang { 258969db28dcSHong Zhang PetscMPIInt rank,size; 25907adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mat)->comm; 259169db28dcSHong Zhang PetscErrorCode ierr; 259269db28dcSHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0; 259369db28dcSHong Zhang PetscMPIInt *send_rank=PETSC_NULL,*recv_rank=PETSC_NULL; 2594d0f46423SBarry Smith PetscInt *rowrange=mat->rmap->range; 259569db28dcSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 259669db28dcSHong Zhang Mat A=aij->A,B=aij->B,C=*matredundant; 259769db28dcSHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 259869db28dcSHong Zhang PetscScalar *sbuf_a; 259969db28dcSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2600d0f46423SBarry Smith PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 2601d0f46423SBarry Smith PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray,M,N; 260269db28dcSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2603a77337e4SBarry Smith MatScalar *aworkA,*aworkB; 2604a77337e4SBarry Smith PetscScalar *vals; 260569db28dcSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 260669db28dcSHong Zhang MPI_Request *s_waits1=PETSC_NULL,*s_waits2=PETSC_NULL,*s_waits3=PETSC_NULL, 260769db28dcSHong Zhang *r_waits1=PETSC_NULL,*r_waits2=PETSC_NULL,*r_waits3=PETSC_NULL; 260869db28dcSHong Zhang MPI_Status recv_status,*send_status; 260969db28dcSHong Zhang PetscInt *sbuf_nz=PETSC_NULL,*rbuf_nz=PETSC_NULL,count; 261069db28dcSHong Zhang PetscInt **rbuf_j=PETSC_NULL; 261169db28dcSHong Zhang PetscScalar **rbuf_a=PETSC_NULL; 261269db28dcSHong Zhang Mat_Redundant *redund=PETSC_NULL; 261369db28dcSHong Zhang PetscContainer container; 261469db28dcSHong Zhang 261569db28dcSHong Zhang PetscFunctionBegin; 261669db28dcSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 261769db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 261869db28dcSHong Zhang 261969db28dcSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 262069db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2621e32f2f54SBarry Smith if (M != N || M != mat->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 262269db28dcSHong Zhang ierr = MatGetLocalSize(C,&M,&N);CHKERRQ(ierr); 2623e32f2f54SBarry Smith if (M != N || M != mlocal_sub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong local size"); 262469db28dcSHong Zhang ierr = PetscObjectQuery((PetscObject)C,"Mat_Redundant",(PetscObject *)&container);CHKERRQ(ierr); 2625bf0cc555SLisandro Dalcin if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exit"); 262669db28dcSHong Zhang ierr = PetscContainerGetPointer(container,(void **)&redund);CHKERRQ(ierr); 2627e32f2f54SBarry Smith if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 262869db28dcSHong Zhang 262969db28dcSHong Zhang nsends = redund->nsends; 263069db28dcSHong Zhang nrecvs = redund->nrecvs; 26311d79065fSBarry Smith send_rank = redund->send_rank; 26321d79065fSBarry Smith recv_rank = redund->recv_rank; 26331d79065fSBarry Smith sbuf_nz = redund->sbuf_nz; 26341d79065fSBarry Smith rbuf_nz = redund->rbuf_nz; 263569db28dcSHong Zhang sbuf_j = redund->sbuf_j; 263669db28dcSHong Zhang sbuf_a = redund->sbuf_a; 263769db28dcSHong Zhang rbuf_j = redund->rbuf_j; 263869db28dcSHong Zhang rbuf_a = redund->rbuf_a; 263969db28dcSHong Zhang } 264069db28dcSHong Zhang 264169db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 264269db28dcSHong Zhang PetscMPIInt subrank,subsize; 264369db28dcSHong Zhang PetscInt nleftover,np_subcomm; 264469db28dcSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 264569db28dcSHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 264669db28dcSHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2647a2ea699eSBarry Smith ierr = PetscMalloc2(size,PetscMPIInt,&send_rank,size,PetscMPIInt,&recv_rank);CHKERRQ(ierr); 264869db28dcSHong Zhang np_subcomm = size/nsubcomm; 264969db28dcSHong Zhang nleftover = size - nsubcomm*np_subcomm; 265069db28dcSHong Zhang nsends = 0; nrecvs = 0; 265169db28dcSHong Zhang for (i=0; i<size; i++){ /* i=rank*/ 265269db28dcSHong Zhang if (subrank == i/nsubcomm && rank != i){ /* my_subrank == other's subrank */ 265369db28dcSHong Zhang send_rank[nsends] = i; nsends++; 265469db28dcSHong Zhang recv_rank[nrecvs++] = i; 265569db28dcSHong Zhang } 265669db28dcSHong Zhang } 265769db28dcSHong Zhang if (rank >= size - nleftover){/* this proc is a leftover processor */ 265869db28dcSHong Zhang i = size-nleftover-1; 265969db28dcSHong Zhang j = 0; 266069db28dcSHong Zhang while (j < nsubcomm - nleftover){ 266169db28dcSHong Zhang send_rank[nsends++] = i; 266269db28dcSHong Zhang i--; j++; 266369db28dcSHong Zhang } 266469db28dcSHong Zhang } 266569db28dcSHong Zhang 266669db28dcSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1){ /* this proc recvs from leftover processors */ 266769db28dcSHong Zhang for (i=0; i<nleftover; i++){ 266869db28dcSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 266969db28dcSHong Zhang } 267069db28dcSHong Zhang } 267169db28dcSHong Zhang 267269db28dcSHong Zhang /* allocate sbuf_j, sbuf_a */ 267369db28dcSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 267469db28dcSHong Zhang ierr = PetscMalloc(i*sizeof(PetscInt),&sbuf_j);CHKERRQ(ierr); 267569db28dcSHong Zhang ierr = PetscMalloc((nzlocal+1)*sizeof(PetscScalar),&sbuf_a);CHKERRQ(ierr); 267669db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 267769db28dcSHong Zhang 267869db28dcSHong Zhang /* copy mat's local entries into the buffers */ 267969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 268069db28dcSHong Zhang rownz_max = 0; 268169db28dcSHong Zhang rptr = sbuf_j; 268269db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 268369db28dcSHong Zhang vals = sbuf_a; 268469db28dcSHong Zhang rptr[0] = 0; 268569db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 268669db28dcSHong Zhang row = i + rstart; 268769db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 268869db28dcSHong Zhang ncols = nzA + nzB; 268969db28dcSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 269069db28dcSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 269169db28dcSHong Zhang /* load the column indices for this row into cols */ 269269db28dcSHong Zhang lwrite = 0; 269369db28dcSHong Zhang for (l=0; l<nzB; l++) { 269469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart){ 269569db28dcSHong Zhang vals[lwrite] = aworkB[l]; 269669db28dcSHong Zhang cols[lwrite++] = ctmp; 269769db28dcSHong Zhang } 269869db28dcSHong Zhang } 269969db28dcSHong Zhang for (l=0; l<nzA; l++){ 270069db28dcSHong Zhang vals[lwrite] = aworkA[l]; 270169db28dcSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 270269db28dcSHong Zhang } 270369db28dcSHong Zhang for (l=0; l<nzB; l++) { 270469db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend){ 270569db28dcSHong Zhang vals[lwrite] = aworkB[l]; 270669db28dcSHong Zhang cols[lwrite++] = ctmp; 270769db28dcSHong Zhang } 270869db28dcSHong Zhang } 270969db28dcSHong Zhang vals += ncols; 271069db28dcSHong Zhang cols += ncols; 271169db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 271269db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 271369db28dcSHong Zhang } 2714e32f2f54SBarry Smith if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz); 271569db28dcSHong Zhang } else { /* only copy matrix values into sbuf_a */ 271669db28dcSHong Zhang rptr = sbuf_j; 271769db28dcSHong Zhang vals = sbuf_a; 271869db28dcSHong Zhang rptr[0] = 0; 271969db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 272069db28dcSHong Zhang row = i + rstart; 272169db28dcSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 272269db28dcSHong Zhang ncols = nzA + nzB; 2723a2ea699eSBarry Smith cworkB = b->j + b->i[i]; 2724a2ea699eSBarry Smith aworkA = a->a + a->i[i]; 2725a2ea699eSBarry Smith aworkB = b->a + b->i[i]; 272669db28dcSHong Zhang lwrite = 0; 272769db28dcSHong Zhang for (l=0; l<nzB; l++) { 272869db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 272969db28dcSHong Zhang } 273069db28dcSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 273169db28dcSHong Zhang for (l=0; l<nzB; l++) { 273269db28dcSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 273369db28dcSHong Zhang } 273469db28dcSHong Zhang vals += ncols; 273569db28dcSHong Zhang rptr[i+1] = rptr[i] + ncols; 273669db28dcSHong Zhang } 273769db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 273869db28dcSHong Zhang 273969db28dcSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 274069db28dcSHong Zhang /*--------------------------------------------------*/ 274169db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 274269db28dcSHong Zhang ierr = PetscMalloc2(3*(nsends + nrecvs)+1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 274369db28dcSHong Zhang s_waits2 = s_waits3 + nsends; 274469db28dcSHong Zhang s_waits1 = s_waits2 + nsends; 274569db28dcSHong Zhang r_waits1 = s_waits1 + nsends; 274669db28dcSHong Zhang r_waits2 = r_waits1 + nrecvs; 274769db28dcSHong Zhang r_waits3 = r_waits2 + nrecvs; 274869db28dcSHong Zhang } else { 274969db28dcSHong Zhang ierr = PetscMalloc2(nsends + nrecvs +1,MPI_Request,&s_waits3,nsends+1,MPI_Status,&send_status);CHKERRQ(ierr); 275069db28dcSHong Zhang r_waits3 = s_waits3 + nsends; 275169db28dcSHong Zhang } 275269db28dcSHong Zhang 275369db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 275469db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 275569db28dcSHong Zhang /* get new tags to keep the communication clean */ 275669db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 275769db28dcSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 27581d79065fSBarry Smith ierr = PetscMalloc4(nsends,PetscInt,&sbuf_nz,nrecvs,PetscInt,&rbuf_nz,nrecvs,PetscInt*,&rbuf_j,nrecvs,PetscScalar*,&rbuf_a);CHKERRQ(ierr); 275969db28dcSHong Zhang 276069db28dcSHong Zhang /* post receives of other's nzlocal */ 276169db28dcSHong Zhang for (i=0; i<nrecvs; i++){ 276269db28dcSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 276369db28dcSHong Zhang } 276469db28dcSHong Zhang /* send nzlocal to others */ 276569db28dcSHong Zhang for (i=0; i<nsends; i++){ 276669db28dcSHong Zhang sbuf_nz[i] = nzlocal; 276769db28dcSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 276869db28dcSHong Zhang } 276969db28dcSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 277069db28dcSHong Zhang count = nrecvs; 277169db28dcSHong Zhang while (count) { 277269db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 277369db28dcSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 277469db28dcSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 277569db28dcSHong Zhang ierr = PetscMalloc((rbuf_nz[imdex]+1)*sizeof(PetscScalar),&rbuf_a[imdex]);CHKERRQ(ierr); 277669db28dcSHong Zhang 277769db28dcSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 277869db28dcSHong Zhang rbuf_nz[imdex] += i + 2; 277969db28dcSHong Zhang ierr = PetscMalloc(rbuf_nz[imdex]*sizeof(PetscInt),&rbuf_j[imdex]);CHKERRQ(ierr); 278069db28dcSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 278169db28dcSHong Zhang count--; 278269db28dcSHong Zhang } 278369db28dcSHong Zhang /* wait on sends of nzlocal */ 278469db28dcSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 278569db28dcSHong Zhang /* send mat->i,j to others, and recv from other's */ 278669db28dcSHong Zhang /*------------------------------------------------*/ 278769db28dcSHong Zhang for (i=0; i<nsends; i++){ 278869db28dcSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 278969db28dcSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 279069db28dcSHong Zhang } 279169db28dcSHong Zhang /* wait on receives of mat->i,j */ 279269db28dcSHong Zhang /*------------------------------*/ 279369db28dcSHong Zhang count = nrecvs; 279469db28dcSHong Zhang while (count) { 279569db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2796e32f2f54SBarry Smith if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 279769db28dcSHong Zhang count--; 279869db28dcSHong Zhang } 279969db28dcSHong Zhang /* wait on sends of mat->i,j */ 280069db28dcSHong Zhang /*---------------------------*/ 280169db28dcSHong Zhang if (nsends) { 280269db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 280369db28dcSHong Zhang } 280469db28dcSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 280569db28dcSHong Zhang 280669db28dcSHong Zhang /* post receives, send and receive mat->a */ 280769db28dcSHong Zhang /*----------------------------------------*/ 280869db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 280969db28dcSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 281069db28dcSHong Zhang } 281169db28dcSHong Zhang for (i=0; i<nsends; i++){ 281269db28dcSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 281369db28dcSHong Zhang } 281469db28dcSHong Zhang count = nrecvs; 281569db28dcSHong Zhang while (count) { 281669db28dcSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2817e32f2f54SBarry Smith if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 281869db28dcSHong Zhang count--; 281969db28dcSHong Zhang } 282069db28dcSHong Zhang if (nsends) { 282169db28dcSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 282269db28dcSHong Zhang } 282369db28dcSHong Zhang 282469db28dcSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 282569db28dcSHong Zhang 282669db28dcSHong Zhang /* create redundant matrix */ 282769db28dcSHong Zhang /*-------------------------*/ 282869db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX){ 282969db28dcSHong Zhang /* compute rownz_max for preallocation */ 283069db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 283169db28dcSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 283269db28dcSHong Zhang rptr = rbuf_j[imdex]; 283369db28dcSHong Zhang for (i=0; i<j; i++){ 283469db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 283569db28dcSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 283669db28dcSHong Zhang } 283769db28dcSHong Zhang } 283869db28dcSHong Zhang 283969db28dcSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 284069db28dcSHong Zhang ierr = MatSetSizes(C,mlocal_sub,mlocal_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 2841a2f3521dSMark F. Adams ierr = MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr); 284269db28dcSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 284369db28dcSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,PETSC_NULL);CHKERRQ(ierr); 284469db28dcSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,PETSC_NULL,rownz_max,PETSC_NULL);CHKERRQ(ierr); 284569db28dcSHong Zhang } else { 284669db28dcSHong Zhang C = *matredundant; 284769db28dcSHong Zhang } 284869db28dcSHong Zhang 284969db28dcSHong Zhang /* insert local matrix entries */ 285069db28dcSHong Zhang rptr = sbuf_j; 285169db28dcSHong Zhang cols = sbuf_j + rend-rstart + 1; 285269db28dcSHong Zhang vals = sbuf_a; 285369db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 285469db28dcSHong Zhang row = i + rstart; 285569db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 285669db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 285769db28dcSHong Zhang vals += ncols; 285869db28dcSHong Zhang cols += ncols; 285969db28dcSHong Zhang } 286069db28dcSHong Zhang /* insert received matrix entries */ 286169db28dcSHong Zhang for (imdex=0; imdex<nrecvs; imdex++){ 286269db28dcSHong Zhang rstart = rowrange[recv_rank[imdex]]; 286369db28dcSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 286469db28dcSHong Zhang rptr = rbuf_j[imdex]; 286569db28dcSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 286669db28dcSHong Zhang vals = rbuf_a[imdex]; 286769db28dcSHong Zhang for (i=0; i<rend-rstart; i++){ 286869db28dcSHong Zhang row = i + rstart; 286969db28dcSHong Zhang ncols = rptr[i+1] - rptr[i]; 287069db28dcSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 287169db28dcSHong Zhang vals += ncols; 287269db28dcSHong Zhang cols += ncols; 287369db28dcSHong Zhang } 287469db28dcSHong Zhang } 287569db28dcSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 287669db28dcSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 287769db28dcSHong Zhang ierr = MatGetSize(C,&M,&N);CHKERRQ(ierr); 2878e32f2f54SBarry Smith if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"redundant mat size %d != input mat size %d",M,mat->rmap->N); 287969db28dcSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 288069db28dcSHong Zhang PetscContainer container; 288169db28dcSHong Zhang *matredundant = C; 288269db28dcSHong Zhang /* create a supporting struct and attach it to C for reuse */ 288338f2d2fdSLisandro Dalcin ierr = PetscNewLog(C,Mat_Redundant,&redund);CHKERRQ(ierr); 288469db28dcSHong Zhang ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 288569db28dcSHong Zhang ierr = PetscContainerSetPointer(container,redund);CHKERRQ(ierr); 288669db28dcSHong Zhang ierr = PetscContainerSetUserDestroy(container,PetscContainerDestroy_MatRedundant);CHKERRQ(ierr); 2887bf0cc555SLisandro Dalcin ierr = PetscObjectCompose((PetscObject)C,"Mat_Redundant",(PetscObject)container);CHKERRQ(ierr); 2888bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 288969db28dcSHong Zhang 289069db28dcSHong Zhang redund->nzlocal = nzlocal; 289169db28dcSHong Zhang redund->nsends = nsends; 289269db28dcSHong Zhang redund->nrecvs = nrecvs; 289369db28dcSHong Zhang redund->send_rank = send_rank; 28941d79065fSBarry Smith redund->recv_rank = recv_rank; 289569db28dcSHong Zhang redund->sbuf_nz = sbuf_nz; 28961d79065fSBarry Smith redund->rbuf_nz = rbuf_nz; 289769db28dcSHong Zhang redund->sbuf_j = sbuf_j; 289869db28dcSHong Zhang redund->sbuf_a = sbuf_a; 289969db28dcSHong Zhang redund->rbuf_j = rbuf_j; 290069db28dcSHong Zhang redund->rbuf_a = rbuf_a; 290169db28dcSHong Zhang 2902bf0cc555SLisandro Dalcin redund->Destroy = C->ops->destroy; 290369db28dcSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 290469db28dcSHong Zhang } 290569db28dcSHong Zhang PetscFunctionReturn(0); 290669db28dcSHong Zhang } 290769db28dcSHong Zhang 290803bc72f1SMatthew Knepley #undef __FUNCT__ 2909c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2910c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2911c91732d9SHong Zhang { 2912c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2913c91732d9SHong Zhang PetscErrorCode ierr; 2914c91732d9SHong Zhang PetscInt i,*idxb = 0; 2915c91732d9SHong Zhang PetscScalar *va,*vb; 2916c91732d9SHong Zhang Vec vtmp; 2917c91732d9SHong Zhang 2918c91732d9SHong Zhang PetscFunctionBegin; 2919c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2920c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2921c91732d9SHong Zhang if (idx) { 2922192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2923d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2924c91732d9SHong Zhang } 2925c91732d9SHong Zhang } 2926c91732d9SHong Zhang 2927d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2928c91732d9SHong Zhang if (idx) { 2929d0f46423SBarry Smith ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2930c91732d9SHong Zhang } 2931c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2932c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2933c91732d9SHong Zhang 2934d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++){ 2935c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2936c91732d9SHong Zhang va[i] = vb[i]; 2937c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2938c91732d9SHong Zhang } 2939c91732d9SHong Zhang } 2940c91732d9SHong Zhang 2941c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2942c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2943c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 29446bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2945c91732d9SHong Zhang PetscFunctionReturn(0); 2946c91732d9SHong Zhang } 2947c91732d9SHong Zhang 2948c91732d9SHong Zhang #undef __FUNCT__ 2949c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2950c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2951c87e5d42SMatthew Knepley { 2952c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2953c87e5d42SMatthew Knepley PetscErrorCode ierr; 2954c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2955c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2956c87e5d42SMatthew Knepley Vec vtmp; 2957c87e5d42SMatthew Knepley 2958c87e5d42SMatthew Knepley PetscFunctionBegin; 2959c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2960c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2961c87e5d42SMatthew Knepley if (idx) { 2962c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2963c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2964c87e5d42SMatthew Knepley } 2965c87e5d42SMatthew Knepley } 2966c87e5d42SMatthew Knepley 2967c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2968c87e5d42SMatthew Knepley if (idx) { 2969c87e5d42SMatthew Knepley ierr = PetscMalloc(A->rmap->n*sizeof(PetscInt),&idxb);CHKERRQ(ierr); 2970c87e5d42SMatthew Knepley } 2971c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2972c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2973c87e5d42SMatthew Knepley 2974c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++){ 2975c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2976c87e5d42SMatthew Knepley va[i] = vb[i]; 2977c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2978c87e5d42SMatthew Knepley } 2979c87e5d42SMatthew Knepley } 2980c87e5d42SMatthew Knepley 2981c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2982c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2983c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 29846bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2985c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2986c87e5d42SMatthew Knepley } 2987c87e5d42SMatthew Knepley 2988c87e5d42SMatthew Knepley #undef __FUNCT__ 298903bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 299003bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 299103bc72f1SMatthew Knepley { 299203bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 2993d0f46423SBarry Smith PetscInt n = A->rmap->n; 2994d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 299503bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 299603bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 299703bc72f1SMatthew Knepley Vec diagV, offdiagV; 299803bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 299903bc72f1SMatthew Knepley PetscInt r; 300003bc72f1SMatthew Knepley PetscErrorCode ierr; 300103bc72f1SMatthew Knepley 300203bc72f1SMatthew Knepley PetscFunctionBegin; 300303bc72f1SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 3004e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &diagV);CHKERRQ(ierr); 3005e64afeacSLisandro Dalcin ierr = VecCreateSeq(((PetscObject)A)->comm, n, &offdiagV);CHKERRQ(ierr); 300603bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 300703bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 300803bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 300903bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 301003bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 301103bc72f1SMatthew Knepley for (r = 0; r < n; ++r) { 3012028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 301303bc72f1SMatthew Knepley a[r] = diagA[r]; 301403bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 301503bc72f1SMatthew Knepley } else { 301603bc72f1SMatthew Knepley a[r] = offdiagA[r]; 301703bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 301803bc72f1SMatthew Knepley } 301903bc72f1SMatthew Knepley } 302003bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 302103bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 302203bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30236bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30246bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 302503bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 302603bc72f1SMatthew Knepley PetscFunctionReturn(0); 302703bc72f1SMatthew Knepley } 302803bc72f1SMatthew Knepley 30295494a064SHong Zhang #undef __FUNCT__ 3030c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 3031c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 3032c87e5d42SMatthew Knepley { 3033c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 3034c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 3035c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 3036c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 3037c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 3038c87e5d42SMatthew Knepley Vec diagV, offdiagV; 3039c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 3040c87e5d42SMatthew Knepley PetscInt r; 3041c87e5d42SMatthew Knepley PetscErrorCode ierr; 3042c87e5d42SMatthew Knepley 3043c87e5d42SMatthew Knepley PetscFunctionBegin; 3044c87e5d42SMatthew Knepley ierr = PetscMalloc2(n,PetscInt,&diagIdx,n,PetscInt,&offdiagIdx);CHKERRQ(ierr); 3045d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr); 3046d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr); 3047c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 3048c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 3049c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 3050c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 3051c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 3052c87e5d42SMatthew Knepley for (r = 0; r < n; ++r) { 3053c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 3054c87e5d42SMatthew Knepley a[r] = diagA[r]; 3055c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 3056c87e5d42SMatthew Knepley } else { 3057c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 3058c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 3059c87e5d42SMatthew Knepley } 3060c87e5d42SMatthew Knepley } 3061c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 3062c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 3063c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30646bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30656bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 3066c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 3067c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3068c87e5d42SMatthew Knepley } 3069c87e5d42SMatthew Knepley 3070c87e5d42SMatthew Knepley #undef __FUNCT__ 3071d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ" 3072d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat) 30735494a064SHong Zhang { 30745494a064SHong Zhang PetscErrorCode ierr; 3075f6d58c54SBarry Smith Mat *dummy; 30765494a064SHong Zhang 30775494a064SHong Zhang PetscFunctionBegin; 3078f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 3079f6d58c54SBarry Smith *newmat = *dummy; 3080f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 30815494a064SHong Zhang PetscFunctionReturn(0); 30825494a064SHong Zhang } 30835494a064SHong Zhang 30847087cfbeSBarry Smith extern PetscErrorCode MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*); 3085bbead8a2SBarry Smith 3086bbead8a2SBarry Smith #undef __FUNCT__ 3087bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ" 3088713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values) 3089bbead8a2SBarry Smith { 3090bbead8a2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 3091bbead8a2SBarry Smith PetscErrorCode ierr; 3092bbead8a2SBarry Smith 3093bbead8a2SBarry Smith PetscFunctionBegin; 3094bbead8a2SBarry Smith ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr); 3095bbead8a2SBarry Smith PetscFunctionReturn(0); 3096bbead8a2SBarry Smith } 3097bbead8a2SBarry Smith 309873a71a0fSBarry Smith #undef __FUNCT__ 309973a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ" 310073a71a0fSBarry Smith static PetscErrorCode MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx) 310173a71a0fSBarry Smith { 310273a71a0fSBarry Smith PetscErrorCode ierr; 310373a71a0fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)x->data; 310473a71a0fSBarry Smith 310573a71a0fSBarry Smith PetscFunctionBegin; 310673a71a0fSBarry Smith ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr); 310773a71a0fSBarry Smith ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr); 310873a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 310973a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 311073a71a0fSBarry Smith PetscFunctionReturn(0); 311173a71a0fSBarry Smith } 3112bbead8a2SBarry Smith 31138a729477SBarry Smith /* -------------------------------------------------------------------*/ 3114cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 3115cda55fadSBarry Smith MatGetRow_MPIAIJ, 3116cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 3117cda55fadSBarry Smith MatMult_MPIAIJ, 311897304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 31197c922b88SBarry Smith MatMultTranspose_MPIAIJ, 31207c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 3121103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 3122103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 3123103bf8bdSMatthew Knepley #else 3124cda55fadSBarry Smith 0, 3125103bf8bdSMatthew Knepley #endif 3126cda55fadSBarry Smith 0, 3127cda55fadSBarry Smith 0, 312897304618SKris Buschelman /*10*/ 0, 3129cda55fadSBarry Smith 0, 3130cda55fadSBarry Smith 0, 313141f059aeSBarry Smith MatSOR_MPIAIJ, 3132b7c46309SBarry Smith MatTranspose_MPIAIJ, 313397304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 3134cda55fadSBarry Smith MatEqual_MPIAIJ, 3135cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 3136cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 3137cda55fadSBarry Smith MatNorm_MPIAIJ, 313897304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 3139cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 3140cda55fadSBarry Smith MatSetOption_MPIAIJ, 3141cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 3142d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 3143cda55fadSBarry Smith 0, 3144103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 3145719d5645SBarry Smith 0, 3146103bf8bdSMatthew Knepley #else 3147cda55fadSBarry Smith 0, 3148103bf8bdSMatthew Knepley #endif 3149cda55fadSBarry Smith 0, 3150cda55fadSBarry Smith 0, 31514994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ, 3152103bf8bdSMatthew Knepley #ifdef PETSC_HAVE_PBGL 3153719d5645SBarry Smith 0, 3154103bf8bdSMatthew Knepley #else 3155cda55fadSBarry Smith 0, 3156103bf8bdSMatthew Knepley #endif 3157cda55fadSBarry Smith 0, 3158cda55fadSBarry Smith 0, 3159cda55fadSBarry Smith 0, 3160d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 3161cda55fadSBarry Smith 0, 3162cda55fadSBarry Smith 0, 3163cda55fadSBarry Smith 0, 3164cda55fadSBarry Smith 0, 3165d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 3166cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 3167cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 3168cda55fadSBarry Smith MatGetValues_MPIAIJ, 3169cb5b572fSBarry Smith MatCopy_MPIAIJ, 3170d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 3171cda55fadSBarry Smith MatScale_MPIAIJ, 3172cda55fadSBarry Smith 0, 3173cda55fadSBarry Smith 0, 3174564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 317573a71a0fSBarry Smith /*49*/ MatSetRandom_MPIAIJ, 3176cda55fadSBarry Smith 0, 3177cda55fadSBarry Smith 0, 3178cda55fadSBarry Smith 0, 3179cda55fadSBarry Smith 0, 3180d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_MPIAIJ, 3181cda55fadSBarry Smith 0, 3182cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 318372e6a0cfSJed Brown MatPermute_MPIAIJ, 3184cda55fadSBarry Smith 0, 3185d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 3186e03a110bSBarry Smith MatDestroy_MPIAIJ, 3187e03a110bSBarry Smith MatView_MPIAIJ, 3188357abbc8SBarry Smith 0, 3189f996eeb8SHong Zhang MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ, 3190f996eeb8SHong Zhang /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ, 3191f996eeb8SHong Zhang MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ, 3192a2243be0SBarry Smith 0, 3193a2243be0SBarry Smith 0, 3194a2243be0SBarry Smith 0, 3195d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 3196c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 3197a2243be0SBarry Smith 0, 3198a2243be0SBarry Smith MatSetColoring_MPIAIJ, 3199dcf5cc72SBarry Smith 0, 320097304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 32013acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 320297304618SKris Buschelman 0, 320397304618SKris Buschelman 0, 320497304618SKris Buschelman 0, 3205f1f41ecbSJed Brown MatFindZeroDiagonals_MPIAIJ, 320697304618SKris Buschelman /*80*/ 0, 320797304618SKris Buschelman 0, 320897304618SKris Buschelman 0, 32095bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 32106284ec50SHong Zhang 0, 32116284ec50SHong Zhang 0, 32126284ec50SHong Zhang 0, 32136284ec50SHong Zhang 0, 3214865e5f61SKris Buschelman 0, 3215d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 321626be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 321726be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 3218cf3ca8ceSHong Zhang MatPtAP_MPIAIJ_MPIAIJ, 3219cf3ca8ceSHong Zhang MatPtAPSymbolic_MPIAIJ_MPIAIJ, 3220cf3ca8ceSHong Zhang /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ, 32217a7894deSKris Buschelman 0, 32227a7894deSKris Buschelman 0, 32237a7894deSKris Buschelman 0, 32247a7894deSKris Buschelman 0, 3225d519adbfSMatthew Knepley /*99*/ 0, 3226d2b207f1SPeter Brune 0, 3227d2b207f1SPeter Brune 0, 32282fd7e33dSBarry Smith MatConjugate_MPIAIJ, 32292fd7e33dSBarry Smith 0, 3230d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 323199cafbc1SBarry Smith MatRealPart_MPIAIJ, 323269db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 323369db28dcSHong Zhang 0, 323469db28dcSHong Zhang 0, 3235d519adbfSMatthew Knepley /*109*/0, 323603bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 32375494a064SHong Zhang MatGetRowMin_MPIAIJ, 32385494a064SHong Zhang 0, 32395494a064SHong Zhang 0, 3240d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ, 3241bd0c2dcbSBarry Smith 0, 3242bd0c2dcbSBarry Smith 0, 3243bd0c2dcbSBarry Smith 0, 3244bd0c2dcbSBarry Smith 0, 32458fb81238SShri Abhyankar /*119*/0, 32468fb81238SShri Abhyankar 0, 32478fb81238SShri Abhyankar 0, 3248d6037b41SHong Zhang 0, 3249b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 3250f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ, 32510716a85fSBarry Smith MatGetColumnNorms_MPIAIJ, 3252bbead8a2SBarry Smith MatInvertBlockDiagonal_MPIAIJ, 3253b9614d88SDmitry Karpeev 0, 325437868618SMatthew G Knepley MatGetSubMatricesParallel_MPIAIJ, 3255187b3c17SHong Zhang /*129*/0, 3256187b3c17SHong Zhang MatTransposeMatMult_MPIAIJ_MPIAIJ, 3257187b3c17SHong Zhang MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ, 3258187b3c17SHong Zhang MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ, 3259187b3c17SHong Zhang 0, 3260187b3c17SHong Zhang /*134*/0, 3261187b3c17SHong Zhang 0, 3262187b3c17SHong Zhang 0, 3263187b3c17SHong Zhang 0, 3264187b3c17SHong Zhang 0 3265bd0c2dcbSBarry Smith }; 326636ce4990SBarry Smith 32672e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 32682e8a6d31SBarry Smith 3269fb2e594dSBarry Smith EXTERN_C_BEGIN 32704a2ae208SSatish Balay #undef __FUNCT__ 32714a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 32727087cfbeSBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat) 32732e8a6d31SBarry Smith { 32742e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 3275dfbe8321SBarry Smith PetscErrorCode ierr; 32762e8a6d31SBarry Smith 32772e8a6d31SBarry Smith PetscFunctionBegin; 32782e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 32792e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 32802e8a6d31SBarry Smith PetscFunctionReturn(0); 32812e8a6d31SBarry Smith } 3282fb2e594dSBarry Smith EXTERN_C_END 32832e8a6d31SBarry Smith 3284fb2e594dSBarry Smith EXTERN_C_BEGIN 32854a2ae208SSatish Balay #undef __FUNCT__ 32864a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 32877087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat) 32882e8a6d31SBarry Smith { 32892e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data; 3290dfbe8321SBarry Smith PetscErrorCode ierr; 32912e8a6d31SBarry Smith 32922e8a6d31SBarry Smith PetscFunctionBegin; 32932e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 32942e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 32952e8a6d31SBarry Smith PetscFunctionReturn(0); 32962e8a6d31SBarry Smith } 3297fb2e594dSBarry Smith EXTERN_C_END 32988a729477SBarry Smith 329927508adbSBarry Smith EXTERN_C_BEGIN 33004a2ae208SSatish Balay #undef __FUNCT__ 3301a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 33027087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3303a23d5eceSKris Buschelman { 3304a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3305dfbe8321SBarry Smith PetscErrorCode ierr; 3306b1d57f15SBarry Smith PetscInt i; 33072576faa2SJed Brown PetscBool d_realalloc = PETSC_FALSE,o_realalloc = PETSC_FALSE; 3308a23d5eceSKris Buschelman 3309a23d5eceSKris Buschelman PetscFunctionBegin; 33102576faa2SJed Brown if (d_nz >= 0 || d_nnz) d_realalloc = PETSC_TRUE; 33112576faa2SJed Brown if (o_nz >= 0 || o_nnz) o_realalloc = PETSC_TRUE; 3312a23d5eceSKris Buschelman if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5; 3313a23d5eceSKris Buschelman if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2; 3314e32f2f54SBarry Smith if (d_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz); 3315e32f2f54SBarry Smith if (o_nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz); 3316899cda47SBarry Smith 331726283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 331826283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3319a23d5eceSKris Buschelman if (d_nnz) { 3320d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3321e32f2f54SBarry Smith if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]); 3322a23d5eceSKris Buschelman } 3323a23d5eceSKris Buschelman } 3324a23d5eceSKris Buschelman if (o_nnz) { 3325d0f46423SBarry Smith for (i=0; i<B->rmap->n; i++) { 3326e32f2f54SBarry Smith if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]); 3327a23d5eceSKris Buschelman } 3328a23d5eceSKris Buschelman } 3329a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3330899cda47SBarry Smith 3331526dfc15SBarry Smith if (!B->preallocated) { 3332899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3333899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3334d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3335f9e9af59SJed Brown ierr = MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3336899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 3337899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->A);CHKERRQ(ierr); 3338899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3339d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3340f9e9af59SJed Brown ierr = MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3341899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 3342899cda47SBarry Smith ierr = PetscLogObjectParent(B,b->B);CHKERRQ(ierr); 3343526dfc15SBarry Smith } 3344899cda47SBarry Smith 3345c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3346c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 33472576faa2SJed Brown /* Do not error if the user did not give real preallocation information. Ugly because this would overwrite a previous user call to MatSetOption(). */ 33482576faa2SJed Brown if (!d_realalloc) {ierr = MatSetOption(b->A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);} 33492576faa2SJed Brown if (!o_realalloc) {ierr = MatSetOption(b->B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr);} 3350526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3351a23d5eceSKris Buschelman PetscFunctionReturn(0); 3352a23d5eceSKris Buschelman } 3353a23d5eceSKris Buschelman EXTERN_C_END 3354a23d5eceSKris Buschelman 33554a2ae208SSatish Balay #undef __FUNCT__ 33564a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3357dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3358d6dfbf8fSBarry Smith { 3359d6dfbf8fSBarry Smith Mat mat; 3360416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3361dfbe8321SBarry Smith PetscErrorCode ierr; 3362d6dfbf8fSBarry Smith 33633a40ed3dSBarry Smith PetscFunctionBegin; 3364416022c9SBarry Smith *newmat = 0; 33657adad957SLisandro Dalcin ierr = MatCreate(((PetscObject)matin)->comm,&mat);CHKERRQ(ierr); 3366d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 3367a2f3521dSMark F. Adams ierr = MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);CHKERRQ(ierr); 33687adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 33691d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3370273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3371e1b6402fSHong Zhang 3372d5f3da31SBarry Smith mat->factortype = matin->factortype; 3373d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3374a2f3521dSMark F. Adams mat->cmap->bs = matin->cmap->bs; 3375c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3376e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3377273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3378d6dfbf8fSBarry Smith 337917699dbbSLois Curfman McInnes a->size = oldmat->size; 338017699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3381e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3382e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3383e7641de0SSatish Balay a->rowindices = 0; 3384bcd2baecSBarry Smith a->rowvalues = 0; 3385bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3386d6dfbf8fSBarry Smith 33871e1e43feSBarry Smith ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr); 33881e1e43feSBarry Smith ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3389899cda47SBarry Smith 33902ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3391aa482453SBarry Smith #if defined (PETSC_USE_CTABLE) 33920f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3393b1fc9764SSatish Balay #else 3394d0f46423SBarry Smith ierr = PetscMalloc((mat->cmap->N)*sizeof(PetscInt),&a->colmap);CHKERRQ(ierr); 3395d0f46423SBarry Smith ierr = PetscLogObjectMemory(mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3396d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3397b1fc9764SSatish Balay #endif 3398416022c9SBarry Smith } else a->colmap = 0; 33993f41c07dSBarry Smith if (oldmat->garray) { 3400b1d57f15SBarry Smith PetscInt len; 3401d0f46423SBarry Smith len = oldmat->B->cmap->n; 3402b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);CHKERRQ(ierr); 340352e6d16bSBarry Smith ierr = PetscLogObjectMemory(mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3404b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3405416022c9SBarry Smith } else a->garray = 0; 3406d6dfbf8fSBarry Smith 3407416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 340852e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->lvec);CHKERRQ(ierr); 3409a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 341052e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->Mvctx);CHKERRQ(ierr); 34112e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 341252e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->A);CHKERRQ(ierr); 34132e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 341452e6d16bSBarry Smith ierr = PetscLogObjectParent(mat,a->B);CHKERRQ(ierr); 34157adad957SLisandro Dalcin ierr = PetscFListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 34168a729477SBarry Smith *newmat = mat; 34173a40ed3dSBarry Smith PetscFunctionReturn(0); 34188a729477SBarry Smith } 3419416022c9SBarry Smith 34201a4ee126SBarry Smith 34211a4ee126SBarry Smith 34224a2ae208SSatish Balay #undef __FUNCT__ 34235bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3424112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 34258fb81238SShri Abhyankar { 34268fb81238SShri Abhyankar PetscScalar *vals,*svals; 34278fb81238SShri Abhyankar MPI_Comm comm = ((PetscObject)viewer)->comm; 34288fb81238SShri Abhyankar PetscErrorCode ierr; 34291a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 34308fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 34318fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 34328fb81238SShri Abhyankar PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols; 34338fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 34348fb81238SShri Abhyankar int fd; 343508ea439dSMark F. Adams PetscInt bs = 1; 34368fb81238SShri Abhyankar 34378fb81238SShri Abhyankar PetscFunctionBegin; 34388fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 34398fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 34408fb81238SShri Abhyankar if (!rank) { 34418fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 34428fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char *)header,4,PETSC_INT);CHKERRQ(ierr); 34438fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 34448fb81238SShri Abhyankar } 34458fb81238SShri Abhyankar 344608ea439dSMark F. Adams ierr = PetscOptionsBegin(comm,PETSC_NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 344708ea439dSMark F. Adams ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,PETSC_NULL);CHKERRQ(ierr); 344808ea439dSMark F. Adams ierr = PetscOptionsEnd();CHKERRQ(ierr); 344908ea439dSMark F. Adams 34508fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 34518fb81238SShri Abhyankar 34528fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 34538fb81238SShri Abhyankar M = header[1]; N = header[2]; 34548fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 34558fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 34568fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 34578fb81238SShri Abhyankar 34588fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 34598fb81238SShri Abhyankar if (sizesset) { 34608fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 34618fb81238SShri Abhyankar } 3462abd38a8fSBarry 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); 3463abd38a8fSBarry 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); 34648fb81238SShri Abhyankar 346508ea439dSMark F. Adams /* determine ownership of all (block) rows */ 346608ea439dSMark F. Adams if ( M%bs ) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs); 346708ea439dSMark F. Adams if (newMat->rmap->n < 0 ) m = bs*((M/bs)/size + (((M/bs) % size) > rank)); /* PETSC_DECIDE */ 34684683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 34698fb81238SShri Abhyankar 34708fb81238SShri Abhyankar ierr = PetscMalloc((size+1)*sizeof(PetscInt),&rowners);CHKERRQ(ierr); 34718fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 34728fb81238SShri Abhyankar 34738fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 34748fb81238SShri Abhyankar if (!rank) { 34758fb81238SShri Abhyankar mmax = rowners[1]; 34765c4ea359SMatthew G Knepley for (i=2; i<=size; i++) { 34778fb81238SShri Abhyankar mmax = PetscMax(mmax, rowners[i]); 34788fb81238SShri Abhyankar } 34798fb81238SShri Abhyankar } else mmax = m; 34808fb81238SShri Abhyankar 34818fb81238SShri Abhyankar rowners[0] = 0; 34828fb81238SShri Abhyankar for (i=2; i<=size; i++) { 34838fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 34848fb81238SShri Abhyankar } 34858fb81238SShri Abhyankar rstart = rowners[rank]; 34868fb81238SShri Abhyankar rend = rowners[rank+1]; 34878fb81238SShri Abhyankar 34888fb81238SShri Abhyankar /* distribute row lengths to all processors */ 34898fb81238SShri Abhyankar ierr = PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);CHKERRQ(ierr); 34908fb81238SShri Abhyankar if (!rank) { 34918fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 34925c4ea359SMatthew G Knepley ierr = PetscMalloc(mmax*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr); 34938fb81238SShri Abhyankar ierr = PetscMalloc(size*sizeof(PetscInt),&procsnz);CHKERRQ(ierr); 34948fb81238SShri Abhyankar ierr = PetscMemzero(procsnz,size*sizeof(PetscInt));CHKERRQ(ierr); 34958fb81238SShri Abhyankar for (j=0; j<m; j++) { 34968fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 34978fb81238SShri Abhyankar } 34988fb81238SShri Abhyankar for (i=1; i<size; i++) { 34998fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 35008fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 35018fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 35028fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 35038fb81238SShri Abhyankar } 3504a25532f0SBarry Smith ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 35058fb81238SShri Abhyankar } 35068fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 35078fb81238SShri Abhyankar } else { 3508a25532f0SBarry Smith ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 35098fb81238SShri Abhyankar } 35108fb81238SShri Abhyankar 35118fb81238SShri Abhyankar if (!rank) { 35128fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 35138fb81238SShri Abhyankar maxnz = 0; 35148fb81238SShri Abhyankar for (i=0; i<size; i++) { 35158fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 35168fb81238SShri Abhyankar } 35178fb81238SShri Abhyankar ierr = PetscMalloc(maxnz*sizeof(PetscInt),&cols);CHKERRQ(ierr); 35188fb81238SShri Abhyankar 35198fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 35208fb81238SShri Abhyankar nz = procsnz[0]; 35218fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 35228fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 35238fb81238SShri Abhyankar 35248fb81238SShri Abhyankar /* read in every one elses and ship off */ 35258fb81238SShri Abhyankar for (i=1; i<size; i++) { 35268fb81238SShri Abhyankar nz = procsnz[i]; 35278fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 3528a25532f0SBarry Smith ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 35298fb81238SShri Abhyankar } 35308fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 35318fb81238SShri Abhyankar } else { 35328fb81238SShri Abhyankar /* determine buffer space needed for message */ 35338fb81238SShri Abhyankar nz = 0; 35348fb81238SShri Abhyankar for (i=0; i<m; i++) { 35358fb81238SShri Abhyankar nz += ourlens[i]; 35368fb81238SShri Abhyankar } 35378fb81238SShri Abhyankar ierr = PetscMalloc(nz*sizeof(PetscInt),&mycols);CHKERRQ(ierr); 35388fb81238SShri Abhyankar 35398fb81238SShri Abhyankar /* receive message of column indices*/ 3540a25532f0SBarry Smith ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 35418fb81238SShri Abhyankar } 35428fb81238SShri Abhyankar 35438fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 35448fb81238SShri Abhyankar if (N != M) { 35458fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 35468fb81238SShri Abhyankar else n = newMat->cmap->n; 35478fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 35488fb81238SShri Abhyankar cstart = cend - n; 35498fb81238SShri Abhyankar } else { 35508fb81238SShri Abhyankar cstart = rstart; 35518fb81238SShri Abhyankar cend = rend; 35528fb81238SShri Abhyankar n = cend - cstart; 35538fb81238SShri Abhyankar } 35548fb81238SShri Abhyankar 35558fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 35568fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 35578fb81238SShri Abhyankar jj = 0; 35588fb81238SShri Abhyankar for (i=0; i<m; i++) { 35598fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 35608fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 35618fb81238SShri Abhyankar jj++; 35628fb81238SShri Abhyankar } 35638fb81238SShri Abhyankar } 35648fb81238SShri Abhyankar 35658fb81238SShri Abhyankar for (i=0; i<m; i++) { 35668fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 35678fb81238SShri Abhyankar } 35688fb81238SShri Abhyankar if (!sizesset) { 35698fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 35708fb81238SShri Abhyankar } 357108ea439dSMark F. Adams 357208ea439dSMark F. Adams if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);} 357308ea439dSMark F. Adams 35748fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 35758fb81238SShri Abhyankar 35768fb81238SShri Abhyankar for (i=0; i<m; i++) { 35778fb81238SShri Abhyankar ourlens[i] += offlens[i]; 35788fb81238SShri Abhyankar } 35798fb81238SShri Abhyankar 35808fb81238SShri Abhyankar if (!rank) { 35818fb81238SShri Abhyankar ierr = PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 35828fb81238SShri Abhyankar 35838fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 35848fb81238SShri Abhyankar nz = procsnz[0]; 35858fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 35868fb81238SShri Abhyankar 35878fb81238SShri Abhyankar /* insert into matrix */ 35888fb81238SShri Abhyankar jj = rstart; 35898fb81238SShri Abhyankar smycols = mycols; 35908fb81238SShri Abhyankar svals = vals; 35918fb81238SShri Abhyankar for (i=0; i<m; i++) { 35928fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35938fb81238SShri Abhyankar smycols += ourlens[i]; 35948fb81238SShri Abhyankar svals += ourlens[i]; 35958fb81238SShri Abhyankar jj++; 35968fb81238SShri Abhyankar } 35978fb81238SShri Abhyankar 35988fb81238SShri Abhyankar /* read in other processors and ship out */ 35998fb81238SShri Abhyankar for (i=1; i<size; i++) { 36008fb81238SShri Abhyankar nz = procsnz[i]; 36018fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3602a25532f0SBarry Smith ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 36038fb81238SShri Abhyankar } 36048fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 36058fb81238SShri Abhyankar } else { 36068fb81238SShri Abhyankar /* receive numeric values */ 36078fb81238SShri Abhyankar ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);CHKERRQ(ierr); 36088fb81238SShri Abhyankar 36098fb81238SShri Abhyankar /* receive message of values*/ 3610a25532f0SBarry Smith ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 36118fb81238SShri Abhyankar 36128fb81238SShri Abhyankar /* insert into matrix */ 36138fb81238SShri Abhyankar jj = rstart; 36148fb81238SShri Abhyankar smycols = mycols; 36158fb81238SShri Abhyankar svals = vals; 36168fb81238SShri Abhyankar for (i=0; i<m; i++) { 36178fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 36188fb81238SShri Abhyankar smycols += ourlens[i]; 36198fb81238SShri Abhyankar svals += ourlens[i]; 36208fb81238SShri Abhyankar jj++; 36218fb81238SShri Abhyankar } 36228fb81238SShri Abhyankar } 36238fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 36248fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 36258fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 36268fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 36278fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 36288fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 362908ea439dSMark F. Adams 36308fb81238SShri Abhyankar PetscFunctionReturn(0); 36318fb81238SShri Abhyankar } 36328fb81238SShri Abhyankar 36338fb81238SShri Abhyankar #undef __FUNCT__ 36344a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 36354aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 36364aa3045dSJed Brown { 36374aa3045dSJed Brown PetscErrorCode ierr; 36384aa3045dSJed Brown IS iscol_local; 36394aa3045dSJed Brown PetscInt csize; 36404aa3045dSJed Brown 36414aa3045dSJed Brown PetscFunctionBegin; 36424aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3643b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3644b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3645e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3646b79d0421SJed Brown } else { 3647c5bfad50SMark F. Adams PetscInt cbs; 3648c5bfad50SMark F. Adams ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 36494aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3650c5bfad50SMark F. Adams ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr); 3651b79d0421SJed Brown } 36524aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3653b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3654b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 36556bf464f9SBarry Smith ierr = ISDestroy(&iscol_local);CHKERRQ(ierr); 3656b79d0421SJed Brown } 36574aa3045dSJed Brown PetscFunctionReturn(0); 36584aa3045dSJed Brown } 36594aa3045dSJed Brown 366029dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*); 36614aa3045dSJed Brown #undef __FUNCT__ 36624aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3663a0ff6018SBarry Smith /* 366429da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 366529da9460SBarry Smith in local and then by concatenating the local matrices the end result. 366629da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 36674aa3045dSJed Brown 36684aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3669a0ff6018SBarry Smith */ 36704aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3671a0ff6018SBarry Smith { 3672dfbe8321SBarry Smith PetscErrorCode ierr; 367332dcc486SBarry Smith PetscMPIInt rank,size; 3674a2f3521dSMark F. Adams PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs; 367529dcf524SDmitry Karpeev PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol; 367629dcf524SDmitry Karpeev PetscBool allcolumns, colflag; 367729dcf524SDmitry Karpeev Mat M,Mreuse; 3678a77337e4SBarry Smith MatScalar *vwork,*aa; 36797adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mat)->comm; 368000e6dbe6SBarry Smith Mat_SeqAIJ *aij; 36817e2c5f70SBarry Smith 3682a0ff6018SBarry Smith 3683a0ff6018SBarry Smith PetscFunctionBegin; 36841dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 36851dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 368600e6dbe6SBarry Smith 368729dcf524SDmitry Karpeev ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr); 368829dcf524SDmitry Karpeev ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr); 368929dcf524SDmitry Karpeev if (colflag && ncol == mat->cmap->N){ 369029dcf524SDmitry Karpeev allcolumns = PETSC_TRUE; 369129dcf524SDmitry Karpeev } else { 369229dcf524SDmitry Karpeev allcolumns = PETSC_FALSE; 369329dcf524SDmitry Karpeev } 3694fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3695fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);CHKERRQ(ierr); 3696e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 369729dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3698fee21e36SBarry Smith } else { 369929dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3700fee21e36SBarry Smith } 3701a0ff6018SBarry Smith 3702a0ff6018SBarry Smith /* 3703a0ff6018SBarry Smith m - number of local rows 3704a0ff6018SBarry Smith n - number of columns (same on all processors) 3705a0ff6018SBarry Smith rstart - first row in new global matrix generated 3706a0ff6018SBarry Smith */ 3707fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3708a2f3521dSMark F. Adams ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr); 3709a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3710fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 371100e6dbe6SBarry Smith ii = aij->i; 371200e6dbe6SBarry Smith jj = aij->j; 371300e6dbe6SBarry Smith 3714a0ff6018SBarry Smith /* 371500e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 371600e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3717a0ff6018SBarry Smith */ 371800e6dbe6SBarry Smith 371900e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 37206a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3721ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3722ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3723e2c4fddaSBarry Smith nlocal = m; 37246a6a5d1dSBarry Smith } else { 3725ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3726ab50ec6bSBarry Smith } 3727ab50ec6bSBarry Smith } else { 37286a6a5d1dSBarry Smith nlocal = csize; 37296a6a5d1dSBarry Smith } 3730b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 373100e6dbe6SBarry Smith rstart = rend - nlocal; 373265e19b50SBarry 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); 373300e6dbe6SBarry Smith 373400e6dbe6SBarry Smith /* next, compute all the lengths */ 3735b1d57f15SBarry Smith ierr = PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);CHKERRQ(ierr); 373600e6dbe6SBarry Smith olens = dlens + m; 373700e6dbe6SBarry Smith for (i=0; i<m; i++) { 373800e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 373900e6dbe6SBarry Smith olen = 0; 374000e6dbe6SBarry Smith dlen = 0; 374100e6dbe6SBarry Smith for (j=0; j<jend; j++) { 374200e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 374300e6dbe6SBarry Smith else dlen++; 374400e6dbe6SBarry Smith jj++; 374500e6dbe6SBarry Smith } 374600e6dbe6SBarry Smith olens[i] = olen; 374700e6dbe6SBarry Smith dlens[i] = dlen; 374800e6dbe6SBarry Smith } 3749f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3750f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 3751a2f3521dSMark F. Adams ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); 37527adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3753e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3754606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3755a0ff6018SBarry Smith } else { 3756b1d57f15SBarry Smith PetscInt ml,nl; 3757a0ff6018SBarry Smith 3758a0ff6018SBarry Smith M = *newmat; 3759a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3760e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3761a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3762c48de900SBarry Smith /* 3763c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3764c48de900SBarry Smith rather than the slower MatSetValues(). 3765c48de900SBarry Smith */ 3766c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3767c48de900SBarry Smith M->assembled = PETSC_FALSE; 3768a0ff6018SBarry Smith } 3769a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3770fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 377100e6dbe6SBarry Smith ii = aij->i; 377200e6dbe6SBarry Smith jj = aij->j; 377300e6dbe6SBarry Smith aa = aij->a; 3774a0ff6018SBarry Smith for (i=0; i<m; i++) { 3775a0ff6018SBarry Smith row = rstart + i; 377600e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 377700e6dbe6SBarry Smith cwork = jj; jj += nz; 377800e6dbe6SBarry Smith vwork = aa; aa += nz; 37798c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3780a0ff6018SBarry Smith } 3781a0ff6018SBarry Smith 3782a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3783a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3784a0ff6018SBarry Smith *newmat = M; 3785fee21e36SBarry Smith 3786fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3787fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3788fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3789bf0cc555SLisandro Dalcin ierr = MatDestroy(&Mreuse);CHKERRQ(ierr); 3790fee21e36SBarry Smith } 3791fee21e36SBarry Smith 3792a0ff6018SBarry Smith PetscFunctionReturn(0); 3793a0ff6018SBarry Smith } 3794273d9f13SBarry Smith 3795e2e86b8fSSatish Balay EXTERN_C_BEGIN 37964a2ae208SSatish Balay #undef __FUNCT__ 3797ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 37987087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3799ccd8e176SBarry Smith { 3800899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3801899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3802ccd8e176SBarry Smith const PetscInt *JJ; 3803ccd8e176SBarry Smith PetscScalar *values; 3804ccd8e176SBarry Smith PetscErrorCode ierr; 3805ccd8e176SBarry Smith 3806ccd8e176SBarry Smith PetscFunctionBegin; 3807e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3808899cda47SBarry Smith 380926283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 381026283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3811d0f46423SBarry Smith m = B->rmap->n; 3812d0f46423SBarry Smith cstart = B->cmap->rstart; 3813d0f46423SBarry Smith cend = B->cmap->rend; 3814d0f46423SBarry Smith rstart = B->rmap->rstart; 3815899cda47SBarry Smith 38161d79065fSBarry Smith ierr = PetscMalloc2(m,PetscInt,&d_nnz,m,PetscInt,&o_nnz);CHKERRQ(ierr); 3817ccd8e176SBarry Smith 3818ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3819ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3820ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3821ecc77c7aSBarry Smith JJ = J + Ii[i]; 3822e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3823ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3824d0f46423SBarry 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); 3825ecc77c7aSBarry Smith } 3826ecc77c7aSBarry Smith #endif 3827ecc77c7aSBarry Smith 3828ccd8e176SBarry Smith for (i=0; i<m; i++) { 3829b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3830b7940d39SSatish Balay JJ = J + Ii[i]; 3831ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3832ccd8e176SBarry Smith d = 0; 38330daa03b5SJed Brown for (j=0; j<nnz; j++) { 38340daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3835ccd8e176SBarry Smith } 3836ccd8e176SBarry Smith d_nnz[i] = d; 3837ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3838ccd8e176SBarry Smith } 3839ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 38401d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3841ccd8e176SBarry Smith 3842ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3843ccd8e176SBarry Smith else { 3844ccd8e176SBarry Smith ierr = PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);CHKERRQ(ierr); 3845ccd8e176SBarry Smith ierr = PetscMemzero(values,nnz_max*sizeof(PetscScalar));CHKERRQ(ierr); 3846ccd8e176SBarry Smith } 3847ccd8e176SBarry Smith 3848ccd8e176SBarry Smith for (i=0; i<m; i++) { 3849ccd8e176SBarry Smith ii = i + rstart; 3850b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3851b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3852ccd8e176SBarry Smith } 3853ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3854ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3855ccd8e176SBarry Smith 3856ccd8e176SBarry Smith if (!v) { 3857ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3858ccd8e176SBarry Smith } 38597827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3860ccd8e176SBarry Smith PetscFunctionReturn(0); 3861ccd8e176SBarry Smith } 3862e2e86b8fSSatish Balay EXTERN_C_END 3863ccd8e176SBarry Smith 3864ccd8e176SBarry Smith #undef __FUNCT__ 3865ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 38661eea217eSSatish Balay /*@ 3867ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3868ccd8e176SBarry Smith (the default parallel PETSc format). 3869ccd8e176SBarry Smith 3870ccd8e176SBarry Smith Collective on MPI_Comm 3871ccd8e176SBarry Smith 3872ccd8e176SBarry Smith Input Parameters: 3873a1661176SMatthew Knepley + B - the matrix 3874ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 38750daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3876ccd8e176SBarry Smith - v - optional values in the matrix 3877ccd8e176SBarry Smith 3878ccd8e176SBarry Smith Level: developer 3879ccd8e176SBarry Smith 388012251496SSatish Balay Notes: 388112251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 388212251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 388312251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 388412251496SSatish Balay 388512251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 388612251496SSatish Balay 388712251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 388812251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 388912251496SSatish Balay as shown: 389012251496SSatish Balay 389112251496SSatish Balay 1 0 0 389212251496SSatish Balay 2 0 3 P0 389312251496SSatish Balay ------- 389412251496SSatish Balay 4 5 6 P1 389512251496SSatish Balay 389612251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 389712251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 389812251496SSatish Balay j = {0,0,2} [size = nz = 6] 389912251496SSatish Balay v = {1,2,3} [size = nz = 6] 390012251496SSatish Balay 390112251496SSatish Balay Process1 [P1]: rows_owned=[2] 390212251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 390312251496SSatish Balay j = {0,1,2} [size = nz = 6] 390412251496SSatish Balay v = {4,5,6} [size = nz = 6] 390512251496SSatish Balay 3906ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3907ccd8e176SBarry Smith 390869b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, 39098d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3910ccd8e176SBarry Smith @*/ 39117087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3912ccd8e176SBarry Smith { 39134ac538c5SBarry Smith PetscErrorCode ierr; 3914ccd8e176SBarry Smith 3915ccd8e176SBarry Smith PetscFunctionBegin; 39164ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3917ccd8e176SBarry Smith PetscFunctionReturn(0); 3918ccd8e176SBarry Smith } 3919ccd8e176SBarry Smith 3920ccd8e176SBarry Smith #undef __FUNCT__ 39214a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3922273d9f13SBarry Smith /*@C 3923ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3924273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3925273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3926273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3927273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3928273d9f13SBarry Smith 3929273d9f13SBarry Smith Collective on MPI_Comm 3930273d9f13SBarry Smith 3931273d9f13SBarry Smith Input Parameters: 3932273d9f13SBarry Smith + A - the matrix 3933273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3934273d9f13SBarry Smith (same value is used for all local rows) 3935273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3936273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 3937273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 3938273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 39393287b5eaSJed Brown For matrices that will be factored, you must leave room for (and set) 39403287b5eaSJed Brown the diagonal entry even if it is zero. 3941273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3942273d9f13SBarry Smith submatrix (same value is used for all local rows). 3943273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3944273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 3945273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 3946273d9f13SBarry Smith structure. The size of this array is equal to the number 3947273d9f13SBarry Smith of local rows, i.e 'm'. 3948273d9f13SBarry Smith 394949a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 395049a6f317SBarry Smith 3951273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3952ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 39530598bfebSBarry Smith storage. The stored row and column indices begin with zero. 39540598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3955273d9f13SBarry Smith 3956273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3957273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3958273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3959273d9f13SBarry Smith 3960273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3961a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3962a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3963a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3964a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3965a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3966a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3967a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3968a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3969273d9f13SBarry Smith 3970273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3971273d9f13SBarry Smith 3972aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3973aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3974aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3975aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3976aa95bbe8SBarry Smith 3977273d9f13SBarry Smith Example usage: 3978273d9f13SBarry Smith 3979273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3980273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3981273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3982273d9f13SBarry Smith as follows: 3983273d9f13SBarry Smith 3984273d9f13SBarry Smith .vb 3985273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3986273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3987273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3988273d9f13SBarry Smith ------------------------------------- 3989273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3990273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3991273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3992273d9f13SBarry Smith ------------------------------------- 3993273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3994273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3995273d9f13SBarry Smith .ve 3996273d9f13SBarry Smith 3997273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3998273d9f13SBarry Smith 3999273d9f13SBarry Smith .vb 4000273d9f13SBarry Smith A B C 4001273d9f13SBarry Smith D E F 4002273d9f13SBarry Smith G H I 4003273d9f13SBarry Smith .ve 4004273d9f13SBarry Smith 4005273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 4006273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 4007273d9f13SBarry Smith 4008273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4009273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4010273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 4011273d9f13SBarry Smith 4012273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 4013273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 4014273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 4015273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4016273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4017273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4018273d9f13SBarry Smith 4019273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4020273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4021273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4022273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4023273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4024273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4025273d9f13SBarry Smith .vb 4026273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4027273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4028273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4029273d9f13SBarry Smith .ve 4030273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4031273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4032273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4033273d9f13SBarry Smith 34 values. 4034273d9f13SBarry Smith 4035273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4036273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4037273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4038273d9f13SBarry Smith .vb 4039273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4040273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4041273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4042273d9f13SBarry Smith .ve 4043273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4044273d9f13SBarry Smith hence pre-allocation is perfect. 4045273d9f13SBarry Smith 4046273d9f13SBarry Smith Level: intermediate 4047273d9f13SBarry Smith 4048273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4049273d9f13SBarry Smith 405069b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(), 4051ab978733SBarry Smith MPIAIJ, MatGetInfo(), PetscSplitOwnership() 4052273d9f13SBarry Smith @*/ 40537087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 4054273d9f13SBarry Smith { 40554ac538c5SBarry Smith PetscErrorCode ierr; 4056273d9f13SBarry Smith 4057273d9f13SBarry Smith PetscFunctionBegin; 40586ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 40596ba663aaSJed Brown PetscValidType(B,1); 40604ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 4061273d9f13SBarry Smith PetscFunctionReturn(0); 4062273d9f13SBarry Smith } 4063273d9f13SBarry Smith 40644a2ae208SSatish Balay #undef __FUNCT__ 40652fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 406658d36128SBarry Smith /*@ 40672fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 40682fb0ec9aSBarry Smith CSR format the local rows. 40692fb0ec9aSBarry Smith 40702fb0ec9aSBarry Smith Collective on MPI_Comm 40712fb0ec9aSBarry Smith 40722fb0ec9aSBarry Smith Input Parameters: 40732fb0ec9aSBarry Smith + comm - MPI communicator 40742fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 40752fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 40762fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 40772fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 40782fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 40792fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 40802fb0ec9aSBarry Smith . i - row indices 40812fb0ec9aSBarry Smith . j - column indices 40822fb0ec9aSBarry Smith - a - matrix values 40832fb0ec9aSBarry Smith 40842fb0ec9aSBarry Smith Output Parameter: 40852fb0ec9aSBarry Smith . mat - the matrix 408603bfb495SBarry Smith 40872fb0ec9aSBarry Smith Level: intermediate 40882fb0ec9aSBarry Smith 40892fb0ec9aSBarry Smith Notes: 40902fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 40912fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 40928d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 40932fb0ec9aSBarry Smith 409412251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 409512251496SSatish Balay 409612251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 409712251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 409812251496SSatish Balay as shown: 409912251496SSatish Balay 410012251496SSatish Balay 1 0 0 410112251496SSatish Balay 2 0 3 P0 410212251496SSatish Balay ------- 410312251496SSatish Balay 4 5 6 P1 410412251496SSatish Balay 410512251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 410612251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 410712251496SSatish Balay j = {0,0,2} [size = nz = 6] 410812251496SSatish Balay v = {1,2,3} [size = nz = 6] 410912251496SSatish Balay 411012251496SSatish Balay Process1 [P1]: rows_owned=[2] 411112251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 411212251496SSatish Balay j = {0,1,2} [size = nz = 6] 411312251496SSatish Balay v = {4,5,6} [size = nz = 6] 41142fb0ec9aSBarry Smith 41152fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 41162fb0ec9aSBarry Smith 41172fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 411869b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays() 41192fb0ec9aSBarry Smith @*/ 41207087cfbeSBarry 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) 41212fb0ec9aSBarry Smith { 41222fb0ec9aSBarry Smith PetscErrorCode ierr; 41232fb0ec9aSBarry Smith 41242fb0ec9aSBarry Smith PetscFunctionBegin; 412569b1f4b7SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4126e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 41272fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4128d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 4129a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */ 41302fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 41312fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 41322fb0ec9aSBarry Smith PetscFunctionReturn(0); 41332fb0ec9aSBarry Smith } 41342fb0ec9aSBarry Smith 41352fb0ec9aSBarry Smith #undef __FUNCT__ 413669b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ" 4137273d9f13SBarry Smith /*@C 413869b1f4b7SBarry Smith MatCreateAIJ - Creates a sparse parallel matrix in AIJ format 4139273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 4140273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 4141273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 4142273d9f13SBarry Smith performance can be increased by more than a factor of 50. 4143273d9f13SBarry Smith 4144273d9f13SBarry Smith Collective on MPI_Comm 4145273d9f13SBarry Smith 4146273d9f13SBarry Smith Input Parameters: 4147273d9f13SBarry Smith + comm - MPI communicator 4148273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 4149273d9f13SBarry Smith This value should be the same as the local size used in creating the 4150273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 4151273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 4152273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 4153273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 4154273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 4155273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 4156273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 4157273d9f13SBarry Smith (same value is used for all local rows) 4158273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 4159273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 4160273d9f13SBarry Smith or PETSC_NULL, if d_nz is used to specify the nonzero structure. 4161273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 4162273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 4163273d9f13SBarry Smith submatrix (same value is used for all local rows). 4164273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 4165273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 4166273d9f13SBarry Smith each row) or PETSC_NULL, if o_nz is used to specify the nonzero 4167273d9f13SBarry Smith structure. The size of this array is equal to the number 4168273d9f13SBarry Smith of local rows, i.e 'm'. 4169273d9f13SBarry Smith 4170273d9f13SBarry Smith Output Parameter: 4171273d9f13SBarry Smith . A - the matrix 4172273d9f13SBarry Smith 4173175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 4174ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 4175175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 4176175b88e8SBarry Smith 4177273d9f13SBarry Smith Notes: 417849a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 417949a6f317SBarry Smith 4180273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 4181273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 4182273d9f13SBarry Smith storage requirements for this matrix. 4183273d9f13SBarry Smith 4184273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 4185273d9f13SBarry Smith processor than it must be used on all processors that share the object for 4186273d9f13SBarry Smith that argument. 4187273d9f13SBarry Smith 4188273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 4189273d9f13SBarry Smith (possibly both). 4190273d9f13SBarry Smith 419133a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 419233a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 419333a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 419433a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 419533a7c187SSatish Balay values corresponding to [m x N] submatrix. 4196273d9f13SBarry Smith 419733a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 419833a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 419933a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 420033a7c187SSatish Balay 420133a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 420233a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 420333a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 420433a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 420533a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 420633a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 420733a7c187SSatish Balay illustrates this concept. 420833a7c187SSatish Balay 420933a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 421033a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 421133a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 421233a7c187SSatish Balay local matrix (a rectangular submatrix). 4213273d9f13SBarry Smith 4214273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 4215273d9f13SBarry Smith 421697d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 421797d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 421897d05335SKris Buschelman type of communicator, use the construction mechanism: 421978102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 422097d05335SKris Buschelman 4221273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 4222273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 4223273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 4224273d9f13SBarry Smith 4225273d9f13SBarry Smith Options Database Keys: 4226923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 4227923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 4228273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 4229273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 4230273d9f13SBarry Smith the user still MUST index entries starting at 0! 4231273d9f13SBarry Smith 4232273d9f13SBarry Smith 4233273d9f13SBarry Smith Example usage: 4234273d9f13SBarry Smith 4235273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 4236273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 4237273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 4238273d9f13SBarry Smith as follows: 4239273d9f13SBarry Smith 4240273d9f13SBarry Smith .vb 4241273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 4242273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 4243273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 4244273d9f13SBarry Smith ------------------------------------- 4245273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 4246273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 4247273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 4248273d9f13SBarry Smith ------------------------------------- 4249273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 4250273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 4251273d9f13SBarry Smith .ve 4252273d9f13SBarry Smith 4253273d9f13SBarry Smith This can be represented as a collection of submatrices as: 4254273d9f13SBarry Smith 4255273d9f13SBarry Smith .vb 4256273d9f13SBarry Smith A B C 4257273d9f13SBarry Smith D E F 4258273d9f13SBarry Smith G H I 4259273d9f13SBarry Smith .ve 4260273d9f13SBarry Smith 4261273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 4262273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 4263273d9f13SBarry Smith 4264273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4265273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4266273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 4267273d9f13SBarry Smith 4268273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 4269273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 4270273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 4271273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4272273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4273273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4274273d9f13SBarry Smith 4275273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4276273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4277273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4278273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4279273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4280273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4281273d9f13SBarry Smith .vb 4282273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4283273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4284273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4285273d9f13SBarry Smith .ve 4286273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4287273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4288273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4289273d9f13SBarry Smith 34 values. 4290273d9f13SBarry Smith 4291273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4292273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4293273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4294273d9f13SBarry Smith .vb 4295273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4296273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4297273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4298273d9f13SBarry Smith .ve 4299273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4300273d9f13SBarry Smith hence pre-allocation is perfect. 4301273d9f13SBarry Smith 4302273d9f13SBarry Smith Level: intermediate 4303273d9f13SBarry Smith 4304273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4305273d9f13SBarry Smith 4306ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 43072fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4308273d9f13SBarry Smith @*/ 430969b1f4b7SBarry 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) 4310273d9f13SBarry Smith { 43116849ba73SBarry Smith PetscErrorCode ierr; 4312b1d57f15SBarry Smith PetscMPIInt size; 4313273d9f13SBarry Smith 4314273d9f13SBarry Smith PetscFunctionBegin; 4315f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4316f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4317273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4318273d9f13SBarry Smith if (size > 1) { 4319273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4320273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4321273d9f13SBarry Smith } else { 4322273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4323273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4324273d9f13SBarry Smith } 4325273d9f13SBarry Smith PetscFunctionReturn(0); 4326273d9f13SBarry Smith } 4327195d93cdSBarry Smith 43284a2ae208SSatish Balay #undef __FUNCT__ 43294a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 43309230625dSJed Brown PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[]) 4331195d93cdSBarry Smith { 4332195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data; 4333b1d57f15SBarry Smith 4334195d93cdSBarry Smith PetscFunctionBegin; 4335195d93cdSBarry Smith *Ad = a->A; 4336195d93cdSBarry Smith *Ao = a->B; 4337195d93cdSBarry Smith *colmap = a->garray; 4338195d93cdSBarry Smith PetscFunctionReturn(0); 4339195d93cdSBarry Smith } 4340a2243be0SBarry Smith 4341a2243be0SBarry Smith #undef __FUNCT__ 4342a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4343dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4344a2243be0SBarry Smith { 4345dfbe8321SBarry Smith PetscErrorCode ierr; 4346b1d57f15SBarry Smith PetscInt i; 4347a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4348a2243be0SBarry Smith 4349a2243be0SBarry Smith PetscFunctionBegin; 43508ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 435108b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4352a2243be0SBarry Smith ISColoring ocoloring; 4353a2243be0SBarry Smith 4354a2243be0SBarry Smith /* set coloring for diagonal portion */ 4355a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4356a2243be0SBarry Smith 4357a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 43587adad957SLisandro Dalcin ierr = ISAllGatherColors(((PetscObject)A)->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);CHKERRQ(ierr); 4359d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4360d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4361a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4362a2243be0SBarry Smith } 4363a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4364d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4365a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43666bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4367a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 436808b6dcc0SBarry Smith ISColoringValue *colors; 4369b1d57f15SBarry Smith PetscInt *larray; 4370a2243be0SBarry Smith ISColoring ocoloring; 4371a2243be0SBarry Smith 4372a2243be0SBarry Smith /* set coloring for diagonal portion */ 4373d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4374d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4375d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4376a2243be0SBarry Smith } 4377992144d0SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr); 4378d0f46423SBarry Smith ierr = PetscMalloc((a->A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4379d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4380a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4381a2243be0SBarry Smith } 4382a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4383d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4384a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 43856bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4386a2243be0SBarry Smith 4387a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4388d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr); 4389992144d0SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,PETSC_NULL,larray);CHKERRQ(ierr); 4390d0f46423SBarry Smith ierr = PetscMalloc((a->B->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr); 4391d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4392a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4393a2243be0SBarry Smith } 4394a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4395d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4396a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43976bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 43986bf464f9SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4399a2243be0SBarry Smith 4400a2243be0SBarry Smith PetscFunctionReturn(0); 4401a2243be0SBarry Smith } 4402a2243be0SBarry Smith 4403779c1a83SBarry Smith #undef __FUNCT__ 4404779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4405b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4406779c1a83SBarry Smith { 4407779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4408dfbe8321SBarry Smith PetscErrorCode ierr; 4409779c1a83SBarry Smith 4410779c1a83SBarry Smith PetscFunctionBegin; 4411779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4412779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4413a2243be0SBarry Smith PetscFunctionReturn(0); 4414a2243be0SBarry Smith } 4415c5d6d63eSBarry Smith 4416c5d6d63eSBarry Smith #undef __FUNCT__ 441790431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic" 441890431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat) 44199b8102ccSHong Zhang { 44209b8102ccSHong Zhang PetscErrorCode ierr; 4421a2f3521dSMark F. Adams PetscInt m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs; 44229b8102ccSHong Zhang PetscInt *indx; 44239b8102ccSHong Zhang 44249b8102ccSHong Zhang PetscFunctionBegin; 44259b8102ccSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 44269b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4427a2f3521dSMark F. Adams ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr); 44289b8102ccSHong Zhang if (n == PETSC_DECIDE){ 44299b8102ccSHong Zhang ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 44309b8102ccSHong Zhang } 4431a22543b6SHong Zhang /* Check sum(n) = N */ 4432a95133b1SBarry Smith ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4433a22543b6SHong Zhang if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N); 4434a22543b6SHong Zhang 44359b8102ccSHong Zhang ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 44369b8102ccSHong Zhang rstart -= m; 44379b8102ccSHong Zhang 44389b8102ccSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 44399b8102ccSHong Zhang for (i=0;i<m;i++) { 44409b8102ccSHong Zhang ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 44419b8102ccSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 44429b8102ccSHong Zhang ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);CHKERRQ(ierr); 44439b8102ccSHong Zhang } 44449b8102ccSHong Zhang 44459b8102ccSHong Zhang ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 44469b8102ccSHong Zhang ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4447a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr); 44489b8102ccSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 44499b8102ccSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 44509b8102ccSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 44519b8102ccSHong Zhang PetscFunctionReturn(0); 44529b8102ccSHong Zhang } 44539b8102ccSHong Zhang 44549b8102ccSHong Zhang #undef __FUNCT__ 445590431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric" 445690431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat) 44579b8102ccSHong Zhang { 44589b8102ccSHong Zhang PetscErrorCode ierr; 44599b8102ccSHong Zhang PetscInt m,N,i,rstart,nnz,Ii; 44609b8102ccSHong Zhang PetscInt *indx; 44619b8102ccSHong Zhang PetscScalar *values; 44629b8102ccSHong Zhang 44639b8102ccSHong Zhang PetscFunctionBegin; 44649b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 44659b8102ccSHong Zhang ierr = MatGetOwnershipRange(outmat,&rstart,PETSC_NULL);CHKERRQ(ierr); 44669b8102ccSHong Zhang for (i=0;i<m;i++) { 44679b8102ccSHong Zhang ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44689b8102ccSHong Zhang Ii = i + rstart; 4469a22543b6SHong Zhang ierr = MatSetValues_MPIAIJ(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 44709b8102ccSHong Zhang ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 44719b8102ccSHong Zhang } 44729b8102ccSHong Zhang ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44739b8102ccSHong Zhang ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 44749b8102ccSHong Zhang PetscFunctionReturn(0); 44759b8102ccSHong Zhang } 44769b8102ccSHong Zhang 44779b8102ccSHong Zhang #undef __FUNCT__ 447890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ" 4479bc08b0f1SBarry Smith /*@ 448090431a8fSHong Zhang MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential 448151dd7536SBarry Smith matrices from each processor 4482c5d6d63eSBarry Smith 4483c5d6d63eSBarry Smith Collective on MPI_Comm 4484c5d6d63eSBarry Smith 4485c5d6d63eSBarry Smith Input Parameters: 448651dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4487d6bb3c2dSHong Zhang . inmat - the input sequential matrices 44880e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4489d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 449051dd7536SBarry Smith 449151dd7536SBarry Smith Output Parameter: 449251dd7536SBarry Smith . outmat - the parallel matrix generated 4493c5d6d63eSBarry Smith 44947e25d530SSatish Balay Level: advanced 44957e25d530SSatish Balay 4496f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4497c5d6d63eSBarry Smith 4498c5d6d63eSBarry Smith @*/ 449990431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4500c5d6d63eSBarry Smith { 4501dfbe8321SBarry Smith PetscErrorCode ierr; 4502c5d6d63eSBarry Smith 4503c5d6d63eSBarry Smith PetscFunctionBegin; 45049b8102ccSHong Zhang ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4505d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 450690431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr); 45070e36024fSHong Zhang } 450890431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr); 45099b8102ccSHong Zhang ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4510c5d6d63eSBarry Smith PetscFunctionReturn(0); 4511c5d6d63eSBarry Smith } 4512c5d6d63eSBarry Smith 4513c5d6d63eSBarry Smith #undef __FUNCT__ 4514c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4515dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4516c5d6d63eSBarry Smith { 4517dfbe8321SBarry Smith PetscErrorCode ierr; 451832dcc486SBarry Smith PetscMPIInt rank; 4519b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4520de4209c5SBarry Smith size_t len; 4521b1d57f15SBarry Smith const PetscInt *indx; 4522c5d6d63eSBarry Smith PetscViewer out; 4523c5d6d63eSBarry Smith char *name; 4524c5d6d63eSBarry Smith Mat B; 4525b3cc6726SBarry Smith const PetscScalar *values; 4526c5d6d63eSBarry Smith 4527c5d6d63eSBarry Smith PetscFunctionBegin; 4528c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4529c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4530f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4531f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4532f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4533a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 4534f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 4535f204ca49SKris Buschelman ierr = MatSeqAIJSetPreallocation(B,0,PETSC_NULL);CHKERRQ(ierr); 4536c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4537c5d6d63eSBarry Smith for (i=0;i<m;i++) { 4538c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4539c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4540c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4541c5d6d63eSBarry Smith } 4542c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4543c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4544c5d6d63eSBarry Smith 45457adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)A)->comm,&rank);CHKERRQ(ierr); 4546c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4547c5d6d63eSBarry Smith ierr = PetscMalloc((len+5)*sizeof(char),&name);CHKERRQ(ierr); 4548c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4549852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4550a2ea699eSBarry Smith ierr = PetscFree(name);CHKERRQ(ierr); 4551c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 45526bf464f9SBarry Smith ierr = PetscViewerDestroy(&out);CHKERRQ(ierr); 45536bf464f9SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 4554c5d6d63eSBarry Smith PetscFunctionReturn(0); 4555c5d6d63eSBarry Smith } 4556e5f2cdd8SHong Zhang 455709573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 455851a7d1a8SHong Zhang #undef __FUNCT__ 455951a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 45607087cfbeSBarry Smith PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 456151a7d1a8SHong Zhang { 456251a7d1a8SHong Zhang PetscErrorCode ierr; 4563671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4564776b82aeSLisandro Dalcin PetscContainer container; 456551a7d1a8SHong Zhang 456651a7d1a8SHong Zhang PetscFunctionBegin; 4567671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4568671beff6SHong Zhang if (container) { 4569776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 457051a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 45713e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 45723e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 457351a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 457451a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4575533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 457602c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4577533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 457802c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 457905b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 458005b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 458105b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 45826bf464f9SBarry Smith ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr); 4583bf0cc555SLisandro Dalcin ierr = PetscFree(merge);CHKERRQ(ierr); 4584671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4585671beff6SHong Zhang } 458651a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 458751a7d1a8SHong Zhang PetscFunctionReturn(0); 458851a7d1a8SHong Zhang } 458951a7d1a8SHong Zhang 4590c6db04a5SJed Brown #include <../src/mat/utils/freespace.h> 4591c6db04a5SJed Brown #include <petscbt.h> 45924ebed01fSBarry Smith 4593e5f2cdd8SHong Zhang #undef __FUNCT__ 459490431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric" 459590431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat) 459655d1abb9SHong Zhang { 459755d1abb9SHong Zhang PetscErrorCode ierr; 45987adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)mpimat)->comm; 459955d1abb9SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4600b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4601a2ea699eSBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj; 4602b1d57f15SBarry Smith PetscInt proc,m; 4603b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4604b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4605b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 460655d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 460755d1abb9SHong Zhang MPI_Status *status; 4608a77337e4SBarry Smith MatScalar *aa=a->a; 4609dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 461055d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4611776b82aeSLisandro Dalcin PetscContainer container; 461255d1abb9SHong Zhang 461355d1abb9SHong Zhang PetscFunctionBegin; 46144ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 46153c2c1871SHong Zhang 461655d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 461755d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 461855d1abb9SHong Zhang 461955d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);CHKERRQ(ierr); 4620776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void **)&merge);CHKERRQ(ierr); 4621bf0cc555SLisandro Dalcin 462255d1abb9SHong Zhang bi = merge->bi; 462355d1abb9SHong Zhang bj = merge->bj; 462455d1abb9SHong Zhang buf_ri = merge->buf_ri; 462555d1abb9SHong Zhang buf_rj = merge->buf_rj; 462655d1abb9SHong Zhang 462755d1abb9SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 46287a2fc3feSBarry Smith owners = merge->rowmap->range; 462955d1abb9SHong Zhang len_s = merge->len_s; 463055d1abb9SHong Zhang 463155d1abb9SHong Zhang /* send and recv matrix values */ 463255d1abb9SHong Zhang /*-----------------------------*/ 4633357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 463455d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 463555d1abb9SHong Zhang 463655d1abb9SHong Zhang ierr = PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);CHKERRQ(ierr); 463755d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++){ 463855d1abb9SHong Zhang if (!len_s[proc]) continue; 463955d1abb9SHong Zhang i = owners[proc]; 464055d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 464155d1abb9SHong Zhang k++; 464255d1abb9SHong Zhang } 464355d1abb9SHong Zhang 46440c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 46450c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 464655d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 464755d1abb9SHong Zhang 464855d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 464955d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 465055d1abb9SHong Zhang 465155d1abb9SHong Zhang /* insert mat values of mpimat */ 465255d1abb9SHong Zhang /*----------------------------*/ 4653a77337e4SBarry Smith ierr = PetscMalloc(N*sizeof(PetscScalar),&ba_i);CHKERRQ(ierr); 46540572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 465555d1abb9SHong Zhang 465655d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ 465755d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 465855d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 465955d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 466055d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 466155d1abb9SHong Zhang } 466255d1abb9SHong Zhang 466355d1abb9SHong Zhang /* set values of ba */ 46647a2fc3feSBarry Smith m = merge->rowmap->n; 466555d1abb9SHong Zhang for (i=0; i<m; i++) { 466655d1abb9SHong Zhang arow = owners[rank] + i; 466755d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 466855d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4669a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 467055d1abb9SHong Zhang 467155d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 467255d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 467355d1abb9SHong Zhang aj = a->j + ai[arow]; 467455d1abb9SHong Zhang aa = a->a + ai[arow]; 467555d1abb9SHong Zhang nextaj = 0; 467655d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 467755d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 467855d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 467955d1abb9SHong Zhang } 468055d1abb9SHong Zhang } 468155d1abb9SHong Zhang 468255d1abb9SHong Zhang /* add received vals into ba */ 468355d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 468455d1abb9SHong Zhang /* i-th row */ 468555d1abb9SHong Zhang if (i == *nextrow[k]) { 468655d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 468755d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 468855d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 468955d1abb9SHong Zhang nextaj = 0; 469055d1abb9SHong Zhang for (j=0; nextaj<anzi; j++){ 469155d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */ 469255d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 469355d1abb9SHong Zhang } 469455d1abb9SHong Zhang } 469555d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 469655d1abb9SHong Zhang } 469755d1abb9SHong Zhang } 469855d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 469955d1abb9SHong Zhang } 470055d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 470155d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 470255d1abb9SHong Zhang 4703533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 470455d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 470555d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 47061d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 47074ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 470855d1abb9SHong Zhang PetscFunctionReturn(0); 470955d1abb9SHong Zhang } 471038f152feSBarry Smith 47116bc0bbbfSBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat); 47126bc0bbbfSBarry Smith 471338f152feSBarry Smith #undef __FUNCT__ 471490431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic" 471590431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4716e5f2cdd8SHong Zhang { 4717f08fae4eSHong Zhang PetscErrorCode ierr; 471855a3bba9SHong Zhang Mat B_mpi; 4719c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4720b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4721b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4722d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4723a2f3521dSMark F. Adams PetscInt len,proc,*dnz,*onz,bs,cbs; 4724b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4725b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 472655d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 472758cb9c82SHong Zhang MPI_Status *status; 4728a1a86e44SBarry Smith PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL; 4729be0fcf8dSHong Zhang PetscBT lnkbt; 473051a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4731776b82aeSLisandro Dalcin PetscContainer container; 473202c68681SHong Zhang 4733e5f2cdd8SHong Zhang PetscFunctionBegin; 47344ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 47353c2c1871SHong Zhang 473638f152feSBarry Smith /* make sure it is a PETSc comm */ 473738f152feSBarry Smith ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr); 4738e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4739e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 474055d1abb9SHong Zhang 474151a7d1a8SHong Zhang ierr = PetscNew(Mat_Merge_SeqsToMPI,&merge);CHKERRQ(ierr); 4742c2234fe3SHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&status);CHKERRQ(ierr); 4743e5f2cdd8SHong Zhang 47446abd8857SHong Zhang /* determine row ownership */ 4745f08fae4eSHong Zhang /*---------------------------------------------------------*/ 474626283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 474726283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 474826283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 474926283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 475026283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4751b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&len_si);CHKERRQ(ierr); 4752b1d57f15SBarry Smith ierr = PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);CHKERRQ(ierr); 475355d1abb9SHong Zhang 47547a2fc3feSBarry Smith m = merge->rowmap->n; 47557a2fc3feSBarry Smith owners = merge->rowmap->range; 47566abd8857SHong Zhang 47576abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 47586abd8857SHong Zhang /*---------------------------------------------------------*/ 47593e06a4e6SHong Zhang len_s = merge->len_s; 476051a7d1a8SHong Zhang 47612257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4762c2234fe3SHong Zhang merge->nsend = 0; 4763409913e3SHong Zhang for (proc=0; proc<size; proc++){ 47642257cef7SHong Zhang len_si[proc] = 0; 47653e06a4e6SHong Zhang if (proc == rank){ 47666abd8857SHong Zhang len_s[proc] = 0; 47673e06a4e6SHong Zhang } else { 476802c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 47693e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 47703e06a4e6SHong Zhang } 47713e06a4e6SHong Zhang if (len_s[proc]) { 4772c2234fe3SHong Zhang merge->nsend++; 47732257cef7SHong Zhang nrows = 0; 47742257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 47752257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 47762257cef7SHong Zhang } 47772257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 47782257cef7SHong Zhang len += len_si[proc]; 4779409913e3SHong Zhang } 478058cb9c82SHong Zhang } 4781409913e3SHong Zhang 47822257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 47832257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 478451a7d1a8SHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 478555d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4786671beff6SHong Zhang 47873e06a4e6SHong Zhang /* post the Irecv of j-structure */ 47883e06a4e6SHong Zhang /*-------------------------------*/ 47892c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 47903e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 479102c68681SHong Zhang 47923e06a4e6SHong Zhang /* post the Isend of j-structure */ 4793affca5deSHong Zhang /*--------------------------------*/ 47941d79065fSBarry Smith ierr = PetscMalloc2(merge->nsend,MPI_Request,&si_waits,merge->nsend,MPI_Request,&sj_waits);CHKERRQ(ierr); 47953e06a4e6SHong Zhang 47962257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++){ 4797409913e3SHong Zhang if (!len_s[proc]) continue; 479802c68681SHong Zhang i = owners[proc]; 4799b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 480051a7d1a8SHong Zhang k++; 480151a7d1a8SHong Zhang } 480251a7d1a8SHong Zhang 48033e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 48043e06a4e6SHong Zhang /*------------------------------------------------*/ 48050c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 48060c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 480702c68681SHong Zhang 480802c68681SHong Zhang /* send and recv i-structure */ 480902c68681SHong Zhang /*---------------------------*/ 48102c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 481102c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 481202c68681SHong Zhang 4813b1d57f15SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);CHKERRQ(ierr); 48143e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 48152257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++){ 481602c68681SHong Zhang if (!len_s[proc]) continue; 48173e06a4e6SHong Zhang /* form outgoing message for i-structure: 48183e06a4e6SHong Zhang buf_si[0]: nrows to be sent 48193e06a4e6SHong Zhang [1:nrows]: row index (global) 48203e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 48213e06a4e6SHong Zhang */ 48223e06a4e6SHong Zhang /*-------------------------------------------*/ 48232257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 48243e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 48253e06a4e6SHong Zhang buf_si[0] = nrows; 48263e06a4e6SHong Zhang buf_si_i[0] = 0; 48273e06a4e6SHong Zhang nrows = 0; 48283e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++){ 48293e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 48303e06a4e6SHong Zhang if (anzi) { 48313e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 48323e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 48333e06a4e6SHong Zhang nrows++; 48343e06a4e6SHong Zhang } 48353e06a4e6SHong Zhang } 4836b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 483702c68681SHong Zhang k++; 48382257cef7SHong Zhang buf_si += len_si[proc]; 483902c68681SHong Zhang } 48402257cef7SHong Zhang 48410c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 48420c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 484302c68681SHong Zhang 4844ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 48453e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++){ 4846ae15b995SBarry 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); 48473e06a4e6SHong Zhang } 48483e06a4e6SHong Zhang 48493e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 485002c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 485102c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 48521d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 48532257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 48543e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4855bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 485658cb9c82SHong Zhang 4857bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4858bcc1bcd5SHong Zhang /*----------------------------------------------*/ 485958cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4860b1d57f15SBarry Smith ierr = PetscMalloc((m+1)*sizeof(PetscInt),&bi);CHKERRQ(ierr); 486158cb9c82SHong Zhang bi[0] = 0; 486258cb9c82SHong Zhang 4863be0fcf8dSHong Zhang /* create and initialize a linked list */ 4864be0fcf8dSHong Zhang nlnk = N+1; 4865be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 486658cb9c82SHong Zhang 4867bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 4868bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4869a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 487058cb9c82SHong Zhang current_space = free_space; 487158cb9c82SHong Zhang 4872bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 48730572522cSBarry Smith ierr = PetscMalloc3(merge->nrecv,PetscInt*,&buf_ri_k,merge->nrecv,PetscInt*,&nextrow,merge->nrecv,PetscInt*,&nextai);CHKERRQ(ierr); 48741d79065fSBarry Smith 48753e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++){ 48762257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 48773e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 48783e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 48792257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */ 48803e06a4e6SHong Zhang } 48812257cef7SHong Zhang 4882bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4883bcc1bcd5SHong Zhang len = 0; 488458cb9c82SHong Zhang for (i=0;i<m;i++) { 488558cb9c82SHong Zhang bnzi = 0; 488658cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 488758cb9c82SHong Zhang arow = owners[rank] + i; 488858cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 488958cb9c82SHong Zhang aj = a->j + ai[arow]; 4890dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 489158cb9c82SHong Zhang bnzi += nlnk; 489258cb9c82SHong Zhang /* add received col data into lnk */ 489351a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++){ /* k-th received message */ 489455d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 48953e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 48963e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 4897dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 48983e06a4e6SHong Zhang bnzi += nlnk; 48993e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 49003e06a4e6SHong Zhang } 490158cb9c82SHong Zhang } 4902bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 490358cb9c82SHong Zhang 490458cb9c82SHong Zhang /* if free space is not available, make more free space */ 490558cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 49064238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 490758cb9c82SHong Zhang nspacedouble++; 490858cb9c82SHong Zhang } 490958cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4910be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4911bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4912bcc1bcd5SHong Zhang 491358cb9c82SHong Zhang current_space->array += bnzi; 491458cb9c82SHong Zhang current_space->local_used += bnzi; 491558cb9c82SHong Zhang current_space->local_remaining -= bnzi; 491658cb9c82SHong Zhang 491758cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 491858cb9c82SHong Zhang } 4919bcc1bcd5SHong Zhang 49201d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4921bcc1bcd5SHong Zhang 4922b1d57f15SBarry Smith ierr = PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);CHKERRQ(ierr); 4923a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4924be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4925409913e3SHong Zhang 4926bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4927bcc1bcd5SHong Zhang /*---------------------------------------*/ 4928a2f3521dSMark F. Adams ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr); 4929f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 493054b84b50SHong Zhang if (n==PETSC_DECIDE) { 4931f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 493254b84b50SHong Zhang } else { 4933f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 493454b84b50SHong Zhang } 4935a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr); 4936bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4937bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4938bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 49397e63b356SHong Zhang ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 494058cb9c82SHong Zhang 494190431a8fSHong Zhang /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */ 49426abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4943affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4944affca5deSHong Zhang merge->bi = bi; 4945affca5deSHong Zhang merge->bj = bj; 494602c68681SHong Zhang merge->buf_ri = buf_ri; 494702c68681SHong Zhang merge->buf_rj = buf_rj; 4948de0260b3SHong Zhang merge->coi = PETSC_NULL; 4949de0260b3SHong Zhang merge->coj = PETSC_NULL; 4950de0260b3SHong Zhang merge->owners_co = PETSC_NULL; 4951affca5deSHong Zhang 4952bf0cc555SLisandro Dalcin ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 4953bf0cc555SLisandro Dalcin 4954affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4955776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4956776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4957affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4958bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 4959affca5deSHong Zhang *mpimat = B_mpi; 496038f152feSBarry Smith 49614ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4962e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4963e5f2cdd8SHong Zhang } 496425616d81SHong Zhang 496538f152feSBarry Smith #undef __FUNCT__ 496690431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ" 4967d4036a1aSHong Zhang /*@C 496890431a8fSHong Zhang MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential 4969d4036a1aSHong Zhang matrices from each processor 4970d4036a1aSHong Zhang 4971d4036a1aSHong Zhang Collective on MPI_Comm 4972d4036a1aSHong Zhang 4973d4036a1aSHong Zhang Input Parameters: 4974d4036a1aSHong Zhang + comm - the communicators the parallel matrix will live on 4975d4036a1aSHong Zhang . seqmat - the input sequential matrices 4976d4036a1aSHong Zhang . m - number of local rows (or PETSC_DECIDE) 4977d4036a1aSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4978d4036a1aSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4979d4036a1aSHong Zhang 4980d4036a1aSHong Zhang Output Parameter: 4981d4036a1aSHong Zhang . mpimat - the parallel matrix generated 4982d4036a1aSHong Zhang 4983d4036a1aSHong Zhang Level: advanced 4984d4036a1aSHong Zhang 4985d4036a1aSHong Zhang Notes: 4986d4036a1aSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4987d4036a1aSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4988d4036a1aSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4989d4036a1aSHong Zhang @*/ 499090431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 499155d1abb9SHong Zhang { 499255d1abb9SHong Zhang PetscErrorCode ierr; 49937e63b356SHong Zhang PetscMPIInt size; 499455d1abb9SHong Zhang 499555d1abb9SHong Zhang PetscFunctionBegin; 49967e63b356SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 49977e63b356SHong Zhang if (size == 1){ 49987e63b356SHong Zhang ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49997e63b356SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 50007e63b356SHong Zhang ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr); 50017e63b356SHong Zhang } else { 50027e63b356SHong Zhang ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 50037e63b356SHong Zhang } 50047e63b356SHong Zhang ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 50057e63b356SHong Zhang PetscFunctionReturn(0); 50067e63b356SHong Zhang } 50074ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 500855d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 500990431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 501055d1abb9SHong Zhang } 501190431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr); 50124ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 501355d1abb9SHong Zhang PetscFunctionReturn(0); 501455d1abb9SHong Zhang } 50154ebed01fSBarry Smith 501625616d81SHong Zhang #undef __FUNCT__ 50174a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat" 5018bc08b0f1SBarry Smith /*@ 50194a2b5492SBarry Smith MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with 50208661ff28SBarry Smith mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained 50218661ff28SBarry Smith with MatGetSize() 502225616d81SHong Zhang 502332fba14fSHong Zhang Not Collective 502425616d81SHong Zhang 502525616d81SHong Zhang Input Parameters: 502625616d81SHong Zhang + A - the matrix 502725616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 502825616d81SHong Zhang 502925616d81SHong Zhang Output Parameter: 503025616d81SHong Zhang . A_loc - the local sequential matrix generated 503125616d81SHong Zhang 503225616d81SHong Zhang Level: developer 503325616d81SHong Zhang 5034ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed() 50358661ff28SBarry Smith 503625616d81SHong Zhang @*/ 50374a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 503825616d81SHong Zhang { 503925616d81SHong Zhang PetscErrorCode ierr; 504001b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 504101b7ae99SHong Zhang Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data; 504201b7ae99SHong Zhang PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray; 5043a77337e4SBarry Smith MatScalar *aa=a->a,*ba=b->a,*cam; 5044a77337e4SBarry Smith PetscScalar *ca; 5045d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 50465a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 50478661ff28SBarry Smith PetscBool match; 504825616d81SHong Zhang 504925616d81SHong Zhang PetscFunctionBegin; 5050251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 50518661ff28SBarry Smith if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 50524ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 505301b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5054dea91ad1SHong Zhang ierr = PetscMalloc((1+am)*sizeof(PetscInt),&ci);CHKERRQ(ierr); 5055dea91ad1SHong Zhang ci[0] = 0; 505601b7ae99SHong Zhang for (i=0; i<am; i++){ 5057dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 505801b7ae99SHong Zhang } 5059dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);CHKERRQ(ierr); 5060dea91ad1SHong Zhang ierr = PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);CHKERRQ(ierr); 5061dea91ad1SHong Zhang k = 0; 506201b7ae99SHong Zhang for (i=0; i<am; i++) { 50635a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50645a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 506501b7ae99SHong Zhang /* off-diagonal portion of A */ 50665a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50675a7d977cSHong Zhang col = cmap[*bj]; 50685a7d977cSHong Zhang if (col >= cstart) break; 50695a7d977cSHong Zhang cj[k] = col; bj++; 50705a7d977cSHong Zhang ca[k++] = *ba++; 50715a7d977cSHong Zhang } 50725a7d977cSHong Zhang /* diagonal portion of A */ 50735a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 50745a7d977cSHong Zhang cj[k] = cstart + *aj++; 50755a7d977cSHong Zhang ca[k++] = *aa++; 50765a7d977cSHong Zhang } 50775a7d977cSHong Zhang /* off-diagonal portion of A */ 50785a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 50795a7d977cSHong Zhang cj[k] = cmap[*bj++]; 50805a7d977cSHong Zhang ca[k++] = *ba++; 50815a7d977cSHong Zhang } 508225616d81SHong Zhang } 5083dea91ad1SHong Zhang /* put together the new matrix */ 5084d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 5085dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5086dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 5087dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 5088e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 5089e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 5090dea91ad1SHong Zhang mat->nonew = 0; 50915a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 50925a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 5093a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 50945a7d977cSHong Zhang for (i=0; i<am; i++) { 50955a7d977cSHong Zhang /* off-diagonal portion of A */ 50965a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50975a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50985a7d977cSHong Zhang col = cmap[*bj]; 50995a7d977cSHong Zhang if (col >= cstart) break; 5100a77337e4SBarry Smith *cam++ = *ba++; bj++; 51015a7d977cSHong Zhang } 51025a7d977cSHong Zhang /* diagonal portion of A */ 5103ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 5104a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 51055a7d977cSHong Zhang /* off-diagonal portion of A */ 5106f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 5107a77337e4SBarry Smith *cam++ = *ba++; bj++; 5108f33d1a9aSHong Zhang } 51095a7d977cSHong Zhang } 51108661ff28SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 51114ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 511225616d81SHong Zhang PetscFunctionReturn(0); 511325616d81SHong Zhang } 511425616d81SHong Zhang 511532fba14fSHong Zhang #undef __FUNCT__ 51164a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed" 511732fba14fSHong Zhang /*@C 5118ba264940SBarry Smith MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns 511932fba14fSHong Zhang 512032fba14fSHong Zhang Not Collective 512132fba14fSHong Zhang 512232fba14fSHong Zhang Input Parameters: 512332fba14fSHong Zhang + A - the matrix 512432fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 512532fba14fSHong Zhang - row, col - index sets of rows and columns to extract (or PETSC_NULL) 512632fba14fSHong Zhang 512732fba14fSHong Zhang Output Parameter: 512832fba14fSHong Zhang . A_loc - the local sequential matrix generated 512932fba14fSHong Zhang 513032fba14fSHong Zhang Level: developer 513132fba14fSHong Zhang 5132ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat() 5133ba264940SBarry Smith 513432fba14fSHong Zhang @*/ 51354a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 513632fba14fSHong Zhang { 513732fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 513832fba14fSHong Zhang PetscErrorCode ierr; 513932fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 514032fba14fSHong Zhang IS isrowa,iscola; 514132fba14fSHong Zhang Mat *aloc; 51424a2b5492SBarry Smith PetscBool match; 514332fba14fSHong Zhang 514432fba14fSHong Zhang PetscFunctionBegin; 5145251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 51464a2b5492SBarry Smith if (!match) SETERRQ(((PetscObject)A)->comm, PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 51474ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 514832fba14fSHong Zhang if (!row){ 5149d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 515032fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 515132fba14fSHong Zhang } else { 515232fba14fSHong Zhang isrowa = *row; 515332fba14fSHong Zhang } 515432fba14fSHong Zhang if (!col){ 5155d0f46423SBarry Smith start = A->cmap->rstart; 515632fba14fSHong Zhang cmap = a->garray; 5157d0f46423SBarry Smith nzA = a->A->cmap->n; 5158d0f46423SBarry Smith nzB = a->B->cmap->n; 515932fba14fSHong Zhang ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 516032fba14fSHong Zhang ncols = 0; 516132fba14fSHong Zhang for (i=0; i<nzB; i++) { 516232fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 516332fba14fSHong Zhang else break; 516432fba14fSHong Zhang } 516532fba14fSHong Zhang imark = i; 516632fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 516732fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 5168d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 516932fba14fSHong Zhang } else { 517032fba14fSHong Zhang iscola = *col; 517132fba14fSHong Zhang } 517232fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX){ 517332fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 517432fba14fSHong Zhang aloc[0] = *A_loc; 517532fba14fSHong Zhang } 517632fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 517732fba14fSHong Zhang *A_loc = aloc[0]; 517832fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 517932fba14fSHong Zhang if (!row){ 51806bf464f9SBarry Smith ierr = ISDestroy(&isrowa);CHKERRQ(ierr); 518132fba14fSHong Zhang } 518232fba14fSHong Zhang if (!col){ 51836bf464f9SBarry Smith ierr = ISDestroy(&iscola);CHKERRQ(ierr); 518432fba14fSHong Zhang } 51854ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 518632fba14fSHong Zhang PetscFunctionReturn(0); 518732fba14fSHong Zhang } 518832fba14fSHong Zhang 518925616d81SHong Zhang #undef __FUNCT__ 519025616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 519125616d81SHong Zhang /*@C 519232fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 519325616d81SHong Zhang 519425616d81SHong Zhang Collective on Mat 519525616d81SHong Zhang 519625616d81SHong Zhang Input Parameters: 5197e240928fSHong Zhang + A,B - the matrices in mpiaij format 519825616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 519925616d81SHong Zhang - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL) 520025616d81SHong Zhang 520125616d81SHong Zhang Output Parameter: 520225616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 520325616d81SHong Zhang - B_seq - the sequential matrix generated 520425616d81SHong Zhang 520525616d81SHong Zhang Level: developer 520625616d81SHong Zhang 520725616d81SHong Zhang @*/ 520866bfb163SHong Zhang PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq) 520925616d81SHong Zhang { 5210899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 521125616d81SHong Zhang PetscErrorCode ierr; 5212b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 521325616d81SHong Zhang IS isrowb,iscolb; 521466bfb163SHong Zhang Mat *bseq=PETSC_NULL; 521525616d81SHong Zhang 521625616d81SHong Zhang PetscFunctionBegin; 5217d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 5218e32f2f54SBarry 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); 521925616d81SHong Zhang } 52204ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 522125616d81SHong Zhang 522225616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5223d0f46423SBarry Smith start = A->cmap->rstart; 522425616d81SHong Zhang cmap = a->garray; 5225d0f46423SBarry Smith nzA = a->A->cmap->n; 5226d0f46423SBarry Smith nzB = a->B->cmap->n; 5227b1d57f15SBarry Smith ierr = PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);CHKERRQ(ierr); 522825616d81SHong Zhang ncols = 0; 52290390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 523025616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 523125616d81SHong Zhang else break; 523225616d81SHong Zhang } 523325616d81SHong Zhang imark = i; 52340390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 52350390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 5236d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 5237d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 523825616d81SHong Zhang } else { 5239e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 524025616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 524125616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 524225616d81SHong Zhang bseq[0] = *B_seq; 524325616d81SHong Zhang } 524425616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 524525616d81SHong Zhang *B_seq = bseq[0]; 524625616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 524725616d81SHong Zhang if (!rowb){ 52486bf464f9SBarry Smith ierr = ISDestroy(&isrowb);CHKERRQ(ierr); 524925616d81SHong Zhang } else { 525025616d81SHong Zhang *rowb = isrowb; 525125616d81SHong Zhang } 525225616d81SHong Zhang if (!colb){ 52536bf464f9SBarry Smith ierr = ISDestroy(&iscolb);CHKERRQ(ierr); 525425616d81SHong Zhang } else { 525525616d81SHong Zhang *colb = iscolb; 525625616d81SHong Zhang } 52574ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 525825616d81SHong Zhang PetscFunctionReturn(0); 525925616d81SHong Zhang } 5260429d309bSHong Zhang 5261a61c8c0fSHong Zhang #undef __FUNCT__ 5262f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ" 5263f8487c73SHong Zhang /* 5264f8487c73SHong Zhang MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 526501b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 5266429d309bSHong Zhang 5267429d309bSHong Zhang Collective on Mat 5268429d309bSHong Zhang 5269429d309bSHong Zhang Input Parameters: 5270429d309bSHong Zhang + A,B - the matrices in mpiaij format 5271598bc09dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 5272429d309bSHong Zhang 5273429d309bSHong Zhang Output Parameter: 5274b7f45c76SHong Zhang + startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or PETSC_NULL) 5275b7f45c76SHong Zhang . startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL) 5276598bc09dSHong Zhang . bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL) 5277598bc09dSHong Zhang - B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N 5278429d309bSHong Zhang 5279429d309bSHong Zhang Level: developer 5280429d309bSHong Zhang 5281f8487c73SHong Zhang */ 5282b7f45c76SHong Zhang PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 5283429d309bSHong Zhang { 5284a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 5285429d309bSHong Zhang PetscErrorCode ierr; 5286899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 528787025532SHong Zhang Mat_SeqAIJ *b_oth; 5288a6b2eed2SHong Zhang VecScatter ctx=a->Mvctx; 52897adad957SLisandro Dalcin MPI_Comm comm=((PetscObject)ctx)->comm; 52907adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 5291d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 5292dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 5293dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 5294e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 5295910ba992SMatthew Knepley MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL; 529687025532SHong Zhang MPI_Status *sstatus,rstatus; 5297aa5bb8c0SSatish Balay PetscMPIInt jj; 5298e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 5299ba8c8a56SBarry Smith PetscScalar *vals; 5300429d309bSHong Zhang 5301429d309bSHong Zhang PetscFunctionBegin; 5302d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend){ 5303e32f2f54SBarry 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); 5304429d309bSHong Zhang } 53054ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5306a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5307a6b2eed2SHong Zhang 5308a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5309a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5310e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5311e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5312a6b2eed2SHong Zhang nrecvs = gen_from->n; 5313a6b2eed2SHong Zhang nsends = gen_to->n; 5314d7ee0231SBarry Smith 5315d7ee0231SBarry Smith ierr = PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);CHKERRQ(ierr); 5316a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5317a6b2eed2SHong Zhang sstarts = gen_to->starts; 5318a6b2eed2SHong Zhang sprocs = gen_to->procs; 5319a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5320e42f35eeSHong Zhang sbs = gen_to->bs; 5321e42f35eeSHong Zhang rstarts = gen_from->starts; 5322e42f35eeSHong Zhang rprocs = gen_from->procs; 5323e42f35eeSHong Zhang rbs = gen_from->bs; 5324429d309bSHong Zhang 5325b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5326429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5327a6b2eed2SHong Zhang /* i-array */ 5328a6b2eed2SHong Zhang /*---------*/ 5329a6b2eed2SHong Zhang /* post receives */ 5330a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5331e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5332e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 533387025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5334429d309bSHong Zhang } 5335a6b2eed2SHong Zhang 5336a6b2eed2SHong Zhang /* pack the outgoing message */ 53371d79065fSBarry Smith ierr = PetscMalloc2(nsends+1,PetscInt,&sstartsj,nrecvs+1,PetscInt,&rstartsj);CHKERRQ(ierr); 5338a6b2eed2SHong Zhang sstartsj[0] = 0; rstartsj[0] = 0; 5339a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5340a6b2eed2SHong Zhang k = 0; 5341a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5342e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5343e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 534487025532SHong Zhang for (j=0; j<nrows; j++) { 5345d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5346e42f35eeSHong Zhang for (l=0; l<sbs; l++){ 5347e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* rowlength */ 5348e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 5349e42f35eeSHong Zhang len += ncols; 5350e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5351e42f35eeSHong Zhang } 5352a6b2eed2SHong Zhang k++; 5353429d309bSHong Zhang } 5354e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5355dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5356429d309bSHong Zhang } 535787025532SHong Zhang /* recvs and sends of i-array are completed */ 535887025532SHong Zhang i = nrecvs; 535987025532SHong Zhang while (i--) { 5360aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 536187025532SHong Zhang } 53620c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5363e42f35eeSHong Zhang 5364a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5365a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscInt),&bufj);CHKERRQ(ierr); 5366a6b2eed2SHong Zhang ierr = PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);CHKERRQ(ierr); 5367a6b2eed2SHong Zhang 536887025532SHong Zhang /* create i-array of B_oth */ 536987025532SHong Zhang ierr = PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);CHKERRQ(ierr); 537087025532SHong Zhang b_othi[0] = 0; 5371a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5372a6b2eed2SHong Zhang k = 0; 5373a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 5374fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5375e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 537687025532SHong Zhang for (j=0; j<nrows; j++) { 537787025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5378a6b2eed2SHong Zhang len += rowlen[j]; k++; 5379a6b2eed2SHong Zhang } 5380dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5381a6b2eed2SHong Zhang } 5382a6b2eed2SHong Zhang 538387025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 538487025532SHong Zhang ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);CHKERRQ(ierr); 5385dd6ea824SBarry Smith ierr = PetscMalloc((b_othi[aBn]+1)*sizeof(MatScalar),&b_otha);CHKERRQ(ierr); 5386a6b2eed2SHong Zhang 538787025532SHong Zhang /* j-array */ 538887025532SHong Zhang /*---------*/ 5389a6b2eed2SHong Zhang /* post receives of j-array */ 5390a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++){ 539187025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 539287025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5393a6b2eed2SHong Zhang } 5394e42f35eeSHong Zhang 5395e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5396a6b2eed2SHong Zhang k = 0; 5397a6b2eed2SHong Zhang for (i=0; i<nsends; i++){ 5398e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5399a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 540087025532SHong Zhang for (j=0; j<nrows; j++) { 5401d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5402e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5403e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5404a6b2eed2SHong Zhang for (l=0; l<ncols; l++){ 5405a6b2eed2SHong Zhang *bufJ++ = cols[l]; 540687025532SHong Zhang } 5407e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,PETSC_NULL);CHKERRQ(ierr); 5408e42f35eeSHong Zhang } 540987025532SHong Zhang } 541087025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 541187025532SHong Zhang } 541287025532SHong Zhang 541387025532SHong Zhang /* recvs and sends of j-array are completed */ 541487025532SHong Zhang i = nrecvs; 541587025532SHong Zhang while (i--) { 5416aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 541787025532SHong Zhang } 54180c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 541987025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX){ 5420b7f45c76SHong Zhang sstartsj = *startsj_s; 54211d79065fSBarry Smith rstartsj = *startsj_r; 542287025532SHong Zhang bufa = *bufa_ptr; 542387025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 542487025532SHong Zhang b_otha = b_oth->a; 542587025532SHong Zhang } else { 5426e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 542787025532SHong Zhang } 542887025532SHong Zhang 542987025532SHong Zhang /* a-array */ 543087025532SHong Zhang /*---------*/ 543187025532SHong Zhang /* post receives of a-array */ 543287025532SHong Zhang for (i=0; i<nrecvs; i++){ 543387025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 543487025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 543587025532SHong Zhang } 5436e42f35eeSHong Zhang 5437e42f35eeSHong Zhang /* pack the outgoing message a-array */ 543887025532SHong Zhang k = 0; 543987025532SHong Zhang for (i=0; i<nsends; i++){ 5440e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 544187025532SHong Zhang bufA = bufa+sstartsj[i]; 544287025532SHong Zhang for (j=0; j<nrows; j++) { 5443d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5444e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++){ 5445e42f35eeSHong Zhang ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 544687025532SHong Zhang for (l=0; l<ncols; l++){ 5447a6b2eed2SHong Zhang *bufA++ = vals[l]; 5448a6b2eed2SHong Zhang } 5449e42f35eeSHong Zhang ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,PETSC_NULL,&vals);CHKERRQ(ierr); 5450e42f35eeSHong Zhang } 5451a6b2eed2SHong Zhang } 545287025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5453a6b2eed2SHong Zhang } 545487025532SHong Zhang /* recvs and sends of a-array are completed */ 545587025532SHong Zhang i = nrecvs; 545687025532SHong Zhang while (i--) { 5457aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 545887025532SHong Zhang } 54590c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5460d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5461a6b2eed2SHong Zhang 546287025532SHong Zhang if (scall == MAT_INITIAL_MATRIX){ 5463a6b2eed2SHong Zhang /* put together the new matrix */ 5464d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5465a6b2eed2SHong Zhang 5466a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5467a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 546887025532SHong Zhang b_oth = (Mat_SeqAIJ *)(*B_oth)->data; 5469e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5470e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 547187025532SHong Zhang b_oth->nonew = 0; 5472a6b2eed2SHong Zhang 5473a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5474b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr){ 54751d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5476dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5477dea91ad1SHong Zhang } else { 5478b7f45c76SHong Zhang *startsj_s = sstartsj; 54791d79065fSBarry Smith *startsj_r = rstartsj; 548087025532SHong Zhang *bufa_ptr = bufa; 548187025532SHong Zhang } 5482dea91ad1SHong Zhang } 54834ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5484429d309bSHong Zhang PetscFunctionReturn(0); 5485429d309bSHong Zhang } 5486ccd8e176SBarry Smith 548743eb5e2fSMatthew Knepley #undef __FUNCT__ 548843eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 548943eb5e2fSMatthew Knepley /*@C 549043eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 549143eb5e2fSMatthew Knepley 549243eb5e2fSMatthew Knepley Not Collective 549343eb5e2fSMatthew Knepley 549443eb5e2fSMatthew Knepley Input Parameters: 549543eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 549643eb5e2fSMatthew Knepley 549743eb5e2fSMatthew Knepley Output Parameter: 549843eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 549943eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 550043eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 550143eb5e2fSMatthew Knepley 550243eb5e2fSMatthew Knepley Level: developer 550343eb5e2fSMatthew Knepley 550443eb5e2fSMatthew Knepley @*/ 550543eb5e2fSMatthew Knepley #if defined (PETSC_USE_CTABLE) 55067087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 550743eb5e2fSMatthew Knepley #else 55087087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 550943eb5e2fSMatthew Knepley #endif 551043eb5e2fSMatthew Knepley { 551143eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 551243eb5e2fSMatthew Knepley 551343eb5e2fSMatthew Knepley PetscFunctionBegin; 55140700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5515e414b56bSJed Brown PetscValidPointer(lvec, 2); 5516e414b56bSJed Brown PetscValidPointer(colmap, 3); 5517e414b56bSJed Brown PetscValidPointer(multScatter, 4); 551843eb5e2fSMatthew Knepley a = (Mat_MPIAIJ *) A->data; 551943eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 552043eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 552143eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 552243eb5e2fSMatthew Knepley PetscFunctionReturn(0); 552343eb5e2fSMatthew Knepley } 552443eb5e2fSMatthew Knepley 552517667f90SBarry Smith EXTERN_C_BEGIN 552619fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*); 552719fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*); 552819fd82e9SBarry Smith extern PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*); 552917667f90SBarry Smith EXTERN_C_END 553017667f90SBarry Smith 5531fc4dec0aSBarry Smith #undef __FUNCT__ 5532fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5533fc4dec0aSBarry Smith /* 5534fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5535fc4dec0aSBarry Smith 5536fc4dec0aSBarry Smith n p p 5537fc4dec0aSBarry Smith ( ) ( ) ( ) 5538fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5539fc4dec0aSBarry Smith ( ) ( ) ( ) 5540fc4dec0aSBarry Smith 5541fc4dec0aSBarry Smith */ 5542fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5543fc4dec0aSBarry Smith { 5544fc4dec0aSBarry Smith PetscErrorCode ierr; 5545fc4dec0aSBarry Smith Mat At,Bt,Ct; 5546fc4dec0aSBarry Smith 5547fc4dec0aSBarry Smith PetscFunctionBegin; 5548fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5549fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5550fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 55516bf464f9SBarry Smith ierr = MatDestroy(&At);CHKERRQ(ierr); 55526bf464f9SBarry Smith ierr = MatDestroy(&Bt);CHKERRQ(ierr); 5553fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 55546bf464f9SBarry Smith ierr = MatDestroy(&Ct);CHKERRQ(ierr); 5555fc4dec0aSBarry Smith PetscFunctionReturn(0); 5556fc4dec0aSBarry Smith } 5557fc4dec0aSBarry Smith 5558fc4dec0aSBarry Smith #undef __FUNCT__ 5559fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5560fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5561fc4dec0aSBarry Smith { 5562fc4dec0aSBarry Smith PetscErrorCode ierr; 5563d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5564fc4dec0aSBarry Smith Mat Cmat; 5565fc4dec0aSBarry Smith 5566fc4dec0aSBarry Smith PetscFunctionBegin; 5567e32f2f54SBarry 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); 556839804f7cSBarry Smith ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr); 5569fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5570a2f3521dSMark F. Adams ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 5571fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 5572fc4dec0aSBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr); 557338556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 557438556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5575f75ecaa4SHong Zhang 5576f75ecaa4SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ; 5577fc4dec0aSBarry Smith *C = Cmat; 5578fc4dec0aSBarry Smith PetscFunctionReturn(0); 5579fc4dec0aSBarry Smith } 5580fc4dec0aSBarry Smith 5581fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5582fc4dec0aSBarry Smith #undef __FUNCT__ 5583fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5584fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5585fc4dec0aSBarry Smith { 5586fc4dec0aSBarry Smith PetscErrorCode ierr; 5587fc4dec0aSBarry Smith 5588fc4dec0aSBarry Smith PetscFunctionBegin; 5589fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX){ 5590fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 5591fc4dec0aSBarry Smith } 5592fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 5593fc4dec0aSBarry Smith PetscFunctionReturn(0); 5594fc4dec0aSBarry Smith } 5595fc4dec0aSBarry Smith 55965c9eb25fSBarry Smith EXTERN_C_BEGIN 5597611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5598bccb9932SShri Abhyankar extern PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5599611f576cSBarry Smith #endif 56003bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 56013bf14a46SMatthew Knepley extern PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 56023bf14a46SMatthew Knepley #endif 5603611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 56045c9eb25fSBarry Smith extern PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5605611f576cSBarry Smith #endif 560617f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 560717f1a0eaSHong Zhang extern PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*); 560817f1a0eaSHong Zhang #endif 56095c9eb25fSBarry Smith EXTERN_C_END 56105c9eb25fSBarry Smith 5611ccd8e176SBarry Smith /*MC 5612ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5613ccd8e176SBarry Smith 5614ccd8e176SBarry Smith Options Database Keys: 5615ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5616ccd8e176SBarry Smith 5617ccd8e176SBarry Smith Level: beginner 5618ccd8e176SBarry Smith 561969b1f4b7SBarry Smith .seealso: MatCreateAIJ() 5620ccd8e176SBarry Smith M*/ 5621ccd8e176SBarry Smith 5622ccd8e176SBarry Smith EXTERN_C_BEGIN 5623ccd8e176SBarry Smith #undef __FUNCT__ 5624ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 56257087cfbeSBarry Smith PetscErrorCode MatCreate_MPIAIJ(Mat B) 5626ccd8e176SBarry Smith { 5627ccd8e176SBarry Smith Mat_MPIAIJ *b; 5628ccd8e176SBarry Smith PetscErrorCode ierr; 5629ccd8e176SBarry Smith PetscMPIInt size; 5630ccd8e176SBarry Smith 5631ccd8e176SBarry Smith PetscFunctionBegin; 56327adad957SLisandro Dalcin ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr); 563338f2d2fdSLisandro Dalcin ierr = PetscNewLog(B,Mat_MPIAIJ,&b);CHKERRQ(ierr); 5634ccd8e176SBarry Smith B->data = (void*)b; 5635ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5636ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5637ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5638ccd8e176SBarry Smith b->size = size; 56397adad957SLisandro Dalcin ierr = MPI_Comm_rank(((PetscObject)B)->comm,&b->rank);CHKERRQ(ierr); 5640ccd8e176SBarry Smith 5641ccd8e176SBarry Smith /* build cache for off array entries formed */ 56427adad957SLisandro Dalcin ierr = MatStashCreate_Private(((PetscObject)B)->comm,1,&B->stash);CHKERRQ(ierr); 5643ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5644ccd8e176SBarry Smith b->colmap = 0; 5645ccd8e176SBarry Smith b->garray = 0; 5646ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5647ccd8e176SBarry Smith 5648ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 5649ccd8e176SBarry Smith b->lvec = PETSC_NULL; 5650ccd8e176SBarry Smith b->Mvctx = PETSC_NULL; 5651ccd8e176SBarry Smith 5652ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5653ccd8e176SBarry Smith b->rowindices = 0; 5654ccd8e176SBarry Smith b->rowvalues = 0; 5655ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5656ccd8e176SBarry Smith 5657bbf3fe20SPaul Mullowney /* flexible pointer used in CUSP/CUSPARSE classes */ 5658bbf3fe20SPaul Mullowney b->spptr = PETSC_NULL; 5659f60c3dc2SHong Zhang 5660611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5661ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C", 5662bccb9932SShri Abhyankar "MatGetFactor_aij_mumps", 5663bccb9932SShri Abhyankar MatGetFactor_aij_mumps);CHKERRQ(ierr); 5664611f576cSBarry Smith #endif 56653bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5666ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C", 56673bf14a46SMatthew Knepley "MatGetFactor_mpiaij_pastix", 56683bf14a46SMatthew Knepley MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 56693bf14a46SMatthew Knepley #endif 5670611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5671ec1065edSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C", 56725c9eb25fSBarry Smith "MatGetFactor_mpiaij_superlu_dist", 56735c9eb25fSBarry Smith MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5674611f576cSBarry Smith #endif 567517f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 567617f1a0eaSHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_clique_C", 567717f1a0eaSHong Zhang "MatGetFactor_aij_clique", 567817f1a0eaSHong Zhang MatGetFactor_aij_clique);CHKERRQ(ierr); 567917f1a0eaSHong Zhang #endif 5680ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C", 5681ccd8e176SBarry Smith "MatStoreValues_MPIAIJ", 5682ccd8e176SBarry Smith MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5683ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C", 5684ccd8e176SBarry Smith "MatRetrieveValues_MPIAIJ", 5685ccd8e176SBarry Smith MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5686ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C", 5687ccd8e176SBarry Smith "MatGetDiagonalBlock_MPIAIJ", 5688ccd8e176SBarry Smith MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5689ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C", 5690ccd8e176SBarry Smith "MatIsTranspose_MPIAIJ", 5691ccd8e176SBarry Smith MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5692ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C", 5693ccd8e176SBarry Smith "MatMPIAIJSetPreallocation_MPIAIJ", 5694ccd8e176SBarry Smith MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5695ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C", 5696ccd8e176SBarry Smith "MatMPIAIJSetPreallocationCSR_MPIAIJ", 5697ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5698ccd8e176SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C", 5699ccd8e176SBarry Smith "MatDiagonalScaleLocal_MPIAIJ", 5700ccd8e176SBarry Smith MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 57015a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C", 57025a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJPERM", 57035a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 57045a11e1b2SBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C", 57055a11e1b2SBarry Smith "MatConvert_MPIAIJ_MPIAIJCRL", 57065a11e1b2SBarry Smith MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5707471cc821SHong Zhang ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C", 5708471cc821SHong Zhang "MatConvert_MPIAIJ_MPISBAIJ", 5709471cc821SHong Zhang MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5710fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_mpidense_mpiaij_C", 5711fc4dec0aSBarry Smith "MatMatMult_MPIDense_MPIAIJ", 5712fc4dec0aSBarry Smith MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5713fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C", 5714fc4dec0aSBarry Smith "MatMatMultSymbolic_MPIDense_MPIAIJ", 5715fc4dec0aSBarry Smith MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5716fc4dec0aSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C", 5717fc4dec0aSBarry Smith "MatMatMultNumeric_MPIDense_MPIAIJ", 5718fc4dec0aSBarry Smith MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 571917667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5720ccd8e176SBarry Smith PetscFunctionReturn(0); 5721ccd8e176SBarry Smith } 5722ccd8e176SBarry Smith EXTERN_C_END 572381824310SBarry Smith 572403bfb495SBarry Smith #undef __FUNCT__ 572503bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 572658d36128SBarry Smith /*@ 572703bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 572803bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 572903bfb495SBarry Smith 573003bfb495SBarry Smith Collective on MPI_Comm 573103bfb495SBarry Smith 573203bfb495SBarry Smith Input Parameters: 573303bfb495SBarry Smith + comm - MPI communicator 573403bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 573503bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 573603bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 573703bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 573803bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 573903bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 574003bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 574103bfb495SBarry Smith . j - column indices 574203bfb495SBarry Smith . a - matrix values 574303bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 574403bfb495SBarry Smith . oj - column indices 574503bfb495SBarry Smith - oa - matrix values 574603bfb495SBarry Smith 574703bfb495SBarry Smith Output Parameter: 574803bfb495SBarry Smith . mat - the matrix 574903bfb495SBarry Smith 575003bfb495SBarry Smith Level: advanced 575103bfb495SBarry Smith 575203bfb495SBarry Smith Notes: 5753292fb18eSBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user 5754292fb18eSBarry Smith must free the arrays once the matrix has been destroyed and not before. 575503bfb495SBarry Smith 575603bfb495SBarry Smith The i and j indices are 0 based 575703bfb495SBarry Smith 575869b1f4b7SBarry Smith See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 575903bfb495SBarry Smith 57607b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 57617b55108eSBarry Smith 57627b55108eSBarry Smith You cannot later use MatSetValues() to change values in this matrix. 576303bfb495SBarry Smith 576403bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 576503bfb495SBarry Smith 576603bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 576769b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays() 576803bfb495SBarry Smith @*/ 57697087cfbeSBarry Smith PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[], 577003bfb495SBarry Smith PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 577103bfb495SBarry Smith { 577203bfb495SBarry Smith PetscErrorCode ierr; 577303bfb495SBarry Smith Mat_MPIAIJ *maij; 577403bfb495SBarry Smith 577503bfb495SBarry Smith PetscFunctionBegin; 5776e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 5777ea345e14SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 5778ea345e14SBarry Smith if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 577903bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 578003bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 578103bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 578203bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 57838d7a6e47SBarry Smith maij->donotstash = PETSC_TRUE; 57848d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 578503bfb495SBarry Smith 578626283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 578726283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 578803bfb495SBarry Smith 578903bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5790d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 579103bfb495SBarry Smith 57928d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57938d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57948d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57958d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 57968d7a6e47SBarry Smith 579703bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 579803bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 579903bfb495SBarry Smith PetscFunctionReturn(0); 580003bfb495SBarry Smith } 580103bfb495SBarry Smith 580281824310SBarry Smith /* 580381824310SBarry Smith Special version for direct calls from Fortran 580481824310SBarry Smith */ 5805b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h> 58067087cfbeSBarry Smith 580781824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 580881824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 580981824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 581081824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 581181824310SBarry Smith #endif 581281824310SBarry Smith 581381824310SBarry Smith /* Change these macros so can be used in void function */ 581481824310SBarry Smith #undef CHKERRQ 5815e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 581681824310SBarry Smith #undef SETERRQ2 5817e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 58184994cf47SJed Brown #undef SETERRQ3 58194994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 582081824310SBarry Smith #undef SETERRQ 5821e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 582281824310SBarry Smith 582381824310SBarry Smith EXTERN_C_BEGIN 582481824310SBarry Smith #undef __FUNCT__ 582581824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 58261f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 582781824310SBarry Smith { 582881824310SBarry Smith Mat mat = *mmat; 582981824310SBarry Smith PetscInt m = *mm, n = *mn; 583081824310SBarry Smith InsertMode addv = *maddv; 583181824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 583281824310SBarry Smith PetscScalar value; 583381824310SBarry Smith PetscErrorCode ierr; 5834899cda47SBarry Smith 58354994cf47SJed Brown MatCheckPreallocated(mat,1); 583681824310SBarry Smith if (mat->insertmode == NOT_SET_VALUES) { 583781824310SBarry Smith mat->insertmode = addv; 583881824310SBarry Smith } 583981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 584081824310SBarry Smith else if (mat->insertmode != addv) { 5841e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 584281824310SBarry Smith } 584381824310SBarry Smith #endif 584481824310SBarry Smith { 5845d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5846d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5847ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 584881824310SBarry Smith 584981824310SBarry Smith /* Some Variables required in the macro */ 585081824310SBarry Smith Mat A = aij->A; 585181824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 585281824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5853dd6ea824SBarry Smith MatScalar *aa = a->a; 5854ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE); 585581824310SBarry Smith Mat B = aij->B; 585681824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5857d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5858dd6ea824SBarry Smith MatScalar *ba = b->a; 585981824310SBarry Smith 586081824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 586181824310SBarry Smith PetscInt nonew = a->nonew; 5862dd6ea824SBarry Smith MatScalar *ap1,*ap2; 586381824310SBarry Smith 586481824310SBarry Smith PetscFunctionBegin; 586581824310SBarry Smith for (i=0; i<m; i++) { 586681824310SBarry Smith if (im[i] < 0) continue; 586781824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5868e32f2f54SBarry 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); 586981824310SBarry Smith #endif 587081824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 587181824310SBarry Smith row = im[i] - rstart; 587281824310SBarry Smith lastcol1 = -1; 587381824310SBarry Smith rp1 = aj + ai[row]; 587481824310SBarry Smith ap1 = aa + ai[row]; 587581824310SBarry Smith rmax1 = aimax[row]; 587681824310SBarry Smith nrow1 = ailen[row]; 587781824310SBarry Smith low1 = 0; 587881824310SBarry Smith high1 = nrow1; 587981824310SBarry Smith lastcol2 = -1; 588081824310SBarry Smith rp2 = bj + bi[row]; 588181824310SBarry Smith ap2 = ba + bi[row]; 588281824310SBarry Smith rmax2 = bimax[row]; 588381824310SBarry Smith nrow2 = bilen[row]; 588481824310SBarry Smith low2 = 0; 588581824310SBarry Smith high2 = nrow2; 588681824310SBarry Smith 588781824310SBarry Smith for (j=0; j<n; j++) { 588881824310SBarry Smith if (roworiented) value = v[i*n+j]; else value = v[i+j*m]; 588981824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 589081824310SBarry Smith if (in[j] >= cstart && in[j] < cend){ 589181824310SBarry Smith col = in[j] - cstart; 589281824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 589381824310SBarry Smith } else if (in[j] < 0) continue; 589481824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5895cb9801acSJed 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); 589681824310SBarry Smith #endif 589781824310SBarry Smith else { 589881824310SBarry Smith if (mat->was_assembled) { 589981824310SBarry Smith if (!aij->colmap) { 5900ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 590181824310SBarry Smith } 590281824310SBarry Smith #if defined (PETSC_USE_CTABLE) 590381824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 590481824310SBarry Smith col--; 590581824310SBarry Smith #else 590681824310SBarry Smith col = aij->colmap[in[j]] - 1; 590781824310SBarry Smith #endif 590881824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 5909ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 591081824310SBarry Smith col = in[j]; 591181824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 591281824310SBarry Smith B = aij->B; 591381824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 591481824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 591581824310SBarry Smith rp2 = bj + bi[row]; 591681824310SBarry Smith ap2 = ba + bi[row]; 591781824310SBarry Smith rmax2 = bimax[row]; 591881824310SBarry Smith nrow2 = bilen[row]; 591981824310SBarry Smith low2 = 0; 592081824310SBarry Smith high2 = nrow2; 5921d0f46423SBarry Smith bm = aij->B->rmap->n; 592281824310SBarry Smith ba = b->a; 592381824310SBarry Smith } 592481824310SBarry Smith } else col = in[j]; 592581824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 592681824310SBarry Smith } 592781824310SBarry Smith } 592881824310SBarry Smith } else { 592981824310SBarry Smith if (!aij->donotstash) { 593081824310SBarry Smith if (roworiented) { 5931ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 593281824310SBarry Smith } else { 5933ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 593481824310SBarry Smith } 593581824310SBarry Smith } 593681824310SBarry Smith } 593781824310SBarry Smith }} 593881824310SBarry Smith PetscFunctionReturnVoid(); 593981824310SBarry Smith } 594081824310SBarry Smith EXTERN_C_END 594103bfb495SBarry Smith 5942