Actual source code: fdmpiaij.c
1: /*$Id: fdmpiaij.c,v 1.41 2001/06/21 21:16:31 bsmith Exp $*/
3: #include src/mat/impls/aij/mpi/mpiaij.h
4: #include src/vec/vecimpl.h
6: EXTERN int CreateColmap_MPIAIJ_Private(Mat);
7: EXTERN int MatGetColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
8: EXTERN int MatRestoreColumnIJ_SeqAIJ(Mat,int,PetscTruth,int*,int**,int**,PetscTruth*);
10: int MatFDColoringCreate_MPIAIJ(Mat mat,ISColoring iscoloring,MatFDColoring c)
11: {
12: Mat_MPIAIJ *aij = (Mat_MPIAIJ*)mat->data;
13: int i,*is,n,nrows,j,k,m,*rows = 0,ierr,*A_ci,*A_cj,ncols,col;
14: int nis = iscoloring->n,*ncolsonproc,size,nctot,*cols,*disp,*B_ci,*B_cj;
15: int *rowhit,M = mat->m,cstart = aij->cstart,cend = aij->cend,colb;
16: int *columnsforrow,l;
17: IS *isa;
18: PetscTruth done,flg;
21: if (!mat->assembled) {
22: SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be assembled first; MatAssemblyBegin/End();");
23: }
25: ISColoringGetIS(iscoloring,PETSC_IGNORE,&isa);
26: c->M = mat->M; /* set the global rows and columns and local rows */
27: c->N = mat->N;
28: c->m = mat->m;
29: c->rstart = aij->rstart;
31: c->ncolors = nis;
32: ierr = PetscMalloc(nis*sizeof(int),&c->ncolumns);
33: ierr = PetscMalloc(nis*sizeof(int*),&c->columns);
34: ierr = PetscMalloc(nis*sizeof(int),&c->nrows);
35: ierr = PetscMalloc(nis*sizeof(int*),&c->rows);
36: ierr = PetscMalloc(nis*sizeof(int*),&c->columnsforrow);
37: PetscLogObjectMemory(c,5*nis*sizeof(int));
39: /* Allow access to data structures of local part of matrix */
40: if (!aij->colmap) {
41: CreateColmap_MPIAIJ_Private(mat);
42: }
43: /*
44: Calls the _SeqAIJ() version of these routines to make sure it does not
45: get the reduced (by inodes) version of I and J
46: */
47: MatGetColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);
48: MatGetColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);
50: MPI_Comm_size(mat->comm,&size);
51: PetscMalloc(2*size*sizeof(int*),&ncolsonproc);
52: disp = ncolsonproc + size;
54: PetscMalloc((M+1)*sizeof(int),&rowhit);
55: PetscMalloc((M+1)*sizeof(int),&columnsforrow);
57: /*
58: Temporary option to allow for debugging/testing
59: */
60: PetscOptionsHasName(PETSC_NULL,"-matfdcoloring_slow",&flg);
62: for (i=0; i<nis; i++) {
63: ISGetLocalSize(isa[i],&n);
64: ISGetIndices(isa[i],&is);
65: c->ncolumns[i] = n;
66: c->ncolumns[i] = n;
67: if (n) {
68: PetscMalloc(n*sizeof(int),&c->columns[i]);
69: PetscLogObjectMemory(c,n*sizeof(int));
70: PetscMemcpy(c->columns[i],is,n*sizeof(int));
71: } else {
72: c->columns[i] = 0;
73: }
75: /* Determine the total (parallel) number of columns of this color */
76: MPI_Allgather(&n,1,MPI_INT,ncolsonproc,1,MPI_INT,mat->comm);
77: nctot = 0; for (j=0; j<size; j++) {nctot += ncolsonproc[j];}
78: if (!nctot) {
79: PetscLogInfo((PetscObject)mat,"MatFDColoringCreate_MPIAIJ: Coloring of matrix has some unneeded colors with no corresponding rowsn");
80: }
82: disp[0] = 0;
83: for (j=1; j<size; j++) {
84: disp[j] = disp[j-1] + ncolsonproc[j-1];
85: }
86:
87: /* Get complete list of columns for color on each processor */
88: PetscMalloc((nctot+1)*sizeof(int),&cols);
89: MPI_Allgatherv(is,n,MPI_INT,cols,ncolsonproc,disp,MPI_INT,mat->comm);
91: /*
92: Mark all rows affect by these columns
93: */
94: if (!flg) {/*-----------------------------------------------------------------------------*/
95: /* crude, fast version */
96: PetscMemzero(rowhit,M*sizeof(int));
97: /* loop over columns*/
98: for (j=0; j<nctot; j++) {
99: col = cols[j];
100: if (col >= cstart && col < cend) {
101: /* column is in diagonal block of matrix */
102: rows = A_cj + A_ci[col-cstart];
103: m = A_ci[col-cstart+1] - A_ci[col-cstart];
104: } else {
105: #if defined (PETSC_USE_CTABLE)
106: PetscTableFind(aij->colmap,col+1,&colb);CHKERRQ(ierr)
107: colb --;
108: #else
109: colb = aij->colmap[col] - 1;
110: #endif
111: if (colb == -1) {
112: m = 0;
113: } else {
114: rows = B_cj + B_ci[colb];
115: m = B_ci[colb+1] - B_ci[colb];
116: }
117: }
118: /* loop over columns marking them in rowhit */
119: for (k=0; k<m; k++) {
120: rowhit[*rows++] = col + 1;
121: }
122: }
124: /* count the number of hits */
125: nrows = 0;
126: for (j=0; j<M; j++) {
127: if (rowhit[j]) nrows++;
128: }
129: c->nrows[i] = nrows;
130: ierr = PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);
131: ierr = PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);
132: PetscLogObjectMemory(c,2*(nrows+1)*sizeof(int));
133: nrows = 0;
134: for (j=0; j<M; j++) {
135: if (rowhit[j]) {
136: c->rows[i][nrows] = j;
137: c->columnsforrow[i][nrows] = rowhit[j] - 1;
138: nrows++;
139: }
140: }
141: } else {/*-------------------------------------------------------------------------------*/
142: /* slow version, using rowhit as a linked list */
143: int currentcol,fm,mfm;
144: rowhit[M] = M;
145: nrows = 0;
146: /* loop over columns*/
147: for (j=0; j<nctot; j++) {
148: col = cols[j];
149: if (col >= cstart && col < cend) {
150: /* column is in diagonal block of matrix */
151: rows = A_cj + A_ci[col-cstart];
152: m = A_ci[col-cstart+1] - A_ci[col-cstart];
153: } else {
154: #if defined (PETSC_USE_CTABLE)
155: PetscTableFind(aij->colmap,col+1,&colb);
156: colb --;
157: #else
158: colb = aij->colmap[col] - 1;
159: #endif
160: if (colb == -1) {
161: m = 0;
162: } else {
163: rows = B_cj + B_ci[colb];
164: m = B_ci[colb+1] - B_ci[colb];
165: }
166: }
167: /* loop over columns marking them in rowhit */
168: fm = M; /* fm points to first entry in linked list */
169: for (k=0; k<m; k++) {
170: currentcol = *rows++;
171: /* is it already in the list? */
172: do {
173: mfm = fm;
174: fm = rowhit[fm];
175: } while (fm < currentcol);
176: /* not in list so add it */
177: if (fm != currentcol) {
178: nrows++;
179: columnsforrow[currentcol] = col;
180: /* next three lines insert new entry into linked list */
181: rowhit[mfm] = currentcol;
182: rowhit[currentcol] = fm;
183: fm = currentcol;
184: /* fm points to present position in list since we know the columns are sorted */
185: } else {
186: SETERRQ(PETSC_ERR_PLIB,"Invalid coloring of matrix detected");
187: }
188: }
189: }
190: c->nrows[i] = nrows;
191: PetscMalloc((nrows+1)*sizeof(int),&c->rows[i]);
192: PetscMalloc((nrows+1)*sizeof(int),&c->columnsforrow[i]);
193: PetscLogObjectMemory(c,(nrows+1)*sizeof(int));
194: /* now store the linked list of rows into c->rows[i] */
195: nrows = 0;
196: fm = rowhit[M];
197: do {
198: c->rows[i][nrows] = fm;
199: c->columnsforrow[i][nrows++] = columnsforrow[fm];
200: fm = rowhit[fm];
201: } while (fm < M);
202: } /* ---------------------------------------------------------------------------------------*/
203: PetscFree(cols);
204: }
206: /* Optimize by adding the vscale, and scaleforrow[][] fields */
207: /*
208: vscale will contain the "diagonal" on processor scalings followed by the off processor
209: */
210: VecCreateGhost(mat->comm,aij->A->m,PETSC_DETERMINE,aij->B->n,aij->garray,&c->vscale);CHKERRQ(ierr)
211: PetscMalloc(c->ncolors*sizeof(int*),&c->vscaleforrow);
212: for (k=0; k<c->ncolors; k++) {
213: PetscMalloc((c->nrows[k]+1)*sizeof(int),&c->vscaleforrow[k]);
214: for (l=0; l<c->nrows[k]; l++) {
215: col = c->columnsforrow[k][l];
216: if (col >= cstart && col < cend) {
217: /* column is in diagonal block of matrix */
218: colb = col - cstart;
219: } else {
220: /* column is in "off-processor" part */
221: #if defined (PETSC_USE_CTABLE)
222: PetscTableFind(aij->colmap,col+1,&colb);
223: colb --;
224: #else
225: colb = aij->colmap[col] - 1;
226: #endif
227: colb += cend - cstart;
228: }
229: c->vscaleforrow[k][l] = colb;
230: }
231: }
232: ISColoringRestoreIS(iscoloring,&isa);
234: PetscFree(rowhit);
235: PetscFree(columnsforrow);
236: PetscFree(ncolsonproc);
237: MatRestoreColumnIJ_SeqAIJ(aij->A,0,PETSC_FALSE,&ncols,&A_ci,&A_cj,&done);
238: MatRestoreColumnIJ_SeqAIJ(aij->B,0,PETSC_FALSE,&ncols,&B_ci,&B_cj,&done);
239: return(0);
240: }