-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_tests.cpp
164 lines (135 loc) · 4.03 KB
/
mpi_tests.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
/**
* @file mpi_tests.cpp
* @author Patrick Flick <[email protected]>
* @brief GTest Unit Tests for the parallel MPI code.
*
* Copyright (c) 2014 Georgia Institute of Technology. All Rights Reserved.
*/
/*
* Add your own test cases here. We will test your final submission using
* a more extensive tests suite. Make sure your code works for many different
* input cases.
*
* Note:
* The google test framework is configured, such that
* only errors from the processor with rank = 0 are shown.
*/
#include <mpi.h>
#include <gtest/gtest.h>
#include <vector>
#include <algorithm>
#include "radix_sort.h"
#include "utils.h"
#include "mystruct.h"
/*********************************************************************
* Add your own test cases here. *
*********************************************************************/
// Other test cases can include:
// - all elements are equal
// - elements are randomly picked
// - elements are already sorted (or sorted inversely)
/* testing radix sorting of unsigned integers */
unsigned int nokey_func(const unsigned int& x) {
return x;
}
template <typename T>
void test_sort_global(std::vector<T>& global_in, unsigned int (*key_func)(const T&), MPI_Datatype dt, unsigned int k) {
int rank, p;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
ASSERT_TRUE(global_in.size() % p == 0) << "input size has to be a mutliple of p";
// scatter data from rank 0 to all processors
std::vector<T> local_x = scatter(global_in, dt, MPI_COMM_WORLD);
// radix sort the input
radix_sort(&local_x[0], &local_x[0]+local_x.size(), key_func, dt, MPI_COMM_WORLD, k);
// gather sorted datat back for verification
std::vector<T> global_result = gather(local_x, dt, MPI_COMM_WORLD);
// verify results on root process
if (rank == 0) {
std::stable_sort(global_in.begin(), global_in.end());
EXPECT_EQ(global_in, global_result);
}
}
void test_rand_ints(int n) {
std::vector<unsigned int> x;
int rank, p;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (rank == 0) {
// sort with 10 elements per process
x.resize(n*p);
for (size_t i = 0; i < x.size(); ++i)
x[i] = rand() % n;
}
test_sort_global(x, &nokey_func, MPI_UNSIGNED, 4);
}
void test_rand_same(int n) {
std::vector<unsigned int> x;
int rank, p;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
int randval = rand() %n;
if (rank == 0) {
// sort with 10 elements per process
x.resize(n*p);
for (size_t i = 0; i < x.size(); ++i)
x[i] = randval;
}
test_sort_global(x, &nokey_func, MPI_UNSIGNED, 4);
}
TEST(MpiTest, Sort10rand) {
test_rand_ints(10);
}
TEST(MpiTest, Sort1000rand) {
test_rand_ints(1000);
}
TEST(MpiTest, Sort500rand) {
test_rand_ints(500);
}
TEST(MpiTest, Sort45rand) {
test_rand_ints(45);
}
TEST(MpiTest, SortSample) {
for(int i = 1; i < 25; i++) {
test_rand_ints(i);
}
}
TEST(MpiTest, Sort100krand) {
test_rand_ints(100000);
}
TEST(MpiTest, Sort10krand) {
test_rand_ints(10000);
}
TEST(MpiTest, Sort10kSame) {
test_rand_same(10000);
}
void test_rand_mystruct(int n) {
std::vector<MyStruct> x;
int rank, p;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (rank == 0) {
// sort with 10 elements per process
x.resize(n*p);
std::generate(x.begin(), x.end(), mystruct_rand);
}
MPI_Datatype dt = mystruct_get_mpi_type();
test_sort_global(x, &mystruct_key_access, dt, 8);
}
TEST(MpiTest, SortMyStruct100rand) {
test_rand_mystruct(15);
}
TEST(MpiTest, SortMyStruct100k) {
test_rand_mystruct(100000);
}
TEST(MpiTest, SortMyStructMill) {
test_rand_mystruct(1000000);
}
TEST(MpiTest, SortMyStruct500) {
test_rand_mystruct(500);
}
TEST(MpiTest, SortMyStructLoop) {
for(int i = 1; i < 100; i++) {
test_rand_mystruct(i);
}
}