GatedSpectrometerTest.cu 19.1 KB
Newer Older
Tobias Winchen's avatar
Tobias Winchen committed
1
#include "psrdada_cpp/effelsberg/edd/DadaBufferLayout.hpp"
2
3
#include "psrdada_cpp/effelsberg/edd/GatedSpectrometer.cuh"

Tobias Winchen's avatar
Tobias Winchen committed
4
#include "psrdada_cpp/dada_db.hpp"
5
6
7
8
9
10
11
#include "psrdada_cpp/dada_null_sink.hpp"
#include "psrdada_cpp/multilog.hpp"
#include "gtest/gtest.h"

#include "thrust/device_vector.h"
#include "thrust/extrema.h"

Tobias Winchen's avatar
Tobias Winchen committed
12
TEST(GatedSpectrometer, BitManipulationMacros) {
13
  for (int i = 0; i < 64; i++) {
14
    uint64_t v = 0;
15
16
17
18
19
20
21
22
23
24
25
    SET_BIT(v, i);

    for (int j = 0; j < 64; j++) {
      if (j == i)
        EXPECT_EQ(TEST_BIT(v, j), 1);
      else
        EXPECT_EQ(TEST_BIT(v, j), 0);
    }
  }
}

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

TEST(GatedSpectrometer, stokes_IQUV)
{
    float I,Q,U,V;
    // No field
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){0.0f,0.0f}, (float2){0.0f,0.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 0);
    EXPECT_FLOAT_EQ(Q, 0);
    EXPECT_FLOAT_EQ(U, 0);
    EXPECT_FLOAT_EQ(V, 0);

    // For p1 = Ex, p2 = Ey
    // horizontal polarized
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){1.0f,0.0f}, (float2){0.0f,0.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, 1);
    EXPECT_FLOAT_EQ(U, 0);
    EXPECT_FLOAT_EQ(V, 0);

    // vertical polarized
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){0.0f,0.0f}, (float2){1.0f,0.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, -1);
    EXPECT_FLOAT_EQ(U, 0);
    EXPECT_FLOAT_EQ(V, 0);

    //linear +45 deg.
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){1.0f/std::sqrt(2),0.0f}, (float2){1.0f/std::sqrt(2),0.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, 0);
    EXPECT_FLOAT_EQ(U, 1);
    EXPECT_FLOAT_EQ(V, 0);

    //linear -45 deg.
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){-1.0f/std::sqrt(2),0.0f}, (float2){1.0f/std::sqrt(2),0.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, 0);
    EXPECT_FLOAT_EQ(U, -1);
    EXPECT_FLOAT_EQ(V, 0);

    //left circular
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){.0f,1.0f/std::sqrt(2)}, (float2){1.0f/std::sqrt(2),.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, 0);
    EXPECT_FLOAT_EQ(U, 0);
    EXPECT_FLOAT_EQ(V, -1);

    // right circular
    psrdada_cpp::effelsberg::edd::stokes_IQUV((float2){.0f,-1.0f/std::sqrt(2)}, (float2){1.0f/std::sqrt(2),.0f}, I, Q, U, V);
    EXPECT_FLOAT_EQ(I, 1);
    EXPECT_FLOAT_EQ(Q, 0);
    EXPECT_FLOAT_EQ(U, 0);
    EXPECT_FLOAT_EQ(V, 1);
}


Tobias Winchen's avatar
Tobias Winchen committed
82

Tobias Winchen's avatar
Tobias Winchen committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
void testStokesAccumulateParam(size_t nchans, size_t naccumulate){
    // Test the stokes accumulate kernel for different channel / accumualte
    // numbers

    CUDA_ERROR_CHECK(cudaDeviceSynchronize());

    thrust::device_vector<float2> P0(nchans * naccumulate);
    thrust::device_vector<float2> P1(nchans * naccumulate);
    thrust::fill(P0.begin(), P0.end(), (float2){0, 0});
    thrust::fill(P1.begin(), P1.end(), (float2){0, 0});
    thrust::device_vector<float> I(nchans);
    thrust::device_vector<float> Q(nchans);
    thrust::device_vector<float> U(nchans);
    thrust::device_vector<float> V(nchans);
    thrust::fill(I.begin(), I.end(), 0);
    thrust::fill(Q.begin(), Q.end(), 0);
    thrust::fill(U.begin(), U.end(), 0);
    thrust::fill(V.begin(), V.end(), 0);

    // This channel should be left circular polarized
    size_t idx0 = 23;
104
    for (size_t k = 0; k< naccumulate; k++)
Tobias Winchen's avatar
Tobias Winchen committed
105
106
107
108
109
    {
        size_t idx = idx0 + k * nchans;
        P0[idx] = (float2){0.0f, 1.0f/std::sqrt(2)};
        P1[idx] = (float2){1.0f/std::sqrt(2),0.0f};
    }
Tobias Winchen's avatar
Tobias Winchen committed
110

Tobias Winchen's avatar
Tobias Winchen committed
111
112
113
114
115
116
117
118
119
120
121
122
123
    psrdada_cpp::effelsberg::edd::stokes_accumulate<<<1024, 1024>>>(
          thrust::raw_pointer_cast(P0.data()),
          thrust::raw_pointer_cast(P1.data()),
          thrust::raw_pointer_cast(I.data()),
          thrust::raw_pointer_cast(Q.data()),
          thrust::raw_pointer_cast(U.data()),
          thrust::raw_pointer_cast(V.data()),
          nchans,
          naccumulate
            );

    CUDA_ERROR_CHECK(cudaDeviceSynchronize());
    thrust::pair<thrust::device_vector<float>::iterator, thrust::device_vector<float>::iterator> minmax;
Tobias Winchen's avatar
Tobias Winchen committed
124

Tobias Winchen's avatar
Tobias Winchen committed
125
126
127
128
129
130
131
132
133
134
135
    minmax = thrust::minmax_element(I.begin(), I.end());
    EXPECT_FLOAT_EQ(*minmax.first, 0);
    EXPECT_FLOAT_EQ(*minmax.second, naccumulate);

    minmax = thrust::minmax_element(Q.begin(), Q.end());
    EXPECT_FLOAT_EQ(*minmax.first, 0);
    EXPECT_FLOAT_EQ(*minmax.second, 0);

    minmax = thrust::minmax_element(U.begin(), U.end());
    EXPECT_FLOAT_EQ(*minmax.first, 0);
    EXPECT_FLOAT_EQ(*minmax.second, 0);
136

Tobias Winchen's avatar
Tobias Winchen committed
137
138
139
140
    minmax = thrust::minmax_element(V.begin(), V.end());
    EXPECT_FLOAT_EQ(*minmax.first, -1. * naccumulate);
    EXPECT_FLOAT_EQ(*minmax.second, 0);
};
Tobias Winchen's avatar
Tobias Winchen committed
141

Tobias Winchen's avatar
Tobias Winchen committed
142
143
144
145
146
147
148

TEST(GatedSpectrometer, stokes_accumulate)
{
    testStokesAccumulateParam(8 * 1024 * 1024 + 1, 5);
    testStokesAccumulateParam(8 * 1024 * 1024 + 1, 32);
    testStokesAccumulateParam(1024 + 1, 5);
    testStokesAccumulateParam(1024 + 1, 64);
149
150
}

151

152

Tobias Winchen's avatar
Tobias Winchen committed
153

154
155
TEST(GatedSpectrometer, GatingKernel)
{
156
157
  const size_t blockSize = 1024;
  const size_t nBlocks = 16 * 1024;
158
159
160

  thrust::device_vector<float> G0(blockSize * nBlocks);
  thrust::device_vector<float> G1(blockSize * nBlocks);
161
  thrust::device_vector<uint64_t> _sideChannelData(nBlocks);
162
163
164
165
  thrust::device_vector<psrdada_cpp::effelsberg::edd::uint64_cu> _nG0(nBlocks);
  thrust::device_vector<psrdada_cpp::effelsberg::edd::uint64_cu> _nG1(nBlocks);
  thrust::device_vector<float> baseLineG0(1);
  thrust::device_vector<float> baseLineG1(1);
166

167
168
  thrust::device_vector<float> baseLineG0_update(1);
  thrust::device_vector<float> baseLineG1_update(1);
169
170
171
172
173
174
  thrust::fill(G0.begin(), G0.end(), 42);
  thrust::fill(G1.begin(), G1.end(), 23);
  thrust::fill(_sideChannelData.begin(), _sideChannelData.end(), 0);

  // everything to G0
  {
175
176
    thrust::fill(_nG0.begin(), _nG0.end(), 0);
    thrust::fill(_nG1.begin(), _nG1.end(), 0);
177
178
179
180
181
    baseLineG0[0] = -3;
    baseLineG1[0] = -4;
    baseLineG0_update[0] = 0;
    baseLineG1_update[0] = 0;

182
183
    const uint64_t *sideCD =
        (uint64_t *)(thrust::raw_pointer_cast(_sideChannelData.data()));
184
    psrdada_cpp::effelsberg::edd::gating<<<1024 , 1024>>>(
185
186
          thrust::raw_pointer_cast(G0.data()),
          thrust::raw_pointer_cast(G1.data()), sideCD,
187
          G0.size(), blockSize, 0, 1,
188
189
190
          0,
          thrust::raw_pointer_cast(baseLineG0.data()),
          thrust::raw_pointer_cast(baseLineG1.data()),
191
192
          thrust::raw_pointer_cast(baseLineG0_update.data()),
          thrust::raw_pointer_cast(baseLineG1_update.data()),
193
194
195
          thrust::raw_pointer_cast(_nG0.data()),
          thrust::raw_pointer_cast(_nG1.data())
          );
196

197
198
199
200
201
202
    thrust::pair<thrust::device_vector<float>::iterator, thrust::device_vector<float>::iterator> minmax;
    minmax = thrust::minmax_element(G0.begin(), G0.end());
    EXPECT_EQ(*minmax.first, 42);
    EXPECT_EQ(*minmax.second, 42);

    minmax = thrust::minmax_element(G1.begin(), G1.end());
203
204
    EXPECT_EQ(*minmax.first, -4.);
    EXPECT_EQ(*minmax.second, -4.);
205
206

    EXPECT_EQ(_nG0[0], G0.size());
207
    EXPECT_EQ(_nG1[0], 0u);
208

209
210
    EXPECT_FLOAT_EQ(42.f, baseLineG0_update[0] / (_nG0[0] + 1E-121));
    EXPECT_FLOAT_EQ(0.f, baseLineG1_update[0] / (_nG1[0] + 1E-121));
211
212
  }

Tobias Winchen's avatar
Tobias Winchen committed
213
  // everything to G1 // with baseline -5
214
  {
215
216
    thrust::fill(_nG0.begin(), _nG0.end(), 0);
    thrust::fill(_nG1.begin(), _nG1.end(), 0);
217
218
219
220
221
    baseLineG0[0] = 5.;
    baseLineG1[0] = -2;
    baseLineG0_update[0] = 0;
    baseLineG1_update[0] = 0;

222
    thrust::fill(_sideChannelData.begin(), _sideChannelData.end(), 1L);
223
224
    const uint64_t *sideCD =
        (uint64_t *)(thrust::raw_pointer_cast(_sideChannelData.data()));
225
226
227
    psrdada_cpp::effelsberg::edd::gating<<<1024, 1024>>>(
          thrust::raw_pointer_cast(G0.data()),
          thrust::raw_pointer_cast(G1.data()), sideCD,
228
          G0.size(), blockSize, 0, 1,
229
230
231
          0,
          thrust::raw_pointer_cast(baseLineG0.data()),
          thrust::raw_pointer_cast(baseLineG1.data()),
232
233
          thrust::raw_pointer_cast(baseLineG0_update.data()),
          thrust::raw_pointer_cast(baseLineG1_update.data()),
234
235
236
          thrust::raw_pointer_cast(_nG0.data()),
          thrust::raw_pointer_cast(_nG1.data())
          );
237
238
    thrust::pair<thrust::device_vector<float>::iterator, thrust::device_vector<float>::iterator> minmax;
    minmax = thrust::minmax_element(G0.begin(), G0.end());
239
240
    EXPECT_EQ(*minmax.first, 5.);
    EXPECT_EQ(*minmax.second, 5.);
241
242
243
244

    minmax = thrust::minmax_element(G1.begin(), G1.end());
    EXPECT_EQ(*minmax.first, 42);
    EXPECT_EQ(*minmax.second, 42);
245

246
247
    EXPECT_EQ(_nG0[0], 0u);
    EXPECT_EQ(_nG1[0], G1.size());
248
249
250

    EXPECT_FLOAT_EQ(0.f, baseLineG0_update[0] / (_nG0[0] + 1E-121));
    EXPECT_FLOAT_EQ(42.f, baseLineG1_update[0] / (_nG1[0] + 1E-121));
251
252
253
  }
}

Tobias Winchen's avatar
Tobias Winchen committed
254
255
256
257
258
259
260
261
262
263
TEST(GatedSpectrometer, array_sum) {

  const size_t NBLOCKS = 16 * 32;
  const size_t NTHREADS = 1024;

  size_t inputLength = 1 << 22 + 1 ;
  size_t dataLength = inputLength;
  ////zero pad input array
  //if (inputLength % (2 * NTHREADS) != 0)
  //  dataLength = (inputLength / (2 * NTHREADS) + 1) * 2 * NTHREADS;
Tobias Winchen's avatar
Tobias Winchen committed
264
  thrust::device_vector<float> data(dataLength);
Tobias Winchen's avatar
Tobias Winchen committed
265
266
267
268
269
270
271
272
273
274
275
  thrust::fill(data.begin(), data.begin() + inputLength, 1);
  //thrust::fill(data.begin() + inputLength, data.end(), 0);
  thrust::device_vector<float> blr(NTHREADS * 2);

  thrust::fill(blr.begin(), blr.end(), 0);

  psrdada_cpp::effelsberg::edd::array_sum<<<NBLOCKS, NTHREADS, NTHREADS* sizeof(float)>>>(thrust::raw_pointer_cast(data.data()), data.size(), thrust::raw_pointer_cast(blr.data()));
  psrdada_cpp::effelsberg::edd::array_sum<<<1, NTHREADS, NTHREADS* sizeof(float)>>>(thrust::raw_pointer_cast(blr.data()), blr.size(), thrust::raw_pointer_cast(blr.data()));

  EXPECT_EQ(size_t(blr[0]), inputLength);
}
Tobias Winchen's avatar
Tobias Winchen committed
276
277


278
class GatedTestSinkSinglePol{
Tobias Winchen's avatar
Tobias Winchen committed
279
280
281
282
283
284
    private:
        size_t fft_length;
        size_t call_count;
        size_t nHeaps;
        size_t naccumulate;
    public:
285
        GatedTestSinkSinglePol(size_t fft_length, size_t nHeaps, size_t naccumulate): fft_length(fft_length), nHeaps(nHeaps), naccumulate(naccumulate), call_count(0) {};
Tobias Winchen's avatar
Tobias Winchen committed
286
287
288
289
290
    // Test the correctness of output of the processing test
        void init(psrdada_cpp::RawBytes&){
        };
        bool operator()(psrdada_cpp::RawBytes& buf)
        {
291
            const size_t number_of_sc_items = 2;
Tobias Winchen's avatar
Tobias Winchen committed
292
293
294
295
296
297
298
            const size_t nchans = fft_length / 2 + 1;
            EXPECT_EQ(buf.used_bytes(), (32 / 8 * nchans + number_of_sc_items* 64 / 8) * 2);

            float *G0 = reinterpret_cast<float*>(buf.ptr());
            float *G1 = reinterpret_cast<float*>(buf.ptr() + nchans * 32 /8);

            // Expected half number of samples per gate
299
            uint64_t *S = reinterpret_cast<uint64_t*>(buf.ptr() + 2 *nchans * 32 /8);
Tobias Winchen's avatar
Tobias Winchen committed
300
            EXPECT_EQ(S[0], nHeaps * 4096 / 2);
301
            EXPECT_EQ(S[2], nHeaps * 4096 / 2);
Tobias Winchen's avatar
Tobias Winchen committed
302

303
            // Correct number of overflowed samples
304
305
            EXPECT_EQ(S[1], 27uL);
            EXPECT_EQ(S[3], 23uL);  // First heap has 23 and bit set, thus G1
Tobias Winchen's avatar
Tobias Winchen committed
306
307
308
309
310
311
312

            call_count ++;
            return false;
        };

};

313
TEST(GatedSpectrometer, processingSinglePol)
Tobias Winchen's avatar
Tobias Winchen committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
{
    const size_t nbits = 8;
    const size_t nHeaps = 1024;
    const size_t fft_length = 1024 * 64;
    const size_t naccumulate = 4096 * nHeaps / fft_length;

    const size_t heapSize = 4096 * nbits / 8;
    const size_t inputBufferSize = nHeaps * (heapSize + 64 / 8);

    psrdada_cpp::DadaDB idbuffer(5, inputBufferSize, 1, 4096);
    idbuffer.create();

    psrdada_cpp::effelsberg::edd::DadaBufferLayout bufferLayout(idbuffer.key(), heapSize, 1);

328
    GatedTestSinkSinglePol sink(fft_length, nHeaps, naccumulate);
Tobias Winchen's avatar
Tobias Winchen committed
329
    psrdada_cpp::effelsberg::edd::GatedSpectrometer<
330
        GatedTestSinkSinglePol,
Tobias Winchen's avatar
Tobias Winchen committed
331
332
333
334
335
336
337
338
339
340
341
342
343
        psrdada_cpp::effelsberg::edd::SinglePolarizationInput,
        psrdada_cpp::effelsberg::edd::GatedPowerSpectrumOutput>
            spectrometer(bufferLayout,
                    0, 0,
                    fft_length,
                    naccumulate, nbits,
                    sink);

    char  *raw_buffer = new char[inputBufferSize];

    psrdada_cpp::RawBytes buff(raw_buffer, inputBufferSize, inputBufferSize);
    EXPECT_NO_THROW(spectrometer.init(buff));

344
345
    //// fill sci data
    uint64_t* sc_items = reinterpret_cast<uint64_t*>(raw_buffer + nHeaps * 4096);
346
    for (size_t i = 0; i < nHeaps; i+=2)
Tobias Winchen's avatar
Tobias Winchen committed
347
    {
348
349
350
        sc_items[i] = 0;
        sc_items[i+1] = 0;
        SET_BIT(sc_items[i], 0);
Tobias Winchen's avatar
Tobias Winchen committed
351
    }
352
353
    sc_items[0] |= (23UL) << 32;
    sc_items[1] |= (27UL) << 32;
Tobias Winchen's avatar
Tobias Winchen committed
354

355
    for (int i = 0; i < 12; i++)
Tobias Winchen's avatar
Tobias Winchen committed
356
    {
357
        EXPECT_NO_THROW(spectrometer(buff));
Tobias Winchen's avatar
Tobias Winchen committed
358
    }
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

    delete [] raw_buffer;
}


class GatedTestSinkFullStokes{
    private:
        size_t fft_length;
        size_t call_count;
        size_t nHeaps;
        size_t naccumulate;
    public:
        GatedTestSinkFullStokes(size_t fft_length, size_t nHeaps, size_t naccumulate): fft_length(fft_length), nHeaps(nHeaps), naccumulate(naccumulate), call_count(0) {};
    // Test the correctness of output of the processing test
        void init(psrdada_cpp::RawBytes&){
        };
        bool operator()(psrdada_cpp::RawBytes& buf)
        {
            const size_t number_of_sc_items = 2;
            const size_t nchans = fft_length / 2 + 1;
            EXPECT_EQ(buf.used_bytes(), (32 / 8 * nchans + number_of_sc_items* 64 / 8) * 2 * 4); // 4 Spectra (IQUV), 2 Gates

            // Expected half number of samples per gate
            for(int i =0; i <4; i++)
            {
Tobias Winchen's avatar
Tobias Winchen committed
384
385
386
                uint64_t *S = reinterpret_cast<uint64_t*>(buf.ptr() + 8 *nchans * 32 /8 + i * 4 *sizeof(size_t));
                EXPECT_EQ(S[0], nHeaps * 4096) << "G0, S" << i; // expect samples from two polarizations
                EXPECT_EQ(S[2], nHeaps * 4096) << "G1, S" << i;
387
388

                // Correct number of overflowed samples
389
390
                EXPECT_EQ(S[1], uint64_t(27 + 7)) << "G0, S" << i;;
                EXPECT_EQ(S[3], uint64_t(23 + 3)) << "G1, S" << i;;  // First heap has 23+3 and bit set, thus G1
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
            }

            call_count ++;
            return false;
        };

};




TEST(GatedSpectrometer, processingFullStokes)
{
    const size_t nbits = 8;
    const size_t nHeaps = 1024;
    const size_t fft_length = 1024 * 64;
    const size_t naccumulate = 4096 * nHeaps / fft_length;

    const size_t heapSize = 4096 * nbits / 8;
    const size_t inputBufferSize = 2 * nHeaps * (heapSize + 64 / 8) ;

    psrdada_cpp::DadaDB idbuffer(5, inputBufferSize, 1, 4096);
    idbuffer.create();

    psrdada_cpp::effelsberg::edd::DadaBufferLayout bufferLayout(idbuffer.key(), heapSize, 1);

    GatedTestSinkFullStokes sink(fft_length, nHeaps, naccumulate);
    psrdada_cpp::effelsberg::edd::GatedSpectrometer<
        GatedTestSinkFullStokes,
        psrdada_cpp::effelsberg::edd::DualPolarizationInput,
        psrdada_cpp::effelsberg::edd::GatedFullStokesOutput>
            spectrometer(bufferLayout,
                    0, 0,
                    fft_length,
                    naccumulate, nbits,
                    sink);

    char  *raw_buffer = new char[inputBufferSize];

    psrdada_cpp::RawBytes buff(raw_buffer, inputBufferSize, inputBufferSize);
    EXPECT_NO_THROW(spectrometer.init(buff));

Tobias Winchen's avatar
Tobias Winchen committed
433
    //// fill sci data
434
    uint64_t* sc_items = reinterpret_cast<uint64_t*>(raw_buffer + 2*nHeaps * 4096);
435
    for (size_t i = 0; i < 2 * nHeaps; i+=4)
Tobias Winchen's avatar
Tobias Winchen committed
436
    {
Tobias Winchen's avatar
Tobias Winchen committed
437
438
439
440
        sc_items[i] = 0uL;
        sc_items[i+1] = 0uL;
        sc_items[i+2] = 0uL;
        sc_items[i+3] = 0uL;
Tobias Winchen's avatar
Tobias Winchen committed
441
        SET_BIT(sc_items[i], 0);
442
        SET_BIT(sc_items[i + 1], 0);
Tobias Winchen's avatar
Tobias Winchen committed
443
    }
444
445
    sc_items[0] |= (23UL) << 32;
    sc_items[1] |= (3UL) << 32;
Tobias Winchen's avatar
Tobias Winchen committed
446

447
448
    sc_items[2] |= (27UL) << 32;
    sc_items[3] |= (7UL) << 32;
Tobias Winchen's avatar
Tobias Winchen committed
449
450


451
    for (int i = 0; i < 12; i++)
Tobias Winchen's avatar
Tobias Winchen committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    {
        EXPECT_NO_THROW(spectrometer(buff));
    }

    delete [] raw_buffer;
}









Tobias Winchen's avatar
Tobias Winchen committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
struct gated_params
{
    std::size_t fft_length;
    std::size_t naccumulate;
    std::size_t nbits;
    std::size_t nHeaps;
    std::string msg;
};


class ExecutionTests: public testing::TestWithParam<gated_params> {
// Test that the spectrometers execute without error in certain parameter
// settings

};


TEST_P(ExecutionTests, SinglePolOutput)
{
    gated_params params = GetParam();

    const size_t heapSize = 4096 * params.nbits / 8;
    const size_t inputBufferSize = params.nHeaps * (heapSize + 64 / 8);

    psrdada_cpp::DadaDB buffer(5, inputBufferSize, 1, 4096);
    buffer.create();

    psrdada_cpp::effelsberg::edd::DadaBufferLayout bufferLayout(buffer.key(), heapSize, 1);
495
496
    // Test buffer consistency with input parameters
    EXPECT_EQ(bufferLayout.getNHeaps(), params.nHeaps);
Tobias Winchen's avatar
Tobias Winchen committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510

    psrdada_cpp::NullSink sink;

    psrdada_cpp::effelsberg::edd::GatedSpectrometer<
        psrdada_cpp::NullSink,
        psrdada_cpp::effelsberg::edd::SinglePolarizationInput,
        psrdada_cpp::effelsberg::edd::GatedPowerSpectrumOutput>
            spectrometer(bufferLayout,
                    0, 0,
                     params.fft_length,
                    params.naccumulate, params.nbits,
                    sink);

    char  *raw_buffer = new char[inputBufferSize];
511
    memset(raw_buffer, 0, inputBufferSize);
Tobias Winchen's avatar
Tobias Winchen committed
512

Tobias Winchen's avatar
Tobias Winchen committed
513
    psrdada_cpp::RawBytes buff(raw_buffer, inputBufferSize, inputBufferSize);
Tobias Winchen's avatar
Tobias Winchen committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

    EXPECT_NO_THROW(spectrometer.init(buff)) << params.msg;
    for (int i = 0; i < 5; i++)
    {
        EXPECT_NO_THROW(spectrometer(buff)) << params.msg;
    }

    delete [] raw_buffer;
}


TEST_P(ExecutionTests, FullStokesOutput)
{
    gated_params params = GetParam();

    const size_t heapSize = 4096 * params.nbits / 8;
    const size_t inputBufferSize = params.nHeaps * (heapSize + 64 / 8);

    psrdada_cpp::DadaDB buffer(5, inputBufferSize, 1, 4096);
    buffer.create();

    psrdada_cpp::effelsberg::edd::DadaBufferLayout bufferLayout(buffer.key(), heapSize, 1);
536
537
    // Test buffer consistency with input parameters
    EXPECT_EQ(bufferLayout.getNHeaps(), params.nHeaps);
Tobias Winchen's avatar
Tobias Winchen committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551

    psrdada_cpp::NullSink sink;

    psrdada_cpp::effelsberg::edd::GatedSpectrometer<
        psrdada_cpp::NullSink,
        psrdada_cpp::effelsberg::edd::DualPolarizationInput,
        psrdada_cpp::effelsberg::edd::GatedFullStokesOutput>
            spectrometer(bufferLayout,
                    0, 0,
                     params.fft_length,
                    params.naccumulate, params.nbits,
                    sink);

    char  *raw_buffer = new char[inputBufferSize];
552
    memset(raw_buffer, 0, inputBufferSize);
Tobias Winchen's avatar
Tobias Winchen committed
553

Tobias Winchen's avatar
Tobias Winchen committed
554
    psrdada_cpp::RawBytes buff(raw_buffer, inputBufferSize, inputBufferSize);
Tobias Winchen's avatar
Tobias Winchen committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568

    EXPECT_NO_THROW(spectrometer.init(buff)) << params.msg;
    for (int i = 0; i < 5; i++)
    {
        EXPECT_NO_THROW(spectrometer(buff)) << params.msg;
    }

    delete [] raw_buffer;
}


INSTANTIATE_TEST_CASE_P (GatedSpectrometer,
        ExecutionTests,
        testing::Values(
569
570
571
572
573
574
575
576
577

          // fft_length; naccumulate; nbits; nHeaps; msg;
          gated_params({2*1024, 2,  8, 1024, "1k Channel:  8 Bit"}),
          gated_params({2*1024, 2, 10, 1024, "1k Channel: 10 Bit"}),
          gated_params({2*1024, 2, 12, 1024, "1k Channel: 12 Bit"}),
          gated_params({2*1024 * 1024, 2,  8, 8*1024, "1M Channel, buffer 8x larger than spectrum; 8 bit"}),
          gated_params({2*1024 * 1024, 2, 10, 8*1024, "1M Channel, buffer 8x larger than spectrum; 10-bit"}),
          gated_params({2*1024 * 1024, 2, 12, 8*1024, "1M Channel, buffer 8x larger than spectrum; 12-bit"}),
          gated_params({2*1024 * 1024, 2048, 8, 1024, "1M Channel, integration larger than buffer"}),
Tobias Winchen's avatar
Tobias Winchen committed
578
579
580
          gated_params({2*1024, 4096, 8, 1024, "1k Channel, integration larger than buffer"})
            )
        );
Tobias Winchen's avatar
Tobias Winchen committed
581
582
583
584