forked from libMesh/libmesh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpetsc_matrix.C
1281 lines (974 loc) · 37.1 KB
/
petsc_matrix.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The libMesh Finite Element Library.
// Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// C++ includes
#include <unistd.h> // mkstemp
#include <fstream>
#include "libmesh/libmesh_config.h"
#ifdef LIBMESH_HAVE_PETSC
// Local includes
#include "libmesh/petsc_matrix.h"
#include "libmesh/dof_map.h"
#include "libmesh/dense_matrix.h"
#include "libmesh/petsc_vector.h"
// For some reason, the blocked matrix API calls below seem to break with PETSC 3.2 & presumably earlier.
// For example:
// [0]PETSC ERROR: --------------------- Error Message ------------------------------------
// [0]PETSC ERROR: Nonconforming object sizes!
// [0]PETSC ERROR: Attempt to set block size 3 with BAIJ 1!
// [0]PETSC ERROR: ------------------------------------------------------------------------
// so as a cowardly workaround disable the functionality in this translation unit for older PETSc's
#if PETSC_VERSION_LESS_THAN(3,3,0)
# undef LIBMESH_ENABLE_BLOCKED_STORAGE
#endif
#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
namespace
{
using namespace libMesh;
// historic libMesh n_nz & n_oz arrays are set up for PETSc's AIJ format.
// however, when the blocksize is >1, we need to transform these into
// their BAIJ counterparts.
inline
void transform_preallocation_arrays (const PetscInt blocksize,
const std::vector<numeric_index_type> & n_nz,
const std::vector<numeric_index_type> & n_oz,
std::vector<numeric_index_type> & b_n_nz,
std::vector<numeric_index_type> & b_n_oz)
{
libmesh_assert_equal_to (n_nz.size(), n_oz.size());
libmesh_assert_equal_to (n_nz.size()%blocksize, 0);
b_n_nz.clear(); /**/ b_n_nz.reserve(n_nz.size()/blocksize);
b_n_oz.clear(); /**/ b_n_oz.reserve(n_oz.size()/blocksize);
for (std::size_t nn=0; nn<n_nz.size(); nn += blocksize)
{
b_n_nz.push_back (n_nz[nn]/blocksize);
b_n_oz.push_back (n_oz[nn]/blocksize);
}
}
}
#endif
namespace libMesh
{
//-----------------------------------------------------------------------
// PetscMatrix members
// Constructor
template <typename T>
PetscMatrix<T>::PetscMatrix(const Parallel::Communicator & comm_in) :
SparseMatrix<T>(comm_in),
_destroy_mat_on_exit(true),
_mat_type(AIJ)
{}
// Constructor taking an existing Mat but not the responsibility
// for destroying it
template <typename T>
PetscMatrix<T>::PetscMatrix(Mat mat_in,
const Parallel::Communicator & comm_in) :
SparseMatrix<T>(comm_in),
_destroy_mat_on_exit(false),
_mat_type(AIJ)
{
this->_mat = mat_in;
this->_is_initialized = true;
}
// Destructor
template <typename T>
PetscMatrix<T>::~PetscMatrix()
{
this->clear();
}
template <typename T>
void PetscMatrix<T>::set_matrix_type(PetscMatrixType mat_type)
{
_mat_type = mat_type;
}
template <typename T>
void PetscMatrix<T>::init (const numeric_index_type m_in,
const numeric_index_type n_in,
const numeric_index_type m_l,
const numeric_index_type n_l,
const numeric_index_type nnz,
const numeric_index_type noz,
const numeric_index_type blocksize_in)
{
// So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
libmesh_ignore(blocksize_in);
// Clear initialized matrices
if (this->initialized())
this->clear();
this->_is_initialized = true;
PetscErrorCode ierr = 0;
PetscInt m_global = static_cast<PetscInt>(m_in);
PetscInt n_global = static_cast<PetscInt>(n_in);
PetscInt m_local = static_cast<PetscInt>(m_l);
PetscInt n_local = static_cast<PetscInt>(n_l);
PetscInt n_nz = static_cast<PetscInt>(nnz);
PetscInt n_oz = static_cast<PetscInt>(noz);
ierr = MatCreate(this->comm().get(), &_mat);
LIBMESH_CHKERR(ierr);
ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
LIBMESH_CHKERR(ierr);
PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
ierr = MatSetBlockSize(_mat,blocksize);
LIBMESH_CHKERR(ierr);
#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
if (blocksize > 1)
{
// specified blocksize, bs>1.
// double check sizes.
libmesh_assert_equal_to (m_local % blocksize, 0);
libmesh_assert_equal_to (n_local % blocksize, 0);
libmesh_assert_equal_to (m_global % blocksize, 0);
libmesh_assert_equal_to (n_global % blocksize, 0);
libmesh_assert_equal_to (n_nz % blocksize, 0);
libmesh_assert_equal_to (n_oz % blocksize, 0);
ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
LIBMESH_CHKERR(ierr);
ierr = MatSeqBAIJSetPreallocation(_mat, blocksize, n_nz/blocksize, NULL);
LIBMESH_CHKERR(ierr);
ierr = MatMPIBAIJSetPreallocation(_mat, blocksize,
n_nz/blocksize, NULL,
n_oz/blocksize, NULL);
LIBMESH_CHKERR(ierr);
}
else
#endif
{
switch (_mat_type) {
case AIJ:
ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
LIBMESH_CHKERR(ierr);
ierr = MatSeqAIJSetPreallocation(_mat, n_nz, NULL);
LIBMESH_CHKERR(ierr);
ierr = MatMPIAIJSetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
LIBMESH_CHKERR(ierr);
break;
case HYPRE:
#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
ierr = MatSetType(_mat, MATHYPRE);
LIBMESH_CHKERR(ierr);
ierr = MatHYPRESetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
LIBMESH_CHKERR(ierr);
#else
libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
#endif
break;
default: libmesh_error_msg("Unsupported petsc matrix type");
}
}
// Make it an error for PETSc to allocate new nonzero entries during assembly
#if PETSC_VERSION_LESS_THAN(3,0,0)
ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR);
#else
ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
#endif
LIBMESH_CHKERR(ierr);
// Is prefix information available somewhere? Perhaps pass in the system name?
ierr = MatSetOptionsPrefix(_mat, "");
LIBMESH_CHKERR(ierr);
ierr = MatSetFromOptions(_mat);
LIBMESH_CHKERR(ierr);
this->zero ();
}
template <typename T>
void PetscMatrix<T>::init (const numeric_index_type m_in,
const numeric_index_type n_in,
const numeric_index_type m_l,
const numeric_index_type n_l,
const std::vector<numeric_index_type> & n_nz,
const std::vector<numeric_index_type> & n_oz,
const numeric_index_type blocksize_in)
{
// So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
libmesh_ignore(blocksize_in);
// Clear initialized matrices
if (this->initialized())
this->clear();
this->_is_initialized = true;
// Make sure the sparsity pattern isn't empty unless the matrix is 0x0
libmesh_assert_equal_to (n_nz.size(), m_l);
libmesh_assert_equal_to (n_oz.size(), m_l);
PetscErrorCode ierr = 0;
PetscInt m_global = static_cast<PetscInt>(m_in);
PetscInt n_global = static_cast<PetscInt>(n_in);
PetscInt m_local = static_cast<PetscInt>(m_l);
PetscInt n_local = static_cast<PetscInt>(n_l);
ierr = MatCreate(this->comm().get(), &_mat);
LIBMESH_CHKERR(ierr);
ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
LIBMESH_CHKERR(ierr);
PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
ierr = MatSetBlockSize(_mat,blocksize);
LIBMESH_CHKERR(ierr);
#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
if (blocksize > 1)
{
// specified blocksize, bs>1.
// double check sizes.
libmesh_assert_equal_to (m_local % blocksize, 0);
libmesh_assert_equal_to (n_local % blocksize, 0);
libmesh_assert_equal_to (m_global % blocksize, 0);
libmesh_assert_equal_to (n_global % blocksize, 0);
ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
LIBMESH_CHKERR(ierr);
// transform the per-entry n_nz and n_oz arrays into their block counterparts.
std::vector<numeric_index_type> b_n_nz, b_n_oz;
transform_preallocation_arrays (blocksize,
n_nz, n_oz,
b_n_nz, b_n_oz);
ierr = MatSeqBAIJSetPreallocation (_mat,
blocksize,
0,
numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()));
LIBMESH_CHKERR(ierr);
ierr = MatMPIBAIJSetPreallocation (_mat,
blocksize,
0,
numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()),
0,
numeric_petsc_cast(b_n_oz.empty() ? nullptr : b_n_oz.data()));
LIBMESH_CHKERR(ierr);
}
else
#endif
{
switch (_mat_type) {
case AIJ:
ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
LIBMESH_CHKERR(ierr);
ierr = MatSeqAIJSetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()));
LIBMESH_CHKERR(ierr);
ierr = MatMPIAIJSetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
0,
numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
break;
case HYPRE:
#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
ierr = MatSetType(_mat, MATHYPRE);
LIBMESH_CHKERR(ierr);
ierr = MatHYPRESetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
0,
numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
LIBMESH_CHKERR(ierr);
#else
libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
#endif
break;
default: libmesh_error_msg("Unsupported petsc matrix type");
}
}
// Is prefix information available somewhere? Perhaps pass in the system name?
ierr = MatSetOptionsPrefix(_mat, "");
LIBMESH_CHKERR(ierr);
ierr = MatSetFromOptions(_mat);
LIBMESH_CHKERR(ierr);
this->zero();
}
template <typename T>
void PetscMatrix<T>::init ()
{
libmesh_assert(this->_dof_map);
// Clear initialized matrices
if (this->initialized())
this->clear();
this->_is_initialized = true;
const numeric_index_type my_m = this->_dof_map->n_dofs();
const numeric_index_type my_n = my_m;
const numeric_index_type n_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
const numeric_index_type m_l = n_l;
const std::vector<numeric_index_type> & n_nz = this->_dof_map->get_n_nz();
const std::vector<numeric_index_type> & n_oz = this->_dof_map->get_n_oz();
// Make sure the sparsity pattern isn't empty unless the matrix is 0x0
libmesh_assert_equal_to (n_nz.size(), m_l);
libmesh_assert_equal_to (n_oz.size(), m_l);
PetscErrorCode ierr = 0;
PetscInt m_global = static_cast<PetscInt>(my_m);
PetscInt n_global = static_cast<PetscInt>(my_n);
PetscInt m_local = static_cast<PetscInt>(m_l);
PetscInt n_local = static_cast<PetscInt>(n_l);
ierr = MatCreate(this->comm().get(), &_mat);
LIBMESH_CHKERR(ierr);
ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
LIBMESH_CHKERR(ierr);
PetscInt blocksize = static_cast<PetscInt>(this->_dof_map->block_size());
ierr = MatSetBlockSize(_mat,blocksize);
LIBMESH_CHKERR(ierr);
#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
if (blocksize > 1)
{
// specified blocksize, bs>1.
// double check sizes.
libmesh_assert_equal_to (m_local % blocksize, 0);
libmesh_assert_equal_to (n_local % blocksize, 0);
libmesh_assert_equal_to (m_global % blocksize, 0);
libmesh_assert_equal_to (n_global % blocksize, 0);
ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
LIBMESH_CHKERR(ierr);
// transform the per-entry n_nz and n_oz arrays into their block counterparts.
std::vector<numeric_index_type> b_n_nz, b_n_oz;
transform_preallocation_arrays (blocksize,
n_nz, n_oz,
b_n_nz, b_n_oz);
ierr = MatSeqBAIJSetPreallocation (_mat,
blocksize,
0,
numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()));
LIBMESH_CHKERR(ierr);
ierr = MatMPIBAIJSetPreallocation (_mat,
blocksize,
0,
numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()),
0,
numeric_petsc_cast(b_n_oz.empty() ? nullptr : b_n_oz.data()));
LIBMESH_CHKERR(ierr);
}
else
#endif
{
switch (_mat_type) {
case AIJ:
ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
LIBMESH_CHKERR(ierr);
ierr = MatSeqAIJSetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()));
LIBMESH_CHKERR(ierr);
ierr = MatMPIAIJSetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
0,
numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
break;
case HYPRE:
#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
ierr = MatSetType(_mat, MATHYPRE);
LIBMESH_CHKERR(ierr);
ierr = MatHYPRESetPreallocation (_mat,
0,
numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
0,
numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
LIBMESH_CHKERR(ierr);
#else
libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
#endif
break;
default: libmesh_error_msg("Unsupported petsc matrix type");
}
}
// Is prefix information available somewhere? Perhaps pass in the system name?
ierr = MatSetOptionsPrefix(_mat, "");
LIBMESH_CHKERR(ierr);
ierr = MatSetFromOptions(_mat);
LIBMESH_CHKERR(ierr);
this->zero();
}
template <typename T>
void PetscMatrix<T>::update_preallocation_and_zero ()
{
libmesh_not_implemented();
}
template <typename T>
void PetscMatrix<T>::reset_preallocation()
{
#if !PETSC_VERSION_LESS_THAN(3,8,0)
libmesh_assert (this->initialized());
auto ierr = MatResetPreallocation(_mat);
LIBMESH_CHKERR(ierr);
#else
libmesh_warning("Your version of PETSc doesn't support resetting of "
"preallocation, so we will use your most recent sparsity "
"pattern. This may result in a degradation of performance\n");
#endif
}
template <typename T>
void PetscMatrix<T>::zero ()
{
libmesh_assert (this->initialized());
semiparallel_only();
PetscErrorCode ierr=0;
PetscInt m_l, n_l;
ierr = MatGetLocalSize(_mat,&m_l,&n_l);
LIBMESH_CHKERR(ierr);
if (n_l)
{
ierr = MatZeroEntries(_mat);
LIBMESH_CHKERR(ierr);
}
}
template <typename T>
void PetscMatrix<T>::zero_rows (std::vector<numeric_index_type> & rows, T diag_value)
{
libmesh_assert (this->initialized());
semiparallel_only();
PetscErrorCode ierr=0;
#if PETSC_RELEASE_LESS_THAN(3,1,1)
if (!rows.empty())
ierr = MatZeroRows(_mat, rows.size(),
numeric_petsc_cast(rows.data()), diag_value);
else
ierr = MatZeroRows(_mat, 0, NULL, diag_value);
#else
// As of petsc-dev at the time of 3.1.0, MatZeroRows now takes two additional
// optional arguments. The optional arguments (x,b) can be used to specify the
// solutions for the zeroed rows (x) and right hand side (b) to update.
// Could be useful for setting boundary conditions...
if (!rows.empty())
ierr = MatZeroRows(_mat, cast_int<PetscInt>(rows.size()),
numeric_petsc_cast(rows.data()), diag_value,
NULL, NULL);
else
ierr = MatZeroRows(_mat, 0, NULL, diag_value, NULL,
NULL);
#endif
LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::clear ()
{
PetscErrorCode ierr=0;
if ((this->initialized()) && (this->_destroy_mat_on_exit))
{
semiparallel_only();
ierr = LibMeshMatDestroy (&_mat);
LIBMESH_CHKERR(ierr);
this->_is_initialized = false;
}
}
template <typename T>
Real PetscMatrix<T>::l1_norm () const
{
libmesh_assert (this->initialized());
semiparallel_only();
PetscErrorCode ierr=0;
PetscReal petsc_value;
Real value;
libmesh_assert (this->closed());
ierr = MatNorm(_mat, NORM_1, &petsc_value);
LIBMESH_CHKERR(ierr);
value = static_cast<Real>(petsc_value);
return value;
}
template <typename T>
Real PetscMatrix<T>::linfty_norm () const
{
libmesh_assert (this->initialized());
semiparallel_only();
PetscErrorCode ierr=0;
PetscReal petsc_value;
Real value;
libmesh_assert (this->closed());
ierr = MatNorm(_mat, NORM_INFINITY, &petsc_value);
LIBMESH_CHKERR(ierr);
value = static_cast<Real>(petsc_value);
return value;
}
template <typename T>
void PetscMatrix<T>::print_matlab (const std::string & name) const
{
libmesh_assert (this->initialized());
semiparallel_only();
if (!this->closed())
{
libmesh_deprecated();
libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_matlab().\n"
"Please update your code, as this warning will become an error in a future release.");
const_cast<PetscMatrix<T> *>(this)->close();
}
PetscErrorCode ierr=0;
PetscViewer petsc_viewer;
ierr = PetscViewerCreate (this->comm().get(),
&petsc_viewer);
LIBMESH_CHKERR(ierr);
/**
* Create an ASCII file containing the matrix
* if a filename was provided.
*/
if (name != "")
{
ierr = PetscViewerASCIIOpen( this->comm().get(),
name.c_str(),
&petsc_viewer);
LIBMESH_CHKERR(ierr);
#if PETSC_VERSION_LESS_THAN(3,7,0)
ierr = PetscViewerSetFormat (petsc_viewer,
PETSC_VIEWER_ASCII_MATLAB);
#else
ierr = PetscViewerPushFormat (petsc_viewer,
PETSC_VIEWER_ASCII_MATLAB);
#endif
LIBMESH_CHKERR(ierr);
ierr = MatView (_mat, petsc_viewer);
LIBMESH_CHKERR(ierr);
}
/**
* Otherwise the matrix will be dumped to the screen.
*/
else
{
#if PETSC_VERSION_LESS_THAN(3,7,0)
ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
PETSC_VIEWER_ASCII_MATLAB);
#else
ierr = PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
PETSC_VIEWER_ASCII_MATLAB);
#endif
LIBMESH_CHKERR(ierr);
ierr = MatView (_mat, PETSC_VIEWER_STDOUT_WORLD);
LIBMESH_CHKERR(ierr);
}
/**
* Destroy the viewer.
*/
ierr = LibMeshPetscViewerDestroy (&petsc_viewer);
LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::print_personal(std::ostream & os) const
{
libmesh_assert (this->initialized());
// Routine must be called in parallel on parallel matrices
// and serial on serial matrices.
semiparallel_only();
// #ifndef NDEBUG
// if (os != std::cout)
// libMesh::err << "Warning! PETSc can only print to std::cout!" << std::endl;
// #endif
// Matrix must be in an assembled state to be printed
if (!this->closed())
{
libmesh_deprecated();
libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_personal().\n"
"Please update your code, as this warning will become an error in a future release.");
const_cast<PetscMatrix<T> *>(this)->close();
}
PetscErrorCode ierr=0;
// Print to screen if ostream is stdout
if (os.rdbuf() == std::cout.rdbuf())
{
ierr = MatView(_mat, PETSC_VIEWER_STDOUT_SELF);
LIBMESH_CHKERR(ierr);
}
// Otherwise, print to the requested file, in a roundabout way...
else
{
// We will create a temporary filename, and file, for PETSc to
// write to.
std::string temp_filename;
{
// Template for temporary filename
char c[] = "temp_petsc_matrix.XXXXXX";
// Generate temporary, unique filename only on processor 0. We will
// use this filename for PetscViewerASCIIOpen, before copying it into
// the user's stream
if (this->processor_id() == 0)
{
int fd = mkstemp(c);
// Check to see that mkstemp did not fail.
if (fd == -1)
libmesh_error_msg("mkstemp failed in PetscMatrix::print_personal()");
// mkstemp returns a file descriptor for an open file,
// so let's close it before we hand it to PETSc!
::close (fd);
}
// Store temporary filename as string, makes it easier to broadcast
temp_filename = c;
}
// Now broadcast the filename from processor 0 to all processors.
this->comm().broadcast(temp_filename);
// PetscViewer object for passing to MatView
PetscViewer petsc_viewer;
// This PETSc function only takes a string and handles the opening/closing
// of the file internally. Since print_personal() takes a reference to
// an ostream, we have to do an extra step... print_personal() should probably
// have a version that takes a string to get rid of this problem.
ierr = PetscViewerASCIIOpen( this->comm().get(),
temp_filename.c_str(),
&petsc_viewer);
LIBMESH_CHKERR(ierr);
// Probably don't need to set the format if it's default...
// ierr = PetscViewerSetFormat (petsc_viewer,
// PETSC_VIEWER_DEFAULT);
// LIBMESH_CHKERR(ierr);
// Finally print the matrix using the viewer
ierr = MatView (_mat, petsc_viewer);
LIBMESH_CHKERR(ierr);
if (this->processor_id() == 0)
{
// Now the inefficient bit: open temp_filename as an ostream and copy the contents
// into the user's desired ostream. We can't just do a direct file copy, we don't even have the filename!
std::ifstream input_stream(temp_filename.c_str());
os << input_stream.rdbuf(); // The "most elegant" way to copy one stream into another.
// os.close(); // close not defined in ostream
// Now remove the temporary file
input_stream.close();
std::remove(temp_filename.c_str());
}
}
}
template <typename T>
void PetscMatrix<T>::add_matrix(const DenseMatrix<T> & dm,
const std::vector<numeric_index_type> & rows,
const std::vector<numeric_index_type> & cols)
{
libmesh_assert (this->initialized());
const numeric_index_type n_rows = dm.m();
const numeric_index_type n_cols = dm.n();
libmesh_assert_equal_to (rows.size(), n_rows);
libmesh_assert_equal_to (cols.size(), n_cols);
PetscErrorCode ierr=0;
ierr = MatSetValues(_mat,
n_rows, numeric_petsc_cast(rows.data()),
n_cols, numeric_petsc_cast(cols.data()),
const_cast<PetscScalar *>(dm.get_values().data()),
ADD_VALUES);
LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::add_block_matrix(const DenseMatrix<T> & dm,
const std::vector<numeric_index_type> & brows,
const std::vector<numeric_index_type> & bcols)
{
libmesh_assert (this->initialized());
const numeric_index_type n_brows =
cast_int<numeric_index_type>(brows.size());
const numeric_index_type n_bcols =
cast_int<numeric_index_type>(bcols.size());
PetscErrorCode ierr=0;
#ifndef NDEBUG
const numeric_index_type n_rows =
cast_int<numeric_index_type>(dm.m());
const numeric_index_type n_cols =
cast_int<numeric_index_type>(dm.n());
const numeric_index_type blocksize = n_rows / n_brows;
libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
libmesh_assert_equal_to (blocksize*n_brows, n_rows);
libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
PetscInt petsc_blocksize;
ierr = MatGetBlockSize(_mat, &petsc_blocksize);
LIBMESH_CHKERR(ierr);
libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
#endif
// These casts are required for PETSc <= 2.1.5
ierr = MatSetValuesBlocked(_mat,
n_brows, numeric_petsc_cast(brows.data()),
n_bcols, numeric_petsc_cast(bcols.data()),
const_cast<PetscScalar *>(dm.get_values().data()),
ADD_VALUES);
LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::_get_submatrix(SparseMatrix<T> & submatrix,
const std::vector<numeric_index_type> & rows,
const std::vector<numeric_index_type> & cols,
const bool reuse_submatrix) const
{
if (!this->closed())
{
libmesh_deprecated();
libmesh_warning("The matrix must be assembled before calling PetscMatrix::create_submatrix().\n"
"Please update your code, as this warning will become an error in a future release.");
const_cast<PetscMatrix<T> *>(this)->close();
}
// Make sure the SparseMatrix passed in is really a PetscMatrix
PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
// If we're not reusing submatrix and submatrix is already initialized
// then we need to clear it, otherwise we get a memory leak.
if (!reuse_submatrix && submatrix.initialized())
submatrix.clear();
// Construct row and column index sets.
PetscErrorCode ierr=0;
IS isrow, iscol;
ierr = ISCreateLibMesh(this->comm().get(),
cast_int<PetscInt>(rows.size()),
numeric_petsc_cast(rows.data()),
PETSC_USE_POINTER,
&isrow); LIBMESH_CHKERR(ierr);
ierr = ISCreateLibMesh(this->comm().get(),
cast_int<PetscInt>(cols.size()),
numeric_petsc_cast(cols.data()),
PETSC_USE_POINTER,
&iscol); LIBMESH_CHKERR(ierr);
// Extract submatrix
ierr = LibMeshCreateSubMatrix(_mat,
isrow,
iscol,
#if PETSC_RELEASE_LESS_THAN(3,0,1)
PETSC_DECIDE,
#endif
(reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
&(petsc_submatrix->_mat)); LIBMESH_CHKERR(ierr);
// Specify that the new submatrix is initialized and close it.
petsc_submatrix->_is_initialized = true;
petsc_submatrix->close();
// Clean up PETSc data structures
ierr = LibMeshISDestroy(&isrow); LIBMESH_CHKERR(ierr);
ierr = LibMeshISDestroy(&iscol); LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::get_diagonal (NumericVector<T> & dest) const
{
// Make sure the NumericVector passed in is really a PetscVector
PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
// Needs a const_cast since PETSc does not work with const.
PetscErrorCode ierr =
MatGetDiagonal(const_cast<PetscMatrix<T> *>(this)->mat(),petsc_dest.vec()); LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::get_transpose (SparseMatrix<T> & dest) const
{
// Make sure the SparseMatrix passed in is really a PetscMatrix
PetscMatrix<T> & petsc_dest = cast_ref<PetscMatrix<T> &>(dest);
// If we aren't reusing the matrix then need to clear dest,
// otherwise we get a memory leak
if (&petsc_dest != this)
dest.clear();
PetscErrorCode ierr;
#if PETSC_VERSION_LESS_THAN(3,0,0)
if (&petsc_dest == this)
ierr = MatTranspose(_mat,NULL);
else
ierr = MatTranspose(_mat,&petsc_dest._mat);
LIBMESH_CHKERR(ierr);
#else
// FIXME - we can probably use MAT_REUSE_MATRIX in more situations
if (&petsc_dest == this)
ierr = MatTranspose(_mat,MAT_REUSE_MATRIX,&petsc_dest._mat);
else
ierr = MatTranspose(_mat,MAT_INITIAL_MATRIX,&petsc_dest._mat);
LIBMESH_CHKERR(ierr);
#endif
// Specify that the transposed matrix is initialized and close it.
petsc_dest._is_initialized = true;
petsc_dest.close();
}
template <typename T>
void PetscMatrix<T>::close ()
{
semiparallel_only();
// BSK - 1/19/2004
// strictly this check should be OK, but it seems to
// fail on matrix-free matrices. Do they falsely
// state they are assembled? Check with the developers...
// if (this->closed())
// return;
PetscErrorCode ierr=0;
ierr = MatAssemblyBegin (_mat, MAT_FINAL_ASSEMBLY);
LIBMESH_CHKERR(ierr);
ierr = MatAssemblyEnd (_mat, MAT_FINAL_ASSEMBLY);
LIBMESH_CHKERR(ierr);
}
template <typename T>
void PetscMatrix<T>::flush ()
{
semiparallel_only();
PetscErrorCode ierr=0;
ierr = MatAssemblyBegin (_mat, MAT_FLUSH_ASSEMBLY);
LIBMESH_CHKERR(ierr);
ierr = MatAssemblyEnd (_mat, MAT_FLUSH_ASSEMBLY);
LIBMESH_CHKERR(ierr);