Actual source code: mpibaij.c
1: /*$Id: mpibaij.c,v 1.220 2001/04/09 15:13:50 bsmith Exp $*/
3: #include src/mat/impls/baij/mpi/mpibaij.h
4: #include src/vec/vecimpl.h
6: EXTERN int MatSetUpMultiply_MPIBAIJ(Mat);
7: EXTERN int DisAssemble_MPIBAIJ(Mat);
8: EXTERN int MatIncreaseOverlap_MPIBAIJ(Mat,int,IS *,int);
9: EXTERN int MatGetSubMatrices_MPIBAIJ(Mat,int,IS *,IS *,MatReuse,Mat **);
10: EXTERN int MatGetValues_SeqBAIJ(Mat,int,int *,int,int *,Scalar *);
11: EXTERN int MatSetValues_SeqBAIJ(Mat,int,int *,int,int *,Scalar *,InsertMode);
12: EXTERN int MatSetValuesBlocked_SeqBAIJ(Mat,int,int*,int,int*,Scalar*,InsertMode);
13: EXTERN int MatGetRow_SeqBAIJ(Mat,int,int*,int**,Scalar**);
14: EXTERN int MatRestoreRow_SeqBAIJ(Mat,int,int*,int**,Scalar**);
15: EXTERN int MatPrintHelp_SeqBAIJ(Mat);
16: EXTERN int MatZeroRows_SeqBAIJ(Mat,IS,Scalar*);
18: /* UGLY, ugly, ugly
19: When MatScalar == Scalar the function MatSetValuesBlocked_MPIBAIJ_MatScalar() does
20: not exist. Otherwise ..._MatScalar() takes matrix elements in single precision and
21: inserts them into the single precision data structure. The function MatSetValuesBlocked_MPIBAIJ()
22: converts the entries into single precision and then calls ..._MatScalar() to put them
23: into the single precision data structures.
24: */
25: #if defined(PETSC_USE_MAT_SINGLE)
26: EXTERN int MatSetValuesBlocked_SeqBAIJ_MatScalar(Mat,int,int*,int,int*,MatScalar*,InsertMode);
27: EXTERN int MatSetValues_MPIBAIJ_MatScalar(Mat,int,int*,int,int*,MatScalar*,InsertMode);
28: EXTERN int MatSetValuesBlocked_MPIBAIJ_MatScalar(Mat,int,int*,int,int*,MatScalar*,InsertMode);
29: EXTERN int MatSetValues_MPIBAIJ_HT_MatScalar(Mat,int,int*,int,int*,MatScalar*,InsertMode);
30: EXTERN int MatSetValuesBlocked_MPIBAIJ_HT_MatScalar(Mat,int,int*,int,int*,MatScalar*,InsertMode);
31: #else
32: #define MatSetValuesBlocked_SeqBAIJ_MatScalar MatSetValuesBlocked_SeqBAIJ
33: #define MatSetValues_MPIBAIJ_MatScalar MatSetValues_MPIBAIJ
34: #define MatSetValuesBlocked_MPIBAIJ_MatScalar MatSetValuesBlocked_MPIBAIJ
35: #define MatSetValues_MPIBAIJ_HT_MatScalar MatSetValues_MPIBAIJ_HT
36: #define MatSetValuesBlocked_MPIBAIJ_HT_MatScalar MatSetValuesBlocked_MPIBAIJ_HT
37: #endif
39: int MatGetRowMax_MPIBAIJ(Mat A,Vec v)
40: {
41: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
42: int ierr,i;
43: Scalar *va,*vb;
44: Vec vtmp;
47:
48: MatGetRowMax(a->A,v);
49: VecGetArray(v,&va);
51: VecCreateSeq(PETSC_COMM_SELF,A->m,&vtmp);
52: MatGetRowMax(a->B,vtmp);
53: VecGetArray(vtmp,&vb);
55: for (i=0; i<A->m; i++){
56: if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) va[i] = vb[i];
57: }
59: VecRestoreArray(v,&va);
60: VecRestoreArray(vtmp,&vb);
61: VecDestroy(vtmp);
62:
63: return(0);
64: }
66: EXTERN_C_BEGIN
67: int MatStoreValues_MPIBAIJ(Mat mat)
68: {
69: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data;
70: int ierr;
73: MatStoreValues(aij->A);
74: MatStoreValues(aij->B);
75: return(0);
76: }
77: EXTERN_C_END
79: EXTERN_C_BEGIN
80: int MatRetrieveValues_MPIBAIJ(Mat mat)
81: {
82: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data;
83: int ierr;
86: MatRetrieveValues(aij->A);
87: MatRetrieveValues(aij->B);
88: return(0);
89: }
90: EXTERN_C_END
92: /*
93: Local utility routine that creates a mapping from the global column
94: number to the local number in the off-diagonal part of the local
95: storage of the matrix. This is done in a non scable way since the
96: length of colmap equals the global matrix length.
97: */
98: static int CreateColmap_MPIBAIJ_Private(Mat mat)
99: {
100: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
101: Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)baij->B->data;
102: int nbs = B->nbs,i,bs=B->bs,ierr;
105: #if defined (PETSC_USE_CTABLE)
106: PetscTableCreate(baij->nbs,&baij->colmap);
107: for (i=0; i<nbs; i++){
108: PetscTableAdd(baij->colmap,baij->garray[i]+1,i*bs+1);
109: }
110: #else
111: PetscMalloc((baij->Nbs+1)*sizeof(int),&baij->colmap);
112: PetscLogObjectMemory(mat,baij->Nbs*sizeof(int));
113: PetscMemzero(baij->colmap,baij->Nbs*sizeof(int));
114: for (i=0; i<nbs; i++) baij->colmap[baij->garray[i]] = i*bs+1;
115: #endif
116: return(0);
117: }
119: #define CHUNKSIZE 10
121: #define MatSetValues_SeqBAIJ_A_Private(row,col,value,addv)
122: {
123:
124: brow = row/bs;
125: rp = aj + ai[brow]; ap = aa + bs2*ai[brow];
126: rmax = aimax[brow]; nrow = ailen[brow];
127: bcol = col/bs;
128: ridx = row % bs; cidx = col % bs;
129: low = 0; high = nrow;
130: while (high-low > 3) {
131: t = (low+high)/2;
132: if (rp[t] > bcol) high = t;
133: else low = t;
134: }
135: for (_i=low; _i<high; _i++) {
136: if (rp[_i] > bcol) break;
137: if (rp[_i] == bcol) {
138: bap = ap + bs2*_i + bs*cidx + ridx;
139: if (addv == ADD_VALUES) *bap += value;
140: else *bap = value;
141: goto a_noinsert;
142: }
143: }
144: if (a->nonew == 1) goto a_noinsert;
145: else if (a->nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix");
146: if (nrow >= rmax) {
147: /* there is no extra room in row, therefore enlarge */
148: int new_nz = ai[a->mbs] + CHUNKSIZE,len,*new_i,*new_j;
149: MatScalar *new_a;
150:
151: if (a->nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
152:
153: /* malloc new storage space */
154: len = new_nz*(sizeof(int)+bs2*sizeof(MatScalar))+(a->mbs+1)*sizeof(int);
155: PetscMalloc(len,&new_a);
156: new_j = (int*)(new_a + bs2*new_nz);
157: new_i = new_j + new_nz;
158:
159: /* copy over old data into new slots */
160: for (ii=0; ii<brow+1; ii++) {new_i[ii] = ai[ii];}
161: for (ii=brow+1; ii<a->mbs+1; ii++) {new_i[ii] = ai[ii]+CHUNKSIZE;}
162: PetscMemcpy(new_j,aj,(ai[brow]+nrow)*sizeof(int));
163: len = (new_nz - CHUNKSIZE - ai[brow] - nrow);
164: PetscMemcpy(new_j+ai[brow]+nrow+CHUNKSIZE,aj+ai[brow]+nrow,len*sizeof(int));
165: PetscMemcpy(new_a,aa,(ai[brow]+nrow)*bs2*sizeof(MatScalar));
166: PetscMemzero(new_a+bs2*(ai[brow]+nrow),bs2*CHUNKSIZE*sizeof(Scalar));
167: PetscMemcpy(new_a+bs2*(ai[brow]+nrow+CHUNKSIZE),
168: aa+bs2*(ai[brow]+nrow),bs2*len*sizeof(MatScalar));
169: /* free up old matrix storage */
170: PetscFree(a->a);
171: if (!a->singlemalloc) {
172: PetscFree(a->i);
173: PetscFree(a->j);
174: }
175: aa = a->a = new_a; ai = a->i = new_i; aj = a->j = new_j;
176: a->singlemalloc = PETSC_TRUE;
177:
178: rp = aj + ai[brow]; ap = aa + bs2*ai[brow];
179: rmax = aimax[brow] = aimax[brow] + CHUNKSIZE;
180: PetscLogObjectMemory(A,CHUNKSIZE*(sizeof(int) + bs2*sizeof(MatScalar)));
181: a->maxnz += bs2*CHUNKSIZE;
182: a->reallocs++;
183: a->nz++;
184: }
185: N = nrow++ - 1;
186: /* shift up all the later entries in this row */
187: for (ii=N; ii>=_i; ii--) {
188: rp[ii+1] = rp[ii];
189: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
190: }
191: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar)); }
192: rp[_i] = bcol;
193: ap[bs2*_i + bs*cidx + ridx] = value;
194: a_noinsert:;
195: ailen[brow] = nrow;
196: }
198: #define MatSetValues_SeqBAIJ_B_Private(row,col,value,addv)
199: {
200: brow = row/bs;
201: rp = bj + bi[brow]; ap = ba + bs2*bi[brow];
202: rmax = bimax[brow]; nrow = bilen[brow];
203: bcol = col/bs;
204: ridx = row % bs; cidx = col % bs;
205: low = 0; high = nrow;
206: while (high-low > 3) {
207: t = (low+high)/2;
208: if (rp[t] > bcol) high = t;
209: else low = t;
210: }
211: for (_i=low; _i<high; _i++) {
212: if (rp[_i] > bcol) break;
213: if (rp[_i] == bcol) {
214: bap = ap + bs2*_i + bs*cidx + ridx;
215: if (addv == ADD_VALUES) *bap += value;
216: else *bap = value;
217: goto b_noinsert;
218: }
219: }
220: if (b->nonew == 1) goto b_noinsert;
221: else if (b->nonew == -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero into matrix");
222: if (nrow >= rmax) {
223: /* there is no extra room in row, therefore enlarge */
224: int new_nz = bi[b->mbs] + CHUNKSIZE,len,*new_i,*new_j;
225: MatScalar *new_a;
226:
227: if (b->nonew == -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
228:
229: /* malloc new storage space */
230: len = new_nz*(sizeof(int)+bs2*sizeof(MatScalar))+(b->mbs+1)*sizeof(int);
231: ierr = PetscMalloc(len,&new_a);
232: new_j = (int*)(new_a + bs2*new_nz);
233: new_i = new_j + new_nz;
234:
235: /* copy over old data into new slots */
236: for (ii=0; ii<brow+1; ii++) {new_i[ii] = bi[ii];}
237: for (ii=brow+1; ii<b->mbs+1; ii++) {new_i[ii] = bi[ii]+CHUNKSIZE;}
238: PetscMemcpy(new_j,bj,(bi[brow]+nrow)*sizeof(int));
239: len = (new_nz - CHUNKSIZE - bi[brow] - nrow);
240: PetscMemcpy(new_j+bi[brow]+nrow+CHUNKSIZE,bj+bi[brow]+nrow,len*sizeof(int));
241: PetscMemcpy(new_a,ba,(bi[brow]+nrow)*bs2*sizeof(MatScalar));
242: PetscMemzero(new_a+bs2*(bi[brow]+nrow),bs2*CHUNKSIZE*sizeof(MatScalar));
243: PetscMemcpy(new_a+bs2*(bi[brow]+nrow+CHUNKSIZE),
244: ba+bs2*(bi[brow]+nrow),bs2*len*sizeof(MatScalar));
245: /* free up old matrix storage */
246: PetscFree(b->a);
247: if (!b->singlemalloc) {
248: PetscFree(b->i);
249: PetscFree(b->j);
250: }
251: ba = b->a = new_a; bi = b->i = new_i; bj = b->j = new_j;
252: b->singlemalloc = PETSC_TRUE;
253:
254: rp = bj + bi[brow]; ap = ba + bs2*bi[brow];
255: rmax = bimax[brow] = bimax[brow] + CHUNKSIZE;
256: PetscLogObjectMemory(B,CHUNKSIZE*(sizeof(int) + bs2*sizeof(MatScalar)));
257: b->maxnz += bs2*CHUNKSIZE;
258: b->reallocs++;
259: b->nz++;
260: }
261: N = nrow++ - 1;
262: /* shift up all the later entries in this row */
263: for (ii=N; ii>=_i; ii--) {
264: rp[ii+1] = rp[ii];
265: PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
266: }
267: if (N>=_i) { PetscMemzero(ap+bs2*_i,bs2*sizeof(MatScalar));}
268: rp[_i] = bcol;
269: ap[bs2*_i + bs*cidx + ridx] = value;
270: b_noinsert:;
271: bilen[brow] = nrow;
272: }
274: #if defined(PETSC_USE_MAT_SINGLE)
275: int MatSetValues_MPIBAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
276: {
277: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
278: int ierr,i,N = m*n;
279: MatScalar *vsingle;
282: if (N > b->setvalueslen) {
283: if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
284: PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
285: b->setvalueslen = N;
286: }
287: vsingle = b->setvaluescopy;
289: for (i=0; i<N; i++) {
290: vsingle[i] = v[i];
291: }
292: MatSetValues_MPIBAIJ_MatScalar(mat,m,im,n,in,vsingle,addv);
293: return(0);
294: }
296: int MatSetValuesBlocked_MPIBAIJ(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
297: {
298: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
299: int ierr,i,N = m*n*b->bs2;
300: MatScalar *vsingle;
303: if (N > b->setvalueslen) {
304: if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
305: PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
306: b->setvalueslen = N;
307: }
308: vsingle = b->setvaluescopy;
309: for (i=0; i<N; i++) {
310: vsingle[i] = v[i];
311: }
312: MatSetValuesBlocked_MPIBAIJ_MatScalar(mat,m,im,n,in,vsingle,addv);
313: return(0);
314: }
316: int MatSetValues_MPIBAIJ_HT(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
317: {
318: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
319: int ierr,i,N = m*n;
320: MatScalar *vsingle;
323: if (N > b->setvalueslen) {
324: if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
325: PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
326: b->setvalueslen = N;
327: }
328: vsingle = b->setvaluescopy;
329: for (i=0; i<N; i++) {
330: vsingle[i] = v[i];
331: }
332: MatSetValues_MPIBAIJ_HT_MatScalar(mat,m,im,n,in,vsingle,addv);
333: return(0);
334: }
336: int MatSetValuesBlocked_MPIBAIJ_HT(Mat mat,int m,int *im,int n,int *in,Scalar *v,InsertMode addv)
337: {
338: Mat_MPIBAIJ *b = (Mat_MPIBAIJ*)mat->data;
339: int ierr,i,N = m*n*b->bs2;
340: MatScalar *vsingle;
343: if (N > b->setvalueslen) {
344: if (b->setvaluescopy) {PetscFree(b->setvaluescopy);}
345: PetscMalloc(N*sizeof(MatScalar),&b->setvaluescopy);
346: b->setvalueslen = N;
347: }
348: vsingle = b->setvaluescopy;
349: for (i=0; i<N; i++) {
350: vsingle[i] = v[i];
351: }
352: MatSetValuesBlocked_MPIBAIJ_HT_MatScalar(mat,m,im,n,in,vsingle,addv);
353: return(0);
354: }
355: #endif
357: int MatSetValues_MPIBAIJ_MatScalar(Mat mat,int m,int *im,int n,int *in,MatScalar *v,InsertMode addv)
358: {
359: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
360: MatScalar value;
361: PetscTruth roworiented = baij->roworiented;
362: int ierr,i,j,row,col;
363: int rstart_orig=baij->rstart_bs;
364: int rend_orig=baij->rend_bs,cstart_orig=baij->cstart_bs;
365: int cend_orig=baij->cend_bs,bs=baij->bs;
367: /* Some Variables required in the macro */
368: Mat A = baij->A;
369: Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)(A)->data;
370: int *aimax=a->imax,*ai=a->i,*ailen=a->ilen,*aj=a->j;
371: MatScalar *aa=a->a;
373: Mat B = baij->B;
374: Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(B)->data;
375: int *bimax=b->imax,*bi=b->i,*bilen=b->ilen,*bj=b->j;
376: MatScalar *ba=b->a;
378: int *rp,ii,nrow,_i,rmax,N,brow,bcol;
379: int low,high,t,ridx,cidx,bs2=a->bs2;
380: MatScalar *ap,*bap;
383: for (i=0; i<m; i++) {
384: if (im[i] < 0) continue;
385: #if defined(PETSC_USE_BOPT_g)
386: if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
387: #endif
388: if (im[i] >= rstart_orig && im[i] < rend_orig) {
389: row = im[i] - rstart_orig;
390: for (j=0; j<n; j++) {
391: if (in[j] >= cstart_orig && in[j] < cend_orig){
392: col = in[j] - cstart_orig;
393: if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
394: MatSetValues_SeqBAIJ_A_Private(row,col,value,addv);
395: /* MatSetValues_SeqBAIJ(baij->A,1,&row,1,&col,&value,addv); */
396: } else if (in[j] < 0) continue;
397: #if defined(PETSC_USE_BOPT_g)
398: else if (in[j] >= mat->N) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");}
399: #endif
400: else {
401: if (mat->was_assembled) {
402: if (!baij->colmap) {
403: CreateColmap_MPIBAIJ_Private(mat);
404: }
405: #if defined (PETSC_USE_CTABLE)
406: PetscTableFind(baij->colmap,in[j]/bs + 1,&col);
407: col = col - 1 + in[j]%bs;
408: #else
409: col = baij->colmap[in[j]/bs] - 1 + in[j]%bs;
410: #endif
411: if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
412: DisAssemble_MPIBAIJ(mat);
413: col = in[j];
414: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
415: B = baij->B;
416: b = (Mat_SeqBAIJ*)(B)->data;
417: bimax=b->imax;bi=b->i;bilen=b->ilen;bj=b->j;
418: ba=b->a;
419: }
420: } else col = in[j];
421: if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
422: MatSetValues_SeqBAIJ_B_Private(row,col,value,addv);
423: /* MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv); */
424: }
425: }
426: } else {
427: if (!baij->donotstash) {
428: if (roworiented) {
429: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
430: } else {
431: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
432: }
433: }
434: }
435: }
436: return(0);
437: }
439: int MatSetValuesBlocked_MPIBAIJ_MatScalar(Mat mat,int m,int *im,int n,int *in,MatScalar *v,InsertMode addv)
440: {
441: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
442: MatScalar *value,*barray=baij->barray;
443: PetscTruth roworiented = baij->roworiented;
444: int ierr,i,j,ii,jj,row,col,rstart=baij->rstart;
445: int rend=baij->rend,cstart=baij->cstart,stepval;
446: int cend=baij->cend,bs=baij->bs,bs2=baij->bs2;
447:
449: if(!barray) {
450: ierr = PetscMalloc(bs2*sizeof(MatScalar),&barray);
451: baij->barray = barray;
452: }
454: if (roworiented) {
455: stepval = (n-1)*bs;
456: } else {
457: stepval = (m-1)*bs;
458: }
459: for (i=0; i<m; i++) {
460: if (im[i] < 0) continue;
461: #if defined(PETSC_USE_BOPT_g)
462: if (im[i] >= baij->Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large, row %d max %d",im[i],baij->Mbs);
463: #endif
464: if (im[i] >= rstart && im[i] < rend) {
465: row = im[i] - rstart;
466: for (j=0; j<n; j++) {
467: /* If NumCol = 1 then a copy is not required */
468: if ((roworiented) && (n == 1)) {
469: barray = v + i*bs2;
470: } else if((!roworiented) && (m == 1)) {
471: barray = v + j*bs2;
472: } else { /* Here a copy is required */
473: if (roworiented) {
474: value = v + i*(stepval+bs)*bs + j*bs;
475: } else {
476: value = v + j*(stepval+bs)*bs + i*bs;
477: }
478: for (ii=0; ii<bs; ii++,value+=stepval) {
479: for (jj=0; jj<bs; jj++) {
480: *barray++ = *value++;
481: }
482: }
483: barray -=bs2;
484: }
485:
486: if (in[j] >= cstart && in[j] < cend){
487: col = in[j] - cstart;
488: MatSetValuesBlocked_SeqBAIJ_MatScalar(baij->A,1,&row,1,&col,barray,addv);
489: }
490: else if (in[j] < 0) continue;
491: #if defined(PETSC_USE_BOPT_g)
492: else if (in[j] >= baij->Nbs) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large, col %d max %d",in[j],baij->Nbs);}
493: #endif
494: else {
495: if (mat->was_assembled) {
496: if (!baij->colmap) {
497: CreateColmap_MPIBAIJ_Private(mat);
498: }
500: #if defined(PETSC_USE_BOPT_g)
501: #if defined (PETSC_USE_CTABLE)
502: { int data;
503: PetscTableFind(baij->colmap,in[j]+1,&data);
504: if ((data - 1) % bs) SETERRQ(PETSC_ERR_PLIB,"Incorrect colmap");
505: }
506: #else
507: if ((baij->colmap[in[j]] - 1) % bs) SETERRQ(PETSC_ERR_PLIB,"Incorrect colmap");
508: #endif
509: #endif
510: #if defined (PETSC_USE_CTABLE)
511: PetscTableFind(baij->colmap,in[j]+1,&col);
512: col = (col - 1)/bs;
513: #else
514: col = (baij->colmap[in[j]] - 1)/bs;
515: #endif
516: if (col < 0 && !((Mat_SeqBAIJ*)(baij->A->data))->nonew) {
517: DisAssemble_MPIBAIJ(mat);
518: col = in[j];
519: }
520: }
521: else col = in[j];
522: MatSetValuesBlocked_SeqBAIJ_MatScalar(baij->B,1,&row,1,&col,barray,addv);
523: }
524: }
525: } else {
526: if (!baij->donotstash) {
527: if (roworiented) {
528: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
529: } else {
530: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
531: }
532: }
533: }
534: }
535: return(0);
536: }
538: #define HASH_KEY 0.6180339887
539: #define HASH(size,key,tmp) (tmp = (key)*HASH_KEY,(int)((size)*(tmp-(int)tmp)))
540: /* #define HASH(size,key) ((int)((size)*fmod(((key)*HASH_KEY),1))) */
541: /* #define HASH(size,key,tmp) ((int)((size)*fmod(((key)*HASH_KEY),1))) */
542: int MatSetValues_MPIBAIJ_HT_MatScalar(Mat mat,int m,int *im,int n,int *in,MatScalar *v,InsertMode addv)
543: {
544: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
545: PetscTruth roworiented = baij->roworiented;
546: int ierr,i,j,row,col;
547: int rstart_orig=baij->rstart_bs;
548: int rend_orig=baij->rend_bs,Nbs=baij->Nbs;
549: int h1,key,size=baij->ht_size,bs=baij->bs,*HT=baij->ht,idx;
550: PetscReal tmp;
551: MatScalar **HD = baij->hd,value;
552: #if defined(PETSC_USE_BOPT_g)
553: int total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
554: #endif
558: for (i=0; i<m; i++) {
559: #if defined(PETSC_USE_BOPT_g)
560: if (im[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
561: if (im[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
562: #endif
563: row = im[i];
564: if (row >= rstart_orig && row < rend_orig) {
565: for (j=0; j<n; j++) {
566: col = in[j];
567: if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
568: /* Look up into the Hash Table */
569: key = (row/bs)*Nbs+(col/bs)+1;
570: h1 = HASH(size,key,tmp);
572:
573: idx = h1;
574: #if defined(PETSC_USE_BOPT_g)
575: insert_ct++;
576: total_ct++;
577: if (HT[idx] != key) {
578: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
579: if (idx == size) {
580: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
581: if (idx == h1) {
582: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"(row,col) has no entry in the hash table");
583: }
584: }
585: }
586: #else
587: if (HT[idx] != key) {
588: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
589: if (idx == size) {
590: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
591: if (idx == h1) {
592: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"(row,col) has no entry in the hash table");
593: }
594: }
595: }
596: #endif
597: /* A HASH table entry is found, so insert the values at the correct address */
598: if (addv == ADD_VALUES) *(HD[idx]+ (col % bs)*bs + (row % bs)) += value;
599: else *(HD[idx]+ (col % bs)*bs + (row % bs)) = value;
600: }
601: } else {
602: if (!baij->donotstash) {
603: if (roworiented) {
604: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
605: } else {
606: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
607: }
608: }
609: }
610: }
611: #if defined(PETSC_USE_BOPT_g)
612: baij->ht_total_ct = total_ct;
613: baij->ht_insert_ct = insert_ct;
614: #endif
615: return(0);
616: }
618: int MatSetValuesBlocked_MPIBAIJ_HT_MatScalar(Mat mat,int m,int *im,int n,int *in,MatScalar *v,InsertMode addv)
619: {
620: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
621: PetscTruth roworiented = baij->roworiented;
622: int ierr,i,j,ii,jj,row,col;
623: int rstart=baij->rstart ;
624: int rend=baij->rend,stepval,bs=baij->bs,bs2=baij->bs2;
625: int h1,key,size=baij->ht_size,idx,*HT=baij->ht,Nbs=baij->Nbs;
626: PetscReal tmp;
627: MatScalar **HD = baij->hd,*baij_a;
628: MatScalar *v_t,*value;
629: #if defined(PETSC_USE_BOPT_g)
630: int total_ct=baij->ht_total_ct,insert_ct=baij->ht_insert_ct;
631: #endif
632:
635: if (roworiented) {
636: stepval = (n-1)*bs;
637: } else {
638: stepval = (m-1)*bs;
639: }
640: for (i=0; i<m; i++) {
641: #if defined(PETSC_USE_BOPT_g)
642: if (im[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
643: if (im[i] >= baij->Mbs) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
644: #endif
645: row = im[i];
646: v_t = v + i*bs2;
647: if (row >= rstart && row < rend) {
648: for (j=0; j<n; j++) {
649: col = in[j];
651: /* Look up into the Hash Table */
652: key = row*Nbs+col+1;
653: h1 = HASH(size,key,tmp);
654:
655: idx = h1;
656: #if defined(PETSC_USE_BOPT_g)
657: total_ct++;
658: insert_ct++;
659: if (HT[idx] != key) {
660: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++,total_ct++);
661: if (idx == size) {
662: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++,total_ct++);
663: if (idx == h1) {
664: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"(row,col) has no entry in the hash table");
665: }
666: }
667: }
668: #else
669: if (HT[idx] != key) {
670: for (idx=h1; (idx<size) && (HT[idx]!=key); idx++);
671: if (idx == size) {
672: for (idx=0; (idx<h1) && (HT[idx]!=key); idx++);
673: if (idx == h1) {
674: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"(row,col) has no entry in the hash table");
675: }
676: }
677: }
678: #endif
679: baij_a = HD[idx];
680: if (roworiented) {
681: /*value = v + i*(stepval+bs)*bs + j*bs;*/
682: /* value = v + (i*(stepval+bs)+j)*bs; */
683: value = v_t;
684: v_t += bs;
685: if (addv == ADD_VALUES) {
686: for (ii=0; ii<bs; ii++,value+=stepval) {
687: for (jj=ii; jj<bs2; jj+=bs) {
688: baij_a[jj] += *value++;
689: }
690: }
691: } else {
692: for (ii=0; ii<bs; ii++,value+=stepval) {
693: for (jj=ii; jj<bs2; jj+=bs) {
694: baij_a[jj] = *value++;
695: }
696: }
697: }
698: } else {
699: value = v + j*(stepval+bs)*bs + i*bs;
700: if (addv == ADD_VALUES) {
701: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
702: for (jj=0; jj<bs; jj++) {
703: baij_a[jj] += *value++;
704: }
705: }
706: } else {
707: for (ii=0; ii<bs; ii++,value+=stepval,baij_a+=bs) {
708: for (jj=0; jj<bs; jj++) {
709: baij_a[jj] = *value++;
710: }
711: }
712: }
713: }
714: }
715: } else {
716: if (!baij->donotstash) {
717: if (roworiented) {
718: MatStashValuesRowBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
719: } else {
720: MatStashValuesColBlocked_Private(&mat->bstash,im[i],n,in,v,m,n,i);
721: }
722: }
723: }
724: }
725: #if defined(PETSC_USE_BOPT_g)
726: baij->ht_total_ct = total_ct;
727: baij->ht_insert_ct = insert_ct;
728: #endif
729: return(0);
730: }
732: int MatGetValues_MPIBAIJ(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v)
733: {
734: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
735: int bs=baij->bs,ierr,i,j,bsrstart = baij->rstart*bs,bsrend = baij->rend*bs;
736: int bscstart = baij->cstart*bs,bscend = baij->cend*bs,row,col,data;
739: for (i=0; i<m; i++) {
740: if (idxm[i] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative row");
741: if (idxm[i] >= mat->M) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
742: if (idxm[i] >= bsrstart && idxm[i] < bsrend) {
743: row = idxm[i] - bsrstart;
744: for (j=0; j<n; j++) {
745: if (idxn[j] < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative column");
746: if (idxn[j] >= mat->N) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
747: if (idxn[j] >= bscstart && idxn[j] < bscend){
748: col = idxn[j] - bscstart;
749: MatGetValues_SeqBAIJ(baij->A,1,&row,1,&col,v+i*n+j);
750: } else {
751: if (!baij->colmap) {
752: CreateColmap_MPIBAIJ_Private(mat);
753: }
754: #if defined (PETSC_USE_CTABLE)
755: PetscTableFind(baij->colmap,idxn[j]/bs+1,&data);
756: data --;
757: #else
758: data = baij->colmap[idxn[j]/bs]-1;
759: #endif
760: if((data < 0) || (baij->garray[data/bs] != idxn[j]/bs)) *(v+i*n+j) = 0.0;
761: else {
762: col = data + idxn[j]%bs;
763: MatGetValues_SeqBAIJ(baij->B,1,&row,1,&col,v+i*n+j);
764: }
765: }
766: }
767: } else {
768: SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
769: }
770: }
771: return(0);
772: }
774: int MatNorm_MPIBAIJ(Mat mat,NormType type,PetscReal *norm)
775: {
776: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
777: Mat_SeqBAIJ *amat = (Mat_SeqBAIJ*)baij->A->data,*bmat = (Mat_SeqBAIJ*)baij->B->data;
778: int ierr,i,bs2=baij->bs2;
779: PetscReal sum = 0.0;
780: MatScalar *v;
783: if (baij->size == 1) {
784: MatNorm(baij->A,type,norm);
785: } else {
786: if (type == NORM_FROBENIUS) {
787: v = amat->a;
788: for (i=0; i<amat->nz*bs2; i++) {
789: #if defined(PETSC_USE_COMPLEX)
790: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
791: #else
792: sum += (*v)*(*v); v++;
793: #endif
794: }
795: v = bmat->a;
796: for (i=0; i<bmat->nz*bs2; i++) {
797: #if defined(PETSC_USE_COMPLEX)
798: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
799: #else
800: sum += (*v)*(*v); v++;
801: #endif
802: }
803: MPI_Allreduce(&sum,norm,1,MPI_DOUBLE,MPI_SUM,mat->comm);
804: *norm = sqrt(*norm);
805: } else {
806: SETERRQ(PETSC_ERR_SUP,"No support for this norm yet");
807: }
808: }
809: return(0);
810: }
813: /*
814: Creates the hash table, and sets the table
815: This table is created only once.
816: If new entried need to be added to the matrix
817: then the hash table has to be destroyed and
818: recreated.
819: */
820: int MatCreateHashTable_MPIBAIJ_Private(Mat mat,PetscReal factor)
821: {
822: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
823: Mat A = baij->A,B=baij->B;
824: Mat_SeqBAIJ *a=(Mat_SeqBAIJ *)A->data,*b=(Mat_SeqBAIJ *)B->data;
825: int i,j,k,nz=a->nz+b->nz,h1,*ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j;
826: int size,bs2=baij->bs2,rstart=baij->rstart,ierr;
827: int cstart=baij->cstart,*garray=baij->garray,row,col,Nbs=baij->Nbs;
828: int *HT,key;
829: MatScalar **HD;
830: PetscReal tmp;
831: #if defined(PETSC_USE_BOPT_g)
832: int ct=0,max=0;
833: #endif
836: baij->ht_size=(int)(factor*nz);
837: size = baij->ht_size;
839: if (baij->ht) {
840: return(0);
841: }
842:
843: /* Allocate Memory for Hash Table */
844: ierr = PetscMalloc((size)*(sizeof(int)+sizeof(MatScalar*))+1,&baij->hd);
845: baij->ht = (int*)(baij->hd + size);
846: HD = baij->hd;
847: HT = baij->ht;
850: PetscMemzero(HD,size*(sizeof(int)+sizeof(Scalar*)));
851:
853: /* Loop Over A */
854: for (i=0; i<a->mbs; i++) {
855: for (j=ai[i]; j<ai[i+1]; j++) {
856: row = i+rstart;
857: col = aj[j]+cstart;
858:
859: key = row*Nbs + col + 1;
860: h1 = HASH(size,key,tmp);
861: for (k=0; k<size; k++){
862: if (HT[(h1+k)%size] == 0.0) {
863: HT[(h1+k)%size] = key;
864: HD[(h1+k)%size] = a->a + j*bs2;
865: break;
866: #if defined(PETSC_USE_BOPT_g)
867: } else {
868: ct++;
869: #endif
870: }
871: }
872: #if defined(PETSC_USE_BOPT_g)
873: if (k> max) max = k;
874: #endif
875: }
876: }
877: /* Loop Over B */
878: for (i=0; i<b->mbs; i++) {
879: for (j=bi[i]; j<bi[i+1]; j++) {
880: row = i+rstart;
881: col = garray[bj[j]];
882: key = row*Nbs + col + 1;
883: h1 = HASH(size,key,tmp);
884: for (k=0; k<size; k++){
885: if (HT[(h1+k)%size] == 0.0) {
886: HT[(h1+k)%size] = key;
887: HD[(h1+k)%size] = b->a + j*bs2;
888: break;
889: #if defined(PETSC_USE_BOPT_g)
890: } else {
891: ct++;
892: #endif
893: }
894: }
895: #if defined(PETSC_USE_BOPT_g)
896: if (k> max) max = k;
897: #endif
898: }
899: }
900:
901: /* Print Summary */
902: #if defined(PETSC_USE_BOPT_g)
903: for (i=0,j=0; i<size; i++) {
904: if (HT[i]) {j++;}
905: }
906: PetscLogInfo(0,"MatCreateHashTable_MPIBAIJ_Private: Average Search = %5.2f,max search = %dn",(j== 0)? 0.0:((PetscReal)(ct+j))/j,max);
907: #endif
908: return(0);
909: }
911: int MatAssemblyBegin_MPIBAIJ(Mat mat,MatAssemblyType mode)
912: {
913: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
914: int ierr,nstash,reallocs;
915: InsertMode addv;
918: if (baij->donotstash) {
919: return(0);
920: }
922: /* make sure all processors are either in INSERTMODE or ADDMODE */
923: MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);
924: if (addv == (ADD_VALUES|INSERT_VALUES)) {
925: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
926: }
927: mat->insertmode = addv; /* in case this processor had no cache */
929: MatStashScatterBegin_Private(&mat->stash,baij->rowners_bs);
930: MatStashScatterBegin_Private(&mat->bstash,baij->rowners);
931: MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
932: PetscLogInfo(0,"MatAssemblyBegin_MPIBAIJ:Stash has %d entries,uses %d mallocs.n",nstash,reallocs);
933: MatStashGetInfo_Private(&mat->bstash,&nstash,&reallocs);
934: PetscLogInfo(0,"MatAssemblyBegin_MPIBAIJ:Block-Stash has %d entries, uses %d mallocs.n",nstash,reallocs);
935: return(0);
936: }
938: int MatAssemblyEnd_MPIBAIJ(Mat mat,MatAssemblyType mode)
939: {
940: Mat_MPIBAIJ *baij=(Mat_MPIBAIJ*)mat->data;
941: Mat_SeqBAIJ *a=(Mat_SeqBAIJ*)baij->A->data,*b=(Mat_SeqBAIJ*)baij->B->data;
942: int i,j,rstart,ncols,n,ierr,flg,bs2=baij->bs2;
943: int *row,*col,other_disassembled;
944: PetscTruth r1,r2,r3;
945: MatScalar *val;
946: InsertMode addv = mat->insertmode;
949: if (!baij->donotstash) {
950: while (1) {
951: MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
952: if (!flg) break;
954: for (i=0; i<n;) {
955: /* Now identify the consecutive vals belonging to the same row */
956: for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
957: if (j < n) ncols = j-i;
958: else ncols = n-i;
959: /* Now assemble all these values with a single function call */
960: MatSetValues_MPIBAIJ_MatScalar(mat,1,row+i,ncols,col+i,val+i,addv);
961: i = j;
962: }
963: }
964: MatStashScatterEnd_Private(&mat->stash);
965: /* Now process the block-stash. Since the values are stashed column-oriented,
966: set the roworiented flag to column oriented, and after MatSetValues()
967: restore the original flags */
968: r1 = baij->roworiented;
969: r2 = a->roworiented;
970: r3 = b->roworiented;
971: baij->roworiented = PETSC_FALSE;
972: a->roworiented = PETSC_FALSE;
973: b->roworiented = PETSC_FALSE;
974: while (1) {
975: MatStashScatterGetMesg_Private(&mat->bstash,&n,&row,&col,&val,&flg);
976: if (!flg) break;
977:
978: for (i=0; i<n;) {
979: /* Now identify the consecutive vals belonging to the same row */
980: for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
981: if (j < n) ncols = j-i;
982: else ncols = n-i;
983: MatSetValuesBlocked_MPIBAIJ_MatScalar(mat,1,row+i,ncols,col+i,val+i*bs2,addv);
984: i = j;
985: }
986: }
987: MatStashScatterEnd_Private(&mat->bstash);
988: baij->roworiented = r1;
989: a->roworiented = r2;
990: b->roworiented = r3;
991: }
993: MatAssemblyBegin(baij->A,mode);
994: MatAssemblyEnd(baij->A,mode);
996: /* determine if any processor has disassembled, if so we must
997: also disassemble ourselfs, in order that we may reassemble. */
998: /*
999: if nonzero structure of submatrix B cannot change then we know that
1000: no processor disassembled thus we can skip this stuff
1001: */
1002: if (!((Mat_SeqBAIJ*)baij->B->data)->nonew) {
1003: MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);
1004: if (mat->was_assembled && !other_disassembled) {
1005: DisAssemble_MPIBAIJ(mat);
1006: }
1007: }
1009: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
1010: MatSetUpMultiply_MPIBAIJ(mat);
1011: }
1012: MatAssemblyBegin(baij->B,mode);
1013: MatAssemblyEnd(baij->B,mode);
1014:
1015: #if defined(PETSC_USE_BOPT_g)
1016: if (baij->ht && mode== MAT_FINAL_ASSEMBLY) {
1017: PetscLogInfo(0,"MatAssemblyEnd_MPIBAIJ:Average Hash Table Search in MatSetValues = %5.2fn",((double)baij->ht_total_ct)/baij->ht_insert_ct);
1018: baij->ht_total_ct = 0;
1019: baij->ht_insert_ct = 0;
1020: }
1021: #endif
1022: if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
1023: MatCreateHashTable_MPIBAIJ_Private(mat,baij->ht_fact);
1024: mat->ops->setvalues = MatSetValues_MPIBAIJ_HT;
1025: mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
1026: }
1028: if (baij->rowvalues) {
1029: PetscFree(baij->rowvalues);
1030: baij->rowvalues = 0;
1031: }
1032: return(0);
1033: }
1035: static int MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
1036: {
1037: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1038: int ierr,bs = baij->bs,size = baij->size,rank = baij->rank;
1039: PetscTruth isascii,isdraw;
1040: PetscViewer sviewer;
1041: PetscViewerFormat format;
1044: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
1045: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
1046: if (isascii) {
1047: PetscViewerGetFormat(viewer,&format);
1048: if (format == PETSC_VIEWER_ASCII_INFO_LONG) {
1049: MatInfo info;
1050: MPI_Comm_rank(mat->comm,&rank);
1051: MatGetInfo(mat,MAT_LOCAL,&info);
1052: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %d nz %d nz alloced %d bs %d mem %dn",
1053: rank,mat->m,(int)info.nz_used*bs,(int)info.nz_allocated*bs,
1054: baij->bs,(int)info.memory);
1055: MatGetInfo(baij->A,MAT_LOCAL,&info);
1056: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %d n",rank,(int)info.nz_used*bs);
1057: MatGetInfo(baij->B,MAT_LOCAL,&info);
1058: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %d n",rank,(int)info.nz_used*bs);
1059: PetscViewerFlush(viewer);
1060: VecScatterView(baij->Mvctx,viewer);
1061: return(0);
1062: } else if (format == PETSC_VIEWER_ASCII_INFO) {
1063: PetscViewerASCIIPrintf(viewer," block size is %dn",bs);
1064: return(0);
1065: }
1066: }
1068: if (isdraw) {
1069: PetscDraw draw;
1070: PetscTruth isnull;
1071: PetscViewerDrawGetDraw(viewer,0,&draw);
1072: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
1073: }
1075: if (size == 1) {
1076: MatView(baij->A,viewer);
1077: } else {
1078: /* assemble the entire matrix onto first processor. */
1079: Mat A;
1080: Mat_SeqBAIJ *Aloc;
1081: int M = mat->M,N = mat->N,*ai,*aj,col,i,j,k,*rvals,mbs = baij->mbs;
1082: MatScalar *a;
1084: if (!rank) {
1085: MatCreateMPIBAIJ(mat->comm,baij->bs,M,N,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);
1086: } else {
1087: MatCreateMPIBAIJ(mat->comm,baij->bs,0,0,M,N,0,PETSC_NULL,0,PETSC_NULL,&A);
1088: }
1089: PetscLogObjectParent(mat,A);
1091: /* copy over the A part */
1092: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1093: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1094: PetscMalloc(bs*sizeof(int),&rvals);
1096: for (i=0; i<mbs; i++) {
1097: rvals[0] = bs*(baij->rstart + i);
1098: for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
1099: for (j=ai[i]; j<ai[i+1]; j++) {
1100: col = (baij->cstart+aj[j])*bs;
1101: for (k=0; k<bs; k++) {
1102: MatSetValues_MPIBAIJ_MatScalar(A,bs,rvals,1,&col,a,INSERT_VALUES);
1103: col++; a += bs;
1104: }
1105: }
1106: }
1107: /* copy over the B part */
1108: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1109: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1110: for (i=0; i<mbs; i++) {
1111: rvals[0] = bs*(baij->rstart + i);
1112: for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
1113: for (j=ai[i]; j<ai[i+1]; j++) {
1114: col = baij->garray[aj[j]]*bs;
1115: for (k=0; k<bs; k++) {
1116: MatSetValues_MPIBAIJ_MatScalar(A,bs,rvals,1,&col,a,INSERT_VALUES);
1117: col++; a += bs;
1118: }
1119: }
1120: }
1121: PetscFree(rvals);
1122: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1123: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1124: /*
1125: Everyone has to call to draw the matrix since the graphics waits are
1126: synchronized across all processors that share the PetscDraw object
1127: */
1128: PetscViewerGetSingleton(viewer,&sviewer);
1129: if (!rank) {
1130: MatView(((Mat_MPIBAIJ*)(A->data))->A,sviewer);
1131: }
1132: PetscViewerRestoreSingleton(viewer,&sviewer);
1133: MatDestroy(A);
1134: }
1135: return(0);
1136: }
1138: int MatView_MPIBAIJ(Mat mat,PetscViewer viewer)
1139: {
1140: int ierr;
1141: PetscTruth isascii,isdraw,issocket,isbinary;
1144: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
1145: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
1146: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
1147: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
1148: if (isascii || isdraw || issocket || isbinary) {
1149: MatView_MPIBAIJ_ASCIIorDraworSocket(mat,viewer);
1150: } else {
1151: SETERRQ1(1,"Viewer type %s not supported by MPIBAIJ matrices",((PetscObject)viewer)->type_name);
1152: }
1153: return(0);
1154: }
1156: int MatDestroy_MPIBAIJ(Mat mat)
1157: {
1158: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1159: int ierr;
1162: #if defined(PETSC_USE_LOG)
1163: PetscLogObjectState((PetscObject)mat,"Rows=%d,Cols=%d",mat->M,mat->N);
1164: #endif
1165: MatStashDestroy_Private(&mat->stash);
1166: MatStashDestroy_Private(&mat->bstash);
1167: PetscFree(baij->rowners);
1168: MatDestroy(baij->A);
1169: MatDestroy(baij->B);
1170: #if defined (PETSC_USE_CTABLE)
1171: if (baij->colmap) {PetscTableDelete(baij->colmap);}
1172: #else
1173: if (baij->colmap) {PetscFree(baij->colmap);}
1174: #endif
1175: if (baij->garray) {PetscFree(baij->garray);}
1176: if (baij->lvec) {VecDestroy(baij->lvec);}
1177: if (baij->Mvctx) {VecScatterDestroy(baij->Mvctx);}
1178: if (baij->rowvalues) {PetscFree(baij->rowvalues);}
1179: if (baij->barray) {PetscFree(baij->barray);}
1180: if (baij->hd) {PetscFree(baij->hd);}
1181: #if defined(PETSC_USE_MAT_SINGLE)
1182: if (baij->setvaluescopy) {PetscFree(baij->setvaluescopy);}
1183: #endif
1184: PetscFree(baij);
1185: return(0);
1186: }
1188: int MatMult_MPIBAIJ(Mat A,Vec xx,Vec yy)
1189: {
1190: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1191: int ierr,nt;
1194: VecGetLocalSize(xx,&nt);
1195: if (nt != A->n) {
1196: SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible partition of A and xx");
1197: }
1198: VecGetLocalSize(yy,&nt);
1199: if (nt != A->m) {
1200: SETERRQ(PETSC_ERR_ARG_SIZ,"Incompatible parition of A and yy");
1201: }
1202: VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1203: (*a->A->ops->mult)(a->A,xx,yy);
1204: VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1205: (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
1206: VecScatterPostRecvs(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1207: return(0);
1208: }
1210: int MatMultAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1211: {
1212: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1213: int ierr;
1216: VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1217: (*a->A->ops->multadd)(a->A,xx,yy,zz);
1218: VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
1219: (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
1220: return(0);
1221: }
1223: int MatMultTranspose_MPIBAIJ(Mat A,Vec xx,Vec yy)
1224: {
1225: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1226: int ierr;
1229: /* do nondiagonal part */
1230: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1231: /* send it on its way */
1232: VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1233: /* do local part */
1234: (*a->A->ops->multtranspose)(a->A,xx,yy);
1235: /* receive remote parts: note this assumes the values are not actually */
1236: /* inserted in yy until the next line, which is true for my implementation*/
1237: /* but is not perhaps always true. */
1238: VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1239: return(0);
1240: }
1242: int MatMultTransposeAdd_MPIBAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1243: {
1244: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1245: int ierr;
1248: /* do nondiagonal part */
1249: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
1250: /* send it on its way */
1251: VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1252: /* do local part */
1253: (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
1254: /* receive remote parts: note this assumes the values are not actually */
1255: /* inserted in yy until the next line, which is true for my implementation*/
1256: /* but is not perhaps always true. */
1257: VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
1258: return(0);
1259: }
1261: /*
1262: This only works correctly for square matrices where the subblock A->A is the
1263: diagonal block
1264: */
1265: int MatGetDiagonal_MPIBAIJ(Mat A,Vec v)
1266: {
1267: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1268: int ierr;
1271: if (A->M != A->N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
1272: MatGetDiagonal(a->A,v);
1273: return(0);
1274: }
1276: int MatScale_MPIBAIJ(Scalar *aa,Mat A)
1277: {
1278: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1279: int ierr;
1282: MatScale(aa,a->A);
1283: MatScale(aa,a->B);
1284: return(0);
1285: }
1287: int MatGetOwnershipRange_MPIBAIJ(Mat matin,int *m,int *n)
1288: {
1289: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
1292: if (m) *m = mat->rstart_bs;
1293: if (n) *n = mat->rend_bs;
1294: return(0);
1295: }
1297: int MatGetRow_MPIBAIJ(Mat matin,int row,int *nz,int **idx,Scalar **v)
1298: {
1299: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ*)matin->data;
1300: Scalar *vworkA,*vworkB,**pvA,**pvB,*v_p;
1301: int bs = mat->bs,bs2 = mat->bs2,i,ierr,*cworkA,*cworkB,**pcA,**pcB;
1302: int nztot,nzA,nzB,lrow,brstart = mat->rstart*bs,brend = mat->rend*bs;
1303: int *cmap,*idx_p,cstart = mat->cstart;
1306: if (mat->getrowactive == PETSC_TRUE) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
1307: mat->getrowactive = PETSC_TRUE;
1309: if (!mat->rowvalues && (idx || v)) {
1310: /*
1311: allocate enough space to hold information from the longest row.
1312: */
1313: Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ*)mat->A->data,*Ba = (Mat_SeqBAIJ*)mat->B->data;
1314: int max = 1,mbs = mat->mbs,tmp;
1315: for (i=0; i<mbs; i++) {
1316: tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1317: if (max < tmp) { max = tmp; }
1318: }
1319: PetscMalloc(max*bs2*(sizeof(int)+sizeof(Scalar)),&mat->rowvalues);
1320: mat->rowindices = (int*)(mat->rowvalues + max*bs2);
1321: }
1322:
1323: if (row < brstart || row >= brend) SETERRQ(PETSC_ERR_SUP,"Only local rows")
1324: lrow = row - brstart;
1326: pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1327: if (!v) {pvA = 0; pvB = 0;}
1328: if (!idx) {pcA = 0; if (!v) pcB = 0;}
1329: (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1330: (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1331: nztot = nzA + nzB;
1333: cmap = mat->garray;
1334: if (v || idx) {
1335: if (nztot) {
1336: /* Sort by increasing column numbers, assuming A and B already sorted */
1337: int imark = -1;
1338: if (v) {
1339: *v = v_p = mat->rowvalues;
1340: for (i=0; i<nzB; i++) {
1341: if (cmap[cworkB[i]/bs] < cstart) v_p[i] = vworkB[i];
1342: else break;
1343: }
1344: imark = i;
1345: for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i];
1346: for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i];
1347: }
1348: if (idx) {
1349: *idx = idx_p = mat->rowindices;
1350: if (imark > -1) {
1351: for (i=0; i<imark; i++) {
1352: idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs;
1353: }
1354: } else {
1355: for (i=0; i<nzB; i++) {
1356: if (cmap[cworkB[i]/bs] < cstart)
1357: idx_p[i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1358: else break;
1359: }
1360: imark = i;
1361: }
1362: for (i=0; i<nzA; i++) idx_p[imark+i] = cstart*bs + cworkA[i];
1363: for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]/bs]*bs + cworkB[i]%bs ;
1364: }
1365: } else {
1366: if (idx) *idx = 0;
1367: if (v) *v = 0;
1368: }
1369: }
1370: *nz = nztot;
1371: (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1372: (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1373: return(0);
1374: }
1376: int MatRestoreRow_MPIBAIJ(Mat mat,int row,int *nz,int **idx,Scalar **v)
1377: {
1378: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1381: if (baij->getrowactive == PETSC_FALSE) {
1382: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow not called");
1383: }
1384: baij->getrowactive = PETSC_FALSE;
1385: return(0);
1386: }
1388: int MatGetBlockSize_MPIBAIJ(Mat mat,int *bs)
1389: {
1390: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1393: *bs = baij->bs;
1394: return(0);
1395: }
1397: int MatZeroEntries_MPIBAIJ(Mat A)
1398: {
1399: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1400: int ierr;
1403: MatZeroEntries(l->A);
1404: MatZeroEntries(l->B);
1405: return(0);
1406: }
1408: int MatGetInfo_MPIBAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1409: {
1410: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)matin->data;
1411: Mat A = a->A,B = a->B;
1412: int ierr;
1413: PetscReal isend[5],irecv[5];
1416: info->block_size = (double)a->bs;
1417: MatGetInfo(A,MAT_LOCAL,info);
1418: isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1419: isend[3] = info->memory; isend[4] = info->mallocs;
1420: MatGetInfo(B,MAT_LOCAL,info);
1421: isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1422: isend[3] += info->memory; isend[4] += info->mallocs;
1423: if (flag == MAT_LOCAL) {
1424: info->nz_used = isend[0];
1425: info->nz_allocated = isend[1];
1426: info->nz_unneeded = isend[2];
1427: info->memory = isend[3];
1428: info->mallocs = isend[4];
1429: } else if (flag == MAT_GLOBAL_MAX) {
1430: MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_MAX,matin->comm);
1431: info->nz_used = irecv[0];
1432: info->nz_allocated = irecv[1];
1433: info->nz_unneeded = irecv[2];
1434: info->memory = irecv[3];
1435: info->mallocs = irecv[4];
1436: } else if (flag == MAT_GLOBAL_SUM) {
1437: MPI_Allreduce(isend,irecv,5,MPI_DOUBLE,MPI_SUM,matin->comm);
1438: info->nz_used = irecv[0];
1439: info->nz_allocated = irecv[1];
1440: info->nz_unneeded = irecv[2];
1441: info->memory = irecv[3];
1442: info->mallocs = irecv[4];
1443: } else {
1444: SETERRQ1(1,"Unknown MatInfoType argument %d",flag);
1445: }
1446: info->rows_global = (double)A->M;
1447: info->columns_global = (double)A->N;
1448: info->rows_local = (double)A->m;
1449: info->columns_local = (double)A->N;
1450: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1451: info->fill_ratio_needed = 0;
1452: info->factor_mallocs = 0;
1453: return(0);
1454: }
1456: int MatSetOption_MPIBAIJ(Mat A,MatOption op)
1457: {
1458: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1459: int ierr;
1462: if (op == MAT_NO_NEW_NONZERO_LOCATIONS ||
1463: op == MAT_YES_NEW_NONZERO_LOCATIONS ||
1464: op == MAT_COLUMNS_UNSORTED ||
1465: op == MAT_COLUMNS_SORTED ||
1466: op == MAT_NEW_NONZERO_ALLOCATION_ERR ||
1467: op == MAT_KEEP_ZEROED_ROWS ||
1468: op == MAT_NEW_NONZERO_LOCATION_ERR) {
1469: MatSetOption(a->A,op);
1470: MatSetOption(a->B,op);
1471: } else if (op == MAT_ROW_ORIENTED) {
1472: a->roworiented = PETSC_TRUE;
1473: MatSetOption(a->A,op);
1474: MatSetOption(a->B,op);
1475: } else if (op == MAT_ROWS_SORTED ||
1476: op == MAT_ROWS_UNSORTED ||
1477: op == MAT_YES_NEW_DIAGONALS ||
1478: op == MAT_USE_HASH_TABLE) {
1479: PetscLogInfo(A,"Info:MatSetOption_MPIBAIJ:Option ignoredn");
1480: } else if (op == MAT_COLUMN_ORIENTED) {
1481: a->roworiented = PETSC_FALSE;
1482: MatSetOption(a->A,op);
1483: MatSetOption(a->B,op);
1484: } else if (op == MAT_IGNORE_OFF_PROC_ENTRIES) {
1485: a->donotstash = PETSC_TRUE;
1486: } else if (op == MAT_NO_NEW_DIAGONALS) {
1487: SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1488: } else if (op == MAT_USE_HASH_TABLE) {
1489: a->ht_flag = PETSC_TRUE;
1490: } else {
1491: SETERRQ(PETSC_ERR_SUP,"unknown option");
1492: }
1493: return(0);
1494: }
1496: int MatTranspose_MPIBAIJ(Mat A,Mat *matout)
1497: {
1498: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)A->data;
1499: Mat_SeqBAIJ *Aloc;
1500: Mat B;
1501: int ierr,M=A->M,N=A->N,*ai,*aj,i,*rvals,j,k,col;
1502: int bs=baij->bs,mbs=baij->mbs;
1503: MatScalar *a;
1504:
1506: if (!matout && M != N) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1507: MatCreateMPIBAIJ(A->comm,baij->bs,A->n,A->m,N,M,0,PETSC_NULL,0,PETSC_NULL,&B);
1508:
1509: /* copy over the A part */
1510: Aloc = (Mat_SeqBAIJ*)baij->A->data;
1511: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1512: PetscMalloc(bs*sizeof(int),&rvals);
1513:
1514: for (i=0; i<mbs; i++) {
1515: rvals[0] = bs*(baij->rstart + i);
1516: for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
1517: for (j=ai[i]; j<ai[i+1]; j++) {
1518: col = (baij->cstart+aj[j])*bs;
1519: for (k=0; k<bs; k++) {
1520: MatSetValues_MPIBAIJ_MatScalar(B,1,&col,bs,rvals,a,INSERT_VALUES);
1521: col++; a += bs;
1522: }
1523: }
1524: }
1525: /* copy over the B part */
1526: Aloc = (Mat_SeqBAIJ*)baij->B->data;
1527: ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
1528: for (i=0; i<mbs; i++) {
1529: rvals[0] = bs*(baij->rstart + i);
1530: for (j=1; j<bs; j++) { rvals[j] = rvals[j-1] + 1; }
1531: for (j=ai[i]; j<ai[i+1]; j++) {
1532: col = baij->garray[aj[j]]*bs;
1533: for (k=0; k<bs; k++) {
1534: MatSetValues_MPIBAIJ_MatScalar(B,1,&col,bs,rvals,a,INSERT_VALUES);
1535: col++; a += bs;
1536: }
1537: }
1538: }
1539: PetscFree(rvals);
1540: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1541: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1542:
1543: if (matout) {
1544: *matout = B;
1545: } else {
1546: MatHeaderCopy(A,B);
1547: }
1548: return(0);
1549: }
1551: int MatDiagonalScale_MPIBAIJ(Mat mat,Vec ll,Vec rr)
1552: {
1553: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
1554: Mat a = baij->A,b = baij->B;
1555: int ierr,s1,s2,s3;
1558: MatGetLocalSize(mat,&s2,&s3);
1559: if (rr) {
1560: VecGetLocalSize(rr,&s1);
1561: if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1562: /* Overlap communication with computation. */
1563: VecScatterBegin(rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD,baij->Mvctx);
1564: }
1565: if (ll) {
1566: VecGetLocalSize(ll,&s1);
1567: if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1568: (*b->ops->diagonalscale)(b,ll,PETSC_NULL);
1569: }
1570: /* scale the diagonal block */
1571: (*a->ops->diagonalscale)(a,ll,rr);
1573: if (rr) {
1574: /* Do a scatter end and then right scale the off-diagonal block */
1575: VecScatterEnd(rr,baij->lvec,INSERT_VALUES,SCATTER_FORWARD,baij->Mvctx);
1576: (*b->ops->diagonalscale)(b,PETSC_NULL,baij->lvec);
1577: }
1578:
1579: return(0);
1580: }
1582: int MatZeroRows_MPIBAIJ(Mat A,IS is,Scalar *diag)
1583: {
1584: Mat_MPIBAIJ *l = (Mat_MPIBAIJ*)A->data;
1585: int i,ierr,N,*rows,*owners = l->rowners,size = l->size;
1586: int *procs,*nprocs,j,idx,nsends,*work,row;
1587: int nmax,*svalues,*starts,*owner,nrecvs,rank = l->rank;
1588: int *rvalues,tag = A->tag,count,base,slen,n,*source;
1589: int *lens,imdex,*lrows,*values,bs=l->bs,rstart_bs=l->rstart_bs;
1590: MPI_Comm comm = A->comm;
1591: MPI_Request *send_waits,*recv_waits;
1592: MPI_Status recv_status,*send_status;
1593: IS istmp;
1594: PetscTruth found;
1595:
1597: ISGetLocalSize(is,&N);
1598: ISGetIndices(is,&rows);
1599:
1600: /* first count number of contributors to each processor */
1601: ierr = PetscMalloc(2*size*sizeof(int),&nprocs);
1602: ierr = PetscMemzero(nprocs,2*size*sizeof(int));
1603: procs = nprocs + size;
1604: ierr = PetscMalloc((N+1)*sizeof(int),&owner); /* see note*/
1605: for (i=0; i<N; i++) {
1606: idx = rows[i];
1607: found = PETSC_FALSE;
1608: for (j=0; j<size; j++) {
1609: if (idx >= owners[j]*bs && idx < owners[j+1]*bs) {
1610: nprocs[j]++; procs[j] = 1; owner[i] = j; found = PETSC_TRUE; break;
1611: }
1612: }
1613: if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
1614: }
1615: nsends = 0; for (i=0; i<size; i++) { nsends += procs[i];}
1616:
1617: /* inform other processors of number of messages and max length*/
1618: ierr = PetscMalloc(2*size*sizeof(int),&work);
1619: ierr = MPI_Allreduce(nprocs,work,2*size,MPI_INT,PetscMaxSum_Op,comm);
1620: nmax = work[rank];
1621: nrecvs = work[size+rank];
1622: ierr = PetscFree(work);
1623:
1624: /* post receives: */
1625: PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(int),&rvalues);
1626: PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
1627: for (i=0; i<nrecvs; i++) {
1628: MPI_Irecv(rvalues+nmax*i,nmax,MPI_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
1629: }
1630:
1631: /* do sends:
1632: 1) starts[i] gives the starting index in svalues for stuff going to
1633: the ith processor
1634: */
1635: PetscMalloc((N+1)*sizeof(int),&svalues);
1636: PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
1637: PetscMalloc((size+1)*sizeof(int),&starts);
1638: starts[0] = 0;
1639: for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
1640: for (i=0; i<N; i++) {
1641: svalues[starts[owner[i]]++] = rows[i];
1642: }
1643: ISRestoreIndices(is,&rows);
1644:
1645: starts[0] = 0;
1646: for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[i-1];}
1647: count = 0;
1648: for (i=0; i<size; i++) {
1649: if (procs[i]) {
1650: MPI_Isend(svalues+starts[i],nprocs[i],MPI_INT,i,tag,comm,send_waits+count++);
1651: }
1652: }
1653: PetscFree(starts);
1655: base = owners[rank]*bs;
1656:
1657: /* wait on receives */
1658: ierr = PetscMalloc(2*(nrecvs+1)*sizeof(int),&lens);
1659: source = lens + nrecvs;
1660: count = nrecvs; slen = 0;
1661: while (count) {
1662: MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
1663: /* unpack receives into our local space */
1664: MPI_Get_count(&recv_status,MPI_INT,&n);
1665: source[imdex] = recv_status.MPI_SOURCE;
1666: lens[imdex] = n;
1667: slen += n;
1668: count--;
1669: }
1670: PetscFree(recv_waits);
1671:
1672: /* move the data into the send scatter */
1673: PetscMalloc((slen+1)*sizeof(int),&lrows);
1674: count = 0;
1675: for (i=0; i<nrecvs; i++) {
1676: values = rvalues + i*nmax;
1677: for (j=0; j<lens[i]; j++) {
1678: lrows[count++] = values[j] - base;
1679: }
1680: }
1681: PetscFree(rvalues);
1682: PetscFree(lens);
1683: PetscFree(owner);
1684: PetscFree(nprocs);
1685:
1686: /* actually zap the local rows */
1687: ISCreateGeneral(PETSC_COMM_SELF,slen,lrows,&istmp);
1688: PetscLogObjectParent(A,istmp);
1690: /*
1691: Zero the required rows. If the "diagonal block" of the matrix
1692: is square and the user wishes to set the diagonal we use seperate
1693: code so that MatSetValues() is not called for each diagonal allocating
1694: new memory, thus calling lots of mallocs and slowing things down.
1696: Contributed by: Mathew Knepley
1697: */
1698: /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1699: MatZeroRows_SeqBAIJ(l->B,istmp,0);
1700: if (diag && (l->A->M == l->A->N)) {
1701: MatZeroRows_SeqBAIJ(l->A,istmp,diag);
1702: } else if (diag) {
1703: MatZeroRows_SeqBAIJ(l->A,istmp,0);
1704: if (((Mat_SeqBAIJ*)l->A->data)->nonew) {
1705: SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options n
1706: MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
1707: }
1708: for (i=0; i<slen; i++) {
1709: row = lrows[i] + rstart_bs;
1710: MatSetValues(A,1,&row,1,&row,diag,INSERT_VALUES);
1711: }
1712: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1713: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1714: } else {
1715: MatZeroRows_SeqBAIJ(l->A,istmp,0);
1716: }
1718: ISDestroy(istmp);
1719: PetscFree(lrows);
1721: /* wait on sends */
1722: if (nsends) {
1723: PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
1724: MPI_Waitall(nsends,send_waits,send_status);
1725: PetscFree(send_status);
1726: }
1727: PetscFree(send_waits);
1728: PetscFree(svalues);
1730: return(0);
1731: }
1733: int MatPrintHelp_MPIBAIJ(Mat A)
1734: {
1735: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1736: MPI_Comm comm = A->comm;
1737: static int called = 0;
1738: int ierr;
1741: if (!a->rank) {
1742: MatPrintHelp_SeqBAIJ(a->A);
1743: }
1744: if (called) {return(0);} else called = 1;
1745: (*PetscHelpPrintf)(comm," Options for MATMPIBAIJ matrix format (the defaults):n");
1746: (*PetscHelpPrintf)(comm," -mat_use_hash_table <factor>: Use hashtable for efficient matrix assemblyn");
1747: return(0);
1748: }
1750: int MatSetUnfactored_MPIBAIJ(Mat A)
1751: {
1752: Mat_MPIBAIJ *a = (Mat_MPIBAIJ*)A->data;
1753: int ierr;
1756: MatSetUnfactored(a->A);
1757: return(0);
1758: }
1760: static int MatDuplicate_MPIBAIJ(Mat,MatDuplicateOption,Mat *);
1762: int MatEqual_MPIBAIJ(Mat A,Mat B,PetscTruth *flag)
1763: {
1764: Mat_MPIBAIJ *matB = (Mat_MPIBAIJ*)B->data,*matA = (Mat_MPIBAIJ*)A->data;
1765: Mat a,b,c,d;
1766: PetscTruth flg;
1767: int ierr;
1770: PetscTypeCompare((PetscObject)B,MATMPIBAIJ,&flg);
1771: if (!flg) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices must be same type");
1772: a = matA->A; b = matA->B;
1773: c = matB->A; d = matB->B;
1775: MatEqual(a,c,&flg);
1776: if (flg == PETSC_TRUE) {
1777: MatEqual(b,d,&flg);
1778: }
1779: MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);
1780: return(0);
1781: }
1784: int MatSetUpPreallocation_MPIBAIJ(Mat A)
1785: {
1786: int ierr;
1789: MatMPIBAIJSetPreallocation(A,1,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1790: return(0);
1791: }
1793: /* -------------------------------------------------------------------*/
1794: static struct _MatOps MatOps_Values = {
1795: MatSetValues_MPIBAIJ,
1796: MatGetRow_MPIBAIJ,
1797: MatRestoreRow_MPIBAIJ,
1798: MatMult_MPIBAIJ,
1799: MatMultAdd_MPIBAIJ,
1800: MatMultTranspose_MPIBAIJ,
1801: MatMultTransposeAdd_MPIBAIJ,
1802: 0,
1803: 0,
1804: 0,
1805: 0,
1806: 0,
1807: 0,
1808: 0,
1809: MatTranspose_MPIBAIJ,
1810: MatGetInfo_MPIBAIJ,
1811: MatEqual_MPIBAIJ,
1812: MatGetDiagonal_MPIBAIJ,
1813: MatDiagonalScale_MPIBAIJ,
1814: MatNorm_MPIBAIJ,
1815: MatAssemblyBegin_MPIBAIJ,
1816: MatAssemblyEnd_MPIBAIJ,
1817: 0,
1818: MatSetOption_MPIBAIJ,
1819: MatZeroEntries_MPIBAIJ,
1820: MatZeroRows_MPIBAIJ,
1821: 0,
1822: 0,
1823: 0,
1824: 0,
1825: MatSetUpPreallocation_MPIBAIJ,
1826: 0,
1827: MatGetOwnershipRange_MPIBAIJ,
1828: 0,
1829: 0,
1830: 0,
1831: 0,
1832: MatDuplicate_MPIBAIJ,
1833: 0,
1834: 0,
1835: 0,
1836: 0,
1837: 0,
1838: MatGetSubMatrices_MPIBAIJ,
1839: MatIncreaseOverlap_MPIBAIJ,
1840: MatGetValues_MPIBAIJ,
1841: 0,
1842: MatPrintHelp_MPIBAIJ,
1843: MatScale_MPIBAIJ,
1844: 0,
1845: 0,
1846: 0,
1847: MatGetBlockSize_MPIBAIJ,
1848: 0,
1849: 0,
1850: 0,
1851: 0,
1852: 0,
1853: 0,
1854: MatSetUnfactored_MPIBAIJ,
1855: 0,
1856: MatSetValuesBlocked_MPIBAIJ,
1857: 0,
1858: MatDestroy_MPIBAIJ,
1859: MatView_MPIBAIJ,
1860: MatGetMaps_Petsc,
1861: 0,
1862: 0,
1863: 0,
1864: 0,
1865: 0,
1866: 0,
1867: MatGetRowMax_MPIBAIJ};
1870: EXTERN_C_BEGIN
1871: int MatGetDiagonalBlock_MPIBAIJ(Mat A,PetscTruth *iscopy,MatReuse reuse,Mat *a)
1872: {
1874: *a = ((Mat_MPIBAIJ *)A->data)->A;
1875: *iscopy = PETSC_FALSE;
1876: return(0);
1877: }
1878: EXTERN_C_END
1880: EXTERN_C_BEGIN
1881: int MatCreate_MPIBAIJ(Mat B)
1882: {
1883: Mat_MPIBAIJ *b;
1884: int ierr;
1885: PetscTruth flg;
1889: PetscNew(Mat_MPIBAIJ,&b);
1890: B->data = (void*)b;
1892: ierr = PetscMemzero(b,sizeof(Mat_MPIBAIJ));
1893: ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
1894: B->mapping = 0;
1895: B->factor = 0;
1896: B->assembled = PETSC_FALSE;
1898: B->insertmode = NOT_SET_VALUES;
1899: MPI_Comm_rank(B->comm,&b->rank);
1900: MPI_Comm_size(B->comm,&b->size);
1902: /* build local table of row and column ownerships */
1903: ierr = PetscMalloc(3*(b->size+2)*sizeof(int),&b->rowners);
1904: PetscLogObjectMemory(B,3*(b->size+2)*sizeof(int)+sizeof(struct _p_Mat)+sizeof(Mat_MPIBAIJ));
1905: b->cowners = b->rowners + b->size + 2;
1906: b->rowners_bs = b->cowners + b->size + 2;
1908: /* build cache for off array entries formed */
1909: MatStashCreate_Private(B->comm,1,&B->stash);
1910: b->donotstash = PETSC_FALSE;
1911: b->colmap = PETSC_NULL;
1912: b->garray = PETSC_NULL;
1913: b->roworiented = PETSC_TRUE;
1915: #if defined(PETSC_USE_MAT_SINGLE)
1916: /* stuff for MatSetValues_XXX in single precision */
1917: b->setvalueslen = 0;
1918: b->setvaluescopy = PETSC_NULL;
1919: #endif
1921: /* stuff used in block assembly */
1922: b->barray = 0;
1924: /* stuff used for matrix vector multiply */
1925: b->lvec = 0;
1926: b->Mvctx = 0;
1928: /* stuff for MatGetRow() */
1929: b->rowindices = 0;
1930: b->rowvalues = 0;
1931: b->getrowactive = PETSC_FALSE;
1933: /* hash table stuff */
1934: b->ht = 0;
1935: b->hd = 0;
1936: b->ht_size = 0;
1937: b->ht_flag = PETSC_FALSE;
1938: b->ht_fact = 0;
1939: b->ht_total_ct = 0;
1940: b->ht_insert_ct = 0;
1942: PetscOptionsHasName(PETSC_NULL,"-mat_use_hash_table",&flg);
1943: if (flg) {
1944: double fact = 1.39;
1945: MatSetOption(B,MAT_USE_HASH_TABLE);
1946: PetscOptionsGetDouble(PETSC_NULL,"-mat_use_hash_table",&fact,PETSC_NULL);
1947: if (fact <= 1.0) fact = 1.39;
1948: MatMPIBAIJSetHashTableFactor(B,fact);
1949: PetscLogInfo(0,"MatCreateMPIBAIJ:Hash table Factor used %5.2fn",fact);
1950: }
1951: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
1952: "MatStoreValues_MPIBAIJ",
1953: MatStoreValues_MPIBAIJ);
1954: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
1955: "MatRetrieveValues_MPIBAIJ",
1956: MatRetrieveValues_MPIBAIJ);
1957: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
1958: "MatGetDiagonalBlock_MPIBAIJ",
1959: MatGetDiagonalBlock_MPIBAIJ);
1960: return(0);
1961: }
1962: EXTERN_C_END
1964: /*@C
1965: MatMPIBAIJSetPreallocation - Creates a sparse parallel matrix in block AIJ format
1966: (block compressed row). For good matrix assembly performance
1967: the user should preallocate the matrix storage by setting the parameters
1968: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
1969: performance can be increased by more than a factor of 50.
1971: Collective on Mat
1973: Input Parameters:
1974: + A - the matrix
1975: . bs - size of blockk
1976: . d_nz - number of block nonzeros per block row in diagonal portion of local
1977: submatrix (same for all local rows)
1978: . d_nnz - array containing the number of block nonzeros in the various block rows
1979: of the in diagonal portion of the local (possibly different for each block
1980: row) or PETSC_NULL. You must leave room for the diagonal entry even if it is zero.
1981: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
1982: submatrix (same for all local rows).
1983: - o_nnz - array containing the number of nonzeros in the various block rows of the
1984: off-diagonal portion of the local submatrix (possibly different for
1985: each block row) or PETSC_NULL.
1987: Output Parameter:
1990: Options Database Keys:
1991: . -mat_no_unroll - uses code that does not unroll the loops in the
1992: block calculations (much slower)
1993: . -mat_block_size - size of the blocks to use
1995: Notes:
1996: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
1997: than it must be used on all processors that share the object for that argument.
1999: Storage Information:
2000: For a square global matrix we define each processor's diagonal portion
2001: to be its local rows and the corresponding columns (a square submatrix);
2002: each processor's off-diagonal portion encompasses the remainder of the
2003: local matrix (a rectangular submatrix).
2005: The user can specify preallocated storage for the diagonal part of
2006: the local submatrix with either d_nz or d_nnz (not both). Set
2007: d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
2008: memory allocation. Likewise, specify preallocated storage for the
2009: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
2011: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2012: the figure below we depict these three local rows and all columns (0-11).
2014: .vb
2015: 0 1 2 3 4 5 6 7 8 9 10 11
2016: -------------------
2017: row 3 | o o o d d d o o o o o o
2018: row 4 | o o o d d d o o o o o o
2019: row 5 | o o o d d d o o o o o o
2020: -------------------
2021: .ve
2022:
2023: Thus, any entries in the d locations are stored in the d (diagonal)
2024: submatrix, and any entries in the o locations are stored in the
2025: o (off-diagonal) submatrix. Note that the d and the o submatrices are
2026: stored simply in the MATSEQBAIJ format for compressed row storage.
2028: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
2029: and o_nz should indicate the number of block nonzeros per row in the o matrix.
2030: In general, for PDE problems in which most nonzeros are near the diagonal,
2031: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
2032: or you will get TERRIBLE performance; see the users' manual chapter on
2033: matrices.
2035: Level: intermediate
2037: .keywords: matrix, block, aij, compressed row, sparse, parallel
2039: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ()
2040: @*/
2041: int MatMPIBAIJSetPreallocation(Mat B,int bs,int d_nz,int *d_nnz,int o_nz,int *o_nnz)
2042: {
2043: Mat_MPIBAIJ *b;
2044: int ierr,i;
2045: PetscTruth flg2;
2048: PetscTypeCompare((PetscObject)B,MATMPIBAIJ,&flg2);
2049: if (!flg2) return(0);
2051: B->preallocated = PETSC_TRUE;
2052: PetscOptionsGetInt(PETSC_NULL,"-mat_block_size",&bs,PETSC_NULL);
2054: if (bs < 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive");
2055: if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
2056: if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
2057: if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %d",d_nz);
2058: if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %d",o_nz);
2059: if (d_nnz) {
2060: for (i=0; i<B->m/bs; i++) {
2061: if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than -1: local row %d value %d",i,d_nnz[i]);
2062: }
2063: }
2064: if (o_nnz) {
2065: for (i=0; i<B->m/bs; i++) {
2066: if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than -1: local row %d value %d",i,o_nnz[i]);
2067: }
2068: }
2069:
2070: PetscSplitOwnershipBlock(B->comm,bs,&B->m,&B->M);
2071: PetscSplitOwnershipBlock(B->comm,bs,&B->n,&B->N);
2072: MapCreateMPI(B->comm,B->m,B->M,&B->rmap);
2073: MapCreateMPI(B->comm,B->n,B->N,&B->cmap);
2075: b = (Mat_MPIBAIJ*)B->data;
2076: b->bs = bs;
2077: b->bs2 = bs*bs;
2078: b->mbs = B->m/bs;
2079: b->nbs = B->n/bs;
2080: b->Mbs = B->M/bs;
2081: b->Nbs = B->N/bs;
2083: MPI_Allgather(&b->mbs,1,MPI_INT,b->rowners+1,1,MPI_INT,B->comm);
2084: b->rowners[0] = 0;
2085: for (i=2; i<=b->size; i++) {
2086: b->rowners[i] += b->rowners[i-1];
2087: }
2088: b->rstart = b->rowners[b->rank];
2089: b->rend = b->rowners[b->rank+1];
2091: MPI_Allgather(&b->nbs,1,MPI_INT,b->cowners+1,1,MPI_INT,B->comm);
2092: b->cowners[0] = 0;
2093: for (i=2; i<=b->size; i++) {
2094: b->cowners[i] += b->cowners[i-1];
2095: }
2096: b->cstart = b->cowners[b->rank];
2097: b->cend = b->cowners[b->rank+1];
2099: for (i=0; i<=b->size; i++) {
2100: b->rowners_bs[i] = b->rowners[i]*bs;
2101: }
2102: b->rstart_bs = b->rstart*bs;
2103: b->rend_bs = b->rend*bs;
2104: b->cstart_bs = b->cstart*bs;
2105: b->cend_bs = b->cend*bs;
2107: MatCreateSeqBAIJ(PETSC_COMM_SELF,bs,B->m,B->n,d_nz,d_nnz,&b->A);
2108: PetscLogObjectParent(B,b->A);
2109: MatCreateSeqBAIJ(PETSC_COMM_SELF,bs,B->m,B->N,o_nz,o_nnz,&b->B);
2110: PetscLogObjectParent(B,b->B);
2111: MatStashCreate_Private(B->comm,bs,&B->bstash);
2113: return(0);
2114: }
2116: /*@C
2117: MatCreateMPIBAIJ - Creates a sparse parallel matrix in block AIJ format
2118: (block compressed row). For good matrix assembly performance
2119: the user should preallocate the matrix storage by setting the parameters
2120: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
2121: performance can be increased by more than a factor of 50.
2123: Collective on MPI_Comm
2125: Input Parameters:
2126: + comm - MPI communicator
2127: . bs - size of blockk
2128: . m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2129: This value should be the same as the local size used in creating the
2130: y vector for the matrix-vector product y = Ax.
2131: . n - number of local columns (or PETSC_DECIDE to have calculated if N is given)
2132: This value should be the same as the local size used in creating the
2133: x vector for the matrix-vector product y = Ax.
2134: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2135: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2136: . d_nz - number of nonzero blocks per block row in diagonal portion of local
2137: submatrix (same for all local rows)
2138: . d_nnz - array containing the number of nonzero blocks in the various block rows
2139: of the in diagonal portion of the local (possibly different for each block
2140: row) or PETSC_NULL. You must leave room for the diagonal entry even if it is zero.
2141: . o_nz - number of nonzero blocks per block row in the off-diagonal portion of local
2142: submatrix (same for all local rows).
2143: - o_nnz - array containing the number of nonzero blocks in the various block rows of the
2144: off-diagonal portion of the local submatrix (possibly different for
2145: each block row) or PETSC_NULL.
2147: Output Parameter:
2148: . A - the matrix
2150: Options Database Keys:
2151: . -mat_no_unroll - uses code that does not unroll the loops in the
2152: block calculations (much slower)
2153: . -mat_block_size - size of the blocks to use
2155: Notes:
2156: A nonzero block is any block that as 1 or more nonzeros in it
2158: The user MUST specify either the local or global matrix dimensions
2159: (possibly both).
2161: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one processor
2162: than it must be used on all processors that share the object for that argument.
2164: Storage Information:
2165: For a square global matrix we define each processor's diagonal portion
2166: to be its local rows and the corresponding columns (a square submatrix);
2167: each processor's off-diagonal portion encompasses the remainder of the
2168: local matrix (a rectangular submatrix).
2170: The user can specify preallocated storage for the diagonal part of
2171: the local submatrix with either d_nz or d_nnz (not both). Set
2172: d_nz=PETSC_DEFAULT and d_nnz=PETSC_NULL for PETSc to control dynamic
2173: memory allocation. Likewise, specify preallocated storage for the
2174: off-diagonal part of the local submatrix with o_nz or o_nnz (not both).
2176: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2177: the figure below we depict these three local rows and all columns (0-11).
2179: .vb
2180: 0 1 2 3 4 5 6 7 8 9 10 11
2181: -------------------
2182: row 3 | o o o d d d o o o o o o
2183: row 4 | o o o d d d o o o o o o
2184: row 5 | o o o d d d o o o o o o
2185: -------------------
2186: .ve
2187:
2188: Thus, any entries in the d locations are stored in the d (diagonal)
2189: submatrix, and any entries in the o locations are stored in the
2190: o (off-diagonal) submatrix. Note that the d and the o submatrices are
2191: stored simply in the MATSEQBAIJ format for compressed row storage.
2193: Now d_nz should indicate the number of block nonzeros per row in the d matrix,
2194: and o_nz should indicate the number of block nonzeros per row in the o matrix.
2195: In general, for PDE problems in which most nonzeros are near the diagonal,
2196: one expects d_nz >> o_nz. For large problems you MUST preallocate memory
2197: or you will get TERRIBLE performance; see the users' manual chapter on
2198: matrices.
2200: Level: intermediate
2202: .keywords: matrix, block, aij, compressed row, sparse, parallel
2204: .seealso: MatCreate(), MatCreateSeqBAIJ(), MatSetValues(), MatCreateMPIBAIJ()
2205: @*/
2206: int MatCreateMPIBAIJ(MPI_Comm comm,int bs,int m,int n,int M,int N,int d_nz,int *d_nnz,int o_nz,int *o_nnz,Mat *A)
2207: {
2208: int ierr,size;
2211: MatCreate(comm,m,n,M,N,A);
2212: MPI_Comm_size(comm,&size);
2213: if (size > 1) {
2214: MatSetType(*A,MATMPIBAIJ);
2215: MatMPIBAIJSetPreallocation(*A,bs,d_nz,d_nnz,o_nz,o_nnz);
2216: } else {
2217: MatSetType(*A,MATSEQBAIJ);
2218: MatSeqBAIJSetPreallocation(*A,bs,d_nz,d_nnz);
2219: }
2220: return(0);
2221: }
2223: static int MatDuplicate_MPIBAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
2224: {
2225: Mat mat;
2226: Mat_MPIBAIJ *a,*oldmat = (Mat_MPIBAIJ*)matin->data;
2227: int ierr,len=0;
2230: *newmat = 0;
2231: MatCreate(matin->comm,matin->m,matin->n,matin->M,matin->N,&mat);
2232: MatSetType(mat,MATMPIBAIJ);
2233: mat->preallocated = PETSC_TRUE;
2234: mat->assembled = PETSC_TRUE;
2235: a = (Mat_MPIBAIJ*)mat->data;
2236: a->bs = oldmat->bs;
2237: a->bs2 = oldmat->bs2;
2238: a->mbs = oldmat->mbs;
2239: a->nbs = oldmat->nbs;
2240: a->Mbs = oldmat->Mbs;
2241: a->Nbs = oldmat->Nbs;
2242:
2243: a->rstart = oldmat->rstart;
2244: a->rend = oldmat->rend;
2245: a->cstart = oldmat->cstart;
2246: a->cend = oldmat->cend;
2247: a->size = oldmat->size;
2248: a->rank = oldmat->rank;
2249: a->donotstash = oldmat->donotstash;
2250: a->roworiented = oldmat->roworiented;
2251: a->rowindices = 0;
2252: a->rowvalues = 0;
2253: a->getrowactive = PETSC_FALSE;
2254: a->barray = 0;
2255: a->rstart_bs = oldmat->rstart_bs;
2256: a->rend_bs = oldmat->rend_bs;
2257: a->cstart_bs = oldmat->cstart_bs;
2258: a->cend_bs = oldmat->cend_bs;
2260: /* hash table stuff */
2261: a->ht = 0;
2262: a->hd = 0;
2263: a->ht_size = 0;
2264: a->ht_flag = oldmat->ht_flag;
2265: a->ht_fact = oldmat->ht_fact;
2266: a->ht_total_ct = 0;
2267: a->ht_insert_ct = 0;
2269: PetscMemcpy(a->rowners,oldmat->rowners,3*(a->size+2)*sizeof(int));
2270: MatStashCreate_Private(matin->comm,1,&mat->stash);
2271: MatStashCreate_Private(matin->comm,oldmat->bs,&mat->bstash);
2272: if (oldmat->colmap) {
2273: #if defined (PETSC_USE_CTABLE)
2274: PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2275: #else
2276: PetscMalloc((a->Nbs)*sizeof(int),&a->colmap);
2277: PetscLogObjectMemory(mat,(a->Nbs)*sizeof(int));
2278: PetscMemcpy(a->colmap,oldmat->colmap,(a->Nbs)*sizeof(int));
2279: #endif
2280: } else a->colmap = 0;
2281: if (oldmat->garray && (len = ((Mat_SeqBAIJ*)(oldmat->B->data))->nbs)) {
2282: PetscMalloc(len*sizeof(int),&a->garray);
2283: PetscLogObjectMemory(mat,len*sizeof(int));
2284: PetscMemcpy(a->garray,oldmat->garray,len*sizeof(int));
2285: } else a->garray = 0;
2286:
2287: VecDuplicate(oldmat->lvec,&a->lvec);
2288: PetscLogObjectParent(mat,a->lvec);
2289: VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2291: PetscLogObjectParent(mat,a->Mvctx);
2292: MatDuplicate(oldmat->A,cpvalues,&a->A);
2293: PetscLogObjectParent(mat,a->A);
2294: MatDuplicate(oldmat->B,cpvalues,&a->B);
2295: PetscLogObjectParent(mat,a->B);
2296: PetscFListDuplicate(matin->qlist,&mat->qlist);
2297: *newmat = mat;
2298: return(0);
2299: }
2301: #include petscsys.h
2303: EXTERN_C_BEGIN
2304: int MatLoad_MPIBAIJ(PetscViewer viewer,MatType type,Mat *newmat)
2305: {
2306: Mat A;
2307: int i,nz,ierr,j,rstart,rend,fd;
2308: Scalar *vals,*buf;
2309: MPI_Comm comm = ((PetscObject)viewer)->comm;
2310: MPI_Status status;
2311: int header[4],rank,size,*rowlengths = 0,M,N,m,*rowners,*browners,maxnz,*cols;
2312: int *locrowlens,*sndcounts = 0,*procsnz = 0,jj,*mycols,*ibuf;
2313: int tag = ((PetscObject)viewer)->tag,bs=1,Mbs,mbs,extra_rows;
2314: int *dlens,*odlens,*mask,*masked1,*masked2,rowcount,odcount;
2315: int dcount,kmax,k,nzcount,tmp;
2316:
2318: PetscOptionsGetInt(PETSC_NULL,"-matload_block_size",&bs,PETSC_NULL);
2320: MPI_Comm_size(comm,&size);
2321: MPI_Comm_rank(comm,&rank);
2322: if (!rank) {
2323: PetscViewerBinaryGetDescriptor(viewer,&fd);
2324: PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
2325: if (header[0] != MAT_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2326: if (header[3] < 0) {
2327: SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as MPIBAIJ");
2328: }
2329: }
2331: MPI_Bcast(header+1,3,MPI_INT,0,comm);
2332: M = header[1]; N = header[2];
2334: if (M != N) SETERRQ(PETSC_ERR_SUP,"Can only do square matrices");
2336: /*
2337: This code adds extra rows to make sure the number of rows is
2338: divisible by the blocksize
2339: */
2340: Mbs = M/bs;
2341: extra_rows = bs - M + bs*(Mbs);
2342: if (extra_rows == bs) extra_rows = 0;
2343: else Mbs++;
2344: if (extra_rows &&!rank) {
2345: PetscLogInfo(0,"MatLoad_MPIBAIJ:Padding loaded matrix to match blocksizen");
2346: }
2348: /* determine ownership of all rows */
2349: mbs = Mbs/size + ((Mbs % size) > rank);
2350: m = mbs*bs;
2351: ierr = PetscMalloc(2*(size+2)*sizeof(int),&rowners);
2352: browners = rowners + size + 1;
2353: ierr = MPI_Allgather(&mbs,1,MPI_INT,rowners+1,1,MPI_INT,comm);
2354: rowners[0] = 0;
2355: for (i=2; i<=size; i++) rowners[i] += rowners[i-1];
2356: for (i=0; i<=size; i++) browners[i] = rowners[i]*bs;
2357: rstart = rowners[rank];
2358: rend = rowners[rank+1];
2360: /* distribute row lengths to all processors */
2361: PetscMalloc((rend-rstart)*bs*sizeof(int),&locrowlens);
2362: if (!rank) {
2363: PetscMalloc((M+extra_rows)*sizeof(int),&rowlengths);
2364: PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
2365: for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;
2366: PetscMalloc(size*sizeof(int),&sndcounts);
2367: for (i=0; i<size; i++) sndcounts[i] = browners[i+1] - browners[i];
2368: MPI_Scatterv(rowlengths,sndcounts,browners,MPI_INT,locrowlens,(rend-rstart)*bs,MPI_INT,0,comm);
2369: PetscFree(sndcounts);
2370: } else {
2371: MPI_Scatterv(0,0,0,MPI_INT,locrowlens,(rend-rstart)*bs,MPI_INT,0,comm);
2372: }
2374: if (!rank) {
2375: /* calculate the number of nonzeros on each processor */
2376: PetscMalloc(size*sizeof(int),&procsnz);
2377: PetscMemzero(procsnz,size*sizeof(int));
2378: for (i=0; i<size; i++) {
2379: for (j=rowners[i]*bs; j< rowners[i+1]*bs; j++) {
2380: procsnz[i] += rowlengths[j];
2381: }
2382: }
2383: PetscFree(rowlengths);
2384:
2385: /* determine max buffer needed and allocate it */
2386: maxnz = 0;
2387: for (i=0; i<size; i++) {
2388: maxnz = PetscMax(maxnz,procsnz[i]);
2389: }
2390: PetscMalloc(maxnz*sizeof(int),&cols);
2392: /* read in my part of the matrix column indices */
2393: nz = procsnz[0];
2394: ierr = PetscMalloc(nz*sizeof(int),&ibuf);
2395: mycols = ibuf;
2396: if (size == 1) nz -= extra_rows;
2397: PetscBinaryRead(fd,mycols,nz,PETSC_INT);
2398: if (size == 1) for (i=0; i< extra_rows; i++) { mycols[nz+i] = M+i; }
2400: /* read in every ones (except the last) and ship off */
2401: for (i=1; i<size-1; i++) {
2402: nz = procsnz[i];
2403: PetscBinaryRead(fd,cols,nz,PETSC_INT);
2404: MPI_Send(cols,nz,MPI_INT,i,tag,comm);
2405: }
2406: /* read in the stuff for the last proc */
2407: if (size != 1) {
2408: nz = procsnz[size-1] - extra_rows; /* the extra rows are not on the disk */
2409: PetscBinaryRead(fd,cols,nz,PETSC_INT);
2410: for (i=0; i<extra_rows; i++) cols[nz+i] = M+i;
2411: MPI_Send(cols,nz+extra_rows,MPI_INT,size-1,tag,comm);
2412: }
2413: PetscFree(cols);
2414: } else {
2415: /* determine buffer space needed for message */
2416: nz = 0;
2417: for (i=0; i<m; i++) {
2418: nz += locrowlens[i];
2419: }
2420: ierr = PetscMalloc(nz*sizeof(int),&ibuf);
2421: mycols = ibuf;
2422: /* receive message of column indices*/
2423: MPI_Recv(mycols,nz,MPI_INT,0,tag,comm,&status);
2424: MPI_Get_count(&status,MPI_INT,&maxnz);
2425: if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2426: }
2427:
2428: /* loop over local rows, determining number of off diagonal entries */
2429: ierr = PetscMalloc(2*(rend-rstart+1)*sizeof(int),&dlens);
2430: odlens = dlens + (rend-rstart);
2431: ierr = PetscMalloc(3*Mbs*sizeof(int),&mask);
2432: ierr = PetscMemzero(mask,3*Mbs*sizeof(int));
2433: masked1 = mask + Mbs;
2434: masked2 = masked1 + Mbs;
2435: rowcount = 0; nzcount = 0;
2436: for (i=0; i<mbs; i++) {
2437: dcount = 0;
2438: odcount = 0;
2439: for (j=0; j<bs; j++) {
2440: kmax = locrowlens[rowcount];
2441: for (k=0; k<kmax; k++) {
2442: tmp = mycols[nzcount++]/bs;
2443: if (!mask[tmp]) {
2444: mask[tmp] = 1;
2445: if (tmp < rstart || tmp >= rend) masked2[odcount++] = tmp;
2446: else masked1[dcount++] = tmp;
2447: }
2448: }
2449: rowcount++;
2450: }
2451:
2452: dlens[i] = dcount;
2453: odlens[i] = odcount;
2455: /* zero out the mask elements we set */
2456: for (j=0; j<dcount; j++) mask[masked1[j]] = 0;
2457: for (j=0; j<odcount; j++) mask[masked2[j]] = 0;
2458: }
2460: /* create our matrix */
2461: MatCreateMPIBAIJ(comm,bs,m,m,M+extra_rows,N+extra_rows,0,dlens,0,odlens,newmat);
2462: A = *newmat;
2463: MatSetOption(A,MAT_COLUMNS_SORTED);
2464:
2465: if (!rank) {
2466: PetscMalloc(maxnz*sizeof(Scalar),&buf);
2467: /* read in my part of the matrix numerical values */
2468: nz = procsnz[0];
2469: vals = buf;
2470: mycols = ibuf;
2471: if (size == 1) nz -= extra_rows;
2472: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2473: if (size == 1) for (i=0; i< extra_rows; i++) { vals[nz+i] = 1.0; }
2475: /* insert into matrix */
2476: jj = rstart*bs;
2477: for (i=0; i<m; i++) {
2478: MatSetValues(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2479: mycols += locrowlens[i];
2480: vals += locrowlens[i];
2481: jj++;
2482: }
2483: /* read in other processors (except the last one) and ship out */
2484: for (i=1; i<size-1; i++) {
2485: nz = procsnz[i];
2486: vals = buf;
2487: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2488: MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
2489: }
2490: /* the last proc */
2491: if (size != 1){
2492: nz = procsnz[i] - extra_rows;
2493: vals = buf;
2494: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2495: for (i=0; i<extra_rows; i++) vals[nz+i] = 1.0;
2496: MPI_Send(vals,nz+extra_rows,MPIU_SCALAR,size-1,A->tag,comm);
2497: }
2498: PetscFree(procsnz);
2499: } else {
2500: /* receive numeric values */
2501: PetscMalloc(nz*sizeof(Scalar),&buf);
2503: /* receive message of values*/
2504: vals = buf;
2505: mycols = ibuf;
2506: ierr = MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);
2507: ierr = MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2508: if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2510: /* insert into matrix */
2511: jj = rstart*bs;
2512: for (i=0; i<m; i++) {
2513: ierr = MatSetValues(A,1,&jj,locrowlens[i],mycols,vals,INSERT_VALUES);
2514: mycols += locrowlens[i];
2515: vals += locrowlens[i];
2516: jj++;
2517: }
2518: }
2519: PetscFree(locrowlens);
2520: PetscFree(buf);
2521: PetscFree(ibuf);
2522: PetscFree(rowners);
2523: PetscFree(dlens);
2524: PetscFree(mask);
2525: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2526: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2527: return(0);
2528: }
2529: EXTERN_C_END
2531: /*@
2532: MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the HashTable.
2534: Input Parameters:
2535: . mat - the matrix
2536: . fact - factor
2538: Collective on Mat
2540: Level: advanced
2542: Notes:
2543: This can also be set by the command line option: -mat_use_hash_table fact
2545: .keywords: matrix, hashtable, factor, HT
2547: .seealso: MatSetOption()
2548: @*/
2549: int MatMPIBAIJSetHashTableFactor(Mat mat,PetscReal fact)
2550: {
2551: Mat_MPIBAIJ *baij;
2552: int ierr;
2553: PetscTruth flg;
2557: PetscTypeCompare((PetscObject)mat,MATMPIBAIJ,&flg);
2558: if (!flg) {
2559: SETERRQ(PETSC_ERR_ARG_WRONG,"Incorrect matrix type. Use MPIBAIJ only.");
2560: }
2561: baij = (Mat_MPIBAIJ*)mat->data;
2562: baij->ht_fact = fact;
2563: return(0);
2564: }
2566: int MatMPIBAIJGetSeqBAIJ(Mat A,Mat *Ad,Mat *Ao,int **colmap)
2567: {
2568: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
2570: *Ad = a->A;
2571: *Ao = a->B;
2572: *colmap = a->garray;
2573: return(0);
2574: }