Debian Codegraph @

packages/muelu/research/caglusa/MueLu_IOhelpers.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef MUELU_IOHELPERS_HPP
11 #define MUELU_IOHELPERS_HPP
12
13 #include <Teuchos_ParameterList.hpp>
14 #include <Xpetra_IO.hpp>
15 #include <Xpetra_Operator.hpp>
16
17 namespace MueLu {
18
19 using Teuchos::RCP;
20 using Teuchos::rcp;
21 using Teuchos::rcp_dynamic_cast;
22
23 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
24 struct IOhelpers {
25 static Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
26 Read(const std::string& filename,
27 const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > rowMap,
28 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > colMap,
29 const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > domainMap = Teuchos::null,
30 const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > rangeMap = Teuchos::null,
31 const bool callFillComplete = true,
32 const bool binary = false,
33 const bool readLocal = false) {
34 using IO = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
35 Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A;
36 if (readLocal)
37 A = IO::ReadLocal(filename, rowMap, colMap, domainMap, rangeMap, callFillComplete, binary);
38 else
39 A = IO::Read(filename, rowMap, colMap, domainMap, rangeMap, callFillComplete, binary);
40 return A;
41 }
42
43 static Teuchos::RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
44 Read(std::string& filename,
45 RCP<const Teuchos::Comm<int> >& comm) {
46 Teuchos::ParameterList hierarchicalParams;
47 Teuchos::updateParametersFromXmlFileAndBroadcast(filename, Teuchos::Ptr<Teuchos::ParameterList>(&hierarchicalParams), *comm);
48 auto op = Read(hierarchicalParams, comm);
49 return op;
50 }
51
52 static Teuchos::RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
53 Read(Teuchos::ParameterList& hierarchicalParams,
54 RCP<const Teuchos::Comm<int> >& comm) {
55 using HOp = Xpetra::HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
56 using blocked_matrix_type = typename HOp::blocked_matrix_type;
57 using blocked_map_type = typename blocked_matrix_type::blocked_map_type;
58 using matrix_type = typename HOp::matrix_type;
59 using map_type = typename HOp::map_type;
60 using lo_vec_type = typename blocked_map_type::lo_vec_type;
61
62 auto lib = Xpetra::UseTpetra;
63 RCP<HOp> op;
64 RCP<const map_type> map, near_colmap, clusterCoeffMap, ghosted_clusterCoeffMap, clusterMap, ghosted_clusterMap;
65 RCP<matrix_type> nearField, basisMatrix, kernelApproximations, kernelBlockGraph;
66
67 std::vector<RCP<blocked_matrix_type> > transferMatrices;
68 RCP<lo_vec_type> clusterSizes;
69 RCP<blocked_map_type> blockedClusterMap, ghosted_blockedClusterMap;
70 RCP<blocked_matrix_type> blockKernelApproximations;
71
72 const bool readBinary = hierarchicalParams.get<bool>("read binary", false);
73 const bool readLocal = hierarchicalParams.get<bool>("read local", false);
74
75 using IO = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
76
77 // row, domain and range map of the operator
78 map = IO::ReadMap(hierarchicalParams.get<std::string>("map"), lib, comm, readBinary);
79 // colmap of near field
80 near_colmap = IO::ReadMap(hierarchicalParams.get<std::string>("near colmap"), lib, comm, readBinary);
81 if (hierarchicalParams.isType<std::string>("coefficient map")) {
82 // 1-to-1 map for the cluster coefficients
83 clusterCoeffMap = IO::ReadMap(hierarchicalParams.get<std::string>("coefficient map"), lib, comm, readBinary);
84 // overlapping map for the cluster coefficients
85 ghosted_clusterCoeffMap = IO::ReadMap(hierarchicalParams.get<std::string>("ghosted coefficient map"), lib, comm, readBinary);
86 // 1-to-1 map for the clusters
87 clusterMap = IO::ReadMap(hierarchicalParams.get<std::string>("cluster map"), lib, comm, readBinary);
88 // overlapping map for the clusters
89 ghosted_clusterMap = IO::ReadMap(hierarchicalParams.get<std::string>("ghosted cluster map"), lib, comm, readBinary);
90
91 // blocked cluster map
92 clusterSizes = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ReadMultiVectorLO(hierarchicalParams.get<std::string>("gid_cluster_to_gid_coeff"), clusterMap)->getVectorNonConst(0);
93 blockedClusterMap = rcp(new blocked_map_type(clusterCoeffMap, clusterSizes));
94 }
95
96 // near field interactions
97 nearField = Read(hierarchicalParams.get<std::string>("near field matrix"), map, near_colmap, map, map, true, readBinary, readLocal);
98
99 if (hierarchicalParams.isType<std::string>("coefficient map")) {
100 // far field basis expansion coefficients
101 basisMatrix = IOhelpers::Read(hierarchicalParams.get<std::string>("basis expansion coefficient matrix"), map, clusterCoeffMap, clusterCoeffMap, map, true, readBinary, readLocal);
102
103 // far field interactions
104 kernelApproximations = IOhelpers::Read(hierarchicalParams.get<std::string>("far field interaction matrix"), clusterCoeffMap, ghosted_clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, true, readBinary, readLocal);
105 // block graph of far field interactions
106 kernelBlockGraph = IOhelpers::Read(hierarchicalParams.get<std::string>("far field interaction matrix") + ".block", clusterMap, ghosted_clusterMap, clusterMap, clusterMap, true, readBinary, readLocal);
107
108 {
109 auto import = kernelBlockGraph->getCrsGraph()->getImporter();
110 RCP<lo_vec_type> ghosted_clusterSizes = Xpetra::VectorFactory<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>::Build(ghosted_clusterMap);
111 ghosted_clusterSizes->doImport(*clusterSizes, *import, Xpetra::INSERT);
112 ghosted_blockedClusterMap = rcp(new blocked_map_type(ghosted_clusterCoeffMap, ghosted_clusterSizes));
113 }
114
115 blockKernelApproximations = rcp(new blocked_matrix_type(kernelApproximations, kernelBlockGraph, blockedClusterMap, ghosted_blockedClusterMap));
116
117 // Transfer matrices
118 auto transfersList = hierarchicalParams.sublist("shift coefficient matrices");
119 for (int i = 0; i < transfersList.numParams(); i++) {
120 std::string filename = transfersList.get<std::string>(std::to_string(i));
121 auto transferPoint = IOhelpers::Read(filename, clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, clusterCoeffMap, true, readBinary, readLocal);
122 auto transferBlock = IOhelpers::Read(filename + ".block", clusterMap, clusterMap, clusterMap, clusterMap, true, readBinary, readLocal);
123 auto transfer = rcp(new blocked_matrix_type(transferPoint, transferBlock, blockedClusterMap));
124 transferMatrices.push_back(transfer);
125 }
126 }
127
128 RCP<Teuchos::ParameterList> params;
129 if (hierarchicalParams.isSublist("params")) {
130 params = rcp(new Teuchos::ParameterList(hierarchicalParams.sublist("params")));
131 }
132
133 if (hierarchicalParams.isType<std::string>("coefficient map")) {
134 op = rcp(new HOp(nearField, blockKernelApproximations, basisMatrix, transferMatrices, params));
135
136 return op;
137 } else
138 return nearField;
139 }
140 };
141
142 } // namespace MueLu
143
144 #endif