64 const mesh::Topology& topology, std::span<const std::int64_t> nodes_g,
66 md::mdspan<
const std::int32_t, md::dextents<std::size_t, 2>> xdofmap,
68 md::mdspan<
const std::int64_t, md::dextents<std::size_t, 2>> entities,
69 std::span<const T> data)
71 assert(entities.extent(0) == data.size());
73 logger().info(
"XDMF distribute entity data");
77 std::vector<int> cell_vertex_dofs;
80 const std::vector<int>& local_index = cmap_dof_layout.
entity_dofs(0, i);
81 assert(local_index.size() == 1);
82 cell_vertex_dofs.push_back(local_index[0]);
87 auto to_vertex_entities
95 std::vector<int> entity_vertex_dofs;
96 for (std::size_t i = 0; i < vertex_dofs.size(); ++i)
98 auto it = std::find(entity_layout.begin(), entity_layout.end(),
100 if (it != entity_layout.end())
102 entity_vertex_dofs.push_back(
103 std::ranges::distance(entity_layout.begin(), it));
107 const std::size_t num_vert_per_e
110 assert(ents.extent(1) == entity_layout.size());
111 std::vector<std::int64_t> entities_v(ents.extent(0) * num_vert_per_e);
112 for (std::size_t e = 0; e < ents.extent(0); ++e)
114 std::span entity(entities_v.data() + e * num_vert_per_e, num_vert_per_e);
115 for (std::size_t i = 0; i < num_vert_per_e; ++i)
116 entity[i] = ents(e, entity_vertex_dofs[i]);
117 std::ranges::sort(entity);
120 std::array shape{ents.extent(0), num_vert_per_e};
121 return std::pair(std::move(entities_v), shape);
123 const auto [entities_v_b, shapev] = to_vertex_entities(
124 cmap_dof_layout, entity_dim, cell_vertex_dofs, cell_type, entities);
126 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entities_v(
127 entities_v_b.data(), shapev);
129 MPI_Comm comm = topology.
comm();
130 MPI_Datatype compound_type;
131 MPI_Type_contiguous(entities_v.extent(1), MPI_INT64_T, &compound_type);
132 MPI_Type_commit(&compound_type);
135 auto send_entities_to_postmaster
136 = [](MPI_Comm pm_comm, MPI_Datatype pm_compound_type,
137 std::int64_t pm_num_nodes_g,
auto pm_entities,
138 std::span<const T> pm_data)
143 std::vector<int> dest0;
144 dest0.reserve(pm_entities.extent(0));
145 for (std::size_t e = 0; e < pm_entities.extent(0); ++e)
150 std::vector<int> perm(dest0.size());
151 std::iota(perm.begin(), perm.end(), 0);
152 std::ranges::sort(perm, [&dest0](
auto x0,
auto x1)
153 {
return dest0[x0] < dest0[x1]; });
158 std::vector<int> dest;
159 std::vector<std::int32_t> num_items_send;
161 auto it = perm.begin();
162 while (it != perm.end())
164 dest.push_back(dest0[*it]);
166 = std::find_if(it, perm.end(), [&dest0, r = dest.back()](
auto idx)
167 { return dest0[idx] != r; });
168 num_items_send.push_back(std::ranges::distance(it, it1));
174 std::vector<int> send_disp(num_items_send.size() + 1, 0);
175 std::partial_sum(num_items_send.begin(), num_items_send.end(),
176 std::next(send_disp.begin()));
181 std::ranges::sort(src);
186 int err = MPI_Dist_graph_create_adjacent(
187 pm_comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(),
188 dest.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
192 std::vector<int> num_items_recv(src.size());
193 num_items_send.reserve(1);
194 num_items_recv.reserve(1);
195 MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
196 num_items_recv.data(), 1, MPI_INT, comm0);
200 std::vector<int> recv_disp(num_items_recv.size() + 1, 0);
201 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
202 std::next(recv_disp.begin()));
205 std::vector<std::int64_t> send_buffer;
206 std::vector<T> send_values_buffer;
207 send_buffer.reserve(pm_entities.size());
208 send_values_buffer.reserve(pm_data.size());
209 for (std::size_t e = 0; e < pm_entities.extent(0); ++e)
213 = std::next(pm_entities.data_handle(), idx * pm_entities.extent(1));
214 send_buffer.insert(send_buffer.end(), it, it + pm_entities.extent(1));
215 send_values_buffer.push_back(pm_data[idx]);
218 std::vector<std::int64_t> recv_buffer(recv_disp.back()
219 * pm_entities.extent(1));
220 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
221 send_disp.data(), pm_compound_type,
222 recv_buffer.data(), num_items_recv.data(),
223 recv_disp.data(), pm_compound_type, comm0);
225 std::vector<T> recv_values_buffer(recv_disp.back());
226 err = MPI_Neighbor_alltoallv(
227 send_values_buffer.data(), num_items_send.data(), send_disp.data(),
231 err = MPI_Comm_free(&comm0);
234 std::array shape{recv_buffer.size() / (pm_entities.extent(1)),
235 (pm_entities.extent(1))};
236 return std::tuple<std::vector<std::int64_t>, std::vector<T>,
237 std::array<std::size_t, 2>>(
238 std::move(recv_buffer), std::move(recv_values_buffer), shape);
240 const auto [entitiesp_b, entitiesp_v, shapep] = send_entities_to_postmaster(
241 comm, compound_type, num_nodes_g, entities_v, data);
242 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entitiesp(
243 entitiesp_b.data(), shapep);
246 auto indices_to_postoffice = [](MPI_Comm po_comm, std::int64_t num_nodes,
247 std::span<const std::int64_t> indices)
250 std::vector<std::pair<int, std::int64_t>> dest_to_index;
251 std::ranges::transform(
252 indices, std::back_inserter(dest_to_index),
253 [size, num_nodes](
auto n)
257 std::ranges::sort(dest_to_index);
261 std::vector<int> dest;
262 std::vector<std::int32_t> num_items_send;
264 auto it = dest_to_index.begin();
265 while (it != dest_to_index.end())
267 dest.push_back(it->first);
269 = std::find_if(it, dest_to_index.end(), [r = dest.back()](
auto idx)
270 { return idx.first != r; });
271 num_items_send.push_back(std::ranges::distance(it, it1));
277 std::vector<int> send_disp(num_items_send.size() + 1, 0);
278 std::partial_sum(num_items_send.begin(), num_items_send.end(),
279 std::next(send_disp.begin()));
284 std::ranges::sort(src);
288 int err = MPI_Dist_graph_create_adjacent(
289 po_comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(),
290 dest.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
295 std::vector<int> num_items_recv(src.size());
296 num_items_send.reserve(1);
297 num_items_recv.reserve(1);
298 MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
299 num_items_recv.data(), 1, MPI_INT, comm0);
303 std::vector<int> recv_disp(num_items_recv.size() + 1, 0);
304 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
305 std::next(recv_disp.begin()));
308 std::vector<std::int64_t> send_buffer;
309 send_buffer.reserve(indices.size());
310 std::ranges::transform(dest_to_index, std::back_inserter(send_buffer),
311 [](
auto x) {
return x.second; });
313 std::vector<std::int64_t> recv_buffer(recv_disp.back());
314 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
315 send_disp.data(), MPI_INT64_T,
316 recv_buffer.data(), num_items_recv.data(),
317 recv_disp.data(), MPI_INT64_T, comm0);
319 err = MPI_Comm_free(&comm0);
321 return std::tuple(std::move(recv_buffer), std::move(recv_disp),
322 std::move(src), std::move(dest));
324 const auto [nodes_g_p, nodes_g_p_disp, post_src, post_dest]
325 = indices_to_postoffice(comm, num_nodes_g, nodes_g);
329 = [](MPI_Comm cr_comm, MPI_Datatype cr_compound_type,
330 std::span<const std::int64_t> indices_recv,
331 std::span<const int> indices_recv_disp, std::span<const int> src,
332 std::span<const int> dest,
auto entities, std::span<const T> cr_data)
336 std::multimap<std::int64_t, int> node_to_rank;
337 for (std::size_t i = 0; i < indices_recv_disp.size() - 1; ++i)
338 for (
int j = indices_recv_disp[i]; j < indices_recv_disp[i + 1]; ++j)
339 node_to_rank.insert({indices_recv[j], i});
341 std::vector<std::vector<std::int64_t>> send_data(dest.size());
342 std::vector<std::vector<T>> send_values(dest.size());
343 for (std::size_t e = 0; e < entities.extent(0); ++e)
345 std::span e_recv(entities.data_handle() + e * entities.extent(1),
347 auto [it0, it1] = node_to_rank.equal_range(entities(e, 0));
348 for (
auto it = it0; it != it1; ++it)
351 send_data[p].insert(send_data[p].end(), e_recv.begin(), e_recv.end());
352 send_values[p].push_back(cr_data[e]);
357 int err = MPI_Dist_graph_create_adjacent(
358 cr_comm, src.size(), src.data(), MPI_UNWEIGHTED, dest.size(),
359 dest.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
362 std::vector<int> num_items_send;
363 num_items_send.reserve(send_data.size());
364 for (
auto& x : send_data)
365 num_items_send.push_back(x.size() / entities.extent(1));
367 std::vector<int> num_items_recv(src.size());
368 num_items_send.reserve(1);
369 num_items_recv.reserve(1);
370 err = MPI_Neighbor_alltoall(num_items_send.data(), 1, MPI_INT,
371 num_items_recv.data(), 1, MPI_INT, comm0);
375 std::vector<std::int32_t> send_disp(num_items_send.size() + 1, 0);
376 std::partial_sum(num_items_send.begin(), num_items_send.end(),
377 std::next(send_disp.begin()));
380 std::vector<std::int32_t> recv_disp(num_items_recv.size() + 1, 0);
381 std::partial_sum(num_items_recv.begin(), num_items_recv.end(),
382 std::next(recv_disp.begin()));
385 std::vector<std::int64_t> send_buffer;
386 std::vector<T> send_values_buffer;
387 for (
auto& x : send_data)
388 send_buffer.insert(send_buffer.end(), x.begin(), x.end());
389 for (
auto& v : send_values)
390 send_values_buffer.insert(send_values_buffer.end(), v.begin(), v.end());
391 std::vector<std::int64_t> recv_buffer(entities.extent(1)
393 err = MPI_Neighbor_alltoallv(send_buffer.data(), num_items_send.data(),
394 send_disp.data(), cr_compound_type,
395 recv_buffer.data(), num_items_recv.data(),
396 recv_disp.data(), cr_compound_type, comm0);
400 std::vector<T> recv_values_buffer(recv_disp.back());
401 err = MPI_Neighbor_alltoallv(
402 send_values_buffer.data(), num_items_send.data(), send_disp.data(),
408 err = MPI_Comm_free(&comm0);
411 std::array shape{recv_buffer.size() / entities.extent(1),
413 return std::tuple<std::vector<std::int64_t>, std::vector<T>,
414 std::array<std::size_t, 2>>(
415 std::move(recv_buffer), std::move(recv_values_buffer), shape);
419 const auto [entities_data_b, entities_values, shape_eb]
420 = candidate_ranks(comm, compound_type, nodes_g_p, nodes_g_p_disp,
421 post_dest, post_src, entitiesp, std::span(entitiesp_v));
422 md::mdspan<const std::int64_t, md::dextents<std::size_t, 2>> entities_data(
423 entities_data_b.data(), shape_eb);
432 auto select_entities = [](
const mesh::Topology& topo,
auto xdofmap,
433 std::span<const std::int64_t> nodes,
434 std::span<const int> cell_vertex_dofs,
435 auto entities_data, std::span<const T> values)
437 logger().info(
"XDMF build map");
440 throw std::runtime_error(
"Missing cell-vertex connectivity.");
443 boost::unordered_flat_map<std::int64_t, std::int32_t> input_idx_to_vertex;
444 input_idx_to_vertex.reserve(c_to_v->num_nodes() * cell_vertex_dofs.size());
445 for (
int c = 0; c < c_to_v->num_nodes(); ++c)
447 auto vertices = c_to_v->links(c);
448 std::span xdofs(xdofmap.data_handle() + c * xdofmap.extent(1),
450 for (std::size_t v = 0; v < vertices.size(); ++v)
451 input_idx_to_vertex[nodes[xdofs[cell_vertex_dofs[v]]]] = vertices[v];
454 std::vector<std::int32_t> local_entities;
455 std::vector<T> local_data;
456 local_entities.reserve(entities_data.extent(0) * entities_data.extent(1));
457 local_data.reserve(entities_data.extent(0));
458 std::vector<std::int32_t> entity(entities_data.extent(1));
459 for (std::size_t e = 0; e < entities_data.extent(0); ++e)
461 bool entity_found =
true;
462 for (std::size_t i = 0; i < entities_data.extent(1); ++i)
464 if (
auto it = input_idx_to_vertex.find(entities_data(e, i));
465 it == input_idx_to_vertex.end())
469 entity_found =
false;
473 entity[i] = it->second;
478 local_entities.insert(local_entities.end(), entity.begin(),
480 local_data.push_back(values[e]);
484 return std::pair(std::move(local_entities), std::move(local_data));
487 MPI_Type_free(&compound_type);
489 return select_entities(topology, xdofmap, nodes_g, cell_vertex_dofs,
490 entities_data, std::span(entities_values));