Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2517 eromero 1
/*
2
   Interface for MatDense class.
3
 
4
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5
   SLEPc - Scalable Library for Eigenvalue Problem Computations
6
   Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
7
 
8
   This file is part of SLEPc.
9
 
10
   SLEPc is free software: you can redistribute it and/or modify it under  the
11
   terms of version 3 of the GNU Lesser General Public License as published by
12
   the Free Software Foundation.
13
 
14
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
15
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
16
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
17
   more details.
18
 
19
   You  should have received a copy of the GNU Lesser General  Public  License
20
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22
*/
23
 
24
#include <private/matdenseimpl.h>            /*I "slepcmatdense.h" I*/
25
#include <petscblaslapack.h>
26
 
27
PetscFList       MatDenseList = 0;
28
PetscBool        MatDenseRegisterAllCalled = PETSC_FALSE;
2533 eromero 29
PetscClassId     MATDENSE_CLASSID = 0;
2544 eromero 30
PetscLogEvent    MATDENSE_Duplicate = 0,MATDENSE_SetUpPreallocation = 0,MATDENSE_MatMult = 0,MATDENSE_Copy = 0,MATDENSE_AXPY = 0,MATDENSE_View = 0,MATDENSE_Convert = 0,MATDENSE_BlasMatMult = 0;
2518 eromero 31
static PetscBool MatDensePackageInitialized = PETSC_FALSE;
2517 eromero 32
 
33
#undef __FUNCT__
34
#define __FUNCT__ "MatDenseCreate"
35
/*@
36
   MatDenseCreate - Creates an empty dense matrix object. The type can then be
37
   set with MatDenseSetType(), or MatDenseSetFromOptions().
38
 
39
   Collective on MatDense
40
 
41
   Input Parameters:
42
.  m - the dense matrix
43
 
44
   Note:
45
   If you never call MatDenseSetType() or MatDenseFromOptions() it will generate
46
   an error when you try to use the object.
47
 
48
   Level: developer
49
@*/
2522 eromero 50
PetscErrorCode MatDenseCreate(MPI_Comm comm,MatDense *A)
2517 eromero 51
{
52
  MatDense        B;
53
  PetscErrorCode  ierr;
54
 
55
  PetscFunctionBegin;
56
  PetscValidPointer(A,1);
57
  *A = PETSC_NULL;
2852 eromero 58
  ierr = PetscHeaderCreate(B,_p_MatDense,struct _MatDenseOps,MATDENSE_CLASSID,0,"MatDense","Dense Matrix","MatDense",comm,MatDenseDestroy,MatDenseView);CHKERRQ(ierr);
2517 eromero 59
  *A = B;
60
  B->data = PETSC_NULL;
61
  B->ld = 0;
2852 eromero 62
  B->Mmax = B->Nmax = 0;
2517 eromero 63
  B->m0 = B->n0 = B->m = B->n = 0;
64
  B->is_allocated = PETSC_FALSE;
65
  B->is_hermitian = B->is_triangular = B->is_impl = PETSC_FALSE;
2560 eromero 66
  B->matmult_buffersize = 0;
2852 eromero 67
  B->use_impl = PETSC_TRUE;
68
  B->use_mpi_queue = PETSC_FALSE;
69
  B->n_getarray = 0;
70
  B->is_pending = PETSC_FALSE;
2517 eromero 71
  PetscFunctionReturn(0);
72
}
73
 
74
#undef __FUNCT__
75
#define __FUNCT__ "MatDenseSetMaxSizes"
76
/*@
2533 eromero 77
   MatDenseSetMaxSizes - Sets the maximum size of rows (also called
78
   leading dimension) and columns.
2517 eromero 79
 
80
   Collective on MatDense
81
 
82
   Input Parameters:
83
+  A - the dense matrix
2533 eromero 84
.  M - the maximum rows size
85
-  N - the maximum columns size
2517 eromero 86
 
87
   Level: developer
88
@*/
2533 eromero 89
PetscErrorCode MatDenseSetMaxSizes(MatDense A, PetscInt M, PetscInt N)
2517 eromero 90
{
91
  PetscFunctionBegin;
2533 eromero 92
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
2517 eromero 93
  if (A->is_allocated == PETSC_TRUE) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot change/reset sizes of the matrix after set the data");
2533 eromero 94
  if (M <= 0 || N <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Invalid dimension value");
95
  A->ld = A->Mmax = M;
96
  A->Nmax = N;
2517 eromero 97
  PetscFunctionReturn(0);
98
}
99
 
100
#undef __FUNCT__  
101
#define __FUNCT__ "MatDenseSetSizes"
102
/*@
103
  MatDenseSetSizes - Sets the local sizes.
104
 
105
  Collective on MatDense
106
 
107
  Input Parameters:
108
+  A - the matrix
2522 eromero 109
.  m0 - displacement of the first row
110
.  n0 - displacement of the first column
111
.  m - number of rows
112
-  n - number of columns
2517 eromero 113
 
114
  Level: beginner
115
@*/
116
PetscErrorCode MatDenseSetSizes(MatDense A,PetscInt m0,PetscInt n0,PetscInt m,PetscInt n)
117
{
118
  PetscErrorCode ierr;
119
 
120
  PetscFunctionBegin;
2533 eromero 121
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
122
  if (m0 < 0 || m0 > A->Mmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local column size %D invalid or larger than maximum column size %D",m0,A->Mmax);
123
  if (n0 < 0 || n0 > A->Nmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local row size %D invalid or larger than maximum row size %D",n0,A->Nmax);
124
  if (m < 0 || m0+m > A->Mmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local column size %D invalid or larger than maximum column size %D",m0+m,A->Mmax);
125
  if (n < 0 || n0+n > A->Nmax) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Local row size %D invalid or larger than maximum row size %D",n0+n,A->Nmax);
2852 eromero 126
  if (A->n_getarray > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"The sizes cannot be changed when a MatDenseRestore* call is pending");
2517 eromero 127
  if (A->ops->setsizes) {
128
    ierr = (*A->ops->setsizes)(A,m0,n0,m,n);CHKERRQ(ierr);
129
  }
130
  A->m = m;
131
  A->n = n;
132
  A->m0 = m0;
133
  A->n0 = n0;
134
  PetscFunctionReturn(0);
135
}
136
 
2522 eromero 137
#undef __FUNCT__  
138
#define __FUNCT__ "MatDenseGetSizes"
139
/*@
140
   MatDenseGetSizes - Returns the current numbers of rows and columns in a matrix.
141
 
142
   Not Collective
143
 
144
   Input Parameter:
145
.  mat - the matrix
146
 
147
   Output Parameters:
148
+  m0 - displacement of the first row
149
.  n0 - displacement of the first column
150
.  m - the number of rows
151
-  n - the number of columns
152
 
153
   Note: both output parameters can be PETSC_NULL on input.
154
 
155
   Level: beginner
156
 
157
   Concepts: matrices^size
158
@*/
159
PetscErrorCode  MatDenseGetSizes(MatDense mat,PetscInt *m0,PetscInt *n0,PetscInt *m,PetscInt* n)
160
{
161
  PetscFunctionBegin;
162
  PetscValidHeaderSpecific(mat,MATDENSE_CLASSID,1);
2852 eromero 163
  if (m0) *m0 = mat->m0;
164
  if (n0) *n0 = mat->n0;
2522 eromero 165
  if (m) *m = mat->m;
166
  if (n) *n = mat->n;
167
  PetscFunctionReturn(0);
168
}
169
 
170
 
2517 eromero 171
#undef __FUNCT__
172
#define __FUNCT__ "MatDenseDestroy"
173
/*@
174
   MatDenseDestroy - Frees space taken by a matrix.
175
 
176
   Collective on MatDense
177
 
178
   Input Parameter:
179
.  A - the matrix
180
 
181
   Level: beginner
182
 
183
@*/
2518 eromero 184
PetscErrorCode  MatDenseDestroy(MatDense *A)
2517 eromero 185
{
186
  PetscErrorCode ierr;
187
 
188
  PetscFunctionBegin;
189
  if (!*A) PetscFunctionReturn(0);
190
  PetscValidHeaderSpecific(*A,MATDENSE_CLASSID,1);
191
  if (--((PetscObject)(*A))->refct > 0) {*A = PETSC_NULL; PetscFunctionReturn(0);}
192
 
2852 eromero 193
  ierr = PetscCheckMatDenseForRead(*A);CHKERRQ(ierr);
2517 eromero 194
  if ((*A)->ops->destroy) {
2533 eromero 195
    ierr = (*(*A)->ops->destroy)(*A);CHKERRQ(ierr);
2517 eromero 196
  }
197
 
198
  ierr = PetscHeaderDestroy(A);CHKERRQ(ierr);
199
  PetscFunctionReturn(0);
200
}
201
 
202
#undef __FUNCT__  
203
#define __FUNCT__ "MatDenseGetArray"
204
/*@C
205
   MatDenseGetArray - Returns a pointer to the element values in the matrix.
206
   The result of this routine is dependent on the underlying matrix data
207
   structure, and may not even work for certain matrix types.  You MUST
208
   call MatRestoreArray() when you no longer need to access the array.
209
 
210
   Not Collective
211
 
212
   Input Parameter:
213
.  A - the matrix
214
 
215
   Output Parameter:
216
.  v - the location of the values
217
 
218
   Level: advanced
219
 
220
   Concepts: matrices^access array
221
 
222
.seealso: MatDenseRestoreArray()
223
@*/
224
PetscErrorCode  MatDenseGetArray(MatDense A,PetscScalar *v[])
225
{
226
  PetscErrorCode ierr;
227
 
228
  PetscFunctionBegin;
229
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
230
  PetscValidType(A,1);
231
  PetscValidPointer(v,2);
232
  if (!A->ops->getarray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatDense type %s",((PetscObject)A)->type_name);
2852 eromero 233
  ierr = PetscCheckMatDenseForUpdate(A);CHKERRQ(ierr);
2517 eromero 234
  ierr = (*A->ops->getarray)(A,v);CHKERRQ(ierr);
2852 eromero 235
  A->n_getarray++;
2517 eromero 236
  CHKMEMQ;
237
  PetscFunctionReturn(0);
238
}
239
 
240
#undef __FUNCT__  
241
#define __FUNCT__ "MatDenseRestoreArray"
242
/*@C
243
   MatDenseRestoreArray - Restores the matrix after MatDenseGetArray() has been called.
244
 
245
   Not Collective
246
 
247
   Input Parameter:
248
+  A - the matrix
249
-  v - the location of the values
250
 
251
   Level: advanced
252
 
253
.seealso: MatGetArray(), MatRestoreArrayF90()
254
@*/
255
PetscErrorCode  MatDenseRestoreArray(MatDense A,PetscScalar *v[])
256
{
257
  PetscErrorCode ierr;
258
 
259
  PetscFunctionBegin;
2533 eromero 260
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
2517 eromero 261
  PetscValidType(A,1);
262
  PetscValidPointer(v,2);
263
#if defined(PETSC_USE_DEBUG)
264
  CHKMEMQ;
265
#endif
2852 eromero 266
  A->n_getarray--;
2517 eromero 267
  if (!A->ops->restorearray) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatDense type %s",((PetscObject)A)->type_name);
268
  ierr = (*A->ops->restorearray)(A,v);CHKERRQ(ierr);
269
  ierr = PetscObjectStateIncrease((PetscObject)A);CHKERRQ(ierr);
270
  PetscFunctionReturn(0);
271
}
272
 
273
#undef __FUNCT__  
274
#define __FUNCT__ "MatDenseGetArrayRead"
275
/*@C
276
   MatDenseGetArrayRead - Returns a pointer to the element values in the matrix.
277
   The result of this routine is dependent on the underlying matrix data
278
   structure, and may not even work for certain matrix types.  You MUST
279
   call MatRestoreArrayRead() when you no longer need to access the array.
280
 
281
   Not Collective
282
 
283
   Input Parameter:
284
.  A - the matrix
285
 
286
   Output Parameter:
287
.  v - the location of the values
288
 
289
   Level: advanced
290
 
291
   Note:
292
   The routine considers that the values of the array will not be modified.
293
 
294
   Concepts: matrices^access array
295
 
296
.seealso: MatDenseRestoreArrayRead()
297
@*/
2544 eromero 298
PetscErrorCode  MatDenseGetArrayRead(MatDense A,const PetscScalar *v[])
2517 eromero 299
{
300
  PetscErrorCode ierr;
301
 
302
  PetscFunctionBegin;
303
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
304
  PetscValidType(A,1);
305
  PetscValidPointer(v,2);
306
  if (!A->ops->getarrayread) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatDense type %s",((PetscObject)A)->type_name);
2852 eromero 307
  ierr = PetscCheckMatDenseForRead(A);CHKERRQ(ierr);
2517 eromero 308
  ierr = (*A->ops->getarrayread)(A,v);CHKERRQ(ierr);
309
  CHKMEMQ;
310
  PetscFunctionReturn(0);
311
}
312
 
313
#undef __FUNCT__  
314
#define __FUNCT__ "MatDenseRestoreArrayRead"
315
/*@C
316
   MatDenseRestoreArrayRead - Restores the matrix after MatDenseGetArrayRead() has been called.
317
 
318
   Not Collective
319
 
320
   Input Parameter:
321
+  A - the matrix
322
-  v - the location of the values
323
 
324
   Level: advanced
325
 
326
.seealso: MatDenseGetArrayRead()
327
@*/
2544 eromero 328
PetscErrorCode  MatDenseRestoreArrayRead(MatDense A,const PetscScalar *v[])
2517 eromero 329
{
330
  PetscErrorCode ierr;
331
 
332
  PetscFunctionBegin;
2533 eromero 333
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
2517 eromero 334
  PetscValidType(A,1);
335
  PetscValidPointer(v,2);
336
#if defined(PETSC_USE_DEBUG)
337
  CHKMEMQ;
338
#endif
339
  if (!A->ops->restorearrayread) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatDense type %s",((PetscObject)A)->type_name);
340
  ierr = (*A->ops->restorearrayread)(A,v);CHKERRQ(ierr);
341
  PetscFunctionReturn(0);
342
}
343
 
344
 
345
#undef __FUNCT__  
346
#define __FUNCT__ "MatDenseDuplicate"
347
/*@
348
   MatDenseDuplicate - Duplicates a matrix.
349
 
350
   Collective on MatDense
351
 
352
   Input Parameters:
353
+  A - the matrix
2544 eromero 354
-  op - MATDENSE_COPY_VALUES copies the numerical values in the matrix, MATDENSE_DO_NOT_COPY_VALUES avoids that, and MATDENSE_MAKE_SIBLING makes A and M share the same data.
2517 eromero 355
 
356
   Output Parameter:
357
.  M - pointer to place new matrix
358
 
359
   Level: intermediate
360
 
361
   Concepts: matrices^duplicating
362
 
363
.seealso: MatDenseCopy()
364
@*/
365
PetscErrorCode  MatDenseDuplicate(MatDense A,MatDenseDuplicateOption op,MatDense *M)
366
{
367
  PetscErrorCode ierr;
368
  MatDense       B;
369
 
370
  PetscFunctionBegin;
371
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
372
  PetscValidType(A,1);
373
  PetscValidPointer(M,3);
2852 eromero 374
  ierr = PetscCheckMatDenseForRead(A);CHKERRQ(ierr);
2517 eromero 375
 
376
  *M = 0;
377
  if (!A->ops->duplicate) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_SUP,"Not written for this matrix type");
378
  ierr = PetscLogEventBegin(MATDENSE_Duplicate,A,0,0,0);CHKERRQ(ierr);
379
  ierr = (*A->ops->duplicate)(A,op,M);CHKERRQ(ierr);
380
  ierr = PetscLogEventEnd(MATDENSE_Duplicate,A,0,0,0);CHKERRQ(ierr);
381
  B = *M;
2544 eromero 382
  if (op == MATDENSE_MAKE_SIBLING) {
383
    B->ld = A->ld;
384
    B->is_allocated = A->is_allocated;
385
    B->is_hermitian = A->is_hermitian;
386
    B->is_triangular = A->is_triangular;
387
    B->is_impl = A->is_impl;
2560 eromero 388
    B->matmult_buffersize = A->matmult_buffersize;
2852 eromero 389
    B->use_impl = A->use_impl;
390
    B->use_mpi_queue = A->use_mpi_queue;
391
    B->n_getarray = 0;
392
    B->is_pending = PETSC_FALSE;
2544 eromero 393
  }
2517 eromero 394
  ierr = PetscObjectStateIncrease((PetscObject)B);CHKERRQ(ierr);
395
  PetscFunctionReturn(0);
396
}
397
 
2544 eromero 398
 
2517 eromero 399
#undef __FUNCT__  
2544 eromero 400
#define __FUNCT__ "MatDenseAreSiblings"
401
/*@C
402
   MatDenseAreSiblings - return whether one matrix comes from MatDenseDuplicte
403
   of the other with the MATDENSE_MAKE_SIBLING option.
404
 
405
   Not Collective
406
 
407
   Input Parameter:
408
.  A,B - the matrices
409
 
410
   Output Parameter:
411
.  aresiblings - whether the matrices are siblings
412
 
413
   Level: advanced
414
 
415
.seealso: MatDenseDuplicate()
416
@*/
417
PetscErrorCode  MatDenseAreSiblings(MatDense A,MatDense B,PetscBool *aresiblings)
418
{
419
  PetscErrorCode ierr;
420
 
421
  PetscFunctionBegin;
422
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
423
  PetscValidType(A,1);
424
  PetscValidHeaderSpecific(B,MATDENSE_CLASSID,2);
425
  PetscValidType(B,2);
426
  PetscValidPointer(aresiblings,3);
427
  if (A->ops->aresiblings != B->ops->aresiblings)
428
    *aresiblings = PETSC_FALSE;
429
  else if (A->ops->aresiblings) {
430
    ierr = (*A->ops->aresiblings)(A,B,aresiblings);CHKERRQ(ierr);
431
  } else {
432
    *aresiblings = A->data == B->data ? PETSC_TRUE : PETSC_FALSE;
433
  }
434
  PetscFunctionReturn(0);
435
}
436
 
437
 
438
#undef __FUNCT__  
2517 eromero 439
#define __FUNCT__ "MatDenseSetFromOptions"
440
/*@
441
   MatDenseSetFromOptions - Creates a matrix where the type is determined
442
   from the options database. The default matrix type is
443
   VEC, using the routines MatDenseCreateVec() if
444
   you do not select a type in the options database.
445
 
446
   Collective on MatDense
447
 
448
   Input Parameter:
449
.  A - the matrix
450
 
451
   Options Database Keys:
452
+    -mat_type vec     - VEC type, uses MatDenseCreateVec()
453
-    -mat_type magma   - MAGMA type, uses MatDenseCreateMAGMA()
454
 
455
   Even More Options Database Keys:
456
   See the manpages for particular formats (e.g., MatDenseCreateVec())
457
   for additional format-specific options.
458
 
459
   Level: beginner
460
 
461
.keywords: matrix, create
462
 
463
.seealso: MatDenseCreateVec(), MatDenseCreateMAGMA()
464
@*/
465
PetscErrorCode  MatDenseSetFromOptions(MatDense A)
466
{
467
  PetscErrorCode ierr;
2533 eromero 468
  const char     *deft = MATDENSEBASIC;
2517 eromero 469
  char           type[256];
2852 eromero 470
  PetscBool      flg,opb;
2560 eromero 471
  PetscInt       opi;
2517 eromero 472
 
473
  PetscFunctionBegin;
2533 eromero 474
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
2517 eromero 475
 
476
  ierr = PetscOptionsBegin(((PetscObject)A)->comm,((PetscObject)A)->prefix,"Dense matrix options","MatDense");CHKERRQ(ierr);
477
    ierr = PetscOptionsList("-matdense_type","Dense matrix type","MatDenseSetType",MatDenseList,deft,type,256,&flg);CHKERRQ(ierr);
478
    if (flg) {
479
      ierr = MatDenseSetType(A,type);CHKERRQ(ierr);
480
    } else if (!((PetscObject)A)->type_name) {
481
      ierr = MatDenseSetType(A,deft);CHKERRQ(ierr);
482
    }
2560 eromero 483
    ierr = PetscOptionsInt("-matdense_buffersize","buffersize(KB) for matrix-matrix multiplication","",A->matmult_buffersize/1024,&opi,&flg);CHKERRQ(ierr);
484
    if (flg) {
485
      A->matmult_buffersize = opi*1024;
486
    }
2852 eromero 487
    ierr = PetscOptionsBool("-matdense_enhance_structures","Call structured functions for dense matrices","",A->use_impl,&opb,&flg);CHKERRQ(ierr);
488
    if (flg == PETSC_TRUE) A->use_impl = opb;
489
    ierr = PetscOptionsBool("-matdense_mpi_queue","Queue synchronous MPI calls","",A->use_mpi_queue,&opb,&flg);CHKERRQ(ierr);
490
    if (flg == PETSC_TRUE) A->use_mpi_queue = opb;
2517 eromero 491
 
492
    if (A->ops->setfromoptions) {
493
      ierr = (*A->ops->setfromoptions)(A);CHKERRQ(ierr);
494
    }
495
 
496
    /* process any options handlers added with PetscObjectAddOptionsHandler() */
2518 eromero 497
    ierr = PetscObjectProcessOptionsHandlers((PetscObject)A);CHKERRQ(ierr);
2517 eromero 498
  ierr = PetscOptionsEnd();CHKERRQ(ierr);
499
 
500
  PetscFunctionReturn(0);
501
}
502
 
503
#undef __FUNCT__  
504
#define __FUNCT__ "MatDenseSetUpPreallocation"
505
/*@
506
   MatDenseSetUpPreallocation
507
 
508
   Collective on MatDense
509
 
510
   Input Parameter:
511
.  B - the matrix
512
 
513
   Level: beginner
514
 
515
.keywords: matrix, create
516
@*/
517
PetscErrorCode  MatDenseSetUpPreallocation(MatDense B)
518
{
519
  PetscErrorCode ierr;
520
 
521
  PetscFunctionBegin;
2533 eromero 522
  PetscValidHeaderSpecific(B,MATDENSE_CLASSID,1);
2517 eromero 523
  if (B->is_allocated == PETSC_FALSE && B->ops->setuppreallocation) {
524
    ierr = PetscInfo(B,"Warning not preallocating matrix storage\n");CHKERRQ(ierr);
2518 eromero 525
    ierr = PetscLogEventBegin(MATDENSE_SetUpPreallocation,B,0,0,0);CHKERRQ(ierr);
2517 eromero 526
    ierr = (*B->ops->setuppreallocation)(B);CHKERRQ(ierr);
2522 eromero 527
    ierr = PetscLogEventEnd(MATDENSE_SetUpPreallocation,B,0,0,0);CHKERRQ(ierr);
528
    B->is_allocated = PETSC_TRUE;
2517 eromero 529
  }
530
  PetscFunctionReturn(0);
531
}
532
 
533
#undef __FUNCT__  
534
#define __FUNCT__ "MatDenseSetType"
535
/*@C
536
   MatDenseSetType - Builds matrix object for a particular matrix type
537
 
538
   Collective on MatDense
539
 
540
   Input Parameters:
541
+  mat      - the matrix object
542
-  matype   - matrix type
543
 
544
   Options Database Key:
545
.  -matdense_type  <method> - Sets the type; use -help for a list
546
    of available methods (for instance, seqaij)
547
 
548
  Level: intermediate
549
 
550
.keywords: MatDense, MatDenseType, set, method
551
 
552
.seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
553
@*/
554
PetscErrorCode  MatDenseSetType(MatDense mat, const MatDenseType matype)
555
{
556
  PetscErrorCode ierr,(*r)(MatDense);
557
  PetscBool      sametype;
558
 
559
  PetscFunctionBegin;
2533 eromero 560
  PetscValidHeaderSpecific(mat,MATDENSE_CLASSID,1);
2517 eromero 561
 
562
  ierr = PetscTypeCompare((PetscObject)mat,matype,&sametype);CHKERRQ(ierr);
563
  if (sametype) PetscFunctionReturn(0);
564
 
2533 eromero 565
  ierr =  PetscFListFind(MatDenseList,((PetscObject)mat)->comm,matype,PETSC_TRUE,(void(**)(void))&r);CHKERRQ(ierr);
2517 eromero 566
  if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatDense type given: %s",matype);
567
 
568
  /* free the old data structure if it existed */
569
  if (mat->ops->destroy) {
2533 eromero 570
    ierr = (*mat->ops->destroy)(mat);CHKERRQ(ierr);
2517 eromero 571
    mat->ops->destroy = PETSC_NULL;
572
  }
573
  mat->is_allocated = PETSC_FALSE;
574
 
575
  /* create the new data structure */
576
  ierr = (*r)(mat);CHKERRQ(ierr);
577
  PetscFunctionReturn(0);
578
}
579
 
2852 eromero 580
static PetscErrorCode MatDenseView_Default_ASCII(MatDense xin,PetscViewer viewer);
2522 eromero 581
 
582
#undef __FUNCT__  
583
#define __FUNCT__ "MatDenseView"
584
/*@C
585
   MatDenseView - Visualizes a dense matrix object.
586
 
587
   Collective on MatDense
588
 
589
   Input Parameters:
590
+  mat - the matrix
591
-  viewer - visualization context
592
 
593
  Notes:
594
  The available visualization contexts include
595
+    PETSC_VIEWER_STDOUT_SELF - standard output (default)
596
.    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
597
        output where only the first processor opens
598
        the file.  All other processors send their
599
        data to the first processor to print.
600
-     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
601
 
602
   The user can open alternative visualization contexts with
603
+    PetscViewerASCIIOpen() - Outputs matrix to a specified file
604
.    PetscViewerBinaryOpen() - Outputs matrix in binary to a
605
         specified file; corresponding input uses MatLoad()
606
.    PetscViewerDrawOpen() - Outputs nonzero matrix structure to
607
         an X window display
608
-    PetscViewerSocketOpen() - Outputs matrix to Socket viewer.
609
         Currently only the sequential dense and AIJ
610
         matrix types support the Socket viewer.
611
 
612
   The user can call PetscViewerSetFormat() to specify the output
613
   format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF,
614
   PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen).  Available formats include
615
+    PETSC_VIEWER_DEFAULT - default, prints matrix contents
616
.    PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format
617
.    PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros
618
.    PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse
619
         format common among all matrix types
620
.    PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific
621
         format (which is in many cases the same as the default)
622
.    PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix
623
         size and structure (not the matrix entries)
624
.    PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about
625
         the matrix structure
626
 
627
   Level: beginner
628
 
629
   Concepts: matrices^viewing
630
 
631
.seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
632
          PetscViewerSocketOpen(), PetscViewerBinaryOpen()
633
@*/
634
PetscErrorCode  MatDenseView(MatDense mat,PetscViewer viewer)
635
{
636
  PetscErrorCode    ierr;
637
  PetscInt          rows,cols;
638
  PetscBool         iascii;
639
  PetscViewerFormat format;
640
 
641
  PetscFunctionBegin;
642
  PetscValidHeaderSpecific(mat,MATDENSE_CLASSID,1);
643
  PetscValidType(mat,1);
644
  if (!viewer) {
645
    ierr = PetscViewerASCIIGetStdout(((PetscObject)mat)->comm,&viewer);CHKERRQ(ierr);
646
  }
647
  PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
648
  PetscCheckSameComm(mat,1,viewer,2);
2852 eromero 649
  ierr = PetscCheckMatDenseForRead(mat);CHKERRQ(ierr);
2522 eromero 650
 
651
  ierr = PetscLogEventBegin(MATDENSE_View,mat,viewer,0,0);CHKERRQ(ierr);
652
  ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
653
  if (iascii) {
654
    ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);  
655
    if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
656
      ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mat,viewer,"Matrix Object");CHKERRQ(ierr);
657
      ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
658
      ierr = MatDenseGetSizes(mat,PETSC_NULL,PETSC_NULL,&rows,&cols);CHKERRQ(ierr);
659
      ierr = PetscViewerASCIIPrintf(viewer,"rows=%D, cols=%D\n",rows,cols);CHKERRQ(ierr);
660
    }
661
  }
662
  if (mat->ops->view) {
663
    ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
664
    ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr);
665
    ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
2852 eromero 666
  } else if (iascii) {
667
    ierr = MatDenseView_Default_ASCII(mat,viewer);CHKERRQ(ierr);
668
  } else {
2522 eromero 669
    SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
670
  }
671
  if (iascii) {
672
    ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);  
673
    if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
674
      ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
675
    }
676
  }
677
  ierr = PetscLogEventEnd(MATDENSE_View,mat,viewer,0,0);CHKERRQ(ierr);
678
  PetscFunctionReturn(0);
679
}
680
 
2852 eromero 681
#undef __FUNCT__  
682
#define __FUNCT__ "MatDenseView_Default_ASCII"
683
static PetscErrorCode MatDenseView_Default_ASCII(MatDense xin,PetscViewer viewer)
684
{
685
  PetscErrorCode    ierr;
686
  PetscInt          i,j,n,m,n0,m0;
687
  const char        *name;
688
  PetscViewerFormat format;
689
  const PetscScalar *xv;
2522 eromero 690
 
2852 eromero 691
  PetscFunctionBegin;
692
  ierr = MatDenseGetSizes(xin,&m0,&n0,&m,&n);CHKERRQ(ierr);
693
  ierr = MatDenseGetArrayRead(xin,&xv);CHKERRQ(ierr);
694
  ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
695
  if (format == PETSC_VIEWER_ASCII_MATLAB) {
696
    ierr = PetscObjectGetName((PetscObject)xin,&name);CHKERRQ(ierr);
697
    ierr = PetscViewerASCIIPrintf(viewer,"%s = [\n",name);CHKERRQ(ierr);
698
    for (i=m0; i<m+m0; i++) {
699
      for (j=n0; j<n+n0; j++) {
700
        PetscScalar v = xv[xin->ld*j+i];
701
#if defined(PETSC_USE_COMPLEX)
702
        if (PetscImaginaryPart(xv[i]) > 0.0) {
703
          ierr = PetscViewerASCIIPrintf(viewer,"%18.16e + %18.16ei ",PetscRealPart(v),PetscImaginaryPart(v));CHKERRQ(ierr);
704
        } else if (PetscImaginaryPart(v) < 0.0) {
705
          ierr = PetscViewerASCIIPrintf(viewer,"%18.16e - %18.16ei ",PetscRealPart(v),-PetscImaginaryPart(v));CHKERRQ(ierr);
706
        } else {
707
          ierr = PetscViewerASCIIPrintf(viewer,"%18.16e ",PetscRealPart(v));CHKERRQ(ierr);
708
        }
709
#else
710
        ierr = PetscViewerASCIIPrintf(viewer,"%18.16e ",(double) v);CHKERRQ(ierr);
711
#endif
712
      }
713
      ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
714
    }
715
    ierr = PetscViewerASCIIPrintf(viewer,"];\n");CHKERRQ(ierr);
716
  } else {
717
    ierr = PetscObjectPrintClassNamePrefixType((PetscObject)xin,viewer,"Vector Object");CHKERRQ(ierr);
718
    for (i=m0; i<m+m0; i++) {
719
      ierr = PetscViewerASCIIPrintf(viewer,"Row %####d: ",i);CHKERRQ(ierr);
720
      for (j=n0; j<n+n0; j++) {
721
        PetscScalar v = xv[xin->ld*j+i];
722
#if defined(PETSC_USE_COMPLEX)
723
        if (PetscImaginaryPart(v) > 0.0) {
724
          ierr = PetscViewerASCIIPrintf(viewer,"%G + %G i ",PetscRealPart(v),PetscImaginaryPart(v));CHKERRQ(ierr);
725
        } else if (PetscImaginaryPart(v) < 0.0) {
726
          ierr = PetscViewerASCIIPrintf(viewer,"%G - %G i ",PetscRealPart(v),-PetscImaginaryPart(v));CHKERRQ(ierr);
727
        } else {
728
          ierr = PetscViewerASCIIPrintf(viewer,"%G ",PetscRealPart(v));CHKERRQ(ierr);
729
        }
730
#else
731
        ierr = PetscViewerASCIIPrintf(viewer,"%G ",(double) v);CHKERRQ(ierr);
732
#endif
733
      }
734
      ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
735
    }
736
  }
737
  ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
738
  ierr = MatDenseRestoreArrayRead(xin,&xv);CHKERRQ(ierr);
739
  PetscFunctionReturn(0);
740
}
741
 
2517 eromero 742
#undef __FUNCT__
743
#define __FUNCT__ "MatDenseFinalizePackage"
744
/*@C
745
  MatDenseFinalizePackage - This function destroys everything in the Slepc interface to the MatDense package. It is
746
  called from SlepcFinalize().
747
 
748
  Level: developer
749
 
750
.seealso: SlepcFinalize()
751
@*/
752
PetscErrorCode MatDenseFinalizePackage(void)
753
{
754
  PetscFunctionBegin;
755
  MatDensePackageInitialized = PETSC_FALSE;
756
  MatDenseList               = 0;
757
  MatDenseRegisterAllCalled  = PETSC_FALSE;
758
  PetscFunctionReturn(0);
759
}
760
 
761
#undef __FUNCT__
762
#define __FUNCT__ "MatDenseInitializePackage"
763
/*@C
764
  MatDenseInitializePackage - This function initializes everything in the EPSDense package. It is called
765
  from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to MatDenseCreate()
766
  when using static libraries.
767
 
768
  Input Parameter:
769
  path - The dynamic library path, or PETSC_NULL
770
 
771
  Level: developer
772
 
773
.seealso: SlepcInitialize()
774
@*/
775
PetscErrorCode MatDenseInitializePackage(const char *path) {
776
  char           logList[256];
777
  char           *className;
778
  PetscBool      opt;
779
  PetscErrorCode ierr;
780
 
781
  PetscFunctionBegin;
782
  if (MatDensePackageInitialized) PetscFunctionReturn(0);
783
  MatDensePackageInitialized = PETSC_TRUE;
784
  /* Register Classes */
785
  ierr = PetscClassIdRegister("MatDense",&MATDENSE_CLASSID);CHKERRQ(ierr);
786
  /* Register Constructors */
787
  ierr = MatDenseRegisterAll(path);CHKERRQ(ierr);
788
  /* Register Events */
789
  ierr = PetscLogEventRegister("MatDenseDuplicate",MATDENSE_CLASSID,&MATDENSE_Duplicate);CHKERRQ(ierr);
2544 eromero 790
  ierr = PetscLogEventRegister("MatDensePrealloc",MATDENSE_CLASSID,&MATDENSE_SetUpPreallocation);CHKERRQ(ierr);
791
  ierr = PetscLogEventRegister("MatDenseMatMult",MATDENSE_CLASSID,&MATDENSE_MatMult);CHKERRQ(ierr);
792
  ierr = PetscLogEventRegister("MatDenseBlasMatMult",MATDENSE_CLASSID,&MATDENSE_BlasMatMult);CHKERRQ(ierr);
2517 eromero 793
  ierr = PetscLogEventRegister("MatDenseCopy",MATDENSE_CLASSID,&MATDENSE_Copy);CHKERRQ(ierr);
794
  ierr = PetscLogEventRegister("MatDenseAXPY",MATDENSE_CLASSID,&MATDENSE_AXPY);CHKERRQ(ierr);
2522 eromero 795
  ierr = PetscLogEventRegister("MatDenseView",MATDENSE_CLASSID,&MATDENSE_View);CHKERRQ(ierr);
796
  ierr = PetscLogEventRegister("MatDenseConvert",MATDENSE_CLASSID,&MATDENSE_Convert);CHKERRQ(ierr);
2517 eromero 797
  /* Process info exclusions */
798
  ierr = PetscOptionsGetString(PETSC_NULL,"-info_exclude",logList,256,&opt);CHKERRQ(ierr);
799
  if (opt) {
800
    ierr = PetscStrstr(logList,"matdense",&className);CHKERRQ(ierr);
801
    if (className) {
802
      ierr = PetscInfoDeactivateClass(MATDENSE_CLASSID);CHKERRQ(ierr);
803
    }
804
  }
805
  /* Process summary exclusions */
806
  ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_exclude",logList,256,&opt);CHKERRQ(ierr);
807
  if (opt) {
808
    ierr = PetscStrstr(logList,"matdense",&className);CHKERRQ(ierr);
809
    if (className) {
810
      ierr = PetscLogEventDeactivateClass(MATDENSE_CLASSID);CHKERRQ(ierr);
811
    }
812
  }
813
  ierr = PetscRegisterFinalize(MatDenseFinalizePackage);CHKERRQ(ierr);
814
  PetscFunctionReturn(0);
815
}
816
 
817
#undef __FUNCT__
818
#define __FUNCT__ "MatDenseGetType"
819
/*@C
820
   MatDenseGetType - Gets the MatDense type as a string from the MatDense object.
821
 
822
   Not Collective
823
 
824
   Input Parameter:
825
.  A - the dense matrix
826
 
827
   Output Parameter:
828
.  name - name of MatDense method
829
 
830
   Level: intermediate
831
 
832
.seealso: MatDenseSetType()
833
@*/
834
PetscErrorCode MatDenseGetType(MatDense A,const MatDenseType *type)
835
{
836
  PetscFunctionBegin;
837
  PetscValidHeaderSpecific(A,MATDENSE_CLASSID,1);
838
  PetscValidPointer(type,2);
839
  *type = ((PetscObject)A)->type_name;
840
  PetscFunctionReturn(0);
841
}
842
 
843
#undef __FUNCT__
844
#define __FUNCT__ "MatDenseRegister"
845
/*@C
846
  MatDenseRegister - See MatDenseRegisterDynamic()
847
 
848
  Level: advanced
849
@*/
850
PetscErrorCode MatDenseRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(MatDense))
851
{
852
  PetscErrorCode ierr;
853
  char           fullname[PETSC_MAX_PATH_LEN];
854
 
855
  PetscFunctionBegin;
856
  ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
857
  ierr = PetscFListAdd(&MatDenseList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
858
  PetscFunctionReturn(0);
859
}
860
 
861
#undef __FUNCT__
862
#define __FUNCT__ "MatDenseRegisterDestroy"
863
/*@
864
   MatDenseRegisterDestroy - Frees the list of MatDense methods that were
865
   registered by MatDenseRegisterDynamic().
866
 
867
   Not Collective
868
 
869
   Level: advanced
870
 
871
.seealso: MatDenseRegisterDynamic(), MatDenseRegisterAll()
872
@*/
873
PetscErrorCode MatDenseRegisterDestroy(void)
874
{
875
  PetscErrorCode ierr;
876
 
877
  PetscFunctionBegin;
878
  ierr = PetscFListDestroy(&MatDenseList);CHKERRQ(ierr);
879
  MatDenseRegisterAllCalled = PETSC_FALSE;
880
  PetscFunctionReturn(0);
881
}