Chi-Tech
CBC_SPDS.cc
Go to the documentation of this file.
1#include "CBC_SPDS.h"
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8
10#include "utils/chi_timer.h"
11
12namespace lbs
13{
14
16 const chi_mesh::MeshContinuum& grid,
17 bool cycle_allowance_flag,
18 bool verbose)
19 : SPDS(omega, grid, verbose)
20{
22 << " Building sweep ordering for Omega = "
23 << omega.PrintS();
24
25 size_t num_loc_cells = grid.local_cells.size();
26
27 //============================================= Populate Cell Relationships
28 Chi::log.Log0Verbose1() << "Populating cell relationships";
29 std::vector<std::set<std::pair<int, double>>> cell_successors(num_loc_cells);
30 std::set<int> location_successors;
31 std::set<int> location_dependencies;
32
34 omega, location_dependencies, location_successors, cell_successors);
35
36 location_successors_.reserve(location_successors.size());
37 location_dependencies_.reserve(location_dependencies.size());
38
39 for (auto v : location_successors)
40 location_successors_.push_back(v);
41
42 for (auto v : location_dependencies)
43 location_dependencies_.push_back(v);
44
45 //============================================= Build graph
46 chi::DirectedGraph local_DG;
47
48 // Add vertex for each local cell
49 for (int c = 0; c < num_loc_cells; ++c)
50 local_DG.AddVertex();
51
52 // Create graph edges
53 for (int c = 0; c < num_loc_cells; c++)
54 for (auto& successor : cell_successors[c])
55 local_DG.AddEdge(c, successor.first, successor.second);
56
57 //============================================= Remove local cycles if allowed
59
60 if (cycle_allowance_flag)
61 {
63 << Chi::program_timer.GetTimeString() << " Removing inter-cell cycles.";
64
65 auto edges_to_remove = local_DG.RemoveCyclicDependencies();
66
67 for (auto& edge_to_remove : edges_to_remove)
68 {
69 local_cyclic_dependencies_.emplace_back(edge_to_remove.first,
70 edge_to_remove.second);
71 }
72 }
73
74 //============================================= Generate topological sorting
77 << " Generating topological sorting for local sweep ordering";
78 auto so_temp = local_DG.GenerateTopologicalSort();
79 spls_.item_id.clear();
80 for (auto v : so_temp)
81 spls_.item_id.emplace_back(v);
82
83 if (spls_.item_id.empty())
84 {
86 << "Topological sorting for local sweep-ordering failed. "
87 << "Cyclic dependencies detected. Cycles need to be allowed"
88 << " by calling application.";
89 Chi::Exit(EXIT_FAILURE);
90 }
91
92 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Create Task
93 // Dependency Graphs
94 // All locations will gather other locations' dependencies
95 // so that each location has the ability to build
96 // the global task graph.
97
99 << " Communicating sweep dependencies.";
100
101 // auto& global_dependencies = sweep_order->global_dependencies;
102 std::vector<std::vector<int>> global_dependencies;
103 global_dependencies.resize(Chi::mpi.process_count);
104
106 location_dependencies_, global_dependencies);
107
108 ////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Build task
109 //// dependency graph
110 // BuildTaskDependencyGraph(global_dependencies, cycle_allowance_flag);
111
112 constexpr auto INCOMING =
114 constexpr auto OUTGOING =
116
117 // For each local cell create a task
118 for (const auto& cell : grid_.local_cells)
119 {
120 const size_t num_faces = cell.faces_.size();
121 unsigned int num_dependencies = 0;
122 std::vector<uint64_t> succesors;
123
124 for (size_t f = 0; f < num_faces; ++f)
125 if (cell_face_orientations_[cell.local_id_][f] == INCOMING)
126 {
127 if (cell.faces_[f].has_neighbor_)
128 ++num_dependencies;
129
130 }
131 else if (cell_face_orientations_[cell.local_id_][f] == OUTGOING)
132 {
133 const auto& face = cell.faces_[f];
134 if (face.has_neighbor_ and grid.IsCellLocal(face.neighbor_id_))
135 succesors.push_back(grid.cells[face.neighbor_id_].local_id_);
136 }
137
138 task_list_.push_back({num_dependencies,
139 succesors,
140 /*reference_id_=*/cell.local_id_,
141 /*cell_ptr_=*/&cell,
142 /*completed_=*/false});
143 } // for cell in SPLS
144
146
148 << " Done computing sweep ordering.\n\n";
149}
150
151const std::vector<chi_mesh::sweep_management::Task>& CBC_SPDS::TaskList() const
152{
153 return task_list_;
154}
155
156} // namespace lbs
static chi::Timer program_timer
Definition: chi_runtime.h:79
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllError()
Definition: chi_log.h:239
LogStream Log0Verbose1()
Definition: chi_log.h:234
std::vector< size_t > GenerateTopologicalSort()
std::vector< std::pair< size_t, size_t > > RemoveCyclicDependencies()
void AddVertex(size_t id, void *context=nullptr)
bool AddEdge(size_t from, size_t to, double weight=1.0)
void Barrier() const
Definition: mpi_info.cc:38
std::string GetTimeString() const
Definition: chi_timer.cc:38
LocalCellHandler local_cells
GlobalCellHandler cells
bool IsCellLocal(uint64_t cell_global_index) const
std::vector< int > location_dependencies_
Definition: SPDS.h:63
const chi_mesh::MeshContinuum & grid_
Definition: SPDS.h:59
std::vector< int > location_successors_
Definition: SPDS.h:64
std::vector< std::pair< int, int > > local_cyclic_dependencies_
Definition: SPDS.h:68
void PrintedGhostedGraph() const
Definition: SPDS.cc:179
std::vector< std::vector< FaceOrientation > > cell_face_orientations_
Definition: SPDS.h:70
void PopulateCellRelationships(const chi_mesh::Vector3 &omega, std::set< int > &location_dependencies, std::set< int > &location_successors, std::vector< std::set< std::pair< int, double > > > &cell_successors)
Definition: SPDS.cc:51
const std::vector< chi_mesh::sweep_management::Task > & TaskList() const
Definition: CBC_SPDS.cc:151
std::vector< chi_mesh::sweep_management::Task > task_list_
Definition: CBC_SPDS.h:23
CBC_SPDS(const chi_mesh::Vector3 &omega, const chi_mesh::MeshContinuum &grid, bool cycle_allowance_flag, bool verbose)
Definition: CBC_SPDS.cc:15
void CommunicateLocationDependencies(const std::vector< int > &location_dependencies, std::vector< std::vector< int > > &global_dependencies)
std::string PrintS() const
std::vector< int > item_id
Definition: SPLS.h:13