Skip to content

Commit

Permalink
#126: add support for rank-level user_defined QOI
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Oct 29, 2024
1 parent 0030760 commit b85e23d
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 26 deletions.
6 changes: 6 additions & 0 deletions data/reader_test_data/reader_test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"doubleSample": 2.213,
"stringSample": "abc",
"elementIDSample": 30000000000
},
"user_defined": {
"intSample": 1,
"doubleSample": 2.213,
"stringSample": "abc",
"elementIDSample": 30000000000
}
},
"phases": [
Expand Down
2 changes: 1 addition & 1 deletion data/synthetic_attributes/data.0.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/synthetic_attributes/data.1.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/synthetic_attributes/data.2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/synthetic_attributes/data.3.json

Large diffs are not rendered by default.

Binary file modified data/synthetic_attributes_compressed/data.0.json.br
Binary file not shown.
Binary file modified data/synthetic_attributes_compressed/data.1.json.br
Binary file not shown.
Binary file modified data/synthetic_attributes_compressed/data.2.json.br
Binary file not shown.
Binary file modified data/synthetic_attributes_compressed/data.3.json.br
Binary file not shown.
22 changes: 16 additions & 6 deletions scripts/create_synthetic_attributes_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def get_attributes_dict(id):
"stringAttribute": f"id is {id}"
}

def get_user_defined_dict(id):
"""Creates some user_defined list"""

return {
"doubleUserDefined": float(id) * 3.14,
"elementIDUserDefined": id + 30000000000,
"intUserDefined": id,
"stringUserDefined": f"id is {id}"
}

project_dir = os.path.dirname(os.path.dirname(__file__))
test_data_dir = os.path.join(project_dir, "data", "lb_test_data")

Expand All @@ -28,16 +38,16 @@ def get_attributes_dict(id):
with open(json_file, encoding="utf-8") as f:
json_data = json.load(f)

# Add rank attributes
rank_attributes = get_attributes_dict(rank_id)
json_data["metadata"]["attributes"] = rank_attributes
# Add rank attributes and user_defined field
json_data["metadata"]["attributes"] = get_attributes_dict(rank_id)
json_data["metadata"]["user_defined"] = get_user_defined_dict(rank_id)

# Then object attributes
# Then object attributes and user_defined field
for phase in json_data["phases"]:
for task in phase["tasks"]:
task_id = task["entity"]["id"]
attributes = get_attributes_dict(task_id)
task["attributes"] = attributes
task["attributes"] = get_attributes_dict(task_id)
task["user_defined"] = get_user_defined_dict(task_id)

# Write out json
output_file = os.path.join(output_dir, f"data.{rank_id}.json")
Expand Down
14 changes: 9 additions & 5 deletions src/vt-tv/api/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ struct Info {
// Look in attributes (will throw an error if QOI doesn't exist)
qoi_getter = [&](Rank rank, PhaseType phase) {
(void)phase;
return convertQOIVariantTypeToDouble_(getRankAttribute(rank, rank_qoi));
return convertQOIVariantTypeToDouble_(getRankAttributeOrUserDefined(rank, rank_qoi));
};
}
return qoi_getter;
Expand Down Expand Up @@ -907,18 +907,22 @@ struct Info {
}

/**
* \brief Get the specified attribute of a rank at a given phase
* \brief Get the specified attribute or user_defined QOI of a rank at a given phase
*
* \param[in] rank the current rank
* \param[in] rank_qoi the attribute
* \param[in] rank_qoi the attribute or user_defined QOI
*
* \return the requested attribute
* \return the requested attribute or user_defined QOI
*/
QOIVariantTypes getRankAttribute(Rank rank, std::string rank_qoi) const {
QOIVariantTypes getRankAttributeOrUserDefined(Rank rank, std::string rank_qoi) const {
auto rank_attributes = rank.getAttributes();
if (rank_attributes.count(rank_qoi) > 0) {
return rank_attributes.at(rank_qoi);
} else {
auto rank_user_defined = rank.getUserDefined();
if (rank_user_defined.count(rank_qoi) > 0) {
return rank_user_defined.at(rank_qoi);
}
throw std::runtime_error("Invalid Rank QOI: " + rank_qoi);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/vt-tv/api/rank.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ struct Rank {
Rank(
NodeType in_rank,
std::unordered_map<PhaseType, PhaseWork> in_phase_info,
std::unordered_map<std::string, QOIVariantTypes> in_user_defined = {},
std::unordered_map<std::string, QOIVariantTypes> in_attributes = {})
: rank_(in_rank),
phase_info_(std::move(in_phase_info)),
user_defined_(std::move(in_user_defined)),
attributes_(std::move(in_attributes)) { }

/**
Expand Down Expand Up @@ -100,6 +102,13 @@ struct Rank {
return phase_info_.at(phase).getLoad();
}

/**
* \brief Get user_defined fields
*
* \return user_defined fields
*/
auto const& getUserDefined() const { return user_defined_; }

/**
* \brief Get attribute fields
*
Expand Down Expand Up @@ -151,6 +160,7 @@ struct Rank {
void serialize(SerializerT& s) {
s | rank_;
s | phase_info_;
s | user_defined_;
s | attributes_;
}

Expand All @@ -160,6 +170,7 @@ struct Rank {
/// Work for each phase
std::unordered_map<PhaseType, PhaseWork> phase_info_;
/// QOIs to be visualized
std::unordered_map<std::string, QOIVariantTypes> user_defined_;
std::unordered_map<std::string, QOIVariantTypes> attributes_;
};

Expand Down
19 changes: 12 additions & 7 deletions src/vt-tv/utility/json_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,6 @@ std::unique_ptr<Info> JSONReader::parse() {
ElementIDType to_id = to.value("id", to["seq_id"]);

assert(bytes.is_number() && "bytes must be a number");
// assert(from.is_number());
// assert(to.is_number());

// fmt::print(" From: {}, to: {}\n", from_id, to_id);

// Object on this rank sent data
auto from_it = objects.find(from_id);
Expand All @@ -262,20 +258,29 @@ std::unique_ptr<Info> JSONReader::parse() {
}
}

std::unordered_map<std::string, QOIVariantTypes> readed_metadata;
std::unordered_map<std::string, QOIVariantTypes> readed_attributes;
std::unordered_map<std::string, QOIVariantTypes> readed_user_defined;
if (j.find("metadata") != j.end()) {
auto metadata = j["metadata"];
if (metadata.find("attributes") != metadata.end()) {
auto attributes = metadata["attributes"];
if (attributes.is_object()) {
for (auto& [key, value] : attributes.items()) {
readed_metadata[key] = value;
readed_attributes[key] = value;
}
}
}
if (metadata.find("user_defined") != metadata.end()) {
auto user_defined = metadata["user_defined"];
if (user_defined.is_object()) {
for (auto& [key, value] : user_defined.items()) {
readed_user_defined[key] = value;
}
}
}
}

Rank r{rank_, std::move(phase_info), std::move(readed_metadata)};
Rank r{rank_, std::move(phase_info), std::move(readed_user_defined), std::move(readed_attributes)};

std::unordered_map<NodeType, Rank> rank_info;
rank_info.try_emplace(rank_, std::move(r));
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/api/test_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,13 @@ TEST_F(InfoTest, test_get_rank_qoi) {
}
}

ASSERT_EQ(std::get<int>(info.getRankAttribute(rank_0, "attr1")), 12.0);
ASSERT_EQ(std::get<int>(info.getRankAttribute(rank_1, "attr1")), 13.0);
ASSERT_EQ(std::get<int>(info.getRankAttributeOrUserDefined(rank_0, "attr1")), 12.0);
ASSERT_EQ(std::get<int>(info.getRankAttributeOrUserDefined(rank_1, "attr1")), 13.0);

ASSERT_EQ(
std::get<std::string>(info.getRankAttribute(rank_0, "attr2")), "ab");
std::get<std::string>(info.getRankAttributeOrUserDefined(rank_0, "attr2")), "ab");
ASSERT_EQ(
std::get<std::string>(info.getRankAttribute(rank_1, "attr2")), "cd");
std::get<std::string>(info.getRankAttributeOrUserDefined(rank_1, "attr2")), "cd");

// Test getRankQOIAtPhase method
ASSERT_EQ(info.getRankQOIAtPhase(0, 0, "sent_volume"), 2.0);
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/utility/test_json_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ TEST_F(JSONReaderTest, test_json_reader_metadata_attributes) {
EXPECT_EQ("abc", std::get<std::string>(rank_attributes.at("stringSample")));
}


TEST_F(JSONReaderTest, test_json_reader_rank_user_defined) {
std::filesystem::path p =
std::filesystem::path(SRC_DIR) / "data/lb_test_data";
std::string path = std::filesystem::absolute(p).string();

NodeType rank = 0;
JSONReader reader{rank};

reader.readFile(path + "/reader_test_data.json");
auto info = reader.parse();
auto& rank_info = info->getRank(rank);
EXPECT_EQ(rank_info.getRankID(), rank);

auto& rank_user_defined = rank_info.getUserDefined();
EXPECT_TRUE(rank_user_defined.find("intSample") != rank_user_defined.end());
EXPECT_EQ(1, std::get<int>(rank_user_defined.at("intSample")));

EXPECT_TRUE(rank_user_defined.find("doubleSample") != rank_user_defined.end());
EXPECT_EQ(2.213, std::get<double>(rank_user_defined.at("doubleSample")));

EXPECT_TRUE(rank_user_defined.find("stringSample") != rank_user_defined.end());
EXPECT_EQ("abc", std::get<std::string>(rank_user_defined.at("stringSample")));
}

TEST_F(JSONReaderTest, test_json_reader_object_info_attributes) {
std::filesystem::path p =
std::filesystem::path(SRC_DIR) / "data/reader_test_data";
Expand Down

0 comments on commit b85e23d

Please sign in to comment.