UnpackerSource.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2023 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // dspsr/Kernel/Classes/dsp/UnpackerSource.h
10 
11 #ifndef __dsp_Kernel_Classes_UnpackerSource_h
12 #define __dsp_Kernel_Classes_UnpackerSource_h
13 
14 #include "dsp/InputSource.h"
15 
16 namespace dsp {
17 
19  template<class UnpackerType,class InputType>
20  class UnpackerSource : public InputSource<InputType>
21  {
22 
23  public:
24 
25  typedef typename InputType::OutputType BitContainerType;
26 
28  UnpackerSource (const char* name) : InputSource<InputType>(name) {}
29 
32 
35 
37  bool get_device_supported (Memory*) const;
38 
40  void set_device (Memory*);
41 
43  void prepare ();
44 
46  void reserve ();
47 
49  void add_extensions (Extensions*);
50 
52  void combine (const Operation*);
53 
55  void reset ();
56 
58  void report () const;
59 
61  uint64_t bytes_storage() const override;
62 
64  uint64_t bytes_scratch () const override;
65 
67  const InputType* get_input () const { return input; }
68  InputType* get_input () { return input; }
69 
71  void set_input (InputType* input);
72 
74  const UnpackerType* get_unpacker () const;
75  UnpackerType* get_unpacker ();
76 
78  void set_unpacker (UnpackerType* unpacker);
79 
82 
85 
87  bool has_output () const;
88 
90  // (should not normally need to be used)
91  virtual void set_bit_container (BitContainerType*);
92 
95 
97  uint64_t get_resolution () const;
98 
100  virtual void load (TimeSeries* data);
101 
103  virtual void set_scratch (Scratch* scratch);
104 
106  virtual void set_cerr (std::ostream& os) const;
107 
108  protected:
109 
111  virtual void operation ();
112 
115 
118 
121 
124 
127  };
128 }
129 
130 template<class UnT,class InT>
132 {
133  return unpacker->get_device_supported(device_memory);
134 }
135 
136 template<class UnT,class InT>
138 {
139  if (!unpacker)
140  throw Error (InvalidState, "dsp::UnpackerSource<UnT,InT>::set_device", "Unpacker not set");
141 
142  if (!unpacker->get_device_supported(device_memory))
143  throw Error (InvalidParam, "dsp::UnpackerSource<UnT,InT>::set_device", "Memory not supported by Unpacker");
144 
145  unpacker->set_device( device_memory );
146 
147  if (!bit_container)
148  set_bit_container (new BitContainerType);
149 
150  bit_container->set_memory( device_memory );
151 }
152 
153 template<class UnT,class InT>
155 {
156  if (!unpacker)
157  throw Error (InvalidState, "dsp::UnpackerSource<UnT,InT>::get_order_supported", "Unpacker not set");
158 
159  return unpacker->get_order_supported(order);
160 }
161 
162 template<class UnT,class InT>
164 {
165  if (!unpacker)
166  throw Error (InvalidState, "dsp::UnpackerSource<UnT,InT>::set_output_order", "Unpacker not set");
167 
168  unpacker->set_output_order(order);
169 }
170 
171 template<class UnT,class InT>
173 {
174  if (Operation::verbose)
175  this->cerr << "dsp::UnpackerSource<UnT,InT>::set_bit_container ptr=" << raw << std::endl;
176 
177  bit_container = raw;
178 
179  if (unpacker)
180  {
181  if (Operation::verbose)
182  this->cerr << "dsp::UnpackerSource<UnT,InT>::set_bit_container call Unpacker::set_input" << std::endl;
183  unpacker -> set_input (raw);
184  }
185 }
186 
187 template<class UnT,class InT>
189 {
190  if (Operation::verbose)
191  this->cerr << "dsp::UnpackerSource<UnT,InT>::set_output (TimeSeries*) " << _output << std::endl;
192 
193  output = _output;
194 
195  if (unpacker)
196  {
197  if (Operation::verbose)
198  this->cerr << "dsp::UnpackerSource<UnT,InT>::set_output call Unpacker::set_output" << std::endl;
199  unpacker -> set_output (_output);
200  }
201 }
202 
203 template<class UnT,class InT>
205 {
206  return output;
207 }
208 
209 template<class UnT,class InT>
211 {
212  return output;
213 }
214 
215 template<class UnT,class InT>
217 {
218  input = _input;
219 
220  if (!input)
221  return;
222 
223  if (!unpacker || !unpacker->matches (input->get_info()))
224  set_unpacker ( UnT::create( input->get_info() ) );
225 }
226 
227 template<class UnT,class InT>
229 {
230  unpacker = _unpacker;
231 
232  if (unpacker)
233  {
234  if (bit_container)
235  unpacker -> set_input (bit_container);
236  if (output)
237  unpacker -> set_output (output);
238  }
239 }
240 
241 template<class UnT,class InT>
243 {
244  return unpacker;
245 }
246 
247 template<class UnT,class InT>
249 {
250  return unpacker;
251 }
252 
253 template<class UnT,class InT>
255 {
256  if (Operation::verbose)
257  this->cerr << "dsp::UnpackerSource<UnT,InT>::load (TimeSeries* = " << _output << ")" << std::endl;
258 
259  set_output (_output);
260 
261  operation ();
262 }
263 
264 template<class UnT,class InT>
266 {
267  if (Operation::verbose)
268  this->cerr << "dsp::UnpackerSource<UnT,InT>::prepare" << std::endl;
269 
270  if (!bit_container)
271  set_bit_container (new BitContainerType);
272 
273  input->set_output( bit_container );
274 
275  input->prepare();
276  unpacker->prepare();
277 
278  unpacker->match_resolution (input);
279 
280  if (post_load_operation)
281  post_load_operation->prepare ();
282 
283  this->prepared = true;
284 }
285 
286 template<class UnT,class InT>
288 {
289  if (Operation::verbose)
290  this->cerr << "dsp::UnpackerSource<UnT,InT>::reserve" << std::endl;
291 
292  if (!bit_container)
293  set_bit_container (new BitContainerType);
294 
295  input->reserve( bit_container );
296  unpacker->reserve();
297  if (post_load_operation)
298  post_load_operation->reserve ();
299 }
300 
301 template<class UnT,class InT>
303 {
304  if (input)
305  input->add_extensions (ext);
306  if (unpacker)
307  unpacker->add_extensions (ext);
308  if (post_load_operation)
309  post_load_operation->add_extensions (ext);
310 }
311 
312 template<class UnT,class InT>
314 {
315  Operation::combine (other);
316 
317  const UnpackerSource<UnT,InT>* like = dynamic_cast<const UnpackerSource<UnT,InT>*>( other );
318  if (!like)
319  return;
320 
321  input->combine (like->input);
322  unpacker->combine (like->unpacker);
323  if (post_load_operation)
324  post_load_operation->combine (like->post_load_operation);
325 
326 }
327 
328 template<class UnT,class InT>
330 {
331  Operation::reset ();
332 
333  input->reset ();
334  unpacker->reset ();
335  if (post_load_operation)
336  post_load_operation->reset ();
337 }
338 
339 template<class UnT,class InT>
341 {
342  if (input)
343  input->report ();
344  if (unpacker)
345  unpacker->report ();
346  if (post_load_operation)
347  post_load_operation->report ();
348 }
349 
350 template<class UnT,class InT>
352 {
353  return input->bytes_storage() + unpacker->bytes_storage();
354 }
355 
356 template<class UnT,class InT>
358 {
359  return std::max(input->bytes_scratch(), unpacker->bytes_scratch());
360 }
361 
362 template<class UnT,class InT>
364 {
365  if (!bit_container)
366  set_bit_container (new BitContainerType);
367 
368  input->load (bit_container);
369 
370  if (post_load_operation)
371  {
372  if (Operation::verbose)
373  this->cerr << "dsp::UnpackerSource<UnT,InT>::operation post_load_operation->operate()" << std::endl;
374  post_load_operation->operate ();
375  }
376 
377  if (!output)
378  return;
379 
380  unpacker->operate ();
381 }
382 
383 template<class UnT,class InT>
385 {
386  if (Operation::verbose)
387  this->cerr << "dsp::UnpackerSource<UnT,InT>::set_post_load_operation(" << op << ")" << std::endl;
388  post_load_operation = op;
389 }
390 
391 template<class UnT,class InT>
393 {
394  unsigned resolution = input->get_resolution();
395  if (Operation::verbose)
396  this->cerr << "dsp::UnpackerSource<UnT,InT>::get_resolution input resolution=" << resolution << std::endl;
397 
398  if (unpacker->get_resolution())
399  {
400  resolution = unpacker->get_resolution();
401  if (Operation::verbose)
402  this->cerr << "dsp::UnpackerSource<UnT,InT>::get_resolution unpacker resolution=" << resolution << std::endl;
403  }
404 
405  // ensure that the block size is a multiple of four
406  if (resolution % 4)
407  {
408  if (resolution % 2 == 0)
409  resolution *= 2;
410  else
411  resolution *= 4;
412  }
413 
414  return resolution;
415 }
416 
417 template<class UnT,class InT>
419 {
420  if (Operation::verbose)
421  this->cerr << "dsp::dsp::UnpackerSource<UnT,InT>::set_scratch" << std::endl;
422 
423  Operation::set_scratch(scratch);
424 
425  if (input && !input->has_context())
426  input->set_scratch(scratch);
427 
428  if (unpacker)
429  unpacker->set_scratch(scratch);
430 }
431 
432 template<class UnT,class InT>
433 void dsp::UnpackerSource<UnT,InT>::set_cerr (std::ostream& os) const
434 {
435  Operation::set_cerr( os );
436 
437  if (Operation::verbose)
438  this->cerr << "dsp::dsp::UnpackerSource<UnT,InT>::set_cerr" << std::endl;
439 
440  if (input && !input->has_context())
441  input->set_cerr( os );
442 
443  if (bit_container)
444  bit_container->set_cerr( os );
445 
446  if (unpacker)
447  unpacker->set_cerr( os );
448 }
449 
450 #endif // !defined(__dsp_Kernel_Classes_UnpackerSource_h)
void set_input(InputType *input)
Set the InputType operator (should not normally need to be used)
Definition: UnpackerSource.h:216
Produces TimeSeries data by integrating an InputType with an UnpackerType.
Definition: UnpackerSource.h:25
TimeSeries * get_output()
Get the TimeSeries object used to store output data.
Definition: UnpackerSource.h:204
Contains all Baseband Data Reduction Library classes.
Definition: ASCIIObservation.h:17
void set_output_order(TimeSeries::Order)
Set the order of the dimensions in the output TimeSeries.
Definition: UnpackerSource.h:163
Reference::To< UnpackerType > unpacker
Appropriate UnpackerType subclass.
Definition: UnpackerSource.h:127
Reference::To< Operation > post_load_operation
Optional operation performed between load and unpack.
Definition: UnpackerSource.h:136
Source objects that have an input Type.
Definition: InputSource.h:26
void reserve()
Reserve the maximum amount of output space required.
Definition: UnpackerSource.h:287
Order
Order of the dimensions.
Definition: TimeSeries.h:39
void set_output(TimeSeries *output)
Set the TimeSeries object used to store output data.
Definition: UnpackerSource.h:188
void set_post_load_operation(Operation *op)
Set custom post load operation.
Definition: UnpackerSource.h:384
virtual void load(TimeSeries *data)
Load and convert the next block of data.
Definition: UnpackerSource.h:254
void add_extensions(Extensions *)
Add any input and unpacker extensions.
Definition: UnpackerSource.h:302
Reference::To< InputType > input
Appropriate InputType subclass.
Definition: UnpackerSource.h:124
Reference::To< TimeSeries > output
The container in which the TimeSeries data is unpacked.
Definition: UnpackerSource.h:133
std::string name
Operation name.
Definition: Operation.h:153
Scratch * scratch
Shared scratch space, if needed.
Definition: Operation.h:140
Scratch space that can be shared between Operations.
Definition: Scratch.h:27
uint64_t bytes_storage() const override
The number of bytes of additional storage used by the operation.
Definition: UnpackerSource.h:351
Defines the interface by which operations are performed on data.
Definition: Operation.h:37
bool has_output() const
Return true if this object has a TimeSeries object to store output data.
Definition: UnpackerSource.h:210
uint64_t bytes_scratch() const override
The number of bytes of scratch space used by the operation.
Definition: UnpackerSource.h:357
Arrays of consecutive samples for each polarization and frequency channel.
Definition: TimeSeries.h:29
void combine(const Operation *)
Combine the input and unpacker.
Definition: UnpackerSource.h:313
virtual void operation()
Load the TimeSeries specified with set_output.
Definition: UnpackerSource.h:363
void reset()
Reset the input and unpacker.
Definition: UnpackerSource.h:329
virtual void set_bit_container(BitContainerType *)
Set the container into which intermediate raw data will be loaded.
Definition: UnpackerSource.h:172
void report() const
Report operation statistics.
Definition: UnpackerSource.h:340
virtual void set_scratch(Scratch *scratch)
Set the scratch space.
Definition: UnpackerSource.h:418
bool get_order_supported(TimeSeries::Order) const
Return true if the source supports the specified output order.
Definition: UnpackerSource.h:154
bool get_device_supported(Memory *) const
Return true if the unpacker can operate on the specified device.
Definition: UnpackerSource.h:131
virtual void set_cerr(std::ostream &os) const
Set verbosity ostream.
Definition: UnpackerSource.h:433
void prepare()
Prepare the input and unpacker.
Definition: UnpackerSource.h:265
void set_device(Memory *)
Set the device on which the unpacker will operate.
Definition: UnpackerSource.h:137
Reference::To< BitContainerType > bit_container
The container in which the intermediate raw data is loaded.
Definition: UnpackerSource.h:130
const InputType * get_input() const
Return pointer to the appropriate InputType.
Definition: UnpackerSource.h:77
UnpackerSource(const char *name)
Constructor.
Definition: UnpackerSource.h:38
const UnpackerType * get_unpacker() const
Return pointer to the appropriate UnpackerType.
Definition: UnpackerSource.h:242
Pure virtual base class of objects that manage memory allocation and destruction.
Definition: Memory.h:23
uint64_t get_resolution() const
Get the minimum number of time samples that can be output by the source.
Definition: UnpackerSource.h:392
void set_unpacker(UnpackerType *unpacker)
Set the UnpackerType (should not normally need to be used)
Definition: UnpackerSource.h:228

Generated using doxygen 1.8.17