Actual source code: dense.c
petsc-3.5.3 2015-01-31
2: /*
3: Defines the basic matrix operations for sequential dense.
4: */
6: #include <../src/mat/impls/dense/seq/dense.h> /*I "petscmat.h" I*/
7: #include <petscblaslapack.h>
9: #include <../src/mat/impls/aij/seq/aij.h>
13: PETSC_EXTERN PetscErrorCode MatConvert_SeqDense_SeqAIJ(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
14: {
15: Mat B;
16: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
18: PetscInt i, j;
19: PetscInt *rows, *nnz;
20: MatScalar *aa = a->v, *vals;
23: MatCreate(PetscObjectComm((PetscObject)A),&B);
24: MatSetSizes(B,A->rmap->n,A->cmap->n,A->rmap->N,A->cmap->N);
25: MatSetType(B,MATSEQAIJ);
26: PetscCalloc3(A->rmap->n,&rows,A->rmap->n,&nnz,A->rmap->n,&vals);
27: for (j=0; j<A->cmap->n; j++) {
28: for (i=0; i<A->rmap->n; i++) if (aa[i] != 0.0 || i == j) ++nnz[i];
29: aa += a->lda;
30: }
31: MatSeqAIJSetPreallocation(B,PETSC_DETERMINE,nnz);
32: aa = a->v;
33: for (j=0; j<A->cmap->n; j++) {
34: PetscInt numRows = 0;
35: for (i=0; i<A->rmap->n; i++) if (aa[i] != 0.0 || i == j) {rows[numRows] = i; vals[numRows++] = aa[i];}
36: MatSetValues(B,numRows,rows,1,&j,vals,INSERT_VALUES);
37: aa += a->lda;
38: }
39: PetscFree3(rows,nnz,vals);
40: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
41: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
43: if (reuse == MAT_REUSE_MATRIX) {
44: MatHeaderReplace(A,B);
45: } else {
46: *newmat = B;
47: }
48: return(0);
49: }
53: PetscErrorCode MatAXPY_SeqDense(Mat Y,PetscScalar alpha,Mat X,MatStructure str)
54: {
55: Mat_SeqDense *x = (Mat_SeqDense*)X->data,*y = (Mat_SeqDense*)Y->data;
56: PetscScalar oalpha = alpha;
57: PetscInt j;
58: PetscBLASInt N,m,ldax,lday,one = 1;
62: PetscBLASIntCast(X->rmap->n*X->cmap->n,&N);
63: PetscBLASIntCast(X->rmap->n,&m);
64: PetscBLASIntCast(x->lda,&ldax);
65: PetscBLASIntCast(y->lda,&lday);
66: if (ldax>m || lday>m) {
67: for (j=0; j<X->cmap->n; j++) {
68: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&m,&oalpha,x->v+j*ldax,&one,y->v+j*lday,&one));
69: }
70: } else {
71: PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&N,&oalpha,x->v,&one,y->v,&one));
72: }
73: PetscObjectStateIncrease((PetscObject)Y);
74: PetscLogFlops(PetscMax(2*N-1,0));
75: return(0);
76: }
80: PetscErrorCode MatGetInfo_SeqDense(Mat A,MatInfoType flag,MatInfo *info)
81: {
82: PetscInt N = A->rmap->n*A->cmap->n;
85: info->block_size = 1.0;
86: info->nz_allocated = (double)N;
87: info->nz_used = (double)N;
88: info->nz_unneeded = (double)0;
89: info->assemblies = (double)A->num_ass;
90: info->mallocs = 0;
91: info->memory = ((PetscObject)A)->mem;
92: info->fill_ratio_given = 0;
93: info->fill_ratio_needed = 0;
94: info->factor_mallocs = 0;
95: return(0);
96: }
100: PetscErrorCode MatScale_SeqDense(Mat A,PetscScalar alpha)
101: {
102: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
103: PetscScalar oalpha = alpha;
105: PetscBLASInt one = 1,j,nz,lda;
108: PetscBLASIntCast(a->lda,&lda);
109: if (lda>A->rmap->n) {
110: PetscBLASIntCast(A->rmap->n,&nz);
111: for (j=0; j<A->cmap->n; j++) {
112: PetscStackCallBLAS("BLASscal",BLASscal_(&nz,&oalpha,a->v+j*lda,&one));
113: }
114: } else {
115: PetscBLASIntCast(A->rmap->n*A->cmap->n,&nz);
116: PetscStackCallBLAS("BLASscal",BLASscal_(&nz,&oalpha,a->v,&one));
117: }
118: PetscLogFlops(nz);
119: return(0);
120: }
124: PetscErrorCode MatIsHermitian_SeqDense(Mat A,PetscReal rtol,PetscBool *fl)
125: {
126: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
127: PetscInt i,j,m = A->rmap->n,N;
128: PetscScalar *v = a->v;
131: *fl = PETSC_FALSE;
132: if (A->rmap->n != A->cmap->n) return(0);
133: N = a->lda;
135: for (i=0; i<m; i++) {
136: for (j=i+1; j<m; j++) {
137: if (PetscAbsScalar(v[i+j*N] - PetscConj(v[j+i*N])) > rtol) return(0);
138: }
139: }
140: *fl = PETSC_TRUE;
141: return(0);
142: }
146: PetscErrorCode MatDuplicateNoCreate_SeqDense(Mat newi,Mat A,MatDuplicateOption cpvalues)
147: {
148: Mat_SeqDense *mat = (Mat_SeqDense*)A->data,*l;
150: PetscInt lda = (PetscInt)mat->lda,j,m;
153: PetscLayoutReference(A->rmap,&newi->rmap);
154: PetscLayoutReference(A->cmap,&newi->cmap);
155: MatSeqDenseSetPreallocation(newi,NULL);
156: if (cpvalues == MAT_COPY_VALUES) {
157: l = (Mat_SeqDense*)newi->data;
158: if (lda>A->rmap->n) {
159: m = A->rmap->n;
160: for (j=0; j<A->cmap->n; j++) {
161: PetscMemcpy(l->v+j*m,mat->v+j*lda,m*sizeof(PetscScalar));
162: }
163: } else {
164: PetscMemcpy(l->v,mat->v,A->rmap->n*A->cmap->n*sizeof(PetscScalar));
165: }
166: }
167: newi->assembled = PETSC_TRUE;
168: return(0);
169: }
173: PetscErrorCode MatDuplicate_SeqDense(Mat A,MatDuplicateOption cpvalues,Mat *newmat)
174: {
178: MatCreate(PetscObjectComm((PetscObject)A),newmat);
179: MatSetSizes(*newmat,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
180: MatSetType(*newmat,((PetscObject)A)->type_name);
181: MatDuplicateNoCreate_SeqDense(*newmat,A,cpvalues);
182: return(0);
183: }
186: extern PetscErrorCode MatLUFactor_SeqDense(Mat,IS,IS,const MatFactorInfo*);
190: PetscErrorCode MatLUFactorNumeric_SeqDense(Mat fact,Mat A,const MatFactorInfo *info_dummy)
191: {
192: MatFactorInfo info;
196: MatDuplicateNoCreate_SeqDense(fact,A,MAT_COPY_VALUES);
197: MatLUFactor_SeqDense(fact,0,0,&info);
198: return(0);
199: }
203: PetscErrorCode MatSolve_SeqDense(Mat A,Vec xx,Vec yy)
204: {
205: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
207: PetscScalar *x,*y;
208: PetscBLASInt one = 1,info,m;
211: PetscBLASIntCast(A->rmap->n,&m);
212: VecGetArray(xx,&x);
213: VecGetArray(yy,&y);
214: PetscMemcpy(y,x,A->rmap->n*sizeof(PetscScalar));
215: if (A->factortype == MAT_FACTOR_LU) {
216: #if defined(PETSC_MISSING_LAPACK_GETRS)
217: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
218: #else
219: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("N",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info));
220: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"GETRS - Bad solve");
221: #endif
222: } else if (A->factortype == MAT_FACTOR_CHOLESKY) {
223: #if defined(PETSC_MISSING_LAPACK_POTRS)
224: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
225: #else
226: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info));
227: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"POTRS Bad solve");
228: #endif
229: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be factored to solve");
230: VecRestoreArray(xx,&x);
231: VecRestoreArray(yy,&y);
232: PetscLogFlops(2.0*A->cmap->n*A->cmap->n - A->cmap->n);
233: return(0);
234: }
238: PetscErrorCode MatMatSolve_SeqDense(Mat A,Mat B,Mat X)
239: {
240: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
242: PetscScalar *b,*x;
243: PetscInt n;
244: PetscBLASInt nrhs,info,m;
245: PetscBool flg;
248: PetscBLASIntCast(A->rmap->n,&m);
249: PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
250: if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix B must be MATDENSE matrix");
251: PetscObjectTypeCompareAny((PetscObject)X,&flg,MATSEQDENSE,MATMPIDENSE,NULL);
252: if (!flg) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Matrix X must be MATDENSE matrix");
254: MatGetSize(B,NULL,&n);
255: PetscBLASIntCast(n,&nrhs);
256: MatDenseGetArray(B,&b);
257: MatDenseGetArray(X,&x);
259: PetscMemcpy(x,b,m*nrhs*sizeof(PetscScalar));
261: if (A->factortype == MAT_FACTOR_LU) {
262: #if defined(PETSC_MISSING_LAPACK_GETRS)
263: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
264: #else
265: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("N",&m,&nrhs,mat->v,&mat->lda,mat->pivots,x,&m,&info));
266: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"GETRS - Bad solve");
267: #endif
268: } else if (A->factortype == MAT_FACTOR_CHOLESKY) {
269: #if defined(PETSC_MISSING_LAPACK_POTRS)
270: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
271: #else
272: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&m,&nrhs,mat->v,&mat->lda,x,&m,&info));
273: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"POTRS Bad solve");
274: #endif
275: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix must be factored to solve");
277: MatDenseRestoreArray(B,&b);
278: MatDenseRestoreArray(X,&x);
279: PetscLogFlops(nrhs*(2.0*m*m - m));
280: return(0);
281: }
285: PetscErrorCode MatSolveTranspose_SeqDense(Mat A,Vec xx,Vec yy)
286: {
287: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
289: PetscScalar *x,*y;
290: PetscBLASInt one = 1,info,m;
293: PetscBLASIntCast(A->rmap->n,&m);
294: VecGetArray(xx,&x);
295: VecGetArray(yy,&y);
296: PetscMemcpy(y,x,A->rmap->n*sizeof(PetscScalar));
297: /* assume if pivots exist then use LU; else Cholesky */
298: if (mat->pivots) {
299: #if defined(PETSC_MISSING_LAPACK_GETRS)
300: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
301: #else
302: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("T",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info));
303: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"POTRS - Bad solve");
304: #endif
305: } else {
306: #if defined(PETSC_MISSING_LAPACK_POTRS)
307: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
308: #else
309: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info));
310: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"POTRS - Bad solve");
311: #endif
312: }
313: VecRestoreArray(xx,&x);
314: VecRestoreArray(yy,&y);
315: PetscLogFlops(2.0*A->cmap->n*A->cmap->n - A->cmap->n);
316: return(0);
317: }
321: PetscErrorCode MatSolveAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
322: {
323: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
325: PetscScalar *x,*y,sone = 1.0;
326: Vec tmp = 0;
327: PetscBLASInt one = 1,info,m;
330: PetscBLASIntCast(A->rmap->n,&m);
331: VecGetArray(xx,&x);
332: VecGetArray(yy,&y);
333: if (!A->rmap->n || !A->cmap->n) return(0);
334: if (yy == zz) {
335: VecDuplicate(yy,&tmp);
336: PetscLogObjectParent((PetscObject)A,(PetscObject)tmp);
337: VecCopy(yy,tmp);
338: }
339: PetscMemcpy(y,x,A->rmap->n*sizeof(PetscScalar));
340: /* assume if pivots exist then use LU; else Cholesky */
341: if (mat->pivots) {
342: #if defined(PETSC_MISSING_LAPACK_GETRS)
343: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
344: #else
345: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("N",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info));
346: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad solve");
347: #endif
348: } else {
349: #if defined(PETSC_MISSING_LAPACK_POTRS)
350: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
351: #else
352: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info));
353: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad solve");
354: #endif
355: }
356: if (tmp) {
357: VecAXPY(yy,sone,tmp);
358: VecDestroy(&tmp);
359: } else {
360: VecAXPY(yy,sone,zz);
361: }
362: VecRestoreArray(xx,&x);
363: VecRestoreArray(yy,&y);
364: PetscLogFlops(2.0*A->cmap->n*A->cmap->n);
365: return(0);
366: }
370: PetscErrorCode MatSolveTransposeAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
371: {
372: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
374: PetscScalar *x,*y,sone = 1.0;
375: Vec tmp;
376: PetscBLASInt one = 1,info,m;
379: PetscBLASIntCast(A->rmap->n,&m);
380: if (!A->rmap->n || !A->cmap->n) return(0);
381: VecGetArray(xx,&x);
382: VecGetArray(yy,&y);
383: if (yy == zz) {
384: VecDuplicate(yy,&tmp);
385: PetscLogObjectParent((PetscObject)A,(PetscObject)tmp);
386: VecCopy(yy,tmp);
387: }
388: PetscMemcpy(y,x,A->rmap->n*sizeof(PetscScalar));
389: /* assume if pivots exist then use LU; else Cholesky */
390: if (mat->pivots) {
391: #if defined(PETSC_MISSING_LAPACK_GETRS)
392: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
393: #else
394: PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("T",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info));
395: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad solve");
396: #endif
397: } else {
398: #if defined(PETSC_MISSING_LAPACK_POTRS)
399: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
400: #else
401: PetscStackCallBLAS("LAPACKpotrs",LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info));
402: if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad solve");
403: #endif
404: }
405: if (tmp) {
406: VecAXPY(yy,sone,tmp);
407: VecDestroy(&tmp);
408: } else {
409: VecAXPY(yy,sone,zz);
410: }
411: VecRestoreArray(xx,&x);
412: VecRestoreArray(yy,&y);
413: PetscLogFlops(2.0*A->cmap->n*A->cmap->n);
414: return(0);
415: }
417: /* ---------------------------------------------------------------*/
418: /* COMMENT: I have chosen to hide row permutation in the pivots,
419: rather than put it in the Mat->row slot.*/
422: PetscErrorCode MatLUFactor_SeqDense(Mat A,IS row,IS col,const MatFactorInfo *minfo)
423: {
424: #if defined(PETSC_MISSING_LAPACK_GETRF)
426: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GETRF - Lapack routine is unavailable.");
427: #else
428: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
430: PetscBLASInt n,m,info;
433: PetscBLASIntCast(A->cmap->n,&n);
434: PetscBLASIntCast(A->rmap->n,&m);
435: if (!mat->pivots) {
436: PetscMalloc1((A->rmap->n+1),&mat->pivots);
437: PetscLogObjectMemory((PetscObject)A,A->rmap->n*sizeof(PetscBLASInt));
438: }
439: if (!A->rmap->n || !A->cmap->n) return(0);
440: PetscFPTrapPush(PETSC_FP_TRAP_OFF);
441: PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&m,&n,mat->v,&mat->lda,mat->pivots,&info));
442: PetscFPTrapPop();
444: if (info<0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to LU factorization");
445: if (info>0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Bad LU factorization");
446: A->ops->solve = MatSolve_SeqDense;
447: A->ops->solvetranspose = MatSolveTranspose_SeqDense;
448: A->ops->solveadd = MatSolveAdd_SeqDense;
449: A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqDense;
450: A->factortype = MAT_FACTOR_LU;
452: PetscLogFlops((2.0*A->cmap->n*A->cmap->n*A->cmap->n)/3);
453: #endif
454: return(0);
455: }
459: PetscErrorCode MatCholeskyFactor_SeqDense(Mat A,IS perm,const MatFactorInfo *factinfo)
460: {
461: #if defined(PETSC_MISSING_LAPACK_POTRF)
463: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"POTRF - Lapack routine is unavailable.");
464: #else
465: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
467: PetscBLASInt info,n;
470: PetscBLASIntCast(A->cmap->n,&n);
471: PetscFree(mat->pivots);
473: if (!A->rmap->n || !A->cmap->n) return(0);
474: PetscStackCallBLAS("LAPACKpotrf",LAPACKpotrf_("L",&n,mat->v,&mat->lda,&info));
475: if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_CH_ZRPVT,"Bad factorization: zero pivot in row %D",(PetscInt)info-1);
476: A->ops->solve = MatSolve_SeqDense;
477: A->ops->solvetranspose = MatSolveTranspose_SeqDense;
478: A->ops->solveadd = MatSolveAdd_SeqDense;
479: A->ops->solvetransposeadd = MatSolveTransposeAdd_SeqDense;
480: A->factortype = MAT_FACTOR_CHOLESKY;
482: PetscLogFlops((A->cmap->n*A->cmap->n*A->cmap->n)/3.0);
483: #endif
484: return(0);
485: }
490: PetscErrorCode MatCholeskyFactorNumeric_SeqDense(Mat fact,Mat A,const MatFactorInfo *info_dummy)
491: {
493: MatFactorInfo info;
496: info.fill = 1.0;
498: MatDuplicateNoCreate_SeqDense(fact,A,MAT_COPY_VALUES);
499: MatCholeskyFactor_SeqDense(fact,0,&info);
500: return(0);
501: }
505: PetscErrorCode MatCholeskyFactorSymbolic_SeqDense(Mat fact,Mat A,IS row,const MatFactorInfo *info)
506: {
508: fact->assembled = PETSC_TRUE;
509: fact->preallocated = PETSC_TRUE;
510: fact->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqDense;
511: return(0);
512: }
516: PetscErrorCode MatLUFactorSymbolic_SeqDense(Mat fact,Mat A,IS row,IS col,const MatFactorInfo *info)
517: {
519: fact->preallocated = PETSC_TRUE;
520: fact->assembled = PETSC_TRUE;
521: fact->ops->lufactornumeric = MatLUFactorNumeric_SeqDense;
522: return(0);
523: }
527: PETSC_EXTERN PetscErrorCode MatGetFactor_seqdense_petsc(Mat A,MatFactorType ftype,Mat *fact)
528: {
532: MatCreate(PetscObjectComm((PetscObject)A),fact);
533: MatSetSizes(*fact,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
534: MatSetType(*fact,((PetscObject)A)->type_name);
535: if (ftype == MAT_FACTOR_LU) {
536: (*fact)->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqDense;
537: } else {
538: (*fact)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqDense;
539: }
540: (*fact)->factortype = ftype;
541: return(0);
542: }
544: /* ------------------------------------------------------------------*/
547: PetscErrorCode MatSOR_SeqDense(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec xx)
548: {
549: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
550: PetscScalar *x,*b,*v = mat->v,zero = 0.0,xt;
552: PetscInt m = A->rmap->n,i;
553: PetscBLASInt o = 1,bm;
556: PetscBLASIntCast(m,&bm);
557: if (flag & SOR_ZERO_INITIAL_GUESS) {
558: /* this is a hack fix, should have another version without the second BLASdot */
559: VecSet(xx,zero);
560: }
561: VecGetArray(xx,&x);
562: VecGetArray(bb,&b);
563: its = its*lits;
564: if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
565: while (its--) {
566: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
567: for (i=0; i<m; i++) {
568: PetscStackCallBLAS("BLASdot",xt = b[i] - BLASdot_(&bm,v+i,&bm,x,&o));
569: x[i] = (1. - omega)*x[i] + omega*(xt+v[i + i*m]*x[i])/(v[i + i*m]+shift);
570: }
571: }
572: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
573: for (i=m-1; i>=0; i--) {
574: PetscStackCallBLAS("BLASdot",xt = b[i] - BLASdot_(&bm,v+i,&bm,x,&o));
575: x[i] = (1. - omega)*x[i] + omega*(xt+v[i + i*m]*x[i])/(v[i + i*m]+shift);
576: }
577: }
578: }
579: VecRestoreArray(bb,&b);
580: VecRestoreArray(xx,&x);
581: return(0);
582: }
584: /* -----------------------------------------------------------------*/
587: PetscErrorCode MatMultTranspose_SeqDense(Mat A,Vec xx,Vec yy)
588: {
589: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
590: PetscScalar *v = mat->v,*x,*y;
592: PetscBLASInt m, n,_One=1;
593: PetscScalar _DOne=1.0,_DZero=0.0;
596: PetscBLASIntCast(A->rmap->n,&m);
597: PetscBLASIntCast(A->cmap->n,&n);
598: if (!A->rmap->n || !A->cmap->n) return(0);
599: VecGetArray(xx,&x);
600: VecGetArray(yy,&y);
601: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&m,&n,&_DOne,v,&mat->lda,x,&_One,&_DZero,y,&_One));
602: VecRestoreArray(xx,&x);
603: VecRestoreArray(yy,&y);
604: PetscLogFlops(2.0*A->rmap->n*A->cmap->n - A->cmap->n);
605: return(0);
606: }
610: PetscErrorCode MatMult_SeqDense(Mat A,Vec xx,Vec yy)
611: {
612: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
613: PetscScalar *v = mat->v,*x,*y,_DOne=1.0,_DZero=0.0;
615: PetscBLASInt m, n, _One=1;
618: PetscBLASIntCast(A->rmap->n,&m);
619: PetscBLASIntCast(A->cmap->n,&n);
620: if (!A->rmap->n || !A->cmap->n) return(0);
621: VecGetArray(xx,&x);
622: VecGetArray(yy,&y);
623: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DZero,y,&_One));
624: VecRestoreArray(xx,&x);
625: VecRestoreArray(yy,&y);
626: PetscLogFlops(2.0*A->rmap->n*A->cmap->n - A->rmap->n);
627: return(0);
628: }
632: PetscErrorCode MatMultAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
633: {
634: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
635: PetscScalar *v = mat->v,*x,*y,_DOne=1.0;
637: PetscBLASInt m, n, _One=1;
640: PetscBLASIntCast(A->rmap->n,&m);
641: PetscBLASIntCast(A->cmap->n,&n);
642: if (!A->rmap->n || !A->cmap->n) return(0);
643: if (zz != yy) {VecCopy(zz,yy);}
644: VecGetArray(xx,&x);
645: VecGetArray(yy,&y);
646: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DOne,y,&_One));
647: VecRestoreArray(xx,&x);
648: VecRestoreArray(yy,&y);
649: PetscLogFlops(2.0*A->rmap->n*A->cmap->n);
650: return(0);
651: }
655: PetscErrorCode MatMultTransposeAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
656: {
657: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
658: PetscScalar *v = mat->v,*x,*y;
660: PetscBLASInt m, n, _One=1;
661: PetscScalar _DOne=1.0;
664: PetscBLASIntCast(A->rmap->n,&m);
665: PetscBLASIntCast(A->cmap->n,&n);
666: if (!A->rmap->n || !A->cmap->n) return(0);
667: if (zz != yy) {VecCopy(zz,yy);}
668: VecGetArray(xx,&x);
669: VecGetArray(yy,&y);
670: PetscStackCallBLAS("BLASgemv",BLASgemv_("T",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DOne,y,&_One));
671: VecRestoreArray(xx,&x);
672: VecRestoreArray(yy,&y);
673: PetscLogFlops(2.0*A->rmap->n*A->cmap->n);
674: return(0);
675: }
677: /* -----------------------------------------------------------------*/
680: PetscErrorCode MatGetRow_SeqDense(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **vals)
681: {
682: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
683: PetscScalar *v;
685: PetscInt i;
688: *ncols = A->cmap->n;
689: if (cols) {
690: PetscMalloc1((A->cmap->n+1),cols);
691: for (i=0; i<A->cmap->n; i++) (*cols)[i] = i;
692: }
693: if (vals) {
694: PetscMalloc1((A->cmap->n+1),vals);
695: v = mat->v + row;
696: for (i=0; i<A->cmap->n; i++) {(*vals)[i] = *v; v += mat->lda;}
697: }
698: return(0);
699: }
703: PetscErrorCode MatRestoreRow_SeqDense(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **vals)
704: {
708: if (cols) {PetscFree(*cols);}
709: if (vals) {PetscFree(*vals); }
710: return(0);
711: }
712: /* ----------------------------------------------------------------*/
715: PetscErrorCode MatSetValues_SeqDense(Mat A,PetscInt m,const PetscInt indexm[],PetscInt n,const PetscInt indexn[],const PetscScalar v[],InsertMode addv)
716: {
717: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
718: PetscInt i,j,idx=0;
721: if (!mat->roworiented) {
722: if (addv == INSERT_VALUES) {
723: for (j=0; j<n; j++) {
724: if (indexn[j] < 0) {idx += m; continue;}
725: #if defined(PETSC_USE_DEBUG)
726: if (indexn[j] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap->n-1);
727: #endif
728: for (i=0; i<m; i++) {
729: if (indexm[i] < 0) {idx++; continue;}
730: #if defined(PETSC_USE_DEBUG)
731: if (indexm[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap->n-1);
732: #endif
733: mat->v[indexn[j]*mat->lda + indexm[i]] = v[idx++];
734: }
735: }
736: } else {
737: for (j=0; j<n; j++) {
738: if (indexn[j] < 0) {idx += m; continue;}
739: #if defined(PETSC_USE_DEBUG)
740: if (indexn[j] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap->n-1);
741: #endif
742: for (i=0; i<m; i++) {
743: if (indexm[i] < 0) {idx++; continue;}
744: #if defined(PETSC_USE_DEBUG)
745: if (indexm[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap->n-1);
746: #endif
747: mat->v[indexn[j]*mat->lda + indexm[i]] += v[idx++];
748: }
749: }
750: }
751: } else {
752: if (addv == INSERT_VALUES) {
753: for (i=0; i<m; i++) {
754: if (indexm[i] < 0) { idx += n; continue;}
755: #if defined(PETSC_USE_DEBUG)
756: if (indexm[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap->n-1);
757: #endif
758: for (j=0; j<n; j++) {
759: if (indexn[j] < 0) { idx++; continue;}
760: #if defined(PETSC_USE_DEBUG)
761: if (indexn[j] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap->n-1);
762: #endif
763: mat->v[indexn[j]*mat->lda + indexm[i]] = v[idx++];
764: }
765: }
766: } else {
767: for (i=0; i<m; i++) {
768: if (indexm[i] < 0) { idx += n; continue;}
769: #if defined(PETSC_USE_DEBUG)
770: if (indexm[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap->n-1);
771: #endif
772: for (j=0; j<n; j++) {
773: if (indexn[j] < 0) { idx++; continue;}
774: #if defined(PETSC_USE_DEBUG)
775: if (indexn[j] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap->n-1);
776: #endif
777: mat->v[indexn[j]*mat->lda + indexm[i]] += v[idx++];
778: }
779: }
780: }
781: }
782: return(0);
783: }
787: PetscErrorCode MatGetValues_SeqDense(Mat A,PetscInt m,const PetscInt indexm[],PetscInt n,const PetscInt indexn[],PetscScalar v[])
788: {
789: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
790: PetscInt i,j;
793: /* row-oriented output */
794: for (i=0; i<m; i++) {
795: if (indexm[i] < 0) {v += n;continue;}
796: if (indexm[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D requested larger than number rows %D",indexm[i],A->rmap->n);
797: for (j=0; j<n; j++) {
798: if (indexn[j] < 0) {v++; continue;}
799: if (indexn[j] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column %D requested larger than number columns %D",indexn[j],A->cmap->n);
800: *v++ = mat->v[indexn[j]*mat->lda + indexm[i]];
801: }
802: }
803: return(0);
804: }
806: /* -----------------------------------------------------------------*/
810: PetscErrorCode MatLoad_SeqDense(Mat newmat,PetscViewer viewer)
811: {
812: Mat_SeqDense *a;
814: PetscInt *scols,i,j,nz,header[4];
815: int fd;
816: PetscMPIInt size;
817: PetscInt *rowlengths = 0,M,N,*cols,grows,gcols;
818: PetscScalar *vals,*svals,*v,*w;
819: MPI_Comm comm;
822: PetscObjectGetComm((PetscObject)viewer,&comm);
823: MPI_Comm_size(comm,&size);
824: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
825: PetscViewerBinaryGetDescriptor(viewer,&fd);
826: PetscBinaryRead(fd,header,4,PETSC_INT);
827: if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not matrix object");
828: M = header[1]; N = header[2]; nz = header[3];
830: /* set global size if not set already*/
831: if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
832: MatSetSizes(newmat,M,N,M,N);
833: } else {
834: /* if sizes and type are already set, check if the vector global sizes are correct */
835: MatGetSize(newmat,&grows,&gcols);
836: if (M != grows || N != gcols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%d, %d) than the input matrix (%d, %d)",M,N,grows,gcols);
837: }
838: MatSeqDenseSetPreallocation(newmat,NULL);
840: if (nz == MATRIX_BINARY_FORMAT_DENSE) { /* matrix in file is dense */
841: a = (Mat_SeqDense*)newmat->data;
842: v = a->v;
843: /* Allocate some temp space to read in the values and then flip them
844: from row major to column major */
845: PetscMalloc1((M*N > 0 ? M*N : 1),&w);
846: /* read in nonzero values */
847: PetscBinaryRead(fd,w,M*N,PETSC_SCALAR);
848: /* now flip the values and store them in the matrix*/
849: for (j=0; j<N; j++) {
850: for (i=0; i<M; i++) {
851: *v++ =w[i*N+j];
852: }
853: }
854: PetscFree(w);
855: } else {
856: /* read row lengths */
857: PetscMalloc1((M+1),&rowlengths);
858: PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
860: a = (Mat_SeqDense*)newmat->data;
861: v = a->v;
863: /* read column indices and nonzeros */
864: PetscMalloc1((nz+1),&scols);
865: cols = scols;
866: PetscBinaryRead(fd,cols,nz,PETSC_INT);
867: PetscMalloc1((nz+1),&svals);
868: vals = svals;
869: PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);
871: /* insert into matrix */
872: for (i=0; i<M; i++) {
873: for (j=0; j<rowlengths[i]; j++) v[i+M*scols[j]] = svals[j];
874: svals += rowlengths[i]; scols += rowlengths[i];
875: }
876: PetscFree(vals);
877: PetscFree(cols);
878: PetscFree(rowlengths);
879: }
880: MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
881: MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
882: return(0);
883: }
887: static PetscErrorCode MatView_SeqDense_ASCII(Mat A,PetscViewer viewer)
888: {
889: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
890: PetscErrorCode ierr;
891: PetscInt i,j;
892: const char *name;
893: PetscScalar *v;
894: PetscViewerFormat format;
895: #if defined(PETSC_USE_COMPLEX)
896: PetscBool allreal = PETSC_TRUE;
897: #endif
900: PetscViewerGetFormat(viewer,&format);
901: if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
902: return(0); /* do nothing for now */
903: } else if (format == PETSC_VIEWER_ASCII_COMMON) {
904: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
905: for (i=0; i<A->rmap->n; i++) {
906: v = a->v + i;
907: PetscViewerASCIIPrintf(viewer,"row %D:",i);
908: for (j=0; j<A->cmap->n; j++) {
909: #if defined(PETSC_USE_COMPLEX)
910: if (PetscRealPart(*v) != 0.0 && PetscImaginaryPart(*v) != 0.0) {
911: PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",j,(double)PetscRealPart(*v),(double)PetscImaginaryPart(*v));
912: } else if (PetscRealPart(*v)) {
913: PetscViewerASCIIPrintf(viewer," (%D, %g) ",j,(double)PetscRealPart(*v));
914: }
915: #else
916: if (*v) {
917: PetscViewerASCIIPrintf(viewer," (%D, %g) ",j,(double)*v);
918: }
919: #endif
920: v += a->lda;
921: }
922: PetscViewerASCIIPrintf(viewer,"\n");
923: }
924: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
925: } else {
926: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
927: #if defined(PETSC_USE_COMPLEX)
928: /* determine if matrix has all real values */
929: v = a->v;
930: for (i=0; i<A->rmap->n*A->cmap->n; i++) {
931: if (PetscImaginaryPart(v[i])) { allreal = PETSC_FALSE; break;}
932: }
933: #endif
934: if (format == PETSC_VIEWER_ASCII_MATLAB) {
935: PetscObjectGetName((PetscObject)A,&name);
936: PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",A->rmap->n,A->cmap->n);
937: PetscViewerASCIIPrintf(viewer,"%s = zeros(%D,%D);\n",name,A->rmap->n,A->cmap->n);
938: PetscViewerASCIIPrintf(viewer,"%s = [\n",name);
939: }
941: for (i=0; i<A->rmap->n; i++) {
942: v = a->v + i;
943: for (j=0; j<A->cmap->n; j++) {
944: #if defined(PETSC_USE_COMPLEX)
945: if (allreal) {
946: PetscViewerASCIIPrintf(viewer,"%18.16e ",(double)PetscRealPart(*v));
947: } else {
948: PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei ",(double)PetscRealPart(*v),(double)PetscImaginaryPart(*v));
949: }
950: #else
951: PetscViewerASCIIPrintf(viewer,"%18.16e ",(double)*v);
952: #endif
953: v += a->lda;
954: }
955: PetscViewerASCIIPrintf(viewer,"\n");
956: }
957: if (format == PETSC_VIEWER_ASCII_MATLAB) {
958: PetscViewerASCIIPrintf(viewer,"];\n");
959: }
960: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
961: }
962: PetscViewerFlush(viewer);
963: return(0);
964: }
968: static PetscErrorCode MatView_SeqDense_Binary(Mat A,PetscViewer viewer)
969: {
970: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
971: PetscErrorCode ierr;
972: int fd;
973: PetscInt ict,j,n = A->cmap->n,m = A->rmap->n,i,*col_lens,nz = m*n;
974: PetscScalar *v,*anonz,*vals;
975: PetscViewerFormat format;
978: PetscViewerBinaryGetDescriptor(viewer,&fd);
980: PetscViewerGetFormat(viewer,&format);
981: if (format == PETSC_VIEWER_NATIVE) {
982: /* store the matrix as a dense matrix */
983: PetscMalloc1(4,&col_lens);
985: col_lens[0] = MAT_FILE_CLASSID;
986: col_lens[1] = m;
987: col_lens[2] = n;
988: col_lens[3] = MATRIX_BINARY_FORMAT_DENSE;
990: PetscBinaryWrite(fd,col_lens,4,PETSC_INT,PETSC_TRUE);
991: PetscFree(col_lens);
993: /* write out matrix, by rows */
994: PetscMalloc1((m*n+1),&vals);
995: v = a->v;
996: for (j=0; j<n; j++) {
997: for (i=0; i<m; i++) {
998: vals[j + i*n] = *v++;
999: }
1000: }
1001: PetscBinaryWrite(fd,vals,n*m,PETSC_SCALAR,PETSC_FALSE);
1002: PetscFree(vals);
1003: } else {
1004: PetscMalloc1((4+nz),&col_lens);
1006: col_lens[0] = MAT_FILE_CLASSID;
1007: col_lens[1] = m;
1008: col_lens[2] = n;
1009: col_lens[3] = nz;
1011: /* store lengths of each row and write (including header) to file */
1012: for (i=0; i<m; i++) col_lens[4+i] = n;
1013: PetscBinaryWrite(fd,col_lens,4+m,PETSC_INT,PETSC_TRUE);
1015: /* Possibly should write in smaller increments, not whole matrix at once? */
1016: /* store column indices (zero start index) */
1017: ict = 0;
1018: for (i=0; i<m; i++) {
1019: for (j=0; j<n; j++) col_lens[ict++] = j;
1020: }
1021: PetscBinaryWrite(fd,col_lens,nz,PETSC_INT,PETSC_FALSE);
1022: PetscFree(col_lens);
1024: /* store nonzero values */
1025: PetscMalloc1((nz+1),&anonz);
1026: ict = 0;
1027: for (i=0; i<m; i++) {
1028: v = a->v + i;
1029: for (j=0; j<n; j++) {
1030: anonz[ict++] = *v; v += a->lda;
1031: }
1032: }
1033: PetscBinaryWrite(fd,anonz,nz,PETSC_SCALAR,PETSC_FALSE);
1034: PetscFree(anonz);
1035: }
1036: return(0);
1037: }
1039: #include <petscdraw.h>
1042: PetscErrorCode MatView_SeqDense_Draw_Zoom(PetscDraw draw,void *Aa)
1043: {
1044: Mat A = (Mat) Aa;
1045: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1046: PetscErrorCode ierr;
1047: PetscInt m = A->rmap->n,n = A->cmap->n,color,i,j;
1048: PetscScalar *v = a->v;
1049: PetscViewer viewer;
1050: PetscDraw popup;
1051: PetscReal xl,yl,xr,yr,x_l,x_r,y_l,y_r,scale,maxv = 0.0;
1052: PetscViewerFormat format;
1055: PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
1056: PetscViewerGetFormat(viewer,&format);
1057: PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
1059: /* Loop over matrix elements drawing boxes */
1060: if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1061: /* Blue for negative and Red for positive */
1062: color = PETSC_DRAW_BLUE;
1063: for (j = 0; j < n; j++) {
1064: x_l = j;
1065: x_r = x_l + 1.0;
1066: for (i = 0; i < m; i++) {
1067: y_l = m - i - 1.0;
1068: y_r = y_l + 1.0;
1069: if (PetscRealPart(v[j*m+i]) > 0.) {
1070: color = PETSC_DRAW_RED;
1071: } else if (PetscRealPart(v[j*m+i]) < 0.) {
1072: color = PETSC_DRAW_BLUE;
1073: } else {
1074: continue;
1075: }
1076: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1077: }
1078: }
1079: } else {
1080: /* use contour shading to indicate magnitude of values */
1081: /* first determine max of all nonzero values */
1082: for (i = 0; i < m*n; i++) {
1083: if (PetscAbsScalar(v[i]) > maxv) maxv = PetscAbsScalar(v[i]);
1084: }
1085: scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
1086: PetscDrawGetPopup(draw,&popup);
1087: if (popup) {PetscDrawScalePopup(popup,0.0,maxv);}
1088: for (j = 0; j < n; j++) {
1089: x_l = j;
1090: x_r = x_l + 1.0;
1091: for (i = 0; i < m; i++) {
1092: y_l = m - i - 1.0;
1093: y_r = y_l + 1.0;
1094: color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(v[j*m+i]));
1095: PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1096: }
1097: }
1098: }
1099: return(0);
1100: }
1104: PetscErrorCode MatView_SeqDense_Draw(Mat A,PetscViewer viewer)
1105: {
1106: PetscDraw draw;
1107: PetscBool isnull;
1108: PetscReal xr,yr,xl,yl,h,w;
1112: PetscViewerDrawGetDraw(viewer,0,&draw);
1113: PetscDrawIsNull(draw,&isnull);
1114: if (isnull) return(0);
1116: PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1117: xr = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
1118: xr += w; yr += h; xl = -w; yl = -h;
1119: PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1120: PetscDrawZoom(draw,MatView_SeqDense_Draw_Zoom,A);
1121: PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1122: return(0);
1123: }
1127: PetscErrorCode MatView_SeqDense(Mat A,PetscViewer viewer)
1128: {
1130: PetscBool iascii,isbinary,isdraw;
1133: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1134: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1135: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1137: if (iascii) {
1138: MatView_SeqDense_ASCII(A,viewer);
1139: } else if (isbinary) {
1140: MatView_SeqDense_Binary(A,viewer);
1141: } else if (isdraw) {
1142: MatView_SeqDense_Draw(A,viewer);
1143: }
1144: return(0);
1145: }
1149: PetscErrorCode MatDestroy_SeqDense(Mat mat)
1150: {
1151: Mat_SeqDense *l = (Mat_SeqDense*)mat->data;
1155: #if defined(PETSC_USE_LOG)
1156: PetscLogObjectState((PetscObject)mat,"Rows %D Cols %D",mat->rmap->n,mat->cmap->n);
1157: #endif
1158: PetscFree(l->pivots);
1159: if (!l->user_alloc) {PetscFree(l->v);}
1160: PetscFree(mat->data);
1162: PetscObjectChangeTypeName((PetscObject)mat,0);
1163: PetscObjectComposeFunction((PetscObject)mat,"MatDenseGetArray_C",NULL);
1164: PetscObjectComposeFunction((PetscObject)mat,"MatDenseRestoreArray_C",NULL);
1165: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_seqdense_seqaij_C",NULL);
1166: #if defined(PETSC_HAVE_ELEMENTAL)
1167: PetscObjectComposeFunction((PetscObject)mat,"MatConvert_seqdense_elemental_C",NULL);
1168: #endif
1169: PetscObjectComposeFunction((PetscObject)mat,"MatGetFactor_petsc_C",NULL);
1170: PetscObjectComposeFunction((PetscObject)mat,"MatSeqDenseSetPreallocation_C",NULL);
1171: PetscObjectComposeFunction((PetscObject)mat,"MatMatMult_seqaij_seqdense_C",NULL);
1172: PetscObjectComposeFunction((PetscObject)mat,"MatMatMultSymbolic_seqaij_seqdense_C",NULL);
1173: PetscObjectComposeFunction((PetscObject)mat,"MatMatMultNumeric_seqaij_seqdense_C",NULL);
1174: PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMult_seqaij_seqdense_C",NULL);
1175: PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultSymbolic_seqaij_seqdense_C",NULL);
1176: PetscObjectComposeFunction((PetscObject)mat,"MatTransposeMatMultNumeric_seqaij_seqdense_C",NULL);
1177: return(0);
1178: }
1182: PetscErrorCode MatTranspose_SeqDense(Mat A,MatReuse reuse,Mat *matout)
1183: {
1184: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1186: PetscInt k,j,m,n,M;
1187: PetscScalar *v,tmp;
1190: v = mat->v; m = A->rmap->n; M = mat->lda; n = A->cmap->n;
1191: if (reuse == MAT_REUSE_MATRIX && *matout == A) { /* in place transpose */
1192: if (m != n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can not transpose non-square matrix in place");
1193: else {
1194: for (j=0; j<m; j++) {
1195: for (k=0; k<j; k++) {
1196: tmp = v[j + k*M];
1197: v[j + k*M] = v[k + j*M];
1198: v[k + j*M] = tmp;
1199: }
1200: }
1201: }
1202: } else { /* out-of-place transpose */
1203: Mat tmat;
1204: Mat_SeqDense *tmatd;
1205: PetscScalar *v2;
1207: if (reuse == MAT_INITIAL_MATRIX) {
1208: MatCreate(PetscObjectComm((PetscObject)A),&tmat);
1209: MatSetSizes(tmat,A->cmap->n,A->rmap->n,A->cmap->n,A->rmap->n);
1210: MatSetType(tmat,((PetscObject)A)->type_name);
1211: MatSeqDenseSetPreallocation(tmat,NULL);
1212: } else {
1213: tmat = *matout;
1214: }
1215: tmatd = (Mat_SeqDense*)tmat->data;
1216: v = mat->v; v2 = tmatd->v;
1217: for (j=0; j<n; j++) {
1218: for (k=0; k<m; k++) v2[j + k*n] = v[k + j*M];
1219: }
1220: MatAssemblyBegin(tmat,MAT_FINAL_ASSEMBLY);
1221: MatAssemblyEnd(tmat,MAT_FINAL_ASSEMBLY);
1222: *matout = tmat;
1223: }
1224: return(0);
1225: }
1229: PetscErrorCode MatEqual_SeqDense(Mat A1,Mat A2,PetscBool *flg)
1230: {
1231: Mat_SeqDense *mat1 = (Mat_SeqDense*)A1->data;
1232: Mat_SeqDense *mat2 = (Mat_SeqDense*)A2->data;
1233: PetscInt i,j;
1234: PetscScalar *v1,*v2;
1237: if (A1->rmap->n != A2->rmap->n) {*flg = PETSC_FALSE; return(0);}
1238: if (A1->cmap->n != A2->cmap->n) {*flg = PETSC_FALSE; return(0);}
1239: for (i=0; i<A1->rmap->n; i++) {
1240: v1 = mat1->v+i; v2 = mat2->v+i;
1241: for (j=0; j<A1->cmap->n; j++) {
1242: if (*v1 != *v2) {*flg = PETSC_FALSE; return(0);}
1243: v1 += mat1->lda; v2 += mat2->lda;
1244: }
1245: }
1246: *flg = PETSC_TRUE;
1247: return(0);
1248: }
1252: PetscErrorCode MatGetDiagonal_SeqDense(Mat A,Vec v)
1253: {
1254: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1256: PetscInt i,n,len;
1257: PetscScalar *x,zero = 0.0;
1260: VecSet(v,zero);
1261: VecGetSize(v,&n);
1262: VecGetArray(v,&x);
1263: len = PetscMin(A->rmap->n,A->cmap->n);
1264: if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming mat and vec");
1265: for (i=0; i<len; i++) {
1266: x[i] = mat->v[i*mat->lda + i];
1267: }
1268: VecRestoreArray(v,&x);
1269: return(0);
1270: }
1274: PetscErrorCode MatDiagonalScale_SeqDense(Mat A,Vec ll,Vec rr)
1275: {
1276: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1277: PetscScalar *l,*r,x,*v;
1279: PetscInt i,j,m = A->rmap->n,n = A->cmap->n;
1282: if (ll) {
1283: VecGetSize(ll,&m);
1284: VecGetArray(ll,&l);
1285: if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vec wrong size");
1286: for (i=0; i<m; i++) {
1287: x = l[i];
1288: v = mat->v + i;
1289: for (j=0; j<n; j++) { (*v) *= x; v+= m;}
1290: }
1291: VecRestoreArray(ll,&l);
1292: PetscLogFlops(n*m);
1293: }
1294: if (rr) {
1295: VecGetSize(rr,&n);
1296: VecGetArray(rr,&r);
1297: if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vec wrong size");
1298: for (i=0; i<n; i++) {
1299: x = r[i];
1300: v = mat->v + i*m;
1301: for (j=0; j<m; j++) (*v++) *= x;
1302: }
1303: VecRestoreArray(rr,&r);
1304: PetscLogFlops(n*m);
1305: }
1306: return(0);
1307: }
1311: PetscErrorCode MatNorm_SeqDense(Mat A,NormType type,PetscReal *nrm)
1312: {
1313: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1314: PetscScalar *v = mat->v;
1315: PetscReal sum = 0.0;
1316: PetscInt lda =mat->lda,m=A->rmap->n,i,j;
1320: if (type == NORM_FROBENIUS) {
1321: if (lda>m) {
1322: for (j=0; j<A->cmap->n; j++) {
1323: v = mat->v+j*lda;
1324: for (i=0; i<m; i++) {
1325: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1326: }
1327: }
1328: } else {
1329: for (i=0; i<A->cmap->n*A->rmap->n; i++) {
1330: sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1331: }
1332: }
1333: *nrm = PetscSqrtReal(sum);
1334: PetscLogFlops(2.0*A->cmap->n*A->rmap->n);
1335: } else if (type == NORM_1) {
1336: *nrm = 0.0;
1337: for (j=0; j<A->cmap->n; j++) {
1338: v = mat->v + j*mat->lda;
1339: sum = 0.0;
1340: for (i=0; i<A->rmap->n; i++) {
1341: sum += PetscAbsScalar(*v); v++;
1342: }
1343: if (sum > *nrm) *nrm = sum;
1344: }
1345: PetscLogFlops(A->cmap->n*A->rmap->n);
1346: } else if (type == NORM_INFINITY) {
1347: *nrm = 0.0;
1348: for (j=0; j<A->rmap->n; j++) {
1349: v = mat->v + j;
1350: sum = 0.0;
1351: for (i=0; i<A->cmap->n; i++) {
1352: sum += PetscAbsScalar(*v); v += mat->lda;
1353: }
1354: if (sum > *nrm) *nrm = sum;
1355: }
1356: PetscLogFlops(A->cmap->n*A->rmap->n);
1357: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No two norm");
1358: return(0);
1359: }
1363: PetscErrorCode MatSetOption_SeqDense(Mat A,MatOption op,PetscBool flg)
1364: {
1365: Mat_SeqDense *aij = (Mat_SeqDense*)A->data;
1369: switch (op) {
1370: case MAT_ROW_ORIENTED:
1371: aij->roworiented = flg;
1372: break;
1373: case MAT_NEW_NONZERO_LOCATIONS:
1374: case MAT_NEW_NONZERO_LOCATION_ERR:
1375: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1376: case MAT_NEW_DIAGONALS:
1377: case MAT_KEEP_NONZERO_PATTERN:
1378: case MAT_IGNORE_OFF_PROC_ENTRIES:
1379: case MAT_USE_HASH_TABLE:
1380: case MAT_IGNORE_LOWER_TRIANGULAR:
1381: PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1382: break;
1383: case MAT_SPD:
1384: case MAT_SYMMETRIC:
1385: case MAT_STRUCTURALLY_SYMMETRIC:
1386: case MAT_HERMITIAN:
1387: case MAT_SYMMETRY_ETERNAL:
1388: /* These options are handled directly by MatSetOption() */
1389: break;
1390: default:
1391: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %s",MatOptions[op]);
1392: }
1393: return(0);
1394: }
1398: PetscErrorCode MatZeroEntries_SeqDense(Mat A)
1399: {
1400: Mat_SeqDense *l = (Mat_SeqDense*)A->data;
1402: PetscInt lda=l->lda,m=A->rmap->n,j;
1405: if (lda>m) {
1406: for (j=0; j<A->cmap->n; j++) {
1407: PetscMemzero(l->v+j*lda,m*sizeof(PetscScalar));
1408: }
1409: } else {
1410: PetscMemzero(l->v,A->rmap->n*A->cmap->n*sizeof(PetscScalar));
1411: }
1412: return(0);
1413: }
1417: PetscErrorCode MatZeroRows_SeqDense(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1418: {
1419: PetscErrorCode ierr;
1420: Mat_SeqDense *l = (Mat_SeqDense*)A->data;
1421: PetscInt m = l->lda, n = A->cmap->n, i,j;
1422: PetscScalar *slot,*bb;
1423: const PetscScalar *xx;
1426: #if defined(PETSC_USE_DEBUG)
1427: for (i=0; i<N; i++) {
1428: if (rows[i] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row requested to be zeroed");
1429: if (rows[i] >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D requested to be zeroed greater than or equal number of rows %D",rows[i],A->rmap->n);
1430: }
1431: #endif
1433: /* fix right hand side if needed */
1434: if (x && b) {
1435: VecGetArrayRead(x,&xx);
1436: VecGetArray(b,&bb);
1437: for (i=0; i<N; i++) bb[rows[i]] = diag*xx[rows[i]];
1438: VecRestoreArrayRead(x,&xx);
1439: VecRestoreArray(b,&bb);
1440: }
1442: for (i=0; i<N; i++) {
1443: slot = l->v + rows[i];
1444: for (j=0; j<n; j++) { *slot = 0.0; slot += m;}
1445: }
1446: if (diag != 0.0) {
1447: if (A->rmap->n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only coded for square matrices");
1448: for (i=0; i<N; i++) {
1449: slot = l->v + (m+1)*rows[i];
1450: *slot = diag;
1451: }
1452: }
1453: return(0);
1454: }
1458: PetscErrorCode MatDenseGetArray_SeqDense(Mat A,PetscScalar *array[])
1459: {
1460: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1463: if (mat->lda != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get array for Dense matrices with LDA different from number of rows");
1464: *array = mat->v;
1465: return(0);
1466: }
1470: PetscErrorCode MatDenseRestoreArray_SeqDense(Mat A,PetscScalar *array[])
1471: {
1473: *array = 0; /* user cannot accidently use the array later */
1474: return(0);
1475: }
1479: /*@C
1480: MatDenseGetArray - gives access to the array where the data for a SeqDense matrix is stored
1482: Not Collective
1484: Input Parameter:
1485: . mat - a MATSEQDENSE or MATMPIDENSE matrix
1487: Output Parameter:
1488: . array - pointer to the data
1490: Level: intermediate
1492: .seealso: MatDenseRestoreArray()
1493: @*/
1494: PetscErrorCode MatDenseGetArray(Mat A,PetscScalar **array)
1495: {
1499: PetscUseMethod(A,"MatDenseGetArray_C",(Mat,PetscScalar**),(A,array));
1500: return(0);
1501: }
1505: /*@C
1506: MatDenseRestoreArray - returns access to the array where the data for a dense matrix is stored obtained by MatDenseGetArray()
1508: Not Collective
1510: Input Parameters:
1511: . mat - a MATSEQDENSE or MATMPIDENSE matrix
1512: . array - pointer to the data
1514: Level: intermediate
1516: .seealso: MatDenseGetArray()
1517: @*/
1518: PetscErrorCode MatDenseRestoreArray(Mat A,PetscScalar **array)
1519: {
1523: PetscUseMethod(A,"MatDenseRestoreArray_C",(Mat,PetscScalar**),(A,array));
1524: return(0);
1525: }
1529: static PetscErrorCode MatGetSubMatrix_SeqDense(Mat A,IS isrow,IS iscol,PetscInt cs,MatReuse scall,Mat *B)
1530: {
1531: Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1533: PetscInt i,j,nrows,ncols;
1534: const PetscInt *irow,*icol;
1535: PetscScalar *av,*bv,*v = mat->v;
1536: Mat newmat;
1539: ISGetIndices(isrow,&irow);
1540: ISGetIndices(iscol,&icol);
1541: ISGetLocalSize(isrow,&nrows);
1542: ISGetLocalSize(iscol,&ncols);
1544: /* Check submatrixcall */
1545: if (scall == MAT_REUSE_MATRIX) {
1546: PetscInt n_cols,n_rows;
1547: MatGetSize(*B,&n_rows,&n_cols);
1548: if (n_rows != nrows || n_cols != ncols) {
1549: /* resize the result matrix to match number of requested rows/columns */
1550: MatSetSizes(*B,nrows,ncols,nrows,ncols);
1551: }
1552: newmat = *B;
1553: } else {
1554: /* Create and fill new matrix */
1555: MatCreate(PetscObjectComm((PetscObject)A),&newmat);
1556: MatSetSizes(newmat,nrows,ncols,nrows,ncols);
1557: MatSetType(newmat,((PetscObject)A)->type_name);
1558: MatSeqDenseSetPreallocation(newmat,NULL);
1559: }
1561: /* Now extract the data pointers and do the copy,column at a time */
1562: bv = ((Mat_SeqDense*)newmat->data)->v;
1564: for (i=0; i<ncols; i++) {
1565: av = v + mat->lda*icol[i];
1566: for (j=0; j<nrows; j++) *bv++ = av[irow[j]];
1567: }
1569: /* Assemble the matrices so that the correct flags are set */
1570: MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
1571: MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
1573: /* Free work space */
1574: ISRestoreIndices(isrow,&irow);
1575: ISRestoreIndices(iscol,&icol);
1576: *B = newmat;
1577: return(0);
1578: }
1582: PetscErrorCode MatGetSubMatrices_SeqDense(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1583: {
1585: PetscInt i;
1588: if (scall == MAT_INITIAL_MATRIX) {
1589: PetscMalloc1((n+1),B);
1590: }
1592: for (i=0; i<n; i++) {
1593: MatGetSubMatrix_SeqDense(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
1594: }
1595: return(0);
1596: }
1600: PetscErrorCode MatAssemblyBegin_SeqDense(Mat mat,MatAssemblyType mode)
1601: {
1603: return(0);
1604: }
1608: PetscErrorCode MatAssemblyEnd_SeqDense(Mat mat,MatAssemblyType mode)
1609: {
1611: return(0);
1612: }
1616: PetscErrorCode MatCopy_SeqDense(Mat A,Mat B,MatStructure str)
1617: {
1618: Mat_SeqDense *a = (Mat_SeqDense*)A->data,*b = (Mat_SeqDense*)B->data;
1620: PetscInt lda1=a->lda,lda2=b->lda, m=A->rmap->n,n=A->cmap->n, j;
1623: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1624: if (A->ops->copy != B->ops->copy) {
1625: MatCopy_Basic(A,B,str);
1626: return(0);
1627: }
1628: if (m != B->rmap->n || n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"size(B) != size(A)");
1629: if (lda1>m || lda2>m) {
1630: for (j=0; j<n; j++) {
1631: PetscMemcpy(b->v+j*lda2,a->v+j*lda1,m*sizeof(PetscScalar));
1632: }
1633: } else {
1634: PetscMemcpy(b->v,a->v,A->rmap->n*A->cmap->n*sizeof(PetscScalar));
1635: }
1636: return(0);
1637: }
1641: PetscErrorCode MatSetUp_SeqDense(Mat A)
1642: {
1646: MatSeqDenseSetPreallocation(A,0);
1647: return(0);
1648: }
1652: static PetscErrorCode MatConjugate_SeqDense(Mat A)
1653: {
1654: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1655: PetscInt i,nz = A->rmap->n*A->cmap->n;
1656: PetscScalar *aa = a->v;
1659: for (i=0; i<nz; i++) aa[i] = PetscConj(aa[i]);
1660: return(0);
1661: }
1665: static PetscErrorCode MatRealPart_SeqDense(Mat A)
1666: {
1667: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1668: PetscInt i,nz = A->rmap->n*A->cmap->n;
1669: PetscScalar *aa = a->v;
1672: for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1673: return(0);
1674: }
1678: static PetscErrorCode MatImaginaryPart_SeqDense(Mat A)
1679: {
1680: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1681: PetscInt i,nz = A->rmap->n*A->cmap->n;
1682: PetscScalar *aa = a->v;
1685: for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1686: return(0);
1687: }
1689: /* ----------------------------------------------------------------*/
1692: PetscErrorCode MatMatMult_SeqDense_SeqDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1693: {
1697: if (scall == MAT_INITIAL_MATRIX) {
1698: PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
1699: MatMatMultSymbolic_SeqDense_SeqDense(A,B,fill,C);
1700: PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
1701: }
1702: PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
1703: MatMatMultNumeric_SeqDense_SeqDense(A,B,*C);
1704: PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
1705: return(0);
1706: }
1710: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqDense(Mat A,Mat B,PetscReal fill,Mat *C)
1711: {
1713: PetscInt m=A->rmap->n,n=B->cmap->n;
1714: Mat Cmat;
1717: if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
1718: MatCreate(PETSC_COMM_SELF,&Cmat);
1719: MatSetSizes(Cmat,m,n,m,n);
1720: MatSetType(Cmat,MATSEQDENSE);
1721: MatSeqDenseSetPreallocation(Cmat,NULL);
1723: *C = Cmat;
1724: return(0);
1725: }
1729: PetscErrorCode MatMatMultNumeric_SeqDense_SeqDense(Mat A,Mat B,Mat C)
1730: {
1731: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1732: Mat_SeqDense *b = (Mat_SeqDense*)B->data;
1733: Mat_SeqDense *c = (Mat_SeqDense*)C->data;
1734: PetscBLASInt m,n,k;
1735: PetscScalar _DOne=1.0,_DZero=0.0;
1739: PetscBLASIntCast(A->rmap->n,&m);
1740: PetscBLASIntCast(B->cmap->n,&n);
1741: PetscBLASIntCast(A->cmap->n,&k);
1742: PetscStackCallBLAS("BLASgemv",BLASgemm_("N","N",&m,&n,&k,&_DOne,a->v,&a->lda,b->v,&b->lda,&_DZero,c->v,&c->lda));
1743: return(0);
1744: }
1748: PetscErrorCode MatTransposeMatMult_SeqDense_SeqDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1749: {
1753: if (scall == MAT_INITIAL_MATRIX) {
1754: PetscLogEventBegin(MAT_TransposeMatMultSymbolic,A,B,0,0);
1755: MatTransposeMatMultSymbolic_SeqDense_SeqDense(A,B,fill,C);
1756: PetscLogEventEnd(MAT_TransposeMatMultSymbolic,A,B,0,0);
1757: }
1758: PetscLogEventBegin(MAT_TransposeMatMultNumeric,A,B,0,0);
1759: MatTransposeMatMultNumeric_SeqDense_SeqDense(A,B,*C);
1760: PetscLogEventEnd(MAT_TransposeMatMultNumeric,A,B,0,0);
1761: return(0);
1762: }
1766: PetscErrorCode MatTransposeMatMultSymbolic_SeqDense_SeqDense(Mat A,Mat B,PetscReal fill,Mat *C)
1767: {
1769: PetscInt m=A->cmap->n,n=B->cmap->n;
1770: Mat Cmat;
1773: if (A->rmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->rmap->n %d != B->rmap->n %d\n",A->rmap->n,B->rmap->n);
1774: MatCreate(PETSC_COMM_SELF,&Cmat);
1775: MatSetSizes(Cmat,m,n,m,n);
1776: MatSetType(Cmat,MATSEQDENSE);
1777: MatSeqDenseSetPreallocation(Cmat,NULL);
1779: Cmat->assembled = PETSC_TRUE;
1781: *C = Cmat;
1782: return(0);
1783: }
1787: PetscErrorCode MatTransposeMatMultNumeric_SeqDense_SeqDense(Mat A,Mat B,Mat C)
1788: {
1789: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1790: Mat_SeqDense *b = (Mat_SeqDense*)B->data;
1791: Mat_SeqDense *c = (Mat_SeqDense*)C->data;
1792: PetscBLASInt m,n,k;
1793: PetscScalar _DOne=1.0,_DZero=0.0;
1797: PetscBLASIntCast(A->cmap->n,&m);
1798: PetscBLASIntCast(B->cmap->n,&n);
1799: PetscBLASIntCast(A->rmap->n,&k);
1800: /*
1801: Note the m and n arguments below are the number rows and columns of A', not A!
1802: */
1803: PetscStackCallBLAS("BLASgemv",BLASgemm_("T","N",&m,&n,&k,&_DOne,a->v,&a->lda,b->v,&b->lda,&_DZero,c->v,&c->lda));
1804: return(0);
1805: }
1809: PetscErrorCode MatGetRowMax_SeqDense(Mat A,Vec v,PetscInt idx[])
1810: {
1811: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1813: PetscInt i,j,m = A->rmap->n,n = A->cmap->n,p;
1814: PetscScalar *x;
1815: MatScalar *aa = a->v;
1818: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1820: VecSet(v,0.0);
1821: VecGetArray(v,&x);
1822: VecGetLocalSize(v,&p);
1823: if (p != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1824: for (i=0; i<m; i++) {
1825: x[i] = aa[i]; if (idx) idx[i] = 0;
1826: for (j=1; j<n; j++) {
1827: if (PetscRealPart(x[i]) < PetscRealPart(aa[i+m*j])) {x[i] = aa[i + m*j]; if (idx) idx[i] = j;}
1828: }
1829: }
1830: VecRestoreArray(v,&x);
1831: return(0);
1832: }
1836: PetscErrorCode MatGetRowMaxAbs_SeqDense(Mat A,Vec v,PetscInt idx[])
1837: {
1838: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1840: PetscInt i,j,m = A->rmap->n,n = A->cmap->n,p;
1841: PetscScalar *x;
1842: PetscReal atmp;
1843: MatScalar *aa = a->v;
1846: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1848: VecSet(v,0.0);
1849: VecGetArray(v,&x);
1850: VecGetLocalSize(v,&p);
1851: if (p != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1852: for (i=0; i<m; i++) {
1853: x[i] = PetscAbsScalar(aa[i]);
1854: for (j=1; j<n; j++) {
1855: atmp = PetscAbsScalar(aa[i+m*j]);
1856: if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = j;}
1857: }
1858: }
1859: VecRestoreArray(v,&x);
1860: return(0);
1861: }
1865: PetscErrorCode MatGetRowMin_SeqDense(Mat A,Vec v,PetscInt idx[])
1866: {
1867: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1869: PetscInt i,j,m = A->rmap->n,n = A->cmap->n,p;
1870: PetscScalar *x;
1871: MatScalar *aa = a->v;
1874: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1876: VecSet(v,0.0);
1877: VecGetArray(v,&x);
1878: VecGetLocalSize(v,&p);
1879: if (p != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1880: for (i=0; i<m; i++) {
1881: x[i] = aa[i]; if (idx) idx[i] = 0;
1882: for (j=1; j<n; j++) {
1883: if (PetscRealPart(x[i]) > PetscRealPart(aa[i+m*j])) {x[i] = aa[i + m*j]; if (idx) idx[i] = j;}
1884: }
1885: }
1886: VecRestoreArray(v,&x);
1887: return(0);
1888: }
1892: PetscErrorCode MatGetColumnVector_SeqDense(Mat A,Vec v,PetscInt col)
1893: {
1894: Mat_SeqDense *a = (Mat_SeqDense*)A->data;
1896: PetscScalar *x;
1899: if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1901: VecGetArray(v,&x);
1902: PetscMemcpy(x,a->v+col*a->lda,A->rmap->n*sizeof(PetscScalar));
1903: VecRestoreArray(v,&x);
1904: return(0);
1905: }
1910: PetscErrorCode MatGetColumnNorms_SeqDense(Mat A,NormType type,PetscReal *norms)
1911: {
1913: PetscInt i,j,m,n;
1914: PetscScalar *a;
1917: MatGetSize(A,&m,&n);
1918: PetscMemzero(norms,n*sizeof(PetscReal));
1919: MatDenseGetArray(A,&a);
1920: if (type == NORM_2) {
1921: for (i=0; i<n; i++) {
1922: for (j=0; j<m; j++) {
1923: norms[i] += PetscAbsScalar(a[j]*a[j]);
1924: }
1925: a += m;
1926: }
1927: } else if (type == NORM_1) {
1928: for (i=0; i<n; i++) {
1929: for (j=0; j<m; j++) {
1930: norms[i] += PetscAbsScalar(a[j]);
1931: }
1932: a += m;
1933: }
1934: } else if (type == NORM_INFINITY) {
1935: for (i=0; i<n; i++) {
1936: for (j=0; j<m; j++) {
1937: norms[i] = PetscMax(PetscAbsScalar(a[j]),norms[i]);
1938: }
1939: a += m;
1940: }
1941: } else SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"Unknown NormType");
1942: MatDenseRestoreArray(A,&a);
1943: if (type == NORM_2) {
1944: for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
1945: }
1946: return(0);
1947: }
1951: static PetscErrorCode MatSetRandom_SeqDense(Mat x,PetscRandom rctx)
1952: {
1954: PetscScalar *a;
1955: PetscInt m,n,i;
1958: MatGetSize(x,&m,&n);
1959: MatDenseGetArray(x,&a);
1960: for (i=0; i<m*n; i++) {
1961: PetscRandomGetValue(rctx,a+i);
1962: }
1963: MatDenseRestoreArray(x,&a);
1964: return(0);
1965: }
1968: /* -------------------------------------------------------------------*/
1969: static struct _MatOps MatOps_Values = { MatSetValues_SeqDense,
1970: MatGetRow_SeqDense,
1971: MatRestoreRow_SeqDense,
1972: MatMult_SeqDense,
1973: /* 4*/ MatMultAdd_SeqDense,
1974: MatMultTranspose_SeqDense,
1975: MatMultTransposeAdd_SeqDense,
1976: 0,
1977: 0,
1978: 0,
1979: /* 10*/ 0,
1980: MatLUFactor_SeqDense,
1981: MatCholeskyFactor_SeqDense,
1982: MatSOR_SeqDense,
1983: MatTranspose_SeqDense,
1984: /* 15*/ MatGetInfo_SeqDense,
1985: MatEqual_SeqDense,
1986: MatGetDiagonal_SeqDense,
1987: MatDiagonalScale_SeqDense,
1988: MatNorm_SeqDense,
1989: /* 20*/ MatAssemblyBegin_SeqDense,
1990: MatAssemblyEnd_SeqDense,
1991: MatSetOption_SeqDense,
1992: MatZeroEntries_SeqDense,
1993: /* 24*/ MatZeroRows_SeqDense,
1994: 0,
1995: 0,
1996: 0,
1997: 0,
1998: /* 29*/ MatSetUp_SeqDense,
1999: 0,
2000: 0,
2001: 0,
2002: 0,
2003: /* 34*/ MatDuplicate_SeqDense,
2004: 0,
2005: 0,
2006: 0,
2007: 0,
2008: /* 39*/ MatAXPY_SeqDense,
2009: MatGetSubMatrices_SeqDense,
2010: 0,
2011: MatGetValues_SeqDense,
2012: MatCopy_SeqDense,
2013: /* 44*/ MatGetRowMax_SeqDense,
2014: MatScale_SeqDense,
2015: 0,
2016: 0,
2017: 0,
2018: /* 49*/ MatSetRandom_SeqDense,
2019: 0,
2020: 0,
2021: 0,
2022: 0,
2023: /* 54*/ 0,
2024: 0,
2025: 0,
2026: 0,
2027: 0,
2028: /* 59*/ 0,
2029: MatDestroy_SeqDense,
2030: MatView_SeqDense,
2031: 0,
2032: 0,
2033: /* 64*/ 0,
2034: 0,
2035: 0,
2036: 0,
2037: 0,
2038: /* 69*/ MatGetRowMaxAbs_SeqDense,
2039: 0,
2040: 0,
2041: 0,
2042: 0,
2043: /* 74*/ 0,
2044: 0,
2045: 0,
2046: 0,
2047: 0,
2048: /* 79*/ 0,
2049: 0,
2050: 0,
2051: 0,
2052: /* 83*/ MatLoad_SeqDense,
2053: 0,
2054: MatIsHermitian_SeqDense,
2055: 0,
2056: 0,
2057: 0,
2058: /* 89*/ MatMatMult_SeqDense_SeqDense,
2059: MatMatMultSymbolic_SeqDense_SeqDense,
2060: MatMatMultNumeric_SeqDense_SeqDense,
2061: 0,
2062: 0,
2063: /* 94*/ 0,
2064: 0,
2065: 0,
2066: 0,
2067: 0,
2068: /* 99*/ 0,
2069: 0,
2070: 0,
2071: MatConjugate_SeqDense,
2072: 0,
2073: /*104*/ 0,
2074: MatRealPart_SeqDense,
2075: MatImaginaryPart_SeqDense,
2076: 0,
2077: 0,
2078: /*109*/ MatMatSolve_SeqDense,
2079: 0,
2080: MatGetRowMin_SeqDense,
2081: MatGetColumnVector_SeqDense,
2082: 0,
2083: /*114*/ 0,
2084: 0,
2085: 0,
2086: 0,
2087: 0,
2088: /*119*/ 0,
2089: 0,
2090: 0,
2091: 0,
2092: 0,
2093: /*124*/ 0,
2094: MatGetColumnNorms_SeqDense,
2095: 0,
2096: 0,
2097: 0,
2098: /*129*/ 0,
2099: MatTransposeMatMult_SeqDense_SeqDense,
2100: MatTransposeMatMultSymbolic_SeqDense_SeqDense,
2101: MatTransposeMatMultNumeric_SeqDense_SeqDense,
2102: 0,
2103: /*134*/ 0,
2104: 0,
2105: 0,
2106: 0,
2107: 0,
2108: /*139*/ 0,
2109: 0,
2110: 0
2111: };
2115: /*@C
2116: MatCreateSeqDense - Creates a sequential dense matrix that
2117: is stored in column major order (the usual Fortran 77 manner). Many
2118: of the matrix operations use the BLAS and LAPACK routines.
2120: Collective on MPI_Comm
2122: Input Parameters:
2123: + comm - MPI communicator, set to PETSC_COMM_SELF
2124: . m - number of rows
2125: . n - number of columns
2126: - data - optional location of matrix data in column major order. Set data=NULL for PETSc
2127: to control all matrix memory allocation.
2129: Output Parameter:
2130: . A - the matrix
2132: Notes:
2133: The data input variable is intended primarily for Fortran programmers
2134: who wish to allocate their own matrix memory space. Most users should
2135: set data=NULL.
2137: Level: intermediate
2139: .keywords: dense, matrix, LAPACK, BLAS
2141: .seealso: MatCreate(), MatCreateDense(), MatSetValues()
2142: @*/
2143: PetscErrorCode MatCreateSeqDense(MPI_Comm comm,PetscInt m,PetscInt n,PetscScalar *data,Mat *A)
2144: {
2148: MatCreate(comm,A);
2149: MatSetSizes(*A,m,n,m,n);
2150: MatSetType(*A,MATSEQDENSE);
2151: MatSeqDenseSetPreallocation(*A,data);
2152: return(0);
2153: }
2157: /*@C
2158: MatSeqDenseSetPreallocation - Sets the array used for storing the matrix elements
2160: Collective on MPI_Comm
2162: Input Parameters:
2163: + B - the matrix
2164: - data - the array (or NULL)
2166: Notes:
2167: The data input variable is intended primarily for Fortran programmers
2168: who wish to allocate their own matrix memory space. Most users should
2169: need not call this routine.
2171: Level: intermediate
2173: .keywords: dense, matrix, LAPACK, BLAS
2175: .seealso: MatCreate(), MatCreateDense(), MatSetValues(), MatSeqDenseSetLDA()
2177: @*/
2178: PetscErrorCode MatSeqDenseSetPreallocation(Mat B,PetscScalar data[])
2179: {
2183: PetscTryMethod(B,"MatSeqDenseSetPreallocation_C",(Mat,PetscScalar[]),(B,data));
2184: return(0);
2185: }
2189: PetscErrorCode MatSeqDenseSetPreallocation_SeqDense(Mat B,PetscScalar *data)
2190: {
2191: Mat_SeqDense *b;
2195: B->preallocated = PETSC_TRUE;
2197: PetscLayoutSetUp(B->rmap);
2198: PetscLayoutSetUp(B->cmap);
2200: b = (Mat_SeqDense*)B->data;
2201: b->Mmax = B->rmap->n;
2202: b->Nmax = B->cmap->n;
2203: if (b->lda <= 0 || b->changelda) b->lda = B->rmap->n;
2205: if (!data) { /* petsc-allocated storage */
2206: if (!b->user_alloc) { PetscFree(b->v); }
2207: PetscCalloc1((size_t)b->lda*b->Nmax,&b->v);
2208: PetscLogObjectMemory((PetscObject)B,b->lda*b->Nmax*sizeof(PetscScalar));
2210: b->user_alloc = PETSC_FALSE;
2211: } else { /* user-allocated storage */
2212: if (!b->user_alloc) { PetscFree(b->v); }
2213: b->v = data;
2214: b->user_alloc = PETSC_TRUE;
2215: }
2216: B->assembled = PETSC_TRUE;
2217: return(0);
2218: }
2222: PETSC_EXTERN PetscErrorCode MatConvert_SeqDense_Elemental(Mat A, MatType newtype,MatReuse reuse,Mat *newmat)
2223: {
2225: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for requested operation");
2226: return(0);
2227: }
2231: /*@C
2232: MatSeqDenseSetLDA - Declare the leading dimension of the user-provided array
2234: Input parameter:
2235: + A - the matrix
2236: - lda - the leading dimension
2238: Notes:
2239: This routine is to be used in conjunction with MatSeqDenseSetPreallocation();
2240: it asserts that the preallocation has a leading dimension (the LDA parameter
2241: of Blas and Lapack fame) larger than M, the first dimension of the matrix.
2243: Level: intermediate
2245: .keywords: dense, matrix, LAPACK, BLAS
2247: .seealso: MatCreate(), MatCreateSeqDense(), MatSeqDenseSetPreallocation(), MatSetMaximumSize()
2249: @*/
2250: PetscErrorCode MatSeqDenseSetLDA(Mat B,PetscInt lda)
2251: {
2252: Mat_SeqDense *b = (Mat_SeqDense*)B->data;
2255: if (lda < B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"LDA %D must be at least matrix dimension %D",lda,B->rmap->n);
2256: b->lda = lda;
2257: b->changelda = PETSC_FALSE;
2258: b->Mmax = PetscMax(b->Mmax,lda);
2259: return(0);
2260: }
2262: /*MC
2263: MATSEQDENSE - MATSEQDENSE = "seqdense" - A matrix type to be used for sequential dense matrices.
2265: Options Database Keys:
2266: . -mat_type seqdense - sets the matrix type to "seqdense" during a call to MatSetFromOptions()
2268: Level: beginner
2270: .seealso: MatCreateSeqDense()
2272: M*/
2276: PETSC_EXTERN PetscErrorCode MatCreate_SeqDense(Mat B)
2277: {
2278: Mat_SeqDense *b;
2280: PetscMPIInt size;
2283: MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
2284: if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");
2286: PetscNewLog(B,&b);
2287: PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
2288: B->data = (void*)b;
2290: b->pivots = 0;
2291: b->roworiented = PETSC_TRUE;
2292: b->v = 0;
2293: b->changelda = PETSC_FALSE;
2295: PetscObjectComposeFunction((PetscObject)B,"MatDenseGetArray_C",MatDenseGetArray_SeqDense);
2296: PetscObjectComposeFunction((PetscObject)B,"MatDenseRestoreArray_C",MatDenseRestoreArray_SeqDense);
2297: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqdense_seqaij_C",MatConvert_SeqDense_SeqAIJ);
2298: #if defined(PETSC_HAVE_ELEMENTAL)
2299: PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqdense_elemental_C",MatConvert_SeqDense_Elemental);
2300: #endif
2301: PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqdense_petsc);
2302: PetscObjectComposeFunction((PetscObject)B,"MatSeqDenseSetPreallocation_C",MatSeqDenseSetPreallocation_SeqDense);
2303: PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqaij_seqdense_C",MatMatMult_SeqAIJ_SeqDense);
2304: PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqaij_seqdense_C",MatMatMultSymbolic_SeqAIJ_SeqDense);
2305: PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqaij_seqdense_C",MatMatMultNumeric_SeqAIJ_SeqDense);
2306: PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMult_seqaij_seqdense_C",MatTransposeMatMult_SeqAIJ_SeqDense);
2307: PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMultSymbolic_seqaij_seqdense_C",MatTransposeMatMultSymbolic_SeqAIJ_SeqDense);
2308: PetscObjectComposeFunction((PetscObject)B,"MatTransposeMatMultNumeric_seqaij_seqdense_C",MatTransposeMatMultNumeric_SeqAIJ_SeqDense);
2309: PetscObjectChangeTypeName((PetscObject)B,MATSEQDENSE);
2310: return(0);
2311: }