Skip to content

Commit

Permalink
Refactor examples (#178)
Browse files Browse the repository at this point in the history
* Move print_result to header file, remove kamping:: after using namespace, add missing license header

* Print rank, add function to print result on all PEs, take Communicator by reference

* Add documentation
  • Loading branch information
DanielSeemaier authored Mar 24, 2022
1 parent a195106 commit 34ca36b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 33 deletions.
17 changes: 5 additions & 12 deletions examples/alltoall_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// You should have received a copy of the GNU Lesser General Public License along with KaMPI.ng. If not, see
// <https://www.gnu.org/licenses/>.

#include "helpers_for_examples.hpp"
#include "kamping/checking_casts.hpp"
#include "kamping/communicator.hpp"
#include "kamping/parameter_factories.hpp"
Expand All @@ -20,26 +21,18 @@
#include <numeric>
#include <vector>

template <typename T>
void print_result(std::vector<T>& result, kamping::Communicator comm) {
if (comm.rank() == 0) {
for (auto elem: result) {
std::cout << elem << std::endl;
}
}
}

int main() {
using namespace kamping;

MPI_Init(NULL, NULL);
MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
kamping::Communicator comm;
std::vector<int> input(asserting_cast<size_t>(comm.size()));
Communicator comm;
std::vector<int> input(asserting_cast<size_t>(comm.size()));
std::iota(input.begin(), input.end(), 0);
std::vector<int> output;

comm.alltoall(send_buf(input), recv_buf(output));
print_result(output, comm);
print_result_on_root(output, comm);

MPI_Finalize();
return 0;
Expand Down
41 changes: 41 additions & 0 deletions examples/helpers_for_examples.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of KaMPI.ng.
//
// Copyright 2022 The KaMPI.ng Authors
//
// KaMPI.ng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
// version. KaMPI.ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with KaMPI.ng. If not, see
// <https://www.gnu.org/licenses/>.

#include <vector>

#include "kamping/communicator.hpp"

namespace kamping {
/// @brief Print all elements in a container, prefixed with the rank of the current PE.
/// @tparam T Type of the elements contained in the container.
/// @param result The container whose elements are printed.
/// @param comm KaMPI.ng communicator to get the rank of the PE.
template <typename T>
void print_result(std::vector<T> const& result, Communicator const& comm) {
for (auto const& elem: result) {
std::cout << "[PE " << comm.rank() << "] " << elem << "\n";
}
std::cout << std::flush;
}

/// @brief Print all elements in a container only on the root PE.
/// @tparam T Type of the elements contained in the container.
/// @param result The container whose elements are printed on the root PE.
/// @param comm KaMPI.ng communicator to determine which PE is the root PE.
template <typename T>
void print_result_on_root(std::vector<T> const& result, Communicator const& comm) {
if (comm.is_root()) {
print_result(result, comm);
}
}
} // namespace kamping
49 changes: 28 additions & 21 deletions examples/reduce_example.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// This file is part of KaMPI.ng.
//
// Copyright 2022 The KaMPI.ng Authors
//
// KaMPI.ng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
// version. KaMPI.ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.
//
// You should have received a copy of the GNU Lesser General Public License along with KaMPI.ng. If not, see
// <https://www.gnu.org/licenses/>.

#include "helpers_for_examples.hpp"
#include "kamping/collectives/reduce.hpp"
#include "kamping/communicator.hpp"
#include "kamping/mpi_ops.hpp"
Expand All @@ -7,14 +21,6 @@
#include <mpi.h>
#include <vector>

template <typename T>
void print_result(std::vector<T>& result, kamping::Communicator comm) {
if (comm.rank() == 0) {
for (auto elem: result) {
std::cout << elem << std::endl;
}
}
}
struct my_plus {
template <typename T>
auto operator()(T a, T b) {
Expand All @@ -23,28 +29,29 @@ struct my_plus {
};

int main() {
using namespace kamping;

MPI_Init(NULL, NULL);
MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_ARE_FATAL);
kamping::Communicator comm;
std::vector<double> input = {1, 2, 3};
std::vector<double> output;
using namespace kamping;
Communicator comm;
std::vector<double> input = {1, 2, 3};
std::vector<double> output;

auto result0 = comm.reduce(send_buf(input), op(ops::plus<>()), root(0)).extract_recv_buffer();
print_result(result0, comm);
print_result_on_root(result0, comm);
auto result1 = comm.reduce(send_buf(input), op(ops::plus<double>())).extract_recv_buffer();
print_result(result1, comm);
auto result2 = comm.reduce(send_buf(input), kamping::op(my_plus{}, commutative)).extract_recv_buffer();
print_result(result2, comm);
print_result_on_root(result1, comm);
auto result2 = comm.reduce(send_buf(input), op(my_plus{}, commutative)).extract_recv_buffer();
print_result_on_root(result2, comm);

auto result3 [[maybe_unused]] = comm.reduce(
send_buf(input), kamping::recv_buf(output), kamping::op([](auto a, auto b) { return a + b; }, non_commutative));
print_result(output, comm);
auto result3 [[maybe_unused]] =
comm.reduce(send_buf(input), recv_buf(output), op([](auto a, auto b) { return a + b; }, non_commutative));
print_result_on_root(output, comm);

std::vector<std::pair<int, double>> input2 = {{3, 0.25}};

auto result4 = comm.reduce(
send_buf(input2), kamping::op(
send_buf(input2), op(
[](auto a, auto b) {
// dummy
return std::pair(a.first + b.first, a.second + b.second);
Expand Down Expand Up @@ -72,7 +79,7 @@ int main() {
input3[1].y = 0.75;
}

auto result5 = comm.reduce(send_buf(input3), kamping::op(ops::max<>(), commutative)).extract_recv_buffer();
auto result5 = comm.reduce(send_buf(input3), op(ops::max<>(), commutative)).extract_recv_buffer();
if (comm.rank() == 0) {
for (auto& elem: result5) {
std::cout << elem.x << " " << elem.y << " " << elem.z << std::endl;
Expand Down

0 comments on commit 34ca36b

Please sign in to comment.