Actual source code: mpibaij.c
petsc-3.5.2 2014-09-08
2: #include <../src/mat/impls/baij/mpi/mpibaij.h> /*I "petscmat.h" I*/
3: #include <petscblaslapack.h>
4: #include <petscsf.h>
6: extern PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat);
7: extern PetscErrorCode MatDisAssemble_MPIBAIJ(Mat);
8: extern PetscErrorCode MatGetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],PetscScalar []);
9: extern PetscErrorCode MatSetValues_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt [],const PetscScalar [],InsertMode);
10: extern PetscErrorCode MatSetValuesBlocked_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[],const PetscScalar[],InsertMode);
11: extern PetscErrorCode MatGetRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
12: extern PetscErrorCode MatRestoreRow_SeqBAIJ(Mat,PetscInt,PetscInt*,PetscInt*[],PetscScalar*[]);
13: extern PetscErrorCode MatZeroRows_SeqBAIJ(Mat,PetscInt,const PetscInt[],PetscScalar,Vec,Vec);
17: PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A,Vec v,PetscInt idx[])
18: {
19: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
21: PetscInt i,*idxb = 0;
22: PetscScalar *va,*vb;
23: Vec vtmp;
26: MatGetRowMaxAbs(a->A,v,idx);
27: VecGetArray(v,&va);
28: if (idx) {
29: for (i=0; i<A->rmap->n; i++) {
30: if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
31: }
32: }
34: VecCreateSeq(PETSC_COMM_SELF,A->rmap->n,&vtmp);
35: if (idx) {PetscMalloc1(A->rmap->n,&idxb);}
36: MatGetRowMaxAbs(a->B,vtmp,idxb);
37: VecGetArray(vtmp,&vb);
39: for (i=0; i<A->rmap->n; i++) {
40: if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
41: va[i] = vb[i];
42: if (idx) idx[i] = A->cmap->bs*a->garray[idxb[i]/A->cmap->bs] + (idxb[i] % A->cmap->bs);
43: }
44: }
46: VecRestoreArray(v,&va);
47: VecRestoreArray(vtmp,&vb);
48: PetscFree(idxb);
49: VecDestroy(&vtmp);
50: return(0);
51: }
55: PetscErrorCode MatStoreValues_MPIBAIJ(Mat mat)
56: {
57: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)mat->data;
61: MatStoreValues(aij->A);
62: MatStoreValues(aij->B);
63: return(0);
64: }
68: PetscErrorCode MatRetrieveValues_MPIBAIJ(Mat mat)
69: {
70: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)mat->data;
74: MatRetrieveValues(aij->A);
75: MatRetrieveValues(aij->B);
76: return(0);
77: }
79: /*
80: Local utility routine that creates a mapping from the global column
81: number to the local number in the off-diagonal part of the local
82: storage of the matrix. This is done in a non scalable way since the
83: length of colmap equals the global matrix length.
84: */
87: PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
88: {
89: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
90: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data;
92: PetscInt nbs = B->nbs,i,bs=mat->rmap->bs;
95: #if defined(PETSC_USE_CTABLE)
96: PetscTableCreate(baij->nbs,baij->Nbs+1,&baij->colmap);
97: for (i=0; i<nbs; i++) {
98: PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1,INSERT_VALUES);
99: }
100: #else
101: PetscMalloc1((baij->Nbs+1),&baij->colmap);
102: PetscLogObjectMemory((PetscObject)mat,baij->Nbs*sizeof(PetscInt));
103: PetscMemzero(baij->colmap,baij->Nbs*sizeof(PetscInt));
104: for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
105: #endif
106: return(0);
107: }
109: #define MatSetValues_SeqBAIJ_A_Private(row,col,value,addv) \
110: { \
111: \
112: brow = row/bs; \
113: rp = aj + ai[brow]; ap = aa + bs2*ai[brow]; \
114: rmax = aimax[brow]; nrow = ailen[brow]; \
115: bcol = col/bs; \
116: ridx = row % bs; cidx = col % bs; \
117: low = 0; high = nrow; \
118: while (high-low > 3) { \
119: t = (low+high)/2; \
120: if (rp[t] > bcol) high = t; \
121: else low = t; \
122: } \
123: for (_i=low; _i<high; _i++) { \
124: if (rp[_i] > bcol) break; \
125: if (rp[_i] == bcol) { \
126: bap = ap + bs2*_i + bs*cidx + ridx; \
127: if (addv == ADD_VALUES) *bap += value; \
128: else *bap = value; \
129: goto a_noinsert; \
130: } \
131: } \
132: if (a->nonew == 1) goto a_noinsert; \
133: if (a->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
134: MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,aimax,a->nonew,MatScalar); \
135: N = nrow++ - 1; \
136: /* shift up all the later entries in this row */ \
137: for (ii=N; ii>=_i; ii--) { \
138: rp[ii+1] = rp[ii]; \
139: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
140: } \
141: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); } \
142: rp[_i] = bcol; \
143: ap[bs2*_i + bs*cidx + ridx] = value; \
144: a_noinsert:; \
145: ailen[brow] = nrow; \
146: }
148: #define MatSetValues_SeqBAIJ_B_Private(row,col,value,addv) \
149: { \
150: brow = row/bs; \
151: rp = bj + bi[brow]; ap = ba + bs2*bi[brow]; \
152: rmax = bimax[brow]; nrow = bilen[brow]; \
153: bcol = col/bs; \
154: ridx = row % bs; cidx = col % bs; \
155: low = 0; high = nrow; \
156: while (high-low > 3) { \
157: t = (low+high)/2; \
158: if (rp[t] > bcol) high = t; \
159: else low = t; \
160: } \
161: for (_i=low; _i<high; _i++) { \
162: if (rp[_i] > bcol) break; \
163: if (rp[_i] == bcol) { \
164: bap = ap + bs2*_i + bs*cidx + ridx; \
165: if (addv == ADD_VALUES) *bap += value; \
166: else *bap = value; \
167: goto b_noinsert; \
168: } \
169: } \
170: if (b->nonew == 1) goto b_noinsert; \
171: if (b->nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
172: MatSeqXAIJReallocateAIJ(B,b->mbs,bs2,nrow,brow,bcol,rmax,ba,bi,bj,rp,ap,bimax,b->nonew,MatScalar); \
173: N = nrow++ - 1; \
174: /* shift up all the later entries in this row */ \
175: for (ii=N; ii>=_i; ii--) { \
176: rp[ii+1] = rp[ii]; \
177: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar)); \
178: } \
179: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));} \
180: rp[_i] = bcol; \
181: ap[bs2*_i + bs*cidx + ridx] = value; \
182: b_noinsert:; \
183: bilen[brow] = nrow; \
184: }
188: PetscErrorCode MatSetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
189: {
190: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
191: MatScalar value;
192: PetscBool roworiented = baij->roworiented;
194: PetscInt i,j,row,col;
195: PetscInt rstart_orig=mat->rmap->rstart;
196: PetscInt rend_orig =mat->rmap->rend,cstart_orig=mat->cmap->rstart;
197: PetscInt cend_orig =mat->cmap->rend,bs=mat->rmap->bs;
199: /* Some Variables required in the macro */
200: Mat A = baij->A;
201: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)(A)->data;
202: PetscInt *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
203: MatScalar *aa =a->a;
205: Mat B = baij->B;
206: Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(B)->data;
207: PetscInt *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
208: MatScalar *ba =b->a;
210: PetscInt *rp,ii,nrow,_i,rmax,N,brow,bcol;
211: PetscInt low,high,t,ridx,cidx,bs2=a->bs2;
212: MatScalar *ap,*bap;
215: for (i=0; i<m; i++) {
216: if (im[i] < 0) continue;
217: #if defined(PETSC_USE_DEBUG)
218: 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);
219: #endif
220: if (im[i] >= rstart_orig && im[i] < rend_orig) {
221: row = im[i] - rstart_orig;
222: for (j=0; j<n; j++) {
223: if (in[j] >= cstart_orig && in[j] < cend_orig) {
224: col = in[j] - cstart_orig;
225: if (roworiented) value = v[i*n+j];
226: else value = v[i+j*m];
227: MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
228: /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
229: } else if (in[j] < 0) continue;
230: #if defined(PETSC_USE_DEBUG)
231: 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);
232: #endif
233: else {
234: if (mat->was_assembled) {
235: if (!baij->colmap) {
236: MatCreateColmap_MPIBAIJ_Private(mat);
237: }
238: #if defined(PETSC_USE_CTABLE)
239: PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
240: col = col - 1;
241: #else
242: col = baij->colmap[in[j]/bs] - 1;
243: #endif
244: if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
245: MatDisAssemble_MPIBAIJ(mat);
246: col = in[j];
247: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
248: B = baij->B;
249: b = (Mat_SeqBAIJ*)(B)->data;
250: bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
251: ba =b->a;
252: } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", im[i], in[j]);
253: else col += in[j]%bs;
254: } else col = in[j];
255: if (roworiented) value = v[i*n+j];
256: else value = v[i+j*m];
257: MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
258: /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
259: }
260: }
261: } else {
262: 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]);
263: if (!baij->donotstash) {
264: mat->assembled = PETSC_FALSE;
265: if (roworiented) {
266: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
267: } else {
268: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
269: }
270: }
271: }
272: }
273: return(0);
274: }
278: PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
279: {
280: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
281: const PetscScalar *value;
282: MatScalar *barray = baij->barray;
283: PetscBool roworiented = baij->roworiented;
284: PetscErrorCode ierr;
285: PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs;
286: PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval;
287: PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
290: if (!barray) {
291: PetscMalloc1(bs2,&barray);
292: baij->barray = barray;
293: }
295: if (roworiented) stepval = (n-1)*bs;
296: else stepval = (m-1)*bs;
298: for (i=0; i<m; i++) {
299: if (im[i] < 0) continue;
300: #if defined(PETSC_USE_DEBUG)
301: 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);
302: #endif
303: if (im[i] >= rstart && im[i] < rend) {
304: row = im[i] - rstart;
305: for (j=0; j<n; j++) {
306: /* If NumCol = 1 then a copy is not required */
307: if ((roworiented) && (n == 1)) {
308: barray = (MatScalar*)v + i*bs2;
309: } else if ((!roworiented) && (m == 1)) {
310: barray = (MatScalar*)v + j*bs2;
311: } else { /* Here a copy is required */
312: if (roworiented) {
313: value = v + (i*(stepval+bs) + j)*bs;
314: } else {
315: value = v + (j*(stepval+bs) + i)*bs;
316: }
317: for (ii=0; ii<bs; ii++,value+=bs+stepval) {
318: for (jj=0; jj<bs; jj++) barray[jj] = value[jj];
319: barray += bs;
320: }
321: barray -= bs2;
322: }
324: if (in[j] >= cstart && in[j] < cend) {
325: col = in[j] - cstart;
326: MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);
327: } else if (in[j] < 0) continue;
328: #if defined(PETSC_USE_DEBUG)
329: 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);
330: #endif
331: else {
332: if (mat->was_assembled) {
333: if (!baij->colmap) {
334: MatCreateColmap_MPIBAIJ_Private(mat);
335: }
337: #if defined(PETSC_USE_DEBUG)
338: #if defined(PETSC_USE_CTABLE)
339: { PetscInt data;
340: PetscTableFind(baij->colmap,in[j]+1,&data);
341: if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
342: }
343: #else
344: if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
345: #endif
346: #endif
347: #if defined(PETSC_USE_CTABLE)
348: PetscTableFind(baij->colmap,in[j]+1,&col);
349: col = (col - 1)/bs;
350: #else
351: col = (baij->colmap[in[j]] - 1)/bs;
352: #endif
353: if (col < 0 && !((Mat_SeqBAIJ*)(baij->B->data))->nonew) {
354: MatDisAssemble_MPIBAIJ(mat);
355: col = in[j];
356: } else if (col < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", bs*im[i], bs*in[j]);
357: } else col = in[j];
358: MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
359: }
360: }
361: } else {
362: 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]);
363: if (!baij->donotstash) {
364: if (roworiented) {
365: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
366: } else {
367: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
368: }
369: }
370: }
371: }
372: return(0);
373: }
375: #define HASH_KEY 0.6180339887
376: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(PetscInt)((size)*(tmp-(PetscInt)tmp)))
377: /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
378: /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
381: PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
382: {
383: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
384: PetscBool roworiented = baij->roworiented;
386: PetscInt i,j,row,col;
387: PetscInt rstart_orig=mat->rmap->rstart;
388: PetscInt rend_orig =mat->rmap->rend,Nbs=baij->Nbs;
389: PetscInt h1,key,size=baij->ht_size,bs=mat->rmap->bs,*HT=baij->ht,idx;
390: PetscReal tmp;
391: MatScalar **HD = baij->hd,value;
392: #if defined(PETSC_USE_DEBUG)
393: PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
394: #endif
397: for (i=0; i<m; i++) {
398: #if defined(PETSC_USE_DEBUG)
399: if (im[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
400: 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);
401: #endif
402: row = im[i];
403: if (row >= rstart_orig && row < rend_orig) {
404: for (j=0; j<n; j++) {
405: col = in[j];
406: if (roworiented) value = v[i*n+j];
407: else value = v[i+j*m];
408: /* Look up PetscInto the Hash Table */
409: key = (row/bs)*Nbs+(col/bs)+1;
410: h1 = HASH(size,key,tmp);
413: idx = h1;
414: #if defined(PETSC_USE_DEBUG)
415: insert_ct++;
416: total_ct++;
417: if (HT[idx] != key) {
418: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
419: if (idx == size) {
420: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
421: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
422: }
423: }
424: #else
425: if (HT[idx] != key) {
426: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
427: if (idx == size) {
428: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
429: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
430: }
431: }
432: #endif
433: /* A HASH table entry is found, so insert the values at the correct address */
434: if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
435: else *(HD[idx]+ (col % bs)*bs + (row % bs)) = value;
436: }
437: } else if (!baij->donotstash) {
438: if (roworiented) {
439: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n,PETSC_FALSE);
440: } else {
441: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m,PETSC_FALSE);
442: }
443: }
444: }
445: #if defined(PETSC_USE_DEBUG)
446: baij->ht_total_ct = total_ct;
447: baij->ht_insert_ct = insert_ct;
448: #endif
449: return(0);
450: }
454: PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
455: {
456: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
457: PetscBool roworiented = baij->roworiented;
458: PetscErrorCode ierr;
459: PetscInt i,j,ii,jj,row,col;
460: PetscInt rstart=baij->rstartbs;
461: PetscInt rend =mat->rmap->rend,stepval,bs=mat->rmap->bs,bs2=baij->bs2,nbs2=n*bs2;
462: PetscInt h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
463: PetscReal tmp;
464: MatScalar **HD = baij->hd,*baij_a;
465: const PetscScalar *v_t,*value;
466: #if defined(PETSC_USE_DEBUG)
467: PetscInt total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
468: #endif
471: if (roworiented) stepval = (n-1)*bs;
472: else stepval = (m-1)*bs;
474: for (i=0; i<m; i++) {
475: #if defined(PETSC_USE_DEBUG)
476: if (im[i] < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",im[i]);
477: 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);
478: #endif
479: row = im[i];
480: v_t = v + i*nbs2;
481: if (row >= rstart && row < rend) {
482: for (j=0; j<n; j++) {
483: col = in[j];
485: /* Look up into the Hash Table */
486: key = row*Nbs+col+1;
487: h1 = HASH(size,key,tmp);
489: idx = h1;
490: #if defined(PETSC_USE_DEBUG)
491: total_ct++;
492: insert_ct++;
493: if (HT[idx] != key) {
494: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++) ;
495: if (idx == size) {
496: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++) ;
497: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
498: }
499: }
500: #else
501: if (HT[idx] != key) {
502: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++) ;
503: if (idx == size) {
504: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++) ;
505: if (idx == h1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"(%D,%D) has no entry in the hash table", row, col);
506: }
507: }
508: #endif
509: baij_a = HD[idx];
510: if (roworiented) {
511: /*value = v + i*(stepval+bs)*bs + j*bs;*/
512: /* value = v + (i*(stepval+bs)+j)*bs; */
513: value = v_t;
514: v_t += bs;
515: if (addv == ADD_VALUES) {
516: for (ii=0; ii<bs; ii++,value+=stepval) {
517: for (jj=ii; jj<bs2; jj+=bs) {
518: baij_a[jj] += *value++;
519: }
520: }
521: } else {
522: for (ii=0; ii<bs; ii++,value+=stepval) {
523: for (jj=ii; jj<bs2; jj+=bs) {
524: baij_a[jj] = *value++;
525: }
526: }
527: }
528: } else {
529: value = v + j*(stepval+bs)*bs + i*bs;
530: if (addv == ADD_VALUES) {
531: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
532: for (jj=0; jj<bs; jj++) {
533: baij_a[jj] += *value++;
534: }
535: }
536: } else {
537: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
538: for (jj=0; jj<bs; jj++) {
539: baij_a[jj] = *value++;
540: }
541: }
542: }
543: }
544: }
545: } else {
546: if (!baij->donotstash) {
547: if (roworiented) {
548: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
549: } else {
550: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
551: }
552: }
553: }
554: }
555: #if defined(PETSC_USE_DEBUG)
556: baij->ht_total_ct = total_ct;
557: baij->ht_insert_ct = insert_ct;
558: #endif
559: return(0);
560: }
564: PetscErrorCode MatGetValues_MPIBAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
565: {
566: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
568: PetscInt bs = mat->rmap->bs,i,j,bsrstart = mat->rmap->rstart,bsrend = mat->rmap->rend;
569: PetscInt bscstart = mat->cmap->rstart,bscend = mat->cmap->rend,row,col,data;
572: for (i=0; i<m; i++) {
573: if (idxm[i] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);*/
574: 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);
575: if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
576: row = idxm[i] - bsrstart;
577: for (j=0; j<n; j++) {
578: if (idxn[j] < 0) continue; /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]); */
579: 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);
580: if (idxn[j] >= bscstart && idxn[j] < bscend) {
581: col = idxn[j] - bscstart;
582: MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
583: } else {
584: if (!baij->colmap) {
585: MatCreateColmap_MPIBAIJ_Private(mat);
586: }
587: #if defined(PETSC_USE_CTABLE)
588: PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
589: data--;
590: #else
591: data = baij->colmap[idxn[j]/bs]-1;
592: #endif
593: if ((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
594: else {
595: col = data + idxn[j]%bs;
596: MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
597: }
598: }
599: }
600: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local values currently supported");
601: }
602: return(0);
603: }
607: PetscErrorCode MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *nrm)
608: {
609: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
610: Mat_SeqBAIJ *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
612: PetscInt i,j,bs2=baij->bs2,bs=baij->A->rmap->bs,nz,row,col;
613: PetscReal sum = 0.0;
614: MatScalar *v;
617: if (baij->size == 1) {
618: MatNorm(baij->A,type,nrm);
619: } else {
620: if (type == NORM_FROBENIUS) {
621: v = amat->a;
622: nz = amat->nz*bs2;
623: for (i=0; i<nz; i++) {
624: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
625: }
626: v = bmat->a;
627: nz = bmat->nz*bs2;
628: for (i=0; i<nz; i++) {
629: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
630: }
631: MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
632: *nrm = PetscSqrtReal(*nrm);
633: } else if (type == NORM_1) { /* max column sum */
634: PetscReal *tmp,*tmp2;
635: PetscInt *jj,*garray=baij->garray,cstart=baij->rstartbs;
636: PetscMalloc2(mat->cmap->N,&tmp,mat->cmap->N,&tmp2);
637: PetscMemzero(tmp,mat->cmap->N*sizeof(PetscReal));
638: v = amat->a; jj = amat->j;
639: for (i=0; i<amat->nz; i++) {
640: for (j=0; j<bs; j++) {
641: col = bs*(cstart + *jj) + j; /* column index */
642: for (row=0; row<bs; row++) {
643: tmp[col] += PetscAbsScalar(*v); v++;
644: }
645: }
646: jj++;
647: }
648: v = bmat->a; jj = bmat->j;
649: for (i=0; i<bmat->nz; i++) {
650: for (j=0; j<bs; j++) {
651: col = bs*garray[*jj] + j;
652: for (row=0; row<bs; row++) {
653: tmp[col] += PetscAbsScalar(*v); v++;
654: }
655: }
656: jj++;
657: }
658: MPI_Allreduce(tmp,tmp2,mat->cmap->N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)mat));
659: *nrm = 0.0;
660: for (j=0; j<mat->cmap->N; j++) {
661: if (tmp2[j] > *nrm) *nrm = tmp2[j];
662: }
663: PetscFree2(tmp,tmp2);
664: } else if (type == NORM_INFINITY) { /* max row sum */
665: PetscReal *sums;
666: PetscMalloc1(bs,&sums);
667: sum = 0.0;
668: for (j=0; j<amat->mbs; j++) {
669: for (row=0; row<bs; row++) sums[row] = 0.0;
670: v = amat->a + bs2*amat->i[j];
671: nz = amat->i[j+1]-amat->i[j];
672: for (i=0; i<nz; i++) {
673: for (col=0; col<bs; col++) {
674: for (row=0; row<bs; row++) {
675: sums[row] += PetscAbsScalar(*v); v++;
676: }
677: }
678: }
679: v = bmat->a + bs2*bmat->i[j];
680: nz = bmat->i[j+1]-bmat->i[j];
681: for (i=0; i<nz; i++) {
682: for (col=0; col<bs; col++) {
683: for (row=0; row<bs; row++) {
684: sums[row] += PetscAbsScalar(*v); v++;
685: }
686: }
687: }
688: for (row=0; row<bs; row++) {
689: if (sums[row] > sum) sum = sums[row];
690: }
691: }
692: MPI_Allreduce(&sum,nrm,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)mat));
693: PetscFree(sums);
694: } else SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"No support for this norm yet");
695: }
696: return(0);
697: }
699: /*
700: Creates the hash table, and sets the table
701: This table is created only once.
702: If new entried need to be added to the matrix
703: then the hash table has to be destroyed and
704: recreated.
705: */
708: PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
709: {
710: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
711: Mat A = baij->A,B=baij->B;
712: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data,*b=(Mat_SeqBAIJ*)B->data;
713: PetscInt i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
715: PetscInt ht_size,bs2=baij->bs2,rstart=baij->rstartbs;
716: PetscInt cstart=baij->cstartbs,*garray=baij->garray,row,col,Nbs=baij->Nbs;
717: PetscInt *HT,key;
718: MatScalar **HD;
719: PetscReal tmp;
720: #if defined(PETSC_USE_INFO)
721: PetscInt ct=0,max=0;
722: #endif
725: if (baij->ht) return(0);
727: baij->ht_size = (PetscInt)(factor*nz);
728: ht_size = baij->ht_size;
730: /* Allocate Memory for Hash Table */
731: PetscCalloc2(ht_size,&baij->hd,ht_size,&baij->ht);
732: HD = baij->hd;
733: HT = baij->ht;
735: /* Loop Over A */
736: for (i=0; i<a->mbs; i++) {
737: for (j=ai[i]; j<ai[i+1]; j++) {
738: row = i+rstart;
739: col = aj[j]+cstart;
741: key = row*Nbs + col + 1;
742: h1 = HASH(ht_size,key,tmp);
743: for (k=0; k<ht_size; k++) {
744: if (!HT[(h1+k)%ht_size]) {
745: HT[(h1+k)%ht_size] = key;
746: HD[(h1+k)%ht_size] = a->a + j*bs2;
747: break;
748: #if defined(PETSC_USE_INFO)
749: } else {
750: ct++;
751: #endif
752: }
753: }
754: #if defined(PETSC_USE_INFO)
755: if (k> max) max = k;
756: #endif
757: }
758: }
759: /* Loop Over B */
760: for (i=0; i<b->mbs; i++) {
761: for (j=bi[i]; j<bi[i+1]; j++) {
762: row = i+rstart;
763: col = garray[bj[j]];
764: key = row*Nbs + col + 1;
765: h1 = HASH(ht_size,key,tmp);
766: for (k=0; k<ht_size; k++) {
767: if (!HT[(h1+k)%ht_size]) {
768: HT[(h1+k)%ht_size] = key;
769: HD[(h1+k)%ht_size] = b->a + j*bs2;
770: break;
771: #if defined(PETSC_USE_INFO)
772: } else {
773: ct++;
774: #endif
775: }
776: }
777: #if defined(PETSC_USE_INFO)
778: if (k> max) max = k;
779: #endif
780: }
781: }
783: /* Print Summary */
784: #if defined(PETSC_USE_INFO)
785: for (i=0,j=0; i<ht_size; i++) {
786: if (HT[i]) j++;
787: }
788: PetscInfo2(mat,"Average Search = %5.2f,max search = %D\n",(!j)? 0.0:((PetscReal)(ct+j))/j,max);
789: #endif
790: return(0);
791: }
795: PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
796: {
797: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
799: PetscInt nstash,reallocs;
800: InsertMode addv;
803: if (baij->donotstash || mat->nooffprocentries) return(0);
805: /* make sure all processors are either in INSERTMODE or ADDMODE */
806: MPI_Allreduce((PetscEnum*)&mat->insertmode,(PetscEnum*)&addv,1,MPIU_ENUM,MPI_BOR,PetscObjectComm((PetscObject)mat));
807: if (addv == (ADD_VALUES|INSERT_VALUES)) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
808: mat->insertmode = addv; /* in case this processor had no cache */
810: MatStashScatterBegin_Private(mat,&mat->stash,mat->rmap->range);
811: MatStashScatterBegin_Private(mat,&mat->bstash,baij->rangebs);
812: MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
813: PetscInfo2(mat,"Stash has %D entries,uses %D mallocs.\n",nstash,reallocs);
814: MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);
815: PetscInfo2(mat,"Block-Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
816: return(0);
817: }
821: PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
822: {
823: Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data;
824: Mat_SeqBAIJ *a =(Mat_SeqBAIJ*)baij->A->data;
826: PetscInt i,j,rstart,ncols,flg,bs2=baij->bs2;
827: PetscInt *row,*col;
828: PetscBool r1,r2,r3,other_disassembled;
829: MatScalar *val;
830: InsertMode addv = mat->insertmode;
831: PetscMPIInt n;
834: /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
835: if (!baij->donotstash && !mat->nooffprocentries) {
836: while (1) {
837: MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
838: if (!flg) break;
840: for (i=0; i<n;) {
841: /* Now identify the consecutive vals belonging to the same row */
842: for (j=i,rstart=row[j]; j<n; j++) {
843: if (row[j] != rstart) break;
844: }
845: if (j < n) ncols = j-i;
846: else ncols = n-i;
847: /* Now assemble all these values with a single function call */
848: MatSetValues_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
849: i = j;
850: }
851: }
852: MatStashScatterEnd_Private(&mat->stash);
853: /* Now process the block-stash. Since the values are stashed column-oriented,
854: set the roworiented flag to column oriented, and after MatSetValues()
855: restore the original flags */
856: r1 = baij->roworiented;
857: r2 = a->roworiented;
858: r3 = ((Mat_SeqBAIJ*)baij->B->data)->roworiented;
860: baij->roworiented = PETSC_FALSE;
861: a->roworiented = PETSC_FALSE;
863: (((Mat_SeqBAIJ*)baij->B->data))->roworiented = PETSC_FALSE; /* b->roworiented */
864: while (1) {
865: MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
866: if (!flg) break;
868: for (i=0; i<n;) {
869: /* Now identify the consecutive vals belonging to the same row */
870: for (j=i,rstart=row[j]; j<n; j++) {
871: if (row[j] != rstart) break;
872: }
873: if (j < n) ncols = j-i;
874: else ncols = n-i;
875: MatSetValuesBlocked_MPIBAIJ(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
876: i = j;
877: }
878: }
879: MatStashScatterEnd_Private(&mat->bstash);
881: baij->roworiented = r1;
882: a->roworiented = r2;
884: ((Mat_SeqBAIJ*)baij->B->data)->roworiented = r3; /* b->roworiented */
885: }
887: MatAssemblyBegin(baij->A,mode);
888: MatAssemblyEnd(baij->A,mode);
890: /* determine if any processor has disassembled, if so we must
891: also disassemble ourselfs, in order that we may reassemble. */
892: /*
893: if nonzero structure of submatrix B cannot change then we know that
894: no processor disassembled thus we can skip this stuff
895: */
896: if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
897: MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPIU_BOOL,MPI_PROD,PetscObjectComm((PetscObject)mat));
898: if (mat->was_assembled && !other_disassembled) {
899: MatDisAssemble_MPIBAIJ(mat);
900: }
901: }
903: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
904: MatSetUpMultiply_MPIBAIJ(mat);
905: }
906: MatAssemblyBegin(baij->B,mode);
907: MatAssemblyEnd(baij->B,mode);
909: #if defined(PETSC_USE_INFO)
910: if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
911: PetscInfo1(mat,"Average Hash Table Search in MatSetValues = %5.2f\n",((PetscReal)baij->ht_total_ct)/baij->ht_insert_ct);
913: baij->ht_total_ct = 0;
914: baij->ht_insert_ct = 0;
915: }
916: #endif
917: if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
918: MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);
920: mat->ops->setvalues = MatSetValues_MPIBAIJ_HT;
921: mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
922: }
924: PetscFree2(baij->rowvalues,baij->rowindices);
926: baij->rowvalues = 0;
928: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
929: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
930: PetscObjectState state = baij->A->nonzerostate + baij->B->nonzerostate;
931: MPI_Allreduce(&state,&mat->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)mat));
932: }
933: return(0);
934: }
936: extern PetscErrorCode MatView_SeqBAIJ(Mat,PetscViewer);
937: #include <petscdraw.h>
940: static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
941: {
942: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
943: PetscErrorCode ierr;
944: PetscMPIInt rank = baij->rank;
945: PetscInt bs = mat->rmap->bs;
946: PetscBool iascii,isdraw;
947: PetscViewer sviewer;
948: PetscViewerFormat format;
951: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
952: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
953: if (iascii) {
954: PetscViewerGetFormat(viewer,&format);
955: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
956: MatInfo info;
957: MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
958: MatGetInfo(mat,MAT_LOCAL,&info);
959: PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
960: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D bs %D mem %D\n",
961: rank,mat->rmap->n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,mat->rmap->bs,(PetscInt)info.memory);
962: MatGetInfo(baij->A,MAT_LOCAL,&info);
963: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
964: MatGetInfo(baij->B,MAT_LOCAL,&info);
965: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
966: PetscViewerFlush(viewer);
967: PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
968: PetscViewerASCIIPrintf(viewer,"Information on VecScatter used in matrix-vector product: \n");
969: VecScatterView(baij->Mvctx,viewer);
970: return(0);
971: } else if (format == PETSC_VIEWER_ASCII_INFO) {
972: PetscViewerASCIIPrintf(viewer," block size is %D\n",bs);
973: return(0);
974: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
975: return(0);
976: }
977: }
979: if (isdraw) {
980: PetscDraw draw;
981: PetscBool isnull;
982: PetscViewerDrawGetDraw(viewer,0,&draw);
983: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
984: }
986: {
987: /* assemble the entire matrix onto first processor. */
988: Mat A;
989: Mat_SeqBAIJ *Aloc;
990: PetscInt M = mat->rmap->N,N = mat->cmap->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
991: MatScalar *a;
993: /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
994: /* Perhaps this should be the type of mat? */
995: MatCreate(PetscObjectComm((PetscObject)mat),&A);
996: if (!rank) {
997: MatSetSizes(A,M,N,M,N);
998: } else {
999: MatSetSizes(A,0,0,M,N);
1000: }
1001: MatSetType(A,MATMPIBAIJ);
1002: MatMPIBAIJSetPreallocation(A,mat->rmap->bs,0,NULL,0,NULL);
1003: MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
1004: PetscLogObjectParent((PetscObject)mat,(PetscObject)A);
1006: /* copy over the A part */
1007: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1008: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1009: PetscMalloc1(bs,&rvals);
1011: for (i=0; i<mbs; i++) {
1012: rvals[0] = bs*(baij->rstartbs + i);
1013: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1014: for (j=ai[i]; j<ai[i+1]; j++) {
1015: col = (baij->cstartbs+aj[j])*bs;
1016: for (k=0; k<bs; k++) {
1017: MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1018: col++; a += bs;
1019: }
1020: }
1021: }
1022: /* copy over the B part */
1023: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1024: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1025: for (i=0; i<mbs; i++) {
1026: rvals[0] = bs*(baij->rstartbs + i);
1027: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1028: for (j=ai[i]; j<ai[i+1]; j++) {
1029: col = baij->garray[aj[j]]*bs;
1030: for (k=0; k<bs; k++) {
1031: MatSetValues_MPIBAIJ(A,bs,rvals,1,&col,a,INSERT_VALUES);
1032: col++; a += bs;
1033: }
1034: }
1035: }
1036: PetscFree(rvals);
1037: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1038: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1039: /*
1040: Everyone has to call to draw the matrix since the graphics waits are
1041: synchronized across all processors that share the PetscDraw object
1042: */
1043: PetscViewerGetSingleton(viewer,&sviewer);
1044: if (!rank) {
1045: MatView_SeqBAIJ(((Mat_MPIBAIJ*)(A->data))->A,sviewer);
1046: }
1047: PetscViewerRestoreSingleton(viewer,&sviewer);
1048: MatDestroy(&A);
1049: }
1050: return(0);
1051: }
1055: static PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat,PetscViewer viewer)
1056: {
1057: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)mat->data;
1058: Mat_SeqBAIJ *A = (Mat_SeqBAIJ*)a->A->data;
1059: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)a->B->data;
1061: PetscInt i,*row_lens,*crow_lens,bs = mat->rmap->bs,j,k,bs2=a->bs2,header[4],nz,rlen;
1062: PetscInt *range=0,nzmax,*column_indices,cnt,col,*garray = a->garray,cstart = mat->cmap->rstart/bs,len,pcnt,l,ll;
1063: int fd;
1064: PetscScalar *column_values;
1065: FILE *file;
1066: PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag;
1067: PetscInt message_count,flowcontrolcount;
1070: MPI_Comm_rank(PetscObjectComm((PetscObject)mat),&rank);
1071: MPI_Comm_size(PetscObjectComm((PetscObject)mat),&size);
1072: nz = bs2*(A->nz + B->nz);
1073: rlen = mat->rmap->n;
1074: if (!rank) {
1075: header[0] = MAT_FILE_CLASSID;
1076: header[1] = mat->rmap->N;
1077: header[2] = mat->cmap->N;
1079: MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1080: PetscViewerBinaryGetDescriptor(viewer,&fd);
1081: PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
1082: /* get largest number of rows any processor has */
1083: range = mat->rmap->range;
1084: for (i=1; i<size; i++) {
1085: rlen = PetscMax(rlen,range[i+1] - range[i]);
1086: }
1087: } else {
1088: MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,PetscObjectComm((PetscObject)mat));
1089: }
1091: PetscMalloc1((rlen/bs),&crow_lens);
1092: /* compute lengths of each row */
1093: for (i=0; i<a->mbs; i++) {
1094: crow_lens[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
1095: }
1096: /* store the row lengths to the file */
1097: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1098: if (!rank) {
1099: MPI_Status status;
1100: PetscMalloc1(rlen,&row_lens);
1101: rlen = (range[1] - range[0])/bs;
1102: for (i=0; i<rlen; i++) {
1103: for (j=0; j<bs; j++) {
1104: row_lens[i*bs+j] = bs*crow_lens[i];
1105: }
1106: }
1107: PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1108: for (i=1; i<size; i++) {
1109: rlen = (range[i+1] - range[i])/bs;
1110: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1111: MPI_Recv(crow_lens,rlen,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1112: for (k=0; k<rlen; k++) {
1113: for (j=0; j<bs; j++) {
1114: row_lens[k*bs+j] = bs*crow_lens[k];
1115: }
1116: }
1117: PetscBinaryWrite(fd,row_lens,bs*rlen,PETSC_INT,PETSC_TRUE);
1118: }
1119: PetscViewerFlowControlEndMaster(viewer,&message_count);
1120: PetscFree(row_lens);
1121: } else {
1122: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1123: MPI_Send(crow_lens,mat->rmap->n/bs,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1124: PetscViewerFlowControlEndWorker(viewer,&message_count);
1125: }
1126: PetscFree(crow_lens);
1128: /* load up the local column indices. Include for all rows not just one for each block row since process 0 does not have the
1129: information needed to make it for each row from a block row. This does require more communication but still not more than
1130: the communication needed for the nonzero values */
1131: nzmax = nz; /* space a largest processor needs */
1132: MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,PetscObjectComm((PetscObject)mat));
1133: PetscMalloc1(nzmax,&column_indices);
1134: cnt = 0;
1135: for (i=0; i<a->mbs; i++) {
1136: pcnt = cnt;
1137: for (j=B->i[i]; j<B->i[i+1]; j++) {
1138: if ((col = garray[B->j[j]]) > cstart) break;
1139: for (l=0; l<bs; l++) {
1140: column_indices[cnt++] = bs*col+l;
1141: }
1142: }
1143: for (k=A->i[i]; k<A->i[i+1]; k++) {
1144: for (l=0; l<bs; l++) {
1145: column_indices[cnt++] = bs*(A->j[k] + cstart)+l;
1146: }
1147: }
1148: for (; j<B->i[i+1]; j++) {
1149: for (l=0; l<bs; l++) {
1150: column_indices[cnt++] = bs*garray[B->j[j]]+l;
1151: }
1152: }
1153: len = cnt - pcnt;
1154: for (k=1; k<bs; k++) {
1155: PetscMemcpy(&column_indices[cnt],&column_indices[pcnt],len*sizeof(PetscInt));
1156: cnt += len;
1157: }
1158: }
1159: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1161: /* store the columns to the file */
1162: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1163: if (!rank) {
1164: MPI_Status status;
1165: PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
1166: for (i=1; i<size; i++) {
1167: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1168: MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1169: MPI_Recv(column_indices,cnt,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1170: PetscBinaryWrite(fd,column_indices,cnt,PETSC_INT,PETSC_TRUE);
1171: }
1172: PetscViewerFlowControlEndMaster(viewer,&message_count);
1173: } else {
1174: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1175: MPI_Send(&cnt,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1176: MPI_Send(column_indices,cnt,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1177: PetscViewerFlowControlEndWorker(viewer,&message_count);
1178: }
1179: PetscFree(column_indices);
1181: /* load up the numerical values */
1182: PetscMalloc1(nzmax,&column_values);
1183: cnt = 0;
1184: for (i=0; i<a->mbs; i++) {
1185: rlen = bs*(B->i[i+1] - B->i[i] + A->i[i+1] - A->i[i]);
1186: for (j=B->i[i]; j<B->i[i+1]; j++) {
1187: if (garray[B->j[j]] > cstart) break;
1188: for (l=0; l<bs; l++) {
1189: for (ll=0; ll<bs; ll++) {
1190: column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1191: }
1192: }
1193: cnt += bs;
1194: }
1195: for (k=A->i[i]; k<A->i[i+1]; k++) {
1196: for (l=0; l<bs; l++) {
1197: for (ll=0; ll<bs; ll++) {
1198: column_values[cnt + l*rlen + ll] = A->a[bs2*k+l+bs*ll];
1199: }
1200: }
1201: cnt += bs;
1202: }
1203: for (; j<B->i[i+1]; j++) {
1204: for (l=0; l<bs; l++) {
1205: for (ll=0; ll<bs; ll++) {
1206: column_values[cnt + l*rlen + ll] = B->a[bs2*j+l+bs*ll];
1207: }
1208: }
1209: cnt += bs;
1210: }
1211: cnt += (bs-1)*rlen;
1212: }
1213: if (cnt != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,nz);
1215: /* store the column values to the file */
1216: PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);
1217: if (!rank) {
1218: MPI_Status status;
1219: PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
1220: for (i=1; i<size; i++) {
1221: PetscViewerFlowControlStepMaster(viewer,i,&message_count,flowcontrolcount);
1222: MPI_Recv(&cnt,1,MPIU_INT,i,tag,PetscObjectComm((PetscObject)mat),&status);
1223: MPI_Recv(column_values,cnt,MPIU_SCALAR,i,tag,PetscObjectComm((PetscObject)mat),&status);
1224: PetscBinaryWrite(fd,column_values,cnt,PETSC_SCALAR,PETSC_TRUE);
1225: }
1226: PetscViewerFlowControlEndMaster(viewer,&message_count);
1227: } else {
1228: PetscViewerFlowControlStepWorker(viewer,rank,&message_count);
1229: MPI_Send(&nz,1,MPIU_INT,0,tag,PetscObjectComm((PetscObject)mat));
1230: MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,PetscObjectComm((PetscObject)mat));
1231: PetscViewerFlowControlEndWorker(viewer,&message_count);
1232: }
1233: PetscFree(column_values);
1235: PetscViewerBinaryGetInfoPointer(viewer,&file);
1236: if (file) {
1237: fprintf(file,"-matload_block_size %d\n",(int)mat->rmap->bs);
1238: }
1239: return(0);
1240: }
1244: PetscErrorCode MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
1245: {
1247: PetscBool iascii,isdraw,issocket,isbinary;
1250: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1251: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1252: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSOCKET,&issocket);
1253: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1254: if (iascii || isdraw || issocket) {
1255: MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);
1256: } else if (isbinary) {
1257: MatView_MPIBAIJ_Binary(mat,viewer);
1258: }
1259: return(0);
1260: }
1264: PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
1265: {
1266: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1270: #if defined(PETSC_USE_LOG)
1271: PetscLogObjectState((PetscObject)mat,"Rows=%D,Cols=%D",mat->rmap->N,mat->cmap->N);
1272: #endif
1273: MatStashDestroy_Private(&mat->stash);
1274: MatStashDestroy_Private(&mat->bstash);
1275: MatDestroy(&baij->A);
1276: MatDestroy(&baij->B);
1277: #if defined(PETSC_USE_CTABLE)
1278: PetscTableDestroy(&baij->colmap);
1279: #else
1280: PetscFree(baij->colmap);
1281: #endif
1282: PetscFree(baij->garray);
1283: VecDestroy(&baij->lvec);
1284: VecScatterDestroy(&baij->Mvctx);
1285: PetscFree2(baij->rowvalues,baij->rowindices);
1286: PetscFree(baij->barray);
1287: PetscFree2(baij->hd,baij->ht);
1288: PetscFree(baij->rangebs);
1289: PetscFree(mat->data);
1291: PetscObjectChangeTypeName((PetscObject)mat,0);
1292: PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C",NULL);
1293: PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C",NULL);
1294: PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C",NULL);
1295: PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocation_C",NULL);
1296: PetscObjectComposeFunction((PetscObject)mat,"MatMPIBAIJSetPreallocationCSR_C",NULL);
1297: PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",NULL);
1298: PetscObjectComposeFunction((PetscObject)mat,"MatSetHashTableFactor_C",NULL);
1299: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpisbaij_C",NULL);
1300: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_mpibaij_mpibstrm_C",NULL);
1301: return(0);
1302: }
1306: PetscErrorCode MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1307: {
1308: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1310: PetscInt nt;
1313: VecGetLocalSize(xx,&nt);
1314: if (nt != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1315: VecGetLocalSize(yy,&nt);
1316: if (nt != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1317: VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1318: (*a->A->ops->mult)(a->A,xx,yy);
1319: VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1320: (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1321: return(0);
1322: }
1326: PetscErrorCode MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1327: {
1328: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1332: VecScatterBegin(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1333: (*a->A->ops->multadd)(a->A,xx,yy,zz);
1334: VecScatterEnd(a->Mvctx,xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD);
1335: (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1336: return(0);
1337: }
1341: PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1342: {
1343: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1345: PetscBool merged;
1348: VecScatterGetMerged(a->Mvctx,&merged);
1349: /* do nondiagonal part */
1350: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1351: if (!merged) {
1352: /* send it on its way */
1353: VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1354: /* do local part */
1355: (*a->A->ops->multtranspose)(a->A,xx,yy);
1356: /* receive remote parts: note this assumes the values are not actually */
1357: /* inserted in yy until the next line */
1358: VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1359: } else {
1360: /* do local part */
1361: (*a->A->ops->multtranspose)(a->A,xx,yy);
1362: /* send it on its way */
1363: VecScatterBegin(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1364: /* values actually were received in the Begin() but we need to call this nop */
1365: VecScatterEnd(a->Mvctx,a->lvec,yy,ADD_VALUES,SCATTER_REVERSE);
1366: }
1367: return(0);
1368: }
1372: PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1373: {
1374: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1378: /* do nondiagonal part */
1379: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1380: /* send it on its way */
1381: VecScatterBegin(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1382: /* do local part */
1383: (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1384: /* receive remote parts: note this assumes the values are not actually */
1385: /* inserted in yy until the next line, which is true for my implementation*/
1386: /* but is not perhaps always true. */
1387: VecScatterEnd(a->Mvctx,a->lvec,zz,ADD_VALUES,SCATTER_REVERSE);
1388: return(0);
1389: }
1391: /*
1392: This only works correctly for square matrices where the subblock A->A is the
1393: diagonal block
1394: */
1397: PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1398: {
1399: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1403: if (A->rmap->N != A->cmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1404: MatGetDiagonal(a->A,v);
1405: return(0);
1406: }
1410: PetscErrorCode MatScale_MPIBAIJ(Mat A,PetscScalar aa)
1411: {
1412: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1416: MatScale(a->A,aa);
1417: MatScale(a->B,aa);
1418: return(0);
1419: }
1423: PetscErrorCode MatGetRow_MPIBAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1424: {
1425: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
1426: PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p;
1428: PetscInt bs = matin->rmap->bs,bs2 = mat->bs2,i,*cworkA,*cworkB,**pcA,**pcB;
1429: PetscInt nztot,nzA,nzB,lrow,brstart = matin->rmap->rstart,brend = matin->rmap->rend;
1430: PetscInt *cmap,*idx_p,cstart = mat->cstartbs;
1433: if (row < brstart || row >= brend) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only local rows");
1434: if (mat->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Already active");
1435: mat->getrowactive = PETSC_TRUE;
1437: if (!mat->rowvalues && (idx || v)) {
1438: /*
1439: allocate enough space to hold information from the longest row.
1440: */
1441: Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1442: PetscInt max = 1,mbs = mat->mbs,tmp;
1443: for (i=0; i<mbs; i++) {
1444: tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1445: if (max < tmp) max = tmp;
1446: }
1447: PetscMalloc2(max*bs2,&mat->rowvalues,max*bs2,&mat->rowindices);
1448: }
1449: lrow = row - brstart;
1451: pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1452: if (!v) {pvA = 0; pvB = 0;}
1453: if (!idx) {pcA = 0; if (!v) pcB = 0;}
1454: (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1455: (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1456: nztot = nzA + nzB;
1458: cmap = mat->garray;
1459: if (v || idx) {
1460: if (nztot) {
1461: /* Sort by increasing column numbers, assuming A and B already sorted */
1462: PetscInt imark = -1;
1463: if (v) {
1464: *v = v_p = mat->rowvalues;
1465: for (i=0; i<nzB; i++) {
1466: if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1467: else break;
1468: }
1469: imark = i;
1470: for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i];
1471: for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i];
1472: }
1473: if (idx) {
1474: *idx = idx_p = mat->rowindices;
1475: if (imark > -1) {
1476: for (i=0; i<imark; i++) {
1477: idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1478: }
1479: } else {
1480: for (i=0; i<nzB; i++) {
1481: if (cmap[cworkB[i]/bs] < cstart) idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1482: else break;
1483: }
1484: imark = i;
1485: }
1486: for (i=0; i<nzA; i++) idx_p[imark+i] = cstart*bs + cworkA[i];
1487: for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1488: }
1489: } else {
1490: if (idx) *idx = 0;
1491: if (v) *v = 0;
1492: }
1493: }
1494: *nz = nztot;
1495: (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1496: (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1497: return(0);
1498: }
1502: PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1503: {
1504: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1507: if (!baij->getrowactive) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1508: baij->getrowactive = PETSC_FALSE;
1509: return(0);
1510: }
1514: PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
1515: {
1516: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1520: MatZeroEntries(l->A);
1521: MatZeroEntries(l->B);
1522: return(0);
1523: }
1527: PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1528: {
1529: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)matin->data;
1530: Mat A = a->A,B = a->B;
1532: PetscReal isend[5],irecv[5];
1535: info->block_size = (PetscReal)matin->rmap->bs;
1537: MatGetInfo(A,MAT_LOCAL,info);
1539: isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1540: isend[3] = info->memory; isend[4] = info->mallocs;
1542: MatGetInfo(B,MAT_LOCAL,info);
1544: isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1545: isend[3] += info->memory; isend[4] += info->mallocs;
1547: if (flag == MAT_LOCAL) {
1548: info->nz_used = isend[0];
1549: info->nz_allocated = isend[1];
1550: info->nz_unneeded = isend[2];
1551: info->memory = isend[3];
1552: info->mallocs = isend[4];
1553: } else if (flag == MAT_GLOBAL_MAX) {
1554: MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)matin));
1556: info->nz_used = irecv[0];
1557: info->nz_allocated = irecv[1];
1558: info->nz_unneeded = irecv[2];
1559: info->memory = irecv[3];
1560: info->mallocs = irecv[4];
1561: } else if (flag == MAT_GLOBAL_SUM) {
1562: MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)matin));
1564: info->nz_used = irecv[0];
1565: info->nz_allocated = irecv[1];
1566: info->nz_unneeded = irecv[2];
1567: info->memory = irecv[3];
1568: info->mallocs = irecv[4];
1569: } else SETERRQ1(PetscObjectComm((PetscObject)matin),PETSC_ERR_ARG_WRONG,"Unknown MatInfoType argument %d",(int)flag);
1570: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1571: info->fill_ratio_needed = 0;
1572: info->factor_mallocs = 0;
1573: return(0);
1574: }
1578: PetscErrorCode MatSetOption_MPIBAIJ(Mat A,MatOption op,PetscBool flg)
1579: {
1580: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1584: switch (op) {
1585: case MAT_NEW_NONZERO_LOCATIONS:
1586: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1587: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1588: case MAT_KEEP_NONZERO_PATTERN:
1589: case MAT_NEW_NONZERO_LOCATION_ERR:
1590: MatSetOption(a->A,op,flg);
1591: MatSetOption(a->B,op,flg);
1592: break;
1593: case MAT_ROW_ORIENTED:
1594: a->roworiented = flg;
1596: MatSetOption(a->A,op,flg);
1597: MatSetOption(a->B,op,flg);
1598: break;
1599: case MAT_NEW_DIAGONALS:
1600: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1601: break;
1602: case MAT_IGNORE_OFF_PROC_ENTRIES:
1603: a->donotstash = flg;
1604: break;
1605: case MAT_USE_HASH_TABLE:
1606: a->ht_flag = flg;
1607: break;
1608: case MAT_SYMMETRIC:
1609: case MAT_STRUCTURALLY_SYMMETRIC:
1610: case MAT_HERMITIAN:
1611: case MAT_SYMMETRY_ETERNAL:
1612: MatSetOption(a->A,op,flg);
1613: break;
1614: default:
1615: SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"unknown option %d",op);
1616: }
1617: return(0);
1618: }
1622: PetscErrorCode MatTranspose_MPIBAIJ(Mat A,MatReuse reuse,Mat *matout)
1623: {
1624: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)A->data;
1625: Mat_SeqBAIJ *Aloc;
1626: Mat B;
1628: PetscInt M =A->rmap->N,N=A->cmap->N,*ai,*aj,i,*rvals,j,k,col;
1629: PetscInt bs=A->rmap->bs,mbs=baij->mbs;
1630: MatScalar *a;
1633: if (reuse == MAT_REUSE_MATRIX && A == *matout && M != N) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1634: if (reuse == MAT_INITIAL_MATRIX || *matout == A) {
1635: MatCreate(PetscObjectComm((PetscObject)A),&B);
1636: MatSetSizes(B,A->cmap->n,A->rmap->n,N,M);
1637: MatSetType(B,((PetscObject)A)->type_name);
1638: /* Do not know preallocation information, but must set block size */
1639: MatMPIBAIJSetPreallocation(B,A->rmap->bs,PETSC_DECIDE,NULL,PETSC_DECIDE,NULL);
1640: } else {
1641: B = *matout;
1642: }
1644: /* copy over the A part */
1645: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1646: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1647: PetscMalloc1(bs,&rvals);
1649: for (i=0; i<mbs; i++) {
1650: rvals[0] = bs*(baij->rstartbs + i);
1651: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1652: for (j=ai[i]; j<ai[i+1]; j++) {
1653: col = (baij->cstartbs+aj[j])*bs;
1654: for (k=0; k<bs; k++) {
1655: MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1657: col++; a += bs;
1658: }
1659: }
1660: }
1661: /* copy over the B part */
1662: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1663: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1664: for (i=0; i<mbs; i++) {
1665: rvals[0] = bs*(baij->rstartbs + i);
1666: for (j=1; j<bs; j++) rvals[j] = rvals[j-1] + 1;
1667: for (j=ai[i]; j<ai[i+1]; j++) {
1668: col = baij->garray[aj[j]]*bs;
1669: for (k=0; k<bs; k++) {
1670: MatSetValues_MPIBAIJ(B,1,&col,bs,rvals,a,INSERT_VALUES);
1671: col++;
1672: a += bs;
1673: }
1674: }
1675: }
1676: PetscFree(rvals);
1677: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1678: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1680: if (reuse == MAT_INITIAL_MATRIX || *matout != A) *matout = B;
1681: else {
1682: MatHeaderMerge(A,B);
1683: }
1684: return(0);
1685: }
1689: PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
1690: {
1691: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1692: Mat a = baij->A,b = baij->B;
1694: PetscInt s1,s2,s3;
1697: MatGetLocalSize(mat,&s2,&s3);
1698: if (rr) {
1699: VecGetLocalSize(rr,&s1);
1700: if (s1!=s3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1701: /* Overlap communication with computation. */
1702: VecScatterBegin(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1703: }
1704: if (ll) {
1705: VecGetLocalSize(ll,&s1);
1706: if (s1!=s2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1707: (*b->ops->diagonalscale)(b,ll,NULL);
1708: }
1709: /* scale the diagonal block */
1710: (*a->ops->diagonalscale)(a,ll,rr);
1712: if (rr) {
1713: /* Do a scatter end and then right scale the off-diagonal block */
1714: VecScatterEnd(baij->Mvctx,rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD);
1715: (*b->ops->diagonalscale)(b,NULL,baij->lvec);
1716: }
1717: return(0);
1718: }
1722: PetscErrorCode MatZeroRows_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1723: {
1724: Mat_MPIBAIJ *l = (Mat_MPIBAIJ *) A->data;
1725: PetscInt *owners = A->rmap->range;
1726: PetscInt n = A->rmap->n;
1727: PetscSF sf;
1728: PetscInt *lrows;
1729: PetscSFNode *rrows;
1730: PetscInt r, p = 0, len = 0;
1734: /* Create SF where leaves are input rows and roots are owned rows */
1735: PetscMalloc1(n, &lrows);
1736: for (r = 0; r < n; ++r) lrows[r] = -1;
1737: if (!A->nooffproczerorows) {PetscMalloc1(N, &rrows);}
1738: for (r = 0; r < N; ++r) {
1739: const PetscInt idx = rows[r];
1740: 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);
1741: if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1742: PetscLayoutFindOwner(A->rmap,idx,&p);
1743: }
1744: if (A->nooffproczerorows) {
1745: if (p != l->rank) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"MAT_NO_OFF_PROC_ZERO_ROWS set, but row %D is not owned by rank %d",idx,l->rank);
1746: lrows[len++] = idx - owners[p];
1747: } else {
1748: rrows[r].rank = p;
1749: rrows[r].index = rows[r] - owners[p];
1750: }
1751: }
1752: if (!A->nooffproczerorows) {
1753: PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1754: PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1755: /* Collect flags for rows to be zeroed */
1756: PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1757: PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1758: PetscSFDestroy(&sf);
1759: /* Compress and put in row numbers */
1760: for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1761: }
1762: /* fix right hand side if needed */
1763: if (x && b) {
1764: const PetscScalar *xx;
1765: PetscScalar *bb;
1767: VecGetArrayRead(x,&xx);
1768: VecGetArray(b,&bb);
1769: for (r = 0; r < len; ++r) bb[lrows[r]] = diag*xx[lrows[r]];
1770: VecRestoreArrayRead(x,&xx);
1771: VecRestoreArray(b,&bb);
1772: }
1774: /* actually zap the local rows */
1775: /*
1776: Zero the required rows. If the "diagonal block" of the matrix
1777: is square and the user wishes to set the diagonal we use separate
1778: code so that MatSetValues() is not called for each diagonal allocating
1779: new memory, thus calling lots of mallocs and slowing things down.
1781: */
1782: /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1783: MatZeroRows_SeqBAIJ(l->B,len,lrows,0.0,NULL,NULL);
1784: if ((diag != 0.0) && (l->A->rmap->N == l->A->cmap->N)) {
1785: MatZeroRows_SeqBAIJ(l->A,len,lrows,diag,NULL,NULL);
1786: } else if (diag != 0.0) {
1787: MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,0,0);
1788: 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\
1789: MAT_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1790: for (r = 0; r < len; ++r) {
1791: const PetscInt row = lrows[r] + A->rmap->rstart;
1792: MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
1793: }
1794: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1795: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1796: } else {
1797: MatZeroRows_SeqBAIJ(l->A,len,lrows,0.0,NULL,NULL);
1798: }
1799: PetscFree(lrows);
1801: /* only change matrix nonzero state if pattern was allowed to be changed */
1802: if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1803: PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1804: MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1805: }
1806: return(0);
1807: }
1811: PetscErrorCode MatZeroRowsColumns_MPIBAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1812: {
1813: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1814: PetscErrorCode ierr;
1815: PetscMPIInt n = A->rmap->n;
1816: PetscInt i,j,k,r,p = 0,len = 0,row,col,count;
1817: PetscInt *lrows,*owners = A->rmap->range;
1818: PetscSFNode *rrows;
1819: PetscSF sf;
1820: const PetscScalar *xx;
1821: PetscScalar *bb,*mask;
1822: Vec xmask,lmask;
1823: Mat_SeqBAIJ *baij = (Mat_SeqBAIJ*)l->B->data;
1824: PetscInt bs = A->rmap->bs, bs2 = baij->bs2;
1825: PetscScalar *aa;
1828: /* Create SF where leaves are input rows and roots are owned rows */
1829: PetscMalloc1(n, &lrows);
1830: for (r = 0; r < n; ++r) lrows[r] = -1;
1831: PetscMalloc1(N, &rrows);
1832: for (r = 0; r < N; ++r) {
1833: const PetscInt idx = rows[r];
1834: 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);
1835: if (idx < owners[p] || owners[p+1] <= idx) { /* short-circuit the search if the last p owns this row too */
1836: PetscLayoutFindOwner(A->rmap,idx,&p);
1837: }
1838: rrows[r].rank = p;
1839: rrows[r].index = rows[r] - owners[p];
1840: }
1841: PetscSFCreate(PetscObjectComm((PetscObject) A), &sf);
1842: PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER);
1843: /* Collect flags for rows to be zeroed */
1844: PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1845: PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *) rows, lrows, MPI_LOR);
1846: PetscSFDestroy(&sf);
1847: /* Compress and put in row numbers */
1848: for (r = 0; r < n; ++r) if (lrows[r] >= 0) lrows[len++] = r;
1849: /* zero diagonal part of matrix */
1850: MatZeroRowsColumns(l->A,len,lrows,diag,x,b);
1851: /* handle off diagonal part of matrix */
1852: MatGetVecs(A,&xmask,NULL);
1853: VecDuplicate(l->lvec,&lmask);
1854: VecGetArray(xmask,&bb);
1855: for (i=0; i<len; i++) bb[lrows[i]] = 1;
1856: VecRestoreArray(xmask,&bb);
1857: VecScatterBegin(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1858: VecScatterEnd(l->Mvctx,xmask,lmask,ADD_VALUES,SCATTER_FORWARD);
1859: VecDestroy(&xmask);
1860: if (x) {
1861: VecScatterBegin(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1862: VecScatterEnd(l->Mvctx,x,l->lvec,INSERT_VALUES,SCATTER_FORWARD);
1863: VecGetArrayRead(l->lvec,&xx);
1864: VecGetArray(b,&bb);
1865: }
1866: VecGetArray(lmask,&mask);
1867: /* remove zeroed rows of off diagonal matrix */
1868: for (i = 0; i < len; ++i) {
1869: row = lrows[i];
1870: count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
1871: aa = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
1872: for (k = 0; k < count; ++k) {
1873: aa[0] = 0.0;
1874: aa += bs;
1875: }
1876: }
1877: /* loop over all elements of off process part of matrix zeroing removed columns*/
1878: for (i = 0; i < l->B->rmap->N; ++i) {
1879: row = i/bs;
1880: for (j = baij->i[row]; j < baij->i[row+1]; ++j) {
1881: for (k = 0; k < bs; ++k) {
1882: col = bs*baij->j[j] + k;
1883: if (PetscAbsScalar(mask[col])) {
1884: aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1885: if (b) bb[i] -= aa[0]*xx[col];
1886: aa[0] = 0.0;
1887: }
1888: }
1889: }
1890: }
1891: if (x) {
1892: VecRestoreArray(b,&bb);
1893: VecRestoreArrayRead(l->lvec,&xx);
1894: }
1895: VecRestoreArray(lmask,&mask);
1896: VecDestroy(&lmask);
1897: PetscFree(lrows);
1899: /* only change matrix nonzero state if pattern was allowed to be changed */
1900: if (!((Mat_SeqBAIJ*)(l->A->data))->keepnonzeropattern) {
1901: PetscObjectState state = l->A->nonzerostate + l->B->nonzerostate;
1902: MPI_Allreduce(&state,&A->nonzerostate,1,MPIU_INT64,MPI_SUM,PetscObjectComm((PetscObject)A));
1903: }
1904: return(0);
1905: }
1909: PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1910: {
1911: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1915: MatSetUnfactored(a->A);
1916: return(0);
1917: }
1919: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat*);
1923: PetscErrorCode MatEqual_MPIBAIJ(Mat A,Mat B,PetscBool *flag)
1924: {
1925: Mat_MPIBAIJ *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
1926: Mat a,b,c,d;
1927: PetscBool flg;
1931: a = matA->A; b = matA->B;
1932: c = matB->A; d = matB->B;
1934: MatEqual(a,c,&flg);
1935: if (flg) {
1936: MatEqual(b,d,&flg);
1937: }
1938: MPI_Allreduce(&flg,flag,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)A));
1939: return(0);
1940: }
1944: PetscErrorCode MatCopy_MPIBAIJ(Mat A,Mat B,MatStructure str)
1945: {
1947: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1948: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)B->data;
1951: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1952: if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1953: MatCopy_Basic(A,B,str);
1954: } else {
1955: MatCopy(a->A,b->A,str);
1956: MatCopy(a->B,b->B,str);
1957: }
1958: return(0);
1959: }
1963: PetscErrorCode MatSetUp_MPIBAIJ(Mat A)
1964: {
1968: MatMPIBAIJSetPreallocation(A,A->rmap->bs,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1969: return(0);
1970: }
1974: PetscErrorCode MatAXPYGetPreallocation_MPIBAIJ(Mat Y,const PetscInt *yltog,Mat X,const PetscInt *xltog,PetscInt *nnz)
1975: {
1977: PetscInt bs = Y->rmap->bs,m = Y->rmap->N/bs;
1978: Mat_SeqBAIJ *x = (Mat_SeqBAIJ*)X->data;
1979: Mat_SeqBAIJ *y = (Mat_SeqBAIJ*)Y->data;
1982: MatAXPYGetPreallocation_MPIX_private(m,x->i,x->j,xltog,y->i,y->j,yltog,nnz);
1983: return(0);
1984: }
1988: PetscErrorCode MatAXPY_MPIBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1989: {
1991: Mat_MPIBAIJ *xx=(Mat_MPIBAIJ*)X->data,*yy=(Mat_MPIBAIJ*)Y->data;
1992: PetscBLASInt bnz,one=1;
1993: Mat_SeqBAIJ *x,*y;
1996: if (str == SAME_NONZERO_PATTERN) {
1997: PetscScalar alpha = a;
1998: x = (Mat_SeqBAIJ*)xx->A->data;
1999: y = (Mat_SeqBAIJ*)yy->A->data;
2000: PetscBLASIntCast(x->nz,&bnz);
2001: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2002: x = (Mat_SeqBAIJ*)xx->B->data;
2003: y = (Mat_SeqBAIJ*)yy->B->data;
2004: PetscBLASIntCast(x->nz,&bnz);
2005: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2006: PetscObjectStateIncrease((PetscObject)Y);
2007: } else {
2008: Mat B;
2009: PetscInt *nnz_d,*nnz_o,bs=Y->rmap->bs;
2010: PetscMalloc1(yy->A->rmap->N,&nnz_d);
2011: PetscMalloc1(yy->B->rmap->N,&nnz_o);
2012: MatCreate(PetscObjectComm((PetscObject)Y),&B);
2013: PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2014: MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2015: MatSetBlockSizesFromMats(B,Y,Y);
2016: MatSetType(B,MATMPIBAIJ);
2017: MatAXPYGetPreallocation_SeqBAIJ(yy->A,xx->A,nnz_d);
2018: MatAXPYGetPreallocation_MPIBAIJ(yy->B,yy->garray,xx->B,xx->garray,nnz_o);
2019: MatMPIBAIJSetPreallocation(B,bs,0,nnz_d,0,nnz_o);
2020: /* MatAXPY_BasicWithPreallocation() for BAIJ matrix is much slower than AIJ, even for bs=1 ! */
2021: MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2022: MatHeaderReplace(Y,B);
2023: PetscFree(nnz_d);
2024: PetscFree(nnz_o);
2025: }
2026: return(0);
2027: }
2031: PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
2032: {
2033: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2037: MatRealPart(a->A);
2038: MatRealPart(a->B);
2039: return(0);
2040: }
2044: PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
2045: {
2046: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2050: MatImaginaryPart(a->A);
2051: MatImaginaryPart(a->B);
2052: return(0);
2053: }
2057: PetscErrorCode MatGetSubMatrix_MPIBAIJ(Mat mat,IS isrow,IS iscol,MatReuse call,Mat *newmat)
2058: {
2060: IS iscol_local;
2061: PetscInt csize;
2064: ISGetLocalSize(iscol,&csize);
2065: if (call == MAT_REUSE_MATRIX) {
2066: PetscObjectQuery((PetscObject)*newmat,"ISAllGather",(PetscObject*)&iscol_local);
2067: if (!iscol_local) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2068: } else {
2069: ISAllGather(iscol,&iscol_local);
2070: }
2071: MatGetSubMatrix_MPIBAIJ_Private(mat,isrow,iscol_local,csize,call,newmat);
2072: if (call == MAT_INITIAL_MATRIX) {
2073: PetscObjectCompose((PetscObject)*newmat,"ISAllGather",(PetscObject)iscol_local);
2074: ISDestroy(&iscol_local);
2075: }
2076: return(0);
2077: }
2078: extern PetscErrorCode MatGetSubMatrices_MPIBAIJ_local(Mat,PetscInt,const IS[],const IS[],MatReuse,PetscBool*,PetscBool*,Mat*);
2081: /*
2082: Not great since it makes two copies of the submatrix, first an SeqBAIJ
2083: in local and then by concatenating the local matrices the end result.
2084: Writing it directly would be much like MatGetSubMatrices_MPIBAIJ()
2085: */
2086: PetscErrorCode MatGetSubMatrix_MPIBAIJ_Private(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2087: {
2089: PetscMPIInt rank,size;
2090: PetscInt i,m,n,rstart,row,rend,nz,*cwork,j,bs;
2091: PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal,ncol,nrow;
2092: Mat M,Mreuse;
2093: MatScalar *vwork,*aa;
2094: MPI_Comm comm;
2095: IS isrow_new, iscol_new;
2096: PetscBool idflag,allrows, allcols;
2097: Mat_SeqBAIJ *aij;
2100: PetscObjectGetComm((PetscObject)mat,&comm);
2101: MPI_Comm_rank(comm,&rank);
2102: MPI_Comm_size(comm,&size);
2103: /* The compression and expansion should be avoided. Doesn't point
2104: out errors, might change the indices, hence buggey */
2105: ISCompressIndicesGeneral(mat->rmap->N,mat->rmap->n,mat->rmap->bs,1,&isrow,&isrow_new);
2106: ISCompressIndicesGeneral(mat->cmap->N,mat->cmap->n,mat->cmap->bs,1,&iscol,&iscol_new);
2108: /* Check for special case: each processor gets entire matrix columns */
2109: ISIdentity(iscol,&idflag);
2110: ISGetLocalSize(iscol,&ncol);
2111: if (idflag && ncol == mat->cmap->N) allcols = PETSC_TRUE;
2112: else allcols = PETSC_FALSE;
2114: ISIdentity(isrow,&idflag);
2115: ISGetLocalSize(isrow,&nrow);
2116: if (idflag && nrow == mat->rmap->N) allrows = PETSC_TRUE;
2117: else allrows = PETSC_FALSE;
2119: if (call == MAT_REUSE_MATRIX) {
2120: PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject*)&Mreuse);
2121: if (!Mreuse) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2122: MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_REUSE_MATRIX,&allrows,&allcols,&Mreuse);
2123: } else {
2124: MatGetSubMatrices_MPIBAIJ_local(mat,1,&isrow_new,&iscol_new,MAT_INITIAL_MATRIX,&allrows,&allcols,&Mreuse);
2125: }
2126: ISDestroy(&isrow_new);
2127: ISDestroy(&iscol_new);
2128: /*
2129: m - number of local rows
2130: n - number of columns (same on all processors)
2131: rstart - first row in new global matrix generated
2132: */
2133: MatGetBlockSize(mat,&bs);
2134: MatGetSize(Mreuse,&m,&n);
2135: m = m/bs;
2136: n = n/bs;
2138: if (call == MAT_INITIAL_MATRIX) {
2139: aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2140: ii = aij->i;
2141: jj = aij->j;
2143: /*
2144: Determine the number of non-zeros in the diagonal and off-diagonal
2145: portions of the matrix in order to do correct preallocation
2146: */
2148: /* first get start and end of "diagonal" columns */
2149: if (csize == PETSC_DECIDE) {
2150: ISGetSize(isrow,&mglobal);
2151: if (mglobal == n*bs) { /* square matrix */
2152: nlocal = m;
2153: } else {
2154: nlocal = n/size + ((n % size) > rank);
2155: }
2156: } else {
2157: nlocal = csize/bs;
2158: }
2159: MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2160: rstart = rend - nlocal;
2161: 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);
2163: /* next, compute all the lengths */
2164: PetscMalloc2(m+1,&dlens,m+1,&olens);
2165: for (i=0; i<m; i++) {
2166: jend = ii[i+1] - ii[i];
2167: olen = 0;
2168: dlen = 0;
2169: for (j=0; j<jend; j++) {
2170: if (*jj < rstart || *jj >= rend) olen++;
2171: else dlen++;
2172: jj++;
2173: }
2174: olens[i] = olen;
2175: dlens[i] = dlen;
2176: }
2177: MatCreate(comm,&M);
2178: MatSetSizes(M,bs*m,bs*nlocal,PETSC_DECIDE,bs*n);
2179: MatSetType(M,((PetscObject)mat)->type_name);
2180: MatMPIBAIJSetPreallocation(M,bs,0,dlens,0,olens);
2181: PetscFree2(dlens,olens);
2182: } else {
2183: PetscInt ml,nl;
2185: M = *newmat;
2186: MatGetLocalSize(M,&ml,&nl);
2187: if (ml != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2188: MatZeroEntries(M);
2189: /*
2190: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2191: rather than the slower MatSetValues().
2192: */
2193: M->was_assembled = PETSC_TRUE;
2194: M->assembled = PETSC_FALSE;
2195: }
2196: MatSetOption(M,MAT_ROW_ORIENTED,PETSC_FALSE);
2197: MatGetOwnershipRange(M,&rstart,&rend);
2198: aij = (Mat_SeqBAIJ*)(Mreuse)->data;
2199: ii = aij->i;
2200: jj = aij->j;
2201: aa = aij->a;
2202: for (i=0; i<m; i++) {
2203: row = rstart/bs + i;
2204: nz = ii[i+1] - ii[i];
2205: cwork = jj; jj += nz;
2206: vwork = aa; aa += nz*bs*bs;
2207: MatSetValuesBlocked_MPIBAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2208: }
2210: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2211: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2212: *newmat = M;
2214: /* save submatrix used in processor for next request */
2215: if (call == MAT_INITIAL_MATRIX) {
2216: PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2217: PetscObjectDereference((PetscObject)Mreuse);
2218: }
2219: return(0);
2220: }
2224: PetscErrorCode MatPermute_MPIBAIJ(Mat A,IS rowp,IS colp,Mat *B)
2225: {
2226: MPI_Comm comm,pcomm;
2227: PetscInt clocal_size,nrows;
2228: const PetscInt *rows;
2229: PetscMPIInt size;
2230: IS crowp,lcolp;
2234: PetscObjectGetComm((PetscObject)A,&comm);
2235: /* make a collective version of 'rowp' */
2236: PetscObjectGetComm((PetscObject)rowp,&pcomm);
2237: if (pcomm==comm) {
2238: crowp = rowp;
2239: } else {
2240: ISGetSize(rowp,&nrows);
2241: ISGetIndices(rowp,&rows);
2242: ISCreateGeneral(comm,nrows,rows,PETSC_COPY_VALUES,&crowp);
2243: ISRestoreIndices(rowp,&rows);
2244: }
2245: ISSetPermutation(crowp);
2246: /* make a local version of 'colp' */
2247: PetscObjectGetComm((PetscObject)colp,&pcomm);
2248: MPI_Comm_size(pcomm,&size);
2249: if (size==1) {
2250: lcolp = colp;
2251: } else {
2252: ISAllGather(colp,&lcolp);
2253: }
2254: ISSetPermutation(lcolp);
2255: /* now we just get the submatrix */
2256: MatGetLocalSize(A,NULL,&clocal_size);
2257: MatGetSubMatrix_MPIBAIJ_Private(A,crowp,lcolp,clocal_size,MAT_INITIAL_MATRIX,B);
2258: /* clean up */
2259: if (pcomm!=comm) {
2260: ISDestroy(&crowp);
2261: }
2262: if (size>1) {
2263: ISDestroy(&lcolp);
2264: }
2265: return(0);
2266: }
2270: PetscErrorCode MatGetGhosts_MPIBAIJ(Mat mat,PetscInt *nghosts,const PetscInt *ghosts[])
2271: {
2272: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*) mat->data;
2273: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data;
2276: if (nghosts) *nghosts = B->nbs;
2277: if (ghosts) *ghosts = baij->garray;
2278: return(0);
2279: }
2283: PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A,Mat *newmat)
2284: {
2285: Mat B;
2286: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2287: Mat_SeqBAIJ *ad = (Mat_SeqBAIJ*)a->A->data,*bd = (Mat_SeqBAIJ*)a->B->data;
2288: Mat_SeqAIJ *b;
2290: PetscMPIInt size,rank,*recvcounts = 0,*displs = 0;
2291: PetscInt sendcount,i,*rstarts = A->rmap->range,n,cnt,j,bs = A->rmap->bs;
2292: PetscInt m,*garray = a->garray,*lens,*jsendbuf,*a_jsendbuf,*b_jsendbuf;
2295: MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
2296: MPI_Comm_rank(PetscObjectComm((PetscObject)A),&rank);
2298: /* ----------------------------------------------------------------
2299: Tell every processor the number of nonzeros per row
2300: */
2301: PetscMalloc1((A->rmap->N/bs),&lens);
2302: for (i=A->rmap->rstart/bs; i<A->rmap->rend/bs; i++) {
2303: 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];
2304: }
2305: sendcount = A->rmap->rend/bs - A->rmap->rstart/bs;
2306: PetscMalloc1(2*size,&recvcounts);
2307: displs = recvcounts + size;
2308: for (i=0; i<size; i++) {
2309: recvcounts[i] = A->rmap->range[i+1]/bs - A->rmap->range[i]/bs;
2310: displs[i] = A->rmap->range[i]/bs;
2311: }
2312: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2313: MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2314: #else
2315: MPI_Allgatherv(lens+A->rmap->rstart/bs,sendcount,MPIU_INT,lens,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2316: #endif
2317: /* ---------------------------------------------------------------
2318: Create the sequential matrix of the same type as the local block diagonal
2319: */
2320: MatCreate(PETSC_COMM_SELF,&B);
2321: MatSetSizes(B,A->rmap->N/bs,A->cmap->N/bs,PETSC_DETERMINE,PETSC_DETERMINE);
2322: MatSetType(B,MATSEQAIJ);
2323: MatSeqAIJSetPreallocation(B,0,lens);
2324: b = (Mat_SeqAIJ*)B->data;
2326: /*--------------------------------------------------------------------
2327: Copy my part of matrix column indices over
2328: */
2329: sendcount = ad->nz + bd->nz;
2330: jsendbuf = b->j + b->i[rstarts[rank]/bs];
2331: a_jsendbuf = ad->j;
2332: b_jsendbuf = bd->j;
2333: n = A->rmap->rend/bs - A->rmap->rstart/bs;
2334: cnt = 0;
2335: for (i=0; i<n; i++) {
2337: /* put in lower diagonal portion */
2338: m = bd->i[i+1] - bd->i[i];
2339: while (m > 0) {
2340: /* is it above diagonal (in bd (compressed) numbering) */
2341: if (garray[*b_jsendbuf] > A->rmap->rstart/bs + i) break;
2342: jsendbuf[cnt++] = garray[*b_jsendbuf++];
2343: m--;
2344: }
2346: /* put in diagonal portion */
2347: for (j=ad->i[i]; j<ad->i[i+1]; j++) {
2348: jsendbuf[cnt++] = A->rmap->rstart/bs + *a_jsendbuf++;
2349: }
2351: /* put in upper diagonal portion */
2352: while (m-- > 0) {
2353: jsendbuf[cnt++] = garray[*b_jsendbuf++];
2354: }
2355: }
2356: if (cnt != sendcount) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupted PETSc matrix: nz given %D actual nz %D",sendcount,cnt);
2358: /*--------------------------------------------------------------------
2359: Gather all column indices to all processors
2360: */
2361: for (i=0; i<size; i++) {
2362: recvcounts[i] = 0;
2363: for (j=A->rmap->range[i]/bs; j<A->rmap->range[i+1]/bs; j++) {
2364: recvcounts[i] += lens[j];
2365: }
2366: }
2367: displs[0] = 0;
2368: for (i=1; i<size; i++) {
2369: displs[i] = displs[i-1] + recvcounts[i-1];
2370: }
2371: #if defined(PETSC_HAVE_MPI_IN_PLACE)
2372: MPI_Allgatherv(MPI_IN_PLACE,0,MPI_DATATYPE_NULL,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2373: #else
2374: MPI_Allgatherv(jsendbuf,sendcount,MPIU_INT,b->j,recvcounts,displs,MPIU_INT,PetscObjectComm((PetscObject)A));
2375: #endif
2376: /*--------------------------------------------------------------------
2377: Assemble the matrix into useable form (note numerical values not yet set)
2378: */
2379: /* set the b->ilen (length of each row) values */
2380: PetscMemcpy(b->ilen,lens,(A->rmap->N/bs)*sizeof(PetscInt));
2381: /* set the b->i indices */
2382: b->i[0] = 0;
2383: for (i=1; i<=A->rmap->N/bs; i++) {
2384: b->i[i] = b->i[i-1] + lens[i-1];
2385: }
2386: PetscFree(lens);
2387: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2388: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2389: PetscFree(recvcounts);
2391: if (A->symmetric) {
2392: MatSetOption(B,MAT_SYMMETRIC,PETSC_TRUE);
2393: } else if (A->hermitian) {
2394: MatSetOption(B,MAT_HERMITIAN,PETSC_TRUE);
2395: } else if (A->structurally_symmetric) {
2396: MatSetOption(B,MAT_STRUCTURALLY_SYMMETRIC,PETSC_TRUE);
2397: }
2398: *newmat = B;
2399: return(0);
2400: }
2404: PetscErrorCode MatSOR_MPIBAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
2405: {
2406: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
2408: Vec bb1 = 0;
2411: if (flag == SOR_APPLY_UPPER) {
2412: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2413: return(0);
2414: }
2416: if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) {
2417: VecDuplicate(bb,&bb1);
2418: }
2420: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2421: if (flag & SOR_ZERO_INITIAL_GUESS) {
2422: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2423: its--;
2424: }
2426: while (its--) {
2427: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2428: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2430: /* update rhs: bb1 = bb - B*x */
2431: VecScale(mat->lvec,-1.0);
2432: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2434: /* local sweep */
2435: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,1,xx);
2436: }
2437: } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2438: if (flag & SOR_ZERO_INITIAL_GUESS) {
2439: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2440: its--;
2441: }
2442: while (its--) {
2443: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2444: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2446: /* update rhs: bb1 = bb - B*x */
2447: VecScale(mat->lvec,-1.0);
2448: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2450: /* local sweep */
2451: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,1,xx);
2452: }
2453: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2454: if (flag & SOR_ZERO_INITIAL_GUESS) {
2455: (*mat->A->ops->sor)(mat->A,bb,omega,flag,fshift,lits,1,xx);
2456: its--;
2457: }
2458: while (its--) {
2459: VecScatterBegin(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2460: VecScatterEnd(mat->Mvctx,xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD);
2462: /* update rhs: bb1 = bb - B*x */
2463: VecScale(mat->lvec,-1.0);
2464: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
2466: /* local sweep */
2467: (*mat->A->ops->sor)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,1,xx);
2468: }
2469: } else SETERRQ(PetscObjectComm((PetscObject)matin),PETSC_ERR_SUP,"Parallel version of SOR requested not supported");
2471: VecDestroy(&bb1);
2472: return(0);
2473: }
2477: PetscErrorCode MatGetColumnNorms_MPIBAIJ(Mat A,NormType type,PetscReal *norms)
2478: {
2480: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ*)A->data;
2481: PetscInt N,i,*garray = aij->garray;
2482: PetscInt ib,jb,bs = A->rmap->bs;
2483: Mat_SeqBAIJ *a_aij = (Mat_SeqBAIJ*) aij->A->data;
2484: MatScalar *a_val = a_aij->a;
2485: Mat_SeqBAIJ *b_aij = (Mat_SeqBAIJ*) aij->B->data;
2486: MatScalar *b_val = b_aij->a;
2487: PetscReal *work;
2490: MatGetSize(A,NULL,&N);
2491: PetscCalloc1(N,&work);
2492: if (type == NORM_2) {
2493: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2494: for (jb=0; jb<bs; jb++) {
2495: for (ib=0; ib<bs; ib++) {
2496: work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val * *a_val);
2497: a_val++;
2498: }
2499: }
2500: }
2501: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2502: for (jb=0; jb<bs; jb++) {
2503: for (ib=0; ib<bs; ib++) {
2504: work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val * *b_val);
2505: b_val++;
2506: }
2507: }
2508: }
2509: } else if (type == NORM_1) {
2510: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2511: for (jb=0; jb<bs; jb++) {
2512: for (ib=0; ib<bs; ib++) {
2513: work[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val);
2514: a_val++;
2515: }
2516: }
2517: }
2518: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2519: for (jb=0; jb<bs; jb++) {
2520: for (ib=0; ib<bs; ib++) {
2521: work[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val);
2522: b_val++;
2523: }
2524: }
2525: }
2526: } else if (type == NORM_INFINITY) {
2527: for (i=a_aij->i[0]; i<a_aij->i[aij->A->rmap->n/bs]; i++) {
2528: for (jb=0; jb<bs; jb++) {
2529: for (ib=0; ib<bs; ib++) {
2530: int col = A->cmap->rstart + a_aij->j[i] * bs + jb;
2531: work[col] = PetscMax(PetscAbsScalar(*a_val), work[col]);
2532: a_val++;
2533: }
2534: }
2535: }
2536: for (i=b_aij->i[0]; i<b_aij->i[aij->B->rmap->n/bs]; i++) {
2537: for (jb=0; jb<bs; jb++) {
2538: for (ib=0; ib<bs; ib++) {
2539: int col = garray[b_aij->j[i]] * bs + jb;
2540: work[col] = PetscMax(PetscAbsScalar(*b_val), work[col]);
2541: b_val++;
2542: }
2543: }
2544: }
2545: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
2546: if (type == NORM_INFINITY) {
2547: MPI_Allreduce(work,norms,N,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)A));
2548: } else {
2549: MPI_Allreduce(work,norms,N,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)A));
2550: }
2551: PetscFree(work);
2552: if (type == NORM_2) {
2553: for (i=0; i<N; i++) norms[i] = PetscSqrtReal(norms[i]);
2554: }
2555: return(0);
2556: }
2560: PetscErrorCode MatInvertBlockDiagonal_MPIBAIJ(Mat A,const PetscScalar **values)
2561: {
2562: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*) A->data;
2566: MatInvertBlockDiagonal(a->A,values);
2567: return(0);
2568: }
2571: /* -------------------------------------------------------------------*/
2572: static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2573: MatGetRow_MPIBAIJ,
2574: MatRestoreRow_MPIBAIJ,
2575: MatMult_MPIBAIJ,
2576: /* 4*/ MatMultAdd_MPIBAIJ,
2577: MatMultTranspose_MPIBAIJ,
2578: MatMultTransposeAdd_MPIBAIJ,
2579: 0,
2580: 0,
2581: 0,
2582: /*10*/ 0,
2583: 0,
2584: 0,
2585: MatSOR_MPIBAIJ,
2586: MatTranspose_MPIBAIJ,
2587: /*15*/ MatGetInfo_MPIBAIJ,
2588: MatEqual_MPIBAIJ,
2589: MatGetDiagonal_MPIBAIJ,
2590: MatDiagonalScale_MPIBAIJ,
2591: MatNorm_MPIBAIJ,
2592: /*20*/ MatAssemblyBegin_MPIBAIJ,
2593: MatAssemblyEnd_MPIBAIJ,
2594: MatSetOption_MPIBAIJ,
2595: MatZeroEntries_MPIBAIJ,
2596: /*24*/ MatZeroRows_MPIBAIJ,
2597: 0,
2598: 0,
2599: 0,
2600: 0,
2601: /*29*/ MatSetUp_MPIBAIJ,
2602: 0,
2603: 0,
2604: 0,
2605: 0,
2606: /*34*/ MatDuplicate_MPIBAIJ,
2607: 0,
2608: 0,
2609: 0,
2610: 0,
2611: /*39*/ MatAXPY_MPIBAIJ,
2612: MatGetSubMatrices_MPIBAIJ,
2613: MatIncreaseOverlap_MPIBAIJ,
2614: MatGetValues_MPIBAIJ,
2615: MatCopy_MPIBAIJ,
2616: /*44*/ 0,
2617: MatScale_MPIBAIJ,
2618: 0,
2619: 0,
2620: MatZeroRowsColumns_MPIBAIJ,
2621: /*49*/ 0,
2622: 0,
2623: 0,
2624: 0,
2625: 0,
2626: /*54*/ MatFDColoringCreate_MPIXAIJ,
2627: 0,
2628: MatSetUnfactored_MPIBAIJ,
2629: MatPermute_MPIBAIJ,
2630: MatSetValuesBlocked_MPIBAIJ,
2631: /*59*/ MatGetSubMatrix_MPIBAIJ,
2632: MatDestroy_MPIBAIJ,
2633: MatView_MPIBAIJ,
2634: 0,
2635: 0,
2636: /*64*/ 0,
2637: 0,
2638: 0,
2639: 0,
2640: 0,
2641: /*69*/ MatGetRowMaxAbs_MPIBAIJ,
2642: 0,
2643: 0,
2644: 0,
2645: 0,
2646: /*74*/ 0,
2647: MatFDColoringApply_BAIJ,
2648: 0,
2649: 0,
2650: 0,
2651: /*79*/ 0,
2652: 0,
2653: 0,
2654: 0,
2655: MatLoad_MPIBAIJ,
2656: /*84*/ 0,
2657: 0,
2658: 0,
2659: 0,
2660: 0,
2661: /*89*/ 0,
2662: 0,
2663: 0,
2664: 0,
2665: 0,
2666: /*94*/ 0,
2667: 0,
2668: 0,
2669: 0,
2670: 0,
2671: /*99*/ 0,
2672: 0,
2673: 0,
2674: 0,
2675: 0,
2676: /*104*/0,
2677: MatRealPart_MPIBAIJ,
2678: MatImaginaryPart_MPIBAIJ,
2679: 0,
2680: 0,
2681: /*109*/0,
2682: 0,
2683: 0,
2684: 0,
2685: 0,
2686: /*114*/MatGetSeqNonzeroStructure_MPIBAIJ,
2687: 0,
2688: MatGetGhosts_MPIBAIJ,
2689: 0,
2690: 0,
2691: /*119*/0,
2692: 0,
2693: 0,
2694: 0,
2695: MatGetMultiProcBlock_MPIBAIJ,
2696: /*124*/0,
2697: MatGetColumnNorms_MPIBAIJ,
2698: MatInvertBlockDiagonal_MPIBAIJ,
2699: 0,
2700: 0,
2701: /*129*/ 0,
2702: 0,
2703: 0,
2704: 0,
2705: 0,
2706: /*134*/ 0,
2707: 0,
2708: 0,
2709: 0,
2710: 0,
2711: /*139*/ 0,
2712: 0,
2713: 0,
2714: MatFDColoringSetUp_MPIXAIJ
2715: };
2719: PetscErrorCode MatGetDiagonalBlock_MPIBAIJ(Mat A,Mat *a)
2720: {
2722: *a = ((Mat_MPIBAIJ*)A->data)->A;
2723: return(0);
2724: }
2726: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType,MatReuse,Mat*);
2730: PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[],const PetscScalar V[])
2731: {
2732: PetscInt m,rstart,cstart,cend;
2733: PetscInt i,j,d,nz,nz_max=0,*d_nnz=0,*o_nnz=0;
2734: const PetscInt *JJ =0;
2735: PetscScalar *values=0;
2736: PetscBool roworiented = ((Mat_MPIBAIJ*)B->data)->roworiented;
2740: PetscLayoutSetBlockSize(B->rmap,bs);
2741: PetscLayoutSetBlockSize(B->cmap,bs);
2742: PetscLayoutSetUp(B->rmap);
2743: PetscLayoutSetUp(B->cmap);
2744: PetscLayoutGetBlockSize(B->rmap,&bs);
2745: m = B->rmap->n/bs;
2746: rstart = B->rmap->rstart/bs;
2747: cstart = B->cmap->rstart/bs;
2748: cend = B->cmap->rend/bs;
2750: if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
2751: PetscMalloc2(m,&d_nnz,m,&o_nnz);
2752: for (i=0; i<m; i++) {
2753: nz = ii[i+1] - ii[i];
2754: if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative number of columns %D",i,nz);
2755: nz_max = PetscMax(nz_max,nz);
2756: JJ = jj + ii[i];
2757: for (j=0; j<nz; j++) {
2758: if (*JJ >= cstart) break;
2759: JJ++;
2760: }
2761: d = 0;
2762: for (; j<nz; j++) {
2763: if (*JJ++ >= cend) break;
2764: d++;
2765: }
2766: d_nnz[i] = d;
2767: o_nnz[i] = nz - d;
2768: }
2769: MatMPIBAIJSetPreallocation(B,bs,0,d_nnz,0,o_nnz);
2770: PetscFree2(d_nnz,o_nnz);
2772: values = (PetscScalar*)V;
2773: if (!values) {
2774: PetscMalloc1(bs*bs*nz_max,&values);
2775: PetscMemzero(values,bs*bs*nz_max*sizeof(PetscScalar));
2776: }
2777: for (i=0; i<m; i++) {
2778: PetscInt row = i + rstart;
2779: PetscInt ncols = ii[i+1] - ii[i];
2780: const PetscInt *icols = jj + ii[i];
2781: if (!roworiented) { /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2782: const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
2783: MatSetValuesBlocked_MPIBAIJ(B,1,&row,ncols,icols,svals,INSERT_VALUES);
2784: } else { /* block ordering does not match so we can only insert one block at a time. */
2785: PetscInt j;
2786: for (j=0; j<ncols; j++) {
2787: const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
2788: MatSetValuesBlocked_MPIBAIJ(B,1,&row,1,&icols[j],svals,INSERT_VALUES);
2789: }
2790: }
2791: }
2793: if (!V) { PetscFree(values); }
2794: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2795: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2796: MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2797: return(0);
2798: }
2802: /*@C
2803: MatMPIBAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in BAIJ format
2804: (the default parallel PETSc format).
2806: Collective on MPI_Comm
2808: Input Parameters:
2809: + B - the matrix
2810: . bs - the block size
2811: . i - the indices into j for the start of each local row (starts with zero)
2812: . j - the column indices for each local row (starts with zero) these must be sorted for each row
2813: - v - optional values in the matrix
2815: Level: developer
2817: Notes: The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED. For example, C programs
2818: may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
2819: over rows within a block and the last index is over columns within a block row. Fortran programs will likely set
2820: MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2821: block column and the second index is over columns within a block.
2823: .keywords: matrix, aij, compressed row, sparse, parallel
2825: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIBAIJSetPreallocation(), MatCreateAIJ(), MPIAIJ, MatCreateMPIBAIJWithArrays(), MPIBAIJ
2826: @*/
2827: PetscErrorCode MatMPIBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2828: {
2835: PetscTryMethod(B,"MatMPIBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2836: return(0);
2837: }
2841: PetscErrorCode MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt *d_nnz,PetscInt o_nz,const PetscInt *o_nnz)
2842: {
2843: Mat_MPIBAIJ *b;
2845: PetscInt i;
2848: MatSetBlockSize(B,PetscAbs(bs));
2849: PetscLayoutSetUp(B->rmap);
2850: PetscLayoutSetUp(B->cmap);
2851: PetscLayoutGetBlockSize(B->rmap,&bs);
2853: if (d_nnz) {
2854: for (i=0; i<B->rmap->n/bs; i++) {
2855: 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]);
2856: }
2857: }
2858: if (o_nnz) {
2859: for (i=0; i<B->rmap->n/bs; i++) {
2860: 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]);
2861: }
2862: }
2864: b = (Mat_MPIBAIJ*)B->data;
2865: b->bs2 = bs*bs;
2866: b->mbs = B->rmap->n/bs;
2867: b->nbs = B->cmap->n/bs;
2868: b->Mbs = B->rmap->N/bs;
2869: b->Nbs = B->cmap->N/bs;
2871: for (i=0; i<=b->size; i++) {
2872: b->rangebs[i] = B->rmap->range[i]/bs;
2873: }
2874: b->rstartbs = B->rmap->rstart/bs;
2875: b->rendbs = B->rmap->rend/bs;
2876: b->cstartbs = B->cmap->rstart/bs;
2877: b->cendbs = B->cmap->rend/bs;
2879: if (!B->preallocated) {
2880: MatCreate(PETSC_COMM_SELF,&b->A);
2881: MatSetSizes(b->A,B->rmap->n,B->cmap->n,B->rmap->n,B->cmap->n);
2882: MatSetType(b->A,MATSEQBAIJ);
2883: PetscLogObjectParent((PetscObject)B,(PetscObject)b->A);
2884: MatCreate(PETSC_COMM_SELF,&b->B);
2885: MatSetSizes(b->B,B->rmap->n,B->cmap->N,B->rmap->n,B->cmap->N);
2886: MatSetType(b->B,MATSEQBAIJ);
2887: PetscLogObjectParent((PetscObject)B,(PetscObject)b->B);
2888: MatStashCreate_Private(PetscObjectComm((PetscObject)B),bs,&B->bstash);
2889: }
2891: MatSeqBAIJSetPreallocation(b->A,bs,d_nz,d_nnz);
2892: MatSeqBAIJSetPreallocation(b->B,bs,o_nz,o_nnz);
2893: B->preallocated = PETSC_TRUE;
2894: return(0);
2895: }
2897: extern PetscErrorCode MatDiagonalScaleLocal_MPIBAIJ(Mat,Vec);
2898: extern PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat,PetscReal);
2902: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype,MatReuse reuse,Mat *adj)
2903: {
2904: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)B->data;
2906: Mat_SeqBAIJ *d = (Mat_SeqBAIJ*) b->A->data,*o = (Mat_SeqBAIJ*) b->B->data;
2907: PetscInt M = B->rmap->n/B->rmap->bs,i,*ii,*jj,cnt,j,k,rstart = B->rmap->rstart/B->rmap->bs;
2908: const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
2911: PetscMalloc1((M+1),&ii);
2912: ii[0] = 0;
2913: for (i=0; i<M; i++) {
2914: 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]);
2915: 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]);
2916: ii[i+1] = ii[i] + id[i+1] - id[i] + io[i+1] - io[i];
2917: /* remove one from count of matrix has diagonal */
2918: for (j=id[i]; j<id[i+1]; j++) {
2919: if (jd[j] == i) {ii[i+1]--;break;}
2920: }
2921: }
2922: PetscMalloc1(ii[M],&jj);
2923: cnt = 0;
2924: for (i=0; i<M; i++) {
2925: for (j=io[i]; j<io[i+1]; j++) {
2926: if (garray[jo[j]] > rstart) break;
2927: jj[cnt++] = garray[jo[j]];
2928: }
2929: for (k=id[i]; k<id[i+1]; k++) {
2930: if (jd[k] != i) {
2931: jj[cnt++] = rstart + jd[k];
2932: }
2933: }
2934: for (; j<io[i+1]; j++) {
2935: jj[cnt++] = garray[jo[j]];
2936: }
2937: }
2938: MatCreateMPIAdj(PetscObjectComm((PetscObject)B),M,B->cmap->N/B->rmap->bs,ii,jj,NULL,adj);
2939: return(0);
2940: }
2942: #include <../src/mat/impls/aij/mpi/mpiaij.h>
2944: PETSC_EXTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat,MatType,MatReuse,Mat*);
2948: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
2949: {
2951: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
2952: Mat B;
2953: Mat_MPIAIJ *b;
2956: if (!A->assembled) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"Matrix must be assembled");
2958: MatCreate(PetscObjectComm((PetscObject)A),&B);
2959: MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
2960: MatSetType(B,MATMPIAIJ);
2961: MatSeqAIJSetPreallocation(B,0,NULL);
2962: MatMPIAIJSetPreallocation(B,0,NULL,0,NULL);
2963: b = (Mat_MPIAIJ*) B->data;
2965: MatDestroy(&b->A);
2966: MatDestroy(&b->B);
2967: MatDisAssemble_MPIBAIJ(A);
2968: MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A);
2969: MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B);
2970: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2971: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2972: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2973: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2974: if (reuse == MAT_REUSE_MATRIX) {
2975: MatHeaderReplace(A,B);
2976: } else {
2977: *newmat = B;
2978: }
2979: return(0);
2980: }
2982: #if defined(PETSC_HAVE_MUMPS)
2983: PETSC_EXTERN PetscErrorCode MatGetFactor_baij_mumps(Mat,MatFactorType,Mat*);
2984: #endif
2986: /*MC
2987: MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
2989: Options Database Keys:
2990: + -mat_type mpibaij - sets the matrix type to "mpibaij" during a call to MatSetFromOptions()
2991: . -mat_block_size <bs> - set the blocksize used to store the matrix
2992: - -mat_use_hash_table <fact>
2994: Level: beginner
2996: .seealso: MatCreateMPIBAIJ
2997: M*/
2999: PETSC_EXTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBSTRM(Mat,MatType,MatReuse,Mat*);
3003: PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
3004: {
3005: Mat_MPIBAIJ *b;
3007: PetscBool flg;
3010: PetscNewLog(B,&b);
3011: B->data = (void*)b;
3013: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
3014: B->assembled = PETSC_FALSE;
3016: B->insertmode = NOT_SET_VALUES;
3017: MPI_Comm_rank(PetscObjectComm((PetscObject)B),&b->rank);
3018: MPI_Comm_size(PetscObjectComm((PetscObject)B),&b->size);
3020: /* build local table of row and column ownerships */
3021: PetscMalloc1((b->size+1),&b->rangebs);
3023: /* build cache for off array entries formed */
3024: MatStashCreate_Private(PetscObjectComm((PetscObject)B),1,&B->stash);
3026: b->donotstash = PETSC_FALSE;
3027: b->colmap = NULL;
3028: b->garray = NULL;
3029: b->roworiented = PETSC_TRUE;
3031: /* stuff used in block assembly */
3032: b->barray = 0;
3034: /* stuff used for matrix vector multiply */
3035: b->lvec = 0;
3036: b->Mvctx = 0;
3038: /* stuff for MatGetRow() */
3039: b->rowindices = 0;
3040: b->rowvalues = 0;
3041: b->getrowactive = PETSC_FALSE;
3043: /* hash table stuff */
3044: b->ht = 0;
3045: b->hd = 0;
3046: b->ht_size = 0;
3047: b->ht_flag = PETSC_FALSE;
3048: b->ht_fact = 0;
3049: b->ht_total_ct = 0;
3050: b->ht_insert_ct = 0;
3052: /* stuff for MatGetSubMatrices_MPIBAIJ_local() */
3053: b->ijonly = PETSC_FALSE;
3055: PetscOptionsBegin(PetscObjectComm((PetscObject)B),NULL,"Options for loading MPIBAIJ matrix 1","Mat");
3056: PetscOptionsBool("-mat_use_hash_table","Use hash table to save memory in constructing matrix","MatSetOption",PETSC_FALSE,&flg,NULL);
3057: if (flg) {
3058: PetscReal fact = 1.39;
3059: MatSetOption(B,MAT_USE_HASH_TABLE,PETSC_TRUE);
3060: PetscOptionsReal("-mat_use_hash_table","Use hash table factor","MatMPIBAIJSetHashTableFactor",fact,&fact,NULL);
3061: if (fact <= 1.0) fact = 1.39;
3062: MatMPIBAIJSetHashTableFactor(B,fact);
3063: PetscInfo1(B,"Hash table Factor used %5.2f\n",fact);
3064: }
3065: PetscOptionsEnd();
3067: #if defined(PETSC_HAVE_MUMPS)
3068: PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_baij_mumps);
3069: #endif
3070: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiadj_C",MatConvert_MPIBAIJ_MPIAdj);
3071: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpiaij_C",MatConvert_MPIBAIJ_MPIAIJ);
3072: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpisbaij_C",MatConvert_MPIBAIJ_MPISBAIJ);
3073: PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_MPIBAIJ);
3074: PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_MPIBAIJ);
3075: PetscObjectComposeFunction((PetscObject)B,"MatGetDiagonalBlock_C",MatGetDiagonalBlock_MPIBAIJ);
3076: PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocation_C",MatMPIBAIJSetPreallocation_MPIBAIJ);
3077: PetscObjectComposeFunction((PetscObject)B,"MatMPIBAIJSetPreallocationCSR_C",MatMPIBAIJSetPreallocationCSR_MPIBAIJ);
3078: PetscObjectComposeFunction((PetscObject)B,"MatDiagonalScaleLocal_C",MatDiagonalScaleLocal_MPIBAIJ);
3079: PetscObjectComposeFunction((PetscObject)B,"MatSetHashTableFactor_C",MatSetHashTableFactor_MPIBAIJ);
3080: PetscObjectComposeFunction((PetscObject)B,"MatConvert_mpibaij_mpibstrm_C",MatConvert_MPIBAIJ_MPIBSTRM);
3081: PetscObjectChangeTypeName((PetscObject)B,MATMPIBAIJ);
3082: return(0);
3083: }
3085: /*MC
3086: MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3088: This matrix type is identical to MATSEQBAIJ when constructed with a single process communicator,
3089: and MATMPIBAIJ otherwise.
3091: Options Database Keys:
3092: . -mat_type baij - sets the matrix type to "baij" during a call to MatSetFromOptions()
3094: Level: beginner
3096: .seealso: MatCreateBAIJ(),MATSEQBAIJ,MATMPIBAIJ, MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3097: M*/
3101: /*@C
3102: MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in block AIJ format
3103: (block compressed row). For good matrix assembly performance
3104: the user should preallocate the matrix storage by setting the parameters
3105: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
3106: performance can be increased by more than a factor of 50.
3108: Collective on Mat
3110: Input Parameters:
3111: + B - the matrix
3112: . bs - size of block
3113: . d_nz - number of block nonzeros per block row in diagonal portion of local
3114: submatrix (same for all local rows)
3115: . d_nnz - array containing the number of block nonzeros in the various block rows
3116: of the in diagonal portion of the local (possibly different for each block
3117: row) or NULL. If you plan to factor the matrix you must leave room for the diagonal entry and
3118: set it even if it is zero.
3119: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
3120: submatrix (same for all local rows).
3121: - o_nnz - array containing the number of nonzeros in the various block rows of the
3122: off-diagonal portion of the local submatrix (possibly different for
3123: each block row) or NULL.
3125: If the *_nnz parameter is given then the *_nz parameter is ignored
3127: Options Database Keys:
3128: + -mat_block_size - size of the blocks to use
3129: - -mat_use_hash_table <fact>
3131: Notes:
3132: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
3133: than it must be used on all processors that share the object for that argument.
3135: Storage Information:
3136: For a square global matrix we define each processor's diagonal portion
3137: to be its local rows and the corresponding columns (a square submatrix);
3138: each processor's off-diagonal portion encompasses the remainder of the
3139: local matrix (a rectangular submatrix).
3141: The user can specify preallocated storage for the diagonal part of
3142: the local submatrix with either d_nz or d_nnz (not both). Set
3143: d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3144: memory allocation. Likewise, specify preallocated storage for the
3145: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3147: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3148: the figure below we depict these three local rows and all columns (0-11).
3150: .vb
3151: 0 1 2 3 4 5 6 7 8 9 10 11
3152: --------------------------
3153: row 3 |o o o d d d o o o o o o
3154: row 4 |o o o d d d o o o o o o
3155: row 5 |o o o d d d o o o o o o
3156: --------------------------
3157: .ve
3159: Thus, any entries in the d locations are stored in the d (diagonal)
3160: submatrix, and any entries in the o locations are stored in the
3161: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3162: stored simply in the MATSEQBAIJ format for compressed row storage.
3164: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3165: and o_nz should indicate the number of block nonzeros per row in the o matrix.
3166: In general, for PDE problems in which most nonzeros are near the diagonal,
3167: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
3168: or you will get TERRIBLE performance; see the users' manual chapter on
3169: matrices.
3171: You can call MatGetInfo() to get information on how effective the preallocation was;
3172: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3173: You can also run with the option -info and look for messages with the string
3174: malloc in them to see if additional memory allocation was needed.
3176: Level: intermediate
3178: .keywords: matrix, block, aij, compressed row, sparse, parallel
3180: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocationCSR(), PetscSplitOwnership()
3181: @*/
3182: PetscErrorCode MatMPIBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
3183: {
3190: PetscTryMethod(B,"MatMPIBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[],PetscInt,const PetscInt[]),(B,bs,d_nz,d_nnz,o_nz,o_nnz));
3191: return(0);
3192: }
3196: /*@C
3197: MatCreateBAIJ - Creates a sparse parallel matrix in block AIJ format
3198: (block compressed row). For good matrix assembly performance
3199: the user should preallocate the matrix storage by setting the parameters
3200: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
3201: performance can be increased by more than a factor of 50.
3203: Collective on MPI_Comm
3205: Input Parameters:
3206: + comm - MPI communicator
3207: . bs - size of blockk
3208: . m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
3209: This value should be the same as the local size used in creating the
3210: y vector for the matrix-vector product y = Ax.
3211: . n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
3212: This value should be the same as the local size used in creating the
3213: x vector for the matrix-vector product y = Ax.
3214: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3215: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3216: . d_nz - number of nonzero blocks per block row in diagonal portion of local
3217: submatrix (same for all local rows)
3218: . d_nnz - array containing the number of nonzero blocks in the various block rows
3219: of the in diagonal portion of the local (possibly different for each block
3220: row) or NULL. If you plan to factor the matrix you must leave room for the diagonal entry
3221: and set it even if it is zero.
3222: . o_nz - number of nonzero blocks per block row in the off-diagonal portion of local
3223: submatrix (same for all local rows).
3224: - o_nnz - array containing the number of nonzero blocks in the various block rows of the
3225: off-diagonal portion of the local submatrix (possibly different for
3226: each block row) or NULL.
3228: Output Parameter:
3229: . A - the matrix
3231: Options Database Keys:
3232: + -mat_block_size - size of the blocks to use
3233: - -mat_use_hash_table <fact>
3235: It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3236: MatXXXXSetPreallocation() paradgm instead of this routine directly.
3237: [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
3239: Notes:
3240: If the *_nnz parameter is given then the *_nz parameter is ignored
3242: A nonzero block is any block that as 1 or more nonzeros in it
3244: The user MUST specify either the local or global matrix dimensions
3245: (possibly both).
3247: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
3248: than it must be used on all processors that share the object for that argument.
3250: Storage Information:
3251: For a square global matrix we define each processor's diagonal portion
3252: to be its local rows and the corresponding columns (a square submatrix);
3253: each processor's off-diagonal portion encompasses the remainder of the
3254: local matrix (a rectangular submatrix).
3256: The user can specify preallocated storage for the diagonal part of
3257: the local submatrix with either d_nz or d_nnz (not both). Set
3258: d_nz=PETSC_DEFAULT and d_nnz=NULL for PETSc to control dynamic
3259: memory allocation. Likewise, specify preallocated storage for the
3260: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
3262: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3263: the figure below we depict these three local rows and all columns (0-11).
3265: .vb
3266: 0 1 2 3 4 5 6 7 8 9 10 11
3267: --------------------------
3268: row 3 |o o o d d d o o o o o o
3269: row 4 |o o o d d d o o o o o o
3270: row 5 |o o o d d d o o o o o o
3271: --------------------------
3272: .ve
3274: Thus, any entries in the d locations are stored in the d (diagonal)
3275: submatrix, and any entries in the o locations are stored in the
3276: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3277: stored simply in the MATSEQBAIJ format for compressed row storage.
3279: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
3280: and o_nz should indicate the number of block nonzeros per row in the o matrix.
3281: In general, for PDE problems in which most nonzeros are near the diagonal,
3282: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
3283: or you will get TERRIBLE performance; see the users' manual chapter on
3284: matrices.
3286: Level: intermediate
3288: .keywords: matrix, block, aij, compressed row, sparse, parallel
3290: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateBAIJ(), MatMPIBAIJSetPreallocation(), MatMPIBAIJSetPreallocationCSR()
3291: @*/
3292: 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)
3293: {
3295: PetscMPIInt size;
3298: MatCreate(comm,A);
3299: MatSetSizes(*A,m,n,M,N);
3300: MPI_Comm_size(comm,&size);
3301: if (size > 1) {
3302: MatSetType(*A,MATMPIBAIJ);
3303: MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
3304: } else {
3305: MatSetType(*A,MATSEQBAIJ);
3306: MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
3307: }
3308: return(0);
3309: }
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);
3325: PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
3327: mat->factortype = matin->factortype;
3328: mat->preallocated = PETSC_TRUE;
3329: mat->assembled = PETSC_TRUE;
3330: mat->insertmode = NOT_SET_VALUES;
3332: a = (Mat_MPIBAIJ*)mat->data;
3333: mat->rmap->bs = matin->rmap->bs;
3334: a->bs2 = oldmat->bs2;
3335: a->mbs = oldmat->mbs;
3336: a->nbs = oldmat->nbs;
3337: a->Mbs = oldmat->Mbs;
3338: a->Nbs = oldmat->Nbs;
3340: PetscLayoutReference(matin->rmap,&mat->rmap);
3341: PetscLayoutReference(matin->cmap,&mat->cmap);
3343: a->size = oldmat->size;
3344: a->rank = oldmat->rank;
3345: a->donotstash = oldmat->donotstash;
3346: a->roworiented = oldmat->roworiented;
3347: a->rowindices = 0;
3348: a->rowvalues = 0;
3349: a->getrowactive = PETSC_FALSE;
3350: a->barray = 0;
3351: a->rstartbs = oldmat->rstartbs;
3352: a->rendbs = oldmat->rendbs;
3353: a->cstartbs = oldmat->cstartbs;
3354: a->cendbs = oldmat->cendbs;
3356: /* hash table stuff */
3357: a->ht = 0;
3358: a->hd = 0;
3359: a->ht_size = 0;
3360: a->ht_flag = oldmat->ht_flag;
3361: a->ht_fact = oldmat->ht_fact;
3362: a->ht_total_ct = 0;
3363: a->ht_insert_ct = 0;
3365: PetscMemcpy(a->rangebs,oldmat->rangebs,(a->size+1)*sizeof(PetscInt));
3366: if (oldmat->colmap) {
3367: #if defined(PETSC_USE_CTABLE)
3368: PetscTableCreateCopy(oldmat->colmap,&a->colmap);
3369: #else
3370: PetscMalloc1((a->Nbs),&a->colmap);
3371: PetscLogObjectMemory((PetscObject)mat,(a->Nbs)*sizeof(PetscInt));
3372: PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(PetscInt));
3373: #endif
3374: } else a->colmap = 0;
3376: if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
3377: PetscMalloc1(len,&a->garray);
3378: PetscLogObjectMemory((PetscObject)mat,len*sizeof(PetscInt));
3379: PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt));
3380: } else a->garray = 0;
3382: MatStashCreate_Private(PetscObjectComm((PetscObject)matin),matin->rmap->bs,&mat->bstash);
3383: VecDuplicate(oldmat->lvec,&a->lvec);
3384: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->lvec);
3385: VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
3386: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->Mvctx);
3388: MatDuplicate(oldmat->A,cpvalues,&a->A);
3389: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->A);
3390: MatDuplicate(oldmat->B,cpvalues,&a->B);
3391: PetscLogObjectParent((PetscObject)mat,(PetscObject)a->B);
3392: PetscFunctionListDuplicate(((PetscObject)matin)->qlist,&((PetscObject)mat)->qlist);
3393: *newmat = mat;
3394: return(0);
3395: }
3399: PetscErrorCode MatLoad_MPIBAIJ(Mat newmat,PetscViewer viewer)
3400: {
3402: int fd;
3403: PetscInt i,nz,j,rstart,rend;
3404: PetscScalar *vals,*buf;
3405: MPI_Comm comm;
3406: MPI_Status status;
3407: PetscMPIInt rank,size,maxnz;
3408: PetscInt header[4],*rowlengths = 0,M,N,m,*rowners,*cols;
3409: PetscInt *locrowlens = NULL,*procsnz = NULL,*browners = NULL;
3410: PetscInt jj,*mycols,*ibuf,bs=1,Mbs,mbs,extra_rows,mmax;
3411: PetscMPIInt tag = ((PetscObject)viewer)->tag;
3412: PetscInt *dlens = NULL,*odlens = NULL,*mask = NULL,*masked1 = NULL,*masked2 = NULL,rowcount,odcount;
3413: PetscInt dcount,kmax,k,nzcount,tmp,mend,sizesset=1,grows,gcols;
3416: PetscObjectGetComm((PetscObject)viewer,&comm);
3417: PetscOptionsBegin(comm,NULL,"Options for loading MPIBAIJ matrix 2","Mat");
3418: PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
3419: PetscOptionsEnd();
3421: MPI_Comm_size(comm,&size);
3422: MPI_Comm_rank(comm,&rank);
3423: if (!rank) {
3424: PetscViewerBinaryGetDescriptor(viewer,&fd);
3425: PetscBinaryRead(fd,(char*)header,4,PETSC_INT);
3426: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
3427: }
3429: if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) sizesset = 0;
3431: MPI_Bcast(header+1,3,MPIU_INT,0,comm);
3432: M = header[1]; N = header[2];
3434: /* If global rows/cols are set to PETSC_DECIDE, set it to the sizes given in the file */
3435: if (sizesset && newmat->rmap->N < 0) newmat->rmap->N = M;
3436: if (sizesset && newmat->cmap->N < 0) newmat->cmap->N = N;
3438: /* If global sizes are set, check if they are consistent with that given in the file */
3439: if (sizesset) {
3440: MatGetSize(newmat,&grows,&gcols);
3441: }
3442: if (sizesset && newmat->rmap->N != grows) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of rows:Matrix in file has (%d) and input matrix has (%d)",M,grows);
3443: if (sizesset && newmat->cmap->N != gcols) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Inconsistent # of cols:Matrix in file has (%d) and input matrix has (%d)",N,gcols);
3445: if (M != N) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Can only do square matrices");
3447: /*
3448: This code adds extra rows to make sure the number of rows is
3449: divisible by the blocksize
3450: */
3451: Mbs = M/bs;
3452: extra_rows = bs - M + bs*Mbs;
3453: if (extra_rows == bs) extra_rows = 0;
3454: else Mbs++;
3455: if (extra_rows && !rank) {
3456: PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
3457: }
3459: /* determine ownership of all rows */
3460: if (newmat->rmap->n < 0) { /* PETSC_DECIDE */
3461: mbs = Mbs/size + ((Mbs % size) > rank);
3462: m = mbs*bs;
3463: } else { /* User set */
3464: m = newmat->rmap->n;
3465: mbs = m/bs;
3466: }
3467: PetscMalloc2(size+1,&rowners,size+1,&browners);
3468: MPI_Allgather(&mbs,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);
3470: /* process 0 needs enough room for process with most rows */
3471: if (!rank) {
3472: mmax = rowners[1];
3473: for (i=2; i<=size; i++) {
3474: mmax = PetscMax(mmax,rowners[i]);
3475: }
3476: mmax*=bs;
3477: } else mmax = -1; /* unused, but compiler warns anyway */
3479: rowners[0] = 0;
3480: for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
3481: for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
3482: rstart = rowners[rank];
3483: rend = rowners[rank+1];
3485: /* distribute row lengths to all processors */
3486: PetscMalloc1(m,&locrowlens);
3487: if (!rank) {
3488: mend = m;
3489: if (size == 1) mend = mend - extra_rows;
3490: PetscBinaryRead(fd,locrowlens,mend,PETSC_INT);
3491: for (j=mend; j<m; j++) locrowlens[j] = 1;
3492: PetscMalloc1(mmax,&rowlengths);
3493: PetscCalloc1(size,&procsnz);
3494: for (j=0; j<m; j++) {
3495: procsnz[0] += locrowlens[j];
3496: }
3497: for (i=1; i<size; i++) {
3498: mend = browners[i+1] - browners[i];
3499: if (i == size-1) mend = mend - extra_rows;
3500: PetscBinaryRead(fd,rowlengths,mend,PETSC_INT);
3501: for (j=mend; j<browners[i+1] - browners[i]; j++) rowlengths[j] = 1;
3502: /* calculate the number of nonzeros on each processor */
3503: for (j=0; j<browners[i+1]-browners[i]; j++) {
3504: procsnz[i] += rowlengths[j];
3505: }
3506: MPI_Send(rowlengths,browners[i+1]-browners[i],MPIU_INT,i,tag,comm);
3507: }
3508: PetscFree(rowlengths);
3509: } else {
3510: MPI_Recv(locrowlens,m,MPIU_INT,0,tag,comm,&status);
3511: }
3513: if (!rank) {
3514: /* determine max buffer needed and allocate it */
3515: maxnz = procsnz[0];
3516: for (i=1; i<size; i++) {
3517: maxnz = PetscMax(maxnz,procsnz[i]);
3518: }
3519: PetscMalloc1(maxnz,&cols);
3521: /* read in my part of the matrix column indices */
3522: nz = procsnz[0];
3523: PetscMalloc1((nz+1),&ibuf);
3524: mycols = ibuf;
3525: if (size == 1) nz -= extra_rows;
3526: PetscBinaryRead(fd,mycols,nz,PETSC_INT);
3527: if (size == 1) {
3528: for (i=0; i< extra_rows; i++) mycols[nz+i] = M+i;
3529: }
3531: /* read in every ones (except the last) and ship off */
3532: for (i=1; i<size-1; i++) {
3533: nz = procsnz[i];
3534: PetscBinaryRead(fd,cols,nz,PETSC_INT);
3535: MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
3536: }
3537: /* read in the stuff for the last proc */
3538: if (size != 1) {
3539: nz = procsnz[size-1] - extra_rows; /* the extra rows are not on the disk */
3540: PetscBinaryRead(fd,cols,nz,PETSC_INT);
3541: for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
3542: MPI_Send(cols,nz+extra_rows,MPIU_INT,size-1,tag,comm);
3543: }
3544: PetscFree(cols);
3545: } else {
3546: /* determine buffer space needed for message */
3547: nz = 0;
3548: for (i=0; i<m; i++) {
3549: nz += locrowlens[i];
3550: }
3551: PetscMalloc1((nz+1),&ibuf);
3552: mycols = ibuf;
3553: /* receive message of column indices*/
3554: MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
3555: MPI_Get_count(&status,MPIU_INT,&maxnz);
3556: if (maxnz != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
3557: }
3559: /* loop over local rows, determining number of off diagonal entries */
3560: PetscMalloc2(rend-rstart,&dlens,rend-rstart,&odlens);
3561: PetscCalloc3(Mbs,&mask,Mbs,&masked1,Mbs,&masked2);
3562: rowcount = 0; nzcount = 0;
3563: for (i=0; i<mbs; i++) {
3564: dcount = 0;
3565: odcount = 0;
3566: for (j=0; j<bs; j++) {
3567: kmax = locrowlens[rowcount];
3568: for (k=0; k<kmax; k++) {
3569: tmp = mycols[nzcount++]/bs;
3570: if (!mask[tmp]) {
3571: mask[tmp] = 1;
3572: if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
3573: else masked1[dcount++] = tmp;
3574: }
3575: }
3576: rowcount++;
3577: }
3579: dlens[i] = dcount;
3580: odlens[i] = odcount;
3582: /* zero out the mask elements we set */
3583: for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
3584: for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
3585: }
3588: if (!sizesset) {
3589: MatSetSizes(newmat,m,m,M+extra_rows,N+extra_rows);
3590: }
3591: MatMPIBAIJSetPreallocation(newmat,bs,0,dlens,0,odlens);
3593: if (!rank) {
3594: PetscMalloc1((maxnz+1),&buf);
3595: /* read in my part of the matrix numerical values */
3596: nz = procsnz[0];
3597: vals = buf;
3598: mycols = ibuf;
3599: if (size == 1) nz -= extra_rows;
3600: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3601: if (size == 1) {
3602: for (i=0; i< extra_rows; i++) vals[nz+i] = 1.0;
3603: }
3605: /* insert into matrix */
3606: jj = rstart*bs;
3607: for (i=0; i<m; i++) {
3608: MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3609: mycols += locrowlens[i];
3610: vals += locrowlens[i];
3611: jj++;
3612: }
3613: /* read in other processors (except the last one) and ship out */
3614: for (i=1; i<size-1; i++) {
3615: nz = procsnz[i];
3616: vals = buf;
3617: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3618: MPIULong_Send(vals,nz,MPIU_SCALAR,i,((PetscObject)newmat)->tag,comm);
3619: }
3620: /* the last proc */
3621: if (size != 1) {
3622: nz = procsnz[i] - extra_rows;
3623: vals = buf;
3624: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
3625: for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
3626: MPIULong_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,((PetscObject)newmat)->tag,comm);
3627: }
3628: PetscFree(procsnz);
3629: } else {
3630: /* receive numeric values */
3631: PetscMalloc1((nz+1),&buf);
3633: /* receive message of values*/
3634: vals = buf;
3635: mycols = ibuf;
3636: MPIULong_Recv(vals,nz,MPIU_SCALAR,0,((PetscObject)newmat)->tag,comm);
3638: /* insert into matrix */
3639: jj = rstart*bs;
3640: for (i=0; i<m; i++) {
3641: MatSetValues_MPIBAIJ(newmat,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
3642: mycols += locrowlens[i];
3643: vals += locrowlens[i];
3644: jj++;
3645: }
3646: }
3647: PetscFree(locrowlens);
3648: PetscFree(buf);
3649: PetscFree(ibuf);
3650: PetscFree2(rowners,browners);
3651: PetscFree2(dlens,odlens);
3652: PetscFree3(mask,masked1,masked2);
3653: MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
3654: MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
3655: return(0);
3656: }
3660: /*@
3661: MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
3663: Input Parameters:
3664: . mat - the matrix
3665: . fact - factor
3667: Not Collective, each process can use a different factor
3669: Level: advanced
3671: Notes:
3672: This can also be set by the command line option: -mat_use_hash_table <fact>
3674: .keywords: matrix, hashtable, factor, HT
3676: .seealso: MatSetOption()
3677: @*/
3678: PetscErrorCode MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
3679: {
3683: PetscTryMethod(mat,"MatSetHashTableFactor_C",(Mat,PetscReal),(mat,fact));
3684: return(0);
3685: }
3689: PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat mat,PetscReal fact)
3690: {
3691: Mat_MPIBAIJ *baij;
3694: baij = (Mat_MPIBAIJ*)mat->data;
3695: baij->ht_fact = fact;
3696: return(0);
3697: }
3701: PetscErrorCode MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,const PetscInt *colmap[])
3702: {
3703: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
3706: if (Ad) *Ad = a->A;
3707: if (Ao) *Ao = a->B;
3708: if (colmap) *colmap = a->garray;
3709: return(0);
3710: }
3712: /*
3713: Special version for direct calls from Fortran (to eliminate two function call overheads
3714: */
3715: #if defined(PETSC_HAVE_FORTRAN_CAPS)
3716: #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
3717: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
3718: #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
3719: #endif
3723: /*@C
3724: MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to MatSetValuesBlocked()
3726: Collective on Mat
3728: Input Parameters:
3729: + mat - the matrix
3730: . min - number of input rows
3731: . im - input rows
3732: . nin - number of input columns
3733: . in - input columns
3734: . v - numerical values input
3735: - addvin - INSERT_VALUES or ADD_VALUES
3737: Notes: This has a complete copy of MatSetValuesBlocked_MPIBAIJ() which is terrible code un-reuse.
3739: Level: advanced
3741: .seealso: MatSetValuesBlocked()
3742: @*/
3743: PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin,PetscInt *min,const PetscInt im[],PetscInt *nin,const PetscInt in[],const MatScalar v[],InsertMode *addvin)
3744: {
3745: /* convert input arguments to C version */
3746: Mat mat = *matin;
3747: PetscInt m = *min, n = *nin;
3748: InsertMode addv = *addvin;
3750: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
3751: const MatScalar *value;
3752: MatScalar *barray = baij->barray;
3753: PetscBool roworiented = baij->roworiented;
3754: PetscErrorCode ierr;
3755: PetscInt i,j,ii,jj,row,col,rstart=baij->rstartbs;
3756: PetscInt rend=baij->rendbs,cstart=baij->cstartbs,stepval;
3757: PetscInt cend=baij->cendbs,bs=mat->rmap->bs,bs2=baij->bs2;
3760: /* tasks normally handled by MatSetValuesBlocked() */
3761: if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
3762: #if defined(PETSC_USE_DEBUG)
3763: else if (mat->insertmode != addv) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
3764: if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3765: #endif
3766: if (mat->assembled) {
3767: mat->was_assembled = PETSC_TRUE;
3768: mat->assembled = PETSC_FALSE;
3769: }
3770: PetscLogEventBegin(MAT_SetValues,mat,0,0,0);
3773: if (!barray) {
3774: PetscMalloc1(bs2,&barray);
3775: baij->barray = barray;
3776: }
3778: if (roworiented) stepval = (n-1)*bs;
3779: else stepval = (m-1)*bs;
3781: for (i=0; i<m; i++) {
3782: if (im[i] < 0) continue;
3783: #if defined(PETSC_USE_DEBUG)
3784: 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);
3785: #endif
3786: if (im[i] >= rstart && im[i] < rend) {
3787: row = im[i] - rstart;
3788: for (j=0; j<n; j++) {
3789: /* If NumCol = 1 then a copy is not required */
3790: if ((roworiented) && (n == 1)) {
3791: barray = (MatScalar*)v + i*bs2;
3792: } else if ((!roworiented) && (m == 1)) {
3793: barray = (MatScalar*)v + j*bs2;
3794: } else { /* Here a copy is required */
3795: if (roworiented) {
3796: value = v + i*(stepval+bs)*bs + j*bs;
3797: } else {
3798: value = v + j*(stepval+bs)*bs + i*bs;
3799: }
3800: for (ii=0; ii<bs; ii++,value+=stepval) {
3801: for (jj=0; jj<bs; jj++) {
3802: *barray++ = *value++;
3803: }
3804: }
3805: barray -=bs2;
3806: }
3808: if (in[j] >= cstart && in[j] < cend) {
3809: col = in[j] - cstart;
3810: MatSetValuesBlocked_SeqBAIJ(baij->A,1,&row,1,&col,barray,addv);
3811: } else if (in[j] < 0) continue;
3812: #if defined(PETSC_USE_DEBUG)
3813: 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);
3814: #endif
3815: else {
3816: if (mat->was_assembled) {
3817: if (!baij->colmap) {
3818: MatCreateColmap_MPIBAIJ_Private(mat);
3819: }
3821: #if defined(PETSC_USE_DEBUG)
3822: #if defined(PETSC_USE_CTABLE)
3823: { PetscInt data;
3824: PetscTableFind(baij->colmap,in[j]+1,&data);
3825: if ((data - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3826: }
3827: #else
3828: if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect colmap");
3829: #endif
3830: #endif
3831: #if defined(PETSC_USE_CTABLE)
3832: PetscTableFind(baij->colmap,in[j]+1,&col);
3833: col = (col - 1)/bs;
3834: #else
3835: col = (baij->colmap[in[j]] - 1)/bs;
3836: #endif
3837: if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
3838: MatDisAssemble_MPIBAIJ(mat);
3839: col = in[j];
3840: }
3841: } else col = in[j];
3842: MatSetValuesBlocked_SeqBAIJ(baij->B,1,&row,1,&col,barray,addv);
3843: }
3844: }
3845: } else {
3846: if (!baij->donotstash) {
3847: if (roworiented) {
3848: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3849: } else {
3850: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
3851: }
3852: }
3853: }
3854: }
3856: /* task normally handled by MatSetValuesBlocked() */
3857: PetscLogEventEnd(MAT_SetValues,mat,0,0,0);
3858: return(0);
3859: }
3863: /*@
3864: MatCreateMPIBAIJWithArrays - creates a MPI BAIJ matrix using arrays that contain in standard
3865: CSR format the local rows.
3867: Collective on MPI_Comm
3869: Input Parameters:
3870: + comm - MPI communicator
3871: . bs - the block size, only a block size of 1 is supported
3872: . m - number of local rows (Cannot be PETSC_DECIDE)
3873: . n - This value should be the same as the local size used in creating the
3874: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
3875: calculated if N is given) For square matrices n is almost always m.
3876: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
3877: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
3878: . i - row indices
3879: . j - column indices
3880: - a - matrix values
3882: Output Parameter:
3883: . mat - the matrix
3885: Level: intermediate
3887: Notes:
3888: The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
3889: thus you CANNOT change the matrix entries by changing the values of a[] after you have
3890: called this routine. Use MatCreateMPIAIJWithSplitArrays() to avoid needing to copy the arrays.
3892: The order of the entries in values is the same as the block compressed sparse row storage format; that is, it is
3893: the same as a three dimensional array in Fortran values(bs,bs,nnz) that contains the first column of the first
3894: block, followed by the second column of the first block etc etc. That is, the blocks are contiguous in memory
3895: with column-major ordering within blocks.
3897: The i and j indices are 0 based, and i indices are indices corresponding to the local j array.
3899: .keywords: matrix, aij, compressed row, sparse, parallel
3901: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
3902: MPIAIJ, MatCreateAIJ(), MatCreateMPIAIJWithSplitArrays()
3903: @*/
3904: 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)
3905: {
3909: if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
3910: if (m < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
3911: MatCreate(comm,mat);
3912: MatSetSizes(*mat,m,n,M,N);
3913: MatSetType(*mat,MATMPISBAIJ);
3914: MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_FALSE);
3915: MatMPIBAIJSetPreallocationCSR(*mat,bs,i,j,a);
3916: MatSetOption(*mat,MAT_ROW_ORIENTED,PETSC_TRUE);
3917: return(0);
3918: }