Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
Sps.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "cheetah/data/SpCandidate.h"
26 #include "cheetah/cuda_utils/cuda_errorhandling.h"
27 #include "cheetah/data/DmTrialsMetadata.h"
28 #include "cheetah/data/DmTrials.h"
29 #include "cheetah/data/SpCcl.h"
30 #include "cheetah/data/TimeFrequency.h"
31 #include "cheetah/data/Units.h"
32 #include "cheetah/data/DedispersionMeasure.h"
33 #include "cheetah/cuda_utils/nvtx.h"
34 #include "cheetah/ddtr/DedispersionTrialPlan.h"
35 #include "panda/Resource.h"
36 #include "panda/Log.h"
37 #include "panda/Error.h"
38 #include <cstdlib>
39 
40 namespace ska {
41 namespace cheetah {
42 namespace sps {
43 namespace emulator {
44 
45 template <class SpsTraits>
46 Sps<SpsTraits>::Sps(sps::Config const& config)
47  : _config(config)
48  , _max_delay(0)
49 {
50 }
51 
52 template<class SpsTraits>
53 Sps<SpsTraits>::Sps(Sps&& other)
54  : _dm_trial_metadata(std::move(other._dm_trial_metadata))
55  , _config(other._config)
56  , _max_delay(other._max_delay)
57  , _dm_factors(std::move(other._dm_factors))
58 {
59 }
60 
61 template <class SpsTraits>
62 Sps<SpsTraits>::~Sps()
63 {
64 }
65 
66 template <class SpsTraits>
67 std::size_t Sps<SpsTraits>::set_dedispersion_strategy(std::size_t memory_limit, TimeFrequencyType const& tf)
68 {
69  std::size_t samples = _config.dedispersion_samples();
70  _max_delay = _config.maximum_delay_offset(tf);
71  if(memory_limit > 0)
72  {
73  if( memory_limit < _max_delay) {
74  PANDA_LOG_WARN << "reducing dedispersion buffer size due to memory limitation provided (" << memory_limit << ")";
75  _max_delay = memory_limit - 1;
76  samples = memory_limit;
77  }
78  if( memory_limit < samples ) {
79  PANDA_LOG_WARN << "reducing dedispersion buffer size due to memory limitation provided (" << memory_limit << ")";
80  samples = memory_limit;
81  }
82  }
83  _dm_trial_metadata = _config.generate_dmtrials_metadata(tf.sample_interval(), samples, _max_delay);
84  _dedispersion_samples = samples;
85  return samples;
86 }
87 
88 template <class SpsTraits>
89 template <typename DmHandler, typename SpHandler>
90 void Sps<SpsTraits>::operator()(panda::PoolResource<cheetah::Cpu>&, BufferType& data, DmHandler& dm_handler, SpHandler& sp_handler)
91 {
92  if(data.composition().empty()) return;
93 
94  auto const& tf_obj = *(data.composition().front());
95  std::size_t nchans = tf_obj.number_of_channels();
96 
97  // DmTrials type
98  std::size_t nsamples = data.data_size() / nchans;
99  if(nsamples != _dedispersion_samples)
100  {
101  try
102  {
103  _dm_trial_metadata = _config.generate_dmtrials_metadata(tf_obj.sample_interval(), nsamples, _max_delay);
104  }
105  catch (const std::exception& e)
106  {
107  PANDA_LOG_ERROR << "Exception caught: " << e.what();
108  return;
109  }
110  }
111 
112  data::DimensionIndex<data::Time> offset_samples(data.offset_first_block()/(nchans * sizeof(NumericalRep)));
113  auto const& start_time = tf_obj.start_time(offset_samples);
114  std::shared_ptr<DmTrialsType> dm_trials_ptr = DmTrialsType::make_shared(_dm_trial_metadata, start_time);
115 
116  dm_handler(dm_trials_ptr);
117 
118  // SpCcl list
119  std::shared_ptr<data::SpCcl<NumericalRep>> sp_candidate_list = std::make_shared<data::SpCcl<NumericalRep>>(data.composition(), offset_samples);
120 
121  std::srand(time(NULL));
122  std::size_t ncands = 0.5 + (_config.emulator_config().candidate_rate() * (nsamples * tf_obj.sample_interval())) / boost::units::si::seconds;
123 
124  sp_candidate_list->reserve(ncands);
125 
126  for (std::size_t idx=0; idx<ncands; ++idx)
127  {
128  DmTrialsType& dm_trials = *(dm_trials_ptr);
129  std::size_t dmindex = std::rand() % (_config.dm_trials().size() - 1);
130  data::DmTrials<ska::panda::Cpu, float>::DmTrialType current_trial_dm = dm_trials[dmindex];
131  data::DmTrialsMetadata::DmType this_dm = current_trial_dm.dm();
132 
133  float this_width = rand() % 9 + 1;
134 
135  std::size_t block_range = nsamples - offset_samples;
136  std::size_t this_sample = rand() % (block_range - 1);
137 
138  float sigma = rand() % 90 + 10;
139 
140  typename data::SpCcl<NumericalRep>::SpCandidateType::Dm dm(this_dm);
141  typename data::SpCcl<NumericalRep>::SpCandidateType::MsecTimeType width(this_width * boost::units::si::milli * boost::units::si::seconds);
142  typename data::SpCcl<NumericalRep>::SpCandidateType::MsecTimeType start_ms(this_sample * tf_obj.sample_interval());
143 
144  typename data::SpCcl<NumericalRep>::SpCandidateType candidate(dm, start_ms, width, sigma, idx);
145  sp_candidate_list->emplace_calculate_duration(std::move(candidate));
146  }
147  sp_handler(sp_candidate_list);
148 }
149 
150 } // namespace emulator
151 } // namespace sps
152 } // namespace cheetah
153 } // namespace ska
Some limits and constants for FLDO.
Definition: Brdz.h:35