-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
206 lines (173 loc) · 5.77 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @file main.cpp
* @author Patrick Flick <[email protected]>
* @brief Implements functionality for benchmarking your sorting implementations.
*
* Copyright (c) 2016 Georgia Institute of Technology. All Rights Reserved.
*/
/*********************************************************************
* !! DO NOT CHANGE THIS FILE !! *
*********************************************************************/
#include <time.h> // for clock_gettime()
// for in/output
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
// for MPI
#include <mpi.h>
// own includes
#include "utils.h"
#include "radix_sort.h"
#ifdef USE_MYSTRUCT_OPT
#include "mystruct_opt.h"
#else
#include "mystruct.h"
#endif
// the executable name
std::string exe_name;
/**
* Prints the usage of the program.
*/
void print_usage()
{
std::cerr << "Usage: " << exe_name << " [options]" << std::endl;
std::cerr << " Options:" << std::endl;
std::cerr << " -n <n> Sets the total number of input elements. This number must be a multiple of the number of MPI processes." << std::endl;
std::cerr << " OR" << std::endl;
std::cerr << " -m <m> Sets the number of input elements per process." << std::endl;
std::cerr << " Optional:" << std::endl;
std::cerr << " -t Print timing results in tablular format: \"n\tp\ttime\"" << std::endl;
std::cerr << " Example:" << std::endl;
std::cerr << " " << exe_name << " -m 1000" << std::endl;
std::cerr << " Generates 1000 random elements on each process and then sorts them using parallel radix sort." << std::endl;
}
// enable time measurements on MAC OS
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
// timing function that works for both Linux and MAC OS
void my_gettime(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_MONOTONIC, ts);
#endif
}
int main(int argc, char *argv[])
{
// Init MPI
MPI_Init(&argc, &argv);
// set up MPI
int rank, p;
// get total size of processors and current rank
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
int n = -1;
int m = -1;
int k = -1;
bool tabular = false;
/***************************
* Parse input arguments *
***************************/
if (rank == 0) {
exe_name = argv[0];
// forget about first argument (which is the executable's name)
argc--;
argv++;
// parse optional parameters
while(argc > 0 && argv[0][0] == '-')
{
char option = argv[0][1];
switch (option)
{
case 'n':
// the next argument must be the number
argv++;
argc--;
n = atoi(argv[0]);
break;
case 'k':
// the next argument must be the number
argv++;
argc--;
k = atoi(argv[0]);
break;
case 't':
tabular = true;
break;
case 'm':
// the next argument must be the number
argv++;
argc--;
m = atoi(argv[0]);
break;
default:
print_usage();
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
// iterate to next argument
argv++;
argc--;
}
if (m < 1 && (n < p || n % p != 0)) {
std::cerr << "[ERROR] n must be a multiple of p" << std::endl;
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
if (m < 1) {
m = n / p;
} else {
n = m*p;
}
if (k < 1) {
k = 8;
}
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&m, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&k, 1, MPI_INT, 0, MPI_COMM_WORLD);
// seed the random generator
// NOTE: This is for random input generation.
srand(1337*rank);
/****************************
* Generate input locally *
****************************/
std::vector<MyStruct> input(m);
std::generate(input.begin(), input.end(), mystruct_rand);
// get input datatype
MPI_Datatype mystruct_dt = mystruct_get_mpi_type();
/*****************************************
* Parallel, distirbuted radix sorting *
*****************************************/
// Timing only on master node (and barrierized)
MPI_Barrier (MPI_COMM_WORLD);
// start timer
struct timespec t_start, t_end;
my_gettime(&t_start);
// actually sort
radix_sort(&input[0], &input[0] + m, &mystruct_key_access, mystruct_dt, MPI_COMM_WORLD, k);
MPI_Barrier (MPI_COMM_WORLD);
// get elapsed time in seconds
my_gettime(&t_end);
double time_ms = (t_end.tv_sec - t_start.tv_sec) * 1e+3
+ (double) (t_end.tv_nsec - t_start.tv_nsec) * 1e-6;
// Timing only on master node (and barrierized)
// output time
if (rank == 0) {
if (tabular) {
std::cout << n << "\t" << p << "\t" << time_ms << std::endl;
} else {
std::cout << "Time for sorting " << n << " elements: " << time_ms << " ms" << std::endl;
}
}
// finish up
MPI_Finalize();
return 0;
}