Actual source code: dense.c

  1: #define PETSCMAT_DLL

  3: /*
  4:      Defines the basic matrix operations for sequential dense.
  5: */

 7:  #include src/mat/impls/dense/seq/dense.h
 8:  #include petscblaslapack.h

 12: PetscErrorCode MatAXPY_SeqDense(Mat Y,PetscScalar alpha,Mat X,MatStructure str)
 13: {
 14:   Mat_SeqDense   *x = (Mat_SeqDense*)X->data,*y = (Mat_SeqDense*)Y->data;
 15:   PetscScalar    oalpha = alpha;
 16:   PetscInt       j;
 17:   PetscBLASInt   N = (PetscBLASInt)X->rmap.n*X->cmap.n,m=(PetscBLASInt)X->rmap.n,ldax = x->lda,lday=y->lda,one = 1;

 21:   if (ldax>m || lday>m) {
 22:     for (j=0; j<X->cmap.n; j++) {
 23:       BLASaxpy_(&m,&oalpha,x->v+j*ldax,&one,y->v+j*lday,&one);
 24:     }
 25:   } else {
 26:     BLASaxpy_(&N,&oalpha,x->v,&one,y->v,&one);
 27:   }
 28:   PetscLogFlops(2*N-1);
 29:   return(0);
 30: }

 34: PetscErrorCode MatGetInfo_SeqDense(Mat A,MatInfoType flag,MatInfo *info)
 35: {
 36:   PetscInt     N = A->rmap.n*A->cmap.n;

 39:   info->rows_global       = (double)A->rmap.n;
 40:   info->columns_global    = (double)A->cmap.n;
 41:   info->rows_local        = (double)A->rmap.n;
 42:   info->columns_local     = (double)A->cmap.n;
 43:   info->block_size        = 1.0;
 44:   info->nz_allocated      = (double)N;
 45:   info->nz_used           = (double)N;
 46:   info->nz_unneeded       = (double)0;
 47:   info->assemblies        = (double)A->num_ass;
 48:   info->mallocs           = 0;
 49:   info->memory            = A->mem;
 50:   info->fill_ratio_given  = 0;
 51:   info->fill_ratio_needed = 0;
 52:   info->factor_mallocs    = 0;
 53:   return(0);
 54: }

 58: PetscErrorCode MatScale_SeqDense(Mat A,PetscScalar alpha)
 59: {
 60:   Mat_SeqDense   *a = (Mat_SeqDense*)A->data;
 61:   PetscScalar    oalpha = alpha;
 62:   PetscBLASInt   one = 1,lda = a->lda,j,nz;

 66:   if (lda>A->rmap.n) {
 67:     nz = (PetscBLASInt)A->rmap.n;
 68:     for (j=0; j<A->cmap.n; j++) {
 69:       BLASscal_(&nz,&oalpha,a->v+j*lda,&one);
 70:     }
 71:   } else {
 72:     nz = (PetscBLASInt)A->rmap.n*A->cmap.n;
 73:     BLASscal_(&nz,&oalpha,a->v,&one);
 74:   }
 75:   PetscLogFlops(nz);
 76:   return(0);
 77: }
 78: 
 79: /* ---------------------------------------------------------------*/
 80: /* COMMENT: I have chosen to hide row permutation in the pivots,
 81:    rather than put it in the Mat->row slot.*/
 84: PetscErrorCode MatLUFactor_SeqDense(Mat A,IS row,IS col,MatFactorInfo *minfo)
 85: {
 86: #if defined(PETSC_MISSING_LAPACK_GETRF) 
 88:   SETERRQ(PETSC_ERR_SUP,"GETRF - Lapack routine is unavailable.");
 89: #else
 90:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
 92:   PetscBLASInt   n = (PetscBLASInt)A->cmap.n,m = (PetscBLASInt)A->rmap.n,info;

 95:   if (!mat->pivots) {
 96:     PetscMalloc((A->rmap.n+1)*sizeof(PetscBLASInt),&mat->pivots);
 97:     PetscLogObjectMemory(A,A->rmap.n*sizeof(PetscBLASInt));
 98:   }
 99:   A->factor = FACTOR_LU;
100:   if (!A->rmap.n || !A->cmap.n) return(0);
101:   LAPACKgetrf_(&m,&n,mat->v,&mat->lda,mat->pivots,&info);
102:   if (info<0) SETERRQ(PETSC_ERR_LIB,"Bad argument to LU factorization");
103:   if (info>0) SETERRQ(PETSC_ERR_MAT_LU_ZRPVT,"Bad LU factorization");
104:   PetscLogFlops((2*A->cmap.n*A->cmap.n*A->cmap.n)/3);
105: #endif
106:   return(0);
107: }

111: PetscErrorCode MatDuplicate_SeqDense(Mat A,MatDuplicateOption cpvalues,Mat *newmat)
112: {
113:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data,*l;
115:   PetscInt       lda = (PetscInt)mat->lda,j,m;
116:   Mat            newi;

119:   MatCreate(A->comm,&newi);
120:   MatSetSizes(newi,A->rmap.n,A->cmap.n,A->rmap.n,A->cmap.n);
121:   MatSetType(newi,A->type_name);
122:   MatSeqDenseSetPreallocation(newi,PETSC_NULL);
123:   if (cpvalues == MAT_COPY_VALUES) {
124:     l = (Mat_SeqDense*)newi->data;
125:     if (lda>A->rmap.n) {
126:       m = A->rmap.n;
127:       for (j=0; j<A->cmap.n; j++) {
128:         PetscMemcpy(l->v+j*m,mat->v+j*lda,m*sizeof(PetscScalar));
129:       }
130:     } else {
131:       PetscMemcpy(l->v,mat->v,A->rmap.n*A->cmap.n*sizeof(PetscScalar));
132:     }
133:   }
134:   newi->assembled = PETSC_TRUE;
135:   *newmat = newi;
136:   return(0);
137: }

141: PetscErrorCode MatLUFactorSymbolic_SeqDense(Mat A,IS row,IS col,MatFactorInfo *info,Mat *fact)
142: {

146:   MatDuplicate_SeqDense(A,MAT_DO_NOT_COPY_VALUES,fact);
147:   return(0);
148: }

152: PetscErrorCode MatLUFactorNumeric_SeqDense(Mat A,MatFactorInfo *info_dummy,Mat *fact)
153: {
154:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data,*l = (Mat_SeqDense*)(*fact)->data;
156:   PetscInt       lda1=mat->lda,lda2=l->lda, m=A->rmap.n,n=A->cmap.n, j;
157:   MatFactorInfo  info;

160:   /* copy the numerical values */
161:   if (lda1>m || lda2>m ) {
162:     for (j=0; j<n; j++) {
163:       PetscMemcpy(l->v+j*lda2,mat->v+j*lda1,m*sizeof(PetscScalar));
164:     }
165:   } else {
166:     PetscMemcpy(l->v,mat->v,A->rmap.n*A->cmap.n*sizeof(PetscScalar));
167:   }
168:   (*fact)->factor = 0;
169:   MatLUFactor(*fact,0,0,&info);
170:   return(0);
171: }

175: PetscErrorCode MatCholeskyFactorSymbolic_SeqDense(Mat A,IS row,MatFactorInfo *info,Mat *fact)
176: {

180:   MatConvert(A,MATSAME,MAT_INITIAL_MATRIX,fact);
181:   return(0);
182: }

186: PetscErrorCode MatCholeskyFactor_SeqDense(Mat A,IS perm,MatFactorInfo *factinfo)
187: {
188: #if defined(PETSC_MISSING_LAPACK_POTRF) 
190:   SETERRQ(PETSC_ERR_SUP,"POTRF - Lapack routine is unavailable.");
191: #else
192:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
194:   PetscBLASInt   n = (PetscBLASInt)A->cmap.n,info;
195: 
197:   PetscFree(mat->pivots);
198:   mat->pivots = 0;

200:   if (!A->rmap.n || !A->cmap.n) return(0);
201:   LAPACKpotrf_("L",&n,mat->v,&mat->lda,&info);
202:   if (info) SETERRQ1(PETSC_ERR_MAT_CH_ZRPVT,"Bad factorization: zero pivot in row %D",(PetscInt)info-1);
203:   A->factor = FACTOR_CHOLESKY;
204:   PetscLogFlops((A->cmap.n*A->cmap.n*A->cmap.n)/3);
205: #endif
206:   return(0);
207: }

211: PetscErrorCode MatCholeskyFactorNumeric_SeqDense(Mat A,MatFactorInfo *info_dummy,Mat *fact)
212: {
214:   MatFactorInfo  info;

217:   info.fill = 1.0;
218:   MatCholeskyFactor_SeqDense(*fact,0,&info);
219:   return(0);
220: }

224: PetscErrorCode MatSolve_SeqDense(Mat A,Vec xx,Vec yy)
225: {
226:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
228:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n, one = 1,info;
229:   PetscScalar    *x,*y;
230: 
232:   VecGetArray(xx,&x);
233:   VecGetArray(yy,&y);
234:   PetscMemcpy(y,x,A->rmap.n*sizeof(PetscScalar));
235:   if (A->factor == FACTOR_LU) {
236: #if defined(PETSC_MISSING_LAPACK_GETRS) 
237:     SETERRQ(PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
238: #else
239:     LAPACKgetrs_("N",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info);
240:     if (info) SETERRQ(PETSC_ERR_LIB,"GETRS - Bad solve");
241: #endif
242:   } else if (A->factor == FACTOR_CHOLESKY){
243: #if defined(PETSC_MISSING_LAPACK_POTRS) 
244:     SETERRQ(PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
245: #else
246:     LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info);
247:     if (info) SETERRQ(PETSC_ERR_LIB,"POTRS Bad solve");
248: #endif
249:   }
250:   else SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Matrix must be factored to solve");
251:   VecRestoreArray(xx,&x);
252:   VecRestoreArray(yy,&y);
253:   PetscLogFlops(2*A->cmap.n*A->cmap.n - A->cmap.n);
254:   return(0);
255: }

259: PetscErrorCode MatSolveTranspose_SeqDense(Mat A,Vec xx,Vec yy)
260: {
261:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
263:   PetscBLASInt   m = (PetscBLASInt) A->rmap.n,one = 1,info;
264:   PetscScalar    *x,*y;
265: 
267:   VecGetArray(xx,&x);
268:   VecGetArray(yy,&y);
269:   PetscMemcpy(y,x,A->rmap.n*sizeof(PetscScalar));
270:   /* assume if pivots exist then use LU; else Cholesky */
271:   if (mat->pivots) {
272: #if defined(PETSC_MISSING_LAPACK_GETRS) 
273:     SETERRQ(PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
274: #else
275:     LAPACKgetrs_("T",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info);
276:     if (info) SETERRQ(PETSC_ERR_LIB,"POTRS - Bad solve");
277: #endif
278:   } else {
279: #if defined(PETSC_MISSING_LAPACK_POTRS) 
280:     SETERRQ(PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
281: #else
282:     LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info);
283:     if (info) SETERRQ(PETSC_ERR_LIB,"POTRS - Bad solve");
284: #endif
285:   }
286:   VecRestoreArray(xx,&x);
287:   VecRestoreArray(yy,&y);
288:   PetscLogFlops(2*A->cmap.n*A->cmap.n - A->cmap.n);
289:   return(0);
290: }

294: PetscErrorCode MatSolveAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
295: {
296:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
298:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n,one = 1,info;
299:   PetscScalar    *x,*y,sone = 1.0;
300:   Vec            tmp = 0;
301: 
303:   VecGetArray(xx,&x);
304:   VecGetArray(yy,&y);
305:   if (!A->rmap.n || !A->cmap.n) return(0);
306:   if (yy == zz) {
307:     VecDuplicate(yy,&tmp);
308:     PetscLogObjectParent(A,tmp);
309:     VecCopy(yy,tmp);
310:   }
311:   PetscMemcpy(y,x,A->rmap.n*sizeof(PetscScalar));
312:   /* assume if pivots exist then use LU; else Cholesky */
313:   if (mat->pivots) {
314: #if defined(PETSC_MISSING_LAPACK_GETRS) 
315:     SETERRQ(PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
316: #else
317:     LAPACKgetrs_("N",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info);
318:     if (info) SETERRQ(PETSC_ERR_LIB,"Bad solve");
319: #endif
320:   } else {
321: #if defined(PETSC_MISSING_LAPACK_POTRS) 
322:     SETERRQ(PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
323: #else
324:     LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info);
325:     if (info) SETERRQ(PETSC_ERR_LIB,"Bad solve");
326: #endif
327:   }
328:   if (tmp) {VecAXPY(yy,sone,tmp); VecDestroy(tmp);}
329:   else     {VecAXPY(yy,sone,zz);}
330:   VecRestoreArray(xx,&x);
331:   VecRestoreArray(yy,&y);
332:   PetscLogFlops(2*A->cmap.n*A->cmap.n);
333:   return(0);
334: }

338: PetscErrorCode MatSolveTransposeAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
339: {
340:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
342:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n,one = 1,info;
343:   PetscScalar    *x,*y,sone = 1.0;
344:   Vec            tmp;
345: 
347:   if (!A->rmap.n || !A->cmap.n) return(0);
348:   VecGetArray(xx,&x);
349:   VecGetArray(yy,&y);
350:   if (yy == zz) {
351:     VecDuplicate(yy,&tmp);
352:     PetscLogObjectParent(A,tmp);
353:     VecCopy(yy,tmp);
354:   }
355:   PetscMemcpy(y,x,A->rmap.n*sizeof(PetscScalar));
356:   /* assume if pivots exist then use LU; else Cholesky */
357:   if (mat->pivots) {
358: #if defined(PETSC_MISSING_LAPACK_GETRS) 
359:     SETERRQ(PETSC_ERR_SUP,"GETRS - Lapack routine is unavailable.");
360: #else
361:     LAPACKgetrs_("T",&m,&one,mat->v,&mat->lda,mat->pivots,y,&m,&info);
362:     if (info) SETERRQ(PETSC_ERR_LIB,"Bad solve");
363: #endif
364:   } else {
365: #if defined(PETSC_MISSING_LAPACK_POTRS) 
366:     SETERRQ(PETSC_ERR_SUP,"POTRS - Lapack routine is unavailable.");
367: #else
368:     LAPACKpotrs_("L",&m,&one,mat->v,&mat->lda,y,&m,&info);
369:     if (info) SETERRQ(PETSC_ERR_LIB,"Bad solve");
370: #endif
371:   }
372:   if (tmp) {
373:     VecAXPY(yy,sone,tmp);
374:     VecDestroy(tmp);
375:   } else {
376:     VecAXPY(yy,sone,zz);
377:   }
378:   VecRestoreArray(xx,&x);
379:   VecRestoreArray(yy,&y);
380:   PetscLogFlops(2*A->cmap.n*A->cmap.n);
381:   return(0);
382: }
383: /* ------------------------------------------------------------------*/
386: PetscErrorCode MatRelax_SeqDense(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal shift,PetscInt its,PetscInt lits,Vec xx)
387: {
388:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
389:   PetscScalar    *x,*b,*v = mat->v,zero = 0.0,xt;
391:   PetscInt       m = A->rmap.n,i;
392: #if !defined(PETSC_USE_COMPLEX)
393:   PetscBLASInt   bm = (PetscBLASInt)m, o = 1;
394: #endif

397:   if (flag & SOR_ZERO_INITIAL_GUESS) {
398:     /* this is a hack fix, should have another version without the second BLASdot */
399:     VecSet(xx,zero);
400:   }
401:   VecGetArray(xx,&x);
402:   VecGetArray(bb,&b);
403:   its  = its*lits;
404:   if (its <= 0) SETERRQ2(PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
405:   while (its--) {
406:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
407:       for (i=0; i<m; i++) {
408: #if defined(PETSC_USE_COMPLEX)
409:         /* cannot use BLAS dot for complex because compiler/linker is 
410:            not happy about returning a double complex */
411:         PetscInt         _i;
412:         PetscScalar sum = b[i];
413:         for (_i=0; _i<m; _i++) {
414:           sum -= PetscConj(v[i+_i*m])*x[_i];
415:         }
416:         xt = sum;
417: #else
418:         xt = b[i] - BLASdot_(&bm,v+i,&bm,x,&o);
419: #endif
420:         x[i] = (1. - omega)*x[i] + omega*(xt+v[i + i*m]*x[i])/(v[i + i*m]+shift);
421:       }
422:     }
423:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
424:       for (i=m-1; i>=0; i--) {
425: #if defined(PETSC_USE_COMPLEX)
426:         /* cannot use BLAS dot for complex because compiler/linker is 
427:            not happy about returning a double complex */
428:         PetscInt         _i;
429:         PetscScalar sum = b[i];
430:         for (_i=0; _i<m; _i++) {
431:           sum -= PetscConj(v[i+_i*m])*x[_i];
432:         }
433:         xt = sum;
434: #else
435:         xt = b[i] - BLASdot_(&bm,v+i,&bm,x,&o);
436: #endif
437:         x[i] = (1. - omega)*x[i] + omega*(xt+v[i + i*m]*x[i])/(v[i + i*m]+shift);
438:       }
439:     }
440:   }
441:   VecRestoreArray(bb,&b);
442:   VecRestoreArray(xx,&x);
443:   return(0);
444: }

446: /* -----------------------------------------------------------------*/
449: PetscErrorCode MatMultTranspose_SeqDense(Mat A,Vec xx,Vec yy)
450: {
451:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
452:   PetscScalar    *v = mat->v,*x,*y;
454:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n, n = (PetscBLASInt)A->cmap.n,_One=1;
455:   PetscScalar    _DOne=1.0,_DZero=0.0;

458:   if (!A->rmap.n || !A->cmap.n) return(0);
459:   VecGetArray(xx,&x);
460:   VecGetArray(yy,&y);
461:   BLASgemv_("T",&m,&n,&_DOne,v,&mat->lda,x,&_One,&_DZero,y,&_One);
462:   VecRestoreArray(xx,&x);
463:   VecRestoreArray(yy,&y);
464:   PetscLogFlops(2*A->rmap.n*A->cmap.n - A->cmap.n);
465:   return(0);
466: }

470: PetscErrorCode MatMult_SeqDense(Mat A,Vec xx,Vec yy)
471: {
472:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
473:   PetscScalar    *v = mat->v,*x,*y,_DOne=1.0,_DZero=0.0;
475:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n, n = (PetscBLASInt)A->cmap.n, _One=1;

478:   if (!A->rmap.n || !A->cmap.n) return(0);
479:   VecGetArray(xx,&x);
480:   VecGetArray(yy,&y);
481:   BLASgemv_("N",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DZero,y,&_One);
482:   VecRestoreArray(xx,&x);
483:   VecRestoreArray(yy,&y);
484:   PetscLogFlops(2*A->rmap.n*A->cmap.n - A->rmap.n);
485:   return(0);
486: }

490: PetscErrorCode MatMultAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
491: {
492:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
493:   PetscScalar    *v = mat->v,*x,*y,_DOne=1.0;
495:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n, n = (PetscBLASInt)A->cmap.n, _One=1;

498:   if (!A->rmap.n || !A->cmap.n) return(0);
499:   if (zz != yy) {VecCopy(zz,yy);}
500:   VecGetArray(xx,&x);
501:   VecGetArray(yy,&y);
502:   BLASgemv_("N",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DOne,y,&_One);
503:   VecRestoreArray(xx,&x);
504:   VecRestoreArray(yy,&y);
505:   PetscLogFlops(2*A->rmap.n*A->cmap.n);
506:   return(0);
507: }

511: PetscErrorCode MatMultTransposeAdd_SeqDense(Mat A,Vec xx,Vec zz,Vec yy)
512: {
513:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
514:   PetscScalar    *v = mat->v,*x,*y;
516:   PetscBLASInt   m = (PetscBLASInt)A->rmap.n, n = (PetscBLASInt)A->cmap.n, _One=1;
517:   PetscScalar    _DOne=1.0;

520:   if (!A->rmap.n || !A->cmap.n) return(0);
521:   if (zz != yy) {VecCopy(zz,yy);}
522:   VecGetArray(xx,&x);
523:   VecGetArray(yy,&y);
524:   BLASgemv_("T",&m,&n,&_DOne,v,&(mat->lda),x,&_One,&_DOne,y,&_One);
525:   VecRestoreArray(xx,&x);
526:   VecRestoreArray(yy,&y);
527:   PetscLogFlops(2*A->rmap.n*A->cmap.n);
528:   return(0);
529: }

531: /* -----------------------------------------------------------------*/
534: PetscErrorCode MatGetRow_SeqDense(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **vals)
535: {
536:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
537:   PetscScalar    *v;
539:   PetscInt       i;
540: 
542:   *ncols = A->cmap.n;
543:   if (cols) {
544:     PetscMalloc((A->cmap.n+1)*sizeof(PetscInt),cols);
545:     for (i=0; i<A->cmap.n; i++) (*cols)[i] = i;
546:   }
547:   if (vals) {
548:     PetscMalloc((A->cmap.n+1)*sizeof(PetscScalar),vals);
549:     v    = mat->v + row;
550:     for (i=0; i<A->cmap.n; i++) {(*vals)[i] = *v; v += mat->lda;}
551:   }
552:   return(0);
553: }

557: PetscErrorCode MatRestoreRow_SeqDense(Mat A,PetscInt row,PetscInt *ncols,PetscInt **cols,PetscScalar **vals)
558: {
561:   if (cols) {PetscFree(*cols);}
562:   if (vals) {PetscFree(*vals); }
563:   return(0);
564: }
565: /* ----------------------------------------------------------------*/
568: PetscErrorCode MatSetValues_SeqDense(Mat A,PetscInt m,const PetscInt indexm[],PetscInt n,const PetscInt indexn[],const PetscScalar v[],InsertMode addv)
569: {
570:   Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
571:   PetscInt     i,j,idx=0;
572: 
574:   if (!mat->roworiented) {
575:     if (addv == INSERT_VALUES) {
576:       for (j=0; j<n; j++) {
577:         if (indexn[j] < 0) {idx += m; continue;}
578: #if defined(PETSC_USE_DEBUG)  
579:         if (indexn[j] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap.n-1);
580: #endif
581:         for (i=0; i<m; i++) {
582:           if (indexm[i] < 0) {idx++; continue;}
583: #if defined(PETSC_USE_DEBUG)  
584:           if (indexm[i] >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap.n-1);
585: #endif
586:           mat->v[indexn[j]*mat->lda + indexm[i]] = v[idx++];
587:         }
588:       }
589:     } else {
590:       for (j=0; j<n; j++) {
591:         if (indexn[j] < 0) {idx += m; continue;}
592: #if defined(PETSC_USE_DEBUG)  
593:         if (indexn[j] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap.n-1);
594: #endif
595:         for (i=0; i<m; i++) {
596:           if (indexm[i] < 0) {idx++; continue;}
597: #if defined(PETSC_USE_DEBUG)  
598:           if (indexm[i] >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap.n-1);
599: #endif
600:           mat->v[indexn[j]*mat->lda + indexm[i]] += v[idx++];
601:         }
602:       }
603:     }
604:   } else {
605:     if (addv == INSERT_VALUES) {
606:       for (i=0; i<m; i++) {
607:         if (indexm[i] < 0) { idx += n; continue;}
608: #if defined(PETSC_USE_DEBUG)  
609:         if (indexm[i] >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap.n-1);
610: #endif
611:         for (j=0; j<n; j++) {
612:           if (indexn[j] < 0) { idx++; continue;}
613: #if defined(PETSC_USE_DEBUG)  
614:           if (indexn[j] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap.n-1);
615: #endif
616:           mat->v[indexn[j]*mat->lda + indexm[i]] = v[idx++];
617:         }
618:       }
619:     } else {
620:       for (i=0; i<m; i++) {
621:         if (indexm[i] < 0) { idx += n; continue;}
622: #if defined(PETSC_USE_DEBUG)  
623:         if (indexm[i] >= A->rmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",indexm[i],A->rmap.n-1);
624: #endif
625:         for (j=0; j<n; j++) {
626:           if (indexn[j] < 0) { idx++; continue;}
627: #if defined(PETSC_USE_DEBUG)  
628:           if (indexn[j] >= A->cmap.n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",indexn[j],A->cmap.n-1);
629: #endif
630:           mat->v[indexn[j]*mat->lda + indexm[i]] += v[idx++];
631:         }
632:       }
633:     }
634:   }
635:   return(0);
636: }

640: PetscErrorCode MatGetValues_SeqDense(Mat A,PetscInt m,const PetscInt indexm[],PetscInt n,const PetscInt indexn[],PetscScalar v[])
641: {
642:   Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
643:   PetscInt     i,j;
644:   PetscScalar  *vpt = v;

647:   /* row-oriented output */
648:   for (i=0; i<m; i++) {
649:     for (j=0; j<n; j++) {
650:       *vpt++ = mat->v[indexn[j]*mat->lda + indexm[i]];
651:     }
652:   }
653:   return(0);
654: }

656: /* -----------------------------------------------------------------*/

658:  #include petscsys.h

662: PetscErrorCode MatLoad_SeqDense(PetscViewer viewer, MatType type,Mat *A)
663: {
664:   Mat_SeqDense   *a;
665:   Mat            B;
667:   PetscInt       *scols,i,j,nz,header[4];
668:   int            fd;
669:   PetscMPIInt    size;
670:   PetscInt       *rowlengths = 0,M,N,*cols;
671:   PetscScalar    *vals,*svals,*v,*w;
672:   MPI_Comm       comm = ((PetscObject)viewer)->comm;

675:   MPI_Comm_size(comm,&size);
676:   if (size > 1) SETERRQ(PETSC_ERR_ARG_WRONG,"view must have one processor");
677:   PetscViewerBinaryGetDescriptor(viewer,&fd);
678:   PetscBinaryRead(fd,header,4,PETSC_INT);
679:   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Not matrix object");
680:   M = header[1]; N = header[2]; nz = header[3];

682:   if (nz == MATRIX_BINARY_FORMAT_DENSE) { /* matrix in file is dense */
683:     MatCreate(comm,A);
684:     MatSetSizes(*A,M,N,M,N);
685:     MatSetType(*A,type);
686:     MatSeqDenseSetPreallocation(*A,PETSC_NULL);
687:     B    = *A;
688:     a    = (Mat_SeqDense*)B->data;
689:     v    = a->v;
690:     /* Allocate some temp space to read in the values and then flip them
691:        from row major to column major */
692:     PetscMalloc((M*N > 0 ? M*N : 1)*sizeof(PetscScalar),&w);
693:     /* read in nonzero values */
694:     PetscBinaryRead(fd,w,M*N,PETSC_SCALAR);
695:     /* now flip the values and store them in the matrix*/
696:     for (j=0; j<N; j++) {
697:       for (i=0; i<M; i++) {
698:         *v++ =w[i*N+j];
699:       }
700:     }
701:     PetscFree(w);
702:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
703:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
704:   } else {
705:     /* read row lengths */
706:     PetscMalloc((M+1)*sizeof(PetscInt),&rowlengths);
707:     PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

709:     /* create our matrix */
710:     MatCreate(comm,A);
711:     MatSetSizes(*A,M,N,M,N);
712:     MatSetType(*A,type);
713:     MatSeqDenseSetPreallocation(*A,PETSC_NULL);
714:     B = *A;
715:     a = (Mat_SeqDense*)B->data;
716:     v = a->v;

718:     /* read column indices and nonzeros */
719:     PetscMalloc((nz+1)*sizeof(PetscInt),&scols);
720:     cols = scols;
721:     PetscBinaryRead(fd,cols,nz,PETSC_INT);
722:     PetscMalloc((nz+1)*sizeof(PetscScalar),&svals);
723:     vals = svals;
724:     PetscBinaryRead(fd,vals,nz,PETSC_SCALAR);

726:     /* insert into matrix */
727:     for (i=0; i<M; i++) {
728:       for (j=0; j<rowlengths[i]; j++) v[i+M*scols[j]] = svals[j];
729:       svals += rowlengths[i]; scols += rowlengths[i];
730:     }
731:     PetscFree(vals);
732:     PetscFree(cols);
733:     PetscFree(rowlengths);

735:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
736:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
737:   }
738:   return(0);
739: }

741:  #include petscsys.h

745: static PetscErrorCode MatView_SeqDense_ASCII(Mat A,PetscViewer viewer)
746: {
747:   Mat_SeqDense      *a = (Mat_SeqDense*)A->data;
748:   PetscErrorCode    ierr;
749:   PetscInt          i,j;
750:   const char        *name;
751:   PetscScalar       *v;
752:   PetscViewerFormat format;

755:   PetscObjectGetName((PetscObject)A,&name);
756:   PetscViewerGetFormat(viewer,&format);
757:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
758:     return(0);  /* do nothing for now */
759:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
760:     PetscViewerASCIIUseTabs(viewer,PETSC_NO);
761:     for (i=0; i<A->rmap.n; i++) {
762:       v = a->v + i;
763:       PetscViewerASCIIPrintf(viewer,"row %D:",i);
764:       for (j=0; j<A->cmap.n; j++) {
765: #if defined(PETSC_USE_COMPLEX)
766:         if (PetscRealPart(*v) != 0.0 && PetscImaginaryPart(*v) != 0.0) {
767:           PetscViewerASCIIPrintf(viewer," (%D, %G + %G i) ",j,PetscRealPart(*v),PetscImaginaryPart(*v));
768:         } else if (PetscRealPart(*v)) {
769:           PetscViewerASCIIPrintf(viewer," (%D, %G) ",j,PetscRealPart(*v));
770:         }
771: #else
772:         if (*v) {
773:           PetscViewerASCIIPrintf(viewer," (%D, %G) ",j,*v);
774:         }
775: #endif
776:         v += a->lda;
777:       }
778:       PetscViewerASCIIPrintf(viewer,"\n");
779:     }
780:     PetscViewerASCIIUseTabs(viewer,PETSC_YES);
781:   } else {
782:     PetscViewerASCIIUseTabs(viewer,PETSC_NO);
783: #if defined(PETSC_USE_COMPLEX)
784:     PetscTruth allreal = PETSC_TRUE;
785:     /* determine if matrix has all real values */
786:     v = a->v;
787:     for (i=0; i<A->rmap.n*A->cmap.n; i++) {
788:         if (PetscImaginaryPart(v[i])) { allreal = PETSC_FALSE; break ;}
789:     }
790: #endif
791:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
792:       PetscObjectGetName((PetscObject)A,&name);
793:       PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",A->rmap.n,A->cmap.n);
794:       PetscViewerASCIIPrintf(viewer,"%s = zeros(%D,%D);\n",name,A->rmap.n,A->cmap.n);
795:       PetscViewerASCIIPrintf(viewer,"%s = [\n",name);
796:     }

798:     for (i=0; i<A->rmap.n; i++) {
799:       v = a->v + i;
800:       for (j=0; j<A->cmap.n; j++) {
801: #if defined(PETSC_USE_COMPLEX)
802:         if (allreal) {
803:           PetscViewerASCIIPrintf(viewer,"%6.4e ",PetscRealPart(*v));
804:         } else {
805:           PetscViewerASCIIPrintf(viewer,"%6.4e + %6.4e i ",PetscRealPart(*v),PetscImaginaryPart(*v));
806:         }
807: #else
808:         PetscViewerASCIIPrintf(viewer,"%6.4e ",*v);
809: #endif
810:         v += a->lda;
811:       }
812:       PetscViewerASCIIPrintf(viewer,"\n");
813:     }
814:     if (format == PETSC_VIEWER_ASCII_MATLAB) {
815:       PetscViewerASCIIPrintf(viewer,"];\n");
816:     }
817:     PetscViewerASCIIUseTabs(viewer,PETSC_YES);
818:   }
819:   PetscViewerFlush(viewer);
820:   return(0);
821: }

825: static PetscErrorCode MatView_SeqDense_Binary(Mat A,PetscViewer viewer)
826: {
827:   Mat_SeqDense      *a = (Mat_SeqDense*)A->data;
828:   PetscErrorCode    ierr;
829:   int               fd;
830:   PetscInt          ict,j,n = A->cmap.n,m = A->rmap.n,i,*col_lens,nz = m*n;
831:   PetscScalar       *v,*anonz,*vals;
832:   PetscViewerFormat format;
833: 
835:   PetscViewerBinaryGetDescriptor(viewer,&fd);

837:   PetscViewerGetFormat(viewer,&format);
838:   if (format == PETSC_VIEWER_BINARY_NATIVE) {
839:     /* store the matrix as a dense matrix */
840:     PetscMalloc(4*sizeof(PetscInt),&col_lens);
841:     col_lens[0] = MAT_FILE_COOKIE;
842:     col_lens[1] = m;
843:     col_lens[2] = n;
844:     col_lens[3] = MATRIX_BINARY_FORMAT_DENSE;
845:     PetscBinaryWrite(fd,col_lens,4,PETSC_INT,PETSC_TRUE);
846:     PetscFree(col_lens);

848:     /* write out matrix, by rows */
849:     PetscMalloc((m*n+1)*sizeof(PetscScalar),&vals);
850:     v    = a->v;
851:     for (i=0; i<m; i++) {
852:       for (j=0; j<n; j++) {
853:         vals[i + j*m] = *v++;
854:       }
855:     }
856:     PetscBinaryWrite(fd,vals,n*m,PETSC_SCALAR,PETSC_FALSE);
857:     PetscFree(vals);
858:   } else {
859:     PetscMalloc((4+nz)*sizeof(PetscInt),&col_lens);
860:     col_lens[0] = MAT_FILE_COOKIE;
861:     col_lens[1] = m;
862:     col_lens[2] = n;
863:     col_lens[3] = nz;

865:     /* store lengths of each row and write (including header) to file */
866:     for (i=0; i<m; i++) col_lens[4+i] = n;
867:     PetscBinaryWrite(fd,col_lens,4+m,PETSC_INT,PETSC_TRUE);

869:     /* Possibly should write in smaller increments, not whole matrix at once? */
870:     /* store column indices (zero start index) */
871:     ict = 0;
872:     for (i=0; i<m; i++) {
873:       for (j=0; j<n; j++) col_lens[ict++] = j;
874:     }
875:     PetscBinaryWrite(fd,col_lens,nz,PETSC_INT,PETSC_FALSE);
876:     PetscFree(col_lens);

878:     /* store nonzero values */
879:     PetscMalloc((nz+1)*sizeof(PetscScalar),&anonz);
880:     ict  = 0;
881:     for (i=0; i<m; i++) {
882:       v = a->v + i;
883:       for (j=0; j<n; j++) {
884:         anonz[ict++] = *v; v += a->lda;
885:       }
886:     }
887:     PetscBinaryWrite(fd,anonz,nz,PETSC_SCALAR,PETSC_FALSE);
888:     PetscFree(anonz);
889:   }
890:   return(0);
891: }

895: PetscErrorCode MatView_SeqDense_Draw_Zoom(PetscDraw draw,void *Aa)
896: {
897:   Mat               A = (Mat) Aa;
898:   Mat_SeqDense      *a = (Mat_SeqDense*)A->data;
899:   PetscErrorCode    ierr;
900:   PetscInt          m = A->rmap.n,n = A->cmap.n,color,i,j;
901:   PetscScalar       *v = a->v;
902:   PetscViewer       viewer;
903:   PetscDraw         popup;
904:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,scale,maxv = 0.0;
905:   PetscViewerFormat format;


909:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
910:   PetscViewerGetFormat(viewer,&format);
911:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

913:   /* Loop over matrix elements drawing boxes */
914:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
915:     /* Blue for negative and Red for positive */
916:     color = PETSC_DRAW_BLUE;
917:     for(j = 0; j < n; j++) {
918:       x_l = j;
919:       x_r = x_l + 1.0;
920:       for(i = 0; i < m; i++) {
921:         y_l = m - i - 1.0;
922:         y_r = y_l + 1.0;
923: #if defined(PETSC_USE_COMPLEX)
924:         if (PetscRealPart(v[j*m+i]) >  0.) {
925:           color = PETSC_DRAW_RED;
926:         } else if (PetscRealPart(v[j*m+i]) <  0.) {
927:           color = PETSC_DRAW_BLUE;
928:         } else {
929:           continue;
930:         }
931: #else
932:         if (v[j*m+i] >  0.) {
933:           color = PETSC_DRAW_RED;
934:         } else if (v[j*m+i] <  0.) {
935:           color = PETSC_DRAW_BLUE;
936:         } else {
937:           continue;
938:         }
939: #endif
940:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
941:       }
942:     }
943:   } else {
944:     /* use contour shading to indicate magnitude of values */
945:     /* first determine max of all nonzero values */
946:     for(i = 0; i < m*n; i++) {
947:       if (PetscAbsScalar(v[i]) > maxv) maxv = PetscAbsScalar(v[i]);
948:     }
949:     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
950:     PetscDrawGetPopup(draw,&popup);
951:     if (popup) {PetscDrawScalePopup(popup,0.0,maxv);}
952:     for(j = 0; j < n; j++) {
953:       x_l = j;
954:       x_r = x_l + 1.0;
955:       for(i = 0; i < m; i++) {
956:         y_l   = m - i - 1.0;
957:         y_r   = y_l + 1.0;
958:         color = PETSC_DRAW_BASIC_COLORS + (int)(scale*PetscAbsScalar(v[j*m+i]));
959:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
960:       }
961:     }
962:   }
963:   return(0);
964: }

968: PetscErrorCode MatView_SeqDense_Draw(Mat A,PetscViewer viewer)
969: {
970:   PetscDraw      draw;
971:   PetscTruth     isnull;
972:   PetscReal      xr,yr,xl,yl,h,w;

976:   PetscViewerDrawGetDraw(viewer,0,&draw);
977:   PetscDrawIsNull(draw,&isnull);
978:   if (isnull) return(0);

980:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
981:   xr  = A->cmap.n; yr = A->rmap.n; h = yr/10.0; w = xr/10.0;
982:   xr += w;    yr += h;  xl = -w;     yl = -h;
983:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
984:   PetscDrawZoom(draw,MatView_SeqDense_Draw_Zoom,A);
985:   PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);
986:   return(0);
987: }

991: PetscErrorCode MatView_SeqDense(Mat A,PetscViewer viewer)
992: {
994:   PetscTruth     issocket,iascii,isbinary,isdraw;

997:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_SOCKET,&issocket);
998:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
999:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);
1000:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);

1002:   if (iascii) {
1003:     MatView_SeqDense_ASCII(A,viewer);
1004: #if defined(PETSC_USE_SOCKET_VIEWER)
1005:   } else if (issocket) {
1006:     Mat_SeqDense   *a = (Mat_SeqDense*)A->data;
1007:     if (a->lda>A->rmap.n) SETERRQ(PETSC_ERR_SUP,"Case can not handle LDA");
1008:     PetscViewerSocketPutScalar(viewer,A->rmap.n,A->cmap.n,a->v);
1009: #endif
1010:   } else if (isbinary) {
1011:     MatView_SeqDense_Binary(A,viewer);
1012:   } else if (isdraw) {
1013:     MatView_SeqDense_Draw(A,viewer);
1014:   } else {
1015:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by dense matrix",((PetscObject)viewer)->type_name);
1016:   }
1017:   return(0);
1018: }

1022: PetscErrorCode MatDestroy_SeqDense(Mat mat)
1023: {
1024:   Mat_SeqDense   *l = (Mat_SeqDense*)mat->data;

1028: #if defined(PETSC_USE_LOG)
1029:   PetscLogObjectState((PetscObject)mat,"Rows %D Cols %D",mat->rmap.n,mat->cmap.n);
1030: #endif
1031:   PetscFree(l->pivots);
1032:   if (!l->user_alloc) {PetscFree(l->v);}
1033:   PetscFree(l);
1034:   PetscObjectComposeFunctionDynamic((PetscObject)mat,"MatSeqDenseSetPreallocation_C","",PETSC_NULL);
1035:   return(0);
1036: }

1040: PetscErrorCode MatTranspose_SeqDense(Mat A,Mat *matout)
1041: {
1042:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
1044:   PetscInt       k,j,m,n,M;
1045:   PetscScalar    *v,tmp;

1048:   v = mat->v; m = A->rmap.n; M = mat->lda; n = A->cmap.n;
1049:   if (!matout) { /* in place transpose */
1050:     if (m != n) {
1051:       SETERRQ(PETSC_ERR_SUP,"Can not transpose non-square matrix in place");
1052:     } else {
1053:       for (j=0; j<m; j++) {
1054:         for (k=0; k<j; k++) {
1055:           tmp = v[j + k*M];
1056:           v[j + k*M] = v[k + j*M];
1057:           v[k + j*M] = tmp;
1058:         }
1059:       }
1060:     }
1061:   } else { /* out-of-place transpose */
1062:     Mat          tmat;
1063:     Mat_SeqDense *tmatd;
1064:     PetscScalar  *v2;

1066:     MatCreate(A->comm,&tmat);
1067:     MatSetSizes(tmat,A->cmap.n,A->rmap.n,A->cmap.n,A->rmap.n);
1068:     MatSetType(tmat,A->type_name);
1069:     MatSeqDenseSetPreallocation(tmat,PETSC_NULL);
1070:     tmatd = (Mat_SeqDense*)tmat->data;
1071:     v = mat->v; v2 = tmatd->v;
1072:     for (j=0; j<n; j++) {
1073:       for (k=0; k<m; k++) v2[j + k*n] = v[k + j*M];
1074:     }
1075:     MatAssemblyBegin(tmat,MAT_FINAL_ASSEMBLY);
1076:     MatAssemblyEnd(tmat,MAT_FINAL_ASSEMBLY);
1077:     *matout = tmat;
1078:   }
1079:   return(0);
1080: }

1084: PetscErrorCode MatEqual_SeqDense(Mat A1,Mat A2,PetscTruth *flg)
1085: {
1086:   Mat_SeqDense *mat1 = (Mat_SeqDense*)A1->data;
1087:   Mat_SeqDense *mat2 = (Mat_SeqDense*)A2->data;
1088:   PetscInt     i,j;
1089:   PetscScalar  *v1 = mat1->v,*v2 = mat2->v;

1092:   if (A1->rmap.n != A2->rmap.n) {*flg = PETSC_FALSE; return(0);}
1093:   if (A1->cmap.n != A2->cmap.n) {*flg = PETSC_FALSE; return(0);}
1094:   for (i=0; i<A1->rmap.n; i++) {
1095:     v1 = mat1->v+i; v2 = mat2->v+i;
1096:     for (j=0; j<A1->cmap.n; j++) {
1097:       if (*v1 != *v2) {*flg = PETSC_FALSE; return(0);}
1098:       v1 += mat1->lda; v2 += mat2->lda;
1099:     }
1100:   }
1101:   *flg = PETSC_TRUE;
1102:   return(0);
1103: }

1107: PetscErrorCode MatGetDiagonal_SeqDense(Mat A,Vec v)
1108: {
1109:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
1111:   PetscInt       i,n,len;
1112:   PetscScalar    *x,zero = 0.0;

1115:   VecSet(v,zero);
1116:   VecGetSize(v,&n);
1117:   VecGetArray(v,&x);
1118:   len = PetscMin(A->rmap.n,A->cmap.n);
1119:   if (n != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming mat and vec");
1120:   for (i=0; i<len; i++) {
1121:     x[i] = mat->v[i*mat->lda + i];
1122:   }
1123:   VecRestoreArray(v,&x);
1124:   return(0);
1125: }

1129: PetscErrorCode MatDiagonalScale_SeqDense(Mat A,Vec ll,Vec rr)
1130: {
1131:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
1132:   PetscScalar    *l,*r,x,*v;
1134:   PetscInt       i,j,m = A->rmap.n,n = A->cmap.n;

1137:   if (ll) {
1138:     VecGetSize(ll,&m);
1139:     VecGetArray(ll,&l);
1140:     if (m != A->rmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vec wrong size");
1141:     for (i=0; i<m; i++) {
1142:       x = l[i];
1143:       v = mat->v + i;
1144:       for (j=0; j<n; j++) { (*v) *= x; v+= m;}
1145:     }
1146:     VecRestoreArray(ll,&l);
1147:     PetscLogFlops(n*m);
1148:   }
1149:   if (rr) {
1150:     VecGetSize(rr,&n);
1151:     VecGetArray(rr,&r);
1152:     if (n != A->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vec wrong size");
1153:     for (i=0; i<n; i++) {
1154:       x = r[i];
1155:       v = mat->v + i*m;
1156:       for (j=0; j<m; j++) { (*v++) *= x;}
1157:     }
1158:     VecRestoreArray(rr,&r);
1159:     PetscLogFlops(n*m);
1160:   }
1161:   return(0);
1162: }

1166: PetscErrorCode MatNorm_SeqDense(Mat A,NormType type,PetscReal *nrm)
1167: {
1168:   Mat_SeqDense *mat = (Mat_SeqDense*)A->data;
1169:   PetscScalar  *v = mat->v;
1170:   PetscReal    sum = 0.0;
1171:   PetscInt     lda=mat->lda,m=A->rmap.n,i,j;

1175:   if (type == NORM_FROBENIUS) {
1176:     if (lda>m) {
1177:       for (j=0; j<A->cmap.n; j++) {
1178:         v = mat->v+j*lda;
1179:         for (i=0; i<m; i++) {
1180: #if defined(PETSC_USE_COMPLEX)
1181:           sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1182: #else
1183:           sum += (*v)*(*v); v++;
1184: #endif
1185:         }
1186:       }
1187:     } else {
1188:       for (i=0; i<A->cmap.n*A->rmap.n; i++) {
1189: #if defined(PETSC_USE_COMPLEX)
1190:         sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1191: #else
1192:         sum += (*v)*(*v); v++;
1193: #endif
1194:       }
1195:     }
1196:     *nrm = sqrt(sum);
1197:     PetscLogFlops(2*A->cmap.n*A->rmap.n);
1198:   } else if (type == NORM_1) {
1199:     *nrm = 0.0;
1200:     for (j=0; j<A->cmap.n; j++) {
1201:       v = mat->v + j*mat->lda;
1202:       sum = 0.0;
1203:       for (i=0; i<A->rmap.n; i++) {
1204:         sum += PetscAbsScalar(*v);  v++;
1205:       }
1206:       if (sum > *nrm) *nrm = sum;
1207:     }
1208:     PetscLogFlops(A->cmap.n*A->rmap.n);
1209:   } else if (type == NORM_INFINITY) {
1210:     *nrm = 0.0;
1211:     for (j=0; j<A->rmap.n; j++) {
1212:       v = mat->v + j;
1213:       sum = 0.0;
1214:       for (i=0; i<A->cmap.n; i++) {
1215:         sum += PetscAbsScalar(*v); v += mat->lda;
1216:       }
1217:       if (sum > *nrm) *nrm = sum;
1218:     }
1219:     PetscLogFlops(A->cmap.n*A->rmap.n);
1220:   } else {
1221:     SETERRQ(PETSC_ERR_SUP,"No two norm");
1222:   }
1223:   return(0);
1224: }

1228: PetscErrorCode MatSetOption_SeqDense(Mat A,MatOption op)
1229: {
1230:   Mat_SeqDense   *aij = (Mat_SeqDense*)A->data;
1232: 
1234:   switch (op) {
1235:   case MAT_ROW_ORIENTED:
1236:     aij->roworiented = PETSC_TRUE;
1237:     break;
1238:   case MAT_COLUMN_ORIENTED:
1239:     aij->roworiented = PETSC_FALSE;
1240:     break;
1241:   case MAT_ROWS_SORTED:
1242:   case MAT_ROWS_UNSORTED:
1243:   case MAT_COLUMNS_SORTED:
1244:   case MAT_COLUMNS_UNSORTED:
1245:   case MAT_NO_NEW_NONZERO_LOCATIONS:
1246:   case MAT_YES_NEW_NONZERO_LOCATIONS:
1247:   case MAT_NEW_NONZERO_LOCATION_ERR:
1248:   case MAT_NO_NEW_DIAGONALS:
1249:   case MAT_YES_NEW_DIAGONALS:
1250:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1251:   case MAT_USE_HASH_TABLE:
1252:     PetscInfo(A,"Option ignored\n");
1253:     break;
1254:   case MAT_SYMMETRIC:
1255:   case MAT_STRUCTURALLY_SYMMETRIC:
1256:   case MAT_NOT_SYMMETRIC:
1257:   case MAT_NOT_STRUCTURALLY_SYMMETRIC:
1258:   case MAT_HERMITIAN:
1259:   case MAT_NOT_HERMITIAN:
1260:   case MAT_SYMMETRY_ETERNAL:
1261:   case MAT_NOT_SYMMETRY_ETERNAL:
1262:     break;
1263:   default:
1264:     SETERRQ(PETSC_ERR_SUP,"unknown option");
1265:   }
1266:   return(0);
1267: }

1271: PetscErrorCode MatZeroEntries_SeqDense(Mat A)
1272: {
1273:   Mat_SeqDense   *l = (Mat_SeqDense*)A->data;
1275:   PetscInt       lda=l->lda,m=A->rmap.n,j;

1278:   if (lda>m) {
1279:     for (j=0; j<A->cmap.n; j++) {
1280:       PetscMemzero(l->v+j*lda,m*sizeof(PetscScalar));
1281:     }
1282:   } else {
1283:     PetscMemzero(l->v,A->rmap.n*A->cmap.n*sizeof(PetscScalar));
1284:   }
1285:   return(0);
1286: }

1290: PetscErrorCode MatZeroRows_SeqDense(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
1291: {
1292:   Mat_SeqDense   *l = (Mat_SeqDense*)A->data;
1293:   PetscInt       n = A->cmap.n,i,j;
1294:   PetscScalar    *slot;

1297:   for (i=0; i<N; i++) {
1298:     slot = l->v + rows[i];
1299:     for (j=0; j<n; j++) { *slot = 0.0; slot += n;}
1300:   }
1301:   if (diag != 0.0) {
1302:     for (i=0; i<N; i++) {
1303:       slot = l->v + (n+1)*rows[i];
1304:       *slot = diag;
1305:     }
1306:   }
1307:   return(0);
1308: }

1312: PetscErrorCode MatGetArray_SeqDense(Mat A,PetscScalar *array[])
1313: {
1314:   Mat_SeqDense *mat = (Mat_SeqDense*)A->data;

1317:   if (mat->lda != A->rmap.n) SETERRQ(PETSC_ERR_SUP,"Cannot get array for Dense matrices with LDA different from number of rows");
1318:   *array = mat->v;
1319:   return(0);
1320: }

1324: PetscErrorCode MatRestoreArray_SeqDense(Mat A,PetscScalar *array[])
1325: {
1327:   *array = 0; /* user cannot accidently use the array later */
1328:   return(0);
1329: }

1333: static PetscErrorCode MatGetSubMatrix_SeqDense(Mat A,IS isrow,IS iscol,PetscInt cs,MatReuse scall,Mat *B)
1334: {
1335:   Mat_SeqDense   *mat = (Mat_SeqDense*)A->data;
1337:   PetscInt       i,j,*irow,*icol,nrows,ncols;
1338:   PetscScalar    *av,*bv,*v = mat->v;
1339:   Mat            newmat;

1342:   ISGetIndices(isrow,&irow);
1343:   ISGetIndices(iscol,&icol);
1344:   ISGetLocalSize(isrow,&nrows);
1345:   ISGetLocalSize(iscol,&ncols);
1346: 
1347:   /* Check submatrixcall */
1348:   if (scall == MAT_REUSE_MATRIX) {
1349:     PetscInt n_cols,n_rows;
1350:     MatGetSize(*B,&n_rows,&n_cols);
1351:     if (n_rows != nrows || n_cols != ncols) {
1352:       /* resize the result result matrix to match number of requested rows/columns */
1353:       MatSetSizes(*B,nrows,nrows,nrows,nrows);
1354:     }
1355:     newmat = *B;
1356:   } else {
1357:     /* Create and fill new matrix */
1358:     MatCreate(A->comm,&newmat);
1359:     MatSetSizes(newmat,nrows,ncols,nrows,ncols);
1360:     MatSetType(newmat,A->type_name);
1361:     MatSeqDenseSetPreallocation(newmat,PETSC_NULL);
1362:   }

1364:   /* Now extract the data pointers and do the copy,column at a time */
1365:   bv = ((Mat_SeqDense*)newmat->data)->v;
1366: 
1367:   for (i=0; i<ncols; i++) {
1368:     av = v + mat->lda*icol[i];
1369:     for (j=0; j<nrows; j++) {
1370:       *bv++ = av[irow[j]];
1371:     }
1372:   }

1374:   /* Assemble the matrices so that the correct flags are set */
1375:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
1376:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);

1378:   /* Free work space */
1379:   ISRestoreIndices(isrow,&irow);
1380:   ISRestoreIndices(iscol,&icol);
1381:   *B = newmat;
1382:   return(0);
1383: }

1387: PetscErrorCode MatGetSubMatrices_SeqDense(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1388: {
1390:   PetscInt       i;

1393:   if (scall == MAT_INITIAL_MATRIX) {
1394:     PetscMalloc((n+1)*sizeof(Mat),B);
1395:   }

1397:   for (i=0; i<n; i++) {
1398:     MatGetSubMatrix_SeqDense(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
1399:   }
1400:   return(0);
1401: }

1405: PetscErrorCode MatAssemblyBegin_SeqDense(Mat mat,MatAssemblyType mode)
1406: {
1408:   return(0);
1409: }

1413: PetscErrorCode MatAssemblyEnd_SeqDense(Mat mat,MatAssemblyType mode)
1414: {
1416:   return(0);
1417: }

1421: PetscErrorCode MatCopy_SeqDense(Mat A,Mat B,MatStructure str)
1422: {
1423:   Mat_SeqDense   *a = (Mat_SeqDense*)A->data,*b = (Mat_SeqDense *)B->data;
1425:   PetscInt       lda1=a->lda,lda2=b->lda, m=A->rmap.n,n=A->cmap.n, j;

1428:   /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1429:   if (A->ops->copy != B->ops->copy) {
1430:     MatCopy_Basic(A,B,str);
1431:     return(0);
1432:   }
1433:   if (m != B->rmap.n || n != B->cmap.n) SETERRQ(PETSC_ERR_ARG_SIZ,"size(B) != size(A)");
1434:   if (lda1>m || lda2>m) {
1435:     for (j=0; j<n; j++) {
1436:       PetscMemcpy(b->v+j*lda2,a->v+j*lda1,m*sizeof(PetscScalar));
1437:     }
1438:   } else {
1439:     PetscMemcpy(b->v,a->v,A->rmap.n*A->cmap.n*sizeof(PetscScalar));
1440:   }
1441:   return(0);
1442: }

1446: PetscErrorCode MatSetUpPreallocation_SeqDense(Mat A)
1447: {

1451:    MatSeqDenseSetPreallocation(A,0);
1452:   return(0);
1453: }

1457: PetscErrorCode MatSetSizes_SeqDense(Mat A,PetscInt m,PetscInt n,PetscInt M,PetscInt N)
1458: {
1459:   Mat_SeqDense   *a = (Mat_SeqDense*)A->data;
1462:   /* this will not be called before lda, Mmax,  and Nmax have been set */
1463:   m = PetscMax(m,M);
1464:   n = PetscMax(n,N);
1465:   if (m > a->Mmax) SETERRQ2(PETSC_ERR_SUP,"Cannot yet resize number rows of dense matrix larger then its initial size %d, requested %d",a->lda,(int)m);
1466:   if (n > a->Nmax) SETERRQ2(PETSC_ERR_SUP,"Cannot yet resize number columns of dense matrix larger then its initial size %d, requested %d",a->Nmax,(int)n);
1467:   A->rmap.n = A->rmap.n = m;
1468:   A->cmap.n = A->cmap.N = n;
1469:   if (a->changelda) a->lda = m;
1470:   PetscMemzero(a->v,m*n*sizeof(PetscScalar));
1471:   return(0);
1472: }

1474: /* ----------------------------------------------------------------*/
1477: PetscErrorCode MatMatMult_SeqDense_SeqDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1478: {

1482:   if (scall == MAT_INITIAL_MATRIX){
1483:     MatMatMultSymbolic_SeqDense_SeqDense(A,B,fill,C);
1484:   }
1485:   MatMatMultNumeric_SeqDense_SeqDense(A,B,*C);
1486:   return(0);
1487: }

1491: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqDense(Mat A,Mat B,PetscReal fill,Mat *C)
1492: {
1494:   PetscInt       m=A->rmap.n,n=B->cmap.n;
1495:   Mat            Cmat;

1498:   if (A->cmap.n != B->rmap.n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap.n %d != B->rmap.n %d\n",A->cmap.n,B->rmap.n);
1499:   MatCreate(PETSC_COMM_SELF,&Cmat);
1500:   MatSetSizes(Cmat,m,n,m,n);
1501:   MatSetType(Cmat,MATSEQDENSE);
1502:   MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);
1503:   Cmat->assembled = PETSC_TRUE;
1504:   *C = Cmat;
1505:   return(0);
1506: }

1510: PetscErrorCode MatMatMultNumeric_SeqDense_SeqDense(Mat A,Mat B,Mat C)
1511: {
1512:   Mat_SeqDense   *a = (Mat_SeqDense*)A->data;
1513:   Mat_SeqDense   *b = (Mat_SeqDense*)B->data;
1514:   Mat_SeqDense   *c = (Mat_SeqDense*)C->data;
1515:   PetscBLASInt   m=(PetscBLASInt)A->rmap.n,n=(PetscBLASInt)B->cmap.n,k=(PetscBLASInt)A->cmap.n;
1516:   PetscScalar    _DOne=1.0,_DZero=0.0;

1519:   BLASgemm_("N","N",&m,&n,&k,&_DOne,a->v,&a->lda,b->v,&b->lda,&_DZero,c->v,&c->lda);
1520:   return(0);
1521: }

1525: PetscErrorCode MatMatMultTranspose_SeqDense_SeqDense(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
1526: {

1530:   if (scall == MAT_INITIAL_MATRIX){
1531:     MatMatMultTransposeSymbolic_SeqDense_SeqDense(A,B,fill,C);
1532:   }
1533:   MatMatMultTransposeNumeric_SeqDense_SeqDense(A,B,*C);
1534:   return(0);
1535: }

1539: PetscErrorCode MatMatMultTransposeSymbolic_SeqDense_SeqDense(Mat A,Mat B,PetscReal fill,Mat *C)
1540: {
1542:   PetscInt       m=A->cmap.n,n=B->cmap.n;
1543:   Mat            Cmat;

1546:   if (A->rmap.n != B->rmap.n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->rmap.n %d != B->rmap.n %d\n",A->rmap.n,B->rmap.n);
1547:   MatCreate(PETSC_COMM_SELF,&Cmat);
1548:   MatSetSizes(Cmat,m,n,m,n);
1549:   MatSetType(Cmat,MATSEQDENSE);
1550:   MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);
1551:   Cmat->assembled = PETSC_TRUE;
1552:   *C = Cmat;
1553:   return(0);
1554: }

1558: PetscErrorCode MatMatMultTransposeNumeric_SeqDense_SeqDense(Mat A,Mat B,Mat C)
1559: {
1560:   Mat_SeqDense   *a = (Mat_SeqDense*)A->data;
1561:   Mat_SeqDense   *b = (Mat_SeqDense*)B->data;
1562:   Mat_SeqDense   *c = (Mat_SeqDense*)C->data;
1563:   PetscBLASInt   m=(PetscBLASInt)A->cmap.n,n=(PetscBLASInt)B->cmap.n,k=(PetscBLASInt)A->rmap.n;
1564:   PetscScalar    _DOne=1.0,_DZero=0.0;

1567:   BLASgemm_("T","N",&m,&n,&k,&_DOne,a->v,&a->lda,b->v,&b->lda,&_DZero,c->v,&c->lda);
1568:   return(0);
1569: }
1570: /* -------------------------------------------------------------------*/
1571: static struct _MatOps MatOps_Values = {MatSetValues_SeqDense,
1572:        MatGetRow_SeqDense,
1573:        MatRestoreRow_SeqDense,
1574:        MatMult_SeqDense,
1575: /* 4*/ MatMultAdd_SeqDense,
1576:        MatMultTranspose_SeqDense,
1577:        MatMultTransposeAdd_SeqDense,
1578:        MatSolve_SeqDense,
1579:        MatSolveAdd_SeqDense,
1580:        MatSolveTranspose_SeqDense,
1581: /*10*/ MatSolveTransposeAdd_SeqDense,
1582:        MatLUFactor_SeqDense,
1583:        MatCholeskyFactor_SeqDense,
1584:        MatRelax_SeqDense,
1585:        MatTranspose_SeqDense,
1586: /*15*/ MatGetInfo_SeqDense,
1587:        MatEqual_SeqDense,
1588:        MatGetDiagonal_SeqDense,
1589:        MatDiagonalScale_SeqDense,
1590:        MatNorm_SeqDense,
1591: /*20*/ MatAssemblyBegin_SeqDense,
1592:        MatAssemblyEnd_SeqDense,
1593:        0,
1594:        MatSetOption_SeqDense,
1595:        MatZeroEntries_SeqDense,
1596: /*25*/ MatZeroRows_SeqDense,
1597:        MatLUFactorSymbolic_SeqDense,
1598:        MatLUFactorNumeric_SeqDense,
1599:        MatCholeskyFactorSymbolic_SeqDense,
1600:        MatCholeskyFactorNumeric_SeqDense,
1601: /*30*/ MatSetUpPreallocation_SeqDense,
1602:        0,
1603:        0,
1604:        MatGetArray_SeqDense,
1605:        MatRestoreArray_SeqDense,
1606: /*35*/ MatDuplicate_SeqDense,
1607:        0,
1608:        0,
1609:        0,
1610:        0,
1611: /*40*/ MatAXPY_SeqDense,
1612:        MatGetSubMatrices_SeqDense,
1613:        0,
1614:        MatGetValues_SeqDense,
1615:        MatCopy_SeqDense,
1616: /*45*/ 0,
1617:        MatScale_SeqDense,
1618:        0,
1619:        0,
1620:        0,
1621: /*50*/ 0,
1622:        0,
1623:        0,
1624:        0,
1625:        0,
1626: /*55*/ 0,
1627:        0,
1628:        0,
1629:        0,
1630:        0,
1631: /*60*/ 0,
1632:        MatDestroy_SeqDense,
1633:        MatView_SeqDense,
1634:        0,
1635:        0,
1636: /*65*/ 0,
1637:        0,
1638:        0,
1639:        0,
1640:        0,
1641: /*70*/ 0,
1642:        0,
1643:        0,
1644:        0,
1645:        0,
1646: /*75*/ 0,
1647:        0,
1648:        0,
1649:        0,
1650:        0,
1651: /*80*/ 0,
1652:        0,
1653:        0,
1654:        0,
1655: /*84*/ MatLoad_SeqDense,
1656:        0,
1657:        0,
1658:        0,
1659:        0,
1660:        0,
1661: /*90*/ MatMatMult_SeqDense_SeqDense,
1662:        MatMatMultSymbolic_SeqDense_SeqDense,
1663:        MatMatMultNumeric_SeqDense_SeqDense,
1664:        0,
1665:        0,
1666: /*95*/ 0,
1667:        MatMatMultTranspose_SeqDense_SeqDense,
1668:        MatMatMultTransposeSymbolic_SeqDense_SeqDense,
1669:        MatMatMultTransposeNumeric_SeqDense_SeqDense,
1670:        0,
1671: /*100*/0,
1672:        0,
1673:        0,
1674:        0,
1675:        MatSetSizes_SeqDense};

1679: /*@C
1680:    MatCreateSeqDense - Creates a sequential dense matrix that 
1681:    is stored in column major order (the usual Fortran 77 manner). Many 
1682:    of the matrix operations use the BLAS and LAPACK routines.

1684:    Collective on MPI_Comm

1686:    Input Parameters:
1687: +  comm - MPI communicator, set to PETSC_COMM_SELF
1688: .  m - number of rows
1689: .  n - number of columns
1690: -  data - optional location of matrix data.  Set data=PETSC_NULL for PETSc
1691:    to control all matrix memory allocation.

1693:    Output Parameter:
1694: .  A - the matrix

1696:    Notes:
1697:    The data input variable is intended primarily for Fortran programmers
1698:    who wish to allocate their own matrix memory space.  Most users should
1699:    set data=PETSC_NULL.

1701:    Level: intermediate

1703: .keywords: dense, matrix, LAPACK, BLAS

1705: .seealso: MatCreate(), MatCreateMPIDense(), MatSetValues()
1706: @*/
1707: PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqDense(MPI_Comm comm,PetscInt m,PetscInt n,PetscScalar *data,Mat *A)
1708: {

1712:   MatCreate(comm,A);
1713:   MatSetSizes(*A,m,n,m,n);
1714:   MatSetType(*A,MATSEQDENSE);
1715:   MatSeqDenseSetPreallocation(*A,data);
1716:   return(0);
1717: }

1721: /*@C
1722:    MatSeqDenseSetPreallocation - Sets the array used for storing the matrix elements

1724:    Collective on MPI_Comm

1726:    Input Parameters:
1727: +  A - the matrix
1728: -  data - the array (or PETSC_NULL)

1730:    Notes:
1731:    The data input variable is intended primarily for Fortran programmers
1732:    who wish to allocate their own matrix memory space.  Most users should
1733:    need not call this routine.

1735:    Level: intermediate

1737: .keywords: dense, matrix, LAPACK, BLAS

1739: .seealso: MatCreate(), MatCreateMPIDense(), MatSetValues()
1740: @*/
1741: PetscErrorCode PETSCMAT_DLLEXPORT MatSeqDenseSetPreallocation(Mat B,PetscScalar data[])
1742: {
1743:   PetscErrorCode ierr,(*f)(Mat,PetscScalar[]);

1746:   PetscObjectQueryFunction((PetscObject)B,"MatSeqDenseSetPreallocation_C",(void (**)(void))&f);
1747:   if (f) {
1748:     (*f)(B,data);
1749:   }
1750:   return(0);
1751: }

1756: PetscErrorCode PETSCMAT_DLLEXPORT MatSeqDenseSetPreallocation_SeqDense(Mat B,PetscScalar *data)
1757: {
1758:   Mat_SeqDense   *b;

1762:   B->preallocated = PETSC_TRUE;
1763:   b               = (Mat_SeqDense*)B->data;
1764:   if (!data) {
1765:     PetscMalloc((b->lda*b->Nmax+1)*sizeof(PetscScalar),&b->v);
1766:     PetscMemzero(b->v,b->lda*b->Nmax*sizeof(PetscScalar));
1767:     b->user_alloc = PETSC_FALSE;
1768:     PetscLogObjectMemory(B,b->lda*b->Nmax*sizeof(PetscScalar));
1769:   } else { /* user-allocated storage */
1770:     b->v          = data;
1771:     b->user_alloc = PETSC_TRUE;
1772:   }
1773:   return(0);
1774: }

1779: /*@C
1780:   MatSeqDenseSetLDA - Declare the leading dimension of the user-provided array

1782:   Input parameter:
1783: + A - the matrix
1784: - lda - the leading dimension

1786:   Notes:
1787:   This routine is to be used in conjunction with MatSeqDenseSetPreallocation;
1788:   it asserts that the preallocation has a leading dimension (the LDA parameter
1789:   of Blas and Lapack fame) larger than M, the first dimension of the matrix.

1791:   Level: intermediate

1793: .keywords: dense, matrix, LAPACK, BLAS

1795: .seealso: MatCreate(), MatCreateSeqDense(), MatSeqDenseSetPreallocation(), MatSetMaximumSize()
1796: @*/
1797: PetscErrorCode PETSCMAT_DLLEXPORT MatSeqDenseSetLDA(Mat B,PetscInt lda)
1798: {
1799:   Mat_SeqDense *b = (Mat_SeqDense*)B->data;

1802:   if (lda < B->rmap.n) SETERRQ2(PETSC_ERR_ARG_SIZ,"LDA %D must be at least matrix dimension %D",lda,B->rmap.n);
1803:   b->lda       = lda;
1804:   b->changelda = PETSC_FALSE;
1805:   b->Mmax      = PetscMax(b->Mmax,lda);
1806:   return(0);
1807: }

1809: /*MC
1810:    MATSEQDENSE - MATSEQDENSE = "seqdense" - A matrix type to be used for sequential dense matrices.

1812:    Options Database Keys:
1813: . -mat_type seqdense - sets the matrix type to "seqdense" during a call to MatSetFromOptions()

1815:   Level: beginner

1817: .seealso: MatCreateSeqDense()

1819: M*/

1824: PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqDense(Mat B)
1825: {
1826:   Mat_SeqDense   *b;
1828:   PetscMPIInt    size;

1831:   MPI_Comm_size(B->comm,&size);
1832:   if (size > 1) SETERRQ(PETSC_ERR_ARG_WRONG,"Comm must be of size 1");

1834:   B->rmap.bs = B->cmap.bs = 1;
1835:   PetscMapInitialize(B->comm,&B->rmap);
1836:   PetscMapInitialize(B->comm,&B->cmap);

1838:   PetscNew(Mat_SeqDense,&b);
1839:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
1840:   B->factor       = 0;
1841:   B->mapping      = 0;
1842:   PetscLogObjectMemory(B,sizeof(struct _p_Mat));
1843:   B->data         = (void*)b;


1846:   b->pivots       = 0;
1847:   b->roworiented  = PETSC_TRUE;
1848:   b->v            = 0;
1849:   b->lda          = B->rmap.n;
1850:   b->changelda    = PETSC_FALSE;
1851:   b->Mmax         = B->rmap.n;
1852:   b->Nmax         = B->cmap.n;

1854:   PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqDenseSetPreallocation_C",
1855:                                     "MatSeqDenseSetPreallocation_SeqDense",
1856:                                      MatSeqDenseSetPreallocation_SeqDense);
1857:   return(0);
1858: }