Actual source code: mpiaij.c
1: #define PETSCMAT_DLL
3: #include src/mat/impls/aij/mpi/mpiaij.h
4: #include src/inline/spops.h
6: /*
7: Local utility routine that creates a mapping from the global column
8: number to the local number in the off-diagonal part of the local
9: storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at
10: a slightly higher hash table cost; without it it is not scalable (each processor
11: has an order N integer array but is fast to acess.
12: */
15: PetscErrorCode CreateColmap_MPIAIJ_Private(Mat mat)
16: {
17: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
19: PetscInt n = aij->B->cmap.n,i;
22: #if defined (PETSC_USE_CTABLE)
23: PetscTableCreate(n,&aij->colmap);
24: for (i=0; i<n; i++){
25: PetscTableAdd(aij->colmap,aij->garray[i]+1,i+1);
26: }
27: #else
28: PetscMalloc((mat->cmap.N+1)*sizeof(PetscInt),&aij->colmap);
29: PetscLogObjectMemory(mat,mat->cmap.N*sizeof(PetscInt));
30: PetscMemzero(aij->colmap,mat->cmap.N*sizeof(PetscInt));
31: for (i=0; i<n; i++) aij->colmap[aij->garray[i]] = i+1;
32: #endif
33: return(0);
34: }
37: #define CHUNKSIZE 15
38: #define MatSetValues_SeqAIJ_A_Private(row,col,value,addv) \
39: { \
40: if (col <= lastcol1) low1 = 0; else high1 = nrow1; \
41: lastcol1 = col;\
42: while (high1-low1 > 5) { \
43: t = (low1+high1)/2; \
44: if (rp1[t] > col) high1 = t; \
45: else low1 = t; \
46: } \
47: for (_i=low1; _i<high1; _i++) { \
48: if (rp1[_i] > col) break; \
49: if (rp1[_i] == col) { \
50: if (addv == ADD_VALUES) ap1[_i] += value; \
51: else ap1[_i] = value; \
52: goto a_noinsert; \
53: } \
54: } \
55: if (value == 0.0 && ignorezeroentries) goto a_noinsert; \
56: if (nonew == 1) goto a_noinsert; \
57: if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
58: MatSeqXAIJReallocateAIJ(A,am,1,nrow1,row,col,rmax1,aa,ai,aj,rp1,ap1,aimax,nonew); \
59: N = nrow1++ - 1; a->nz++; high1++; \
60: /* shift up all the later entries in this row */ \
61: for (ii=N; ii>=_i; ii--) { \
62: rp1[ii+1] = rp1[ii]; \
63: ap1[ii+1] = ap1[ii]; \
64: } \
65: rp1[_i] = col; \
66: ap1[_i] = value; \
67: a_noinsert: ; \
68: ailen[row] = nrow1; \
69: }
72: #define MatSetValues_SeqAIJ_B_Private(row,col,value,addv) \
73: { \
74: if (col <= lastcol2) low2 = 0; else high2 = nrow2; \
75: lastcol2 = col;\
76: while (high2-low2 > 5) { \
77: t = (low2+high2)/2; \
78: if (rp2[t] > col) high2 = t; \
79: else low2 = t; \
80: } \
81: for (_i=low2; _i<high2; _i++) { \
82: if (rp2[_i] > col) break; \
83: if (rp2[_i] == col) { \
84: if (addv == ADD_VALUES) ap2[_i] += value; \
85: else ap2[_i] = value; \
86: goto b_noinsert; \
87: } \
88: } \
89: if (value == 0.0 && ignorezeroentries) goto b_noinsert; \
90: if (nonew == 1) goto b_noinsert; \
91: if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) into matrix", row, col); \
92: MatSeqXAIJReallocateAIJ(B,bm,1,nrow2,row,col,rmax2,ba,bi,bj,rp2,ap2,bimax,nonew); \
93: N = nrow2++ - 1; b->nz++; high2++;\
94: /* shift up all the later entries in this row */ \
95: for (ii=N; ii>=_i; ii--) { \
96: rp2[ii+1] = rp2[ii]; \
97: ap2[ii+1] = ap2[ii]; \
98: } \
99: rp2[_i] = col; \
100: ap2[_i] = value; \
101: b_noinsert: ; \
102: bilen[row] = nrow2; \
103: }
107: PetscErrorCode MatSetValuesRow_MPIAIJ(Mat A,PetscInt row,const PetscScalar v[])
108: {
109: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)A->data;
110: Mat_SeqAIJ *a = (Mat_SeqAIJ*)mat->A->data,*b = (Mat_SeqAIJ*)mat->B->data;
112: PetscInt l,*garray = mat->garray,diag;
115: /* code only works for square matrices A */
117: /* find size of row to the left of the diagonal part */
118: MatGetOwnershipRange(A,&diag,0);
119: row = row - diag;
120: for (l=0; l<b->i[row+1]-b->i[row]; l++) {
121: if (garray[b->j[b->i[row]+l]] > diag) break;
122: }
123: PetscMemcpy(b->a+b->i[row],v,l*sizeof(PetscScalar));
125: /* diagonal part */
126: PetscMemcpy(a->a+a->i[row],v+l,(a->i[row+1]-a->i[row])*sizeof(PetscScalar));
128: /* right of diagonal part */
129: PetscMemcpy(b->a+b->i[row]+l,v+l+a->i[row+1]-a->i[row],(b->i[row+1]-b->i[row]-l)*sizeof(PetscScalar));
130: return(0);
131: }
135: PetscErrorCode MatSetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode addv)
136: {
137: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
138: PetscScalar value;
140: PetscInt i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
141: PetscInt cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
142: PetscTruth roworiented = aij->roworiented;
144: /* Some Variables required in the macro */
145: Mat A = aij->A;
146: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
147: PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
148: PetscScalar *aa = a->a;
149: PetscTruth ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
150: Mat B = aij->B;
151: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
152: PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
153: PetscScalar *ba = b->a;
155: PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
156: PetscInt nonew = a->nonew;
157: PetscScalar *ap1,*ap2;
160: for (i=0; i<m; i++) {
161: if (im[i] < 0) continue;
162: #if defined(PETSC_USE_DEBUG)
163: if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
164: #endif
165: if (im[i] >= rstart && im[i] < rend) {
166: row = im[i] - rstart;
167: lastcol1 = -1;
168: rp1 = aj + ai[row];
169: ap1 = aa + ai[row];
170: rmax1 = aimax[row];
171: nrow1 = ailen[row];
172: low1 = 0;
173: high1 = nrow1;
174: lastcol2 = -1;
175: rp2 = bj + bi[row];
176: ap2 = ba + bi[row];
177: rmax2 = bimax[row];
178: nrow2 = bilen[row];
179: low2 = 0;
180: high2 = nrow2;
182: for (j=0; j<n; j++) {
183: if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
184: if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
185: if (in[j] >= cstart && in[j] < cend){
186: col = in[j] - cstart;
187: MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
188: } else if (in[j] < 0) continue;
189: #if defined(PETSC_USE_DEBUG)
190: else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
191: #endif
192: else {
193: if (mat->was_assembled) {
194: if (!aij->colmap) {
195: CreateColmap_MPIAIJ_Private(mat);
196: }
197: #if defined (PETSC_USE_CTABLE)
198: PetscTableFind(aij->colmap,in[j]+1,&col);
199: col--;
200: #else
201: col = aij->colmap[in[j]] - 1;
202: #endif
203: if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
204: DisAssemble_MPIAIJ(mat);
205: col = in[j];
206: /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
207: B = aij->B;
208: b = (Mat_SeqAIJ*)B->data;
209: bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
210: rp2 = bj + bi[row];
211: ap2 = ba + bi[row];
212: rmax2 = bimax[row];
213: nrow2 = bilen[row];
214: low2 = 0;
215: high2 = nrow2;
216: bm = aij->B->rmap.n;
217: ba = b->a;
218: }
219: } else col = in[j];
220: MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
221: }
222: }
223: } else {
224: if (!aij->donotstash) {
225: if (roworiented) {
226: if (ignorezeroentries && v[i*n] == 0.0) continue;
227: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
228: } else {
229: if (ignorezeroentries && v[i] == 0.0) continue;
230: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
231: }
232: }
233: }
234: }
235: return(0);
236: }
241: PetscErrorCode MatGetValues_MPIAIJ(Mat mat,PetscInt m,const PetscInt idxm[],PetscInt n,const PetscInt idxn[],PetscScalar v[])
242: {
243: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
245: PetscInt i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
246: PetscInt cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
249: for (i=0; i<m; i++) {
250: if (idxm[i] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",idxm[i]);
251: if (idxm[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm[i],mat->rmap.N-1);
252: if (idxm[i] >= rstart && idxm[i] < rend) {
253: row = idxm[i] - rstart;
254: for (j=0; j<n; j++) {
255: if (idxn[j] < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",idxn[j]);
256: if (idxn[j] >= mat->cmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",idxn[j],mat->cmap.N-1);
257: if (idxn[j] >= cstart && idxn[j] < cend){
258: col = idxn[j] - cstart;
259: MatGetValues(aij->A,1,&row,1,&col,v+i*n+j);
260: } else {
261: if (!aij->colmap) {
262: CreateColmap_MPIAIJ_Private(mat);
263: }
264: #if defined (PETSC_USE_CTABLE)
265: PetscTableFind(aij->colmap,idxn[j]+1,&col);
266: col --;
267: #else
268: col = aij->colmap[idxn[j]] - 1;
269: #endif
270: if ((col < 0) || (aij->garray[col] != idxn[j])) *(v+i*n+j) = 0.0;
271: else {
272: MatGetValues(aij->B,1,&row,1,&col,v+i*n+j);
273: }
274: }
275: }
276: } else {
277: SETERRQ(PETSC_ERR_SUP,"Only local values currently supported");
278: }
279: }
280: return(0);
281: }
285: PetscErrorCode MatAssemblyBegin_MPIAIJ(Mat mat,MatAssemblyType mode)
286: {
287: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
289: PetscInt nstash,reallocs;
290: InsertMode addv;
293: if (aij->donotstash) {
294: return(0);
295: }
297: /* make sure all processors are either in INSERTMODE or ADDMODE */
298: MPI_Allreduce(&mat->insertmode,&addv,1,MPI_INT,MPI_BOR,mat->comm);
299: if (addv == (ADD_VALUES|INSERT_VALUES)) {
300: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Some processors inserted others added");
301: }
302: mat->insertmode = addv; /* in case this processor had no cache */
304: MatStashScatterBegin_Private(&mat->stash,mat->rmap.range);
305: MatStashGetInfo_Private(&mat->stash,&nstash,&reallocs);
306: PetscInfo2(aij->A,"Stash has %D entries, uses %D mallocs.\n",nstash,reallocs);
307: return(0);
308: }
312: PetscErrorCode MatAssemblyEnd_MPIAIJ(Mat mat,MatAssemblyType mode)
313: {
314: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
315: Mat_SeqAIJ *a=(Mat_SeqAIJ *)aij->A->data;
317: PetscMPIInt n;
318: PetscInt i,j,rstart,ncols,flg;
319: PetscInt *row,*col,other_disassembled;
320: PetscScalar *val;
321: InsertMode addv = mat->insertmode;
323: /* do not use 'b = (Mat_SeqAIJ *)aij->B->data' as B can be reset in disassembly */
325: if (!aij->donotstash) {
326: while (1) {
327: MatStashScatterGetMesg_Private(&mat->stash,&n,&row,&col,&val,&flg);
328: if (!flg) break;
330: for (i=0; i<n;) {
331: /* Now identify the consecutive vals belonging to the same row */
332: for (j=i,rstart=row[j]; j<n; j++) { if (row[j] != rstart) break; }
333: if (j < n) ncols = j-i;
334: else ncols = n-i;
335: /* Now assemble all these values with a single function call */
336: MatSetValues_MPIAIJ(mat,1,row+i,ncols,col+i,val+i,addv);
337: i = j;
338: }
339: }
340: MatStashScatterEnd_Private(&mat->stash);
341: }
342: a->compressedrow.use = PETSC_FALSE;
343: MatAssemblyBegin(aij->A,mode);
344: MatAssemblyEnd(aij->A,mode);
346: /* determine if any processor has disassembled, if so we must
347: also disassemble ourselfs, in order that we may reassemble. */
348: /*
349: if nonzero structure of submatrix B cannot change then we know that
350: no processor disassembled thus we can skip this stuff
351: */
352: if (!((Mat_SeqAIJ*)aij->B->data)->nonew) {
353: MPI_Allreduce(&mat->was_assembled,&other_disassembled,1,MPI_INT,MPI_PROD,mat->comm);
354: if (mat->was_assembled && !other_disassembled) {
355: DisAssemble_MPIAIJ(mat);
356: }
357: }
358: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) {
359: MatSetUpMultiply_MPIAIJ(mat);
360: }
361: MatSetOption(aij->B,MAT_DO_NOT_USE_INODES);
362: ((Mat_SeqAIJ *)aij->B->data)->compressedrow.use = PETSC_TRUE; /* b->compressedrow.use */
363: MatAssemblyBegin(aij->B,mode);
364: MatAssemblyEnd(aij->B,mode);
366: PetscFree(aij->rowvalues);
367: aij->rowvalues = 0;
369: /* used by MatAXPY() */
370: a->xtoy = 0; ((Mat_SeqAIJ *)aij->B->data)->xtoy = 0; /* b->xtoy = 0 */
371: a->XtoY = 0; ((Mat_SeqAIJ *)aij->B->data)->XtoY = 0; /* b->XtoY = 0 */
373: return(0);
374: }
378: PetscErrorCode MatZeroEntries_MPIAIJ(Mat A)
379: {
380: Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
384: MatZeroEntries(l->A);
385: MatZeroEntries(l->B);
386: return(0);
387: }
391: PetscErrorCode MatZeroRows_MPIAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
392: {
393: Mat_MPIAIJ *l = (Mat_MPIAIJ*)A->data;
395: PetscMPIInt size = l->size,imdex,n,rank = l->rank,tag = A->tag,lastidx = -1;
396: PetscInt i,*owners = A->rmap.range;
397: PetscInt *nprocs,j,idx,nsends,row;
398: PetscInt nmax,*svalues,*starts,*owner,nrecvs;
399: PetscInt *rvalues,count,base,slen,*source;
400: PetscInt *lens,*lrows,*values,rstart=A->rmap.rstart;
401: MPI_Comm comm = A->comm;
402: MPI_Request *send_waits,*recv_waits;
403: MPI_Status recv_status,*send_status;
404: #if defined(PETSC_DEBUG)
405: PetscTruth found = PETSC_FALSE;
406: #endif
409: /* first count number of contributors to each processor */
410: PetscMalloc(2*size*sizeof(PetscInt),&nprocs);
411: PetscMemzero(nprocs,2*size*sizeof(PetscInt));
412: PetscMalloc((N+1)*sizeof(PetscInt),&owner); /* see note*/
413: j = 0;
414: for (i=0; i<N; i++) {
415: if (lastidx > (idx = rows[i])) j = 0;
416: lastidx = idx;
417: for (; j<size; j++) {
418: if (idx >= owners[j] && idx < owners[j+1]) {
419: nprocs[2*j]++;
420: nprocs[2*j+1] = 1;
421: owner[i] = j;
422: #if defined(PETSC_DEBUG)
423: found = PETSC_TRUE;
424: #endif
425: break;
426: }
427: }
428: #if defined(PETSC_DEBUG)
429: if (!found) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Index out of range");
430: found = PETSC_FALSE;
431: #endif
432: }
433: nsends = 0; for (i=0; i<size; i++) { nsends += nprocs[2*i+1];}
435: /* inform other processors of number of messages and max length*/
436: PetscMaxSum(comm,nprocs,&nmax,&nrecvs);
438: /* post receives: */
439: PetscMalloc((nrecvs+1)*(nmax+1)*sizeof(PetscInt),&rvalues);
440: PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);
441: for (i=0; i<nrecvs; i++) {
442: MPI_Irecv(rvalues+nmax*i,nmax,MPIU_INT,MPI_ANY_SOURCE,tag,comm,recv_waits+i);
443: }
445: /* do sends:
446: 1) starts[i] gives the starting index in svalues for stuff going to
447: the ith processor
448: */
449: PetscMalloc((N+1)*sizeof(PetscInt),&svalues);
450: PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);
451: PetscMalloc((size+1)*sizeof(PetscInt),&starts);
452: starts[0] = 0;
453: for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
454: for (i=0; i<N; i++) {
455: svalues[starts[owner[i]]++] = rows[i];
456: }
458: starts[0] = 0;
459: for (i=1; i<size+1; i++) { starts[i] = starts[i-1] + nprocs[2*i-2];}
460: count = 0;
461: for (i=0; i<size; i++) {
462: if (nprocs[2*i+1]) {
463: MPI_Isend(svalues+starts[i],nprocs[2*i],MPIU_INT,i,tag,comm,send_waits+count++);
464: }
465: }
466: PetscFree(starts);
468: base = owners[rank];
470: /* wait on receives */
471: PetscMalloc(2*(nrecvs+1)*sizeof(PetscInt),&lens);
472: source = lens + nrecvs;
473: count = nrecvs; slen = 0;
474: while (count) {
475: MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);
476: /* unpack receives into our local space */
477: MPI_Get_count(&recv_status,MPIU_INT,&n);
478: source[imdex] = recv_status.MPI_SOURCE;
479: lens[imdex] = n;
480: slen += n;
481: count--;
482: }
483: PetscFree(recv_waits);
484:
485: /* move the data into the send scatter */
486: PetscMalloc((slen+1)*sizeof(PetscInt),&lrows);
487: count = 0;
488: for (i=0; i<nrecvs; i++) {
489: values = rvalues + i*nmax;
490: for (j=0; j<lens[i]; j++) {
491: lrows[count++] = values[j] - base;
492: }
493: }
494: PetscFree(rvalues);
495: PetscFree(lens);
496: PetscFree(owner);
497: PetscFree(nprocs);
498:
499: /* actually zap the local rows */
500: /*
501: Zero the required rows. If the "diagonal block" of the matrix
502: is square and the user wishes to set the diagonal we use separate
503: code so that MatSetValues() is not called for each diagonal allocating
504: new memory, thus calling lots of mallocs and slowing things down.
506: Contributed by: Matthew Knepley
507: */
508: /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
509: MatZeroRows(l->B,slen,lrows,0.0);
510: if ((diag != 0.0) && (l->A->rmap.N == l->A->cmap.N)) {
511: MatZeroRows(l->A,slen,lrows,diag);
512: } else if (diag != 0.0) {
513: MatZeroRows(l->A,slen,lrows,0.0);
514: if (((Mat_SeqAIJ*)l->A->data)->nonew) {
515: SETERRQ(PETSC_ERR_SUP,"MatZeroRows() on rectangular matrices cannot be used with the Mat options\n\
516: MAT_NO_NEW_NONZERO_LOCATIONS,MAT_NEW_NONZERO_LOCATION_ERR,MAT_NEW_NONZERO_ALLOCATION_ERR");
517: }
518: for (i = 0; i < slen; i++) {
519: row = lrows[i] + rstart;
520: MatSetValues(A,1,&row,1,&row,&diag,INSERT_VALUES);
521: }
522: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
523: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
524: } else {
525: MatZeroRows(l->A,slen,lrows,0.0);
526: }
527: PetscFree(lrows);
529: /* wait on sends */
530: if (nsends) {
531: PetscMalloc(nsends*sizeof(MPI_Status),&send_status);
532: MPI_Waitall(nsends,send_waits,send_status);
533: PetscFree(send_status);
534: }
535: PetscFree(send_waits);
536: PetscFree(svalues);
538: return(0);
539: }
543: PetscErrorCode MatMult_MPIAIJ(Mat A,Vec xx,Vec yy)
544: {
545: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
547: PetscInt nt;
550: VecGetLocalSize(xx,&nt);
551: if (nt != A->cmap.n) {
552: SETERRQ2(PETSC_ERR_ARG_SIZ,"Incompatible partition of A (%D) and xx (%D)",A->cmap.n,nt);
553: }
554: VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
555: (*a->A->ops->mult)(a->A,xx,yy);
556: VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
557: (*a->B->ops->multadd)(a->B,a->lvec,yy,yy);
558: return(0);
559: }
563: PetscErrorCode MatMultAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
564: {
565: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
569: VecScatterBegin(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
570: (*a->A->ops->multadd)(a->A,xx,yy,zz);
571: VecScatterEnd(xx,a->lvec,INSERT_VALUES,SCATTER_FORWARD,a->Mvctx);
572: (*a->B->ops->multadd)(a->B,a->lvec,zz,zz);
573: return(0);
574: }
578: PetscErrorCode MatMultTranspose_MPIAIJ(Mat A,Vec xx,Vec yy)
579: {
580: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
582: PetscTruth merged;
585: VecScatterGetMerged(a->Mvctx,&merged);
586: /* do nondiagonal part */
587: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
588: if (!merged) {
589: /* send it on its way */
590: VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
591: /* do local part */
592: (*a->A->ops->multtranspose)(a->A,xx,yy);
593: /* receive remote parts: note this assumes the values are not actually */
594: /* added in yy until the next line, */
595: VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
596: } else {
597: /* do local part */
598: (*a->A->ops->multtranspose)(a->A,xx,yy);
599: /* send it on its way */
600: VecScatterBegin(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
601: /* values actually were received in the Begin() but we need to call this nop */
602: VecScatterEnd(a->lvec,yy,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
603: }
604: return(0);
605: }
610: PetscErrorCode MatIsTranspose_MPIAIJ(Mat Amat,Mat Bmat,PetscReal tol,PetscTruth *f)
611: {
612: MPI_Comm comm;
613: Mat_MPIAIJ *Aij = (Mat_MPIAIJ *) Amat->data, *Bij;
614: Mat Adia = Aij->A, Bdia, Aoff,Boff,*Aoffs,*Boffs;
615: IS Me,Notme;
617: PetscInt M,N,first,last,*notme,i;
618: PetscMPIInt size;
622: /* Easy test: symmetric diagonal block */
623: Bij = (Mat_MPIAIJ *) Bmat->data; Bdia = Bij->A;
624: MatIsTranspose(Adia,Bdia,tol,f);
625: if (!*f) return(0);
626: PetscObjectGetComm((PetscObject)Amat,&comm);
627: MPI_Comm_size(comm,&size);
628: if (size == 1) return(0);
630: /* Hard test: off-diagonal block. This takes a MatGetSubMatrix. */
631: MatGetSize(Amat,&M,&N);
632: MatGetOwnershipRange(Amat,&first,&last);
633: PetscMalloc((N-last+first)*sizeof(PetscInt),¬me);
634: for (i=0; i<first; i++) notme[i] = i;
635: for (i=last; i<M; i++) notme[i-last+first] = i;
636: ISCreateGeneral(MPI_COMM_SELF,N-last+first,notme,&Notme);
637: ISCreateStride(MPI_COMM_SELF,last-first,first,1,&Me);
638: MatGetSubMatrices(Amat,1,&Me,&Notme,MAT_INITIAL_MATRIX,&Aoffs);
639: Aoff = Aoffs[0];
640: MatGetSubMatrices(Bmat,1,&Notme,&Me,MAT_INITIAL_MATRIX,&Boffs);
641: Boff = Boffs[0];
642: MatIsTranspose(Aoff,Boff,tol,f);
643: MatDestroyMatrices(1,&Aoffs);
644: MatDestroyMatrices(1,&Boffs);
645: ISDestroy(Me);
646: ISDestroy(Notme);
648: return(0);
649: }
654: PetscErrorCode MatMultTransposeAdd_MPIAIJ(Mat A,Vec xx,Vec yy,Vec zz)
655: {
656: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
660: /* do nondiagonal part */
661: (*a->B->ops->multtranspose)(a->B,xx,a->lvec);
662: /* send it on its way */
663: VecScatterBegin(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
664: /* do local part */
665: (*a->A->ops->multtransposeadd)(a->A,xx,yy,zz);
666: /* receive remote parts */
667: VecScatterEnd(a->lvec,zz,ADD_VALUES,SCATTER_REVERSE,a->Mvctx);
668: return(0);
669: }
671: /*
672: This only works correctly for square matrices where the subblock A->A is the
673: diagonal block
674: */
677: PetscErrorCode MatGetDiagonal_MPIAIJ(Mat A,Vec v)
678: {
680: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
683: if (A->rmap.N != A->cmap.N) SETERRQ(PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block");
684: if (A->rmap.rstart != A->cmap.rstart || A->rmap.rend != A->cmap.rend) {
685: SETERRQ(PETSC_ERR_ARG_SIZ,"row partition must equal col partition");
686: }
687: MatGetDiagonal(a->A,v);
688: return(0);
689: }
693: PetscErrorCode MatScale_MPIAIJ(Mat A,PetscScalar aa)
694: {
695: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
699: MatScale(a->A,aa);
700: MatScale(a->B,aa);
701: return(0);
702: }
706: PetscErrorCode MatDestroy_MPIAIJ(Mat mat)
707: {
708: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
712: #if defined(PETSC_USE_LOG)
713: PetscLogObjectState((PetscObject)mat,"Rows=%D, Cols=%D",mat->rmap.N,mat->cmap.N);
714: #endif
715: MatStashDestroy_Private(&mat->stash);
716: MatDestroy(aij->A);
717: MatDestroy(aij->B);
718: #if defined (PETSC_USE_CTABLE)
719: if (aij->colmap) {PetscTableDelete(aij->colmap);}
720: #else
721: PetscFree(aij->colmap);
722: #endif
723: PetscFree(aij->garray);
724: if (aij->lvec) {VecDestroy(aij->lvec);}
725: if (aij->Mvctx) {VecScatterDestroy(aij->Mvctx);}
726: PetscFree(aij->rowvalues);
727: PetscFree(aij);
729: PetscObjectChangeTypeName((PetscObject)mat,0);
730: PetscObjectComposeFunction((PetscObject)mat,"MatStoreValues_C","",PETSC_NULL);
731: PetscObjectComposeFunction((PetscObject)mat,"MatRetrieveValues_C","",PETSC_NULL);
732: PetscObjectComposeFunction((PetscObject)mat,"MatGetDiagonalBlock_C","",PETSC_NULL);
733: PetscObjectComposeFunction((PetscObject)mat,"MatIsTranspose_C","",PETSC_NULL);
734: PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocation_C","",PETSC_NULL);
735: PetscObjectComposeFunction((PetscObject)mat,"MatMPIAIJSetPreallocationCSR_C","",PETSC_NULL);
736: PetscObjectComposeFunction((PetscObject)mat,"MatDiagonalScaleLocal_C","",PETSC_NULL);
737: return(0);
738: }
742: PetscErrorCode MatView_MPIAIJ_Binary(Mat mat,PetscViewer viewer)
743: {
744: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
745: Mat_SeqAIJ* A = (Mat_SeqAIJ*)aij->A->data;
746: Mat_SeqAIJ* B = (Mat_SeqAIJ*)aij->B->data;
747: PetscErrorCode ierr;
748: PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag;
749: int fd;
750: PetscInt nz,header[4],*row_lengths,*range=0,rlen,i;
751: PetscInt nzmax,*column_indices,j,k,col,*garray = aij->garray,cnt,cstart = mat->cmap.rstart,rnz;
752: PetscScalar *column_values;
755: MPI_Comm_rank(mat->comm,&rank);
756: MPI_Comm_size(mat->comm,&size);
757: nz = A->nz + B->nz;
758: if (!rank) {
759: header[0] = MAT_FILE_COOKIE;
760: header[1] = mat->rmap.N;
761: header[2] = mat->cmap.N;
762: MPI_Reduce(&nz,&header[3],1,MPIU_INT,MPI_SUM,0,mat->comm);
763: PetscViewerBinaryGetDescriptor(viewer,&fd);
764: PetscBinaryWrite(fd,header,4,PETSC_INT,PETSC_TRUE);
765: /* get largest number of rows any processor has */
766: rlen = mat->rmap.n;
767: range = mat->rmap.range;
768: for (i=1; i<size; i++) {
769: rlen = PetscMax(rlen,range[i+1] - range[i]);
770: }
771: } else {
772: MPI_Reduce(&nz,0,1,MPIU_INT,MPI_SUM,0,mat->comm);
773: rlen = mat->rmap.n;
774: }
776: /* load up the local row counts */
777: PetscMalloc((rlen+1)*sizeof(PetscInt),&row_lengths);
778: for (i=0; i<mat->rmap.n; i++) {
779: row_lengths[i] = A->i[i+1] - A->i[i] + B->i[i+1] - B->i[i];
780: }
782: /* store the row lengths to the file */
783: if (!rank) {
784: MPI_Status status;
785: PetscBinaryWrite(fd,row_lengths,mat->rmap.n,PETSC_INT,PETSC_TRUE);
786: for (i=1; i<size; i++) {
787: rlen = range[i+1] - range[i];
788: MPI_Recv(row_lengths,rlen,MPIU_INT,i,tag,mat->comm,&status);
789: PetscBinaryWrite(fd,row_lengths,rlen,PETSC_INT,PETSC_TRUE);
790: }
791: } else {
792: MPI_Send(row_lengths,mat->rmap.n,MPIU_INT,0,tag,mat->comm);
793: }
794: PetscFree(row_lengths);
796: /* load up the local column indices */
797: nzmax = nz; /* )th processor needs space a largest processor needs */
798: MPI_Reduce(&nz,&nzmax,1,MPIU_INT,MPI_MAX,0,mat->comm);
799: PetscMalloc((nzmax+1)*sizeof(PetscInt),&column_indices);
800: cnt = 0;
801: for (i=0; i<mat->rmap.n; i++) {
802: for (j=B->i[i]; j<B->i[i+1]; j++) {
803: if ( (col = garray[B->j[j]]) > cstart) break;
804: column_indices[cnt++] = col;
805: }
806: for (k=A->i[i]; k<A->i[i+1]; k++) {
807: column_indices[cnt++] = A->j[k] + cstart;
808: }
809: for (; j<B->i[i+1]; j++) {
810: column_indices[cnt++] = garray[B->j[j]];
811: }
812: }
813: if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
815: /* store the column indices to the file */
816: if (!rank) {
817: MPI_Status status;
818: PetscBinaryWrite(fd,column_indices,nz,PETSC_INT,PETSC_TRUE);
819: for (i=1; i<size; i++) {
820: MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);
821: if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
822: MPI_Recv(column_indices,rnz,MPIU_INT,i,tag,mat->comm,&status);
823: PetscBinaryWrite(fd,column_indices,rnz,PETSC_INT,PETSC_TRUE);
824: }
825: } else {
826: MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);
827: MPI_Send(column_indices,nz,MPIU_INT,0,tag,mat->comm);
828: }
829: PetscFree(column_indices);
831: /* load up the local column values */
832: PetscMalloc((nzmax+1)*sizeof(PetscScalar),&column_values);
833: cnt = 0;
834: for (i=0; i<mat->rmap.n; i++) {
835: for (j=B->i[i]; j<B->i[i+1]; j++) {
836: if ( garray[B->j[j]] > cstart) break;
837: column_values[cnt++] = B->a[j];
838: }
839: for (k=A->i[i]; k<A->i[i+1]; k++) {
840: column_values[cnt++] = A->a[k];
841: }
842: for (; j<B->i[i+1]; j++) {
843: column_values[cnt++] = B->a[j];
844: }
845: }
846: if (cnt != A->nz + B->nz) SETERRQ2(PETSC_ERR_PLIB,"Internal PETSc error: cnt = %D nz = %D",cnt,A->nz+B->nz);
848: /* store the column values to the file */
849: if (!rank) {
850: MPI_Status status;
851: PetscBinaryWrite(fd,column_values,nz,PETSC_SCALAR,PETSC_TRUE);
852: for (i=1; i<size; i++) {
853: MPI_Recv(&rnz,1,MPIU_INT,i,tag,mat->comm,&status);
854: if (rnz > nzmax) SETERRQ2(PETSC_ERR_LIB,"Internal PETSc error: nz = %D nzmax = %D",nz,nzmax);
855: MPI_Recv(column_values,rnz,MPIU_SCALAR,i,tag,mat->comm,&status);
856: PetscBinaryWrite(fd,column_values,rnz,PETSC_SCALAR,PETSC_TRUE);
857: }
858: } else {
859: MPI_Send(&nz,1,MPIU_INT,0,tag,mat->comm);
860: MPI_Send(column_values,nz,MPIU_SCALAR,0,tag,mat->comm);
861: }
862: PetscFree(column_values);
863: return(0);
864: }
868: PetscErrorCode MatView_MPIAIJ_ASCIIorDraworSocket(Mat mat,PetscViewer viewer)
869: {
870: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
871: PetscErrorCode ierr;
872: PetscMPIInt rank = aij->rank,size = aij->size;
873: PetscTruth isdraw,iascii,isbinary;
874: PetscViewer sviewer;
875: PetscViewerFormat format;
878: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
879: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
880: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
881: if (iascii) {
882: PetscViewerGetFormat(viewer,&format);
883: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
884: MatInfo info;
885: PetscTruth inodes;
887: MPI_Comm_rank(mat->comm,&rank);
888: MatGetInfo(mat,MAT_LOCAL,&info);
889: MatInodeGetInodeSizes(aij->A,PETSC_NULL,(PetscInt **)&inodes,PETSC_NULL);
890: if (!inodes) {
891: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, not using I-node routines\n",
892: rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
893: } else {
894: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Local rows %D nz %D nz alloced %D mem %D, using I-node routines\n",
895: rank,mat->rmap.n,(PetscInt)info.nz_used,(PetscInt)info.nz_allocated,(PetscInt)info.memory);
896: }
897: MatGetInfo(aij->A,MAT_LOCAL,&info);
898: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] on-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
899: MatGetInfo(aij->B,MAT_LOCAL,&info);
900: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] off-diagonal part: nz %D \n",rank,(PetscInt)info.nz_used);
901: PetscViewerFlush(viewer);
902: VecScatterView(aij->Mvctx,viewer);
903: return(0);
904: } else if (format == PETSC_VIEWER_ASCII_INFO) {
905: PetscInt inodecount,inodelimit,*inodes;
906: MatInodeGetInodeSizes(aij->A,&inodecount,&inodes,&inodelimit);
907: if (inodes) {
908: PetscViewerASCIIPrintf(viewer,"using I-node (on process 0) routines: found %D nodes, limit used is %D\n",inodecount,inodelimit);
909: } else {
910: PetscViewerASCIIPrintf(viewer,"not using I-node (on process 0) routines\n");
911: }
912: return(0);
913: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
914: return(0);
915: }
916: } else if (isbinary) {
917: if (size == 1) {
918: PetscObjectSetName((PetscObject)aij->A,mat->name);
919: MatView(aij->A,viewer);
920: } else {
921: MatView_MPIAIJ_Binary(mat,viewer);
922: }
923: return(0);
924: } else if (isdraw) {
925: PetscDraw draw;
926: PetscTruth isnull;
927: PetscViewerDrawGetDraw(viewer,0,&draw);
928: PetscDrawIsNull(draw,&isnull); if (isnull) return(0);
929: }
931: if (size == 1) {
932: PetscObjectSetName((PetscObject)aij->A,mat->name);
933: MatView(aij->A,viewer);
934: } else {
935: /* assemble the entire matrix onto first processor. */
936: Mat A;
937: Mat_SeqAIJ *Aloc;
938: PetscInt M = mat->rmap.N,N = mat->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
939: PetscScalar *a;
941: MatCreate(mat->comm,&A);
942: if (!rank) {
943: MatSetSizes(A,M,N,M,N);
944: } else {
945: MatSetSizes(A,0,0,M,N);
946: }
947: /* This is just a temporary matrix, so explicitly using MATMPIAIJ is probably best */
948: MatSetType(A,MATMPIAIJ);
949: MatMPIAIJSetPreallocation(A,0,PETSC_NULL,0,PETSC_NULL);
950: PetscLogObjectParent(mat,A);
952: /* copy over the A part */
953: Aloc = (Mat_SeqAIJ*)aij->A->data;
954: m = aij->A->rmap.n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
955: row = mat->rmap.rstart;
956: for (i=0; i<ai[m]; i++) {aj[i] += mat->cmap.rstart ;}
957: for (i=0; i<m; i++) {
958: MatSetValues(A,1,&row,ai[i+1]-ai[i],aj,a,INSERT_VALUES);
959: row++; a += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
960: }
961: aj = Aloc->j;
962: for (i=0; i<ai[m]; i++) {aj[i] -= mat->cmap.rstart;}
964: /* copy over the B part */
965: Aloc = (Mat_SeqAIJ*)aij->B->data;
966: m = aij->B->rmap.n; ai = Aloc->i; aj = Aloc->j; a = Aloc->a;
967: row = mat->rmap.rstart;
968: PetscMalloc((ai[m]+1)*sizeof(PetscInt),&cols);
969: ct = cols;
970: for (i=0; i<ai[m]; i++) {cols[i] = aij->garray[aj[i]];}
971: for (i=0; i<m; i++) {
972: MatSetValues(A,1,&row,ai[i+1]-ai[i],cols,a,INSERT_VALUES);
973: row++; a += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
974: }
975: PetscFree(ct);
976: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
977: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
978: /*
979: Everyone has to call to draw the matrix since the graphics waits are
980: synchronized across all processors that share the PetscDraw object
981: */
982: PetscViewerGetSingleton(viewer,&sviewer);
983: if (!rank) {
984: PetscObjectSetName((PetscObject)((Mat_MPIAIJ*)(A->data))->A,mat->name);
985: MatView(((Mat_MPIAIJ*)(A->data))->A,sviewer);
986: }
987: PetscViewerRestoreSingleton(viewer,&sviewer);
988: MatDestroy(A);
989: }
990: return(0);
991: }
995: PetscErrorCode MatView_MPIAIJ(Mat mat,PetscViewer viewer)
996: {
998: PetscTruth iascii,isdraw,issocket,isbinary;
999:
1001: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
1002: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);
1003: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
1004: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
1005: if (iascii || isdraw || isbinary || issocket) {
1006: MatView_MPIAIJ_ASCIIorDraworSocket(mat,viewer);
1007: } else {
1008: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by MPIAIJ matrices",((PetscObject)viewer)->type_name);
1009: }
1010: return(0);
1011: }
1017: PetscErrorCode MatRelax_MPIAIJ(Mat matin,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1018: {
1019: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1021: Vec bb1;
1024: if (its <= 0 || lits <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
1026: VecDuplicate(bb,&bb1);
1028: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP){
1029: if (flag & SOR_ZERO_INITIAL_GUESS) {
1030: (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,lits,xx);
1031: its--;
1032: }
1033:
1034: while (its--) {
1035: VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1036: VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1038: /* update rhs: bb1 = bb - B*x */
1039: VecScale(mat->lvec,-1.0);
1040: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1042: /* local sweep */
1043: (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_SYMMETRIC_SWEEP,fshift,lits,lits,xx);
1044:
1045: }
1046: } else if (flag & SOR_LOCAL_FORWARD_SWEEP){
1047: if (flag & SOR_ZERO_INITIAL_GUESS) {
1048: (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);
1049: its--;
1050: }
1051: while (its--) {
1052: VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1053: VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1055: /* update rhs: bb1 = bb - B*x */
1056: VecScale(mat->lvec,-1.0);
1057: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1059: /* local sweep */
1060: (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_FORWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1061:
1062: }
1063: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP){
1064: if (flag & SOR_ZERO_INITIAL_GUESS) {
1065: (*mat->A->ops->relax)(mat->A,bb,omega,flag,fshift,lits,PETSC_NULL,xx);
1066: its--;
1067: }
1068: while (its--) {
1069: VecScatterBegin(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1070: VecScatterEnd(xx,mat->lvec,INSERT_VALUES,SCATTER_FORWARD,mat->Mvctx);
1072: /* update rhs: bb1 = bb - B*x */
1073: VecScale(mat->lvec,-1.0);
1074: (*mat->B->ops->multadd)(mat->B,mat->lvec,bb,bb1);
1076: /* local sweep */
1077: (*mat->A->ops->relax)(mat->A,bb1,omega,SOR_BACKWARD_SWEEP,fshift,lits,PETSC_NULL,xx);
1078:
1079: }
1080: } else {
1081: SETERRQ(PETSC_ERR_SUP,"Parallel SOR not supported");
1082: }
1084: VecDestroy(bb1);
1085: return(0);
1086: }
1090: PetscErrorCode MatPermute_MPIAIJ(Mat A,IS rowp,IS colp,Mat *B)
1091: {
1092: MPI_Comm comm,pcomm;
1093: PetscInt first,local_size,nrows,*rows;
1094: int ntids;
1095: IS crowp,growp,irowp,lrowp,lcolp,icolp;
1099: PetscObjectGetComm((PetscObject)A,&comm);
1100: /* make a collective version of 'rowp' */
1101: PetscObjectGetComm((PetscObject)rowp,&pcomm);
1102: if (pcomm==comm) {
1103: crowp = rowp;
1104: } else {
1105: ISGetSize(rowp,&nrows);
1106: ISGetIndices(rowp,&rows);
1107: ISCreateGeneral(comm,nrows,rows,&crowp);
1108: ISRestoreIndices(rowp,&rows);
1109: }
1110: /* collect the global row permutation and invert it */
1111: ISAllGather(crowp,&growp);
1112: ISSetPermutation(growp);
1113: if (pcomm!=comm) {
1114: ISDestroy(crowp);
1115: }
1116: ISInvertPermutation(growp,PETSC_DECIDE,&irowp);
1117: /* get the local target indices */
1118: MatGetOwnershipRange(A,&first,PETSC_NULL);
1119: MatGetLocalSize(A,&local_size,PETSC_NULL);
1120: ISGetIndices(irowp,&rows);
1121: ISCreateGeneral(MPI_COMM_SELF,local_size,rows+first,&lrowp);
1122: ISRestoreIndices(irowp,&rows);
1123: ISDestroy(irowp);
1124: /* the column permutation is so much easier;
1125: make a local version of 'colp' and invert it */
1126: PetscObjectGetComm((PetscObject)colp,&pcomm);
1127: MPI_Comm_size(pcomm,&ntids);
1128: if (ntids==1) {
1129: lcolp = colp;
1130: } else {
1131: ISGetSize(colp,&nrows);
1132: ISGetIndices(colp,&rows);
1133: ISCreateGeneral(MPI_COMM_SELF,nrows,rows,&lcolp);
1134: }
1135: ISInvertPermutation(lcolp,PETSC_DECIDE,&icolp);
1136: ISSetPermutation(lcolp);
1137: if (ntids>1) {
1138: ISRestoreIndices(colp,&rows);
1139: ISDestroy(lcolp);
1140: }
1141: /* now we just get the submatrix */
1142: MatGetSubMatrix(A,lrowp,icolp,local_size,MAT_INITIAL_MATRIX,B);
1143: /* clean up */
1144: ISDestroy(lrowp);
1145: ISDestroy(icolp);
1146: return(0);
1147: }
1151: PetscErrorCode MatGetInfo_MPIAIJ(Mat matin,MatInfoType flag,MatInfo *info)
1152: {
1153: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1154: Mat A = mat->A,B = mat->B;
1156: PetscReal isend[5],irecv[5];
1159: info->block_size = 1.0;
1160: MatGetInfo(A,MAT_LOCAL,info);
1161: isend[0] = info->nz_used; isend[1] = info->nz_allocated; isend[2] = info->nz_unneeded;
1162: isend[3] = info->memory; isend[4] = info->mallocs;
1163: MatGetInfo(B,MAT_LOCAL,info);
1164: isend[0] += info->nz_used; isend[1] += info->nz_allocated; isend[2] += info->nz_unneeded;
1165: isend[3] += info->memory; isend[4] += info->mallocs;
1166: if (flag == MAT_LOCAL) {
1167: info->nz_used = isend[0];
1168: info->nz_allocated = isend[1];
1169: info->nz_unneeded = isend[2];
1170: info->memory = isend[3];
1171: info->mallocs = isend[4];
1172: } else if (flag == MAT_GLOBAL_MAX) {
1173: MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_MAX,matin->comm);
1174: info->nz_used = irecv[0];
1175: info->nz_allocated = irecv[1];
1176: info->nz_unneeded = irecv[2];
1177: info->memory = irecv[3];
1178: info->mallocs = irecv[4];
1179: } else if (flag == MAT_GLOBAL_SUM) {
1180: MPI_Allreduce(isend,irecv,5,MPIU_REAL,MPI_SUM,matin->comm);
1181: info->nz_used = irecv[0];
1182: info->nz_allocated = irecv[1];
1183: info->nz_unneeded = irecv[2];
1184: info->memory = irecv[3];
1185: info->mallocs = irecv[4];
1186: }
1187: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1188: info->fill_ratio_needed = 0;
1189: info->factor_mallocs = 0;
1190: info->rows_global = (double)matin->rmap.N;
1191: info->columns_global = (double)matin->cmap.N;
1192: info->rows_local = (double)matin->rmap.n;
1193: info->columns_local = (double)matin->cmap.N;
1195: return(0);
1196: }
1200: PetscErrorCode MatSetOption_MPIAIJ(Mat A,MatOption op)
1201: {
1202: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1206: switch (op) {
1207: case MAT_NO_NEW_NONZERO_LOCATIONS:
1208: case MAT_YES_NEW_NONZERO_LOCATIONS:
1209: case MAT_COLUMNS_UNSORTED:
1210: case MAT_COLUMNS_SORTED:
1211: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1212: case MAT_KEEP_ZEROED_ROWS:
1213: case MAT_NEW_NONZERO_LOCATION_ERR:
1214: case MAT_USE_INODES:
1215: case MAT_DO_NOT_USE_INODES:
1216: case MAT_IGNORE_ZERO_ENTRIES:
1217: MatSetOption(a->A,op);
1218: MatSetOption(a->B,op);
1219: break;
1220: case MAT_ROW_ORIENTED:
1221: a->roworiented = PETSC_TRUE;
1222: MatSetOption(a->A,op);
1223: MatSetOption(a->B,op);
1224: break;
1225: case MAT_ROWS_SORTED:
1226: case MAT_ROWS_UNSORTED:
1227: case MAT_YES_NEW_DIAGONALS:
1228: PetscInfo(A,"Option ignored\n");
1229: break;
1230: case MAT_COLUMN_ORIENTED:
1231: a->roworiented = PETSC_FALSE;
1232: MatSetOption(a->A,op);
1233: MatSetOption(a->B,op);
1234: break;
1235: case MAT_IGNORE_OFF_PROC_ENTRIES:
1236: a->donotstash = PETSC_TRUE;
1237: break;
1238: case MAT_NO_NEW_DIAGONALS:
1239: SETERRQ(PETSC_ERR_SUP,"MAT_NO_NEW_DIAGONALS");
1240: case MAT_SYMMETRIC:
1241: MatSetOption(a->A,op);
1242: break;
1243: case MAT_STRUCTURALLY_SYMMETRIC:
1244: case MAT_HERMITIAN:
1245: case MAT_SYMMETRY_ETERNAL:
1246: MatSetOption(a->A,op);
1247: break;
1248: case MAT_NOT_SYMMETRIC:
1249: case MAT_NOT_STRUCTURALLY_SYMMETRIC:
1250: case MAT_NOT_HERMITIAN:
1251: case MAT_NOT_SYMMETRY_ETERNAL:
1252: break;
1253: default:
1254: SETERRQ1(PETSC_ERR_SUP,"unknown option %d",op);
1255: }
1256: return(0);
1257: }
1261: PetscErrorCode MatGetRow_MPIAIJ(Mat matin,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1262: {
1263: Mat_MPIAIJ *mat = (Mat_MPIAIJ*)matin->data;
1264: PetscScalar *vworkA,*vworkB,**pvA,**pvB,*v_p;
1266: PetscInt i,*cworkA,*cworkB,**pcA,**pcB,cstart = matin->cmap.rstart;
1267: PetscInt nztot,nzA,nzB,lrow,rstart = matin->rmap.rstart,rend = matin->rmap.rend;
1268: PetscInt *cmap,*idx_p;
1271: if (mat->getrowactive) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Already active");
1272: mat->getrowactive = PETSC_TRUE;
1274: if (!mat->rowvalues && (idx || v)) {
1275: /*
1276: allocate enough space to hold information from the longest row.
1277: */
1278: Mat_SeqAIJ *Aa = (Mat_SeqAIJ*)mat->A->data,*Ba = (Mat_SeqAIJ*)mat->B->data;
1279: PetscInt max = 1,tmp;
1280: for (i=0; i<matin->rmap.n; i++) {
1281: tmp = Aa->i[i+1] - Aa->i[i] + Ba->i[i+1] - Ba->i[i];
1282: if (max < tmp) { max = tmp; }
1283: }
1284: PetscMalloc(max*(sizeof(PetscInt)+sizeof(PetscScalar)),&mat->rowvalues);
1285: mat->rowindices = (PetscInt*)(mat->rowvalues + max);
1286: }
1288: if (row < rstart || row >= rend) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Only local rows")
1289: lrow = row - rstart;
1291: pvA = &vworkA; pcA = &cworkA; pvB = &vworkB; pcB = &cworkB;
1292: if (!v) {pvA = 0; pvB = 0;}
1293: if (!idx) {pcA = 0; if (!v) pcB = 0;}
1294: (*mat->A->ops->getrow)(mat->A,lrow,&nzA,pcA,pvA);
1295: (*mat->B->ops->getrow)(mat->B,lrow,&nzB,pcB,pvB);
1296: nztot = nzA + nzB;
1298: cmap = mat->garray;
1299: if (v || idx) {
1300: if (nztot) {
1301: /* Sort by increasing column numbers, assuming A and B already sorted */
1302: PetscInt imark = -1;
1303: if (v) {
1304: *v = v_p = mat->rowvalues;
1305: for (i=0; i<nzB; i++) {
1306: if (cmap[cworkB[i]] < cstart) v_p[i] = vworkB[i];
1307: else break;
1308: }
1309: imark = i;
1310: for (i=0; i<nzA; i++) v_p[imark+i] = vworkA[i];
1311: for (i=imark; i<nzB; i++) v_p[nzA+i] = vworkB[i];
1312: }
1313: if (idx) {
1314: *idx = idx_p = mat->rowindices;
1315: if (imark > -1) {
1316: for (i=0; i<imark; i++) {
1317: idx_p[i] = cmap[cworkB[i]];
1318: }
1319: } else {
1320: for (i=0; i<nzB; i++) {
1321: if (cmap[cworkB[i]] < cstart) idx_p[i] = cmap[cworkB[i]];
1322: else break;
1323: }
1324: imark = i;
1325: }
1326: for (i=0; i<nzA; i++) idx_p[imark+i] = cstart + cworkA[i];
1327: for (i=imark; i<nzB; i++) idx_p[nzA+i] = cmap[cworkB[i]];
1328: }
1329: } else {
1330: if (idx) *idx = 0;
1331: if (v) *v = 0;
1332: }
1333: }
1334: *nz = nztot;
1335: (*mat->A->ops->restorerow)(mat->A,lrow,&nzA,pcA,pvA);
1336: (*mat->B->ops->restorerow)(mat->B,lrow,&nzB,pcB,pvB);
1337: return(0);
1338: }
1342: PetscErrorCode MatRestoreRow_MPIAIJ(Mat mat,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1343: {
1344: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1347: if (!aij->getrowactive) {
1348: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"MatGetRow() must be called first");
1349: }
1350: aij->getrowactive = PETSC_FALSE;
1351: return(0);
1352: }
1356: PetscErrorCode MatNorm_MPIAIJ(Mat mat,NormType type,PetscReal *norm)
1357: {
1358: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1359: Mat_SeqAIJ *amat = (Mat_SeqAIJ*)aij->A->data,*bmat = (Mat_SeqAIJ*)aij->B->data;
1361: PetscInt i,j,cstart = mat->cmap.rstart;
1362: PetscReal sum = 0.0;
1363: PetscScalar *v;
1366: if (aij->size == 1) {
1367: MatNorm(aij->A,type,norm);
1368: } else {
1369: if (type == NORM_FROBENIUS) {
1370: v = amat->a;
1371: for (i=0; i<amat->nz; i++) {
1372: #if defined(PETSC_USE_COMPLEX)
1373: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1374: #else
1375: sum += (*v)*(*v); v++;
1376: #endif
1377: }
1378: v = bmat->a;
1379: for (i=0; i<bmat->nz; i++) {
1380: #if defined(PETSC_USE_COMPLEX)
1381: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1382: #else
1383: sum += (*v)*(*v); v++;
1384: #endif
1385: }
1386: MPI_Allreduce(&sum,norm,1,MPIU_REAL,MPI_SUM,mat->comm);
1387: *norm = sqrt(*norm);
1388: } else if (type == NORM_1) { /* max column norm */
1389: PetscReal *tmp,*tmp2;
1390: PetscInt *jj,*garray = aij->garray;
1391: PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp);
1392: PetscMalloc((mat->cmap.N+1)*sizeof(PetscReal),&tmp2);
1393: PetscMemzero(tmp,mat->cmap.N*sizeof(PetscReal));
1394: *norm = 0.0;
1395: v = amat->a; jj = amat->j;
1396: for (j=0; j<amat->nz; j++) {
1397: tmp[cstart + *jj++ ] += PetscAbsScalar(*v); v++;
1398: }
1399: v = bmat->a; jj = bmat->j;
1400: for (j=0; j<bmat->nz; j++) {
1401: tmp[garray[*jj++]] += PetscAbsScalar(*v); v++;
1402: }
1403: MPI_Allreduce(tmp,tmp2,mat->cmap.N,MPIU_REAL,MPI_SUM,mat->comm);
1404: for (j=0; j<mat->cmap.N; j++) {
1405: if (tmp2[j] > *norm) *norm = tmp2[j];
1406: }
1407: PetscFree(tmp);
1408: PetscFree(tmp2);
1409: } else if (type == NORM_INFINITY) { /* max row norm */
1410: PetscReal ntemp = 0.0;
1411: for (j=0; j<aij->A->rmap.n; j++) {
1412: v = amat->a + amat->i[j];
1413: sum = 0.0;
1414: for (i=0; i<amat->i[j+1]-amat->i[j]; i++) {
1415: sum += PetscAbsScalar(*v); v++;
1416: }
1417: v = bmat->a + bmat->i[j];
1418: for (i=0; i<bmat->i[j+1]-bmat->i[j]; i++) {
1419: sum += PetscAbsScalar(*v); v++;
1420: }
1421: if (sum > ntemp) ntemp = sum;
1422: }
1423: MPI_Allreduce(&ntemp,norm,1,MPIU_REAL,MPI_MAX,mat->comm);
1424: } else {
1425: SETERRQ(PETSC_ERR_SUP,"No support for two norm");
1426: }
1427: }
1428: return(0);
1429: }
1433: PetscErrorCode MatTranspose_MPIAIJ(Mat A,Mat *matout)
1434: {
1435: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1436: Mat_SeqAIJ *Aloc = (Mat_SeqAIJ*)a->A->data;
1438: PetscInt M = A->rmap.N,N = A->cmap.N,m,*ai,*aj,row,*cols,i,*ct;
1439: Mat B;
1440: PetscScalar *array;
1443: if (!matout && M != N) {
1444: SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1445: }
1447: MatCreate(A->comm,&B);
1448: MatSetSizes(B,A->cmap.n,A->rmap.n,N,M);
1449: MatSetType(B,A->type_name);
1450: MatMPIAIJSetPreallocation(B,0,PETSC_NULL,0,PETSC_NULL);
1452: /* copy over the A part */
1453: Aloc = (Mat_SeqAIJ*)a->A->data;
1454: m = a->A->rmap.n; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1455: row = A->rmap.rstart;
1456: for (i=0; i<ai[m]; i++) {aj[i] += A->cmap.rstart ;}
1457: for (i=0; i<m; i++) {
1458: MatSetValues(B,ai[i+1]-ai[i],aj,1,&row,array,INSERT_VALUES);
1459: row++; array += ai[i+1]-ai[i]; aj += ai[i+1]-ai[i];
1460: }
1461: aj = Aloc->j;
1462: for (i=0; i<ai[m]; i++) {aj[i] -= A->cmap.rstart ;}
1464: /* copy over the B part */
1465: Aloc = (Mat_SeqAIJ*)a->B->data;
1466: m = a->B->rmap.n; ai = Aloc->i; aj = Aloc->j; array = Aloc->a;
1467: row = A->rmap.rstart;
1468: PetscMalloc((1+ai[m])*sizeof(PetscInt),&cols);
1469: ct = cols;
1470: for (i=0; i<ai[m]; i++) {cols[i] = a->garray[aj[i]];}
1471: for (i=0; i<m; i++) {
1472: MatSetValues(B,ai[i+1]-ai[i],cols,1,&row,array,INSERT_VALUES);
1473: row++; array += ai[i+1]-ai[i]; cols += ai[i+1]-ai[i];
1474: }
1475: PetscFree(ct);
1476: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1477: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1478: if (matout) {
1479: *matout = B;
1480: } else {
1481: MatHeaderCopy(A,B);
1482: }
1483: return(0);
1484: }
1488: PetscErrorCode MatDiagonalScale_MPIAIJ(Mat mat,Vec ll,Vec rr)
1489: {
1490: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
1491: Mat a = aij->A,b = aij->B;
1493: PetscInt s1,s2,s3;
1496: MatGetLocalSize(mat,&s2,&s3);
1497: if (rr) {
1498: VecGetLocalSize(rr,&s1);
1499: if (s1!=s3) SETERRQ(PETSC_ERR_ARG_SIZ,"right vector non-conforming local size");
1500: /* Overlap communication with computation. */
1501: VecScatterBegin(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);
1502: }
1503: if (ll) {
1504: VecGetLocalSize(ll,&s1);
1505: if (s1!=s2) SETERRQ(PETSC_ERR_ARG_SIZ,"left vector non-conforming local size");
1506: (*b->ops->diagonalscale)(b,ll,0);
1507: }
1508: /* scale the diagonal block */
1509: (*a->ops->diagonalscale)(a,ll,rr);
1511: if (rr) {
1512: /* Do a scatter end and then right scale the off-diagonal block */
1513: VecScatterEnd(rr,aij->lvec,INSERT_VALUES,SCATTER_FORWARD,aij->Mvctx);
1514: (*b->ops->diagonalscale)(b,0,aij->lvec);
1515: }
1516:
1517: return(0);
1518: }
1522: PetscErrorCode MatSetBlockSize_MPIAIJ(Mat A,PetscInt bs)
1523: {
1524: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1528: MatSetBlockSize(a->A,bs);
1529: MatSetBlockSize(a->B,bs);
1530: return(0);
1531: }
1534: PetscErrorCode MatSetUnfactored_MPIAIJ(Mat A)
1535: {
1536: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1540: MatSetUnfactored(a->A);
1541: return(0);
1542: }
1546: PetscErrorCode MatEqual_MPIAIJ(Mat A,Mat B,PetscTruth *flag)
1547: {
1548: Mat_MPIAIJ *matB = (Mat_MPIAIJ*)B->data,*matA = (Mat_MPIAIJ*)A->data;
1549: Mat a,b,c,d;
1550: PetscTruth flg;
1554: a = matA->A; b = matA->B;
1555: c = matB->A; d = matB->B;
1557: MatEqual(a,c,&flg);
1558: if (flg) {
1559: MatEqual(b,d,&flg);
1560: }
1561: MPI_Allreduce(&flg,flag,1,MPI_INT,MPI_LAND,A->comm);
1562: return(0);
1563: }
1567: PetscErrorCode MatCopy_MPIAIJ(Mat A,Mat B,MatStructure str)
1568: {
1570: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1571: Mat_MPIAIJ *b = (Mat_MPIAIJ *)B->data;
1574: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1575: if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1576: /* because of the column compression in the off-processor part of the matrix a->B,
1577: the number of columns in a->B and b->B may be different, hence we cannot call
1578: the MatCopy() directly on the two parts. If need be, we can provide a more
1579: efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
1580: then copying the submatrices */
1581: MatCopy_Basic(A,B,str);
1582: } else {
1583: MatCopy(a->A,b->A,str);
1584: MatCopy(a->B,b->B,str);
1585: }
1586: return(0);
1587: }
1591: PetscErrorCode MatSetUpPreallocation_MPIAIJ(Mat A)
1592: {
1596: MatMPIAIJSetPreallocation(A,PETSC_DEFAULT,0,PETSC_DEFAULT,0);
1597: return(0);
1598: }
1600: #include petscblaslapack.h
1603: PetscErrorCode MatAXPY_MPIAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1604: {
1606: PetscInt i;
1607: Mat_MPIAIJ *xx = (Mat_MPIAIJ *)X->data,*yy = (Mat_MPIAIJ *)Y->data;
1608: PetscBLASInt bnz,one=1;
1609: Mat_SeqAIJ *x,*y;
1612: if (str == SAME_NONZERO_PATTERN) {
1613: PetscScalar alpha = a;
1614: x = (Mat_SeqAIJ *)xx->A->data;
1615: y = (Mat_SeqAIJ *)yy->A->data;
1616: bnz = (PetscBLASInt)x->nz;
1617: BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1618: x = (Mat_SeqAIJ *)xx->B->data;
1619: y = (Mat_SeqAIJ *)yy->B->data;
1620: bnz = (PetscBLASInt)x->nz;
1621: BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
1622: } else if (str == SUBSET_NONZERO_PATTERN) {
1623: MatAXPY_SeqAIJ(yy->A,a,xx->A,str);
1625: x = (Mat_SeqAIJ *)xx->B->data;
1626: y = (Mat_SeqAIJ *)yy->B->data;
1627: if (y->xtoy && y->XtoY != xx->B) {
1628: PetscFree(y->xtoy);
1629: MatDestroy(y->XtoY);
1630: }
1631: if (!y->xtoy) { /* get xtoy */
1632: MatAXPYGetxtoy_Private(xx->B->rmap.n,x->i,x->j,xx->garray,y->i,y->j,yy->garray,&y->xtoy);
1633: y->XtoY = xx->B;
1634: }
1635: for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
1636: } else {
1637: MatAXPY_Basic(Y,a,X,str);
1638: }
1639: return(0);
1640: }
1642: EXTERN PetscErrorCode MatConjugate_SeqAIJ(Mat);
1646: PetscErrorCode MatConjugate_MPIAIJ(Mat mat)
1647: {
1648: #if defined(PETSC_USE_COMPLEX)
1650: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1653: MatConjugate_SeqAIJ(aij->A);
1654: MatConjugate_SeqAIJ(aij->B);
1655: #else
1657: #endif
1658: return(0);
1659: }
1663: PetscErrorCode MatRealPart_MPIAIJ(Mat A)
1664: {
1665: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1669: MatRealPart(a->A);
1670: MatRealPart(a->B);
1671: return(0);
1672: }
1676: PetscErrorCode MatImaginaryPart_MPIAIJ(Mat A)
1677: {
1678: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
1682: MatImaginaryPart(a->A);
1683: MatImaginaryPart(a->B);
1684: return(0);
1685: }
1687: #ifdef PETSC_HAVE_PBGL
1688: #include <boost/parallel/mpi/bsp_process_group.hpp>
1689: typedef boost::parallel::mpi::bsp_process_group process_group_type;
1691: #include <boost/graph/distributed/adjacency_list.hpp>
1692: #include <boost/parallel/mpi/bsp_process_group.hpp>
1694: #include <boost/graph/distributed/petsc/interface.hpp>
1695: #include <boost/graph/distributed/ilu_0.hpp>
1697: namespace petsc = boost::distributed::petsc;
1698: using namespace std;
1699: typedef double value_type;
1700: typedef boost::graph::distributed::ilu_elimination_state elimination_state;
1701: typedef boost::adjacency_list<boost::listS,
1702: boost::distributedS<process_group_type, boost::vecS>,
1703: boost::bidirectionalS,
1704: // Vertex properties
1705: boost::no_property,
1706: // Edge properties
1707: boost::property<boost::edge_weight_t, value_type,
1708: boost::property<boost::edge_finished_t, elimination_state> > > graph_type;
1710: typedef boost::graph_traits<graph_type>::vertex_descriptor vertex_type;
1711: typedef boost::graph_traits<graph_type>::edge_descriptor edge_type;
1712: typedef boost::property_map<graph_type, boost::edge_weight_t>::type weight_map_type;
1716: /*
1717: This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1718: */
1719: PetscErrorCode MatILUFactorSymbolic_MPIAIJ(Mat A, IS isrow, IS iscol, MatFactorInfo *info, Mat *fact)
1720: {
1721: PetscTruth row_identity, col_identity;
1722: PetscObjectContainer c;
1723: PetscInt m, n, M, N;
1724: PetscErrorCode ierr;
1727: if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels = 0 supported for parallel ilu");
1728: ISIdentity(isrow, &row_identity);
1729: ISIdentity(iscol, &col_identity);
1730: if (!row_identity || !col_identity) {
1731: SETERRQ(PETSC_ERR_ARG_WRONG,"Row and column permutations must be identity for parallel ILU");
1732: }
1734: process_group_type pg;
1735: graph_type* graph_p = new graph_type(petsc::num_global_vertices(A), pg, petsc::matrix_distribution(A, pg));
1736: graph_type& graph = *graph_p;
1737: petsc::read_matrix(A, graph, get(boost::edge_weight, graph));
1739: //write_graphviz("petsc_matrix_as_graph.dot", graph, default_writer(), matrix_graph_writer<graph_type>(graph));
1740: boost::property_map<graph_type, boost::edge_finished_t>::type finished = get(boost::edge_finished, graph);
1741: BGL_FORALL_EDGES(e, graph, graph_type)
1742: put(finished, e, boost::graph::distributed::unseen);
1744: ilu_0(graph, get(boost::edge_weight, graph), get(boost::edge_finished, graph));
1746: /* put together the new matrix */
1747: MatCreate(A->comm, fact);
1748: MatGetLocalSize(A, &m, &n);
1749: MatGetSize(A, &M, &N);
1750: MatSetSizes(*fact, m, n, M, N);
1751: MatSetType(*fact, A->type_name);
1752: MatAssemblyBegin(*fact, MAT_FINAL_ASSEMBLY);
1753: MatAssemblyEnd(*fact, MAT_FINAL_ASSEMBLY);
1754: (*fact)->factor = FACTOR_LU;
1756: PetscObjectContainerCreate(A->comm, &c);
1757: PetscObjectContainerSetPointer(c, graph_p);
1758: PetscObjectCompose((PetscObject) (*fact), "graph", (PetscObject) c);
1759: return(0);
1760: }
1764: PetscErrorCode MatLUFactorNumeric_MPIAIJ(Mat A, MatFactorInfo *info, Mat *B)
1765: {
1767: return(0);
1768: }
1772: /*
1773: This uses the parallel ILU factorization of Peter Gottschling <pgottsch@osl.iu.edu>
1774: */
1775: PetscErrorCode MatSolve_MPIAIJ(Mat A, Vec b, Vec x)
1776: {
1777: graph_type* graph_p;
1778: PetscObjectContainer c;
1779: PetscErrorCode ierr;
1782: PetscObjectQuery((PetscObject) A, "graph", (PetscObject *) &c);
1783: PetscObjectContainerGetPointer(c, (void **) &graph_p);
1784: VecCopy(b, x);
1785: return(0);
1786: }
1787: #endif
1789: /* -------------------------------------------------------------------*/
1790: static struct _MatOps MatOps_Values = {MatSetValues_MPIAIJ,
1791: MatGetRow_MPIAIJ,
1792: MatRestoreRow_MPIAIJ,
1793: MatMult_MPIAIJ,
1794: /* 4*/ MatMultAdd_MPIAIJ,
1795: MatMultTranspose_MPIAIJ,
1796: MatMultTransposeAdd_MPIAIJ,
1797: #ifdef PETSC_HAVE_PBGL
1798: MatSolve_MPIAIJ,
1799: #else
1800: 0,
1801: #endif
1802: 0,
1803: 0,
1804: /*10*/ 0,
1805: 0,
1806: 0,
1807: MatRelax_MPIAIJ,
1808: MatTranspose_MPIAIJ,
1809: /*15*/ MatGetInfo_MPIAIJ,
1810: MatEqual_MPIAIJ,
1811: MatGetDiagonal_MPIAIJ,
1812: MatDiagonalScale_MPIAIJ,
1813: MatNorm_MPIAIJ,
1814: /*20*/ MatAssemblyBegin_MPIAIJ,
1815: MatAssemblyEnd_MPIAIJ,
1816: 0,
1817: MatSetOption_MPIAIJ,
1818: MatZeroEntries_MPIAIJ,
1819: /*25*/ MatZeroRows_MPIAIJ,
1820: 0,
1821: #ifdef PETSC_HAVE_PBGL
1822: MatLUFactorNumeric_MPIAIJ,
1823: #else
1824: 0,
1825: #endif
1826: 0,
1827: 0,
1828: /*30*/ MatSetUpPreallocation_MPIAIJ,
1829: #ifdef PETSC_HAVE_PBGL
1830: MatILUFactorSymbolic_MPIAIJ,
1831: #else
1832: 0,
1833: #endif
1834: 0,
1835: 0,
1836: 0,
1837: /*35*/ MatDuplicate_MPIAIJ,
1838: 0,
1839: 0,
1840: 0,
1841: 0,
1842: /*40*/ MatAXPY_MPIAIJ,
1843: MatGetSubMatrices_MPIAIJ,
1844: MatIncreaseOverlap_MPIAIJ,
1845: MatGetValues_MPIAIJ,
1846: MatCopy_MPIAIJ,
1847: /*45*/ 0,
1848: MatScale_MPIAIJ,
1849: 0,
1850: 0,
1851: 0,
1852: /*50*/ MatSetBlockSize_MPIAIJ,
1853: 0,
1854: 0,
1855: 0,
1856: 0,
1857: /*55*/ MatFDColoringCreate_MPIAIJ,
1858: 0,
1859: MatSetUnfactored_MPIAIJ,
1860: MatPermute_MPIAIJ,
1861: 0,
1862: /*60*/ MatGetSubMatrix_MPIAIJ,
1863: MatDestroy_MPIAIJ,
1864: MatView_MPIAIJ,
1865: 0,
1866: 0,
1867: /*65*/ 0,
1868: 0,
1869: 0,
1870: 0,
1871: 0,
1872: /*70*/ 0,
1873: 0,
1874: MatSetColoring_MPIAIJ,
1875: #if defined(PETSC_HAVE_ADIC)
1876: MatSetValuesAdic_MPIAIJ,
1877: #else
1878: 0,
1879: #endif
1880: MatSetValuesAdifor_MPIAIJ,
1881: /*75*/ 0,
1882: 0,
1883: 0,
1884: 0,
1885: 0,
1886: /*80*/ 0,
1887: 0,
1888: 0,
1889: 0,
1890: /*84*/ MatLoad_MPIAIJ,
1891: 0,
1892: 0,
1893: 0,
1894: 0,
1895: 0,
1896: /*90*/ MatMatMult_MPIAIJ_MPIAIJ,
1897: MatMatMultSymbolic_MPIAIJ_MPIAIJ,
1898: MatMatMultNumeric_MPIAIJ_MPIAIJ,
1899: MatPtAP_Basic,
1900: MatPtAPSymbolic_MPIAIJ,
1901: /*95*/ MatPtAPNumeric_MPIAIJ,
1902: 0,
1903: 0,
1904: 0,
1905: 0,
1906: /*100*/0,
1907: MatPtAPSymbolic_MPIAIJ_MPIAIJ,
1908: MatPtAPNumeric_MPIAIJ_MPIAIJ,
1909: MatConjugate_MPIAIJ,
1910: 0,
1911: /*105*/MatSetValuesRow_MPIAIJ,
1912: MatRealPart_MPIAIJ,
1913: MatImaginaryPart_MPIAIJ};
1915: /* ----------------------------------------------------------------------------------------*/
1920: PetscErrorCode MatStoreValues_MPIAIJ(Mat mat)
1921: {
1922: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1926: MatStoreValues(aij->A);
1927: MatStoreValues(aij->B);
1928: return(0);
1929: }
1935: PetscErrorCode MatRetrieveValues_MPIAIJ(Mat mat)
1936: {
1937: Mat_MPIAIJ *aij = (Mat_MPIAIJ *)mat->data;
1941: MatRetrieveValues(aij->A);
1942: MatRetrieveValues(aij->B);
1943: return(0);
1944: }
1947: #include petscpc.h
1951: PetscErrorCode MatMPIAIJSetPreallocation_MPIAIJ(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
1952: {
1953: Mat_MPIAIJ *b;
1955: PetscInt i;
1958: B->preallocated = PETSC_TRUE;
1959: if (d_nz == PETSC_DEFAULT || d_nz == PETSC_DECIDE) d_nz = 5;
1960: if (o_nz == PETSC_DEFAULT || o_nz == PETSC_DECIDE) o_nz = 2;
1961: if (d_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"d_nz cannot be less than 0: value %D",d_nz);
1962: if (o_nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"o_nz cannot be less than 0: value %D",o_nz);
1964: B->rmap.bs = B->cmap.bs = 1;
1965: PetscMapInitialize(B->comm,&B->rmap);
1966: PetscMapInitialize(B->comm,&B->cmap);
1967: if (d_nnz) {
1968: for (i=0; i<B->rmap.n; i++) {
1969: if (d_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"d_nnz cannot be less than 0: local row %D value %D",i,d_nnz[i]);
1970: }
1971: }
1972: if (o_nnz) {
1973: for (i=0; i<B->rmap.n; i++) {
1974: if (o_nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"o_nnz cannot be less than 0: local row %D value %D",i,o_nnz[i]);
1975: }
1976: }
1977: b = (Mat_MPIAIJ*)B->data;
1979: /* Explicitly create 2 MATSEQAIJ matrices. */
1980: MatCreate(PETSC_COMM_SELF,&b->A);
1981: MatSetSizes(b->A,B->rmap.n,B->cmap.n,B->rmap.n,B->cmap.n);
1982: MatSetType(b->A,MATSEQAIJ);
1983: PetscLogObjectParent(B,b->A);
1984: MatCreate(PETSC_COMM_SELF,&b->B);
1985: MatSetSizes(b->B,B->rmap.n,B->cmap.N,B->rmap.n,B->cmap.N);
1986: MatSetType(b->B,MATSEQAIJ);
1987: PetscLogObjectParent(B,b->B);
1989: MatSeqAIJSetPreallocation(b->A,d_nz,d_nnz);
1990: MatSeqAIJSetPreallocation(b->B,o_nz,o_nnz);
1992: return(0);
1993: }
1998: PetscErrorCode MatDuplicate_MPIAIJ(Mat matin,MatDuplicateOption cpvalues,Mat *newmat)
1999: {
2000: Mat mat;
2001: Mat_MPIAIJ *a,*oldmat = (Mat_MPIAIJ*)matin->data;
2005: *newmat = 0;
2006: MatCreate(matin->comm,&mat);
2007: MatSetSizes(mat,matin->rmap.n,matin->cmap.n,matin->rmap.N,matin->cmap.N);
2008: MatSetType(mat,matin->type_name);
2009: PetscMemcpy(mat->ops,matin->ops,sizeof(struct _MatOps));
2010: a = (Mat_MPIAIJ*)mat->data;
2011:
2012: mat->factor = matin->factor;
2013: mat->rmap.bs = matin->rmap.bs;
2014: mat->assembled = PETSC_TRUE;
2015: mat->insertmode = NOT_SET_VALUES;
2016: mat->preallocated = PETSC_TRUE;
2018: a->size = oldmat->size;
2019: a->rank = oldmat->rank;
2020: a->donotstash = oldmat->donotstash;
2021: a->roworiented = oldmat->roworiented;
2022: a->rowindices = 0;
2023: a->rowvalues = 0;
2024: a->getrowactive = PETSC_FALSE;
2026: PetscMapCopy(mat->comm,&matin->rmap,&mat->rmap);
2027: PetscMapCopy(mat->comm,&matin->cmap,&mat->cmap);
2029: MatStashCreate_Private(matin->comm,1,&mat->stash);
2030: if (oldmat->colmap) {
2031: #if defined (PETSC_USE_CTABLE)
2032: PetscTableCreateCopy(oldmat->colmap,&a->colmap);
2033: #else
2034: PetscMalloc((mat->cmap.N)*sizeof(PetscInt),&a->colmap);
2035: PetscLogObjectMemory(mat,(mat->cmap.N)*sizeof(PetscInt));
2036: PetscMemcpy(a->colmap,oldmat->colmap,(mat->cmap.N)*sizeof(PetscInt));
2037: #endif
2038: } else a->colmap = 0;
2039: if (oldmat->garray) {
2040: PetscInt len;
2041: len = oldmat->B->cmap.n;
2042: PetscMalloc((len+1)*sizeof(PetscInt),&a->garray);
2043: PetscLogObjectMemory(mat,len*sizeof(PetscInt));
2044: if (len) { PetscMemcpy(a->garray,oldmat->garray,len*sizeof(PetscInt)); }
2045: } else a->garray = 0;
2046:
2047: VecDuplicate(oldmat->lvec,&a->lvec);
2048: PetscLogObjectParent(mat,a->lvec);
2049: VecScatterCopy(oldmat->Mvctx,&a->Mvctx);
2050: PetscLogObjectParent(mat,a->Mvctx);
2051: MatDuplicate(oldmat->A,cpvalues,&a->A);
2052: PetscLogObjectParent(mat,a->A);
2053: MatDuplicate(oldmat->B,cpvalues,&a->B);
2054: PetscLogObjectParent(mat,a->B);
2055: PetscFListDuplicate(matin->qlist,&mat->qlist);
2056: *newmat = mat;
2057: return(0);
2058: }
2060: #include petscsys.h
2064: PetscErrorCode MatLoad_MPIAIJ(PetscViewer viewer, MatType type,Mat *newmat)
2065: {
2066: Mat A;
2067: PetscScalar *vals,*svals;
2068: MPI_Comm comm = ((PetscObject)viewer)->comm;
2069: MPI_Status status;
2071: PetscMPIInt rank,size,tag = ((PetscObject)viewer)->tag,maxnz;
2072: PetscInt i,nz,j,rstart,rend,mmax;
2073: PetscInt header[4],*rowlengths = 0,M,N,m,*cols;
2074: PetscInt *ourlens = PETSC_NULL,*procsnz = PETSC_NULL,*offlens = PETSC_NULL,jj,*mycols,*smycols;
2075: PetscInt cend,cstart,n,*rowners;
2076: int fd;
2079: MPI_Comm_size(comm,&size);
2080: MPI_Comm_rank(comm,&rank);
2081: if (!rank) {
2082: PetscViewerBinaryGetDescriptor(viewer,&fd);
2083: PetscBinaryRead(fd,(char *)header,4,PETSC_INT);
2084: if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object");
2085: }
2087: MPI_Bcast(header+1,3,MPIU_INT,0,comm);
2088: M = header[1]; N = header[2];
2089: /* determine ownership of all rows */
2090: m = M/size + ((M % size) > rank);
2091: PetscMalloc((size+1)*sizeof(PetscInt),&rowners);
2092: MPI_Allgather(&m,1,MPIU_INT,rowners+1,1,MPIU_INT,comm);
2094: /* First process needs enough room for process with most rows */
2095: if (!rank) {
2096: mmax = rowners[1];
2097: for (i=2; i<size; i++) {
2098: mmax = PetscMax(mmax,rowners[i]);
2099: }
2100: } else mmax = m;
2102: rowners[0] = 0;
2103: for (i=2; i<=size; i++) {
2104: mmax = PetscMax(mmax,rowners[i]);
2105: rowners[i] += rowners[i-1];
2106: }
2107: rstart = rowners[rank];
2108: rend = rowners[rank+1];
2110: /* distribute row lengths to all processors */
2111: PetscMalloc2(mmax,PetscInt,&ourlens,mmax,PetscInt,&offlens);
2112: if (!rank) {
2113: PetscBinaryRead(fd,ourlens,m,PETSC_INT);
2114: PetscMalloc(m*sizeof(PetscInt),&rowlengths);
2115: PetscMalloc(size*sizeof(PetscInt),&procsnz);
2116: PetscMemzero(procsnz,size*sizeof(PetscInt));
2117: for (j=0; j<m; j++) {
2118: procsnz[0] += ourlens[j];
2119: }
2120: for (i=1; i<size; i++) {
2121: PetscBinaryRead(fd,rowlengths,rowners[i+1]-rowners[i],PETSC_INT);
2122: /* calculate the number of nonzeros on each processor */
2123: for (j=0; j<rowners[i+1]-rowners[i]; j++) {
2124: procsnz[i] += rowlengths[j];
2125: }
2126: MPI_Send(rowlengths,rowners[i+1]-rowners[i],MPIU_INT,i,tag,comm);
2127: }
2128: PetscFree(rowlengths);
2129: } else {
2130: MPI_Recv(ourlens,m,MPIU_INT,0,tag,comm,&status);
2131: }
2133: if (!rank) {
2134: /* determine max buffer needed and allocate it */
2135: maxnz = 0;
2136: for (i=0; i<size; i++) {
2137: maxnz = PetscMax(maxnz,procsnz[i]);
2138: }
2139: PetscMalloc(maxnz*sizeof(PetscInt),&cols);
2141: /* read in my part of the matrix column indices */
2142: nz = procsnz[0];
2143: PetscMalloc(nz*sizeof(PetscInt),&mycols);
2144: PetscBinaryRead(fd,mycols,nz,PETSC_INT);
2146: /* read in every one elses and ship off */
2147: for (i=1; i<size; i++) {
2148: nz = procsnz[i];
2149: PetscBinaryRead(fd,cols,nz,PETSC_INT);
2150: MPI_Send(cols,nz,MPIU_INT,i,tag,comm);
2151: }
2152: PetscFree(cols);
2153: } else {
2154: /* determine buffer space needed for message */
2155: nz = 0;
2156: for (i=0; i<m; i++) {
2157: nz += ourlens[i];
2158: }
2159: PetscMalloc(nz*sizeof(PetscInt),&mycols);
2161: /* receive message of column indices*/
2162: MPI_Recv(mycols,nz,MPIU_INT,0,tag,comm,&status);
2163: MPI_Get_count(&status,MPIU_INT,&maxnz);
2164: if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2165: }
2167: /* determine column ownership if matrix is not square */
2168: if (N != M) {
2169: n = N/size + ((N % size) > rank);
2170: MPI_Scan(&n,&cend,1,MPIU_INT,MPI_SUM,comm);
2171: cstart = cend - n;
2172: } else {
2173: cstart = rstart;
2174: cend = rend;
2175: n = cend - cstart;
2176: }
2178: /* loop over local rows, determining number of off diagonal entries */
2179: PetscMemzero(offlens,m*sizeof(PetscInt));
2180: jj = 0;
2181: for (i=0; i<m; i++) {
2182: for (j=0; j<ourlens[i]; j++) {
2183: if (mycols[jj] < cstart || mycols[jj] >= cend) offlens[i]++;
2184: jj++;
2185: }
2186: }
2188: /* create our matrix */
2189: for (i=0; i<m; i++) {
2190: ourlens[i] -= offlens[i];
2191: }
2192: MatCreate(comm,&A);
2193: MatSetSizes(A,m,n,M,N);
2194: MatSetType(A,type);
2195: MatMPIAIJSetPreallocation(A,0,ourlens,0,offlens);
2197: MatSetOption(A,MAT_COLUMNS_SORTED);
2198: for (i=0; i<m; i++) {
2199: ourlens[i] += offlens[i];
2200: }
2202: if (!rank) {
2203: PetscMalloc((maxnz+1)*sizeof(PetscScalar),&vals);
2205: /* read in my part of the matrix numerical values */
2206: nz = procsnz[0];
2207: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2208:
2209: /* insert into matrix */
2210: jj = rstart;
2211: smycols = mycols;
2212: svals = vals;
2213: for (i=0; i<m; i++) {
2214: MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2215: smycols += ourlens[i];
2216: svals += ourlens[i];
2217: jj++;
2218: }
2220: /* read in other processors and ship out */
2221: for (i=1; i<size; i++) {
2222: nz = procsnz[i];
2223: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
2224: MPI_Send(vals,nz,MPIU_SCALAR,i,A->tag,comm);
2225: }
2226: PetscFree(procsnz);
2227: } else {
2228: /* receive numeric values */
2229: PetscMalloc((nz+1)*sizeof(PetscScalar),&vals);
2231: /* receive message of values*/
2232: MPI_Recv(vals,nz,MPIU_SCALAR,0,A->tag,comm,&status);
2233: MPI_Get_count(&status,MPIU_SCALAR,&maxnz);
2234: if (maxnz != nz) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"something is wrong with file");
2236: /* insert into matrix */
2237: jj = rstart;
2238: smycols = mycols;
2239: svals = vals;
2240: for (i=0; i<m; i++) {
2241: MatSetValues_MPIAIJ(A,1,&jj,ourlens[i],smycols,svals,INSERT_VALUES);
2242: smycols += ourlens[i];
2243: svals += ourlens[i];
2244: jj++;
2245: }
2246: }
2247: PetscFree2(ourlens,offlens);
2248: PetscFree(vals);
2249: PetscFree(mycols);
2250: PetscFree(rowners);
2252: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
2253: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
2254: *newmat = A;
2255: return(0);
2256: }
2260: /*
2261: Not great since it makes two copies of the submatrix, first an SeqAIJ
2262: in local and then by concatenating the local matrices the end result.
2263: Writing it directly would be much like MatGetSubMatrices_MPIAIJ()
2264: */
2265: PetscErrorCode MatGetSubMatrix_MPIAIJ(Mat mat,IS isrow,IS iscol,PetscInt csize,MatReuse call,Mat *newmat)
2266: {
2268: PetscMPIInt rank,size;
2269: PetscInt i,m,n,rstart,row,rend,nz,*cwork,j;
2270: PetscInt *ii,*jj,nlocal,*dlens,*olens,dlen,olen,jend,mglobal;
2271: Mat *local,M,Mreuse;
2272: PetscScalar *vwork,*aa;
2273: MPI_Comm comm = mat->comm;
2274: Mat_SeqAIJ *aij;
2278: MPI_Comm_rank(comm,&rank);
2279: MPI_Comm_size(comm,&size);
2281: if (call == MAT_REUSE_MATRIX) {
2282: PetscObjectQuery((PetscObject)*newmat,"SubMatrix",(PetscObject *)&Mreuse);
2283: if (!Mreuse) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Submatrix passed in was not used before, cannot reuse");
2284: local = &Mreuse;
2285: MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&local);
2286: } else {
2287: MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);
2288: Mreuse = *local;
2289: PetscFree(local);
2290: }
2292: /*
2293: m - number of local rows
2294: n - number of columns (same on all processors)
2295: rstart - first row in new global matrix generated
2296: */
2297: MatGetSize(Mreuse,&m,&n);
2298: if (call == MAT_INITIAL_MATRIX) {
2299: aij = (Mat_SeqAIJ*)(Mreuse)->data;
2300: ii = aij->i;
2301: jj = aij->j;
2303: /*
2304: Determine the number of non-zeros in the diagonal and off-diagonal
2305: portions of the matrix in order to do correct preallocation
2306: */
2308: /* first get start and end of "diagonal" columns */
2309: if (csize == PETSC_DECIDE) {
2310: ISGetSize(isrow,&mglobal);
2311: if (mglobal == n) { /* square matrix */
2312: nlocal = m;
2313: } else {
2314: nlocal = n/size + ((n % size) > rank);
2315: }
2316: } else {
2317: nlocal = csize;
2318: }
2319: MPI_Scan(&nlocal,&rend,1,MPIU_INT,MPI_SUM,comm);
2320: rstart = rend - nlocal;
2321: if (rank == size - 1 && rend != n) {
2322: SETERRQ2(PETSC_ERR_ARG_SIZ,"Local column sizes %D do not add up to total number of columns %D",rend,n);
2323: }
2325: /* next, compute all the lengths */
2326: PetscMalloc((2*m+1)*sizeof(PetscInt),&dlens);
2327: olens = dlens + m;
2328: for (i=0; i<m; i++) {
2329: jend = ii[i+1] - ii[i];
2330: olen = 0;
2331: dlen = 0;
2332: for (j=0; j<jend; j++) {
2333: if (*jj < rstart || *jj >= rend) olen++;
2334: else dlen++;
2335: jj++;
2336: }
2337: olens[i] = olen;
2338: dlens[i] = dlen;
2339: }
2340: MatCreate(comm,&M);
2341: MatSetSizes(M,m,nlocal,PETSC_DECIDE,n);
2342: MatSetType(M,mat->type_name);
2343: MatMPIAIJSetPreallocation(M,0,dlens,0,olens);
2344: PetscFree(dlens);
2345: } else {
2346: PetscInt ml,nl;
2348: M = *newmat;
2349: MatGetLocalSize(M,&ml,&nl);
2350: if (ml != m) SETERRQ(PETSC_ERR_ARG_SIZ,"Previous matrix must be same size/layout as request");
2351: MatZeroEntries(M);
2352: /*
2353: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2354: rather than the slower MatSetValues().
2355: */
2356: M->was_assembled = PETSC_TRUE;
2357: M->assembled = PETSC_FALSE;
2358: }
2359: MatGetOwnershipRange(M,&rstart,&rend);
2360: aij = (Mat_SeqAIJ*)(Mreuse)->data;
2361: ii = aij->i;
2362: jj = aij->j;
2363: aa = aij->a;
2364: for (i=0; i<m; i++) {
2365: row = rstart + i;
2366: nz = ii[i+1] - ii[i];
2367: cwork = jj; jj += nz;
2368: vwork = aa; aa += nz;
2369: MatSetValues_MPIAIJ(M,1,&row,nz,cwork,vwork,INSERT_VALUES);
2370: }
2372: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
2373: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
2374: *newmat = M;
2376: /* save submatrix used in processor for next request */
2377: if (call == MAT_INITIAL_MATRIX) {
2378: PetscObjectCompose((PetscObject)M,"SubMatrix",(PetscObject)Mreuse);
2379: PetscObjectDereference((PetscObject)Mreuse);
2380: }
2382: return(0);
2383: }
2388: PetscErrorCode MatMPIAIJSetPreallocationCSR_MPIAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
2389: {
2390: PetscInt m,cstart, cend,j,nnz,i,d;
2391: PetscInt *d_nnz,*o_nnz,nnz_max = 0,rstart,ii;
2392: const PetscInt *JJ;
2393: PetscScalar *values;
2397: if (Ii[0]) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Ii[0] must be 0 it is %D",Ii[0]);
2399: B->rmap.bs = B->cmap.bs = 1;
2400: PetscMapInitialize(B->comm,&B->rmap);
2401: PetscMapInitialize(B->comm,&B->cmap);
2402: m = B->rmap.n;
2403: cstart = B->cmap.rstart;
2404: cend = B->cmap.rend;
2405: rstart = B->rmap.rstart;
2407: PetscMalloc((2*m+1)*sizeof(PetscInt),&d_nnz);
2408: o_nnz = d_nnz + m;
2410: for (i=0; i<m; i++) {
2411: nnz = Ii[i+1]- Ii[i];
2412: JJ = J + Ii[i];
2413: nnz_max = PetscMax(nnz_max,nnz);
2414: if (nnz < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Local row %D has a negative %D number of columns",i,nnz);
2415: for (j=0; j<nnz; j++) {
2416: if (*JJ >= cstart) break;
2417: JJ++;
2418: }
2419: d = 0;
2420: for (; j<nnz; j++) {
2421: if (*JJ++ >= cend) break;
2422: d++;
2423: }
2424: d_nnz[i] = d;
2425: o_nnz[i] = nnz - d;
2426: }
2427: MatMPIAIJSetPreallocation(B,0,d_nnz,0,o_nnz);
2428: PetscFree(d_nnz);
2430: if (v) values = (PetscScalar*)v;
2431: else {
2432: PetscMalloc((nnz_max+1)*sizeof(PetscScalar),&values);
2433: PetscMemzero(values,nnz_max*sizeof(PetscScalar));
2434: }
2436: MatSetOption(B,MAT_COLUMNS_SORTED);
2437: for (i=0; i<m; i++) {
2438: ii = i + rstart;
2439: nnz = Ii[i+1]- Ii[i];
2440: MatSetValues_MPIAIJ(B,1,&ii,nnz,J+Ii[i],values+(v ? Ii[i] : 0),INSERT_VALUES);
2441: }
2442: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
2443: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
2444: MatSetOption(B,MAT_COLUMNS_UNSORTED);
2446: if (!v) {
2447: PetscFree(values);
2448: }
2449: return(0);
2450: }
2455: /*@
2456: MatMPIAIJSetPreallocationCSR - Allocates memory for a sparse parallel matrix in AIJ format
2457: (the default parallel PETSc format).
2459: Collective on MPI_Comm
2461: Input Parameters:
2462: + B - the matrix
2463: . i - the indices into j for the start of each local row (starts with zero)
2464: . j - the column indices for each local row (starts with zero) these must be sorted for each row
2465: - v - optional values in the matrix
2467: Level: developer
2469: Notes: this actually copies the values from i[], j[], and a[] to put them into PETSc's internal
2470: storage format. Thus changing the values in a[] after this call will not effect the matrix values.
2472: .keywords: matrix, aij, compressed row, sparse, parallel
2474: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatCreateMPIAIJ(), MPIAIJ,
2475: MatCreateSeqAIJWithArrays()
2476: @*/
2477: PetscErrorCode MatMPIAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2478: {
2479: PetscErrorCode ierr,(*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
2482: PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",(void (**)(void))&f);
2483: if (f) {
2484: (*f)(B,i,j,v);
2485: }
2486: return(0);
2487: }
2491: /*@C
2492: MatMPIAIJSetPreallocation - Preallocates memory for a sparse parallel matrix in AIJ format
2493: (the default parallel PETSc format). For good matrix assembly performance
2494: the user should preallocate the matrix storage by setting the parameters
2495: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
2496: performance can be increased by more than a factor of 50.
2498: Collective on MPI_Comm
2500: Input Parameters:
2501: + A - the matrix
2502: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
2503: (same value is used for all local rows)
2504: . d_nnz - array containing the number of nonzeros in the various rows of the
2505: DIAGONAL portion of the local submatrix (possibly different for each row)
2506: or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2507: The size of this array is equal to the number of local rows, i.e 'm'.
2508: You must leave room for the diagonal entry even if it is zero.
2509: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
2510: submatrix (same value is used for all local rows).
2511: - o_nnz - array containing the number of nonzeros in the various rows of the
2512: OFF-DIAGONAL portion of the local submatrix (possibly different for
2513: each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2514: structure. The size of this array is equal to the number
2515: of local rows, i.e 'm'.
2517: If the *_nnz parameter is given then the *_nz parameter is ignored
2519: The AIJ format (also called the Yale sparse matrix format or
2520: compressed row storage (CSR)), is fully compatible with standard Fortran 77
2521: storage. The stored row and column indices begin with zero. See the users manual for details.
2523: The parallel matrix is partitioned such that the first m0 rows belong to
2524: process 0, the next m1 rows belong to process 1, the next m2 rows belong
2525: to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2527: The DIAGONAL portion of the local submatrix of a processor can be defined
2528: as the submatrix which is obtained by extraction the part corresponding
2529: to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2530: first row that belongs to the processor, and r2 is the last row belonging
2531: to the this processor. This is a square mxm matrix. The remaining portion
2532: of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2534: If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2536: Example usage:
2537:
2538: Consider the following 8x8 matrix with 34 non-zero values, that is
2539: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2540: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2541: as follows:
2543: .vb
2544: 1 2 0 | 0 3 0 | 0 4
2545: Proc0 0 5 6 | 7 0 0 | 8 0
2546: 9 0 10 | 11 0 0 | 12 0
2547: -------------------------------------
2548: 13 0 14 | 15 16 17 | 0 0
2549: Proc1 0 18 0 | 19 20 21 | 0 0
2550: 0 0 0 | 22 23 0 | 24 0
2551: -------------------------------------
2552: Proc2 25 26 27 | 0 0 28 | 29 0
2553: 30 0 0 | 31 32 33 | 0 34
2554: .ve
2556: This can be represented as a collection of submatrices as:
2558: .vb
2559: A B C
2560: D E F
2561: G H I
2562: .ve
2564: Where the submatrices A,B,C are owned by proc0, D,E,F are
2565: owned by proc1, G,H,I are owned by proc2.
2567: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2568: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2569: The 'M','N' parameters are 8,8, and have the same values on all procs.
2571: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2572: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2573: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2574: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2575: part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2576: matrix, ans [DF] as another SeqAIJ matrix.
2578: When d_nz, o_nz parameters are specified, d_nz storage elements are
2579: allocated for every row of the local diagonal submatrix, and o_nz
2580: storage locations are allocated for every row of the OFF-DIAGONAL submat.
2581: One way to choose d_nz and o_nz is to use the max nonzerors per local
2582: rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2583: In this case, the values of d_nz,o_nz are:
2584: .vb
2585: proc0 : dnz = 2, o_nz = 2
2586: proc1 : dnz = 3, o_nz = 2
2587: proc2 : dnz = 1, o_nz = 4
2588: .ve
2589: We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2590: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2591: for proc3. i.e we are using 12+15+10=37 storage locations to store
2592: 34 values.
2594: When d_nnz, o_nnz parameters are specified, the storage is specified
2595: for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2596: In the above case the values for d_nnz,o_nnz are:
2597: .vb
2598: proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2599: proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2600: proc2: d_nnz = [1,1] and o_nnz = [4,4]
2601: .ve
2602: Here the space allocated is sum of all the above values i.e 34, and
2603: hence pre-allocation is perfect.
2605: Level: intermediate
2607: .keywords: matrix, aij, compressed row, sparse, parallel
2609: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateMPIAIJ(), MatMPIAIJSetPreallocationCSR(),
2610: MPIAIJ
2611: @*/
2612: PetscErrorCode MatMPIAIJSetPreallocation(Mat B,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[])
2613: {
2614: PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[],PetscInt,const PetscInt[]);
2617: PetscObjectQueryFunction((PetscObject)B,"MatMPIAIJSetPreallocation_C",(void (**)(void))&f);
2618: if (f) {
2619: (*f)(B,d_nz,d_nnz,o_nz,o_nnz);
2620: }
2621: return(0);
2622: }
2626: /*@C
2627: MatCreateMPIAIJWithArrays - creates a MPI AIJ matrix using arrays that contain in standard
2628: CSR format the local rows.
2630: Collective on MPI_Comm
2632: Input Parameters:
2633: + comm - MPI communicator
2634: . m - number of local rows (Cannot be PETSC_DECIDE)
2635: . n - This value should be the same as the local size used in creating the
2636: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2637: calculated if N is given) For square matrices n is almost always m.
2638: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2639: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2640: . i - row indices
2641: . j - column indices
2642: - a - matrix values
2644: Output Parameter:
2645: . mat - the matrix
2646: Level: intermediate
2648: Notes:
2649: The i, j, and a arrays ARE copied by this routine into the internal format used by PETSc;
2650: thus you CANNOT change the matrix entries by changing the values of a[] after you have
2651: called this routine.
2653: The i and j indices are 0 based
2655: .keywords: matrix, aij, compressed row, sparse, parallel
2657: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2658: MPIAIJ, MatCreateMPIAIJ()
2659: @*/
2660: PetscErrorCode MatCreateMPIAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,const PetscInt i[],const PetscInt j[],const PetscScalar a[],Mat *mat)
2661: {
2665: if (i[0]) {
2666: SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
2667: }
2668: if (m < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"local number of rows (m) cannot be PETSC_DECIDE, or negative");
2669: MatCreate(comm,mat);
2670: MatSetType(*mat,MATMPIAIJ);
2671: MatMPIAIJSetPreallocationCSR(*mat,i,j,a);
2672: return(0);
2673: }
2677: /*@C
2678: MatCreateMPIAIJ - Creates a sparse parallel matrix in AIJ format
2679: (the default parallel PETSc format). For good matrix assembly performance
2680: the user should preallocate the matrix storage by setting the parameters
2681: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
2682: performance can be increased by more than a factor of 50.
2684: Collective on MPI_Comm
2686: Input Parameters:
2687: + comm - MPI communicator
2688: . m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
2689: This value should be the same as the local size used in creating the
2690: y vector for the matrix-vector product y = Ax.
2691: . n - This value should be the same as the local size used in creating the
2692: x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
2693: calculated if N is given) For square matrices n is almost always m.
2694: . M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
2695: . N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
2696: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
2697: (same value is used for all local rows)
2698: . d_nnz - array containing the number of nonzeros in the various rows of the
2699: DIAGONAL portion of the local submatrix (possibly different for each row)
2700: or PETSC_NULL, if d_nz is used to specify the nonzero structure.
2701: The size of this array is equal to the number of local rows, i.e 'm'.
2702: You must leave room for the diagonal entry even if it is zero.
2703: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
2704: submatrix (same value is used for all local rows).
2705: - o_nnz - array containing the number of nonzeros in the various rows of the
2706: OFF-DIAGONAL portion of the local submatrix (possibly different for
2707: each row) or PETSC_NULL, if o_nz is used to specify the nonzero
2708: structure. The size of this array is equal to the number
2709: of local rows, i.e 'm'.
2711: Output Parameter:
2712: . A - the matrix
2714: Notes:
2715: If the *_nnz parameter is given then the *_nz parameter is ignored
2717: m,n,M,N parameters specify the size of the matrix, and its partitioning across
2718: processors, while d_nz,d_nnz,o_nz,o_nnz parameters specify the approximate
2719: storage requirements for this matrix.
2721: If PETSC_DECIDE or PETSC_DETERMINE is used for a particular argument on one
2722: processor than it must be used on all processors that share the object for
2723: that argument.
2725: The user MUST specify either the local or global matrix dimensions
2726: (possibly both).
2728: The parallel matrix is partitioned such that the first m0 rows belong to
2729: process 0, the next m1 rows belong to process 1, the next m2 rows belong
2730: to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
2732: The DIAGONAL portion of the local submatrix of a processor can be defined
2733: as the submatrix which is obtained by extraction the part corresponding
2734: to the rows r1-r2 and columns r1-r2 of the global matrix, where r1 is the
2735: first row that belongs to the processor, and r2 is the last row belonging
2736: to the this processor. This is a square mxm matrix. The remaining portion
2737: of the local submatrix (mxN) constitute the OFF-DIAGONAL portion.
2739: If o_nnz, d_nnz are specified, then o_nz, and d_nz are ignored.
2741: When calling this routine with a single process communicator, a matrix of
2742: type SEQAIJ is returned. If a matrix of type MPIAIJ is desired for this
2743: type of communicator, use the construction mechanism:
2744: MatCreate(...,&A); MatSetType(A,MPIAIJ); MatMPIAIJSetPreallocation(A,...);
2746: By default, this format uses inodes (identical nodes) when possible.
2747: We search for consecutive rows with the same nonzero structure, thereby
2748: reusing matrix information to achieve increased efficiency.
2750: Options Database Keys:
2751: + -mat_no_inode - Do not use inodes
2752: . -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2753: - -mat_aij_oneindex - Internally use indexing starting at 1
2754: rather than 0. Note that when calling MatSetValues(),
2755: the user still MUST index entries starting at 0!
2758: Example usage:
2759:
2760: Consider the following 8x8 matrix with 34 non-zero values, that is
2761: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
2762: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
2763: as follows:
2765: .vb
2766: 1 2 0 | 0 3 0 | 0 4
2767: Proc0 0 5 6 | 7 0 0 | 8 0
2768: 9 0 10 | 11 0 0 | 12 0
2769: -------------------------------------
2770: 13 0 14 | 15 16 17 | 0 0
2771: Proc1 0 18 0 | 19 20 21 | 0 0
2772: 0 0 0 | 22 23 0 | 24 0
2773: -------------------------------------
2774: Proc2 25 26 27 | 0 0 28 | 29 0
2775: 30 0 0 | 31 32 33 | 0 34
2776: .ve
2778: This can be represented as a collection of submatrices as:
2780: .vb
2781: A B C
2782: D E F
2783: G H I
2784: .ve
2786: Where the submatrices A,B,C are owned by proc0, D,E,F are
2787: owned by proc1, G,H,I are owned by proc2.
2789: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2790: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
2791: The 'M','N' parameters are 8,8, and have the same values on all procs.
2793: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
2794: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
2795: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
2796: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
2797: part as SeqAIJ matrices. for eg: proc1 will store [E] as a SeqAIJ
2798: matrix, ans [DF] as another SeqAIJ matrix.
2800: When d_nz, o_nz parameters are specified, d_nz storage elements are
2801: allocated for every row of the local diagonal submatrix, and o_nz
2802: storage locations are allocated for every row of the OFF-DIAGONAL submat.
2803: One way to choose d_nz and o_nz is to use the max nonzerors per local
2804: rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
2805: In this case, the values of d_nz,o_nz are:
2806: .vb
2807: proc0 : dnz = 2, o_nz = 2
2808: proc1 : dnz = 3, o_nz = 2
2809: proc2 : dnz = 1, o_nz = 4
2810: .ve
2811: We are allocating m*(d_nz+o_nz) storage locations for every proc. This
2812: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
2813: for proc3. i.e we are using 12+15+10=37 storage locations to store
2814: 34 values.
2816: When d_nnz, o_nnz parameters are specified, the storage is specified
2817: for every row, coresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
2818: In the above case the values for d_nnz,o_nnz are:
2819: .vb
2820: proc0: d_nnz = [2,2,2] and o_nnz = [2,2,2]
2821: proc1: d_nnz = [3,3,2] and o_nnz = [2,1,1]
2822: proc2: d_nnz = [1,1] and o_nnz = [4,4]
2823: .ve
2824: Here the space allocated is sum of all the above values i.e 34, and
2825: hence pre-allocation is perfect.
2827: Level: intermediate
2829: .keywords: matrix, aij, compressed row, sparse, parallel
2831: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatMPIAIJSetPreallocation(), MatMPIAIJSetPreallocationCSR(),
2832: MPIAIJ, MatCreateMPIAIJWithArrays()
2833: @*/
2834: PetscErrorCode MatCreateMPIAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,PetscInt d_nz,const PetscInt d_nnz[],PetscInt o_nz,const PetscInt o_nnz[],Mat *A)
2835: {
2837: PetscMPIInt size;
2840: MatCreate(comm,A);
2841: MatSetSizes(*A,m,n,M,N);
2842: MPI_Comm_size(comm,&size);
2843: if (size > 1) {
2844: MatSetType(*A,MATMPIAIJ);
2845: MatMPIAIJSetPreallocation(*A,d_nz,d_nnz,o_nz,o_nnz);
2846: } else {
2847: MatSetType(*A,MATSEQAIJ);
2848: MatSeqAIJSetPreallocation(*A,d_nz,d_nnz);
2849: }
2850: return(0);
2851: }
2855: PetscErrorCode MatMPIAIJGetSeqAIJ(Mat A,Mat *Ad,Mat *Ao,PetscInt *colmap[])
2856: {
2857: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
2860: *Ad = a->A;
2861: *Ao = a->B;
2862: *colmap = a->garray;
2863: return(0);
2864: }
2868: PetscErrorCode MatSetColoring_MPIAIJ(Mat A,ISColoring coloring)
2869: {
2871: PetscInt i;
2872: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2875: if (coloring->ctype == IS_COLORING_LOCAL) {
2876: ISColoringValue *allcolors,*colors;
2877: ISColoring ocoloring;
2879: /* set coloring for diagonal portion */
2880: MatSetColoring_SeqAIJ(a->A,coloring);
2882: /* set coloring for off-diagonal portion */
2883: ISAllGatherColors(A->comm,coloring->n,coloring->colors,PETSC_NULL,&allcolors);
2884: PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2885: for (i=0; i<a->B->cmap.n; i++) {
2886: colors[i] = allcolors[a->garray[i]];
2887: }
2888: PetscFree(allcolors);
2889: ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2890: MatSetColoring_SeqAIJ(a->B,ocoloring);
2891: ISColoringDestroy(ocoloring);
2892: } else if (coloring->ctype == IS_COLORING_GHOSTED) {
2893: ISColoringValue *colors;
2894: PetscInt *larray;
2895: ISColoring ocoloring;
2897: /* set coloring for diagonal portion */
2898: PetscMalloc((a->A->cmap.n+1)*sizeof(PetscInt),&larray);
2899: for (i=0; i<a->A->cmap.n; i++) {
2900: larray[i] = i + A->cmap.rstart;
2901: }
2902: ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->A->cmap.n,larray,PETSC_NULL,larray);
2903: PetscMalloc((a->A->cmap.n+1)*sizeof(ISColoringValue),&colors);
2904: for (i=0; i<a->A->cmap.n; i++) {
2905: colors[i] = coloring->colors[larray[i]];
2906: }
2907: PetscFree(larray);
2908: ISColoringCreate(PETSC_COMM_SELF,coloring->n,a->A->cmap.n,colors,&ocoloring);
2909: MatSetColoring_SeqAIJ(a->A,ocoloring);
2910: ISColoringDestroy(ocoloring);
2912: /* set coloring for off-diagonal portion */
2913: PetscMalloc((a->B->cmap.n+1)*sizeof(PetscInt),&larray);
2914: ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,a->B->cmap.n,a->garray,PETSC_NULL,larray);
2915: PetscMalloc((a->B->cmap.n+1)*sizeof(ISColoringValue),&colors);
2916: for (i=0; i<a->B->cmap.n; i++) {
2917: colors[i] = coloring->colors[larray[i]];
2918: }
2919: PetscFree(larray);
2920: ISColoringCreate(MPI_COMM_SELF,coloring->n,a->B->cmap.n,colors,&ocoloring);
2921: MatSetColoring_SeqAIJ(a->B,ocoloring);
2922: ISColoringDestroy(ocoloring);
2923: } else {
2924: SETERRQ1(PETSC_ERR_SUP,"No support ISColoringType %d",(int)coloring->ctype);
2925: }
2927: return(0);
2928: }
2930: #if defined(PETSC_HAVE_ADIC)
2933: PetscErrorCode MatSetValuesAdic_MPIAIJ(Mat A,void *advalues)
2934: {
2935: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2939: MatSetValuesAdic_SeqAIJ(a->A,advalues);
2940: MatSetValuesAdic_SeqAIJ(a->B,advalues);
2941: return(0);
2942: }
2943: #endif
2947: PetscErrorCode MatSetValuesAdifor_MPIAIJ(Mat A,PetscInt nl,void *advalues)
2948: {
2949: Mat_MPIAIJ *a = (Mat_MPIAIJ*)A->data;
2953: MatSetValuesAdifor_SeqAIJ(a->A,nl,advalues);
2954: MatSetValuesAdifor_SeqAIJ(a->B,nl,advalues);
2955: return(0);
2956: }
2960: /*@C
2961: MatMerge - Creates a single large PETSc matrix by concatinating sequential
2962: matrices from each processor
2964: Collective on MPI_Comm
2966: Input Parameters:
2967: + comm - the communicators the parallel matrix will live on
2968: . inmat - the input sequential matrices
2969: . n - number of local columns (or PETSC_DECIDE)
2970: - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
2972: Output Parameter:
2973: . outmat - the parallel matrix generated
2975: Level: advanced
2977: Notes: The number of columns of the matrix in EACH processor MUST be the same.
2979: @*/
2980: PetscErrorCode MatMerge(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2981: {
2983: PetscInt m,N,i,rstart,nnz,Ii,*dnz,*onz;
2984: PetscInt *indx;
2985: PetscScalar *values;
2988: MatGetSize(inmat,&m,&N);
2989: if (scall == MAT_INITIAL_MATRIX){
2990: /* count nonzeros in each row, for diagonal and off diagonal portion of matrix */
2991: if (n == PETSC_DECIDE){
2992: PetscSplitOwnership(comm,&n,&N);
2993: }
2994: MPI_Scan(&m, &rstart,1,MPIU_INT,MPI_SUM,comm);
2995: rstart -= m;
2997: MatPreallocateInitialize(comm,m,n,dnz,onz);
2998: for (i=0;i<m;i++) {
2999: MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
3000: MatPreallocateSet(i+rstart,nnz,indx,dnz,onz);
3001: MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,PETSC_NULL);
3002: }
3003: /* This routine will ONLY return MPIAIJ type matrix */
3004: MatCreate(comm,outmat);
3005: MatSetSizes(*outmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3006: MatSetType(*outmat,MATMPIAIJ);
3007: MatMPIAIJSetPreallocation(*outmat,0,dnz,0,onz);
3008: MatPreallocateFinalize(dnz,onz);
3009:
3010: } else if (scall == MAT_REUSE_MATRIX){
3011: MatGetOwnershipRange(*outmat,&rstart,PETSC_NULL);
3012: } else {
3013: SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3014: }
3016: for (i=0;i<m;i++) {
3017: MatGetRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
3018: Ii = i + rstart;
3019: MatSetValues(*outmat,1,&Ii,nnz,indx,values,INSERT_VALUES);
3020: MatRestoreRow_SeqAIJ(inmat,i,&nnz,&indx,&values);
3021: }
3022: MatDestroy(inmat);
3023: MatAssemblyBegin(*outmat,MAT_FINAL_ASSEMBLY);
3024: MatAssemblyEnd(*outmat,MAT_FINAL_ASSEMBLY);
3026: return(0);
3027: }
3031: PetscErrorCode MatFileSplit(Mat A,char *outfile)
3032: {
3033: PetscErrorCode ierr;
3034: PetscMPIInt rank;
3035: PetscInt m,N,i,rstart,nnz;
3036: size_t len;
3037: const PetscInt *indx;
3038: PetscViewer out;
3039: char *name;
3040: Mat B;
3041: const PetscScalar *values;
3044: MatGetLocalSize(A,&m,0);
3045: MatGetSize(A,0,&N);
3046: /* Should this be the type of the diagonal block of A? */
3047: MatCreate(PETSC_COMM_SELF,&B);
3048: MatSetSizes(B,m,N,m,N);
3049: MatSetType(B,MATSEQAIJ);
3050: MatSeqAIJSetPreallocation(B,0,PETSC_NULL);
3051: MatGetOwnershipRange(A,&rstart,0);
3052: for (i=0;i<m;i++) {
3053: MatGetRow(A,i+rstart,&nnz,&indx,&values);
3054: MatSetValues(B,1,&i,nnz,indx,values,INSERT_VALUES);
3055: MatRestoreRow(A,i+rstart,&nnz,&indx,&values);
3056: }
3057: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3058: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
3060: MPI_Comm_rank(A->comm,&rank);
3061: PetscStrlen(outfile,&len);
3062: PetscMalloc((len+5)*sizeof(char),&name);
3063: sprintf(name,"%s.%d",outfile,rank);
3064: PetscViewerBinaryOpen(PETSC_COMM_SELF,name,FILE_MODE_APPEND,&out);
3065: PetscFree(name);
3066: MatView(B,out);
3067: PetscViewerDestroy(out);
3068: MatDestroy(B);
3069: return(0);
3070: }
3072: EXTERN PetscErrorCode MatDestroy_MPIAIJ(Mat);
3075: PetscErrorCode MatDestroy_MPIAIJ_SeqsToMPI(Mat A)
3076: {
3077: PetscErrorCode ierr;
3078: Mat_Merge_SeqsToMPI *merge;
3079: PetscObjectContainer container;
3082: PetscObjectQuery((PetscObject)A,"MatMergeSeqsToMPI",(PetscObject *)&container);
3083: if (container) {
3084: PetscObjectContainerGetPointer(container,(void **)&merge);
3085: PetscFree(merge->id_r);
3086: PetscFree(merge->len_s);
3087: PetscFree(merge->len_r);
3088: PetscFree(merge->bi);
3089: PetscFree(merge->bj);
3090: PetscFree(merge->buf_ri);
3091: PetscFree(merge->buf_rj);
3092: PetscFree(merge->coi);
3093: PetscFree(merge->coj);
3094: PetscFree(merge->owners_co);
3095: PetscFree(merge->rowmap.range);
3096:
3097: PetscObjectContainerDestroy(container);
3098: PetscObjectCompose((PetscObject)A,"MatMergeSeqsToMPI",0);
3099: }
3100: PetscFree(merge);
3102: MatDestroy_MPIAIJ(A);
3103: return(0);
3104: }
3106: #include src/mat/utils/freespace.h
3107: #include petscbt.h
3108: static PetscEvent logkey_seqstompinum = 0;
3111: /*@C
3112: MatMerge_SeqsToMPI - Creates a MPIAIJ matrix by adding sequential
3113: matrices from each processor
3115: Collective on MPI_Comm
3117: Input Parameters:
3118: + comm - the communicators the parallel matrix will live on
3119: . seqmat - the input sequential matrices
3120: . m - number of local rows (or PETSC_DECIDE)
3121: . n - number of local columns (or PETSC_DECIDE)
3122: - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3124: Output Parameter:
3125: . mpimat - the parallel matrix generated
3127: Level: advanced
3129: Notes:
3130: The dimensions of the sequential matrix in each processor MUST be the same.
3131: The input seqmat is included into the container "Mat_Merge_SeqsToMPI", and will be
3132: destroyed when mpimat is destroyed. Call PetscObjectQuery() to access seqmat.
3133: @*/
3134: PetscErrorCode MatMerge_SeqsToMPINumeric(Mat seqmat,Mat mpimat)
3135: {
3136: PetscErrorCode ierr;
3137: MPI_Comm comm=mpimat->comm;
3138: Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data;
3139: PetscMPIInt size,rank,taga,*len_s;
3140: PetscInt N=mpimat->cmap.N,i,j,*owners,*ai=a->i,*aj=a->j;
3141: PetscInt proc,m;
3142: PetscInt **buf_ri,**buf_rj;
3143: PetscInt k,anzi,*bj_i,*bi,*bj,arow,bnzi,nextaj;
3144: PetscInt nrows,**buf_ri_k,**nextrow,**nextai;
3145: MPI_Request *s_waits,*r_waits;
3146: MPI_Status *status;
3147: MatScalar *aa=a->a,**abuf_r,*ba_i;
3148: Mat_Merge_SeqsToMPI *merge;
3149: PetscObjectContainer container;
3150:
3152: if (!logkey_seqstompinum) {
3154: }
3157: MPI_Comm_size(comm,&size);
3158: MPI_Comm_rank(comm,&rank);
3160: PetscObjectQuery((PetscObject)mpimat,"MatMergeSeqsToMPI",(PetscObject *)&container);
3161: if (container) {
3162: PetscObjectContainerGetPointer(container,(void **)&merge);
3163: }
3164: bi = merge->bi;
3165: bj = merge->bj;
3166: buf_ri = merge->buf_ri;
3167: buf_rj = merge->buf_rj;
3169: PetscMalloc(size*sizeof(MPI_Status),&status);
3170: owners = merge->rowmap.range;
3171: len_s = merge->len_s;
3173: /* send and recv matrix values */
3174: /*-----------------------------*/
3175: PetscObjectGetNewTag((PetscObject)mpimat,&taga);
3176: PetscPostIrecvScalar(comm,taga,merge->nrecv,merge->id_r,merge->len_r,&abuf_r,&r_waits);
3178: PetscMalloc((merge->nsend+1)*sizeof(MPI_Request),&s_waits);
3179: for (proc=0,k=0; proc<size; proc++){
3180: if (!len_s[proc]) continue;
3181: i = owners[proc];
3182: MPI_Isend(aa+ai[i],len_s[proc],MPIU_MATSCALAR,proc,taga,comm,s_waits+k);
3183: k++;
3184: }
3186: if (merge->nrecv) {MPI_Waitall(merge->nrecv,r_waits,status);}
3187: if (merge->nsend) {MPI_Waitall(merge->nsend,s_waits,status);}
3188: PetscFree(status);
3190: PetscFree(s_waits);
3191: PetscFree(r_waits);
3193: /* insert mat values of mpimat */
3194: /*----------------------------*/
3195: PetscMalloc(N*sizeof(MatScalar),&ba_i);
3196: PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3197: nextrow = buf_ri_k + merge->nrecv;
3198: nextai = nextrow + merge->nrecv;
3200: for (k=0; k<merge->nrecv; k++){
3201: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3202: nrows = *(buf_ri_k[k]);
3203: nextrow[k] = buf_ri_k[k]+1; /* next row number of k-th recved i-structure */
3204: nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */
3205: }
3207: /* set values of ba */
3208: m = merge->rowmap.n;
3209: for (i=0; i<m; i++) {
3210: arow = owners[rank] + i;
3211: bj_i = bj+bi[i]; /* col indices of the i-th row of mpimat */
3212: bnzi = bi[i+1] - bi[i];
3213: PetscMemzero(ba_i,bnzi*sizeof(MatScalar));
3215: /* add local non-zero vals of this proc's seqmat into ba */
3216: anzi = ai[arow+1] - ai[arow];
3217: aj = a->j + ai[arow];
3218: aa = a->a + ai[arow];
3219: nextaj = 0;
3220: for (j=0; nextaj<anzi; j++){
3221: if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3222: ba_i[j] += aa[nextaj++];
3223: }
3224: }
3226: /* add received vals into ba */
3227: for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3228: /* i-th row */
3229: if (i == *nextrow[k]) {
3230: anzi = *(nextai[k]+1) - *nextai[k];
3231: aj = buf_rj[k] + *(nextai[k]);
3232: aa = abuf_r[k] + *(nextai[k]);
3233: nextaj = 0;
3234: for (j=0; nextaj<anzi; j++){
3235: if (*(bj_i + j) == aj[nextaj]){ /* bcol == acol */
3236: ba_i[j] += aa[nextaj++];
3237: }
3238: }
3239: nextrow[k]++; nextai[k]++;
3240: }
3241: }
3242: MatSetValues(mpimat,1,&arow,bnzi,bj_i,ba_i,INSERT_VALUES);
3243: }
3244: MatAssemblyBegin(mpimat,MAT_FINAL_ASSEMBLY);
3245: MatAssemblyEnd(mpimat,MAT_FINAL_ASSEMBLY);
3247: PetscFree(abuf_r);
3248: PetscFree(ba_i);
3249: PetscFree(buf_ri_k);
3251: return(0);
3252: }
3254: static PetscEvent logkey_seqstompisym = 0;
3257: PetscErrorCode MatMerge_SeqsToMPISymbolic(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,Mat *mpimat)
3258: {
3259: PetscErrorCode ierr;
3260: Mat B_mpi;
3261: Mat_SeqAIJ *a=(Mat_SeqAIJ*)seqmat->data;
3262: PetscMPIInt size,rank,tagi,tagj,*len_s,*len_si,*len_ri;
3263: PetscInt **buf_rj,**buf_ri,**buf_ri_k;
3264: PetscInt M=seqmat->rmap.n,N=seqmat->cmap.n,i,*owners,*ai=a->i,*aj=a->j;
3265: PetscInt len,proc,*dnz,*onz;
3266: PetscInt k,anzi,*bi,*bj,*lnk,nlnk,arow,bnzi,nspacedouble=0;
3267: PetscInt nrows,*buf_s,*buf_si,*buf_si_i,**nextrow,**nextai;
3268: MPI_Request *si_waits,*sj_waits,*ri_waits,*rj_waits;
3269: MPI_Status *status;
3270: PetscFreeSpaceList free_space=PETSC_NULL,current_space=PETSC_NULL;
3271: PetscBT lnkbt;
3272: Mat_Merge_SeqsToMPI *merge;
3273: PetscObjectContainer container;
3276: if (!logkey_seqstompisym) {
3278: }
3281: /* make sure it is a PETSc comm */
3282: PetscCommDuplicate(comm,&comm,PETSC_NULL);
3283: MPI_Comm_size(comm,&size);
3284: MPI_Comm_rank(comm,&rank);
3285:
3286: PetscNew(Mat_Merge_SeqsToMPI,&merge);
3287: PetscMalloc(size*sizeof(MPI_Status),&status);
3289: /* determine row ownership */
3290: /*---------------------------------------------------------*/
3291: merge->rowmap.n = m;
3292: merge->rowmap.N = M;
3293: merge->rowmap.bs = 1;
3294: PetscMapInitialize(comm,&merge->rowmap);
3295: PetscMalloc(size*sizeof(PetscMPIInt),&len_si);
3296: PetscMalloc(size*sizeof(PetscMPIInt),&merge->len_s);
3297:
3298: m = merge->rowmap.n;
3299: M = merge->rowmap.N;
3300: owners = merge->rowmap.range;
3302: /* determine the number of messages to send, their lengths */
3303: /*---------------------------------------------------------*/
3304: len_s = merge->len_s;
3306: len = 0; /* length of buf_si[] */
3307: merge->nsend = 0;
3308: for (proc=0; proc<size; proc++){
3309: len_si[proc] = 0;
3310: if (proc == rank){
3311: len_s[proc] = 0;
3312: } else {
3313: len_si[proc] = owners[proc+1] - owners[proc] + 1;
3314: len_s[proc] = ai[owners[proc+1]] - ai[owners[proc]]; /* num of rows to be sent to [proc] */
3315: }
3316: if (len_s[proc]) {
3317: merge->nsend++;
3318: nrows = 0;
3319: for (i=owners[proc]; i<owners[proc+1]; i++){
3320: if (ai[i+1] > ai[i]) nrows++;
3321: }
3322: len_si[proc] = 2*(nrows+1);
3323: len += len_si[proc];
3324: }
3325: }
3327: /* determine the number and length of messages to receive for ij-structure */
3328: /*-------------------------------------------------------------------------*/
3329: PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&merge->nrecv);
3330: PetscGatherMessageLengths2(comm,merge->nsend,merge->nrecv,len_s,len_si,&merge->id_r,&merge->len_r,&len_ri);
3332: /* post the Irecv of j-structure */
3333: /*-------------------------------*/
3334: PetscCommGetNewTag(comm,&tagj);
3335: PetscPostIrecvInt(comm,tagj,merge->nrecv,merge->id_r,merge->len_r,&buf_rj,&rj_waits);
3337: /* post the Isend of j-structure */
3338: /*--------------------------------*/
3339: PetscMalloc((2*merge->nsend+1)*sizeof(MPI_Request),&si_waits);
3340: sj_waits = si_waits + merge->nsend;
3342: for (proc=0, k=0; proc<size; proc++){
3343: if (!len_s[proc]) continue;
3344: i = owners[proc];
3345: MPI_Isend(aj+ai[i],len_s[proc],MPIU_INT,proc,tagj,comm,sj_waits+k);
3346: k++;
3347: }
3349: /* receives and sends of j-structure are complete */
3350: /*------------------------------------------------*/
3351: if (merge->nrecv) {MPI_Waitall(merge->nrecv,rj_waits,status);}
3352: if (merge->nsend) {MPI_Waitall(merge->nsend,sj_waits,status);}
3353:
3354: /* send and recv i-structure */
3355: /*---------------------------*/
3356: PetscCommGetNewTag(comm,&tagi);
3357: PetscPostIrecvInt(comm,tagi,merge->nrecv,merge->id_r,len_ri,&buf_ri,&ri_waits);
3358:
3359: PetscMalloc((len+1)*sizeof(PetscInt),&buf_s);
3360: buf_si = buf_s; /* points to the beginning of k-th msg to be sent */
3361: for (proc=0,k=0; proc<size; proc++){
3362: if (!len_s[proc]) continue;
3363: /* form outgoing message for i-structure:
3364: buf_si[0]: nrows to be sent
3365: [1:nrows]: row index (global)
3366: [nrows+1:2*nrows+1]: i-structure index
3367: */
3368: /*-------------------------------------------*/
3369: nrows = len_si[proc]/2 - 1;
3370: buf_si_i = buf_si + nrows+1;
3371: buf_si[0] = nrows;
3372: buf_si_i[0] = 0;
3373: nrows = 0;
3374: for (i=owners[proc]; i<owners[proc+1]; i++){
3375: anzi = ai[i+1] - ai[i];
3376: if (anzi) {
3377: buf_si_i[nrows+1] = buf_si_i[nrows] + anzi; /* i-structure */
3378: buf_si[nrows+1] = i-owners[proc]; /* local row index */
3379: nrows++;
3380: }
3381: }
3382: MPI_Isend(buf_si,len_si[proc],MPIU_INT,proc,tagi,comm,si_waits+k);
3383: k++;
3384: buf_si += len_si[proc];
3385: }
3387: if (merge->nrecv) {MPI_Waitall(merge->nrecv,ri_waits,status);}
3388: if (merge->nsend) {MPI_Waitall(merge->nsend,si_waits,status);}
3390: PetscInfo2(seqmat,"nsend: %D, nrecv: %D\n",merge->nsend,merge->nrecv);
3391: for (i=0; i<merge->nrecv; i++){
3392: PetscInfo3(seqmat,"recv len_ri=%D, len_rj=%D from [%D]\n",len_ri[i],merge->len_r[i],merge->id_r[i]);
3393: }
3395: PetscFree(len_si);
3396: PetscFree(len_ri);
3397: PetscFree(rj_waits);
3398: PetscFree(si_waits);
3399: PetscFree(ri_waits);
3400: PetscFree(buf_s);
3401: PetscFree(status);
3403: /* compute a local seq matrix in each processor */
3404: /*----------------------------------------------*/
3405: /* allocate bi array and free space for accumulating nonzero column info */
3406: PetscMalloc((m+1)*sizeof(PetscInt),&bi);
3407: bi[0] = 0;
3409: /* create and initialize a linked list */
3410: nlnk = N+1;
3411: PetscLLCreate(N,N,nlnk,lnk,lnkbt);
3412:
3413: /* initial FreeSpace size is 2*(num of local nnz(seqmat)) */
3414: len = 0;
3415: len = ai[owners[rank+1]] - ai[owners[rank]];
3416: PetscFreeSpaceGet((PetscInt)(2*len+1),&free_space);
3417: current_space = free_space;
3419: /* determine symbolic info for each local row */
3420: PetscMalloc((3*merge->nrecv+1)*sizeof(PetscInt**),&buf_ri_k);
3421: nextrow = buf_ri_k + merge->nrecv;
3422: nextai = nextrow + merge->nrecv;
3423: for (k=0; k<merge->nrecv; k++){
3424: buf_ri_k[k] = buf_ri[k]; /* beginning of k-th recved i-structure */
3425: nrows = *buf_ri_k[k];
3426: nextrow[k] = buf_ri_k[k] + 1; /* next row number of k-th recved i-structure */
3427: nextai[k] = buf_ri_k[k] + (nrows + 1);/* poins to the next i-structure of k-th recved i-structure */
3428: }
3430: MatPreallocateInitialize(comm,m,n,dnz,onz);
3431: len = 0;
3432: for (i=0;i<m;i++) {
3433: bnzi = 0;
3434: /* add local non-zero cols of this proc's seqmat into lnk */
3435: arow = owners[rank] + i;
3436: anzi = ai[arow+1] - ai[arow];
3437: aj = a->j + ai[arow];
3438: PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3439: bnzi += nlnk;
3440: /* add received col data into lnk */
3441: for (k=0; k<merge->nrecv; k++){ /* k-th received message */
3442: if (i == *nextrow[k]) { /* i-th row */
3443: anzi = *(nextai[k]+1) - *nextai[k];
3444: aj = buf_rj[k] + *nextai[k];
3445: PetscLLAdd(anzi,aj,N,nlnk,lnk,lnkbt);
3446: bnzi += nlnk;
3447: nextrow[k]++; nextai[k]++;
3448: }
3449: }
3450: if (len < bnzi) len = bnzi; /* =max(bnzi) */
3452: /* if free space is not available, make more free space */
3453: if (current_space->local_remaining<bnzi) {
3454: PetscFreeSpaceGet(current_space->total_array_size,¤t_space);
3455: nspacedouble++;
3456: }
3457: /* copy data into free space, then initialize lnk */
3458: PetscLLClean(N,N,bnzi,lnk,current_space->array,lnkbt);
3459: MatPreallocateSet(i+owners[rank],bnzi,current_space->array,dnz,onz);
3461: current_space->array += bnzi;
3462: current_space->local_used += bnzi;
3463: current_space->local_remaining -= bnzi;
3464:
3465: bi[i+1] = bi[i] + bnzi;
3466: }
3467:
3468: PetscFree(buf_ri_k);
3470: PetscMalloc((bi[m]+1)*sizeof(PetscInt),&bj);
3471: PetscFreeSpaceContiguous(&free_space,bj);
3472: PetscLLDestroy(lnk,lnkbt);
3474: /* create symbolic parallel matrix B_mpi */
3475: /*---------------------------------------*/
3476: MatCreate(comm,&B_mpi);
3477: if (n==PETSC_DECIDE) {
3478: MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,N);
3479: } else {
3480: MatSetSizes(B_mpi,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
3481: }
3482: MatSetType(B_mpi,MATMPIAIJ);
3483: MatMPIAIJSetPreallocation(B_mpi,0,dnz,0,onz);
3484: MatPreallocateFinalize(dnz,onz);
3486: /* B_mpi is not ready for use - assembly will be done by MatMerge_SeqsToMPINumeric() */
3487: B_mpi->assembled = PETSC_FALSE;
3488: B_mpi->ops->destroy = MatDestroy_MPIAIJ_SeqsToMPI;
3489: merge->bi = bi;
3490: merge->bj = bj;
3491: merge->buf_ri = buf_ri;
3492: merge->buf_rj = buf_rj;
3493: merge->coi = PETSC_NULL;
3494: merge->coj = PETSC_NULL;
3495: merge->owners_co = PETSC_NULL;
3497: /* attach the supporting struct to B_mpi for reuse */
3498: PetscObjectContainerCreate(PETSC_COMM_SELF,&container);
3499: PetscObjectContainerSetPointer(container,merge);
3500: PetscObjectCompose((PetscObject)B_mpi,"MatMergeSeqsToMPI",(PetscObject)container);
3501: *mpimat = B_mpi;
3503: PetscCommDestroy(&comm);
3505: return(0);
3506: }
3508: static PetscEvent logkey_seqstompi = 0;
3511: PetscErrorCode MatMerge_SeqsToMPI(MPI_Comm comm,Mat seqmat,PetscInt m,PetscInt n,MatReuse scall,Mat *mpimat)
3512: {
3513: PetscErrorCode ierr;
3516: if (!logkey_seqstompi) {
3518: }
3520: if (scall == MAT_INITIAL_MATRIX){
3521: MatMerge_SeqsToMPISymbolic(comm,seqmat,m,n,mpimat);
3522: }
3523: MatMerge_SeqsToMPINumeric(seqmat,*mpimat);
3525: return(0);
3526: }
3527: static PetscEvent logkey_getlocalmat = 0;
3530: /*@C
3531: MatGetLocalMat - Creates a SeqAIJ matrix by taking all its local rows
3533: Not Collective
3535: Input Parameters:
3536: + A - the matrix
3537: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3539: Output Parameter:
3540: . A_loc - the local sequential matrix generated
3542: Level: developer
3544: @*/
3545: PetscErrorCode MatGetLocalMat(Mat A,MatReuse scall,Mat *A_loc)
3546: {
3547: PetscErrorCode ierr;
3548: Mat_MPIAIJ *mpimat=(Mat_MPIAIJ*)A->data;
3549: Mat_SeqAIJ *mat,*a=(Mat_SeqAIJ*)(mpimat->A)->data,*b=(Mat_SeqAIJ*)(mpimat->B)->data;
3550: PetscInt *ai=a->i,*aj=a->j,*bi=b->i,*bj=b->j,*cmap=mpimat->garray;
3551: PetscScalar *aa=a->a,*ba=b->a,*ca;
3552: PetscInt am=A->rmap.n,i,j,k,cstart=A->cmap.rstart;
3553: PetscInt *ci,*cj,col,ncols_d,ncols_o,jo;
3556: if (!logkey_getlocalmat) {
3558: }
3560: if (scall == MAT_INITIAL_MATRIX){
3561: PetscMalloc((1+am)*sizeof(PetscInt),&ci);
3562: ci[0] = 0;
3563: for (i=0; i<am; i++){
3564: ci[i+1] = ci[i] + (ai[i+1] - ai[i]) + (bi[i+1] - bi[i]);
3565: }
3566: PetscMalloc((1+ci[am])*sizeof(PetscInt),&cj);
3567: PetscMalloc((1+ci[am])*sizeof(PetscScalar),&ca);
3568: k = 0;
3569: for (i=0; i<am; i++) {
3570: ncols_o = bi[i+1] - bi[i];
3571: ncols_d = ai[i+1] - ai[i];
3572: /* off-diagonal portion of A */
3573: for (jo=0; jo<ncols_o; jo++) {
3574: col = cmap[*bj];
3575: if (col >= cstart) break;
3576: cj[k] = col; bj++;
3577: ca[k++] = *ba++;
3578: }
3579: /* diagonal portion of A */
3580: for (j=0; j<ncols_d; j++) {
3581: cj[k] = cstart + *aj++;
3582: ca[k++] = *aa++;
3583: }
3584: /* off-diagonal portion of A */
3585: for (j=jo; j<ncols_o; j++) {
3586: cj[k] = cmap[*bj++];
3587: ca[k++] = *ba++;
3588: }
3589: }
3590: /* put together the new matrix */
3591: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,am,A->cmap.N,ci,cj,ca,A_loc);
3592: /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3593: /* Since these are PETSc arrays, change flags to free them as necessary. */
3594: mat = (Mat_SeqAIJ*)(*A_loc)->data;
3595: mat->free_a = PETSC_TRUE;
3596: mat->free_ij = PETSC_TRUE;
3597: mat->nonew = 0;
3598: } else if (scall == MAT_REUSE_MATRIX){
3599: mat=(Mat_SeqAIJ*)(*A_loc)->data;
3600: ci = mat->i; cj = mat->j; ca = mat->a;
3601: for (i=0; i<am; i++) {
3602: /* off-diagonal portion of A */
3603: ncols_o = bi[i+1] - bi[i];
3604: for (jo=0; jo<ncols_o; jo++) {
3605: col = cmap[*bj];
3606: if (col >= cstart) break;
3607: *ca++ = *ba++; bj++;
3608: }
3609: /* diagonal portion of A */
3610: ncols_d = ai[i+1] - ai[i];
3611: for (j=0; j<ncols_d; j++) *ca++ = *aa++;
3612: /* off-diagonal portion of A */
3613: for (j=jo; j<ncols_o; j++) {
3614: *ca++ = *ba++; bj++;
3615: }
3616: }
3617: } else {
3618: SETERRQ1(PETSC_ERR_ARG_WRONG,"Invalid MatReuse %d",(int)scall);
3619: }
3622: return(0);
3623: }
3625: static PetscEvent logkey_getlocalmatcondensed = 0;
3628: /*@C
3629: MatGetLocalMatCondensed - Creates a SeqAIJ matrix by taking all its local rows and NON-ZERO columns
3631: Not Collective
3633: Input Parameters:
3634: + A - the matrix
3635: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3636: - row, col - index sets of rows and columns to extract (or PETSC_NULL)
3638: Output Parameter:
3639: . A_loc - the local sequential matrix generated
3641: Level: developer
3643: @*/
3644: PetscErrorCode MatGetLocalMatCondensed(Mat A,MatReuse scall,IS *row,IS *col,Mat *A_loc)
3645: {
3646: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
3647: PetscErrorCode ierr;
3648: PetscInt i,start,end,ncols,nzA,nzB,*cmap,imark,*idx;
3649: IS isrowa,iscola;
3650: Mat *aloc;
3653: if (!logkey_getlocalmatcondensed) {
3655: }
3657: if (!row){
3658: start = A->rmap.rstart; end = A->rmap.rend;
3659: ISCreateStride(PETSC_COMM_SELF,end-start,start,1,&isrowa);
3660: } else {
3661: isrowa = *row;
3662: }
3663: if (!col){
3664: start = A->cmap.rstart;
3665: cmap = a->garray;
3666: nzA = a->A->cmap.n;
3667: nzB = a->B->cmap.n;
3668: PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3669: ncols = 0;
3670: for (i=0; i<nzB; i++) {
3671: if (cmap[i] < start) idx[ncols++] = cmap[i];
3672: else break;
3673: }
3674: imark = i;
3675: for (i=0; i<nzA; i++) idx[ncols++] = start + i;
3676: for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i];
3677: ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&iscola);
3678: PetscFree(idx);
3679: } else {
3680: iscola = *col;
3681: }
3682: if (scall != MAT_INITIAL_MATRIX){
3683: PetscMalloc(sizeof(Mat),&aloc);
3684: aloc[0] = *A_loc;
3685: }
3686: MatGetSubMatrices(A,1,&isrowa,&iscola,scall,&aloc);
3687: *A_loc = aloc[0];
3688: PetscFree(aloc);
3689: if (!row){
3690: ISDestroy(isrowa);
3691: }
3692: if (!col){
3693: ISDestroy(iscola);
3694: }
3696: return(0);
3697: }
3699: static PetscEvent logkey_GetBrowsOfAcols = 0;
3702: /*@C
3703: MatGetBrowsOfAcols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns of local A
3705: Collective on Mat
3707: Input Parameters:
3708: + A,B - the matrices in mpiaij format
3709: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3710: - rowb, colb - index sets of rows and columns of B to extract (or PETSC_NULL)
3712: Output Parameter:
3713: + rowb, colb - index sets of rows and columns of B to extract
3714: . brstart - row index of B_seq from which next B->rmap.n rows are taken from B's local rows
3715: - B_seq - the sequential matrix generated
3717: Level: developer
3719: @*/
3720: PetscErrorCode MatGetBrowsOfAcols(Mat A,Mat B,MatReuse scall,IS *rowb,IS *colb,PetscInt *brstart,Mat *B_seq)
3721: {
3722: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
3723: PetscErrorCode ierr;
3724: PetscInt *idx,i,start,ncols,nzA,nzB,*cmap,imark;
3725: IS isrowb,iscolb;
3726: Mat *bseq;
3727:
3729: if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3730: SETERRQ4(PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%D, %D) != (%D,%D)",A->cmap.rstart,A->cmap.rend,B->rmap.rstart,B->rmap.rend);
3731: }
3732: if (!logkey_GetBrowsOfAcols) {
3734: }
3736:
3737: if (scall == MAT_INITIAL_MATRIX){
3738: start = A->cmap.rstart;
3739: cmap = a->garray;
3740: nzA = a->A->cmap.n;
3741: nzB = a->B->cmap.n;
3742: PetscMalloc((nzA+nzB)*sizeof(PetscInt), &idx);
3743: ncols = 0;
3744: for (i=0; i<nzB; i++) { /* row < local row index */
3745: if (cmap[i] < start) idx[ncols++] = cmap[i];
3746: else break;
3747: }
3748: imark = i;
3749: for (i=0; i<nzA; i++) idx[ncols++] = start + i; /* local rows */
3750: for (i=imark; i<nzB; i++) idx[ncols++] = cmap[i]; /* row > local row index */
3751: ISCreateGeneral(PETSC_COMM_SELF,ncols,idx,&isrowb);
3752: PetscFree(idx);
3753: *brstart = imark;
3754: ISCreateStride(PETSC_COMM_SELF,B->cmap.N,0,1,&iscolb);
3755: } else {
3756: if (!rowb || !colb) SETERRQ(PETSC_ERR_SUP,"IS rowb and colb must be provided for MAT_REUSE_MATRIX");
3757: isrowb = *rowb; iscolb = *colb;
3758: PetscMalloc(sizeof(Mat),&bseq);
3759: bseq[0] = *B_seq;
3760: }
3761: MatGetSubMatrices(B,1,&isrowb,&iscolb,scall,&bseq);
3762: *B_seq = bseq[0];
3763: PetscFree(bseq);
3764: if (!rowb){
3765: ISDestroy(isrowb);
3766: } else {
3767: *rowb = isrowb;
3768: }
3769: if (!colb){
3770: ISDestroy(iscolb);
3771: } else {
3772: *colb = iscolb;
3773: }
3775: return(0);
3776: }
3778: static PetscEvent logkey_GetBrowsOfAocols = 0;
3781: /*@C
3782: MatGetBrowsOfAoCols - Creates a SeqAIJ matrix by taking rows of B that equal to nonzero columns
3783: of the OFF-DIAGONAL portion of local A
3785: Collective on Mat
3787: Input Parameters:
3788: + A,B - the matrices in mpiaij format
3789: . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3790: . startsj - starting point in B's sending and receiving j-arrays, saved for MAT_REUSE (or PETSC_NULL)
3791: - bufa_ptr - array for sending matrix values, saved for MAT_REUSE (or PETSC_NULL)
3793: Output Parameter:
3794: + B_oth - the sequential matrix generated
3796: Level: developer
3798: @*/
3799: PetscErrorCode MatGetBrowsOfAoCols(Mat A,Mat B,MatReuse scall,PetscInt **startsj,PetscScalar **bufa_ptr,Mat *B_oth)
3800: {
3801: VecScatter_MPI_General *gen_to,*gen_from;
3802: PetscErrorCode ierr;
3803: Mat_MPIAIJ *a=(Mat_MPIAIJ*)A->data;
3804: Mat_SeqAIJ *b_oth;
3805: VecScatter ctx=a->Mvctx;
3806: MPI_Comm comm=ctx->comm;
3807: PetscMPIInt *rprocs,*sprocs,tag=ctx->tag,rank;
3808: PetscInt *rowlen,*bufj,*bufJ,ncols,aBn=a->B->cmap.n,row,*b_othi,*b_othj;
3809: PetscScalar *rvalues,*svalues,*b_otha,*bufa,*bufA;
3810: PetscInt i,k,l,nrecvs,nsends,nrows,*srow,*rstarts,*rstartsj = 0,*sstarts,*sstartsj,len;
3811: MPI_Request *rwaits = PETSC_NULL,*swaits = PETSC_NULL;
3812: MPI_Status *sstatus,rstatus;
3813: PetscInt *cols;
3814: PetscScalar *vals;
3815: PetscMPIInt j;
3816:
3818: if (A->cmap.rstart != B->rmap.rstart || A->cmap.rend != B->rmap.rend){
3819: SETERRQ4(PETSC_ERR_ARG_SIZ,"Matrix local dimensions are incompatible, (%d, %d) != (%d,%d)",A->cmap.rstart,A->cmap.rend,B->rmap.rstart,B->rmap.rend);
3820: }
3821: if (!logkey_GetBrowsOfAocols) {
3823: }
3825: MPI_Comm_rank(comm,&rank);
3827: gen_to = (VecScatter_MPI_General*)ctx->todata;
3828: gen_from = (VecScatter_MPI_General*)ctx->fromdata;
3829: rvalues = gen_from->values; /* holds the length of sending row */
3830: svalues = gen_to->values; /* holds the length of receiving row */
3831: nrecvs = gen_from->n;
3832: nsends = gen_to->n;
3834: PetscMalloc2(nrecvs,MPI_Request,&rwaits,nsends,MPI_Request,&swaits);
3835: srow = gen_to->indices; /* local row index to be sent */
3836: rstarts = gen_from->starts;
3837: sstarts = gen_to->starts;
3838: rprocs = gen_from->procs;
3839: sprocs = gen_to->procs;
3840: sstatus = gen_to->sstatus;
3842: if (!startsj || !bufa_ptr) scall = MAT_INITIAL_MATRIX;
3843: if (scall == MAT_INITIAL_MATRIX){
3844: /* i-array */
3845: /*---------*/
3846: /* post receives */
3847: for (i=0; i<nrecvs; i++){
3848: rowlen = (PetscInt*)rvalues + rstarts[i];
3849: nrows = rstarts[i+1]-rstarts[i];
3850: MPI_Irecv(rowlen,nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3851: }
3853: /* pack the outgoing message */
3854: PetscMalloc((nsends+nrecvs+3)*sizeof(PetscInt),&sstartsj);
3855: rstartsj = sstartsj + nsends +1;
3856: sstartsj[0] = 0; rstartsj[0] = 0;
3857: len = 0; /* total length of j or a array to be sent */
3858: k = 0;
3859: for (i=0; i<nsends; i++){
3860: rowlen = (PetscInt*)svalues + sstarts[i];
3861: nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3862: for (j=0; j<nrows; j++) {
3863: row = srow[k] + B->rmap.range[rank]; /* global row idx */
3864: MatGetRow_MPIAIJ(B,row,&rowlen[j],PETSC_NULL,PETSC_NULL); /* rowlength */
3865: len += rowlen[j];
3866: MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,PETSC_NULL);
3867: k++;
3868: }
3869: MPI_Isend(rowlen,nrows,MPIU_INT,sprocs[i],tag,comm,swaits+i);
3870: sstartsj[i+1] = len; /* starting point of (i+1)-th outgoing msg in bufj and bufa */
3871: }
3872: /* recvs and sends of i-array are completed */
3873: i = nrecvs;
3874: while (i--) {
3875: MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3876: }
3877: if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3878: /* allocate buffers for sending j and a arrays */
3879: PetscMalloc((len+1)*sizeof(PetscInt),&bufj);
3880: PetscMalloc((len+1)*sizeof(PetscScalar),&bufa);
3882: /* create i-array of B_oth */
3883: PetscMalloc((aBn+2)*sizeof(PetscInt),&b_othi);
3884: b_othi[0] = 0;
3885: len = 0; /* total length of j or a array to be received */
3886: k = 0;
3887: for (i=0; i<nrecvs; i++){
3888: rowlen = (PetscInt*)rvalues + rstarts[i];
3889: nrows = rstarts[i+1]-rstarts[i];
3890: for (j=0; j<nrows; j++) {
3891: b_othi[k+1] = b_othi[k] + rowlen[j];
3892: len += rowlen[j]; k++;
3893: }
3894: rstartsj[i+1] = len; /* starting point of (i+1)-th incoming msg in bufj and bufa */
3895: }
3897: /* allocate space for j and a arrrays of B_oth */
3898: PetscMalloc((b_othi[aBn]+1)*sizeof(PetscInt),&b_othj);
3899: PetscMalloc((b_othi[aBn]+1)*sizeof(PetscScalar),&b_otha);
3901: /* j-array */
3902: /*---------*/
3903: /* post receives of j-array */
3904: for (i=0; i<nrecvs; i++){
3905: nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3906: MPI_Irecv(b_othj+rstartsj[i],nrows,MPIU_INT,rprocs[i],tag,comm,rwaits+i);
3907: }
3908: k = 0;
3909: for (i=0; i<nsends; i++){
3910: nrows = sstarts[i+1]-sstarts[i]; /* num of rows */
3911: bufJ = bufj+sstartsj[i];
3912: for (j=0; j<nrows; j++) {
3913: row = srow[k++] + B->rmap.range[rank]; /* global row idx */
3914: MatGetRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3915: for (l=0; l<ncols; l++){
3916: *bufJ++ = cols[l];
3917: }
3918: MatRestoreRow_MPIAIJ(B,row,&ncols,&cols,PETSC_NULL);
3919: }
3920: MPI_Isend(bufj+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_INT,sprocs[i],tag,comm,swaits+i);
3921: }
3923: /* recvs and sends of j-array are completed */
3924: i = nrecvs;
3925: while (i--) {
3926: MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3927: }
3928: if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3929: } else if (scall == MAT_REUSE_MATRIX){
3930: sstartsj = *startsj;
3931: rstartsj = sstartsj + nsends +1;
3932: bufa = *bufa_ptr;
3933: b_oth = (Mat_SeqAIJ*)(*B_oth)->data;
3934: b_otha = b_oth->a;
3935: } else {
3936: SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Matrix P does not posses an object container");
3937: }
3939: /* a-array */
3940: /*---------*/
3941: /* post receives of a-array */
3942: for (i=0; i<nrecvs; i++){
3943: nrows = rstartsj[i+1]-rstartsj[i]; /* length of the msg received */
3944: MPI_Irecv(b_otha+rstartsj[i],nrows,MPIU_SCALAR,rprocs[i],tag,comm,rwaits+i);
3945: }
3946: k = 0;
3947: for (i=0; i<nsends; i++){
3948: nrows = sstarts[i+1]-sstarts[i];
3949: bufA = bufa+sstartsj[i];
3950: for (j=0; j<nrows; j++) {
3951: row = srow[k++] + B->rmap.range[rank]; /* global row idx */
3952: MatGetRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);
3953: for (l=0; l<ncols; l++){
3954: *bufA++ = vals[l];
3955: }
3956: MatRestoreRow_MPIAIJ(B,row,&ncols,PETSC_NULL,&vals);
3958: }
3959: MPI_Isend(bufa+sstartsj[i],sstartsj[i+1]-sstartsj[i],MPIU_SCALAR,sprocs[i],tag,comm,swaits+i);
3960: }
3961: /* recvs and sends of a-array are completed */
3962: i = nrecvs;
3963: while (i--) {
3964: MPI_Waitany(nrecvs,rwaits,&j,&rstatus);
3965: }
3966: if (nsends) {MPI_Waitall(nsends,swaits,sstatus);}
3967: PetscFree2(rwaits,swaits);
3969: if (scall == MAT_INITIAL_MATRIX){
3970: /* put together the new matrix */
3971: MatCreateSeqAIJWithArrays(PETSC_COMM_SELF,aBn,B->cmap.N,b_othi,b_othj,b_otha,B_oth);
3973: /* MatCreateSeqAIJWithArrays flags matrix so PETSc doesn't free the user's arrays. */
3974: /* Since these are PETSc arrays, change flags to free them as necessary. */
3975: b_oth = (Mat_SeqAIJ *)(*B_oth)->data;
3976: b_oth->free_a = PETSC_TRUE;
3977: b_oth->free_ij = PETSC_TRUE;
3978: b_oth->nonew = 0;
3980: PetscFree(bufj);
3981: if (!startsj || !bufa_ptr){
3982: PetscFree(sstartsj);
3983: PetscFree(bufa_ptr);
3984: } else {
3985: *startsj = sstartsj;
3986: *bufa_ptr = bufa;
3987: }
3988: }
3990:
3991: return(0);
3992: }
3996: /*@C
3997: MatGetCommunicationStructs - Provides access to the communication structures used in matrix-vector multiplication.
3999: Not Collective
4001: Input Parameters:
4002: . A - The matrix in mpiaij format
4004: Output Parameter:
4005: + lvec - The local vector holding off-process values from the argument to a matrix-vector product
4006: . colmap - A map from global column index to local index into lvec
4007: - multScatter - A scatter from the argument of a matrix-vector product to lvec
4009: Level: developer
4011: @*/
4012: #if defined (PETSC_USE_CTABLE)
4013: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscTable *colmap, VecScatter *multScatter)
4014: #else
4015: PetscErrorCode MatGetCommunicationStructs(Mat A, Vec *lvec, PetscInt *colmap[], VecScatter *multScatter)
4016: #endif
4017: {
4018: Mat_MPIAIJ *a;
4025: a = (Mat_MPIAIJ *) A->data;
4026: if (lvec) *lvec = a->lvec;
4027: if (colmap) *colmap = a->colmap;
4028: if (multScatter) *multScatter = a->Mvctx;
4029: return(0);
4030: }
4037: /*MC
4038: MATMPIAIJ - MATMPIAIJ = "mpiaij" - A matrix type to be used for parallel sparse matrices.
4040: Options Database Keys:
4041: . -mat_type mpiaij - sets the matrix type to "mpiaij" during a call to MatSetFromOptions()
4043: Level: beginner
4045: .seealso: MatCreateMPIAIJ
4046: M*/
4051: PetscErrorCode MatCreate_MPIAIJ(Mat B)
4052: {
4053: Mat_MPIAIJ *b;
4055: PetscMPIInt size;
4058: MPI_Comm_size(B->comm,&size);
4060: PetscNew(Mat_MPIAIJ,&b);
4061: B->data = (void*)b;
4062: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
4063: B->factor = 0;
4064: B->rmap.bs = 1;
4065: B->assembled = PETSC_FALSE;
4066: B->mapping = 0;
4068: B->insertmode = NOT_SET_VALUES;
4069: b->size = size;
4070: MPI_Comm_rank(B->comm,&b->rank);
4072: /* build cache for off array entries formed */
4073: MatStashCreate_Private(B->comm,1,&B->stash);
4074: b->donotstash = PETSC_FALSE;
4075: b->colmap = 0;
4076: b->garray = 0;
4077: b->roworiented = PETSC_TRUE;
4079: /* stuff used for matrix vector multiply */
4080: b->lvec = PETSC_NULL;
4081: b->Mvctx = PETSC_NULL;
4083: /* stuff for MatGetRow() */
4084: b->rowindices = 0;
4085: b->rowvalues = 0;
4086: b->getrowactive = PETSC_FALSE;
4089: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
4090: "MatStoreValues_MPIAIJ",
4091: MatStoreValues_MPIAIJ);
4092: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
4093: "MatRetrieveValues_MPIAIJ",
4094: MatRetrieveValues_MPIAIJ);
4095: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetDiagonalBlock_C",
4096: "MatGetDiagonalBlock_MPIAIJ",
4097: MatGetDiagonalBlock_MPIAIJ);
4098: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
4099: "MatIsTranspose_MPIAIJ",
4100: MatIsTranspose_MPIAIJ);
4101: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocation_C",
4102: "MatMPIAIJSetPreallocation_MPIAIJ",
4103: MatMPIAIJSetPreallocation_MPIAIJ);
4104: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMPIAIJSetPreallocationCSR_C",
4105: "MatMPIAIJSetPreallocationCSR_MPIAIJ",
4106: MatMPIAIJSetPreallocationCSR_MPIAIJ);
4107: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatDiagonalScaleLocal_C",
4108: "MatDiagonalScaleLocal_MPIAIJ",
4109: MatDiagonalScaleLocal_MPIAIJ);
4110: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicsrperm_C",
4111: "MatConvert_MPIAIJ_MPICSRPERM",
4112: MatConvert_MPIAIJ_MPICSRPERM);
4113: PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_mpiaij_mpicrl_C",
4114: "MatConvert_MPIAIJ_MPICRL",
4115: MatConvert_MPIAIJ_MPICRL);
4116: PetscObjectChangeTypeName((PetscObject)B,MATMPIAIJ);
4117: return(0);
4118: }
4121: /*
4122: Special version for direct calls from Fortran
4123: */
4124: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4125: #define matsetvaluesmpiaij_ MATSETVALUESMPIAIJ
4126: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4127: #define matsetvaluesmpiaij_ matsetvaluesmpiaij
4128: #endif
4130: /* Change these macros so can be used in void function */
4131: #undef CHKERRQ
4132: #define CHKERRQ(ierr) CHKERRABORT(mat->comm,ierr)
4133: #undef SETERRQ2
4134: #define SETERRQ2(ierr,b,c,d) CHKERRABORT(mat->comm,ierr)
4135: #undef SETERRQ
4136: #define SETERRQ(ierr,b) CHKERRABORT(mat->comm,ierr)
4141: void PETSC_STDCALL matsetvaluesmpiaij_(Mat *mmat,PetscInt *mm,const PetscInt im[],PetscInt *mn,const PetscInt in[],const PetscScalar v[],InsertMode *maddv,PetscErrorCode *_ierr)
4142: {
4143: Mat mat = *mmat;
4144: PetscInt m = *mm, n = *mn;
4145: InsertMode addv = *maddv;
4146: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
4147: PetscScalar value;
4150: MatPreallocated(mat);
4151: if (mat->insertmode == NOT_SET_VALUES) {
4152: mat->insertmode = addv;
4153: }
4154: #if defined(PETSC_USE_DEBUG)
4155: else if (mat->insertmode != addv) {
4156: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values");
4157: }
4158: #endif
4159: {
4160: PetscInt i,j,rstart = mat->rmap.rstart,rend = mat->rmap.rend;
4161: PetscInt cstart = mat->cmap.rstart,cend = mat->cmap.rend,row,col;
4162: PetscTruth roworiented = aij->roworiented;
4164: /* Some Variables required in the macro */
4165: Mat A = aij->A;
4166: Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
4167: PetscInt *aimax = a->imax,*ai = a->i,*ailen = a->ilen,*aj = a->j;
4168: PetscScalar *aa = a->a;
4169: PetscTruth ignorezeroentries = (((a->ignorezeroentries)&&(addv==ADD_VALUES))?PETSC_TRUE:PETSC_FALSE);
4170: Mat B = aij->B;
4171: Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
4172: PetscInt *bimax = b->imax,*bi = b->i,*bilen = b->ilen,*bj = b->j,bm = aij->B->rmap.n,am = aij->A->rmap.n;
4173: PetscScalar *ba = b->a;
4175: PetscInt *rp1,*rp2,ii,nrow1,nrow2,_i,rmax1,rmax2,N,low1,high1,low2,high2,t,lastcol1,lastcol2;
4176: PetscInt nonew = a->nonew;
4177: PetscScalar *ap1,*ap2;
4180: for (i=0; i<m; i++) {
4181: if (im[i] < 0) continue;
4182: #if defined(PETSC_USE_DEBUG)
4183: if (im[i] >= mat->rmap.N) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",im[i],mat->rmap.N-1);
4184: #endif
4185: if (im[i] >= rstart && im[i] < rend) {
4186: row = im[i] - rstart;
4187: lastcol1 = -1;
4188: rp1 = aj + ai[row];
4189: ap1 = aa + ai[row];
4190: rmax1 = aimax[row];
4191: nrow1 = ailen[row];
4192: low1 = 0;
4193: high1 = nrow1;
4194: lastcol2 = -1;
4195: rp2 = bj + bi[row];
4196: ap2 = ba + bi[row];
4197: rmax2 = bimax[row];
4198: nrow2 = bilen[row];
4199: low2 = 0;
4200: high2 = nrow2;
4202: for (j=0; j<n; j++) {
4203: if (roworiented) value = v[i*n+j]; else value = v[i+j*m];
4204: if (ignorezeroentries && value == 0.0 && (addv == ADD_VALUES)) continue;
4205: if (in[j] >= cstart && in[j] < cend){
4206: col = in[j] - cstart;
4207: MatSetValues_SeqAIJ_A_Private(row,col,value,addv);
4208: } else if (in[j] < 0) continue;
4209: #if defined(PETSC_USE_DEBUG)
4210: else if (in[j] >= mat->cmap.N) {SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[j],mat->cmap.N-1);}
4211: #endif
4212: else {
4213: if (mat->was_assembled) {
4214: if (!aij->colmap) {
4215: CreateColmap_MPIAIJ_Private(mat);
4216: }
4217: #if defined (PETSC_USE_CTABLE)
4218: PetscTableFind(aij->colmap,in[j]+1,&col);
4219: col--;
4220: #else
4221: col = aij->colmap[in[j]] - 1;
4222: #endif
4223: if (col < 0 && !((Mat_SeqAIJ*)(aij->A->data))->nonew) {
4224: DisAssemble_MPIAIJ(mat);
4225: col = in[j];
4226: /* Reinitialize the variables required by MatSetValues_SeqAIJ_B_Private() */
4227: B = aij->B;
4228: b = (Mat_SeqAIJ*)B->data;
4229: bimax = b->imax; bi = b->i; bilen = b->ilen; bj = b->j;
4230: rp2 = bj + bi[row];
4231: ap2 = ba + bi[row];
4232: rmax2 = bimax[row];
4233: nrow2 = bilen[row];
4234: low2 = 0;
4235: high2 = nrow2;
4236: bm = aij->B->rmap.n;
4237: ba = b->a;
4238: }
4239: } else col = in[j];
4240: MatSetValues_SeqAIJ_B_Private(row,col,value,addv);
4241: }
4242: }
4243: } else {
4244: if (!aij->donotstash) {
4245: if (roworiented) {
4246: if (ignorezeroentries && v[i*n] == 0.0) continue;
4247: MatStashValuesRow_Private(&mat->stash,im[i],n,in,v+i*n);
4248: } else {
4249: if (ignorezeroentries && v[i] == 0.0) continue;
4250: MatStashValuesCol_Private(&mat->stash,im[i],n,in,v+i,m);
4251: }
4252: }
4253: }
4254: }}
4255: PetscFunctionReturnVoid();
4256: }