Chi-Tech
pwld_02_ordering.cc
Go to the documentation of this file.
2
4
5#include "chi_log.h"
6#include "chi_mpi.h"
7
8#include "utils/chi_timer.h"
9
10#include "chi_mpi_utils.h"
11
13{
14
15// ###################################################################
16/**Reorders the nodes for parallel computation in a Continuous
17 * Finite Element calculation.*/
19{
20 const std::string fname = __FUNCTION__;
21 chi::Timer t_stage[6];
22
23 t_stage[0].Reset();
24 //================================================== Check cell views avail
25 size_t num_loc_cells = ref_grid_.local_cells.size();
26
27 //================================================== Get local DOF count
28 // and set
29 // cell_local_block_address
30 cell_local_block_address_.resize(num_loc_cells, 0);
31
32 uint64_t local_node_count = 0;
33 for (const auto& cell : ref_grid_.local_cells)
34 {
35 const auto& cell_mapping = GetCellMapping(cell);
36 cell_local_block_address_[cell.local_id_] =
37 static_cast<int64_t>(local_node_count);
38 local_node_count += cell_mapping.NumNodes();
39 }
40
41 //================================================== Allgather node_counts
42 locJ_block_size_.assign(Chi::mpi.process_count, 0);
43 MPI_Allgather(&local_node_count, // sendbuf
44 1,
45 MPI_UNSIGNED_LONG_LONG, // sendcount, sendtype
46 locJ_block_size_.data(), // recvbuf
47 1,
48 MPI_UNSIGNED_LONG_LONG, // recvcount, recvtype
49 Chi::mpi.comm); // comm
50
51 //================================================== Assign
52 // local_block_address
53 uint64_t running_block_address = 0;
54 for (int locI = 0; locI < Chi::mpi.process_count; ++locI)
55 {
56 if (locI == Chi::mpi.location_id)
57 local_block_address_ = static_cast<int64_t>(running_block_address);
58
59 running_block_address += locJ_block_size_[locI];
60 }
61 const uint64_t global_node_count = running_block_address;
62
63 local_base_block_size_ = local_node_count;
64 globl_base_block_size_ = global_node_count;
65
66 //================================================== Collect ghost cell ids
67 // needing block addresses
68 std::map<int, std::vector<uint64_t>> ghost_cell_ids_consolidated;
69
70 for (uint64_t global_id : ref_grid_.cells.GetGhostGlobalIDs())
71 {
72 const auto& cell = ref_grid_.cells[global_id];
73 const int locI = static_cast<int>(cell.partition_id_);
74
75 std::vector<uint64_t>& locI_cell_id_list =
76 ghost_cell_ids_consolidated[locI];
77
78 locI_cell_id_list.push_back(cell.global_id_);
79 }
80
81 //================================================== AllToAll to get query
82 // cell-ids
83 const std::map<int, std::vector<uint64_t>> query_ghost_cell_ids_consolidated =
84 chi_mpi_utils::MapAllToAll(ghost_cell_ids_consolidated,
85 MPI_UNSIGNED_LONG_LONG);
86
87 //================================================== Map all query cell-ids
88 std::map<int, std::vector<uint64_t>> mapped_ghost_cell_ids_consolidated;
89 for (const auto& [pid, cell_id_list] : query_ghost_cell_ids_consolidated)
90 {
91 std::vector<uint64_t>& map_list = mapped_ghost_cell_ids_consolidated[pid];
92
93 for (uint64_t cell_global_id : cell_id_list)
94 {
95 const auto& cell = ref_grid_.cells[cell_global_id];
96
97 const uint64_t cell_block_address =
99 map_list.push_back(cell_block_address);
100 }
101 }
102
103 //================================================== Communicate back the
104 // mapping
105 const std::map<int, std::vector<uint64_t>> global_id_mapping =
106 chi_mpi_utils::MapAllToAll(mapped_ghost_cell_ids_consolidated,
107 MPI_UNSIGNED_LONG_LONG);
108
109 //================================================== Process global id mapping
110 for (const auto& [pid, mapping_list] : global_id_mapping)
111 {
112 const auto& global_id_list = ghost_cell_ids_consolidated.at(pid);
113
114 if (mapping_list.size() != global_id_list.size())
115 throw std::logic_error(fname + ": Ghost cell mapping error.");
116
117 const size_t list_size = mapping_list.size();
118 for (size_t k = 0; k < list_size; ++k)
119 neighbor_cell_block_address_.emplace_back(
120 global_id_list[k], static_cast<int64_t>(mapping_list[k]));
121 }
122
123 //================================================== Print info
125 << "Local dof count, start, total " << local_node_count << " "
126 << local_block_address_ << " " << global_node_count;
127}
128
129} // namespace chi_math::spatial_discretization
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllVerbose2()
Definition: chi_log.h:242
const MPI_Comm & comm
MPI communicator.
Definition: mpi_info.h:28
const int & process_count
Total number of processes.
Definition: mpi_info.h:27
void Reset()
Definition: chi_timer.cc:16
const CellMapping & GetCellMapping(const chi_mesh::Cell &cell) const
std::vector< uint64_t > locJ_block_size_
const chi_mesh::MeshContinuum & ref_grid_
std::vector< std::pair< uint64_t, int64_t > > neighbor_cell_block_address_
std::vector< uint64_t > GetGhostGlobalIDs() const
void push_back(std::unique_ptr< chi_mesh::Cell > new_cell)
LocalCellHandler local_cells
GlobalCellHandler cells
std::map< K, std::vector< T > > MapAllToAll(const std::map< K, std::vector< T > > &pid_data_pairs, const MPI_Datatype data_mpi_type, const MPI_Comm communicator=Chi::mpi.comm)