xref: /petsc/src/mat/impls/adj/mpi/mpiadj.c (revision 141963915506c7f79f51055c07c4434756e2519e)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
3b97cf49bSBarry Smith /*
4b97cf49bSBarry Smith     Defines the basic matrix operations for the ADJ adjacency list matrix data-structure.
5b97cf49bSBarry Smith */
67c4f633dSBarry Smith #include "../src/mat/impls/adj/mpi/mpiadj.h"
7325e03aeSBarry Smith #include "petscsys.h"
8b97cf49bSBarry Smith 
94a2ae208SSatish Balay #undef __FUNCT__
104a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAdj_ASCII"
11dfbe8321SBarry Smith PetscErrorCode MatView_MPIAdj_ASCII(Mat A,PetscViewer viewer)
12b97cf49bSBarry Smith {
133eda8832SBarry Smith   Mat_MPIAdj        *a = (Mat_MPIAdj*)A->data;
14dfbe8321SBarry Smith   PetscErrorCode    ierr;
15d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n;
162dcb1b2aSMatthew Knepley   const char        *name;
17ce1411ecSBarry Smith   PetscViewerFormat format;
18b97cf49bSBarry Smith 
19433994e6SBarry Smith   PetscFunctionBegin;
203a7fca6bSBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
21b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
22fb9695e5SSatish Balay   if (format == PETSC_VIEWER_ASCII_INFO) {
233a40ed3dSBarry Smith     PetscFunctionReturn(0);
24fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
25ffac6cdbSBarry Smith     SETERRQ(PETSC_ERR_SUP,"Matlab format not supported");
260752156aSBarry Smith   } else {
27b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
28b97cf49bSBarry Smith     for (i=0; i<m; i++) {
29d0f46423SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"row %D:",i+A->rmap->rstart);CHKERRQ(ierr);
30b97cf49bSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
3177431f27SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer," %D ",a->j[j]);CHKERRQ(ierr);
320752156aSBarry Smith       }
33b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
34b97cf49bSBarry Smith     }
35b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
36b97cf49bSBarry Smith   }
37b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
383a40ed3dSBarry Smith   PetscFunctionReturn(0);
39b97cf49bSBarry Smith }
40b97cf49bSBarry Smith 
414a2ae208SSatish Balay #undef __FUNCT__
424a2ae208SSatish Balay #define __FUNCT__ "MatView_MPIAdj"
43dfbe8321SBarry Smith PetscErrorCode MatView_MPIAdj(Mat A,PetscViewer viewer)
44b97cf49bSBarry Smith {
45dfbe8321SBarry Smith   PetscErrorCode ierr;
4632077d6dSBarry Smith   PetscTruth     iascii;
47b97cf49bSBarry Smith 
48433994e6SBarry Smith   PetscFunctionBegin;
4932077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
5032077d6dSBarry Smith   if (iascii) {
513eda8832SBarry Smith     ierr = MatView_MPIAdj_ASCII(A,viewer);CHKERRQ(ierr);
525cd90555SBarry Smith   } else {
5379a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIAdj",((PetscObject)viewer)->type_name);
54b97cf49bSBarry Smith   }
553a40ed3dSBarry Smith   PetscFunctionReturn(0);
56b97cf49bSBarry Smith }
57b97cf49bSBarry Smith 
584a2ae208SSatish Balay #undef __FUNCT__
594a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_MPIAdj"
60dfbe8321SBarry Smith PetscErrorCode MatDestroy_MPIAdj(Mat mat)
61b97cf49bSBarry Smith {
623eda8832SBarry Smith   Mat_MPIAdj     *a = (Mat_MPIAdj*)mat->data;
63dfbe8321SBarry Smith   PetscErrorCode ierr;
6494d884c6SBarry Smith 
6594d884c6SBarry Smith   PetscFunctionBegin;
66aa482453SBarry Smith #if defined(PETSC_USE_LOG)
67d0f46423SBarry Smith   PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D, NZ=%D",mat->rmap->n,mat->cmap->n,a->nz);
68b97cf49bSBarry Smith #endif
6905b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
700400557aSBarry Smith   if (a->freeaij) {
71*14196391SBarry Smith     if (a->freeaijwithfree) {
72*14196391SBarry Smith       if (a->i) free(a->i);
73*14196391SBarry Smith       if (a->j) free(a->j);
74*14196391SBarry Smith     } else {
75606d414cSSatish Balay       ierr = PetscFree(a->i);CHKERRQ(ierr);
76606d414cSSatish Balay       ierr = PetscFree(a->j);CHKERRQ(ierr);
7705b42c5fSBarry Smith       ierr = PetscFree(a->values);CHKERRQ(ierr);
78*14196391SBarry Smith     }
790400557aSBarry Smith   }
801198db28SBarry Smith   ierr = PetscFree(a);CHKERRQ(ierr);
81dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)mat,0);CHKERRQ(ierr);
82901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMPIAdjSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
833a40ed3dSBarry Smith   PetscFunctionReturn(0);
84b97cf49bSBarry Smith }
85b97cf49bSBarry Smith 
864a2ae208SSatish Balay #undef __FUNCT__
874a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_MPIAdj"
884e0d8c25SBarry Smith PetscErrorCode MatSetOption_MPIAdj(Mat A,MatOption op,PetscTruth flg)
89b97cf49bSBarry Smith {
903eda8832SBarry Smith   Mat_MPIAdj     *a = (Mat_MPIAdj*)A->data;
9163ba0a88SBarry Smith   PetscErrorCode ierr;
92b97cf49bSBarry Smith 
93433994e6SBarry Smith   PetscFunctionBegin;
9412c028f9SKris Buschelman   switch (op) {
9577e54ba9SKris Buschelman   case MAT_SYMMETRIC:
9612c028f9SKris Buschelman   case MAT_STRUCTURALLY_SYMMETRIC:
979a4540c5SBarry Smith   case MAT_HERMITIAN:
984e0d8c25SBarry Smith     a->symmetric = flg;
999a4540c5SBarry Smith     break;
1009a4540c5SBarry Smith   case MAT_SYMMETRY_ETERNAL:
1019a4540c5SBarry Smith     break;
10212c028f9SKris Buschelman   default:
103290bbb0aSBarry Smith     ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
10412c028f9SKris Buschelman     break;
105b97cf49bSBarry Smith   }
1063a40ed3dSBarry Smith   PetscFunctionReturn(0);
107b97cf49bSBarry Smith }
108b97cf49bSBarry Smith 
109b97cf49bSBarry Smith 
110b97cf49bSBarry Smith /*
111b97cf49bSBarry Smith      Adds diagonal pointers to sparse matrix structure.
112b97cf49bSBarry Smith */
113b97cf49bSBarry Smith 
1144a2ae208SSatish Balay #undef __FUNCT__
1154a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_MPIAdj"
116dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_MPIAdj(Mat A)
117b97cf49bSBarry Smith {
1183eda8832SBarry Smith   Mat_MPIAdj     *a = (Mat_MPIAdj*)A->data;
1196849ba73SBarry Smith   PetscErrorCode ierr;
120d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
121b97cf49bSBarry Smith 
122433994e6SBarry Smith   PetscFunctionBegin;
12309f38230SBarry Smith   ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
12409f38230SBarry Smith   ierr = PetscLogObjectMemory(A,m*sizeof(PetscInt));CHKERRQ(ierr);
125d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
126b97cf49bSBarry Smith     for (j=a->i[i]; j<a->i[i+1]; j++) {
127b97cf49bSBarry Smith       if (a->j[j] == i) {
12809f38230SBarry Smith         a->diag[i] = j;
129b97cf49bSBarry Smith         break;
130b97cf49bSBarry Smith       }
131b97cf49bSBarry Smith     }
132b97cf49bSBarry Smith   }
1333a40ed3dSBarry Smith   PetscFunctionReturn(0);
134b97cf49bSBarry Smith }
135b97cf49bSBarry Smith 
1364a2ae208SSatish Balay #undef __FUNCT__
1374a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_MPIAdj"
138b24ad042SBarry Smith PetscErrorCode MatGetRow_MPIAdj(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139b97cf49bSBarry Smith {
1403eda8832SBarry Smith   Mat_MPIAdj *a = (Mat_MPIAdj*)A->data;
141b24ad042SBarry Smith   PetscInt   *itmp;
142b97cf49bSBarry Smith 
143433994e6SBarry Smith   PetscFunctionBegin;
144d0f46423SBarry Smith   row -= A->rmap->rstart;
1450752156aSBarry Smith 
146d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row out of range");
147b97cf49bSBarry Smith 
148b97cf49bSBarry Smith   *nz = a->i[row+1] - a->i[row];
149b97cf49bSBarry Smith   if (v) *v = PETSC_NULL;
150b97cf49bSBarry Smith   if (idx) {
151b97cf49bSBarry Smith     itmp = a->j + a->i[row];
152b97cf49bSBarry Smith     if (*nz) {
153b97cf49bSBarry Smith       *idx = itmp;
154b97cf49bSBarry Smith     }
155b97cf49bSBarry Smith     else *idx = 0;
156b97cf49bSBarry Smith   }
1573a40ed3dSBarry Smith   PetscFunctionReturn(0);
158b97cf49bSBarry Smith }
159b97cf49bSBarry Smith 
1604a2ae208SSatish Balay #undef __FUNCT__
1614a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_MPIAdj"
162b24ad042SBarry Smith PetscErrorCode MatRestoreRow_MPIAdj(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
163b97cf49bSBarry Smith {
164433994e6SBarry Smith   PetscFunctionBegin;
1653a40ed3dSBarry Smith   PetscFunctionReturn(0);
166b97cf49bSBarry Smith }
167b97cf49bSBarry Smith 
1684a2ae208SSatish Balay #undef __FUNCT__
1694a2ae208SSatish Balay #define __FUNCT__ "MatEqual_MPIAdj"
170dfbe8321SBarry Smith PetscErrorCode MatEqual_MPIAdj(Mat A,Mat B,PetscTruth* flg)
171b97cf49bSBarry Smith {
1723eda8832SBarry Smith   Mat_MPIAdj     *a = (Mat_MPIAdj *)A->data,*b = (Mat_MPIAdj *)B->data;
173dfbe8321SBarry Smith   PetscErrorCode ierr;
1740f5bd95cSBarry Smith   PetscTruth     flag;
175b97cf49bSBarry Smith 
176433994e6SBarry Smith   PetscFunctionBegin;
177b97cf49bSBarry Smith   /* If the  matrix dimensions are not equal,or no of nonzeros */
178d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) ||(a->nz != b->nz)) {
1790f5bd95cSBarry Smith     flag = PETSC_FALSE;
180b97cf49bSBarry Smith   }
181b97cf49bSBarry Smith 
182b97cf49bSBarry Smith   /* if the a->i are the same */
183d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),&flag);CHKERRQ(ierr);
184b97cf49bSBarry Smith 
185b97cf49bSBarry Smith   /* if a->j are the same */
186b24ad042SBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),&flag);CHKERRQ(ierr);
187b97cf49bSBarry Smith 
1887adad957SLisandro Dalcin   ierr = MPI_Allreduce(&flag,flg,1,MPI_INT,MPI_LAND,((PetscObject)A)->comm);CHKERRQ(ierr);
1893a40ed3dSBarry Smith   PetscFunctionReturn(0);
190b97cf49bSBarry Smith }
191b97cf49bSBarry Smith 
1924a2ae208SSatish Balay #undef __FUNCT__
1934a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_MPIAdj"
1948f7157efSSatish Balay PetscErrorCode MatGetRowIJ_MPIAdj(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1956cd91f64SBarry Smith {
196dfbe8321SBarry Smith   PetscErrorCode ierr;
197b24ad042SBarry Smith   PetscMPIInt    size;
198b24ad042SBarry Smith   PetscInt       i;
1996cd91f64SBarry Smith   Mat_MPIAdj     *a = (Mat_MPIAdj *)A->data;
2006cd91f64SBarry Smith 
2016cd91f64SBarry Smith   PetscFunctionBegin;
2027adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)A)->comm,&size);CHKERRQ(ierr);
2036cd91f64SBarry Smith   if (size > 1) {*done = PETSC_FALSE; PetscFunctionReturn(0);}
204d0f46423SBarry Smith   *m    = A->rmap->n;
2056cd91f64SBarry Smith   *ia   = a->i;
2066cd91f64SBarry Smith   *ja   = a->j;
2076cd91f64SBarry Smith   *done = PETSC_TRUE;
208d42735a1SBarry Smith   if (oshift) {
209d42735a1SBarry Smith     for (i=0; i<(*ia)[*m]; i++) {
210d42735a1SBarry Smith       (*ja)[i]++;
211d42735a1SBarry Smith     }
212d42735a1SBarry Smith     for (i=0; i<=(*m); i++) (*ia)[i]++;
213d42735a1SBarry Smith   }
214d42735a1SBarry Smith   PetscFunctionReturn(0);
215d42735a1SBarry Smith }
216d42735a1SBarry Smith 
2174a2ae208SSatish Balay #undef __FUNCT__
2184a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_MPIAdj"
2198f7157efSSatish Balay PetscErrorCode MatRestoreRowIJ_MPIAdj(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth blockcompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
220d42735a1SBarry Smith {
221b24ad042SBarry Smith   PetscInt   i;
222d42735a1SBarry Smith   Mat_MPIAdj *a = (Mat_MPIAdj *)A->data;
223d42735a1SBarry Smith 
224d42735a1SBarry Smith   PetscFunctionBegin;
225e005ede5SBarry Smith   if (ia && a->i != *ia) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ia passed back is not one obtained with MatGetRowIJ()");
226e005ede5SBarry Smith   if (ja && a->j != *ja) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ja passed back is not one obtained with MatGetRowIJ()");
227d42735a1SBarry Smith   if (oshift) {
228d42735a1SBarry Smith     for (i=0; i<=(*m); i++) (*ia)[i]--;
229d42735a1SBarry Smith     for (i=0; i<(*ia)[*m]; i++) {
230d42735a1SBarry Smith       (*ja)[i]--;
231d42735a1SBarry Smith     }
232d42735a1SBarry Smith   }
2336cd91f64SBarry Smith   PetscFunctionReturn(0);
2346cd91f64SBarry Smith }
235b97cf49bSBarry Smith 
23617667f90SBarry Smith #undef __FUNCT__
23717667f90SBarry Smith #define __FUNCT__ "MatConvertFrom_MPIAdj"
238a313700dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConvertFrom_MPIAdj(Mat A,const MatType type,MatReuse reuse,Mat *newmat)
23917667f90SBarry Smith {
24017667f90SBarry Smith   Mat               B;
24117667f90SBarry Smith   PetscErrorCode    ierr;
24217667f90SBarry Smith   PetscInt          i,m,N,nzeros = 0,*ia,*ja,len,rstart,cnt,j,*a;
24317667f90SBarry Smith   const PetscInt    *rj;
24417667f90SBarry Smith   const PetscScalar *ra;
24517667f90SBarry Smith   MPI_Comm          comm;
24617667f90SBarry Smith 
24717667f90SBarry Smith   PetscFunctionBegin;
24817667f90SBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
24917667f90SBarry Smith   ierr = MatGetLocalSize(A,&m,PETSC_NULL);CHKERRQ(ierr);
25017667f90SBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,PETSC_NULL);CHKERRQ(ierr);
25117667f90SBarry Smith 
25217667f90SBarry Smith   /* count the number of nonzeros per row */
25317667f90SBarry Smith   for (i=0; i<m; i++) {
25417667f90SBarry Smith     ierr   = MatGetRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr);
25517667f90SBarry Smith     for (j=0; j<len; j++) {
25617667f90SBarry Smith       if (rj[j] == i+rstart) {len--; break;}    /* don't count diagonal */
25717667f90SBarry Smith     }
25817667f90SBarry Smith     ierr   = MatRestoreRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr);
25917667f90SBarry Smith     nzeros += len;
26017667f90SBarry Smith   }
26117667f90SBarry Smith 
26217667f90SBarry Smith   /* malloc space for nonzeros */
26317667f90SBarry Smith   ierr = PetscMalloc((nzeros+1)*sizeof(PetscInt),&a);CHKERRQ(ierr);
26417667f90SBarry Smith   ierr = PetscMalloc((N+1)*sizeof(PetscInt),&ia);CHKERRQ(ierr);
26517667f90SBarry Smith   ierr = PetscMalloc((nzeros+1)*sizeof(PetscInt),&ja);CHKERRQ(ierr);
26617667f90SBarry Smith 
26717667f90SBarry Smith   nzeros = 0;
26817667f90SBarry Smith   ia[0]  = 0;
26917667f90SBarry Smith   for (i=0; i<m; i++) {
27017667f90SBarry Smith     ierr    = MatGetRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr);
27117667f90SBarry Smith     cnt     = 0;
27217667f90SBarry Smith     for (j=0; j<len; j++) {
27317667f90SBarry Smith       if (rj[j] != i+rstart) { /* if not diagonal */
27417667f90SBarry Smith         a[nzeros+cnt]    = (PetscInt) PetscAbsScalar(ra[j]);
27517667f90SBarry Smith         ja[nzeros+cnt++] = rj[j];
27617667f90SBarry Smith       }
27717667f90SBarry Smith     }
27817667f90SBarry Smith     ierr    = MatRestoreRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr);
27917667f90SBarry Smith     nzeros += cnt;
28017667f90SBarry Smith     ia[i+1] = nzeros;
28117667f90SBarry Smith   }
28217667f90SBarry Smith 
28317667f90SBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
28417667f90SBarry Smith   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
28558ec18a5SLisandro Dalcin   ierr = MatSetSizes(B,m,PETSC_DETERMINE,PETSC_DETERMINE,N);CHKERRQ(ierr);
28617667f90SBarry Smith   ierr = MatSetType(B,type);CHKERRQ(ierr);
28717667f90SBarry Smith   ierr = MatMPIAdjSetPreallocation(B,ia,ja,a);CHKERRQ(ierr);
28817667f90SBarry Smith 
28917667f90SBarry Smith   if (reuse == MAT_REUSE_MATRIX) {
29017667f90SBarry Smith     ierr = MatHeaderReplace(A,B);CHKERRQ(ierr);
29117667f90SBarry Smith   } else {
29217667f90SBarry Smith     *newmat = B;
29317667f90SBarry Smith   }
29417667f90SBarry Smith   PetscFunctionReturn(0);
29517667f90SBarry Smith }
29617667f90SBarry Smith 
297b97cf49bSBarry Smith /* -------------------------------------------------------------------*/
2980b3b1487SBarry Smith static struct _MatOps MatOps_Values = {0,
2993eda8832SBarry Smith        MatGetRow_MPIAdj,
3003eda8832SBarry Smith        MatRestoreRow_MPIAdj,
301b97cf49bSBarry Smith        0,
30297304618SKris Buschelman /* 4*/ 0,
303b97cf49bSBarry Smith        0,
304b97cf49bSBarry Smith        0,
305b97cf49bSBarry Smith        0,
3060b3b1487SBarry Smith        0,
3070b3b1487SBarry Smith        0,
30897304618SKris Buschelman /*10*/ 0,
3090b3b1487SBarry Smith        0,
3100b3b1487SBarry Smith        0,
3110b3b1487SBarry Smith        0,
3120b3b1487SBarry Smith        0,
31397304618SKris Buschelman /*15*/ 0,
3143eda8832SBarry Smith        MatEqual_MPIAdj,
3150b3b1487SBarry Smith        0,
3160b3b1487SBarry Smith        0,
3170b3b1487SBarry Smith        0,
31897304618SKris Buschelman /*20*/ 0,
3190b3b1487SBarry Smith        0,
3203eda8832SBarry Smith        MatSetOption_MPIAdj,
3210b3b1487SBarry Smith        0,
322d519adbfSMatthew Knepley /*24*/ 0,
3230b3b1487SBarry Smith        0,
3240b3b1487SBarry Smith        0,
3250b3b1487SBarry Smith        0,
3260b3b1487SBarry Smith        0,
327d519adbfSMatthew Knepley /*29*/ 0,
3280b3b1487SBarry Smith        0,
329273d9f13SBarry Smith        0,
330273d9f13SBarry Smith        0,
3310b3b1487SBarry Smith        0,
332d519adbfSMatthew Knepley /*34*/ 0,
3330b3b1487SBarry Smith        0,
3340b3b1487SBarry Smith        0,
3350b3b1487SBarry Smith        0,
3360b3b1487SBarry Smith        0,
337d519adbfSMatthew Knepley /*39*/ 0,
3380b3b1487SBarry Smith        0,
3390b3b1487SBarry Smith        0,
3400b3b1487SBarry Smith        0,
3410b3b1487SBarry Smith        0,
342d519adbfSMatthew Knepley /*44*/ 0,
3430b3b1487SBarry Smith        0,
3440b3b1487SBarry Smith        0,
3450b3b1487SBarry Smith        0,
3460b3b1487SBarry Smith        0,
347d519adbfSMatthew Knepley /*49*/ 0,
3486cd91f64SBarry Smith        MatGetRowIJ_MPIAdj,
349d42735a1SBarry Smith        MatRestoreRowIJ_MPIAdj,
350b97cf49bSBarry Smith        0,
351b97cf49bSBarry Smith        0,
352d519adbfSMatthew Knepley /*54*/ 0,
353b97cf49bSBarry Smith        0,
3540752156aSBarry Smith        0,
3550752156aSBarry Smith        0,
3560b3b1487SBarry Smith        0,
357d519adbfSMatthew Knepley /*59*/ 0,
358b9b97703SBarry Smith        MatDestroy_MPIAdj,
359b9b97703SBarry Smith        MatView_MPIAdj,
36017667f90SBarry Smith        MatConvertFrom_MPIAdj,
36197304618SKris Buschelman        0,
362d519adbfSMatthew Knepley /*64*/ 0,
36397304618SKris Buschelman        0,
36497304618SKris Buschelman        0,
36597304618SKris Buschelman        0,
36697304618SKris Buschelman        0,
367d519adbfSMatthew Knepley /*69*/ 0,
36897304618SKris Buschelman        0,
36997304618SKris Buschelman        0,
37097304618SKris Buschelman        0,
37197304618SKris Buschelman        0,
372d519adbfSMatthew Knepley /*74*/ 0,
37397304618SKris Buschelman        0,
37497304618SKris Buschelman        0,
37597304618SKris Buschelman        0,
37697304618SKris Buschelman        0,
377d519adbfSMatthew Knepley /*79*/ 0,
37897304618SKris Buschelman        0,
37997304618SKris Buschelman        0,
38097304618SKris Buschelman        0,
381865e5f61SKris Buschelman        0,
382d519adbfSMatthew Knepley /*84*/ 0,
383865e5f61SKris Buschelman        0,
384865e5f61SKris Buschelman        0,
385865e5f61SKris Buschelman        0,
386865e5f61SKris Buschelman        0,
387d519adbfSMatthew Knepley /*89*/ 0,
388865e5f61SKris Buschelman        0,
389865e5f61SKris Buschelman        0,
390865e5f61SKris Buschelman        0,
391865e5f61SKris Buschelman        0,
392d519adbfSMatthew Knepley /*94*/ 0,
393865e5f61SKris Buschelman        0,
394865e5f61SKris Buschelman        0,
395865e5f61SKris Buschelman        0};
396b97cf49bSBarry Smith 
397a23d5eceSKris Buschelman EXTERN_C_BEGIN
398a23d5eceSKris Buschelman #undef __FUNCT__
399a23d5eceSKris Buschelman #define __FUNCT__ "MatMPIAdjSetPreallocation_MPIAdj"
400be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAdjSetPreallocation_MPIAdj(Mat B,PetscInt *i,PetscInt *j,PetscInt *values)
401a23d5eceSKris Buschelman {
402a23d5eceSKris Buschelman   Mat_MPIAdj     *b = (Mat_MPIAdj *)B->data;
403dfbe8321SBarry Smith   PetscErrorCode ierr;
4042515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
405b24ad042SBarry Smith   PetscInt       ii;
406a23d5eceSKris Buschelman #endif
407a23d5eceSKris Buschelman 
408a23d5eceSKris Buschelman   PetscFunctionBegin;
40958ec18a5SLisandro Dalcin   ierr = PetscMapSetBlockSize(B->rmap,1);CHKERRQ(ierr);
41058ec18a5SLisandro Dalcin   ierr = PetscMapSetBlockSize(B->cmap,1);CHKERRQ(ierr);
41158ec18a5SLisandro Dalcin   ierr = PetscMapSetUp(B->rmap);CHKERRQ(ierr);
41258ec18a5SLisandro Dalcin   ierr = PetscMapSetUp(B->cmap);CHKERRQ(ierr);
41358ec18a5SLisandro Dalcin 
4142515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
41577431f27SBarry Smith   if (i[0] != 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"First i[] index must be zero, instead it is %D\n",i[0]);
416d0f46423SBarry Smith   for (ii=1; ii<B->rmap->n; ii++) {
417a23d5eceSKris Buschelman     if (i[ii] < 0 || i[ii] < i[ii-1]) {
41877431f27SBarry Smith       SETERRQ4(PETSC_ERR_ARG_OUTOFRANGE,"i[%D]=%D index is out of range: i[%D]=%D",ii,i[ii],ii-1,i[ii-1]);
419a23d5eceSKris Buschelman     }
420a23d5eceSKris Buschelman   }
421d0f46423SBarry Smith   for (ii=0; ii<i[B->rmap->n]; ii++) {
422d0f46423SBarry Smith     if (j[ii] < 0 || j[ii] >= B->cmap->N) {
42377431f27SBarry Smith       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index %D out of range %D\n",ii,j[ii]);
424a23d5eceSKris Buschelman     }
425a23d5eceSKris Buschelman   }
426a23d5eceSKris Buschelman #endif
42758ec18a5SLisandro Dalcin   B->preallocated = PETSC_TRUE;
428a23d5eceSKris Buschelman 
429a23d5eceSKris Buschelman   b->j      = j;
430a23d5eceSKris Buschelman   b->i      = i;
431a23d5eceSKris Buschelman   b->values = values;
432a23d5eceSKris Buschelman 
433d0f46423SBarry Smith   b->nz               = i[B->rmap->n];
434a23d5eceSKris Buschelman   b->diag             = 0;
435a23d5eceSKris Buschelman   b->symmetric        = PETSC_FALSE;
436a23d5eceSKris Buschelman   b->freeaij          = PETSC_TRUE;
437a23d5eceSKris Buschelman 
438a23d5eceSKris Buschelman   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
439a23d5eceSKris Buschelman   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
440a23d5eceSKris Buschelman   PetscFunctionReturn(0);
441a23d5eceSKris Buschelman }
442a23d5eceSKris Buschelman EXTERN_C_END
443b97cf49bSBarry Smith 
4440bad9183SKris Buschelman /*MC
445fafad747SKris Buschelman    MATMPIADJ - MATMPIADJ = "mpiadj" - A matrix type to be used for distributed adjacency matrices,
4460bad9183SKris Buschelman    intended for use constructing orderings and partitionings.
4470bad9183SKris Buschelman 
4480bad9183SKris Buschelman   Level: beginner
4490bad9183SKris Buschelman 
4500bad9183SKris Buschelman .seealso: MatCreateMPIAdj
4510bad9183SKris Buschelman M*/
4520bad9183SKris Buschelman 
453273d9f13SBarry Smith EXTERN_C_BEGIN
4544a2ae208SSatish Balay #undef __FUNCT__
4554a2ae208SSatish Balay #define __FUNCT__ "MatCreate_MPIAdj"
456be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MPIAdj(Mat B)
457273d9f13SBarry Smith {
458273d9f13SBarry Smith   Mat_MPIAdj     *b;
4596849ba73SBarry Smith   PetscErrorCode ierr;
460b24ad042SBarry Smith   PetscMPIInt    size,rank;
461273d9f13SBarry Smith 
462273d9f13SBarry Smith   PetscFunctionBegin;
4637adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
4647adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)B)->comm,&rank);CHKERRQ(ierr);
465273d9f13SBarry Smith 
46638f2d2fdSLisandro Dalcin   ierr                = PetscNewLog(B,Mat_MPIAdj,&b);CHKERRQ(ierr);
467b0a32e0cSBarry Smith   B->data             = (void*)b;
468273d9f13SBarry Smith   ierr                = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
469273d9f13SBarry Smith   B->mapping          = 0;
470273d9f13SBarry Smith   B->assembled        = PETSC_FALSE;
471273d9f13SBarry Smith 
472a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAdjSetPreallocation_C",
473a23d5eceSKris Buschelman                                     "MatMPIAdjSetPreallocation_MPIAdj",
474a23d5eceSKris Buschelman                                      MatMPIAdjSetPreallocation_MPIAdj);CHKERRQ(ierr);
47517667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATMPIADJ);CHKERRQ(ierr);
476273d9f13SBarry Smith   PetscFunctionReturn(0);
477273d9f13SBarry Smith }
478273d9f13SBarry Smith EXTERN_C_END
479273d9f13SBarry Smith 
4804a2ae208SSatish Balay #undef __FUNCT__
4814a2ae208SSatish Balay #define __FUNCT__ "MatMPIAdjSetPreallocation"
482a23d5eceSKris Buschelman /*@C
483a23d5eceSKris Buschelman    MatMPIAdjSetPreallocation - Sets the array used for storing the matrix elements
484a23d5eceSKris Buschelman 
485a23d5eceSKris Buschelman    Collective on MPI_Comm
486a23d5eceSKris Buschelman 
487a23d5eceSKris Buschelman    Input Parameters:
488a23d5eceSKris Buschelman +  A - the matrix
489a23d5eceSKris Buschelman .  i - the indices into j for the start of each row
490a23d5eceSKris Buschelman .  j - the column indices for each row (sorted for each row).
491a23d5eceSKris Buschelman        The indices in i and j start with zero (NOT with one).
492a23d5eceSKris Buschelman -  values - [optional] edge weights
493a23d5eceSKris Buschelman 
494a23d5eceSKris Buschelman    Level: intermediate
495a23d5eceSKris Buschelman 
496a23d5eceSKris Buschelman .seealso: MatCreate(), MatCreateMPIAdj(), MatSetValues()
497a23d5eceSKris Buschelman @*/
498be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatMPIAdjSetPreallocation(Mat B,PetscInt *i,PetscInt *j,PetscInt *values)
499273d9f13SBarry Smith {
500b24ad042SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt*,PetscInt*,PetscInt*);
501273d9f13SBarry Smith 
502273d9f13SBarry Smith   PetscFunctionBegin;
503a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatMPIAdjSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
504a23d5eceSKris Buschelman   if (f) {
505a23d5eceSKris Buschelman     ierr = (*f)(B,i,j,values);CHKERRQ(ierr);
506273d9f13SBarry Smith   }
507273d9f13SBarry Smith   PetscFunctionReturn(0);
508273d9f13SBarry Smith }
509273d9f13SBarry Smith 
5104a2ae208SSatish Balay #undef __FUNCT__
5114a2ae208SSatish Balay #define __FUNCT__ "MatCreateMPIAdj"
512b97cf49bSBarry Smith /*@C
5133eda8832SBarry Smith    MatCreateMPIAdj - Creates a sparse matrix representing an adjacency list.
514b97cf49bSBarry Smith    The matrix does not have numerical values associated with it, but is
515b97cf49bSBarry Smith    intended for ordering (to reduce bandwidth etc) and partitioning.
516b97cf49bSBarry Smith 
517ef5ee4d1SLois Curfman McInnes    Collective on MPI_Comm
518ef5ee4d1SLois Curfman McInnes 
519b97cf49bSBarry Smith    Input Parameters:
520c2e958feSBarry Smith +  comm - MPI communicator
5210752156aSBarry Smith .  m - number of local rows
52258ec18a5SLisandro Dalcin .  N - number of global columns
523b97cf49bSBarry Smith .  i - the indices into j for the start of each row
524329f5518SBarry Smith .  j - the column indices for each row (sorted for each row).
525ef5ee4d1SLois Curfman McInnes        The indices in i and j start with zero (NOT with one).
526329f5518SBarry Smith -  values -[optional] edge weights
527b97cf49bSBarry Smith 
528b97cf49bSBarry Smith    Output Parameter:
529b97cf49bSBarry Smith .  A - the matrix
530b97cf49bSBarry Smith 
53115091d37SBarry Smith    Level: intermediate
53215091d37SBarry Smith 
5334bc6d8c0SBarry Smith    Notes: This matrix object does not support most matrix operations, include
5344bc6d8c0SBarry Smith    MatSetValues().
535c2e958feSBarry Smith    You must NOT free the ii, values and jj arrays yourself. PETSc will free them
536ededeb1aSvictorle    when the matrix is destroyed; you must allocate them with PetscMalloc(). If you
5371198db28SBarry Smith     call from Fortran you need not create the arrays with PetscMalloc().
538327e1a73SBarry Smith    Should not include the matrix diagonals.
539b97cf49bSBarry Smith 
540e3f7e1d6Svictorle    If you already have a matrix, you can create its adjacency matrix by a call
541ededeb1aSvictorle    to MatConvert, specifying a type of MATMPIADJ.
542ededeb1aSvictorle 
543ef5ee4d1SLois Curfman McInnes    Possible values for MatSetOption() - MAT_STRUCTURALLY_SYMMETRIC
544b97cf49bSBarry Smith 
545e3f7e1d6Svictorle .seealso: MatCreate(), MatConvert(), MatGetOrdering()
546b97cf49bSBarry Smith @*/
54758ec18a5SLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMPIAdj(MPI_Comm comm,PetscInt m,PetscInt N,PetscInt *i,PetscInt *j,PetscInt *values,Mat *A)
548b97cf49bSBarry Smith {
549dfbe8321SBarry Smith   PetscErrorCode ierr;
550b97cf49bSBarry Smith 
551433994e6SBarry Smith   PetscFunctionBegin;
552f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
55358ec18a5SLisandro Dalcin   ierr = MatSetSizes(*A,m,PETSC_DETERMINE,PETSC_DETERMINE,N);CHKERRQ(ierr);
554273d9f13SBarry Smith   ierr = MatSetType(*A,MATMPIADJ);CHKERRQ(ierr);
555273d9f13SBarry Smith   ierr = MatMPIAdjSetPreallocation(*A,i,j,values);CHKERRQ(ierr);
5563a40ed3dSBarry Smith   PetscFunctionReturn(0);
557b97cf49bSBarry Smith }
558b97cf49bSBarry Smith 
559c2e958feSBarry Smith 
560433994e6SBarry Smith 
561433994e6SBarry Smith 
562433994e6SBarry Smith 
563433994e6SBarry Smith 
564433994e6SBarry Smith 
565