GatedSpectrometer.cu 14.7 KB
Newer Older
1
2
3
4
5
#include "psrdada_cpp/effelsberg/edd/GatedSpectrometer.cuh"
#include "psrdada_cpp/common.hpp"
#include "psrdada_cpp/cuda_utils.hpp"
#include "psrdada_cpp/raw_bytes.hpp"
#include <cuda.h>
6
#include <cuda_profiler_api.h>
7
8
9
10
11
12
13

#include <iostream>

namespace psrdada_cpp {
namespace effelsberg {
namespace edd {

Tobias Winchen's avatar
Tobias Winchen committed
14
15
16
17
18





Tobias Winchen's avatar
Tobias Winchen committed
19
__global__ void gating(float* __restrict__ G0, float* __restrict__ G1, const int64_t* __restrict__ sideChannelData,
20
                       size_t N, size_t heapSize, size_t bitpos,
Tobias Winchen's avatar
Tobias Winchen committed
21
                       size_t noOfSideChannels, size_t selectedSideChannel, const float* __restrict__ _baseLineN) {
22
  float baseLine = (*_baseLineN) / N;
Tobias Winchen's avatar
Tobias Winchen committed
23
  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; (i < N);
24
       i += blockDim.x * gridDim.x) {
Tobias Winchen's avatar
Tobias Winchen committed
25
    const float w = G0[i] - baseLine;
26
27
28
29
30
31
32
33
    const int64_t sideChannelItem =
        sideChannelData[((i / heapSize) * (noOfSideChannels)) +
                        selectedSideChannel]; // Probably not optimal access as
                                              // same data is copied for several
                                              // threads, but maybe efficiently
                                              // handled by cache?

    const int bit_set = TEST_BIT(sideChannelItem, bitpos);
Tobias Winchen's avatar
Tobias Winchen committed
34
35
    G1[i] = w * bit_set + baseLine;
    G0[i] = w * (!bit_set) + baseLine;
36
37
38
  }
}

Tobias Winchen's avatar
Tobias Winchen committed
39

40
__global__ void countBitSet(const int64_t *sideChannelData, size_t N, size_t
41
    bitpos, size_t noOfSideChannels, size_t selectedSideChannel, size_t
42
    *nBitsSet)
43
{
Tobias Winchen's avatar
Tobias Winchen committed
44
45
  // really not optimized reduction, but here only trivial array sizes.
  int i = blockIdx.x * blockDim.x + threadIdx.x;
46
  __shared__ unsigned int x[256];
Tobias Winchen's avatar
Tobias Winchen committed
47
48

  if (i == 0)
49
    nBitsSet[0] = 0;
Tobias Winchen's avatar
Tobias Winchen committed
50

51
  if (i * noOfSideChannels + selectedSideChannel < N)
Tobias Winchen's avatar
Tobias Winchen committed
52
53
54
55
56
57
58
59
60
61
62
    x[threadIdx.x] = TEST_BIT(sideChannelData[i * noOfSideChannels + selectedSideChannel], bitpos);
  else
    x[threadIdx.x] = 0;
  __syncthreads();

  for(int s = blockDim.x / 2; s > 0; s = s / 2)
  {
    if (threadIdx.x < s)
      x[threadIdx.x] += x[threadIdx.x + s];
    __syncthreads();
  }
63

Tobias Winchen's avatar
Tobias Winchen committed
64
  if(threadIdx.x == 0)
65
   nBitsSet[0] += x[threadIdx.x];
Tobias Winchen's avatar
Tobias Winchen committed
66
}
67

68

Tobias Winchen's avatar
Tobias Winchen committed
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
// blocksize for the array sum kernel
#define array_sum_Nthreads 1024

__global__ void array_sum(float *in, size_t N, float *out) {
  extern __shared__ float data[];

  size_t tid = threadIdx.x;

  float ls = 0;

  for (size_t i = blockIdx.x * blockDim.x + threadIdx.x; (i < N);
       i += blockDim.x * gridDim.x) {
    ls += in[i]; // + in[i + blockDim.x];   // loading two elements increase the used bandwidth by ~10% but requires matching blocksize and size of input array
  }

  data[tid] = ls;
  __syncthreads();

  for (size_t i = blockDim.x / 2; i > 0; i /= 2) {
    if (tid < i) {
      data[tid] += data[tid + i];
    }
    __syncthreads();
  }

  // unroll last warp
  // if (tid < 32)
  //{
  //  warpReduce(data, tid);
  //}

  if (tid == 0) {
    out[blockIdx.x] = data[0];
  }
}


106
107
template <class HandlerType, typename IntegratedPowerType>
GatedSpectrometer<HandlerType, IntegratedPowerType>::GatedSpectrometer(
108
109
110
111
112
113
114
115
116
117
118
119
    std::size_t buffer_bytes, std::size_t nSideChannels,
    std::size_t selectedSideChannel, std::size_t selectedBit,
    std::size_t speadHeapSize, std::size_t fft_length, std::size_t naccumulate,
    std::size_t nbits, float input_level, float output_level,
    HandlerType &handler)
    : _buffer_bytes(buffer_bytes), _nSideChannels(nSideChannels),
      _selectedSideChannel(selectedSideChannel), _selectedBit(selectedBit),
      _speadHeapSize(speadHeapSize), _fft_length(fft_length),
      _naccumulate(naccumulate), _nbits(nbits), _handler(handler), _fft_plan(0),
      _call_count(0) {
  assert(((_nbits == 12) || (_nbits == 8)));
  assert(_naccumulate > 0); // Sanity check
120
  BOOST_LOG_TRIVIAL(info)
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      << "Creating new GatedSpectrometer instance with parameters: \n"
      << "  fft_length = " << _fft_length << "\n"
      << "  naccumulate = " << _naccumulate << "\n"
      << "  nSideChannels = " << _nSideChannels << "\n"
      << "  speadHeapSize = " << _speadHeapSize << " byte\n"
      << "  selectedSideChannel = " << _selectedSideChannel
      << "  selectedBit = " << _selectedBit;

  _sideChannelSize = nSideChannels * sizeof(int64_t);
  _totalHeapSize = _speadHeapSize + _sideChannelSize;
  _nHeaps = buffer_bytes / _totalHeapSize;
  _gapSize = (buffer_bytes - _nHeaps * _totalHeapSize);
  _dataBlockBytes = _nHeaps * _speadHeapSize;
  assert((nSideChannels == 0) ||
         (selectedSideChannel <
          nSideChannels));  // Sanity check of side channel value
  assert(selectedBit < 64); // Sanity check of selected bit
138
  BOOST_LOG_TRIVIAL(info) << "Resulting memory configuration: \n"
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
                           << "  totalSizeOfHeap: " << _totalHeapSize
                           << " byte\n"
                           << "  number of heaps per buffer: " << _nHeaps
                           << "\n"
                           << "  resulting gap: " << _gapSize << " byte\n"
                           << "  datablock size in buffer: " << _dataBlockBytes
                           << " byte\n";

  std::size_t nsamps_per_buffer = _dataBlockBytes * 8 / nbits;
  std::size_t n64bit_words = _dataBlockBytes / sizeof(uint64_t);
  _nchans = _fft_length / 2 + 1;
  int batch = nsamps_per_buffer / _fft_length;
  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";
  _raw_voltage_db.resize(n64bit_words);
  _sideChannelData_db.resize(_sideChannelSize * _nHeaps);
  BOOST_LOG_TRIVIAL(debug) << "  Input voltages size (in 64-bit words): "
                           << _raw_voltage_db.size();
  _unpacked_voltage_G0.resize(nsamps_per_buffer);
  _unpacked_voltage_G1.resize(nsamps_per_buffer);
Tobias Winchen's avatar
Tobias Winchen committed
173
174

  _baseLineN.resize(array_sum_Nthreads);
175
176
  BOOST_LOG_TRIVIAL(debug) << "  Unpacked voltages size (in samples): "
                           << _unpacked_voltage_G0.size();
177
  _channelised_voltage.resize(_nchans * batch);
178
  BOOST_LOG_TRIVIAL(debug) << "  Channelised voltages size: "
179
                           << _channelised_voltage.size();
180
181
182
183
184
185
  _power_db_G0.resize(_nchans * batch / _naccumulate);
  _power_db_G1.resize(_nchans * batch / _naccumulate);
  BOOST_LOG_TRIVIAL(debug) << "  Powers size: " << _power_db_G0.size() << ", "
                           << _power_db_G1.size();
  // on the host both power are stored in the same data buffer
  _host_power_db.resize( _power_db_G0.size() + _power_db_G1 .size());
Tobias Winchen's avatar
Tobias Winchen committed
186
  _noOfBitSetsInSideChannel.resize(1);
187
188
189
190
191
192
193

  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));
Tobias Winchen's avatar
Tobias Winchen committed
194
  _detector.reset(new DetectorAccumulator<IntegratedPowerType>(_nchans, _naccumulate, scaling,
195
196
197
198
                                          offset, _proc_stream));
} // constructor


199
200
template <class HandlerType, typename IntegratedPowerType>
GatedSpectrometer<HandlerType, IntegratedPowerType>::~GatedSpectrometer() {
201
202
203
204
205
206
207
208
209
  BOOST_LOG_TRIVIAL(debug) << "Destroying GatedSpectrometer";
  if (!_fft_plan)
    cufftDestroy(_fft_plan);
  cudaStreamDestroy(_h2d_stream);
  cudaStreamDestroy(_proc_stream);
  cudaStreamDestroy(_d2h_stream);
}


210
211
template <class HandlerType, typename IntegratedPowerType>
void GatedSpectrometer<HandlerType, IntegratedPowerType>::init(RawBytes &block) {
212
213
214
215
216
  BOOST_LOG_TRIVIAL(debug) << "GatedSpectrometer init called";
  _handler.init(block);
}


217
218
template <class HandlerType, typename IntegratedPowerType>
void GatedSpectrometer<HandlerType, IntegratedPowerType>::process(
219
    thrust::device_vector<RawVoltageType> const &digitiser_raw,
220
    thrust::device_vector<int64_t> const &sideChannelData,
221
    thrust::device_vector<IntegratedPowerType> &detected_G0,
222
    thrust::device_vector<IntegratedPowerType> &detected_G1, thrust::device_vector<size_t> &noOfBitSet) {
223
224
225
226
227
228
229
230
231
232
233
  BOOST_LOG_TRIVIAL(debug) << "Unpacking raw voltages";
  switch (_nbits) {
  case 8:
    _unpacker->unpack<8>(digitiser_raw, _unpacked_voltage_G0);
    break;
  case 12:
    _unpacker->unpack<12>(digitiser_raw, _unpacked_voltage_G0);
    break;
  default:
    throw std::runtime_error("Unsupported number of bits");
  }
Tobias Winchen's avatar
Tobias Winchen committed
234
235
236
  BOOST_LOG_TRIVIAL(debug) << "Calculate baseline";
  psrdada_cpp::effelsberg::edd::array_sum<<<64, array_sum_Nthreads, array_sum_Nthreads * sizeof(float), _proc_stream>>>(thrust::raw_pointer_cast(_unpacked_voltage_G0.data()), _unpacked_voltage_G0.size(), thrust::raw_pointer_cast(_baseLineN.data()));
  psrdada_cpp::effelsberg::edd::array_sum<<<1, array_sum_Nthreads, array_sum_Nthreads * sizeof(float), _proc_stream>>>(thrust::raw_pointer_cast(_baseLineN.data()), _baseLineN.size(), thrust::raw_pointer_cast(_baseLineN.data()));
237

238
  BOOST_LOG_TRIVIAL(debug) << "Perform gating";
239
240
  gating<<<1024, 1024, 0, _proc_stream>>>(
      thrust::raw_pointer_cast(_unpacked_voltage_G0.data()),
241
242
      thrust::raw_pointer_cast(_unpacked_voltage_G1.data()),
      thrust::raw_pointer_cast(sideChannelData.data()),
243
      _unpacked_voltage_G0.size(), _speadHeapSize, _selectedBit, _nSideChannels,
Tobias Winchen's avatar
Tobias Winchen committed
244
      _selectedSideChannel, thrust::raw_pointer_cast(_baseLineN.data()));
245

Tobias Winchen's avatar
Tobias Winchen committed
246
  countBitSet<<<(sideChannelData.size()+255)/256, 256, 0,
247
248
    _proc_stream>>>(thrust::raw_pointer_cast(sideChannelData.data()),
        sideChannelData.size(), _selectedBit,
Tobias Winchen's avatar
Tobias Winchen committed
249
250
        _nSideChannels, _selectedBit,
        thrust::raw_pointer_cast(noOfBitSet.data()));
251
252
253
254
255

  BOOST_LOG_TRIVIAL(debug) << "Performing FFT 1";
  UnpackedVoltageType *_unpacked_voltage_ptr =
      thrust::raw_pointer_cast(_unpacked_voltage_G0.data());
  ChannelisedVoltageType *_channelised_voltage_ptr =
256
      thrust::raw_pointer_cast(_channelised_voltage.data());
257
258
  CUFFT_ERROR_CHECK(cufftExecR2C(_fft_plan, (cufftReal *)_unpacked_voltage_ptr,
                                 (cufftComplex *)_channelised_voltage_ptr));
259
  _detector->detect(_channelised_voltage, detected_G0);
260
261
262
263
264
265

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

266
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_proc_stream));
267
  _detector->detect(_channelised_voltage, detected_G1);
Tobias Winchen's avatar
Tobias Winchen committed
268
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_proc_stream));
269
  BOOST_LOG_TRIVIAL(debug) << "Exit processing";
270
271
272
} // process


273
274
template <class HandlerType, typename IntegratedPowerType>
bool GatedSpectrometer<HandlerType, IntegratedPowerType>::operator()(RawBytes &block) {
275
276
277
278
279
280
281
  ++_call_count;
  BOOST_LOG_TRIVIAL(debug) << "GatedSpectrometer operator() called (count = "
                           << _call_count << ")";
  if (block.used_bytes() != _buffer_bytes) { /* Unexpected buffer size */
    BOOST_LOG_TRIVIAL(error) << "Unexpected Buffer Size - Got "
                             << block.used_bytes() << " byte, expected "
                             << _buffer_bytes << " byte)";
Tobias Winchen's avatar
Tobias Winchen committed
282
283
284
    cudaDeviceSynchronize();
    cudaProfilerStop();
    return true;
285
286
  }

287
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_h2d_stream));
288
289
290
291
292
  _raw_voltage_db.swap();
  _sideChannelData_db.swap();

  BOOST_LOG_TRIVIAL(debug) << "   block.used_bytes() = " << block.used_bytes()
                           << ", dataBlockBytes = " << _dataBlockBytes << "\n";
Tobias Winchen's avatar
Tobias Winchen committed
293

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
  CUDA_ERROR_CHECK(cudaMemcpyAsync(static_cast<void *>(_raw_voltage_db.a_ptr()),
                                   static_cast<void *>(block.ptr()),
                                   _dataBlockBytes, cudaMemcpyHostToDevice,
                                   _h2d_stream));
  CUDA_ERROR_CHECK(cudaMemcpyAsync(
      static_cast<void *>(_sideChannelData_db.a_ptr()),
      static_cast<void *>(block.ptr() + _dataBlockBytes + _gapSize),
      _sideChannelSize * _nHeaps, cudaMemcpyHostToDevice, _h2d_stream));

  if (_call_count == 1) {
    return false;
  }

  // Synchronize all streams
  _power_db_G0.swap();
  _power_db_G1.swap();
Tobias Winchen's avatar
Tobias Winchen committed
310
  _noOfBitSetsInSideChannel.swap();
311
312

  process(_raw_voltage_db.b(), _sideChannelData_db.b(), _power_db_G0.a(),
313
          _power_db_G1.a(), _noOfBitSetsInSideChannel.a());
314

Tobias Winchen's avatar
Tobias Winchen committed
315
  // signal that data block has been processed
316
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_proc_stream));
Tobias Winchen's avatar
Tobias Winchen committed
317

318
319
320
321
  if (_call_count == 2) {
    return false;
  }

Tobias Winchen's avatar
Tobias Winchen committed
322
  BOOST_LOG_TRIVIAL(debug) << "Copy Data back to device";
323
  CUDA_ERROR_CHECK(cudaStreamSynchronize(_d2h_stream));
Tobias Winchen's avatar
Tobias Winchen committed
324
  BOOST_LOG_TRIVIAL(debug) << "Swap host power";
325
  _host_power_db.swap();
Tobias Winchen's avatar
Tobias Winchen committed
326
  BOOST_LOG_TRIVIAL(debug) << "swap no of bit channel";
327
  std::swap(_noOfBitSetsInSideChannel_host[0], _noOfBitSetsInSideChannel_host[1]);
Tobias Winchen's avatar
Tobias Winchen committed
328
  BOOST_LOG_TRIVIAL(debug) << "Foo";
329
330
331
332
333
  CUDA_ERROR_CHECK(
      cudaMemcpyAsync(static_cast<void *>(_host_power_db.a_ptr()),
                      static_cast<void *>(_power_db_G0.b_ptr()),
                      _power_db_G0.size() * sizeof(IntegratedPowerType),
                      cudaMemcpyDeviceToHost, _d2h_stream));
Tobias Winchen's avatar
Tobias Winchen committed
334
  BOOST_LOG_TRIVIAL(debug) << "Bar";
335
336
  CUDA_ERROR_CHECK(cudaMemcpyAsync(
      static_cast<void *>(_host_power_db.a_ptr() +
Tobias Winchen's avatar
Tobias Winchen committed
337
                          (_power_db_G0.size())),           // as I am adding BEFORE the cast to void, I dont need the sizeof
338
339
340
341
      static_cast<void *>(_power_db_G1.b_ptr()),
      _power_db_G1.size() * sizeof(IntegratedPowerType), cudaMemcpyDeviceToHost,
      _d2h_stream));

Tobias Winchen's avatar
Tobias Winchen committed
342
343
344
  //CUDA_ERROR_CHECK(cudaMemcpyAsync(static_cast<void *>(&_noOfBitSetsInSideChannel_host[0]),
  //      static_cast<void *>(_noOfBitSetsInSideChannel.b_ptr()),
  //        1 * sizeof(size_t),cudaMemcpyDeviceToHost, _d2h_stream));
345

Tobias Winchen's avatar
Tobias Winchen committed
346
  BOOST_LOG_TRIVIAL(debug) << "Copy Data back to device";
347

348
349
350
351
  if (_call_count == 3) {
    return false;
  }

Tobias Winchen's avatar
Tobias Winchen committed
352
  BOOST_LOG_TRIVIAL(info) << _call_count << ": No of bit set in side channel: " << _noOfBitSetsInSideChannel_host[1] << std::endl;
353
354
355
356
357
358
359
360
  // Wrap _detected_host_previous in a RawBytes object here;
  RawBytes bytes(reinterpret_cast<char *>(_host_power_db.b_ptr()),
                 _host_power_db.size() * sizeof(IntegratedPowerType),
                 _host_power_db.size() * sizeof(IntegratedPowerType));
  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
361
362
363

  _handler(bytes);
  return false; //
364
365
366
367
368
369
} // operator ()

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