From 8793e52fb32e6142ae25ccfa2fd61bb1457034a1 Mon Sep 17 00:00:00 2001 From: Robert Cohn Date: Mon, 2 Oct 2023 14:55:53 -0500 Subject: [PATCH] cleanup --- examples/mhp/CMakeLists.txt | 13 -- examples/mhp/stencil-2d-matrix-rows.cpp | 185 ---------------------- examples/mhp/stencil-2d-matrix.cpp | 202 ------------------------ examples/mhp/tile-example.cpp | 84 ---------- 4 files changed, 484 deletions(-) delete mode 100644 examples/mhp/stencil-2d-matrix-rows.cpp delete mode 100644 examples/mhp/stencil-2d-matrix.cpp delete mode 100644 examples/mhp/tile-example.cpp diff --git a/examples/mhp/CMakeLists.txt b/examples/mhp/CMakeLists.txt index 6058eff6cd..d4510ee3e2 100644 --- a/examples/mhp/CMakeLists.txt +++ b/examples/mhp/CMakeLists.txt @@ -9,11 +9,6 @@ if(ENABLE_SYCL) add_executable(mhp_dot_product_benchmark dot_product_benchmark.cpp) target_link_libraries(mhp_dot_product_benchmark DR::mpi cxxopts) add_mpi_test(mhp_dot_product mhp_dot_product_benchmark 1 -n 1000) - - add_executable(tile-example tile-example.cpp) - target_link_libraries(tile-example cxxopts DR::mpi) - add_mpi_test(tile-example tile-example 2) - endif() add_executable(vector-add vector-add.cpp) @@ -31,14 +26,6 @@ add_mhp_example(stencil-1d-array) add_mhp_example(stencil-1d-pointer) add_mhp_example(hello_world) -add_executable(stencil-2d-matrix stencil-2d-matrix.cpp) -target_link_libraries(stencil-2d-matrix cxxopts DR::mpi) -add_mpi_test(stencil-2d-matrix stencil-2d-matrix 2) - -add_executable(stencil-2d-matrix-rows stencil-2d-matrix-rows.cpp) -target_link_libraries(stencil-2d-matrix-rows cxxopts DR::mpi) -add_mpi_test(stencil-2d-matrix-rows stencil-2d-matrix-rows 2) - if(OpenMP_FOUND) add_executable(vector-add-ref vector-add-ref.cpp) target_link_libraries(vector-add-ref PRIVATE MPI::MPI_CXX OpenMP::OpenMP_CXX diff --git a/examples/mhp/stencil-2d-matrix-rows.cpp b/examples/mhp/stencil-2d-matrix-rows.cpp deleted file mode 100644 index 77ec937b27..0000000000 --- a/examples/mhp/stencil-2d-matrix-rows.cpp +++ /dev/null @@ -1,185 +0,0 @@ -// SPDX-FileCopyrightText: Intel Corporation -// -// SPDX-License-Identifier: BSD-3-Clause - -#include - -#include "../include/data-utils.hpp" -#include "cxxopts.hpp" -#include "mpi.h" - -#include "dr/mhp.hpp" - -using T = float; - -int comm_rank; - -cxxopts::ParseResult options; - -std::size_t nc = 0; -std::size_t nr = 0; -std::size_t steps = 0; - -// -// verification -// -int matrix_compare(std::string label, dr::mhp::distributed_dense_matrix &dm, - std::vector> &vv) { - int res = 0; - for (std::size_t i = 0; i < vv.size(); i++) { - for (std::size_t j = 0; j < vv[i].size(); j++) { - if (!is_equal(vv[i][j], (T)dm.begin()[{i, j}])) { - fmt::print("{}: {} Fail in cell [{},{}] values: ref {} matrix {})\n", - comm_rank, label, i, j, vv[i][j], (T)dm.begin()[{i, j}]); - res = -1; - } - } - } - return res; -} - -// -// calculation formula -// -T calculate(T arg1, T arg2, T arg3, T arg4, T arg5) { - return arg1 + arg2 + arg3 + arg4 + arg5; -} - -// -// verification of results -// -void stencil_op_verify(std::vector> &va, - std::vector> &vb) { - for (std::size_t i = 1; i < va.size() - 1; i++) { - for (std::size_t j = 1; j < va[i].size() - 1; j++) - vb[i][j] = calculate(va[i - 1][j], va[i][j], va[i + 1][j], va[i][j - 1], - va[i][j + 1]); - } -} - -int stencil_check(dr::mhp::distributed_dense_matrix &a, - dr::mhp::distributed_dense_matrix &b) { - - std::vector> va(nr), vb(nr); - - for (auto &r : va) { - r.resize(nc); - rng::iota(r, 10); - } - - for (auto &r : vb) { - r.resize(nc); - rng::iota(r, 10); - } - - for (std::size_t s = 0; s < steps; s++) { - stencil_op_verify(va, vb); - std::swap(va, vb); - } - - return matrix_compare("A", a, (steps % 2) ? vb : va) + - matrix_compare("B", b, (steps % 2) ? va : vb); -} - -// -// stencil -// -auto stencil_op = [](auto &&p) { - /* FixMe: should not return a row, but zipped range of iterators should be - * passed to stencil */ - - std::vector out_row((*p).size()); - - out_row[0] = p[0][0]; - for (std::size_t i = 1; i < nc - 1; i++) { - out_row[i] = - calculate(p[-1][i], p[0][i - 1], p[0][i], p[0][i + 1], p[1][i]); - } - out_row[nc - 1] = p[0][nc - 1]; - return out_row; -}; - -int stencil() { - auto dist = dr::mhp::distribution().halo(1); // 1 row - dr::mhp::distributed_dense_matrix a(nr, nc, -1, dist), b(nr, nc, -1, dist); - - // the same operation on every row - dr::mhp::for_each(a.rows(), [](auto row) { rng::iota(row, 10); }); - - // different operation on every row is posiible by access to the row (*r) - for (auto r = b.rows().begin(); r != b.rows().end(); r++) { - if (r.is_local()) - rng::iota(*r, 10); - } - - // subranges of rows - auto in = rng::subrange(a.rows().begin() + 1, a.rows().end() - 1); - auto out = rng::subrange(b.rows().begin() + 1, b.rows().end() - 1); - - for (std::size_t s = 0; s < steps; s++) { - dr::mhp::halo(in).exchange(); - dr::mhp::transform(in, out.begin(), stencil_op); - std::swap(in, out); - dr::mhp::barrier(); - } - - dr::mhp::fence(); - MPI_Barrier(MPI_COMM_WORLD); - - // verify the result of operation by comparison with sequential - // version on std structures - if (comm_rank == 0) { - if (0 == stencil_check(a, b)) { - fmt::print("stencil-2d-matrix-rows: check OK!\n"); - } else { - fmt::print("stencil-2d-matrix-rows: check failed\n"); - return 1; - } - } - return 0; -} - -int main(int argc, char *argv[]) { - - cxxopts::Options options_spec(argv[0], "stencil 2d"); - // clang-format off - options_spec.add_options() - ("log", "Enable logging") - ("rows", "Number of rows", cxxopts::value()->default_value("20")) - ("cols", "Number of columns", cxxopts::value()->default_value("10")) - ("steps", "Number of time steps", cxxopts::value()->default_value("3")) - ("help", "Print help"); - // clang-format on - - try { - options = options_spec.parse(argc, argv); - } catch (const cxxopts::OptionParseException &e) { - std::cout << options_spec.help() << "\n"; - exit(1); - } - - if (options.count("help")) { - std::cout << options_spec.help() << std::endl; - exit(0); - } - - nr = options["rows"].as(); - nc = options["cols"].as(); - steps = options["steps"].as(); - std::ofstream *logfile = nullptr; - if (options.count("log")) { - logfile = new std::ofstream(fmt::format("dr.{}.log", comm_rank)); - dr::drlog.set_file(*logfile); - } - dr::drlog.debug("Rank: {}\n", comm_rank); - - MPI_Init(&argc, &argv); - MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank); - dr::mhp::init(); - - auto error = stencil(); - - dr::mhp::finalize(); - MPI_Finalize(); - return error; -} diff --git a/examples/mhp/stencil-2d-matrix.cpp b/examples/mhp/stencil-2d-matrix.cpp deleted file mode 100644 index 11f6756576..0000000000 --- a/examples/mhp/stencil-2d-matrix.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// SPDX-FileCopyrightText: Intel Corporation -// -// SPDX-License-Identifier: BSD-3-Clause - -#include "../include/data-utils.hpp" - -#include "cxxopts.hpp" -#include "dr/mhp.hpp" -#include "mpi.h" - -#include - -using T = float; - -int comm_rank; -int comm_size; - -cxxopts::ParseResult options; - -std::size_t nc; -std::size_t nr; -std::size_t steps; -std::size_t stencil_choice; - -// -// debug and verification functions -// - -int matrix_compare(std::string label, dr::mhp::distributed_dense_matrix &dm, - std::vector> &vv) { - int res = 0; - for (std::size_t i = 0; i < vv.size(); i++) { - for (std::size_t j = 0; j < vv[i].size(); j++) { - if (!is_equal(vv[i][j], (T)dm.begin()[{i, j}])) { - fmt::print("{}: {} Fail in cell [{},{}] values: ref {} matrix {})\n", - comm_rank, label, i, j, vv[i][j], (T)dm.begin()[{i, j}]); - res = -1; - } - } - } - return res; -} - -// -// calculation formula -// -T calculate(T arg1, T arg2, T arg3, T arg4, T arg5) { - return arg1 + arg2 + arg3 + arg4 + arg5; -} - -// -// verification of results -// - -auto stencil_op_verify(std::vector> &va, - std::vector> &vb) { - for (std::size_t i = 1; i < va.size() - 1; i++) { - for (std::size_t j = 1; j < va[i].size() - 1; j++) - vb[i][j] = calculate(va[i - 1][j], va[i][j], va[i + 1][j], va[i][j - 1], - va[i][j + 1]); - } -} - -int stencil_check(dr::mhp::distributed_dense_matrix &a, - dr::mhp::distributed_dense_matrix &b) { - - std::vector> va(nr), vb(nr); - - for (auto r = va.begin(); r != va.end(); r++) { - (*r).resize(nc); - rng::iota(*r, 10); - } - - for (auto r = vb.begin(); r != vb.end(); r++) { - (*r).resize(nc); - rng::iota(*r, 10); - } - - for (std::size_t s = 0; s < steps; s++) { - stencil_op_verify(va, vb); - std::swap(va, vb); - } - - return matrix_compare("A", a, (steps % 2) ? vb : va) + - matrix_compare("B", b, (steps % 2) ? va : vb); -} - -// -// stencil -// - -/* first access a row, then element in row */ -auto stencil_op_two_step_access = [](auto &p) { - return calculate(p[-1][0], p[0][0], p[+1][0], p[0][-1], p[0][+1]); -}; - -/* access element at once, with 2 coordinates */ -auto stencil_op_one_step_access = [](auto &p) { - return calculate(p[{-1, 0}], p[{0, 0}], p[{+1, 0}], p[{0, -1}], p[{0, +1}]); -}; - -int stencil(auto stencil_op) { - auto dist = dr::mhp::distribution().halo(1, 1); // 1 row - dr::mhp::distributed_dense_matrix a(nr, nc, -1, dist), b(nr, nc, -1, dist); - - // 1st approach - different operation for each row is possible here - for (auto r = a.rows().begin(); r != a.rows().end(); r++) { - if (r.is_local()) - rng::iota(*r, 10); - } - - // 2nd approach - the same operation for each row - dr::mhp::for_each(b.rows(), [](auto row) { rng::iota(row, 10); }); - - // rectgangular subrange of 2d matrix - auto in = dr::mhp::subrange(a, {1, a.shape()[0] - 1}, {1, a.shape()[1] - 1}); - auto out = dr::mhp::subrange(b, {1, b.shape()[0] - 1}, {1, b.shape()[1] - 1}); - - // transform of the above subrange - for (std::size_t s = 0; s < steps; s++) { - dr::mhp::halo(in).exchange(); - dr::mhp::transform(in, out.begin(), stencil_op); - std::swap(in, out); - dr::mhp::barrier(); - } - - dr::mhp::barrier(); - - if (comm_rank == 0) { - if (0 == stencil_check(a, b)) { - fmt::print("stencil-2d-matrix ({}-step stencil): check OK!\n", - stencil_choice); - } else { - fmt::print("stencil-2d-matrix ({}-step stencil): check failed\n", - stencil_choice); - return 1; - } - } - return 0; -} - -int main(int argc, char *argv[]) { - - cxxopts::Options options_spec(argv[0], "stencil-2d-matrix"); - - // clang-format off - options_spec.add_options() - ("log", "Enable logging") - ("rows", "Number of rows", cxxopts::value()->default_value("20")) - ("cols", "Number of columns", cxxopts::value()->default_value("10")) - ("steps", "Number of time steps", cxxopts::value()->default_value("5")) - ("stencil", "Choice of stencil, 1-step or 2-step", cxxopts::value()->default_value("1")) - ("help", "Print help"); - // clang-format on - - try { - options = options_spec.parse(argc, argv); - } catch (const cxxopts::OptionParseException &e) { - std::cout << options_spec.help() << "\n"; - exit(1); - } - - if (options.count("help")) { - std::cout << options_spec.help() << std::endl; - exit(0); - } - - nr = options["rows"].as(); - nc = options["cols"].as(); - steps = options["steps"].as(); - stencil_choice = options["stencil"].as(); - - std::ofstream *logfile = nullptr; - if (options.count("log")) { - logfile = new std::ofstream(fmt::format("dr.{}.log", comm_rank)); - dr::drlog.set_file(*logfile); - } - dr::drlog.debug("Rank: {}\n", comm_rank); - - MPI_Init(&argc, &argv); - MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank); - MPI_Comm_size(MPI_COMM_WORLD, &comm_size); - dr::mhp::init(); - - int error = -1; - switch (stencil_choice) { - case 1: - error = stencil(stencil_op_one_step_access); - break; - case 2: - error = stencil(stencil_op_two_step_access); - break; - default: - fmt::print("Error: stencil arg should be 1 for 1-step stencil or 2 for " - "2-step stencil\n"); - break; - } - - dr::mhp::finalize(); - MPI_Finalize(); - return error; -} diff --git a/examples/mhp/tile-example.cpp b/examples/mhp/tile-example.cpp deleted file mode 100644 index 886e6976a1..0000000000 --- a/examples/mhp/tile-example.cpp +++ /dev/null @@ -1,84 +0,0 @@ -// SPDX-FileCopyrightText: Intel Corporation -// -// SPDX-License-Identifier: BSD-3-Clause - -#include - -#include "cxxopts.hpp" -#include "mpi.h" - -#include "dr/mhp.hpp" - -using T = float; - -MPI_Comm comm; -int comm_rank; -int comm_size; - -int main(int argc, char *argv[]) { - MPI_Init(&argc, &argv); - comm = MPI_COMM_WORLD; - MPI_Comm_rank(comm, &comm_rank); - MPI_Comm_size(comm, &comm_size); - dr::mhp::init(); - - std::size_t nc = 16; - std::size_t nr = 16; - - { - - auto dist = dr::mhp::distribution().halo(1); // 1 row - dr::mhp::distributed_dense_matrix a(nr, nc, -1, dist); - - // different operation on every row - user must be aware of rows - // distribution - for (auto r = a.rows().begin(); r != a.rows().end(); r++) { - if (r.is_local()) - rng::iota(*r, 10); - } - - for (auto r = a.rows().begin(); r != a.rows().end(); r++) { - if (r.is_local()) { - auto &&row = *r; - fmt::print("{}\n", row); - } - } - - dr::mhp::fence(); - fflush(stdout); - MPI_Barrier(comm); - fflush(stdout); - MPI_Barrier(comm); - - if (comm_rank == 0) { - - fmt::print("Printing out whole matrix:\n"); - for (auto &&tile : a.tile_segments()) { - for (auto &&[index, value] : tile) { - auto &&[i, j] = index; - fmt::print("({}, {}): {}\n", i, j, value); - } - } - - fmt::print("Printing out individual tiles:\n"); - for (std::size_t i = 0; i < a.grid_shape()[0]; i++) { - for (std::size_t j = 0; j < a.grid_shape()[1]; j++) { - fmt::print("Tile {}, {}\n", i, j); - - auto &&tile = a.tile({i, j}); - - for (auto &&[index, value] : tile) { - auto &&[i, j] = index; - fmt::print("({}, {}): {}\n", i, j, value); - } - } - } - } - - MPI_Barrier(comm); - } - - dr::mhp::finalize(); - MPI_Finalize(); - return 0; -}