61 const mesh::Topology& topology, std::span<const std::int64_t> nodes_g,
63 md::mdspan<
const std::int32_t, md::dextents<std::size_t, 2>> xdofmap,
65 md::mdspan<
const std::int64_t, md::dextents<std::size_t, 2>> entities,
66 std::span<const T> data)
68 assert(entities.extent(0) == data.size());
70 spdlog::info(
"XDMF distribute entity data");
74 std::vector<int> cell_vertex_dofs;
77 const std::vector<int>& local_index = cmap_dof_layout.
entity_dofs(0, i);
78 assert(local_index.size() == 1);
79 cell_vertex_dofs.push_back(local_index[0]);
84 auto to_vertex_entities
92 const std::vector<int>& entity_layout
94 std::vector<int> entity_vertex_dofs;
95 for (std::size_t i = 0; i < cell_vertex_dofs.size(); ++i)
97 auto it = std::find(entity_layout.begin(), entity_layout.end(),
99 if (it != entity_layout.end())
100 entity_vertex_dofs.push_back(
101 std::ranges::distance(entity_layout.begin(), it));
107 assert(entities.extent(1) == entity_layout.size());
108 std::vector<std::int64_t> entities_v(entities.extent(0) * num_vert_per_e);
109 for (std::size_t e = 0; e < entities.extent(0); ++e)
111 std::span entity(entities_v.data() + e * num_vert_per_e, num_vert_per_e);
112 for (std::size_t i = 0; i < num_vert_per_e; ++i)
113 entity[i] = entities(e, entity_vertex_dofs[i]);
114 std::ranges::sort(entity);
117 std::array shape{entities.extent(0), num_vert_per_e};
118 return std::pair(std::move(entities_v), shape);
120 const auto [entities_v_b, shapev] = to_vertex_entities(
121 cmap_dof_layout, entity_dim, cell_vertex_dofs, cell_type, entities);
123 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entities_v(
124 entities_v_b.data(), shapev);
126 MPI_Comm comm = topology.
comm();
127 MPI_Datatype compound_type;
128 MPI_Type_contiguous(entities_v.extent(1), MPI_INT64_T, &compound_type);
129 MPI_Type_commit(&compound_type);
132 auto send_entities_to_postmaster
133 = [](MPI_Comm comm, MPI_Datatype compound_type, std::int64_t num_nodes_g,
134 auto entities, std::span<const T> data)
139 std::vector<int> dest0;
140 dest0.reserve(entities.extent(0));
141 for (std::size_t e = 0; e < entities.extent(0); ++e)
146 std::vector<int> perm(dest0.size());
147 std::iota(perm.begin(), perm.end(), 0);
148 std::ranges::sort(perm, [&dest0](
auto x0,
auto x1)
149 {
return dest0[x0] < dest0[x1]; });
154 std::vector<int> dest;
155 std::vector<std::int32_t> num_items_send;
157 auto it = perm.begin();
158 while (it != perm.end())
160 dest.push_back(dest0[*it]);
162 = std::find_if(it, perm.end(), [&dest0, r = dest.back()](
auto idx)
163 { return dest0[idx] != r; });
164 num_items_send.push_back(std::ranges::distance(it, it1));
170 std::vector<int> send_disp(num_items_send.size() + 1, 0);
171 std::partial_sum(num_items_send.begin(), num_items_send.end(),
172 std::next(send_disp.begin()));
177 std::ranges::sort(src);
182 int err = MPI_Dist_graph_create_adjacent(
183 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
184 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
188 std::vector<int> num_items_recv(src.size());
189 num_items_send.reserve(1);
190 num_items_recv.reserve(1);
191 MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
192 num_items_recv.data(), 1, MPI_INT, comm0);
196 std::vector<int> recv_disp(num_items_recv.size() + 1, 0);
197 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
198 std::next(recv_disp.begin()));
201 std::vector<std::int64_t> send_buffer;
202 std::vector<T> send_values_buffer;
203 send_buffer.reserve(entities.size());
204 send_values_buffer.reserve(data.size());
205 for (std::size_t e = 0; e < entities.extent(0); ++e)
208 auto it = std::next(entities.data_handle(), idx * entities.extent(1));
209 send_buffer.insert(send_buffer.end(), it, it + entities.extent(1));
210 send_values_buffer.push_back(data[idx]);
213 std::vector<std::int64_t> recv_buffer(recv_disp.back()
214 * entities.extent(1));
215 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
216 send_disp.data(), compound_type,
217 recv_buffer.data(), num_items_recv.data(),
218 recv_disp.data(), compound_type, comm0);
220 std::vector<T> recv_values_buffer(recv_disp.back());
221 err = MPI_Neighbor_alltoallv(
222 send_values_buffer.data(), num_items_send.data(), send_disp.data(),
226 err = MPI_Comm_free(&comm0);
229 std::array shape{recv_buffer.size() / (entities.extent(1)),
230 (entities.extent(1))};
231 return std::tuple<std::vector<std::int64_t>, std::vector<T>,
232 std::array<std::size_t, 2>>(
233 std::move(recv_buffer), std::move(recv_values_buffer), shape);
235 const auto [entitiesp_b, entitiesp_v, shapep] = send_entities_to_postmaster(
236 comm, compound_type, num_nodes_g, entities_v, data);
237 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entitiesp(
238 entitiesp_b.data(), shapep);
241 auto indices_to_postoffice = [](MPI_Comm comm, std::int64_t num_nodes,
242 std::span<const std::int64_t> indices)
245 std::vector<std::pair<int, std::int64_t>> dest_to_index;
246 std::ranges::transform(
247 indices, std::back_inserter(dest_to_index),
248 [size, num_nodes](
auto n)
252 std::ranges::sort(dest_to_index);
256 std::vector<int> dest;
257 std::vector<std::int32_t> num_items_send;
259 auto it = dest_to_index.begin();
260 while (it != dest_to_index.end())
262 dest.push_back(it->first);
264 = std::find_if(it, dest_to_index.end(), [r = dest.back()](
auto idx)
265 { return idx.first != r; });
266 num_items_send.push_back(std::ranges::distance(it, it1));
272 std::vector<int> send_disp(num_items_send.size() + 1, 0);
273 std::partial_sum(num_items_send.begin(), num_items_send.end(),
274 std::next(send_disp.begin()));
279 std::ranges::sort(src);
283 int err = MPI_Dist_graph_create_adjacent(
284 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
285 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
290 std::vector<int> num_items_recv(src.size());
291 num_items_send.reserve(1);
292 num_items_recv.reserve(1);
293 MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
294 num_items_recv.data(), 1, MPI_INT, comm0);
298 std::vector<int> recv_disp(num_items_recv.size() + 1, 0);
299 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
300 std::next(recv_disp.begin()));
303 std::vector<std::int64_t> send_buffer;
304 send_buffer.reserve(indices.size());
305 std::ranges::transform(dest_to_index, std::back_inserter(send_buffer),
306 [](
auto x) {
return x.second; });
308 std::vector<std::int64_t> recv_buffer(recv_disp.back());
309 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
310 send_disp.data(), MPI_INT64_T,
311 recv_buffer.data(), num_items_recv.data(),
312 recv_disp.data(), MPI_INT64_T, comm0);
314 err = MPI_Comm_free(&comm0);
316 return std::tuple(std::move(recv_buffer), std::move(recv_disp),
317 std::move(src), std::move(dest));
319 const auto [nodes_g_p, recv_disp, src, dest]
320 = indices_to_postoffice(comm, num_nodes_g, nodes_g);
324 = [](MPI_Comm comm, MPI_Datatype compound_type,
325 std::span<const std::int64_t> indices_recv,
326 std::span<const int> indices_recv_disp, std::span<const int> src,
327 std::span<const int> dest,
auto entities, std::span<const T> data)
331 std::multimap<std::int64_t, int> node_to_rank;
332 for (std::size_t i = 0; i < indices_recv_disp.size() - 1; ++i)
333 for (
int j = indices_recv_disp[i]; j < indices_recv_disp[i + 1]; ++j)
334 node_to_rank.insert({indices_recv[j], i});
336 std::vector<std::vector<std::int64_t>> send_data(dest.size());
337 std::vector<std::vector<T>> send_values(dest.size());
338 for (std::size_t e = 0; e < entities.extent(0); ++e)
340 std::span e_recv(entities.data_handle() + e * entities.extent(1),
342 auto [it0, it1] = node_to_rank.equal_range(entities(e, 0));
343 for (
auto it = it0; it != it1; ++it)
346 send_data[p].insert(send_data[p].end(), e_recv.begin(), e_recv.end());
347 send_values[p].push_back(data[e]);
352 int err = MPI_Dist_graph_create_adjacent(
353 comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(), dest.data(),
354 MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
357 std::vector<int> num_items_send;
358 num_items_send.reserve(send_data.size());
359 for (
auto& x : send_data)
360 num_items_send.push_back(x.size() / entities.extent(1));
362 std::vector<int> num_items_recv(src.size());
363 num_items_send.reserve(1);
364 num_items_recv.reserve(1);
365 err = MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
366 num_items_recv.data(), 1, MPI_INT, comm0);
370 std::vector<std::int32_t> send_disp(num_items_send.size() + 1, 0);
371 std::partial_sum(num_items_send.begin(), num_items_send.end(),
372 std::next(send_disp.begin()));
375 std::vector<std::int32_t> recv_disp(num_items_recv.size() + 1, 0);
376 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
377 std::next(recv_disp.begin()));
380 std::vector<std::int64_t> send_buffer;
381 std::vector<T> send_values_buffer;
382 for (
auto& x : send_data)
383 send_buffer.insert(send_buffer.end(), x.begin(), x.end());
384 for (
auto& v : send_values)
385 send_values_buffer.insert(send_values_buffer.end(), v.begin(), v.end());
386 std::vector<std::int64_t> recv_buffer(entities.extent(1)
388 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
389 send_disp.data(), compound_type,
390 recv_buffer.data(), num_items_recv.data(),
391 recv_disp.data(), compound_type, comm0);
395 std::vector<T> recv_values_buffer(recv_disp.back());
396 err = MPI_Neighbor_alltoallv(
397 send_values_buffer.data(), num_items_send.data(), send_disp.data(),
403 err = MPI_Comm_free(&comm0);
406 std::array shape{recv_buffer.size() / entities.extent(1),
408 return std::tuple<std::vector<std::int64_t>, std::vector<T>,
409 std::array<std::size_t, 2>>(
410 std::move(recv_buffer), std::move(recv_values_buffer), shape);
414 const auto [entities_data_b, entities_values, shape_eb]
415 = candidate_ranks(comm, compound_type, nodes_g_p, recv_disp, dest, src,
416 entitiesp, std::span(entitiesp_v));
417 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entities_data(
418 entities_data_b.data(), shape_eb);
429 std::span<const std::int64_t> nodes_g,
430 std::span<const int> cell_vertex_dofs,
auto entities_data,
431 std::span<const T> entities_values)
433 spdlog::info(
"XDMF build map");
436 throw std::runtime_error(
"Missing cell-vertex connectivity.");
438 std::map<std::int64_t, std::int32_t> input_idx_to_vertex;
439 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
441 auto vertices = c_to_v->links(c);
442 std::span xdofs(xdofmap.data_handle() + c * xdofmap.extent(1),
444 for (std::size_t v = 0; v < vertices.size(); ++v)
445 input_idx_to_vertex[nodes_g[xdofs[cell_vertex_dofs[v]]]] = vertices[v];
448 std::vector<std::int32_t> entities;
450 std::vector<std::int32_t> entity(entities_data.extent(1));
451 for (std::size_t e = 0; e < entities_data.extent(0); ++e)
453 bool entity_found =
true;
454 for (std::size_t i = 0; i < entities_data.extent(1); ++i)
456 if (
auto it = input_idx_to_vertex.find(entities_data(e, i));
457 it == input_idx_to_vertex.end())
461 entity_found =
false;
465 entity[i] = it->second;
470 entities.insert(entities.end(), entity.begin(), entity.end());
471 data.push_back(entities_values[e]);
475 return std::pair(std::move(entities), std::move(data));
478 MPI_Type_free(&compound_type);
480 return select_entities(topology, xdofmap, nodes_g, cell_vertex_dofs,
481 entities_data, std::span(entities_values));