xref: /petsc/src/mat/impls/adj/mpi/mpiadj.c (revision c2e958fe75ba9f152b17667af5d455ec9536503b)
1*c2e958feSBarry Smith /*$Id: mpicsr.c,v 1.34 2000/01/11 21:01:00 bsmith Exp bsmith $*/
2b97cf49bSBarry Smith 
3b97cf49bSBarry Smith /*
4b97cf49bSBarry Smith     Defines the basic matrix operations for the ADJ adjacency list matrix data-structure.
5b97cf49bSBarry Smith */
6b97cf49bSBarry Smith #include "sys.h"
7329f5518SBarry Smith #include "src/mat/impls/csr/mpi/mpicsr.h"
8b97cf49bSBarry Smith 
9b97cf49bSBarry Smith #undef __FUNC__
10329f5518SBarry Smith #define __FUNC__ "MatView_MPICSR_ASCII"
11329f5518SBarry Smith extern int MatView_MPICSR_ASCII(Mat A,Viewer viewer)
12b97cf49bSBarry Smith {
13329f5518SBarry Smith   Mat_MPICSR  *a = (Mat_MPICSR*)A->data;
14b97cf49bSBarry Smith   int         ierr,i,j,m = a->m, format;
15b97cf49bSBarry Smith   char        *outputname;
16b97cf49bSBarry Smith 
17433994e6SBarry Smith   PetscFunctionBegin;
1877ed5343SBarry Smith   ierr = ViewerGetOutputname(viewer,&outputname);CHKERRQ(ierr);
19d132466eSBarry Smith   ierr = ViewerGetFormat(viewer,&format);CHKERRQ(ierr);
20b97cf49bSBarry Smith   if (format == VIEWER_FORMAT_ASCII_INFO) {
213a40ed3dSBarry Smith     PetscFunctionReturn(0);
220752156aSBarry Smith   } else {
236831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
24b97cf49bSBarry Smith     for (i=0; i<m; i++) {
256831982aSBarry Smith       ierr = ViewerASCIISynchronizedPrintf(viewer,"row %d:",i+a->rstart);CHKERRQ(ierr);
26b97cf49bSBarry Smith       for (j=a->i[i]; j<a->i[i+1]; j++) {
276831982aSBarry Smith         ierr = ViewerASCIISynchronizedPrintf(viewer," %d ",a->j[j]);CHKERRQ(ierr);
280752156aSBarry Smith       }
296831982aSBarry Smith       ierr = ViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr);
30b97cf49bSBarry Smith     }
316831982aSBarry Smith     ierr = ViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
32b97cf49bSBarry Smith   }
336831982aSBarry Smith   ierr = ViewerFlush(viewer);CHKERRQ(ierr);
343a40ed3dSBarry Smith   PetscFunctionReturn(0);
35b97cf49bSBarry Smith }
36b97cf49bSBarry Smith 
37b97cf49bSBarry Smith #undef __FUNC__
38329f5518SBarry Smith #define __FUNC__ "MatView_MPICSR"
39329f5518SBarry Smith int MatView_MPICSR(Mat A,Viewer viewer)
40b97cf49bSBarry Smith {
41b97cf49bSBarry Smith   int        ierr;
426831982aSBarry Smith   PetscTruth isascii;
43b97cf49bSBarry Smith 
44433994e6SBarry Smith   PetscFunctionBegin;
456831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
460f5bd95cSBarry Smith   if (isascii) {
47329f5518SBarry Smith     ierr = MatView_MPICSR_ASCII(A,viewer);CHKERRQ(ierr);
485cd90555SBarry Smith   } else {
49329f5518SBarry Smith     SETERRQ1(1,1,"Viewer type %s not supported by MPICSR",((PetscObject)viewer)->type_name);
50b97cf49bSBarry Smith   }
513a40ed3dSBarry Smith   PetscFunctionReturn(0);
52b97cf49bSBarry Smith }
53b97cf49bSBarry Smith 
54b97cf49bSBarry Smith #undef __FUNC__
55329f5518SBarry Smith #define __FUNC__ "MatDestroy_MPICSR"
56329f5518SBarry Smith int MatDestroy_MPICSR(Mat mat)
57b97cf49bSBarry Smith {
58329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)mat->data;
5994d884c6SBarry Smith   int        ierr;
6094d884c6SBarry Smith 
6194d884c6SBarry Smith   PetscFunctionBegin;
6294d884c6SBarry Smith 
6394d884c6SBarry Smith   if (mat->mapping) {
6494d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->mapping);CHKERRQ(ierr);
6594d884c6SBarry Smith   }
6694d884c6SBarry Smith   if (mat->bmapping) {
6794d884c6SBarry Smith     ierr = ISLocalToGlobalMappingDestroy(mat->bmapping);CHKERRQ(ierr);
6894d884c6SBarry Smith   }
69c7fcc2eaSBarry Smith   if (mat->rmap) {
70c7fcc2eaSBarry Smith     ierr = MapDestroy(mat->rmap);CHKERRQ(ierr);
71c7fcc2eaSBarry Smith   }
72c7fcc2eaSBarry Smith   if (mat->cmap) {
73c7fcc2eaSBarry Smith     ierr = MapDestroy(mat->cmap);CHKERRQ(ierr);
74c7fcc2eaSBarry Smith   }
75b97cf49bSBarry Smith 
76aa482453SBarry Smith #if defined(PETSC_USE_LOG)
7794d884c6SBarry Smith   PLogObjectState((PetscObject)mat,"Rows=%d, Cols=%d, NZ=%d",mat->m,mat->n,a->nz);
78b97cf49bSBarry Smith #endif
79606d414cSSatish Balay   if (a->diag) {ierr = PetscFree(a->diag);CHKERRQ(ierr);}
80*c2e958feSBarry Smith   if (a->values) {ierr = PetscFree(a->values);CHKERRQ(ierr);}
81606d414cSSatish Balay   ierr = PetscFree(a->i);CHKERRQ(ierr);
82606d414cSSatish Balay   ierr = PetscFree(a->j);CHKERRQ(ierr);
83606d414cSSatish Balay   ierr = PetscFree(a->rowners);CHKERRQ(ierr);
84606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
85b97cf49bSBarry Smith 
8694d884c6SBarry Smith   PLogObjectDestroy(mat);
8794d884c6SBarry Smith   PetscHeaderDestroy(mat);
883a40ed3dSBarry Smith   PetscFunctionReturn(0);
89b97cf49bSBarry Smith }
90b97cf49bSBarry Smith 
91b97cf49bSBarry Smith 
92b97cf49bSBarry Smith #undef __FUNC__
93329f5518SBarry Smith #define __FUNC__ "MatSetOption_MPICSR"
94329f5518SBarry Smith int MatSetOption_MPICSR(Mat A,MatOption op)
95b97cf49bSBarry Smith {
96329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)A->data;
97b97cf49bSBarry Smith 
98433994e6SBarry Smith   PetscFunctionBegin;
99b97cf49bSBarry Smith   if (op == MAT_STRUCTURALLY_SYMMETRIC) {
100b97cf49bSBarry Smith     a->symmetric = PETSC_TRUE;
101b97cf49bSBarry Smith   } else {
102329f5518SBarry Smith     PLogInfo(A,"MatSetOption_MPICSR:Option ignored\n");
103b97cf49bSBarry Smith   }
1043a40ed3dSBarry Smith   PetscFunctionReturn(0);
105b97cf49bSBarry Smith }
106b97cf49bSBarry Smith 
107b97cf49bSBarry Smith 
108b97cf49bSBarry Smith /*
109b97cf49bSBarry Smith      Adds diagonal pointers to sparse matrix structure.
110b97cf49bSBarry Smith */
111b97cf49bSBarry Smith 
112b97cf49bSBarry Smith #undef __FUNC__
113329f5518SBarry Smith #define __FUNC__ "MatMarkDiagonal_MPICSR"
114329f5518SBarry Smith int MatMarkDiagonal_MPICSR(Mat A)
115b97cf49bSBarry Smith {
116329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)A->data;
117b97cf49bSBarry Smith   int        i,j,*diag,m = a->m;
118b97cf49bSBarry Smith 
119433994e6SBarry Smith   PetscFunctionBegin;
120b97cf49bSBarry Smith   diag = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(diag);
121b97cf49bSBarry Smith   PLogObjectMemory(A,(m+1)*sizeof(int));
122b97cf49bSBarry Smith   for (i=0; i<a->m; i++) {
123b97cf49bSBarry Smith     for (j=a->i[i]; j<a->i[i+1]; j++) {
124b97cf49bSBarry Smith       if (a->j[j] == i) {
125b97cf49bSBarry Smith         diag[i] = j;
126b97cf49bSBarry Smith         break;
127b97cf49bSBarry Smith       }
128b97cf49bSBarry Smith     }
129b97cf49bSBarry Smith   }
130b97cf49bSBarry Smith   a->diag = diag;
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
132b97cf49bSBarry Smith }
133b97cf49bSBarry Smith 
134b97cf49bSBarry Smith #undef __FUNC__
135329f5518SBarry Smith #define __FUNC__ "MatGetSize_MPICSR"
136329f5518SBarry Smith int MatGetSize_MPICSR(Mat A,int *m,int *n)
137b97cf49bSBarry Smith {
138433994e6SBarry Smith   PetscFunctionBegin;
1390752156aSBarry Smith   if (m) *m = A->M;
1400752156aSBarry Smith   if (n) *n = A->N;
1413a40ed3dSBarry Smith   PetscFunctionReturn(0);
142b97cf49bSBarry Smith }
143b97cf49bSBarry Smith 
144b97cf49bSBarry Smith #undef __FUNC__
145329f5518SBarry Smith #define __FUNC__ "MatGetSize_MPICSR"
146329f5518SBarry Smith int MatGetLocalSize_MPICSR(Mat A,int *m,int *n)
147b97cf49bSBarry Smith {
148329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)A->data;
149433994e6SBarry Smith   PetscFunctionBegin;
1500752156aSBarry Smith   if (m) *m = a->m;
1510752156aSBarry Smith   if (n) *n = A->N;
1523a40ed3dSBarry Smith   PetscFunctionReturn(0);
153b97cf49bSBarry Smith }
154b97cf49bSBarry Smith 
155b97cf49bSBarry Smith #undef __FUNC__
156329f5518SBarry Smith #define __FUNC__ "MatGetOwnershipRange_MPICSR"
157329f5518SBarry Smith int MatGetOwnershipRange_MPICSR(Mat A,int *m,int *n)
158b97cf49bSBarry Smith {
159329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)A->data;
160433994e6SBarry Smith   PetscFunctionBegin;
161*c2e958feSBarry Smith   if (m) *m = a->rstart;
162*c2e958feSBarry Smith   if (n) *n = a->rend;
1633a40ed3dSBarry Smith   PetscFunctionReturn(0);
164b97cf49bSBarry Smith }
165b97cf49bSBarry Smith 
166b97cf49bSBarry Smith #undef __FUNC__
167329f5518SBarry Smith #define __FUNC__ "MatGetRow_MPICSR"
168329f5518SBarry Smith int MatGetRow_MPICSR(Mat A,int row,int *nz,int **idx,Scalar **v)
169b97cf49bSBarry Smith {
170329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR*)A->data;
171b97cf49bSBarry Smith   int        *itmp;
172b97cf49bSBarry Smith 
173433994e6SBarry Smith   PetscFunctionBegin;
1740752156aSBarry Smith   row -= a->rstart;
1750752156aSBarry Smith 
176a8c6a408SBarry Smith   if (row < 0 || row >= a->m) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Row out of range");
177b97cf49bSBarry Smith 
178b97cf49bSBarry Smith   *nz = a->i[row+1] - a->i[row];
179b97cf49bSBarry Smith   if (v) *v = PETSC_NULL;
180b97cf49bSBarry Smith   if (idx) {
181b97cf49bSBarry Smith     itmp = a->j + a->i[row];
182b97cf49bSBarry Smith     if (*nz) {
183b97cf49bSBarry Smith       *idx = itmp;
184b97cf49bSBarry Smith     }
185b97cf49bSBarry Smith     else *idx = 0;
186b97cf49bSBarry Smith   }
1873a40ed3dSBarry Smith   PetscFunctionReturn(0);
188b97cf49bSBarry Smith }
189b97cf49bSBarry Smith 
190b97cf49bSBarry Smith #undef __FUNC__
191329f5518SBarry Smith #define __FUNC__ "MatRestoreRow_MPICSR"
192329f5518SBarry Smith int MatRestoreRow_MPICSR(Mat A,int row,int *nz,int **idx,Scalar **v)
193b97cf49bSBarry Smith {
194433994e6SBarry Smith   PetscFunctionBegin;
1953a40ed3dSBarry Smith   PetscFunctionReturn(0);
196b97cf49bSBarry Smith }
197b97cf49bSBarry Smith 
198b97cf49bSBarry Smith #undef __FUNC__
199329f5518SBarry Smith #define __FUNC__ "MatGetBlockSize_MPICSR"
200329f5518SBarry Smith int MatGetBlockSize_MPICSR(Mat A,int *bs)
201b97cf49bSBarry Smith {
202433994e6SBarry Smith   PetscFunctionBegin;
203b97cf49bSBarry Smith   *bs = 1;
2043a40ed3dSBarry Smith   PetscFunctionReturn(0);
205b97cf49bSBarry Smith }
206b97cf49bSBarry Smith 
207b97cf49bSBarry Smith 
208b97cf49bSBarry Smith #undef __FUNC__
209329f5518SBarry Smith #define __FUNC__ "MatEqual_MPICSR"
210329f5518SBarry Smith int MatEqual_MPICSR(Mat A,Mat B,PetscTruth* flg)
211b97cf49bSBarry Smith {
212329f5518SBarry Smith   Mat_MPICSR *a = (Mat_MPICSR *)A->data,*b = (Mat_MPICSR *)B->data;
2130f5bd95cSBarry Smith   int         ierr;
2140f5bd95cSBarry Smith   PetscTruth  flag;
215b97cf49bSBarry Smith 
216433994e6SBarry Smith   PetscFunctionBegin;
217329f5518SBarry Smith   if (B->type != MATMPICSR) SETERRQ(PETSC_ERR_ARG_INCOMP,0,"Matrices must be same type");
218b97cf49bSBarry Smith 
219b97cf49bSBarry Smith   /* If the  matrix dimensions are not equal,or no of nonzeros */
2200752156aSBarry Smith   if ((a->m != b->m) ||(a->nz != b->nz)) {
2210f5bd95cSBarry Smith     flag = PETSC_FALSE;
222b97cf49bSBarry Smith   }
223b97cf49bSBarry Smith 
224b97cf49bSBarry Smith   /* if the a->i are the same */
2250f5bd95cSBarry Smith   ierr = PetscMemcmp(a->i,b->i,(a->m+1)*sizeof(int),&flag);CHKERRQ(ierr);
226b97cf49bSBarry Smith 
227b97cf49bSBarry Smith   /* if a->j are the same */
2280f5bd95cSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(int),&flag);CHKERRQ(ierr);
229b97cf49bSBarry Smith 
230ca161407SBarry Smith   ierr = MPI_Allreduce(&flag,flg,1,MPI_INT,MPI_LAND,A->comm);CHKERRQ(ierr);
2313a40ed3dSBarry Smith   PetscFunctionReturn(0);
232b97cf49bSBarry Smith }
233b97cf49bSBarry Smith 
234b97cf49bSBarry Smith 
235b97cf49bSBarry Smith /* -------------------------------------------------------------------*/
2360b3b1487SBarry Smith static struct _MatOps MatOps_Values = {0,
237329f5518SBarry Smith        MatGetRow_MPICSR,
238329f5518SBarry Smith        MatRestoreRow_MPICSR,
239b97cf49bSBarry Smith        0,
240b97cf49bSBarry Smith        0,
241b97cf49bSBarry Smith        0,
242b97cf49bSBarry Smith        0,
2430b3b1487SBarry Smith        0,
2440b3b1487SBarry Smith        0,
2450b3b1487SBarry Smith        0,
2460b3b1487SBarry Smith        0,
2470b3b1487SBarry Smith        0,
2480b3b1487SBarry Smith        0,
2490b3b1487SBarry Smith        0,
2500b3b1487SBarry Smith        0,
2510b3b1487SBarry Smith        0,
252329f5518SBarry Smith        MatEqual_MPICSR,
2530b3b1487SBarry Smith        0,
2540b3b1487SBarry Smith        0,
2550b3b1487SBarry Smith        0,
2560b3b1487SBarry Smith        0,
2570b3b1487SBarry Smith        0,
2580b3b1487SBarry Smith        0,
259329f5518SBarry Smith        MatSetOption_MPICSR,
2600b3b1487SBarry Smith        0,
2610b3b1487SBarry Smith        0,
2620b3b1487SBarry Smith        0,
2630b3b1487SBarry Smith        0,
2640b3b1487SBarry Smith        0,
2650b3b1487SBarry Smith        0,
266329f5518SBarry Smith        MatGetSize_MPICSR,
267329f5518SBarry Smith        MatGetLocalSize_MPICSR,
268329f5518SBarry Smith        MatGetOwnershipRange_MPICSR,
2690b3b1487SBarry Smith        0,
2700b3b1487SBarry Smith        0,
2710b3b1487SBarry Smith        0,
2720b3b1487SBarry Smith        0,
2730b3b1487SBarry Smith        0,
2740b3b1487SBarry Smith        0,
2750b3b1487SBarry Smith        0,
2760b3b1487SBarry Smith        0,
2770b3b1487SBarry Smith        0,
2780b3b1487SBarry Smith        0,
2790b3b1487SBarry Smith        0,
2800b3b1487SBarry Smith        0,
2810b3b1487SBarry Smith        0,
2820b3b1487SBarry Smith        0,
2830b3b1487SBarry Smith        0,
2840b3b1487SBarry Smith        0,
2850b3b1487SBarry Smith        0,
2860b3b1487SBarry Smith        0,
287b97cf49bSBarry Smith        0,
288329f5518SBarry Smith        MatGetBlockSize_MPICSR,
289b97cf49bSBarry Smith        0,
290b97cf49bSBarry Smith        0,
291b97cf49bSBarry Smith        0,
292b97cf49bSBarry Smith        0,
293b97cf49bSBarry Smith        0,
2940752156aSBarry Smith        0,
2950752156aSBarry Smith        0,
2960b3b1487SBarry Smith        0,
2970b3b1487SBarry Smith        0,
2980b3b1487SBarry Smith        0,
2990b3b1487SBarry Smith        0,
3000b3b1487SBarry Smith        0,
3010b3b1487SBarry Smith        MatGetMaps_Petsc};
302b97cf49bSBarry Smith 
303b97cf49bSBarry Smith 
304b97cf49bSBarry Smith #undef __FUNC__
305329f5518SBarry Smith #define __FUNC__ "MatCreateMPICSR"
306b97cf49bSBarry Smith /*@C
307329f5518SBarry Smith    MatCreateMPICSR - Creates a sparse matrix representing an adjacency list.
308b97cf49bSBarry Smith    The matrix does not have numerical values associated with it, but is
309b97cf49bSBarry Smith    intended for ordering (to reduce bandwidth etc) and partitioning.
310b97cf49bSBarry Smith 
311ef5ee4d1SLois Curfman McInnes    Collective on MPI_Comm
312ef5ee4d1SLois Curfman McInnes 
313b97cf49bSBarry Smith    Input Parameters:
314*c2e958feSBarry Smith +  comm - MPI communicator
3150752156aSBarry Smith .  m - number of local rows
316b97cf49bSBarry Smith .  n - number of columns
317b97cf49bSBarry Smith .  i - the indices into j for the start of each row
318329f5518SBarry Smith .  j - the column indices for each row (sorted for each row).
319ef5ee4d1SLois Curfman McInnes        The indices in i and j start with zero (NOT with one).
320329f5518SBarry Smith -  values -[optional] edge weights
321b97cf49bSBarry Smith 
322b97cf49bSBarry Smith    Output Parameter:
323b97cf49bSBarry Smith .  A - the matrix
324b97cf49bSBarry Smith 
32515091d37SBarry Smith    Level: intermediate
32615091d37SBarry Smith 
3274bc6d8c0SBarry Smith    Notes: This matrix object does not support most matrix operations, include
3284bc6d8c0SBarry Smith    MatSetValues().
329*c2e958feSBarry Smith    You must NOT free the ii, values and jj arrays yourself. PETSc will free them
330*c2e958feSBarry Smith    when the matrix is destroyed. And you must allocate them with PetscMalloc()
331b97cf49bSBarry Smith 
332ef5ee4d1SLois Curfman McInnes    Possible values for MatSetOption() - MAT_STRUCTURALLY_SYMMETRIC
333b97cf49bSBarry Smith 
33491e9ee9fSBarry Smith .seealso: MatCreate(), MatCreateSeqAdj(), MatGetOrdering()
335b97cf49bSBarry Smith @*/
336*c2e958feSBarry Smith int MatCreateMPICSR(MPI_Comm comm,int m,int n,int *i,int *j,int *values,Mat *A)
337b97cf49bSBarry Smith {
338b97cf49bSBarry Smith   Mat        B;
339329f5518SBarry Smith   Mat_MPICSR *b;
340f1af5d2fSBarry Smith   int        ii,ierr,size,rank;
341f1af5d2fSBarry Smith   PetscTruth flg;
342b97cf49bSBarry Smith 
343433994e6SBarry Smith   PetscFunctionBegin;
344d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
345d132466eSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
346b97cf49bSBarry Smith 
347b97cf49bSBarry Smith   *A                  = 0;
348329f5518SBarry Smith   PetscHeaderCreate(B,_p_Mat,struct _MatOps,MAT_COOKIE,MATMPICSR,"Mat",comm,MatDestroy,MatView);
349b97cf49bSBarry Smith   PLogObjectCreate(B);
350329f5518SBarry Smith   B->data             = (void*)(b = PetscNew(Mat_MPICSR));CHKPTRQ(b);
351329f5518SBarry Smith   ierr                = PetscMemzero(b,sizeof(Mat_MPICSR));CHKERRQ(ierr);
352549d3d68SSatish Balay   ierr                = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
353329f5518SBarry Smith   B->ops->destroy     = MatDestroy_MPICSR;
354329f5518SBarry Smith   B->ops->view        = MatView_MPICSR;
355b97cf49bSBarry Smith   B->factor           = 0;
356b97cf49bSBarry Smith   B->lupivotthreshold = 1.0;
357b97cf49bSBarry Smith   B->mapping          = 0;
358b97cf49bSBarry Smith   B->assembled        = PETSC_FALSE;
359b97cf49bSBarry Smith 
3600752156aSBarry Smith   b->m = m; B->m = m;
361ca161407SBarry Smith   ierr = MPI_Allreduce(&m,&B->M,1,MPI_INT,MPI_SUM,comm);CHKERRQ(ierr);
3620752156aSBarry Smith   B->n = n; B->N = n;
3630752156aSBarry Smith 
364c7fcc2eaSBarry Smith   /* the information in the maps duplicates the information computed below, eventually
365c7fcc2eaSBarry Smith      we should remove the duplicate information that is not contained in the maps */
366488ecbafSBarry Smith   ierr = MapCreateMPI(comm,m,B->M,&B->rmap);CHKERRQ(ierr);
367c7fcc2eaSBarry Smith   /* we don't know the "local columns" so just use the row information :-(*/
368488ecbafSBarry Smith   ierr = MapCreateMPI(comm,m,B->M,&B->cmap);CHKERRQ(ierr);
369c7fcc2eaSBarry Smith 
3700752156aSBarry Smith   b->rowners = (int*)PetscMalloc((size+1)*sizeof(int));CHKPTRQ(b->rowners);
371329f5518SBarry Smith   PLogObjectMemory(B,(size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPICSR));
372ca161407SBarry Smith   ierr = MPI_Allgather(&m,1,MPI_INT,b->rowners+1,1,MPI_INT,comm);CHKERRQ(ierr);
3730752156aSBarry Smith   b->rowners[0] = 0;
3740752156aSBarry Smith   for (ii=2; ii<=size; ii++) {
3750752156aSBarry Smith     b->rowners[ii] += b->rowners[ii-1];
3760752156aSBarry Smith   }
3770752156aSBarry Smith   b->rstart = b->rowners[rank];
3780752156aSBarry Smith   b->rend   = b->rowners[rank+1];
379b97cf49bSBarry Smith 
380*c2e958feSBarry Smith #if defined(PETSC_BOPT_g)
381*c2e958feSBarry Smith   if (i[0] != 0) SETERR1(1,1,"First i[] index must be zero, instead it is %d\n",i[0]);
382*c2e958feSBarry Smith   for (ii=1; ii<m; ii++) {
383*c2e958feSBarry Smith     if (i[ii] < 0 || i[ii] > i[ii-1]) {
384*c2e958feSBarry Smith       SETERRQ4(1,1,"i[%d] index is out of range: i[%d]",ii,i[ii],ii-1,i[ii-1]);
385*c2e958feSBarry Smith     }
386*c2e958feSBarry Smith   }
387*c2e958feSBarry Smith   for (ii=0; ii<i[m]; ii++) {
388*c2e958feSBarry Smith     if (i[ii] < 0 || i[ii] >= N) {
389*c2e958feSBarry Smith       SETERRQ2(1,1,"Column index %d out of range %d\n",ii,i[ii]);
390*c2e958feSBarry Smith     }
391*c2e958feSBarry Smith   }
392*c2e958feSBarry Smith #endif
393*c2e958feSBarry Smith 
394b97cf49bSBarry Smith   b->j  = j;
395b97cf49bSBarry Smith   b->i  = i;
396329f5518SBarry Smith   b->values = values;
397b97cf49bSBarry Smith 
398b97cf49bSBarry Smith   b->nz               = i[m];
399b97cf49bSBarry Smith   b->diag             = 0;
400b97cf49bSBarry Smith   b->symmetric        = PETSC_FALSE;
401b97cf49bSBarry Smith 
402b97cf49bSBarry Smith   *A = B;
403b97cf49bSBarry Smith 
404b97cf49bSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
405b97cf49bSBarry Smith   if (flg) {ierr = MatPrintHelp(B);CHKERRQ(ierr); }
406b97cf49bSBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
407b97cf49bSBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4083a40ed3dSBarry Smith   PetscFunctionReturn(0);
409b97cf49bSBarry Smith }
410b97cf49bSBarry Smith 
411b97cf49bSBarry Smith 
412b97cf49bSBarry Smith 
413*c2e958feSBarry Smith #undef __FUNC__
414*c2e958feSBarry Smith #define __FUNC__ "MatConvert_MPICSR"
415*c2e958feSBarry Smith int MatConvert_MPICSR(Mat A,MatType type,Mat *B)
416*c2e958feSBarry Smith {
417*c2e958feSBarry Smith   int      i,ierr,m,n,M,N,nzeros = 0,*ia,*ja,*rj,len,rstart,cnt,j,*a;
418*c2e958feSBarry Smith   Scalar   *ra;
419*c2e958feSBarry Smith   MPI_Comm comm;
420*c2e958feSBarry Smith 
421*c2e958feSBarry Smith   PetscFunctionBegin;
422*c2e958feSBarry Smith   ierr = MatGetSize(A,PETSC_NULL,&N);CHKERRQ(ierr);
423*c2e958feSBarry Smith   ierr = MatGetLocalSize(A,&m,PETSC_NULL);CHKERRQ(ierr);
424*c2e958feSBarry Smith   ierr = MatGetOwnershipRange(A,&rstart,PETSC_NULL);CHKERRQ(ierr);
425*c2e958feSBarry Smith 
426*c2e958feSBarry Smith   /* count the number of nonzeros per row */
427*c2e958feSBarry Smith   for (i=0; i<m; i++) {
428*c2e958feSBarry Smith     ierr   = MatGetRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr);
429*c2e958feSBarry Smith     for (j=0; j<len; j++) {
430*c2e958feSBarry Smith       if (rj[j] == i+rstart) {len--; break;}    /* don't count diagonal */
431*c2e958feSBarry Smith     }
432*c2e958feSBarry Smith     ierr   = MatRestoreRow(A,i+rstart,&len,&rj,PETSC_NULL);CHKERRQ(ierr);
433*c2e958feSBarry Smith     nzeros += len;
434*c2e958feSBarry Smith   }
435*c2e958feSBarry Smith 
436*c2e958feSBarry Smith   /* malloc space for nonzeros */
437*c2e958feSBarry Smith   a  = (int*)PetscMalloc((nzeros+1)*sizeof(int));CHKPTRQ(a);
438*c2e958feSBarry Smith   ia = (int*)PetscMalloc((N+1)*sizeof(int));CHKPTRQ(ia);
439*c2e958feSBarry Smith   ja = (int*)PetscMalloc((nzeros+1)*sizeof(int));CHKPTRQ(ja);
440*c2e958feSBarry Smith 
441*c2e958feSBarry Smith   nzeros = 0;
442*c2e958feSBarry Smith   ia[0]  = 0;
443*c2e958feSBarry Smith   for (i=0; i<m; i++) {
444*c2e958feSBarry Smith     ierr    = MatGetRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr);
445*c2e958feSBarry Smith     cnt     = 0;
446*c2e958feSBarry Smith     for (j=0; j<len; j++) {
447*c2e958feSBarry Smith       if (rj[j] != i+rstart) { /* if not diagonal */
448*c2e958feSBarry Smith         a[nzeros+cnt]    = (int) ra[j];
449*c2e958feSBarry Smith         ja[nzeros+cnt++] = rj[j];
450*c2e958feSBarry Smith       }
451*c2e958feSBarry Smith     }
452*c2e958feSBarry Smith     ierr    = MatRestoreRow(A,i+rstart,&len,&rj,&ra);CHKERRQ(ierr);
453*c2e958feSBarry Smith     nzeros += cnt;
454*c2e958feSBarry Smith     ia[i+1] = nzeros;
455*c2e958feSBarry Smith   }
456*c2e958feSBarry Smith 
457*c2e958feSBarry Smith   ierr = PetscObjectGetComm((PetscObject)A,&comm);CHKERRQ(ierr);
458*c2e958feSBarry Smith   ierr = MatCreateMPICSR(comm,m,N,ia,ja,a,B);CHKERRQ(ierr);
459*c2e958feSBarry Smith 
460*c2e958feSBarry Smith   PetscFunctionReturn(0);
461*c2e958feSBarry Smith }
462433994e6SBarry Smith 
463433994e6SBarry Smith 
464433994e6SBarry Smith 
465433994e6SBarry Smith 
466433994e6SBarry Smith 
467433994e6SBarry Smith 
468