GatedSpectrometer.cu 25.1 KB
Newer Older
1
#include "psrdada_cpp/effelsberg/edd/GatedSpectrometer.cuh"
2
#include "psrdada_cpp/effelsberg/edd/Tools.cuh"
3
4
5
#include "psrdada_cpp/common.hpp"
#include "psrdada_cpp/cuda_utils.hpp"
#include "psrdada_cpp/raw_bytes.hpp"
6

7
#include <cuda.h>
8
#include <cuda_profiler_api.h>
9
#include <thrust/system/cuda/execution_policy.h>
10
11

#include <iostream>
12
#include <iomanip>
13
14
#include <cstring>
#include <sstream>
15
16
17
18
19

namespace psrdada_cpp {
namespace effelsberg {
namespace edd {

20
// Reduce thread local vatiable v in shared array x, so that x[0]
21
template<typename T>
22
__device__ void sum_reduce(T *x, const T &v)
23
24
25
26
27
28
29
30
31
{
  x[threadIdx.x] = v;
  __syncthreads();
  for(int s = blockDim.x / 2; s > 0; s = s / 2)
  {
    if (threadIdx.x < s)
      x[threadIdx.x] += x[threadIdx.x + s];
    __syncthreads();
  }
32
}
33
34


35
36
37
38
39
40
41
42
43
44
45
// If one of the side channel items is lsot, then both are considered as lost
// here
__global__ void mergeSideChannels(uint64_t* __restrict__ A, uint64_t* __restrict__ B, size_t N)
{
  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; (i < N);
       i += blockDim.x * gridDim.x)
  {
    uint64_t v = A[i] || B[i];
    A[i] = v;
    B[i] = v;
  }
46
47
48
}


49
50
51
52
53
54
55
56
57
58
__global__ void gating(float* __restrict__ G0,
        float* __restrict__ G1,
        const uint64_t* __restrict__ sideChannelData,
        size_t N, size_t heapSize, size_t bitpos,
        size_t noOfSideChannels, size_t selectedSideChannel,
        const float  baseLineG0,
        const float  baseLineG1,
        float* __restrict__ baseLineNG0,
        float* __restrict__ baseLineNG1,
        uint64_cu* stats_G0, uint64_cu* stats_G1) {
59
  // statistics values for samopels to G0, G1
60
61
62
  uint32_t _G0stats = 0;
  uint32_t _G1stats = 0;

63
64
65
  float baselineUpdateG0 = 0;
  float baselineUpdateG1 = 0;

Tobias Winchen's avatar
Tobias Winchen committed
66
  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; (i < N);
67
       i += blockDim.x * gridDim.x) {
68
69
    const float v = G0[i];

70
71
    const uint64_t sideChannelItem = sideChannelData[((i / heapSize) * (noOfSideChannels)) +
                        selectedSideChannel];
72

73
74
    const unsigned int bit_set = TEST_BIT(sideChannelItem, bitpos);
    const unsigned int heap_lost = TEST_BIT(sideChannelItem, 63);
75
76
77
    G1[i] = (v - baseLineG1) * bit_set * (!heap_lost) + baseLineG1;
    G0[i] = (v - baseLineG0) * (!bit_set) *(!heap_lost) + baseLineG0;

78
79
    _G0stats += (!bit_set) *(!heap_lost);
    _G1stats += bit_set * (!heap_lost);
80
81
82

    baselineUpdateG1 += v * bit_set * (!heap_lost);
    baselineUpdateG0 += v * (!bit_set) *(!heap_lost);
83
  }
84

85
86
87
  __shared__ uint32_t x[1024];

  // Reduce G0, G1
88
  sum_reduce<uint32_t>(x, _G0stats);
89
  if(threadIdx.x == 0) {
90
    atomicAdd(stats_G0,  (uint64_cu) x[threadIdx.x]);
91
  }
92
  __syncthreads();
93
94

  sum_reduce<uint32_t>(x, _G1stats);
95
  if(threadIdx.x == 0) {
96
    atomicAdd(stats_G1,  (uint64_cu) x[threadIdx.x]);
97
  }
98
  __syncthreads();
99

100
101
102
  //reuse shared array
  float *y = (float*) x;
  //update the baseline array
103
  sum_reduce<float>(y, baselineUpdateG0);
104
  if(threadIdx.x == 0) {
105
    atomicAdd(baseLineNG0, y[threadIdx.x]);
106
  }
Tobias Winchen's avatar
Tobias Winchen committed
107
  __syncthreads();
108
109

  sum_reduce<float>(y, baselineUpdateG1);
110
  if(threadIdx.x == 0) {
111
    atomicAdd(baseLineNG1, y[threadIdx.x]);
112
  }
113
  __syncthreads();
Tobias Winchen's avatar
Tobias Winchen committed
114
}
115

116

117

118
119
template <class HandlerType>
GatedSpectrometer<HandlerType>::GatedSpectrometer(
120
121
    const DadaBufferLayout &dadaBufferLayout,
    std::size_t selectedSideChannel, std::size_t selectedBit, std::size_t fft_length, std::size_t naccumulate,
122
    std::size_t nbits, float input_level, float output_level,
123
    HandlerType &handler) : _dadaBufferLayout(dadaBufferLayout),
124
      _selectedSideChannel(selectedSideChannel), _selectedBit(selectedBit),
125
      _fft_length(fft_length),
126
      _naccumulate(naccumulate), _nbits(nbits), _handler(handler), _fft_plan(0),
127
      _call_count(0), _nsamps_per_heap(4096), _processing_efficiency(0.){
128
129

  // Sanity checks
130
  assert(((_nbits == 12) || (_nbits == 8)));
131
132
133
134
135
  assert(_naccumulate > 0);

  // check for any device errors
  CUDA_ERROR_CHECK(cudaDeviceSynchronize());

136
  BOOST_LOG_TRIVIAL(info)
137
      << "Creating new GatedSpectrometer instance with parameters: \n"
138
139
      << "  fft_length           " << _fft_length << "\n"
      << "  naccumulate          " << _naccumulate << "\n"
140
141
      << "  nSideChannels        " << _dadaBufferLayout.getNSideChannels() << "\n"
      << "  speadHeapSize        " << _dadaBufferLayout.getHeapSize() << " byte\n"
142
143
144
      << "  selectedSideChannel  " << _selectedSideChannel << "\n"
      << "  selectedBit          " << _selectedBit << "\n"
      << "  output bit depth     " << sizeof(IntegratedPowerType) * 8;
145

146
147
  assert((_dadaBufferLayout.getNSideChannels() == 0) ||
         (selectedSideChannel < _dadaBufferLayout.getNSideChannels()));  // Sanity check of side channel value
148
149
  assert(selectedBit < 64); // Sanity check of selected bit

150
   _nsamps_per_buffer = _dadaBufferLayout.sizeOfData() * 8 / nbits;
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

  _nsamps_per_output_spectra = fft_length * naccumulate;
  int nBlocks;
  if (_nsamps_per_output_spectra <= _nsamps_per_buffer)
  { // one buffer block is used for one or multiple output spectra
    size_t N = _nsamps_per_buffer / _nsamps_per_output_spectra;
    // All data in one block has to be used
    assert(N * _nsamps_per_output_spectra == _nsamps_per_buffer);
    nBlocks = 1;
  }
  else
  { // multiple blocks are integrated intoone output
    size_t N =  _nsamps_per_output_spectra /  _nsamps_per_buffer;
    // All data in multiple blocks has to be used
    assert(N * _nsamps_per_buffer == _nsamps_per_output_spectra);
    nBlocks = N;
  }
  BOOST_LOG_TRIVIAL(debug) << "Integrating  " << _nsamps_per_output_spectra << " samples from " << nBlocks << " into one spectra.";

170
  _nchans = _fft_length / 2 + 1;
171
  int batch = _nsamps_per_buffer / _fft_length;
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
  float dof = 2 * _naccumulate;
  float scale =
      std::pow(input_level * std::sqrt(static_cast<float>(_nchans)), 2);
  float offset = scale * dof;
  float scaling = scale * std::sqrt(2 * dof) / output_level;
  BOOST_LOG_TRIVIAL(debug)
      << "Correction factors for 8-bit conversion: offset = " << offset
      << ", scaling = " << scaling;

  BOOST_LOG_TRIVIAL(debug) << "Generating FFT plan";
  int n[] = {static_cast<int>(_fft_length)};
  CUFFT_ERROR_CHECK(cufftPlanMany(&_fft_plan, 1, n, NULL, 1, _fft_length, NULL,
                                  1, _nchans, CUFFT_R2C, batch));
  cufftSetStream(_fft_plan, _proc_stream);

  BOOST_LOG_TRIVIAL(debug) << "Allocating memory";
188
189
190
191
  polarization0._raw_voltage.resize(_dadaBufferLayout.sizeOfData() / sizeof(uint64_t));
  polarization1._raw_voltage.resize(_dadaBufferLayout.sizeOfData() / sizeof(uint64_t));
  polarization0._sideChannelData.resize(_dadaBufferLayout.getNSideChannels() * _dadaBufferLayout.getNHeaps());
  polarization1._sideChannelData.resize(_dadaBufferLayout.getNSideChannels() * _dadaBufferLayout.getNHeaps());
192
  BOOST_LOG_TRIVIAL(debug) << "  Input voltages size (in 64-bit words): "
193
                           << polarization0._raw_voltage.size();
194
195
  _unpacked_voltage_G0.resize(_nsamps_per_buffer);
  _unpacked_voltage_G1.resize(_nsamps_per_buffer);
Tobias Winchen's avatar
Tobias Winchen committed
196

197
198
199
200
201
  polarization0._baseLineG0.resize(1);
  polarization0._baseLineG1.resize(1);
  polarization1._baseLineG0.resize(1);
  polarization1._baseLineG1.resize(1);

202
203
  BOOST_LOG_TRIVIAL(debug) << "  Unpacked voltages size (in samples): "
                           << _unpacked_voltage_G0.size();
204
205
206
207
  polarization0._channelised_voltage_G0.resize(_nchans * batch);
  polarization0._channelised_voltage_G1.resize(_nchans * batch);
  polarization1._channelised_voltage_G0.resize(_nchans * batch);
  polarization1._channelised_voltage_G1.resize(_nchans * batch);
208
  BOOST_LOG_TRIVIAL(debug) << "  Channelised voltages size: "
209
210
211
212
213
214
215
                           << polarization0._channelised_voltage_G0.size();

   stokes_G0.resize(_nchans, batch / (_naccumulate / nBlocks));
   stokes_G1.resize(_nchans, batch / (_naccumulate / nBlocks));

  // on the host full output is stored together with sci data in one buffer
  _host_power_db.resize( 8 * (_nchans * sizeof(IntegratedPowerType) + sizeof(size_t)) * batch / (_naccumulate / nBlocks));
216
217
218
219
220
221
222
223
224
225

  CUDA_ERROR_CHECK(cudaStreamCreate(&_h2d_stream));
  CUDA_ERROR_CHECK(cudaStreamCreate(&_proc_stream));
  CUDA_ERROR_CHECK(cudaStreamCreate(&_d2h_stream));
  CUFFT_ERROR_CHECK(cufftSetStream(_fft_plan, _proc_stream));

  _unpacker.reset(new Unpacker(_proc_stream));
} // constructor


226
227
228

template <class HandlerType>
GatedSpectrometer<HandlerType>::~GatedSpectrometer() {
229
230
231
232
233
234
235
236
237
  BOOST_LOG_TRIVIAL(debug) << "Destroying GatedSpectrometer";
  if (!_fft_plan)
    cufftDestroy(_fft_plan);
  cudaStreamDestroy(_h2d_stream);
  cudaStreamDestroy(_proc_stream);
  cudaStreamDestroy(_d2h_stream);
}


238
239
240

template <class HandlerType>
void GatedSpectrometer<HandlerType>::init(RawBytes &block) {
241
  BOOST_LOG_TRIVIAL(debug) << "GatedSpectrometer init called";
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  std::stringstream headerInfo;
  headerInfo << "\n"
      << "# Gated spectrometer parameters: \n"
      << "fft_length               " << _fft_length << "\n"
      << "nchannels                " << _fft_length << "\n"
      << "naccumulate              " << _naccumulate << "\n"
      << "selected_side_channel    " << _selectedSideChannel << "\n"
      << "selected_bit             " << _selectedBit << "\n"
      << "output_bit_depth         " << sizeof(IntegratedPowerType) * 8;

  size_t bEnd = std::strlen(block.ptr());
  if (bEnd + headerInfo.str().size() < block.total_bytes())
  {
    std::strcpy(block.ptr() + bEnd, headerInfo.str().c_str());
  }
  else
  {
    BOOST_LOG_TRIVIAL(warning) << "Header of size " << block.total_bytes()
      << " bytes already contains " << bEnd
      << "bytes. Cannot add gated spectrometer info of size "
      << headerInfo.str().size() << " bytes.";
  }

265
266
267
268
  _handler.init(block);
}


269
270
271

template <class HandlerType>
void GatedSpectrometer<HandlerType>::gated_fft(
272
        PolarizationData &data,
273
274
275
276
  thrust::device_vector<uint64_cu> &_noOfBitSetsIn_G0,
  thrust::device_vector<uint64_cu> &_noOfBitSetsIn_G1
        )
{
277
278
279
  BOOST_LOG_TRIVIAL(debug) << "Unpacking raw voltages";
  switch (_nbits) {
  case 8:
280
    _unpacker->unpack<8>(data._raw_voltage.b(), _unpacked_voltage_G0);
281
282
    break;
  case 12:
283
    _unpacker->unpack<12>(data._raw_voltage.b(), _unpacked_voltage_G0);
284
285
286
287
    break;
  default:
    throw std::runtime_error("Unsupported number of bits");
  }
288

289
290
291
  // Get baseline from previous block
  float previous_baseLineG0 = data._baseLineG0[0];
  float previous_baseLineG1 = data._baseLineG1[0];
292

293
294
295
  uint64_t NG0 = 0;
  uint64_t NG1 = 0;

296
// Loop over outputblocks, for case of multiple output blocks per input block
297
  int step = data._sideChannelData.b().size() / _noOfBitSetsIn_G0.size();
298

299
  for (size_t i = 0; i < _noOfBitSetsIn_G0.size(); i++)
300
  { // ToDo: Should be in one kernel call
301
  gating<<<1024, 1024, 0, _proc_stream>>>(
302
303
304
305
306
307
308
      thrust::raw_pointer_cast(_unpacked_voltage_G0.data() + i * step * _nsamps_per_heap),
      thrust::raw_pointer_cast(_unpacked_voltage_G1.data() + i * step * _nsamps_per_heap),
      thrust::raw_pointer_cast(data._sideChannelData.b().data() + i * step),
      _unpacked_voltage_G0.size() / _noOfBitSetsIn_G0.size(),
      _dadaBufferLayout.getHeapSize(),
      _selectedBit,
      _dadaBufferLayout.getNSideChannels(),
309
      _selectedSideChannel,
310
311
312
313
314
      previous_baseLineG0, previous_baseLineG1,
      thrust::raw_pointer_cast(data._baseLineG0.data()),
      thrust::raw_pointer_cast(data._baseLineG1.data()),
      thrust::raw_pointer_cast(_noOfBitSetsIn_G0.data() + i),
      thrust::raw_pointer_cast(_noOfBitSetsIn_G1.data() + i)
315
      );
316
317
    NG0 += _noOfBitSetsIn_G0[i];
    NG1 += _noOfBitSetsIn_G1[i];
318
  }
319
320
321
  data._baseLineG0[0] /= NG0;
  data._baseLineG1[0] /= NG1;
  BOOST_LOG_TRIVIAL(debug) << "Updating Baselines\n G0: " << previous_baseLineG0 << " -> " << data._baseLineG0[0] << ", " << previous_baseLineG1 << " -> " << data._baseLineG1[0] ;
322

323
324
325
326
327

  BOOST_LOG_TRIVIAL(debug) << "Performing FFT 1";
  UnpackedVoltageType *_unpacked_voltage_ptr =
      thrust::raw_pointer_cast(_unpacked_voltage_G0.data());
  ChannelisedVoltageType *_channelised_voltage_ptr =
328
      thrust::raw_pointer_cast(data._channelised_voltage_G0.data());
329
330
331
332
333
  CUFFT_ERROR_CHECK(cufftExecR2C(_fft_plan, (cufftReal *)_unpacked_voltage_ptr,
                                 (cufftComplex *)_channelised_voltage_ptr));

  BOOST_LOG_TRIVIAL(debug) << "Performing FFT 2";
  _unpacked_voltage_ptr = thrust::raw_pointer_cast(_unpacked_voltage_G1.data());
334
  _channelised_voltage_ptr = thrust::raw_pointer_cast(data._channelised_voltage_G1.data());
335
336
337
  CUFFT_ERROR_CHECK(cufftExecR2C(_fft_plan, (cufftReal *)_unpacked_voltage_ptr,
                                 (cufftComplex *)_channelised_voltage_ptr));

Tobias Winchen's avatar
Tobias Winchen committed
338
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_proc_stream));
339
  BOOST_LOG_TRIVIAL(debug) << "Exit processing";
340
341
342
} // process


343
344
template <class HandlerType>
bool GatedSpectrometer<HandlerType>::operator()(RawBytes &block) {
345
346
347
348
349
350
351
352
353
354
355
356
357
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    ++_call_count;
    BOOST_LOG_TRIVIAL(debug) << "GatedSpectrometer operator() called (count = "
                             << _call_count << ")";
    if (block.used_bytes() != _dadaBufferLayout.getBufferSize()) {
      // Stop on unexpected buffer size
      BOOST_LOG_TRIVIAL(error) << "Unexpected Buffer Size - Got "
                               << block.used_bytes() << " byte, expected "
                               << _dadaBufferLayout.getBufferSize() << " byte)";
      CUDA_ERROR_CHECK(cudaDeviceSynchronize());
      cudaProfilerStop();
      return true;
    }

    // Copy data to device
    CUDA_ERROR_CHECK(cudaStreamSynchronize(_h2d_stream));
    polarization0.swap();
    polarization1.swap();

    BOOST_LOG_TRIVIAL(debug) << "   block.used_bytes() = " <<
        block.used_bytes() << ", dataBlockBytes = " <<
        _dadaBufferLayout.sizeOfData() << "\n";

    // Copy the data with stride to the GPU:
    // CPU: P1P2P1P2P1P2 ...
    // GPU: P1P1P1 ... P2P2P2 ...
    // If this is a bottleneck the gating kernel could sort the layout out
    // during copy
    int heapsize_bytes = _nsamps_per_heap * _nbits / 8;
    CUDA_ERROR_CHECK(cudaMemcpy2DAsync(
      static_cast<void *>(polarization0._raw_voltage.a_ptr()),
        heapsize_bytes,
        static_cast<void *>(block.ptr()),
        2 * heapsize_bytes,
        heapsize_bytes, _dadaBufferLayout.sizeOfData() / heapsize_bytes/ 2,
        cudaMemcpyHostToDevice, _h2d_stream));

    CUDA_ERROR_CHECK(cudaMemcpy2DAsync(
      static_cast<void *>(polarization1._raw_voltage.a_ptr()),
        heapsize_bytes,
        static_cast<void *>(block.ptr()) + heapsize_bytes,
        2 * heapsize_bytes,
        heapsize_bytes, _dadaBufferLayout.sizeOfData() / heapsize_bytes/ 2,
        cudaMemcpyHostToDevice, _h2d_stream));

    CUDA_ERROR_CHECK(cudaMemcpy2DAsync(
        static_cast<void *>(polarization0._sideChannelData.a_ptr()),
        sizeof(uint64_t),
        static_cast<void *>(block.ptr() + _dadaBufferLayout.sizeOfData() + _dadaBufferLayout.sizeOfGap()),
        2 * sizeof(uint64_t),
        sizeof(uint64_t),
        _dadaBufferLayout.sizeOfSideChannelData() / 2 / sizeof(uint64_t),
        cudaMemcpyHostToDevice, _h2d_stream));

    CUDA_ERROR_CHECK(cudaMemcpy2DAsync(
        static_cast<void *>(polarization1._sideChannelData.a_ptr()),
        sizeof(uint64_t),
        static_cast<void *>(block.ptr() + _dadaBufferLayout.sizeOfData() + _dadaBufferLayout.sizeOfGap() + sizeof(uint64_t)),
        2 * sizeof(uint64_t),
        sizeof(uint64_t),
        _dadaBufferLayout.sizeOfSideChannelData() / 2 / sizeof(uint64_t), cudaMemcpyHostToDevice, _h2d_stream));

    BOOST_LOG_TRIVIAL(debug) << "First side channel item: 0x" <<   std::setw(16)
        << std::setfill('0') << std::hex <<
        (reinterpret_cast<uint64_t*>(block.ptr() + _dadaBufferLayout.sizeOfData()
                                     + _dadaBufferLayout.sizeOfGap()))[0] << std::dec;
410

411

412
413
414
  if (_call_count == 1) {
    return false;
  }
415

416
  // process data
417
418
  // check if new outblock is started:  _call_count -1 because this is the block number on the device
  bool newBlock = (((_call_count-1) * _nsamps_per_buffer) % _nsamps_per_output_spectra == 0);
419

420
421
  // only if  a newblock is started the output buffer is swapped. Otherwise the
  // new data is added to it
422
  if (newBlock)
423
  {
424
425
426
427
428
      BOOST_LOG_TRIVIAL(debug) << "Starting new output block.";
      stokes_G0.swap();
      stokes_G1.swap();
      stokes_G0.reset(_proc_stream);
      stokes_G1.reset(_proc_stream);
429
  }
430

431
432
  mergeSideChannels<<<1024, 1024, 0, _proc_stream>>>(thrust::raw_pointer_cast(polarization0._sideChannelData.a().data()),
          thrust::raw_pointer_cast(polarization1._sideChannelData.a().data()), polarization1._sideChannelData.a().size());
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

  gated_fft(polarization0, stokes_G0._noOfBitSets.a(), stokes_G1._noOfBitSets.a());
  gated_fft(polarization1, stokes_G0._noOfBitSets.a(), stokes_G1._noOfBitSets.a());

  stokes_accumulate<<<1024, 1024, 0, _proc_stream>>>(
          thrust::raw_pointer_cast(polarization0._channelised_voltage_G0.data()),
          thrust::raw_pointer_cast(polarization1._channelised_voltage_G0.data()),
          thrust::raw_pointer_cast(stokes_G0.I.a().data()),
          thrust::raw_pointer_cast(stokes_G0.Q.a().data()),
          thrust::raw_pointer_cast(stokes_G0.U.a().data()),
          thrust::raw_pointer_cast(stokes_G0.V.a().data()),
          _nchans, _naccumulate
          );

  stokes_accumulate<<<1024, 1024, 0, _proc_stream>>>(
          thrust::raw_pointer_cast(polarization0._channelised_voltage_G1.data()),
          thrust::raw_pointer_cast(polarization1._channelised_voltage_G1.data()),
          thrust::raw_pointer_cast(stokes_G1.I.a().data()),
          thrust::raw_pointer_cast(stokes_G1.Q.a().data()),
          thrust::raw_pointer_cast(stokes_G1.U.a().data()),
          thrust::raw_pointer_cast(stokes_G1.V.a().data()),
          _nchans, _naccumulate
          );


458
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_proc_stream));
Tobias Winchen's avatar
Tobias Winchen committed
459

460
  if ((_call_count == 2) || (!newBlock)) {
461
462
463
    return false;
  }

464
  // copy data to host if block is finished
465
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_d2h_stream));
466
  _host_power_db.swap();
467
468
  // OUTPUT MEMORY LAYOUT:
  // I G0, IG1,Q G0, QG1, U G0,UG1,V G0,VG1, 8xSCI, ...
469

470
  for (size_t i = 0; i < stokes_G0._noOfBitSets.size(); i++)
471
  {
472
473
474
    size_t memslicesize = (_nchans * sizeof(IntegratedPowerType));
    size_t memOffset = 8 * i * (memslicesize +  + sizeof(size_t));
    // Copy  II QQ UU VV
475
476
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset) ,
477
478
479
480
481
482
483
484
                        static_cast<void *>(stokes_G0.I.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 1 * memslicesize) ,
                        static_cast<void *>(stokes_G1.I.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
485
                        cudaMemcpyDeviceToHost, _d2h_stream));
486

487
    CUDA_ERROR_CHECK(
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 2 * memslicesize) ,
                        static_cast<void *>(stokes_G0.Q.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 3 * memslicesize) ,
                        static_cast<void *>(stokes_G1.Q.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 4 * memslicesize) ,
                        static_cast<void *>(stokes_G0.U.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 5 * memslicesize) ,
                        static_cast<void *>(stokes_G1.U.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 6 * memslicesize) ,
                        static_cast<void *>(stokes_G0.V.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    CUDA_ERROR_CHECK(
        cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr() + memOffset + 7 * memslicesize) ,
                        static_cast<void *>(stokes_G1.V.b_ptr() + i * memslicesize),
                        _nchans * sizeof(IntegratedPowerType),
                        cudaMemcpyDeviceToHost, _d2h_stream));

    // Copy SCI
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize),
          static_cast<void *>(stokes_G0._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 1 * sizeof(size_t)),
          static_cast<void *>(stokes_G1._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 2 * sizeof(size_t)),
          static_cast<void *>(stokes_G0._noOfBitSets.b_ptr() + i ),
537
538
539
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
540
541
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 3 * sizeof(size_t)),
          static_cast<void *>(stokes_G1._noOfBitSets.b_ptr() + i ),
542
543
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 4 * sizeof(size_t)),
          static_cast<void *>(stokes_G0._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 5 * sizeof(size_t)),
          static_cast<void *>(stokes_G1._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 6 * sizeof(size_t)),
          static_cast<void *>(stokes_G0._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));
    CUDA_ERROR_CHECK(
        cudaMemcpyAsync( static_cast<void *>(_host_power_db.a_ptr() + memOffset + 8 * memslicesize + 7 * sizeof(size_t)),
          static_cast<void *>(stokes_G1._noOfBitSets.b_ptr() + i ),
            1 * sizeof(size_t),
            cudaMemcpyDeviceToHost, _d2h_stream));

565
  }
566
567

  BOOST_LOG_TRIVIAL(debug) << "Copy Data back to host";
568

569
570
571
572
  if (_call_count == 3) {
    return false;
  }

573
  // calculate off value
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  //BOOST_LOG_TRIVIAL(info) << "Buffer block: " << _call_count-3 << " with " << _noOfBitSetsIn_G0.size() << "x2 output heaps:";
  //size_t total_samples_lost = 0;
  //for (size_t i = 0; i < _noOfBitSetsIn_G0.size(); i++)
  //{
  //  size_t memOffset = 2 * i * (_nchans * sizeof(IntegratedPowerType) + sizeof(size_t));

  //  size_t* on_values = reinterpret_cast<size_t*> (_host_power_db.b_ptr() + memOffset + 2 * _nchans * sizeof(IntegratedPowerType));
  //  size_t* off_values = reinterpret_cast<size_t*> (_host_power_db.b_ptr() + memOffset + 2 * _nchans * sizeof(IntegratedPowerType) + sizeof(size_t));

  //  size_t samples_lost = _nsamps_per_output_spectra - (*on_values) - (*off_values);
  //  total_samples_lost += samples_lost;

  //  BOOST_LOG_TRIVIAL(info) << "    Heap " << i << ":\n"
  //    <<"                            Samples with  bit set  : " << *on_values << std::endl
  //    <<"                            Samples without bit set: " << *off_values << std::endl
  //    <<"                            Samples lost           : " << samples_lost << " out of " << _nsamps_per_output_spectra << std::endl;
  //}
  //double efficiency = 1. - double(total_samples_lost) / (_nsamps_per_output_spectra * _noOfBitSetsIn_G0.size());
  //double prev_average = _processing_efficiency / (_call_count- 3 - 1);
  //_processing_efficiency += efficiency;
  //double average = _processing_efficiency / (_call_count-3);
  //BOOST_LOG_TRIVIAL(info) << "Total processing efficiency of this buffer block:" << std::setprecision(6) << efficiency << ". Run average: " << average << " (Trend: " << std::showpos << (average - prev_average) << ")";
596
597

  // Wrap in a RawBytes object here;
598
  RawBytes bytes(reinterpret_cast<char *>(_host_power_db.b_ptr()),
599
600
                 _host_power_db.size(),
                 _host_power_db.size());
601
602
603
604
  BOOST_LOG_TRIVIAL(debug) << "Calling handler";
  // The handler can't do anything asynchronously without a copy here
  // as it would be unsafe (given that it does not own the memory it
  // is being passed).
Tobias Winchen's avatar
Tobias Winchen committed
605
606
607

  _handler(bytes);
  return false; //
608
609
610
611
612
613
} // operator ()

} // edd
} // effelsberg
} // psrdada_cpp