Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel run with one proc w/o partition file #721

Merged
merged 20 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ jobs:
with:
ref: gh-pages
clean: false

- name: Move generated files where git can see them
run: cp -rp docs/html/* .

- name: Prevent generated docs dir from being committed and overwriting on the next run.
run: rm -Rf docs/html

Expand Down
13 changes: 13 additions & 0 deletions include/core/Partition_Data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PARTITION_DATA_H
#define PARTITION_DATA_H

struct PartitionData
{
using Tuple = std::tuple<int, std::string, std::string, std::string>;
int mpi_world_rank;
std::unordered_set<std::string> catchment_ids;
std::unordered_set<std::string> nexus_ids;
std::vector<Tuple> remote_connections;
};

#endif //PARTITION_DATA_H
37 changes: 37 additions & 0 deletions include/core/Partition_One.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef PARTITION_ONE_H
#define PARTITION_ONE_H

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <unordered_set>
#include <FeatureBuilder.hpp>
#include "features/Features.hpp"
#include <FeatureCollection.hpp>
#include "Partition_Data.hpp"

class Partition_One {

public:
/**
* The function that parses geojson::GeoJSON data and build unordered sets of catchment_ids and nexus_ids

* @param catchment_collection the geojson::GeoJSON data containing all the necessary hydrofabric info
*/
void generate_partition(geojson::GeoJSON& catchment_collection)
{
for(auto& feature: *catchment_collection)
{
std::string cat_id = feature->get_id();
partition_data.catchment_ids.emplace(cat_id);
std::string nex_id = feature->get_property("toid").as_string();
partition_data.nexus_ids.emplace(nex_id);
}
partition_data.mpi_world_rank = 0;
}

PartitionData partition_data;
};

#endif // PARTITION_ONE_H
17 changes: 3 additions & 14 deletions include/core/Partition_Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,7 @@
#include "features/Features.hpp"
#include <FeatureCollection.hpp>
#include "JSONProperty.hpp"

using Tuple = std::tuple<int, std::string, std::string, std::string>;

//This struct is moved from private section to here so that the unit test function can access it
struct PartitionData
{
int mpi_world_rank;
std::unordered_set<std::string> catchment_ids;
std::unordered_set<std::string> nexus_ids;
std::vector<Tuple> remote_connections;
};

#include "Partition_Data.hpp"

class Partitions_Parser {

Expand Down Expand Up @@ -63,8 +52,8 @@ class Partitions_Parser {
std::string remote_nex_id;
std::string remote_cat_id;
std::string direction;
Tuple tmp_tuple;
std::vector<Tuple> remote_conn_vec;
PartitionData::Tuple tmp_tuple;
std::vector<PartitionData::Tuple> remote_conn_vec;
int part_counter = 0;
for(auto &partition: tree.get_child("partitions")) {
//Get partition id
Expand Down
68 changes: 42 additions & 26 deletions src/NGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ int mpi_rank = 0;
#include "core/Partition_Parser.hpp"
#include <HY_Features_MPI.hpp>

#include "core/Partition_One.hpp"

std::string PARTITION_PATH = "";
int mpi_num_procs;
#endif // NGEN_MPI_ACTIVE
Expand Down Expand Up @@ -249,10 +251,16 @@ int main(int argc, char *argv[]) {
REALIZATION_CONFIG_PATH = argv[5];

#ifdef NGEN_MPI_ACTIVE

// Initalize MPI
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs);

if (argc >= 7) {
PARTITION_PATH = argv[6];
}
else {
else if (mpi_num_procs > 1) {
std::cout << "Missing required argument for partition file path." << std::endl;
exit(-1);
}
Expand All @@ -261,17 +269,11 @@ int main(int argc, char *argv[]) {
if (strcmp(argv[7], MPI_HF_SUB_CLI_FLAG) == 0) {
is_subdivided_hydrofabric_wanted = true;
}
else {
else if (mpi_num_procs > 1) {
std::cout << "Unexpected arg '" << argv[7] << "'; try " << MPI_HF_SUB_CLI_FLAG << std::endl;
exit(-1);
}
}

// Initalize MPI
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs);

#endif // NGEN_MPI_ACTIVE

#ifdef WRITE_PID_FILE_FOR_GDB_SERVER
Expand Down Expand Up @@ -336,20 +338,23 @@ int main(int argc, char *argv[]) {
std::cout << "Building Nexus collection" << std::endl;

#ifdef NGEN_MPI_ACTIVE
Partitions_Parser partition_parser(PARTITION_PATH);
// TODO: add something here to make sure this step worked for every rank, and maybe to checksum the file
partition_parser.parse_partition_file();

std::vector<PartitionData> &partitions = partition_parser.partition_ranks;
PartitionData &local_data = partitions[mpi_rank];
if (!nexus_subset_ids.empty()) {
std::cerr << "Warning: CLI provided nexus subset will be ignored when using partition config";
}
if (!catchment_subset_ids.empty()) {
std::cerr << "Warning: CLI provided catchment subset will be ignored when using partition config";
PartitionData local_data;
if (mpi_num_procs > 1) {
Partitions_Parser partition_parser(PARTITION_PATH);
// TODO: add something here to make sure this step worked for every rank, and maybe to checksum the file
partition_parser.parse_partition_file();

std::vector<PartitionData> &partitions = partition_parser.partition_ranks;
local_data = std::move(partitions[mpi_rank]);
if (!nexus_subset_ids.empty()) {
std::cerr << "Warning: CLI provided nexus subset will be ignored when using partition config";
}
if (!catchment_subset_ids.empty()) {
std::cerr << "Warning: CLI provided catchment subset will be ignored when using partition config";
}
nexus_subset_ids = std::vector<std::string>(local_data.nexus_ids.begin(), local_data.nexus_ids.end());
catchment_subset_ids = std::vector<std::string>(local_data.catchment_ids.begin(), local_data.catchment_ids.end());
}
nexus_subset_ids = std::vector<std::string>(local_data.nexus_ids.begin(), local_data.nexus_ids.end());
catchment_subset_ids = std::vector<std::string>(local_data.catchment_ids.begin(), local_data.catchment_ids.end());
#endif // NGEN_MPI_ACTIVE

// TODO: Instead of iterating through a collection of FeatureBase objects mapping to nexi, we instead want to iterate through HY_HydroLocation objects
Expand Down Expand Up @@ -379,14 +384,14 @@ int main(int argc, char *argv[]) {

for(auto& feature: *catchment_collection)
{
//feature->set_id(feature->get_property("ID").as_string());
//feature->set_id(feature->get_property("id").as_string());
nexus_collection->add_feature(feature);
//std::cout<<"Catchment "<<feature->get_id()<<" -> Nexus "<<feature->get_property("toID").as_string()<<std::endl;
//std::cout<<"Catchment "<<feature->get_id()<<" -> Nexus "<<feature->get_property("toid").as_string()<<std::endl;
}
//Update the feature ids for the combined collection, using the alternative property 'id'
//to map features to their primary id as well as the alternative property
nexus_collection->update_ids("id");
std::cout<<"Initializing formulations\n";
std::cout<<"Initializing formulations" << std::endl;
std::shared_ptr<realization::Formulation_Manager> manager = std::make_shared<realization::Formulation_Manager>(REALIZATION_CONFIG_PATH);
manager->read(catchment_collection, utils::getStdOut());

Expand All @@ -406,10 +411,17 @@ int main(int argc, char *argv[]) {
}
}
#endif //NGEN_ROUTING_ACTIVE
std::cout<<"Building Feature Index\n";
std::cout<<"Building Feature Index" <<std::endl;;
std::string link_key = "toid";
nexus_collection->link_features_from_property(nullptr, &link_key);

#ifdef NGEN_MPI_ACTIVE
//mpirun with one processor without partition file
if (mpi_num_procs == 1) {
Partition_One partition_one;
partition_one.generate_partition(catchment_collection);
local_data = std::move(partition_one.partition_data);
}
hy_features::HY_Features_MPI features = hy_features::HY_Features_MPI(local_data, nexus_collection, manager, mpi_rank, mpi_num_procs);
#else
hy_features::HY_Features features = hy_features::HY_Features(nexus_collection, manager);
Expand All @@ -425,7 +437,11 @@ int main(int argc, char *argv[]) {
//Still hacking nexus output for the moment
for(const auto& id : features.nexuses()) {
#ifdef NGEN_MPI_ACTIVE
if (!features.is_remote_sender_nexus(id)) {
if (mpi_num_procs > 1) {
if (!features.is_remote_sender_nexus(id)) {
nexus_outfiles[id].open(manager->get_output_root() + id + "_output.csv", std::ios::trunc);
}
} else {
nexus_outfiles[id].open(manager->get_output_root() + id + "_output.csv", std::ios::trunc);
}
#else
Expand Down
15 changes: 14 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ ngen_add_test(
# NGEN_WITH_MPI
)

########################## Partition_One Tests
ngen_add_test(
test_partition_one
OBJECTS
utils/Partition_One_Test.cpp
LIBRARIES
gmock
NGen::core
NGen::geojson
NGen::geopackage
REQUIRES
NGEN_WITH_SQLITE
)

########################## MultiLayer Tests
ngen_add_test(
test_multilayer
Expand Down Expand Up @@ -452,7 +466,6 @@ ngen_add_test(
NGen::logging
NGen::ngen_bmi
testbmicppmodel

)

# Discover for test_all
Expand Down
Loading
Loading