Actual source code: mpibaij.c
petsc-3.11.4 2019-09-28
2: #include <../src/mat/impls/baij/mpi/mpibaij.h>
4: #include <petscblaslapack.h>
5: #include <petscsf.h>
7: #if defined(PETSC_HAVE_HYPRE)
8: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat,MatType,MatReuse,Mat*);
9: #endif
11: PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
12: {
13: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
15: PetscInt i,*idxb = 0;
16: PetscScalar *va,*vb;
17: Vec vtmp;
20: MatGetRowMaxAbs(a->A,v,idx);
21: VecGetArray(v,&va);
22: if (idx) {
23: for (i=0; i<A->rmap->n; i++) {
24: if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
25: }
26: }
28: VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
29: if (idx) {PetscMalloc1(A->rmap->n,&idxb);}
30: MatGetRowMaxAbs(a->B,vtmp,idxb);
31: VecGetArray(vtmp,&vb);
33: for (i=0; i<A->rmap->n; i++) {
34: if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
35: va[i] = vb[i];
36: if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);
37: }
38: }
40: VecRestoreArray(v,&va);
41: VecRestoreArray(vtmp,&vb);
42: PetscFree(idxb);
43: VecDestroy(&vtmp);
44: return(0);
45: }
47: PetscErrorCode MatStoreValues_MPIBAIJ(Mat mat)
48: {
49: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)mat->data;
53: MatStoreValues(aij->A);
54: MatStoreValues(aij->B);
55: return(0);
56: }
58: PetscErrorCode MatRetrieveValues_MPIBAIJ(Mat mat)
59: {
60: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)mat->data;
64: MatRetrieveValues(aij->A);
65: MatRetrieveValues(aij->B);
66: return(0);
67: }
69: /*
70: Local utility routine that creates a mapping from the global column
71: number to the local number in the off-diagonal part of the local
72: storage of the matrix. This is done in a non scalable way since the
73: length of colmap equals the global matrix length.
74: */
75: PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
76: {
77: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
78: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data;
80: PetscInt nbs = B->nbs,i,bs=mat->rmap->bs;
83: #if defined(PETSC_USE_CTABLE)
84: PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);
85: for (i=0; i<nbs; i++) {
86: PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);
87: }
88: #else
89: PetscMalloc1(baij->Nbs+1,&baij->colmap);
90: PetscLogObjectMemory((PetscObject)mat,baij->Nbs*sizeof(PetscInt));
91: PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));
92: for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
93: #endif
94: return(0);
95: }
97: #define MatSetValues_SeqBAIJ_A_Private(row,col,value,addv,orow,ocol) \
98: { \
99: \
100: brow = row/bs; \
101: rp = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
102: rmax = aimax[brow]; nrow = ailen[brow]; \
103: bcol = col/bs; \
104: ridx = row % bs; cidx = col % bs; \
105: low = 0; high = nrow; \
106: while (high-low > 3) { \
107: t = (low+high)/2; \
108: if (rp[t] > bcol) high = t; \
109: else low = t; \
110: } \
111: for (_i=low; _i<high; _i++) { \
112: if (rp[_i] > bcol) break; \
113: if (rp[_i] == bcol) { \
114: bap = ap + bs2*_i + bs*cidx + ridx; \
115: if (addv == ADD_VALUES) *bap += value; \
116: else *bap = value; \
117: goto a_noinsert; \
118: } \
119: } \
120: if (a->nonew == 1) goto a_noinsert; \
121: if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
122: MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
123: N = nrow++ - 1; \
124: /* shift up all the later entries in this row */ \
125: for (ii=N; ii>=_i; ii--) { \
126: rp[ii+1] = rp[ii]; \
127: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
128: } \
129: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); } \
130: rp[_i] = bcol; \
131: ap[bs2*_i + bs*cidx + ridx] = value; \
132: a_noinsert:; \
133: ailen[brow] = nrow; \
134: }
136: #define MatSetValues_SeqBAIJ_B_Private(row,col,value,addv,orow,ocol) \
137: { \
138: brow = row/bs; \
139: rp = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
140: rmax = bimax[brow]; nrow = bilen[brow]; \
141: bcol = col/bs; \
142: ridx = row % bs; cidx = col % bs; \
143: low = 0; high = nrow; \
144: while (high-low > 3) { \
145: t = (low+high)/2; \
146: if (rp[t] > bcol) high = t; \
147: else low = t; \
148: } \
149: for (_i=low; _i<high; _i++) { \
150: if (rp[_i] > bcol) break; \
151: if (rp[_i] == bcol) { \
152: bap = ap + bs2*_i + bs*cidx + ridx; \
153: if (addv == ADD_VALUES) *bap += value; \
154: else *bap = value; \
155: goto b_noinsert; \
156: } \
157: } \
158: if (b->nonew == 1) goto b_noinsert; \
159: if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at global row/column (%D, %D) into matrix", orow, ocol); \
160: MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
161: N = nrow++ - 1; \
162: /* shift up all the later entries in this row */ \
163: for (ii=N; ii>=_i; ii--) { \
164: rp[ii+1] = rp[ii]; \
165: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
166: } \
167: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));} \
168: rp[_i] = bcol; \
169: ap[bs2*_i + bs*cidx + ridx] = value; \
170: b_noinsert:; \
171: bilen[brow] = nrow; \
172: }
174: PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
175: {
176: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
177: MatScalar value;
178: PetscBool roworiented = baij->roworiented;
180: PetscInt i,j,row,col;
181: PetscInt rstart_orig=mat->rmap->rstart;
182: PetscInt rend_orig =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
183: PetscInt cend_orig =mat->cmap->rend,bs=mat->rmap->bs;
185: /* Some Variables required in the macro */
186: Mat A = baij->A;
187: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)(A)->data;
188: PetscInt *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
189: MatScalar *aa =a->a;
191: Mat B = baij->B;
192: Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(B)->data;
193: PetscInt *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
194: MatScalar *ba =b->a;
196: PetscInt *rp,ii,nrow,_i,rmax,N,brow,bcol;
197: PetscInt low,high,t,ridx,cidx,bs2=a->bs2;
198: MatScalar *ap,*bap;
201: for (i=0; i<m; i++) {
202: if (im[i] < 0) continue;
203: #if defined(PETSC_USE_DEBUG)
204: 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);
205: #endif
206: if (im[i] >= rstart_orig && im[i] < rend_orig) {
207: row = im[i] - rstart_orig;
208: for (j=0; j<n; j++) {
209: if (in[j] >= cstart_orig && in[j] < cend_orig) {
210: col = in[j] - cstart_orig;
211: if (roworiented) value = v[i*n+j];
212: else value = v[i+j*m];
213: MatSetValues_SeqBAIJ_A_Private(row,col,value,addv,im[i],in[j]);
214: /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
215: } else if (in[j] < 0) continue;
216: #if defined(PETSC_USE_DEBUG)
217: 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);
218: #endif
219: else {
220: if (mat->was_assembled) {
221: if (!baij->colmap) {
222: MatCreateColmap_MPIBAIJ_Private(mat);
223: }
224: #if defined(PETSC_USE_CTABLE)
225: PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
226: col = col - 1;
227: #else
228: col = baij->colmap[in[j]/bs] - 1;
229: #endif
230: if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
231: MatDisAssemble_MPIBAIJ(mat);
232: col = in[j];
233: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
234: B = baij->B;
235: b = (Mat_SeqBAIJ*)(B)->data;
236: bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
237: ba =b->a;
238: } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
239: else col += in[j]%bs;
240: } else col = in[j];
241: if (roworiented) value = v[i*n+j];
242: else value = v[i+j*m];
243: MatSetValues_SeqBAIJ_B_Private(row,col,value,addv,im[i],in[j]);
244: /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
245: }
246: }
247: } else {
248: 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]);
249: if (!baij->donotstash) {
250: mat->assembled = PETSC_FALSE;
251: if (roworiented) {
252: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
253: } else {
254: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
255: }
256: }
257: }
258: }
259: return(0);
260: }
262: PETSC_STATIC_INLINE PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A,PetscInt row,PetscInt col,const PetscScalar v[],InsertMode is,PetscInt orow,PetscInt ocol)
263: {
264: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data;
265: PetscInt *rp,low,high,t,ii,jj,nrow,i,rmax,N;
266: PetscInt *imax=a->imax,*ai=a->i,*ailen=a->ilen;
267: PetscErrorCode ierr;
268: PetscInt *aj =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs;
269: PetscBool roworiented=a->roworiented;
270: const PetscScalar *value = v;
271: MatScalar *ap,*aa = a->a,*bap;
274: rp = aj + ai[row];
275: ap = aa + bs2*ai[row];
276: rmax = imax[row];
277: nrow = ailen[row];
278: value = v;
279: low = 0;
280: high = nrow;
281: while (high-low > 7) {
282: t = (low+high)/2;
283: if (rp[t] > col) high = t;
284: else low = t;
285: }
286: for (i=low; i<high; i++) {
287: if (rp[i] > col) break;
288: if (rp[i] == col) {
289: bap = ap + bs2*i;
290: if (roworiented) {
291: if (is == ADD_VALUES) {
292: for (ii=0; ii<bs; ii++) {
293: for (jj=ii; jj<bs2; jj+=bs) {
294: bap[jj] += *value++;
295: }
296: }
297: } else {
298: for (ii=0; ii<bs; ii++) {
299: for (jj=ii; jj<bs2; jj+=bs) {
300: bap[jj] = *value++;
301: }
302: }
303: }
304: } else {
305: if (is == ADD_VALUES) {
306: for (ii=0; ii<bs; ii++,value+=bs) {
307: for (jj=0; jj<bs; jj++) {
308: bap[jj] += value[jj];
309: }
310: bap += bs;
311: }
312: } else {
313: for (ii=0; ii<bs; ii++,value+=bs) {
314: for (jj=0; jj<bs; jj++) {
315: bap[jj] = value[jj];
316: }
317: bap += bs;
318: }
319: }
320: }
321: goto noinsert2;
322: }
323: }
324: if (nonew == 1) goto noinsert2;
325: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new global block indexed nonzero block (%D, %D) in the matrix", orow, ocol);
326: MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
327: N = nrow++ - 1; high++;
328: /* shift up all the later entries in this row */
329: for (ii=N; ii>=i; ii--) {
330: rp[ii+1] = rp[ii];
331: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
332: }
333: if (N >= i) {
334: PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
335: }
336: rp[i] = col;
337: bap = ap + bs2*i;
338: if (roworiented) {
339: for (ii=0; ii<bs; ii++) {
340: for (jj=ii; jj<bs2; jj+=bs) {
341: bap[jj] = *value++;
342: }
343: }
344: } else {
345: for (ii=0; ii<bs; ii++) {
346: for (jj=0; jj<bs; jj++) {
347: *bap++ = *value++;
348: }
349: }
350: }
351: noinsert2:;
352: ailen[row] = nrow;
353: return(0);
354: }
356: /*
357: This routine should be optimized so that the block copy at ** Here a copy is required ** below is not needed
358: by passing additional stride information into the MatSetValuesBlocked_SeqBAIJ_Inlined() routine
359: */
360: PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
361: {
362: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
363: const PetscScalar *value;
364: MatScalar *barray = baij->barray;
365: PetscBool roworiented = baij->roworiented;
366: PetscErrorCode ierr;
367: PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs;
368: PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval;
369: PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
372: if (!barray) {
373: PetscMalloc1(bs2,&barray);
374: baij->barray = barray;
375: }
377: if (roworiented) stepval = (n-1)*bs;
378: else stepval = (m-1)*bs;
380: for (i=0; i<m; i++) {
381: if (im[i] < 0) continue;
382: #if defined(PETSC_USE_DEBUG)
383: if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block indexed row too large %D max %D",im[i],baij->Mbs-1);
384: #endif
385: if (im[i] >= rstart && im[i] < rend) {
386: row = im[i] - rstart;
387: for (j=0; j<n; j++) {
388: /* If NumCol = 1 then a copy is not required */
389: if ((roworiented) && (n == 1)) {
390: barray = (MatScalar*)v + i*bs2;
391: } else if ((!roworiented) && (m == 1)) {
392: barray = (MatScalar*)v + j*bs2;
393: } else { /* Here a copy is required */
394: if (roworiented) {
395: value = v + (i*(stepval+bs) + j)*bs;
396: } else {
397: value = v + (j*(stepval+bs) + i)*bs;
398: }
399: for (ii=0; ii<bs; ii++,value+=bs+stepval) {
400: for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
401: barray += bs;
402: }
403: barray -= bs2;
404: }
406: if (in[j] >= cstart && in[j] < cend) {
407: col = in[j] - cstart;
408: MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
409: } else if (in[j] < 0) continue;
410: #if defined(PETSC_USE_DEBUG)
411: else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Block indexed column too large %D max %D",in[j],baij->Nbs-1);
412: #endif
413: else {
414: if (mat->was_assembled) {
415: if (!baij->colmap) {
416: MatCreateColmap_MPIBAIJ_Private(mat);
417: }
419: #if defined(PETSC_USE_DEBUG)
420: #if defined(PETSC_USE_CTABLE)
421: { PetscInt data;
422: PetscTableFind(baij->colmap,in[j]+1,&data);
423: if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
424: }
425: #else
426: if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
427: #endif
428: #endif
429: #if defined(PETSC_USE_CTABLE)
430: PetscTableFind(baij->colmap,in[j]+1,&col);
431: col = (col - 1)/bs;
432: #else
433: col = (baij->colmap[in[j]] - 1)/bs;
434: #endif
435: if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
436: MatDisAssemble_MPIBAIJ(mat);
437: col = in[j];
438: } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new blocked indexed nonzero block (%D, %D) into matrix",im[i],in[j]);
439: } else col = in[j];
440: MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
441: }
442: }
443: } else {
444: if (mat->nooffprocentries) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Setting off process block indexed row %D even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set",im[i]);
445: if (!baij->donotstash) {
446: if (roworiented) {
447: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
448: } else {
449: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
450: }
451: }
452: }
453: }
454: return(0);
455: }
457: #define HASH_KEY 0.6180339887
458: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
459: /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
460: /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
461: PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
462: {
463: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
464: PetscBool roworiented = baij->roworiented;
466: PetscInt i,j,row,col;
467: PetscInt rstart_orig=mat->rmap->rstart;
468: PetscInt rend_orig =mat->rmap->rend,Nbs=baij->Nbs;
469: PetscInt h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
470: PetscReal tmp;
471: MatScalar **HD = baij->hd,value;
472: #if defined(PETSC_USE_DEBUG)
473: PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
474: #endif
477: for (i=0; i<m; i++) {
478: #if defined(PETSC_USE_DEBUG)
479: if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
480: 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);
481: #endif
482: row = im[i];
483: if (row >= rstart_orig && row < rend_orig) {
484: for (j=0; j<n; j++) {
485: col = in[j];
486: if (roworiented) value = v[i*n+j];
487: else value = v[i+j*m];
488: /* Look up PetscInto the Hash Table */
489: key = (row/bs)*Nbs+(col/bs)+1;
490: h1 = HASH(size,key,tmp);
493: idx = h1;
494: #if defined(PETSC_USE_DEBUG)
495: insert_ct++;
496: total_ct++;
497: if (HT[idx] != key) {
498: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
499: if (idx == size) {
500: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
501: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
502: }
503: }
504: #else
505: if (HT[idx] != key) {
506: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
507: if (idx == size) {
508: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
509: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
510: }
511: }
512: #endif
513: /* A HASH table entry is found, so insert the values at the correct address */
514: if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
515: else *(HD[idx]+ (col % bs)*bs + (row % bs)) = value;
516: }
517: } else if (!baij->donotstash) {
518: if (roworiented) {
519: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
520: } else {
521: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
522: }
523: }
524: }
525: #if defined(PETSC_USE_DEBUG)
526: baij->ht_total_ct += total_ct;
527: baij->ht_insert_ct += insert_ct;
528: #endif
529: return(0);
530: }
532: PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
533: {
534: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
535: PetscBool roworiented = baij->roworiented;
536: PetscErrorCode ierr;
537: PetscInt i,j,ii,jj,row,col;
538: PetscInt rstart=baij->rstartbs;
539: PetscInt rend =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
540: PetscInt h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
541: PetscReal tmp;
542: MatScalar **HD = baij->hd,*baij_a;
543: const PetscScalar *v_t,*value;
544: #if defined(PETSC_USE_DEBUG)
545: PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
546: #endif
549: if (roworiented) stepval = (n-1)*bs;
550: else stepval = (m-1)*bs;
552: for (i=0; i<m; i++) {
553: #if defined(PETSC_USE_DEBUG)
554: if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
555: if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],baij->Mbs-1);
556: #endif
557: row = im[i];
558: v_t = v + i*nbs2;
559: if (row >= rstart && row < rend) {
560: for (j=0; j<n; j++) {
561: col = in[j];
563: /* Look up into the Hash Table */
564: key = row*Nbs+col+1;
565: h1 = HASH(size,key,tmp);
567: idx = h1;
568: #if defined(PETSC_USE_DEBUG)
569: total_ct++;
570: insert_ct++;
571: if (HT[idx] != key) {
572: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
573: if (idx == size) {
574: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
575: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
576: }
577: }
578: #else
579: if (HT[idx] != key) {
580: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
581: if (idx == size) {
582: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
583: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
584: }
585: }
586: #endif
587: baij_a = HD[idx];
588: if (roworiented) {
589: /*value = v + i*(stepval+bs)*bs + j*bs;*/
590: /* value = v + (i*(stepval+bs)+j)*bs; */
591: value = v_t;
592: v_t += bs;
593: if (addv == ADD_VALUES) {
594: for (ii=0; ii<bs; ii++,value+=stepval) {
595: for (jj=ii; jj<bs2; jj+=bs) {
596: baij_a[jj] += *value++;
597: }
598: }
599: } else {
600: for (ii=0; ii<bs; ii++,value+=stepval) {
601: for (jj=ii; jj<bs2; jj+=bs) {
602: baij_a[jj] = *value++;
603: }
604: }
605: }
606: } else {
607: value = v + j*(stepval+bs)*bs + i*bs;
608: if (addv == ADD_VALUES) {
609: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
610: for (jj=0; jj<bs; jj++) {
611: baij_a[jj] += *value++;
612: }
613: }
614: } else {
615: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
616: for (jj=0; jj<bs; jj++) {
617: baij_a[jj] = *value++;
618: }
619: }
620: }
621: }
622: }
623: } else {
624: if (!baij->donotstash) {
625: if (roworiented) {
626: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
627: } else {
628: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
629: }
630: }
631: }
632: }
633: #if defined(PETSC_USE_DEBUG)
634: baij->ht_total_ct += total_ct;
635: baij->ht_insert_ct += insert_ct;
636: #endif
637: return(0);
638: }
640: PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
641: {
642: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
644: PetscInt bs = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
645: PetscInt bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
648: for (i=0; i<m; i++) {
649: if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
650: 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);
651: if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
652: row = idxm[i] - bsrstart;
653: for (j=0; j<n; j++) {
654: if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
655: 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);
656: if (idxn[j] >= bscstart && idxn[j] < bscend) {
657: col = idxn[j] - bscstart;
658: MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
659: } else {
660: if (!baij->colmap) {
661: MatCreateColmap_MPIBAIJ_Private(mat);
662: }
663: #if defined(PETSC_USE_CTABLE)
664: PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
665: data--;
666: #else
667: data = baij->colmap[idxn[j]/bs]-1;
668: #endif
669: if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
670: else {
671: col = data + idxn[j]%bs;
672: MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
673: }
674: }
675: }
676: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
677: }
678: return(0);
679: }
681: PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
682: {
683: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
684: Mat_SeqBAIJ *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
686: PetscInt i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
687: PetscReal sum = 0.0;
688: MatScalar *v;
691: if (baij->size == 1) {
692: MatNorm(baij->A,type,nrm);
693: } else {
694: if (type == NORM_FROBENIUS) {
695: v = amat->a;
696: nz = amat->nz*bs2;
697: for (i=0; i<nz; i++) {
698: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
699: }
700: v = bmat->a;
701: nz = bmat->nz*bs2;
702: for (i=0; i<nz; i++) {
703: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
704: }
705: MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
706: *nrm = PetscSqrtReal(*nrm);
707: } else if (type == NORM_1) { /* max column sum */
708: PetscReal *tmp,*tmp2;
709: PetscInt *jj,*garray=baij->garray,cstart=baij->rstartbs;
710: PetscMalloc2(mat->cmap->N,&tmp,mat->cmap->N,&tmp2);
711: PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));
712: v = amat->a; jj = amat->j;
713: for (i=0; i<amat->nz; i++) {
714: for (j=0; j<bs; j++) {
715: col = bs*(cstart + *jj) + j; /* column index */
716: for (row=0; row<bs; row++) {
717: tmp[col] += PetscAbsScalar(*v); v++;
718: }
719: }
720: jj++;
721: }
722: v = bmat->a; jj = bmat->j;
723: for (i=0; i<bmat->nz; i++) {
724: for (j=0; j<bs; j++) {
725: col = bs*garray[*jj] + j;
726: for (row=0; row<bs; row++) {
727: tmp[col] += PetscAbsScalar(*v); v++;
728: }
729: }
730: jj++;
731: }
732: MPIU_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
733: *nrm = 0.0;
734: for (j=0; j<mat->cmap->N; j++) {
735: if (tmp2[j] > *nrm) *nrm = tmp2[j];
736: }
737: PetscFree2(tmp,tmp2);
738: } else if (type == NORM_INFINITY) { /* max row sum */
739: PetscReal *sums;
740: PetscMalloc1(bs,&sums);
741: sum = 0.0;
742: for (j=0; j<amat->mbs; j++) {
743: for (row=0; row<bs; row++) sums[row] = 0.0;
744: v = amat->a + bs2*amat->i[j];
745: nz = amat->i[j+1]-amat->i[j];
746: for (i=0; i<nz; i++) {
747: for (col=0; col<bs; col++) {
748: for (row=0; row<bs; row++) {
749: sums[row] += PetscAbsScalar(*v); v++;
750: }
751: }
752: }
753: v = bmat->a + bs2*bmat->i[j];
754: nz = bmat->i[j+1]-bmat->i[j];
755: for (i=0; i<nz; i++) {
756: for (col=0; col<bs; col++) {
757: for (row=0; row<bs; row++) {
758: sums[row] += PetscAbsScalar(*v); v++;
759: }
760: }
761: }
762: for (row=0; row<bs; row++) {
763: if (sums[row] > sum) sum = sums[row];
764: }
765: }
766: MPIU_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
767: PetscFree(sums);
768: } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
769: }
770: return(0);
771: }
773: /*
774: Creates the hash table, and sets the table
775: This table is created only once.
776: If new entried need to be added to the matrix
777: then the hash table has to be destroyed and
778: recreated.
779: */
780: PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
781: {
782: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
783: Mat A = baij->A,B=baij->B;
784: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
785: PetscInt i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
787: PetscInt ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
788: PetscInt cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
789: PetscInt *HT,key;
790: MatScalar **HD;
791: PetscReal tmp;
792: #if defined(PETSC_USE_INFO)
793: PetscInt ct=0,max=0;
794: #endif
797: if (baij->ht) return(0);
799: baij->ht_size = (PetscInt)(factor*nz);
800: ht_size = baij->ht_size;
802: /* Allocate Memory for Hash Table */
803: PetscCalloc2(ht_size,&baij->hd,ht_size,&baij->ht);
804: HD = baij->hd;
805: HT = baij->ht;
807: /* Loop Over A */
808: for (i=0; i<a->mbs; i++) {
809: for (j=ai[i]; j<ai[i+1]; j++) {
810: row = i+rstart;
811: col = aj[j]+cstart;
813: key = row*Nbs + col + 1;
814: h1 = HASH(ht_size,key,tmp);
815: for (k=0; k<ht_size; k++) {
816: if (!HT[(h1+k)%ht_size]) {
817: HT[(h1+k)%ht_size] = key;
818: HD[(h1+k)%ht_size] = a->a + j*bs2;
819: break;
820: #if defined(PETSC_USE_INFO)
821: } else {
822: ct++;
823: #endif
824: }
825: }
826: #if defined(PETSC_USE_INFO)
827: if (k> max) max = k;
828: #endif
829: }
830: }
831: /* Loop Over B */
832: for (i=0; i<b->mbs; i++) {
833: for (j=bi[i]; j<bi[i+1]; j++) {
834: row = i+rstart;
835: col = garray[bj[j]];
836: key = row*Nbs + col + 1;
837: h1 = HASH(ht_size,key,tmp);
838: for (k=0; k<ht_size; k++) {
839: if (!HT[(h1+k)%ht_size]) {
840: HT[(h1+k)%ht_size] = key;
841: HD[(h1+k)%ht_size] = b->a + j*bs2;
842: break;
843: #if defined(PETSC_USE_INFO)
844: } else {
845: ct++;
846: #endif
847: }
848: }
849: #if defined(PETSC_USE_INFO)
850: if (k> max) max = k;
851: #endif
852: }
853: }
855: /* Print Summary */
856: #if defined(PETSC_USE_INFO)
857: for (i=0,j=0; i<ht_size; i++) {
858: if (HT[i]) j++;
859: }
860: PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);
861: #endif
862: return(0);
863: }
865: PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
866: {
867: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
869: PetscInt nstash,reallocs;
872: if (baij->donotstash || mat->nooffprocentries) return(0);
874: MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
875: MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
876: MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
877: PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
878: MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);
879: PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
880: return(0);
881: }
883: PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
884: {
885: Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data;
886: Mat_SeqBAIJ *a =(Mat_SeqBAIJ*)baij->A->data;
888: PetscInt i,j,rstart,ncols,flg,bs2=baij->bs2;
889: PetscInt *row,*col;
890: PetscBool r1,r2,r3,other_disassembled;
891: MatScalar *val;
892: PetscMPIInt n;
895: /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
896: if (!baij->donotstash && !mat->nooffprocentries) {
897: while (1) {
898: MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
899: if (!flg) break;
901: for (i=0; i<n;) {
902: /* Now identify the consecutive vals belonging to the same row */
903: for (j=i,rstart=row[j]; j<n; j++) {
904: if (row[j] != rstart) break;
905: }
906: if (j < n) ncols = j-i;
907: else ncols = n-i;
908: /* Now assemble all these values with a single function call */
909: MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,mat->insertmode);
910: i = j;
911: }
912: }
913: MatStashScatterEnd_Private(&mat->stash);
914: /* Now process the block-stash. Since the values are stashed column-oriented,
915: set the roworiented flag to column oriented, and after MatSetValues()
916: restore the original flags */
917: r1 = baij->roworiented;
918: r2 = a->roworiented;
919: r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
921: baij->roworiented = PETSC_FALSE;
922: a->roworiented = PETSC_FALSE;
924: (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */
925: while (1) {
926: MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
927: if (!flg) break;
929: for (i=0; i<n;) {
930: /* Now identify the consecutive vals belonging to the same row */
931: for (j=i,rstart=row[j]; j<n; j++) {
932: if (row[j] != rstart) break;
933: }
934: if (j < n) ncols = j-i;
935: else ncols = n-i;
936: MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,mat->insertmode);
937: i = j;
938: }
939: }
940: MatStashScatterEnd_Private(&mat->bstash);
942: baij->roworiented = r1;
943: a->roworiented = r2;
945: ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */
946: }
948: MatAssemblyBegin(baij->A,mode);
949: MatAssemblyEnd(baij->A,mode);
951: /* determine if any processor has disassembled, if so we must
952: also disassemble ourselfs, in order that we may reassemble. */
953: /*
954: if nonzero structure of submatrix B cannot change then we know that
955: no processor disassembled thus we can skip this stuff
956: */
957: if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
958: MPIU_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
959: if (mat->was_assembled && !other_disassembled) {
960: MatDisAssemble_MPIBAIJ(mat);
961: }
962: }
964: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
965: MatSetUpMultiply_MPIBAIJ(mat);
966: }
967: MatAssemblyBegin(baij->B,mode);
968: MatAssemblyEnd(baij->B,mode);
970: #if defined(PETSC_USE_INFO)
971: if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
972: PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",(double)((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);
974: baij->ht_total_ct = 0;
975: baij->ht_insert_ct = 0;
976: }
977: #endif
978: if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
979: MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);
981: mat->ops->setvalues = MatSetValues_MPIBAIJ_HT;
982: mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
983: }
985: PetscFree2(baij->rowvalues,baij->rowindices);
987: baij->rowvalues = 0;
989: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
990: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
991: PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
992: MPIU_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
993: }
994: return(0);
995: }
997: extern PetscErrorCode MatView_SeqBAIJ(Mat,PetscViewer);
998: #include <petscdraw.h>
999: static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1000: {
1001: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1002: PetscErrorCode ierr;
1003: PetscMPIInt rank = baij->rank;
1004: PetscInt bs = mat->rmap->bs;
1005: PetscBool iascii,isdraw;
1006: PetscViewer sviewer;
1007: PetscViewerFormat format;
1010: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1011: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1012: if (iascii) {
1013: PetscViewerGetFormat(viewer,&format);
1014: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1015: MatInfo info;
1016: MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1017: MatGetInfo(mat,MAT_LOCAL,&info);
1018: PetscViewerASCIIPushSynchronized(viewer);
1019: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %g\n",
1020: rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(double)info.memory);
1021: MatGetInfo(baij->A,MAT_LOCAL,&info);
1022: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1023: MatGetInfo(baij->B,MAT_LOCAL,&info);
1024: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
1025: PetscViewerFlush(viewer);
1026: PetscViewerASCIIPopSynchronized(viewer);
1027: PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
1028: VecScatterView(baij->Mvctx,viewer);
1029: return(0);
1030: } else if (format == PETSC_VIEWER_ASCII_INFO) {
1031: PetscViewerASCIIPrintf(viewer," block size is %D\n",bs);
1032: return(0);
1033: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
1034: return(0);
1035: }
1036: }
1038: if (isdraw) {
1039: PetscDraw draw;
1040: PetscBool isnull;
1041: PetscViewerDrawGetDraw(viewer,0,&draw);
1042: PetscDrawIsNull(draw,&isnull);
1043: if (isnull) return(0);
1044: }
1046: {
1047: /* assemble the entire matrix onto first processor. */
1048: Mat A;
1049: Mat_SeqBAIJ *Aloc;
1050: PetscInt M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
1051: MatScalar *a;
1052: const char *matname;
1054: /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1055: /* Perhaps this should be the type of mat? */
1056: MatCreate(PetscObjectComm((PetscObject)mat),&A);
1057: if (!rank) {
1058: MatSetSizes(A,M,N,M,N);
1059: } else {
1060: MatSetSizes(A,0,0,M,N);
1061: }
1062: MatSetType(A,MATMPIBAIJ);
1063: MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
1064: MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1065: PetscLogObjectParent((PetscObject)mat,(PetscObject)A);
1067: /* copy over the A part */
1068: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1069: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1070: PetscMalloc1(bs,&rvals);
1072: for (i=0; i<mbs; i++) {
1073: rvals[0] = bs*(baij->rstartbs + i);
1074: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1075: for (j=ai[i]; j<ai[i+1]; j++) {
1076: col = (baij->cstartbs+aj[j])*bs;
1077: for (k=0; k<bs; k++) {
1078: MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1079: col++; a += bs;
1080: }
1081: }
1082: }
1083: /* copy over the B part */
1084: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1085: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1086: for (i=0; i<mbs; i++) {
1087: rvals[0] = bs*(baij->rstartbs + i);
1088: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1089: for (j=ai[i]; j<ai[i+1]; j++) {
1090: col = baij->garray[aj[j]]*bs;
1091: for (k=0; k<bs; k++) {
1092: MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1093: col++; a += bs;
1094: }
1095: }
1096: }
1097: PetscFree(rvals);
1098: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1099: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1100: /*
1101: Everyone has to call to draw the matrix since the graphics waits are
1102: synchronized across all processors that share the PetscDraw object
1103: */
1104: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1105: PetscObjectGetName((PetscObject)mat,&matname);
1106: if (!rank) {
1107: PetscObjectSetName((PetscObject)((Mat_MPIBAIJ*)(A->data))->A,matname);
1108: MatView_SeqBAIJ(((Mat_MPIBAIJ*)(A->data))->A,sviewer);
1109: }
1110: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
1111: PetscViewerFlush(viewer);
1112: MatDestroy(&A);
1113: }
1114: return(0);
1115: }
1117: static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1118: {
1119: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)mat->data;
1120: Mat_SeqBAIJ *A = (Mat_SeqBAIJ*)a->A->data;
1121: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)a->B->data;
1123: PetscInt i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1124: PetscInt *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1125: int fd;
1126: PetscScalar *column_values;
1127: FILE *file;
1128: PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag;
1129: PetscInt message_count,flowcontrolcount;
1132: MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1133: MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1134: nz = bs2*(A->nz + B->nz);
1135: rlen = mat->rmap->n;
1136: PetscViewerBinaryGetDescriptor(viewer,&fd);
1137: if (!rank) {
1138: header[0] = MAT_FILE_CLASSID;
1139: header[1] = mat->rmap->N;
1140: header[2] = mat->cmap->N;
1142: MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1143: PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1144: /* get largest number of rows any processor has */
1145: range = mat->rmap->range;
1146: for (i=1; i<size; i++) {
1147: rlen = PetscMax(rlen,range[i+1] - range[i]);
1148: }
1149: } else {
1150: MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1151: }
1153: PetscMalloc1(rlen/bs,&crow_lens);
1154: /* compute lengths of each row */
1155: for (i=0; i<a->mbs; i++) {
1156: crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1157: }
1158: /* store the row lengths to the file */
1159: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1160: if (!rank) {
1161: MPI_Status status;
1162: PetscMalloc1(rlen,&row_lens);
1163: rlen = (range[1] - range[0])/bs;
1164: for (i=0; i<rlen; i++) {
1165: for (j=0; j<bs; j++) {
1166: row_lens[i*bs+j] = bs*crow_lens[i];
1167: }
1168: }
1169: PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1170: for (i=1; i<size; i++) {
1171: rlen = (range[i+1] - range[i])/bs;
1172: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1173: MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1174: for (k=0; k<rlen; k++) {
1175: for (j=0; j<bs; j++) {
1176: row_lens[k*bs+j] = bs*crow_lens[k];
1177: }
1178: }
1179: PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1180: }
1181: PetscViewerFlowControlEndMaster(viewer,&message_count);
1182: PetscFree(row_lens);
1183: } else {
1184: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1185: MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1186: PetscViewerFlowControlEndWorker(viewer,&message_count);
1187: }
1188: PetscFree(crow_lens);
1190: /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1191: information needed to make it for each row from a block row. This does require more communication but still not more than
1192: the communication needed for the nonzero values */
1193: nzmax = nz; /* space a largest processor needs */
1194: MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1195: PetscMalloc1(nzmax,&column_indices);
1196: cnt = 0;
1197: for (i=0; i<a->mbs; i++) {
1198: pcnt = cnt;
1199: for (j=B->i[i]; j<B->i[i+1]; j++) {
1200: if ((col = garray[B->j[j]]) > cstart) break;
1201: for (l=0; l<bs; l++) {
1202: column_indices[cnt++] = bs*col+l;
1203: }
1204: }
1205: for (k=A->i[i]; k<A->i[i+1]; k++) {
1206: for (l=0; l<bs; l++) {
1207: column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1208: }
1209: }
1210: for (; j<B->i[i+1]; j++) {
1211: for (l=0; l<bs; l++) {
1212: column_indices[cnt++] = bs*garray[B->j[j]]+l;
1213: }
1214: }
1215: len = cnt - pcnt;
1216: for (k=1; k<bs; k++) {
1217: PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));
1218: cnt += len;
1219: }
1220: }
1221: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1223: /* store the columns to the file */
1224: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1225: if (!rank) {
1226: MPI_Status status;
1227: PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1228: for (i=1; i<size; i++) {
1229: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1230: MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1231: MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1232: PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);
1233: }
1234: PetscViewerFlowControlEndMaster(viewer,&message_count);
1235: } else {
1236: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1237: MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1238: MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1239: PetscViewerFlowControlEndWorker(viewer,&message_count);
1240: }
1241: PetscFree(column_indices);
1243: /* load up the numerical values */
1244: PetscMalloc1(nzmax,&column_values);
1245: cnt = 0;
1246: for (i=0; i<a->mbs; i++) {
1247: rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1248: for (j=B->i[i]; j<B->i[i+1]; j++) {
1249: if (garray[B->j[j]] > cstart) break;
1250: for (l=0; l<bs; l++) {
1251: for (ll=0; ll<bs; ll++) {
1252: column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1253: }
1254: }
1255: cnt += bs;
1256: }
1257: for (k=A->i[i]; k<A->i[i+1]; k++) {
1258: for (l=0; l<bs; l++) {
1259: for (ll=0; ll<bs; ll++) {
1260: column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1261: }
1262: }
1263: cnt += bs;
1264: }
1265: for (; j<B->i[i+1]; j++) {
1266: for (l=0; l<bs; l++) {
1267: for (ll=0; ll<bs; ll++) {
1268: column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1269: }
1270: }
1271: cnt += bs;
1272: }
1273: cnt += (bs-1)*rlen;
1274: }
1275: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1277: /* store the column values to the file */
1278: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1279: if (!rank) {
1280: MPI_Status status;
1281: PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1282: for (i=1; i<size; i++) {
1283: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1284: MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1285: MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);
1286: PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);
1287: }
1288: PetscViewerFlowControlEndMaster(viewer,&message_count);
1289: } else {
1290: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1291: MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1292: MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1293: PetscViewerFlowControlEndWorker(viewer,&message_count);
1294: }
1295: PetscFree(column_values);
1297: PetscViewerBinaryGetInfoPointer(viewer,&file);
1298: if (file) {
1299: fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1300: }
1301: return(0);
1302: }
1304: PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
1305: {
1307: PetscBool iascii,isdraw,issocket,isbinary;
1310: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1311: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1312: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1313: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1314: if (iascii || isdraw || issocket) {
1315: MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);
1316: } else if (isbinary) {
1317: MatView_MPIBAIJ_Binary(mat,viewer);
1318: }
1319: return(0);
1320: }
1322: PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
1323: {
1324: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1328: #if defined(PETSC_USE_LOG)
1329: PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
1330: #endif
1331: MatStashDestroy_Private(&mat->stash);
1332: MatStashDestroy_Private(&mat->bstash);
1333: MatDestroy(&baij->A);
1334: MatDestroy(&baij->B);
1335: #if defined(PETSC_USE_CTABLE)
1336: PetscTableDestroy(&baij->colmap);
1337: #else
1338: PetscFree(baij->colmap);
1339: #endif
1340: PetscFree(baij->garray);
1341: VecDestroy(&baij->lvec);
1342: VecScatterDestroy(&baij->Mvctx);
1343: PetscFree2(baij->rowvalues,baij->rowindices);
1344: PetscFree(baij->barray);
1345: PetscFree2(baij->hd,baij->ht);
1346: PetscFree(baij->rangebs);
1347: PetscFree(mat->data);
1349: PetscObjectChangeTypeName((PetscObject)mat,0);
1350: PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1351: PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1352: PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C",NULL);
1353: PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C",NULL);
1354: PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1355: PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C",NULL);
1356: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C",NULL);
1357: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C",NULL);
1358: #if defined(PETSC_HAVE_HYPRE)
1359: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_hypre_C",NULL);
1360: #endif
1361: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_is_C",NULL);
1362: PetscObjectComposeFunction((PetscObject)mat,"MatPtAP_is_mpibaij_C",NULL);
1363: return(0);
1364: }
1366: PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1367: {
1368: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1370: PetscInt nt;
1373: VecGetLocalSize(xx,&nt);
1374: if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1375: VecGetLocalSize(yy,&nt);
1376: if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1377: VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1378: (*a->A->ops->mult)(a->A,xx,yy);
1379: VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1380: (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1381: return(0);
1382: }
1384: PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1385: {
1386: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1390: VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1391: (*a->A->ops->multadd)(a->A,xx,yy,zz);
1392: VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1393: (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1394: return(0);
1395: }
1397: PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1398: {
1399: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1403: /* do nondiagonal part */
1404: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1405: /* do local part */
1406: (*a->A->ops->multtranspose)(a->A,xx,yy);
1407: /* add partial results together */
1408: VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1409: VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1410: return(0);
1411: }
1413: PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1414: {
1415: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1419: /* do nondiagonal part */
1420: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1421: /* do local part */
1422: (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1423: /* add partial results together */
1424: VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1425: VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1426: return(0);
1427: }
1429: /*
1430: This only works correctly for square matrices where the subblock A->A is the
1431: diagonal block
1432: */
1433: PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1434: {
1435: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1439: if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1440: MatGetDiagonal(a->A,v);
1441: return(0);
1442: }
1444: PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1445: {
1446: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1450: MatScale(a->A,aa);
1451: MatScale(a->B,aa);
1452: return(0);
1453: }
1455: PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1456: {
1457: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
1458: PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p;
1460: PetscInt bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1461: PetscInt nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1462: PetscInt *cmap,*idx_p,cstart = mat->cstartbs;
1465: if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1466: if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1467: mat->getrowactive = PETSC_TRUE;
1469: if (!mat->rowvalues && (idx || v)) {
1470: /*
1471: allocate enough space to hold information from the longest row.
1472: */
1473: Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1474: PetscInt max = 1,mbs = mat->mbs,tmp;
1475: for (i=0; i<mbs; i++) {
1476: tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1477: if (max < tmp) max = tmp;
1478: }
1479: PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1480: }
1481: lrow = row - brstart;
1483: pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1484: if (!v) {pvA = 0; pvB = 0;}
1485: if (!idx) {pcA = 0; if (!v) pcB = 0;}
1486: (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1487: (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1488: nztot = nzA + nzB;
1490: cmap = mat->garray;
1491: if (v || idx) {
1492: if (nztot) {
1493: /* Sort by increasing column numbers, assuming A and B already sorted */
1494: PetscInt imark = -1;
1495: if (v) {
1496: *v = v_p = mat->rowvalues;
1497: for (i=0; i<nzB; i++) {
1498: if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1499: else break;
1500: }
1501: imark = i;
1502: for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i];
1503: for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i];
1504: }
1505: if (idx) {
1506: *idx = idx_p = mat->rowindices;
1507: if (imark > -1) {
1508: for (i=0; i<imark; i++) {
1509: idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1510: }
1511: } else {
1512: for (i=0; i<nzB; i++) {
1513: if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1514: else break;
1515: }
1516: imark = i;
1517: }
1518: for (i=0; i<nzA; i++) idx_p[imark+i] = cstart*bs + cworkA[i];
1519: for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1520: }
1521: } else {
1522: if (idx) *idx = 0;
1523: if (v) *v = 0;
1524: }
1525: }
1526: *nz = nztot;
1527: (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1528: (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1529: return(0);
1530: }
1532: PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1533: {
1534: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1537: if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1538: baij->getrowactive = PETSC_FALSE;
1539: return(0);
1540: }
1542: PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
1543: {
1544: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1548: MatZeroEntries(l->A);
1549: MatZeroEntries(l->B);
1550: return(0);
1551: }
1553: PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1554: {
1555: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)matin->data;
1556: Mat A = a->A,B = a->B;
1558: PetscReal isend[5],irecv[5];
1561: info->block_size = (PetscReal)matin->rmap->bs;
1563: MatGetInfo(A,MAT_LOCAL,info);
1565: isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1566: isend[3] = info->memory; isend[4] = info->mallocs;
1568: MatGetInfo(B,MAT_LOCAL,info);
1570: isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1571: isend[3] += info->memory; isend[4] += info->mallocs;
1573: if (flag == MAT_LOCAL) {
1574: info->nz_used = isend[0];
1575: info->nz_allocated = isend[1];
1576: info->nz_unneeded = isend[2];
1577: info->memory = isend[3];
1578: info->mallocs = isend[4];
1579: } else if (flag == MAT_GLOBAL_MAX) {
1580: MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));
1582: info->nz_used = irecv[0];
1583: info->nz_allocated = irecv[1];
1584: info->nz_unneeded = irecv[2];
1585: info->memory = irecv[3];
1586: info->mallocs = irecv[4];
1587: } else if (flag == MAT_GLOBAL_SUM) {
1588: MPIU_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));
1590: info->nz_used = irecv[0];
1591: info->nz_allocated = irecv[1];
1592: info->nz_unneeded = irecv[2];
1593: info->memory = irecv[3];
1594: info->mallocs = irecv[4];
1595: } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1596: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1597: info->fill_ratio_needed = 0;
1598: info->factor_mallocs = 0;
1599: return(0);
1600: }
1602: PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
1603: {
1604: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1608: switch (op) {
1609: case MAT_NEW_NONZERO_LOCATIONS:
1610: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1611: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1612: case MAT_KEEP_NONZERO_PATTERN:
1613: case MAT_NEW_NONZERO_LOCATION_ERR:
1614: MatCheckPreallocated(A,1);
1615: MatSetOption(a->A,op,flg);
1616: MatSetOption(a->B,op,flg);
1617: break;
1618: case MAT_ROW_ORIENTED:
1619: MatCheckPreallocated(A,1);
1620: a->roworiented = flg;
1622: MatSetOption(a->A,op,flg);
1623: MatSetOption(a->B,op,flg);
1624: break;
1625: case MAT_NEW_DIAGONALS:
1626: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1627: break;
1628: case MAT_IGNORE_OFF_PROC_ENTRIES:
1629: a->donotstash = flg;
1630: break;
1631: case MAT_USE_HASH_TABLE:
1632: a->ht_flag = flg;
1633: a->ht_fact = 1.39;
1634: break;
1635: case MAT_SYMMETRIC:
1636: case MAT_STRUCTURALLY_SYMMETRIC:
1637: case MAT_HERMITIAN:
1638: case MAT_SUBMAT_SINGLEIS:
1639: case MAT_SYMMETRY_ETERNAL:
1640: MatCheckPreallocated(A,1);
1641: MatSetOption(a->A,op,flg);
1642: break;
1643: default:
1644: SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1645: }
1646: return(0);
1647: }
1649: PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
1650: {
1651: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)A->data;
1652: Mat_SeqBAIJ *Aloc;
1653: Mat B;
1655: PetscInt M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1656: PetscInt bs=A->rmap->bs,mbs=baij->mbs;
1657: MatScalar *a;
1660: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1661: MatCreate(PetscObjectComm((PetscObject)A),&B);
1662: MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1663: MatSetType(B,((PetscObject)A)->type_name);
1664: /* Do not know preallocation information, but must set block size */
1665: MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);
1666: } else {
1667: B = *matout;
1668: }
1670: /* copy over the A part */
1671: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1672: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1673: PetscMalloc1(bs,&rvals);
1675: for (i=0; i<mbs; i++) {
1676: rvals[0] = bs*(baij->rstartbs + i);
1677: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1678: for (j=ai[i]; j<ai[i+1]; j++) {
1679: col = (baij->cstartbs+aj[j])*bs;
1680: for (k=0; k<bs; k++) {
1681: MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1683: col++; a += bs;
1684: }
1685: }
1686: }
1687: /* copy over the B part */
1688: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1689: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1690: for (i=0; i<mbs; i++) {
1691: rvals[0] = bs*(baij->rstartbs + i);
1692: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1693: for (j=ai[i]; j<ai[i+1]; j++) {
1694: col = baij->garray[aj[j]]*bs;
1695: for (k=0; k<bs; k++) {
1696: MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1697: col++;
1698: a += bs;
1699: }
1700: }
1701: }
1702: PetscFree(rvals);
1703: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1704: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1706: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) *matout = B;
1707: else {
1708: MatHeaderMerge(A,&B);
1709: }
1710: return(0);
1711: }
1713: PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
1714: {
1715: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1716: Mat a = baij->A,b = baij->B;
1718: PetscInt s1,s2,s3;
1721: MatGetLocalSize(mat,&s2,&s3);
1722: if (rr) {
1723: VecGetLocalSize(rr,&s1);
1724: if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1725: /* Overlap communication with computation. */
1726: VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1727: }
1728: if (ll) {
1729: VecGetLocalSize(ll,&s1);
1730: if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1731: (*b->ops->diagonalscale)(b,ll,NULL);
1732: }
1733: /* scale the diagonal block */
1734: (*a->ops->diagonalscale)(a,ll,rr);
1736: if (rr) {
1737: /* Do a scatter end and then right scale the off-diagonal block */
1738: VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1739: (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1740: }
1741: return(0);
1742: }
1744: PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1745: {
1746: Mat_MPIBAIJ *l = (Mat_MPIBAIJ *) A->data;
1747: PetscInt *lrows;
1748: PetscInt r, len;
1749: PetscBool cong;
1753: /* get locally owned rows */
1754: MatZeroRowsMapLocal_Private(A,N,rows,&len,&lrows);
1755: /* fix right hand side if needed */
1756: if (x && b) {
1757: const PetscScalar *xx;
1758: PetscScalar *bb;
1760: VecGetArrayRead(x,&xx);
1761: VecGetArray(b,&bb);
1762: for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
1763: VecRestoreArrayRead(x,&xx);
1764: VecRestoreArray(b,&bb);
1765: }
1767: /* actually zap the local rows */
1768: /*
1769: Zero the required rows. If the "diagonal block" of the matrix
1770: is square and the user wishes to set the diagonal we use separate
1771: code so that MatSetValues() is not called for each diagonal allocating
1772: new memory, thus calling lots of mallocs and slowing things down.
1774: */
1775: /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1776: MatZeroRows_SeqBAIJ(l->B,len,lrows,0.0,NULL,NULL);
1777: MatHasCongruentLayouts(A,&cong);
1778: if ((diag != 0.0) && cong) {
1779: MatZeroRows_SeqBAIJ(l->A,len,lrows,diag,NULL,NULL);
1780: } else if (diag != 0.0) {
1781: MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,0,0);
1782: if (((Mat_SeqBAIJ*)l->A->data)->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options \n\
1783: MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1784: for (r = 0; r < len; ++r) {
1785: const PetscInt row = lrows[r] + A->rmap->rstart;
1786: MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
1787: }
1788: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1789: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1790: } else {
1791: MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,NULL,NULL);
1792: }
1793: PetscFree(lrows);
1795: /* only change matrix nonzero state if pattern was allowed to be changed */
1796: if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1797: PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1798: MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1799: }
1800: return(0);
1801: }
1803: PetscErrorCode MatZeroRowsColumns_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1804: {
1805: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1806: PetscErrorCode ierr;
1807: PetscMPIInt n = A->rmap->n;
1808: PetscInt i,j,k,r,p = 0,len = 0,row,col,count;
1809: PetscInt *lrows,*owners = A->rmap->range;
1810: PetscSFNode *rrows;
1811: PetscSF sf;
1812: const PetscScalar *xx;
1813: PetscScalar *bb,*mask;
1814: Vec xmask,lmask;
1815: Mat_SeqBAIJ *baij = (Mat_SeqBAIJ*)l->B->data;
1816: PetscInt bs = A->rmap->bs, bs2 = baij->bs2;
1817: PetscScalar *aa;
1820: /* Create SF where leaves are input rows and roots are owned rows */
1821: PetscMalloc1(n, &lrows);
1822: for (r = 0; r < n; ++r) lrows[r] = -1;
1823: PetscMalloc1(N, &rrows);
1824: for (r = 0; r < N; ++r) {
1825: const PetscInt idx = rows[r];
1826: if (idx < 0 || A->rmap->N <= idx) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range [0,%D)",idx,A->rmap->N);
1827: if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1828: PetscLayoutFindOwner(A->rmap,idx,&p);
1829: }
1830: rrows[r].rank = p;
1831: rrows[r].index = rows[r] - owners[p];
1832: }
1833: PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1834: PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1835: /* Collect flags for rows to be zeroed */
1836: PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1837: PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1838: PetscSFDestroy(&sf);
1839: /* Compress and put in row numbers */
1840: for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1841: /* zero diagonal part of matrix */
1842: MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
1843: /* handle off diagonal part of matrix */
1844: MatCreateVecs(A,&xmask,NULL);
1845: VecDuplicate(l->lvec,&lmask);
1846: VecGetArray(xmask,&bb);
1847: for (i=0; i<len; i++) bb[lrows[i]] = 1;
1848: VecRestoreArray(xmask,&bb);
1849: VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1850: VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1851: VecDestroy(&xmask);
1852: if (x) {
1853: VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1854: VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1855: VecGetArrayRead(l->lvec,&xx);
1856: VecGetArray(b,&bb);
1857: }
1858: VecGetArray(lmask,&mask);
1859: /* remove zeroed rows of off diagonal matrix */
1860: for (i = 0; i < len; ++i) {
1861: row = lrows[i];
1862: count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
1863: aa = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
1864: for (k = 0; k < count; ++k) {
1865: aa[0] = 0.0;
1866: aa += bs;
1867: }
1868: }
1869: /* loop over all elements of off process part of matrix zeroing removed columns*/
1870: for (i = 0; i < l->B->rmap->N; ++i) {
1871: row = i/bs;
1872: for (j = baij->i[row]; j < baij->i[row+1]; ++j) {
1873: for (k = 0; k < bs; ++k) {
1874: col = bs*baij->j[j] + k;
1875: if (PetscAbsScalar(mask[col])) {
1876: aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1877: if (x) bb[i] -= aa[0]*xx[col];
1878: aa[0] = 0.0;
1879: }
1880: }
1881: }
1882: }
1883: if (x) {
1884: VecRestoreArray(b,&bb);
1885: VecRestoreArrayRead(l->lvec,&xx);
1886: }
1887: VecRestoreArray(lmask,&mask);
1888: VecDestroy(&lmask);
1889: PetscFree(lrows);
1891: /* only change matrix nonzero state if pattern was allowed to be changed */
1892: if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1893: PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1894: MPIU_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1895: }
1896: return(0);
1897: }
1899: PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1900: {
1901: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1905: MatSetUnfactored(a->A);
1906: return(0);
1907: }
1909: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat*);
1911: PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool *flag)
1912: {
1913: Mat_MPIBAIJ *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
1914: Mat a,b,c,d;
1915: PetscBool flg;
1919: a = matA->A; b = matA->B;
1920: c = matB->A; d = matB->B;
1922: MatEqual(a,c,&flg);
1923: if (flg) {
1924: MatEqual(b,d,&flg);
1925: }
1926: MPIU_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1927: return(0);
1928: }
1930: PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
1931: {
1933: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1934: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)B->data;
1937: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1938: if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1939: MatCopy_Basic(A,B,str);
1940: } else {
1941: MatCopy(a->A,b->A,str);
1942: MatCopy(a->B,b->B,str);
1943: }
1944: PetscObjectStateIncrease((PetscObject)B);
1945: return(0);
1946: }
1948: PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1949: {
1953: MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1954: return(0);
1955: }
1957: PetscErrorCode MatAXPYGetPreallocation_MPIBAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
1958: {
1960: PetscInt bs = Y->rmap->bs,m = Y->rmap->N/bs;
1961: Mat_SeqBAIJ *x = (Mat_SeqBAIJ*)X->data;
1962: Mat_SeqBAIJ *y = (Mat_SeqBAIJ*)Y->data;
1965: MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
1966: return(0);
1967: }
1969: PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1970: {
1972: Mat_MPIBAIJ *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
1973: PetscBLASInt bnz,one=1;
1974: Mat_SeqBAIJ *x,*y;
1975: PetscInt bs2 = Y->rmap->bs*Y->rmap->bs;
1978: if (str == SAME_NONZERO_PATTERN) {
1979: PetscScalar alpha = a;
1980: x = (Mat_SeqBAIJ*)xx->A->data;
1981: y = (Mat_SeqBAIJ*)yy->A->data;
1982: PetscBLASIntCast(x->nz*bs2,&bnz);
1983: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1984: x = (Mat_SeqBAIJ*)xx->B->data;
1985: y = (Mat_SeqBAIJ*)yy->B->data;
1986: PetscBLASIntCast(x->nz*bs2,&bnz);
1987: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1988: PetscObjectStateIncrease((PetscObject)Y);
1989: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1990: MatAXPY_Basic(Y,a,X,str);
1991: } else {
1992: Mat B;
1993: PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
1994: PetscMalloc1(yy->A->rmap->N,&nnz_d);
1995: PetscMalloc1(yy->B->rmap->N,&nnz_o);
1996: MatCreate(PetscObjectComm((PetscObject)Y),&B);
1997: PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
1998: MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
1999: MatSetBlockSizesFromMats(B,Y,Y);
2000: MatSetType(B,MATMPIBAIJ);
2001: MatAXPYGetPreallocation_SeqBAIJ(yy->A,xx->A,nnz_d);
2002: MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2003: MatMPIBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
2004: /* MatAXPY_BasicWithPreallocation() for BAIJ matrix is much slower than AIJ, even for bs=1 ! */
2005: MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2006: MatHeaderReplace(Y,&B);
2007: PetscFree(nnz_d);
2008: PetscFree(nnz_o);
2009: }
2010: return(0);
2011: }
2013: PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
2014: {
2015: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2019: MatRealPart(a->A);
2020: MatRealPart(a->B);
2021: return(0);
2022: }
2024: PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
2025: {
2026: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2030: MatImaginaryPart(a->A);
2031: MatImaginaryPart(a->B);
2032: return(0);
2033: }
2035: PetscErrorCode MatCreateSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
2036: {
2038: IS iscol_local;
2039: PetscInt csize;
2042: ISGetLocalSize(iscol,&csize);
2043: if (call == MAT_REUSE_MATRIX) {
2044: PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
2045: if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2046: } else {
2047: ISAllGather(iscol,&iscol_local);
2048: }
2049: MatCreateSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
2050: if (call == MAT_INITIAL_MATRIX) {
2051: PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
2052: ISDestroy(&iscol_local);
2053: }
2054: return(0);
2055: }
2057: /*
2058: Not great since it makes two copies of the submatrix, first an SeqBAIJ
2059: in local and then by concatenating the local matrices the end result.
2060: Writing it directly would be much like MatCreateSubMatrices_MPIBAIJ().
2061: This routine is used for BAIJ and SBAIJ matrices (unfortunate dependency).
2062: */
2063: PetscErrorCode MatCreateSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2064: {
2066: PetscMPIInt rank,size;
2067: PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs;
2068: PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2069: Mat M,Mreuse;
2070: MatScalar *vwork,*aa;
2071: MPI_Comm comm;
2072: IS isrow_new, iscol_new;
2073: Mat_SeqBAIJ *aij;
2076: PetscObjectGetComm((PetscObject)mat,&comm);
2077: MPI_Comm_rank(comm,&rank);
2078: MPI_Comm_size(comm,&size);
2079: /* The compression and expansion should be avoided. Doesn't point
2080: out errors, might change the indices, hence buggey */
2081: ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);
2082: ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);
2084: if (call == MAT_REUSE_MATRIX) {
2085: PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
2086: if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2087: MatCreateSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&Mreuse);
2088: } else {
2089: MatCreateSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&Mreuse);
2090: }
2091: ISDestroy(&isrow_new);
2092: ISDestroy(&iscol_new);
2093: /*
2094: m - number of local rows
2095: n - number of columns (same on all processors)
2096: rstart - first row in new global matrix generated
2097: */
2098: MatGetBlockSize(mat,&bs);
2099: MatGetSize(Mreuse,&m,&n);
2100: m = m/bs;
2101: n = n/bs;
2103: if (call == MAT_INITIAL_MATRIX) {
2104: aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2105: ii = aij->i;
2106: jj = aij->j;
2108: /*
2109: Determine the number of non-zeros in the diagonal and off-diagonal
2110: portions of the matrix in order to do correct preallocation
2111: */
2113: /* first get start and end of "diagonal" columns */
2114: if (csize == PETSC_DECIDE) {
2115: ISGetSize(isrow,&mglobal);
2116: if (mglobal == n*bs) { /* square matrix */
2117: nlocal = m;
2118: } else {
2119: nlocal = n/size + ((n % size) > rank);
2120: }
2121: } else {
2122: nlocal = csize/bs;
2123: }
2124: MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2125: rstart = rend - nlocal;
2126: 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);
2128: /* next, compute all the lengths */
2129: PetscMalloc2(m+1,&dlens,m+1,&olens);
2130: for (i=0; i<m; i++) {
2131: jend = ii[i+1] - ii[i];
2132: olen = 0;
2133: dlen = 0;
2134: for (j=0; j<jend; j++) {
2135: if (*jj < rstart || *jj >= rend) olen++;
2136: else dlen++;
2137: jj++;
2138: }
2139: olens[i] = olen;
2140: dlens[i] = dlen;
2141: }
2142: MatCreate(comm,&M);
2143: MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);
2144: MatSetType(M,((PetscObject)mat)->type_name);
2145: MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2146: MatMPISBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2147: PetscFree2(dlens,olens);
2148: } else {
2149: PetscInt ml,nl;
2151: M = *newmat;
2152: MatGetLocalSize(M,&ml,&nl);
2153: if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2154: MatZeroEntries(M);
2155: /*
2156: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2157: rather than the slower MatSetValues().
2158: */
2159: M->was_assembled = PETSC_TRUE;
2160: M->assembled = PETSC_FALSE;
2161: }
2162: MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);
2163: MatGetOwnershipRange(M,&rstart,&rend);
2164: aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2165: ii = aij->i;
2166: jj = aij->j;
2167: aa = aij->a;
2168: for (i=0; i<m; i++) {
2169: row = rstart/bs + i;
2170: nz = ii[i+1] - ii[i];
2171: cwork = jj; jj += nz;
2172: vwork = aa; aa += nz*bs*bs;
2173: MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2174: }
2176: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2177: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2178: *newmat = M;
2180: /* save submatrix used in processor for next request */
2181: if (call == MAT_INITIAL_MATRIX) {
2182: PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2183: PetscObjectDereference((PetscObject)Mreuse);
2184: }
2185: return(0);
2186: }
2188: PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
2189: {
2190: MPI_Comm comm,pcomm;
2191: PetscInt clocal_size,nrows;
2192: const PetscInt *rows;
2193: PetscMPIInt size;
2194: IS crowp,lcolp;
2198: PetscObjectGetComm((PetscObject)A,&comm);
2199: /* make a collective version of 'rowp' */
2200: PetscObjectGetComm((PetscObject)rowp,&pcomm);
2201: if (pcomm==comm) {
2202: crowp = rowp;
2203: } else {
2204: ISGetSize(rowp,&nrows);
2205: ISGetIndices(rowp,&rows);
2206: ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);
2207: ISRestoreIndices(rowp,&rows);
2208: }
2209: ISSetPermutation(crowp);
2210: /* make a local version of 'colp' */
2211: PetscObjectGetComm((PetscObject)colp,&pcomm);
2212: MPI_Comm_size(pcomm,&size);
2213: if (size==1) {
2214: lcolp = colp;
2215: } else {
2216: ISAllGather(colp,&lcolp);
2217: }
2218: ISSetPermutation(lcolp);
2219: /* now we just get the submatrix */
2220: MatGetLocalSize(A,NULL,&clocal_size);
2221: MatCreateSubMatrix_MPIBAIJ_Private(A,crowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);
2222: /* clean up */
2223: if (pcomm!=comm) {
2224: ISDestroy(&crowp);
2225: }
2226: if (size>1) {
2227: ISDestroy(&lcolp);
2228: }
2229: return(0);
2230: }
2232: PetscErrorCode MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
2233: {
2234: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
2235: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data;
2238: if (nghosts) *nghosts = B->nbs;
2239: if (ghosts) *ghosts = baij->garray;
2240: return(0);
2241: }
2243: PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2244: {
2245: Mat B;
2246: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2247: Mat_SeqBAIJ *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2248: Mat_SeqAIJ *b;
2250: PetscMPIInt size,rank,*recvcounts = 0,*displs = 0;
2251: PetscInt sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2252: PetscInt m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2255: MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2256: MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
2258: /* ----------------------------------------------------------------
2259: Tell every processor the number of nonzeros per row
2260: */
2261: PetscMalloc1(A->rmap->N/bs,&lens);
2262: for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2263: lens[i] = ad->i[i-A->rmap->rstart/bs+1] - ad->i[i-A->rmap->rstart/bs] + bd->i[i-A->rmap->rstart/bs+1] - bd->i[i-A->rmap->rstart/bs];
2264: }
2265: PetscMalloc1(2*size,&recvcounts);
2266: displs = recvcounts + size;
2267: for (i=0; i<size; i++) {
2268: recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2269: displs[i] = A->rmap->range[i]/bs;
2270: }
2271: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2272: MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2273: #else
2274: sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2275: MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2276: #endif
2277: /* ---------------------------------------------------------------
2278: Create the sequential matrix of the same type as the local block diagonal
2279: */
2280: MatCreate(PETSC_COMM_SELF,&B);
2281: MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);
2282: MatSetType(B,MATSEQAIJ);
2283: MatSeqAIJSetPreallocation(B,0,lens);
2284: b = (Mat_SeqAIJ*)B->data;
2286: /*--------------------------------------------------------------------
2287: Copy my part of matrix column indices over
2288: */
2289: sendcount = ad->nz + bd->nz;
2290: jsendbuf = b->j + b->i[rstarts[rank]/bs];
2291: a_jsendbuf = ad->j;
2292: b_jsendbuf = bd->j;
2293: n = A->rmap->rend/bs - A->rmap->rstart/bs;
2294: cnt = 0;
2295: for (i=0; i<n; i++) {
2297: /* put in lower diagonal portion */
2298: m = bd->i[i+1] - bd->i[i];
2299: while (m > 0) {
2300: /* is it above diagonal (in bd (compressed) numbering) */
2301: if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2302: jsendbuf[cnt++] = garray[*b_jsendbuf++];
2303: m--;
2304: }
2306: /* put in diagonal portion */
2307: for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2308: jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2309: }
2311: /* put in upper diagonal portion */
2312: while (m-- > 0) {
2313: jsendbuf[cnt++] = garray[*b_jsendbuf++];
2314: }
2315: }
2316: if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2318: /*--------------------------------------------------------------------
2319: Gather all column indices to all processors
2320: */
2321: for (i=0; i<size; i++) {
2322: recvcounts[i] = 0;
2323: for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2324: recvcounts[i] += lens[j];
2325: }
2326: }
2327: displs[0] = 0;
2328: for (i=1; i<size; i++) {
2329: displs[i] = displs[i-1] + recvcounts[i-1];
2330: }
2331: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2332: MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2333: #else
2334: MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2335: #endif
2336: /*--------------------------------------------------------------------
2337: Assemble the matrix into useable form (note numerical values not yet set)
2338: */
2339: /* set the b->ilen (length of each row) values */
2340: PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));
2341: /* set the b->i indices */
2342: b->i[0] = 0;
2343: for (i=1; i<=A->rmap->N/bs; i++) {
2344: b->i[i] = b->i[i-1] + lens[i-1];
2345: }
2346: PetscFree(lens);
2347: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2348: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2349: PetscFree(recvcounts);
2351: if (A->symmetric) {
2352: MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);
2353: } else if (A->hermitian) {
2354: MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);
2355: } else if (A->structurally_symmetric) {
2356: MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
2357: }
2358: *newmat = B;
2359: return(0);
2360: }
2362: PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2363: {
2364: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
2366: Vec bb1 = 0;
2369: if (flag == SOR_APPLY_UPPER) {
2370: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2371: return(0);
2372: }
2374: if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2375: VecDuplicate(bb,&bb1);
2376: }
2378: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2379: if (flag & SOR_ZERO_INITIAL_GUESS) {
2380: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2381: its--;
2382: }
2384: while (its--) {
2385: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2386: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2388: /* update rhs: bb1 = bb - B*x */
2389: VecScale(mat->lvec,-1.0);
2390: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2392: /* local sweep */
2393: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
2394: }
2395: } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2396: if (flag & SOR_ZERO_INITIAL_GUESS) {
2397: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2398: its--;
2399: }
2400: while (its--) {
2401: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2402: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2404: /* update rhs: bb1 = bb - B*x */
2405: VecScale(mat->lvec,-1.0);
2406: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2408: /* local sweep */
2409: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
2410: }
2411: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2412: if (flag & SOR_ZERO_INITIAL_GUESS) {
2413: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2414: its--;
2415: }
2416: while (its--) {
2417: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2418: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2420: /* update rhs: bb1 = bb - B*x */
2421: VecScale(mat->lvec,-1.0);
2422: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2424: /* local sweep */
2425: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
2426: }
2427: } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2429: VecDestroy(&bb1);
2430: return(0);
2431: }
2433: PetscErrorCode MatGetColumnNorms_MPIBAIJ(Mat A,NormType type,PetscReal *norms)
2434: {
2436: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)A->data;
2437: PetscInt N,i,*garray = aij->garray;
2438: PetscInt ib,jb,bs = A->rmap->bs;
2439: Mat_SeqBAIJ *a_aij = (Mat_SeqBAIJ*) aij->A->data;
2440: MatScalar *a_val = a_aij->a;
2441: Mat_SeqBAIJ *b_aij = (Mat_SeqBAIJ*) aij->B->data;
2442: MatScalar *b_val = b_aij->a;
2443: PetscReal *work;
2446: MatGetSize(A,NULL,&N);
2447: PetscCalloc1(N,&work);
2448: if (type == NORM_2) {
2449: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2450: for (jb=0; jb<bs; jb++) {
2451: for (ib=0; ib<bs; ib++) {
2452: work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val * *a_val);
2453: a_val++;
2454: }
2455: }
2456: }
2457: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2458: for (jb=0; jb<bs; jb++) {
2459: for (ib=0; ib<bs; ib++) {
2460: work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val * *b_val);
2461: b_val++;
2462: }
2463: }
2464: }
2465: } else if (type == NORM_1) {
2466: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2467: for (jb=0; jb<bs; jb++) {
2468: for (ib=0; ib<bs; ib++) {
2469: work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val);
2470: a_val++;
2471: }
2472: }
2473: }
2474: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2475: for (jb=0; jb<bs; jb++) {
2476: for (ib=0; ib<bs; ib++) {
2477: work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val);
2478: b_val++;
2479: }
2480: }
2481: }
2482: } else if (type == NORM_INFINITY) {
2483: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2484: for (jb=0; jb<bs; jb++) {
2485: for (ib=0; ib<bs; ib++) {
2486: int col = A->cmap->rstart + a_aij->j[i] * bs + jb;
2487: work[col] = PetscMax(PetscAbsScalar(*a_val), work[col]);
2488: a_val++;
2489: }
2490: }
2491: }
2492: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2493: for (jb=0; jb<bs; jb++) {
2494: for (ib=0; ib<bs; ib++) {
2495: int col = garray[b_aij->j[i]] * bs + jb;
2496: work[col] = PetscMax(PetscAbsScalar(*b_val), work[col]);
2497: b_val++;
2498: }
2499: }
2500: }
2501: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
2502: if (type == NORM_INFINITY) {
2503: MPIU_Allreduce(work,norms,N,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
2504: } else {
2505: MPIU_Allreduce(work,norms,N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
2506: }
2507: PetscFree(work);
2508: if (type == NORM_2) {
2509: for (i=0; i<N; i++) norms[i] = PetscSqrtReal(norms[i]);
2510: }
2511: return(0);
2512: }
2514: PetscErrorCode MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2515: {
2516: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*) A->data;
2520: MatInvertBlockDiagonal(a->A,values);
2521: A->factorerrortype = a->A->factorerrortype;
2522: A->factorerror_zeropivot_value = a->A->factorerror_zeropivot_value;
2523: A->factorerror_zeropivot_row = a->A->factorerror_zeropivot_row;
2524: return(0);
2525: }
2527: PetscErrorCode MatShift_MPIBAIJ(Mat Y,PetscScalar a)
2528: {
2530: Mat_MPIBAIJ *maij = (Mat_MPIBAIJ*)Y->data;
2531: Mat_SeqBAIJ *aij = (Mat_SeqBAIJ*)maij->A->data;
2534: if (!Y->preallocated) {
2535: MatMPIBAIJSetPreallocation(Y,Y->rmap->bs,1,NULL,0,NULL);
2536: } else if (!aij->nz) {
2537: PetscInt nonew = aij->nonew;
2538: MatSeqBAIJSetPreallocation(maij->A,Y->rmap->bs,1,NULL);
2539: aij->nonew = nonew;
2540: }
2541: MatShift_Basic(Y,a);
2542: return(0);
2543: }
2545: PetscErrorCode MatMissingDiagonal_MPIBAIJ(Mat A,PetscBool *missing,PetscInt *d)
2546: {
2547: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2551: if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only works for square matrices");
2552: MatMissingDiagonal(a->A,missing,d);
2553: if (d) {
2554: PetscInt rstart;
2555: MatGetOwnershipRange(A,&rstart,NULL);
2556: *d += rstart/A->rmap->bs;
2558: }
2559: return(0);
2560: }
2562: PetscErrorCode MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
2563: {
2565: *a = ((Mat_MPIBAIJ*)A->data)->A;
2566: return(0);
2567: }
2569: /* -------------------------------------------------------------------*/
2570: static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2571: MatGetRow_MPIBAIJ,
2572: MatRestoreRow_MPIBAIJ,
2573: MatMult_MPIBAIJ,
2574: /* 4*/ MatMultAdd_MPIBAIJ,
2575: MatMultTranspose_MPIBAIJ,
2576: MatMultTransposeAdd_MPIBAIJ,
2577: 0,
2578: 0,
2579: 0,
2580: /*10*/ 0,
2581: 0,
2582: 0,
2583: MatSOR_MPIBAIJ,
2584: MatTranspose_MPIBAIJ,
2585: /*15*/ MatGetInfo_MPIBAIJ,
2586: MatEqual_MPIBAIJ,
2587: MatGetDiagonal_MPIBAIJ,
2588: MatDiagonalScale_MPIBAIJ,
2589: MatNorm_MPIBAIJ,
2590: /*20*/ MatAssemblyBegin_MPIBAIJ,
2591: MatAssemblyEnd_MPIBAIJ,
2592: MatSetOption_MPIBAIJ,
2593: MatZeroEntries_MPIBAIJ,
2594: /*24*/ MatZeroRows_MPIBAIJ,
2595: 0,
2596: 0,
2597: 0,
2598: 0,
2599: /*29*/ MatSetUp_MPIBAIJ,
2600: 0,
2601: 0,
2602: MatGetDiagonalBlock_MPIBAIJ,
2603: 0,
2604: /*34*/ MatDuplicate_MPIBAIJ,
2605: 0,
2606: 0,
2607: 0,
2608: 0,
2609: /*39*/ MatAXPY_MPIBAIJ,
2610: MatCreateSubMatrices_MPIBAIJ,
2611: MatIncreaseOverlap_MPIBAIJ,
2612: MatGetValues_MPIBAIJ,
2613: MatCopy_MPIBAIJ,
2614: /*44*/ 0,
2615: MatScale_MPIBAIJ,
2616: MatShift_MPIBAIJ,
2617: 0,
2618: MatZeroRowsColumns_MPIBAIJ,
2619: /*49*/ 0,
2620: 0,
2621: 0,
2622: 0,
2623: 0,
2624: /*54*/ MatFDColoringCreate_MPIXAIJ,
2625: 0,
2626: MatSetUnfactored_MPIBAIJ,
2627: MatPermute_MPIBAIJ,
2628: MatSetValuesBlocked_MPIBAIJ,
2629: /*59*/ MatCreateSubMatrix_MPIBAIJ,
2630: MatDestroy_MPIBAIJ,
2631: MatView_MPIBAIJ,
2632: 0,
2633: 0,
2634: /*64*/ 0,
2635: 0,
2636: 0,
2637: 0,
2638: 0,
2639: /*69*/ MatGetRowMaxAbs_MPIBAIJ,
2640: 0,
2641: 0,
2642: 0,
2643: 0,
2644: /*74*/ 0,
2645: MatFDColoringApply_BAIJ,
2646: 0,
2647: 0,
2648: 0,
2649: /*79*/ 0,
2650: 0,
2651: 0,
2652: 0,
2653: MatLoad_MPIBAIJ,
2654: /*84*/ 0,
2655: 0,
2656: 0,
2657: 0,
2658: 0,
2659: /*89*/ 0,
2660: 0,
2661: 0,
2662: 0,
2663: 0,
2664: /*94*/ 0,
2665: 0,
2666: 0,
2667: 0,
2668: 0,
2669: /*99*/ 0,
2670: 0,
2671: 0,
2672: 0,
2673: 0,
2674: /*104*/0,
2675: MatRealPart_MPIBAIJ,
2676: MatImaginaryPart_MPIBAIJ,
2677: 0,
2678: 0,
2679: /*109*/0,
2680: 0,
2681: 0,
2682: 0,
2683: MatMissingDiagonal_MPIBAIJ,
2684: /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
2685: 0,
2686: MatGetGhosts_MPIBAIJ,
2687: 0,
2688: 0,
2689: /*119*/0,
2690: 0,
2691: 0,
2692: 0,
2693: MatGetMultiProcBlock_MPIBAIJ,
2694: /*124*/0,
2695: MatGetColumnNorms_MPIBAIJ,
2696: MatInvertBlockDiagonal_MPIBAIJ,
2697: 0,
2698: 0,
2699: /*129*/ 0,
2700: 0,
2701: 0,
2702: 0,
2703: 0,
2704: /*134*/ 0,
2705: 0,
2706: 0,
2707: 0,
2708: 0,
2709: /*139*/ MatSetBlockSizes_Default,
2710: 0,
2711: 0,
2712: MatFDColoringSetUp_MPIXAIJ,
2713: 0,
2714: /*144*/MatCreateMPIMatConcatenateSeqMat_MPIBAIJ
2715: };
2718: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat,MatType,MatReuse,Mat*);
2719: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
2721: PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2722: {
2723: PetscInt m,rstart,cstart,cend;
2724: PetscInt i,j,dlen,olen,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2725: const PetscInt *JJ =0;
2726: PetscScalar *values=0;
2727: PetscBool roworiented = ((Mat_MPIBAIJ*)B->data)->roworiented;
2731: PetscLayoutSetBlockSize(B->rmap,bs);
2732: PetscLayoutSetBlockSize(B->cmap,bs);
2733: PetscLayoutSetUp(B->rmap);
2734: PetscLayoutSetUp(B->cmap);
2735: PetscLayoutGetBlockSize(B->rmap,&bs);
2736: m = B->rmap->n/bs;
2737: rstart = B->rmap->rstart/bs;
2738: cstart = B->cmap->rstart/bs;
2739: cend = B->cmap->rend/bs;
2741: if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2742: PetscMalloc2(m,&d_nnz,m,&o_nnz);
2743: for (i=0; i<m; i++) {
2744: nz = ii[i+1] - ii[i];
2745: if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2746: nz_max = PetscMax(nz_max,nz);
2747: dlen = 0;
2748: olen = 0;
2749: JJ = jj + ii[i];
2750: for (j=0; j<nz; j++) {
2751: if (*JJ < cstart || *JJ >= cend) olen++;
2752: else dlen++;
2753: JJ++;
2754: }
2755: d_nnz[i] = dlen;
2756: o_nnz[i] = olen;
2757: }
2758: MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2759: PetscFree2(d_nnz,o_nnz);
2761: values = (PetscScalar*)V;
2762: if (!values) {
2763: PetscCalloc1(bs*bs*nz_max,&values);
2764: }
2765: for (i=0; i<m; i++) {
2766: PetscInt row = i + rstart;
2767: PetscInt ncols = ii[i+1] - ii[i];
2768: const PetscInt *icols = jj + ii[i];
2769: if (!roworiented) { /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2770: const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2771: MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2772: } else { /* block ordering does not match so we can only insert one block at a time. */
2773: PetscInt j;
2774: for (j=0; j<ncols; j++) {
2775: const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2776: MatSetValuesBlocked_MPIBAIJ(B,1,&row,1,&icols[j],svals,INSERT_VALUES);
2777: }
2778: }
2779: }
2781: if (!V) { PetscFree(values); }
2782: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2783: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2784: MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2785: return(0);
2786: }
2788: /*@C
2789: MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2790: (the default parallel PETSc format).
2792: Collective on MPI_Comm
2794: Input Parameters:
2795: + B - the matrix
2796: . bs - the block size
2797: . i - the indices into j for the start of each local row (starts with zero)
2798: . j - the column indices for each local row (starts with zero) these must be sorted for each row
2799: - v - optional values in the matrix
2801: Level: developer
2803: Notes:
2804: The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED. For example, C programs
2805: may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
2806: over rows within a block and the last index is over columns within a block row. Fortran programs will likely set
2807: MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2808: block column and the second index is over columns within a block.
2810: .keywords: matrix, aij, compressed row, sparse, parallel
2812: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, MatCreateMPIBAIJWithArrays(), MPIBAIJ
2813: @*/
2814: PetscErrorCode MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2815: {
2822: PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2823: return(0);
2824: }
2826: PetscErrorCode MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2827: {
2828: Mat_MPIBAIJ *b;
2830: PetscInt i;
2831: PetscMPIInt size;
2834: MatSetBlockSize(B,PetscAbs(bs));
2835: PetscLayoutSetUp(B->rmap);
2836: PetscLayoutSetUp(B->cmap);
2837: PetscLayoutGetBlockSize(B->rmap,&bs);
2839: if (d_nnz) {
2840: for (i=0; i<B->rmap->n/bs; i++) {
2841: if (d_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %D value %D",i,d_nnz[i]);
2842: }
2843: }
2844: if (o_nnz) {
2845: for (i=0; i<B->rmap->n/bs; i++) {
2846: if (o_nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %D value %D",i,o_nnz[i]);
2847: }
2848: }
2850: b = (Mat_MPIBAIJ*)B->data;
2851: b->bs2 = bs*bs;
2852: b->mbs = B->rmap->n/bs;
2853: b->nbs = B->cmap->n/bs;
2854: b->Mbs = B->rmap->N/bs;
2855: b->Nbs = B->cmap->N/bs;
2857: for (i=0; i<=b->size; i++) {
2858: b->rangebs[i] = B->rmap->range[i]/bs;
2859: }
2860: b->rstartbs = B->rmap->rstart/bs;
2861: b->rendbs = B->rmap->rend/bs;
2862: b->cstartbs = B->cmap->rstart/bs;
2863: b->cendbs = B->cmap->rend/bs;
2865: #if defined(PETSC_USE_CTABLE)
2866: PetscTableDestroy(&b->colmap);
2867: #else
2868: PetscFree(b->colmap);
2869: #endif
2870: PetscFree(b->garray);
2871: VecDestroy(&b->lvec);
2872: VecScatterDestroy(&b->Mvctx);
2874: /* Because the B will have been resized we simply destroy it and create a new one each time */
2875: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
2876: MatDestroy(&b->B);
2877: MatCreate(PETSC_COMM_SELF,&b->B);
2878: MatSetSizes(b->B,B->rmap->n,size > 1 ? B->cmap->N : 0,B->rmap->n,size > 1 ? B->cmap->N : 0);
2879: MatSetType(b->B,MATSEQBAIJ);
2880: PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
2882: if (!B->preallocated) {
2883: MatCreate(PETSC_COMM_SELF,&b->A);
2884: MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2885: MatSetType(b->A,MATSEQBAIJ);
2886: PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2887: MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
2888: }
2890: MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
2891: MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);
2892: B->preallocated = PETSC_TRUE;
2893: B->was_assembled = PETSC_FALSE;
2894: B->assembled = PETSC_FALSE;
2895: return(0);
2896: }
2898: extern PetscErrorCode MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
2899: extern PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
2901: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
2902: {
2903: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)B->data;
2905: Mat_SeqBAIJ *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
2906: PetscInt M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
2907: const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
2910: PetscMalloc1(M+1,&ii);
2911: ii[0] = 0;
2912: for (i=0; i<M; i++) {
2913: if ((id[i+1] - id[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,id[i],id[i+1]);
2914: if ((io[i+1] - io[i]) < 0) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Indices wrong %D %D %D",i,io[i],io[i+1]);
2915: ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
2916: /* remove one from count of matrix has diagonal */
2917: for (j=id[i]; j<id[i+1]; j++) {
2918: if (jd[j] == i) {ii[i+1]--;break;}
2919: }
2920: }
2921: PetscMalloc1(ii[M],&jj);
2922: cnt = 0;
2923: for (i=0; i<M; i++) {
2924: for (j=io[i]; j<io[i+1]; j++) {
2925: if (garray[jo[j]] > rstart) break;
2926: jj[cnt++] = garray[jo[j]];
2927: }
2928: for (k=id[i]; k<id[i+1]; k++) {
2929: if (jd[k] != i) {
2930: jj[cnt++] = rstart + jd[k];
2931: }
2932: }
2933: for (; j<io[i+1]; j++) {
2934: jj[cnt++] = garray[jo[j]];
2935: }
2936: }
2937: MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);
2938: return(0);
2939: }
2941: #include <../src/mat/impls/aij/mpi/mpiaij.h>
2943: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);
2945: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
2946: {
2948: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2949: Mat B;
2950: Mat_MPIAIJ *b;
2953: if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix must be assembled");
2955: if (reuse == MAT_REUSE_MATRIX) {
2956: B = *newmat;
2957: } else {
2958: MatCreate(PetscObjectComm((PetscObject)A),&B);
2959: MatSetType(B,MATMPIAIJ);
2960: MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
2961: MatSetBlockSizes(B,A->rmap->bs,A->cmap->bs);
2962: MatSeqAIJSetPreallocation(B,0,NULL);
2963: MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);
2964: }
2965: b = (Mat_MPIAIJ*) B->data;
2967: if (reuse == MAT_REUSE_MATRIX) {
2968: MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_REUSE_MATRIX, &b->A);
2969: MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_REUSE_MATRIX, &b->B);
2970: } else {
2971: MatDestroy(&b->A);
2972: MatDestroy(&b->B);
2973: MatDisAssemble_MPIBAIJ(A);
2974: MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);
2975: MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);
2976: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2977: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2978: }
2979: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2980: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2982: if (reuse == MAT_INPLACE_MATRIX) {
2983: MatHeaderReplace(A,&B);
2984: } else {
2985: *newmat = B;
2986: }
2987: return(0);
2988: }
2990: /*MC
2991: MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
2993: Options Database Keys:
2994: + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
2995: . -mat_block_size <bs> - set the blocksize used to store the matrix
2996: - -mat_use_hash_table <fact>
2998: Level: beginner
3000: .seealso: MatCreateMPIBAIJ
3001: M*/
3003: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);
3004: PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);
3006: PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
3007: {
3008: Mat_MPIBAIJ *b;
3010: PetscBool flg = PETSC_FALSE;
3013: PetscNewLog(B,&b);
3014: B->data = (void*)b;
3016: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
3017: B->assembled = PETSC_FALSE;
3019: B->insertmode = NOT_SET_VALUES;
3020: MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
3021: MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);
3023: /* build local table of row and column ownerships */
3024: PetscMalloc1(b->size+1,&b->rangebs);
3026: /* build cache for off array entries formed */
3027: MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);
3029: b->donotstash = PETSC_FALSE;
3030: b->colmap = NULL;
3031: b->garray = NULL;
3032: b->roworiented = PETSC_TRUE;
3034: /* stuff used in block assembly */
3035: b->barray = 0;
3037: /* stuff used for matrix vector multiply */
3038: b->lvec = 0;
3039: b->Mvctx = 0;
3041: /* stuff for MatGetRow() */
3042: b->rowindices = 0;
3043: b->rowvalues = 0;
3044: b->getrowactive = PETSC_FALSE;
3046: /* hash table stuff */
3047: b->ht = 0;
3048: b->hd = 0;
3049: b->ht_size = 0;
3050: b->ht_flag = PETSC_FALSE;
3051: b->ht_fact = 0;
3052: b->ht_total_ct = 0;
3053: b->ht_insert_ct = 0;
3055: /* stuff for MatCreateSubMatrices_MPIBAIJ_local() */
3056: b->ijonly = PETSC_FALSE;
3059: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",MatConvert_MPIBAIJ_MPIAdj);
3060: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",MatConvert_MPIBAIJ_MPIAIJ);
3061: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",MatConvert_MPIBAIJ_MPISBAIJ);
3062: #if defined(PETSC_HAVE_HYPRE)
3063: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_hypre_C",MatConvert_AIJ_HYPRE);
3064: #endif
3065: PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIBAIJ);
3066: PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIBAIJ);
3067: PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJ);
3068: PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",MatMPIBAIJSetPreallocationCSR_MPIBAIJ);
3069: PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIBAIJ);
3070: PetscObjectComposeFunction((PetscObject)B,"MatSetHashTableFactor_C",MatSetHashTableFactor_MPIBAIJ);
3071: PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_mpibaij_C",MatPtAP_IS_XAIJ);
3072: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_is_C",MatConvert_XAIJ_IS);
3073: PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);
3075: PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");
3076: PetscOptionsName("-mat_use_hash_table","Use hash table to save time in constructing matrix","MatSetOption",&flg);
3077: if (flg) {
3078: PetscReal fact = 1.39;
3079: MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
3080: PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
3081: if (fact <= 1.0) fact = 1.39;
3082: MatMPIBAIJSetHashTableFactor(B,fact);
3083: PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
3084: }
3085: PetscOptionsEnd();
3086: return(0);
3087: }
3089: /*MC
3090: MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3092: This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3093: and MATMPIBAIJ otherwise.
3095: Options Database Keys:
3096: . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3098: Level: beginner
3100: .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3101: M*/
3103: /*@C
3104: MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3105: (block compressed row). For good matrix assembly performance
3106: the user should preallocate the matrix storage by setting the parameters
3107: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
3108: performance can be increased by more than a factor of 50.
3110: Collective on Mat
3112: Input Parameters:
3113: + B - the matrix
3114: . bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3115: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3116: . d_nz - number of block nonzeros per block row in diagonal portion of local
3117: submatrix (same for all local rows)
3118: . d_nnz - array containing the number of block nonzeros in the various block rows
3119: of the in diagonal portion of the local (possibly different for each block
3120: row) or NULL. If you plan to factor the matrix you must leave room for the diagonal entry and
3121: set it even if it is zero.
3122: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
3123: submatrix (same for all local rows).
3124: - o_nnz - array containing the number of nonzeros in the various block rows of the
3125: off-diagonal portion of the local submatrix (possibly different for
3126: each block row) or NULL.
3128: If the *_nnz parameter is given then the *_nz parameter is ignored
3130: Options Database Keys:
3131: + -mat_block_size - size of the blocks to use
3132: - -mat_use_hash_table <fact>
3134: Notes:
3135: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
3136: than it must be used on all processors that share the object for that argument.
3138: Storage Information:
3139: For a square global matrix we define each processor's diagonal portion
3140: to be its local rows and the corresponding columns (a square submatrix);
3141: each processor's off-diagonal portion encompasses the remainder of the
3142: local matrix (a rectangular submatrix).
3144: The user can specify preallocated storage for the diagonal part of
3145: the local submatrix with either d_nz or d_nnz (not both). Set
3146: d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3147: memory allocation. Likewise, specify preallocated storage for the
3148: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3150: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3151: the figure below we depict these three local rows and all columns (0-11).
3153: .vb
3154: 0 1 2 3 4 5 6 7 8 9 10 11
3155: --------------------------
3156: row 3 |o o o d d d o o o o o o
3157: row 4 |o o o d d d o o o o o o
3158: row 5 |o o o d d d o o o o o o
3159: --------------------------
3160: .ve
3162: Thus, any entries in the d locations are stored in the d (diagonal)
3163: submatrix, and any entries in the o locations are stored in the
3164: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3165: stored simply in the MATSEQBAIJ format for compressed row storage.
3167: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3168: and o_nz should indicate the number of block nonzeros per row in the o matrix.
3169: In general, for PDE problems in which most nonzeros are near the diagonal,
3170: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
3171: or you will get TERRIBLE performance; see the users' manual chapter on
3172: matrices.
3174: You can call MatGetInfo() to get information on how effective the preallocation was;
3175: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3176: You can also run with the option -info and look for messages with the string
3177: malloc in them to see if additional memory allocation was needed.
3179: Level: intermediate
3181: .keywords: matrix, block, aij, compressed row, sparse, parallel
3183: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3184: @*/
3185: PetscErrorCode MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3186: {
3193: PetscTryMethod(B,"MatMPIBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
3194: return(0);
3195: }
3197: /*@C
3198: MatCreateBAIJ - Creates a sparse parallel matrix in block AIJ format
3199: (block compressed row). For good matrix assembly performance
3200: the user should preallocate the matrix storage by setting the parameters
3201: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
3202: performance can be increased by more than a factor of 50.
3204: Collective on MPI_Comm
3206: Input Parameters:
3207: + comm - MPI communicator
3208: . bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
3209: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
3210: . m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
3211: This value should be the same as the local size used in creating the
3212: y vector for the matrix-vector product y = Ax.
3213: . n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
3214: This value should be the same as the local size used in creating the
3215: x vector for the matrix-vector product y = Ax.
3216: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3217: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3218: . d_nz - number of nonzero blocks per block row in diagonal portion of local
3219: submatrix (same for all local rows)
3220: . d_nnz - array containing the number of nonzero blocks in the various block rows
3221: of the in diagonal portion of the local (possibly different for each block
3222: row) or NULL. If you plan to factor the matrix you must leave room for the diagonal entry
3223: and set it even if it is zero.
3224: . o_nz - number of nonzero blocks per block row in the off-diagonal portion of local
3225: submatrix (same for all local rows).
3226: - o_nnz - array containing the number of nonzero blocks in the various block rows of the
3227: off-diagonal portion of the local submatrix (possibly different for
3228: each block row) or NULL.
3230: Output Parameter:
3231: . A - the matrix
3233: Options Database Keys:
3234: + -mat_block_size - size of the blocks to use
3235: - -mat_use_hash_table <fact>
3237: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3238: MatXXXXSetPreallocation() paradigm instead of this routine directly.
3239: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3241: Notes:
3242: If the *_nnz parameter is given then the *_nz parameter is ignored
3244: A nonzero block is any block that as 1 or more nonzeros in it
3246: The user MUST specify either the local or global matrix dimensions
3247: (possibly both).
3249: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
3250: than it must be used on all processors that share the object for that argument.
3252: Storage Information:
3253: For a square global matrix we define each processor's diagonal portion
3254: to be its local rows and the corresponding columns (a square submatrix);
3255: each processor's off-diagonal portion encompasses the remainder of the
3256: local matrix (a rectangular submatrix).
3258: The user can specify preallocated storage for the diagonal part of
3259: the local submatrix with either d_nz or d_nnz (not both). Set
3260: d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3261: memory allocation. Likewise, specify preallocated storage for the
3262: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3264: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3265: the figure below we depict these three local rows and all columns (0-11).
3267: .vb
3268: 0 1 2 3 4 5 6 7 8 9 10 11
3269: --------------------------
3270: row 3 |o o o d d d o o o o o o
3271: row 4 |o o o d d d o o o o o o
3272: row 5 |o o o d d d o o o o o o
3273: --------------------------
3274: .ve
3276: Thus, any entries in the d locations are stored in the d (diagonal)
3277: submatrix, and any entries in the o locations are stored in the
3278: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3279: stored simply in the MATSEQBAIJ format for compressed row storage.
3281: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3282: and o_nz should indicate the number of block nonzeros per row in the o matrix.
3283: In general, for PDE problems in which most nonzeros are near the diagonal,
3284: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
3285: or you will get TERRIBLE performance; see the users' manual chapter on
3286: matrices.
3288: Level: intermediate
3290: .keywords: matrix, block, aij, compressed row, sparse, parallel
3292: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3293: @*/
3294: PetscErrorCode MatCreateBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
3295: {
3297: PetscMPIInt size;
3300: MatCreate(comm,A);
3301: MatSetSizes(*A,m,n,M,N);
3302: MPI_Comm_size(comm,&size);
3303: if (size > 1) {
3304: MatSetType(*A,MATMPIBAIJ);
3305: MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
3306: } else {
3307: MatSetType(*A,MATSEQBAIJ);
3308: MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
3309: }
3310: return(0);
3311: }
3313: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
3314: {
3315: Mat mat;
3316: Mat_MPIBAIJ *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
3318: PetscInt len=0;
3321: *newmat = 0;
3322: MatCreate(PetscObjectComm((PetscObject)matin),&mat);
3323: MatSetSizes(mat,matin->rmap->n,matin->cmap->n,matin->rmap->N,matin->cmap->N);
3324: MatSetType(mat,((PetscObject)matin)->type_name);
3326: mat->factortype = matin->factortype;
3327: mat->preallocated = PETSC_TRUE;
3328: mat->assembled = PETSC_TRUE;
3329: mat->insertmode = NOT_SET_VALUES;
3331: a = (Mat_MPIBAIJ*)mat->data;
3332: mat->rmap->bs = matin->rmap->bs;
3333: a->bs2 = oldmat->bs2;
3334: a->mbs = oldmat->mbs;
3335: a->nbs = oldmat->nbs;
3336: a->Mbs = oldmat->Mbs;
3337: a->Nbs = oldmat->Nbs;
3339: PetscLayoutReference(matin->rmap,&mat->rmap);
3340: PetscLayoutReference(matin->cmap,&mat->cmap);
3342: a->size = oldmat->size;
3343: a->rank = oldmat->rank;
3344: a->donotstash = oldmat->donotstash;
3345: a->roworiented = oldmat->roworiented;
3346: a->rowindices = 0;
3347: a->rowvalues = 0;
3348: a->getrowactive = PETSC_FALSE;
3349: a->barray = 0;
3350: a->rstartbs = oldmat->rstartbs;
3351: a->rendbs = oldmat->rendbs;
3352: a->cstartbs = oldmat->cstartbs;
3353: a->cendbs = oldmat->cendbs;
3355: /* hash table stuff */
3356: a->ht = 0;
3357: a->hd = 0;
3358: a->ht_size = 0;
3359: a->ht_flag = oldmat->ht_flag;
3360: a->ht_fact = oldmat->ht_fact;
3361: a->ht_total_ct = 0;
3362: a->ht_insert_ct = 0;
3364: PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));
3365: if (oldmat->colmap) {
3366: #if defined(PETSC_USE_CTABLE)
3367: PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3368: #else
3369: PetscMalloc1(a->Nbs,&a->colmap);
3370: PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
3371: PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
3372: #endif
3373: } else a->colmap = 0;
3375: if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3376: PetscMalloc1(len,&a->garray);
3377: PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
3378: PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
3379: } else a->garray = 0;
3381: MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
3382: VecDuplicate(oldmat->lvec,&a->lvec);
3383: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
3384: VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3385: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);
3387: MatDuplicate(oldmat->A,cpvalues,&a->A);
3388: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
3389: MatDuplicate(oldmat->B,cpvalues,&a->B);
3390: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
3391: PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3392: *newmat = mat;
3393: return(0);
3394: }
3396: PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
3397: {
3399: int fd;
3400: PetscInt i,nz,j,rstart,rend;
3401: PetscScalar *vals,*buf;
3402: MPI_Comm comm;
3403: MPI_Status status;
3404: PetscMPIInt rank,size,maxnz;
3405: PetscInt header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3406: PetscInt *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
3407: PetscInt jj,*mycols,*ibuf,bs = newmat->rmap->bs,Mbs,mbs,extra_rows,mmax;
3408: PetscMPIInt tag = ((PetscObject)viewer)->tag;
3409: PetscInt *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
3410: PetscInt dcount,kmax,k,nzcount,tmp,mend;
3411: PetscBool isbinary;
3414: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
3415: if (!isbinary) SETERRQ2(PetscObjectComm((PetscObject)newmat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newmat)->type_name);
3417: /* force binary viewer to load .info file if it has not yet done so */
3418: PetscViewerSetUp(viewer);
3419: PetscObjectGetComm((PetscObject)viewer,&comm);
3420: PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");
3421: PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3422: PetscOptionsEnd();
3423: if (bs < 0) bs = 1;
3425: MPI_Comm_size(comm,&size);
3426: MPI_Comm_rank(comm,&rank);
3427: PetscViewerBinaryGetDescriptor(viewer,&fd);
3428: if (!rank) {
3429: PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
3430: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3431: if (header[3] < 0) SETERRQ(PetscObjectComm((PetscObject)newmat),PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as MPIAIJ");
3432: }
3433: MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3434: M = header[1]; N = header[2];
3436: /* If global sizes are set, check if they are consistent with that given in the file */
3437: if (newmat->rmap->N >= 0 && newmat->rmap->N != M) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent # of rows:Matrix in file has (%D) and input matrix has (%D)",newmat->rmap->N,M);
3438: if (newmat->cmap->N >= 0 && newmat->cmap->N != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent # of cols:Matrix in file has (%D) and input matrix has (%D)",newmat->cmap->N,N);
3440: if (M != N) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Can only do square matrices");
3442: /*
3443: This code adds extra rows to make sure the number of rows is
3444: divisible by the blocksize
3445: */
3446: Mbs = M/bs;
3447: extra_rows = bs - M + bs*Mbs;
3448: if (extra_rows == bs) extra_rows = 0;
3449: else Mbs++;
3450: if (extra_rows && !rank) {
3451: PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
3452: }
3454: /* determine ownership of all rows */
3455: if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
3456: mbs = Mbs/size + ((Mbs % size) > rank);
3457: m = mbs*bs;
3458: } else { /* User set */
3459: m = newmat->rmap->n;
3460: mbs = m/bs;
3461: }
3462: PetscMalloc2(size+1,&rowners,size+1,&browners);
3463: MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);
3465: /* process 0 needs enough room for process with most rows */
3466: if (!rank) {
3467: mmax = rowners[1];
3468: for (i=2; i<=size; i++) {
3469: mmax = PetscMax(mmax,rowners[i]);
3470: }
3471: mmax*=bs;
3472: } else mmax = -1; /* unused, but compiler warns anyway */
3474: rowners[0] = 0;
3475: for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
3476: for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
3477: rstart = rowners[rank];
3478: rend = rowners[rank+1];
3480: /* distribute row lengths to all processors */
3481: PetscMalloc1(m,&locrowlens);
3482: if (!rank) {
3483: mend = m;
3484: if (size == 1) mend = mend - extra_rows;
3485: PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);
3486: for (j=mend; j<m; j++) locrowlens[j] = 1;
3487: PetscMalloc1(mmax,&rowlengths);
3488: PetscCalloc1(size,&procsnz);
3489: for (j=0; j<m; j++) {
3490: procsnz[0] += locrowlens[j];
3491: }
3492: for (i=1; i<size; i++) {
3493: mend = browners[i+1] - browners[i];
3494: if (i == size-1) mend = mend - extra_rows;
3495: PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);
3496: for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3497: /* calculate the number of nonzeros on each processor */
3498: for (j=0; j<browners[i+1]-browners[i]; j++) {
3499: procsnz[i] += rowlengths[j];
3500: }
3501: MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);
3502: }
3503: PetscFree(rowlengths);
3504: } else {
3505: MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);
3506: }
3508: if (!rank) {
3509: /* determine max buffer needed and allocate it */
3510: maxnz = procsnz[0];
3511: for (i=1; i<size; i++) {
3512: maxnz = PetscMax(maxnz,procsnz[i]);
3513: }
3514: PetscMalloc1(maxnz,&cols);
3516: /* read in my part of the matrix column indices */
3517: nz = procsnz[0];
3518: PetscMalloc1(nz+1,&ibuf);
3519: mycols = ibuf;
3520: if (size == 1) nz -= extra_rows;
3521: PetscBinaryRead(fd,mycols,nz,PETSC_INT);
3522: if (size == 1) {
3523: for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
3524: }
3526: /* read in every ones (except the last) and ship off */
3527: for (i=1; i<size-1; i++) {
3528: nz = procsnz[i];
3529: PetscBinaryRead(fd,cols,nz,PETSC_INT);
3530: MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
3531: }
3532: /* read in the stuff for the last proc */
3533: if (size != 1) {
3534: nz = procsnz[size-1] - extra_rows; /* the extra rows are not on the disk */
3535: PetscBinaryRead(fd,cols,nz,PETSC_INT);
3536: for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3537: MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
3538: }
3539: PetscFree(cols);
3540: } else {
3541: /* determine buffer space needed for message */
3542: nz = 0;
3543: for (i=0; i<m; i++) {
3544: nz += locrowlens[i];
3545: }
3546: PetscMalloc1(nz+1,&ibuf);
3547: mycols = ibuf;
3548: /* receive message of column indices*/
3549: MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
3550: MPI_Get_count(&status,MPIU_INT,&maxnz);
3551: if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3552: }
3554: /* loop over local rows, determining number of off diagonal entries */
3555: PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
3556: PetscCalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
3557: rowcount = 0; nzcount = 0;
3558: for (i=0; i<mbs; i++) {
3559: dcount = 0;
3560: odcount = 0;
3561: for (j=0; j<bs; j++) {
3562: kmax = locrowlens[rowcount];
3563: for (k=0; k<kmax; k++) {
3564: tmp = mycols[nzcount++]/bs;
3565: if (!mask[tmp]) {
3566: mask[tmp] = 1;
3567: if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
3568: else masked1[dcount++] = tmp;
3569: }
3570: }
3571: rowcount++;
3572: }
3574: dlens[i] = dcount;
3575: odlens[i] = odcount;
3577: /* zero out the mask elements we set */
3578: for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
3579: for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
3580: }
3582: MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
3583: MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);
3585: if (!rank) {
3586: PetscMalloc1(maxnz+1,&buf);
3587: /* read in my part of the matrix numerical values */
3588: nz = procsnz[0];
3589: vals = buf;
3590: mycols = ibuf;
3591: if (size == 1) nz -= extra_rows;
3592: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3593: if (size == 1) {
3594: for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
3595: }
3597: /* insert into matrix */
3598: jj = rstart*bs;
3599: for (i=0; i<m; i++) {
3600: MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3601: mycols += locrowlens[i];
3602: vals += locrowlens[i];
3603: jj++;
3604: }
3605: /* read in other processors (except the last one) and ship out */
3606: for (i=1; i<size-1; i++) {
3607: nz = procsnz[i];
3608: vals = buf;
3609: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3610: MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
3611: }
3612: /* the last proc */
3613: if (size != 1) {
3614: nz = procsnz[i] - extra_rows;
3615: vals = buf;
3616: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3617: for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3618: MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
3619: }
3620: PetscFree(procsnz);
3621: } else {
3622: /* receive numeric values */
3623: PetscMalloc1(nz+1,&buf);
3625: /* receive message of values*/
3626: vals = buf;
3627: mycols = ibuf;
3628: MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);
3630: /* insert into matrix */
3631: jj = rstart*bs;
3632: for (i=0; i<m; i++) {
3633: MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3634: mycols += locrowlens[i];
3635: vals += locrowlens[i];
3636: jj++;
3637: }
3638: }
3639: PetscFree(locrowlens);
3640: PetscFree(buf);
3641: PetscFree(ibuf);
3642: PetscFree2(rowners,browners);
3643: PetscFree2(dlens,odlens);
3644: PetscFree3(mask,masked1,masked2);
3645: MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
3646: MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
3647: return(0);
3648: }
3650: /*@
3651: MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3653: Input Parameters:
3654: . mat - the matrix
3655: . fact - factor
3657: Not Collective, each process can use a different factor
3659: Level: advanced
3661: Notes:
3662: This can also be set by the command line option: -mat_use_hash_table <fact>
3664: .keywords: matrix, hashtable, factor, HT
3666: .seealso: MatSetOption()
3667: @*/
3668: PetscErrorCode MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3669: {
3673: PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));
3674: return(0);
3675: }
3677: PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
3678: {
3679: Mat_MPIBAIJ *baij;
3682: baij = (Mat_MPIBAIJ*)mat->data;
3683: baij->ht_fact = fact;
3684: return(0);
3685: }
3687: PetscErrorCode MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
3688: {
3689: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
3690: PetscBool flg;
3694: PetscObjectTypeCompare((PetscObject)A,MATMPIBAIJ,&flg);
3695: if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"This function requires a MATMPIBAIJ matrix as input");
3696: if (Ad) *Ad = a->A;
3697: if (Ao) *Ao = a->B;
3698: if (colmap) *colmap = a->garray;
3699: return(0);
3700: }
3702: /*
3703: Special version for direct calls from Fortran (to eliminate two function call overheads
3704: */
3705: #if defined(PETSC_HAVE_FORTRAN_CAPS)
3706: #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
3707: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3708: #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
3709: #endif
3711: /*@C
3712: MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
3714: Collective on Mat
3716: Input Parameters:
3717: + mat - the matrix
3718: . min - number of input rows
3719: . im - input rows
3720: . nin - number of input columns
3721: . in - input columns
3722: . v - numerical values input
3723: - addvin - INSERT_VALUES or ADD_VALUES
3725: Notes:
3726: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
3728: Level: advanced
3730: .seealso: MatSetValuesBlocked()
3731: @*/
3732: PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
3733: {
3734: /* convert input arguments to C version */
3735: Mat mat = *matin;
3736: PetscInt m = *min, n = *nin;
3737: InsertMode addv = *addvin;
3739: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
3740: const MatScalar *value;
3741: MatScalar *barray = baij->barray;
3742: PetscBool roworiented = baij->roworiented;
3743: PetscErrorCode ierr;
3744: PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs;
3745: PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3746: PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
3749: /* tasks normally handled by MatSetValuesBlocked() */
3750: if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
3751: #if defined(PETSC_USE_DEBUG)
3752: else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3753: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3754: #endif
3755: if (mat->assembled) {
3756: mat->was_assembled = PETSC_TRUE;
3757: mat->assembled = PETSC_FALSE;
3758: }
3759: PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
3762: if (!barray) {
3763: PetscMalloc1(bs2,&barray);
3764: baij->barray = barray;
3765: }
3767: if (roworiented) stepval = (n-1)*bs;
3768: else stepval = (m-1)*bs;
3770: for (i=0; i<m; i++) {
3771: if (im[i] < 0) continue;
3772: #if defined(PETSC_USE_DEBUG)
3773: if (im[i] >= baij->Mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %D max %D",im[i],baij->Mbs-1);
3774: #endif
3775: if (im[i] >= rstart && im[i] < rend) {
3776: row = im[i] - rstart;
3777: for (j=0; j<n; j++) {
3778: /* If NumCol = 1 then a copy is not required */
3779: if ((roworiented) && (n == 1)) {
3780: barray = (MatScalar*)v + i*bs2;
3781: } else if ((!roworiented) && (m == 1)) {
3782: barray = (MatScalar*)v + j*bs2;
3783: } else { /* Here a copy is required */
3784: if (roworiented) {
3785: value = v + i*(stepval+bs)*bs + j*bs;
3786: } else {
3787: value = v + j*(stepval+bs)*bs + i*bs;
3788: }
3789: for (ii=0; ii<bs; ii++,value+=stepval) {
3790: for (jj=0; jj<bs; jj++) {
3791: *barray++ = *value++;
3792: }
3793: }
3794: barray -=bs2;
3795: }
3797: if (in[j] >= cstart && in[j] < cend) {
3798: col = in[j] - cstart;
3799: MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A,row,col,barray,addv,im[i],in[j]);
3800: } else if (in[j] < 0) continue;
3801: #if defined(PETSC_USE_DEBUG)
3802: else if (in[j] >= baij->Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %D max %D",in[j],baij->Nbs-1);
3803: #endif
3804: else {
3805: if (mat->was_assembled) {
3806: if (!baij->colmap) {
3807: MatCreateColmap_MPIBAIJ_Private(mat);
3808: }
3810: #if defined(PETSC_USE_DEBUG)
3811: #if defined(PETSC_USE_CTABLE)
3812: { PetscInt data;
3813: PetscTableFind(baij->colmap,in[j]+1,&data);
3814: if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3815: }
3816: #else
3817: if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3818: #endif
3819: #endif
3820: #if defined(PETSC_USE_CTABLE)
3821: PetscTableFind(baij->colmap,in[j]+1,&col);
3822: col = (col - 1)/bs;
3823: #else
3824: col = (baij->colmap[in[j]] - 1)/bs;
3825: #endif
3826: if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
3827: MatDisAssemble_MPIBAIJ(mat);
3828: col = in[j];
3829: }
3830: } else col = in[j];
3831: MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B,row,col,barray,addv,im[i],in[j]);
3832: }
3833: }
3834: } else {
3835: if (!baij->donotstash) {
3836: if (roworiented) {
3837: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3838: } else {
3839: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3840: }
3841: }
3842: }
3843: }
3845: /* task normally handled by MatSetValuesBlocked() */
3846: PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
3847: return(0);
3848: }
3850: /*@
3851: MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard block
3852: CSR format the local rows.
3854: Collective on MPI_Comm
3856: Input Parameters:
3857: + comm - MPI communicator
3858: . bs - the block size, only a block size of 1 is supported
3859: . m - number of local rows (Cannot be PETSC_DECIDE)
3860: . n - This value should be the same as the local size used in creating the
3861: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3862: calculated if N is given) For square matrices n is almost always m.
3863: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3864: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3865: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of block elements in that rowth block row of the matrix
3866: . j - column indices
3867: - a - matrix values
3869: Output Parameter:
3870: . mat - the matrix
3872: Level: intermediate
3874: Notes:
3875: The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
3876: thus you CANNOT change the matrix entries by changing the values of a[] after you have
3877: called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
3879: The order of the entries in values is the same as the block compressed sparse row storage format; that is, it is
3880: the same as a three dimensional array in Fortran values(bs,bs,nnz) that contains the first column of the first
3881: block, followed by the second column of the first block etc etc. That is, the blocks are contiguous in memory
3882: with column-major ordering within blocks.
3884: The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
3886: .keywords: matrix, aij, compressed row, sparse, parallel
3888: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
3889: MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3890: @*/
3891: PetscErrorCode MatCreateMPIBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
3892: {
3896: if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3897: if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3898: MatCreate(comm,mat);
3899: MatSetSizes(*mat,m,n,M,N);
3900: MatSetType(*mat,MATMPIBAIJ);
3901: MatSetBlockSize(*mat,bs);
3902: MatSetUp(*mat);
3903: MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_FALSE);
3904: MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);
3905: MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_TRUE);
3906: return(0);
3907: }
3909: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
3910: {
3912: PetscInt m,N,i,rstart,nnz,Ii,bs,cbs;
3913: PetscInt *indx;
3914: PetscScalar *values;
3917: MatGetSize(inmat,&m,&N);
3918: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
3919: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)inmat->data;
3920: PetscInt *dnz,*onz,mbs,Nbs,nbs;
3921: PetscInt *bindx,rmax=a->rmax,j;
3922: PetscMPIInt rank,size;
3924: MatGetBlockSizes(inmat,&bs,&cbs);
3925: mbs = m/bs; Nbs = N/cbs;
3926: if (n == PETSC_DECIDE) {
3927: nbs = n;
3928: PetscSplitOwnership(comm,&nbs,&Nbs);
3929: n = nbs*cbs;
3930: } else {
3931: nbs = n/cbs;
3932: }
3934: PetscMalloc1(rmax,&bindx);
3935: MatPreallocateInitialize(comm,mbs,nbs,dnz,onz); /* inline function, output __end and __rstart are used below */
3937: MPI_Comm_rank(comm,&rank);
3938: MPI_Comm_rank(comm,&size);
3939: if (rank == size-1) {
3940: /* Check sum(nbs) = Nbs */
3941: if (__end != Nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Sum of local block columns %D != global block columns %D",__end,Nbs);
3942: }
3944: rstart = __rstart; /* block rstart of *outmat; see inline function MatPreallocateInitialize */
3945: for (i=0; i<mbs; i++) {
3946: MatGetRow_SeqBAIJ(inmat,i*bs,&nnz,&indx,NULL); /* non-blocked nnz and indx */
3947: nnz = nnz/bs;
3948: for (j=0; j<nnz; j++) bindx[j] = indx[j*bs]/bs;
3949: MatPreallocateSet(i+rstart,nnz,bindx,dnz,onz);
3950: MatRestoreRow_SeqBAIJ(inmat,i*bs,&nnz,&indx,NULL);
3951: }
3952: PetscFree(bindx);
3954: MatCreate(comm,outmat);
3955: MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3956: MatSetBlockSizes(*outmat,bs,cbs);
3957: MatSetType(*outmat,MATBAIJ);
3958: MatSeqBAIJSetPreallocation(*outmat,bs,0,dnz);
3959: MatMPIBAIJSetPreallocation(*outmat,bs,0,dnz,0,onz);
3960: MatPreallocateFinalize(dnz,onz);
3961: }
3963: /* numeric phase */
3964: MatGetBlockSizes(inmat,&bs,&cbs);
3965: MatGetOwnershipRange(*outmat,&rstart,NULL);
3967: for (i=0; i<m; i++) {
3968: MatGetRow_SeqBAIJ(inmat,i,&nnz,&indx,&values);
3969: Ii = i + rstart;
3970: MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
3971: MatRestoreRow_SeqBAIJ(inmat,i,&nnz,&indx,&values);
3972: }
3973: MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
3974: MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
3975: return(0);
3976: }