-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
278 lines (245 loc) · 8.78 KB
/
Copy pathmain.cpp
File metadata and controls
278 lines (245 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (C) 2017-2022 Chris N. Richardson and Garth N. Wells
//
// This file is part of FEniCS-miniapp (https://www.fenicsproject.org)
//
// SPDX-License-Identifier: MIT
#include "cgpoisson_problem.h"
#include "elasticity_problem.h"
#include "mem.h"
#include "mesh.h"
#include "poisson_problem.h"
#include <boost/program_options.hpp>
#include <dolfinx/common/Timer.h>
#include <dolfinx/common/log.h>
#include <dolfinx/common/timing.h>
#include <dolfinx/common/version.h>
#include <dolfinx/fem/Form.h>
#include <dolfinx/fem/Function.h>
#include <dolfinx/fem/FunctionSpace.h>
#include <dolfinx/fem/utils.h>
#include <dolfinx/io/XDMFFile.h>
#include <dolfinx/la/Vector.h>
#include <iomanip>
#include <petscsys.h>
#include <string>
#include <thread>
#include <utility>
namespace po = boost::program_options;
std::string int64_to_human(std::int64_t n)
{
double r = static_cast<double>(n);
const std::string name[] = {"", "thousand", "million", "billion", "trillion"};
int i = 0;
while (r > 1000.0)
{
r /= 1000.0;
i++;
}
if (i > 4)
throw std::runtime_error("number too big");
std::stringstream s;
if (i == 0)
return s.str();
s << " (" << std::setprecision(3) << r << " " << name[i] << ")";
return s.str();
}
void solve(int argc, char* argv[])
{
po::options_description desc("Allowed options");
bool mem_profile;
bool use_subcomm;
desc.add_options()("help,h", "print usage message")(
"problem_type", po::value<std::string>()->default_value("poisson"),
"problem (poisson, cgpoisson, or elasticity)")(
"mesh_type", po::value<std::string>()->default_value("cube"),
"mesh (cube or unstructured)")(
"memory_profiling", po::bool_switch(&mem_profile)->default_value(false),
"turn on memory logging")(
"subcomm_partition", po::bool_switch(&use_subcomm)->default_value(false),
"Use sub-communicator for partitioning")(
"scaling_type", po::value<std::string>()->default_value("weak"),
"scaling (weak or strong)")(
"output", po::value<std::string>()->default_value(""),
"output directory (no output unless this is set)")(
"ndofs", po::value<std::size_t>()->default_value(50000),
"number of degrees of freedom")(
"order", po::value<std::size_t>()->default_value(1), "polynomial order")(
"scatterer", po::value<std::string>()->default_value("neighbor"),
"scatterer for CG (neighbor or p2p)");
po::variables_map vm;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.allow_unregistered()
.run(),
vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
;
return;
}
const std::string problem_type = vm["problem_type"].as<std::string>();
const std::string mesh_type = vm["mesh_type"].as<std::string>();
const std::string scaling_type = vm["scaling_type"].as<std::string>();
const std::size_t ndofs = vm["ndofs"].as<std::size_t>();
const int order = vm["order"].as<std::size_t>();
const std::string scatterer = vm["scatterer"].as<std::string>();
const std::string output_dir = vm["output"].as<std::string>();
const bool output = (output_dir.size() > 0);
const int mpi_rank = dolfinx::MPI::rank(MPI_COMM_WORLD);
bool quit_flag = false;
std::thread mem_thread;
if (mem_profile and mpi_rank == 0)
{
mem_thread = std::thread(process_mem_usage, std::ref(quit_flag));
}
bool strong_scaling;
if (scaling_type == "strong")
strong_scaling = true;
else if (scaling_type == "weak")
strong_scaling = false;
else
throw std::runtime_error("Scaling type '" + scaling_type + "` unknown");
// Get number of processes
const std::size_t num_processes = dolfinx::MPI::size(MPI_COMM_WORLD);
// Assemble problem
std::shared_ptr<dolfinx::mesh::Mesh<double>> mesh;
std::shared_ptr<dolfinx::la::Vector<PetscScalar>> b;
std::shared_ptr<dolfinx::fem::Function<PetscScalar>> u;
std::function<int(dolfinx::fem::Function<PetscScalar>&,
const dolfinx::la::Vector<PetscScalar>&)>
solver_function;
const int ndofs_per_node = (problem_type == "elasticity") ? 3 : 1;
dolfinx::common::Timer t0("ZZZ Create Mesh");
if (mesh_type == "cube")
{
mesh = std::make_shared<dolfinx::mesh::Mesh<double>>(
create_cube_mesh(MPI_COMM_WORLD, ndofs, strong_scaling, ndofs_per_node,
order, use_subcomm));
}
else
{
mesh = create_spoke_mesh(MPI_COMM_WORLD, ndofs, strong_scaling,
ndofs_per_node);
}
t0.stop();
t0.flush();
dolfinx::common::Timer t_ent(
"ZZZ Create facets and facet->cell connectivity");
mesh->topology_mutable()->create_entities(2);
mesh->topology_mutable()->create_connectivity(2, 3);
t_ent.stop();
t_ent.flush();
if (problem_type == "poisson")
{
// Create Poisson problem
std::tie(b, u, solver_function) = poisson::problem(mesh, order);
}
else if (problem_type == "cgpoisson")
{
// Create Poisson problem
std::tie(b, u, solver_function)
= cgpoisson::problem(mesh, order, scatterer);
}
else if (problem_type == "elasticity")
{
// Create elasticity problem. Near-nullspace will be attached to the
// linear operator (matrix).
std::tie(b, u, solver_function) = elastic::problem(mesh, order);
}
else
throw std::runtime_error("Unknown problem type: " + problem_type);
// Print simulation summary
if (dolfinx::MPI::rank(MPI_COMM_WORLD) == 0)
{
char petsc_version[256];
PetscGetVersion(petsc_version, 256);
const std::int64_t num_dofs
= u->function_space()->dofmap()->index_map->size_global()
* u->function_space()->dofmap()->index_map_bs();
const int tdim = mesh->topology()->dim();
const std::int64_t num_cells
= mesh->topology()->index_map(tdim)->size_global();
const std::string num_cells_human = int64_to_human(num_cells);
const std::string num_dofs_human = int64_to_human(num_dofs);
std::cout
<< "----------------------------------------------------------------"
<< std::endl;
std::cout << "Test problem summary" << std::endl;
std::cout << " dolfinx version: " << DOLFINX_VERSION_STRING << std::endl;
std::cout << " dolfinx hash: " << DOLFINX_VERSION_GIT << std::endl;
std::cout << " ufl hash: " << UFCX_SIGNATURE << std::endl;
std::cout << " petsc version: " << petsc_version << std::endl;
std::cout << " Problem type: " << problem_type << std::endl;
std::cout << " Scaling type: " << scaling_type << std::endl;
std::cout << " Num processes: " << num_processes << std::endl;
std::cout << " Num cells: " << num_cells << num_cells_human
<< std::endl;
std::cout << " Total degrees of freedom: " << num_dofs
<< num_dofs_human << std::endl;
std::cout << " Average degrees of freedom per process: "
<< num_dofs / dolfinx::MPI::size(MPI_COMM_WORLD) << std::endl;
std::cout
<< "----------------------------------------------------------------"
<< std::endl;
}
dolfinx::common::Timer t5("ZZZ Solve");
int num_iter = solver_function(*u, *b);
t5.stop();
t5.flush();
if (output)
{
dolfinx::common::Timer t6("ZZZ Output");
std::string filename
= output_dir + "/solution-" + std::to_string(num_processes) + ".xdmf";
dolfinx::io::XDMFFile file(MPI_COMM_WORLD, filename, "w");
file.write_mesh(*mesh);
file.write_function(*u, 0.0);
t6.stop();
t6.flush();
}
// Display timings
dolfinx::list_timings(MPI_COMM_WORLD);
// Report number of Krylov iterations
double norm = dolfinx::la::norm(*(u->x()));
if (dolfinx::MPI::rank(MPI_COMM_WORLD) == 0)
{
std::cout << "*** Number of Krylov iterations: " << num_iter << std::endl;
std::cout << "*** Solution norm: " << norm << std::endl;
}
if (mem_profile and mpi_rank == 0)
{
quit_flag = true;
mem_thread.join();
}
}
int main(int argc, char* argv[])
{
dolfinx::common::Timer t0("Init MPI");
MPI_Init(&argc, &argv);
t0.stop();
t0.flush();
dolfinx::common::Timer t1("Init logging");
dolfinx::init_logging(argc, argv);
t1.stop();
t1.flush();
dolfinx::common::Timer t2("Init PETSc");
PetscInitialize(&argc, &argv, nullptr, nullptr);
t2.stop();
t2.flush();
// Set the logging thread name to show the process rank and enable on
// rank 0 (add more here if desired)
const int mpi_rank = dolfinx::MPI::rank(MPI_COMM_WORLD);
std::string thread_name = "RANK: " + std::to_string(mpi_rank);
std::string fmt = "[%Y-%m-%d %H:%M:%S.%e] [" + thread_name + "] [%l] %v";
spdlog::set_pattern(fmt);
// Turn off logging except on rank 0, if set (e.g. use SPDLOG_LEVEL=info in
// CLI)
if (mpi_rank != 0)
spdlog::set_level(spdlog::level::err);
solve(argc, argv);
PetscFinalize();
MPI_Finalize();
return 0;
}