-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitonic_sort.cpp
84 lines (76 loc) · 2.34 KB
/
bitonic_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define DEBUG_BITONIC
#include <boost/mpi.hpp>
//#include <diy/serialization.hpp>
#include "bitonic_sort.hpp"
#include <vector>
#include <random>
#include <sstream>
#include <algorithm>
typedef std::vector< int> Vector;
namespace mpi = boost::mpi;
template< typename Communicator, typename Data>
void gather_data( Communicator& world, Data& data, Data& gathered_data){
std::vector< Data > orig_data;
std::vector< Data > ordered_orig_data;
data.push_back( world.rank());
mpi::gather( world, data, orig_data, 0);
data.pop_back();
ordered_orig_data.resize( orig_data.size());
if( world.rank() == 0){
for( auto& p : orig_data){
int rank = p.back();
p.pop_back();
ordered_orig_data[ rank] = std::move( orig_data[ rank]);
}
for( auto& i : ordered_orig_data){
gathered_data.insert( gathered_data.end(), i.begin(), i.end());
}
}
}
template< typename Communicator, typename Data>
void generate_correct_answer( Communicator& world,
Data& data,
Data& correct_answer){
gather_data( world, data, correct_answer);
std::sort( correct_answer.begin(), correct_answer.end());
}
template< typename Communicator>
void sort_problem(Communicator& world, std::size_t n){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, .5e9);
std::vector< int> data( n);
for( auto& i : data){ i = dis(gen); }
distributed::bitonic_sort( world, data);
}
int main( int argc, char** argv){
MPI_Init( &argc, &argv);
mpi::environment environment( argc, argv);
mpi::communicator world;
mpi::timer t;
for(auto i = 4; i < 10; ++i){
t.restart();
sort_problem( world, std::pow(10, i));
double time = t.elapsed();
double max_time = 0;
mpi::reduce( world, time, max_time, mpi::maximum< double>(), 0);
if( world.rank() ==0){
std::cout << "Sorted: " << std::pow(10, i) << " numbers with: " << world.size() << " processors in: " << max_time << std::endl << std::flush;
}
}
//std::vector< int> computed_answer;
//gather_data( world, data, computed_answer);
//if(world.rank() == 0){
//std::cout << std::endl;
//if(computed_answer == correct_answer){ std::cout << "ANSWER IS RIGHT!" << std::endl;}
//else{
//std::cout << "ANSWER IS WRONG!" << std::endl << std::endl;
//for( auto & i : computed_answer){ std::cout << i << " "; }
//std::cout << std::endl;
//for( auto & i : correct_answer){ std::cout << i << " "; }
//std::cout << std::endl;
//
//}
//}
return 0;
}