forked from facebookarchive/fbcuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallSortTest.cpp
135 lines (105 loc) · 3.47 KB
/
SmallSortTest.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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "cuda/SmallSortTestBindings.cuh"
#include <algorithm>
#include <cuda_runtime.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <random>
#include <unordered_set>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
gflags::ParseCommandLineFlags(&argc, &argv, true);
auto ret = RUN_ALL_TESTS();
cudaDeviceReset(); // to push stats cleanly
return ret;
}
namespace facebook { namespace cuda {
namespace {
// Add in +/- inf, +/- 0, denorms
void addSpecialFloats(vector<float>& vals) {
// Add in +/- infinity, with duplicates
vals.push_back(numeric_limits<float>::infinity());
vals.push_back(numeric_limits<float>::infinity());
vals.push_back(-numeric_limits<float>::infinity());
vals.push_back(-numeric_limits<float>::infinity());
// Add in +/- zero, with duplicates
vals.push_back(0.0f);
vals.push_back(0.0f);
vals.push_back(-0.0f);
vals.push_back(-0.0f);
// Add in some denorm floats, with duplicates
vals.push_back(numeric_limits<float>::denorm_min() * 4.0f);
vals.push_back(numeric_limits<float>::denorm_min());
vals.push_back(numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min());
vals.push_back(-numeric_limits<float>::denorm_min() * 4.0f);
}
} // namespace
TEST(SmallSort, weird) {
vector<float> vals;
addSpecialFloats(vals);
vector<float> sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < 3; ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = sort(vals);
ASSERT_EQ(sorted.size(), out.size());
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
TEST(SmallSort, sortInRegisters) {
// Test sorting vectors of size 1 to 4 x warpSize, which is the
// maximum in-register size we support
for (int size = 1; size <= 4 * 32; ++size) {
vector<float> vals;
for (int i = 0; i < size; ++i) {
vals.push_back((float) i + 1);
}
vector<float> sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < 3; ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = sort(vals);
ASSERT_EQ(sorted.size(), out.size());
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j]);
}
}
}
}
TEST(SmallSort, sortIndicesInRegisters) {
// Test sorting vectors of size 1 to 4 x warpSize, which is the
// maximum in-register size we support
for (int size = 1; size <= 4 * 32; ++size) {
vector<float> vals;
for (int i = 0; i < size; ++i) {
vals.push_back((float) i);
}
vector<float> sorted = vals;
sort(sorted.begin(), sorted.end(), std::greater<float>());
for (int i = 0; i < 3; ++i) {
shuffle(vals.begin(), vals.end(), random_device());
auto out = sortWithIndices(vals);
ASSERT_EQ(sorted.size(), out.size());
for (int j = 0; j < out.size(); ++j) {
ASSERT_EQ(sorted[j], out[j].first);
int idx = out[j].second;
ASSERT_GE(idx, 0);
ASSERT_LT(idx, vals.size());
ASSERT_EQ(out[j].first, vals[idx]);
}
// Test for uniqueness of indices
unordered_set<int> indices;
for (const auto p : out) {
ASSERT_FALSE(indices.count(p.second));
indices.emplace(p.second);
}
}
}
}
} }