-
Notifications
You must be signed in to change notification settings - Fork 25
[Comm] allow collecting MPI timers from python #1728
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,56 @@ namespace { | |
| recv_vec = result; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Allgather a vector of characters from all MPI ranks into a single string | ||
| * | ||
| * The resulting string is concatenated in rank order and is returned on every rank. | ||
| */ | ||
| template<class Tchar> | ||
| inline void _internal_allgather_str( | ||
| const std::basic_string<Tchar> &send_vec, std::basic_string<Tchar> &recv_vec) { | ||
| StackEntry stack_loc{}; | ||
|
|
||
| if (shamcomm::world_size() == 1) { | ||
| recv_vec = send_vec; | ||
| return; | ||
| } | ||
|
|
||
| i32 wsize = shamcomm::world_size(); | ||
| size_t wsize_sz = static_cast<size_t>(wsize); | ||
|
|
||
| // counts/displacements are expressed in number of characters. | ||
| std::vector<int> counts(wsize_sz); | ||
| std::vector<int> disps(wsize_sz); | ||
|
|
||
| // MPI counts/displacements use `int`. | ||
| int local_count = static_cast<int>(send_vec.size()); | ||
|
|
||
| shamcomm::mpi::Allgather( | ||
| &local_count, 1, MPI_INT, counts.data(), 1, MPI_INT, MPI_COMM_WORLD); | ||
|
|
||
| for (size_t i = 0; i < wsize_sz; i++) { | ||
| disps[i] = (i > 0) ? (disps[i - 1] + counts[i - 1]) : 0; | ||
| } | ||
|
|
||
| int global_len = disps[wsize_sz - 1] + counts[wsize_sz - 1]; | ||
|
|
||
| std::basic_string<Tchar> result; | ||
| result.resize(static_cast<size_t>(global_len)); | ||
|
|
||
| shamcomm::mpi::Allgatherv( | ||
| send_vec.data(), | ||
| local_count, | ||
| MPI_CHAR, | ||
| result.data(), | ||
| counts.data(), | ||
| disps.data(), | ||
| MPI_CHAR, | ||
|
tdavidcl marked this conversation as resolved.
|
||
| MPI_COMM_WORLD); | ||
|
|
||
| recv_vec = result; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void shamcomm::gather_str(const std::string &send_vec, std::string &recv_vec) { | ||
|
|
@@ -94,6 +144,17 @@ void shamcomm::gather_basic_str( | |
| _internal_gather_str(send_vec, recv_vec); | ||
| } | ||
|
|
||
| void shamcomm::allgather_str(const std::string &send_vec, std::string &recv_vec) { | ||
| StackEntry stack_loc{}; | ||
| _internal_allgather_str(send_vec, recv_vec); | ||
| } | ||
|
|
||
| void shamcomm::allgather_basic_str( | ||
| const std::basic_string<byte> &send_vec, std::basic_string<byte> &recv_vec) { | ||
| StackEntry stack_loc{}; | ||
| _internal_allgather_str(send_vec, recv_vec); | ||
| } | ||
|
|
||
| std::unordered_map<std::string, int> shamcomm::string_histogram( | ||
| const std::vector<std::string> &inputs, std::string delimiter) { | ||
| std::string accum_loc = ""; | ||
|
|
@@ -119,3 +180,24 @@ std::unordered_map<std::string, int> shamcomm::string_histogram( | |
|
|
||
| return {}; | ||
| } | ||
|
|
||
| std::unordered_map<std::string, int> shamcomm::all_string_histogram( | ||
| const std::vector<std::string> &inputs, std::string delimiter) { | ||
| std::string accum_loc = ""; | ||
| for (auto &s : inputs) { | ||
| accum_loc += s + delimiter; | ||
| } | ||
|
Comment on lines
+186
to
+189
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeatedly concatenating strings with Example: std::string accum_loc;
size_t total_size = 0;
for (const auto& s : inputs) {
total_size += s.size() + delimiter.size();
}
accum_loc.reserve(total_size);
for (const auto &s : inputs) {
accum_loc.append(s);
accum_loc.append(delimiter);
} |
||
|
|
||
| std::string recv = ""; | ||
| allgather_str(accum_loc, recv); | ||
|
|
||
| std::vector<std::string> splitted = shambase::split_str(recv, delimiter); | ||
|
|
||
| std::unordered_map<std::string, int> histogram; | ||
|
|
||
| for (size_t i = 0; i < splitted.size(); i++) { | ||
| histogram[splitted[i]] += 1; | ||
| } | ||
|
Comment on lines
+198
to
+200
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| return histogram; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||||||||||||||||||
| // -------------------------------------------------------// | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // SHAMROCK code for hydrodynamics | ||||||||||||||||||||||
| // Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me> | ||||||||||||||||||||||
| // SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1 | ||||||||||||||||||||||
| // Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information | ||||||||||||||||||||||
| // | ||||||||||||||||||||||
| // -------------------------------------------------------// | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * @file pyShamcomm.cpp | ||||||||||||||||||||||
| * @author Timothée David--Cléris (tim.shamrock@proton.me) | ||||||||||||||||||||||
| * @brief | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| #include "shamalgs/collective/reduction.hpp" | ||||||||||||||||||||||
| #include "shambindings/pybind11_stl.hpp" | ||||||||||||||||||||||
| #include "shambindings/pybindaliases.hpp" | ||||||||||||||||||||||
| #include "shambindings/pytypealias.hpp" | ||||||||||||||||||||||
| #include "shamcomm/collectives.hpp" | ||||||||||||||||||||||
| #include "shamcomm/logs.hpp" | ||||||||||||||||||||||
| #include "shamcomm/wrapper.hpp" | ||||||||||||||||||||||
| #include <pybind11/pytypes.h> | ||||||||||||||||||||||
| #include <unordered_map> | ||||||||||||||||||||||
| #include <utility> | ||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Register_pymod(shamcommlibinit) { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| py::module shamcomm_module = m.def_submodule("comm", "comm library"); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| shamcomm_module.def("get_timer", [](std::string name) { | ||||||||||||||||||||||
| return shamcomm::mpi::get_timer(std::move(name)); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| shamcomm_module.def("get_timers", []() { | ||||||||||||||||||||||
| return shamcomm::mpi::get_timers(); | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| shamcomm_module.def( | ||||||||||||||||||||||
| "mpi_timers_delta", | ||||||||||||||||||||||
| [](std::unordered_map<std::string, f64> start, std::unordered_map<std::string, f64> end) { | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||
| std::vector<std::string> keys{}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for (auto &[k, v] : end) { | ||||||||||||||||||||||
| keys.push_back(k); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+43
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid potential reallocations while populating the
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| auto key_histo = shamcomm::all_string_histogram(keys); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| std::unordered_map<std::string, f64> deltas{}; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| for (auto &[k, c] : key_histo) { | ||||||||||||||||||||||
| deltas[k] = shamalgs::collective::allreduce_max(end[k] - start[k]); | ||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Example with double end_val = end.count(k) ? end.at(k) : 0.0;
double start_val = start.count(k) ? start.at(k) : 0.0;
deltas[k] = shamalgs::collective::allreduce_max(end_val - start_val); |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return deltas; | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,3 +37,26 @@ TestStart(Unittest, "shamcomm/collectives::gather_str", test_gather_str, 4) { | |
|
|
||
| REQUIRE_EQUAL(recv, result); | ||
| } | ||
|
|
||
| TestStart(Unittest, "shamcomm/collectives::allgather_str", test_allgather_str, 4) { | ||
|
|
||
| std::array<std::string, 4> ref_base{ | ||
| "I'm a very important string", | ||
| "But I'm a very important string", | ||
| "Listen, I'm a very important string", | ||
| "The most importantest string", | ||
| }; | ||
|
|
||
| std::string result = ""; | ||
| for (u32 i = 0; i < ref_base.size(); i++) { | ||
| result += ref_base[i]; | ||
| } | ||
|
Comment on lines
+50
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop for string concatenation can be written more concisely using std::string result = std::accumulate(ref_base.begin(), ref_base.end(), std::string{});References
|
||
|
|
||
| std::string send = ref_base[shamcomm::world_rank()]; | ||
|
|
||
| std::string recv = "random string"; // Just to check that it is overwritten | ||
|
|
||
| shamcomm::allgather_str(send, recv); | ||
|
|
||
| REQUIRE_EQUAL(recv, result); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.