version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
scalapack.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2017 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
15#include <deal.II/base/mpi.h>
16#include <deal.II/base/mpi.templates.h>
17
19#include <deal.II/lac/scalapack.templates.h>
20
21#ifdef DEAL_II_WITH_HDF5
22# include <hdf5.h>
23#endif
24
25#include <limits>
26#include <memory>
27
29
30#ifdef DEAL_II_WITH_HDF5
31
32namespace
33{
34 template <typename number>
35 hid_t
36 hdf5_type_id(const number *)
37 {
39 // don't know what to put here; it does not matter
40 return -1;
41 }
42
43 hid_t
44 hdf5_type_id(const double *)
45 {
46 return H5T_NATIVE_DOUBLE;
47 }
48
49 hid_t
50 hdf5_type_id(const float *)
51 {
52 return H5T_NATIVE_FLOAT;
53 }
54} // namespace
55#endif // DEAL_II_WITH_HDF5
56
57
58
59template <typename NumberType>
61 const size_type n_rows_,
62 const size_type n_columns_,
63 const std::shared_ptr<const Utilities::MPI::ProcessGrid> &process_grid,
64 const size_type row_block_size_,
65 const size_type column_block_size_,
66 const LAPACKSupport::Property property_)
67 : uplo('L')
68 , // for non-symmetric matrices this is not needed
69 first_process_row(0)
70 , first_process_column(0)
71 , submatrix_row(1)
72 , submatrix_column(1)
73{
74 reinit(n_rows_,
75 n_columns_,
76 process_grid,
77 row_block_size_,
78 column_block_size_,
79 property_);
80}
81
82
83
84template <typename NumberType>
86 const size_type size,
87 const std::shared_ptr<const Utilities::MPI::ProcessGrid> &process_grid,
88 const size_type block_size,
89 const LAPACKSupport::Property property)
90 : ScaLAPACKMatrix<NumberType>(size,
91 size,
92 process_grid,
93 block_size,
94 block_size,
95 property)
96{}
97
98
99
100template <typename NumberType>
102 const std::string &filename,
103 const std::shared_ptr<const Utilities::MPI::ProcessGrid> &process_grid,
104 const size_type row_block_size,
105 const size_type column_block_size)
106 : uplo('L')
107 , // for non-symmetric matrices this is not needed
108 first_process_row(0)
109 , first_process_column(0)
110 , submatrix_row(1)
111 , submatrix_column(1)
112{
113#ifndef DEAL_II_WITH_HDF5
114 (void)filename;
115 (void)process_grid;
116 (void)row_block_size;
117 (void)column_block_size;
118 Assert(
119 false,
121 "This function is only available when deal.II is configured with HDF5"));
122#else
123
124 const unsigned int this_mpi_process(
125 Utilities::MPI::this_mpi_process(process_grid->mpi_communicator));
126
127 // Before reading the content from disk the root process determines the
128 // dimensions of the matrix. Subsequently, memory is allocated by a call to
129 // reinit() and the matrix is loaded by a call to load().
130 if (this_mpi_process == 0)
131 {
132 herr_t status = 0;
133
134 // open file in read-only mode
135 hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
136 AssertThrow(file >= 0, ExcIO());
137
138 // get data set in file
139 hid_t dataset = H5Dopen2(file, "/matrix", H5P_DEFAULT);
140 AssertThrow(dataset >= 0, ExcIO());
141
142 // determine file space
143 hid_t filespace = H5Dget_space(dataset);
144
145 // get number of dimensions in data set
146 int rank = H5Sget_simple_extent_ndims(filespace);
147 AssertThrow(rank == 2, ExcIO());
148 hsize_t dims[2];
149 status = H5Sget_simple_extent_dims(filespace, dims, nullptr);
150 AssertThrow(status >= 0, ExcIO());
151
152 // due to ScaLAPACK's column-major memory layout the dimensions are
153 // swapped
154 n_rows = dims[1];
155 n_columns = dims[0];
156
157 // close/release resources
158 status = H5Sclose(filespace);
159 AssertThrow(status >= 0, ExcIO());
160 status = H5Dclose(dataset);
161 AssertThrow(status >= 0, ExcIO());
162 status = H5Fclose(file);
163 AssertThrow(status >= 0, ExcIO());
164 }
165 n_rows = Utilities::MPI::broadcast(process_grid->mpi_communicator,
166 n_rows,
167 0 /*from root*/);
168 n_columns = Utilities::MPI::broadcast(process_grid->mpi_communicator,
169 n_columns,
170 0 /*from root*/);
171
172 // the property will be overwritten by the subsequent call to load()
174 n_columns,
175 process_grid,
179
180 load(filename.c_str());
181
182#endif // DEAL_II_WITH_HDF5
183}
184
185
186
187template <typename NumberType>
188void
190 const size_type n_rows_,
191 const size_type n_columns_,
192 const std::shared_ptr<const Utilities::MPI::ProcessGrid> &process_grid,
193 const size_type row_block_size_,
194 const size_type column_block_size_,
195 const LAPACKSupport::Property property_)
196{
197 Assert(row_block_size_ > 0, ExcMessage("Row block size has to be positive."));
198 Assert(column_block_size_ > 0,
199 ExcMessage("Column block size has to be positive."));
200 Assert(
201 row_block_size_ <= n_rows_,
203 "Row block size can not be greater than the number of rows of the matrix"));
204 Assert(
205 column_block_size_ <= n_columns_,
207 "Column block size can not be greater than the number of columns of the matrix"));
208
210 property = property_;
211 grid = process_grid;
212 n_rows = n_rows_;
213 n_columns = n_columns_;
214 row_block_size = row_block_size_;
215 column_block_size = column_block_size_;
216
217 if (grid->mpi_process_is_active)
218 {
219 // Get local sizes:
220 n_local_rows = numroc_(&n_rows,
221 &row_block_size,
222 &(grid->this_process_row),
223 &first_process_row,
224 &(grid->n_process_rows));
225 n_local_columns = numroc_(&n_columns,
226 &column_block_size,
227 &(grid->this_process_column),
228 &first_process_column,
229 &(grid->n_process_columns));
230
231 // LLD_A = MAX(1,NUMROC(M_A, MB_A, MYROW, RSRC_A, NPROW)), different
232 // between processes
233 int lda = std::max(1, n_local_rows);
234
235 int info = 0;
236 descinit_(descriptor,
237 &n_rows,
238 &n_columns,
239 &row_block_size,
240 &column_block_size,
241 &first_process_row,
242 &first_process_column,
243 &(grid->blacs_context),
244 &lda,
245 &info);
246 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("descinit_", info));
247
248 this->TransposeTable<NumberType>::reinit(n_local_rows, n_local_columns);
249 }
250 else
251 {
252 // set process-local variables to something telling:
253 n_local_rows = -1;
254 n_local_columns = -1;
255 std::fill(std::begin(descriptor), std::end(descriptor), -1);
256 }
257}
258
259
260
261template <typename NumberType>
262void
264 const size_type size,
265 const std::shared_ptr<const Utilities::MPI::ProcessGrid> &process_grid,
266 const size_type block_size,
267 const LAPACKSupport::Property property)
268{
269 reinit(size, size, process_grid, block_size, block_size, property);
270}
271
272
273
274template <typename NumberType>
275void
277 const LAPACKSupport::Property property_)
278{
279 property = property_;
280}
281
282
283
284template <typename NumberType>
287{
288 return property;
289}
290
291
292
293template <typename NumberType>
296{
297 return state;
298}
299
300
301
302template <typename NumberType>
305{
306 // FIXME: another way to copy is to use pdgeadd_ PBLAS routine.
307 // This routine computes the sum of two matrices B := a*A + b*B.
308 // Matrices can have different distribution,in particular matrix A can
309 // be owned by only one process, so we can set a=1 and b=0 to copy
310 // non-distributed matrix A into distributed matrix B.
311 Assert(n_rows == int(matrix.m()), ExcDimensionMismatch(n_rows, matrix.m()));
312 Assert(n_columns == int(matrix.n()),
313 ExcDimensionMismatch(n_columns, matrix.n()));
314
315 if (grid->mpi_process_is_active)
316 {
317 for (int i = 0; i < n_local_rows; ++i)
318 {
319 const int glob_i = global_row(i);
320 for (int j = 0; j < n_local_columns; ++j)
321 {
322 const int glob_j = global_column(j);
323 local_el(i, j) = matrix(glob_i, glob_j);
324 }
325 }
326 }
327 state = LAPACKSupport::matrix;
328 return *this;
329}
330
331
332
333template <typename NumberType>
334void
336 const unsigned int rank)
337{
338 if (n_rows * n_columns == 0)
339 return;
340
341 const unsigned int this_mpi_process(
342 Utilities::MPI::this_mpi_process(this->grid->mpi_communicator));
343
344 if constexpr (running_in_debug_mode())
345 {
346 Assert(Utilities::MPI::max(rank, this->grid->mpi_communicator) == rank,
348 "All processes have to call routine with identical rank"));
349 Assert(Utilities::MPI::min(rank, this->grid->mpi_communicator) == rank,
351 "All processes have to call routine with identical rank"));
352 }
353
354 // root process has to be active in the grid of A
355 if (this_mpi_process == rank)
356 {
357 Assert(grid->mpi_process_is_active, ExcInternalError());
358 Assert(n_rows == int(B.m()), ExcDimensionMismatch(n_rows, B.m()));
359 Assert(n_columns == int(B.n()), ExcDimensionMismatch(n_columns, B.n()));
360 }
361 // Create 1x1 grid for matrix B.
362 // The underlying grid for matrix B only contains the process #rank.
363 // This grid will be used to copy the serial matrix B to the distributed
364 // matrix using the ScaLAPACK routine pgemr2d.
365 MPI_Group group_A;
366 MPI_Comm_group(this->grid->mpi_communicator, &group_A);
367 const int n = 1;
368 const std::vector<int> ranks(n, rank);
369 MPI_Group group_B;
370 MPI_Group_incl(group_A, n, ranks.data(), &group_B);
371 MPI_Comm communicator_B;
372
374 const int ierr = MPI_Comm_create_group(this->grid->mpi_communicator,
375 group_B,
376 mpi_tag,
377 &communicator_B);
378 AssertThrowMPI(ierr);
379 int n_proc_rows_B = 1, n_proc_cols_B = 1;
380 int this_process_row_B = -1, this_process_column_B = -1;
381 int blacs_context_B = -1;
382 if (MPI_COMM_NULL != communicator_B)
383 {
384 // Initialize Cblas context from the provided communicator
385 blacs_context_B = Csys2blacs_handle(communicator_B);
386 const char *order = "Col";
387 Cblacs_gridinit(&blacs_context_B, order, n_proc_rows_B, n_proc_cols_B);
388 Cblacs_gridinfo(blacs_context_B,
389 &n_proc_rows_B,
390 &n_proc_cols_B,
391 &this_process_row_B,
392 &this_process_column_B);
393 Assert(n_proc_rows_B * n_proc_cols_B == 1, ExcInternalError());
394 // the active process of grid B has to be process #rank of the
395 // communicator attached to A
396 Assert(this_mpi_process == rank, ExcInternalError());
397 }
398 const bool mpi_process_is_active_B =
399 (this_process_row_B >= 0 && this_process_column_B >= 0);
400
401 // create descriptor for matrix B
402 std::vector<int> descriptor_B(9, -1);
403 const int first_process_row_B = 0, first_process_col_B = 0;
404
405 if (mpi_process_is_active_B)
406 {
407 // Get local sizes:
408 int n_local_rows_B = numroc_(&n_rows,
409 &n_rows,
410 &this_process_row_B,
411 &first_process_row_B,
412 &n_proc_rows_B);
413 int n_local_cols_B = numroc_(&n_columns,
414 &n_columns,
415 &this_process_column_B,
416 &first_process_col_B,
417 &n_proc_cols_B);
418 Assert(n_local_rows_B == n_rows, ExcInternalError());
419 Assert(n_local_cols_B == n_columns, ExcInternalError());
420
421 int lda = std::max(1, n_local_rows_B);
422 int info = 0;
423 descinit_(descriptor_B.data(),
424 &n_rows,
425 &n_columns,
426 &n_rows,
427 &n_columns,
428 &first_process_row_B,
429 &first_process_col_B,
430 &blacs_context_B,
431 &lda,
432 &info);
433 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("descinit_", info));
434 }
435 if (this->grid->mpi_process_is_active)
436 {
437 const int ii = 1;
438 NumberType *loc_vals_A =
439 this->values.size() > 0 ? this->values.data() : nullptr;
440 const NumberType *loc_vals_B =
441 mpi_process_is_active_B ? &(B(0, 0)) : nullptr;
442
443 // pgemr2d has to be called only for processes active on grid attached to
444 // matrix A
445 pgemr2d(&n_rows,
446 &n_columns,
447 loc_vals_B,
448 &ii,
449 &ii,
450 descriptor_B.data(),
451 loc_vals_A,
452 &ii,
453 &ii,
454 this->descriptor,
455 &(this->grid->blacs_context));
456 }
457 if (mpi_process_is_active_B)
458 Cblacs_gridexit(blacs_context_B);
459
460 MPI_Group_free(&group_A);
461 MPI_Group_free(&group_B);
462 if (MPI_COMM_NULL != communicator_B)
463 Utilities::MPI::free_communicator(communicator_B);
464
465 state = LAPACKSupport::matrix;
466}
467
468
469
470template <typename NumberType>
471unsigned int
472ScaLAPACKMatrix<NumberType>::global_row(const unsigned int loc_row) const
473{
474 Assert(n_local_rows >= 0 && loc_row < static_cast<unsigned int>(n_local_rows),
475 ExcIndexRange(loc_row, 0, n_local_rows));
476 const int i = loc_row + 1;
477 return indxl2g_(&i,
478 &row_block_size,
479 &(grid->this_process_row),
480 &first_process_row,
481 &(grid->n_process_rows)) -
482 1;
483}
484
485
486
487template <typename NumberType>
488unsigned int
489ScaLAPACKMatrix<NumberType>::global_column(const unsigned int loc_column) const
490{
491 Assert(n_local_columns >= 0 &&
492 loc_column < static_cast<unsigned int>(n_local_columns),
493 ExcIndexRange(loc_column, 0, n_local_columns));
494 const int j = loc_column + 1;
495 return indxl2g_(&j,
496 &column_block_size,
497 &(grid->this_process_column),
498 &first_process_column,
499 &(grid->n_process_columns)) -
500 1;
501}
502
503
504
505template <typename NumberType>
506void
508 const unsigned int rank) const
509{
510 if (n_rows * n_columns == 0)
511 return;
512
513 const unsigned int this_mpi_process(
514 Utilities::MPI::this_mpi_process(this->grid->mpi_communicator));
515
516 if constexpr (running_in_debug_mode())
517 {
518 Assert(Utilities::MPI::max(rank, this->grid->mpi_communicator) == rank,
520 "All processes have to call routine with identical rank"));
521 Assert(Utilities::MPI::min(rank, this->grid->mpi_communicator) == rank,
523 "All processes have to call routine with identical rank"));
524 }
525
526 if (this_mpi_process == rank)
527 {
528 // the process which gets the serial copy has to be in the process grid
529 Assert(this->grid->is_process_active(), ExcInternalError());
530 Assert(n_rows == int(B.m()), ExcDimensionMismatch(n_rows, B.m()));
531 Assert(n_columns == int(B.n()), ExcDimensionMismatch(n_columns, B.n()));
532 }
533
534 // Create 1x1 grid for matrix B.
535 // The underlying grid for matrix B only contains the process #rank.
536 // This grid will be used to copy to the distributed matrix to the serial
537 // matrix B using the ScaLAPACK routine pgemr2d.
538 MPI_Group group_A;
539 MPI_Comm_group(this->grid->mpi_communicator, &group_A);
540 const int n = 1;
541 const std::vector<int> ranks(n, rank);
542 MPI_Group group_B;
543 MPI_Group_incl(group_A, n, ranks.data(), &group_B);
544 MPI_Comm communicator_B;
545
547 const int ierr = MPI_Comm_create_group(this->grid->mpi_communicator,
548 group_B,
549 mpi_tag,
550 &communicator_B);
551 AssertThrowMPI(ierr);
552 int n_proc_rows_B = 1, n_proc_cols_B = 1;
553 int this_process_row_B = -1, this_process_column_B = -1;
554 int blacs_context_B = -1;
555 if (MPI_COMM_NULL != communicator_B)
556 {
557 // Initialize Cblas context from the provided communicator
558 blacs_context_B = Csys2blacs_handle(communicator_B);
559 const char *order = "Col";
560 Cblacs_gridinit(&blacs_context_B, order, n_proc_rows_B, n_proc_cols_B);
561 Cblacs_gridinfo(blacs_context_B,
562 &n_proc_rows_B,
563 &n_proc_cols_B,
564 &this_process_row_B,
565 &this_process_column_B);
566 Assert(n_proc_rows_B * n_proc_cols_B == 1, ExcInternalError());
567 // the active process of grid B has to be process #rank of the
568 // communicator attached to A
569 Assert(this_mpi_process == rank, ExcInternalError());
570 }
571 const bool mpi_process_is_active_B =
572 (this_process_row_B >= 0 && this_process_column_B >= 0);
573
574 // create descriptor for matrix B
575 std::vector<int> descriptor_B(9, -1);
576 const int first_process_row_B = 0, first_process_col_B = 0;
577
578 if (mpi_process_is_active_B)
579 {
580 // Get local sizes:
581 int n_local_rows_B = numroc_(&n_rows,
582 &n_rows,
583 &this_process_row_B,
584 &first_process_row_B,
585 &n_proc_rows_B);
586 int n_local_cols_B = numroc_(&n_columns,
587 &n_columns,
588 &this_process_column_B,
589 &first_process_col_B,
590 &n_proc_cols_B);
591 Assert(n_local_rows_B == n_rows, ExcInternalError());
592 Assert(n_local_cols_B == n_columns, ExcInternalError());
593
594 int lda = std::max(1, n_local_rows_B);
595 int info = 0;
596 // fill descriptor for matrix B
597 descinit_(descriptor_B.data(),
598 &n_rows,
599 &n_columns,
600 &n_rows,
601 &n_columns,
602 &first_process_row_B,
603 &first_process_col_B,
604 &blacs_context_B,
605 &lda,
606 &info);
607 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("descinit_", info));
608 }
609 // pgemr2d has to be called only for processes active on grid attached to
610 // matrix A
611 if (this->grid->mpi_process_is_active)
612 {
613 const int ii = 1;
614 const NumberType *loc_vals_A =
615 this->values.size() > 0 ? this->values.data() : nullptr;
616 NumberType *loc_vals_B = mpi_process_is_active_B ? &(B(0, 0)) : nullptr;
617
618 pgemr2d(&n_rows,
619 &n_columns,
620 loc_vals_A,
621 &ii,
622 &ii,
623 this->descriptor,
624 loc_vals_B,
625 &ii,
626 &ii,
627 descriptor_B.data(),
628 &(this->grid->blacs_context));
629 }
630 if (mpi_process_is_active_B)
631 Cblacs_gridexit(blacs_context_B);
632
633 MPI_Group_free(&group_A);
634 MPI_Group_free(&group_B);
635 if (MPI_COMM_NULL != communicator_B)
636 Utilities::MPI::free_communicator(communicator_B);
637}
638
639
640
641template <typename NumberType>
642void
644{
645 // FIXME: use PDGEMR2d for copying?
646 // PDGEMR2d copies a submatrix of A on a submatrix of B.
647 // A and B can have different distributions
648 // see http://icl.cs.utk.edu/lapack-forum/viewtopic.php?t=50
649 Assert(n_rows == int(matrix.m()), ExcDimensionMismatch(n_rows, matrix.m()));
650 Assert(n_columns == int(matrix.n()),
651 ExcDimensionMismatch(n_columns, matrix.n()));
652
653 matrix = 0.;
654 if (grid->mpi_process_is_active)
655 {
656 for (int i = 0; i < n_local_rows; ++i)
657 {
658 const int glob_i = global_row(i);
659 for (int j = 0; j < n_local_columns; ++j)
660 {
661 const int glob_j = global_column(j);
662 matrix(glob_i, glob_j) = local_el(i, j);
663 }
664 }
665 }
666 Utilities::MPI::sum(matrix, grid->mpi_communicator, matrix);
667
668 // we could move the following lines under the main loop above,
669 // but they would be dependent on glob_i and glob_j, which
670 // won't make it much prettier
671 if (state == LAPACKSupport::cholesky)
672 {
673 if (property == LAPACKSupport::lower_triangular)
674 for (unsigned int i = 0; i < matrix.n(); ++i)
675 for (unsigned int j = i + 1; j < matrix.m(); ++j)
676 matrix(i, j) = 0.;
677 else if (property == LAPACKSupport::upper_triangular)
678 for (unsigned int i = 0; i < matrix.n(); ++i)
679 for (unsigned int j = 0; j < i; ++j)
680 matrix(i, j) = 0.;
681 }
682 else if (property == LAPACKSupport::symmetric &&
684 {
685 if (uplo == 'L')
686 for (unsigned int i = 0; i < matrix.n(); ++i)
687 for (unsigned int j = i + 1; j < matrix.m(); ++j)
688 matrix(i, j) = matrix(j, i);
689 else if (uplo == 'U')
690 for (unsigned int i = 0; i < matrix.n(); ++i)
691 for (unsigned int j = 0; j < i; ++j)
692 matrix(i, j) = matrix(j, i);
693 }
694}
695
696
697
698template <typename NumberType>
699void
702 const std::pair<unsigned int, unsigned int> &offset_A,
703 const std::pair<unsigned int, unsigned int> &offset_B,
704 const std::pair<unsigned int, unsigned int> &submatrix_size) const
705{
706 // submatrix is empty
707 if (submatrix_size.first == 0 || submatrix_size.second == 0)
708 return;
709
710 // range checking for matrix A
711 AssertIndexRange(offset_A.first, n_rows - submatrix_size.first + 1);
712 AssertIndexRange(offset_A.second, n_columns - submatrix_size.second + 1);
713
714 // range checking for matrix B
715 AssertIndexRange(offset_B.first, B.n_rows - submatrix_size.first + 1);
716 AssertIndexRange(offset_B.second, B.n_columns - submatrix_size.second + 1);
717
718 // Currently, copying of matrices will only be supported if A and B share the
719 // same MPI communicator
720 int ierr, comparison;
721 ierr = MPI_Comm_compare(grid->mpi_communicator,
722 B.grid->mpi_communicator,
723 &comparison);
724 AssertThrowMPI(ierr);
725 Assert(comparison == MPI_IDENT,
726 ExcMessage("Matrix A and B must have a common MPI Communicator"));
727
728 /*
729 * The routine pgemr2d requires a BLACS context resembling at least the union
730 * of process grids described by the BLACS contexts held by the ProcessGrids
731 * of matrix A and B. As A and B share the same MPI communicator, there is no
732 * need to create a union MPI communicator to initialize the BLACS context
733 */
734 int union_blacs_context = Csys2blacs_handle(this->grid->mpi_communicator);
735 const char *order = "Col";
736 int union_n_process_rows =
737 Utilities::MPI::n_mpi_processes(this->grid->mpi_communicator);
738 int union_n_process_columns = 1;
739 Cblacs_gridinit(&union_blacs_context,
740 order,
741 union_n_process_rows,
742 union_n_process_columns);
743
744 int n_grid_rows_A, n_grid_columns_A, my_row_A, my_column_A;
745 Cblacs_gridinfo(this->grid->blacs_context,
746 &n_grid_rows_A,
747 &n_grid_columns_A,
748 &my_row_A,
749 &my_column_A);
750
751 // check whether process is in the BLACS context of matrix A
752 const bool in_context_A =
753 (my_row_A >= 0 && my_row_A < n_grid_rows_A) &&
754 (my_column_A >= 0 && my_column_A < n_grid_columns_A);
755
756 int n_grid_rows_B, n_grid_columns_B, my_row_B, my_column_B;
757 Cblacs_gridinfo(B.grid->blacs_context,
758 &n_grid_rows_B,
759 &n_grid_columns_B,
760 &my_row_B,
761 &my_column_B);
762
763 // check whether process is in the BLACS context of matrix B
764 const bool in_context_B =
765 (my_row_B >= 0 && my_row_B < n_grid_rows_B) &&
766 (my_column_B >= 0 && my_column_B < n_grid_columns_B);
767
768 const int n_rows_submatrix = submatrix_size.first;
769 const int n_columns_submatrix = submatrix_size.second;
770
771 // due to Fortran indexing one has to be added
772 int ia = offset_A.first + 1, ja = offset_A.second + 1;
773 int ib = offset_B.first + 1, jb = offset_B.second + 1;
774
775 std::array<int, 9> desc_A, desc_B;
776
777 const NumberType *loc_vals_A = nullptr;
778 NumberType *loc_vals_B = nullptr;
779
780 // Note: the function pgemr2d has to be called for all processes in the union
781 // BLACS context If the calling process is not part of the BLACS context of A,
782 // desc_A[1] has to be -1 and all other parameters do not have to be set If
783 // the calling process is not part of the BLACS context of B, desc_B[1] has to
784 // be -1 and all other parameters do not have to be set
785 if (in_context_A)
786 {
787 if (this->values.size() != 0)
788 loc_vals_A = this->values.data();
789
790 for (unsigned int i = 0; i < desc_A.size(); ++i)
791 desc_A[i] = this->descriptor[i];
792 }
793 else
794 desc_A[1] = -1;
795
796 if (in_context_B)
797 {
798 if (B.values.size() != 0)
799 loc_vals_B = B.values.data();
800
801 for (unsigned int i = 0; i < desc_B.size(); ++i)
802 desc_B[i] = B.descriptor[i];
803 }
804 else
805 desc_B[1] = -1;
806
807 pgemr2d(&n_rows_submatrix,
808 &n_columns_submatrix,
809 loc_vals_A,
810 &ia,
811 &ja,
812 desc_A.data(),
813 loc_vals_B,
814 &ib,
815 &jb,
816 desc_B.data(),
817 &union_blacs_context);
818
820
821 // releasing the union BLACS context
822 Cblacs_gridexit(union_blacs_context);
823}
824
825
826
827template <typename NumberType>
828void
830{
831 Assert(n_rows == dest.n_rows, ExcDimensionMismatch(n_rows, dest.n_rows));
832 Assert(n_columns == dest.n_columns,
833 ExcDimensionMismatch(n_columns, dest.n_columns));
834
835 if (this->grid->mpi_process_is_active)
837 this->descriptor[0] == 1,
839 "Copying of ScaLAPACK matrices only implemented for dense matrices"));
840 if (dest.grid->mpi_process_is_active)
842 dest.descriptor[0] == 1,
844 "Copying of ScaLAPACK matrices only implemented for dense matrices"));
845
846 /*
847 * just in case of different process grids or block-cyclic distributions
848 * inter-process communication is necessary
849 * if distributed matrices have the same process grid and block sizes, local
850 * copying is enough
851 */
852 if ((this->grid != dest.grid) || (row_block_size != dest.row_block_size) ||
853 (column_block_size != dest.column_block_size))
854 {
855 /*
856 * get the MPI communicator, which is the union of the source and
857 * destination MPI communicator
858 */
859 int ierr = 0;
860 MPI_Group group_source, group_dest, group_union;
861 ierr = MPI_Comm_group(this->grid->mpi_communicator, &group_source);
862 AssertThrowMPI(ierr);
863 ierr = MPI_Comm_group(dest.grid->mpi_communicator, &group_dest);
864 AssertThrowMPI(ierr);
865 ierr = MPI_Group_union(group_source, group_dest, &group_union);
866 AssertThrowMPI(ierr);
867 MPI_Comm mpi_communicator_union;
868
869 // to create a communicator representing the union of the source
870 // and destination MPI communicator we need a communicator that
871 // is guaranteed to contain all desired processes -- i.e.,
872 // MPI_COMM_WORLD. on the other hand, as documented in the MPI
873 // standard, MPI_Comm_create_group is not collective on all
874 // processes in the first argument, but instead is collective on
875 // only those processes listed in the group. in other words,
876 // there is really no harm in passing MPI_COMM_WORLD as the
877 // first argument, even if the program we are currently running
878 // and that is calling this function only works on a subset of
879 // processes. the same holds for the wrapper/fallback we are using here.
880
882 ierr = MPI_Comm_create_group(MPI_COMM_WORLD,
883 group_union,
884 mpi_tag,
885 &mpi_communicator_union);
886 AssertThrowMPI(ierr);
887
888 /*
889 * The routine pgemr2d requires a BLACS context resembling at least the
890 * union of process grids described by the BLACS contexts of matrix A and
891 * B
892 */
893 int union_blacs_context = Csys2blacs_handle(mpi_communicator_union);
894 const char *order = "Col";
895 int union_n_process_rows =
896 Utilities::MPI::n_mpi_processes(mpi_communicator_union);
897 int union_n_process_columns = 1;
898 Cblacs_gridinit(&union_blacs_context,
899 order,
900 union_n_process_rows,
901 union_n_process_columns);
902
903 const NumberType *loc_vals_source = nullptr;
904 NumberType *loc_vals_dest = nullptr;
905
906 if (this->grid->mpi_process_is_active && (this->values.size() > 0))
907 {
908 AssertThrow(this->values.size() > 0,
910 "source: process is active but local matrix empty"));
911 loc_vals_source = this->values.data();
912 }
913 if (dest.grid->mpi_process_is_active && (dest.values.size() > 0))
914 {
916 dest.values.size() > 0,
918 "destination: process is active but local matrix empty"));
919 loc_vals_dest = dest.values.data();
920 }
921 pgemr2d(&n_rows,
922 &n_columns,
923 loc_vals_source,
924 &submatrix_row,
925 &submatrix_column,
926 descriptor,
927 loc_vals_dest,
928 &dest.submatrix_row,
929 &dest.submatrix_column,
930 dest.descriptor,
931 &union_blacs_context);
932
933 Cblacs_gridexit(union_blacs_context);
934
935 if (mpi_communicator_union != MPI_COMM_NULL)
936 Utilities::MPI::free_communicator(mpi_communicator_union);
937 ierr = MPI_Group_free(&group_source);
938 AssertThrowMPI(ierr);
939 ierr = MPI_Group_free(&group_dest);
940 AssertThrowMPI(ierr);
941 ierr = MPI_Group_free(&group_union);
942 AssertThrowMPI(ierr);
943 }
944 else
945 // process is active in the process grid
946 if (this->grid->mpi_process_is_active)
947 dest.values = this->values;
948
949 dest.state = state;
950 dest.property = property;
951}
952
953
954
955template <typename NumberType>
956void
959{
960 add(B, 0, 1, true);
961}
962
963
964
965template <typename NumberType>
966void
968 const NumberType alpha,
969 const NumberType beta,
970 const bool transpose_B)
971{
972 if (transpose_B)
973 {
974 Assert(n_rows == B.n_columns, ExcDimensionMismatch(n_rows, B.n_columns));
975 Assert(n_columns == B.n_rows, ExcDimensionMismatch(n_columns, B.n_rows));
976 Assert(column_block_size == B.row_block_size,
977 ExcDimensionMismatch(column_block_size, B.row_block_size));
978 Assert(row_block_size == B.column_block_size,
979 ExcDimensionMismatch(row_block_size, B.column_block_size));
980 }
981 else
982 {
983 Assert(n_rows == B.n_rows, ExcDimensionMismatch(n_rows, B.n_rows));
984 Assert(n_columns == B.n_columns,
985 ExcDimensionMismatch(n_columns, B.n_columns));
986 Assert(column_block_size == B.column_block_size,
987 ExcDimensionMismatch(column_block_size, B.column_block_size));
988 Assert(row_block_size == B.row_block_size,
989 ExcDimensionMismatch(row_block_size, B.row_block_size));
990 }
991 Assert(this->grid == B.grid,
992 ExcMessage("The matrices A and B need to have the same process grid"));
993
994 if (this->grid->mpi_process_is_active)
995 {
996 char trans_b = transpose_B ? 'T' : 'N';
997 NumberType *A_loc =
998 (this->values.size() > 0) ? this->values.data() : nullptr;
999 const NumberType *B_loc =
1000 (B.values.size() > 0) ? B.values.data() : nullptr;
1001
1002 pgeadd(&trans_b,
1003 &n_rows,
1004 &n_columns,
1005 &beta,
1006 B_loc,
1007 &B.submatrix_row,
1009 B.descriptor,
1010 &alpha,
1011 A_loc,
1012 &submatrix_row,
1013 &submatrix_column,
1014 descriptor);
1015 }
1016 state = LAPACKSupport::matrix;
1017}
1018
1019
1020
1021template <typename NumberType>
1022void
1025{
1026 add(B, 1, a, false);
1027}
1028
1029
1030
1031template <typename NumberType>
1032void
1035{
1036 add(B, 1, a, true);
1037}
1038
1039
1040
1041template <typename NumberType>
1042void
1045 const NumberType c,
1047 const bool transpose_A,
1048 const bool transpose_B) const
1049{
1050 Assert(this->grid == B.grid,
1051 ExcMessage("The matrices A and B need to have the same process grid"));
1052 Assert(C.grid == B.grid,
1053 ExcMessage("The matrices B and C need to have the same process grid"));
1054
1055 // see for further info:
1056 // https://www.ibm.com/support/knowledgecenter/SSNR5K_4.2.0/com.ibm.cluster.pessl.v4r2.pssl100.doc/am6gr_lgemm.htm
1057 if (!transpose_A && !transpose_B)
1058 {
1059 Assert(this->n_columns == B.n_rows,
1060 ExcDimensionMismatch(this->n_columns, B.n_rows));
1061 Assert(this->n_rows == C.n_rows,
1062 ExcDimensionMismatch(this->n_rows, C.n_rows));
1063 Assert(B.n_columns == C.n_columns,
1064 ExcDimensionMismatch(B.n_columns, C.n_columns));
1065 Assert(this->row_block_size == C.row_block_size,
1066 ExcDimensionMismatch(this->row_block_size, C.row_block_size));
1067 Assert(this->column_block_size == B.row_block_size,
1068 ExcDimensionMismatch(this->column_block_size, B.row_block_size));
1069 Assert(B.column_block_size == C.column_block_size,
1070 ExcDimensionMismatch(B.column_block_size, C.column_block_size));
1071 }
1072 else if (transpose_A && !transpose_B)
1073 {
1074 Assert(this->n_rows == B.n_rows,
1075 ExcDimensionMismatch(this->n_rows, B.n_rows));
1076 Assert(this->n_columns == C.n_rows,
1077 ExcDimensionMismatch(this->n_columns, C.n_rows));
1078 Assert(B.n_columns == C.n_columns,
1079 ExcDimensionMismatch(B.n_columns, C.n_columns));
1080 Assert(this->column_block_size == C.row_block_size,
1081 ExcDimensionMismatch(this->column_block_size, C.row_block_size));
1082 Assert(this->row_block_size == B.row_block_size,
1083 ExcDimensionMismatch(this->row_block_size, B.row_block_size));
1084 Assert(B.column_block_size == C.column_block_size,
1085 ExcDimensionMismatch(B.column_block_size, C.column_block_size));
1086 }
1087 else if (!transpose_A && transpose_B)
1088 {
1089 Assert(this->n_columns == B.n_columns,
1090 ExcDimensionMismatch(this->n_columns, B.n_columns));
1091 Assert(this->n_rows == C.n_rows,
1092 ExcDimensionMismatch(this->n_rows, C.n_rows));
1093 Assert(B.n_rows == C.n_columns,
1094 ExcDimensionMismatch(B.n_rows, C.n_columns));
1095 Assert(this->row_block_size == C.row_block_size,
1096 ExcDimensionMismatch(this->row_block_size, C.row_block_size));
1097 Assert(this->column_block_size == B.column_block_size,
1098 ExcDimensionMismatch(this->column_block_size,
1100 Assert(B.row_block_size == C.column_block_size,
1101 ExcDimensionMismatch(B.row_block_size, C.column_block_size));
1102 }
1103 else // if (transpose_A && transpose_B)
1104 {
1105 Assert(this->n_rows == B.n_columns,
1106 ExcDimensionMismatch(this->n_rows, B.n_columns));
1107 Assert(this->n_columns == C.n_rows,
1108 ExcDimensionMismatch(this->n_columns, C.n_rows));
1109 Assert(B.n_rows == C.n_columns,
1110 ExcDimensionMismatch(B.n_rows, C.n_columns));
1111 Assert(this->column_block_size == C.row_block_size,
1112 ExcDimensionMismatch(this->row_block_size, C.row_block_size));
1113 Assert(this->row_block_size == B.column_block_size,
1114 ExcDimensionMismatch(this->column_block_size, B.row_block_size));
1115 Assert(B.row_block_size == C.column_block_size,
1116 ExcDimensionMismatch(B.column_block_size, C.column_block_size));
1117 }
1118
1119 if (this->grid->mpi_process_is_active)
1120 {
1121 char trans_a = transpose_A ? 'T' : 'N';
1122 char trans_b = transpose_B ? 'T' : 'N';
1123
1124 const NumberType *A_loc =
1125 (this->values.size() > 0) ? this->values.data() : nullptr;
1126 const NumberType *B_loc =
1127 (B.values.size() > 0) ? B.values.data() : nullptr;
1128 NumberType *C_loc = (C.values.size() > 0) ? C.values.data() : nullptr;
1129 int m = C.n_rows;
1130 int n = C.n_columns;
1131 int k = transpose_A ? this->n_rows : this->n_columns;
1132
1133 pgemm(&trans_a,
1134 &trans_b,
1135 &m,
1136 &n,
1137 &k,
1138 &b,
1139 A_loc,
1140 &(this->submatrix_row),
1141 &(this->submatrix_column),
1142 this->descriptor,
1143 B_loc,
1144 &B.submatrix_row,
1146 B.descriptor,
1147 &c,
1148 C_loc,
1149 &C.submatrix_row,
1150 &C.submatrix_column,
1151 C.descriptor);
1152 }
1153 C.state = LAPACKSupport::matrix;
1154}
1155
1156
1157
1158template <typename NumberType>
1159void
1162 const bool adding) const
1163{
1164 if (adding)
1165 mult(1., B, 1., C, false, false);
1166 else
1167 mult(1., B, 0, C, false, false);
1168}
1169
1170
1171
1172template <typename NumberType>
1173void
1176 const bool adding) const
1177{
1178 if (adding)
1179 mult(1., B, 1., C, true, false);
1180 else
1181 mult(1., B, 0, C, true, false);
1182}
1183
1184
1185
1186template <typename NumberType>
1187void
1190 const bool adding) const
1191{
1192 if (adding)
1193 mult(1., B, 1., C, false, true);
1194 else
1195 mult(1., B, 0, C, false, true);
1196}
1197
1198
1199
1200template <typename NumberType>
1201void
1204 const bool adding) const
1205{
1206 if (adding)
1207 mult(1., B, 1., C, true, true);
1208 else
1209 mult(1., B, 0, C, true, true);
1210}
1211
1212
1213
1214template <typename NumberType>
1215void
1217{
1218 Assert(
1219 n_columns == n_rows && property == LAPACKSupport::Property::symmetric,
1220 ExcMessage(
1221 "Cholesky factorization can be applied to symmetric matrices only."));
1223 ExcMessage(
1224 "Matrix has to be in Matrix state before calling this function."));
1225
1226 if (grid->mpi_process_is_active)
1227 {
1228 int info = 0;
1229 NumberType *A_loc = this->values.data();
1230 // pdpotrf_(&uplo,&n_columns,A_loc,&submatrix_row,&submatrix_column,descriptor,&info);
1231 ppotrf(&uplo,
1232 &n_columns,
1233 A_loc,
1234 &submatrix_row,
1235 &submatrix_column,
1236 descriptor,
1237 &info);
1238 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("ppotrf", info));
1239 }
1241 property = (uplo == 'L' ? LAPACKSupport::lower_triangular :
1243}
1244
1245
1246
1247template <typename NumberType>
1248void
1250{
1252 ExcMessage(
1253 "Matrix has to be in Matrix state before calling this function."));
1254
1255 if (grid->mpi_process_is_active)
1256 {
1257 int info = 0;
1258 NumberType *A_loc = this->values.data();
1259
1260 const int iarow = indxg2p_(&submatrix_row,
1261 &row_block_size,
1262 &(grid->this_process_row),
1263 &first_process_row,
1264 &(grid->n_process_rows));
1265 const int mp = numroc_(&n_rows,
1266 &row_block_size,
1267 &(grid->this_process_row),
1268 &iarow,
1269 &(grid->n_process_rows));
1270 ipiv.resize(mp + row_block_size);
1271
1272 pgetrf(&n_rows,
1273 &n_columns,
1274 A_loc,
1275 &submatrix_row,
1276 &submatrix_column,
1277 descriptor,
1278 ipiv.data(),
1279 &info);
1280 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pgetrf", info));
1281 }
1284}
1285
1286
1287
1288template <typename NumberType>
1289void
1291{
1292 // Check whether matrix is symmetric and save flag.
1293 // If a Cholesky factorization has been applied previously,
1294 // the original matrix was symmetric.
1295 const bool is_symmetric = (property == LAPACKSupport::symmetric ||
1297
1298 // Check whether matrix is triangular and is in an unfactorized state.
1299 const bool is_triangular = (property == LAPACKSupport::upper_triangular ||
1300 property == LAPACKSupport::lower_triangular) &&
1301 (state == LAPACKSupport::State::matrix ||
1303
1304 if (is_triangular)
1305 {
1306 if (grid->mpi_process_is_active)
1307 {
1308 const char uploTriangular =
1309 property == LAPACKSupport::upper_triangular ? 'U' : 'L';
1310 const char diag = 'N';
1311 int info = 0;
1312 NumberType *A_loc = this->values.data();
1313 ptrtri(&uploTriangular,
1314 &diag,
1315 &n_columns,
1316 A_loc,
1317 &submatrix_row,
1318 &submatrix_column,
1319 descriptor,
1320 &info);
1321 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("ptrtri", info));
1322 // The inversion is stored in the same part as the triangular matrix,
1323 // so we don't need to re-set the property here.
1324 }
1325 }
1326 else
1327 {
1328 // Matrix is neither in Cholesky nor LU state.
1329 // Compute the required factorizations based on the property of the
1330 // matrix.
1331 if (!(state == LAPACKSupport::State::lu ||
1333 {
1334 if (is_symmetric)
1335 compute_cholesky_factorization();
1336 else
1337 compute_lu_factorization();
1338 }
1339 if (grid->mpi_process_is_active)
1340 {
1341 int info = 0;
1342 NumberType *A_loc = this->values.data();
1343
1344 if (is_symmetric)
1345 {
1346 ppotri(&uplo,
1347 &n_columns,
1348 A_loc,
1349 &submatrix_row,
1350 &submatrix_column,
1351 descriptor,
1352 &info);
1353 AssertThrow(info == 0,
1354 LAPACKSupport::ExcErrorCode("ppotri", info));
1356 }
1357 else
1358 {
1359 int lwork = -1, liwork = -1;
1360 work.resize(1);
1361 iwork.resize(1);
1362
1363 pgetri(&n_columns,
1364 A_loc,
1365 &submatrix_row,
1366 &submatrix_column,
1367 descriptor,
1368 ipiv.data(),
1369 work.data(),
1370 &lwork,
1371 iwork.data(),
1372 &liwork,
1373 &info);
1374
1375 AssertThrow(info == 0,
1376 LAPACKSupport::ExcErrorCode("pgetri", info));
1377 lwork = static_cast<int>(work[0]);
1378 liwork = iwork[0];
1379 work.resize(lwork);
1380 iwork.resize(liwork);
1381
1382 pgetri(&n_columns,
1383 A_loc,
1384 &submatrix_row,
1385 &submatrix_column,
1386 descriptor,
1387 ipiv.data(),
1388 work.data(),
1389 &lwork,
1390 iwork.data(),
1391 &liwork,
1392 &info);
1393
1394 AssertThrow(info == 0,
1395 LAPACKSupport::ExcErrorCode("pgetri", info));
1396 }
1397 }
1398 }
1400}
1401
1402
1403
1404template <typename NumberType>
1405std::vector<NumberType>
1407 const std::pair<unsigned int, unsigned int> &index_limits,
1408 const bool compute_eigenvectors)
1409{
1410 // check validity of index limits
1411 AssertIndexRange(index_limits.first, n_rows);
1412 AssertIndexRange(index_limits.second, n_rows);
1413
1414 std::pair<unsigned int, unsigned int> idx =
1415 std::make_pair(std::min(index_limits.first, index_limits.second),
1416 std::max(index_limits.first, index_limits.second));
1417
1418 // compute all eigenvalues/eigenvectors
1419 if (idx.first == 0 && idx.second == static_cast<unsigned int>(n_rows - 1))
1420 return eigenpairs_symmetric(compute_eigenvectors);
1421 else
1422 return eigenpairs_symmetric(compute_eigenvectors, idx);
1423}
1424
1425
1426
1427template <typename NumberType>
1428std::vector<NumberType>
1430 const std::pair<NumberType, NumberType> &value_limits,
1431 const bool compute_eigenvectors)
1432{
1433 Assert(!std::isnan(value_limits.first),
1434 ExcMessage("value_limits.first is NaN"));
1435 Assert(!std::isnan(value_limits.second),
1436 ExcMessage("value_limits.second is NaN"));
1437
1438 std::pair<unsigned int, unsigned int> indices =
1439 std::make_pair(numbers::invalid_unsigned_int,
1441
1442 return eigenpairs_symmetric(compute_eigenvectors, indices, value_limits);
1443}
1444
1445
1446
1447template <typename NumberType>
1448std::vector<NumberType>
1450 const bool compute_eigenvectors,
1451 const std::pair<unsigned int, unsigned int> &eigenvalue_idx,
1452 const std::pair<NumberType, NumberType> &eigenvalue_limits)
1453{
1455 ExcMessage(
1456 "Matrix has to be in Matrix state before calling this function."));
1457 Assert(property == LAPACKSupport::symmetric,
1458 ExcMessage("Matrix has to be symmetric for this operation."));
1459
1460 std::scoped_lock lock(mutex);
1461
1462 const bool use_values = (std::isnan(eigenvalue_limits.first) ||
1463 std::isnan(eigenvalue_limits.second)) ?
1464 false :
1465 true;
1466 const bool use_indices =
1467 ((eigenvalue_idx.first == numbers::invalid_unsigned_int) ||
1468 (eigenvalue_idx.second == numbers::invalid_unsigned_int)) ?
1469 false :
1470 true;
1471
1472 Assert(
1473 !(use_values && use_indices),
1474 ExcMessage(
1475 "Prescribing both the index and value range for the eigenvalues is ambiguous"));
1476
1477 // if computation of eigenvectors is not required use a sufficiently small
1478 // distributed matrix
1479 std::unique_ptr<ScaLAPACKMatrix<NumberType>> eigenvectors =
1480 compute_eigenvectors ?
1481 std::make_unique<ScaLAPACKMatrix<NumberType>>(n_rows,
1482 grid,
1483 row_block_size) :
1484 std::make_unique<ScaLAPACKMatrix<NumberType>>(
1485 grid->n_process_rows, grid->n_process_columns, grid, 1, 1);
1486
1487 eigenvectors->property = property;
1488 // number of eigenvalues to be returned from psyevx; upon successful exit ev
1489 // contains the m selected eigenvalues in ascending order set to all
1490 // eigenvaleus in case we will be using psyev.
1491 int m = n_rows;
1492 std::vector<NumberType> ev(n_rows);
1493
1494 if (grid->mpi_process_is_active)
1495 {
1496 int info = 0;
1497 /*
1498 * for jobz==N only eigenvalues are computed, for jobz='V' also the
1499 * eigenvectors of the matrix are computed
1500 */
1501 char jobz = compute_eigenvectors ? 'V' : 'N';
1502 char range = 'A';
1503 // default value is to compute all eigenvalues and optionally eigenvectors
1504 bool all_eigenpairs = true;
1505 NumberType vl = NumberType(), vu = NumberType();
1506 int il = 1, iu = 1;
1507 // number of eigenvectors to be returned;
1508 // upon successful exit the first m=nz columns contain the selected
1509 // eigenvectors (only if jobz=='V')
1510 int nz = 0;
1511 NumberType abstol = NumberType();
1512
1513 // orfac decides which eigenvectors should be reorthogonalized
1514 // see
1515 // http://www.netlib.org/scalapack/explore-html/df/d1a/pdsyevx_8f_source.html
1516 // for explanation to keeps simple no reorthogonalized will be done by
1517 // setting orfac to 0
1518 NumberType orfac = 0;
1519 // contains the indices of eigenvectors that failed to converge
1520 std::vector<int> ifail;
1521 // This array contains indices of eigenvectors corresponding to
1522 // a cluster of eigenvalues that could not be reorthogonalized
1523 // due to insufficient workspace
1524 // see
1525 // http://www.netlib.org/scalapack/explore-html/df/d1a/pdsyevx_8f_source.html
1526 // for explanation
1527 std::vector<int> iclustr;
1528 // This array contains the gap between eigenvalues whose
1529 // eigenvectors could not be reorthogonalized.
1530 // see
1531 // http://www.netlib.org/scalapack/explore-html/df/d1a/pdsyevx_8f_source.html
1532 // for explanation
1533 std::vector<NumberType> gap(n_local_rows * n_local_columns);
1534
1535 // index range for eigenvalues is not specified
1536 if (!use_indices)
1537 {
1538 // interval for eigenvalues is not specified and consequently all
1539 // eigenvalues/eigenpairs will be computed
1540 if (!use_values)
1541 {
1542 range = 'A';
1543 all_eigenpairs = true;
1544 }
1545 else
1546 {
1547 range = 'V';
1548 all_eigenpairs = false;
1549 vl = std::min(eigenvalue_limits.first, eigenvalue_limits.second);
1550 vu = std::max(eigenvalue_limits.first, eigenvalue_limits.second);
1551 }
1552 }
1553 else
1554 {
1555 range = 'I';
1556 all_eigenpairs = false;
1557 // as Fortran starts counting/indexing from 1 unlike C/C++, where it
1558 // starts from 0
1559 il = std::min(eigenvalue_idx.first, eigenvalue_idx.second) + 1;
1560 iu = std::max(eigenvalue_idx.first, eigenvalue_idx.second) + 1;
1561 }
1562 NumberType *A_loc = this->values.data();
1563 /*
1564 * by setting lwork to -1 a workspace query for optimal length of work is
1565 * performed
1566 */
1567 int lwork = -1;
1568 int liwork = -1;
1569 NumberType *eigenvectors_loc =
1570 (compute_eigenvectors ? eigenvectors->values.data() : nullptr);
1571 /*
1572 * According to the official "documentation" found on the internet
1573 * (aka source file ppsyevx.f [1]) the work array has to have a
1574 * minimal size of max(3, lwork). Because we query for optimal size
1575 * (lwork == -1) we have to guarantee at least three doubles. The
1576 * necessary size of iwork is not specified, so let's use three as
1577 * well.
1578 * [1]
1579 * https://netlib.org/scalapack/explore-html/df/d1a/pdsyevx_8f_source.html
1580 */
1581 work.resize(3);
1582 iwork.resize(3);
1583
1584 if (all_eigenpairs)
1585 {
1586 psyev(&jobz,
1587 &uplo,
1588 &n_rows,
1589 A_loc,
1590 &submatrix_row,
1591 &submatrix_column,
1592 descriptor,
1593 ev.data(),
1594 eigenvectors_loc,
1595 &eigenvectors->submatrix_row,
1596 &eigenvectors->submatrix_column,
1597 eigenvectors->descriptor,
1598 work.data(),
1599 &lwork,
1600 &info);
1601 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyev", info));
1602 }
1603 else
1604 {
1605 char cmach = compute_eigenvectors ? 'U' : 'S';
1606 plamch(&(this->grid->blacs_context), &cmach, abstol);
1607 abstol *= 2;
1608 ifail.resize(n_rows);
1609 iclustr.resize(2 * grid->n_process_rows * grid->n_process_columns);
1610 gap.resize(grid->n_process_rows * grid->n_process_columns);
1611
1612 psyevx(&jobz,
1613 &range,
1614 &uplo,
1615 &n_rows,
1616 A_loc,
1617 &submatrix_row,
1618 &submatrix_column,
1619 descriptor,
1620 &vl,
1621 &vu,
1622 &il,
1623 &iu,
1624 &abstol,
1625 &m,
1626 &nz,
1627 ev.data(),
1628 &orfac,
1629 eigenvectors_loc,
1630 &eigenvectors->submatrix_row,
1631 &eigenvectors->submatrix_column,
1632 eigenvectors->descriptor,
1633 work.data(),
1634 &lwork,
1635 iwork.data(),
1636 &liwork,
1637 ifail.data(),
1638 iclustr.data(),
1639 gap.data(),
1640 &info);
1641 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyevx", info));
1642 }
1643 lwork = static_cast<int>(work[0]);
1644 work.resize(lwork);
1645
1646 if (all_eigenpairs)
1647 {
1648 psyev(&jobz,
1649 &uplo,
1650 &n_rows,
1651 A_loc,
1652 &submatrix_row,
1653 &submatrix_column,
1654 descriptor,
1655 ev.data(),
1656 eigenvectors_loc,
1657 &eigenvectors->submatrix_row,
1658 &eigenvectors->submatrix_column,
1659 eigenvectors->descriptor,
1660 work.data(),
1661 &lwork,
1662 &info);
1663
1664 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyev", info));
1665 }
1666 else
1667 {
1668 liwork = iwork[0];
1669 AssertThrow(liwork > 0, ExcInternalError());
1670 iwork.resize(liwork);
1671
1672 psyevx(&jobz,
1673 &range,
1674 &uplo,
1675 &n_rows,
1676 A_loc,
1677 &submatrix_row,
1678 &submatrix_column,
1679 descriptor,
1680 &vl,
1681 &vu,
1682 &il,
1683 &iu,
1684 &abstol,
1685 &m,
1686 &nz,
1687 ev.data(),
1688 &orfac,
1689 eigenvectors_loc,
1690 &eigenvectors->submatrix_row,
1691 &eigenvectors->submatrix_column,
1692 eigenvectors->descriptor,
1693 work.data(),
1694 &lwork,
1695 iwork.data(),
1696 &liwork,
1697 ifail.data(),
1698 iclustr.data(),
1699 gap.data(),
1700 &info);
1701
1702 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyevx", info));
1703 }
1704 // if eigenvectors are queried copy eigenvectors to original matrix
1705 // as the temporary matrix eigenvectors has identical dimensions and
1706 // block-cyclic distribution we simply swap the local array
1707 if (compute_eigenvectors)
1708 this->values.swap(eigenvectors->values);
1709
1710 // adapt the size of ev to fit m upon return
1711 while (ev.size() > static_cast<size_type>(m))
1712 ev.pop_back();
1713 }
1714 /*
1715 * send number of computed eigenvalues to inactive processes
1716 */
1717 grid->send_to_inactive(&m, 1);
1718
1719 /*
1720 * inactive processes have to resize array of eigenvalues
1721 */
1722 if (!grid->mpi_process_is_active)
1723 ev.resize(m);
1724 /*
1725 * send the eigenvalues to processors not being part of the process grid
1726 */
1727 grid->send_to_inactive(ev.data(), ev.size());
1728
1729 /*
1730 * if only eigenvalues are queried the content of the matrix will be destroyed
1731 * if the eigenpairs are queried matrix A on exit stores the eigenvectors in
1732 * the columns
1733 */
1734 if (compute_eigenvectors)
1735 {
1738 }
1739 else
1741
1742 return ev;
1743}
1744
1745
1746
1747template <typename NumberType>
1748std::vector<NumberType>
1750 const std::pair<unsigned int, unsigned int> &index_limits,
1751 const bool compute_eigenvectors)
1752{
1753 // Check validity of index limits.
1754 AssertIndexRange(index_limits.first, static_cast<unsigned int>(n_rows));
1755 AssertIndexRange(index_limits.second, static_cast<unsigned int>(n_rows));
1756
1757 const std::pair<unsigned int, unsigned int> idx =
1758 std::make_pair(std::min(index_limits.first, index_limits.second),
1759 std::max(index_limits.first, index_limits.second));
1760
1761 // Compute all eigenvalues/eigenvectors.
1762 if (idx.first == 0 && idx.second == static_cast<unsigned int>(n_rows - 1))
1763 return eigenpairs_symmetric_MRRR(compute_eigenvectors);
1764 else
1765 return eigenpairs_symmetric_MRRR(compute_eigenvectors, idx);
1766}
1767
1768
1769
1770template <typename NumberType>
1771std::vector<NumberType>
1773 const std::pair<NumberType, NumberType> &value_limits,
1774 const bool compute_eigenvectors)
1775{
1776 AssertIsFinite(value_limits.first);
1777 AssertIsFinite(value_limits.second);
1778
1779 const std::pair<unsigned int, unsigned int> indices =
1780 std::make_pair(numbers::invalid_unsigned_int,
1782
1783 return eigenpairs_symmetric_MRRR(compute_eigenvectors, indices, value_limits);
1784}
1785
1786
1787
1788template <typename NumberType>
1789std::vector<NumberType>
1791 const bool compute_eigenvectors,
1792 const std::pair<unsigned int, unsigned int> &eigenvalue_idx,
1793 const std::pair<NumberType, NumberType> &eigenvalue_limits)
1794{
1796 ExcMessage(
1797 "Matrix has to be in Matrix state before calling this function."));
1798 Assert(property == LAPACKSupport::symmetric,
1799 ExcMessage("Matrix has to be symmetric for this operation."));
1800
1801 std::scoped_lock lock(mutex);
1802
1803 const bool use_values = (std::isnan(eigenvalue_limits.first) ||
1804 std::isnan(eigenvalue_limits.second)) ?
1805 false :
1806 true;
1807 const bool use_indices =
1808 ((eigenvalue_idx.first == numbers::invalid_unsigned_int) ||
1809 (eigenvalue_idx.second == numbers::invalid_unsigned_int)) ?
1810 false :
1811 true;
1812
1813 Assert(
1814 !(use_values && use_indices),
1815 ExcMessage(
1816 "Prescribing both the index and value range for the eigenvalues is ambiguous"));
1817
1818 // If computation of eigenvectors is not required, use a sufficiently small
1819 // distributed matrix.
1820 std::unique_ptr<ScaLAPACKMatrix<NumberType>> eigenvectors =
1821 compute_eigenvectors ?
1822 std::make_unique<ScaLAPACKMatrix<NumberType>>(n_rows,
1823 grid,
1824 row_block_size) :
1825 std::make_unique<ScaLAPACKMatrix<NumberType>>(
1826 grid->n_process_rows, grid->n_process_columns, grid, 1, 1);
1827
1828 eigenvectors->property = property;
1829 // Number of eigenvalues to be returned from psyevr; upon successful exit ev
1830 // contains the m selected eigenvalues in ascending order.
1831 int m = n_rows;
1832 std::vector<NumberType> ev(n_rows);
1833
1834 // Number of eigenvectors to be returned;
1835 // Upon successful exit the first m=nz columns contain the selected
1836 // eigenvectors (only if jobz=='V').
1837 int nz = 0;
1838
1839 if (grid->mpi_process_is_active)
1840 {
1841 int info = 0;
1842 /*
1843 * For jobz==N only eigenvalues are computed, for jobz='V' also the
1844 * eigenvectors of the matrix are computed.
1845 */
1846 char jobz = compute_eigenvectors ? 'V' : 'N';
1847 // Default value is to compute all eigenvalues and optionally
1848 // eigenvectors.
1849 char range = 'A';
1850 NumberType vl = NumberType(), vu = NumberType();
1851 int il = 1, iu = 1;
1852
1853 // Index range for eigenvalues is not specified.
1854 if (!use_indices)
1855 {
1856 // Interval for eigenvalues is not specified and consequently all
1857 // eigenvalues/eigenpairs will be computed.
1858 if (!use_values)
1859 {
1860 range = 'A';
1861 }
1862 else
1863 {
1864 range = 'V';
1865 vl = std::min(eigenvalue_limits.first, eigenvalue_limits.second);
1866 vu = std::max(eigenvalue_limits.first, eigenvalue_limits.second);
1867 }
1868 }
1869 else
1870 {
1871 range = 'I';
1872 // As Fortran starts counting/indexing from 1 unlike C/C++, where it
1873 // starts from 0.
1874 il = std::min(eigenvalue_idx.first, eigenvalue_idx.second) + 1;
1875 iu = std::max(eigenvalue_idx.first, eigenvalue_idx.second) + 1;
1876 }
1877 NumberType *A_loc = this->values.data();
1878
1879 /*
1880 * By setting lwork to -1 a workspace query for optimal length of work is
1881 * performed.
1882 */
1883 int lwork = -1;
1884 int liwork = -1;
1885 NumberType *eigenvectors_loc =
1886 (compute_eigenvectors ? eigenvectors->values.data() : nullptr);
1887 work.resize(1);
1888 iwork.resize(1);
1889
1890 psyevr(&jobz,
1891 &range,
1892 &uplo,
1893 &n_rows,
1894 A_loc,
1895 &submatrix_row,
1896 &submatrix_column,
1897 descriptor,
1898 &vl,
1899 &vu,
1900 &il,
1901 &iu,
1902 &m,
1903 &nz,
1904 ev.data(),
1905 eigenvectors_loc,
1906 &eigenvectors->submatrix_row,
1907 &eigenvectors->submatrix_column,
1908 eigenvectors->descriptor,
1909 work.data(),
1910 &lwork,
1911 iwork.data(),
1912 &liwork,
1913 &info);
1914
1915 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyevr", info));
1916
1917 lwork = static_cast<int>(work[0]);
1918 work.resize(lwork);
1919 liwork = iwork[0];
1920 iwork.resize(liwork);
1921
1922 psyevr(&jobz,
1923 &range,
1924 &uplo,
1925 &n_rows,
1926 A_loc,
1927 &submatrix_row,
1928 &submatrix_column,
1929 descriptor,
1930 &vl,
1931 &vu,
1932 &il,
1933 &iu,
1934 &m,
1935 &nz,
1936 ev.data(),
1937 eigenvectors_loc,
1938 &eigenvectors->submatrix_row,
1939 &eigenvectors->submatrix_column,
1940 eigenvectors->descriptor,
1941 work.data(),
1942 &lwork,
1943 iwork.data(),
1944 &liwork,
1945 &info);
1946
1947 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("psyevr", info));
1948
1949 if (compute_eigenvectors)
1951 m == nz,
1952 ExcMessage(
1953 "psyevr failed to compute all eigenvectors for the selected eigenvalues"));
1954
1955 // If eigenvectors are queried, copy eigenvectors to original matrix.
1956 // As the temporary matrix eigenvectors has identical dimensions and
1957 // block-cyclic distribution we simply swap the local array.
1958 if (compute_eigenvectors)
1959 this->values.swap(eigenvectors->values);
1960
1961 // Adapt the size of ev to fit m upon return.
1962 while (ev.size() > static_cast<size_type>(m))
1963 ev.pop_back();
1964 }
1965 /*
1966 * Send number of computed eigenvalues to inactive processes.
1967 */
1968 grid->send_to_inactive(&m, 1);
1969
1970 /*
1971 * Inactive processes have to resize array of eigenvalues.
1972 */
1973 if (!grid->mpi_process_is_active)
1974 ev.resize(m);
1975 /*
1976 * Send the eigenvalues to processors not being part of the process grid.
1977 */
1978 grid->send_to_inactive(ev.data(), ev.size());
1979
1980 /*
1981 * If only eigenvalues are queried, the content of the matrix will be
1982 * destroyed. If the eigenpairs are queried, matrix A on exit stores the
1983 * eigenvectors in the columns.
1984 */
1985 if (compute_eigenvectors)
1986 {
1989 }
1990 else
1992
1993 return ev;
1994}
1995
1996
1997
1998template <typename NumberType>
1999std::vector<NumberType>
2002{
2004 ExcMessage(
2005 "Matrix has to be in Matrix state before calling this function."));
2006 Assert(row_block_size == column_block_size,
2007 ExcDimensionMismatch(row_block_size, column_block_size));
2008
2009 const bool left_singular_vectors = (U != nullptr) ? true : false;
2010 const bool right_singular_vectors = (VT != nullptr) ? true : false;
2011
2012 if (left_singular_vectors)
2013 {
2014 Assert(n_rows == U->n_rows, ExcDimensionMismatch(n_rows, U->n_rows));
2015 Assert(U->n_rows == U->n_columns,
2016 ExcDimensionMismatch(U->n_rows, U->n_columns));
2017 Assert(row_block_size == U->row_block_size,
2018 ExcDimensionMismatch(row_block_size, U->row_block_size));
2019 Assert(column_block_size == U->column_block_size,
2020 ExcDimensionMismatch(column_block_size, U->column_block_size));
2021 Assert(grid->blacs_context == U->grid->blacs_context,
2022 ExcDimensionMismatch(grid->blacs_context, U->grid->blacs_context));
2023 }
2024 if (right_singular_vectors)
2025 {
2026 Assert(n_columns == VT->n_rows,
2027 ExcDimensionMismatch(n_columns, VT->n_rows));
2028 Assert(VT->n_rows == VT->n_columns,
2030 Assert(row_block_size == VT->row_block_size,
2031 ExcDimensionMismatch(row_block_size, VT->row_block_size));
2032 Assert(column_block_size == VT->column_block_size,
2033 ExcDimensionMismatch(column_block_size, VT->column_block_size));
2034 Assert(grid->blacs_context == VT->grid->blacs_context,
2035 ExcDimensionMismatch(grid->blacs_context,
2036 VT->grid->blacs_context));
2037 }
2038 std::scoped_lock lock(mutex);
2039
2040 std::vector<NumberType> sv(std::min(n_rows, n_columns));
2041
2042 if (grid->mpi_process_is_active)
2043 {
2044 char jobu = left_singular_vectors ? 'V' : 'N';
2045 char jobvt = right_singular_vectors ? 'V' : 'N';
2046 NumberType *A_loc = this->values.data();
2047 NumberType *U_loc = left_singular_vectors ? U->values.data() : nullptr;
2048 NumberType *VT_loc = right_singular_vectors ? VT->values.data() : nullptr;
2049 int info = 0;
2050 /*
2051 * by setting lwork to -1 a workspace query for optimal length of work is
2052 * performed
2053 */
2054 int lwork = -1;
2055 work.resize(1);
2056
2057 pgesvd(&jobu,
2058 &jobvt,
2059 &n_rows,
2060 &n_columns,
2061 A_loc,
2062 &submatrix_row,
2063 &submatrix_column,
2064 descriptor,
2065 &*sv.begin(),
2066 U_loc,
2067 &U->submatrix_row,
2068 &U->submatrix_column,
2069 U->descriptor,
2070 VT_loc,
2071 &VT->submatrix_row,
2072 &VT->submatrix_column,
2073 VT->descriptor,
2074 work.data(),
2075 &lwork,
2076 &info);
2077 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pgesvd", info));
2078
2079 lwork = static_cast<int>(work[0]);
2080 work.resize(lwork);
2081
2082 pgesvd(&jobu,
2083 &jobvt,
2084 &n_rows,
2085 &n_columns,
2086 A_loc,
2087 &submatrix_row,
2088 &submatrix_column,
2089 descriptor,
2090 &*sv.begin(),
2091 U_loc,
2092 &U->submatrix_row,
2093 &U->submatrix_column,
2094 U->descriptor,
2095 VT_loc,
2096 &VT->submatrix_row,
2097 &VT->submatrix_column,
2098 VT->descriptor,
2099 work.data(),
2100 &lwork,
2101 &info);
2102 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pgesvd", info));
2103 }
2104
2105 /*
2106 * send the singular values to processors not being part of the process grid
2107 */
2108 grid->send_to_inactive(sv.data(), sv.size());
2109
2112
2113 return sv;
2114}
2115
2116
2117
2118template <typename NumberType>
2119void
2121 const bool transpose)
2122{
2123 Assert(grid == B.grid,
2124 ExcMessage("The matrices A and B need to have the same process grid"));
2126 ExcMessage(
2127 "Matrix has to be in Matrix state before calling this function."));
2129 ExcMessage(
2130 "Matrix B has to be in Matrix state before calling this function."));
2131
2132 if (transpose)
2133 {
2134 Assert(n_columns == B.n_rows, ExcDimensionMismatch(n_columns, B.n_rows));
2135 }
2136 else
2137 {
2138 Assert(n_rows == B.n_rows, ExcDimensionMismatch(n_rows, B.n_rows));
2139 }
2140
2141 // see
2142 // https://www.ibm.com/support/knowledgecenter/en/SSNR5K_4.2.0/com.ibm.cluster.pessl.v4r2.pssl100.doc/am6gr_lgels.htm
2143 Assert(row_block_size == column_block_size,
2144 ExcMessage(
2145 "Use identical block sizes for rows and columns of matrix A"));
2147 ExcMessage(
2148 "Use identical block sizes for rows and columns of matrix B"));
2149 Assert(row_block_size == B.row_block_size,
2150 ExcMessage(
2151 "Use identical block-cyclic distribution for matrices A and B"));
2152
2153 std::scoped_lock lock(mutex);
2154
2155 if (grid->mpi_process_is_active)
2156 {
2157 char trans = transpose ? 'T' : 'N';
2158 NumberType *A_loc = this->values.data();
2159 NumberType *B_loc = B.values.data();
2160 int info = 0;
2161 /*
2162 * by setting lwork to -1 a workspace query for optimal length of work is
2163 * performed
2164 */
2165 int lwork = -1;
2166 work.resize(1);
2167
2168 pgels(&trans,
2169 &n_rows,
2170 &n_columns,
2171 &B.n_columns,
2172 A_loc,
2173 &submatrix_row,
2174 &submatrix_column,
2175 descriptor,
2176 B_loc,
2177 &B.submatrix_row,
2179 B.descriptor,
2180 work.data(),
2181 &lwork,
2182 &info);
2183 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pgels", info));
2184
2185 lwork = static_cast<int>(work[0]);
2186 work.resize(lwork);
2187
2188 pgels(&trans,
2189 &n_rows,
2190 &n_columns,
2191 &B.n_columns,
2192 A_loc,
2193 &submatrix_row,
2194 &submatrix_column,
2195 descriptor,
2196 B_loc,
2197 &B.submatrix_row,
2199 B.descriptor,
2200 work.data(),
2201 &lwork,
2202 &info);
2203 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pgels", info));
2204 }
2206}
2207
2208
2209
2210template <typename NumberType>
2211unsigned int
2213{
2215 ExcMessage(
2216 "Matrix has to be in Matrix state before calling this function."));
2217 Assert(row_block_size == column_block_size,
2218 ExcMessage(
2219 "Use identical block sizes for rows and columns of matrix A"));
2220 Assert(
2221 ratio > 0. && ratio < 1.,
2222 ExcMessage(
2223 "input parameter ratio has to be larger than zero and smaller than 1"));
2224
2226 n_rows,
2227 grid,
2228 row_block_size,
2229 row_block_size,
2231 ScaLAPACKMatrix<NumberType> VT(n_columns,
2232 n_columns,
2233 grid,
2234 row_block_size,
2235 row_block_size,
2237 std::vector<NumberType> sv = this->compute_SVD(&U, &VT);
2238 AssertThrow(sv[0] > std::numeric_limits<NumberType>::min(),
2239 ExcMessage("Matrix has rank 0"));
2240
2241 // Get number of singular values fulfilling the following: sv[i] > sv[0] *
2242 // ratio Obviously, 0-th element already satisfies sv[0] > sv[0] * ratio The
2243 // singular values in sv are ordered by descending value so we break out of
2244 // the loop if a singular value is smaller than sv[0] * ratio.
2245 unsigned int n_sv = 1;
2246 std::vector<NumberType> inv_sigma;
2247 inv_sigma.push_back(1 / sv[0]);
2248
2249 for (unsigned int i = 1; i < sv.size(); ++i)
2250 if (sv[i] > sv[0] * ratio)
2251 {
2252 ++n_sv;
2253 inv_sigma.push_back(1 / sv[i]);
2254 }
2255 else
2256 break;
2257
2258 // For the matrix multiplication we use only the columns of U and rows of VT
2259 // which are associated with singular values larger than the limit. That saves
2260 // computational time for matrices with rank significantly smaller than
2261 // min(n_rows,n_columns)
2262 ScaLAPACKMatrix<NumberType> U_R(n_rows,
2263 n_sv,
2264 grid,
2265 row_block_size,
2266 row_block_size,
2269 n_columns,
2270 grid,
2271 row_block_size,
2272 row_block_size,
2274 U.copy_to(U_R,
2275 std::make_pair(0, 0),
2276 std::make_pair(0, 0),
2277 std::make_pair(n_rows, n_sv));
2278 VT.copy_to(VT_R,
2279 std::make_pair(0, 0),
2280 std::make_pair(0, 0),
2281 std::make_pair(n_sv, n_columns));
2282
2283 VT_R.scale_rows(inv_sigma);
2284 this->reinit(n_columns,
2285 n_rows,
2286 this->grid,
2287 row_block_size,
2288 column_block_size,
2290 VT_R.mult(1, U_R, 0, *this, true, true);
2292 return n_sv;
2293}
2294
2295
2296
2297template <typename NumberType>
2298NumberType
2300 const NumberType a_norm) const
2301{
2303 ExcMessage(
2304 "Matrix has to be in Cholesky state before calling this function."));
2305 std::scoped_lock lock(mutex);
2306 NumberType rcond = 0.;
2307
2308 if (grid->mpi_process_is_active)
2309 {
2310 int liwork = n_local_rows;
2311 iwork.resize(liwork);
2312
2313 int info = 0;
2314 const NumberType *A_loc = this->values.data();
2315
2316 // by setting lwork to -1 a workspace query for optimal length of work is
2317 // performed
2318 int lwork = -1;
2319 work.resize(1);
2320 ppocon(&uplo,
2321 &n_columns,
2322 A_loc,
2323 &submatrix_row,
2324 &submatrix_column,
2325 descriptor,
2326 &a_norm,
2327 &rcond,
2328 work.data(),
2329 &lwork,
2330 iwork.data(),
2331 &liwork,
2332 &info);
2333 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pdpocon", info));
2334 lwork = static_cast<int>(std::ceil(work[0]));
2335 work.resize(lwork);
2336
2337 // now the actual run:
2338 ppocon(&uplo,
2339 &n_columns,
2340 A_loc,
2341 &submatrix_row,
2342 &submatrix_column,
2343 descriptor,
2344 &a_norm,
2345 &rcond,
2346 work.data(),
2347 &lwork,
2348 iwork.data(),
2349 &liwork,
2350 &info);
2351 AssertThrow(info == 0, LAPACKSupport::ExcErrorCode("pdpocon", info));
2352 }
2353 grid->send_to_inactive(&rcond);
2354 return rcond;
2355}
2356
2357
2358
2359template <typename NumberType>
2360NumberType
2362{
2363 const char type('O');
2364
2365 if (property == LAPACKSupport::symmetric)
2366 return norm_symmetric(type);
2367 else
2368 return norm_general(type);
2369}
2370
2371
2372
2373template <typename NumberType>
2374NumberType
2376{
2377 const char type('I');
2378
2379 if (property == LAPACKSupport::symmetric)
2380 return norm_symmetric(type);
2381 else
2382 return norm_general(type);
2383}
2384
2385
2386
2387template <typename NumberType>
2388NumberType
2390{
2391 const char type('F');
2392
2393 if (property == LAPACKSupport::symmetric)
2394 return norm_symmetric(type);
2395 else
2396 return norm_general(type);
2397}
2398
2399
2400
2401template <typename NumberType>
2402NumberType
2404{
2405 Assert(state == LAPACKSupport::matrix ||
2407 ExcMessage("norms can be called in matrix state only."));
2408 std::scoped_lock lock(mutex);
2409 NumberType res = 0.;
2410
2411 if (grid->mpi_process_is_active)
2412 {
2413 const int iarow = indxg2p_(&submatrix_row,
2414 &row_block_size,
2415 &(grid->this_process_row),
2416 &first_process_row,
2417 &(grid->n_process_rows));
2418 const int iacol = indxg2p_(&submatrix_column,
2419 &column_block_size,
2420 &(grid->this_process_column),
2421 &first_process_column,
2422 &(grid->n_process_columns));
2423 const int mp0 = numroc_(&n_rows,
2424 &row_block_size,
2425 &(grid->this_process_row),
2426 &iarow,
2427 &(grid->n_process_rows));
2428 const int nq0 = numroc_(&n_columns,
2429 &column_block_size,
2430 &(grid->this_process_column),
2431 &iacol,
2432 &(grid->n_process_columns));
2433
2434 // type='M': compute largest absolute value
2435 // type='F' || type='E': compute Frobenius norm
2436 // type='0' || type='1': compute infinity norm
2437 int lwork = 0; // for type == 'M' || type == 'F' || type == 'E'
2438 if (type == 'O' || type == '1')
2439 lwork = nq0;
2440 else if (type == 'I')
2441 lwork = mp0;
2442
2443 work.resize(lwork);
2444 const NumberType *A_loc = this->values.begin();
2445 res = plange(&type,
2446 &n_rows,
2447 &n_columns,
2448 A_loc,
2449 &submatrix_row,
2450 &submatrix_column,
2451 descriptor,
2452 work.data());
2453 }
2454 grid->send_to_inactive(&res);
2455 return res;
2456}
2457
2458
2459
2460template <typename NumberType>
2461NumberType
2463{
2464 Assert(state == LAPACKSupport::matrix ||
2466 ExcMessage("norms can be called in matrix state only."));
2467 Assert(property == LAPACKSupport::symmetric,
2468 ExcMessage("Matrix has to be symmetric for this operation."));
2469 std::scoped_lock lock(mutex);
2470 NumberType res = 0.;
2471
2472 if (grid->mpi_process_is_active)
2473 {
2474 // int IROFFA = MOD( IA-1, MB_A )
2475 // int ICOFFA = MOD( JA-1, NB_A )
2476 const int lcm =
2477 ilcm_(&(grid->n_process_rows), &(grid->n_process_columns));
2478 const int v2 = lcm / (grid->n_process_rows);
2479
2480 const int IAROW = indxg2p_(&submatrix_row,
2481 &row_block_size,
2482 &(grid->this_process_row),
2483 &first_process_row,
2484 &(grid->n_process_rows));
2485 const int IACOL = indxg2p_(&submatrix_column,
2486 &column_block_size,
2487 &(grid->this_process_column),
2488 &first_process_column,
2489 &(grid->n_process_columns));
2490 const int Np0 = numroc_(&n_columns /*+IROFFA*/,
2491 &row_block_size,
2492 &(grid->this_process_row),
2493 &IAROW,
2494 &(grid->n_process_rows));
2495 const int Nq0 = numroc_(&n_columns /*+ICOFFA*/,
2496 &column_block_size,
2497 &(grid->this_process_column),
2498 &IACOL,
2499 &(grid->n_process_columns));
2500
2501 const int v1 = iceil_(&Np0, &row_block_size);
2502 const int ldw = (n_local_rows == n_local_columns) ?
2503 0 :
2504 row_block_size * iceil_(&v1, &v2);
2505
2506 const int lwork =
2507 (type == 'M' || type == 'F' || type == 'E') ? 0 : 2 * Nq0 + Np0 + ldw;
2508 work.resize(lwork);
2509 const NumberType *A_loc = this->values.begin();
2510 res = plansy(&type,
2511 &uplo,
2512 &n_columns,
2513 A_loc,
2514 &submatrix_row,
2515 &submatrix_column,
2516 descriptor,
2517 work.data());
2518 }
2519 grid->send_to_inactive(&res);
2520 return res;
2521}
2522
2523
2524
2525#ifdef DEAL_II_WITH_HDF5
2526namespace internal
2527{
2528 namespace
2529 {
2530 void
2531 create_HDF5_state_enum_id(hid_t &state_enum_id)
2532 {
2533 // create HDF5 enum type for LAPACKSupport::State
2535 state_enum_id = H5Tcreate(H5T_ENUM, sizeof(LAPACKSupport::State));
2537 herr_t status = H5Tenum_insert(state_enum_id, "cholesky", &val);
2538 AssertThrow(status >= 0, ExcInternalError());
2540 status = H5Tenum_insert(state_enum_id, "eigenvalues", &val);
2541 AssertThrow(status >= 0, ExcInternalError());
2543 status = H5Tenum_insert(state_enum_id, "inverse_matrix", &val);
2544 AssertThrow(status >= 0, ExcInternalError());
2546 status = H5Tenum_insert(state_enum_id, "inverse_svd", &val);
2547 AssertThrow(status >= 0, ExcInternalError());
2549 status = H5Tenum_insert(state_enum_id, "lu", &val);
2550 AssertThrow(status >= 0, ExcInternalError());
2552 status = H5Tenum_insert(state_enum_id, "matrix", &val);
2553 AssertThrow(status >= 0, ExcInternalError());
2555 status = H5Tenum_insert(state_enum_id, "svd", &val);
2556 AssertThrow(status >= 0, ExcInternalError());
2558 status = H5Tenum_insert(state_enum_id, "unusable", &val);
2559 AssertThrow(status >= 0, ExcInternalError());
2560 }
2561
2562 void
2563 create_HDF5_property_enum_id(hid_t &property_enum_id)
2564 {
2565 // create HDF5 enum type for LAPACKSupport::Property
2566 property_enum_id = H5Tcreate(H5T_ENUM, sizeof(LAPACKSupport::Property));
2568 herr_t status = H5Tenum_insert(property_enum_id, "diagonal", &prop);
2569 AssertThrow(status >= 0, ExcInternalError());
2571 status = H5Tenum_insert(property_enum_id, "general", &prop);
2572 AssertThrow(status >= 0, ExcInternalError());
2574 status = H5Tenum_insert(property_enum_id, "hessenberg", &prop);
2575 AssertThrow(status >= 0, ExcInternalError());
2577 status = H5Tenum_insert(property_enum_id, "lower_triangular", &prop);
2578 AssertThrow(status >= 0, ExcInternalError());
2580 status = H5Tenum_insert(property_enum_id, "symmetric", &prop);
2581 AssertThrow(status >= 0, ExcInternalError());
2583 status = H5Tenum_insert(property_enum_id, "upper_triangular", &prop);
2584 AssertThrow(status >= 0, ExcInternalError());
2585 }
2586 } // namespace
2587} // namespace internal
2588#endif
2589
2590
2591
2592template <typename NumberType>
2593void
2595 const std::string &filename,
2596 const std::pair<unsigned int, unsigned int> &chunk_size) const
2597{
2598#ifndef DEAL_II_WITH_HDF5
2599 (void)filename;
2600 (void)chunk_size;
2601 AssertThrow(false, ExcNeedsHDF5());
2602#else
2603
2604 std::pair<unsigned int, unsigned int> chunks_size_ = chunk_size;
2605
2606 if (chunks_size_.first == numbers::invalid_unsigned_int ||
2607 chunks_size_.second == numbers::invalid_unsigned_int)
2608 {
2609 // default: store the matrix in chunks of columns
2610 chunks_size_.first = n_rows;
2611 chunks_size_.second = 1;
2612 }
2613 Assert(chunks_size_.first > 0,
2614 ExcMessage("The row chunk size must be larger than 0."));
2615 AssertIndexRange(chunks_size_.first, n_rows + 1);
2616 Assert(chunks_size_.second > 0,
2617 ExcMessage("The column chunk size must be larger than 0."));
2618 AssertIndexRange(chunks_size_.second, n_columns + 1);
2619
2620# ifdef H5_HAVE_PARALLEL
2621 // implementation for configurations equipped with a parallel file system
2622 save_parallel(filename, chunks_size_);
2623
2624# else
2625 // implementation for configurations with no parallel file system
2626 save_serial(filename, chunks_size_);
2627
2628# endif
2629#endif
2630}
2631
2632
2633
2634template <typename NumberType>
2635void
2637 const std::string &filename,
2638 const std::pair<unsigned int, unsigned int> &chunk_size) const
2639{
2640#ifndef DEAL_II_WITH_HDF5
2641 (void)filename;
2642 (void)chunk_size;
2644#else
2645
2646 /*
2647 * The content of the distributed matrix is copied to a matrix using a 1x1
2648 * process grid. Therefore, one process has all the data and can write it to a
2649 * file.
2650 *
2651 * Create a 1x1 column grid which will be used to initialize
2652 * an effectively serial ScaLAPACK matrix to gather the contents from the
2653 * current object
2654 */
2655 const auto column_grid =
2656 std::make_shared<Utilities::MPI::ProcessGrid>(this->grid->mpi_communicator,
2657 1,
2658 1);
2659
2660 const int MB = n_rows, NB = n_columns;
2661 ScaLAPACKMatrix<NumberType> tmp(n_rows, n_columns, column_grid, MB, NB);
2662 copy_to(tmp);
2663
2664 // the 1x1 grid has only one process and this one writes
2665 // the content of the matrix to the HDF5 file
2666 if (tmp.grid->mpi_process_is_active)
2667 {
2668 herr_t status;
2669
2670 // create a new file using default properties
2671 hid_t file_id =
2672 H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
2673
2674 // modify dataset creation properties, i.e. enable chunking
2675 hsize_t chunk_dims[2];
2676 // revert order of rows and columns as ScaLAPACK uses column-major
2677 // ordering
2678 chunk_dims[0] = chunk_size.second;
2679 chunk_dims[1] = chunk_size.first;
2680 hid_t data_property = H5Pcreate(H5P_DATASET_CREATE);
2681 status = H5Pset_chunk(data_property, 2, chunk_dims);
2682 AssertThrow(status >= 0, ExcIO());
2683
2684 // create the data space for the dataset
2685 hsize_t dims[2];
2686 // change order of rows and columns as ScaLAPACKMatrix uses column major
2687 // ordering
2688 dims[0] = n_columns;
2689 dims[1] = n_rows;
2690 hid_t dataspace_id = H5Screate_simple(2, dims, nullptr);
2691
2692 // create the dataset within the file using chunk creation properties
2693 hid_t type_id = hdf5_type_id(tmp.values.data());
2694 hid_t dataset_id = H5Dcreate2(file_id,
2695 "/matrix",
2696 type_id,
2697 dataspace_id,
2698 H5P_DEFAULT,
2699 data_property,
2700 H5P_DEFAULT);
2701
2702 // write the dataset
2703 status = H5Dwrite(
2704 dataset_id, type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp.values.data());
2705 AssertThrow(status >= 0, ExcIO());
2706
2707 // create HDF5 enum type for LAPACKSupport::State and
2708 // LAPACKSupport::Property
2709 hid_t state_enum_id, property_enum_id;
2710 internal::create_HDF5_state_enum_id(state_enum_id);
2711 internal::create_HDF5_property_enum_id(property_enum_id);
2712
2713 // create the data space for the state enum
2714 hsize_t dims_state[1];
2715 dims_state[0] = 1;
2716 hid_t state_enum_dataspace = H5Screate_simple(1, dims_state, nullptr);
2717 // create the dataset for the state enum
2718 hid_t state_enum_dataset = H5Dcreate2(file_id,
2719 "/state",
2720 state_enum_id,
2721 state_enum_dataspace,
2722 H5P_DEFAULT,
2723 H5P_DEFAULT,
2724 H5P_DEFAULT);
2725 // write the dataset for the state enum
2726 status = H5Dwrite(state_enum_dataset,
2727 state_enum_id,
2728 H5S_ALL,
2729 H5S_ALL,
2730 H5P_DEFAULT,
2731 &state);
2732 AssertThrow(status >= 0, ExcIO());
2733
2734 // create the data space for the property enum
2735 hsize_t dims_property[1];
2736 dims_property[0] = 1;
2737 hid_t property_enum_dataspace =
2738 H5Screate_simple(1, dims_property, nullptr);
2739 // create the dataset for the property enum
2740 hid_t property_enum_dataset = H5Dcreate2(file_id,
2741 "/property",
2742 property_enum_id,
2743 property_enum_dataspace,
2744 H5P_DEFAULT,
2745 H5P_DEFAULT,
2746 H5P_DEFAULT);
2747 // write the dataset for the property enum
2748 status = H5Dwrite(property_enum_dataset,
2749 property_enum_id,
2750 H5S_ALL,
2751 H5S_ALL,
2752 H5P_DEFAULT,
2753 &property);
2754 AssertThrow(status >= 0, ExcIO());
2755
2756 // end access to the datasets and release resources used by them
2757 status = H5Dclose(dataset_id);
2758 AssertThrow(status >= 0, ExcIO());
2759 status = H5Dclose(state_enum_dataset);
2760 AssertThrow(status >= 0, ExcIO());
2761 status = H5Dclose(property_enum_dataset);
2762 AssertThrow(status >= 0, ExcIO());
2763
2764 // terminate access to the data spaces
2765 status = H5Sclose(dataspace_id);
2766 AssertThrow(status >= 0, ExcIO());
2767 status = H5Sclose(state_enum_dataspace);
2768 AssertThrow(status >= 0, ExcIO());
2769 status = H5Sclose(property_enum_dataspace);
2770 AssertThrow(status >= 0, ExcIO());
2771
2772 // release enum data types
2773 status = H5Tclose(state_enum_id);
2774 AssertThrow(status >= 0, ExcIO());
2775 status = H5Tclose(property_enum_id);
2776 AssertThrow(status >= 0, ExcIO());
2777
2778 // release the creation property
2779 status = H5Pclose(data_property);
2780 AssertThrow(status >= 0, ExcIO());
2781
2782 // close the file.
2783 status = H5Fclose(file_id);
2784 AssertThrow(status >= 0, ExcIO());
2785 }
2786#endif
2787}
2788
2789
2790
2791template <typename NumberType>
2792void
2794 const std::string &filename,
2795 const std::pair<unsigned int, unsigned int> &chunk_size) const
2796{
2797#ifndef DEAL_II_WITH_HDF5
2798 (void)filename;
2799 (void)chunk_size;
2801#else
2802
2803 const unsigned int n_mpi_processes(
2804 Utilities::MPI::n_mpi_processes(this->grid->mpi_communicator));
2805 MPI_Info info = MPI_INFO_NULL;
2806 /*
2807 * The content of the distributed matrix is copied to a matrix using a
2808 * 1xn_processes process grid. Therefore, the processes hold contiguous chunks
2809 * of the matrix, which they can write to the file
2810 *
2811 * Create a 1xn_processes column grid
2812 */
2813 const auto column_grid =
2814 std::make_shared<Utilities::MPI::ProcessGrid>(this->grid->mpi_communicator,
2815 1,
2816 n_mpi_processes);
2817
2818 const int MB = n_rows;
2819 /*
2820 * If the ratio n_columns/n_mpi_processes is smaller than the column block
2821 * size of the original matrix, the redistribution and saving of the matrix
2822 * requires a significant amount of MPI communication. Therefore, it is better
2823 * to set a minimum value for the block size NB, causing only
2824 * ceil(n_columns/NB) processes being actively involved in saving the matrix.
2825 * Example: A 2*10^9 x 400 matrix is distributed on a 80 x 5 process grid
2826 * using block size 32. Instead of distributing the matrix on a 1 x 400
2827 * process grid with a row block size of 2*10^9 and a column block size of 1,
2828 * the minimum value for NB yields that only ceil(400/32)=13 processes will be
2829 * writing the matrix to disk.
2830 */
2831 const int NB = std::max(static_cast<int>(std::ceil(
2832 static_cast<double>(n_columns) / n_mpi_processes)),
2833 column_block_size);
2834
2835 ScaLAPACKMatrix<NumberType> tmp(n_rows, n_columns, column_grid, MB, NB);
2836 copy_to(tmp);
2837
2838 // get pointer to data held by the process
2839 NumberType *data = (tmp.values.size() > 0) ? tmp.values.data() : nullptr;
2840
2841 herr_t status;
2842 // dataset dimensions
2843 hsize_t dims[2];
2844
2845 // set up file access property list with parallel I/O access
2846 hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
2847 status = H5Pset_fapl_mpio(plist_id, tmp.grid->mpi_communicator, info);
2848 AssertThrow(status >= 0, ExcIO());
2849
2850 // create a new file collectively and release property list identifier
2851 hid_t file_id =
2852 H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
2853 status = H5Pclose(plist_id);
2854 AssertThrow(status >= 0, ExcIO());
2855
2856 // As ScaLAPACK, and therefore the class ScaLAPACKMatrix, uses column-major
2857 // ordering but HDF5 row-major ordering, we have to reverse entries related to
2858 // columns and rows in the following. create the dataspace for the dataset
2859 dims[0] = tmp.n_columns;
2860 dims[1] = tmp.n_rows;
2861
2862 hid_t filespace = H5Screate_simple(2, dims, nullptr);
2863
2864 // create the chunked dataset with default properties and close filespace
2865 hsize_t chunk_dims[2];
2866 // revert order of rows and columns as ScaLAPACK uses column-major ordering
2867 chunk_dims[0] = chunk_size.second;
2868 chunk_dims[1] = chunk_size.first;
2869 plist_id = H5Pcreate(H5P_DATASET_CREATE);
2870 H5Pset_chunk(plist_id, 2, chunk_dims);
2871 hid_t type_id = hdf5_type_id(data);
2872 hid_t dset_id = H5Dcreate2(
2873 file_id, "/matrix", type_id, filespace, H5P_DEFAULT, plist_id, H5P_DEFAULT);
2874
2875 status = H5Sclose(filespace);
2876 AssertThrow(status >= 0, ExcIO());
2877
2878 status = H5Pclose(plist_id);
2879 AssertThrow(status >= 0, ExcIO());
2880
2881 // gather the number of local rows and columns from all processes
2882 std::vector<int> proc_n_local_rows(n_mpi_processes),
2883 proc_n_local_columns(n_mpi_processes);
2884 proc_n_local_rows =
2885 Utilities::MPI::all_gather(tmp.grid->mpi_communicator, tmp.n_local_rows);
2886 proc_n_local_columns =
2887 Utilities::MPI::all_gather(tmp.grid->mpi_communicator, tmp.n_local_columns);
2888
2889 const unsigned int my_rank(
2890 Utilities::MPI::this_mpi_process(tmp.grid->mpi_communicator));
2891
2892 // hyperslab selection parameters
2893 // each process defines dataset in memory and writes it to the hyperslab in
2894 // the file
2895 hsize_t count[2];
2896 count[0] = tmp.n_local_columns;
2897 count[1] = tmp.n_rows;
2898 hid_t memspace = H5Screate_simple(2, count, nullptr);
2899
2900 hsize_t offset[2] = {0};
2901 for (unsigned int i = 0; i < my_rank; ++i)
2902 offset[0] += proc_n_local_columns[i];
2903
2904 // select hyperslab in the file.
2905 filespace = H5Dget_space(dset_id);
2906 status = H5Sselect_hyperslab(
2907 filespace, H5S_SELECT_SET, offset, nullptr, count, nullptr);
2908 AssertThrow(status >= 0, ExcIO());
2909
2910 // create property list for independent dataset write
2911 plist_id = H5Pcreate(H5P_DATASET_XFER);
2912 status = H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_INDEPENDENT);
2913 AssertThrow(status >= 0, ExcIO());
2914
2915 // process with no data will not participate in writing to the file
2916 if (tmp.values.size() > 0)
2917 {
2918 status = H5Dwrite(dset_id, type_id, memspace, filespace, plist_id, data);
2919 AssertThrow(status >= 0, ExcIO());
2920 }
2921 // close/release sources
2922 status = H5Dclose(dset_id);
2923 AssertThrow(status >= 0, ExcIO());
2924 status = H5Sclose(filespace);
2925 AssertThrow(status >= 0, ExcIO());
2926 status = H5Sclose(memspace);
2927 AssertThrow(status >= 0, ExcIO());
2928 status = H5Pclose(plist_id);
2929 AssertThrow(status >= 0, ExcIO());
2930 status = H5Fclose(file_id);
2931 AssertThrow(status >= 0, ExcIO());
2932
2933 // before writing the state and property to file wait for
2934 // all processes to finish writing the matrix content to the file
2935 int ierr = MPI_Barrier(tmp.grid->mpi_communicator);
2936 AssertThrowMPI(ierr);
2937
2938 // only root process will write state and property to the file
2939 if (tmp.grid->this_mpi_process == 0)
2940 {
2941 // open file using default properties
2942 hid_t file_id_reopen =
2943 H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
2944
2945 // create HDF5 enum type for LAPACKSupport::State and
2946 // LAPACKSupport::Property
2947 hid_t state_enum_id, property_enum_id;
2948 internal::create_HDF5_state_enum_id(state_enum_id);
2949 internal::create_HDF5_property_enum_id(property_enum_id);
2950
2951 // create the data space for the state enum
2952 hsize_t dims_state[1];
2953 dims_state[0] = 1;
2954 hid_t state_enum_dataspace = H5Screate_simple(1, dims_state, nullptr);
2955 // create the dataset for the state enum
2956 hid_t state_enum_dataset = H5Dcreate2(file_id_reopen,
2957 "/state",
2958 state_enum_id,
2959 state_enum_dataspace,
2960 H5P_DEFAULT,
2961 H5P_DEFAULT,
2962 H5P_DEFAULT);
2963 // write the dataset for the state enum
2964 status = H5Dwrite(state_enum_dataset,
2965 state_enum_id,
2966 H5S_ALL,
2967 H5S_ALL,
2968 H5P_DEFAULT,
2969 &state);
2970 AssertThrow(status >= 0, ExcIO());
2971
2972 // create the data space for the property enum
2973 hsize_t dims_property[1];
2974 dims_property[0] = 1;
2975 hid_t property_enum_dataspace =
2976 H5Screate_simple(1, dims_property, nullptr);
2977 // create the dataset for the property enum
2978 hid_t property_enum_dataset = H5Dcreate2(file_id_reopen,
2979 "/property",
2980 property_enum_id,
2981 property_enum_dataspace,
2982 H5P_DEFAULT,
2983 H5P_DEFAULT,
2984 H5P_DEFAULT);
2985 // write the dataset for the property enum
2986 status = H5Dwrite(property_enum_dataset,
2987 property_enum_id,
2988 H5S_ALL,
2989 H5S_ALL,
2990 H5P_DEFAULT,
2991 &property);
2992 AssertThrow(status >= 0, ExcIO());
2993
2994 status = H5Dclose(state_enum_dataset);
2995 AssertThrow(status >= 0, ExcIO());
2996 status = H5Dclose(property_enum_dataset);
2997 AssertThrow(status >= 0, ExcIO());
2998 status = H5Sclose(state_enum_dataspace);
2999 AssertThrow(status >= 0, ExcIO());
3000 status = H5Sclose(property_enum_dataspace);
3001 AssertThrow(status >= 0, ExcIO());
3002 status = H5Tclose(state_enum_id);
3003 AssertThrow(status >= 0, ExcIO());
3004 status = H5Tclose(property_enum_id);
3005 AssertThrow(status >= 0, ExcIO());
3006 status = H5Fclose(file_id_reopen);
3007 AssertThrow(status >= 0, ExcIO());
3008 }
3009
3010#endif
3011}
3012
3013
3014
3015template <typename NumberType>
3016void
3017ScaLAPACKMatrix<NumberType>::load(const std::string &filename)
3018{
3019#ifndef DEAL_II_WITH_HDF5
3020 (void)filename;
3021 AssertThrow(false, ExcNeedsHDF5());
3022#else
3023# ifdef H5_HAVE_PARALLEL
3024 // implementation for configurations equipped with a parallel file system
3025 load_parallel(filename);
3026
3027# else
3028 // implementation for configurations with no parallel file system
3029 load_serial(filename);
3030# endif
3031#endif
3032}
3033
3034
3035
3036template <typename NumberType>
3037void
3039{
3040#ifndef DEAL_II_WITH_HDF5
3041 (void)filename;
3043#else
3044
3045 /*
3046 * The content of the distributed matrix is copied to a matrix using a 1x1
3047 * process grid. Therefore, one process has all the data and can write it to a
3048 * file
3049 */
3050 // create a 1xP column grid with P being the number of MPI processes
3051 const auto one_grid =
3052 std::make_shared<Utilities::MPI::ProcessGrid>(this->grid->mpi_communicator,
3053 1,
3054 1);
3055
3056 const int MB = n_rows, NB = n_columns;
3057 ScaLAPACKMatrix<NumberType> tmp(n_rows, n_columns, one_grid, MB, NB);
3058
3059 int state_int = -1;
3060 int property_int = -1;
3061
3062 // the 1x1 grid has only one process and this one reads
3063 // the content of the matrix from the HDF5 file
3064 if (tmp.grid->mpi_process_is_active)
3065 {
3066 herr_t status;
3067
3068 // open the file in read-only mode
3069 hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
3070
3071 // open the dataset in the file
3072 hid_t dataset_id = H5Dopen2(file_id, "/matrix", H5P_DEFAULT);
3073
3074 // check the datatype of the data in the file
3075 // datatype of source and destination must have the same class
3076 // see HDF User's Guide: 6.10. Data Transfer: Datatype Conversion and
3077 // Selection
3078 hid_t datatype = H5Dget_type(dataset_id);
3079 H5T_class_t t_class_in = H5Tget_class(datatype);
3080 H5T_class_t t_class = H5Tget_class(hdf5_type_id(tmp.values.data()));
3082 t_class_in == t_class,
3083 ExcMessage(
3084 "The data type of the matrix to be read does not match the archive"));
3085
3086 // get dataspace handle
3087 hid_t dataspace_id = H5Dget_space(dataset_id);
3088 // get number of dimensions
3089 const int ndims = H5Sget_simple_extent_ndims(dataspace_id);
3090 AssertThrow(ndims == 2, ExcIO());
3091 // get every dimension
3092 hsize_t dims[2];
3093 H5Sget_simple_extent_dims(dataspace_id, dims, nullptr);
3095 static_cast<int>(dims[0]) == n_columns,
3096 ExcMessage(
3097 "The number of columns of the matrix does not match the content of the archive"));
3099 static_cast<int>(dims[1]) == n_rows,
3100 ExcMessage(
3101 "The number of rows of the matrix does not match the content of the archive"));
3102
3103 // read data
3104 status = H5Dread(dataset_id,
3105 hdf5_type_id(tmp.values.data()),
3106 H5S_ALL,
3107 H5S_ALL,
3108 H5P_DEFAULT,
3109 tmp.values.data());
3110 AssertThrow(status >= 0, ExcIO());
3111
3112 // create HDF5 enum type for LAPACKSupport::State and
3113 // LAPACKSupport::Property
3114 hid_t state_enum_id, property_enum_id;
3115 internal::create_HDF5_state_enum_id(state_enum_id);
3116 internal::create_HDF5_property_enum_id(property_enum_id);
3117
3118 // open the datasets for the state and property enum in the file
3119 hid_t dataset_state_id = H5Dopen2(file_id, "/state", H5P_DEFAULT);
3120 hid_t datatype_state = H5Dget_type(dataset_state_id);
3121 H5T_class_t t_class_state = H5Tget_class(datatype_state);
3122 AssertThrow(t_class_state == H5T_ENUM, ExcIO());
3123
3124 hid_t dataset_property_id = H5Dopen2(file_id, "/property", H5P_DEFAULT);
3125 hid_t datatype_property = H5Dget_type(dataset_property_id);
3126 H5T_class_t t_class_property = H5Tget_class(datatype_property);
3127 AssertThrow(t_class_property == H5T_ENUM, ExcIO());
3128
3129 // get dataspace handles
3130 hid_t dataspace_state = H5Dget_space(dataset_state_id);
3131 hid_t dataspace_property = H5Dget_space(dataset_property_id);
3132 // get number of dimensions
3133 const int ndims_state = H5Sget_simple_extent_ndims(dataspace_state);
3134 AssertThrow(ndims_state == 1, ExcIO());
3135 const int ndims_property = H5Sget_simple_extent_ndims(dataspace_property);
3136 AssertThrow(ndims_property == 1, ExcIO());
3137 // get every dimension
3138 hsize_t dims_state[1];
3139 H5Sget_simple_extent_dims(dataspace_state, dims_state, nullptr);
3140 AssertThrow(static_cast<int>(dims_state[0]) == 1, ExcIO());
3141 hsize_t dims_property[1];
3142 H5Sget_simple_extent_dims(dataspace_property, dims_property, nullptr);
3143 AssertThrow(static_cast<int>(dims_property[0]) == 1, ExcIO());
3144
3145 // read data
3146 status = H5Dread(dataset_state_id,
3147 state_enum_id,
3148 H5S_ALL,
3149 H5S_ALL,
3150 H5P_DEFAULT,
3151 &tmp.state);
3152 AssertThrow(status >= 0, ExcIO());
3153 // To send the state from the root process to the other processes
3154 // the state enum is casted to an integer, that will be broadcasted and
3155 // subsequently casted back to the enum type
3156 state_int = static_cast<int>(tmp.state);
3157
3158 status = H5Dread(dataset_property_id,
3159 property_enum_id,
3160 H5S_ALL,
3161 H5S_ALL,
3162 H5P_DEFAULT,
3163 &tmp.property);
3164 AssertThrow(status >= 0, ExcIO());
3165 // To send the property from the root process to the other processes
3166 // the state enum is casted to an integer, that will be broadcasted and
3167 // subsequently casted back to the enum type
3168 property_int = static_cast<int>(tmp.property);
3169
3170 // terminate access to the data spaces
3171 status = H5Sclose(dataspace_id);
3172 AssertThrow(status >= 0, ExcIO());
3173 status = H5Sclose(dataspace_state);
3174 AssertThrow(status >= 0, ExcIO());
3175 status = H5Sclose(dataspace_property);
3176 AssertThrow(status >= 0, ExcIO());
3177
3178 // release data type handles
3179 status = H5Tclose(datatype);
3180 AssertThrow(status >= 0, ExcIO());
3181 status = H5Tclose(state_enum_id);
3182 AssertThrow(status >= 0, ExcIO());
3183 status = H5Tclose(property_enum_id);
3184 AssertThrow(status >= 0, ExcIO());
3185
3186 // end access to the data sets and release resources used by them
3187 status = H5Dclose(dataset_state_id);
3188 AssertThrow(status >= 0, ExcIO());
3189 status = H5Dclose(dataset_id);
3190 AssertThrow(status >= 0, ExcIO());
3191 status = H5Dclose(dataset_property_id);
3192 AssertThrow(status >= 0, ExcIO());
3193
3194 // close the file.
3195 status = H5Fclose(file_id);
3196 AssertThrow(status >= 0, ExcIO());
3197 }
3198 // so far only the root process has the correct state integer --> broadcasting
3199 tmp.grid->send_to_inactive(&state_int, 1);
3200 // so far only the root process has the correct property integer -->
3201 // broadcasting
3202 tmp.grid->send_to_inactive(&property_int, 1);
3203
3204 tmp.state = static_cast<LAPACKSupport::State>(state_int);
3205 tmp.property = static_cast<LAPACKSupport::Property>(property_int);
3206
3207 tmp.copy_to(*this);
3208
3209#endif // DEAL_II_WITH_HDF5
3210}
3211
3212
3213
3214template <typename NumberType>
3215void
3217{
3218#ifndef DEAL_II_WITH_HDF5
3219 (void)filename;
3221#else
3222# ifndef H5_HAVE_PARALLEL
3223 (void)filename;
3225# else
3226
3227 const unsigned int n_mpi_processes(
3228 Utilities::MPI::n_mpi_processes(this->grid->mpi_communicator));
3229 MPI_Info info = MPI_INFO_NULL;
3230 /*
3231 * The content of the distributed matrix is copied to a matrix using a
3232 * 1xn_processes process grid. Therefore, the processes hold contiguous chunks
3233 * of the matrix, which they can write to the file
3234 */
3235 // create a 1xP column grid with P being the number of MPI processes
3236 const auto column_grid =
3237 std::make_shared<Utilities::MPI::ProcessGrid>(this->grid->mpi_communicator,
3238 1,
3239 n_mpi_processes);
3240
3241 const int MB = n_rows;
3242 // for the choice of NB see explanation in save_parallel()
3243 const int NB = std::max(static_cast<int>(std::ceil(
3244 static_cast<double>(n_columns) / n_mpi_processes)),
3245 column_block_size);
3246
3247 ScaLAPACKMatrix<NumberType> tmp(n_rows, n_columns, column_grid, MB, NB);
3248
3249 // get pointer to data held by the process
3250 NumberType *data = (tmp.values.size() > 0) ? tmp.values.data() : nullptr;
3251
3252 herr_t status;
3253
3254 // set up file access property list with parallel I/O access
3255 hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
3256 status = H5Pset_fapl_mpio(plist_id, tmp.grid->mpi_communicator, info);
3257 AssertThrow(status >= 0, ExcIO());
3258
3259 // open file collectively in read-only mode and release property list
3260 // identifier
3261 hid_t file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, plist_id);
3262 status = H5Pclose(plist_id);
3263 AssertThrow(status >= 0, ExcIO());
3264
3265 // open the dataset in the file collectively
3266 hid_t dataset_id = H5Dopen2(file_id, "/matrix", H5P_DEFAULT);
3267
3268 // check the datatype of the dataset in the file
3269 // if the classes of type of the dataset and the matrix do not match abort
3270 // see HDF User's Guide: 6.10. Data Transfer: Datatype Conversion and
3271 // Selection
3272 hid_t datatype = hdf5_type_id(data);
3273 hid_t datatype_inp = H5Dget_type(dataset_id);
3274 H5T_class_t t_class_inp = H5Tget_class(datatype_inp);
3275 H5T_class_t t_class = H5Tget_class(datatype);
3277 t_class_inp == t_class,
3278 ExcMessage(
3279 "The data type of the matrix to be read does not match the archive"));
3280
3281 // get the dimensions of the matrix stored in the file
3282 // get dataspace handle
3283 hid_t dataspace_id = H5Dget_space(dataset_id);
3284 // get number of dimensions
3285 const int ndims = H5Sget_simple_extent_ndims(dataspace_id);
3286 AssertThrow(ndims == 2, ExcIO());
3287 // get every dimension
3288 hsize_t dims[2];
3289 status = H5Sget_simple_extent_dims(dataspace_id, dims, nullptr);
3290 AssertThrow(status >= 0, ExcIO());
3292 static_cast<int>(dims[0]) == n_columns,
3293 ExcMessage(
3294 "The number of columns of the matrix does not match the content of the archive"));
3296 static_cast<int>(dims[1]) == n_rows,
3297 ExcMessage(
3298 "The number of rows of the matrix does not match the content of the archive"));
3299
3300 // gather the number of local rows and columns from all processes
3301 std::vector<int> proc_n_local_rows(n_mpi_processes),
3302 proc_n_local_columns(n_mpi_processes);
3303 proc_n_local_rows =
3304 Utilities::MPI::all_gather(tmp.grid->mpi_communicator, tmp.n_local_rows);
3305 proc_n_local_columns =
3306 Utilities::MPI::all_gather(tmp.grid->mpi_communicator, tmp.n_local_columns);
3307
3308 const unsigned int my_rank(
3309 Utilities::MPI::this_mpi_process(tmp.grid->mpi_communicator));
3310
3311 // hyperslab selection parameters
3312 // each process defines dataset in memory and writes it to the hyperslab in
3313 // the file
3314 hsize_t count[2];
3315 count[0] = tmp.n_local_columns;
3316 count[1] = tmp.n_local_rows;
3317
3318 hsize_t offset[2] = {0};
3319 for (unsigned int i = 0; i < my_rank; ++i)
3320 offset[0] += proc_n_local_columns[i];
3321
3322 // select hyperslab in the file
3323 status = H5Sselect_hyperslab(
3324 dataspace_id, H5S_SELECT_SET, offset, nullptr, count, nullptr);
3325 AssertThrow(status >= 0, ExcIO());
3326
3327 // create a memory dataspace independently
3328 hid_t memspace = H5Screate_simple(2, count, nullptr);
3329
3330 // read data independently
3331 status =
3332 H5Dread(dataset_id, datatype, memspace, dataspace_id, H5P_DEFAULT, data);
3333 AssertThrow(status >= 0, ExcIO());
3334
3335 // create HDF5 enum type for LAPACKSupport::State and LAPACKSupport::Property
3336 hid_t state_enum_id, property_enum_id;
3337 internal::create_HDF5_state_enum_id(state_enum_id);
3338 internal::create_HDF5_property_enum_id(property_enum_id);
3339
3340 // open the datasets for the state and property enum in the file
3341 hid_t dataset_state_id = H5Dopen2(file_id, "/state", H5P_DEFAULT);
3342 hid_t datatype_state = H5Dget_type(dataset_state_id);
3343 H5T_class_t t_class_state = H5Tget_class(datatype_state);
3344 AssertThrow(t_class_state == H5T_ENUM, ExcIO());
3345
3346 hid_t dataset_property_id = H5Dopen2(file_id, "/property", H5P_DEFAULT);
3347 hid_t datatype_property = H5Dget_type(dataset_property_id);
3348 H5T_class_t t_class_property = H5Tget_class(datatype_property);
3349 AssertThrow(t_class_property == H5T_ENUM, ExcIO());
3350
3351 // get dataspace handles
3352 hid_t dataspace_state = H5Dget_space(dataset_state_id);
3353 hid_t dataspace_property = H5Dget_space(dataset_property_id);
3354 // get number of dimensions
3355 const int ndims_state = H5Sget_simple_extent_ndims(dataspace_state);
3356 AssertThrow(ndims_state == 1, ExcIO());
3357 const int ndims_property = H5Sget_simple_extent_ndims(dataspace_property);
3358 AssertThrow(ndims_property == 1, ExcIO());
3359 // get every dimension
3360 hsize_t dims_state[1];
3361 H5Sget_simple_extent_dims(dataspace_state, dims_state, nullptr);
3362 AssertThrow(static_cast<int>(dims_state[0]) == 1, ExcIO());
3363 hsize_t dims_property[1];
3364 H5Sget_simple_extent_dims(dataspace_property, dims_property, nullptr);
3365 AssertThrow(static_cast<int>(dims_property[0]) == 1, ExcIO());
3366
3367 // read data
3368 status = H5Dread(
3369 dataset_state_id, state_enum_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp.state);
3370 AssertThrow(status >= 0, ExcIO());
3371
3372 status = H5Dread(dataset_property_id,
3373 property_enum_id,
3374 H5S_ALL,
3375 H5S_ALL,
3376 H5P_DEFAULT,
3377 &tmp.property);
3378 AssertThrow(status >= 0, ExcIO());
3379
3380 // close/release sources
3381 status = H5Sclose(memspace);
3382 AssertThrow(status >= 0, ExcIO());
3383 status = H5Dclose(dataset_id);
3384 AssertThrow(status >= 0, ExcIO());
3385 status = H5Dclose(dataset_state_id);
3386 AssertThrow(status >= 0, ExcIO());
3387 status = H5Dclose(dataset_property_id);
3388 AssertThrow(status >= 0, ExcIO());
3389 status = H5Sclose(dataspace_id);
3390 AssertThrow(status >= 0, ExcIO());
3391 status = H5Sclose(dataspace_state);
3392 AssertThrow(status >= 0, ExcIO());
3393 status = H5Sclose(dataspace_property);
3394 AssertThrow(status >= 0, ExcIO());
3395 // status = H5Tclose(datatype);
3396 // AssertThrow(status >= 0, ExcIO());
3397 status = H5Tclose(state_enum_id);
3398 AssertThrow(status >= 0, ExcIO());
3399 status = H5Tclose(property_enum_id);
3400 AssertThrow(status >= 0, ExcIO());
3401 status = H5Fclose(file_id);
3402 AssertThrow(status >= 0, ExcIO());
3403
3404 // copying the distributed matrices
3405 tmp.copy_to(*this);
3406
3407# endif // H5_HAVE_PARALLEL
3408#endif // DEAL_II_WITH_HDF5
3409}
3410
3411
3412
3413namespace internal
3414{
3415 namespace
3416 {
3417 template <typename NumberType>
3418 void
3419 scale_columns(ScaLAPACKMatrix<NumberType> &matrix,
3420 const ArrayView<const NumberType> &factors)
3421 {
3422 Assert(matrix.n() == factors.size(),
3423 ExcDimensionMismatch(matrix.n(), factors.size()));
3424
3425 for (unsigned int i = 0; i < matrix.local_n(); ++i)
3426 {
3427 const NumberType s = factors[matrix.global_column(i)];
3428
3429 for (unsigned int j = 0; j < matrix.local_m(); ++j)
3430 matrix.local_el(j, i) *= s;
3431 }
3432 }
3433
3434 template <typename NumberType>
3435 void
3436 scale_rows(ScaLAPACKMatrix<NumberType> &matrix,
3437 const ArrayView<const NumberType> &factors)
3438 {
3439 Assert(matrix.m() == factors.size(),
3440 ExcDimensionMismatch(matrix.m(), factors.size()));
3441
3442 for (unsigned int i = 0; i < matrix.local_m(); ++i)
3443 {
3444 const NumberType s = factors[matrix.global_row(i)];
3445
3446 for (unsigned int j = 0; j < matrix.local_n(); ++j)
3447 matrix.local_el(i, j) *= s;
3448 }
3449 }
3450
3451 } // namespace
3452} // namespace internal
3453
3454
3455
3456template <typename NumberType>
3457template <class InputVector>
3458void
3460{
3461 if (this->grid->mpi_process_is_active)
3462 internal::scale_columns(*this, make_array_view(factors));
3463}
3464
3465
3466
3467template <typename NumberType>
3468template <class InputVector>
3469void
3471{
3472 if (this->grid->mpi_process_is_active)
3473 internal::scale_rows(*this, make_array_view(factors));
3474}
3475
3476
3477
3478// instantiations
3479#include "lac/scalapack.inst"
3480
3481
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
std::size_t size() const
Definition array_view.h:737
size_type m() const
size_type n() const
int descriptor[9]
Definition scalapack.h:935
std::vector< NumberType > compute_SVD(ScaLAPACKMatrix< NumberType > *U=nullptr, ScaLAPACKMatrix< NumberType > *VT=nullptr)
std::vector< NumberType > eigenpairs_symmetric_by_value(const std::pair< NumberType, NumberType > &value_limits, const bool compute_eigenvectors)
NumberType frobenius_norm() const
unsigned int pseudoinverse(const NumberType ratio)
std::vector< NumberType > eigenpairs_symmetric_by_value_MRRR(const std::pair< NumberType, NumberType > &value_limits, const bool compute_eigenvectors)
void copy_from(const LAPACKFullMatrix< NumberType > &matrix, const unsigned int rank)
Definition scalapack.cc:335
void save_parallel(const std::string &filename, const std::pair< unsigned int, unsigned int > &chunk_size) const
void least_squares(ScaLAPACKMatrix< NumberType > &B, const bool transpose=false)
ScaLAPACKMatrix< NumberType > & operator=(const FullMatrix< NumberType > &)
Definition scalapack.cc:304
void Tadd(const NumberType b, const ScaLAPACKMatrix< NumberType > &B)
void mTmult(ScaLAPACKMatrix< NumberType > &C, const ScaLAPACKMatrix< NumberType > &B, const bool adding=false) const
void add(const ScaLAPACKMatrix< NumberType > &B, const NumberType a=0., const NumberType b=1., const bool transpose_B=false)
Definition scalapack.cc:967
LAPACKSupport::State get_state() const
Definition scalapack.cc:295
LAPACKSupport::Property get_property() const
Definition scalapack.cc:286
void Tmmult(ScaLAPACKMatrix< NumberType > &C, const ScaLAPACKMatrix< NumberType > &B, const bool adding=false) const
std::vector< NumberType > eigenpairs_symmetric_MRRR(const bool compute_eigenvectors, const std::pair< unsigned int, unsigned int > &index_limits=std::make_pair(numbers::invalid_unsigned_int, numbers::invalid_unsigned_int), const std::pair< NumberType, NumberType > &value_limits=std::make_pair(std::numeric_limits< NumberType >::quiet_NaN(), std::numeric_limits< NumberType >::quiet_NaN()))
void scale_rows(const InputVector &factors)
std::shared_ptr< const Utilities::MPI::ProcessGrid > grid
Definition scalapack.h:900
ScaLAPACKMatrix(const size_type n_rows, const size_type n_columns, const std::shared_ptr< const Utilities::MPI::ProcessGrid > &process_grid, const size_type row_block_size=32, const size_type column_block_size=32, const LAPACKSupport::Property property=LAPACKSupport::Property::general)
Definition scalapack.cc:60
void load(const std::string &filename)
const int submatrix_column
Definition scalapack.h:981
const int submatrix_row
Definition scalapack.h:975
void mmult(ScaLAPACKMatrix< NumberType > &C, const ScaLAPACKMatrix< NumberType > &B, const bool adding=false) const
std::vector< NumberType > eigenpairs_symmetric(const bool compute_eigenvectors, const std::pair< unsigned int, unsigned int > &index_limits=std::make_pair(numbers::invalid_unsigned_int, numbers::invalid_unsigned_int), const std::pair< NumberType, NumberType > &value_limits=std::make_pair(std::numeric_limits< NumberType >::quiet_NaN(), std::numeric_limits< NumberType >::quiet_NaN()))
void save_serial(const std::string &filename, const std::pair< unsigned int, unsigned int > &chunk_size) const
NumberType norm_general(const char type) const
void save(const std::string &filename, const std::pair< unsigned int, unsigned int > &chunk_size=std::make_pair(numbers::invalid_unsigned_int, numbers::invalid_unsigned_int)) const
void load_parallel(const std::string &filename)
NumberType l1_norm() const
void compute_lu_factorization()
NumberType norm_symmetric(const char type) const
void mult(const NumberType b, const ScaLAPACKMatrix< NumberType > &B, const NumberType c, ScaLAPACKMatrix< NumberType > &C, const bool transpose_A=false, const bool transpose_B=false) const
LAPACKSupport::Property property
Definition scalapack.h:893
void set_property(const LAPACKSupport::Property property)
Definition scalapack.cc:276
void reinit(const size_type n_rows, const size_type n_columns, const std::shared_ptr< const Utilities::MPI::ProcessGrid > &process_grid, const size_type row_block_size=32, const size_type column_block_size=32, const LAPACKSupport::Property property=LAPACKSupport::Property::general)
Definition scalapack.cc:189
void load_serial(const std::string &filename)
std::vector< NumberType > eigenpairs_symmetric_by_index_MRRR(const std::pair< unsigned int, unsigned int > &index_limits, const bool compute_eigenvectors)
NumberType reciprocal_condition_number(const NumberType a_norm) const
LAPACKSupport::State state
Definition scalapack.h:887
void TmTmult(ScaLAPACKMatrix< NumberType > &C, const ScaLAPACKMatrix< NumberType > &B, const bool adding=false) const
std::vector< NumberType > eigenpairs_symmetric_by_index(const std::pair< unsigned int, unsigned int > &index_limits, const bool compute_eigenvectors)
unsigned int global_column(const unsigned int loc_column) const
Definition scalapack.cc:489
void copy_to(FullMatrix< NumberType > &matrix) const
Definition scalapack.cc:643
unsigned int global_row(const unsigned int loc_row) const
Definition scalapack.cc:472
void compute_cholesky_factorization()
void copy_transposed(const ScaLAPACKMatrix< NumberType > &B)
Definition scalapack.cc:957
NumberType linfty_norm() const
void scale_columns(const InputVector &factors)
AlignedVector< T > values
Definition table.h:793
void reinit(const size_type size1, const size_type size2, const bool omit_default_initialization=false)
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
const unsigned int v1
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcErrorCode(std::string arg1, types::blas_int arg2)
#define Assert(cond, exc)
#define AssertIsFinite(number)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcNeedsHDF5()
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
const unsigned int my_rank
Definition mpi.cc:917
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
@ cholesky
Contents is a Cholesky decomposition.
@ lu
Contents is an LU decomposition.
@ matrix
Contents is actually a matrix.
@ unusable
Contents is something useless.
@ inverse_matrix
Contents is the inverse of a matrix.
@ svd
Matrix contains singular value decomposition,.
@ inverse_svd
Matrix is the inverse of a singular value decomposition.
@ eigenvalues
Eigenvalue vector is filled.
@ symmetric
Matrix is symmetric.
@ hessenberg
Matrix is in upper Hessenberg form.
@ diagonal
Matrix is diagonal.
@ upper_triangular
Matrix is upper triangular.
@ lower_triangular
Matrix is lower triangular.
@ general
No special properties.
@ scalapack_copy_from
ScaLAPACKMatrix<NumberType>::copy_from.
Definition mpi_tags.h:119
@ scalapack_copy_to2
ScaLAPACKMatrix<NumberType>::copy_to.
Definition mpi_tags.h:117
@ scalapack_copy_to
ScaLAPACKMatrix<NumberType>::copy_to.
Definition mpi_tags.h:115
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:103
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > all_gather(const MPI_Comm comm, const T &object_to_send)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
void free_communicator(MPI_Comm mpi_communicator)
Definition mpi.cc:165
T broadcast(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
std::array< std::pair< Number, Tensor< 1, dim, Number > >, dim > eigenvectors(const SymmetricTensor< 2, dim, Number > &T, const SymmetricTensorEigenvectorMethod method=SymmetricTensorEigenvectorMethod::ql_implicit_shifts)