UnpackerTester.cu 11.8 KB
Newer Older
Ewan Barr's avatar
Ewan Barr committed
1
#include "psrdada_cpp/effelsberg/edd/test/UnpackerTester.cuh"
Ewan Barr's avatar
Ewan Barr committed
2
#include "psrdada_cpp/cuda_utils.hpp"
Ewan Barr's avatar
Ewan Barr committed
3
#include <random>
Ewan Barr's avatar
Ewan Barr committed
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

#define BSWAP64(x) ((0xFF00000000000000 & x) >> 56) | \
                   ((0x00FF000000000000 & x) >> 40) | \
                   ((0x0000FF0000000000 & x) >> 24) | \
                   ((0x000000FF00000000 & x) >>  8) | \
                   ((0x00000000FF000000 & x) <<  8) | \
                   ((0x0000000000FF0000 & x) << 24) | \
                   ((0x000000000000FF00 & x) << 40) | \
                   ((0x00000000000000FF & x) << 56)

namespace psrdada_cpp {
namespace effelsberg {
namespace edd {
namespace test {

UnpackerTester::UnpackerTester()
    : ::testing::Test()
    , _stream(0)
{

}

UnpackerTester::~UnpackerTester()
{

}

void UnpackerTester::SetUp()
{
    CUDA_ERROR_CHECK(cudaStreamCreate(&_stream));
}

void UnpackerTester::TearDown()
{
    CUDA_ERROR_CHECK(cudaStreamDestroy(_stream));
}

void UnpackerTester::unpacker_12_to_32_c_reference(
    InputType const& input,
    OutputType& output)
{
Ewan Barr's avatar
Ewan Barr committed
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
    uint64_t val, rest;
    assert(input.size() % 3 == 0 /*Input must be a multiple of 3 for 12-bit unpacking*/);
    output.reserve(input.size() * 64 / 12);
    for (std::size_t ii = 0; ii < input.size(); ii += 3)
    {
        val = be64toh(input[ii]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0xFFF0000000000000 & val) <<  0) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000FFF0000000000 & val) << 12) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000FFF0000000 & val) << 24) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000000FFF0000 & val) << 36) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000000000FFF0 & val) << 48) >> 52));
        rest = ( 0x000000000000000F & val) << 60; // 4 bits rest.
        val = be64toh(input[ii + 1]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xFF00000000000000 & val) >>  4) | rest) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00FFF00000000000 & val) <<  8) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000FFF00000000 & val) << 20) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000FFF00000 & val) << 32) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000000FFF00 & val) << 44) >> 52));
        rest = ( 0x00000000000000FF & val) << 56; // 8 bits rest.
        val = be64toh(input[ii + 2]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xF000000000000000 & val) >>  8) | rest) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0FFF000000000000 & val) <<  4) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000FFF000000000 & val) << 16) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000FFF000000 & val) << 28) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000000FFF000 & val) << 40) >> 52));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000000000FFF & val) << 52) >> 52));
    }
    assert(output.size() == input.size() * 64 / 12 /*Output is not the expected size*/);
}
Ewan Barr's avatar
Ewan Barr committed
90

Ewan Barr's avatar
Ewan Barr committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
void UnpackerTester::unpacker_10_to_32_c_reference(
    InputType const& input,
    OutputType& output)
{
    uint64_t val, rest;
    assert(input.size() % 5 == 0 /*Input must be a multiple of 5 for 10-bit unpacking*/);
    output.reserve(input.size() * 64 / 10);
    for (std::size_t ii = 0; ii < input.size(); ii += 5)
    {
        val = be64toh(input[ii]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0xFFC0000000000000 & val) <<  0) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x003FF00000000000 & val) << 10) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000FFC00000000 & val) << 20) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000003FF000000 & val) << 30) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000000FFC000 & val) << 40) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000000003FF0 & val) << 50) >> 54));
Tobias Winchen's avatar
Tobias Winchen committed
113
        rest =                   ( 0x000000000000000F & val) << 60; // 4 bits rest.
Ewan Barr's avatar
Ewan Barr committed
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
        val = be64toh(input[ii + 1]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xFC00000000000000 & val) >> 4) | rest) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x03FF000000000000 & val) <<  6) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000FFC000000000 & val) << 16) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000003FF0000000 & val) << 26) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000000FFC0000 & val) << 36) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000000003FF00 & val) << 46) >> 54));
        rest = ( 0x00000000000000FF & val) << 56; // 8 bits rest.
        val = be64toh(input[ii + 2]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xC000000000000000 & val) >> 8) | rest) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x3FF0000000000000 & val) <<  2) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000FFC0000000000 & val) << 12) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000003FF00000000 & val) << 22) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000FFC00000 & val) << 32) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000003FF000 & val) << 42) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000000000FFC & val) << 52) >> 54));
        rest = ( 0x0000000000000003 & val) << 62; // 2 bits rest.
        val = be64toh(input[ii + 3]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xFF00000000000000 & val) >> 2) | rest) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00FFC00000000000 & val) <<  8) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00003FF000000000 & val) << 18) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000FFC000000 & val) << 28) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0000000003FF0000 & val) << 38) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000000000FFC0 & val) << 48) >> 54));
        rest = ( 0x000000000000003F & val) << 58; // 6 bits rest.
        val = be64toh(input[ii + 4]);
        output.push_back(static_cast<float>(
            static_cast<int64_t>(((0xF000000000000000 & val) >> 6) | rest) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0FFC000000000000 & val) <<  4) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x0003FF0000000000 & val) << 14) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000FFC0000000 & val) << 24) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x000000003FF00000 & val) << 34) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000000FFC00 & val) << 44) >> 54));
        output.push_back(static_cast<float>(
            static_cast<int64_t>(( 0x00000000000003FF & val) << 54) >> 54));
    }
Ewan Barr's avatar
Ewan Barr committed
174
175
176
177
178
179
}

void UnpackerTester::unpacker_8_to_32_c_reference(
    InputType const& input,
    OutputType& output)
{
Ewan Barr's avatar
Ewan Barr committed
180
181
182
    output.reserve(input.size() * 8);
    for (uint64_t val: input)
    {
root's avatar
root committed
183
        val = be64toh(val);
Ewan Barr's avatar
Ewan Barr committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0xFF00000000000000 & val) <<  0) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x00FF000000000000 & val) <<  8) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x0000FF0000000000 & val) << 16) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x000000FF00000000 & val) << 24) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x00000000FF000000 & val) << 32) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x0000000000FF0000 & val) << 40) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x000000000000FF00 & val) << 48) >> 56));
        output.push_back(static_cast<float>(
            static_cast<int64_t>((0x00000000000000FF & val) << 56) >> 56));
    }
Ewan Barr's avatar
Ewan Barr committed
201
202
203
204
205
206
}

void UnpackerTester::compare_against_host(
    Unpacker::OutputType const& gpu_output,
    OutputType const& host_output)
{
Ewan Barr's avatar
Ewan Barr committed
207
    OutputType copy_from_gpu = gpu_output;
Tobias Winchen's avatar
Tobias Winchen committed
208
    cudaDeviceSynchronize();
Ewan Barr's avatar
Ewan Barr committed
209
210
211
    ASSERT_EQ(host_output.size(), copy_from_gpu.size());
    for (std::size_t ii = 0; ii < host_output.size(); ++ii)
    {
Tobias Winchen's avatar
Tobias Winchen committed
212
	    EXPECT_EQ(host_output[ii], copy_from_gpu[ii]) << " ii = " << ii;
Ewan Barr's avatar
Ewan Barr committed
213
    }
Ewan Barr's avatar
Ewan Barr committed
214
215
216
217
}

TEST_F(UnpackerTester, 12_bit_unpack_test)
{
Ewan Barr's avatar
Ewan Barr committed
218
219
220
221
    std::size_t n = 1024 * 3;
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,1<<31);
    InputType host_input(n);
222
    for (size_t ii = 0; ii < n; ++ii)
Ewan Barr's avatar
Ewan Barr committed
223
224
225
226
227
    {
        host_input[ii] = distribution(generator);
    }
    Unpacker::InputType gpu_input = host_input;
    Unpacker::OutputType gpu_output;
228
    gpu_output.resize(host_input.size() * sizeof(host_input[0]) * 8 / 12);
Ewan Barr's avatar
Ewan Barr committed
229
230
231
232
233
    OutputType host_output;
    Unpacker unpacker(_stream);
    unpacker.unpack<12>(gpu_input, gpu_output);
    unpacker_12_to_32_c_reference(host_input, host_output);
    compare_against_host(gpu_output, host_output);
Ewan Barr's avatar
Ewan Barr committed
234
235
236
237
}

TEST_F(UnpackerTester, 8_bit_unpack_test)
{
root's avatar
root committed
238
    std::size_t n = 512 * 8;
Ewan Barr's avatar
Ewan Barr committed
239
240
241
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,1<<31);
    InputType host_input(n);
242
    for (size_t ii = 0; ii < n; ++ii)
Ewan Barr's avatar
Ewan Barr committed
243
    {
Tobias Winchen's avatar
Tobias Winchen committed
244
        host_input[ii]  = distribution(generator);
Ewan Barr's avatar
Ewan Barr committed
245
246
247
    }
    Unpacker::InputType gpu_input = host_input;
    Unpacker::OutputType gpu_output;
248
    gpu_output.resize(host_input.size() * sizeof(host_input[0]) * 8 / 8);
Ewan Barr's avatar
Ewan Barr committed
249
250
251
252
253
    OutputType host_output;
    Unpacker unpacker(_stream);
    unpacker.unpack<8>(gpu_input, gpu_output);
    unpacker_8_to_32_c_reference(host_input, host_output);
    compare_against_host(gpu_output, host_output);
Ewan Barr's avatar
Ewan Barr committed
254
255
256
}


Tobias Winchen's avatar
Tobias Winchen committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
TEST_F(UnpackerTester, 10_bit_unpack_test)
{
    std::size_t n = 640;
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,1<<31);
    InputType host_input(n);
    for (size_t ii = 0; ii < n; ++ii)
    {
        host_input[ii] = distribution(generator);
    }
    Unpacker::InputType gpu_input = host_input;
    Unpacker::OutputType gpu_output;
    gpu_output.resize(host_input.size() * sizeof(host_input[0]) * 8 / 10);
    OutputType host_output;
    Unpacker unpacker(_stream);
    unpacker.unpack<10>(gpu_input, gpu_output);
    unpacker_10_to_32_c_reference(host_input, host_output);
    compare_against_host(gpu_output, host_output);
}




Ewan Barr's avatar
Ewan Barr committed
280
281
282
} //namespace test
} //namespace edd
} //namespace meerkat
root's avatar
root committed
283
} //namespace psrdada_cpp