Skip to content

Commit 828cd47

Browse files
committed
Make cut array reading / writing conditions
1 parent b219aa6 commit 828cd47

3 files changed

Lines changed: 53 additions & 24 deletions

File tree

src/ArbConnectivity.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ struct ArbLatticeConnectivity {
1818
std::vector<ZoneIndex> zones;
1919
std::unique_ptr<cut_t[]> cuts;
2020
double grid_size{};
21+
bool has_cuts{};
2122

2223
ArbLatticeConnectivity() = default;
23-
ArbLatticeConnectivity(size_t chunk_begin_, size_t chunk_end_, size_t num_nodes_global_, size_t Q_)
24+
ArbLatticeConnectivity(size_t chunk_begin_, size_t chunk_end_, size_t num_nodes_global_, size_t Q_, bool has_cuts_ = false)
2425
: chunk_begin(chunk_begin_),
2526
chunk_end(chunk_end_),
2627
num_nodes_global(num_nodes_global_),
@@ -29,7 +30,8 @@ struct ArbLatticeConnectivity {
2930
og_index(std::make_unique<Index[]>(chunk_end_ - chunk_begin_)),
3031
nbrs(std::make_unique<Index[]>((chunk_end_ - chunk_begin_) * Q)),
3132
zones_per_node(std::make_unique<ZoneIndex[]>(chunk_end_ - chunk_begin_)),
32-
cuts(std::make_unique<cut_t[]>(26 * (chunk_end_ - chunk_begin_))) {
33+
cuts(has_cuts_ ? std::make_unique<cut_t[]>(26 * (chunk_end_ - chunk_begin_)) : nullptr),
34+
has_cuts(has_cuts_) {
3335
zones.reserve(getLocalSize());
3436
}
3537

src/ArbLattice.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,35 @@ void ArbLattice::readFromCxn(const std::string& cxn_path) {
160160
});
161161
for (size_t i = 0; i != labels.size(); ++i) label_to_ind_map.emplace(labels[i], i);
162162

163-
// Nodes header
164-
process_section("NODES", [&](size_t num_nodes_global) {
163+
// Optional CUTS section - check at the next word to see if it's CUTS or NODES
164+
bool has_cuts = false;
165+
{
166+
file >> word;
167+
check_file_ok("Failed to read section header: expected CUTS or NODES");
168+
if (word == "CUTS") {
169+
size_t n_cuts{};
170+
file >> n_cuts;
171+
check_file_ok("Failed to read CUTS size");
172+
if (n_cuts != 26) throw std::logic_error(wrap_err_msg("Expected CUTS 26, got CUTS " + std::to_string(n_cuts)));
173+
has_cuts = true;
174+
file >> word;
175+
check_file_ok("Failed to read section header: NODES");
176+
}
177+
check_expected_word("NODES", word);
178+
}
179+
180+
// Nodes
181+
{
182+
size_t num_nodes_global{};
183+
file >> num_nodes_global;
184+
check_file_ok("Failed to read section size: NODES");
185+
165186
// Compute the current rank's offset and number of nodes to read
166187
const auto chunk_offsets = computeInitialNodeDist(num_nodes_global, static_cast<size_t>(comm_size));
167188
const auto chunk_begin = static_cast<size_t>(chunk_offsets[comm_rank]), chunk_end = static_cast<size_t>(chunk_offsets[comm_rank + 1]);
168189
const auto num_nodes_local = chunk_end - chunk_begin;
169190

170-
connect = ArbLatticeConnectivity(chunk_begin, chunk_end, num_nodes_global, Q);
191+
connect = ArbLatticeConnectivity(chunk_begin, chunk_end, num_nodes_global, Q, has_cuts);
171192
connect.grid_size = grid_size;
172193

173194
// Skip chunk_begin + 1 (header) newlines
@@ -192,15 +213,17 @@ void ArbLattice::readFromCxn(const std::string& cxn_path) {
192213
auto& zone = connect.zones.emplace_back();
193214
file >> zone;
194215
}
195-
196-
for (size_t d = 0; d != 26; ++d) {
197-
auto& cut = connect.cut_distance(d, local_node_ind);
198-
file >> cut;
216+
217+
if (has_cuts) {
218+
for (size_t d = 0; d != 26; ++d) {
219+
auto& cut = connect.cut_distance(d, local_node_ind);
220+
file >> cut;
221+
}
199222
}
200223

201224
check_file_ok("Failed to read node data");
202225
}
203-
});
226+
}
204227
}
205228

206229
void ArbLattice::partition() {
@@ -299,7 +322,11 @@ void ArbLattice::allocDeviceMemory() {
299322
sizes.coords_pitch = local_sz;
300323
coords_device = cudaMakeUnique2D<real_t>(sizes.coords_pitch, 3);
301324
sizes.cuts_pitch = local_sz;
302-
cut_distances_device = cudaMakeUnique2D<cut_t>(sizes.cuts_pitch, 26);
325+
if (connect.has_cuts) {
326+
cut_distances_device = cudaMakeUnique2D<cut_t>(sizes.cuts_pitch, 26);
327+
} else {
328+
cut_distances_device.reset();
329+
}
303330
sizes.snaps_pitch = local_sz + ghost_nodes.size() + 1;
304331
snaps_device = cudaMakeUnique2D<storage_t>(sizes.snaps_pitch, sizes.snaps * NF);
305332
node_types_device = cudaMakeUnique<flag_t>(local_sz);
@@ -411,8 +438,10 @@ void ArbLattice::initDeviceData(pugi::xml_node arb_node, const std::map<std::str
411438
copyVecToDeviceAsync(neighbors_device.get(), nbrs, inStream);
412439
const auto coords = computeCoords();
413440
copyVecToDeviceAsync(coords_device.get(), coords, inStream);
414-
const auto cuts = computeCutDistances();
415-
copyVecToDeviceAsync(cut_distances_device.get(), cuts, inStream);
441+
if (connect.has_cuts) {
442+
const auto cuts = computeCutDistances();
443+
copyVecToDeviceAsync(cut_distances_device.get(), cuts, inStream);
444+
}
416445
CudaStreamSynchronize(inStream);
417446
}
418447

@@ -424,7 +453,7 @@ void ArbLattice::initContainer() {
424453
#endif
425454
launcher.container.nbrs = neighbors_device.get();
426455
launcher.container.coords = coords_device.get();
427-
launcher.container.Q = cut_distances_device.get();
456+
launcher.container.Q = connect.has_cuts ? cut_distances_device.get() : nullptr;
428457
launcher.container.cuts_pitch = sizes.cuts_pitch;
429458
launcher.container.node_types = node_types_device.get();
430459
launcher.container.nbrs_pitch = sizes.neighbors_pitch;

src/toArb.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,14 @@ static auto makeArbLatticeIndexMap(const lbRegion& region, const std::vector<boo
5757
return retval;
5858
}
5959

60-
static int writeArbLatticeHeader(std::fstream& file, size_t n_nodes, double grid_size, const Model& model, const std::map<std::string, int>& zone_map) {
60+
static int writeArbLatticeHeader(std::fstream& file, size_t n_nodes, double grid_size, const Model& model, const std::map<std::string, int>& zone_map, bool has_cuts) {
6161
file << "OFFSET_DIRECTIONS " << Model_m::offset_directions.size() << '\n';
6262
for (const auto [x, y, z] : Model_m::offset_directions) file << x << ' ' << y << ' ' << z << '\n';
6363
file << "GRID_SIZE " << grid_size << '\n';
6464
file << "NODE_LABELS " << model.nodetypeflags.size() + zone_map.size() << '\n';
6565
for (const auto& ntf : model.nodetypeflags) file << ntf.name << '\n';
6666
for (const auto& [name, zf] : zone_map) file << "_Z_" << name << '\n';
67+
if (has_cuts) file << "CUTS 26\n";
6768
file << "NODES " << n_nodes << '\n';
6869
return file.good() ? EXIT_SUCCESS : EXIT_FAILURE;
6970
}
@@ -74,7 +75,8 @@ static int writeArbLatticeNodes(const Geometry& geo,
7475
const std::unordered_map<long, long>& lin_to_arb_index_map,
7576
const std::vector<bool>& bulk_bmp,
7677
std::fstream& file,
77-
double spacing) {
78+
double spacing,
79+
bool has_cuts) {
7880
const long nx = geo.totalregion.nx, ny = geo.totalregion.ny, nz = geo.totalregion.nz;
7981
const auto get_nbr_id = [&](long my_pos, long nbr_pos) -> long {
8082
if (my_pos != nbr_pos && bulk_bmp[my_pos] && bulk_bmp[nbr_pos]) return -1; // ignore edges between bulk nodes
@@ -121,18 +123,13 @@ static int writeArbLatticeNodes(const Geometry& geo,
121123
++gz_ind;
122124
}
123125

124-
if (geo.Q != nullptr){
126+
if (has_cuts) {
125127
size_t regsize = geo.region.sizeL();
126128
for (int d=0; d < 26; d++){
127129
size_t k = geo.region.offset(x, y, z);
128130
const cut_t q = geo.Q[regsize*d + k];
129131
file << q << ' ';
130132
}
131-
} else {
132-
for (int i=0; i < 26; i++){
133-
const cut_t q = NO_CUT;
134-
file << q << ' ';
135-
}
136133
}
137134
file << '\n';
138135
if (!file.good()) break; // Fail early
@@ -149,13 +146,14 @@ static int writeArbLattice(const Geometry& geo,
149146
const std::vector<bool>& bulk_bmp,
150147
const std::string& filename,
151148
double spacing) {
149+
const bool has_cuts = geo.Q != nullptr;
152150
std::fstream file(filename, std::ios_base::out);
153151
if (!file.good()) {
154152
ERROR("Failed to open .cxn file for writing");
155153
return EXIT_FAILURE;
156154
}
157-
if (writeArbLatticeHeader(file, lin_to_arb_index_map.size(), spacing, model, zone_map)) return EXIT_FAILURE;
158-
return writeArbLatticeNodes(geo, model, zone_map, lin_to_arb_index_map, bulk_bmp, file, spacing);
155+
if (writeArbLatticeHeader(file, lin_to_arb_index_map.size(), spacing, model, zone_map, has_cuts)) return EXIT_FAILURE;
156+
return writeArbLatticeNodes(geo, model, zone_map, lin_to_arb_index_map, bulk_bmp, file, spacing, has_cuts);
159157
}
160158

161159
static int writeArbXml(const Solver& solver, const Geometry& geo, const Model& model, const std::string& cxn_path) {

0 commit comments

Comments
 (0)