HW2: (WIP) Checkpoint with code re-arrangement and elbowSort

This commit is contained in:
2024-12-30 14:23:13 +02:00
parent 1d6b271d2a
commit 4d1d7502aa
11 changed files with 545 additions and 1193 deletions
-212
View File
@@ -1,212 +0,0 @@
/*!
* \file
* \brief Distributed bitonic implementation.
*
* \author
* Christos Choutouridis AEM:8997
* <cchoutou@ece.auth.gr>
*/
#include <vector>
#include <algorithm>
#include <cmath>
#if !defined DEBUG
#define NDEBUG
#endif
#include <cassert>
#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
*
* @param node The current node (MPI process)
* @return True if we need ascending configuration, false otherwise
*/
template <>
bool ascending<SortMode::Bubbletonic>(mpi_id_t node, [[maybe_unused]] size_t depth) noexcept {
return (node % 2) == 0;
}
/*!
* 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
*
* @param node The current node (MPI process)
* @param depth The total depth of the sorting network (same for each step for a given network)
*
* @return True if we need ascending configuration, false otherwise
*/
template <>
bool ascending<SortMode::Bitonic>(mpi_id_t node, size_t depth) noexcept {
return !(node & (1 << depth));
}
/*!
* Returns the node's partner for data exchange during the sorting network iterations
* of Bubbletonic
*
* @param node The current node
* @param step The step of the sorting network
* @return The node id of the partner for data exchange
*/
template <>
mpi_id_t partner<SortMode::Bubbletonic>(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;
}
/*!
* Returns the node's partner for data exchange during the sorting network iterations
* of Bitonic
*
* @param node The current node
* @param step The step of the sorting network
* @return The node id of the partner for data exchange
*/
template <>
mpi_id_t partner<SortMode::Bitonic>(mpi_id_t node, size_t step) noexcept {
return (node ^ (1 << step));
}
/*!
* Predicate to check if a node keeps the small numbers during the bubbletonic sort network exchange.
*
* @param node The node for which we check
* @param partner The partner of the data exchange
* @return True if the node should keep the small values, false otherwise
*/
template <>
bool keepSmall<SortMode::Bubbletonic>(mpi_id_t node, mpi_id_t partner, [[maybe_unused]] size_t depth) noexcept {
assert(node != partner);
return (node < partner);
}
/*!
* Predicate to check if a node keeps the small numbers during the bitonic sort network exchange.
*
* @param node The node for which we check
* @param partner The partner of the data exchange
* @param depth The total depth of the sorting network (same for each step for a given network)
* @return True if the node should keep the small values, false otherwise
*/
template <>
bool keepSmall<SortMode::Bitonic>(mpi_id_t node, mpi_id_t partner, size_t depth) noexcept {
assert(node != partner);
return ascending<SortMode::Bitonic>(node, depth) == (node < partner);
}
/*!
* Predicate to check if the node is active in the current iteration of the bubbletonic
* sort exchange.
*
* @param node The node to check
* @param nodes The total number of nodes
* @return True if the node is active, false otherwise
*/
bool isActive(mpi_id_t node, mpi_id_t nodes) noexcept {
return (node >= 0) && (node < (nodes-1));
}
void exchange(mpi_id_t node, mpi_id_t partner) {
assert(node != partner);
}
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]);
if (!keepsmall && data[node][i] < data[partner][i])
std::swap(data[node][i], data[partner][i]);
}
}
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<SortMode::Bubbletonic>(node, depth);
auto ks = keepSmall<SortMode::Bubbletonic>(node, part, 0);
if (isActive(node, nodes) && node < part) {
exchange(node, part);
minmax(data, node, part, ks);
// elbow-sort here
if (ascending<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
if (ascending<SortMode::Bubbletonic>(part, 0))
std::sort(data[part].begin(), data[part].end(), std::less<>());
else
std::sort(data[part].begin(), data[part].end(), std::greater<>());
}
}
}
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<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
}
for (size_t depth = 0; depth < P-1; ++depth) {
bubbletonic_network(data, P, depth);
}
// Invert the descending ones
for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
if (!ascending<SortMode::Bubbletonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
}
}
void bitonic_network(AllData_t& data, mpi_id_t nodes, size_t depth) {
for (size_t step = depth; step > 0;) {
--step;
for (mpi_id_t node = 0; node < nodes; ++node) { // Currently we do all nodes here!
auto part = partner<SortMode::Bitonic>(node, step);
auto ks = keepSmall<SortMode::Bitonic>(node, part, depth);
if (node < part) {
exchange(node, part);
minmax(data, node, part, ks);
}
}
}
}
void distBitonic(mpi_id_t P, AllData_t& data) {
auto p = static_cast<uint32_t>(std::log2(P));
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<SortMode::Bitonic>(node, 0))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
}
// Run through sort network using elbow-sort
for (size_t depth = 1; depth <= p; ++depth) {
bitonic_network(data, P, depth);
for (mpi_id_t node = 0 ; node < P ; ++node) { // Currently we do all nodes here!
// elbow-sort here
if (ascending<SortMode::Bitonic>(node, depth))
std::sort(data[node].begin(), data[node].end(), std::less<>());
else
std::sort(data[node].begin(), data[node].end(), std::greater<>());
}
}
}
+31
View File
@@ -0,0 +1,31 @@
/*!
* \file
* \brief Distributed sort implementation.
*
* \author
* Christos Choutouridis AEM:8997
* <cchoutou@ece.auth.gr>
*/
#if !defined DEBUG
#define NDEBUG
#endif
#include <cassert>
#include "utils.hpp"
#include "distsort.hpp"
/*!
* Predicate to check if the node is active in the current iteration of the bubbletonic
* sort exchange.
*
* @param node The node to check
* @param nodes The total number of nodes
* @return True if the node is active, false otherwise
*/
bool isActive(mpi_id_t node, size_t nodes) noexcept {
assert(nodes > 0);
return (node >= 0) && (node < (nodes-1));
}
+75 -39
View File
@@ -9,15 +9,19 @@
#include <exception>
#include <iostream>
#include <algorithm>
#include "utils.hpp"
#include "config.h"
#include "distsort.hpp"
// Global session data
session_t session;
MPI_t<> mpi;
session_t session;
MPI_t<> mpi;
distBuffer_t Data;
Log logger;
Timing timer;
/*!
* A small command line argument parser
@@ -30,29 +34,46 @@ bool get_options(int argc, char* argv[]){
for (int i=1 ; i<argc ; ++i) {
std::string arg(argv[i]); // get current argument
if (arg == "-x" || arg == "--xxxxx") {
if (i+2 < argc) {
// session.corpusMtxFile = std::string(argv[++i]);
// session.corpusDataSet = std::string(argv[++i]);
if (arg == "-q" || arg == "--array-size") {
if (i+1 < argc) {
session.arraySize = 1 << atoi(argv[++i]);
}
else
else {
status = false;
}
}
else if (arg == "-v" || arg == "--verbose")
else if (arg == "--ndebug") {
session.ndebug = true;
}
else if (arg == "-t" || arg == "--timing") {
session.timing = true;
}
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/distbubbletonic - A distributed bitonic sort\n\n";
std::cout << "distbitonic -q <> [--ndebug] [-v]\n";
std::cout << "distbitonic -h\n";
std::cout << "distbubbletonic -q <> [--ndebug] [-v]\n";
std::cout << "distbubbletonic -h\n";
std::cout << '\n';
std::cout << "Options:\n\n";
std::cout << " -q | --array-size <size>\n";
std::cout << " Selects the array size according to size = 2^q\n\n";
std::cout << " --ndebug\n";
std::cout << " Skip debug breakpoint when on debug build.\n\n";
std::cout << " -t | --timing\n";
std::cout << " Request timing measurements output to stdout.\n\n";
std::cout << " -v | --verbose\n";
std::cout << " Request a more verbose output to stdout.\n\n";
std::cout << " -h | --help\n";
std::cout << " Prints this and exit.\n\n";
std::cout << "Examples:\n\n";
std::cout << " ...Example case...:\n";
std::cout << " > distBitonic -x <xxxxx> \n\n";
std::cout << " mpirun -np 4 distbitonic -q 24\n";
std::cout << " Runs distbitonic in 4 MPI processes with 2^24 array points each\n\n";
std::cout << " mpirun -np 16 distbubbletonic -q 20\n";
std::cout << " Runs distbubbletonic in 16 MPI processes with 2^20 array points each\n\n";
exit(0);
}
@@ -66,7 +87,6 @@ bool get_options(int argc, char* argv[]){
}
#if !defined TESTING
int main(int argc, char* argv[]) try {
// Initialize MPI environment
@@ -76,34 +96,50 @@ int main(int argc, char* argv[]) try {
if (!get_options(argc, argv))
exit(1);
#if defined DEBUG
/*
* In case of a debug build we will wait here until sleep_wait
* will reset via debugger. In order to do that the user must attach
* debugger to all processes. For example:
* $> mpirun -np 2 ./<program path>
* $> ps aux | grep <program>
* $> gdb <program> <PID1>
* $> gdb <program> <PID2>
*/
#if defined TESTING
volatile bool sleep_wait = false;
#else
logger << "MPI environment initialized." <<
" Rank: " << mpi.rank() <<
" Size: " << mpi.size() <<
logger.endl;
#if defined DEBUG
#if defined TESTING
/*
* In case of a debug build we will wait here until sleep_wait
* will reset via debugger. In order to do that the user must attach
* debugger to all processes. For example:
* $> mpirun -np 2 ./<program path>
* $> ps aux | grep <program>
* $> gdb <program> <PID1>
* $> gdb <program> <PID2>
*/
volatile bool sleep_wait = false;
#else
volatile bool sleep_wait = true;
#endif
while (sleep_wait)
#endif
while (sleep_wait && !session.ndebug)
sleep(1);
#endif
logger << "Initialize local array of " << session.arraySize << " elements" << logger.endl;
std::srand(unsigned(std::time(nullptr)));
Data.resize(session.arraySize);
std::generate(Data.begin(), Data.end(), std::rand);
if (mpi.rank() == 0)
logger << "Starting distributed sorting ... ";
timer.start();
#if CODE_VERSION == BUBBLETONIC
distBubbletonic(Data, mpi.size());
#else
distBitonic (Data, mpi.size());
#endif
timer.stop();
if (mpi.rank() == 0)
logger << " Done." << logger.endl;
std::string timeMsg = "rank " + std::to_string(mpi.rank());
timer.print_dt(timeMsg.c_str());
// 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";
// distBitonic (2, Data);
// distBitonic (4, Data);
std::cout << "[Data]: Rank " << mpi.rank() << ": [" << (int)Data.front() << " .. " << (int)Data.back() << "]" << std::endl;
mpi.finalize();
return 0;
}