diff --git a/homework_2/Makefile b/homework_2/Makefile index 5b80592..00c4264 100644 --- a/homework_2/Makefile +++ b/homework_2/Makefile @@ -207,6 +207,8 @@ deb_distbitonic: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=BITONIC -DDEBUG deb_distbitonic: TARGET := deb_distbitonic deb_distbitonic: $(BUILD_DIR)/$(TARGET) +tests: CC := mpicc +tests: CXX := mpic++ tests: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=BITONIC -DDEBUG -DTESTING tests: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=BITONIC -DDEBUG -DTESTING tests: TARGET := tests diff --git a/homework_2/include/distbitonic.hpp b/homework_2/include/distbitonic.hpp index 643d0f8..98eba1a 100644 --- a/homework_2/include/distbitonic.hpp +++ b/homework_2/include/distbitonic.hpp @@ -10,15 +10,9 @@ #ifndef DISTBITONIC_H_ #define DISTBITONIC_H_ -#if !defined DEBUG -#define NDEBUG -#endif -#include +#include +#include "utils.hpp" -#include -#if !defined TESTING - #include -#endif /*! * Enumerator for the different versions of the sorting method @@ -31,14 +25,6 @@ enum class SortMode { using Data_t = std::vector; using AllData_t = std::vector; -struct mpi_t { - size_t world_size{}; - size_t world_rank{}; - std::string processor_name {}; -}; - -extern mpi_t mpi; - /* * ============================== Sort utilities ============================== */ @@ -47,41 +33,41 @@ extern mpi_t mpi; * The primary function template of ascending(). It is DISABLED since , it is explicitly specialized * for each of the \c SortMode */ -template bool ascending(size_t, [[maybe_unused]] size_t) noexcept = delete; -template <> bool ascending(size_t node, [[maybe_unused]] size_t depth) noexcept; -template <> bool ascending(size_t node, size_t depth) noexcept; +template bool ascending(mpi_id_t, [[maybe_unused]] size_t) noexcept = delete; +template <> bool ascending(mpi_id_t node, [[maybe_unused]] size_t depth) noexcept; +template <> bool ascending(mpi_id_t node, size_t depth) noexcept; /*! * The primary function template of partner(). It is DISABLED since , it is explicitly specialized * for each of the \c SortMode */ -template size_t partner(size_t, size_t) noexcept = delete; -template <> size_t partner(size_t node, size_t step) noexcept; -template <> size_t partner(size_t node, size_t step) noexcept; +template mpi_id_t partner(mpi_id_t, size_t) noexcept = delete; +template <> mpi_id_t partner(mpi_id_t node, size_t step) noexcept; +template <> mpi_id_t partner(mpi_id_t node, size_t step) noexcept; /*! - * The primary function template of keepsmall(). It is DISABLED since , it is explicitly specialized + * The primary function template of keepSmall(). It is DISABLED since , it is explicitly specialized * for each of the \c SortMode */ -template bool keepsmall(size_t, size_t, [[maybe_unused]] size_t) noexcept = delete; -template<> bool keepsmall(size_t node, size_t partner, [[maybe_unused]] size_t depth) noexcept; -template<> bool keepsmall(size_t node, size_t partner, size_t depth) noexcept; +template bool keepSmall(mpi_id_t, mpi_id_t, [[maybe_unused]] size_t) noexcept = delete; +template<> bool keepSmall(mpi_id_t node, mpi_id_t partner, [[maybe_unused]] size_t depth) noexcept; +template<> bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth) noexcept; -bool isActive(size_t node, size_t nodes) noexcept; +bool isActive(mpi_id_t node, mpi_id_t nodes) noexcept; /* * ============================== Data utilities ============================== */ -void exchange(size_t node, size_t partner); -void minmax(AllData_t& data, size_t node, size_t partner, bool keepsmall); +void exchange(mpi_id_t node, mpi_id_t partner); +void minmax(AllData_t& data, mpi_id_t node, mpi_id_t partner, bool keepsmall); /* * ============================== Sort algorithms ============================== */ -void bubbletonic_network(AllData_t& data, size_t nodes); -void distbubbletonic(size_t P, AllData_t& data); +void bubbletonic_network(AllData_t& data, mpi_id_t nodes); +void distBubbletonic(mpi_id_t P, AllData_t& data); -void bitonic_network(AllData_t& data, size_t nodes, size_t depth); -void distbitonic(size_t P, AllData_t& data); +void bitonic_network(AllData_t& data, mpi_id_t nodes, mpi_id_t depth); +void distBitonic(mpi_id_t P, AllData_t& data); #endif //DISTBITONIC_H_ diff --git a/homework_2/include/utils.hpp b/homework_2/include/utils.hpp index b5995b1..5accd7b 100644 --- a/homework_2/include/utils.hpp +++ b/homework_2/include/utils.hpp @@ -12,14 +12,68 @@ #include #include #include +#include -#include "matrix.hpp" +//#include "matrix.hpp" #include "config.h" + +template +struct MPI_t { + using ID_t = TID; // Export TID type (currently int defined by the standard) + + void init(int *argc, char ***argv) { + // Initialize the MPI environment + MPI_Init(argc, argv); + + // Get the number of processes + int size_value, rank_value; + size_ = static_cast(MPI_Comm_size(MPI_COMM_WORLD, &size_value)); + rank_ = static_cast(MPI_Comm_rank(MPI_COMM_WORLD, &rank_value)); + + // Get the name of the processor + char processor_name[MPI_MAX_PROCESSOR_NAME]; + int name_len; + MPI_Get_processor_name(processor_name, &name_len); + name_ = std::string (processor_name, name_len); + } + + void finalize() { + // Finalize the MPI environment. + MPI_Finalize(); + } + + bool exchange(ID_t partner, const void *send_data, void *recv_data, int data_count, MPI_Datatype datatype) { + bool ret = true; + MPI_Status status; + MPI_Sendrecv( + send_data, data_count, datatype, partner, 0, + recv_data, data_count, datatype, partner, 0, + MPI_COMM_WORLD, &status + ); + if (status.MPI_ERROR != MPI_SUCCESS) + ret = false; + + return ret; + } + + // Accessors + [[nodiscard]] ID_t rank() const noexcept { return rank_; } + [[nodiscard]] ID_t size() const noexcept { return size_; } + [[nodiscard]] const std::string& name() const noexcept { return name_; } + +private: + ID_t rank_{}; + ID_t size_{}; + std::string name_{}; +}; + +extern MPI_t<> mpi; +using mpi_id_t = MPI_t<>::ID_t; + /*! * A Logger for entire program. */ - struct Log { struct Endl { } endl; //!< a tag object to to use it as a new line request. diff --git a/homework_2/src/distbitonic.cpp b/homework_2/src/distbitonic.cpp index bbb2c21..c1af9b9 100644 --- a/homework_2/src/distbitonic.cpp +++ b/homework_2/src/distbitonic.cpp @@ -10,10 +10,15 @@ #include #include #include +#if !defined DEBUG +#define NDEBUG +#endif #include + #include "distbitonic.hpp" + /*! * Returns the ascending or descending configuration of the node's sequence based on * the current node (MPI process) and the depth of the sorting network @@ -22,7 +27,7 @@ * @return True if we need ascending configuration, false otherwise */ template <> -bool ascending(size_t node, [[maybe_unused]] size_t depth) noexcept { +bool ascending(mpi_id_t node, [[maybe_unused]] size_t depth) noexcept { return (node % 2) == 0; } @@ -36,7 +41,7 @@ bool ascending(size_t node, [[maybe_unused]] size_t depth * @return True if we need ascending configuration, false otherwise */ template <> -bool ascending(size_t node, size_t depth) noexcept { +bool ascending(mpi_id_t node, size_t depth) noexcept { return !(node & (1 << depth)); } @@ -49,7 +54,7 @@ bool ascending(size_t node, size_t depth) noexcept { * @return The node id of the partner for data exchange */ template <> -size_t partner(size_t node, size_t step) noexcept { +mpi_id_t partner(mpi_id_t node, size_t step) noexcept { // return (node % 2 == step % 2) ? node + 1 : node - 1; return (((node+step) % 2) == 0) ? node + 1 : node - 1; } @@ -63,7 +68,7 @@ size_t partner(size_t node, size_t step) noexcept { * @return The node id of the partner for data exchange */ template <> -size_t partner(size_t node, size_t step) noexcept { +mpi_id_t partner(mpi_id_t node, size_t step) noexcept { return (node ^ (1 << step)); } @@ -76,7 +81,7 @@ size_t partner(size_t node, size_t step) noexcept { * @return True if the node should keep the small values, false otherwise */ template <> -bool keepsmall(size_t node, size_t partner, [[maybe_unused]] size_t depth) noexcept { +bool keepSmall(mpi_id_t node, mpi_id_t partner, [[maybe_unused]] size_t depth) noexcept { assert(node != partner); return (node < partner); } @@ -90,7 +95,7 @@ bool keepsmall(size_t node, size_t partner, [[maybe_unuse * @return True if the node should keep the small values, false otherwise */ template <> -bool keepsmall(size_t node, size_t partner, size_t depth) noexcept { +bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth) noexcept { assert(node != partner); return ascending(node, depth) == (node < partner); } @@ -103,16 +108,16 @@ bool keepsmall(size_t node, size_t partner, size_t depth) noe * @param nodes The total number of nodes * @return True if the node is active, false otherwise */ -bool isActive(size_t node, size_t nodes) noexcept { - return (node < nodes); +bool isActive(mpi_id_t node, mpi_id_t nodes) noexcept { + return (node >= 0) && (node < (nodes-1)); } -void exchange(size_t node, size_t partner) { - assert(node != partner); +void exchange(mpi_id_t node, mpi_id_t partner) { + assert(node != partner); } -void minmax(AllData_t& data, size_t node, size_t partner, bool keepsmall) { +void minmax(AllData_t& data, mpi_id_t node, mpi_id_t partner, bool keepsmall) { for (size_t i = 0; i < data[node].size(); ++i) { if (keepsmall && data[node][i] > data[partner][i]) std::swap(data[node][i], data[partner][i]); @@ -124,10 +129,10 @@ void minmax(AllData_t& data, size_t node, size_t partner, bool keepsmall) { -void bubbletonic_network(AllData_t& data, size_t nodes, size_t depth) { - for (size_t node = 0 ; node < nodes ; ++node) { // Currently we do all nodes here! +void bubbletonic_network(AllData_t& data, mpi_id_t nodes, size_t depth) { + for (mpi_id_t node = 0 ; node < nodes ; ++node) { // Currently we do all nodes here! auto part = partner(node, depth); - auto ks = keepsmall(node, part, 0); + auto ks = keepSmall(node, part, 0); if (isActive(node, nodes) && node < part) { exchange(node, part); minmax(data, node, part, ks); @@ -145,8 +150,8 @@ void bubbletonic_network(AllData_t& data, size_t nodes, size_t depth) { } } -void distbubbletonic(size_t P, AllData_t& data) { - for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! +void distBubbletonic(mpi_id_t P, AllData_t& data) { + for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! // Initially sort to create the half part of a bitonic if (ascending(node, 0)) std::sort(data[node].begin(), data[node].end(), std::less<>()); @@ -159,19 +164,19 @@ void distbubbletonic(size_t P, AllData_t& data) { } // Invert the descending ones - for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! + for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! if (!ascending(node, 0)) std::sort(data[node].begin(), data[node].end(), std::less<>()); } } -void bitonic_network(AllData_t& data, size_t nodes, size_t depth) { +void bitonic_network(AllData_t& data, mpi_id_t nodes, size_t depth) { for (size_t step = depth; step > 0;) { --step; - for (size_t node = 0; node < nodes; ++node) { // Currently we do all nodes here! + for (mpi_id_t node = 0; node < nodes; ++node) { // Currently we do all nodes here! auto part = partner(node, step); - auto ks = keepsmall(node, part, depth); + auto ks = keepSmall(node, part, depth); if (node < part) { exchange(node, part); minmax(data, node, part, ks); @@ -180,10 +185,10 @@ void bitonic_network(AllData_t& data, size_t nodes, size_t depth) { } } -void distbitonic(size_t P, AllData_t& data) { +void distBitonic(mpi_id_t P, AllData_t& data) { auto p = static_cast(std::log2(P)); - for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! + for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! // Initially sort to create the half part of a bitonic if (ascending(node, 0)) std::sort(data[node].begin(), data[node].end(), std::less<>()); @@ -195,7 +200,7 @@ void distbitonic(size_t P, AllData_t& data) { for (size_t depth = 1; depth <= p; ++depth) { bitonic_network(data, P, depth); - for (size_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! + for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here! // elbow-sort here if (ascending(node, depth)) std::sort(data[node].begin(), data[node].end(), std::less<>()); diff --git a/homework_2/src/main.cpp b/homework_2/src/main.cpp index 4b031bf..4e98a49 100644 --- a/homework_2/src/main.cpp +++ b/homework_2/src/main.cpp @@ -9,34 +9,14 @@ #include #include -#include // rand/srand -//#include // rand/srand -#if !defined TESTING - #include -#endif -#include "distbitonic.hpp" #include "utils.hpp" #include "config.h" -//#include "matrix.hpp" + // Global session data session_t session; -mpi_t mpi; - -/* - * Sorting data for up to 8 processes - */ -AllData_t Data { - Data_t (8), - Data_t (8), - Data_t (8), - Data_t (8), - Data_t (8), - Data_t (8), - Data_t (8), - Data_t (8) -}; +MPI_t<> mpi; /*! @@ -62,8 +42,8 @@ bool get_options(int argc, char* argv[]){ else if (arg == "-v" || arg == "--verbose") session.verbose = true; else if (arg == "-h" || arg == "--help") { - std::cout << "distbitonic - A distributed bitonic sort\n\n"; - std::cout << "distbitonic -x <> [-v]\n"; + std::cout << "distBitonic - A distributed bitonic sort\n\n"; + std::cout << "distBitonic -x <> [-v]\n"; std::cout << '\n'; std::cout << "Options:\n\n"; std::cout << " -v | --verbose\n"; @@ -72,7 +52,7 @@ bool get_options(int argc, char* argv[]){ std::cout << " Prints this and exit.\n\n"; std::cout << "Examples:\n\n"; std::cout << " ...Example case...:\n"; - std::cout << " > distbitonic -x \n\n"; + std::cout << " > distBitonic -x \n\n"; exit(0); } @@ -89,13 +69,13 @@ bool get_options(int argc, char* argv[]){ #if !defined TESTING int main(int argc, char* argv[]) try { - // try to read command line + // Initialize MPI environment + mpi.init(&argc, &argv); + + // try to read command line (after MPI parsing) if (!get_options(argc, argv)) exit(1); - // Initialize the MPI environment - MPI_Init(NULL, NULL); - #if defined DEBUG /* * In case of a debug build we will wait here until sleep_wait @@ -107,41 +87,24 @@ int main(int argc, char* argv[]) try { * $> gdb */ #if defined TESTING - volatile bool sleep_wait = false; + volatile bool sleep_wait = false; #else - volatile bool sleep_wait = true; + volatile bool sleep_wait = true; #endif - while (sleep_wait) - sleep(1); + while (sleep_wait) + sleep(1); #endif - // Get the number of processes - MPI_Comm_size(MPI_COMM_WORLD, reinterpret_cast(&mpi.world_size)); + // Print off a hello world message +// std::cout << "Hello world from processor: " << mpi.processor_name +// << " rank " << mpi.world_rank +// << " out of " << mpi.world_size << " processors\n"; - // Get the rank of the process - MPI_Comm_rank(MPI_COMM_WORLD, reinterpret_cast(&mpi.world_rank)); - // Get the name of the processor - char processor_name[MPI_MAX_PROCESSOR_NAME]; - int name_len; - MPI_Get_processor_name(processor_name, &name_len); - mpi.processor_name = std::string (processor_name, name_len); +// distBitonic (2, Data); +// distBitonic (4, Data); - // Print off a hello world message - std::cout << "Hello world from processor: " << mpi.processor_name - << " rank " << mpi.world_rank - << " out of " << mpi.world_size << " processors\n"; - -// std::srand(unsigned(std::time(nullptr))); -// for (auto& v : Data) { -// std::generate(v.begin(), v.end(), std::rand); -// } -// -// distbitonic (2, Data); -// distbitonic (4, Data); - - // Finalize the MPI environment. - MPI_Finalize(); + mpi.finalize(); return 0; } catch (std::exception& e) { diff --git a/homework_2/test/tests_Bitonic.cpp b/homework_2/test/tests_Bitonic.cpp index bbfb68f..0b9b42b 100644 --- a/homework_2/test/tests_Bitonic.cpp +++ b/homework_2/test/tests_Bitonic.cpp @@ -18,7 +18,7 @@ /* ================================== ascending ================================== */ /* - * bool ascending(size_t node, size_t depth); + * bool ascending(mpi_id_t node, size_t depth); * depth 0 (the initial ascending pattern) */ TEST(TdistBitonic_UT, ascending_test1) { @@ -32,13 +32,13 @@ TEST(TdistBitonic_UT, ascending_test1) { EXPECT_EQ(ascending(6, 0), true); EXPECT_EQ(ascending(7, 0), false); - for (size_t node = 0 ; node < 256 ; ++node) { + for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(ascending(node, 0), ((node % 2) ? false : true) ); } } /* - * bool ascending(size_t node, size_t depth); + * bool ascending(mpi_id_t node, size_t depth); * depth 1 */ TEST(TdistBitonic_UT, ascending_test2) { @@ -52,14 +52,14 @@ TEST(TdistBitonic_UT, ascending_test2) { EXPECT_EQ(ascending(6, 1), false); EXPECT_EQ(ascending(7, 1), false); - for (size_t node = 0 ; node < 256 ; ++node) { + for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(ascending(2*node, 1), ((node % 2) ? false:true)); EXPECT_EQ(ascending(2*node+1, 1), ((node % 2) ? false:true)); } } /* - * bool ascending(size_t node, size_t depth); + * bool ascending(mpi_id_t node, size_t depth); * various depths */ TEST(TdistBitonic_UT, ascending_test3) { @@ -67,37 +67,37 @@ TEST(TdistBitonic_UT, ascending_test3) { // Depth = 3 size_t ts_depth = 3; - for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 0 ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); - for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); // Depth = 4 ts_depth = 4; - for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 0L ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); - for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); // Depth = 8 ts_depth = 8; - for (size_t n = 0UL ; n < (1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 0L ; n < (1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = (1UL<<(ts_depth)) ; n < 2*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = (1<<(ts_depth)) ; n < 2*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); - for (size_t n = 2*(1UL<<(ts_depth)) ; n < 3*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 2*(1<<(ts_depth)) ; n < 3*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), true); - for (size_t n = 3*(1UL<<(ts_depth)) ; n < 4*(1UL<<(ts_depth)) ; ++n) + for (mpi_id_t n = 3*(1<<(ts_depth)) ; n < 4*(1<<(ts_depth)) ; ++n) EXPECT_EQ(ascending(n, ts_depth), false); } @@ -107,59 +107,59 @@ TEST(TdistBitonic_UT, ascending_test3) { /* ================================== partner ================================== */ /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * step = 0 */ TEST(TdistBitonic_UT, partner_test1) { - EXPECT_EQ(partner(0, 0), 1UL); - EXPECT_EQ(partner(1, 0), 0UL); - EXPECT_EQ(partner(2, 0), 3UL); - EXPECT_EQ(partner(3, 0), 2UL); - EXPECT_EQ(partner(4, 0), 5UL); - EXPECT_EQ(partner(5, 0), 4UL); - EXPECT_EQ(partner(6, 0), 7UL); - EXPECT_EQ(partner(7, 0), 6UL); + EXPECT_EQ(partner(0, 0), 1); + EXPECT_EQ(partner(1, 0), 0); + EXPECT_EQ(partner(2, 0), 3); + EXPECT_EQ(partner(3, 0), 2); + EXPECT_EQ(partner(4, 0), 5); + EXPECT_EQ(partner(5, 0), 4); + EXPECT_EQ(partner(6, 0), 7); + EXPECT_EQ(partner(7, 0), 6); - for (size_t node = 0 ; node < 256 ; ++node) { + for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(partner(node, 0), (node % 2) ? node-1 : node+1); } } /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * step = 1 */ TEST(TdistBitonic_UT, partner_test2) { - EXPECT_EQ(partner(0, 1), 2UL); - EXPECT_EQ(partner(1, 1), 3UL); - EXPECT_EQ(partner(2, 1), 0UL); - EXPECT_EQ(partner(3, 1), 1UL); - EXPECT_EQ(partner(4, 1), 6UL); - EXPECT_EQ(partner(5, 1), 7UL); - EXPECT_EQ(partner(6, 1), 4UL); - EXPECT_EQ(partner(7, 1), 5UL); - - for (size_t n1 = 0 ; n1 < 256 ; n1 += 2) { - auto n2 = n1 + 1UL; + EXPECT_EQ(partner(0, 1), 2); + EXPECT_EQ(partner(1, 1), 3); + EXPECT_EQ(partner(2, 1), 0); + EXPECT_EQ(partner(3, 1), 1); + EXPECT_EQ(partner(4, 1), 6); + EXPECT_EQ(partner(5, 1), 7); + EXPECT_EQ(partner(6, 1), 4); + EXPECT_EQ(partner(7, 1), 5); + + for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 2) { + auto n2 = n1 + 1; EXPECT_EQ(partner(n1, 1), ((n1 % 4) ? n1-2 : n1+2)); EXPECT_EQ(partner(n2, 1), ((n1 % 4) ? n2-2 : n2+2)); } } /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * various steps */ TEST(TdistBitonic_UT, partner_test3) { // step = 2 size_t ts_step = 2; - for (size_t n1 = 0 ; n1 < 256 ; n1 += 4) { - auto n2 = n1 + 1UL; - auto n3 = n1 + 2UL; - auto n4 = n1 + 3UL; + for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 4) { + auto n2 = n1 + 1; + auto n3 = n1 + 2; + auto n4 = n1 + 3; EXPECT_EQ(partner(n1, ts_step), ((n1 % 8) ? n1-4 : n1+4)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 8) ? n2-4 : n2+4)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 8) ? n3-4 : n3+4)); @@ -169,14 +169,14 @@ TEST(TdistBitonic_UT, partner_test3) { // step = 3 ts_step = 3; - for (size_t n1 = 0 ; n1 < 256 ; n1 += 8) { - auto n2 = n1 + 1UL; - auto n3 = n1 + 2UL; - auto n4 = n1 + 3UL; - auto n5 = n1 + 4UL; - auto n6 = n1 + 5UL; - auto n7 = n1 + 6UL; - auto n8 = n1 + 7UL; + for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 8) { + auto n2 = n1 + 1; + auto n3 = n1 + 2; + auto n4 = n1 + 3; + auto n5 = n1 + 4; + auto n6 = n1 + 5; + auto n7 = n1 + 6; + auto n8 = n1 + 7; EXPECT_EQ(partner(n1, ts_step), ((n1 % 16) ? n1-8 : n1+8)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 16) ? n2-8 : n2+8)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 16) ? n3-8 : n3+8)); @@ -190,22 +190,22 @@ TEST(TdistBitonic_UT, partner_test3) { // step = 4 ts_step = 4; - for (size_t n1 = 0 ; n1 < 256 ; n1 += 16) { - auto n2 = n1 + 1UL; - auto n3 = n1 + 2UL; - auto n4 = n1 + 3UL; - auto n5 = n1 + 4UL; - auto n6 = n1 + 5UL; - auto n7 = n1 + 6UL; - auto n8 = n1 + 7UL; - auto n9 = n1 + 8UL; - auto n10 = n1 + 9UL; - auto n11 = n1 + 10UL; - auto n12 = n1 + 11UL; - auto n13 = n1 + 12UL; - auto n14 = n1 + 13UL; - auto n15 = n1 + 14UL; - auto n16 = n1 + 15UL; + for (mpi_id_t n1 = 0 ; n1 < 256 ; n1 += 16) { + auto n2 = n1 + 1; + auto n3 = n1 + 2; + auto n4 = n1 + 3; + auto n5 = n1 + 4; + auto n6 = n1 + 5; + auto n7 = n1 + 6; + auto n8 = n1 + 7; + auto n9 = n1 + 8; + auto n10 = n1 + 9; + auto n11 = n1 + 10; + auto n12 = n1 + 11; + auto n13 = n1 + 12; + auto n14 = n1 + 13; + auto n15 = n1 + 14; + auto n16 = n1 + 15; EXPECT_EQ(partner(n1, ts_step), ((n1 % 32) ? n1-16 : n1+16)); EXPECT_EQ(partner(n2, ts_step), ((n1 % 32) ? n2-16 : n2+16)); EXPECT_EQ(partner(n3, ts_step), ((n1 % 32) ? n3-16 : n3+16)); @@ -227,108 +227,108 @@ TEST(TdistBitonic_UT, partner_test3) { -/* ================================== keepsmall ================================== */ +/* ================================== keepSmall ================================== */ /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * Assertion check */ TEST(TdistBitonic_UT, keepsmall_test1) { - ASSERT_DEATH(keepsmall(0, 0, 0), ""); - ASSERT_DEATH(keepsmall(1, 1, 42), ""); - ASSERT_DEATH(keepsmall(7, 7, 42), ""); + ASSERT_DEATH(keepSmall(0, 0, 0), ""); + ASSERT_DEATH(keepSmall(1, 1, 42), ""); + ASSERT_DEATH(keepSmall(7, 7, 42), ""); } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepsmall(mpi_id_t node, mpi_id_t partner, size_t depth); * - * depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 0, 1, 1, 0, 0, 1] + * depth: 1 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepSmall: Bool[1, 0, 0, 1, 1, 0, 0, 1] */ TEST(TdistBitonic_UT, keepsmall_test2) { - size_t ts_depth = 1UL; - size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; + size_t ts_depth = 1; + mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 0, 1, 1, 0, 0, 1}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepsmall(mpi_id_t node, mpi_id_t partner, size_t depth); * - * depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 0, 0, 1, 1] + * depth: 2 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepSmall: Bool[1, 1, 0, 0, 0, 0, 1, 1] */ TEST(TdistBitonic_UT, keepsmall_test3) { - size_t ts_depth = 2UL; - size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; + size_t ts_depth = 2; + mpi_id_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; bool ts_expected[] = {1, 1, 0, 0, 0, 0, 1, 1}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepsmall(mpi_id_t node, mpi_id_t partner, size_t depth); * - * depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 0, 1, 0, 1] + * depth: 2 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepSmall: Bool[1, 0, 1, 0, 0, 1, 0, 1] */ TEST(TdistBitonic_UT, keepsmall_test4) { - size_t ts_depth = 2UL; - size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; + size_t ts_depth = 2; + mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 1, 0, 0, 1, 0, 1}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * * depth: 3 | step: 2 | partner: [4, 5, 6, 7, 0, 1, 2, 3] | keepsmall: Bool[1, 1, 1, 1, 0, 0, 0, 0] */ TEST(TdistBitonic_UT, keepsmall_test5) { - size_t ts_depth = 3UL; - size_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3}; + size_t ts_depth = 3; + mpi_id_t ts_partner[] = {4, 5, 6, 7, 0, 1, 2, 3}; bool ts_expected[] = {1, 1, 1, 1, 0, 0, 0, 0}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * * depth: 3 | step: 1 | partner: [2, 3, 0, 1, 6, 7, 4, 5] | keepsmall: Bool[1, 1, 0, 0, 1, 1, 0, 0] */ TEST(TdistBitonic_UT, keepsmall_test6) { - size_t ts_depth = 3UL; - size_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; + size_t ts_depth = 3; + mpi_id_t ts_partner[] = {2, 3, 0, 1, 6, 7, 4, 5}; bool ts_expected[] = {1, 1, 0, 0, 1, 1, 0, 0}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * * depth: 3 | step: 0 | partner: [1, 0, 3, 2, 5, 4, 7, 6] | keepsmall: Bool[1, 0, 1, 0, 1, 0, 1, 0] */ TEST(TdistBitonic_UT, keepsmall_test7) { - size_t ts_depth = 3UL; - size_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; + size_t ts_depth = 3; + mpi_id_t ts_partner[] = {1, 0, 3, 2, 5, 4, 7, 6}; bool ts_expected[] = {1, 0, 1, 0, 1, 0, 1, 0}; - for (size_t node = 0 ; node < 8UL ; ++node ) { - EXPECT_EQ(ts_expected[node], keepsmall(node, ts_partner[node], ts_depth)); + for (mpi_id_t node = 0 ; node < 8 ; ++node ) { + EXPECT_EQ(ts_expected[node], keepSmall(node, ts_partner[node], ts_depth)); } } -TEST(TdistBitonic_UT, distbitonic_test1) { +TEST(TdistBitonic_UT, distBitonic_test1) { AllData_t ts_Data { Data_t (8), Data_t (8) }; @@ -338,7 +338,7 @@ TEST(TdistBitonic_UT, distbitonic_test1) { std::generate(v.begin(), v.end(), std::rand); } - distbitonic(2, ts_Data); + distBitonic(2, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) { @@ -348,7 +348,7 @@ TEST(TdistBitonic_UT, distbitonic_test1) { } } -TEST(TdistBitonic_UT, distbitonic_test2) { +TEST(TdistBitonic_UT, distBitonic_test2) { AllData_t ts_Data { Data_t (8), Data_t (8), Data_t (8), Data_t (8) }; @@ -358,7 +358,7 @@ TEST(TdistBitonic_UT, distbitonic_test2) { std::generate(v.begin(), v.end(), std::rand); } - distbitonic(4, ts_Data); + distBitonic(4, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) { @@ -368,7 +368,7 @@ TEST(TdistBitonic_UT, distbitonic_test2) { } } -TEST(TdistBitonic_UT, distbitonic_test3) { +TEST(TdistBitonic_UT, distBitonic_test3) { AllData_t ts_Data { Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32) @@ -379,7 +379,7 @@ TEST(TdistBitonic_UT, distbitonic_test3) { std::generate(v.begin(), v.end(), std::rand); } - distbitonic(8, ts_Data); + distBitonic(8, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) { diff --git a/homework_2/test/tests_Bubbletonic.cpp b/homework_2/test/tests_Bubbletonic.cpp index 21e19f4..2489c4a 100644 --- a/homework_2/test/tests_Bubbletonic.cpp +++ b/homework_2/test/tests_Bubbletonic.cpp @@ -18,7 +18,7 @@ /* ================================== ascending ================================== */ /* - * bool ascending(size_t node, size_t depth); + * bool ascending(mpi_id_t node, size_t depth); */ TEST(TdistBubbletonic_UT, ascending_Bubbletonic_test1) { EXPECT_EQ(ascending(0, 0), true); @@ -30,7 +30,7 @@ TEST(TdistBubbletonic_UT, ascending_Bubbletonic_test1) { EXPECT_EQ(ascending(6, 0), true); EXPECT_EQ(ascending(7, 0), false); - for (size_t node = 0 ; node < 256 ; ++node) { + for (mpi_id_t node = 0 ; node < 256 ; ++node) { EXPECT_EQ(ascending(node, 7), ((node % 2) ? false : true) ); } } @@ -39,52 +39,52 @@ TEST(TdistBubbletonic_UT, ascending_Bubbletonic_test1) { /* ================================== partner ================================== */ /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * step = 0 */ TEST(TdistBubbletonic_UT, partner_Bubbletonic_test1) { size_t ts_step = 0; - size_t ts_expected[] = {1, 0, 3, 2, 5, 4, 7, 6}; + mpi_id_t ts_expected[] = {1, 0, 3, 2, 5, 4, 7, 6}; - for (size_t node = 0 ; node < 8 ; ++node) { + for (mpi_id_t node = 0 ; node < 8 ; ++node) { EXPECT_EQ(partner(node, ts_step), ts_expected[node]); } } /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * step = 1 */ TEST(TdistBubbletonic_UT, partner_Bubbletonic_test2) { size_t ts_step = 1; - size_t ts_expected[] = {(size_t)-1, 2, 1, 4, 3, 6, 5, 8}; + mpi_id_t ts_expected[] = {(mpi_id_t)-1, 2, 1, 4, 3, 6, 5, 8}; - for (size_t node = 0 ; node < 8 ; ++node) { + for (mpi_id_t node = 0 ; node < 8 ; ++node) { EXPECT_EQ(partner(node, ts_step), ts_expected[node]); } } /* - * size_t partner(size_t node, size_t step); + * mpi_id_t partner(mpi_id_t node, size_t step); * various steps */ TEST(TdistBubbletonic_UT, partner_Bubbletonic_test3) { - size_t ts_even_expected[] = { + mpi_id_t ts_even_expected[] = { 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14 }; - size_t ts_odd_expected[] = { - (size_t)-1, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16 + mpi_id_t ts_odd_expected[] = { + (mpi_id_t)-1, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16 }; for (size_t step = 0 ; step < 32 ; ++step) { if (step % 2) { - for (size_t node = 0; node < 16; ++node) { + for (mpi_id_t node = 0; node < 16; ++node) { EXPECT_EQ(partner(node, step), ts_odd_expected[node]); } } else { - for (size_t node = 0; node < 16; ++node) { + for (mpi_id_t node = 0; node < 16; ++node) { EXPECT_EQ(partner(node, step), ts_even_expected[node]); } } @@ -92,36 +92,36 @@ TEST(TdistBubbletonic_UT, partner_Bubbletonic_test3) { } -/* ================================== keepsmall ================================== */ +/* ================================== keepSmall ================================== */ /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); * Assertion check */ TEST(TdistBubbletonic_UT, keepsmall_test1) { - ASSERT_DEATH(keepsmall(0, 0, 0), ""); - ASSERT_DEATH(keepsmall(1, 1, 42), ""); - ASSERT_DEATH(keepsmall(7, 7, 42), ""); + ASSERT_DEATH(keepSmall(0, 0, 0), ""); + ASSERT_DEATH(keepSmall(1, 1, 42), ""); + ASSERT_DEATH(keepSmall(7, 7, 42), ""); } /* - * bool keepsmall(size_t node, size_t partner, size_t depth); + * bool keepSmall(mpi_id_t node, mpi_id_t partner, size_t depth); */ TEST(TdistBubbletonic_UT, keepsmall_test2) { // Check various combinations - EXPECT_EQ(keepsmall(0, 1, 42), true); - EXPECT_EQ(keepsmall(0, 3, 42), true); - EXPECT_EQ(keepsmall(2, 1, 42), false); - EXPECT_EQ(keepsmall(7, 1, 42), false); - EXPECT_EQ(keepsmall(0, 1, 42), true); - EXPECT_EQ(keepsmall(7, 32,42), true); - EXPECT_EQ(keepsmall(7, 1, 42), false); - EXPECT_EQ(keepsmall(4, 0, 42), false); - EXPECT_EQ(keepsmall(4, 9, 42), true); + EXPECT_EQ(keepSmall(0, 1, 42), true); + EXPECT_EQ(keepSmall(0, 3, 42), true); + EXPECT_EQ(keepSmall(2, 1, 42), false); + EXPECT_EQ(keepSmall(7, 1, 42), false); + EXPECT_EQ(keepSmall(0, 1, 42), true); + EXPECT_EQ(keepSmall(7, 32, 42), true); + EXPECT_EQ(keepSmall(7, 1, 42), false); + EXPECT_EQ(keepSmall(4, 0, 42), false); + EXPECT_EQ(keepSmall(4, 9, 42), true); } -TEST(TdistBubbletonic_UT, distbubbletonic_test1) { +TEST(TdistBubbletonic_UT, distBubbletonic_test1) { AllData_t ts_Data { Data_t (8), Data_t (8) }; @@ -131,7 +131,7 @@ TEST(TdistBubbletonic_UT, distbubbletonic_test1) { std::generate(v.begin(), v.end(), std::rand); } - distbubbletonic(2, ts_Data); + distBubbletonic(2, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) { @@ -142,7 +142,7 @@ TEST(TdistBubbletonic_UT, distbubbletonic_test1) { } -TEST(TdistBubbletonic_UT, distbubbletonic_test2) { +TEST(TdistBubbletonic_UT, distBubbletonic_test2) { AllData_t ts_Data { Data_t (8), Data_t (8), Data_t (8), Data_t (8) }; @@ -152,7 +152,7 @@ TEST(TdistBubbletonic_UT, distbubbletonic_test2) { std::generate(v.begin(), v.end(), std::rand); } - distbubbletonic(4, ts_Data); + distBubbletonic(4, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) { @@ -162,7 +162,7 @@ TEST(TdistBubbletonic_UT, distbubbletonic_test2) { } } -TEST(TdistBubbletonic_UT, distbubbletonic_test3) { +TEST(TdistBubbletonic_UT, distBubbletonic_test3) { AllData_t ts_Data { Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32), Data_t (32) @@ -173,7 +173,7 @@ TEST(TdistBubbletonic_UT, distbubbletonic_test3) { std::generate(v.begin(), v.end(), std::rand); } - distbubbletonic(8, ts_Data); + distBubbletonic(8, ts_Data); auto max = std::numeric_limits::min(); for (auto& v : ts_Data) {