HW2: Add an MPI wrapper for the basic functionality and update all types
This commit is contained in:
@@ -10,10 +10,15 @@
|
||||
#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
|
||||
@@ -22,7 +27,7 @@
|
||||
* @return True if we need ascending configuration, false otherwise
|
||||
*/
|
||||
template <>
|
||||
bool ascending<SortMode::Bubbletonic>(size_t node, [[maybe_unused]] size_t depth) noexcept {
|
||||
bool ascending<SortMode::Bubbletonic>(mpi_id_t node, [[maybe_unused]] size_t depth) noexcept {
|
||||
return (node % 2) == 0;
|
||||
}
|
||||
|
||||
@@ -36,7 +41,7 @@ bool ascending<SortMode::Bubbletonic>(size_t node, [[maybe_unused]] size_t depth
|
||||
* @return True if we need ascending configuration, false otherwise
|
||||
*/
|
||||
template <>
|
||||
bool ascending<SortMode::Bitonic>(size_t node, size_t depth) noexcept {
|
||||
bool ascending<SortMode::Bitonic>(mpi_id_t node, size_t depth) noexcept {
|
||||
return !(node & (1 << depth));
|
||||
}
|
||||
|
||||
@@ -49,7 +54,7 @@ bool ascending<SortMode::Bitonic>(size_t node, size_t depth) noexcept {
|
||||
* @return The node id of the partner for data exchange
|
||||
*/
|
||||
template <>
|
||||
size_t partner<SortMode::Bubbletonic>(size_t node, size_t step) noexcept {
|
||||
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;
|
||||
}
|
||||
@@ -63,7 +68,7 @@ size_t partner<SortMode::Bubbletonic>(size_t node, size_t step) noexcept {
|
||||
* @return The node id of the partner for data exchange
|
||||
*/
|
||||
template <>
|
||||
size_t partner<SortMode::Bitonic>(size_t node, size_t step) noexcept {
|
||||
mpi_id_t partner<SortMode::Bitonic>(mpi_id_t node, size_t step) noexcept {
|
||||
return (node ^ (1 << step));
|
||||
}
|
||||
|
||||
@@ -76,7 +81,7 @@ size_t partner<SortMode::Bitonic>(size_t node, size_t step) noexcept {
|
||||
* @return True if the node should keep the small values, false otherwise
|
||||
*/
|
||||
template <>
|
||||
bool keepsmall<SortMode::Bubbletonic>(size_t node, size_t partner, [[maybe_unused]] size_t depth) noexcept {
|
||||
bool keepSmall<SortMode::Bubbletonic>(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<SortMode::Bubbletonic>(size_t node, size_t partner, [[maybe_unuse
|
||||
* @return True if the node should keep the small values, false otherwise
|
||||
*/
|
||||
template <>
|
||||
bool keepsmall<SortMode::Bitonic>(size_t node, size_t partner, size_t depth) noexcept {
|
||||
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);
|
||||
}
|
||||
@@ -103,16 +108,16 @@ bool keepsmall<SortMode::Bitonic>(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) {
|
||||
|
||||
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<SortMode::Bubbletonic>(node, depth);
|
||||
auto ks = keepsmall<SortMode::Bubbletonic>(node, part, 0);
|
||||
auto ks = keepSmall<SortMode::Bubbletonic>(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<SortMode::Bubbletonic>(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<SortMode::Bubbletonic>(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<SortMode::Bitonic>(node, step);
|
||||
auto ks = keepsmall<SortMode::Bitonic>(node, part, depth);
|
||||
auto ks = keepSmall<SortMode::Bitonic>(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<uint32_t>(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<SortMode::Bitonic>(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<SortMode::Bitonic>(node, depth))
|
||||
std::sort(data[node].begin(), data[node].end(), std::less<>());
|
||||
|
||||
+20
-57
@@ -9,34 +9,14 @@
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
#include <algorithm> // rand/srand
|
||||
//#include <ctime> // rand/srand
|
||||
#if !defined TESTING
|
||||
#include <mpi.h>
|
||||
#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 <xxxxx> \n\n";
|
||||
std::cout << " > distBitonic -x <xxxxx> \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 <program> <PID2>
|
||||
*/
|
||||
#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<int *>(&mpi.world_size));
|
||||
|
||||
// Get the rank of the process
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, reinterpret_cast<int *>(&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);
|
||||
|
||||
// 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::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();
|
||||
// distBitonic (2, Data);
|
||||
// distBitonic (4, Data);
|
||||
|
||||
mpi.finalize();
|
||||
return 0;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
|
||||
Reference in New Issue
Block a user