18a729477SBarry Smith 2c6db04a5SJed Brown #include <../src/mat/impls/aij/mpi/mpiaij.h> /*I "petscmat.h" I*/ 39a6d0b0bSJed Brown #include <petsc-private/vecimpl.h> 4c6db04a5SJed Brown #include <petscblaslapack.h> 50c312b8eSJed Brown #include <petscsf.h> 68a729477SBarry Smith 701bebe75SBarry Smith /*MC 801bebe75SBarry Smith MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices. 901bebe75SBarry Smith 1001bebe75SBarry Smith This matrix type is identical to MATSEQAIJ when constructed with a single process communicator, 1101bebe75SBarry Smith and MATMPIAIJ otherwise. As a result, for single process communicators, 1201bebe75SBarry Smith MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported 1301bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 1401bebe75SBarry Smith the above preallocation routines for simplicity. 1501bebe75SBarry Smith 1601bebe75SBarry Smith Options Database Keys: 1701bebe75SBarry Smith . -mat_type aij - sets the matrix type to "aij" during a call to MatSetFromOptions() 1801bebe75SBarry Smith 199ae82921SPaul Mullowney Developer Notes: Subclasses include MATAIJCUSP, MATAIJCUSPARSE, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when 2001bebe75SBarry Smith enough exist. 2101bebe75SBarry Smith 2201bebe75SBarry Smith Level: beginner 2301bebe75SBarry Smith 2469b1f4b7SBarry Smith .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ 2501bebe75SBarry Smith M*/ 2601bebe75SBarry Smith 2701bebe75SBarry Smith /*MC 2801bebe75SBarry Smith MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices. 2901bebe75SBarry Smith 3001bebe75SBarry Smith This matrix type is identical to MATSEQAIJCRL when constructed with a single process communicator, 3101bebe75SBarry Smith and MATMPIAIJCRL otherwise. As a result, for single process communicators, 3201bebe75SBarry Smith MatSeqAIJSetPreallocation() is supported, and similarly MatMPIAIJSetPreallocation() is supported 3301bebe75SBarry Smith for communicators controlling multiple processes. It is recommended that you call both of 3401bebe75SBarry Smith the above preallocation routines for simplicity. 3501bebe75SBarry Smith 3601bebe75SBarry Smith Options Database Keys: 3701bebe75SBarry Smith . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to MatSetFromOptions() 3801bebe75SBarry Smith 3901bebe75SBarry Smith Level: beginner 4001bebe75SBarry Smith 4101bebe75SBarry Smith .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL 4201bebe75SBarry Smith M*/ 4301bebe75SBarry Smith 44dd6ea824SBarry Smith #undef __FUNCT__ 45f2c98031SJed Brown #define __FUNCT__ "MatFindNonzeroRows_MPIAIJ" 46f2c98031SJed Brown PetscErrorCode MatFindNonzeroRows_MPIAIJ(Mat M,IS *keptrows) 4727d4218bSShri Abhyankar { 4827d4218bSShri Abhyankar PetscErrorCode ierr; 4927d4218bSShri Abhyankar Mat_MPIAIJ *mat = (Mat_MPIAIJ*)M->data; 5027d4218bSShri Abhyankar Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data; 5127d4218bSShri Abhyankar Mat_SeqAIJ *b = (Mat_SeqAIJ*)mat->B->data; 5227d4218bSShri Abhyankar const PetscInt *ia,*ib; 5327d4218bSShri Abhyankar const MatScalar *aa,*bb; 5427d4218bSShri Abhyankar PetscInt na,nb,i,j,*rows,cnt=0,n0rows; 5527d4218bSShri Abhyankar PetscInt m = M->rmap->n,rstart = M->rmap->rstart; 5627d4218bSShri Abhyankar 5727d4218bSShri Abhyankar PetscFunctionBegin; 5827d4218bSShri Abhyankar *keptrows = 0; 5927d4218bSShri Abhyankar ia = a->i; 6027d4218bSShri Abhyankar ib = b->i; 6127d4218bSShri Abhyankar for (i=0; i<m; i++) { 6227d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 6327d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 6427d4218bSShri Abhyankar if (!na && !nb) { 6527d4218bSShri Abhyankar cnt++; 6627d4218bSShri Abhyankar goto ok1; 6727d4218bSShri Abhyankar } 6827d4218bSShri Abhyankar aa = a->a + ia[i]; 6927d4218bSShri Abhyankar for (j=0; j<na; j++) { 7027d4218bSShri Abhyankar if (aa[j] != 0.0) goto ok1; 7127d4218bSShri Abhyankar } 7227d4218bSShri Abhyankar bb = b->a + ib[i]; 7327d4218bSShri Abhyankar for (j=0; j <nb; j++) { 7427d4218bSShri Abhyankar if (bb[j] != 0.0) goto ok1; 7527d4218bSShri Abhyankar } 7627d4218bSShri Abhyankar cnt++; 7727d4218bSShri Abhyankar ok1:; 7827d4218bSShri Abhyankar } 79ce94432eSBarry Smith ierr = MPI_Allreduce(&cnt,&n0rows,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)M));CHKERRQ(ierr); 8027d4218bSShri Abhyankar if (!n0rows) PetscFunctionReturn(0); 81785e854fSJed Brown ierr = PetscMalloc1((M->rmap->n-cnt),&rows);CHKERRQ(ierr); 8227d4218bSShri Abhyankar cnt = 0; 8327d4218bSShri Abhyankar for (i=0; i<m; i++) { 8427d4218bSShri Abhyankar na = ia[i+1] - ia[i]; 8527d4218bSShri Abhyankar nb = ib[i+1] - ib[i]; 8627d4218bSShri Abhyankar if (!na && !nb) continue; 8727d4218bSShri Abhyankar aa = a->a + ia[i]; 8827d4218bSShri Abhyankar for (j=0; j<na;j++) { 8927d4218bSShri Abhyankar if (aa[j] != 0.0) { 9027d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9127d4218bSShri Abhyankar goto ok2; 9227d4218bSShri Abhyankar } 9327d4218bSShri Abhyankar } 9427d4218bSShri Abhyankar bb = b->a + ib[i]; 9527d4218bSShri Abhyankar for (j=0; j<nb; j++) { 9627d4218bSShri Abhyankar if (bb[j] != 0.0) { 9727d4218bSShri Abhyankar rows[cnt++] = rstart + i; 9827d4218bSShri Abhyankar goto ok2; 9927d4218bSShri Abhyankar } 10027d4218bSShri Abhyankar } 10127d4218bSShri Abhyankar ok2:; 10227d4218bSShri Abhyankar } 103ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),cnt,rows,PETSC_OWN_POINTER,keptrows);CHKERRQ(ierr); 10427d4218bSShri Abhyankar PetscFunctionReturn(0); 10527d4218bSShri Abhyankar } 10627d4218bSShri Abhyankar 10727d4218bSShri Abhyankar #undef __FUNCT__ 108f1f41ecbSJed Brown #define __FUNCT__ "MatFindZeroDiagonals_MPIAIJ" 109f1f41ecbSJed Brown PetscErrorCode MatFindZeroDiagonals_MPIAIJ(Mat M,IS *zrows) 110f1f41ecbSJed Brown { 111f1f41ecbSJed Brown Mat_MPIAIJ *aij = (Mat_MPIAIJ*)M->data; 112f1f41ecbSJed Brown PetscErrorCode ierr; 113f1f41ecbSJed Brown PetscInt i,rstart,nrows,*rows; 114f1f41ecbSJed Brown 115f1f41ecbSJed Brown PetscFunctionBegin; 1160298fd71SBarry Smith *zrows = NULL; 117f1f41ecbSJed Brown ierr = MatFindZeroDiagonals_SeqAIJ_Private(aij->A,&nrows,&rows);CHKERRQ(ierr); 1180298fd71SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,NULL);CHKERRQ(ierr); 119f1f41ecbSJed Brown for (i=0; i<nrows; i++) rows[i] += rstart; 120ce94432eSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)M),nrows,rows,PETSC_OWN_POINTER,zrows);CHKERRQ(ierr); 121f1f41ecbSJed Brown PetscFunctionReturn(0); 122f1f41ecbSJed Brown } 123f1f41ecbSJed Brown 124f1f41ecbSJed Brown #undef __FUNCT__ 1250716a85fSBarry Smith #define __FUNCT__ "MatGetColumnNorms_MPIAIJ" 1260716a85fSBarry Smith PetscErrorCode MatGetColumnNorms_MPIAIJ(Mat A,NormType type,PetscReal *norms) 1270716a85fSBarry Smith { 1280716a85fSBarry Smith PetscErrorCode ierr; 1290716a85fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)A->data; 1300716a85fSBarry Smith PetscInt i,n,*garray = aij->garray; 1310716a85fSBarry Smith Mat_SeqAIJ *a_aij = (Mat_SeqAIJ*) aij->A->data; 1320716a85fSBarry Smith Mat_SeqAIJ *b_aij = (Mat_SeqAIJ*) aij->B->data; 1330716a85fSBarry Smith PetscReal *work; 1340716a85fSBarry Smith 1350716a85fSBarry Smith PetscFunctionBegin; 1360298fd71SBarry Smith ierr = MatGetSize(A,NULL,&n);CHKERRQ(ierr); 1371795a4d1SJed Brown ierr = PetscCalloc1(n,&work);CHKERRQ(ierr); 1380716a85fSBarry Smith if (type == NORM_2) { 1390716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1400716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]*a_aij->a[i]); 1410716a85fSBarry Smith } 1420716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1430716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]*b_aij->a[i]); 1440716a85fSBarry Smith } 1450716a85fSBarry Smith } else if (type == NORM_1) { 1460716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1470716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] += PetscAbsScalar(a_aij->a[i]); 1480716a85fSBarry Smith } 1490716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1500716a85fSBarry Smith work[garray[b_aij->j[i]]] += PetscAbsScalar(b_aij->a[i]); 1510716a85fSBarry Smith } 1520716a85fSBarry Smith } else if (type == NORM_INFINITY) { 1530716a85fSBarry Smith for (i=0; i<a_aij->i[aij->A->rmap->n]; i++) { 1540716a85fSBarry Smith work[A->cmap->rstart + a_aij->j[i]] = PetscMax(PetscAbsScalar(a_aij->a[i]), work[A->cmap->rstart + a_aij->j[i]]); 1550716a85fSBarry Smith } 1560716a85fSBarry Smith for (i=0; i<b_aij->i[aij->B->rmap->n]; i++) { 1570716a85fSBarry Smith work[garray[b_aij->j[i]]] = PetscMax(PetscAbsScalar(b_aij->a[i]),work[garray[b_aij->j[i]]]); 1580716a85fSBarry Smith } 1590716a85fSBarry Smith 160ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType"); 1610716a85fSBarry Smith if (type == NORM_INFINITY) { 1627ec4195eSRémi Lacroix ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 1630716a85fSBarry Smith } else { 1647ec4195eSRémi Lacroix ierr = MPI_Allreduce(work,norms,n,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 1650716a85fSBarry Smith } 1660716a85fSBarry Smith ierr = PetscFree(work);CHKERRQ(ierr); 1670716a85fSBarry Smith if (type == NORM_2) { 1688f1a2a5eSBarry Smith for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]); 1690716a85fSBarry Smith } 1700716a85fSBarry Smith PetscFunctionReturn(0); 1710716a85fSBarry Smith } 1720716a85fSBarry Smith 1730716a85fSBarry Smith #undef __FUNCT__ 174dd6ea824SBarry Smith #define __FUNCT__ "MatDistribute_MPIAIJ" 175dd6ea824SBarry Smith /* 176dd6ea824SBarry Smith Distributes a SeqAIJ matrix across a set of processes. Code stolen from 177dd6ea824SBarry Smith MatLoad_MPIAIJ(). Horrible lack of reuse. Should be a routine for each matrix type. 178dd6ea824SBarry Smith 179dd6ea824SBarry Smith Only for square matrices 180b30237c6SBarry Smith 181b30237c6SBarry Smith Used by a preconditioner, hence PETSC_EXTERN 182dd6ea824SBarry Smith */ 1835a576424SJed Brown PETSC_EXTERN PetscErrorCode MatDistribute_MPIAIJ(MPI_Comm comm,Mat gmat,PetscInt m,MatReuse reuse,Mat *inmat) 184dd6ea824SBarry Smith { 185dd6ea824SBarry Smith PetscMPIInt rank,size; 186d892089bSMatthew G. Knepley PetscInt *rowners,*dlens,*olens,i,rstart,rend,j,jj,nz = 0,*gmataj,cnt,row,*ld,bses[2]; 187dd6ea824SBarry Smith PetscErrorCode ierr; 188dd6ea824SBarry Smith Mat mat; 189dd6ea824SBarry Smith Mat_SeqAIJ *gmata; 190dd6ea824SBarry Smith PetscMPIInt tag; 191dd6ea824SBarry Smith MPI_Status status; 192ace3abfcSBarry Smith PetscBool aij; 193dd6ea824SBarry Smith MatScalar *gmataa,*ao,*ad,*gmataarestore=0; 194dd6ea824SBarry Smith 195dd6ea824SBarry Smith PetscFunctionBegin; 196dd6ea824SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 197dd6ea824SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 198dd6ea824SBarry Smith if (!rank) { 199251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)gmat,MATSEQAIJ,&aij);CHKERRQ(ierr); 200ce94432eSBarry Smith if (!aij) SETERRQ1(PetscObjectComm((PetscObject)gmat),PETSC_ERR_SUP,"Currently no support for input matrix of type %s\n",((PetscObject)gmat)->type_name); 201dd6ea824SBarry Smith } 202dd6ea824SBarry Smith if (reuse == MAT_INITIAL_MATRIX) { 203dd6ea824SBarry Smith ierr = MatCreate(comm,&mat);CHKERRQ(ierr); 204dd6ea824SBarry Smith ierr = MatSetSizes(mat,m,m,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 205efcf75d5SBarry Smith if (!rank) { 206efcf75d5SBarry Smith bses[0] = gmat->rmap->bs; 207efcf75d5SBarry Smith bses[1] = gmat->cmap->bs; 208efcf75d5SBarry Smith } 209efcf75d5SBarry Smith ierr = MPI_Bcast(bses,2,MPIU_INT,0,comm);CHKERRQ(ierr); 210efcf75d5SBarry Smith ierr = MatSetBlockSizes(mat,bses[0],bses[1]);CHKERRQ(ierr); 211dd6ea824SBarry Smith ierr = MatSetType(mat,MATAIJ);CHKERRQ(ierr); 212785e854fSJed Brown ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr); 213dcca6d9dSJed Brown ierr = PetscMalloc2(m,&dlens,m,&olens);CHKERRQ(ierr); 214dd6ea824SBarry Smith ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 2152205254eSKarl Rupp 216dd6ea824SBarry Smith rowners[0] = 0; 2172205254eSKarl Rupp for (i=2; i<=size; i++) rowners[i] += rowners[i-1]; 218dd6ea824SBarry Smith rstart = rowners[rank]; 219dd6ea824SBarry Smith rend = rowners[rank+1]; 220dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 221dd6ea824SBarry Smith if (!rank) { 222dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 223dd6ea824SBarry Smith /* send row lengths to all processors */ 224dd6ea824SBarry Smith for (i=0; i<m; i++) dlens[i] = gmata->ilen[i]; 225dd6ea824SBarry Smith for (i=1; i<size; i++) { 226dd6ea824SBarry Smith ierr = MPI_Send(gmata->ilen + rowners[i],rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 227dd6ea824SBarry Smith } 228dd6ea824SBarry Smith /* determine number diagonal and off-diagonal counts */ 229dd6ea824SBarry Smith ierr = PetscMemzero(olens,m*sizeof(PetscInt));CHKERRQ(ierr); 2301795a4d1SJed Brown ierr = PetscCalloc1(m,&ld);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); 259dcca6d9dSJed Brown ierr = PetscMalloc2(nz,&gmataa,nz,&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); 2631795a4d1SJed Brown ierr = PetscCalloc1(m,&ld);CHKERRQ(ierr); 264dd6ea824SBarry Smith jj = 0; 265dd6ea824SBarry Smith for (i=0; i<m; i++) { 266dd6ea824SBarry Smith for (j=0; j<dlens[i]; j++) { 267dd6ea824SBarry Smith if (gmataj[jj] < rstart) ld[i]++; 268dd6ea824SBarry Smith if (gmataj[jj] < rstart || gmataj[jj] >= rend) olens[i]++; 269dd6ea824SBarry Smith jj++; 270dd6ea824SBarry Smith } 271dd6ea824SBarry Smith } 272dd6ea824SBarry Smith /* receive numerical values */ 273dd6ea824SBarry Smith ierr = PetscMemzero(gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 274dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 275dd6ea824SBarry Smith } 276dd6ea824SBarry Smith /* set preallocation */ 277dd6ea824SBarry Smith for (i=0; i<m; i++) { 278dd6ea824SBarry Smith dlens[i] -= olens[i]; 279dd6ea824SBarry Smith } 280dd6ea824SBarry Smith ierr = MatSeqAIJSetPreallocation(mat,0,dlens);CHKERRQ(ierr); 281dd6ea824SBarry Smith ierr = MatMPIAIJSetPreallocation(mat,0,dlens,0,olens);CHKERRQ(ierr); 282dd6ea824SBarry Smith 283dd6ea824SBarry Smith for (i=0; i<m; i++) { 284dd6ea824SBarry Smith dlens[i] += olens[i]; 285dd6ea824SBarry Smith } 286dd6ea824SBarry Smith cnt = 0; 287dd6ea824SBarry Smith for (i=0; i<m; i++) { 288dd6ea824SBarry Smith row = rstart + i; 289dd6ea824SBarry Smith ierr = MatSetValues(mat,1,&row,dlens[i],gmataj+cnt,gmataa+cnt,INSERT_VALUES);CHKERRQ(ierr); 290dd6ea824SBarry Smith cnt += dlens[i]; 291dd6ea824SBarry Smith } 292dd6ea824SBarry Smith if (rank) { 293dd6ea824SBarry Smith ierr = PetscFree2(gmataa,gmataj);CHKERRQ(ierr); 294dd6ea824SBarry Smith } 295dd6ea824SBarry Smith ierr = PetscFree2(dlens,olens);CHKERRQ(ierr); 296dd6ea824SBarry Smith ierr = PetscFree(rowners);CHKERRQ(ierr); 2972205254eSKarl Rupp 298dd6ea824SBarry Smith ((Mat_MPIAIJ*)(mat->data))->ld = ld; 2992205254eSKarl Rupp 300dd6ea824SBarry Smith *inmat = mat; 301dd6ea824SBarry Smith } else { /* column indices are already set; only need to move over numerical values from process 0 */ 302dd6ea824SBarry Smith Mat_SeqAIJ *Ad = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->A->data; 303dd6ea824SBarry Smith Mat_SeqAIJ *Ao = (Mat_SeqAIJ*)((Mat_MPIAIJ*)((*inmat)->data))->B->data; 304dd6ea824SBarry Smith mat = *inmat; 305dd6ea824SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mat,&tag);CHKERRQ(ierr); 306dd6ea824SBarry Smith if (!rank) { 307dd6ea824SBarry Smith /* send numerical values to other processes */ 308dd6ea824SBarry Smith gmata = (Mat_SeqAIJ*) gmat->data; 309dd6ea824SBarry Smith ierr = MatGetOwnershipRanges(mat,(const PetscInt**)&rowners);CHKERRQ(ierr); 310dd6ea824SBarry Smith gmataa = gmata->a; 311dd6ea824SBarry Smith for (i=1; i<size; i++) { 312dd6ea824SBarry Smith nz = gmata->i[rowners[i+1]]-gmata->i[rowners[i]]; 313dd6ea824SBarry Smith ierr = MPI_Send(gmataa + gmata->i[rowners[i]],nz,MPIU_SCALAR,i,tag,comm);CHKERRQ(ierr); 314dd6ea824SBarry Smith } 315dd6ea824SBarry Smith nz = gmata->i[rowners[1]]-gmata->i[rowners[0]]; 316dd6ea824SBarry Smith } else { 317dd6ea824SBarry Smith /* receive numerical values from process 0*/ 318dd6ea824SBarry Smith nz = Ad->nz + Ao->nz; 319785e854fSJed Brown ierr = PetscMalloc1(nz,&gmataa);CHKERRQ(ierr); gmataarestore = gmataa; 320dd6ea824SBarry Smith ierr = MPI_Recv(gmataa,nz,MPIU_SCALAR,0,tag,comm,&status);CHKERRQ(ierr); 321dd6ea824SBarry Smith } 322dd6ea824SBarry Smith /* transfer numerical values into the diagonal A and off diagonal B parts of mat */ 323dd6ea824SBarry Smith ld = ((Mat_MPIAIJ*)(mat->data))->ld; 324dd6ea824SBarry Smith ad = Ad->a; 325dd6ea824SBarry Smith ao = Ao->a; 326d0f46423SBarry Smith if (mat->rmap->n) { 327dd6ea824SBarry Smith i = 0; 328dd6ea824SBarry Smith nz = ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ao += nz; gmataa += nz; 329dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 330dd6ea824SBarry Smith } 331d0f46423SBarry Smith for (i=1; i<mat->rmap->n; i++) { 332dd6ea824SBarry 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; 333dd6ea824SBarry Smith nz = Ad->i[i+1] - Ad->i[i]; ierr = PetscMemcpy(ad,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); ad += nz; gmataa += nz; 334dd6ea824SBarry Smith } 335dd6ea824SBarry Smith i--; 336d0f46423SBarry Smith if (mat->rmap->n) { 33722d28d08SBarry Smith nz = Ao->i[i+1] - Ao->i[i] - ld[i]; ierr = PetscMemcpy(ao,gmataa,nz*sizeof(PetscScalar));CHKERRQ(ierr); 338dd6ea824SBarry Smith } 339dd6ea824SBarry Smith if (rank) { 340dd6ea824SBarry Smith ierr = PetscFree(gmataarestore);CHKERRQ(ierr); 341dd6ea824SBarry Smith } 342dd6ea824SBarry Smith } 343dd6ea824SBarry Smith ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 344dd6ea824SBarry Smith ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 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 3711795a4d1SJed Brown ierr = PetscCalloc1((mat->cmap->N+1),&aij->colmap);CHKERRQ(ierr); 3721795a4d1SJed Brown ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N+1)*sizeof(PetscInt));CHKERRQ(ierr); 373905e6a2fSBarry Smith for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1; 374b1fc9764SSatish Balay #endif 3753a40ed3dSBarry Smith PetscFunctionReturn(0); 3769e25ed09SBarry Smith } 3779e25ed09SBarry Smith 37830770e4dSSatish Balay #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \ 3790520107fSSatish Balay { \ 380db4deed7SKarl Rupp if (col <= lastcol1) low1 = 0; \ 381db4deed7SKarl Rupp 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 { \ 415db4deed7SKarl Rupp if (col <= lastcol2) low2 = 0; \ 416db4deed7SKarl Rupp else high2 = nrow2; \ 417fd3458f5SBarry Smith lastcol2 = col; \ 418fd3458f5SBarry Smith while (high2-low2 > 5) { \ 419fd3458f5SBarry Smith t = (low2+high2)/2; \ 420fd3458f5SBarry Smith if (rp2[t] > col) high2 = t; \ 421fd3458f5SBarry Smith else low2 = t; \ 422ba4e3ef2SSatish Balay } \ 423fd3458f5SBarry Smith for (_i=low2; _i<high2; _i++) { \ 424fd3458f5SBarry Smith if (rp2[_i] > col) break; \ 425fd3458f5SBarry Smith if (rp2[_i] == col) { \ 426fd3458f5SBarry Smith if (addv == ADD_VALUES) ap2[_i] += value; \ 427fd3458f5SBarry Smith else ap2[_i] = value; \ 42830770e4dSSatish Balay goto b_noinsert; \ 42930770e4dSSatish Balay } \ 43030770e4dSSatish Balay } \ 431e44c0bd4SBarry Smith if (value == 0.0 && ignorezeroentries) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 432e44c0bd4SBarry Smith if (nonew == 1) {low2 = 0; high2 = nrow2; goto b_noinsert;} \ 433e32f2f54SBarry Smith if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \ 434fef13f97SBarry Smith MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew,MatScalar); \ 435669a8dbcSSatish Balay N = nrow2++ - 1; b->nz++; high2++; \ 43630770e4dSSatish Balay /* shift up all the later entries in this row */ \ 43730770e4dSSatish Balay for (ii=N; ii>=_i; ii--) { \ 438fd3458f5SBarry Smith rp2[ii+1] = rp2[ii]; \ 439fd3458f5SBarry Smith ap2[ii+1] = ap2[ii]; \ 44030770e4dSSatish Balay } \ 441fd3458f5SBarry Smith rp2[_i] = col; \ 442fd3458f5SBarry Smith ap2[_i] = value; \ 44330770e4dSSatish Balay b_noinsert: ; \ 444fd3458f5SBarry Smith bilen[row] = nrow2; \ 44530770e4dSSatish Balay } 44630770e4dSSatish Balay 4474a2ae208SSatish Balay #undef __FUNCT__ 4482fd7e33dSBarry Smith #define __FUNCT__ "MatSetValuesRow_MPIAIJ" 4492fd7e33dSBarry Smith PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[]) 4502fd7e33dSBarry Smith { 4512fd7e33dSBarry Smith Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data; 4522fd7e33dSBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data; 4532fd7e33dSBarry Smith PetscErrorCode ierr; 4542fd7e33dSBarry Smith PetscInt l,*garray = mat->garray,diag; 4552fd7e33dSBarry Smith 4562fd7e33dSBarry Smith PetscFunctionBegin; 4572fd7e33dSBarry Smith /* code only works for square matrices A */ 4582fd7e33dSBarry Smith 4592fd7e33dSBarry Smith /* find size of row to the left of the diagonal part */ 4602fd7e33dSBarry Smith ierr = MatGetOwnershipRange(A,&diag,0);CHKERRQ(ierr); 4612fd7e33dSBarry Smith row = row - diag; 4622fd7e33dSBarry Smith for (l=0; l<b->i[row+1]-b->i[row]; l++) { 4632fd7e33dSBarry Smith if (garray[b->j[b->i[row]+l]] > diag) break; 4642fd7e33dSBarry Smith } 4652fd7e33dSBarry Smith ierr = PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));CHKERRQ(ierr); 4662fd7e33dSBarry Smith 4672fd7e33dSBarry Smith /* diagonal part */ 4682fd7e33dSBarry Smith ierr = PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));CHKERRQ(ierr); 4692fd7e33dSBarry Smith 4702fd7e33dSBarry Smith /* right of diagonal part */ 4712fd7e33dSBarry 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); 4722fd7e33dSBarry Smith PetscFunctionReturn(0); 4732fd7e33dSBarry Smith } 4742fd7e33dSBarry Smith 4752fd7e33dSBarry Smith #undef __FUNCT__ 4764a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_MPIAIJ" 477b1d57f15SBarry Smith PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv) 4788a729477SBarry Smith { 47944a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 48087828ca2SBarry Smith PetscScalar value; 481dfbe8321SBarry Smith PetscErrorCode ierr; 482d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 483d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 484ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 4858a729477SBarry Smith 4860520107fSSatish Balay /* Some Variables required in the macro */ 4874ee7247eSSatish Balay Mat A = aij->A; 4884ee7247eSSatish Balay Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 48957809a77SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 490a77337e4SBarry Smith MatScalar *aa = a->a; 491ace3abfcSBarry Smith PetscBool ignorezeroentries = a->ignorezeroentries; 49230770e4dSSatish Balay Mat B = aij->B; 49330770e4dSSatish Balay Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 494d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 495a77337e4SBarry Smith MatScalar *ba = b->a; 49630770e4dSSatish Balay 497fd3458f5SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 4988d76821aSHong Zhang PetscInt nonew; 499a77337e4SBarry Smith MatScalar *ap1,*ap2; 5004ee7247eSSatish Balay 5013a40ed3dSBarry Smith PetscFunctionBegin; 50271fd2e92SBarry Smith if (v) PetscValidScalarPointer(v,6); 5038a729477SBarry Smith for (i=0; i<m; i++) { 5045ef9f2a5SBarry Smith if (im[i] < 0) continue; 5052515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 506e32f2f54SBarry 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); 5070a198c4cSBarry Smith #endif 5084b0e389bSBarry Smith if (im[i] >= rstart && im[i] < rend) { 5094b0e389bSBarry Smith row = im[i] - rstart; 510fd3458f5SBarry Smith lastcol1 = -1; 511fd3458f5SBarry Smith rp1 = aj + ai[row]; 512fd3458f5SBarry Smith ap1 = aa + ai[row]; 513fd3458f5SBarry Smith rmax1 = aimax[row]; 514fd3458f5SBarry Smith nrow1 = ailen[row]; 515fd3458f5SBarry Smith low1 = 0; 516fd3458f5SBarry Smith high1 = nrow1; 517fd3458f5SBarry Smith lastcol2 = -1; 518fd3458f5SBarry Smith rp2 = bj + bi[row]; 519d498b1e9SBarry Smith ap2 = ba + bi[row]; 520fd3458f5SBarry Smith rmax2 = bimax[row]; 521d498b1e9SBarry Smith nrow2 = bilen[row]; 522fd3458f5SBarry Smith low2 = 0; 523fd3458f5SBarry Smith high2 = nrow2; 524fd3458f5SBarry Smith 5251eb62cbbSBarry Smith for (j=0; j<n; j++) { 526db4deed7SKarl Rupp if (v) { 527db4deed7SKarl Rupp if (roworiented) value = v[i*n+j]; 528db4deed7SKarl Rupp else value = v[i+j*m]; 529db4deed7SKarl Rupp } else value = 0.0; 530abc0a331SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 531fd3458f5SBarry Smith if (in[j] >= cstart && in[j] < cend) { 532fd3458f5SBarry Smith col = in[j] - cstart; 5338d76821aSHong Zhang nonew = a->nonew; 53430770e4dSSatish Balay MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 535273d9f13SBarry Smith } else if (in[j] < 0) continue; 5362515c552SBarry Smith #if defined(PETSC_USE_DEBUG) 537cb9801acSJed 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); 5380a198c4cSBarry Smith #endif 5391eb62cbbSBarry Smith else { 540227d817aSBarry Smith if (mat->was_assembled) { 541905e6a2fSBarry Smith if (!aij->colmap) { 542ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 543905e6a2fSBarry Smith } 544aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 5450f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 546fa46199cSSatish Balay col--; 547b1fc9764SSatish Balay #else 548905e6a2fSBarry Smith col = aij->colmap[in[j]] - 1; 549b1fc9764SSatish Balay #endif 5500e9bae81SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->B->data))->nonew) { 551ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 5524b0e389bSBarry Smith col = in[j]; 5539bf004c3SSatish Balay /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 554f9508a3cSSatish Balay B = aij->B; 555f9508a3cSSatish Balay b = (Mat_SeqAIJ*)B->data; 556e44c0bd4SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; ba = b->a; 557d498b1e9SBarry Smith rp2 = bj + bi[row]; 558d498b1e9SBarry Smith ap2 = ba + bi[row]; 559d498b1e9SBarry Smith rmax2 = bimax[row]; 560d498b1e9SBarry Smith nrow2 = bilen[row]; 561d498b1e9SBarry Smith low2 = 0; 562d498b1e9SBarry Smith high2 = nrow2; 563d0f46423SBarry Smith bm = aij->B->rmap->n; 564f9508a3cSSatish Balay ba = b->a; 5650e9bae81SBarry 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]); 566c48de900SBarry Smith } else col = in[j]; 5678d76821aSHong Zhang nonew = b->nonew; 56830770e4dSSatish Balay MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 5691eb62cbbSBarry Smith } 5701eb62cbbSBarry Smith } 5715ef9f2a5SBarry Smith } else { 5724cb17eb5SBarry 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]); 57390f02eecSBarry Smith if (!aij->donotstash) { 5745080c13bSMatthew G Knepley mat->assembled = PETSC_FALSE; 575d36fbae8SSatish Balay if (roworiented) { 576ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 577d36fbae8SSatish Balay } else { 578ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 5794b0e389bSBarry Smith } 5801eb62cbbSBarry Smith } 5818a729477SBarry Smith } 58290f02eecSBarry Smith } 5833a40ed3dSBarry Smith PetscFunctionReturn(0); 5848a729477SBarry Smith } 5858a729477SBarry Smith 5864a2ae208SSatish Balay #undef __FUNCT__ 5874a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_MPIAIJ" 588b1d57f15SBarry Smith PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[]) 589b49de8d1SLois Curfman McInnes { 590b49de8d1SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 591dfbe8321SBarry Smith PetscErrorCode ierr; 592d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 593d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 594b49de8d1SLois Curfman McInnes 5953a40ed3dSBarry Smith PetscFunctionBegin; 596b49de8d1SLois Curfman McInnes for (i=0; i<m; i++) { 597e32f2f54SBarry Smith if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/ 598e32f2f54SBarry 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); 599b49de8d1SLois Curfman McInnes if (idxm[i] >= rstart && idxm[i] < rend) { 600b49de8d1SLois Curfman McInnes row = idxm[i] - rstart; 601b49de8d1SLois Curfman McInnes for (j=0; j<n; j++) { 602e32f2f54SBarry Smith if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */ 603e32f2f54SBarry 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); 604b49de8d1SLois Curfman McInnes if (idxn[j] >= cstart && idxn[j] < cend) { 605b49de8d1SLois Curfman McInnes col = idxn[j] - cstart; 606b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 607fa852ad4SSatish Balay } else { 608905e6a2fSBarry Smith if (!aij->colmap) { 609ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 610905e6a2fSBarry Smith } 611aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 6120f5bd95cSBarry Smith ierr = PetscTableFind(aij->colmap,idxn[j]+1,&col);CHKERRQ(ierr); 613fa46199cSSatish Balay col--; 614b1fc9764SSatish Balay #else 615905e6a2fSBarry Smith col = aij->colmap[idxn[j]] - 1; 616b1fc9764SSatish Balay #endif 617e60e1c95SSatish Balay if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0; 618d9d09a02SSatish Balay else { 619b49de8d1SLois Curfman McInnes ierr = MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);CHKERRQ(ierr); 620b49de8d1SLois Curfman McInnes } 621b49de8d1SLois Curfman McInnes } 622b49de8d1SLois Curfman McInnes } 623f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported"); 624b49de8d1SLois Curfman McInnes } 6253a40ed3dSBarry Smith PetscFunctionReturn(0); 626b49de8d1SLois Curfman McInnes } 627bc5ccf88SSatish Balay 628bd0c2dcbSBarry Smith extern PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat,Vec,Vec); 629bd0c2dcbSBarry Smith 6304a2ae208SSatish Balay #undef __FUNCT__ 6314a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyBegin_MPIAIJ" 632dfbe8321SBarry Smith PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode) 633bc5ccf88SSatish Balay { 634bc5ccf88SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 635dfbe8321SBarry Smith PetscErrorCode ierr; 636b1d57f15SBarry Smith PetscInt nstash,reallocs; 637bc5ccf88SSatish Balay InsertMode addv; 638bc5ccf88SSatish Balay 639bc5ccf88SSatish Balay PetscFunctionBegin; 6402205254eSKarl Rupp if (aij->donotstash || mat->nooffprocentries) PetscFunctionReturn(0); 641bc5ccf88SSatish Balay 642bc5ccf88SSatish Balay /* make sure all processors are either in INSERTMODE or ADDMODE */ 643ce94432eSBarry Smith ierr = MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 644ce94432eSBarry Smith if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),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 */ 6686e111a19SKarl Rupp 669bc5ccf88SSatish Balay PetscFunctionBegin; 6704cb17eb5SBarry Smith if (!aij->donotstash && !mat->nooffprocentries) { 671a2d1c673SSatish Balay while (1) { 6728798bf22SSatish Balay ierr = MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);CHKERRQ(ierr); 673a2d1c673SSatish Balay if (!flg) break; 674a2d1c673SSatish Balay 675bc5ccf88SSatish Balay for (i=0; i<n; ) { 676bc5ccf88SSatish Balay /* Now identify the consecutive vals belonging to the same row */ 6772205254eSKarl Rupp for (j=i,rstart=row[j]; j<n; j++) { 6782205254eSKarl Rupp if (row[j] != rstart) break; 6792205254eSKarl Rupp } 680bc5ccf88SSatish Balay if (j < n) ncols = j-i; 681bc5ccf88SSatish Balay else ncols = n-i; 682bc5ccf88SSatish Balay /* Now assemble all these values with a single function call */ 683bc5ccf88SSatish Balay ierr = MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);CHKERRQ(ierr); 6842205254eSKarl Rupp 685bc5ccf88SSatish Balay i = j; 686bc5ccf88SSatish Balay } 687bc5ccf88SSatish Balay } 6888798bf22SSatish Balay ierr = MatStashScatterEnd_Private(&mat->stash);CHKERRQ(ierr); 689bc5ccf88SSatish Balay } 690bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->A,mode);CHKERRQ(ierr); 691bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->A,mode);CHKERRQ(ierr); 692bc5ccf88SSatish Balay 693bc5ccf88SSatish Balay /* determine if any processor has disassembled, if so we must 694bc5ccf88SSatish Balay also disassemble ourselfs, in order that we may reassemble. */ 695bc5ccf88SSatish Balay /* 696bc5ccf88SSatish Balay if nonzero structure of submatrix B cannot change then we know that 697bc5ccf88SSatish Balay no processor disassembled thus we can skip this stuff 698bc5ccf88SSatish Balay */ 699bc5ccf88SSatish Balay if (!((Mat_SeqAIJ*)aij->B->data)->nonew) { 700ce94432eSBarry Smith ierr = MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 701bc5ccf88SSatish Balay if (mat->was_assembled && !other_disassembled) { 702ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 703ad59fb31SSatish Balay } 704ad59fb31SSatish Balay } 705bc5ccf88SSatish Balay if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) { 706bc5ccf88SSatish Balay ierr = MatSetUpMultiply_MPIAIJ(mat);CHKERRQ(ierr); 707bc5ccf88SSatish Balay } 7084e0d8c25SBarry Smith ierr = MatSetOption(aij->B,MAT_USE_INODES,PETSC_FALSE);CHKERRQ(ierr); 709bc5ccf88SSatish Balay ierr = MatAssemblyBegin(aij->B,mode);CHKERRQ(ierr); 710bc5ccf88SSatish Balay ierr = MatAssemblyEnd(aij->B,mode);CHKERRQ(ierr); 711bc5ccf88SSatish Balay 7121d79065fSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 7132205254eSKarl Rupp 714606d414cSSatish Balay aij->rowvalues = 0; 715a30b2313SHong Zhang 716a30b2313SHong Zhang /* used by MatAXPY() */ 71791c97fd4SSatish Balay a->xtoy = 0; ((Mat_SeqAIJ*)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */ 71891c97fd4SSatish Balay a->XtoY = 0; ((Mat_SeqAIJ*)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */ 719a30b2313SHong Zhang 7206bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 721bd0c2dcbSBarry Smith if (a->inode.size) mat->ops->multdiagonalblock = MatMultDiagonalBlock_MPIAIJ; 722bc5ccf88SSatish Balay PetscFunctionReturn(0); 723bc5ccf88SSatish Balay } 724bc5ccf88SSatish Balay 7254a2ae208SSatish Balay #undef __FUNCT__ 7264a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_MPIAIJ" 727dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_MPIAIJ(Mat A) 7281eb62cbbSBarry Smith { 72944a69424SLois Curfman McInnes Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 730dfbe8321SBarry Smith PetscErrorCode ierr; 7313a40ed3dSBarry Smith 7323a40ed3dSBarry Smith PetscFunctionBegin; 73378b31e54SBarry Smith ierr = MatZeroEntries(l->A);CHKERRQ(ierr); 73478b31e54SBarry Smith ierr = MatZeroEntries(l->B);CHKERRQ(ierr); 7353a40ed3dSBarry Smith PetscFunctionReturn(0); 7361eb62cbbSBarry Smith } 7371eb62cbbSBarry Smith 7384a2ae208SSatish Balay #undef __FUNCT__ 7394a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_MPIAIJ" 7402b40b63fSBarry Smith PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 7411eb62cbbSBarry Smith { 7421b1dd7adSMatthew G. Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ *) A->data; 7431b1dd7adSMatthew G. Knepley PetscInt *owners = A->rmap->range; 7441b1dd7adSMatthew G. Knepley PetscInt n = A->rmap->n; 7451b1dd7adSMatthew G. Knepley PetscMPIInt size = mat->size; 7461b1dd7adSMatthew G. Knepley PetscSF sf; 7471b1dd7adSMatthew G. Knepley PetscInt *lrows; 7481b1dd7adSMatthew G. Knepley PetscSFNode *rrows; 7491b1dd7adSMatthew G. Knepley PetscInt lastidx = -1, r, p = 0, len = 0; 7506849ba73SBarry Smith PetscErrorCode ierr; 7511eb62cbbSBarry Smith 7523a40ed3dSBarry Smith PetscFunctionBegin; 7531b1dd7adSMatthew G. Knepley /* Create SF where leaves are input rows and roots are owned rows */ 754785e854fSJed Brown ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr); 7559d80f4afSMatthew G. Knepley for (r = 0; r < n; ++r) lrows[r] = -1; 756785e854fSJed Brown ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr); 7571b1dd7adSMatthew G. Knepley for (r = 0; r < N; ++r) { 7581b1dd7adSMatthew G. Knepley const PetscInt idx = rows[r]; 7591b1dd7adSMatthew G. Knepley PetscBool found = PETSC_FALSE; 7601b1dd7adSMatthew G. Knepley /* Trick for efficient searching for sorted rows */ 7611b1dd7adSMatthew G. Knepley if (lastidx > idx) p = 0; 7626543fbbaSBarry Smith lastidx = idx; 7631b1dd7adSMatthew G. Knepley for (; p < size; ++p) { 7641b1dd7adSMatthew G. Knepley if (idx >= owners[p] && idx < owners[p+1]) { 7651b1dd7adSMatthew G. Knepley rrows[r].rank = p; 7661b1dd7adSMatthew G. Knepley rrows[r].index = rows[r] - owners[p]; 7676543fbbaSBarry Smith found = PETSC_TRUE; 7686543fbbaSBarry Smith break; 7691eb62cbbSBarry Smith } 7701eb62cbbSBarry Smith } 7711b1dd7adSMatthew G. Knepley if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx); 7721eb62cbbSBarry Smith } 7731b1dd7adSMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr); 7741b1dd7adSMatthew G. Knepley ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr); 7751b1dd7adSMatthew G. Knepley /* Collect flags for rows to be zeroed */ 77658c26cb0SMatthew G. Knepley ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 77758c26cb0SMatthew G. Knepley ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 7781b1dd7adSMatthew G. Knepley ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 7791b1dd7adSMatthew G. Knepley /* Compress and put in row numbers */ 7809d80f4afSMatthew G. Knepley for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r; 78197b48c8fSBarry Smith /* fix right hand side if needed */ 78297b48c8fSBarry Smith if (x && b) { 7831b1dd7adSMatthew G. Knepley const PetscScalar *xx; 7841b1dd7adSMatthew G. Knepley PetscScalar *bb; 7851b1dd7adSMatthew G. Knepley 78697b48c8fSBarry Smith ierr = VecGetArrayRead(x, &xx);CHKERRQ(ierr); 78797b48c8fSBarry Smith ierr = VecGetArray(b, &bb);CHKERRQ(ierr); 7881b1dd7adSMatthew G. Knepley for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]]; 78997b48c8fSBarry Smith ierr = VecRestoreArrayRead(x, &xx);CHKERRQ(ierr); 79097b48c8fSBarry Smith ierr = VecRestoreArray(b, &bb);CHKERRQ(ierr); 79197b48c8fSBarry Smith } 7921b1dd7adSMatthew G. Knepley /* Must zero l->B before l->A because the (diag) case below may put values into l->B*/ 7931b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->B, len, lrows, 0.0, 0,0);CHKERRQ(ierr); 7941b1dd7adSMatthew G. Knepley if ((diag != 0.0) && (mat->A->rmap->N == mat->A->cmap->N)) { 7951b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, diag, NULL, NULL);CHKERRQ(ierr); 796f4df32b1SMatthew Knepley } else if (diag != 0.0) { 7971b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr); 7981b1dd7adSMatthew G. Knepley if (((Mat_SeqAIJ *) mat->A->data)->nonew) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatZeroRows() on rectangular matrices cannot be used with the Mat options\nMAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR"); 7991b1dd7adSMatthew G. Knepley for (r = 0; r < len; ++r) { 8001b1dd7adSMatthew G. Knepley const PetscInt row = lrows[r] + A->rmap->rstart; 801f4df32b1SMatthew Knepley ierr = MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES);CHKERRQ(ierr); 802e2d53e46SBarry Smith } 803e2d53e46SBarry Smith ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 804e2d53e46SBarry Smith ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 8056eb55b6aSBarry Smith } else { 8061b1dd7adSMatthew G. Knepley ierr = MatZeroRows(mat->A, len, lrows, 0.0, NULL, NULL);CHKERRQ(ierr); 8076eb55b6aSBarry Smith } 808606d414cSSatish Balay ierr = PetscFree(lrows);CHKERRQ(ierr); 8093a40ed3dSBarry Smith PetscFunctionReturn(0); 8101eb62cbbSBarry Smith } 8111eb62cbbSBarry Smith 8124a2ae208SSatish Balay #undef __FUNCT__ 8139c7c4993SBarry Smith #define __FUNCT__ "MatZeroRowsColumns_MPIAIJ" 8149c7c4993SBarry Smith PetscErrorCode MatZeroRowsColumns_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b) 8159c7c4993SBarry Smith { 8169c7c4993SBarry Smith Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data; 8179c7c4993SBarry Smith PetscErrorCode ierr; 81854bd4135SMatthew G. Knepley PetscMPIInt size = l->size,n = A->rmap->n,lastidx = -1; 81978fab17bSMatthew G. Knepley PetscInt i,j,r,m,p = 0,len = 0; 82054bd4135SMatthew G. Knepley PetscInt *lrows,*owners = A->rmap->range; 82154bd4135SMatthew G. Knepley PetscSFNode *rrows; 82254bd4135SMatthew G. Knepley PetscSF sf; 8239c7c4993SBarry Smith const PetscScalar *xx; 824564f14d6SBarry Smith PetscScalar *bb,*mask; 825564f14d6SBarry Smith Vec xmask,lmask; 826564f14d6SBarry Smith Mat_SeqAIJ *aij = (Mat_SeqAIJ*)l->B->data; 827564f14d6SBarry Smith const PetscInt *aj, *ii,*ridx; 828564f14d6SBarry Smith PetscScalar *aa; 8299c7c4993SBarry Smith #if defined(PETSC_DEBUG) 8309c7c4993SBarry Smith PetscBool found = PETSC_FALSE; 8319c7c4993SBarry Smith #endif 8329c7c4993SBarry Smith 8339c7c4993SBarry Smith PetscFunctionBegin; 83454bd4135SMatthew G. Knepley /* Create SF where leaves are input rows and roots are owned rows */ 83554bd4135SMatthew G. Knepley ierr = PetscMalloc1(n, &lrows);CHKERRQ(ierr); 83654bd4135SMatthew G. Knepley for (r = 0; r < n; ++r) lrows[r] = -1; 83754bd4135SMatthew G. Knepley ierr = PetscMalloc1(N, &rrows);CHKERRQ(ierr); 83854bd4135SMatthew G. Knepley for (r = 0; r < N; ++r) { 83954bd4135SMatthew G. Knepley const PetscInt idx = rows[r]; 84054bd4135SMatthew G. Knepley PetscBool found = PETSC_FALSE; 84154bd4135SMatthew G. Knepley /* Trick for efficient searching for sorted rows */ 84254bd4135SMatthew G. Knepley if (lastidx > idx) p = 0; 8439c7c4993SBarry Smith lastidx = idx; 84454bd4135SMatthew G. Knepley for (; p < size; ++p) { 84554bd4135SMatthew G. Knepley if (idx >= owners[p] && idx < owners[p+1]) { 84654bd4135SMatthew G. Knepley rrows[r].rank = p; 84754bd4135SMatthew G. Knepley rrows[r].index = rows[r] - owners[p]; 8489c7c4993SBarry Smith found = PETSC_TRUE; 8499c7c4993SBarry Smith break; 8509c7c4993SBarry Smith } 8519c7c4993SBarry Smith } 85254bd4135SMatthew G. Knepley if (!found) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %d not found in matrix distribution", idx); 8539c7c4993SBarry Smith } 85454bd4135SMatthew G. Knepley ierr = PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);CHKERRQ(ierr); 85554bd4135SMatthew G. Knepley ierr = PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);CHKERRQ(ierr); 85654bd4135SMatthew G. Knepley /* Collect flags for rows to be zeroed */ 85754bd4135SMatthew G. Knepley ierr = PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 85854bd4135SMatthew G. Knepley ierr = PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);CHKERRQ(ierr); 85954bd4135SMatthew G. Knepley ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 86054bd4135SMatthew G. Knepley /* Compress and put in row numbers */ 86154bd4135SMatthew G. Knepley for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r; 862564f14d6SBarry Smith /* zero diagonal part of matrix */ 86354bd4135SMatthew G. Knepley ierr = MatZeroRowsColumns(l->A,len,lrows,diag,x,b);CHKERRQ(ierr); 864564f14d6SBarry Smith /* handle off diagonal part of matrix */ 8650298fd71SBarry Smith ierr = MatGetVecs(A,&xmask,NULL);CHKERRQ(ierr); 866564f14d6SBarry Smith ierr = VecDuplicate(l->lvec,&lmask);CHKERRQ(ierr); 867564f14d6SBarry Smith ierr = VecGetArray(xmask,&bb);CHKERRQ(ierr); 86854bd4135SMatthew G. Knepley for (i=0; i<len; i++) bb[lrows[i]] = 1; 869564f14d6SBarry Smith ierr = VecRestoreArray(xmask,&bb);CHKERRQ(ierr); 870564f14d6SBarry Smith ierr = VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 871564f14d6SBarry Smith ierr = VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 8726bf464f9SBarry Smith ierr = VecDestroy(&xmask);CHKERRQ(ierr); 873377aa5a1SBarry Smith if (x) { 87467caceb0SMatthew G. Knepley ierr = VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 87567caceb0SMatthew G. Knepley ierr = VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 876564f14d6SBarry Smith ierr = VecGetArrayRead(l->lvec,&xx);CHKERRQ(ierr); 877564f14d6SBarry Smith ierr = VecGetArray(b,&bb);CHKERRQ(ierr); 878377aa5a1SBarry Smith } 879377aa5a1SBarry Smith ierr = VecGetArray(lmask,&mask);CHKERRQ(ierr); 880564f14d6SBarry Smith /* remove zeroed rows of off diagonal matrix */ 881564f14d6SBarry Smith ii = aij->i; 88254bd4135SMatthew G. Knepley for (i=0; i<len; i++) { 883564f14d6SBarry Smith ierr = PetscMemzero(aij->a + ii[lrows[i]],(ii[lrows[i]+1] - ii[lrows[i]])*sizeof(PetscScalar));CHKERRQ(ierr); 8849c7c4993SBarry Smith } 885564f14d6SBarry Smith /* loop over all elements of off process part of matrix zeroing removed columns*/ 886564f14d6SBarry Smith if (aij->compressedrow.use) { 887564f14d6SBarry Smith m = aij->compressedrow.nrows; 888564f14d6SBarry Smith ii = aij->compressedrow.i; 889564f14d6SBarry Smith ridx = aij->compressedrow.rindex; 890564f14d6SBarry Smith for (i=0; i<m; i++) { 891564f14d6SBarry Smith n = ii[i+1] - ii[i]; 892564f14d6SBarry Smith aj = aij->j + ii[i]; 893564f14d6SBarry Smith aa = aij->a + ii[i]; 894564f14d6SBarry Smith 895564f14d6SBarry Smith for (j=0; j<n; j++) { 89625266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 897377aa5a1SBarry Smith if (b) bb[*ridx] -= *aa*xx[*aj]; 898564f14d6SBarry Smith *aa = 0.0; 899564f14d6SBarry Smith } 900564f14d6SBarry Smith aa++; 901564f14d6SBarry Smith aj++; 902564f14d6SBarry Smith } 903564f14d6SBarry Smith ridx++; 904564f14d6SBarry Smith } 905564f14d6SBarry Smith } else { /* do not use compressed row format */ 906564f14d6SBarry Smith m = l->B->rmap->n; 907564f14d6SBarry Smith for (i=0; i<m; i++) { 908564f14d6SBarry Smith n = ii[i+1] - ii[i]; 909564f14d6SBarry Smith aj = aij->j + ii[i]; 910564f14d6SBarry Smith aa = aij->a + ii[i]; 911564f14d6SBarry Smith for (j=0; j<n; j++) { 91225266a92SSatish Balay if (PetscAbsScalar(mask[*aj])) { 913377aa5a1SBarry Smith if (b) bb[i] -= *aa*xx[*aj]; 914564f14d6SBarry Smith *aa = 0.0; 915564f14d6SBarry Smith } 916564f14d6SBarry Smith aa++; 917564f14d6SBarry Smith aj++; 918564f14d6SBarry Smith } 919564f14d6SBarry Smith } 920564f14d6SBarry Smith } 921377aa5a1SBarry Smith if (x) { 922564f14d6SBarry Smith ierr = VecRestoreArray(b,&bb);CHKERRQ(ierr); 923564f14d6SBarry Smith ierr = VecRestoreArrayRead(l->lvec,&xx);CHKERRQ(ierr); 924377aa5a1SBarry Smith } 925377aa5a1SBarry Smith ierr = VecRestoreArray(lmask,&mask);CHKERRQ(ierr); 9266bf464f9SBarry Smith ierr = VecDestroy(&lmask);CHKERRQ(ierr); 9279c7c4993SBarry Smith ierr = PetscFree(lrows);CHKERRQ(ierr); 9289c7c4993SBarry Smith PetscFunctionReturn(0); 9299c7c4993SBarry Smith } 9309c7c4993SBarry Smith 9319c7c4993SBarry Smith #undef __FUNCT__ 9324a2ae208SSatish Balay #define __FUNCT__ "MatMult_MPIAIJ" 933dfbe8321SBarry Smith PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy) 9341eb62cbbSBarry Smith { 935416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 936dfbe8321SBarry Smith PetscErrorCode ierr; 937b1d57f15SBarry Smith PetscInt nt; 938416022c9SBarry Smith 9393a40ed3dSBarry Smith PetscFunctionBegin; 940a2ce50c7SBarry Smith ierr = VecGetLocalSize(xx,&nt);CHKERRQ(ierr); 94165e19b50SBarry 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); 942ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 943f830108cSBarry Smith ierr = (*a->A->ops->mult)(a->A,xx,yy);CHKERRQ(ierr); 944ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 945f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);CHKERRQ(ierr); 9463a40ed3dSBarry Smith PetscFunctionReturn(0); 9471eb62cbbSBarry Smith } 9481eb62cbbSBarry Smith 9494a2ae208SSatish Balay #undef __FUNCT__ 950bd0c2dcbSBarry Smith #define __FUNCT__ "MatMultDiagonalBlock_MPIAIJ" 951bd0c2dcbSBarry Smith PetscErrorCode MatMultDiagonalBlock_MPIAIJ(Mat A,Vec bb,Vec xx) 952bd0c2dcbSBarry Smith { 953bd0c2dcbSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 954bd0c2dcbSBarry Smith PetscErrorCode ierr; 955bd0c2dcbSBarry Smith 956bd0c2dcbSBarry Smith PetscFunctionBegin; 957bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(a->A,bb,xx);CHKERRQ(ierr); 958bd0c2dcbSBarry Smith PetscFunctionReturn(0); 959bd0c2dcbSBarry Smith } 960bd0c2dcbSBarry Smith 961bd0c2dcbSBarry Smith #undef __FUNCT__ 9624a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_MPIAIJ" 963dfbe8321SBarry Smith PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 964da3a660dSBarry Smith { 965416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 966dfbe8321SBarry Smith PetscErrorCode ierr; 9673a40ed3dSBarry Smith 9683a40ed3dSBarry Smith PetscFunctionBegin; 969ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 970f830108cSBarry Smith ierr = (*a->A->ops->multadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 971ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 972f830108cSBarry Smith ierr = (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);CHKERRQ(ierr); 9733a40ed3dSBarry Smith PetscFunctionReturn(0); 974da3a660dSBarry Smith } 975da3a660dSBarry Smith 9764a2ae208SSatish Balay #undef __FUNCT__ 9774a2ae208SSatish Balay #define __FUNCT__ "MatMultTranspose_MPIAIJ" 978dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy) 979da3a660dSBarry Smith { 980416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 981dfbe8321SBarry Smith PetscErrorCode ierr; 982ace3abfcSBarry Smith PetscBool merged; 983da3a660dSBarry Smith 9843a40ed3dSBarry Smith PetscFunctionBegin; 985a5ff213dSBarry Smith ierr = VecScatterGetMerged(a->Mvctx,&merged);CHKERRQ(ierr); 986da3a660dSBarry Smith /* do nondiagonal part */ 9877c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 988a5ff213dSBarry Smith if (!merged) { 989da3a660dSBarry Smith /* send it on its way */ 990ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 991da3a660dSBarry Smith /* do local part */ 9927c922b88SBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 993da3a660dSBarry Smith /* receive remote parts: note this assumes the values are not actually */ 994a5ff213dSBarry Smith /* added in yy until the next line, */ 995ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 996a5ff213dSBarry Smith } else { 997a5ff213dSBarry Smith /* do local part */ 998a5ff213dSBarry Smith ierr = (*a->A->ops->multtranspose)(a->A,xx,yy);CHKERRQ(ierr); 999a5ff213dSBarry Smith /* send it on its way */ 1000ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1001a5ff213dSBarry Smith /* values actually were received in the Begin() but we need to call this nop */ 1002ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1003a5ff213dSBarry Smith } 10043a40ed3dSBarry Smith PetscFunctionReturn(0); 1005da3a660dSBarry Smith } 1006da3a660dSBarry Smith 1007cd0d46ebSvictorle #undef __FUNCT__ 10085fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_MPIAIJ" 10097087cfbeSBarry Smith PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscBool *f) 1010cd0d46ebSvictorle { 10114f423910Svictorle MPI_Comm comm; 1012cd0d46ebSvictorle Mat_MPIAIJ *Aij = (Mat_MPIAIJ*) Amat->data, *Bij; 101366501d38Svictorle Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs; 1014cd0d46ebSvictorle IS Me,Notme; 10156849ba73SBarry Smith PetscErrorCode ierr; 1016b1d57f15SBarry Smith PetscInt M,N,first,last,*notme,i; 1017b1d57f15SBarry Smith PetscMPIInt size; 1018cd0d46ebSvictorle 1019cd0d46ebSvictorle PetscFunctionBegin; 102042e5f5b4Svictorle /* Easy test: symmetric diagonal block */ 102166501d38Svictorle Bij = (Mat_MPIAIJ*) Bmat->data; Bdia = Bij->A; 10225485867bSBarry Smith ierr = MatIsTranspose(Adia,Bdia,tol,f);CHKERRQ(ierr); 1023cd0d46ebSvictorle if (!*f) PetscFunctionReturn(0); 10244f423910Svictorle ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr); 1025b1d57f15SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 1026b1d57f15SBarry Smith if (size == 1) PetscFunctionReturn(0); 102742e5f5b4Svictorle 102842e5f5b4Svictorle /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */ 1029cd0d46ebSvictorle ierr = MatGetSize(Amat,&M,&N);CHKERRQ(ierr); 1030cd0d46ebSvictorle ierr = MatGetOwnershipRange(Amat,&first,&last);CHKERRQ(ierr); 1031785e854fSJed Brown ierr = PetscMalloc1((N-last+first),¬me);CHKERRQ(ierr); 1032cd0d46ebSvictorle for (i=0; i<first; i++) notme[i] = i; 1033cd0d46ebSvictorle for (i=last; i<M; i++) notme[i-last+first] = i; 103470b3c8c7SBarry Smith ierr = ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,PETSC_COPY_VALUES,&Notme);CHKERRQ(ierr); 1035268466fbSBarry Smith ierr = ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);CHKERRQ(ierr); 1036268466fbSBarry Smith ierr = MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);CHKERRQ(ierr); 103766501d38Svictorle Aoff = Aoffs[0]; 1038268466fbSBarry Smith ierr = MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);CHKERRQ(ierr); 103966501d38Svictorle Boff = Boffs[0]; 10405485867bSBarry Smith ierr = MatIsTranspose(Aoff,Boff,tol,f);CHKERRQ(ierr); 104166501d38Svictorle ierr = MatDestroyMatrices(1,&Aoffs);CHKERRQ(ierr); 104266501d38Svictorle ierr = MatDestroyMatrices(1,&Boffs);CHKERRQ(ierr); 10436bf464f9SBarry Smith ierr = ISDestroy(&Me);CHKERRQ(ierr); 10446bf464f9SBarry Smith ierr = ISDestroy(&Notme);CHKERRQ(ierr); 10453e0d0d19SHong Zhang ierr = PetscFree(notme);CHKERRQ(ierr); 1046cd0d46ebSvictorle PetscFunctionReturn(0); 1047cd0d46ebSvictorle } 1048cd0d46ebSvictorle 10494a2ae208SSatish Balay #undef __FUNCT__ 10504a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_MPIAIJ" 1051dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz) 1052da3a660dSBarry Smith { 1053416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1054dfbe8321SBarry Smith PetscErrorCode ierr; 1055da3a660dSBarry Smith 10563a40ed3dSBarry Smith PetscFunctionBegin; 1057da3a660dSBarry Smith /* do nondiagonal part */ 10587c922b88SBarry Smith ierr = (*a->B->ops->multtranspose)(a->B,xx,a->lvec);CHKERRQ(ierr); 1059da3a660dSBarry Smith /* send it on its way */ 1060ca9f406cSSatish Balay ierr = VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 1061da3a660dSBarry Smith /* do local part */ 10627c922b88SBarry Smith ierr = (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);CHKERRQ(ierr); 1063a5ff213dSBarry Smith /* receive remote parts */ 1064ca9f406cSSatish Balay ierr = VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 10653a40ed3dSBarry Smith PetscFunctionReturn(0); 1066da3a660dSBarry Smith } 1067da3a660dSBarry Smith 10681eb62cbbSBarry Smith /* 10691eb62cbbSBarry Smith This only works correctly for square matrices where the subblock A->A is the 10701eb62cbbSBarry Smith diagonal block 10711eb62cbbSBarry Smith */ 10724a2ae208SSatish Balay #undef __FUNCT__ 10734a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_MPIAIJ" 1074dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v) 10751eb62cbbSBarry Smith { 1076dfbe8321SBarry Smith PetscErrorCode ierr; 1077416022c9SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 10783a40ed3dSBarry Smith 10793a40ed3dSBarry Smith PetscFunctionBegin; 1080ce94432eSBarry Smith if (A->rmap->N != A->cmap->N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); 1081e7e72b3dSBarry 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"); 10823a40ed3dSBarry Smith ierr = MatGetDiagonal(a->A,v);CHKERRQ(ierr); 10833a40ed3dSBarry Smith PetscFunctionReturn(0); 10841eb62cbbSBarry Smith } 10851eb62cbbSBarry Smith 10864a2ae208SSatish Balay #undef __FUNCT__ 10874a2ae208SSatish Balay #define __FUNCT__ "MatScale_MPIAIJ" 1088f4df32b1SMatthew Knepley PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa) 1089052efed2SBarry Smith { 1090052efed2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1091dfbe8321SBarry Smith PetscErrorCode ierr; 10923a40ed3dSBarry Smith 10933a40ed3dSBarry Smith PetscFunctionBegin; 1094f4df32b1SMatthew Knepley ierr = MatScale(a->A,aa);CHKERRQ(ierr); 1095f4df32b1SMatthew Knepley ierr = MatScale(a->B,aa);CHKERRQ(ierr); 10963a40ed3dSBarry Smith PetscFunctionReturn(0); 1097052efed2SBarry Smith } 1098052efed2SBarry Smith 10994a2ae208SSatish Balay #undef __FUNCT__ 11004a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAIJ" 1101dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAIJ(Mat mat) 11021eb62cbbSBarry Smith { 110344a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1104dfbe8321SBarry Smith PetscErrorCode ierr; 110583e2fdc7SBarry Smith 11063a40ed3dSBarry Smith PetscFunctionBegin; 1107aa482453SBarry Smith #if defined(PETSC_USE_LOG) 1108d0f46423SBarry Smith PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap->N,mat->cmap->N); 1109a5a9c739SBarry Smith #endif 11108798bf22SSatish Balay ierr = MatStashDestroy_Private(&mat->stash);CHKERRQ(ierr); 11116bf464f9SBarry Smith ierr = VecDestroy(&aij->diag);CHKERRQ(ierr); 11126bf464f9SBarry Smith ierr = MatDestroy(&aij->A);CHKERRQ(ierr); 11136bf464f9SBarry Smith ierr = MatDestroy(&aij->B);CHKERRQ(ierr); 1114aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 11156bc0bbbfSBarry Smith ierr = PetscTableDestroy(&aij->colmap);CHKERRQ(ierr); 1116b1fc9764SSatish Balay #else 111705b42c5fSBarry Smith ierr = PetscFree(aij->colmap);CHKERRQ(ierr); 1118b1fc9764SSatish Balay #endif 111905b42c5fSBarry Smith ierr = PetscFree(aij->garray);CHKERRQ(ierr); 11206bf464f9SBarry Smith ierr = VecDestroy(&aij->lvec);CHKERRQ(ierr); 11216bf464f9SBarry Smith ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr); 112203095fedSBarry Smith ierr = PetscFree2(aij->rowvalues,aij->rowindices);CHKERRQ(ierr); 11238aa348c1SBarry Smith ierr = PetscFree(aij->ld);CHKERRQ(ierr); 1124bf0cc555SLisandro Dalcin ierr = PetscFree(mat->data);CHKERRQ(ierr); 1125901853e0SKris Buschelman 1126dbd8c25aSHong Zhang ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr); 1127bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);CHKERRQ(ierr); 1128bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);CHKERRQ(ierr); 1129bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);CHKERRQ(ierr); 1130bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C",NULL);CHKERRQ(ierr); 1131bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C",NULL);CHKERRQ(ierr); 1132bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C",NULL);CHKERRQ(ierr); 1133bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);CHKERRQ(ierr); 1134bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpiaij_mpisbaij_C",NULL);CHKERRQ(ierr); 11353a40ed3dSBarry Smith PetscFunctionReturn(0); 11361eb62cbbSBarry Smith } 1137ee50ffe9SBarry Smith 11384a2ae208SSatish Balay #undef __FUNCT__ 11398e2fed03SBarry Smith #define __FUNCT__ "MatView_MPIAIJ_Binary" 1140dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer) 11418e2fed03SBarry Smith { 11428e2fed03SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 11438e2fed03SBarry Smith Mat_SeqAIJ *A = (Mat_SeqAIJ*)aij->A->data; 11448e2fed03SBarry Smith Mat_SeqAIJ *B = (Mat_SeqAIJ*)aij->B->data; 11456849ba73SBarry Smith PetscErrorCode ierr; 114632dcc486SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 11476f69ff64SBarry Smith int fd; 1148a788621eSSatish Balay PetscInt nz,header[4],*row_lengths,*range=0,rlen,i; 1149d892089bSMatthew G. Knepley PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap->rstart,rnz = 0; 11508e2fed03SBarry Smith PetscScalar *column_values; 115185ebf7a4SBarry Smith PetscInt message_count,flowcontrolcount; 1152b37d52dbSMark F. Adams FILE *file; 11538e2fed03SBarry Smith 11548e2fed03SBarry Smith PetscFunctionBegin; 1155ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1156ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);CHKERRQ(ierr); 11578e2fed03SBarry Smith nz = A->nz + B->nz; 1158958c9bccSBarry Smith if (!rank) { 11590700a824SBarry Smith header[0] = MAT_FILE_CLASSID; 1160d0f46423SBarry Smith header[1] = mat->rmap->N; 1161d0f46423SBarry Smith header[2] = mat->cmap->N; 11622205254eSKarl Rupp 1163ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 11648e2fed03SBarry Smith ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 11656f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11668e2fed03SBarry Smith /* get largest number of rows any processor has */ 1167d0f46423SBarry Smith rlen = mat->rmap->n; 1168d0f46423SBarry Smith range = mat->rmap->range; 11692205254eSKarl Rupp for (i=1; i<size; i++) rlen = PetscMax(rlen,range[i+1] - range[i]); 11708e2fed03SBarry Smith } else { 1171ce94432eSBarry Smith ierr = MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1172d0f46423SBarry Smith rlen = mat->rmap->n; 11738e2fed03SBarry Smith } 11748e2fed03SBarry Smith 11758e2fed03SBarry Smith /* load up the local row counts */ 1176785e854fSJed Brown ierr = PetscMalloc1((rlen+1),&row_lengths);CHKERRQ(ierr); 11772205254eSKarl Rupp for (i=0; i<mat->rmap->n; i++) row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i]; 11788e2fed03SBarry Smith 11798e2fed03SBarry Smith /* store the row lengths to the file */ 118085ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1181958c9bccSBarry Smith if (!rank) { 1182d0f46423SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,mat->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11838e2fed03SBarry Smith for (i=1; i<size; i++) { 1184639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 11858e2fed03SBarry Smith rlen = range[i+1] - range[i]; 1186ce94432eSBarry Smith ierr = MPIULong_Recv(row_lengths,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 11876f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 11888e2fed03SBarry Smith } 1189639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 11908e2fed03SBarry Smith } else { 1191639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1192ce94432eSBarry Smith ierr = MPIULong_Send(row_lengths,mat->rmap->n,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1193639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 11948e2fed03SBarry Smith } 11958e2fed03SBarry Smith ierr = PetscFree(row_lengths);CHKERRQ(ierr); 11968e2fed03SBarry Smith 11978e2fed03SBarry Smith /* load up the local column indices */ 11981147fc2aSKarl Rupp nzmax = nz; /* th processor needs space a largest processor needs */ 1199ce94432eSBarry Smith ierr = MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1200785e854fSJed Brown ierr = PetscMalloc1((nzmax+1),&column_indices);CHKERRQ(ierr); 12018e2fed03SBarry Smith cnt = 0; 1202d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12038e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12048e2fed03SBarry Smith if ((col = garray[B->j[j]]) > cstart) break; 12058e2fed03SBarry Smith column_indices[cnt++] = col; 12068e2fed03SBarry Smith } 12072205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_indices[cnt++] = A->j[k] + cstart; 12082205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_indices[cnt++] = garray[B->j[j]]; 12098e2fed03SBarry Smith } 1210e32f2f54SBarry 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); 12118e2fed03SBarry Smith 12128e2fed03SBarry Smith /* store the column indices to the file */ 121385ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1214958c9bccSBarry Smith if (!rank) { 12158e2fed03SBarry Smith MPI_Status status; 12166f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12178e2fed03SBarry Smith for (i=1; i<size; i++) { 1218639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1219ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1220e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1221ce94432eSBarry Smith ierr = MPIULong_Recv(column_indices,rnz,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 12226f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr); 12238e2fed03SBarry Smith } 1224639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 12258e2fed03SBarry Smith } else { 1226639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1227ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1228ce94432eSBarry Smith ierr = MPIULong_Send(column_indices,nz,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1229639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 12308e2fed03SBarry Smith } 12318e2fed03SBarry Smith ierr = PetscFree(column_indices);CHKERRQ(ierr); 12328e2fed03SBarry Smith 12338e2fed03SBarry Smith /* load up the local column values */ 1234785e854fSJed Brown ierr = PetscMalloc1((nzmax+1),&column_values);CHKERRQ(ierr); 12358e2fed03SBarry Smith cnt = 0; 1236d0f46423SBarry Smith for (i=0; i<mat->rmap->n; i++) { 12378e2fed03SBarry Smith for (j=B->i[i]; j<B->i[i+1]; j++) { 12388e2fed03SBarry Smith if (garray[B->j[j]] > cstart) break; 12398e2fed03SBarry Smith column_values[cnt++] = B->a[j]; 12408e2fed03SBarry Smith } 12412205254eSKarl Rupp for (k=A->i[i]; k<A->i[i+1]; k++) column_values[cnt++] = A->a[k]; 12422205254eSKarl Rupp for (; j<B->i[i+1]; j++) column_values[cnt++] = B->a[j]; 12438e2fed03SBarry Smith } 1244e32f2f54SBarry 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); 12458e2fed03SBarry Smith 12468e2fed03SBarry Smith /* store the column values to the file */ 124785ebf7a4SBarry Smith ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1248958c9bccSBarry Smith if (!rank) { 12498e2fed03SBarry Smith MPI_Status status; 12506f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12518e2fed03SBarry Smith for (i=1; i<size; i++) { 1252639ff905SBarry Smith ierr = PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);CHKERRQ(ierr); 1253ce94432eSBarry Smith ierr = MPI_Recv(&rnz,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);CHKERRQ(ierr); 1254e32f2f54SBarry Smith if (rnz > nzmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax); 1255ce94432eSBarry Smith ierr = MPIULong_Recv(column_values,rnz,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 12566f69ff64SBarry Smith ierr = PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);CHKERRQ(ierr); 12578e2fed03SBarry Smith } 1258639ff905SBarry Smith ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 12598e2fed03SBarry Smith } else { 1260639ff905SBarry Smith ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1261ce94432eSBarry Smith ierr = MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1262ce94432eSBarry Smith ierr = MPIULong_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1263639ff905SBarry Smith ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 12648e2fed03SBarry Smith } 12658e2fed03SBarry Smith ierr = PetscFree(column_values);CHKERRQ(ierr); 1266b37d52dbSMark F. Adams 1267b37d52dbSMark F. Adams ierr = PetscViewerBinaryGetInfoPointer(viewer,&file);CHKERRQ(ierr); 12682205254eSKarl Rupp if (file) fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs); 12698e2fed03SBarry Smith PetscFunctionReturn(0); 12708e2fed03SBarry Smith } 12718e2fed03SBarry Smith 12729804daf3SBarry Smith #include <petscdraw.h> 12738e2fed03SBarry Smith #undef __FUNCT__ 12744a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ_ASCIIorDraworSocket" 1275dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer) 1276416022c9SBarry Smith { 127744a69424SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1278dfbe8321SBarry Smith PetscErrorCode ierr; 127932dcc486SBarry Smith PetscMPIInt rank = aij->rank,size = aij->size; 1280ace3abfcSBarry Smith PetscBool isdraw,iascii,isbinary; 1281b0a32e0cSBarry Smith PetscViewer sviewer; 1282f3ef73ceSBarry Smith PetscViewerFormat format; 1283416022c9SBarry Smith 12843a40ed3dSBarry Smith PetscFunctionBegin; 1285251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1286251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1287251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 128832077d6dSBarry Smith if (iascii) { 1289b0a32e0cSBarry Smith ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 1290456192e2SBarry Smith if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 12914e220ebcSLois Curfman McInnes MatInfo info; 1292ace3abfcSBarry Smith PetscBool inodes; 1293923f20ffSKris Buschelman 1294ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);CHKERRQ(ierr); 1295888f2ed8SSatish Balay ierr = MatGetInfo(mat,MAT_LOCAL,&info);CHKERRQ(ierr); 12960298fd71SBarry Smith ierr = MatInodeGetInodeSizes(aij->A,NULL,(PetscInt**)&inodes,NULL);CHKERRQ(ierr); 12977b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); 1298923f20ffSKris Buschelman if (!inodes) { 129977431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n", 1300d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13016831982aSBarry Smith } else { 130277431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n", 1303d0f46423SBarry Smith rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);CHKERRQ(ierr); 13046831982aSBarry Smith } 1305888f2ed8SSatish Balay ierr = MatGetInfo(aij->A,MAT_LOCAL,&info);CHKERRQ(ierr); 130677431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1307888f2ed8SSatish Balay ierr = MatGetInfo(aij->B,MAT_LOCAL,&info);CHKERRQ(ierr); 130877431f27SBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);CHKERRQ(ierr); 1309b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 13107b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); 131107d81ca4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");CHKERRQ(ierr); 1312a40aa06bSLois Curfman McInnes ierr = VecScatterView(aij->Mvctx,viewer);CHKERRQ(ierr); 13133a40ed3dSBarry Smith PetscFunctionReturn(0); 1314fb9695e5SSatish Balay } else if (format == PETSC_VIEWER_ASCII_INFO) { 1315923f20ffSKris Buschelman PetscInt inodecount,inodelimit,*inodes; 1316923f20ffSKris Buschelman ierr = MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);CHKERRQ(ierr); 1317923f20ffSKris Buschelman if (inodes) { 1318923f20ffSKris Buschelman ierr = PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);CHKERRQ(ierr); 1319d38fa0fbSBarry Smith } else { 1320d38fa0fbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");CHKERRQ(ierr); 1321d38fa0fbSBarry Smith } 13223a40ed3dSBarry Smith PetscFunctionReturn(0); 13234aedb280SBarry Smith } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) { 13244aedb280SBarry Smith PetscFunctionReturn(0); 132508480c60SBarry Smith } 13268e2fed03SBarry Smith } else if (isbinary) { 13278e2fed03SBarry Smith if (size == 1) { 13287adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 13298e2fed03SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13308e2fed03SBarry Smith } else { 13318e2fed03SBarry Smith ierr = MatView_MPIAIJ_Binary(mat,viewer);CHKERRQ(ierr); 13328e2fed03SBarry Smith } 13338e2fed03SBarry Smith PetscFunctionReturn(0); 13340f5bd95cSBarry Smith } else if (isdraw) { 1335b0a32e0cSBarry Smith PetscDraw draw; 1336ace3abfcSBarry Smith PetscBool isnull; 1337b0a32e0cSBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 1338b0a32e0cSBarry Smith ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); 133919bcc07fSBarry Smith } 134019bcc07fSBarry Smith 134117699dbbSLois Curfman McInnes if (size == 1) { 13427adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)aij->A,((PetscObject)mat)->name);CHKERRQ(ierr); 134378b31e54SBarry Smith ierr = MatView(aij->A,viewer);CHKERRQ(ierr); 13443a40ed3dSBarry Smith } else { 134595373324SBarry Smith /* assemble the entire matrix onto first processor. */ 134695373324SBarry Smith Mat A; 1347ec8511deSBarry Smith Mat_SeqAIJ *Aloc; 1348d0f46423SBarry Smith PetscInt M = mat->rmap->N,N = mat->cmap->N,m,*ai,*aj,row,*cols,i,*ct; 1349dd6ea824SBarry Smith MatScalar *a; 13502ee70a88SLois Curfman McInnes 135132a366e4SMatthew Knepley if (mat->rmap->N > 1024) { 1352ace3abfcSBarry Smith PetscBool flg = PETSC_FALSE; 135332a366e4SMatthew Knepley 13540298fd71SBarry Smith ierr = PetscOptionsGetBool(((PetscObject) mat)->prefix, "-mat_ascii_output_large", &flg,NULL);CHKERRQ(ierr); 1355ce94432eSBarry Smith if (!flg) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_OUTOFRANGE,"ASCII matrix output not allowed for matrices with more than 1024 rows, use binary format instead.\nYou can override this restriction using -mat_ascii_output_large."); 135632a366e4SMatthew Knepley } 13570805154bSBarry Smith 1358ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)mat),&A);CHKERRQ(ierr); 135917699dbbSLois Curfman McInnes if (!rank) { 1360f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,M,N,M,N);CHKERRQ(ierr); 13613a40ed3dSBarry Smith } else { 1362f69a0ea3SMatthew Knepley ierr = MatSetSizes(A,0,0,M,N);CHKERRQ(ierr); 136395373324SBarry Smith } 1364f204ca49SKris Buschelman /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */ 1365f204ca49SKris Buschelman ierr = MatSetType(A,MATMPIAIJ);CHKERRQ(ierr); 13660298fd71SBarry Smith ierr = MatMPIAIJSetPreallocation(A,0,NULL,0,NULL);CHKERRQ(ierr); 13672b82e772SSatish Balay ierr = MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 13683bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)A);CHKERRQ(ierr); 1369416022c9SBarry Smith 137095373324SBarry Smith /* copy over the A part */ 1371ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->A->data; 1372d0f46423SBarry Smith m = aij->A->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1373d0f46423SBarry Smith row = mat->rmap->rstart; 13742205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] += mat->cmap->rstart; 137595373324SBarry Smith for (i=0; i<m; i++) { 1376416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);CHKERRQ(ierr); 137726fbe8dcSKarl Rupp row++; 137826fbe8dcSKarl Rupp a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i]; 137995373324SBarry Smith } 13802ee70a88SLois Curfman McInnes aj = Aloc->j; 13812205254eSKarl Rupp for (i=0; i<ai[m]; i++) aj[i] -= mat->cmap->rstart; 138295373324SBarry Smith 138395373324SBarry Smith /* copy over the B part */ 1384ec8511deSBarry Smith Aloc = (Mat_SeqAIJ*)aij->B->data; 1385d0f46423SBarry Smith m = aij->B->rmap->n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a; 1386d0f46423SBarry Smith row = mat->rmap->rstart; 1387785e854fSJed Brown ierr = PetscMalloc1((ai[m]+1),&cols);CHKERRQ(ierr); 1388b0a32e0cSBarry Smith ct = cols; 13892205254eSKarl Rupp for (i=0; i<ai[m]; i++) cols[i] = aij->garray[aj[i]]; 139095373324SBarry Smith for (i=0; i<m; i++) { 1391416022c9SBarry Smith ierr = MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);CHKERRQ(ierr); 13922205254eSKarl Rupp row++; 13932205254eSKarl Rupp a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i]; 139495373324SBarry Smith } 1395606d414cSSatish Balay ierr = PetscFree(ct);CHKERRQ(ierr); 13966d4a8577SBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 13976d4a8577SBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 139855843e3eSBarry Smith /* 139955843e3eSBarry Smith Everyone has to call to draw the matrix since the graphics waits are 1400b0a32e0cSBarry Smith synchronized across all processors that share the PetscDraw object 140155843e3eSBarry Smith */ 1402b0a32e0cSBarry Smith ierr = PetscViewerGetSingleton(viewer,&sviewer);CHKERRQ(ierr); 1403e03a110bSBarry Smith if (!rank) { 14047adad957SLisandro Dalcin ierr = PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,((PetscObject)mat)->name);CHKERRQ(ierr); 14057566de4bSShri Abhyankar /* Set the type name to MATMPIAIJ so that the correct type can be printed out by PetscObjectPrintClassNamePrefixType() in MatView_SeqAIJ_ASCII()*/ 14067566de4bSShri Abhyankar PetscStrcpy(((PetscObject)((Mat_MPIAIJ*)(A->data))->A)->type_name,MATMPIAIJ); 14076831982aSBarry Smith ierr = MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);CHKERRQ(ierr); 140895373324SBarry Smith } 1409b0a32e0cSBarry Smith ierr = PetscViewerRestoreSingleton(viewer,&sviewer);CHKERRQ(ierr); 14106bf464f9SBarry Smith ierr = MatDestroy(&A);CHKERRQ(ierr); 141195373324SBarry Smith } 14123a40ed3dSBarry Smith PetscFunctionReturn(0); 14131eb62cbbSBarry Smith } 14141eb62cbbSBarry Smith 14154a2ae208SSatish Balay #undef __FUNCT__ 14164a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAIJ" 1417dfbe8321SBarry Smith PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer) 1418416022c9SBarry Smith { 1419dfbe8321SBarry Smith PetscErrorCode ierr; 1420ace3abfcSBarry Smith PetscBool iascii,isdraw,issocket,isbinary; 1421416022c9SBarry Smith 14223a40ed3dSBarry Smith PetscFunctionBegin; 1423251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1424251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 1425251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 1426251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);CHKERRQ(ierr); 142732077d6dSBarry Smith if (iascii || isdraw || isbinary || issocket) { 14287b2a1423SBarry Smith ierr = MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);CHKERRQ(ierr); 1429416022c9SBarry Smith } 14303a40ed3dSBarry Smith PetscFunctionReturn(0); 1431416022c9SBarry Smith } 1432416022c9SBarry Smith 14334a2ae208SSatish Balay #undef __FUNCT__ 143441f059aeSBarry Smith #define __FUNCT__ "MatSOR_MPIAIJ" 143541f059aeSBarry Smith PetscErrorCode MatSOR_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx) 14368a729477SBarry Smith { 143744a69424SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1438dfbe8321SBarry Smith PetscErrorCode ierr; 14396987fefcSBarry Smith Vec bb1 = 0; 1440ace3abfcSBarry Smith PetscBool hasop; 14418a729477SBarry Smith 14423a40ed3dSBarry Smith PetscFunctionBegin; 1443a2b30743SBarry Smith if (flag == SOR_APPLY_UPPER) { 144441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 1445a2b30743SBarry Smith PetscFunctionReturn(0); 1446a2b30743SBarry Smith } 1447a2b30743SBarry Smith 14484e980039SJed Brown if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) { 14494e980039SJed Brown ierr = VecDuplicate(bb,&bb1);CHKERRQ(ierr); 14504e980039SJed Brown } 14514e980039SJed Brown 1452c16cb8f2SBarry Smith if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) { 1453da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 145441f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14552798e883SHong Zhang its--; 1456da3a660dSBarry Smith } 14572798e883SHong Zhang 14582798e883SHong Zhang while (its--) { 1459ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1460ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14612798e883SHong Zhang 1462c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1463efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1464c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14652798e883SHong Zhang 1466c14dc6b6SHong Zhang /* local sweep */ 146741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14682798e883SHong Zhang } 14693a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_FORWARD_SWEEP) { 1470da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 147141f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14722798e883SHong Zhang its--; 1473da3a660dSBarry Smith } 14742798e883SHong Zhang while (its--) { 1475ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1476ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14772798e883SHong Zhang 1478c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1479efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1480c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 1481c14dc6b6SHong Zhang 1482c14dc6b6SHong Zhang /* local sweep */ 148341f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 14842798e883SHong Zhang } 14853a40ed3dSBarry Smith } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) { 1486da3a660dSBarry Smith if (flag & SOR_ZERO_INITIAL_GUESS) { 148741f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);CHKERRQ(ierr); 14882798e883SHong Zhang its--; 1489da3a660dSBarry Smith } 14902798e883SHong Zhang while (its--) { 1491ca9f406cSSatish Balay ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1492ca9f406cSSatish Balay ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 14932798e883SHong Zhang 1494c14dc6b6SHong Zhang /* update rhs: bb1 = bb - B*x */ 1495efb30889SBarry Smith ierr = VecScale(mat->lvec,-1.0);CHKERRQ(ierr); 1496c14dc6b6SHong Zhang ierr = (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);CHKERRQ(ierr); 14972798e883SHong Zhang 1498c14dc6b6SHong Zhang /* local sweep */ 149941f059aeSBarry Smith ierr = (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);CHKERRQ(ierr); 15002798e883SHong Zhang } 1501a7420bb7SBarry Smith } else if (flag & SOR_EISENSTAT) { 1502a7420bb7SBarry Smith Vec xx1; 1503a7420bb7SBarry Smith 1504a7420bb7SBarry Smith ierr = VecDuplicate(bb,&xx1);CHKERRQ(ierr); 150541f059aeSBarry 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); 1506a7420bb7SBarry Smith 1507a7420bb7SBarry Smith ierr = VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1508a7420bb7SBarry Smith ierr = VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 1509a7420bb7SBarry Smith if (!mat->diag) { 15100298fd71SBarry Smith ierr = MatGetVecs(matin,&mat->diag,NULL);CHKERRQ(ierr); 1511a7420bb7SBarry Smith ierr = MatGetDiagonal(matin,mat->diag);CHKERRQ(ierr); 1512a7420bb7SBarry Smith } 1513bd0c2dcbSBarry Smith ierr = MatHasOperation(matin,MATOP_MULT_DIAGONAL_BLOCK,&hasop);CHKERRQ(ierr); 1514bd0c2dcbSBarry Smith if (hasop) { 1515bd0c2dcbSBarry Smith ierr = MatMultDiagonalBlock(matin,xx,bb1);CHKERRQ(ierr); 1516bd0c2dcbSBarry Smith } else { 1517a7420bb7SBarry Smith ierr = VecPointwiseMult(bb1,mat->diag,xx);CHKERRQ(ierr); 1518bd0c2dcbSBarry Smith } 1519887ee2caSBarry Smith ierr = VecAYPX(bb1,(omega-2.0)/omega,bb);CHKERRQ(ierr); 1520887ee2caSBarry Smith 1521a7420bb7SBarry Smith ierr = MatMultAdd(mat->B,mat->lvec,bb1,bb1);CHKERRQ(ierr); 1522a7420bb7SBarry Smith 1523a7420bb7SBarry Smith /* local sweep */ 152441f059aeSBarry 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); 1525a7420bb7SBarry Smith ierr = VecAXPY(xx,1.0,xx1);CHKERRQ(ierr); 15266bf464f9SBarry Smith ierr = VecDestroy(&xx1);CHKERRQ(ierr); 1527ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel SOR not supported"); 1528c14dc6b6SHong Zhang 15296bf464f9SBarry Smith ierr = VecDestroy(&bb1);CHKERRQ(ierr); 15303a40ed3dSBarry Smith PetscFunctionReturn(0); 15318a729477SBarry Smith } 1532a66be287SLois Curfman McInnes 15334a2ae208SSatish Balay #undef __FUNCT__ 153442e855d1Svictor #define __FUNCT__ "MatPermute_MPIAIJ" 153542e855d1Svictor PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B) 153642e855d1Svictor { 153772e6a0cfSJed Brown Mat aA,aB,Aperm; 153872e6a0cfSJed Brown const PetscInt *rwant,*cwant,*gcols,*ai,*bi,*aj,*bj; 153972e6a0cfSJed Brown PetscScalar *aa,*ba; 154072e6a0cfSJed Brown PetscInt i,j,m,n,ng,anz,bnz,*dnnz,*onnz,*tdnnz,*tonnz,*rdest,*cdest,*work,*gcdest; 154172e6a0cfSJed Brown PetscSF rowsf,sf; 15420298fd71SBarry Smith IS parcolp = NULL; 154372e6a0cfSJed Brown PetscBool done; 154442e855d1Svictor PetscErrorCode ierr; 154542e855d1Svictor 154642e855d1Svictor PetscFunctionBegin; 154772e6a0cfSJed Brown ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 154872e6a0cfSJed Brown ierr = ISGetIndices(rowp,&rwant);CHKERRQ(ierr); 154972e6a0cfSJed Brown ierr = ISGetIndices(colp,&cwant);CHKERRQ(ierr); 1550dcca6d9dSJed Brown ierr = PetscMalloc3(PetscMax(m,n),&work,m,&rdest,n,&cdest);CHKERRQ(ierr); 155172e6a0cfSJed Brown 155272e6a0cfSJed Brown /* Invert row permutation to find out where my rows should go */ 1553ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&rowsf);CHKERRQ(ierr); 15540298fd71SBarry Smith ierr = PetscSFSetGraphLayout(rowsf,A->rmap,A->rmap->n,NULL,PETSC_OWN_POINTER,rwant);CHKERRQ(ierr); 1555e9e74f11SJed Brown ierr = PetscSFSetFromOptions(rowsf);CHKERRQ(ierr); 155672e6a0cfSJed Brown for (i=0; i<m; i++) work[i] = A->rmap->rstart + i; 15578bfbc91cSJed Brown ierr = PetscSFReduceBegin(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr); 15588bfbc91cSJed Brown ierr = PetscSFReduceEnd(rowsf,MPIU_INT,work,rdest,MPIU_REPLACE);CHKERRQ(ierr); 155972e6a0cfSJed Brown 156072e6a0cfSJed Brown /* Invert column permutation to find out where my columns should go */ 1561ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 15620298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,A->cmap->n,NULL,PETSC_OWN_POINTER,cwant);CHKERRQ(ierr); 1563e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 156472e6a0cfSJed Brown for (i=0; i<n; i++) work[i] = A->cmap->rstart + i; 15658bfbc91cSJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr); 15668bfbc91cSJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,work,cdest,MPIU_REPLACE);CHKERRQ(ierr); 156772e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 156872e6a0cfSJed Brown 156972e6a0cfSJed Brown ierr = ISRestoreIndices(rowp,&rwant);CHKERRQ(ierr); 157072e6a0cfSJed Brown ierr = ISRestoreIndices(colp,&cwant);CHKERRQ(ierr); 157172e6a0cfSJed Brown ierr = MatMPIAIJGetSeqAIJ(A,&aA,&aB,&gcols);CHKERRQ(ierr); 157272e6a0cfSJed Brown 157372e6a0cfSJed Brown /* Find out where my gcols should go */ 15740298fd71SBarry Smith ierr = MatGetSize(aB,NULL,&ng);CHKERRQ(ierr); 1575785e854fSJed Brown ierr = PetscMalloc1(ng,&gcdest);CHKERRQ(ierr); 1576ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 15770298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,ng,NULL,PETSC_OWN_POINTER,gcols);CHKERRQ(ierr); 1578e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 157972e6a0cfSJed Brown ierr = PetscSFBcastBegin(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 158072e6a0cfSJed Brown ierr = PetscSFBcastEnd(sf,MPIU_INT,cdest,gcdest);CHKERRQ(ierr); 158172e6a0cfSJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 158272e6a0cfSJed Brown 15831795a4d1SJed Brown ierr = PetscCalloc4(m,&dnnz,m,&onnz,m,&tdnnz,m,&tonnz);CHKERRQ(ierr); 158472e6a0cfSJed Brown ierr = MatGetRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 158572e6a0cfSJed Brown ierr = MatGetRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 158672e6a0cfSJed Brown for (i=0; i<m; i++) { 158772e6a0cfSJed Brown PetscInt row = rdest[i],rowner; 158872e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->rmap,row,&rowner);CHKERRQ(ierr); 158972e6a0cfSJed Brown for (j=ai[i]; j<ai[i+1]; j++) { 159072e6a0cfSJed Brown PetscInt cowner,col = cdest[aj[j]]; 159172e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); /* Could build an index for the columns to eliminate this search */ 159272e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 159372e6a0cfSJed Brown else onnz[i]++; 159472e6a0cfSJed Brown } 159572e6a0cfSJed Brown for (j=bi[i]; j<bi[i+1]; j++) { 159672e6a0cfSJed Brown PetscInt cowner,col = gcdest[bj[j]]; 159772e6a0cfSJed Brown ierr = PetscLayoutFindOwner(A->cmap,col,&cowner);CHKERRQ(ierr); 159872e6a0cfSJed Brown if (rowner == cowner) dnnz[i]++; 159972e6a0cfSJed Brown else onnz[i]++; 160072e6a0cfSJed Brown } 160172e6a0cfSJed Brown } 160272e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 160372e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,dnnz,tdnnz);CHKERRQ(ierr); 160472e6a0cfSJed Brown ierr = PetscSFBcastBegin(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 160572e6a0cfSJed Brown ierr = PetscSFBcastEnd(rowsf,MPIU_INT,onnz,tonnz);CHKERRQ(ierr); 160672e6a0cfSJed Brown ierr = PetscSFDestroy(&rowsf);CHKERRQ(ierr); 160772e6a0cfSJed Brown 1608ce94432eSBarry Smith ierr = MatCreateAIJ(PetscObjectComm((PetscObject)A),A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N,0,tdnnz,0,tonnz,&Aperm);CHKERRQ(ierr); 160972e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aA,&aa);CHKERRQ(ierr); 161072e6a0cfSJed Brown ierr = MatSeqAIJGetArray(aB,&ba);CHKERRQ(ierr); 161172e6a0cfSJed Brown for (i=0; i<m; i++) { 161272e6a0cfSJed Brown PetscInt *acols = dnnz,*bcols = onnz; /* Repurpose now-unneeded arrays */ 1613*970468b0SJed Brown PetscInt j0,rowlen; 161472e6a0cfSJed Brown rowlen = ai[i+1] - ai[i]; 1615*970468b0SJed Brown for (j0=j=0; j<rowlen; j0=j) { /* rowlen could be larger than number of rows m, so sum in batches */ 1616*970468b0SJed Brown for ( ; j<PetscMin(rowlen,j0+m); j++) acols[j-j0] = cdest[aj[ai[i]+j]]; 1617*970468b0SJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,acols,aa+ai[i]+j0,INSERT_VALUES);CHKERRQ(ierr); 1618*970468b0SJed Brown } 161972e6a0cfSJed Brown rowlen = bi[i+1] - bi[i]; 1620*970468b0SJed Brown for (j0=j=0; j<rowlen; j0=j) { 1621*970468b0SJed Brown for ( ; j<PetscMin(rowlen,j0+m); j++) bcols[j-j0] = gcdest[bj[bi[i]+j]]; 1622*970468b0SJed Brown ierr = MatSetValues(Aperm,1,&rdest[i],j-j0,bcols,ba+bi[i]+j0,INSERT_VALUES);CHKERRQ(ierr); 1623*970468b0SJed Brown } 162472e6a0cfSJed Brown } 162572e6a0cfSJed Brown ierr = MatAssemblyBegin(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 162672e6a0cfSJed Brown ierr = MatAssemblyEnd(Aperm,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 162772e6a0cfSJed Brown ierr = MatRestoreRowIJ(aA,0,PETSC_FALSE,PETSC_FALSE,&anz,&ai,&aj,&done);CHKERRQ(ierr); 162872e6a0cfSJed Brown ierr = MatRestoreRowIJ(aB,0,PETSC_FALSE,PETSC_FALSE,&bnz,&bi,&bj,&done);CHKERRQ(ierr); 162972e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aA,&aa);CHKERRQ(ierr); 163072e6a0cfSJed Brown ierr = MatSeqAIJRestoreArray(aB,&ba);CHKERRQ(ierr); 163172e6a0cfSJed Brown ierr = PetscFree4(dnnz,onnz,tdnnz,tonnz);CHKERRQ(ierr); 163272e6a0cfSJed Brown ierr = PetscFree3(work,rdest,cdest);CHKERRQ(ierr); 163372e6a0cfSJed Brown ierr = PetscFree(gcdest);CHKERRQ(ierr); 163472e6a0cfSJed Brown if (parcolp) {ierr = ISDestroy(&colp);CHKERRQ(ierr);} 163572e6a0cfSJed Brown *B = Aperm; 163642e855d1Svictor PetscFunctionReturn(0); 163742e855d1Svictor } 163842e855d1Svictor 163942e855d1Svictor #undef __FUNCT__ 16404a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_MPIAIJ" 1641dfbe8321SBarry Smith PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info) 1642a66be287SLois Curfman McInnes { 1643a66be287SLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 1644a66be287SLois Curfman McInnes Mat A = mat->A,B = mat->B; 1645dfbe8321SBarry Smith PetscErrorCode ierr; 1646329f5518SBarry Smith PetscReal isend[5],irecv[5]; 1647a66be287SLois Curfman McInnes 16483a40ed3dSBarry Smith PetscFunctionBegin; 16494e220ebcSLois Curfman McInnes info->block_size = 1.0; 16504e220ebcSLois Curfman McInnes ierr = MatGetInfo(A,MAT_LOCAL,info);CHKERRQ(ierr); 16512205254eSKarl Rupp 16524e220ebcSLois Curfman McInnes isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded; 16534e220ebcSLois Curfman McInnes isend[3] = info->memory; isend[4] = info->mallocs; 16542205254eSKarl Rupp 16554e220ebcSLois Curfman McInnes ierr = MatGetInfo(B,MAT_LOCAL,info);CHKERRQ(ierr); 16562205254eSKarl Rupp 16574e220ebcSLois Curfman McInnes isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded; 16584e220ebcSLois Curfman McInnes isend[3] += info->memory; isend[4] += info->mallocs; 1659a66be287SLois Curfman McInnes if (flag == MAT_LOCAL) { 16604e220ebcSLois Curfman McInnes info->nz_used = isend[0]; 16614e220ebcSLois Curfman McInnes info->nz_allocated = isend[1]; 16624e220ebcSLois Curfman McInnes info->nz_unneeded = isend[2]; 16634e220ebcSLois Curfman McInnes info->memory = isend[3]; 16644e220ebcSLois Curfman McInnes info->mallocs = isend[4]; 1665a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_MAX) { 1666ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 16672205254eSKarl Rupp 16684e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16694e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16704e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16714e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16724e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1673a66be287SLois Curfman McInnes } else if (flag == MAT_GLOBAL_SUM) { 1674ce94432eSBarry Smith ierr = MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));CHKERRQ(ierr); 16752205254eSKarl Rupp 16764e220ebcSLois Curfman McInnes info->nz_used = irecv[0]; 16774e220ebcSLois Curfman McInnes info->nz_allocated = irecv[1]; 16784e220ebcSLois Curfman McInnes info->nz_unneeded = irecv[2]; 16794e220ebcSLois Curfman McInnes info->memory = irecv[3]; 16804e220ebcSLois Curfman McInnes info->mallocs = irecv[4]; 1681a66be287SLois Curfman McInnes } 16824e220ebcSLois Curfman McInnes info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */ 16834e220ebcSLois Curfman McInnes info->fill_ratio_needed = 0; 16844e220ebcSLois Curfman McInnes info->factor_mallocs = 0; 16853a40ed3dSBarry Smith PetscFunctionReturn(0); 1686a66be287SLois Curfman McInnes } 1687a66be287SLois Curfman McInnes 16884a2ae208SSatish Balay #undef __FUNCT__ 16894a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAIJ" 1690ace3abfcSBarry Smith PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op,PetscBool flg) 1691c74985f6SBarry Smith { 1692c0bbcb79SLois Curfman McInnes Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1693dfbe8321SBarry Smith PetscErrorCode ierr; 1694c74985f6SBarry Smith 16953a40ed3dSBarry Smith PetscFunctionBegin; 169612c028f9SKris Buschelman switch (op) { 1697512a5fc5SBarry Smith case MAT_NEW_NONZERO_LOCATIONS: 169812c028f9SKris Buschelman case MAT_NEW_NONZERO_ALLOCATION_ERR: 169928b2fa4aSMatthew Knepley case MAT_UNUSED_NONZERO_LOCATION_ERR: 1700a9817697SBarry Smith case MAT_KEEP_NONZERO_PATTERN: 170112c028f9SKris Buschelman case MAT_NEW_NONZERO_LOCATION_ERR: 170212c028f9SKris Buschelman case MAT_USE_INODES: 170312c028f9SKris Buschelman case MAT_IGNORE_ZERO_ENTRIES: 1704fa1f0d2cSMatthew G Knepley MatCheckPreallocated(A,1); 17054e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 17064e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 170712c028f9SKris Buschelman break; 170812c028f9SKris Buschelman case MAT_ROW_ORIENTED: 17094e0d8c25SBarry Smith a->roworiented = flg; 17102205254eSKarl Rupp 17114e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 17124e0d8c25SBarry Smith ierr = MatSetOption(a->B,op,flg);CHKERRQ(ierr); 171312c028f9SKris Buschelman break; 17144e0d8c25SBarry Smith case MAT_NEW_DIAGONALS: 1715290bbb0aSBarry Smith ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr); 171612c028f9SKris Buschelman break; 171712c028f9SKris Buschelman case MAT_IGNORE_OFF_PROC_ENTRIES: 17185c0f0b64SBarry Smith a->donotstash = flg; 171912c028f9SKris Buschelman break; 1720ffa07934SHong Zhang case MAT_SPD: 1721ffa07934SHong Zhang A->spd_set = PETSC_TRUE; 1722ffa07934SHong Zhang A->spd = flg; 1723ffa07934SHong Zhang if (flg) { 1724ffa07934SHong Zhang A->symmetric = PETSC_TRUE; 1725ffa07934SHong Zhang A->structurally_symmetric = PETSC_TRUE; 1726ffa07934SHong Zhang A->symmetric_set = PETSC_TRUE; 1727ffa07934SHong Zhang A->structurally_symmetric_set = PETSC_TRUE; 1728ffa07934SHong Zhang } 1729ffa07934SHong Zhang break; 173077e54ba9SKris Buschelman case MAT_SYMMETRIC: 17314e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 173225f421beSHong Zhang break; 173377e54ba9SKris Buschelman case MAT_STRUCTURALLY_SYMMETRIC: 1734eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1735eeffb40dSHong Zhang break; 1736bf108f30SBarry Smith case MAT_HERMITIAN: 1737eeffb40dSHong Zhang ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 1738eeffb40dSHong Zhang break; 1739bf108f30SBarry Smith case MAT_SYMMETRY_ETERNAL: 17404e0d8c25SBarry Smith ierr = MatSetOption(a->A,op,flg);CHKERRQ(ierr); 174177e54ba9SKris Buschelman break; 174212c028f9SKris Buschelman default: 1743e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op); 17443a40ed3dSBarry Smith } 17453a40ed3dSBarry Smith PetscFunctionReturn(0); 1746c74985f6SBarry Smith } 1747c74985f6SBarry Smith 17484a2ae208SSatish Balay #undef __FUNCT__ 17494a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAIJ" 1750b1d57f15SBarry Smith PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 175139e00950SLois Curfman McInnes { 1752154123eaSLois Curfman McInnes Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data; 175387828ca2SBarry Smith PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p; 17546849ba73SBarry Smith PetscErrorCode ierr; 1755d0f46423SBarry Smith PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap->rstart; 1756d0f46423SBarry Smith PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap->rstart,rend = matin->rmap->rend; 1757b1d57f15SBarry Smith PetscInt *cmap,*idx_p; 175839e00950SLois Curfman McInnes 17593a40ed3dSBarry Smith PetscFunctionBegin; 1760e32f2f54SBarry Smith if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active"); 17617a0afa10SBarry Smith mat->getrowactive = PETSC_TRUE; 17627a0afa10SBarry Smith 176370f0671dSBarry Smith if (!mat->rowvalues && (idx || v)) { 17647a0afa10SBarry Smith /* 17657a0afa10SBarry Smith allocate enough space to hold information from the longest row. 17667a0afa10SBarry Smith */ 17677a0afa10SBarry Smith Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data; 1768b1d57f15SBarry Smith PetscInt max = 1,tmp; 1769d0f46423SBarry Smith for (i=0; i<matin->rmap->n; i++) { 17707a0afa10SBarry Smith tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i]; 17712205254eSKarl Rupp if (max < tmp) max = tmp; 17727a0afa10SBarry Smith } 1773dcca6d9dSJed Brown ierr = PetscMalloc2(max,&mat->rowvalues,max,&mat->rowindices);CHKERRQ(ierr); 17747a0afa10SBarry Smith } 17757a0afa10SBarry Smith 1776e7e72b3dSBarry Smith if (row < rstart || row >= rend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only local rows"); 1777abc0e9e4SLois Curfman McInnes lrow = row - rstart; 177839e00950SLois Curfman McInnes 1779154123eaSLois Curfman McInnes pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB; 1780154123eaSLois Curfman McInnes if (!v) {pvA = 0; pvB = 0;} 1781154123eaSLois Curfman McInnes if (!idx) {pcA = 0; if (!v) pcB = 0;} 1782f830108cSBarry Smith ierr = (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1783f830108cSBarry Smith ierr = (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 1784154123eaSLois Curfman McInnes nztot = nzA + nzB; 1785154123eaSLois Curfman McInnes 178670f0671dSBarry Smith cmap = mat->garray; 1787154123eaSLois Curfman McInnes if (v || idx) { 1788154123eaSLois Curfman McInnes if (nztot) { 1789154123eaSLois Curfman McInnes /* Sort by increasing column numbers, assuming A and B already sorted */ 1790b1d57f15SBarry Smith PetscInt imark = -1; 1791154123eaSLois Curfman McInnes if (v) { 179270f0671dSBarry Smith *v = v_p = mat->rowvalues; 179339e00950SLois Curfman McInnes for (i=0; i<nzB; i++) { 179470f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i]; 1795154123eaSLois Curfman McInnes else break; 1796154123eaSLois Curfman McInnes } 1797154123eaSLois Curfman McInnes imark = i; 179870f0671dSBarry Smith for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i]; 179970f0671dSBarry Smith for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i]; 1800154123eaSLois Curfman McInnes } 1801154123eaSLois Curfman McInnes if (idx) { 180270f0671dSBarry Smith *idx = idx_p = mat->rowindices; 180370f0671dSBarry Smith if (imark > -1) { 180470f0671dSBarry Smith for (i=0; i<imark; i++) { 180570f0671dSBarry Smith idx_p[i] = cmap[cworkB[i]]; 180670f0671dSBarry Smith } 180770f0671dSBarry Smith } else { 1808154123eaSLois Curfman McInnes for (i=0; i<nzB; i++) { 180970f0671dSBarry Smith if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]]; 1810154123eaSLois Curfman McInnes else break; 1811154123eaSLois Curfman McInnes } 1812154123eaSLois Curfman McInnes imark = i; 181370f0671dSBarry Smith } 181470f0671dSBarry Smith for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i]; 181570f0671dSBarry Smith for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]]; 181639e00950SLois Curfman McInnes } 18173f97c4b0SBarry Smith } else { 18181ca473b0SSatish Balay if (idx) *idx = 0; 18191ca473b0SSatish Balay if (v) *v = 0; 18201ca473b0SSatish Balay } 1821154123eaSLois Curfman McInnes } 182239e00950SLois Curfman McInnes *nz = nztot; 1823f830108cSBarry Smith ierr = (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);CHKERRQ(ierr); 1824f830108cSBarry Smith ierr = (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);CHKERRQ(ierr); 18253a40ed3dSBarry Smith PetscFunctionReturn(0); 182639e00950SLois Curfman McInnes } 182739e00950SLois Curfman McInnes 18284a2ae208SSatish Balay #undef __FUNCT__ 18294a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAIJ" 1830b1d57f15SBarry Smith PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v) 183139e00950SLois Curfman McInnes { 18327a0afa10SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 18333a40ed3dSBarry Smith 18343a40ed3dSBarry Smith PetscFunctionBegin; 1835e7e72b3dSBarry Smith if (!aij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first"); 18367a0afa10SBarry Smith aij->getrowactive = PETSC_FALSE; 18373a40ed3dSBarry Smith PetscFunctionReturn(0); 183839e00950SLois Curfman McInnes } 183939e00950SLois Curfman McInnes 18404a2ae208SSatish Balay #undef __FUNCT__ 18414a2ae208SSatish Balay #define __FUNCT__ "MatNorm_MPIAIJ" 1842dfbe8321SBarry Smith PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm) 1843855ac2c5SLois Curfman McInnes { 1844855ac2c5SLois Curfman McInnes Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 1845ec8511deSBarry Smith Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data; 1846dfbe8321SBarry Smith PetscErrorCode ierr; 1847d0f46423SBarry Smith PetscInt i,j,cstart = mat->cmap->rstart; 1848329f5518SBarry Smith PetscReal sum = 0.0; 1849a77337e4SBarry Smith MatScalar *v; 185004ca555eSLois Curfman McInnes 18513a40ed3dSBarry Smith PetscFunctionBegin; 185217699dbbSLois Curfman McInnes if (aij->size == 1) { 185314183eadSLois Curfman McInnes ierr = MatNorm(aij->A,type,norm);CHKERRQ(ierr); 185437fa93a5SLois Curfman McInnes } else { 185504ca555eSLois Curfman McInnes if (type == NORM_FROBENIUS) { 185604ca555eSLois Curfman McInnes v = amat->a; 185704ca555eSLois Curfman McInnes for (i=0; i<amat->nz; i++) { 1858329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 185904ca555eSLois Curfman McInnes } 186004ca555eSLois Curfman McInnes v = bmat->a; 186104ca555eSLois Curfman McInnes for (i=0; i<bmat->nz; i++) { 1862329f5518SBarry Smith sum += PetscRealPart(PetscConj(*v)*(*v)); v++; 186304ca555eSLois Curfman McInnes } 1864ce94432eSBarry Smith ierr = MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 18658f1a2a5eSBarry Smith *norm = PetscSqrtReal(*norm); 18663a40ed3dSBarry Smith } else if (type == NORM_1) { /* max column norm */ 1867329f5518SBarry Smith PetscReal *tmp,*tmp2; 1868b1d57f15SBarry Smith PetscInt *jj,*garray = aij->garray; 18691795a4d1SJed Brown ierr = PetscCalloc1((mat->cmap->N+1),&tmp);CHKERRQ(ierr); 1870785e854fSJed Brown ierr = PetscMalloc1((mat->cmap->N+1),&tmp2);CHKERRQ(ierr); 187104ca555eSLois Curfman McInnes *norm = 0.0; 187204ca555eSLois Curfman McInnes v = amat->a; jj = amat->j; 187304ca555eSLois Curfman McInnes for (j=0; j<amat->nz; j++) { 1874bfec09a0SHong Zhang tmp[cstart + *jj++] += PetscAbsScalar(*v); v++; 187504ca555eSLois Curfman McInnes } 187604ca555eSLois Curfman McInnes v = bmat->a; jj = bmat->j; 187704ca555eSLois Curfman McInnes for (j=0; j<bmat->nz; j++) { 1878bfec09a0SHong Zhang tmp[garray[*jj++]] += PetscAbsScalar(*v); v++; 187904ca555eSLois Curfman McInnes } 1880ce94432eSBarry Smith ierr = MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1881d0f46423SBarry Smith for (j=0; j<mat->cmap->N; j++) { 188204ca555eSLois Curfman McInnes if (tmp2[j] > *norm) *norm = tmp2[j]; 188304ca555eSLois Curfman McInnes } 1884606d414cSSatish Balay ierr = PetscFree(tmp);CHKERRQ(ierr); 1885606d414cSSatish Balay ierr = PetscFree(tmp2);CHKERRQ(ierr); 18863a40ed3dSBarry Smith } else if (type == NORM_INFINITY) { /* max row norm */ 1887329f5518SBarry Smith PetscReal ntemp = 0.0; 1888d0f46423SBarry Smith for (j=0; j<aij->A->rmap->n; j++) { 1889bfec09a0SHong Zhang v = amat->a + amat->i[j]; 189004ca555eSLois Curfman McInnes sum = 0.0; 189104ca555eSLois Curfman McInnes for (i=0; i<amat->i[j+1]-amat->i[j]; i++) { 1892cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 189304ca555eSLois Curfman McInnes } 1894bfec09a0SHong Zhang v = bmat->a + bmat->i[j]; 189504ca555eSLois Curfman McInnes for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) { 1896cddf8d76SBarry Smith sum += PetscAbsScalar(*v); v++; 189704ca555eSLois Curfman McInnes } 1898515d9167SLois Curfman McInnes if (sum > ntemp) ntemp = sum; 189904ca555eSLois Curfman McInnes } 1900ce94432eSBarry Smith ierr = MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));CHKERRQ(ierr); 1901ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for two norm"); 190237fa93a5SLois Curfman McInnes } 19033a40ed3dSBarry Smith PetscFunctionReturn(0); 1904855ac2c5SLois Curfman McInnes } 1905855ac2c5SLois Curfman McInnes 19064a2ae208SSatish Balay #undef __FUNCT__ 19074a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_MPIAIJ" 1908fc4dec0aSBarry Smith PetscErrorCode MatTranspose_MPIAIJ(Mat A,MatReuse reuse,Mat *matout) 1909b7c46309SBarry Smith { 1910b7c46309SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 1911da668accSHong Zhang Mat_SeqAIJ *Aloc=(Mat_SeqAIJ*)a->A->data,*Bloc=(Mat_SeqAIJ*)a->B->data; 1912dfbe8321SBarry Smith PetscErrorCode ierr; 191380bcc5a1SJed Brown PetscInt M = A->rmap->N,N = A->cmap->N,ma,na,mb,nb,*ai,*aj,*bi,*bj,row,*cols,*cols_tmp,i; 1914d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart,ncol; 19153a40ed3dSBarry Smith Mat B; 1916a77337e4SBarry Smith MatScalar *array; 1917b7c46309SBarry Smith 19183a40ed3dSBarry Smith PetscFunctionBegin; 1919ce94432eSBarry Smith if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place"); 1920da668accSHong Zhang 192180bcc5a1SJed Brown ma = A->rmap->n; na = A->cmap->n; mb = a->B->rmap->n; nb = a->B->cmap->n; 1922da668accSHong Zhang ai = Aloc->i; aj = Aloc->j; 1923da668accSHong Zhang bi = Bloc->i; bj = Bloc->j; 1924fc73b1b3SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout == A) { 192580bcc5a1SJed Brown PetscInt *d_nnz,*g_nnz,*o_nnz; 192680bcc5a1SJed Brown PetscSFNode *oloc; 1927713c93b4SJed Brown PETSC_UNUSED PetscSF sf; 192880bcc5a1SJed Brown 1929dcca6d9dSJed Brown ierr = PetscMalloc4(na,&d_nnz,na,&o_nnz,nb,&g_nnz,nb,&oloc);CHKERRQ(ierr); 193080bcc5a1SJed Brown /* compute d_nnz for preallocation */ 193180bcc5a1SJed Brown ierr = PetscMemzero(d_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 1932da668accSHong Zhang for (i=0; i<ai[ma]; i++) { 1933da668accSHong Zhang d_nnz[aj[i]]++; 1934da668accSHong Zhang aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1935d4bb536fSBarry Smith } 193680bcc5a1SJed Brown /* compute local off-diagonal contributions */ 19370beca09bSJed Brown ierr = PetscMemzero(g_nnz,nb*sizeof(PetscInt));CHKERRQ(ierr); 193880bcc5a1SJed Brown for (i=0; i<bi[ma]; i++) g_nnz[bj[i]]++; 193980bcc5a1SJed Brown /* map those to global */ 1940ce94432eSBarry Smith ierr = PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);CHKERRQ(ierr); 19410298fd71SBarry Smith ierr = PetscSFSetGraphLayout(sf,A->cmap,nb,NULL,PETSC_USE_POINTER,a->garray);CHKERRQ(ierr); 1942e9e74f11SJed Brown ierr = PetscSFSetFromOptions(sf);CHKERRQ(ierr); 194380bcc5a1SJed Brown ierr = PetscMemzero(o_nnz,na*sizeof(PetscInt));CHKERRQ(ierr); 194480bcc5a1SJed Brown ierr = PetscSFReduceBegin(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 194580bcc5a1SJed Brown ierr = PetscSFReduceEnd(sf,MPIU_INT,g_nnz,o_nnz,MPIU_SUM);CHKERRQ(ierr); 194680bcc5a1SJed Brown ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 1947d4bb536fSBarry Smith 1948ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr); 1949d0f46423SBarry Smith ierr = MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);CHKERRQ(ierr); 1950a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->cmap->bs,A->rmap->bs);CHKERRQ(ierr); 19517adad957SLisandro Dalcin ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 195280bcc5a1SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 195380bcc5a1SJed Brown ierr = PetscFree4(d_nnz,o_nnz,g_nnz,oloc);CHKERRQ(ierr); 1954fc4dec0aSBarry Smith } else { 1955fc4dec0aSBarry Smith B = *matout; 19566ffab4bbSHong Zhang ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 19572205254eSKarl Rupp for (i=0; i<ai[ma]; i++) aj[i] += cstart; /* global col index to be used by MatSetValues() */ 1958fc4dec0aSBarry Smith } 1959b7c46309SBarry Smith 1960b7c46309SBarry Smith /* copy over the A part */ 1961da668accSHong Zhang array = Aloc->a; 1962d0f46423SBarry Smith row = A->rmap->rstart; 1963da668accSHong Zhang for (i=0; i<ma; i++) { 1964da668accSHong Zhang ncol = ai[i+1]-ai[i]; 1965da668accSHong Zhang ierr = MatSetValues(B,ncol,aj,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 19662205254eSKarl Rupp row++; 19672205254eSKarl Rupp array += ncol; aj += ncol; 1968b7c46309SBarry Smith } 1969b7c46309SBarry Smith aj = Aloc->j; 1970da668accSHong Zhang for (i=0; i<ai[ma]; i++) aj[i] -= cstart; /* resume local col index */ 1971b7c46309SBarry Smith 1972b7c46309SBarry Smith /* copy over the B part */ 19731795a4d1SJed Brown ierr = PetscCalloc1(bi[mb],&cols);CHKERRQ(ierr); 1974da668accSHong Zhang array = Bloc->a; 1975d0f46423SBarry Smith row = A->rmap->rstart; 19762205254eSKarl Rupp for (i=0; i<bi[mb]; i++) cols[i] = a->garray[bj[i]]; 197761a2fbbaSHong Zhang cols_tmp = cols; 1978da668accSHong Zhang for (i=0; i<mb; i++) { 1979da668accSHong Zhang ncol = bi[i+1]-bi[i]; 198061a2fbbaSHong Zhang ierr = MatSetValues(B,ncol,cols_tmp,1,&row,array,INSERT_VALUES);CHKERRQ(ierr); 19812205254eSKarl Rupp row++; 19822205254eSKarl Rupp array += ncol; cols_tmp += ncol; 1983b7c46309SBarry Smith } 1984fc73b1b3SBarry Smith ierr = PetscFree(cols);CHKERRQ(ierr); 1985fc73b1b3SBarry Smith 19866d4a8577SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 19876d4a8577SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1988815cbec1SBarry Smith if (reuse == MAT_INITIAL_MATRIX || *matout != A) { 19890de55854SLois Curfman McInnes *matout = B; 19900de55854SLois Curfman McInnes } else { 1991eb6b5d47SBarry Smith ierr = MatHeaderMerge(A,B);CHKERRQ(ierr); 19920de55854SLois Curfman McInnes } 19933a40ed3dSBarry Smith PetscFunctionReturn(0); 1994b7c46309SBarry Smith } 1995b7c46309SBarry Smith 19964a2ae208SSatish Balay #undef __FUNCT__ 19974a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_MPIAIJ" 1998dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr) 1999a008b906SSatish Balay { 20004b967eb1SSatish Balay Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 20014b967eb1SSatish Balay Mat a = aij->A,b = aij->B; 2002dfbe8321SBarry Smith PetscErrorCode ierr; 2003b1d57f15SBarry Smith PetscInt s1,s2,s3; 2004a008b906SSatish Balay 20053a40ed3dSBarry Smith PetscFunctionBegin; 20064b967eb1SSatish Balay ierr = MatGetLocalSize(mat,&s2,&s3);CHKERRQ(ierr); 20074b967eb1SSatish Balay if (rr) { 2008e1311b90SBarry Smith ierr = VecGetLocalSize(rr,&s1);CHKERRQ(ierr); 2009e32f2f54SBarry Smith if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size"); 20104b967eb1SSatish Balay /* Overlap communication with computation. */ 2011ca9f406cSSatish Balay ierr = VecScatterBegin(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2012a008b906SSatish Balay } 20134b967eb1SSatish Balay if (ll) { 2014e1311b90SBarry Smith ierr = VecGetLocalSize(ll,&s1);CHKERRQ(ierr); 2015e32f2f54SBarry Smith if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size"); 2016f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,ll,0);CHKERRQ(ierr); 20174b967eb1SSatish Balay } 20184b967eb1SSatish Balay /* scale the diagonal block */ 2019f830108cSBarry Smith ierr = (*a->ops->diagonalscale)(a,ll,rr);CHKERRQ(ierr); 20204b967eb1SSatish Balay 20214b967eb1SSatish Balay if (rr) { 20224b967eb1SSatish Balay /* Do a scatter end and then right scale the off-diagonal block */ 2023ca9f406cSSatish Balay ierr = VecScatterEnd(aij->Mvctx,rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 2024f830108cSBarry Smith ierr = (*b->ops->diagonalscale)(b,0,aij->lvec);CHKERRQ(ierr); 20254b967eb1SSatish Balay } 20263a40ed3dSBarry Smith PetscFunctionReturn(0); 2027a008b906SSatish Balay } 2028a008b906SSatish Balay 20294a2ae208SSatish Balay #undef __FUNCT__ 20304a2ae208SSatish Balay #define __FUNCT__ "MatSetUnfactored_MPIAIJ" 2031dfbe8321SBarry Smith PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A) 2032bb5a7306SBarry Smith { 2033bb5a7306SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2034dfbe8321SBarry Smith PetscErrorCode ierr; 20353a40ed3dSBarry Smith 20363a40ed3dSBarry Smith PetscFunctionBegin; 2037bb5a7306SBarry Smith ierr = MatSetUnfactored(a->A);CHKERRQ(ierr); 20383a40ed3dSBarry Smith PetscFunctionReturn(0); 2039bb5a7306SBarry Smith } 2040bb5a7306SBarry Smith 20414a2ae208SSatish Balay #undef __FUNCT__ 20424a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAIJ" 2043ace3abfcSBarry Smith PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscBool *flag) 2044d4bb536fSBarry Smith { 2045d4bb536fSBarry Smith Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data; 2046d4bb536fSBarry Smith Mat a,b,c,d; 2047ace3abfcSBarry Smith PetscBool flg; 2048dfbe8321SBarry Smith PetscErrorCode ierr; 2049d4bb536fSBarry Smith 20503a40ed3dSBarry Smith PetscFunctionBegin; 2051d4bb536fSBarry Smith a = matA->A; b = matA->B; 2052d4bb536fSBarry Smith c = matB->A; d = matB->B; 2053d4bb536fSBarry Smith 2054d4bb536fSBarry Smith ierr = MatEqual(a,c,&flg);CHKERRQ(ierr); 2055abc0a331SBarry Smith if (flg) { 2056d4bb536fSBarry Smith ierr = MatEqual(b,d,&flg);CHKERRQ(ierr); 2057d4bb536fSBarry Smith } 2058ce94432eSBarry Smith ierr = MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));CHKERRQ(ierr); 20593a40ed3dSBarry Smith PetscFunctionReturn(0); 2060d4bb536fSBarry Smith } 2061d4bb536fSBarry Smith 20624a2ae208SSatish Balay #undef __FUNCT__ 20634a2ae208SSatish Balay #define __FUNCT__ "MatCopy_MPIAIJ" 2064dfbe8321SBarry Smith PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str) 2065cb5b572fSBarry Smith { 2066dfbe8321SBarry Smith PetscErrorCode ierr; 2067cb5b572fSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2068cb5b572fSBarry Smith Mat_MPIAIJ *b = (Mat_MPIAIJ*)B->data; 2069cb5b572fSBarry Smith 2070cb5b572fSBarry Smith PetscFunctionBegin; 207133f4a19fSKris Buschelman /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */ 207233f4a19fSKris Buschelman if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) { 2073cb5b572fSBarry Smith /* because of the column compression in the off-processor part of the matrix a->B, 2074cb5b572fSBarry Smith the number of columns in a->B and b->B may be different, hence we cannot call 2075cb5b572fSBarry Smith the MatCopy() directly on the two parts. If need be, we can provide a more 2076cb5b572fSBarry Smith efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices 2077cb5b572fSBarry Smith then copying the submatrices */ 2078cb5b572fSBarry Smith ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2079cb5b572fSBarry Smith } else { 2080cb5b572fSBarry Smith ierr = MatCopy(a->A,b->A,str);CHKERRQ(ierr); 2081cb5b572fSBarry Smith ierr = MatCopy(a->B,b->B,str);CHKERRQ(ierr); 2082cb5b572fSBarry Smith } 2083cb5b572fSBarry Smith PetscFunctionReturn(0); 2084cb5b572fSBarry Smith } 2085cb5b572fSBarry Smith 20864a2ae208SSatish Balay #undef __FUNCT__ 20874994cf47SJed Brown #define __FUNCT__ "MatSetUp_MPIAIJ" 20884994cf47SJed Brown PetscErrorCode MatSetUp_MPIAIJ(Mat A) 2089273d9f13SBarry Smith { 2090dfbe8321SBarry Smith PetscErrorCode ierr; 2091273d9f13SBarry Smith 2092273d9f13SBarry Smith PetscFunctionBegin; 2093273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);CHKERRQ(ierr); 2094273d9f13SBarry Smith PetscFunctionReturn(0); 2095273d9f13SBarry Smith } 2096273d9f13SBarry Smith 2097ac90fabeSBarry Smith #undef __FUNCT__ 209895b7e79eSJed Brown #define __FUNCT__ "MatAXPYGetPreallocation_MPIAIJ" 209995b7e79eSJed Brown /* This is the same as MatAXPYGetPreallocation_SeqAIJ, except that the local-to-global map is provided */ 210095b7e79eSJed Brown static PetscErrorCode MatAXPYGetPreallocation_MPIAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz) 210195b7e79eSJed Brown { 210295b7e79eSJed Brown PetscInt i,m=Y->rmap->N; 210395b7e79eSJed Brown Mat_SeqAIJ *x = (Mat_SeqAIJ*)X->data; 210495b7e79eSJed Brown Mat_SeqAIJ *y = (Mat_SeqAIJ*)Y->data; 210595b7e79eSJed Brown const PetscInt *xi = x->i,*yi = y->i; 210695b7e79eSJed Brown 210795b7e79eSJed Brown PetscFunctionBegin; 210895b7e79eSJed Brown /* Set the number of nonzeros in the new matrix */ 210995b7e79eSJed Brown for (i=0; i<m; i++) { 211095b7e79eSJed Brown PetscInt j,k,nzx = xi[i+1] - xi[i],nzy = yi[i+1] - yi[i]; 211195b7e79eSJed Brown const PetscInt *xj = x->j+xi[i],*yj = y->j+yi[i]; 211295b7e79eSJed Brown nnz[i] = 0; 211395b7e79eSJed Brown for (j=0,k=0; j<nzx; j++) { /* Point in X */ 211495b7e79eSJed Brown for (; k<nzy && yltog[yj[k]]<xltog[xj[j]]; k++) nnz[i]++; /* Catch up to X */ 211595b7e79eSJed Brown if (k<nzy && yltog[yj[k]]==xltog[xj[j]]) k++; /* Skip duplicate */ 211695b7e79eSJed Brown nnz[i]++; 211795b7e79eSJed Brown } 211895b7e79eSJed Brown for (; k<nzy; k++) nnz[i]++; 211995b7e79eSJed Brown } 212095b7e79eSJed Brown PetscFunctionReturn(0); 212195b7e79eSJed Brown } 212295b7e79eSJed Brown 212395b7e79eSJed Brown #undef __FUNCT__ 2124ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_MPIAIJ" 2125f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str) 2126ac90fabeSBarry Smith { 2127dfbe8321SBarry Smith PetscErrorCode ierr; 2128b1d57f15SBarry Smith PetscInt i; 2129ac90fabeSBarry Smith Mat_MPIAIJ *xx = (Mat_MPIAIJ*)X->data,*yy = (Mat_MPIAIJ*)Y->data; 21304ce68768SBarry Smith PetscBLASInt bnz,one=1; 2131ac90fabeSBarry Smith Mat_SeqAIJ *x,*y; 2132ac90fabeSBarry Smith 2133ac90fabeSBarry Smith PetscFunctionBegin; 2134ac90fabeSBarry Smith if (str == SAME_NONZERO_PATTERN) { 2135f4df32b1SMatthew Knepley PetscScalar alpha = a; 2136ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->A->data; 2137c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 2138ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->A->data; 21398b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2140ac90fabeSBarry Smith x = (Mat_SeqAIJ*)xx->B->data; 2141ac90fabeSBarry Smith y = (Mat_SeqAIJ*)yy->B->data; 2142c5df96a5SBarry Smith ierr = PetscBLASIntCast(x->nz,&bnz);CHKERRQ(ierr); 21438b83055fSJed Brown PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one)); 2144a30b2313SHong Zhang } else if (str == SUBSET_NONZERO_PATTERN) { 2145f4df32b1SMatthew Knepley ierr = MatAXPY_SeqAIJ(yy->A,a,xx->A,str);CHKERRQ(ierr); 2146c537a176SHong Zhang 2147c537a176SHong Zhang x = (Mat_SeqAIJ*)xx->B->data; 2148a30b2313SHong Zhang y = (Mat_SeqAIJ*)yy->B->data; 2149a30b2313SHong Zhang if (y->xtoy && y->XtoY != xx->B) { 2150a30b2313SHong Zhang ierr = PetscFree(y->xtoy);CHKERRQ(ierr); 21516bf464f9SBarry Smith ierr = MatDestroy(&y->XtoY);CHKERRQ(ierr); 2152c537a176SHong Zhang } 2153a30b2313SHong Zhang if (!y->xtoy) { /* get xtoy */ 2154d0f46423SBarry Smith ierr = MatAXPYGetxtoy_Private(xx->B->rmap->n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);CHKERRQ(ierr); 2155a30b2313SHong Zhang y->XtoY = xx->B; 2156407f6b05SHong Zhang ierr = PetscObjectReference((PetscObject)xx->B);CHKERRQ(ierr); 2157c537a176SHong Zhang } 2158f4df32b1SMatthew Knepley for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]); 2159ac90fabeSBarry Smith } else { 21609f5f6813SShri Abhyankar Mat B; 21619f5f6813SShri Abhyankar PetscInt *nnz_d,*nnz_o; 2162785e854fSJed Brown ierr = PetscMalloc1(yy->A->rmap->N,&nnz_d);CHKERRQ(ierr); 2163785e854fSJed Brown ierr = PetscMalloc1(yy->B->rmap->N,&nnz_o);CHKERRQ(ierr); 2164ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)Y),&B);CHKERRQ(ierr); 2165bc5a2726SShri Abhyankar ierr = PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);CHKERRQ(ierr); 21669f5f6813SShri Abhyankar ierr = MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);CHKERRQ(ierr); 2167a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,Y->rmap->bs,Y->cmap->bs);CHKERRQ(ierr); 21689f5f6813SShri Abhyankar ierr = MatSetType(B,MATMPIAIJ);CHKERRQ(ierr); 21699f5f6813SShri Abhyankar ierr = MatAXPYGetPreallocation_SeqAIJ(yy->A,xx->A,nnz_d);CHKERRQ(ierr); 217095b7e79eSJed Brown ierr = MatAXPYGetPreallocation_MPIAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);CHKERRQ(ierr); 2171ecd8bba6SJed Brown ierr = MatMPIAIJSetPreallocation(B,0,nnz_d,0,nnz_o);CHKERRQ(ierr); 21729f5f6813SShri Abhyankar ierr = MatAXPY_BasicWithPreallocation(B,Y,a,X,str);CHKERRQ(ierr); 2173a2ea699eSBarry Smith ierr = MatHeaderReplace(Y,B);CHKERRQ(ierr); 21749f5f6813SShri Abhyankar ierr = PetscFree(nnz_d);CHKERRQ(ierr); 21759f5f6813SShri Abhyankar ierr = PetscFree(nnz_o);CHKERRQ(ierr); 2176ac90fabeSBarry Smith } 2177ac90fabeSBarry Smith PetscFunctionReturn(0); 2178ac90fabeSBarry Smith } 2179ac90fabeSBarry Smith 21807087cfbeSBarry Smith extern PetscErrorCode MatConjugate_SeqAIJ(Mat); 2181354c94deSBarry Smith 2182354c94deSBarry Smith #undef __FUNCT__ 2183354c94deSBarry Smith #define __FUNCT__ "MatConjugate_MPIAIJ" 21847087cfbeSBarry Smith PetscErrorCode MatConjugate_MPIAIJ(Mat mat) 2185354c94deSBarry Smith { 2186354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX) 2187354c94deSBarry Smith PetscErrorCode ierr; 2188354c94deSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2189354c94deSBarry Smith 2190354c94deSBarry Smith PetscFunctionBegin; 2191354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->A);CHKERRQ(ierr); 2192354c94deSBarry Smith ierr = MatConjugate_SeqAIJ(aij->B);CHKERRQ(ierr); 2193354c94deSBarry Smith #else 2194354c94deSBarry Smith PetscFunctionBegin; 2195354c94deSBarry Smith #endif 2196354c94deSBarry Smith PetscFunctionReturn(0); 2197354c94deSBarry Smith } 2198354c94deSBarry Smith 219999cafbc1SBarry Smith #undef __FUNCT__ 220099cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_MPIAIJ" 220199cafbc1SBarry Smith PetscErrorCode MatRealPart_MPIAIJ(Mat A) 220299cafbc1SBarry Smith { 220399cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 220499cafbc1SBarry Smith PetscErrorCode ierr; 220599cafbc1SBarry Smith 220699cafbc1SBarry Smith PetscFunctionBegin; 220799cafbc1SBarry Smith ierr = MatRealPart(a->A);CHKERRQ(ierr); 220899cafbc1SBarry Smith ierr = MatRealPart(a->B);CHKERRQ(ierr); 220999cafbc1SBarry Smith PetscFunctionReturn(0); 221099cafbc1SBarry Smith } 221199cafbc1SBarry Smith 221299cafbc1SBarry Smith #undef __FUNCT__ 221399cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_MPIAIJ" 221499cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A) 221599cafbc1SBarry Smith { 221699cafbc1SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 221799cafbc1SBarry Smith PetscErrorCode ierr; 221899cafbc1SBarry Smith 221999cafbc1SBarry Smith PetscFunctionBegin; 222099cafbc1SBarry Smith ierr = MatImaginaryPart(a->A);CHKERRQ(ierr); 222199cafbc1SBarry Smith ierr = MatImaginaryPart(a->B);CHKERRQ(ierr); 222299cafbc1SBarry Smith PetscFunctionReturn(0); 222399cafbc1SBarry Smith } 222499cafbc1SBarry Smith 2225519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 2226103bf8bdSMatthew Knepley 2227103bf8bdSMatthew Knepley #include <boost/parallel/mpi/bsp_process_group.hpp> 2228a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_default_graph.hpp> 2229a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_0_block.hpp> 2230a2c909beSMatthew Knepley #include <boost/graph/distributed/ilu_preconditioner.hpp> 2231103bf8bdSMatthew Knepley #include <boost/graph/distributed/petsc/interface.hpp> 2232a2c909beSMatthew Knepley #include <boost/multi_array.hpp> 2233d0f46423SBarry Smith #include <boost/parallel/distributed_property_map->hpp> 2234103bf8bdSMatthew Knepley 2235103bf8bdSMatthew Knepley #undef __FUNCT__ 2236103bf8bdSMatthew Knepley #define __FUNCT__ "MatILUFactorSymbolic_MPIAIJ" 2237103bf8bdSMatthew Knepley /* 2238103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2239103bf8bdSMatthew Knepley */ 22400481f469SBarry Smith PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat fact,Mat A, IS isrow, IS iscol, const MatFactorInfo *info) 2241103bf8bdSMatthew Knepley { 2242a2c909beSMatthew Knepley namespace petsc = boost::distributed::petsc; 2243a2c909beSMatthew Knepley 2244a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2245a2c909beSMatthew Knepley using boost::graph::distributed::ilu_default::process_group_type; 2246a2c909beSMatthew Knepley using boost::graph::ilu_permuted; 2247a2c909beSMatthew Knepley 2248ace3abfcSBarry Smith PetscBool row_identity, col_identity; 2249776b82aeSLisandro Dalcin PetscContainer c; 2250103bf8bdSMatthew Knepley PetscInt m, n, M, N; 2251103bf8bdSMatthew Knepley PetscErrorCode ierr; 2252103bf8bdSMatthew Knepley 2253103bf8bdSMatthew Knepley PetscFunctionBegin; 2254e32f2f54SBarry Smith if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu"); 2255103bf8bdSMatthew Knepley ierr = ISIdentity(isrow, &row_identity);CHKERRQ(ierr); 2256103bf8bdSMatthew Knepley ierr = ISIdentity(iscol, &col_identity);CHKERRQ(ierr); 2257f23aa3ddSBarry Smith if (!row_identity || !col_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU"); 2258103bf8bdSMatthew Knepley 2259103bf8bdSMatthew Knepley process_group_type pg; 2260a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2261a2c909beSMatthew Knepley lgraph_type *lgraph_p = new lgraph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg)); 2262a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2263a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2264a2c909beSMatthew Knepley 2265103bf8bdSMatthew Knepley petsc::read_matrix(A, graph, get(boost::edge_weight, graph)); 2266a2c909beSMatthew Knepley ilu_permuted(level_graph); 2267103bf8bdSMatthew Knepley 2268103bf8bdSMatthew Knepley /* put together the new matrix */ 2269ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A), fact);CHKERRQ(ierr); 2270103bf8bdSMatthew Knepley ierr = MatGetLocalSize(A, &m, &n);CHKERRQ(ierr); 2271103bf8bdSMatthew Knepley ierr = MatGetSize(A, &M, &N);CHKERRQ(ierr); 2272719d5645SBarry Smith ierr = MatSetSizes(fact, m, n, M, N);CHKERRQ(ierr); 2273a2f3521dSMark F. Adams ierr = MatSetBlockSizes(fact,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 2274719d5645SBarry Smith ierr = MatSetType(fact, ((PetscObject)A)->type_name);CHKERRQ(ierr); 2275719d5645SBarry Smith ierr = MatAssemblyBegin(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2276719d5645SBarry Smith ierr = MatAssemblyEnd(fact, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2277103bf8bdSMatthew Knepley 2278ce94432eSBarry Smith ierr = PetscContainerCreate(PetscObjectComm((PetscObject)A), &c); 2279776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(c, lgraph_p); 2280719d5645SBarry Smith ierr = PetscObjectCompose((PetscObject) (fact), "graph", (PetscObject) c); 2281bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&c); 2282103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2283103bf8bdSMatthew Knepley } 2284103bf8bdSMatthew Knepley 2285103bf8bdSMatthew Knepley #undef __FUNCT__ 2286103bf8bdSMatthew Knepley #define __FUNCT__ "MatLUFactorNumeric_MPIAIJ" 22870481f469SBarry Smith PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat B,Mat A, const MatFactorInfo *info) 2288103bf8bdSMatthew Knepley { 2289103bf8bdSMatthew Knepley PetscFunctionBegin; 2290103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2291103bf8bdSMatthew Knepley } 2292103bf8bdSMatthew Knepley 2293103bf8bdSMatthew Knepley #undef __FUNCT__ 2294103bf8bdSMatthew Knepley #define __FUNCT__ "MatSolve_MPIAIJ" 2295103bf8bdSMatthew Knepley /* 2296103bf8bdSMatthew Knepley This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu> 2297103bf8bdSMatthew Knepley */ 2298103bf8bdSMatthew Knepley PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x) 2299103bf8bdSMatthew Knepley { 2300a2c909beSMatthew Knepley namespace graph_dist = boost::graph::distributed; 2301a2c909beSMatthew Knepley 2302a2c909beSMatthew Knepley typedef graph_dist::ilu_default::ilu_level_graph_type lgraph_type; 2303a2c909beSMatthew Knepley lgraph_type *lgraph_p; 2304776b82aeSLisandro Dalcin PetscContainer c; 2305103bf8bdSMatthew Knepley PetscErrorCode ierr; 2306103bf8bdSMatthew Knepley 2307103bf8bdSMatthew Knepley PetscFunctionBegin; 2308103bf8bdSMatthew Knepley ierr = PetscObjectQuery((PetscObject) A, "graph", (PetscObject*) &c);CHKERRQ(ierr); 2309776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(c, (void**) &lgraph_p);CHKERRQ(ierr); 2310103bf8bdSMatthew Knepley ierr = VecCopy(b, x);CHKERRQ(ierr); 2311a2c909beSMatthew Knepley 2312a2c909beSMatthew Knepley PetscScalar *array_x; 2313a2c909beSMatthew Knepley ierr = VecGetArray(x, &array_x);CHKERRQ(ierr); 2314a2c909beSMatthew Knepley PetscInt sx; 2315a2c909beSMatthew Knepley ierr = VecGetSize(x, &sx);CHKERRQ(ierr); 2316a2c909beSMatthew Knepley 2317a2c909beSMatthew Knepley PetscScalar *array_b; 2318a2c909beSMatthew Knepley ierr = VecGetArray(b, &array_b);CHKERRQ(ierr); 2319a2c909beSMatthew Knepley PetscInt sb; 2320a2c909beSMatthew Knepley ierr = VecGetSize(b, &sb);CHKERRQ(ierr); 2321a2c909beSMatthew Knepley 2322a2c909beSMatthew Knepley lgraph_type& level_graph = *lgraph_p; 2323a2c909beSMatthew Knepley graph_dist::ilu_default::graph_type& graph(level_graph.graph); 2324a2c909beSMatthew Knepley 2325a2c909beSMatthew Knepley typedef boost::multi_array_ref<PetscScalar, 1> array_ref_type; 23262205254eSKarl Rupp array_ref_type ref_b(array_b, boost::extents[num_vertices(graph)]); 23272205254eSKarl Rupp array_ref_type ref_x(array_x, boost::extents[num_vertices(graph)]); 2328a2c909beSMatthew Knepley 2329a2c909beSMatthew Knepley typedef boost::iterator_property_map<array_ref_type::iterator, 2330a2c909beSMatthew Knepley boost::property_map<graph_dist::ilu_default::graph_type, boost::vertex_index_t>::type> gvector_type; 23312205254eSKarl Rupp gvector_type vector_b(ref_b.begin(), get(boost::vertex_index, graph)); 23322205254eSKarl Rupp gvector_type vector_x(ref_x.begin(), get(boost::vertex_index, graph)); 2333a2c909beSMatthew Knepley 2334a2c909beSMatthew Knepley ilu_set_solve(*lgraph_p, vector_b, vector_x); 2335103bf8bdSMatthew Knepley PetscFunctionReturn(0); 2336103bf8bdSMatthew Knepley } 2337103bf8bdSMatthew Knepley #endif 2338103bf8bdSMatthew Knepley 233969db28dcSHong Zhang #undef __FUNCT__ 23405cc03489SHong Zhang #define __FUNCT__ "MatDestroy_MatRedundant" 23415cc03489SHong Zhang PetscErrorCode MatDestroy_MatRedundant(Mat A) 234269db28dcSHong Zhang { 234369db28dcSHong Zhang PetscErrorCode ierr; 23445cc03489SHong Zhang Mat_Redundant *redund; 234569db28dcSHong Zhang PetscInt i; 23465cc03489SHong Zhang PetscMPIInt size; 234769db28dcSHong Zhang 234869db28dcSHong Zhang PetscFunctionBegin; 23495cc03489SHong Zhang ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr); 23505cc03489SHong Zhang if (size == 1) { 23515cc03489SHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 23525cc03489SHong Zhang redund = a->redundant; 23535cc03489SHong Zhang } else { 23545cc03489SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 23555cc03489SHong Zhang redund = a->redundant; 23565cc03489SHong Zhang } 23575cc03489SHong Zhang if (redund){ 2358c79c5527SHong Zhang if (redund->matseq) { /* via MatGetSubMatrices() */ 23594388c78fSHong Zhang ierr = ISDestroy(&redund->isrow);CHKERRQ(ierr); 23604388c78fSHong Zhang ierr = ISDestroy(&redund->iscol);CHKERRQ(ierr); 23614388c78fSHong Zhang ierr = MatDestroy(&redund->matseq[0]);CHKERRQ(ierr); 23624388c78fSHong Zhang ierr = PetscFree(redund->matseq);CHKERRQ(ierr); 23634388c78fSHong Zhang } else { 23641d79065fSBarry Smith ierr = PetscFree2(redund->send_rank,redund->recv_rank);CHKERRQ(ierr); 236569db28dcSHong Zhang ierr = PetscFree(redund->sbuf_j);CHKERRQ(ierr); 236669db28dcSHong Zhang ierr = PetscFree(redund->sbuf_a);CHKERRQ(ierr); 236769db28dcSHong Zhang for (i=0; i<redund->nrecvs; i++) { 236869db28dcSHong Zhang ierr = PetscFree(redund->rbuf_j[i]);CHKERRQ(ierr); 236969db28dcSHong Zhang ierr = PetscFree(redund->rbuf_a[i]);CHKERRQ(ierr); 237069db28dcSHong Zhang } 23711d79065fSBarry Smith ierr = PetscFree4(redund->sbuf_nz,redund->rbuf_nz,redund->rbuf_j,redund->rbuf_a);CHKERRQ(ierr); 2372c79c5527SHong Zhang } 23730b291e46SHong Zhang 23740b291e46SHong Zhang if (redund->psubcomm) { 23750b291e46SHong Zhang ierr = PetscSubcommDestroy(&redund->psubcomm);CHKERRQ(ierr); 23760b291e46SHong Zhang } 23775cc03489SHong Zhang ierr = redund->Destroy(A);CHKERRQ(ierr); 237869db28dcSHong Zhang ierr = PetscFree(redund);CHKERRQ(ierr); 2379bf0cc555SLisandro Dalcin } 238069db28dcSHong Zhang PetscFunctionReturn(0); 238169db28dcSHong Zhang } 238269db28dcSHong Zhang 238369db28dcSHong Zhang #undef __FUNCT__ 238422559b1cSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ_interlaced" 23857cb6ea77SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ_interlaced(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 2386b4617e5dSHong Zhang { 2387b4617e5dSHong Zhang PetscMPIInt rank,size; 23887cb6ea77SHong Zhang MPI_Comm comm; 2389b4617e5dSHong Zhang PetscErrorCode ierr; 239034d19554SHong Zhang PetscInt nsends=0,nrecvs=0,i,rownz_max=0,M=mat->rmap->N,N=mat->cmap->N; 23915cc03489SHong Zhang PetscMPIInt *send_rank= NULL,*recv_rank=NULL,subrank,subsize; 2392b4617e5dSHong Zhang PetscInt *rowrange = mat->rmap->range; 2393b4617e5dSHong Zhang Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 2394b4617e5dSHong Zhang Mat A = aij->A,B=aij->B,C=*matredundant; 2395b4617e5dSHong Zhang Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data,*b=(Mat_SeqAIJ*)B->data; 2396b4617e5dSHong Zhang PetscScalar *sbuf_a; 2397b4617e5dSHong Zhang PetscInt nzlocal=a->nz+b->nz; 2398b4617e5dSHong Zhang PetscInt j,cstart=mat->cmap->rstart,cend=mat->cmap->rend,row,nzA,nzB,ncols,*cworkA,*cworkB; 239934d19554SHong Zhang PetscInt rstart=mat->rmap->rstart,rend=mat->rmap->rend,*bmap=aij->garray; 2400b4617e5dSHong Zhang PetscInt *cols,ctmp,lwrite,*rptr,l,*sbuf_j; 2401b4617e5dSHong Zhang MatScalar *aworkA,*aworkB; 2402b4617e5dSHong Zhang PetscScalar *vals; 2403b4617e5dSHong Zhang PetscMPIInt tag1,tag2,tag3,imdex; 2404b4617e5dSHong Zhang MPI_Request *s_waits1=NULL,*s_waits2=NULL,*s_waits3=NULL; 2405b4617e5dSHong Zhang MPI_Request *r_waits1=NULL,*r_waits2=NULL,*r_waits3=NULL; 2406b4617e5dSHong Zhang MPI_Status recv_status,*send_status; 2407b4617e5dSHong Zhang PetscInt *sbuf_nz=NULL,*rbuf_nz=NULL,count; 2408b4617e5dSHong Zhang PetscInt **rbuf_j=NULL; 2409b4617e5dSHong Zhang PetscScalar **rbuf_a=NULL; 2410b4617e5dSHong Zhang Mat_Redundant *redund =NULL; 2411b4617e5dSHong Zhang 2412b4617e5dSHong Zhang PetscFunctionBegin; 2413b4617e5dSHong Zhang ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 2414b4617e5dSHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 2415b4617e5dSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 24165cc03489SHong Zhang ierr = MPI_Comm_rank(subcomm,&subrank);CHKERRQ(ierr); 24175cc03489SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2418d3b23db5SHong Zhang 2419b4617e5dSHong Zhang if (reuse == MAT_REUSE_MATRIX) { 2420b4617e5dSHong Zhang if (M != mat->rmap->N || N != mat->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong global size"); 24215cc03489SHong Zhang if (subsize == 1) { 24225cc03489SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 24235cc03489SHong Zhang redund = c->redundant; 24245cc03489SHong Zhang } else { 24255cc03489SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 24265cc03489SHong Zhang redund = c->redundant; 24275cc03489SHong Zhang } 2428b4617e5dSHong Zhang if (nzlocal != redund->nzlocal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. Wrong nzlocal"); 2429b4617e5dSHong Zhang 2430b4617e5dSHong Zhang nsends = redund->nsends; 2431b4617e5dSHong Zhang nrecvs = redund->nrecvs; 2432b4617e5dSHong Zhang send_rank = redund->send_rank; 2433b4617e5dSHong Zhang recv_rank = redund->recv_rank; 2434b4617e5dSHong Zhang sbuf_nz = redund->sbuf_nz; 2435b4617e5dSHong Zhang rbuf_nz = redund->rbuf_nz; 2436b4617e5dSHong Zhang sbuf_j = redund->sbuf_j; 2437b4617e5dSHong Zhang sbuf_a = redund->sbuf_a; 2438b4617e5dSHong Zhang rbuf_j = redund->rbuf_j; 2439b4617e5dSHong Zhang rbuf_a = redund->rbuf_a; 2440b4617e5dSHong Zhang } 2441b4617e5dSHong Zhang 2442b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2443b4617e5dSHong Zhang PetscInt nleftover,np_subcomm; 2444b4617e5dSHong Zhang 2445b4617e5dSHong Zhang /* get the destination processors' id send_rank, nsends and nrecvs */ 2446dcca6d9dSJed Brown ierr = PetscMalloc2(size,&send_rank,size,&recv_rank);CHKERRQ(ierr); 2447b4617e5dSHong Zhang 2448b4617e5dSHong Zhang np_subcomm = size/nsubcomm; 2449b4617e5dSHong Zhang nleftover = size - nsubcomm*np_subcomm; 2450b4617e5dSHong Zhang 245122559b1cSHong Zhang /* block of codes below is specific for INTERLACED */ 245222559b1cSHong Zhang /* ------------------------------------------------*/ 2453b4617e5dSHong Zhang nsends = 0; nrecvs = 0; 2454b4617e5dSHong Zhang for (i=0; i<size; i++) { 2455b4617e5dSHong Zhang if (subrank == i/nsubcomm && i != rank) { /* my_subrank == other's subrank */ 245622559b1cSHong Zhang send_rank[nsends++] = i; 2457b4617e5dSHong Zhang recv_rank[nrecvs++] = i; 2458b4617e5dSHong Zhang } 2459b4617e5dSHong Zhang } 2460b4617e5dSHong Zhang if (rank >= size - nleftover) { /* this proc is a leftover processor */ 2461b4617e5dSHong Zhang i = size-nleftover-1; 2462b4617e5dSHong Zhang j = 0; 2463b4617e5dSHong Zhang while (j < nsubcomm - nleftover) { 2464b4617e5dSHong Zhang send_rank[nsends++] = i; 2465b4617e5dSHong Zhang i--; j++; 2466b4617e5dSHong Zhang } 2467b4617e5dSHong Zhang } 2468b4617e5dSHong Zhang 2469b4617e5dSHong Zhang if (nleftover && subsize == size/nsubcomm && subrank==subsize-1) { /* this proc recvs from leftover processors */ 2470b4617e5dSHong Zhang for (i=0; i<nleftover; i++) { 2471b4617e5dSHong Zhang recv_rank[nrecvs++] = size-nleftover+i; 2472b4617e5dSHong Zhang } 2473b4617e5dSHong Zhang } 247422559b1cSHong Zhang /*----------------------------------------------*/ 2475b4617e5dSHong Zhang 2476b4617e5dSHong Zhang /* allocate sbuf_j, sbuf_a */ 2477b4617e5dSHong Zhang i = nzlocal + rowrange[rank+1] - rowrange[rank] + 2; 2478785e854fSJed Brown ierr = PetscMalloc1(i,&sbuf_j);CHKERRQ(ierr); 2479785e854fSJed Brown ierr = PetscMalloc1((nzlocal+1),&sbuf_a);CHKERRQ(ierr); 2480e37c6257SHong Zhang /* 2481e37c6257SHong Zhang ierr = PetscSynchronizedPrintf(comm,"[%d] nsends %d, nrecvs %d\n",rank,nsends,nrecvs);CHKERRQ(ierr); 24820ec8b6e3SBarry Smith ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr); 2483e37c6257SHong Zhang */ 2484b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2485b4617e5dSHong Zhang 2486b4617e5dSHong Zhang /* copy mat's local entries into the buffers */ 2487b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2488b4617e5dSHong Zhang rownz_max = 0; 2489b4617e5dSHong Zhang rptr = sbuf_j; 2490b4617e5dSHong Zhang cols = sbuf_j + rend-rstart + 1; 2491b4617e5dSHong Zhang vals = sbuf_a; 2492b4617e5dSHong Zhang rptr[0] = 0; 2493b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2494b4617e5dSHong Zhang row = i + rstart; 2495b4617e5dSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 2496b4617e5dSHong Zhang ncols = nzA + nzB; 2497b4617e5dSHong Zhang cworkA = a->j + a->i[i]; cworkB = b->j + b->i[i]; 2498b4617e5dSHong Zhang aworkA = a->a + a->i[i]; aworkB = b->a + b->i[i]; 2499b4617e5dSHong Zhang /* load the column indices for this row into cols */ 2500b4617e5dSHong Zhang lwrite = 0; 2501b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2502b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) { 2503b4617e5dSHong Zhang vals[lwrite] = aworkB[l]; 2504b4617e5dSHong Zhang cols[lwrite++] = ctmp; 2505b4617e5dSHong Zhang } 2506b4617e5dSHong Zhang } 2507b4617e5dSHong Zhang for (l=0; l<nzA; l++) { 2508b4617e5dSHong Zhang vals[lwrite] = aworkA[l]; 2509b4617e5dSHong Zhang cols[lwrite++] = cstart + cworkA[l]; 2510b4617e5dSHong Zhang } 2511b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2512b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) { 2513b4617e5dSHong Zhang vals[lwrite] = aworkB[l]; 2514b4617e5dSHong Zhang cols[lwrite++] = ctmp; 2515b4617e5dSHong Zhang } 2516b4617e5dSHong Zhang } 2517b4617e5dSHong Zhang vals += ncols; 2518b4617e5dSHong Zhang cols += ncols; 2519b4617e5dSHong Zhang rptr[i+1] = rptr[i] + ncols; 2520b4617e5dSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 2521b4617e5dSHong Zhang } 2522b4617e5dSHong Zhang if (rptr[rend-rstart] != a->nz + b->nz) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "rptr[%d] %d != %d + %d",rend-rstart,rptr[rend-rstart+1],a->nz,b->nz); 2523b4617e5dSHong Zhang } else { /* only copy matrix values into sbuf_a */ 2524b4617e5dSHong Zhang rptr = sbuf_j; 2525b4617e5dSHong Zhang vals = sbuf_a; 2526b4617e5dSHong Zhang rptr[0] = 0; 2527b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2528b4617e5dSHong Zhang row = i + rstart; 2529b4617e5dSHong Zhang nzA = a->i[i+1] - a->i[i]; nzB = b->i[i+1] - b->i[i]; 2530b4617e5dSHong Zhang ncols = nzA + nzB; 2531b4617e5dSHong Zhang cworkB = b->j + b->i[i]; 2532b4617e5dSHong Zhang aworkA = a->a + a->i[i]; 2533b4617e5dSHong Zhang aworkB = b->a + b->i[i]; 2534b4617e5dSHong Zhang lwrite = 0; 2535b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2536b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) < cstart) vals[lwrite++] = aworkB[l]; 2537b4617e5dSHong Zhang } 2538b4617e5dSHong Zhang for (l=0; l<nzA; l++) vals[lwrite++] = aworkA[l]; 2539b4617e5dSHong Zhang for (l=0; l<nzB; l++) { 2540b4617e5dSHong Zhang if ((ctmp = bmap[cworkB[l]]) >= cend) vals[lwrite++] = aworkB[l]; 2541b4617e5dSHong Zhang } 2542b4617e5dSHong Zhang vals += ncols; 2543b4617e5dSHong Zhang rptr[i+1] = rptr[i] + ncols; 2544b4617e5dSHong Zhang } 2545b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2546b4617e5dSHong Zhang 2547b4617e5dSHong Zhang /* send nzlocal to others, and recv other's nzlocal */ 2548b4617e5dSHong Zhang /*--------------------------------------------------*/ 2549b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2550dcca6d9dSJed Brown ierr = PetscMalloc2(3*(nsends + nrecvs)+1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr); 2551b4617e5dSHong Zhang 2552b4617e5dSHong Zhang s_waits2 = s_waits3 + nsends; 2553b4617e5dSHong Zhang s_waits1 = s_waits2 + nsends; 2554b4617e5dSHong Zhang r_waits1 = s_waits1 + nsends; 2555b4617e5dSHong Zhang r_waits2 = r_waits1 + nrecvs; 2556b4617e5dSHong Zhang r_waits3 = r_waits2 + nrecvs; 2557b4617e5dSHong Zhang } else { 2558dcca6d9dSJed Brown ierr = PetscMalloc2(nsends + nrecvs +1,&s_waits3,nsends+1,&send_status);CHKERRQ(ierr); 2559b4617e5dSHong Zhang 2560b4617e5dSHong Zhang r_waits3 = s_waits3 + nsends; 2561b4617e5dSHong Zhang } 2562b4617e5dSHong Zhang 2563b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag3);CHKERRQ(ierr); 2564b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2565b4617e5dSHong Zhang /* get new tags to keep the communication clean */ 2566b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag1);CHKERRQ(ierr); 2567b4617e5dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)mat,&tag2);CHKERRQ(ierr); 2568dcca6d9dSJed Brown ierr = PetscMalloc4(nsends,&sbuf_nz,nrecvs,&rbuf_nz,nrecvs,&rbuf_j,nrecvs,&rbuf_a);CHKERRQ(ierr); 2569b4617e5dSHong Zhang 2570b4617e5dSHong Zhang /* post receives of other's nzlocal */ 2571b4617e5dSHong Zhang for (i=0; i<nrecvs; i++) { 2572b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_nz+i,1,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,r_waits1+i);CHKERRQ(ierr); 2573b4617e5dSHong Zhang } 2574b4617e5dSHong Zhang /* send nzlocal to others */ 2575b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2576b4617e5dSHong Zhang sbuf_nz[i] = nzlocal; 2577b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_nz+i,1,MPIU_INT,send_rank[i],tag1,comm,s_waits1+i);CHKERRQ(ierr); 2578b4617e5dSHong Zhang } 2579b4617e5dSHong Zhang /* wait on receives of nzlocal; allocate space for rbuf_j, rbuf_a */ 2580b4617e5dSHong Zhang count = nrecvs; 2581b4617e5dSHong Zhang while (count) { 2582b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits1,&imdex,&recv_status);CHKERRQ(ierr); 2583b4617e5dSHong Zhang 2584b4617e5dSHong Zhang recv_rank[imdex] = recv_status.MPI_SOURCE; 2585b4617e5dSHong Zhang /* allocate rbuf_a and rbuf_j; then post receives of rbuf_j */ 2586785e854fSJed Brown ierr = PetscMalloc1((rbuf_nz[imdex]+1),&rbuf_a[imdex]);CHKERRQ(ierr); 2587b4617e5dSHong Zhang 2588b4617e5dSHong Zhang i = rowrange[recv_status.MPI_SOURCE+1] - rowrange[recv_status.MPI_SOURCE]; /* number of expected mat->i */ 2589b4617e5dSHong Zhang 2590b4617e5dSHong Zhang rbuf_nz[imdex] += i + 2; 2591b4617e5dSHong Zhang 2592785e854fSJed Brown ierr = PetscMalloc1(rbuf_nz[imdex],&rbuf_j[imdex]);CHKERRQ(ierr); 2593b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_j[imdex],rbuf_nz[imdex],MPIU_INT,recv_status.MPI_SOURCE,tag2,comm,r_waits2+imdex);CHKERRQ(ierr); 2594b4617e5dSHong Zhang count--; 2595b4617e5dSHong Zhang } 2596b4617e5dSHong Zhang /* wait on sends of nzlocal */ 2597b4617e5dSHong Zhang if (nsends) {ierr = MPI_Waitall(nsends,s_waits1,send_status);CHKERRQ(ierr);} 2598b4617e5dSHong Zhang /* send mat->i,j to others, and recv from other's */ 2599b4617e5dSHong Zhang /*------------------------------------------------*/ 2600b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2601b4617e5dSHong Zhang j = nzlocal + rowrange[rank+1] - rowrange[rank] + 1; 2602b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_j,j,MPIU_INT,send_rank[i],tag2,comm,s_waits2+i);CHKERRQ(ierr); 2603b4617e5dSHong Zhang } 2604b4617e5dSHong Zhang /* wait on receives of mat->i,j */ 2605b4617e5dSHong Zhang /*------------------------------*/ 2606b4617e5dSHong Zhang count = nrecvs; 2607b4617e5dSHong Zhang while (count) { 2608b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits2,&imdex,&recv_status);CHKERRQ(ierr); 2609b4617e5dSHong Zhang if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 2610b4617e5dSHong Zhang count--; 2611b4617e5dSHong Zhang } 2612b4617e5dSHong Zhang /* wait on sends of mat->i,j */ 2613b4617e5dSHong Zhang /*---------------------------*/ 2614b4617e5dSHong Zhang if (nsends) { 2615b4617e5dSHong Zhang ierr = MPI_Waitall(nsends,s_waits2,send_status);CHKERRQ(ierr); 2616b4617e5dSHong Zhang } 2617b4617e5dSHong Zhang } /* endof if (reuse == MAT_INITIAL_MATRIX) */ 2618b4617e5dSHong Zhang 2619b4617e5dSHong Zhang /* post receives, send and receive mat->a */ 2620b4617e5dSHong Zhang /*----------------------------------------*/ 2621b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2622b4617e5dSHong Zhang ierr = MPI_Irecv(rbuf_a[imdex],rbuf_nz[imdex],MPIU_SCALAR,recv_rank[imdex],tag3,comm,r_waits3+imdex);CHKERRQ(ierr); 2623b4617e5dSHong Zhang } 2624b4617e5dSHong Zhang for (i=0; i<nsends; i++) { 2625b4617e5dSHong Zhang ierr = MPI_Isend(sbuf_a,nzlocal,MPIU_SCALAR,send_rank[i],tag3,comm,s_waits3+i);CHKERRQ(ierr); 2626b4617e5dSHong Zhang } 2627b4617e5dSHong Zhang count = nrecvs; 2628b4617e5dSHong Zhang while (count) { 2629b4617e5dSHong Zhang ierr = MPI_Waitany(nrecvs,r_waits3,&imdex,&recv_status);CHKERRQ(ierr); 2630b4617e5dSHong Zhang if (recv_rank[imdex] != recv_status.MPI_SOURCE) SETERRQ2(PETSC_COMM_SELF,1, "recv_rank %d != MPI_SOURCE %d",recv_rank[imdex],recv_status.MPI_SOURCE); 2631b4617e5dSHong Zhang count--; 2632b4617e5dSHong Zhang } 2633b4617e5dSHong Zhang if (nsends) { 2634b4617e5dSHong Zhang ierr = MPI_Waitall(nsends,s_waits3,send_status);CHKERRQ(ierr); 2635b4617e5dSHong Zhang } 2636b4617e5dSHong Zhang 2637b4617e5dSHong Zhang ierr = PetscFree2(s_waits3,send_status);CHKERRQ(ierr); 2638b4617e5dSHong Zhang 2639b4617e5dSHong Zhang /* create redundant matrix */ 2640b4617e5dSHong Zhang /*-------------------------*/ 2641b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 264219171117SHong Zhang const PetscInt *range; 264319171117SHong Zhang PetscInt rstart_sub,rend_sub,mloc_sub; 264419171117SHong Zhang 2645b4617e5dSHong Zhang /* compute rownz_max for preallocation */ 2646b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2647b4617e5dSHong Zhang j = rowrange[recv_rank[imdex]+1] - rowrange[recv_rank[imdex]]; 2648b4617e5dSHong Zhang rptr = rbuf_j[imdex]; 2649b4617e5dSHong Zhang for (i=0; i<j; i++) { 2650b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2651b4617e5dSHong Zhang if (rownz_max < ncols) rownz_max = ncols; 2652b4617e5dSHong Zhang } 2653b4617e5dSHong Zhang } 2654b4617e5dSHong Zhang 2655b4617e5dSHong Zhang ierr = MatCreate(subcomm,&C);CHKERRQ(ierr); 265619171117SHong Zhang 265719171117SHong Zhang /* get local size of redundant matrix 265819171117SHong Zhang - mloc_sub is chosen for PETSC_SUBCOMM_INTERLACED, works for other types, but may not efficient! */ 265919171117SHong Zhang ierr = MatGetOwnershipRanges(mat,&range);CHKERRQ(ierr); 266019171117SHong Zhang rstart_sub = range[nsubcomm*subrank]; 266119171117SHong Zhang if (subrank+1 < subsize) { /* not the last proc in subcomm */ 266219171117SHong Zhang rend_sub = range[nsubcomm*(subrank+1)]; 266319171117SHong Zhang } else { 266419171117SHong Zhang rend_sub = mat->rmap->N; 266519171117SHong Zhang } 266619171117SHong Zhang mloc_sub = rend_sub - rstart_sub; 266719171117SHong Zhang 266834d19554SHong Zhang if (M == N) { 2669b4617e5dSHong Zhang ierr = MatSetSizes(C,mloc_sub,mloc_sub,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); 267034d19554SHong Zhang } else { /* non-square matrix */ 267134d19554SHong Zhang ierr = MatSetSizes(C,mloc_sub,PETSC_DECIDE,PETSC_DECIDE,mat->cmap->N);CHKERRQ(ierr); 267234d19554SHong Zhang } 2673b4617e5dSHong Zhang ierr = MatSetBlockSizes(C,mat->rmap->bs,mat->cmap->bs);CHKERRQ(ierr); 2674b4617e5dSHong Zhang ierr = MatSetFromOptions(C);CHKERRQ(ierr); 2675b4617e5dSHong Zhang ierr = MatSeqAIJSetPreallocation(C,rownz_max,NULL);CHKERRQ(ierr); 2676b4617e5dSHong Zhang ierr = MatMPIAIJSetPreallocation(C,rownz_max,NULL,rownz_max,NULL);CHKERRQ(ierr); 2677b4617e5dSHong Zhang } else { 2678b4617e5dSHong Zhang C = *matredundant; 2679b4617e5dSHong Zhang } 2680b4617e5dSHong Zhang 2681b4617e5dSHong Zhang /* insert local matrix entries */ 2682b4617e5dSHong Zhang rptr = sbuf_j; 2683b4617e5dSHong Zhang cols = sbuf_j + rend-rstart + 1; 2684b4617e5dSHong Zhang vals = sbuf_a; 2685b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2686b4617e5dSHong Zhang row = i + rstart; 2687b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2688b4617e5dSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 2689b4617e5dSHong Zhang vals += ncols; 2690b4617e5dSHong Zhang cols += ncols; 2691b4617e5dSHong Zhang } 2692b4617e5dSHong Zhang /* insert received matrix entries */ 2693b4617e5dSHong Zhang for (imdex=0; imdex<nrecvs; imdex++) { 2694b4617e5dSHong Zhang rstart = rowrange[recv_rank[imdex]]; 2695b4617e5dSHong Zhang rend = rowrange[recv_rank[imdex]+1]; 2696e37c6257SHong Zhang /* printf("[%d] insert rows %d - %d\n",rank,rstart,rend-1); */ 2697b4617e5dSHong Zhang rptr = rbuf_j[imdex]; 2698b4617e5dSHong Zhang cols = rbuf_j[imdex] + rend-rstart + 1; 2699b4617e5dSHong Zhang vals = rbuf_a[imdex]; 2700b4617e5dSHong Zhang for (i=0; i<rend-rstart; i++) { 2701b4617e5dSHong Zhang row = i + rstart; 2702b4617e5dSHong Zhang ncols = rptr[i+1] - rptr[i]; 2703b4617e5dSHong Zhang ierr = MatSetValues(C,1,&row,ncols,cols,vals,INSERT_VALUES);CHKERRQ(ierr); 2704b4617e5dSHong Zhang vals += ncols; 2705b4617e5dSHong Zhang cols += ncols; 2706b4617e5dSHong Zhang } 2707b4617e5dSHong Zhang } 2708b4617e5dSHong Zhang ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2709b4617e5dSHong Zhang ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2710b4617e5dSHong Zhang 2711b4617e5dSHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2712b4617e5dSHong Zhang *matredundant = C; 27135cc03489SHong Zhang 2714b4617e5dSHong Zhang /* create a supporting struct and attach it to C for reuse */ 2715b00a9115SJed Brown ierr = PetscNewLog(C,&redund);CHKERRQ(ierr); 27165cc03489SHong Zhang if (subsize == 1) { 27175cc03489SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)C->data; 27185cc03489SHong Zhang c->redundant = redund; 27195cc03489SHong Zhang } else { 27205cc03489SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)C->data; 27215cc03489SHong Zhang c->redundant = redund; 27225cc03489SHong Zhang } 2723b4617e5dSHong Zhang 2724b4617e5dSHong Zhang redund->nzlocal = nzlocal; 2725b4617e5dSHong Zhang redund->nsends = nsends; 2726b4617e5dSHong Zhang redund->nrecvs = nrecvs; 2727b4617e5dSHong Zhang redund->send_rank = send_rank; 2728b4617e5dSHong Zhang redund->recv_rank = recv_rank; 2729b4617e5dSHong Zhang redund->sbuf_nz = sbuf_nz; 2730b4617e5dSHong Zhang redund->rbuf_nz = rbuf_nz; 2731b4617e5dSHong Zhang redund->sbuf_j = sbuf_j; 2732b4617e5dSHong Zhang redund->sbuf_a = sbuf_a; 2733b4617e5dSHong Zhang redund->rbuf_j = rbuf_j; 2734b4617e5dSHong Zhang redund->rbuf_a = rbuf_a; 27350b291e46SHong Zhang redund->psubcomm = NULL; 2736b4617e5dSHong Zhang 2737b4617e5dSHong Zhang redund->Destroy = C->ops->destroy; 2738b4617e5dSHong Zhang C->ops->destroy = MatDestroy_MatRedundant; 2739b4617e5dSHong Zhang } 2740b4617e5dSHong Zhang PetscFunctionReturn(0); 2741b4617e5dSHong Zhang } 2742b4617e5dSHong Zhang 2743b4617e5dSHong Zhang #undef __FUNCT__ 274469db28dcSHong Zhang #define __FUNCT__ "MatGetRedundantMatrix_MPIAIJ" 2745b2bf6370SHong Zhang PetscErrorCode MatGetRedundantMatrix_MPIAIJ(Mat mat,PetscInt nsubcomm,MPI_Comm subcomm,MatReuse reuse,Mat *matredundant) 274669db28dcSHong Zhang { 2747f38d543fSHong Zhang PetscErrorCode ierr; 2748c79c5527SHong Zhang MPI_Comm comm; 2749c79c5527SHong Zhang PetscMPIInt size,subsize; 2750c79c5527SHong Zhang PetscInt mloc_sub,rstart,rend,M=mat->rmap->N,N=mat->cmap->N; 2751c79c5527SHong Zhang Mat_Redundant *redund=NULL; 27521f2d8ef4SHong Zhang PetscSubcomm psubcomm=NULL; 2753473f7991SHong Zhang MPI_Comm subcomm_in=subcomm; 27541f2d8ef4SHong Zhang Mat *matseq; 27551f2d8ef4SHong Zhang IS isrow,iscol; 275669db28dcSHong Zhang 275769db28dcSHong Zhang PetscFunctionBegin; 27581f2d8ef4SHong Zhang if (subcomm_in == MPI_COMM_NULL) { /* user does not provide subcomm */ 2759c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 27601f2d8ef4SHong Zhang /* create psubcomm, then get subcomm */ 2761ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 276269db28dcSHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 27637cb6ea77SHong Zhang if (nsubcomm < 1 || nsubcomm > size) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"nsubcomm must between 1 and %D",size); 27647cb6ea77SHong Zhang 2765d3b23db5SHong Zhang ierr = PetscSubcommCreate(comm,&psubcomm);CHKERRQ(ierr); 2766d3b23db5SHong Zhang ierr = PetscSubcommSetNumber(psubcomm,nsubcomm);CHKERRQ(ierr); 2767c79c5527SHong Zhang ierr = PetscSubcommSetType(psubcomm,PETSC_SUBCOMM_CONTIGUOUS);CHKERRQ(ierr); 276819171117SHong Zhang ierr = PetscSubcommSetFromOptions(psubcomm);CHKERRQ(ierr); 2769c79c5527SHong Zhang subcomm = psubcomm->comm; 27707cb6ea77SHong Zhang } else { /* retrieve psubcomm and subcomm */ 2771c79c5527SHong Zhang ierr = PetscObjectGetComm((PetscObject)(*matredundant),&subcomm);CHKERRQ(ierr); 2772c79c5527SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2773c79c5527SHong Zhang if (subsize == 1) { 2774c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 27757cb6ea77SHong Zhang redund = c->redundant; 2776c79c5527SHong Zhang } else { 2777c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 27787cb6ea77SHong Zhang redund = c->redundant; 2779c79c5527SHong Zhang } 27807cb6ea77SHong Zhang psubcomm = redund->psubcomm; 2781fd7037dcSHong Zhang } 27821f2d8ef4SHong Zhang if (psubcomm->type == PETSC_SUBCOMM_INTERLACED) { 27837cb6ea77SHong Zhang ierr = MatGetRedundantMatrix_MPIAIJ_interlaced(mat,nsubcomm,subcomm,reuse,matredundant);CHKERRQ(ierr); 27841f2d8ef4SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { /* psubcomm is created in this routine, free it in MatDestroy_MatRedundant() */ 27851f2d8ef4SHong Zhang ierr = MPI_Comm_size(psubcomm->comm,&subsize);CHKERRQ(ierr); 27861f2d8ef4SHong Zhang if (subsize == 1) { 27871f2d8ef4SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 27881f2d8ef4SHong Zhang c->redundant->psubcomm = psubcomm; 27891f2d8ef4SHong Zhang } else { 27901f2d8ef4SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 27911f2d8ef4SHong Zhang c->redundant->psubcomm = psubcomm ; 27921f2d8ef4SHong Zhang } 27931f2d8ef4SHong Zhang } 27941f2d8ef4SHong Zhang PetscFunctionReturn(0); 2795c79c5527SHong Zhang } 2796c79c5527SHong Zhang } 2797e37c6257SHong Zhang 27981f2d8ef4SHong Zhang /* use MPI subcomm via MatGetSubMatrices(); use subcomm_in or psubcomm->comm (psubcomm->type != INTERLACED) */ 27997cb6ea77SHong Zhang ierr = MPI_Comm_size(subcomm,&subsize);CHKERRQ(ierr); 2800c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2801c79c5527SHong Zhang /* create a local sequential matrix matseq[0] */ 2802c79c5527SHong Zhang mloc_sub = PETSC_DECIDE; 2803c79c5527SHong Zhang ierr = PetscSplitOwnership(subcomm,&mloc_sub,&M);CHKERRQ(ierr); 2804c79c5527SHong Zhang ierr = MPI_Scan(&mloc_sub,&rend,1,MPIU_INT,MPI_SUM,subcomm);CHKERRQ(ierr); 2805c79c5527SHong Zhang rstart = rend - mloc_sub; 2806c79c5527SHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,mloc_sub,rstart,1,&isrow);CHKERRQ(ierr); 2807c79c5527SHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,N,0,1,&iscol);CHKERRQ(ierr); 2808c79c5527SHong Zhang } else { /* reuse == MAT_REUSE_MATRIX */ 2809c79c5527SHong Zhang if (subsize == 1) { 2810c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 2811c79c5527SHong Zhang redund = c->redundant; 2812c79c5527SHong Zhang } else { 2813c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 2814c79c5527SHong Zhang redund = c->redundant; 2815c79c5527SHong Zhang } 2816c79c5527SHong Zhang 2817c79c5527SHong Zhang isrow = redund->isrow; 2818c79c5527SHong Zhang iscol = redund->iscol; 2819c79c5527SHong Zhang matseq = redund->matseq; 2820c79c5527SHong Zhang } 2821c79c5527SHong Zhang ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,reuse,&matseq);CHKERRQ(ierr); 2822c79c5527SHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJ(subcomm,matseq[0],PETSC_DECIDE,reuse,matredundant);CHKERRQ(ierr); 2823c79c5527SHong Zhang 2824c79c5527SHong Zhang if (reuse == MAT_INITIAL_MATRIX) { 2825c79c5527SHong Zhang /* create a supporting struct and attach it to C for reuse */ 2826b00a9115SJed Brown ierr = PetscNewLog(*matredundant,&redund);CHKERRQ(ierr); 2827c79c5527SHong Zhang if (subsize == 1) { 2828c79c5527SHong Zhang Mat_SeqAIJ *c = (Mat_SeqAIJ*)(*matredundant)->data; 2829c79c5527SHong Zhang c->redundant = redund; 2830c79c5527SHong Zhang } else { 2831c79c5527SHong Zhang Mat_MPIAIJ *c = (Mat_MPIAIJ*)(*matredundant)->data; 2832c79c5527SHong Zhang c->redundant = redund; 2833c79c5527SHong Zhang } 2834c79c5527SHong Zhang redund->isrow = isrow; 2835c79c5527SHong Zhang redund->iscol = iscol; 2836c79c5527SHong Zhang redund->matseq = matseq; 28371f2d8ef4SHong Zhang redund->psubcomm = psubcomm; 2838c79c5527SHong Zhang redund->Destroy = (*matredundant)->ops->destroy; 2839c79c5527SHong Zhang (*matredundant)->ops->destroy = MatDestroy_MatRedundant; 2840c79c5527SHong Zhang } 284169db28dcSHong Zhang PetscFunctionReturn(0); 284269db28dcSHong Zhang } 284369db28dcSHong Zhang 284403bc72f1SMatthew Knepley #undef __FUNCT__ 2845c91732d9SHong Zhang #define __FUNCT__ "MatGetRowMaxAbs_MPIAIJ" 2846c91732d9SHong Zhang PetscErrorCode MatGetRowMaxAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2847c91732d9SHong Zhang { 2848c91732d9SHong Zhang Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2849c91732d9SHong Zhang PetscErrorCode ierr; 2850c91732d9SHong Zhang PetscInt i,*idxb = 0; 2851c91732d9SHong Zhang PetscScalar *va,*vb; 2852c91732d9SHong Zhang Vec vtmp; 2853c91732d9SHong Zhang 2854c91732d9SHong Zhang PetscFunctionBegin; 2855c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->A,v,idx);CHKERRQ(ierr); 2856c91732d9SHong Zhang ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2857c91732d9SHong Zhang if (idx) { 2858192daf7cSBarry Smith for (i=0; i<A->rmap->n; i++) { 2859d0f46423SBarry Smith if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2860c91732d9SHong Zhang } 2861c91732d9SHong Zhang } 2862c91732d9SHong Zhang 2863d0f46423SBarry Smith ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2864c91732d9SHong Zhang if (idx) { 2865785e854fSJed Brown ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr); 2866c91732d9SHong Zhang } 2867c91732d9SHong Zhang ierr = MatGetRowMaxAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2868c91732d9SHong Zhang ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2869c91732d9SHong Zhang 2870d0f46423SBarry Smith for (i=0; i<A->rmap->n; i++) { 2871c91732d9SHong Zhang if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) { 2872c91732d9SHong Zhang va[i] = vb[i]; 2873c91732d9SHong Zhang if (idx) idx[i] = a->garray[idxb[i]]; 2874c91732d9SHong Zhang } 2875c91732d9SHong Zhang } 2876c91732d9SHong Zhang 2877c91732d9SHong Zhang ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2878c91732d9SHong Zhang ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2879c91732d9SHong Zhang ierr = PetscFree(idxb);CHKERRQ(ierr); 28806bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2881c91732d9SHong Zhang PetscFunctionReturn(0); 2882c91732d9SHong Zhang } 2883c91732d9SHong Zhang 2884c91732d9SHong Zhang #undef __FUNCT__ 2885c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_MPIAIJ" 2886c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2887c87e5d42SMatthew Knepley { 2888c87e5d42SMatthew Knepley Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 2889c87e5d42SMatthew Knepley PetscErrorCode ierr; 2890c87e5d42SMatthew Knepley PetscInt i,*idxb = 0; 2891c87e5d42SMatthew Knepley PetscScalar *va,*vb; 2892c87e5d42SMatthew Knepley Vec vtmp; 2893c87e5d42SMatthew Knepley 2894c87e5d42SMatthew Knepley PetscFunctionBegin; 2895c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->A,v,idx);CHKERRQ(ierr); 2896c87e5d42SMatthew Knepley ierr = VecGetArray(v,&va);CHKERRQ(ierr); 2897c87e5d42SMatthew Knepley if (idx) { 2898c87e5d42SMatthew Knepley for (i=0; i<A->cmap->n; i++) { 2899c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart; 2900c87e5d42SMatthew Knepley } 2901c87e5d42SMatthew Knepley } 2902c87e5d42SMatthew Knepley 2903c87e5d42SMatthew Knepley ierr = VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);CHKERRQ(ierr); 2904c87e5d42SMatthew Knepley if (idx) { 2905785e854fSJed Brown ierr = PetscMalloc1(A->rmap->n,&idxb);CHKERRQ(ierr); 2906c87e5d42SMatthew Knepley } 2907c87e5d42SMatthew Knepley ierr = MatGetRowMinAbs(a->B,vtmp,idxb);CHKERRQ(ierr); 2908c87e5d42SMatthew Knepley ierr = VecGetArray(vtmp,&vb);CHKERRQ(ierr); 2909c87e5d42SMatthew Knepley 2910c87e5d42SMatthew Knepley for (i=0; i<A->rmap->n; i++) { 2911c87e5d42SMatthew Knepley if (PetscAbsScalar(va[i]) > PetscAbsScalar(vb[i])) { 2912c87e5d42SMatthew Knepley va[i] = vb[i]; 2913c87e5d42SMatthew Knepley if (idx) idx[i] = a->garray[idxb[i]]; 2914c87e5d42SMatthew Knepley } 2915c87e5d42SMatthew Knepley } 2916c87e5d42SMatthew Knepley 2917c87e5d42SMatthew Knepley ierr = VecRestoreArray(v,&va);CHKERRQ(ierr); 2918c87e5d42SMatthew Knepley ierr = VecRestoreArray(vtmp,&vb);CHKERRQ(ierr); 2919c87e5d42SMatthew Knepley ierr = PetscFree(idxb);CHKERRQ(ierr); 29206bf464f9SBarry Smith ierr = VecDestroy(&vtmp);CHKERRQ(ierr); 2921c87e5d42SMatthew Knepley PetscFunctionReturn(0); 2922c87e5d42SMatthew Knepley } 2923c87e5d42SMatthew Knepley 2924c87e5d42SMatthew Knepley #undef __FUNCT__ 292503bc72f1SMatthew Knepley #define __FUNCT__ "MatGetRowMin_MPIAIJ" 292603bc72f1SMatthew Knepley PetscErrorCode MatGetRowMin_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 292703bc72f1SMatthew Knepley { 292803bc72f1SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 2929d0f46423SBarry Smith PetscInt n = A->rmap->n; 2930d0f46423SBarry Smith PetscInt cstart = A->cmap->rstart; 293103bc72f1SMatthew Knepley PetscInt *cmap = mat->garray; 293203bc72f1SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 293303bc72f1SMatthew Knepley Vec diagV, offdiagV; 293403bc72f1SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 293503bc72f1SMatthew Knepley PetscInt r; 293603bc72f1SMatthew Knepley PetscErrorCode ierr; 293703bc72f1SMatthew Knepley 293803bc72f1SMatthew Knepley PetscFunctionBegin; 2939dcca6d9dSJed Brown ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr); 2940ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &diagV);CHKERRQ(ierr); 2941ce94432eSBarry Smith ierr = VecCreateSeq(PetscObjectComm((PetscObject)A), n, &offdiagV);CHKERRQ(ierr); 294203bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->A, diagV, diagIdx);CHKERRQ(ierr); 294303bc72f1SMatthew Knepley ierr = MatGetRowMin(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 294403bc72f1SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 294503bc72f1SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 294603bc72f1SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 294703bc72f1SMatthew Knepley for (r = 0; r < n; ++r) { 2948028cd4eaSSatish Balay if (PetscAbsScalar(diagA[r]) <= PetscAbsScalar(offdiagA[r])) { 294903bc72f1SMatthew Knepley a[r] = diagA[r]; 295003bc72f1SMatthew Knepley idx[r] = cstart + diagIdx[r]; 295103bc72f1SMatthew Knepley } else { 295203bc72f1SMatthew Knepley a[r] = offdiagA[r]; 295303bc72f1SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 295403bc72f1SMatthew Knepley } 295503bc72f1SMatthew Knepley } 295603bc72f1SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 295703bc72f1SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 295803bc72f1SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 29596bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 29606bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 296103bc72f1SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 296203bc72f1SMatthew Knepley PetscFunctionReturn(0); 296303bc72f1SMatthew Knepley } 296403bc72f1SMatthew Knepley 29655494a064SHong Zhang #undef __FUNCT__ 2966c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMax_MPIAIJ" 2967c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMax_MPIAIJ(Mat A, Vec v, PetscInt idx[]) 2968c87e5d42SMatthew Knepley { 2969c87e5d42SMatthew Knepley Mat_MPIAIJ *mat = (Mat_MPIAIJ*) A->data; 2970c87e5d42SMatthew Knepley PetscInt n = A->rmap->n; 2971c87e5d42SMatthew Knepley PetscInt cstart = A->cmap->rstart; 2972c87e5d42SMatthew Knepley PetscInt *cmap = mat->garray; 2973c87e5d42SMatthew Knepley PetscInt *diagIdx, *offdiagIdx; 2974c87e5d42SMatthew Knepley Vec diagV, offdiagV; 2975c87e5d42SMatthew Knepley PetscScalar *a, *diagA, *offdiagA; 2976c87e5d42SMatthew Knepley PetscInt r; 2977c87e5d42SMatthew Knepley PetscErrorCode ierr; 2978c87e5d42SMatthew Knepley 2979c87e5d42SMatthew Knepley PetscFunctionBegin; 2980dcca6d9dSJed Brown ierr = PetscMalloc2(n,&diagIdx,n,&offdiagIdx);CHKERRQ(ierr); 2981d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &diagV);CHKERRQ(ierr); 2982d11e49fbSSatish Balay ierr = VecCreateSeq(PETSC_COMM_SELF, n, &offdiagV);CHKERRQ(ierr); 2983c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->A, diagV, diagIdx);CHKERRQ(ierr); 2984c87e5d42SMatthew Knepley ierr = MatGetRowMax(mat->B, offdiagV, offdiagIdx);CHKERRQ(ierr); 2985c87e5d42SMatthew Knepley ierr = VecGetArray(v, &a);CHKERRQ(ierr); 2986c87e5d42SMatthew Knepley ierr = VecGetArray(diagV, &diagA);CHKERRQ(ierr); 2987c87e5d42SMatthew Knepley ierr = VecGetArray(offdiagV, &offdiagA);CHKERRQ(ierr); 2988c87e5d42SMatthew Knepley for (r = 0; r < n; ++r) { 2989c87e5d42SMatthew Knepley if (PetscAbsScalar(diagA[r]) >= PetscAbsScalar(offdiagA[r])) { 2990c87e5d42SMatthew Knepley a[r] = diagA[r]; 2991c87e5d42SMatthew Knepley idx[r] = cstart + diagIdx[r]; 2992c87e5d42SMatthew Knepley } else { 2993c87e5d42SMatthew Knepley a[r] = offdiagA[r]; 2994c87e5d42SMatthew Knepley idx[r] = cmap[offdiagIdx[r]]; 2995c87e5d42SMatthew Knepley } 2996c87e5d42SMatthew Knepley } 2997c87e5d42SMatthew Knepley ierr = VecRestoreArray(v, &a);CHKERRQ(ierr); 2998c87e5d42SMatthew Knepley ierr = VecRestoreArray(diagV, &diagA);CHKERRQ(ierr); 2999c87e5d42SMatthew Knepley ierr = VecRestoreArray(offdiagV, &offdiagA);CHKERRQ(ierr); 30006bf464f9SBarry Smith ierr = VecDestroy(&diagV);CHKERRQ(ierr); 30016bf464f9SBarry Smith ierr = VecDestroy(&offdiagV);CHKERRQ(ierr); 3002c87e5d42SMatthew Knepley ierr = PetscFree2(diagIdx, offdiagIdx);CHKERRQ(ierr); 3003c87e5d42SMatthew Knepley PetscFunctionReturn(0); 3004c87e5d42SMatthew Knepley } 3005c87e5d42SMatthew Knepley 3006c87e5d42SMatthew Knepley #undef __FUNCT__ 3007d1adec66SJed Brown #define __FUNCT__ "MatGetSeqNonzeroStructure_MPIAIJ" 3008d1adec66SJed Brown PetscErrorCode MatGetSeqNonzeroStructure_MPIAIJ(Mat mat,Mat *newmat) 30095494a064SHong Zhang { 30105494a064SHong Zhang PetscErrorCode ierr; 3011f6d58c54SBarry Smith Mat *dummy; 30125494a064SHong Zhang 30135494a064SHong Zhang PetscFunctionBegin; 3014f6d58c54SBarry Smith ierr = MatGetSubMatrix_MPIAIJ_All(mat,MAT_DO_NOT_GET_VALUES,MAT_INITIAL_MATRIX,&dummy);CHKERRQ(ierr); 3015f6d58c54SBarry Smith *newmat = *dummy; 3016f6d58c54SBarry Smith ierr = PetscFree(dummy);CHKERRQ(ierr); 30175494a064SHong Zhang PetscFunctionReturn(0); 30185494a064SHong Zhang } 30195494a064SHong Zhang 3020bbead8a2SBarry Smith #undef __FUNCT__ 3021bbead8a2SBarry Smith #define __FUNCT__ "MatInvertBlockDiagonal_MPIAIJ" 3022713ccfa9SJed Brown PetscErrorCode MatInvertBlockDiagonal_MPIAIJ(Mat A,const PetscScalar **values) 3023bbead8a2SBarry Smith { 3024bbead8a2SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*) A->data; 3025bbead8a2SBarry Smith PetscErrorCode ierr; 3026bbead8a2SBarry Smith 3027bbead8a2SBarry Smith PetscFunctionBegin; 3028bbead8a2SBarry Smith ierr = MatInvertBlockDiagonal(a->A,values);CHKERRQ(ierr); 3029bbead8a2SBarry Smith PetscFunctionReturn(0); 3030bbead8a2SBarry Smith } 3031bbead8a2SBarry Smith 303273a71a0fSBarry Smith #undef __FUNCT__ 303373a71a0fSBarry Smith #define __FUNCT__ "MatSetRandom_MPIAIJ" 303473a71a0fSBarry Smith static PetscErrorCode MatSetRandom_MPIAIJ(Mat x,PetscRandom rctx) 303573a71a0fSBarry Smith { 303673a71a0fSBarry Smith PetscErrorCode ierr; 303773a71a0fSBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)x->data; 303873a71a0fSBarry Smith 303973a71a0fSBarry Smith PetscFunctionBegin; 304073a71a0fSBarry Smith ierr = MatSetRandom(aij->A,rctx);CHKERRQ(ierr); 304173a71a0fSBarry Smith ierr = MatSetRandom(aij->B,rctx);CHKERRQ(ierr); 304273a71a0fSBarry Smith ierr = MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 304373a71a0fSBarry Smith ierr = MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 304473a71a0fSBarry Smith PetscFunctionReturn(0); 304573a71a0fSBarry Smith } 3046bbead8a2SBarry Smith 30478a729477SBarry Smith /* -------------------------------------------------------------------*/ 3048cda55fadSBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ, 3049cda55fadSBarry Smith MatGetRow_MPIAIJ, 3050cda55fadSBarry Smith MatRestoreRow_MPIAIJ, 3051cda55fadSBarry Smith MatMult_MPIAIJ, 305297304618SKris Buschelman /* 4*/ MatMultAdd_MPIAIJ, 30537c922b88SBarry Smith MatMultTranspose_MPIAIJ, 30547c922b88SBarry Smith MatMultTransposeAdd_MPIAIJ, 3055519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3056103bf8bdSMatthew Knepley MatSolve_MPIAIJ, 3057103bf8bdSMatthew Knepley #else 3058cda55fadSBarry Smith 0, 3059103bf8bdSMatthew Knepley #endif 3060cda55fadSBarry Smith 0, 3061cda55fadSBarry Smith 0, 306297304618SKris Buschelman /*10*/ 0, 3063cda55fadSBarry Smith 0, 3064cda55fadSBarry Smith 0, 306541f059aeSBarry Smith MatSOR_MPIAIJ, 3066b7c46309SBarry Smith MatTranspose_MPIAIJ, 306797304618SKris Buschelman /*15*/ MatGetInfo_MPIAIJ, 3068cda55fadSBarry Smith MatEqual_MPIAIJ, 3069cda55fadSBarry Smith MatGetDiagonal_MPIAIJ, 3070cda55fadSBarry Smith MatDiagonalScale_MPIAIJ, 3071cda55fadSBarry Smith MatNorm_MPIAIJ, 307297304618SKris Buschelman /*20*/ MatAssemblyBegin_MPIAIJ, 3073cda55fadSBarry Smith MatAssemblyEnd_MPIAIJ, 3074cda55fadSBarry Smith MatSetOption_MPIAIJ, 3075cda55fadSBarry Smith MatZeroEntries_MPIAIJ, 3076d519adbfSMatthew Knepley /*24*/ MatZeroRows_MPIAIJ, 3077cda55fadSBarry Smith 0, 3078519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3079719d5645SBarry Smith 0, 3080103bf8bdSMatthew Knepley #else 3081cda55fadSBarry Smith 0, 3082103bf8bdSMatthew Knepley #endif 3083cda55fadSBarry Smith 0, 3084cda55fadSBarry Smith 0, 30854994cf47SJed Brown /*29*/ MatSetUp_MPIAIJ, 3086519f805aSKarl Rupp #if defined(PETSC_HAVE_PBGL) 3087719d5645SBarry Smith 0, 3088103bf8bdSMatthew Knepley #else 3089cda55fadSBarry Smith 0, 3090103bf8bdSMatthew Knepley #endif 3091cda55fadSBarry Smith 0, 3092cda55fadSBarry Smith 0, 3093cda55fadSBarry Smith 0, 3094d519adbfSMatthew Knepley /*34*/ MatDuplicate_MPIAIJ, 3095cda55fadSBarry Smith 0, 3096cda55fadSBarry Smith 0, 3097cda55fadSBarry Smith 0, 3098cda55fadSBarry Smith 0, 3099d519adbfSMatthew Knepley /*39*/ MatAXPY_MPIAIJ, 3100cda55fadSBarry Smith MatGetSubMatrices_MPIAIJ, 3101cda55fadSBarry Smith MatIncreaseOverlap_MPIAIJ, 3102cda55fadSBarry Smith MatGetValues_MPIAIJ, 3103cb5b572fSBarry Smith MatCopy_MPIAIJ, 3104d519adbfSMatthew Knepley /*44*/ MatGetRowMax_MPIAIJ, 3105cda55fadSBarry Smith MatScale_MPIAIJ, 3106cda55fadSBarry Smith 0, 3107cda55fadSBarry Smith 0, 3108564f14d6SBarry Smith MatZeroRowsColumns_MPIAIJ, 310973a71a0fSBarry Smith /*49*/ MatSetRandom_MPIAIJ, 3110cda55fadSBarry Smith 0, 3111cda55fadSBarry Smith 0, 3112cda55fadSBarry Smith 0, 3113cda55fadSBarry Smith 0, 311493dfae19SHong Zhang /*54*/ MatFDColoringCreate_MPIXAIJ, 3115cda55fadSBarry Smith 0, 3116cda55fadSBarry Smith MatSetUnfactored_MPIAIJ, 311772e6a0cfSJed Brown MatPermute_MPIAIJ, 3118cda55fadSBarry Smith 0, 3119d519adbfSMatthew Knepley /*59*/ MatGetSubMatrix_MPIAIJ, 3120e03a110bSBarry Smith MatDestroy_MPIAIJ, 3121e03a110bSBarry Smith MatView_MPIAIJ, 3122357abbc8SBarry Smith 0, 3123f996eeb8SHong Zhang MatMatMatMult_MPIAIJ_MPIAIJ_MPIAIJ, 3124f996eeb8SHong Zhang /*64*/ MatMatMatMultSymbolic_MPIAIJ_MPIAIJ_MPIAIJ, 3125f996eeb8SHong Zhang MatMatMatMultNumeric_MPIAIJ_MPIAIJ_MPIAIJ, 3126a2243be0SBarry Smith 0, 3127a2243be0SBarry Smith 0, 3128a2243be0SBarry Smith 0, 3129d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_MPIAIJ, 3130c87e5d42SMatthew Knepley MatGetRowMinAbs_MPIAIJ, 3131a2243be0SBarry Smith 0, 3132a2243be0SBarry Smith MatSetColoring_MPIAIJ, 3133dcf5cc72SBarry Smith 0, 313497304618SKris Buschelman MatSetValuesAdifor_MPIAIJ, 31353acb8795SBarry Smith /*75*/ MatFDColoringApply_AIJ, 313697304618SKris Buschelman 0, 313797304618SKris Buschelman 0, 313897304618SKris Buschelman 0, 3139f1f41ecbSJed Brown MatFindZeroDiagonals_MPIAIJ, 314097304618SKris Buschelman /*80*/ 0, 314197304618SKris Buschelman 0, 314297304618SKris Buschelman 0, 31435bba2384SShri Abhyankar /*83*/ MatLoad_MPIAIJ, 31446284ec50SHong Zhang 0, 31456284ec50SHong Zhang 0, 31466284ec50SHong Zhang 0, 31476284ec50SHong Zhang 0, 3148865e5f61SKris Buschelman 0, 3149d519adbfSMatthew Knepley /*89*/ MatMatMult_MPIAIJ_MPIAIJ, 315026be0446SHong Zhang MatMatMultSymbolic_MPIAIJ_MPIAIJ, 315126be0446SHong Zhang MatMatMultNumeric_MPIAIJ_MPIAIJ, 3152cf3ca8ceSHong Zhang MatPtAP_MPIAIJ_MPIAIJ, 3153cf3ca8ceSHong Zhang MatPtAPSymbolic_MPIAIJ_MPIAIJ, 3154cf3ca8ceSHong Zhang /*94*/ MatPtAPNumeric_MPIAIJ_MPIAIJ, 31557a7894deSKris Buschelman 0, 31567a7894deSKris Buschelman 0, 31577a7894deSKris Buschelman 0, 31587a7894deSKris Buschelman 0, 3159d519adbfSMatthew Knepley /*99*/ 0, 3160d2b207f1SPeter Brune 0, 3161d2b207f1SPeter Brune 0, 31622fd7e33dSBarry Smith MatConjugate_MPIAIJ, 31632fd7e33dSBarry Smith 0, 3164d519adbfSMatthew Knepley /*104*/MatSetValuesRow_MPIAIJ, 316599cafbc1SBarry Smith MatRealPart_MPIAIJ, 316669db28dcSHong Zhang MatImaginaryPart_MPIAIJ, 316769db28dcSHong Zhang 0, 316869db28dcSHong Zhang 0, 3169d519adbfSMatthew Knepley /*109*/0, 317003bc72f1SMatthew Knepley MatGetRedundantMatrix_MPIAIJ, 31715494a064SHong Zhang MatGetRowMin_MPIAIJ, 31725494a064SHong Zhang 0, 31735494a064SHong Zhang 0, 3174d1adec66SJed Brown /*114*/MatGetSeqNonzeroStructure_MPIAIJ, 3175bd0c2dcbSBarry Smith 0, 3176bd0c2dcbSBarry Smith 0, 3177bd0c2dcbSBarry Smith 0, 3178bd0c2dcbSBarry Smith 0, 31798fb81238SShri Abhyankar /*119*/0, 31808fb81238SShri Abhyankar 0, 31818fb81238SShri Abhyankar 0, 3182d6037b41SHong Zhang 0, 3183b9614d88SDmitry Karpeev MatGetMultiProcBlock_MPIAIJ, 3184f2c98031SJed Brown /*124*/MatFindNonzeroRows_MPIAIJ, 31850716a85fSBarry Smith MatGetColumnNorms_MPIAIJ, 3186bbead8a2SBarry Smith MatInvertBlockDiagonal_MPIAIJ, 3187b9614d88SDmitry Karpeev 0, 318837868618SMatthew G Knepley MatGetSubMatricesParallel_MPIAIJ, 3189187b3c17SHong Zhang /*129*/0, 3190187b3c17SHong Zhang MatTransposeMatMult_MPIAIJ_MPIAIJ, 3191187b3c17SHong Zhang MatTransposeMatMultSymbolic_MPIAIJ_MPIAIJ, 3192187b3c17SHong Zhang MatTransposeMatMultNumeric_MPIAIJ_MPIAIJ, 3193187b3c17SHong Zhang 0, 3194187b3c17SHong Zhang /*134*/0, 3195187b3c17SHong Zhang 0, 3196187b3c17SHong Zhang 0, 3197187b3c17SHong Zhang 0, 31983964eb88SJed Brown 0, 31993964eb88SJed Brown /*139*/0, 3200f9426fe0SMark Adams 0, 3201f86b9fbaSHong Zhang 0, 3202f86b9fbaSHong Zhang MatFDColoringSetUp_MPIXAIJ 3203bd0c2dcbSBarry Smith }; 320436ce4990SBarry Smith 32052e8a6d31SBarry Smith /* ----------------------------------------------------------------------------------------*/ 32062e8a6d31SBarry Smith 32074a2ae208SSatish Balay #undef __FUNCT__ 32084a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_MPIAIJ" 32097087cfbeSBarry Smith PetscErrorCode MatStoreValues_MPIAIJ(Mat mat) 32102e8a6d31SBarry Smith { 32112e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3212dfbe8321SBarry Smith PetscErrorCode ierr; 32132e8a6d31SBarry Smith 32142e8a6d31SBarry Smith PetscFunctionBegin; 32152e8a6d31SBarry Smith ierr = MatStoreValues(aij->A);CHKERRQ(ierr); 32162e8a6d31SBarry Smith ierr = MatStoreValues(aij->B);CHKERRQ(ierr); 32172e8a6d31SBarry Smith PetscFunctionReturn(0); 32182e8a6d31SBarry Smith } 32192e8a6d31SBarry Smith 32204a2ae208SSatish Balay #undef __FUNCT__ 32214a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_MPIAIJ" 32227087cfbeSBarry Smith PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat) 32232e8a6d31SBarry Smith { 32242e8a6d31SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 3225dfbe8321SBarry Smith PetscErrorCode ierr; 32262e8a6d31SBarry Smith 32272e8a6d31SBarry Smith PetscFunctionBegin; 32282e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->A);CHKERRQ(ierr); 32292e8a6d31SBarry Smith ierr = MatRetrieveValues(aij->B);CHKERRQ(ierr); 32302e8a6d31SBarry Smith PetscFunctionReturn(0); 32312e8a6d31SBarry Smith } 32328a729477SBarry Smith 32334a2ae208SSatish Balay #undef __FUNCT__ 3234a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJ" 32357087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3236a23d5eceSKris Buschelman { 3237a23d5eceSKris Buschelman Mat_MPIAIJ *b; 3238dfbe8321SBarry Smith PetscErrorCode ierr; 3239a23d5eceSKris Buschelman 3240a23d5eceSKris Buschelman PetscFunctionBegin; 324126283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 324226283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3243a23d5eceSKris Buschelman b = (Mat_MPIAIJ*)B->data; 3244899cda47SBarry Smith 3245526dfc15SBarry Smith if (!B->preallocated) { 3246899cda47SBarry Smith /* Explicitly create 2 MATSEQAIJ matrices. */ 3247899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->A);CHKERRQ(ierr); 3248d0f46423SBarry Smith ierr = MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);CHKERRQ(ierr); 3249f9e9af59SJed Brown ierr = MatSetBlockSizes(b->A,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3250899cda47SBarry Smith ierr = MatSetType(b->A,MATSEQAIJ);CHKERRQ(ierr); 32513bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);CHKERRQ(ierr); 3252899cda47SBarry Smith ierr = MatCreate(PETSC_COMM_SELF,&b->B);CHKERRQ(ierr); 3253d0f46423SBarry Smith ierr = MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);CHKERRQ(ierr); 3254f9e9af59SJed Brown ierr = MatSetBlockSizes(b->B,B->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 3255899cda47SBarry Smith ierr = MatSetType(b->B,MATSEQAIJ);CHKERRQ(ierr); 32563bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);CHKERRQ(ierr); 3257526dfc15SBarry Smith } 3258899cda47SBarry Smith 3259c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);CHKERRQ(ierr); 3260c60e587dSKris Buschelman ierr = MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);CHKERRQ(ierr); 3261526dfc15SBarry Smith B->preallocated = PETSC_TRUE; 3262a23d5eceSKris Buschelman PetscFunctionReturn(0); 3263a23d5eceSKris Buschelman } 3264a23d5eceSKris Buschelman 32654a2ae208SSatish Balay #undef __FUNCT__ 32664a2ae208SSatish Balay #define __FUNCT__ "MatDuplicate_MPIAIJ" 3267dfbe8321SBarry Smith PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat) 3268d6dfbf8fSBarry Smith { 3269d6dfbf8fSBarry Smith Mat mat; 3270416022c9SBarry Smith Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data; 3271dfbe8321SBarry Smith PetscErrorCode ierr; 3272d6dfbf8fSBarry Smith 32733a40ed3dSBarry Smith PetscFunctionBegin; 3274416022c9SBarry Smith *newmat = 0; 3275ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)matin),&mat);CHKERRQ(ierr); 3276d0f46423SBarry Smith ierr = MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);CHKERRQ(ierr); 3277a2f3521dSMark F. Adams ierr = MatSetBlockSizes(mat,matin->rmap->bs,matin->cmap->bs);CHKERRQ(ierr); 32787adad957SLisandro Dalcin ierr = MatSetType(mat,((PetscObject)matin)->type_name);CHKERRQ(ierr); 32791d5dac46SHong Zhang ierr = PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));CHKERRQ(ierr); 3280273d9f13SBarry Smith a = (Mat_MPIAIJ*)mat->data; 3281e1b6402fSHong Zhang 3282d5f3da31SBarry Smith mat->factortype = matin->factortype; 3283d0f46423SBarry Smith mat->rmap->bs = matin->rmap->bs; 3284a2f3521dSMark F. Adams mat->cmap->bs = matin->cmap->bs; 3285c456f294SBarry Smith mat->assembled = PETSC_TRUE; 3286e7641de0SSatish Balay mat->insertmode = NOT_SET_VALUES; 3287273d9f13SBarry Smith mat->preallocated = PETSC_TRUE; 3288d6dfbf8fSBarry Smith 328917699dbbSLois Curfman McInnes a->size = oldmat->size; 329017699dbbSLois Curfman McInnes a->rank = oldmat->rank; 3291e7641de0SSatish Balay a->donotstash = oldmat->donotstash; 3292e7641de0SSatish Balay a->roworiented = oldmat->roworiented; 3293e7641de0SSatish Balay a->rowindices = 0; 3294bcd2baecSBarry Smith a->rowvalues = 0; 3295bcd2baecSBarry Smith a->getrowactive = PETSC_FALSE; 3296d6dfbf8fSBarry Smith 32971e1e43feSBarry Smith ierr = PetscLayoutReference(matin->rmap,&mat->rmap);CHKERRQ(ierr); 32981e1e43feSBarry Smith ierr = PetscLayoutReference(matin->cmap,&mat->cmap);CHKERRQ(ierr); 3299899cda47SBarry Smith 33002ee70a88SLois Curfman McInnes if (oldmat->colmap) { 3301aa482453SBarry Smith #if defined(PETSC_USE_CTABLE) 33020f5bd95cSBarry Smith ierr = PetscTableCreateCopy(oldmat->colmap,&a->colmap);CHKERRQ(ierr); 3303b1fc9764SSatish Balay #else 3304785e854fSJed Brown ierr = PetscMalloc1((mat->cmap->N),&a->colmap);CHKERRQ(ierr); 33053bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3306d0f46423SBarry Smith ierr = PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap->N)*sizeof(PetscInt));CHKERRQ(ierr); 3307b1fc9764SSatish Balay #endif 3308416022c9SBarry Smith } else a->colmap = 0; 33093f41c07dSBarry Smith if (oldmat->garray) { 3310b1d57f15SBarry Smith PetscInt len; 3311d0f46423SBarry Smith len = oldmat->B->cmap->n; 3312785e854fSJed Brown ierr = PetscMalloc1((len+1),&a->garray);CHKERRQ(ierr); 33133bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));CHKERRQ(ierr); 3314b1d57f15SBarry Smith if (len) { ierr = PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));CHKERRQ(ierr); } 3315416022c9SBarry Smith } else a->garray = 0; 3316d6dfbf8fSBarry Smith 3317416022c9SBarry Smith ierr = VecDuplicate(oldmat->lvec,&a->lvec);CHKERRQ(ierr); 33183bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);CHKERRQ(ierr); 3319a56f8943SBarry Smith ierr = VecScatterCopy(oldmat->Mvctx,&a->Mvctx);CHKERRQ(ierr); 33203bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);CHKERRQ(ierr); 33212e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->A,cpvalues,&a->A);CHKERRQ(ierr); 33223bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);CHKERRQ(ierr); 33232e8a6d31SBarry Smith ierr = MatDuplicate(oldmat->B,cpvalues,&a->B);CHKERRQ(ierr); 33243bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);CHKERRQ(ierr); 3325140e18c1SBarry Smith ierr = PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);CHKERRQ(ierr); 33268a729477SBarry Smith *newmat = mat; 33273a40ed3dSBarry Smith PetscFunctionReturn(0); 33288a729477SBarry Smith } 3329416022c9SBarry Smith 33301a4ee126SBarry Smith 33311a4ee126SBarry Smith 33324a2ae208SSatish Balay #undef __FUNCT__ 33335bba2384SShri Abhyankar #define __FUNCT__ "MatLoad_MPIAIJ" 3334112444f4SShri Abhyankar PetscErrorCode MatLoad_MPIAIJ(Mat newMat, PetscViewer viewer) 33358fb81238SShri Abhyankar { 33368fb81238SShri Abhyankar PetscScalar *vals,*svals; 3337ce94432eSBarry Smith MPI_Comm comm; 33388fb81238SShri Abhyankar PetscErrorCode ierr; 33391a4ee126SBarry Smith PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag; 33408fb81238SShri Abhyankar PetscInt i,nz,j,rstart,rend,mmax,maxnz = 0,grows,gcols; 33418fb81238SShri Abhyankar PetscInt header[4],*rowlengths = 0,M,N,m,*cols; 33420298fd71SBarry Smith PetscInt *ourlens = NULL,*procsnz = NULL,*offlens = NULL,jj,*mycols,*smycols; 33438fb81238SShri Abhyankar PetscInt cend,cstart,n,*rowners,sizesset=1; 33448fb81238SShri Abhyankar int fd; 334508ea439dSMark F. Adams PetscInt bs = 1; 33468fb81238SShri Abhyankar 33478fb81238SShri Abhyankar PetscFunctionBegin; 3348ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 33498fb81238SShri Abhyankar ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 33508fb81238SShri Abhyankar ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 33518fb81238SShri Abhyankar if (!rank) { 33528fb81238SShri Abhyankar ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr); 33538fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,(char*)header,4,PETSC_INT);CHKERRQ(ierr); 33548fb81238SShri Abhyankar if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object"); 33558fb81238SShri Abhyankar } 33568fb81238SShri Abhyankar 33570298fd71SBarry Smith ierr = PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");CHKERRQ(ierr); 33580298fd71SBarry Smith ierr = PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);CHKERRQ(ierr); 335908ea439dSMark F. Adams ierr = PetscOptionsEnd();CHKERRQ(ierr); 336008ea439dSMark F. Adams 33618fb81238SShri Abhyankar if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) sizesset = 0; 33628fb81238SShri Abhyankar 33638fb81238SShri Abhyankar ierr = MPI_Bcast(header+1,3,MPIU_INT,0,comm);CHKERRQ(ierr); 33648fb81238SShri Abhyankar M = header[1]; N = header[2]; 33658fb81238SShri Abhyankar /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */ 33668fb81238SShri Abhyankar if (sizesset && newMat->rmap->N < 0) newMat->rmap->N = M; 33678fb81238SShri Abhyankar if (sizesset && newMat->cmap->N < 0) newMat->cmap->N = N; 33688fb81238SShri Abhyankar 33698fb81238SShri Abhyankar /* If global sizes are set, check if they are consistent with that given in the file */ 33708fb81238SShri Abhyankar if (sizesset) { 33718fb81238SShri Abhyankar ierr = MatGetSize(newMat,&grows,&gcols);CHKERRQ(ierr); 33728fb81238SShri Abhyankar } 3373abd38a8fSBarry 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); 3374abd38a8fSBarry 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); 33758fb81238SShri Abhyankar 337608ea439dSMark F. Adams /* determine ownership of all (block) rows */ 337708ea439dSMark F. Adams if (M%bs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows (%d) and block size (%d)",M,bs); 337808ea439dSMark F. Adams if (newMat->rmap->n < 0) m = bs*((M/bs)/size + (((M/bs) % size) > rank)); /* PETSC_DECIDE */ 33794683f7a4SShri Abhyankar else m = newMat->rmap->n; /* Set by user */ 33808fb81238SShri Abhyankar 3381785e854fSJed Brown ierr = PetscMalloc1((size+1),&rowners);CHKERRQ(ierr); 33828fb81238SShri Abhyankar ierr = MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 33838fb81238SShri Abhyankar 33848fb81238SShri Abhyankar /* First process needs enough room for process with most rows */ 33858fb81238SShri Abhyankar if (!rank) { 33868fb81238SShri Abhyankar mmax = rowners[1]; 33875c4ea359SMatthew G Knepley for (i=2; i<=size; i++) { 33888fb81238SShri Abhyankar mmax = PetscMax(mmax, rowners[i]); 33898fb81238SShri Abhyankar } 33903964eb88SJed Brown } else mmax = -1; /* unused, but compilers complain */ 33918fb81238SShri Abhyankar 33928fb81238SShri Abhyankar rowners[0] = 0; 33938fb81238SShri Abhyankar for (i=2; i<=size; i++) { 33948fb81238SShri Abhyankar rowners[i] += rowners[i-1]; 33958fb81238SShri Abhyankar } 33968fb81238SShri Abhyankar rstart = rowners[rank]; 33978fb81238SShri Abhyankar rend = rowners[rank+1]; 33988fb81238SShri Abhyankar 33998fb81238SShri Abhyankar /* distribute row lengths to all processors */ 3400dcca6d9dSJed Brown ierr = PetscMalloc2(m,&ourlens,m,&offlens);CHKERRQ(ierr); 34018fb81238SShri Abhyankar if (!rank) { 34028fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,ourlens,m,PETSC_INT);CHKERRQ(ierr); 3403785e854fSJed Brown ierr = PetscMalloc1(mmax,&rowlengths);CHKERRQ(ierr); 34041795a4d1SJed Brown ierr = PetscCalloc1(size,&procsnz);CHKERRQ(ierr); 34058fb81238SShri Abhyankar for (j=0; j<m; j++) { 34068fb81238SShri Abhyankar procsnz[0] += ourlens[j]; 34078fb81238SShri Abhyankar } 34088fb81238SShri Abhyankar for (i=1; i<size; i++) { 34098fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);CHKERRQ(ierr); 34108fb81238SShri Abhyankar /* calculate the number of nonzeros on each processor */ 34118fb81238SShri Abhyankar for (j=0; j<rowners[i+1]-rowners[i]; j++) { 34128fb81238SShri Abhyankar procsnz[i] += rowlengths[j]; 34138fb81238SShri Abhyankar } 3414a25532f0SBarry Smith ierr = MPIULong_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);CHKERRQ(ierr); 34158fb81238SShri Abhyankar } 34168fb81238SShri Abhyankar ierr = PetscFree(rowlengths);CHKERRQ(ierr); 34178fb81238SShri Abhyankar } else { 3418a25532f0SBarry Smith ierr = MPIULong_Recv(ourlens,m,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 34198fb81238SShri Abhyankar } 34208fb81238SShri Abhyankar 34218fb81238SShri Abhyankar if (!rank) { 34228fb81238SShri Abhyankar /* determine max buffer needed and allocate it */ 34238fb81238SShri Abhyankar maxnz = 0; 34248fb81238SShri Abhyankar for (i=0; i<size; i++) { 34258fb81238SShri Abhyankar maxnz = PetscMax(maxnz,procsnz[i]); 34268fb81238SShri Abhyankar } 3427785e854fSJed Brown ierr = PetscMalloc1(maxnz,&cols);CHKERRQ(ierr); 34288fb81238SShri Abhyankar 34298fb81238SShri Abhyankar /* read in my part of the matrix column indices */ 34308fb81238SShri Abhyankar nz = procsnz[0]; 3431785e854fSJed Brown ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr); 34328fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,mycols,nz,PETSC_INT);CHKERRQ(ierr); 34338fb81238SShri Abhyankar 34348fb81238SShri Abhyankar /* read in every one elses and ship off */ 34358fb81238SShri Abhyankar for (i=1; i<size; i++) { 34368fb81238SShri Abhyankar nz = procsnz[i]; 34378fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,cols,nz,PETSC_INT);CHKERRQ(ierr); 3438a25532f0SBarry Smith ierr = MPIULong_Send(cols,nz,MPIU_INT,i,tag,comm);CHKERRQ(ierr); 34398fb81238SShri Abhyankar } 34408fb81238SShri Abhyankar ierr = PetscFree(cols);CHKERRQ(ierr); 34418fb81238SShri Abhyankar } else { 34428fb81238SShri Abhyankar /* determine buffer space needed for message */ 34438fb81238SShri Abhyankar nz = 0; 34448fb81238SShri Abhyankar for (i=0; i<m; i++) { 34458fb81238SShri Abhyankar nz += ourlens[i]; 34468fb81238SShri Abhyankar } 3447785e854fSJed Brown ierr = PetscMalloc1(nz,&mycols);CHKERRQ(ierr); 34488fb81238SShri Abhyankar 34498fb81238SShri Abhyankar /* receive message of column indices*/ 3450a25532f0SBarry Smith ierr = MPIULong_Recv(mycols,nz,MPIU_INT,0,tag,comm);CHKERRQ(ierr); 34518fb81238SShri Abhyankar } 34528fb81238SShri Abhyankar 34538fb81238SShri Abhyankar /* determine column ownership if matrix is not square */ 34548fb81238SShri Abhyankar if (N != M) { 34558fb81238SShri Abhyankar if (newMat->cmap->n < 0) n = N/size + ((N % size) > rank); 34568fb81238SShri Abhyankar else n = newMat->cmap->n; 34578fb81238SShri Abhyankar ierr = MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 34588fb81238SShri Abhyankar cstart = cend - n; 34598fb81238SShri Abhyankar } else { 34608fb81238SShri Abhyankar cstart = rstart; 34618fb81238SShri Abhyankar cend = rend; 34628fb81238SShri Abhyankar n = cend - cstart; 34638fb81238SShri Abhyankar } 34648fb81238SShri Abhyankar 34658fb81238SShri Abhyankar /* loop over local rows, determining number of off diagonal entries */ 34668fb81238SShri Abhyankar ierr = PetscMemzero(offlens,m*sizeof(PetscInt));CHKERRQ(ierr); 34678fb81238SShri Abhyankar jj = 0; 34688fb81238SShri Abhyankar for (i=0; i<m; i++) { 34698fb81238SShri Abhyankar for (j=0; j<ourlens[i]; j++) { 34708fb81238SShri Abhyankar if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++; 34718fb81238SShri Abhyankar jj++; 34728fb81238SShri Abhyankar } 34738fb81238SShri Abhyankar } 34748fb81238SShri Abhyankar 34758fb81238SShri Abhyankar for (i=0; i<m; i++) { 34768fb81238SShri Abhyankar ourlens[i] -= offlens[i]; 34778fb81238SShri Abhyankar } 34788fb81238SShri Abhyankar if (!sizesset) { 34798fb81238SShri Abhyankar ierr = MatSetSizes(newMat,m,n,M,N);CHKERRQ(ierr); 34808fb81238SShri Abhyankar } 348108ea439dSMark F. Adams 348208ea439dSMark F. Adams if (bs > 1) {ierr = MatSetBlockSize(newMat,bs);CHKERRQ(ierr);} 348308ea439dSMark F. Adams 34848fb81238SShri Abhyankar ierr = MatMPIAIJSetPreallocation(newMat,0,ourlens,0,offlens);CHKERRQ(ierr); 34858fb81238SShri Abhyankar 34868fb81238SShri Abhyankar for (i=0; i<m; i++) { 34878fb81238SShri Abhyankar ourlens[i] += offlens[i]; 34888fb81238SShri Abhyankar } 34898fb81238SShri Abhyankar 34908fb81238SShri Abhyankar if (!rank) { 3491785e854fSJed Brown ierr = PetscMalloc1((maxnz+1),&vals);CHKERRQ(ierr); 34928fb81238SShri Abhyankar 34938fb81238SShri Abhyankar /* read in my part of the matrix numerical values */ 34948fb81238SShri Abhyankar nz = procsnz[0]; 34958fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 34968fb81238SShri Abhyankar 34978fb81238SShri Abhyankar /* insert into matrix */ 34988fb81238SShri Abhyankar jj = rstart; 34998fb81238SShri Abhyankar smycols = mycols; 35008fb81238SShri Abhyankar svals = vals; 35018fb81238SShri Abhyankar for (i=0; i<m; i++) { 35028fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35038fb81238SShri Abhyankar smycols += ourlens[i]; 35048fb81238SShri Abhyankar svals += ourlens[i]; 35058fb81238SShri Abhyankar jj++; 35068fb81238SShri Abhyankar } 35078fb81238SShri Abhyankar 35088fb81238SShri Abhyankar /* read in other processors and ship out */ 35098fb81238SShri Abhyankar for (i=1; i<size; i++) { 35108fb81238SShri Abhyankar nz = procsnz[i]; 35118fb81238SShri Abhyankar ierr = PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);CHKERRQ(ierr); 3512a25532f0SBarry Smith ierr = MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35138fb81238SShri Abhyankar } 35148fb81238SShri Abhyankar ierr = PetscFree(procsnz);CHKERRQ(ierr); 35158fb81238SShri Abhyankar } else { 35168fb81238SShri Abhyankar /* receive numeric values */ 3517785e854fSJed Brown ierr = PetscMalloc1((nz+1),&vals);CHKERRQ(ierr); 35188fb81238SShri Abhyankar 35198fb81238SShri Abhyankar /* receive message of values*/ 3520a25532f0SBarry Smith ierr = MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newMat)->tag,comm);CHKERRQ(ierr); 35218fb81238SShri Abhyankar 35228fb81238SShri Abhyankar /* insert into matrix */ 35238fb81238SShri Abhyankar jj = rstart; 35248fb81238SShri Abhyankar smycols = mycols; 35258fb81238SShri Abhyankar svals = vals; 35268fb81238SShri Abhyankar for (i=0; i<m; i++) { 35278fb81238SShri Abhyankar ierr = MatSetValues_MPIAIJ(newMat,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);CHKERRQ(ierr); 35288fb81238SShri Abhyankar smycols += ourlens[i]; 35298fb81238SShri Abhyankar svals += ourlens[i]; 35308fb81238SShri Abhyankar jj++; 35318fb81238SShri Abhyankar } 35328fb81238SShri Abhyankar } 35338fb81238SShri Abhyankar ierr = PetscFree2(ourlens,offlens);CHKERRQ(ierr); 35348fb81238SShri Abhyankar ierr = PetscFree(vals);CHKERRQ(ierr); 35358fb81238SShri Abhyankar ierr = PetscFree(mycols);CHKERRQ(ierr); 35368fb81238SShri Abhyankar ierr = PetscFree(rowners);CHKERRQ(ierr); 35378fb81238SShri Abhyankar ierr = MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 35388fb81238SShri Abhyankar ierr = MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 35398fb81238SShri Abhyankar PetscFunctionReturn(0); 35408fb81238SShri Abhyankar } 35418fb81238SShri Abhyankar 35428fb81238SShri Abhyankar #undef __FUNCT__ 35434a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_MPIAIJ" 35444aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat) 35454aa3045dSJed Brown { 35464aa3045dSJed Brown PetscErrorCode ierr; 35474aa3045dSJed Brown IS iscol_local; 35484aa3045dSJed Brown PetscInt csize; 35494aa3045dSJed Brown 35504aa3045dSJed Brown PetscFunctionBegin; 35514aa3045dSJed Brown ierr = ISGetLocalSize(iscol,&csize);CHKERRQ(ierr); 3552b79d0421SJed Brown if (call == MAT_REUSE_MATRIX) { 3553b79d0421SJed Brown ierr = PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);CHKERRQ(ierr); 3554e32f2f54SBarry Smith if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 3555b79d0421SJed Brown } else { 3556c5bfad50SMark F. Adams PetscInt cbs; 3557c5bfad50SMark F. Adams ierr = ISGetBlockSize(iscol,&cbs);CHKERRQ(ierr); 35584aa3045dSJed Brown ierr = ISAllGather(iscol,&iscol_local);CHKERRQ(ierr); 3559c5bfad50SMark F. Adams ierr = ISSetBlockSize(iscol_local,cbs);CHKERRQ(ierr); 3560b79d0421SJed Brown } 35614aa3045dSJed Brown ierr = MatGetSubMatrix_MPIAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);CHKERRQ(ierr); 3562b79d0421SJed Brown if (call == MAT_INITIAL_MATRIX) { 3563b79d0421SJed Brown ierr = PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);CHKERRQ(ierr); 35646bf464f9SBarry Smith ierr = ISDestroy(&iscol_local);CHKERRQ(ierr); 3565b79d0421SJed Brown } 35664aa3045dSJed Brown PetscFunctionReturn(0); 35674aa3045dSJed Brown } 35684aa3045dSJed Brown 356929dcf524SDmitry Karpeev extern PetscErrorCode MatGetSubMatrices_MPIAIJ_Local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,Mat*); 35704aa3045dSJed Brown #undef __FUNCT__ 35714aa3045dSJed Brown #define __FUNCT__ "MatGetSubMatrix_MPIAIJ_Private" 3572a0ff6018SBarry Smith /* 357329da9460SBarry Smith Not great since it makes two copies of the submatrix, first an SeqAIJ 357429da9460SBarry Smith in local and then by concatenating the local matrices the end result. 357529da9460SBarry Smith Writing it directly would be much like MatGetSubMatrices_MPIAIJ() 35764aa3045dSJed Brown 35774aa3045dSJed Brown Note: This requires a sequential iscol with all indices. 3578a0ff6018SBarry Smith */ 35794aa3045dSJed Brown PetscErrorCode MatGetSubMatrix_MPIAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat) 3580a0ff6018SBarry Smith { 3581dfbe8321SBarry Smith PetscErrorCode ierr; 358232dcc486SBarry Smith PetscMPIInt rank,size; 3583a2f3521dSMark F. Adams PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs,cbs; 358429dcf524SDmitry Karpeev PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol; 358529dcf524SDmitry Karpeev PetscBool allcolumns, colflag; 358629dcf524SDmitry Karpeev Mat M,Mreuse; 3587a77337e4SBarry Smith MatScalar *vwork,*aa; 3588ce94432eSBarry Smith MPI_Comm comm; 358900e6dbe6SBarry Smith Mat_SeqAIJ *aij; 35907e2c5f70SBarry Smith 3591a0ff6018SBarry Smith PetscFunctionBegin; 3592ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)mat,&comm);CHKERRQ(ierr); 35931dab6e02SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 35941dab6e02SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 359500e6dbe6SBarry Smith 359629dcf524SDmitry Karpeev ierr = ISIdentity(iscol,&colflag);CHKERRQ(ierr); 359729dcf524SDmitry Karpeev ierr = ISGetLocalSize(iscol,&ncol);CHKERRQ(ierr); 359829dcf524SDmitry Karpeev if (colflag && ncol == mat->cmap->N) { 359929dcf524SDmitry Karpeev allcolumns = PETSC_TRUE; 360029dcf524SDmitry Karpeev } else { 360129dcf524SDmitry Karpeev allcolumns = PETSC_FALSE; 360229dcf524SDmitry Karpeev } 3603fee21e36SBarry Smith if (call == MAT_REUSE_MATRIX) { 3604fee21e36SBarry Smith ierr = PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);CHKERRQ(ierr); 3605e32f2f54SBarry Smith if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse"); 360629dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3607fee21e36SBarry Smith } else { 360829dcf524SDmitry Karpeev ierr = MatGetSubMatrices_MPIAIJ_Local(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&allcolumns,&Mreuse);CHKERRQ(ierr); 3609fee21e36SBarry Smith } 3610a0ff6018SBarry Smith 3611a0ff6018SBarry Smith /* 3612a0ff6018SBarry Smith m - number of local rows 3613a0ff6018SBarry Smith n - number of columns (same on all processors) 3614a0ff6018SBarry Smith rstart - first row in new global matrix generated 3615a0ff6018SBarry Smith */ 3616fee21e36SBarry Smith ierr = MatGetSize(Mreuse,&m,&n);CHKERRQ(ierr); 3617a2f3521dSMark F. Adams ierr = MatGetBlockSizes(Mreuse,&bs,&cbs);CHKERRQ(ierr); 3618a0ff6018SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3619fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 362000e6dbe6SBarry Smith ii = aij->i; 362100e6dbe6SBarry Smith jj = aij->j; 362200e6dbe6SBarry Smith 3623a0ff6018SBarry Smith /* 362400e6dbe6SBarry Smith Determine the number of non-zeros in the diagonal and off-diagonal 362500e6dbe6SBarry Smith portions of the matrix in order to do correct preallocation 3626a0ff6018SBarry Smith */ 362700e6dbe6SBarry Smith 362800e6dbe6SBarry Smith /* first get start and end of "diagonal" columns */ 36296a6a5d1dSBarry Smith if (csize == PETSC_DECIDE) { 3630ab50ec6bSBarry Smith ierr = ISGetSize(isrow,&mglobal);CHKERRQ(ierr); 3631ab50ec6bSBarry Smith if (mglobal == n) { /* square matrix */ 3632e2c4fddaSBarry Smith nlocal = m; 36336a6a5d1dSBarry Smith } else { 3634ab50ec6bSBarry Smith nlocal = n/size + ((n % size) > rank); 3635ab50ec6bSBarry Smith } 3636ab50ec6bSBarry Smith } else { 36376a6a5d1dSBarry Smith nlocal = csize; 36386a6a5d1dSBarry Smith } 3639b1d57f15SBarry Smith ierr = MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 364000e6dbe6SBarry Smith rstart = rend - nlocal; 364165e19b50SBarry 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); 364200e6dbe6SBarry Smith 364300e6dbe6SBarry Smith /* next, compute all the lengths */ 3644785e854fSJed Brown ierr = PetscMalloc1((2*m+1),&dlens);CHKERRQ(ierr); 364500e6dbe6SBarry Smith olens = dlens + m; 364600e6dbe6SBarry Smith for (i=0; i<m; i++) { 364700e6dbe6SBarry Smith jend = ii[i+1] - ii[i]; 364800e6dbe6SBarry Smith olen = 0; 364900e6dbe6SBarry Smith dlen = 0; 365000e6dbe6SBarry Smith for (j=0; j<jend; j++) { 365100e6dbe6SBarry Smith if (*jj < rstart || *jj >= rend) olen++; 365200e6dbe6SBarry Smith else dlen++; 365300e6dbe6SBarry Smith jj++; 365400e6dbe6SBarry Smith } 365500e6dbe6SBarry Smith olens[i] = olen; 365600e6dbe6SBarry Smith dlens[i] = dlen; 365700e6dbe6SBarry Smith } 3658f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&M);CHKERRQ(ierr); 3659f69a0ea3SMatthew Knepley ierr = MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);CHKERRQ(ierr); 3660a2f3521dSMark F. Adams ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); 36617adad957SLisandro Dalcin ierr = MatSetType(M,((PetscObject)mat)->type_name);CHKERRQ(ierr); 3662e2d9671bSKris Buschelman ierr = MatMPIAIJSetPreallocation(M,0,dlens,0,olens);CHKERRQ(ierr); 3663606d414cSSatish Balay ierr = PetscFree(dlens);CHKERRQ(ierr); 3664a0ff6018SBarry Smith } else { 3665b1d57f15SBarry Smith PetscInt ml,nl; 3666a0ff6018SBarry Smith 3667a0ff6018SBarry Smith M = *newmat; 3668a0ff6018SBarry Smith ierr = MatGetLocalSize(M,&ml,&nl);CHKERRQ(ierr); 3669e32f2f54SBarry Smith if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request"); 3670a0ff6018SBarry Smith ierr = MatZeroEntries(M);CHKERRQ(ierr); 3671c48de900SBarry Smith /* 3672c48de900SBarry Smith The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly, 3673c48de900SBarry Smith rather than the slower MatSetValues(). 3674c48de900SBarry Smith */ 3675c48de900SBarry Smith M->was_assembled = PETSC_TRUE; 3676c48de900SBarry Smith M->assembled = PETSC_FALSE; 3677a0ff6018SBarry Smith } 3678a0ff6018SBarry Smith ierr = MatGetOwnershipRange(M,&rstart,&rend);CHKERRQ(ierr); 3679fee21e36SBarry Smith aij = (Mat_SeqAIJ*)(Mreuse)->data; 368000e6dbe6SBarry Smith ii = aij->i; 368100e6dbe6SBarry Smith jj = aij->j; 368200e6dbe6SBarry Smith aa = aij->a; 3683a0ff6018SBarry Smith for (i=0; i<m; i++) { 3684a0ff6018SBarry Smith row = rstart + i; 368500e6dbe6SBarry Smith nz = ii[i+1] - ii[i]; 368600e6dbe6SBarry Smith cwork = jj; jj += nz; 368700e6dbe6SBarry Smith vwork = aa; aa += nz; 36888c638d02SBarry Smith ierr = MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 3689a0ff6018SBarry Smith } 3690a0ff6018SBarry Smith 3691a0ff6018SBarry Smith ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3692a0ff6018SBarry Smith ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3693a0ff6018SBarry Smith *newmat = M; 3694fee21e36SBarry Smith 3695fee21e36SBarry Smith /* save submatrix used in processor for next request */ 3696fee21e36SBarry Smith if (call == MAT_INITIAL_MATRIX) { 3697fee21e36SBarry Smith ierr = PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);CHKERRQ(ierr); 3698bf0cc555SLisandro Dalcin ierr = MatDestroy(&Mreuse);CHKERRQ(ierr); 3699fee21e36SBarry Smith } 3700a0ff6018SBarry Smith PetscFunctionReturn(0); 3701a0ff6018SBarry Smith } 3702273d9f13SBarry Smith 37034a2ae208SSatish Balay #undef __FUNCT__ 3704ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR_MPIAIJ" 37057087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[]) 3706ccd8e176SBarry Smith { 3707899cda47SBarry Smith PetscInt m,cstart, cend,j,nnz,i,d; 3708899cda47SBarry Smith PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii; 3709ccd8e176SBarry Smith const PetscInt *JJ; 3710ccd8e176SBarry Smith PetscScalar *values; 3711ccd8e176SBarry Smith PetscErrorCode ierr; 3712ccd8e176SBarry Smith 3713ccd8e176SBarry Smith PetscFunctionBegin; 3714e32f2f54SBarry Smith if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]); 3715899cda47SBarry Smith 371626283091SBarry Smith ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr); 371726283091SBarry Smith ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr); 3718d0f46423SBarry Smith m = B->rmap->n; 3719d0f46423SBarry Smith cstart = B->cmap->rstart; 3720d0f46423SBarry Smith cend = B->cmap->rend; 3721d0f46423SBarry Smith rstart = B->rmap->rstart; 3722899cda47SBarry Smith 3723dcca6d9dSJed Brown ierr = PetscMalloc2(m,&d_nnz,m,&o_nnz);CHKERRQ(ierr); 3724ccd8e176SBarry Smith 3725ecc77c7aSBarry Smith #if defined(PETSC_USE_DEBUGGING) 3726ecc77c7aSBarry Smith for (i=0; i<m; i++) { 3727ecc77c7aSBarry Smith nnz = Ii[i+1]- Ii[i]; 3728ecc77c7aSBarry Smith JJ = J + Ii[i]; 3729e32f2f54SBarry Smith if (nnz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz); 3730ecc77c7aSBarry Smith if (nnz && (JJ[0] < 0)) SETERRRQ1(PETSC_ERR_ARG_WRONGSTATE,"Row %D starts with negative column index",i,j); 3731d0f46423SBarry 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); 3732ecc77c7aSBarry Smith } 3733ecc77c7aSBarry Smith #endif 3734ecc77c7aSBarry Smith 3735ccd8e176SBarry Smith for (i=0; i<m; i++) { 3736b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3737b7940d39SSatish Balay JJ = J + Ii[i]; 3738ccd8e176SBarry Smith nnz_max = PetscMax(nnz_max,nnz); 3739ccd8e176SBarry Smith d = 0; 37400daa03b5SJed Brown for (j=0; j<nnz; j++) { 37410daa03b5SJed Brown if (cstart <= JJ[j] && JJ[j] < cend) d++; 3742ccd8e176SBarry Smith } 3743ccd8e176SBarry Smith d_nnz[i] = d; 3744ccd8e176SBarry Smith o_nnz[i] = nnz - d; 3745ccd8e176SBarry Smith } 3746ccd8e176SBarry Smith ierr = MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);CHKERRQ(ierr); 37471d79065fSBarry Smith ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr); 3748ccd8e176SBarry Smith 3749ccd8e176SBarry Smith if (v) values = (PetscScalar*)v; 3750ccd8e176SBarry Smith else { 37511795a4d1SJed Brown ierr = PetscCalloc1((nnz_max+1),&values);CHKERRQ(ierr); 3752ccd8e176SBarry Smith } 3753ccd8e176SBarry Smith 3754ccd8e176SBarry Smith for (i=0; i<m; i++) { 3755ccd8e176SBarry Smith ii = i + rstart; 3756b7940d39SSatish Balay nnz = Ii[i+1]- Ii[i]; 3757b7940d39SSatish Balay ierr = MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);CHKERRQ(ierr); 3758ccd8e176SBarry Smith } 3759ccd8e176SBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3760ccd8e176SBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 3761ccd8e176SBarry Smith 3762ccd8e176SBarry Smith if (!v) { 3763ccd8e176SBarry Smith ierr = PetscFree(values);CHKERRQ(ierr); 3764ccd8e176SBarry Smith } 37657827cd58SJed Brown ierr = MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 3766ccd8e176SBarry Smith PetscFunctionReturn(0); 3767ccd8e176SBarry Smith } 3768ccd8e176SBarry Smith 3769ccd8e176SBarry Smith #undef __FUNCT__ 3770ccd8e176SBarry Smith #define __FUNCT__ "MatMPIAIJSetPreallocationCSR" 37711eea217eSSatish Balay /*@ 3772ccd8e176SBarry Smith MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format 3773ccd8e176SBarry Smith (the default parallel PETSc format). 3774ccd8e176SBarry Smith 3775ccd8e176SBarry Smith Collective on MPI_Comm 3776ccd8e176SBarry Smith 3777ccd8e176SBarry Smith Input Parameters: 3778a1661176SMatthew Knepley + B - the matrix 3779ccd8e176SBarry Smith . i - the indices into j for the start of each local row (starts with zero) 37800daa03b5SJed Brown . j - the column indices for each local row (starts with zero) 3781ccd8e176SBarry Smith - v - optional values in the matrix 3782ccd8e176SBarry Smith 3783ccd8e176SBarry Smith Level: developer 3784ccd8e176SBarry Smith 378512251496SSatish Balay Notes: 378612251496SSatish Balay The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 378712251496SSatish Balay thus you CANNOT change the matrix entries by changing the values of a[] after you have 378812251496SSatish Balay called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 378912251496SSatish Balay 379012251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 379112251496SSatish Balay 379212251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 379312251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 379412251496SSatish Balay as shown: 379512251496SSatish Balay 379612251496SSatish Balay 1 0 0 379712251496SSatish Balay 2 0 3 P0 379812251496SSatish Balay ------- 379912251496SSatish Balay 4 5 6 P1 380012251496SSatish Balay 380112251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 380212251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 380312251496SSatish Balay j = {0,0,2} [size = nz = 6] 380412251496SSatish Balay v = {1,2,3} [size = nz = 6] 380512251496SSatish Balay 380612251496SSatish Balay Process1 [P1]: rows_owned=[2] 380712251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 380812251496SSatish Balay j = {0,1,2} [size = nz = 6] 380912251496SSatish Balay v = {4,5,6} [size = nz = 6] 381012251496SSatish Balay 3811ccd8e176SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3812ccd8e176SBarry Smith 381369b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, 38148d7a6e47SBarry Smith MatCreateSeqAIJWithArrays(), MatCreateMPIAIJWithSplitArrays() 3815ccd8e176SBarry Smith @*/ 38167087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[]) 3817ccd8e176SBarry Smith { 38184ac538c5SBarry Smith PetscErrorCode ierr; 3819ccd8e176SBarry Smith 3820ccd8e176SBarry Smith PetscFunctionBegin; 38214ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));CHKERRQ(ierr); 3822ccd8e176SBarry Smith PetscFunctionReturn(0); 3823ccd8e176SBarry Smith } 3824ccd8e176SBarry Smith 3825ccd8e176SBarry Smith #undef __FUNCT__ 38264a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJSetPreallocation" 3827273d9f13SBarry Smith /*@C 3828ccd8e176SBarry Smith MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format 3829273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 3830273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 3831273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 3832273d9f13SBarry Smith performance can be increased by more than a factor of 50. 3833273d9f13SBarry Smith 3834273d9f13SBarry Smith Collective on MPI_Comm 3835273d9f13SBarry Smith 3836273d9f13SBarry Smith Input Parameters: 3837273d9f13SBarry Smith + A - the matrix 3838273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 3839273d9f13SBarry Smith (same value is used for all local rows) 3840273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 3841273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 38420298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 3843273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 38443287b5eaSJed Brown For matrices that will be factored, you must leave room for (and set) 38453287b5eaSJed Brown the diagonal entry even if it is zero. 3846273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 3847273d9f13SBarry Smith submatrix (same value is used for all local rows). 3848273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 3849273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 38500298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 3851273d9f13SBarry Smith structure. The size of this array is equal to the number 3852273d9f13SBarry Smith of local rows, i.e 'm'. 3853273d9f13SBarry Smith 385449a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 385549a6f317SBarry Smith 3856273d9f13SBarry Smith The AIJ format (also called the Yale sparse matrix format or 3857ccd8e176SBarry Smith compressed row storage (CSR)), is fully compatible with standard Fortran 77 38580598bfebSBarry Smith storage. The stored row and column indices begin with zero. 38590598bfebSBarry Smith See the <A href="../../docs/manual.pdf#nameddest=ch_mat">Mat chapter of the users manual</A> for details. 3860273d9f13SBarry Smith 3861273d9f13SBarry Smith The parallel matrix is partitioned such that the first m0 rows belong to 3862273d9f13SBarry Smith process 0, the next m1 rows belong to process 1, the next m2 rows belong 3863273d9f13SBarry Smith to process 2 etc.. where m0,m1,m2... are the input parameter 'm'. 3864273d9f13SBarry Smith 3865273d9f13SBarry Smith The DIAGONAL portion of the local submatrix of a processor can be defined 3866a05b864aSJed Brown as the submatrix which is obtained by extraction the part corresponding to 3867a05b864aSJed Brown the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the 3868a05b864aSJed Brown first row that belongs to the processor, r2 is the last row belonging to 3869a05b864aSJed Brown the this processor, and c1-c2 is range of indices of the local part of a 3870a05b864aSJed Brown vector suitable for applying the matrix to. This is an mxn matrix. In the 3871a05b864aSJed Brown common case of a square matrix, the row and column ranges are the same and 3872a05b864aSJed Brown the DIAGONAL part is also square. The remaining portion of the local 3873a05b864aSJed Brown submatrix (mxN) constitute the OFF-DIAGONAL portion. 3874273d9f13SBarry Smith 3875273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 3876273d9f13SBarry Smith 3877aa95bbe8SBarry Smith You can call MatGetInfo() to get information on how effective the preallocation was; 3878aa95bbe8SBarry Smith for example the fields mallocs,nz_allocated,nz_used,nz_unneeded; 3879aa95bbe8SBarry Smith You can also run with the option -info and look for messages with the string 3880aa95bbe8SBarry Smith malloc in them to see if additional memory allocation was needed. 3881aa95bbe8SBarry Smith 3882273d9f13SBarry Smith Example usage: 3883273d9f13SBarry Smith 3884273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 3885273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 3886273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 3887273d9f13SBarry Smith as follows: 3888273d9f13SBarry Smith 3889273d9f13SBarry Smith .vb 3890273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 3891273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 3892273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 3893273d9f13SBarry Smith ------------------------------------- 3894273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 3895273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 3896273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 3897273d9f13SBarry Smith ------------------------------------- 3898273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 3899273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 3900273d9f13SBarry Smith .ve 3901273d9f13SBarry Smith 3902273d9f13SBarry Smith This can be represented as a collection of submatrices as: 3903273d9f13SBarry Smith 3904273d9f13SBarry Smith .vb 3905273d9f13SBarry Smith A B C 3906273d9f13SBarry Smith D E F 3907273d9f13SBarry Smith G H I 3908273d9f13SBarry Smith .ve 3909273d9f13SBarry Smith 3910273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 3911273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 3912273d9f13SBarry Smith 3913273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3914273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 3915273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 3916273d9f13SBarry Smith 3917273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 3918273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 3919273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 3920273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 3921273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 3922273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 3923273d9f13SBarry Smith 3924273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 3925273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 3926273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 3927273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 3928273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 3929273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 3930273d9f13SBarry Smith .vb 3931273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 3932273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 3933273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 3934273d9f13SBarry Smith .ve 3935273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 3936273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 3937273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 3938273d9f13SBarry Smith 34 values. 3939273d9f13SBarry Smith 3940273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 3941273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 3942273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 3943273d9f13SBarry Smith .vb 3944273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 3945273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 3946273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 3947273d9f13SBarry Smith .ve 3948273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 3949273d9f13SBarry Smith hence pre-allocation is perfect. 3950273d9f13SBarry Smith 3951273d9f13SBarry Smith Level: intermediate 3952273d9f13SBarry Smith 3953273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 3954273d9f13SBarry Smith 395569b1f4b7SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateAIJ(), MatMPIAIJSetPreallocationCSR(), 3956ab978733SBarry Smith MPIAIJ, MatGetInfo(), PetscSplitOwnership() 3957273d9f13SBarry Smith @*/ 39587087cfbeSBarry Smith PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[]) 3959273d9f13SBarry Smith { 39604ac538c5SBarry Smith PetscErrorCode ierr; 3961273d9f13SBarry Smith 3962273d9f13SBarry Smith PetscFunctionBegin; 39636ba663aaSJed Brown PetscValidHeaderSpecific(B,MAT_CLASSID,1); 39646ba663aaSJed Brown PetscValidType(B,1); 39654ac538c5SBarry Smith ierr = PetscTryMethod(B,"MatMPIAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,d_nz,d_nnz,o_nz,o_nnz));CHKERRQ(ierr); 3966273d9f13SBarry Smith PetscFunctionReturn(0); 3967273d9f13SBarry Smith } 3968273d9f13SBarry Smith 39694a2ae208SSatish Balay #undef __FUNCT__ 39702fb0ec9aSBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithArrays" 397158d36128SBarry Smith /*@ 39722fb0ec9aSBarry Smith MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard 39732fb0ec9aSBarry Smith CSR format the local rows. 39742fb0ec9aSBarry Smith 39752fb0ec9aSBarry Smith Collective on MPI_Comm 39762fb0ec9aSBarry Smith 39772fb0ec9aSBarry Smith Input Parameters: 39782fb0ec9aSBarry Smith + comm - MPI communicator 39792fb0ec9aSBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 39802fb0ec9aSBarry Smith . n - This value should be the same as the local size used in creating the 39812fb0ec9aSBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 39822fb0ec9aSBarry Smith calculated if N is given) For square matrices n is almost always m. 39832fb0ec9aSBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 39842fb0ec9aSBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 39852fb0ec9aSBarry Smith . i - row indices 39862fb0ec9aSBarry Smith . j - column indices 39872fb0ec9aSBarry Smith - a - matrix values 39882fb0ec9aSBarry Smith 39892fb0ec9aSBarry Smith Output Parameter: 39902fb0ec9aSBarry Smith . mat - the matrix 399103bfb495SBarry Smith 39922fb0ec9aSBarry Smith Level: intermediate 39932fb0ec9aSBarry Smith 39942fb0ec9aSBarry Smith Notes: 39952fb0ec9aSBarry Smith The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc; 39962fb0ec9aSBarry Smith thus you CANNOT change the matrix entries by changing the values of a[] after you have 39978d7a6e47SBarry Smith called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays. 39982fb0ec9aSBarry Smith 399912251496SSatish Balay The i and j indices are 0 based, and i indices are indices corresponding to the local j array. 400012251496SSatish Balay 400112251496SSatish Balay The format which is used for the sparse matrix input, is equivalent to a 400212251496SSatish Balay row-major ordering.. i.e for the following matrix, the input data expected is 400312251496SSatish Balay as shown: 400412251496SSatish Balay 400512251496SSatish Balay 1 0 0 400612251496SSatish Balay 2 0 3 P0 400712251496SSatish Balay ------- 400812251496SSatish Balay 4 5 6 P1 400912251496SSatish Balay 401012251496SSatish Balay Process0 [P0]: rows_owned=[0,1] 401112251496SSatish Balay i = {0,1,3} [size = nrow+1 = 2+1] 401212251496SSatish Balay j = {0,0,2} [size = nz = 6] 401312251496SSatish Balay v = {1,2,3} [size = nz = 6] 401412251496SSatish Balay 401512251496SSatish Balay Process1 [P1]: rows_owned=[2] 401612251496SSatish Balay i = {0,3} [size = nrow+1 = 1+1] 401712251496SSatish Balay j = {0,1,2} [size = nz = 6] 401812251496SSatish Balay v = {4,5,6} [size = nz = 6] 40192fb0ec9aSBarry Smith 40202fb0ec9aSBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 40212fb0ec9aSBarry Smith 40222fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 402369b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays() 40242fb0ec9aSBarry Smith @*/ 40257087cfbeSBarry 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) 40262fb0ec9aSBarry Smith { 40272fb0ec9aSBarry Smith PetscErrorCode ierr; 40282fb0ec9aSBarry Smith 40292fb0ec9aSBarry Smith PetscFunctionBegin; 403069b1f4b7SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 4031e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 40322fb0ec9aSBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 4033d4146a68SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 4034a2f3521dSMark F. Adams /* ierr = MatSetBlockSizes(M,bs,cbs);CHKERRQ(ierr); */ 40352fb0ec9aSBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 40362fb0ec9aSBarry Smith ierr = MatMPIAIJSetPreallocationCSR(*mat,i,j,a);CHKERRQ(ierr); 40372fb0ec9aSBarry Smith PetscFunctionReturn(0); 40382fb0ec9aSBarry Smith } 40392fb0ec9aSBarry Smith 40402fb0ec9aSBarry Smith #undef __FUNCT__ 404169b1f4b7SBarry Smith #define __FUNCT__ "MatCreateAIJ" 4042273d9f13SBarry Smith /*@C 404369b1f4b7SBarry Smith MatCreateAIJ - Creates a sparse parallel matrix in AIJ format 4044273d9f13SBarry Smith (the default parallel PETSc format). For good matrix assembly performance 4045273d9f13SBarry Smith the user should preallocate the matrix storage by setting the parameters 4046273d9f13SBarry Smith d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately, 4047273d9f13SBarry Smith performance can be increased by more than a factor of 50. 4048273d9f13SBarry Smith 4049273d9f13SBarry Smith Collective on MPI_Comm 4050273d9f13SBarry Smith 4051273d9f13SBarry Smith Input Parameters: 4052273d9f13SBarry Smith + comm - MPI communicator 4053273d9f13SBarry Smith . m - number of local rows (or PETSC_DECIDE to have calculated if M is given) 4054273d9f13SBarry Smith This value should be the same as the local size used in creating the 4055273d9f13SBarry Smith y vector for the matrix-vector product y = Ax. 4056273d9f13SBarry Smith . n - This value should be the same as the local size used in creating the 4057273d9f13SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 4058273d9f13SBarry Smith calculated if N is given) For square matrices n is almost always m. 4059273d9f13SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 4060273d9f13SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 4061273d9f13SBarry Smith . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix 4062273d9f13SBarry Smith (same value is used for all local rows) 4063273d9f13SBarry Smith . d_nnz - array containing the number of nonzeros in the various rows of the 4064273d9f13SBarry Smith DIAGONAL portion of the local submatrix (possibly different for each row) 40650298fd71SBarry Smith or NULL, if d_nz is used to specify the nonzero structure. 4066273d9f13SBarry Smith The size of this array is equal to the number of local rows, i.e 'm'. 4067273d9f13SBarry Smith . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local 4068273d9f13SBarry Smith submatrix (same value is used for all local rows). 4069273d9f13SBarry Smith - o_nnz - array containing the number of nonzeros in the various rows of the 4070273d9f13SBarry Smith OFF-DIAGONAL portion of the local submatrix (possibly different for 40710298fd71SBarry Smith each row) or NULL, if o_nz is used to specify the nonzero 4072273d9f13SBarry Smith structure. The size of this array is equal to the number 4073273d9f13SBarry Smith of local rows, i.e 'm'. 4074273d9f13SBarry Smith 4075273d9f13SBarry Smith Output Parameter: 4076273d9f13SBarry Smith . A - the matrix 4077273d9f13SBarry Smith 4078175b88e8SBarry Smith It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(), 4079ae1d86c5SBarry Smith MatXXXXSetPreallocation() paradgm instead of this routine directly. 4080175b88e8SBarry Smith [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation] 4081175b88e8SBarry Smith 4082273d9f13SBarry Smith Notes: 408349a6f317SBarry Smith If the *_nnz parameter is given then the *_nz parameter is ignored 408449a6f317SBarry Smith 4085273d9f13SBarry Smith m,n,M,N parameters specify the size of the matrix, and its partitioning across 4086273d9f13SBarry Smith processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate 4087273d9f13SBarry Smith storage requirements for this matrix. 4088273d9f13SBarry Smith 4089273d9f13SBarry Smith If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one 4090273d9f13SBarry Smith processor than it must be used on all processors that share the object for 4091273d9f13SBarry Smith that argument. 4092273d9f13SBarry Smith 4093273d9f13SBarry Smith The user MUST specify either the local or global matrix dimensions 4094273d9f13SBarry Smith (possibly both). 4095273d9f13SBarry Smith 409633a7c187SSatish Balay The parallel matrix is partitioned across processors such that the 409733a7c187SSatish Balay first m0 rows belong to process 0, the next m1 rows belong to 409833a7c187SSatish Balay process 1, the next m2 rows belong to process 2 etc.. where 409933a7c187SSatish Balay m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores 410033a7c187SSatish Balay values corresponding to [m x N] submatrix. 4101273d9f13SBarry Smith 410233a7c187SSatish Balay The columns are logically partitioned with the n0 columns belonging 410333a7c187SSatish Balay to 0th partition, the next n1 columns belonging to the next 410433a7c187SSatish Balay partition etc.. where n0,n1,n2... are the the input parameter 'n'. 410533a7c187SSatish Balay 410633a7c187SSatish Balay The DIAGONAL portion of the local submatrix on any given processor 410733a7c187SSatish Balay is the submatrix corresponding to the rows and columns m,n 410833a7c187SSatish Balay corresponding to the given processor. i.e diagonal matrix on 410933a7c187SSatish Balay process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1] 411033a7c187SSatish Balay etc. The remaining portion of the local submatrix [m x (N-n)] 411133a7c187SSatish Balay constitute the OFF-DIAGONAL portion. The example below better 411233a7c187SSatish Balay illustrates this concept. 411333a7c187SSatish Balay 411433a7c187SSatish Balay For a square global matrix we define each processor's diagonal portion 411533a7c187SSatish Balay to be its local rows and the corresponding columns (a square submatrix); 411633a7c187SSatish Balay each processor's off-diagonal portion encompasses the remainder of the 411733a7c187SSatish Balay local matrix (a rectangular submatrix). 4118273d9f13SBarry Smith 4119273d9f13SBarry Smith If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored. 4120273d9f13SBarry Smith 412197d05335SKris Buschelman When calling this routine with a single process communicator, a matrix of 412297d05335SKris Buschelman type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this 412397d05335SKris Buschelman type of communicator, use the construction mechanism: 412478102f6cSMatthew Knepley MatCreate(...,&A); MatSetType(A,MATMPIAIJ); MatSetSizes(A, m,n,M,N); MatMPIAIJSetPreallocation(A,...); 412597d05335SKris Buschelman 4126273d9f13SBarry Smith By default, this format uses inodes (identical nodes) when possible. 4127273d9f13SBarry Smith We search for consecutive rows with the same nonzero structure, thereby 4128273d9f13SBarry Smith reusing matrix information to achieve increased efficiency. 4129273d9f13SBarry Smith 4130273d9f13SBarry Smith Options Database Keys: 4131923f20ffSKris Buschelman + -mat_no_inode - Do not use inodes 4132923f20ffSKris Buschelman . -mat_inode_limit <limit> - Sets inode limit (max limit=5) 4133273d9f13SBarry Smith - -mat_aij_oneindex - Internally use indexing starting at 1 4134273d9f13SBarry Smith rather than 0. Note that when calling MatSetValues(), 4135273d9f13SBarry Smith the user still MUST index entries starting at 0! 4136273d9f13SBarry Smith 4137273d9f13SBarry Smith 4138273d9f13SBarry Smith Example usage: 4139273d9f13SBarry Smith 4140273d9f13SBarry Smith Consider the following 8x8 matrix with 34 non-zero values, that is 4141273d9f13SBarry Smith assembled across 3 processors. Lets assume that proc0 owns 3 rows, 4142273d9f13SBarry Smith proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown 4143273d9f13SBarry Smith as follows: 4144273d9f13SBarry Smith 4145273d9f13SBarry Smith .vb 4146273d9f13SBarry Smith 1 2 0 | 0 3 0 | 0 4 4147273d9f13SBarry Smith Proc0 0 5 6 | 7 0 0 | 8 0 4148273d9f13SBarry Smith 9 0 10 | 11 0 0 | 12 0 4149273d9f13SBarry Smith ------------------------------------- 4150273d9f13SBarry Smith 13 0 14 | 15 16 17 | 0 0 4151273d9f13SBarry Smith Proc1 0 18 0 | 19 20 21 | 0 0 4152273d9f13SBarry Smith 0 0 0 | 22 23 0 | 24 0 4153273d9f13SBarry Smith ------------------------------------- 4154273d9f13SBarry Smith Proc2 25 26 27 | 0 0 28 | 29 0 4155273d9f13SBarry Smith 30 0 0 | 31 32 33 | 0 34 4156273d9f13SBarry Smith .ve 4157273d9f13SBarry Smith 4158273d9f13SBarry Smith This can be represented as a collection of submatrices as: 4159273d9f13SBarry Smith 4160273d9f13SBarry Smith .vb 4161273d9f13SBarry Smith A B C 4162273d9f13SBarry Smith D E F 4163273d9f13SBarry Smith G H I 4164273d9f13SBarry Smith .ve 4165273d9f13SBarry Smith 4166273d9f13SBarry Smith Where the submatrices A,B,C are owned by proc0, D,E,F are 4167273d9f13SBarry Smith owned by proc1, G,H,I are owned by proc2. 4168273d9f13SBarry Smith 4169273d9f13SBarry Smith The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4170273d9f13SBarry Smith The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively. 4171273d9f13SBarry Smith The 'M','N' parameters are 8,8, and have the same values on all procs. 4172273d9f13SBarry Smith 4173273d9f13SBarry Smith The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are 4174273d9f13SBarry Smith submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices 4175273d9f13SBarry Smith corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively. 4176273d9f13SBarry Smith Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL 4177273d9f13SBarry Smith part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ 4178273d9f13SBarry Smith matrix, ans [DF] as another SeqAIJ matrix. 4179273d9f13SBarry Smith 4180273d9f13SBarry Smith When d_nz, o_nz parameters are specified, d_nz storage elements are 4181273d9f13SBarry Smith allocated for every row of the local diagonal submatrix, and o_nz 4182273d9f13SBarry Smith storage locations are allocated for every row of the OFF-DIAGONAL submat. 4183273d9f13SBarry Smith One way to choose d_nz and o_nz is to use the max nonzerors per local 4184273d9f13SBarry Smith rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices. 4185273d9f13SBarry Smith In this case, the values of d_nz,o_nz are: 4186273d9f13SBarry Smith .vb 4187273d9f13SBarry Smith proc0 : dnz = 2, o_nz = 2 4188273d9f13SBarry Smith proc1 : dnz = 3, o_nz = 2 4189273d9f13SBarry Smith proc2 : dnz = 1, o_nz = 4 4190273d9f13SBarry Smith .ve 4191273d9f13SBarry Smith We are allocating m*(d_nz+o_nz) storage locations for every proc. This 4192273d9f13SBarry Smith translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10 4193273d9f13SBarry Smith for proc3. i.e we are using 12+15+10=37 storage locations to store 4194273d9f13SBarry Smith 34 values. 4195273d9f13SBarry Smith 4196273d9f13SBarry Smith When d_nnz, o_nnz parameters are specified, the storage is specified 4197273d9f13SBarry Smith for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices. 4198273d9f13SBarry Smith In the above case the values for d_nnz,o_nnz are: 4199273d9f13SBarry Smith .vb 4200273d9f13SBarry Smith proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2] 4201273d9f13SBarry Smith proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1] 4202273d9f13SBarry Smith proc2: d_nnz = [1,1] and o_nnz = [4,4] 4203273d9f13SBarry Smith .ve 4204273d9f13SBarry Smith Here the space allocated is sum of all the above values i.e 34, and 4205273d9f13SBarry Smith hence pre-allocation is perfect. 4206273d9f13SBarry Smith 4207273d9f13SBarry Smith Level: intermediate 4208273d9f13SBarry Smith 4209273d9f13SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 4210273d9f13SBarry Smith 4211ccd8e176SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 42122fb0ec9aSBarry Smith MPIAIJ, MatCreateMPIAIJWithArrays() 4213273d9f13SBarry Smith @*/ 421469b1f4b7SBarry 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) 4215273d9f13SBarry Smith { 42166849ba73SBarry Smith PetscErrorCode ierr; 4217b1d57f15SBarry Smith PetscMPIInt size; 4218273d9f13SBarry Smith 4219273d9f13SBarry Smith PetscFunctionBegin; 4220f69a0ea3SMatthew Knepley ierr = MatCreate(comm,A);CHKERRQ(ierr); 4221f69a0ea3SMatthew Knepley ierr = MatSetSizes(*A,m,n,M,N);CHKERRQ(ierr); 4222273d9f13SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4223273d9f13SBarry Smith if (size > 1) { 4224273d9f13SBarry Smith ierr = MatSetType(*A,MATMPIAIJ);CHKERRQ(ierr); 4225273d9f13SBarry Smith ierr = MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);CHKERRQ(ierr); 4226273d9f13SBarry Smith } else { 4227273d9f13SBarry Smith ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr); 4228273d9f13SBarry Smith ierr = MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);CHKERRQ(ierr); 4229273d9f13SBarry Smith } 4230273d9f13SBarry Smith PetscFunctionReturn(0); 4231273d9f13SBarry Smith } 4232195d93cdSBarry Smith 42334a2ae208SSatish Balay #undef __FUNCT__ 42344a2ae208SSatish Balay #define __FUNCT__ "MatMPIAIJGetSeqAIJ" 42359230625dSJed Brown PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[]) 4236195d93cdSBarry Smith { 4237195d93cdSBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4238b1d57f15SBarry Smith 4239195d93cdSBarry Smith PetscFunctionBegin; 4240195d93cdSBarry Smith *Ad = a->A; 4241195d93cdSBarry Smith *Ao = a->B; 4242195d93cdSBarry Smith *colmap = a->garray; 4243195d93cdSBarry Smith PetscFunctionReturn(0); 4244195d93cdSBarry Smith } 4245a2243be0SBarry Smith 4246a2243be0SBarry Smith #undef __FUNCT__ 4247a2243be0SBarry Smith #define __FUNCT__ "MatSetColoring_MPIAIJ" 4248dfbe8321SBarry Smith PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring) 4249a2243be0SBarry Smith { 4250dfbe8321SBarry Smith PetscErrorCode ierr; 4251b1d57f15SBarry Smith PetscInt i; 4252a2243be0SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4253a2243be0SBarry Smith 4254a2243be0SBarry Smith PetscFunctionBegin; 42558ee2e534SBarry Smith if (coloring->ctype == IS_COLORING_GLOBAL) { 425608b6dcc0SBarry Smith ISColoringValue *allcolors,*colors; 4257a2243be0SBarry Smith ISColoring ocoloring; 4258a2243be0SBarry Smith 4259a2243be0SBarry Smith /* set coloring for diagonal portion */ 4260a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,coloring);CHKERRQ(ierr); 4261a2243be0SBarry Smith 4262a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4263ce94432eSBarry Smith ierr = ISAllGatherColors(PetscObjectComm((PetscObject)A),coloring->n,coloring->colors,NULL,&allcolors);CHKERRQ(ierr); 4264785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr); 4265d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4266a2243be0SBarry Smith colors[i] = allcolors[a->garray[i]]; 4267a2243be0SBarry Smith } 4268a2243be0SBarry Smith ierr = PetscFree(allcolors);CHKERRQ(ierr); 4269d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4270a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 42716bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4272a2243be0SBarry Smith } else if (coloring->ctype == IS_COLORING_GHOSTED) { 427308b6dcc0SBarry Smith ISColoringValue *colors; 4274b1d57f15SBarry Smith PetscInt *larray; 4275a2243be0SBarry Smith ISColoring ocoloring; 4276a2243be0SBarry Smith 4277a2243be0SBarry Smith /* set coloring for diagonal portion */ 4278785e854fSJed Brown ierr = PetscMalloc1((a->A->cmap->n+1),&larray);CHKERRQ(ierr); 4279d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4280d0f46423SBarry Smith larray[i] = i + A->cmap->rstart; 4281a2243be0SBarry Smith } 42820298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->A->cmap->n,larray,NULL,larray);CHKERRQ(ierr); 4283785e854fSJed Brown ierr = PetscMalloc1((a->A->cmap->n+1),&colors);CHKERRQ(ierr); 4284d0f46423SBarry Smith for (i=0; i<a->A->cmap->n; i++) { 4285a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4286a2243be0SBarry Smith } 4287a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4288d0f46423SBarry Smith ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4289a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->A,ocoloring);CHKERRQ(ierr); 42906bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 4291a2243be0SBarry Smith 4292a2243be0SBarry Smith /* set coloring for off-diagonal portion */ 4293785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&larray);CHKERRQ(ierr); 42940298fd71SBarry Smith ierr = ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,a->B->cmap->n,a->garray,NULL,larray);CHKERRQ(ierr); 4295785e854fSJed Brown ierr = PetscMalloc1((a->B->cmap->n+1),&colors);CHKERRQ(ierr); 4296d0f46423SBarry Smith for (i=0; i<a->B->cmap->n; i++) { 4297a2243be0SBarry Smith colors[i] = coloring->colors[larray[i]]; 4298a2243be0SBarry Smith } 4299a2243be0SBarry Smith ierr = PetscFree(larray);CHKERRQ(ierr); 4300d0f46423SBarry Smith ierr = ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap->n,colors,&ocoloring);CHKERRQ(ierr); 4301a2243be0SBarry Smith ierr = MatSetColoring_SeqAIJ(a->B,ocoloring);CHKERRQ(ierr); 43026bf464f9SBarry Smith ierr = ISColoringDestroy(&ocoloring);CHKERRQ(ierr); 43036bf464f9SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype); 4304a2243be0SBarry Smith PetscFunctionReturn(0); 4305a2243be0SBarry Smith } 4306a2243be0SBarry Smith 4307779c1a83SBarry Smith #undef __FUNCT__ 4308779c1a83SBarry Smith #define __FUNCT__ "MatSetValuesAdifor_MPIAIJ" 4309b1d57f15SBarry Smith PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues) 4310779c1a83SBarry Smith { 4311779c1a83SBarry Smith Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data; 4312dfbe8321SBarry Smith PetscErrorCode ierr; 4313779c1a83SBarry Smith 4314779c1a83SBarry Smith PetscFunctionBegin; 4315779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);CHKERRQ(ierr); 4316779c1a83SBarry Smith ierr = MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);CHKERRQ(ierr); 4317a2243be0SBarry Smith PetscFunctionReturn(0); 4318a2243be0SBarry Smith } 4319c5d6d63eSBarry Smith 4320c5d6d63eSBarry Smith #undef __FUNCT__ 432190431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJSymbolic" 432290431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJSymbolic(MPI_Comm comm,Mat inmat,PetscInt n,Mat *outmat) 43239b8102ccSHong Zhang { 43249b8102ccSHong Zhang PetscErrorCode ierr; 4325a2f3521dSMark F. Adams PetscInt m,N,i,rstart,nnz,*dnz,*onz,sum,bs,cbs; 43269b8102ccSHong Zhang PetscInt *indx; 43279b8102ccSHong Zhang 43289b8102ccSHong Zhang PetscFunctionBegin; 43299b8102ccSHong Zhang /* This routine will ONLY return MPIAIJ type matrix */ 43309b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 4331a2f3521dSMark F. Adams ierr = MatGetBlockSizes(inmat,&bs,&cbs);CHKERRQ(ierr); 43329b8102ccSHong Zhang if (n == PETSC_DECIDE) { 43339b8102ccSHong Zhang ierr = PetscSplitOwnership(comm,&n,&N);CHKERRQ(ierr); 43349b8102ccSHong Zhang } 4335a22543b6SHong Zhang /* Check sum(n) = N */ 4336a95133b1SBarry Smith ierr = MPI_Allreduce(&n,&sum,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 4337a22543b6SHong Zhang if (sum != N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local columns != global columns %d",N); 4338a22543b6SHong Zhang 43399b8102ccSHong Zhang ierr = MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 43409b8102ccSHong Zhang rstart -= m; 43419b8102ccSHong Zhang 43429b8102ccSHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 43439b8102ccSHong Zhang for (i=0; i<m; i++) { 43440298fd71SBarry Smith ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 43459b8102ccSHong Zhang ierr = MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);CHKERRQ(ierr); 43460298fd71SBarry Smith ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,NULL);CHKERRQ(ierr); 43479b8102ccSHong Zhang } 43489b8102ccSHong Zhang 43499b8102ccSHong Zhang ierr = MatCreate(comm,outmat);CHKERRQ(ierr); 43509b8102ccSHong Zhang ierr = MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 4351a2f3521dSMark F. Adams ierr = MatSetBlockSizes(*outmat,bs,cbs);CHKERRQ(ierr); 43529b8102ccSHong Zhang ierr = MatSetType(*outmat,MATMPIAIJ);CHKERRQ(ierr); 43539b8102ccSHong Zhang ierr = MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);CHKERRQ(ierr); 43549b8102ccSHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 43559b8102ccSHong Zhang PetscFunctionReturn(0); 43569b8102ccSHong Zhang } 43579b8102ccSHong Zhang 43589b8102ccSHong Zhang #undef __FUNCT__ 435990431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJNumeric" 436090431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJNumeric(MPI_Comm comm,Mat inmat,PetscInt n,Mat outmat) 43619b8102ccSHong Zhang { 43629b8102ccSHong Zhang PetscErrorCode ierr; 43639b8102ccSHong Zhang PetscInt m,N,i,rstart,nnz,Ii; 43649b8102ccSHong Zhang PetscInt *indx; 43659b8102ccSHong Zhang PetscScalar *values; 43669b8102ccSHong Zhang 43679b8102ccSHong Zhang PetscFunctionBegin; 43689b8102ccSHong Zhang ierr = MatGetSize(inmat,&m,&N);CHKERRQ(ierr); 43690298fd71SBarry Smith ierr = MatGetOwnershipRange(outmat,&rstart,NULL);CHKERRQ(ierr); 43709b8102ccSHong Zhang for (i=0; i<m; i++) { 43719b8102ccSHong Zhang ierr = MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 43729b8102ccSHong Zhang Ii = i + rstart; 43733c79b8e7SHong Zhang ierr = MatSetValues(outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 43749b8102ccSHong Zhang ierr = MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);CHKERRQ(ierr); 43759b8102ccSHong Zhang } 43769b8102ccSHong Zhang ierr = MatAssemblyBegin(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 43779b8102ccSHong Zhang ierr = MatAssemblyEnd(outmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 43789b8102ccSHong Zhang PetscFunctionReturn(0); 43799b8102ccSHong Zhang } 43809b8102ccSHong Zhang 43819b8102ccSHong Zhang #undef __FUNCT__ 438290431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJConcatenateSeqAIJ" 4383bc08b0f1SBarry Smith /*@ 438490431a8fSHong Zhang MatCreateMPIAIJConcatenateSeqAIJ - Creates a single large PETSc matrix by concatenating sequential 438551dd7536SBarry Smith matrices from each processor 4386c5d6d63eSBarry Smith 4387c5d6d63eSBarry Smith Collective on MPI_Comm 4388c5d6d63eSBarry Smith 4389c5d6d63eSBarry Smith Input Parameters: 439051dd7536SBarry Smith + comm - the communicators the parallel matrix will live on 4391d6bb3c2dSHong Zhang . inmat - the input sequential matrices 43920e36024fSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4393d6bb3c2dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 439451dd7536SBarry Smith 439551dd7536SBarry Smith Output Parameter: 439651dd7536SBarry Smith . outmat - the parallel matrix generated 4397c5d6d63eSBarry Smith 43987e25d530SSatish Balay Level: advanced 43997e25d530SSatish Balay 4400f08fae4eSHong Zhang Notes: The number of columns of the matrix in EACH processor MUST be the same. 4401c5d6d63eSBarry Smith 4402c5d6d63eSBarry Smith @*/ 440390431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJConcatenateSeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat) 4404c5d6d63eSBarry Smith { 4405dfbe8321SBarry Smith PetscErrorCode ierr; 4406f4703a44SHong Zhang PetscMPIInt size; 4407c5d6d63eSBarry Smith 4408c5d6d63eSBarry Smith PetscFunctionBegin; 4409f4703a44SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 44109b8102ccSHong Zhang ierr = PetscLogEventBegin(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4411f4703a44SHong Zhang if (size == 1) { 4412f4703a44SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 4413f4703a44SHong Zhang ierr = MatDuplicate(inmat,MAT_COPY_VALUES,outmat);CHKERRQ(ierr); 4414f4703a44SHong Zhang } else { 4415f4703a44SHong Zhang ierr = MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 4416f4703a44SHong Zhang } 4417f4703a44SHong Zhang } else { 4418d6bb3c2dSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 441990431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJSymbolic(comm,inmat,n,outmat);CHKERRQ(ierr); 44200e36024fSHong Zhang } 442190431a8fSHong Zhang ierr = MatCreateMPIAIJConcatenateSeqAIJNumeric(comm,inmat,n,*outmat);CHKERRQ(ierr); 4422f4703a44SHong Zhang } 44239b8102ccSHong Zhang ierr = PetscLogEventEnd(MAT_Merge,inmat,0,0,0);CHKERRQ(ierr); 4424c5d6d63eSBarry Smith PetscFunctionReturn(0); 4425c5d6d63eSBarry Smith } 4426c5d6d63eSBarry Smith 4427c5d6d63eSBarry Smith #undef __FUNCT__ 4428c5d6d63eSBarry Smith #define __FUNCT__ "MatFileSplit" 4429dfbe8321SBarry Smith PetscErrorCode MatFileSplit(Mat A,char *outfile) 4430c5d6d63eSBarry Smith { 4431dfbe8321SBarry Smith PetscErrorCode ierr; 443232dcc486SBarry Smith PetscMPIInt rank; 4433b1d57f15SBarry Smith PetscInt m,N,i,rstart,nnz; 4434de4209c5SBarry Smith size_t len; 4435b1d57f15SBarry Smith const PetscInt *indx; 4436c5d6d63eSBarry Smith PetscViewer out; 4437c5d6d63eSBarry Smith char *name; 4438c5d6d63eSBarry Smith Mat B; 4439b3cc6726SBarry Smith const PetscScalar *values; 4440c5d6d63eSBarry Smith 4441c5d6d63eSBarry Smith PetscFunctionBegin; 4442c5d6d63eSBarry Smith ierr = MatGetLocalSize(A,&m,0);CHKERRQ(ierr); 4443c5d6d63eSBarry Smith ierr = MatGetSize(A,0,&N);CHKERRQ(ierr); 4444f204ca49SKris Buschelman /* Should this be the type of the diagonal block of A? */ 4445f69a0ea3SMatthew Knepley ierr = MatCreate(PETSC_COMM_SELF,&B);CHKERRQ(ierr); 4446f69a0ea3SMatthew Knepley ierr = MatSetSizes(B,m,N,m,N);CHKERRQ(ierr); 4447a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);CHKERRQ(ierr); 4448f204ca49SKris Buschelman ierr = MatSetType(B,MATSEQAIJ);CHKERRQ(ierr); 44490298fd71SBarry Smith ierr = MatSeqAIJSetPreallocation(B,0,NULL);CHKERRQ(ierr); 4450c5d6d63eSBarry Smith ierr = MatGetOwnershipRange(A,&rstart,0);CHKERRQ(ierr); 4451c5d6d63eSBarry Smith for (i=0; i<m; i++) { 4452c5d6d63eSBarry Smith ierr = MatGetRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4453c5d6d63eSBarry Smith ierr = MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);CHKERRQ(ierr); 4454c5d6d63eSBarry Smith ierr = MatRestoreRow(A,i+rstart,&nnz,&indx,&values);CHKERRQ(ierr); 4455c5d6d63eSBarry Smith } 4456c5d6d63eSBarry Smith ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4457c5d6d63eSBarry Smith ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 4458c5d6d63eSBarry Smith 4459ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);CHKERRQ(ierr); 4460c5d6d63eSBarry Smith ierr = PetscStrlen(outfile,&len);CHKERRQ(ierr); 4461785e854fSJed Brown ierr = PetscMalloc1((len+5),&name);CHKERRQ(ierr); 4462c5d6d63eSBarry Smith sprintf(name,"%s.%d",outfile,rank); 4463852598b0SBarry Smith ierr = PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);CHKERRQ(ierr); 4464a2ea699eSBarry Smith ierr = PetscFree(name);CHKERRQ(ierr); 4465c5d6d63eSBarry Smith ierr = MatView(B,out);CHKERRQ(ierr); 44666bf464f9SBarry Smith ierr = PetscViewerDestroy(&out);CHKERRQ(ierr); 44676bf464f9SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 4468c5d6d63eSBarry Smith PetscFunctionReturn(0); 4469c5d6d63eSBarry Smith } 4470e5f2cdd8SHong Zhang 447109573ac7SBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ(Mat); 447251a7d1a8SHong Zhang #undef __FUNCT__ 447351a7d1a8SHong Zhang #define __FUNCT__ "MatDestroy_MPIAIJ_SeqsToMPI" 44747087cfbeSBarry Smith PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A) 447551a7d1a8SHong Zhang { 447651a7d1a8SHong Zhang PetscErrorCode ierr; 4477671beff6SHong Zhang Mat_Merge_SeqsToMPI *merge; 4478776b82aeSLisandro Dalcin PetscContainer container; 447951a7d1a8SHong Zhang 448051a7d1a8SHong Zhang PetscFunctionBegin; 4481671beff6SHong Zhang ierr = PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4482671beff6SHong Zhang if (container) { 4483776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 448451a7d1a8SHong Zhang ierr = PetscFree(merge->id_r);CHKERRQ(ierr); 44853e06a4e6SHong Zhang ierr = PetscFree(merge->len_s);CHKERRQ(ierr); 44863e06a4e6SHong Zhang ierr = PetscFree(merge->len_r);CHKERRQ(ierr); 448751a7d1a8SHong Zhang ierr = PetscFree(merge->bi);CHKERRQ(ierr); 448851a7d1a8SHong Zhang ierr = PetscFree(merge->bj);CHKERRQ(ierr); 4489533163c2SBarry Smith ierr = PetscFree(merge->buf_ri[0]);CHKERRQ(ierr); 449002c68681SHong Zhang ierr = PetscFree(merge->buf_ri);CHKERRQ(ierr); 4491533163c2SBarry Smith ierr = PetscFree(merge->buf_rj[0]);CHKERRQ(ierr); 449202c68681SHong Zhang ierr = PetscFree(merge->buf_rj);CHKERRQ(ierr); 449305b42c5fSBarry Smith ierr = PetscFree(merge->coi);CHKERRQ(ierr); 449405b42c5fSBarry Smith ierr = PetscFree(merge->coj);CHKERRQ(ierr); 449505b42c5fSBarry Smith ierr = PetscFree(merge->owners_co);CHKERRQ(ierr); 44966bf464f9SBarry Smith ierr = PetscLayoutDestroy(&merge->rowmap);CHKERRQ(ierr); 4497bf0cc555SLisandro Dalcin ierr = PetscFree(merge);CHKERRQ(ierr); 4498671beff6SHong Zhang ierr = PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);CHKERRQ(ierr); 4499671beff6SHong Zhang } 450051a7d1a8SHong Zhang ierr = MatDestroy_MPIAIJ(A);CHKERRQ(ierr); 450151a7d1a8SHong Zhang PetscFunctionReturn(0); 450251a7d1a8SHong Zhang } 450351a7d1a8SHong Zhang 4504c6db04a5SJed Brown #include <../src/mat/utils/freespace.h> 4505c6db04a5SJed Brown #include <petscbt.h> 45064ebed01fSBarry Smith 4507e5f2cdd8SHong Zhang #undef __FUNCT__ 450890431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJNumeric" 450990431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJNumeric(Mat seqmat,Mat mpimat) 451055d1abb9SHong Zhang { 451155d1abb9SHong Zhang PetscErrorCode ierr; 4512ce94432eSBarry Smith MPI_Comm comm; 451355d1abb9SHong Zhang Mat_SeqAIJ *a =(Mat_SeqAIJ*)seqmat->data; 4514b1d57f15SBarry Smith PetscMPIInt size,rank,taga,*len_s; 4515a2ea699eSBarry Smith PetscInt N=mpimat->cmap->N,i,j,*owners,*ai=a->i,*aj; 4516b1d57f15SBarry Smith PetscInt proc,m; 4517b1d57f15SBarry Smith PetscInt **buf_ri,**buf_rj; 4518b1d57f15SBarry Smith PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj; 4519b1d57f15SBarry Smith PetscInt nrows,**buf_ri_k,**nextrow,**nextai; 452055d1abb9SHong Zhang MPI_Request *s_waits,*r_waits; 452155d1abb9SHong Zhang MPI_Status *status; 4522a77337e4SBarry Smith MatScalar *aa=a->a; 4523dd6ea824SBarry Smith MatScalar **abuf_r,*ba_i; 452455d1abb9SHong Zhang Mat_Merge_SeqsToMPI *merge; 4525776b82aeSLisandro Dalcin PetscContainer container; 452655d1abb9SHong Zhang 452755d1abb9SHong Zhang PetscFunctionBegin; 4528bedda5b1SHong Zhang ierr = PetscObjectGetComm((PetscObject)mpimat,&comm);CHKERRQ(ierr); 45294ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 45303c2c1871SHong Zhang 453155d1abb9SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 453255d1abb9SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 453355d1abb9SHong Zhang 453455d1abb9SHong Zhang ierr = PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject*)&container);CHKERRQ(ierr); 4535776b82aeSLisandro Dalcin ierr = PetscContainerGetPointer(container,(void**)&merge);CHKERRQ(ierr); 4536bf0cc555SLisandro Dalcin 453755d1abb9SHong Zhang bi = merge->bi; 453855d1abb9SHong Zhang bj = merge->bj; 453955d1abb9SHong Zhang buf_ri = merge->buf_ri; 454055d1abb9SHong Zhang buf_rj = merge->buf_rj; 454155d1abb9SHong Zhang 4542785e854fSJed Brown ierr = PetscMalloc1(size,&status);CHKERRQ(ierr); 45437a2fc3feSBarry Smith owners = merge->rowmap->range; 454455d1abb9SHong Zhang len_s = merge->len_s; 454555d1abb9SHong Zhang 454655d1abb9SHong Zhang /* send and recv matrix values */ 454755d1abb9SHong Zhang /*-----------------------------*/ 4548357abbc8SBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mpimat,&taga);CHKERRQ(ierr); 454955d1abb9SHong Zhang ierr = PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);CHKERRQ(ierr); 455055d1abb9SHong Zhang 4551785e854fSJed Brown ierr = PetscMalloc1((merge->nsend+1),&s_waits);CHKERRQ(ierr); 455255d1abb9SHong Zhang for (proc=0,k=0; proc<size; proc++) { 455355d1abb9SHong Zhang if (!len_s[proc]) continue; 455455d1abb9SHong Zhang i = owners[proc]; 455555d1abb9SHong Zhang ierr = MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);CHKERRQ(ierr); 455655d1abb9SHong Zhang k++; 455755d1abb9SHong Zhang } 455855d1abb9SHong Zhang 45590c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,r_waits,status);CHKERRQ(ierr);} 45600c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,s_waits,status);CHKERRQ(ierr);} 456155d1abb9SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 456255d1abb9SHong Zhang 456355d1abb9SHong Zhang ierr = PetscFree(s_waits);CHKERRQ(ierr); 456455d1abb9SHong Zhang ierr = PetscFree(r_waits);CHKERRQ(ierr); 456555d1abb9SHong Zhang 456655d1abb9SHong Zhang /* insert mat values of mpimat */ 456755d1abb9SHong Zhang /*----------------------------*/ 4568785e854fSJed Brown ierr = PetscMalloc1(N,&ba_i);CHKERRQ(ierr); 4569dcca6d9dSJed Brown ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr); 457055d1abb9SHong Zhang 457155d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { 457255d1abb9SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 457355d1abb9SHong Zhang nrows = *(buf_ri_k[k]); 457455d1abb9SHong Zhang nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */ 457555d1abb9SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 457655d1abb9SHong Zhang } 457755d1abb9SHong Zhang 457855d1abb9SHong Zhang /* set values of ba */ 45797a2fc3feSBarry Smith m = merge->rowmap->n; 458055d1abb9SHong Zhang for (i=0; i<m; i++) { 458155d1abb9SHong Zhang arow = owners[rank] + i; 458255d1abb9SHong Zhang bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */ 458355d1abb9SHong Zhang bnzi = bi[i+1] - bi[i]; 4584a77337e4SBarry Smith ierr = PetscMemzero(ba_i,bnzi*sizeof(PetscScalar));CHKERRQ(ierr); 458555d1abb9SHong Zhang 458655d1abb9SHong Zhang /* add local non-zero vals of this proc's seqmat into ba */ 458755d1abb9SHong Zhang anzi = ai[arow+1] - ai[arow]; 458855d1abb9SHong Zhang aj = a->j + ai[arow]; 458955d1abb9SHong Zhang aa = a->a + ai[arow]; 459055d1abb9SHong Zhang nextaj = 0; 459155d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 459255d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 459355d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 459455d1abb9SHong Zhang } 459555d1abb9SHong Zhang } 459655d1abb9SHong Zhang 459755d1abb9SHong Zhang /* add received vals into ba */ 459855d1abb9SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 459955d1abb9SHong Zhang /* i-th row */ 460055d1abb9SHong Zhang if (i == *nextrow[k]) { 460155d1abb9SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 460255d1abb9SHong Zhang aj = buf_rj[k] + *(nextai[k]); 460355d1abb9SHong Zhang aa = abuf_r[k] + *(nextai[k]); 460455d1abb9SHong Zhang nextaj = 0; 460555d1abb9SHong Zhang for (j=0; nextaj<anzi; j++) { 460655d1abb9SHong Zhang if (*(bj_i + j) == aj[nextaj]) { /* bcol == acol */ 460755d1abb9SHong Zhang ba_i[j] += aa[nextaj++]; 460855d1abb9SHong Zhang } 460955d1abb9SHong Zhang } 461055d1abb9SHong Zhang nextrow[k]++; nextai[k]++; 461155d1abb9SHong Zhang } 461255d1abb9SHong Zhang } 461355d1abb9SHong Zhang ierr = MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);CHKERRQ(ierr); 461455d1abb9SHong Zhang } 461555d1abb9SHong Zhang ierr = MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 461655d1abb9SHong Zhang ierr = MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 461755d1abb9SHong Zhang 4618533163c2SBarry Smith ierr = PetscFree(abuf_r[0]);CHKERRQ(ierr); 461955d1abb9SHong Zhang ierr = PetscFree(abuf_r);CHKERRQ(ierr); 462055d1abb9SHong Zhang ierr = PetscFree(ba_i);CHKERRQ(ierr); 46211d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 46224ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompinum,seqmat,0,0,0);CHKERRQ(ierr); 462355d1abb9SHong Zhang PetscFunctionReturn(0); 462455d1abb9SHong Zhang } 462538f152feSBarry Smith 46266bc0bbbfSBarry Smith extern PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat); 46276bc0bbbfSBarry Smith 462838f152feSBarry Smith #undef __FUNCT__ 462990431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJSymbolic" 463090431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJSymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat) 4631e5f2cdd8SHong Zhang { 4632f08fae4eSHong Zhang PetscErrorCode ierr; 463355a3bba9SHong Zhang Mat B_mpi; 4634c2234fe3SHong Zhang Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data; 4635b1d57f15SBarry Smith PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri; 4636b1d57f15SBarry Smith PetscInt **buf_rj,**buf_ri,**buf_ri_k; 4637d0f46423SBarry Smith PetscInt M=seqmat->rmap->n,N=seqmat->cmap->n,i,*owners,*ai=a->i,*aj=a->j; 4638a2f3521dSMark F. Adams PetscInt len,proc,*dnz,*onz,bs,cbs; 4639b1d57f15SBarry Smith PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0; 4640b1d57f15SBarry Smith PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai; 464155d1abb9SHong Zhang MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits; 464258cb9c82SHong Zhang MPI_Status *status; 46430298fd71SBarry Smith PetscFreeSpaceList free_space=NULL,current_space=NULL; 4644be0fcf8dSHong Zhang PetscBT lnkbt; 464551a7d1a8SHong Zhang Mat_Merge_SeqsToMPI *merge; 4646776b82aeSLisandro Dalcin PetscContainer container; 464702c68681SHong Zhang 4648e5f2cdd8SHong Zhang PetscFunctionBegin; 46494ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 46503c2c1871SHong Zhang 465138f152feSBarry Smith /* make sure it is a PETSc comm */ 46520298fd71SBarry Smith ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr); 4653e5f2cdd8SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 4654e5f2cdd8SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 465555d1abb9SHong Zhang 4656b00a9115SJed Brown ierr = PetscNew(&merge);CHKERRQ(ierr); 4657785e854fSJed Brown ierr = PetscMalloc1(size,&status);CHKERRQ(ierr); 4658e5f2cdd8SHong Zhang 46596abd8857SHong Zhang /* determine row ownership */ 4660f08fae4eSHong Zhang /*---------------------------------------------------------*/ 466126283091SBarry Smith ierr = PetscLayoutCreate(comm,&merge->rowmap);CHKERRQ(ierr); 466226283091SBarry Smith ierr = PetscLayoutSetLocalSize(merge->rowmap,m);CHKERRQ(ierr); 466326283091SBarry Smith ierr = PetscLayoutSetSize(merge->rowmap,M);CHKERRQ(ierr); 466426283091SBarry Smith ierr = PetscLayoutSetBlockSize(merge->rowmap,1);CHKERRQ(ierr); 466526283091SBarry Smith ierr = PetscLayoutSetUp(merge->rowmap);CHKERRQ(ierr); 4666785e854fSJed Brown ierr = PetscMalloc1(size,&len_si);CHKERRQ(ierr); 4667785e854fSJed Brown ierr = PetscMalloc1(size,&merge->len_s);CHKERRQ(ierr); 466855d1abb9SHong Zhang 46697a2fc3feSBarry Smith m = merge->rowmap->n; 46707a2fc3feSBarry Smith owners = merge->rowmap->range; 46716abd8857SHong Zhang 46726abd8857SHong Zhang /* determine the number of messages to send, their lengths */ 46736abd8857SHong Zhang /*---------------------------------------------------------*/ 46743e06a4e6SHong Zhang len_s = merge->len_s; 467551a7d1a8SHong Zhang 46762257cef7SHong Zhang len = 0; /* length of buf_si[] */ 4677c2234fe3SHong Zhang merge->nsend = 0; 4678409913e3SHong Zhang for (proc=0; proc<size; proc++) { 46792257cef7SHong Zhang len_si[proc] = 0; 46803e06a4e6SHong Zhang if (proc == rank) { 46816abd8857SHong Zhang len_s[proc] = 0; 46823e06a4e6SHong Zhang } else { 468302c68681SHong Zhang len_si[proc] = owners[proc+1] - owners[proc] + 1; 46843e06a4e6SHong Zhang len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */ 46853e06a4e6SHong Zhang } 46863e06a4e6SHong Zhang if (len_s[proc]) { 4687c2234fe3SHong Zhang merge->nsend++; 46882257cef7SHong Zhang nrows = 0; 46892257cef7SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 46902257cef7SHong Zhang if (ai[i+1] > ai[i]) nrows++; 46912257cef7SHong Zhang } 46922257cef7SHong Zhang len_si[proc] = 2*(nrows+1); 46932257cef7SHong Zhang len += len_si[proc]; 4694409913e3SHong Zhang } 469558cb9c82SHong Zhang } 4696409913e3SHong Zhang 46972257cef7SHong Zhang /* determine the number and length of messages to receive for ij-structure */ 46982257cef7SHong Zhang /*-------------------------------------------------------------------------*/ 46990298fd71SBarry Smith ierr = PetscGatherNumberOfMessages(comm,NULL,len_s,&merge->nrecv);CHKERRQ(ierr); 470055d1abb9SHong Zhang ierr = PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);CHKERRQ(ierr); 4701671beff6SHong Zhang 47023e06a4e6SHong Zhang /* post the Irecv of j-structure */ 47033e06a4e6SHong Zhang /*-------------------------------*/ 47042c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagj);CHKERRQ(ierr); 47053e06a4e6SHong Zhang ierr = PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);CHKERRQ(ierr); 470602c68681SHong Zhang 47073e06a4e6SHong Zhang /* post the Isend of j-structure */ 4708affca5deSHong Zhang /*--------------------------------*/ 4709dcca6d9dSJed Brown ierr = PetscMalloc2(merge->nsend,&si_waits,merge->nsend,&sj_waits);CHKERRQ(ierr); 47103e06a4e6SHong Zhang 47112257cef7SHong Zhang for (proc=0, k=0; proc<size; proc++) { 4712409913e3SHong Zhang if (!len_s[proc]) continue; 471302c68681SHong Zhang i = owners[proc]; 4714b1d57f15SBarry Smith ierr = MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);CHKERRQ(ierr); 471551a7d1a8SHong Zhang k++; 471651a7d1a8SHong Zhang } 471751a7d1a8SHong Zhang 47183e06a4e6SHong Zhang /* receives and sends of j-structure are complete */ 47193e06a4e6SHong Zhang /*------------------------------------------------*/ 47200c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,rj_waits,status);CHKERRQ(ierr);} 47210c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,sj_waits,status);CHKERRQ(ierr);} 472202c68681SHong Zhang 472302c68681SHong Zhang /* send and recv i-structure */ 472402c68681SHong Zhang /*---------------------------*/ 47252c72b5baSSatish Balay ierr = PetscCommGetNewTag(comm,&tagi);CHKERRQ(ierr); 472602c68681SHong Zhang ierr = PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);CHKERRQ(ierr); 472702c68681SHong Zhang 4728785e854fSJed Brown ierr = PetscMalloc1((len+1),&buf_s);CHKERRQ(ierr); 47293e06a4e6SHong Zhang buf_si = buf_s; /* points to the beginning of k-th msg to be sent */ 47302257cef7SHong Zhang for (proc=0,k=0; proc<size; proc++) { 473102c68681SHong Zhang if (!len_s[proc]) continue; 47323e06a4e6SHong Zhang /* form outgoing message for i-structure: 47333e06a4e6SHong Zhang buf_si[0]: nrows to be sent 47343e06a4e6SHong Zhang [1:nrows]: row index (global) 47353e06a4e6SHong Zhang [nrows+1:2*nrows+1]: i-structure index 47363e06a4e6SHong Zhang */ 47373e06a4e6SHong Zhang /*-------------------------------------------*/ 47382257cef7SHong Zhang nrows = len_si[proc]/2 - 1; 47393e06a4e6SHong Zhang buf_si_i = buf_si + nrows+1; 47403e06a4e6SHong Zhang buf_si[0] = nrows; 47413e06a4e6SHong Zhang buf_si_i[0] = 0; 47423e06a4e6SHong Zhang nrows = 0; 47433e06a4e6SHong Zhang for (i=owners[proc]; i<owners[proc+1]; i++) { 47443e06a4e6SHong Zhang anzi = ai[i+1] - ai[i]; 47453e06a4e6SHong Zhang if (anzi) { 47463e06a4e6SHong Zhang buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */ 47473e06a4e6SHong Zhang buf_si[nrows+1] = i-owners[proc]; /* local row index */ 47483e06a4e6SHong Zhang nrows++; 47493e06a4e6SHong Zhang } 47503e06a4e6SHong Zhang } 4751b1d57f15SBarry Smith ierr = MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);CHKERRQ(ierr); 475202c68681SHong Zhang k++; 47532257cef7SHong Zhang buf_si += len_si[proc]; 475402c68681SHong Zhang } 47552257cef7SHong Zhang 47560c468ba9SBarry Smith if (merge->nrecv) {ierr = MPI_Waitall(merge->nrecv,ri_waits,status);CHKERRQ(ierr);} 47570c468ba9SBarry Smith if (merge->nsend) {ierr = MPI_Waitall(merge->nsend,si_waits,status);CHKERRQ(ierr);} 475802c68681SHong Zhang 4759ae15b995SBarry Smith ierr = PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);CHKERRQ(ierr); 47603e06a4e6SHong Zhang for (i=0; i<merge->nrecv; i++) { 4761ae15b995SBarry 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); 47623e06a4e6SHong Zhang } 47633e06a4e6SHong Zhang 47643e06a4e6SHong Zhang ierr = PetscFree(len_si);CHKERRQ(ierr); 476502c68681SHong Zhang ierr = PetscFree(len_ri);CHKERRQ(ierr); 476602c68681SHong Zhang ierr = PetscFree(rj_waits);CHKERRQ(ierr); 47671d79065fSBarry Smith ierr = PetscFree2(si_waits,sj_waits);CHKERRQ(ierr); 47682257cef7SHong Zhang ierr = PetscFree(ri_waits);CHKERRQ(ierr); 47693e06a4e6SHong Zhang ierr = PetscFree(buf_s);CHKERRQ(ierr); 4770bcc1bcd5SHong Zhang ierr = PetscFree(status);CHKERRQ(ierr); 477158cb9c82SHong Zhang 4772bcc1bcd5SHong Zhang /* compute a local seq matrix in each processor */ 4773bcc1bcd5SHong Zhang /*----------------------------------------------*/ 477458cb9c82SHong Zhang /* allocate bi array and free space for accumulating nonzero column info */ 4775785e854fSJed Brown ierr = PetscMalloc1((m+1),&bi);CHKERRQ(ierr); 477658cb9c82SHong Zhang bi[0] = 0; 477758cb9c82SHong Zhang 4778be0fcf8dSHong Zhang /* create and initialize a linked list */ 4779be0fcf8dSHong Zhang nlnk = N+1; 4780be0fcf8dSHong Zhang ierr = PetscLLCreate(N,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 478158cb9c82SHong Zhang 4782bcc1bcd5SHong Zhang /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */ 4783bcc1bcd5SHong Zhang len = ai[owners[rank+1]] - ai[owners[rank]]; 4784a1a86e44SBarry Smith ierr = PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);CHKERRQ(ierr); 47852205254eSKarl Rupp 478658cb9c82SHong Zhang current_space = free_space; 478758cb9c82SHong Zhang 4788bcc1bcd5SHong Zhang /* determine symbolic info for each local row */ 4789dcca6d9dSJed Brown ierr = PetscMalloc3(merge->nrecv,&buf_ri_k,merge->nrecv,&nextrow,merge->nrecv,&nextai);CHKERRQ(ierr); 47901d79065fSBarry Smith 47913e06a4e6SHong Zhang for (k=0; k<merge->nrecv; k++) { 47922257cef7SHong Zhang buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */ 47933e06a4e6SHong Zhang nrows = *buf_ri_k[k]; 47943e06a4e6SHong Zhang nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */ 47952257cef7SHong Zhang nextai[k] = buf_ri_k[k] + (nrows + 1); /* poins to the next i-structure of k-th recved i-structure */ 47963e06a4e6SHong Zhang } 47972257cef7SHong Zhang 4798bcc1bcd5SHong Zhang ierr = MatPreallocateInitialize(comm,m,n,dnz,onz);CHKERRQ(ierr); 4799bcc1bcd5SHong Zhang len = 0; 480058cb9c82SHong Zhang for (i=0; i<m; i++) { 480158cb9c82SHong Zhang bnzi = 0; 480258cb9c82SHong Zhang /* add local non-zero cols of this proc's seqmat into lnk */ 480358cb9c82SHong Zhang arow = owners[rank] + i; 480458cb9c82SHong Zhang anzi = ai[arow+1] - ai[arow]; 480558cb9c82SHong Zhang aj = a->j + ai[arow]; 4806dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 480758cb9c82SHong Zhang bnzi += nlnk; 480858cb9c82SHong Zhang /* add received col data into lnk */ 480951a7d1a8SHong Zhang for (k=0; k<merge->nrecv; k++) { /* k-th received message */ 481055d1abb9SHong Zhang if (i == *nextrow[k]) { /* i-th row */ 48113e06a4e6SHong Zhang anzi = *(nextai[k]+1) - *nextai[k]; 48123e06a4e6SHong Zhang aj = buf_rj[k] + *nextai[k]; 4813dadf0e6bSHong Zhang ierr = PetscLLAddSorted(anzi,aj,N,nlnk,lnk,lnkbt);CHKERRQ(ierr); 48143e06a4e6SHong Zhang bnzi += nlnk; 48153e06a4e6SHong Zhang nextrow[k]++; nextai[k]++; 48163e06a4e6SHong Zhang } 481758cb9c82SHong Zhang } 4818bcc1bcd5SHong Zhang if (len < bnzi) len = bnzi; /* =max(bnzi) */ 481958cb9c82SHong Zhang 482058cb9c82SHong Zhang /* if free space is not available, make more free space */ 482158cb9c82SHong Zhang if (current_space->local_remaining<bnzi) { 48224238b7adSHong Zhang ierr = PetscFreeSpaceGet(bnzi+current_space->total_array_size,¤t_space);CHKERRQ(ierr); 482358cb9c82SHong Zhang nspacedouble++; 482458cb9c82SHong Zhang } 482558cb9c82SHong Zhang /* copy data into free space, then initialize lnk */ 4826be0fcf8dSHong Zhang ierr = PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);CHKERRQ(ierr); 4827bcc1bcd5SHong Zhang ierr = MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);CHKERRQ(ierr); 4828bcc1bcd5SHong Zhang 482958cb9c82SHong Zhang current_space->array += bnzi; 483058cb9c82SHong Zhang current_space->local_used += bnzi; 483158cb9c82SHong Zhang current_space->local_remaining -= bnzi; 483258cb9c82SHong Zhang 483358cb9c82SHong Zhang bi[i+1] = bi[i] + bnzi; 483458cb9c82SHong Zhang } 4835bcc1bcd5SHong Zhang 48361d79065fSBarry Smith ierr = PetscFree3(buf_ri_k,nextrow,nextai);CHKERRQ(ierr); 4837bcc1bcd5SHong Zhang 4838785e854fSJed Brown ierr = PetscMalloc1((bi[m]+1),&bj);CHKERRQ(ierr); 4839a1a86e44SBarry Smith ierr = PetscFreeSpaceContiguous(&free_space,bj);CHKERRQ(ierr); 4840be0fcf8dSHong Zhang ierr = PetscLLDestroy(lnk,lnkbt);CHKERRQ(ierr); 4841409913e3SHong Zhang 4842bcc1bcd5SHong Zhang /* create symbolic parallel matrix B_mpi */ 4843bcc1bcd5SHong Zhang /*---------------------------------------*/ 4844a2f3521dSMark F. Adams ierr = MatGetBlockSizes(seqmat,&bs,&cbs);CHKERRQ(ierr); 4845f69a0ea3SMatthew Knepley ierr = MatCreate(comm,&B_mpi);CHKERRQ(ierr); 484654b84b50SHong Zhang if (n==PETSC_DECIDE) { 4847f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);CHKERRQ(ierr); 484854b84b50SHong Zhang } else { 4849f69a0ea3SMatthew Knepley ierr = MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 485054b84b50SHong Zhang } 4851a2f3521dSMark F. Adams ierr = MatSetBlockSizes(B_mpi,bs,cbs);CHKERRQ(ierr); 4852bcc1bcd5SHong Zhang ierr = MatSetType(B_mpi,MATMPIAIJ);CHKERRQ(ierr); 4853bcc1bcd5SHong Zhang ierr = MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);CHKERRQ(ierr); 4854bcc1bcd5SHong Zhang ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr); 48557e63b356SHong Zhang ierr = MatSetOption(B_mpi,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 485658cb9c82SHong Zhang 485790431a8fSHong Zhang /* B_mpi is not ready for use - assembly will be done by MatCreateMPIAIJSumSeqAIJNumeric() */ 48586abd8857SHong Zhang B_mpi->assembled = PETSC_FALSE; 4859affca5deSHong Zhang B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI; 4860affca5deSHong Zhang merge->bi = bi; 4861affca5deSHong Zhang merge->bj = bj; 486202c68681SHong Zhang merge->buf_ri = buf_ri; 486302c68681SHong Zhang merge->buf_rj = buf_rj; 48640298fd71SBarry Smith merge->coi = NULL; 48650298fd71SBarry Smith merge->coj = NULL; 48660298fd71SBarry Smith merge->owners_co = NULL; 4867affca5deSHong Zhang 4868bf0cc555SLisandro Dalcin ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 4869bf0cc555SLisandro Dalcin 4870affca5deSHong Zhang /* attach the supporting struct to B_mpi for reuse */ 4871776b82aeSLisandro Dalcin ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr); 4872776b82aeSLisandro Dalcin ierr = PetscContainerSetPointer(container,merge);CHKERRQ(ierr); 4873affca5deSHong Zhang ierr = PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);CHKERRQ(ierr); 4874bf0cc555SLisandro Dalcin ierr = PetscContainerDestroy(&container);CHKERRQ(ierr); 4875affca5deSHong Zhang *mpimat = B_mpi; 487638f152feSBarry Smith 48774ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompisym,seqmat,0,0,0);CHKERRQ(ierr); 4878e5f2cdd8SHong Zhang PetscFunctionReturn(0); 4879e5f2cdd8SHong Zhang } 488025616d81SHong Zhang 488138f152feSBarry Smith #undef __FUNCT__ 488290431a8fSHong Zhang #define __FUNCT__ "MatCreateMPIAIJSumSeqAIJ" 4883d4036a1aSHong Zhang /*@C 488490431a8fSHong Zhang MatCreateMPIAIJSumSeqAIJ - Creates a MPIAIJ matrix by adding sequential 4885d4036a1aSHong Zhang matrices from each processor 4886d4036a1aSHong Zhang 4887d4036a1aSHong Zhang Collective on MPI_Comm 4888d4036a1aSHong Zhang 4889d4036a1aSHong Zhang Input Parameters: 4890d4036a1aSHong Zhang + comm - the communicators the parallel matrix will live on 4891d4036a1aSHong Zhang . seqmat - the input sequential matrices 4892d4036a1aSHong Zhang . m - number of local rows (or PETSC_DECIDE) 4893d4036a1aSHong Zhang . n - number of local columns (or PETSC_DECIDE) 4894d4036a1aSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4895d4036a1aSHong Zhang 4896d4036a1aSHong Zhang Output Parameter: 4897d4036a1aSHong Zhang . mpimat - the parallel matrix generated 4898d4036a1aSHong Zhang 4899d4036a1aSHong Zhang Level: advanced 4900d4036a1aSHong Zhang 4901d4036a1aSHong Zhang Notes: 4902d4036a1aSHong Zhang The dimensions of the sequential matrix in each processor MUST be the same. 4903d4036a1aSHong Zhang The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be 4904d4036a1aSHong Zhang destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat. 4905d4036a1aSHong Zhang @*/ 490690431a8fSHong Zhang PetscErrorCode MatCreateMPIAIJSumSeqAIJ(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat) 490755d1abb9SHong Zhang { 490855d1abb9SHong Zhang PetscErrorCode ierr; 49097e63b356SHong Zhang PetscMPIInt size; 491055d1abb9SHong Zhang 491155d1abb9SHong Zhang PetscFunctionBegin; 49127e63b356SHong Zhang ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 49137e63b356SHong Zhang if (size == 1) { 49147e63b356SHong Zhang ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49157e63b356SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 49167e63b356SHong Zhang ierr = MatDuplicate(seqmat,MAT_COPY_VALUES,mpimat);CHKERRQ(ierr); 49177e63b356SHong Zhang } else { 49187e63b356SHong Zhang ierr = MatCopy(seqmat,*mpimat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 49197e63b356SHong Zhang } 49207e63b356SHong Zhang ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 49217e63b356SHong Zhang PetscFunctionReturn(0); 49227e63b356SHong Zhang } 49234ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 492455d1abb9SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 492590431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJSymbolic(comm,seqmat,m,n,mpimat);CHKERRQ(ierr); 492655d1abb9SHong Zhang } 492790431a8fSHong Zhang ierr = MatCreateMPIAIJSumSeqAIJNumeric(seqmat,*mpimat);CHKERRQ(ierr); 49284ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Seqstompi,seqmat,0,0,0);CHKERRQ(ierr); 492955d1abb9SHong Zhang PetscFunctionReturn(0); 493055d1abb9SHong Zhang } 49314ebed01fSBarry Smith 493225616d81SHong Zhang #undef __FUNCT__ 49334a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMat" 4934bc08b0f1SBarry Smith /*@ 49354a2b5492SBarry Smith MatMPIAIJGetLocalMat - Creates a SeqAIJ from a MPIAIJ matrix by taking all its local rows and putting them into a sequential vector with 49368661ff28SBarry Smith mlocal rows and n columns. Where mlocal is the row count obtained with MatGetLocalSize() and n is the global column count obtained 49378661ff28SBarry Smith with MatGetSize() 493825616d81SHong Zhang 493932fba14fSHong Zhang Not Collective 494025616d81SHong Zhang 494125616d81SHong Zhang Input Parameters: 494225616d81SHong Zhang + A - the matrix 494325616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 494425616d81SHong Zhang 494525616d81SHong Zhang Output Parameter: 494625616d81SHong Zhang . A_loc - the local sequential matrix generated 494725616d81SHong Zhang 494825616d81SHong Zhang Level: developer 494925616d81SHong Zhang 4950ba264940SBarry Smith .seealso: MatGetOwnerShipRange(), MatMPIAIJGetLocalMatCondensed() 49518661ff28SBarry Smith 495225616d81SHong Zhang @*/ 49534a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMat(Mat A,MatReuse scall,Mat *A_loc) 495425616d81SHong Zhang { 495525616d81SHong Zhang PetscErrorCode ierr; 495601b7ae99SHong Zhang Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data; 4957b78526a6SJose E. Roman Mat_SeqAIJ *mat,*a,*b; 4958b78526a6SJose E. Roman PetscInt *ai,*aj,*bi,*bj,*cmap=mpimat->garray; 4959b78526a6SJose E. Roman MatScalar *aa,*ba,*cam; 4960a77337e4SBarry Smith PetscScalar *ca; 4961d0f46423SBarry Smith PetscInt am=A->rmap->n,i,j,k,cstart=A->cmap->rstart; 49625a7d977cSHong Zhang PetscInt *ci,*cj,col,ncols_d,ncols_o,jo; 49638661ff28SBarry Smith PetscBool match; 496425616d81SHong Zhang 496525616d81SHong Zhang PetscFunctionBegin; 4966251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 4967ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 49684ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 4969b78526a6SJose E. Roman a = (Mat_SeqAIJ*)(mpimat->A)->data; 4970b78526a6SJose E. Roman b = (Mat_SeqAIJ*)(mpimat->B)->data; 4971b78526a6SJose E. Roman ai = a->i; aj = a->j; bi = b->i; bj = b->j; 4972b78526a6SJose E. Roman aa = a->a; ba = b->a; 497301b7ae99SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 4974785e854fSJed Brown ierr = PetscMalloc1((1+am),&ci);CHKERRQ(ierr); 4975dea91ad1SHong Zhang ci[0] = 0; 497601b7ae99SHong Zhang for (i=0; i<am; i++) { 4977dea91ad1SHong Zhang ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]); 497801b7ae99SHong Zhang } 4979785e854fSJed Brown ierr = PetscMalloc1((1+ci[am]),&cj);CHKERRQ(ierr); 4980785e854fSJed Brown ierr = PetscMalloc1((1+ci[am]),&ca);CHKERRQ(ierr); 4981dea91ad1SHong Zhang k = 0; 498201b7ae99SHong Zhang for (i=0; i<am; i++) { 49835a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 49845a7d977cSHong Zhang ncols_d = ai[i+1] - ai[i]; 498501b7ae99SHong Zhang /* off-diagonal portion of A */ 49865a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 49875a7d977cSHong Zhang col = cmap[*bj]; 49885a7d977cSHong Zhang if (col >= cstart) break; 49895a7d977cSHong Zhang cj[k] = col; bj++; 49905a7d977cSHong Zhang ca[k++] = *ba++; 49915a7d977cSHong Zhang } 49925a7d977cSHong Zhang /* diagonal portion of A */ 49935a7d977cSHong Zhang for (j=0; j<ncols_d; j++) { 49945a7d977cSHong Zhang cj[k] = cstart + *aj++; 49955a7d977cSHong Zhang ca[k++] = *aa++; 49965a7d977cSHong Zhang } 49975a7d977cSHong Zhang /* off-diagonal portion of A */ 49985a7d977cSHong Zhang for (j=jo; j<ncols_o; j++) { 49995a7d977cSHong Zhang cj[k] = cmap[*bj++]; 50005a7d977cSHong Zhang ca[k++] = *ba++; 50015a7d977cSHong Zhang } 500225616d81SHong Zhang } 5003dea91ad1SHong Zhang /* put together the new matrix */ 5004d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap->N,ci,cj,ca,A_loc);CHKERRQ(ierr); 5005dea91ad1SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5006dea91ad1SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 5007dea91ad1SHong Zhang mat = (Mat_SeqAIJ*)(*A_loc)->data; 5008e6b907acSBarry Smith mat->free_a = PETSC_TRUE; 5009e6b907acSBarry Smith mat->free_ij = PETSC_TRUE; 5010dea91ad1SHong Zhang mat->nonew = 0; 50115a7d977cSHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 50125a7d977cSHong Zhang mat=(Mat_SeqAIJ*)(*A_loc)->data; 5013a77337e4SBarry Smith ci = mat->i; cj = mat->j; cam = mat->a; 50145a7d977cSHong Zhang for (i=0; i<am; i++) { 50155a7d977cSHong Zhang /* off-diagonal portion of A */ 50165a7d977cSHong Zhang ncols_o = bi[i+1] - bi[i]; 50175a7d977cSHong Zhang for (jo=0; jo<ncols_o; jo++) { 50185a7d977cSHong Zhang col = cmap[*bj]; 50195a7d977cSHong Zhang if (col >= cstart) break; 5020a77337e4SBarry Smith *cam++ = *ba++; bj++; 50215a7d977cSHong Zhang } 50225a7d977cSHong Zhang /* diagonal portion of A */ 5023ecc9b87dSHong Zhang ncols_d = ai[i+1] - ai[i]; 5024a77337e4SBarry Smith for (j=0; j<ncols_d; j++) *cam++ = *aa++; 50255a7d977cSHong Zhang /* off-diagonal portion of A */ 5026f33d1a9aSHong Zhang for (j=jo; j<ncols_o; j++) { 5027a77337e4SBarry Smith *cam++ = *ba++; bj++; 5028f33d1a9aSHong Zhang } 50295a7d977cSHong Zhang } 50308661ff28SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall); 50314ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmat,A,0,0,0);CHKERRQ(ierr); 503225616d81SHong Zhang PetscFunctionReturn(0); 503325616d81SHong Zhang } 503425616d81SHong Zhang 503532fba14fSHong Zhang #undef __FUNCT__ 50364a2b5492SBarry Smith #define __FUNCT__ "MatMPIAIJGetLocalMatCondensed" 503732fba14fSHong Zhang /*@C 5038ba264940SBarry Smith MatMPIAIJGetLocalMatCondensed - Creates a SeqAIJ matrix from an MPIAIJ matrix by taking all its local rows and NON-ZERO columns 503932fba14fSHong Zhang 504032fba14fSHong Zhang Not Collective 504132fba14fSHong Zhang 504232fba14fSHong Zhang Input Parameters: 504332fba14fSHong Zhang + A - the matrix 504432fba14fSHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 50450298fd71SBarry Smith - row, col - index sets of rows and columns to extract (or NULL) 504632fba14fSHong Zhang 504732fba14fSHong Zhang Output Parameter: 504832fba14fSHong Zhang . A_loc - the local sequential matrix generated 504932fba14fSHong Zhang 505032fba14fSHong Zhang Level: developer 505132fba14fSHong Zhang 5052ba264940SBarry Smith .seealso: MatGetOwnershipRange(), MatMPIAIJGetLocalMat() 5053ba264940SBarry Smith 505432fba14fSHong Zhang @*/ 50554a2b5492SBarry Smith PetscErrorCode MatMPIAIJGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc) 505632fba14fSHong Zhang { 505732fba14fSHong Zhang Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 505832fba14fSHong Zhang PetscErrorCode ierr; 505932fba14fSHong Zhang PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx; 506032fba14fSHong Zhang IS isrowa,iscola; 506132fba14fSHong Zhang Mat *aloc; 50624a2b5492SBarry Smith PetscBool match; 506332fba14fSHong Zhang 506432fba14fSHong Zhang PetscFunctionBegin; 5065251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&match);CHKERRQ(ierr); 5066ce94432eSBarry Smith if (!match) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_SUP,"Requires MPIAIJ matrix as input"); 50674ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 506832fba14fSHong Zhang if (!row) { 5069d0f46423SBarry Smith start = A->rmap->rstart; end = A->rmap->rend; 507032fba14fSHong Zhang ierr = ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);CHKERRQ(ierr); 507132fba14fSHong Zhang } else { 507232fba14fSHong Zhang isrowa = *row; 507332fba14fSHong Zhang } 507432fba14fSHong Zhang if (!col) { 5075d0f46423SBarry Smith start = A->cmap->rstart; 507632fba14fSHong Zhang cmap = a->garray; 5077d0f46423SBarry Smith nzA = a->A->cmap->n; 5078d0f46423SBarry Smith nzB = a->B->cmap->n; 5079785e854fSJed Brown ierr = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr); 508032fba14fSHong Zhang ncols = 0; 508132fba14fSHong Zhang for (i=0; i<nzB; i++) { 508232fba14fSHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 508332fba14fSHong Zhang else break; 508432fba14fSHong Zhang } 508532fba14fSHong Zhang imark = i; 508632fba14fSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; 508732fba14fSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; 5088d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&iscola);CHKERRQ(ierr); 508932fba14fSHong Zhang } else { 509032fba14fSHong Zhang iscola = *col; 509132fba14fSHong Zhang } 509232fba14fSHong Zhang if (scall != MAT_INITIAL_MATRIX) { 509332fba14fSHong Zhang ierr = PetscMalloc(sizeof(Mat),&aloc);CHKERRQ(ierr); 509432fba14fSHong Zhang aloc[0] = *A_loc; 509532fba14fSHong Zhang } 509632fba14fSHong Zhang ierr = MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);CHKERRQ(ierr); 509732fba14fSHong Zhang *A_loc = aloc[0]; 509832fba14fSHong Zhang ierr = PetscFree(aloc);CHKERRQ(ierr); 509932fba14fSHong Zhang if (!row) { 51006bf464f9SBarry Smith ierr = ISDestroy(&isrowa);CHKERRQ(ierr); 510132fba14fSHong Zhang } 510232fba14fSHong Zhang if (!col) { 51036bf464f9SBarry Smith ierr = ISDestroy(&iscola);CHKERRQ(ierr); 510432fba14fSHong Zhang } 51054ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_Getlocalmatcondensed,A,0,0,0);CHKERRQ(ierr); 510632fba14fSHong Zhang PetscFunctionReturn(0); 510732fba14fSHong Zhang } 510832fba14fSHong Zhang 510925616d81SHong Zhang #undef __FUNCT__ 511025616d81SHong Zhang #define __FUNCT__ "MatGetBrowsOfAcols" 511125616d81SHong Zhang /*@C 511232fba14fSHong Zhang MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A 511325616d81SHong Zhang 511425616d81SHong Zhang Collective on Mat 511525616d81SHong Zhang 511625616d81SHong Zhang Input Parameters: 5117e240928fSHong Zhang + A,B - the matrices in mpiaij format 511825616d81SHong Zhang . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 51190298fd71SBarry Smith - rowb, colb - index sets of rows and columns of B to extract (or NULL) 512025616d81SHong Zhang 512125616d81SHong Zhang Output Parameter: 512225616d81SHong Zhang + rowb, colb - index sets of rows and columns of B to extract 512325616d81SHong Zhang - B_seq - the sequential matrix generated 512425616d81SHong Zhang 512525616d81SHong Zhang Level: developer 512625616d81SHong Zhang 512725616d81SHong Zhang @*/ 512866bfb163SHong Zhang PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,Mat *B_seq) 512925616d81SHong Zhang { 5130899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 513125616d81SHong Zhang PetscErrorCode ierr; 5132b1d57f15SBarry Smith PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark; 513325616d81SHong Zhang IS isrowb,iscolb; 51340298fd71SBarry Smith Mat *bseq=NULL; 513525616d81SHong Zhang 513625616d81SHong Zhang PetscFunctionBegin; 5137d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5138e32f2f54SBarry 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); 513925616d81SHong Zhang } 51404ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 514125616d81SHong Zhang 514225616d81SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5143d0f46423SBarry Smith start = A->cmap->rstart; 514425616d81SHong Zhang cmap = a->garray; 5145d0f46423SBarry Smith nzA = a->A->cmap->n; 5146d0f46423SBarry Smith nzB = a->B->cmap->n; 5147785e854fSJed Brown ierr = PetscMalloc1((nzA+nzB), &idx);CHKERRQ(ierr); 514825616d81SHong Zhang ncols = 0; 51490390132cSHong Zhang for (i=0; i<nzB; i++) { /* row < local row index */ 515025616d81SHong Zhang if (cmap[i] < start) idx[ncols++] = cmap[i]; 515125616d81SHong Zhang else break; 515225616d81SHong Zhang } 515325616d81SHong Zhang imark = i; 51540390132cSHong Zhang for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */ 51550390132cSHong Zhang for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */ 5156d67e408aSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,PETSC_OWN_POINTER,&isrowb);CHKERRQ(ierr); 5157d0f46423SBarry Smith ierr = ISCreateStride(PETSC_COMM_SELF,B->cmap->N,0,1,&iscolb);CHKERRQ(ierr); 515825616d81SHong Zhang } else { 5159e32f2f54SBarry Smith if (!rowb || !colb) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX"); 516025616d81SHong Zhang isrowb = *rowb; iscolb = *colb; 516125616d81SHong Zhang ierr = PetscMalloc(sizeof(Mat),&bseq);CHKERRQ(ierr); 516225616d81SHong Zhang bseq[0] = *B_seq; 516325616d81SHong Zhang } 516425616d81SHong Zhang ierr = MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);CHKERRQ(ierr); 516525616d81SHong Zhang *B_seq = bseq[0]; 516625616d81SHong Zhang ierr = PetscFree(bseq);CHKERRQ(ierr); 516725616d81SHong Zhang if (!rowb) { 51686bf464f9SBarry Smith ierr = ISDestroy(&isrowb);CHKERRQ(ierr); 516925616d81SHong Zhang } else { 517025616d81SHong Zhang *rowb = isrowb; 517125616d81SHong Zhang } 517225616d81SHong Zhang if (!colb) { 51736bf464f9SBarry Smith ierr = ISDestroy(&iscolb);CHKERRQ(ierr); 517425616d81SHong Zhang } else { 517525616d81SHong Zhang *colb = iscolb; 517625616d81SHong Zhang } 51774ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAcols,A,B,0,0);CHKERRQ(ierr); 517825616d81SHong Zhang PetscFunctionReturn(0); 517925616d81SHong Zhang } 5180429d309bSHong Zhang 5181a61c8c0fSHong Zhang #undef __FUNCT__ 5182f8487c73SHong Zhang #define __FUNCT__ "MatGetBrowsOfAoCols_MPIAIJ" 5183f8487c73SHong Zhang /* 5184f8487c73SHong Zhang MatGetBrowsOfAoCols_MPIAIJ - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns 518501b7ae99SHong Zhang of the OFF-DIAGONAL portion of local A 5186429d309bSHong Zhang 5187429d309bSHong Zhang Collective on Mat 5188429d309bSHong Zhang 5189429d309bSHong Zhang Input Parameters: 5190429d309bSHong Zhang + A,B - the matrices in mpiaij format 5191598bc09dSHong Zhang - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 5192429d309bSHong Zhang 5193429d309bSHong Zhang Output Parameter: 51940298fd71SBarry Smith + startsj_s - starting point in B's sending j-arrays, saved for MAT_REUSE (or NULL) 51950298fd71SBarry Smith . startsj_r - starting point in B's receiving j-arrays, saved for MAT_REUSE (or NULL) 51960298fd71SBarry Smith . bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or NULL) 5197598bc09dSHong Zhang - B_oth - the sequential matrix generated with size aBn=a->B->cmap->n by B->cmap->N 5198429d309bSHong Zhang 5199429d309bSHong Zhang Level: developer 5200429d309bSHong Zhang 5201f8487c73SHong Zhang */ 5202b7f45c76SHong Zhang PetscErrorCode MatGetBrowsOfAoCols_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscInt **startsj_s,PetscInt **startsj_r,MatScalar **bufa_ptr,Mat *B_oth) 5203429d309bSHong Zhang { 5204a6b2eed2SHong Zhang VecScatter_MPI_General *gen_to,*gen_from; 5205429d309bSHong Zhang PetscErrorCode ierr; 5206899cda47SBarry Smith Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data; 520787025532SHong Zhang Mat_SeqAIJ *b_oth; 5208a6b2eed2SHong Zhang VecScatter ctx =a->Mvctx; 5209ce94432eSBarry Smith MPI_Comm comm; 52107adad957SLisandro Dalcin PetscMPIInt *rprocs,*sprocs,tag=((PetscObject)ctx)->tag,rank; 5211d0f46423SBarry Smith PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap->n,row,*b_othi,*b_othj; 5212dd6ea824SBarry Smith PetscScalar *rvalues,*svalues; 5213dd6ea824SBarry Smith MatScalar *b_otha,*bufa,*bufA; 5214e42f35eeSHong Zhang PetscInt i,j,k,l,ll,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len; 52150298fd71SBarry Smith MPI_Request *rwaits = NULL,*swaits = NULL; 521687025532SHong Zhang MPI_Status *sstatus,rstatus; 5217aa5bb8c0SSatish Balay PetscMPIInt jj; 5218e42f35eeSHong Zhang PetscInt *cols,sbs,rbs; 5219ba8c8a56SBarry Smith PetscScalar *vals; 5220429d309bSHong Zhang 5221429d309bSHong Zhang PetscFunctionBegin; 5222ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr); 5223d0f46423SBarry Smith if (A->cmap->rstart != B->rmap->rstart || A->cmap->rend != B->rmap->rend) { 5224e32f2f54SBarry 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); 5225429d309bSHong Zhang } 52264ebed01fSBarry Smith ierr = PetscLogEventBegin(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5227a6b2eed2SHong Zhang ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 5228a6b2eed2SHong Zhang 5229a6b2eed2SHong Zhang gen_to = (VecScatter_MPI_General*)ctx->todata; 5230a6b2eed2SHong Zhang gen_from = (VecScatter_MPI_General*)ctx->fromdata; 5231e42f35eeSHong Zhang rvalues = gen_from->values; /* holds the length of receiving row */ 5232e42f35eeSHong Zhang svalues = gen_to->values; /* holds the length of sending row */ 5233a6b2eed2SHong Zhang nrecvs = gen_from->n; 5234a6b2eed2SHong Zhang nsends = gen_to->n; 5235d7ee0231SBarry Smith 5236dcca6d9dSJed Brown ierr = PetscMalloc2(nrecvs,&rwaits,nsends,&swaits);CHKERRQ(ierr); 5237a6b2eed2SHong Zhang srow = gen_to->indices; /* local row index to be sent */ 5238a6b2eed2SHong Zhang sstarts = gen_to->starts; 5239a6b2eed2SHong Zhang sprocs = gen_to->procs; 5240a6b2eed2SHong Zhang sstatus = gen_to->sstatus; 5241e42f35eeSHong Zhang sbs = gen_to->bs; 5242e42f35eeSHong Zhang rstarts = gen_from->starts; 5243e42f35eeSHong Zhang rprocs = gen_from->procs; 5244e42f35eeSHong Zhang rbs = gen_from->bs; 5245429d309bSHong Zhang 5246b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) scall = MAT_INITIAL_MATRIX; 5247429d309bSHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5248a6b2eed2SHong Zhang /* i-array */ 5249a6b2eed2SHong Zhang /*---------*/ 5250a6b2eed2SHong Zhang /* post receives */ 5251a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5252e42f35eeSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5253e42f35eeSHong Zhang nrows = (rstarts[i+1]-rstarts[i])*rbs; /* num of indices to be received */ 525487025532SHong Zhang ierr = MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5255429d309bSHong Zhang } 5256a6b2eed2SHong Zhang 5257a6b2eed2SHong Zhang /* pack the outgoing message */ 5258dcca6d9dSJed Brown ierr = PetscMalloc2(nsends+1,&sstartsj,nrecvs+1,&rstartsj);CHKERRQ(ierr); 52592205254eSKarl Rupp 52602205254eSKarl Rupp sstartsj[0] = 0; 52612205254eSKarl Rupp rstartsj[0] = 0; 5262a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be sent */ 5263a6b2eed2SHong Zhang k = 0; 5264a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5265e42f35eeSHong Zhang rowlen = (PetscInt*)svalues + sstarts[i]*sbs; 5266e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 526787025532SHong Zhang for (j=0; j<nrows; j++) { 5268d0f46423SBarry Smith row = srow[k] + B->rmap->range[rank]; /* global row idx */ 5269e42f35eeSHong Zhang for (l=0; l<sbs; l++) { 52700298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); /* rowlength */ 52712205254eSKarl Rupp 5272e42f35eeSHong Zhang rowlen[j*sbs+l] = ncols; 52732205254eSKarl Rupp 5274e42f35eeSHong Zhang len += ncols; 52750298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+l,&ncols,NULL,NULL);CHKERRQ(ierr); 5276e42f35eeSHong Zhang } 5277a6b2eed2SHong Zhang k++; 5278429d309bSHong Zhang } 5279e42f35eeSHong Zhang ierr = MPI_Isend(rowlen,nrows*sbs,MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 52802205254eSKarl Rupp 5281dea91ad1SHong Zhang sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */ 5282429d309bSHong Zhang } 528387025532SHong Zhang /* recvs and sends of i-array are completed */ 528487025532SHong Zhang i = nrecvs; 528587025532SHong Zhang while (i--) { 5286aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 528787025532SHong Zhang } 52880c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5289e42f35eeSHong Zhang 5290a6b2eed2SHong Zhang /* allocate buffers for sending j and a arrays */ 5291785e854fSJed Brown ierr = PetscMalloc1((len+1),&bufj);CHKERRQ(ierr); 5292785e854fSJed Brown ierr = PetscMalloc1((len+1),&bufa);CHKERRQ(ierr); 5293a6b2eed2SHong Zhang 529487025532SHong Zhang /* create i-array of B_oth */ 5295785e854fSJed Brown ierr = PetscMalloc1((aBn+2),&b_othi);CHKERRQ(ierr); 52962205254eSKarl Rupp 529787025532SHong Zhang b_othi[0] = 0; 5298a6b2eed2SHong Zhang len = 0; /* total length of j or a array to be received */ 5299a6b2eed2SHong Zhang k = 0; 5300a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 5301fd0ff01cSHong Zhang rowlen = (PetscInt*)rvalues + rstarts[i]*rbs; 5302e42f35eeSHong Zhang nrows = rbs*(rstarts[i+1]-rstarts[i]); /* num of rows to be recieved */ 530387025532SHong Zhang for (j=0; j<nrows; j++) { 530487025532SHong Zhang b_othi[k+1] = b_othi[k] + rowlen[j]; 5305a6b2eed2SHong Zhang len += rowlen[j]; k++; 5306a6b2eed2SHong Zhang } 5307dea91ad1SHong Zhang rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */ 5308a6b2eed2SHong Zhang } 5309a6b2eed2SHong Zhang 531087025532SHong Zhang /* allocate space for j and a arrrays of B_oth */ 5311785e854fSJed Brown ierr = PetscMalloc1((b_othi[aBn]+1),&b_othj);CHKERRQ(ierr); 5312785e854fSJed Brown ierr = PetscMalloc1((b_othi[aBn]+1),&b_otha);CHKERRQ(ierr); 5313a6b2eed2SHong Zhang 531487025532SHong Zhang /* j-array */ 531587025532SHong Zhang /*---------*/ 5316a6b2eed2SHong Zhang /* post receives of j-array */ 5317a6b2eed2SHong Zhang for (i=0; i<nrecvs; i++) { 531887025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 531987025532SHong Zhang ierr = MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 5320a6b2eed2SHong Zhang } 5321e42f35eeSHong Zhang 5322e42f35eeSHong Zhang /* pack the outgoing message j-array */ 5323a6b2eed2SHong Zhang k = 0; 5324a6b2eed2SHong Zhang for (i=0; i<nsends; i++) { 5325e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 5326a6b2eed2SHong Zhang bufJ = bufj+sstartsj[i]; 532787025532SHong Zhang for (j=0; j<nrows; j++) { 5328d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5329e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 53300298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5331a6b2eed2SHong Zhang for (l=0; l<ncols; l++) { 5332a6b2eed2SHong Zhang *bufJ++ = cols[l]; 533387025532SHong Zhang } 53340298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,&cols,NULL);CHKERRQ(ierr); 5335e42f35eeSHong Zhang } 533687025532SHong Zhang } 533787025532SHong Zhang ierr = MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 533887025532SHong Zhang } 533987025532SHong Zhang 534087025532SHong Zhang /* recvs and sends of j-array are completed */ 534187025532SHong Zhang i = nrecvs; 534287025532SHong Zhang while (i--) { 5343aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 534487025532SHong Zhang } 53450c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 534687025532SHong Zhang } else if (scall == MAT_REUSE_MATRIX) { 5347b7f45c76SHong Zhang sstartsj = *startsj_s; 53481d79065fSBarry Smith rstartsj = *startsj_r; 534987025532SHong Zhang bufa = *bufa_ptr; 535087025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 535187025532SHong Zhang b_otha = b_oth->a; 5352f23aa3ddSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container"); 535387025532SHong Zhang 535487025532SHong Zhang /* a-array */ 535587025532SHong Zhang /*---------*/ 535687025532SHong Zhang /* post receives of a-array */ 535787025532SHong Zhang for (i=0; i<nrecvs; i++) { 535887025532SHong Zhang nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */ 535987025532SHong Zhang ierr = MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);CHKERRQ(ierr); 536087025532SHong Zhang } 5361e42f35eeSHong Zhang 5362e42f35eeSHong Zhang /* pack the outgoing message a-array */ 536387025532SHong Zhang k = 0; 536487025532SHong Zhang for (i=0; i<nsends; i++) { 5365e42f35eeSHong Zhang nrows = sstarts[i+1]-sstarts[i]; /* num of block rows */ 536687025532SHong Zhang bufA = bufa+sstartsj[i]; 536787025532SHong Zhang for (j=0; j<nrows; j++) { 5368d0f46423SBarry Smith row = srow[k++] + B->rmap->range[rank]; /* global row idx */ 5369e42f35eeSHong Zhang for (ll=0; ll<sbs; ll++) { 53700298fd71SBarry Smith ierr = MatGetRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 537187025532SHong Zhang for (l=0; l<ncols; l++) { 5372a6b2eed2SHong Zhang *bufA++ = vals[l]; 5373a6b2eed2SHong Zhang } 53740298fd71SBarry Smith ierr = MatRestoreRow_MPIAIJ(B,row+ll,&ncols,NULL,&vals);CHKERRQ(ierr); 5375e42f35eeSHong Zhang } 5376a6b2eed2SHong Zhang } 537787025532SHong Zhang ierr = MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);CHKERRQ(ierr); 5378a6b2eed2SHong Zhang } 537987025532SHong Zhang /* recvs and sends of a-array are completed */ 538087025532SHong Zhang i = nrecvs; 538187025532SHong Zhang while (i--) { 5382aa5bb8c0SSatish Balay ierr = MPI_Waitany(nrecvs,rwaits,&jj,&rstatus);CHKERRQ(ierr); 538387025532SHong Zhang } 53840c468ba9SBarry Smith if (nsends) {ierr = MPI_Waitall(nsends,swaits,sstatus);CHKERRQ(ierr);} 5385d7ee0231SBarry Smith ierr = PetscFree2(rwaits,swaits);CHKERRQ(ierr); 5386a6b2eed2SHong Zhang 538787025532SHong Zhang if (scall == MAT_INITIAL_MATRIX) { 5388a6b2eed2SHong Zhang /* put together the new matrix */ 5389d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap->N,b_othi,b_othj,b_otha,B_oth);CHKERRQ(ierr); 5390a6b2eed2SHong Zhang 5391a6b2eed2SHong Zhang /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */ 5392a6b2eed2SHong Zhang /* Since these are PETSc arrays, change flags to free them as necessary. */ 539387025532SHong Zhang b_oth = (Mat_SeqAIJ*)(*B_oth)->data; 5394e6b907acSBarry Smith b_oth->free_a = PETSC_TRUE; 5395e6b907acSBarry Smith b_oth->free_ij = PETSC_TRUE; 539687025532SHong Zhang b_oth->nonew = 0; 5397a6b2eed2SHong Zhang 5398a6b2eed2SHong Zhang ierr = PetscFree(bufj);CHKERRQ(ierr); 5399b7f45c76SHong Zhang if (!startsj_s || !bufa_ptr) { 54001d79065fSBarry Smith ierr = PetscFree2(sstartsj,rstartsj);CHKERRQ(ierr); 5401dea91ad1SHong Zhang ierr = PetscFree(bufa_ptr);CHKERRQ(ierr); 5402dea91ad1SHong Zhang } else { 5403b7f45c76SHong Zhang *startsj_s = sstartsj; 54041d79065fSBarry Smith *startsj_r = rstartsj; 540587025532SHong Zhang *bufa_ptr = bufa; 540687025532SHong Zhang } 5407dea91ad1SHong Zhang } 54084ebed01fSBarry Smith ierr = PetscLogEventEnd(MAT_GetBrowsOfAocols,A,B,0,0);CHKERRQ(ierr); 5409429d309bSHong Zhang PetscFunctionReturn(0); 5410429d309bSHong Zhang } 5411ccd8e176SBarry Smith 541243eb5e2fSMatthew Knepley #undef __FUNCT__ 541343eb5e2fSMatthew Knepley #define __FUNCT__ "MatGetCommunicationStructs" 541443eb5e2fSMatthew Knepley /*@C 541543eb5e2fSMatthew Knepley MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication. 541643eb5e2fSMatthew Knepley 541743eb5e2fSMatthew Knepley Not Collective 541843eb5e2fSMatthew Knepley 541943eb5e2fSMatthew Knepley Input Parameters: 542043eb5e2fSMatthew Knepley . A - The matrix in mpiaij format 542143eb5e2fSMatthew Knepley 542243eb5e2fSMatthew Knepley Output Parameter: 542343eb5e2fSMatthew Knepley + lvec - The local vector holding off-process values from the argument to a matrix-vector product 542443eb5e2fSMatthew Knepley . colmap - A map from global column index to local index into lvec 542543eb5e2fSMatthew Knepley - multScatter - A scatter from the argument of a matrix-vector product to lvec 542643eb5e2fSMatthew Knepley 542743eb5e2fSMatthew Knepley Level: developer 542843eb5e2fSMatthew Knepley 542943eb5e2fSMatthew Knepley @*/ 543043eb5e2fSMatthew Knepley #if defined(PETSC_USE_CTABLE) 54317087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter) 543243eb5e2fSMatthew Knepley #else 54337087cfbeSBarry Smith PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter) 543443eb5e2fSMatthew Knepley #endif 543543eb5e2fSMatthew Knepley { 543643eb5e2fSMatthew Knepley Mat_MPIAIJ *a; 543743eb5e2fSMatthew Knepley 543843eb5e2fSMatthew Knepley PetscFunctionBegin; 54390700a824SBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 1); 5440e414b56bSJed Brown PetscValidPointer(lvec, 2); 5441e414b56bSJed Brown PetscValidPointer(colmap, 3); 5442e414b56bSJed Brown PetscValidPointer(multScatter, 4); 544343eb5e2fSMatthew Knepley a = (Mat_MPIAIJ*) A->data; 544443eb5e2fSMatthew Knepley if (lvec) *lvec = a->lvec; 544543eb5e2fSMatthew Knepley if (colmap) *colmap = a->colmap; 544643eb5e2fSMatthew Knepley if (multScatter) *multScatter = a->Mvctx; 544743eb5e2fSMatthew Knepley PetscFunctionReturn(0); 544843eb5e2fSMatthew Knepley } 544943eb5e2fSMatthew Knepley 54508cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJCRL(Mat,MatType,MatReuse,Mat*); 54518cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPIAIJPERM(Mat,MatType,MatReuse,Mat*); 54528cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatConvert_MPIAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*); 545317667f90SBarry Smith 5454fc4dec0aSBarry Smith #undef __FUNCT__ 5455fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultNumeric_MPIDense_MPIAIJ" 5456fc4dec0aSBarry Smith /* 5457fc4dec0aSBarry Smith Computes (B'*A')' since computing B*A directly is untenable 5458fc4dec0aSBarry Smith 5459fc4dec0aSBarry Smith n p p 5460fc4dec0aSBarry Smith ( ) ( ) ( ) 5461fc4dec0aSBarry Smith m ( A ) * n ( B ) = m ( C ) 5462fc4dec0aSBarry Smith ( ) ( ) ( ) 5463fc4dec0aSBarry Smith 5464fc4dec0aSBarry Smith */ 5465fc4dec0aSBarry Smith PetscErrorCode MatMatMultNumeric_MPIDense_MPIAIJ(Mat A,Mat B,Mat C) 5466fc4dec0aSBarry Smith { 5467fc4dec0aSBarry Smith PetscErrorCode ierr; 5468fc4dec0aSBarry Smith Mat At,Bt,Ct; 5469fc4dec0aSBarry Smith 5470fc4dec0aSBarry Smith PetscFunctionBegin; 5471fc4dec0aSBarry Smith ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&At);CHKERRQ(ierr); 5472fc4dec0aSBarry Smith ierr = MatTranspose(B,MAT_INITIAL_MATRIX,&Bt);CHKERRQ(ierr); 5473fc4dec0aSBarry Smith ierr = MatMatMult(Bt,At,MAT_INITIAL_MATRIX,1.0,&Ct);CHKERRQ(ierr); 54746bf464f9SBarry Smith ierr = MatDestroy(&At);CHKERRQ(ierr); 54756bf464f9SBarry Smith ierr = MatDestroy(&Bt);CHKERRQ(ierr); 5476fc4dec0aSBarry Smith ierr = MatTranspose(Ct,MAT_REUSE_MATRIX,&C);CHKERRQ(ierr); 54776bf464f9SBarry Smith ierr = MatDestroy(&Ct);CHKERRQ(ierr); 5478fc4dec0aSBarry Smith PetscFunctionReturn(0); 5479fc4dec0aSBarry Smith } 5480fc4dec0aSBarry Smith 5481fc4dec0aSBarry Smith #undef __FUNCT__ 5482fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMultSymbolic_MPIDense_MPIAIJ" 5483fc4dec0aSBarry Smith PetscErrorCode MatMatMultSymbolic_MPIDense_MPIAIJ(Mat A,Mat B,PetscReal fill,Mat *C) 5484fc4dec0aSBarry Smith { 5485fc4dec0aSBarry Smith PetscErrorCode ierr; 5486d0f46423SBarry Smith PetscInt m=A->rmap->n,n=B->cmap->n; 5487fc4dec0aSBarry Smith Mat Cmat; 5488fc4dec0aSBarry Smith 5489fc4dec0aSBarry Smith PetscFunctionBegin; 5490e32f2f54SBarry 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); 5491ce94432eSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&Cmat);CHKERRQ(ierr); 5492fc4dec0aSBarry Smith ierr = MatSetSizes(Cmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); 5493a2f3521dSMark F. Adams ierr = MatSetBlockSizes(Cmat,A->rmap->bs,B->cmap->bs);CHKERRQ(ierr); 5494fc4dec0aSBarry Smith ierr = MatSetType(Cmat,MATMPIDENSE);CHKERRQ(ierr); 54950298fd71SBarry Smith ierr = MatMPIDenseSetPreallocation(Cmat,NULL);CHKERRQ(ierr); 549638556019SBarry Smith ierr = MatAssemblyBegin(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 549738556019SBarry Smith ierr = MatAssemblyEnd(Cmat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5498f75ecaa4SHong Zhang 5499f75ecaa4SHong Zhang Cmat->ops->matmultnumeric = MatMatMultNumeric_MPIDense_MPIAIJ; 55002205254eSKarl Rupp 5501fc4dec0aSBarry Smith *C = Cmat; 5502fc4dec0aSBarry Smith PetscFunctionReturn(0); 5503fc4dec0aSBarry Smith } 5504fc4dec0aSBarry Smith 5505fc4dec0aSBarry Smith /* ----------------------------------------------------------------*/ 5506fc4dec0aSBarry Smith #undef __FUNCT__ 5507fc4dec0aSBarry Smith #define __FUNCT__ "MatMatMult_MPIDense_MPIAIJ" 5508fc4dec0aSBarry Smith PetscErrorCode MatMatMult_MPIDense_MPIAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C) 5509fc4dec0aSBarry Smith { 5510fc4dec0aSBarry Smith PetscErrorCode ierr; 5511fc4dec0aSBarry Smith 5512fc4dec0aSBarry Smith PetscFunctionBegin; 5513fc4dec0aSBarry Smith if (scall == MAT_INITIAL_MATRIX) { 55143ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 5515fc4dec0aSBarry Smith ierr = MatMatMultSymbolic_MPIDense_MPIAIJ(A,B,fill,C);CHKERRQ(ierr); 55163ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);CHKERRQ(ierr); 5517fc4dec0aSBarry Smith } 55183ff4c91cSHong Zhang ierr = PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 5519fc4dec0aSBarry Smith ierr = MatMatMultNumeric_MPIDense_MPIAIJ(A,B,*C);CHKERRQ(ierr); 55203ff4c91cSHong Zhang ierr = PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);CHKERRQ(ierr); 5521fc4dec0aSBarry Smith PetscFunctionReturn(0); 5522fc4dec0aSBarry Smith } 5523fc4dec0aSBarry Smith 5524611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 55258cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*); 5526611f576cSBarry Smith #endif 55273bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 55288cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_pastix(Mat,MatFactorType,Mat*); 55293bf14a46SMatthew Knepley #endif 5530611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 55318cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_mpiaij_superlu_dist(Mat,MatFactorType,Mat*); 5532611f576cSBarry Smith #endif 553317f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 55348cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*); 553517f1a0eaSHong Zhang #endif 55365c9eb25fSBarry Smith 5537ccd8e176SBarry Smith /*MC 5538ccd8e176SBarry Smith MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices. 5539ccd8e176SBarry Smith 5540ccd8e176SBarry Smith Options Database Keys: 5541ccd8e176SBarry Smith . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions() 5542ccd8e176SBarry Smith 5543ccd8e176SBarry Smith Level: beginner 5544ccd8e176SBarry Smith 554569b1f4b7SBarry Smith .seealso: MatCreateAIJ() 5546ccd8e176SBarry Smith M*/ 5547ccd8e176SBarry Smith 5548ccd8e176SBarry Smith #undef __FUNCT__ 5549ccd8e176SBarry Smith #define __FUNCT__ "MatCreate_MPIAIJ" 55508cc058d9SJed Brown PETSC_EXTERN PetscErrorCode MatCreate_MPIAIJ(Mat B) 5551ccd8e176SBarry Smith { 5552ccd8e176SBarry Smith Mat_MPIAIJ *b; 5553ccd8e176SBarry Smith PetscErrorCode ierr; 5554ccd8e176SBarry Smith PetscMPIInt size; 5555ccd8e176SBarry Smith 5556ccd8e176SBarry Smith PetscFunctionBegin; 5557ce94432eSBarry Smith ierr = MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);CHKERRQ(ierr); 55582205254eSKarl Rupp 5559b00a9115SJed Brown ierr = PetscNewLog(B,&b);CHKERRQ(ierr); 5560ccd8e176SBarry Smith B->data = (void*)b; 5561ccd8e176SBarry Smith ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr); 5562ccd8e176SBarry Smith B->assembled = PETSC_FALSE; 5563ccd8e176SBarry Smith B->insertmode = NOT_SET_VALUES; 5564ccd8e176SBarry Smith b->size = size; 55652205254eSKarl Rupp 5566ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);CHKERRQ(ierr); 5567ccd8e176SBarry Smith 5568ccd8e176SBarry Smith /* build cache for off array entries formed */ 5569ce94432eSBarry Smith ierr = MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);CHKERRQ(ierr); 55702205254eSKarl Rupp 5571ccd8e176SBarry Smith b->donotstash = PETSC_FALSE; 5572ccd8e176SBarry Smith b->colmap = 0; 5573ccd8e176SBarry Smith b->garray = 0; 5574ccd8e176SBarry Smith b->roworiented = PETSC_TRUE; 5575ccd8e176SBarry Smith 5576ccd8e176SBarry Smith /* stuff used for matrix vector multiply */ 55770298fd71SBarry Smith b->lvec = NULL; 55780298fd71SBarry Smith b->Mvctx = NULL; 5579ccd8e176SBarry Smith 5580ccd8e176SBarry Smith /* stuff for MatGetRow() */ 5581ccd8e176SBarry Smith b->rowindices = 0; 5582ccd8e176SBarry Smith b->rowvalues = 0; 5583ccd8e176SBarry Smith b->getrowactive = PETSC_FALSE; 5584ccd8e176SBarry Smith 5585bbf3fe20SPaul Mullowney /* flexible pointer used in CUSP/CUSPARSE classes */ 55860298fd71SBarry Smith b->spptr = NULL; 5587f60c3dc2SHong Zhang 5588611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS) 5589bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);CHKERRQ(ierr); 5590611f576cSBarry Smith #endif 55913bf14a46SMatthew Knepley #if defined(PETSC_HAVE_PASTIX) 5592bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_mpiaij_pastix);CHKERRQ(ierr); 55933bf14a46SMatthew Knepley #endif 5594611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU_DIST) 5595bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_mpiaij_superlu_dist);CHKERRQ(ierr); 5596611f576cSBarry Smith #endif 559717f1a0eaSHong Zhang #if defined(PETSC_HAVE_CLIQUE) 5598bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);CHKERRQ(ierr); 559917f1a0eaSHong Zhang #endif 5600bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIAIJ);CHKERRQ(ierr); 5601bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIAIJ);CHKERRQ(ierr); 5602bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIAIJ);CHKERRQ(ierr); 5603bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_MPIAIJ);CHKERRQ(ierr); 5604bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJ);CHKERRQ(ierr); 5605bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",MatMPIAIJSetPreallocationCSR_MPIAIJ);CHKERRQ(ierr); 5606bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIAIJ);CHKERRQ(ierr); 5607bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijperm_C",MatConvert_MPIAIJ_MPIAIJPERM);CHKERRQ(ierr); 5608bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpiaijcrl_C",MatConvert_MPIAIJ_MPIAIJCRL);CHKERRQ(ierr); 5609bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpiaij_mpisbaij_C",MatConvert_MPIAIJ_MPISBAIJ);CHKERRQ(ierr); 5610bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMult_mpidense_mpiaij_C",MatMatMult_MPIDense_MPIAIJ);CHKERRQ(ierr); 5611bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_mpidense_mpiaij_C",MatMatMultSymbolic_MPIDense_MPIAIJ);CHKERRQ(ierr); 5612bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_mpidense_mpiaij_C",MatMatMultNumeric_MPIDense_MPIAIJ);CHKERRQ(ierr); 561317667f90SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);CHKERRQ(ierr); 5614ccd8e176SBarry Smith PetscFunctionReturn(0); 5615ccd8e176SBarry Smith } 561681824310SBarry Smith 561703bfb495SBarry Smith #undef __FUNCT__ 561803bfb495SBarry Smith #define __FUNCT__ "MatCreateMPIAIJWithSplitArrays" 561958d36128SBarry Smith /*@ 562003bfb495SBarry Smith MatCreateMPIAIJWithSplitArrays - creates a MPI AIJ matrix using arrays that contain the "diagonal" 562103bfb495SBarry Smith and "off-diagonal" part of the matrix in CSR format. 562203bfb495SBarry Smith 562303bfb495SBarry Smith Collective on MPI_Comm 562403bfb495SBarry Smith 562503bfb495SBarry Smith Input Parameters: 562603bfb495SBarry Smith + comm - MPI communicator 562703bfb495SBarry Smith . m - number of local rows (Cannot be PETSC_DECIDE) 562803bfb495SBarry Smith . n - This value should be the same as the local size used in creating the 562903bfb495SBarry Smith x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have 563003bfb495SBarry Smith calculated if N is given) For square matrices n is almost always m. 563103bfb495SBarry Smith . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given) 563203bfb495SBarry Smith . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given) 563303bfb495SBarry Smith . i - row indices for "diagonal" portion of matrix 563403bfb495SBarry Smith . j - column indices 563503bfb495SBarry Smith . a - matrix values 563603bfb495SBarry Smith . oi - row indices for "off-diagonal" portion of matrix 563703bfb495SBarry Smith . oj - column indices 563803bfb495SBarry Smith - oa - matrix values 563903bfb495SBarry Smith 564003bfb495SBarry Smith Output Parameter: 564103bfb495SBarry Smith . mat - the matrix 564203bfb495SBarry Smith 564303bfb495SBarry Smith Level: advanced 564403bfb495SBarry Smith 564503bfb495SBarry Smith Notes: 5646292fb18eSBarry Smith The i, j, and a arrays ARE NOT copied by this routine into the internal format used by PETSc. The user 5647292fb18eSBarry Smith must free the arrays once the matrix has been destroyed and not before. 564803bfb495SBarry Smith 564903bfb495SBarry Smith The i and j indices are 0 based 565003bfb495SBarry Smith 565169b1f4b7SBarry Smith See MatCreateAIJ() for the definition of "diagonal" and "off-diagonal" portion of the matrix 565203bfb495SBarry Smith 56537b55108eSBarry Smith This sets local rows and cannot be used to set off-processor values. 56547b55108eSBarry Smith 5655dca341c0SJed Brown Use of this routine is discouraged because it is inflexible and cumbersome to use. It is extremely rare that a 5656dca341c0SJed Brown legacy application natively assembles into exactly this split format. The code to do so is nontrivial and does 5657dca341c0SJed Brown not easily support in-place reassembly. It is recommended to use MatSetValues() (or a variant thereof) because 5658dca341c0SJed Brown the resulting assembly is easier to implement, will work with any matrix format, and the user does not have to 5659dca341c0SJed Brown keep track of the underlying array. Use MatSetOption(A,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE) to disable all 5660dca341c0SJed Brown communication if it is known that only local entries will be set. 566103bfb495SBarry Smith 566203bfb495SBarry Smith .keywords: matrix, aij, compressed row, sparse, parallel 566303bfb495SBarry Smith 566403bfb495SBarry Smith .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(), 566569b1f4b7SBarry Smith MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithArrays() 566603bfb495SBarry Smith @*/ 56672205254eSKarl Rupp PetscErrorCode MatCreateMPIAIJWithSplitArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt i[],PetscInt j[],PetscScalar a[],PetscInt oi[], PetscInt oj[],PetscScalar oa[],Mat *mat) 566803bfb495SBarry Smith { 566903bfb495SBarry Smith PetscErrorCode ierr; 567003bfb495SBarry Smith Mat_MPIAIJ *maij; 567103bfb495SBarry Smith 567203bfb495SBarry Smith PetscFunctionBegin; 5673e32f2f54SBarry Smith if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative"); 5674ea345e14SBarry Smith if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0"); 5675ea345e14SBarry Smith if (oi[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"oi (row indices) must start with 0"); 567603bfb495SBarry Smith ierr = MatCreate(comm,mat);CHKERRQ(ierr); 567703bfb495SBarry Smith ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); 567803bfb495SBarry Smith ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); 567903bfb495SBarry Smith maij = (Mat_MPIAIJ*) (*mat)->data; 56802205254eSKarl Rupp 56818d7a6e47SBarry Smith (*mat)->preallocated = PETSC_TRUE; 568203bfb495SBarry Smith 568326283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->rmap);CHKERRQ(ierr); 568426283091SBarry Smith ierr = PetscLayoutSetUp((*mat)->cmap);CHKERRQ(ierr); 568503bfb495SBarry Smith 568603bfb495SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,n,i,j,a,&maij->A);CHKERRQ(ierr); 5687d0f46423SBarry Smith ierr = MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,m,(*mat)->cmap->N,oi,oj,oa,&maij->B);CHKERRQ(ierr); 568803bfb495SBarry Smith 56898d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 56908d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 56918d7a6e47SBarry Smith ierr = MatAssemblyBegin(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 56928d7a6e47SBarry Smith ierr = MatAssemblyEnd(maij->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 56938d7a6e47SBarry Smith 569403bfb495SBarry Smith ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 569503bfb495SBarry Smith ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 5696dca341c0SJed Brown ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); 569703bfb495SBarry Smith PetscFunctionReturn(0); 569803bfb495SBarry Smith } 569903bfb495SBarry Smith 570081824310SBarry Smith /* 570181824310SBarry Smith Special version for direct calls from Fortran 570281824310SBarry Smith */ 5703b45d2f2cSJed Brown #include <petsc-private/fortranimpl.h> 57047087cfbeSBarry Smith 570581824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 570681824310SBarry Smith #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ 570781824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 570881824310SBarry Smith #define matsetvaluesmpiaij_ matsetvaluesmpiaij 570981824310SBarry Smith #endif 571081824310SBarry Smith 571181824310SBarry Smith /* Change these macros so can be used in void function */ 571281824310SBarry Smith #undef CHKERRQ 5713e32f2f54SBarry Smith #define CHKERRQ(ierr) CHKERRABORT(PETSC_COMM_WORLD,ierr) 571481824310SBarry Smith #undef SETERRQ2 5715e32f2f54SBarry Smith #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr) 57164994cf47SJed Brown #undef SETERRQ3 57174994cf47SJed Brown #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr) 571881824310SBarry Smith #undef SETERRQ 5719e32f2f54SBarry Smith #define SETERRQ(c,ierr,b) CHKERRABORT(c,ierr) 572081824310SBarry Smith 572181824310SBarry Smith #undef __FUNCT__ 572281824310SBarry Smith #define __FUNCT__ "matsetvaluesmpiaij_" 57238cc058d9SJed Brown PETSC_EXTERN void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr) 572481824310SBarry Smith { 572581824310SBarry Smith Mat mat = *mmat; 572681824310SBarry Smith PetscInt m = *mm, n = *mn; 572781824310SBarry Smith InsertMode addv = *maddv; 572881824310SBarry Smith Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data; 572981824310SBarry Smith PetscScalar value; 573081824310SBarry Smith PetscErrorCode ierr; 5731899cda47SBarry Smith 57324994cf47SJed Brown MatCheckPreallocated(mat,1); 57332205254eSKarl Rupp if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv; 57342205254eSKarl Rupp 573581824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5736f23aa3ddSBarry Smith else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 573781824310SBarry Smith #endif 573881824310SBarry Smith { 5739d0f46423SBarry Smith PetscInt i,j,rstart = mat->rmap->rstart,rend = mat->rmap->rend; 5740d0f46423SBarry Smith PetscInt cstart = mat->cmap->rstart,cend = mat->cmap->rend,row,col; 5741ace3abfcSBarry Smith PetscBool roworiented = aij->roworiented; 574281824310SBarry Smith 574381824310SBarry Smith /* Some Variables required in the macro */ 574481824310SBarry Smith Mat A = aij->A; 574581824310SBarry Smith Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data; 574681824310SBarry Smith PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j; 5747dd6ea824SBarry Smith MatScalar *aa = a->a; 5748ace3abfcSBarry Smith PetscBool ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES)) ? PETSC_TRUE : PETSC_FALSE); 574981824310SBarry Smith Mat B = aij->B; 575081824310SBarry Smith Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data; 5751d0f46423SBarry Smith PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap->n,am = aij->A->rmap->n; 5752dd6ea824SBarry Smith MatScalar *ba = b->a; 575381824310SBarry Smith 575481824310SBarry Smith PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2; 575581824310SBarry Smith PetscInt nonew = a->nonew; 5756dd6ea824SBarry Smith MatScalar *ap1,*ap2; 575781824310SBarry Smith 575881824310SBarry Smith PetscFunctionBegin; 575981824310SBarry Smith for (i=0; i<m; i++) { 576081824310SBarry Smith if (im[i] < 0) continue; 576181824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5762e32f2f54SBarry 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); 576381824310SBarry Smith #endif 576481824310SBarry Smith if (im[i] >= rstart && im[i] < rend) { 576581824310SBarry Smith row = im[i] - rstart; 576681824310SBarry Smith lastcol1 = -1; 576781824310SBarry Smith rp1 = aj + ai[row]; 576881824310SBarry Smith ap1 = aa + ai[row]; 576981824310SBarry Smith rmax1 = aimax[row]; 577081824310SBarry Smith nrow1 = ailen[row]; 577181824310SBarry Smith low1 = 0; 577281824310SBarry Smith high1 = nrow1; 577381824310SBarry Smith lastcol2 = -1; 577481824310SBarry Smith rp2 = bj + bi[row]; 577581824310SBarry Smith ap2 = ba + bi[row]; 577681824310SBarry Smith rmax2 = bimax[row]; 577781824310SBarry Smith nrow2 = bilen[row]; 577881824310SBarry Smith low2 = 0; 577981824310SBarry Smith high2 = nrow2; 578081824310SBarry Smith 578181824310SBarry Smith for (j=0; j<n; j++) { 57822205254eSKarl Rupp if (roworiented) value = v[i*n+j]; 57832205254eSKarl Rupp else value = v[i+j*m]; 578481824310SBarry Smith if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue; 578581824310SBarry Smith if (in[j] >= cstart && in[j] < cend) { 578681824310SBarry Smith col = in[j] - cstart; 578781824310SBarry Smith MatSetValues_SeqAIJ_A_Private(row,col,value,addv); 578881824310SBarry Smith } else if (in[j] < 0) continue; 578981824310SBarry Smith #if defined(PETSC_USE_DEBUG) 5790cb9801acSJed 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); 579181824310SBarry Smith #endif 579281824310SBarry Smith else { 579381824310SBarry Smith if (mat->was_assembled) { 579481824310SBarry Smith if (!aij->colmap) { 5795ab9863d7SBarry Smith ierr = MatCreateColmap_MPIAIJ_Private(mat);CHKERRQ(ierr); 579681824310SBarry Smith } 579781824310SBarry Smith #if defined(PETSC_USE_CTABLE) 579881824310SBarry Smith ierr = PetscTableFind(aij->colmap,in[j]+1,&col);CHKERRQ(ierr); 579981824310SBarry Smith col--; 580081824310SBarry Smith #else 580181824310SBarry Smith col = aij->colmap[in[j]] - 1; 580281824310SBarry Smith #endif 580381824310SBarry Smith if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) { 5804ab9863d7SBarry Smith ierr = MatDisAssemble_MPIAIJ(mat);CHKERRQ(ierr); 580581824310SBarry Smith col = in[j]; 580681824310SBarry Smith /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */ 580781824310SBarry Smith B = aij->B; 580881824310SBarry Smith b = (Mat_SeqAIJ*)B->data; 580981824310SBarry Smith bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j; 581081824310SBarry Smith rp2 = bj + bi[row]; 581181824310SBarry Smith ap2 = ba + bi[row]; 581281824310SBarry Smith rmax2 = bimax[row]; 581381824310SBarry Smith nrow2 = bilen[row]; 581481824310SBarry Smith low2 = 0; 581581824310SBarry Smith high2 = nrow2; 5816d0f46423SBarry Smith bm = aij->B->rmap->n; 581781824310SBarry Smith ba = b->a; 581881824310SBarry Smith } 581981824310SBarry Smith } else col = in[j]; 582081824310SBarry Smith MatSetValues_SeqAIJ_B_Private(row,col,value,addv); 582181824310SBarry Smith } 582281824310SBarry Smith } 58232205254eSKarl Rupp } else if (!aij->donotstash) { 582481824310SBarry Smith if (roworiented) { 5825ace3abfcSBarry Smith ierr = MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 582681824310SBarry Smith } else { 5827ace3abfcSBarry Smith ierr = MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,(PetscBool)(ignorezeroentries && (addv == ADD_VALUES)));CHKERRQ(ierr); 582881824310SBarry Smith } 582981824310SBarry Smith } 583081824310SBarry Smith } 58312205254eSKarl Rupp } 583281824310SBarry Smith PetscFunctionReturnVoid(); 583381824310SBarry Smith } 583403bfb495SBarry Smith 5835