Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
AccelerationSearchTest.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 #include "cheetah/pipeline/test/AccelerationSearchTest.h"
25 #include "cheetah/pipeline/AccelerationSearch.h"
26 #include "cheetah/pipeline/CheetahConfig.h"
27 #include "cheetah/data/TimeFrequency.h"
28 #include "cheetah/data/Units.h"
29 #include "cheetah/data/DedispersionMeasure.h"
30 #include "cheetah/ddtr/DedispersionConfig.h"
31 #include "cheetah/generators/GaussianNoise.h"
32 #include "cheetah/generators/GaussianNoiseConfig.h"
33 #include <memory>
34 #include <chrono>
35 #include <thread>
36 
37 namespace ska {
38 namespace cheetah {
39 namespace pipeline {
40 namespace test {
41 
42 AccelerationSearchTest::AccelerationSearchTest()
43  : ::testing::Test()
44 {
45 }
46 
47 AccelerationSearchTest::~AccelerationSearchTest()
48 {
49 }
50 
51 void AccelerationSearchTest::SetUp()
52 {
53 }
54 
55 void AccelerationSearchTest::TearDown()
56 {
57 }
58 
59 
60 template<typename NumericalT>
61 template<typename SiftHandlerT>
62 typename TestAccelerationSearchTraitsBase<NumericalT>::template AccelerationTestHandler<SiftHandlerT>* TestAccelerationSearchTraitsBase<NumericalT>::create_acceleration_search_algo(AccelerationSearchAlgoConfig const&, SiftHandlerT& handler)
63 {
64  return new AccelerationTestHandler<SiftHandlerT>(handler);
65 }
66 
67 template<typename NumericalT>
68 template<typename SiftHandlerT>
69 TestAccelerationSearchTraitsBase<NumericalT>::AccelerationTestHandler<SiftHandlerT>::AccelerationTestHandler(SiftHandlerT& handler)
70  : _handler(handler)
71 {
72 }
73 
74 template<typename NumericalT>
75 template<typename SiftHandlerT>
76 void TestAccelerationSearchTraitsBase<NumericalT>::AccelerationTestHandler<SiftHandlerT>::operator()(std::shared_ptr<TestAccelerationSearchTraitsBase<NumericalT>::DmTimeType> const& data)
77 {
78  std::lock_guard<std::mutex> lock(_acceleration_data_mutex);
79  _received_data.push_back(data);
80  std::shared_ptr<data::Ccl> output_data(new data::Ccl());
81  _handler(output_data);
82  _sift_handler_called = true;
83  _acceleration_wait.notify_all();
84 }
85 
86 template<typename NumericalT>
87 template<typename SiftHandlerT>
88 bool TestAccelerationSearchTraitsBase<NumericalT>::AccelerationTestHandler<SiftHandlerT>::wait_sift_handler_called() const
89 {
90  std::unique_lock<std::mutex> lock(_acceleration_data_mutex);
91  _acceleration_wait.wait_for(lock, std::chrono::seconds(5), [this] { return _sift_handler_called; });
92  return _sift_handler_called;
93 }
94 
95 template<typename NumericalT>
96 TestAccelerationSearchTraits<NumericalT>::TestHandler::TestHandler(CheetahConfig<NumericalT> const& config, BeamConfig<NumericalT> const& beam_config, DedispersionHandler dm_handler)
97  : BaseT(config, beam_config, dm_handler)
98 {
99 }
100 
101 template<typename NumericalT>
103 {
104  std::lock_guard<std::mutex> lock(_dedispersion_data_mutex);
105  _received_tf_data.push_back(data.shared_from_this());
106  std::shared_ptr<data::DmTrialsMetadata> dm_trial_metadata = std::make_shared<data::DmTrialsMetadata>(typename data::DmTrialsMetadata::TimeType(data.sample_interval()), 1);
107  dm_trial_metadata->emplace_back(10 * data::parsecs_per_cube_cm);
108  std::shared_ptr<DmTrialsType> dm_trials_ptr = DmTrialsType::make_shared(dm_trial_metadata, data.start_time());
109  this->_dm_handler(dm_trials_ptr);
110  _dm_handler_called = true;
111  _dedispersion_wait.notify_all();
112 }
113 
114 template<typename NumericalT>
116 {
117  std::unique_lock<std::mutex> lock(_dedispersion_data_mutex);
118  _dedispersion_wait.wait_for(lock, std::chrono::seconds(5), [this] { return _dm_handler_called; });
119  return _dm_handler_called;
120 }
121 
122 template<typename NumericalT>
123 void TestAccelerationSearchTraits<NumericalT>::FldoTestHandler::operator()(std::shared_ptr<data::Ocld> const& data)
124 {
125  std::lock_guard<std::mutex> lock(_fldo_data_mutex);
126  _candidate_data.push_back(data);
127  _fldo_handler_called = true;
128  _fldo_wait.notify_all();
129 }
130 
131 template<typename NumericalT>
133 {
134  std::unique_lock<std::mutex> lock(_fldo_data_mutex);
135  _fldo_wait.wait_for(lock, std::chrono::seconds(5), [this] { return _fldo_handler_called; });
136  return _fldo_handler_called;
137 }
138 
139 template<typename NumericalT>
141 {
142  return new FldoTestHandler;
143 }
144 
145 
146 template<typename NumericalT>
147 template<typename DmHandlerT>
149 {
150  return new TestAccelerationSearchTraits<NumericalT>::TestHandler(config, beam_config, dm_handler);
151 }
152 
153 namespace {
154 template<typename NumericalT, typename AccelerationSearchTraitsT>
155 class TestAccelerationSearch : public AccelerationSearch<NumericalT, AccelerationSearchTraitsT>
156 {
158 
159  public:
160  template<typename T>
161  using has_create_fldo_handler_t = typename BaseT::template has_create_fldo_handler_t<T>;
162 };
163 
164 class AccelerationSearchTraits_A : public TestAccelerationSearchTraitsBase<uint8_t>
165 {
166 };
167 
168 struct AccelerationSearchTraits_B : public AccelerationSearchTraits_A
169 {
170  typedef uint8_t NumericalT;
171  struct FldoTestHandler;
173 };
174 } // namespace
175 
176 TEST_F(AccelerationSearchTest, has_create_fldo_handler_test)
177 {
178  typedef TestAccelerationSearch<uint8_t, AccelerationSearchTraits_A> ClassTypeA;
179  static_assert(!panda::HasMethod<AccelerationSearchTraits_A, ClassTypeA::has_create_fldo_handler_t>::value,"");
180 
181  typedef TestAccelerationSearch<uint8_t, AccelerationSearchTraits_B> ClassTypeB;
182  static_assert(panda::HasMethod<AccelerationSearchTraits_B, ClassTypeB::has_create_fldo_handler_t>::value,"");
183  static_assert(std::is_same<pipeline::detail::FldoHandler<ClassTypeB>::FldoHandlerType, typename AccelerationSearchTraits_B::FldoTestHandler>::value, "");
184 
185  typedef TestAccelerationSearch<uint8_t, TestAccelerationSearchTraits<uint8_t>> ClassTypeC;
186  static_assert(panda::HasMethod<TestAccelerationSearchTraits<uint8_t>, ClassTypeC::has_create_fldo_handler_t>::value,"");
187 
189 }
190 
191 template<typename NumericalT, typename AccelerationSearchTraitsT>
193 {
194  static void run()
195  {
196  // Search ~600 seconds of data at standard resoluton
197  typedef data::TimeFrequency<Cpu, NumericalT> TimeFrequencyType;
198  typedef typename TimeFrequencyType::DataType DataType;
199 
200  // Configure components
202  BeamConfig<NumericalT> beam_config;
203 
204  // Configure high-density low DMs
205  ddtr::DedispersionConfig dd_config_low;
206  config.sps_config().dedispersion_config(dd_config_low);
207  dd_config_low.dm_start(ddtr::DedispersionConfig::Dm(0.0 * data::parsecs_per_cube_cm));
208  dd_config_low.dm_end(ddtr::DedispersionConfig::Dm(100.0 * data::parsecs_per_cube_cm));
209  dd_config_low.dm_step(ddtr::DedispersionConfig::Dm(0.1 * data::parsecs_per_cube_cm));
210 
211 /*
212  // Configure low-density high DMs
213  ddtr::DedispersionConfig dd_config_high;
214  config.sps_config().dedispersion_config(dd_config_high);
215  dd_config_high.dm_start(ddtr::DedispersionConfig::Dm(200.0 * data::parsecs_per_cube_cm));
216  dd_config_high.dm_end(ddtr::DedispersionConfig::Dm(300.0 * data::parsecs_per_cube_cm));
217  dd_config_high.dm_step(ddtr::DedispersionConfig::Dm(1.0 * data::parsecs_per_cube_cm));
218 */
219 
220  // Set sps priority
221  config.sps_config().set_priority(0);
222 
223  // Set the size of the dedispersion buffer
224  config.sps_config().set_dedispersion_samples(1<<18);
225 
226  // Set the psbc buffer size via the dump_time in the psbc config, i.e. psbc will flush its buffer after this amount of time
227  config.psbc_config().dump_time(0.0001 * data::seconds);
228 
229  // select the sift algo
230  config.sift_config().template config<sift::simple_sift::Config>().active(true);
231 
232  // select the fldo algo
233  config.fldo_config().template config<fldo::cuda::Config>().active(true);
234 
235  // Create a templated AccelerationSearch object
237  AccelerationSearchType pipeline(config, beam_config);
238 
239 
240  // Set up noise parameters for data to be passed through the pipeline
241  generators::GaussianNoiseConfig noise_config;
242  generators::GaussianNoise<DataType> noise(noise_config);
243  noise_config.mean(96.0);
244  noise_config.std_deviation(10.0);
245 
246  // Start epoch
247  typename utils::ModifiedJulianClock::time_point epoch(utils::julian_day(50000.0));
248 
249  double tsamp_us = 64.0;
250  double f_low = 1.2;
251  double f_high = 1.8;
252  std::size_t total_nsamps = std::size_t(1.0 / (tsamp_us * 1e-6));
253  data::DimensionSize<data::Time> number_of_samples(1000);
254  data::DimensionSize<data::Frequency> number_of_channels(1024);
255  std::size_t loop_count = total_nsamps/number_of_samples + 10;
256  for (std::size_t ii=0; ii<loop_count; ++ii)
257  {
258  auto tf = std::make_shared<TimeFrequencyType>(number_of_samples, number_of_channels);
259  auto f1 = typename TimeFrequencyType::FrequencyType(f_high * boost::units::si::giga * boost::units::si::hertz);
260  auto f2 = typename TimeFrequencyType::FrequencyType(f_low * boost::units::si::giga * boost::units::si::hertz);
261  auto delta = (f2 - f1)/ (double)number_of_channels;
262  tf->set_channel_frequencies_const_width( f1, delta );
263  tf->sample_interval(typename TimeFrequencyType::TimeType(tsamp_us * boost::units::si::micro * data::seconds));
264  tf->start_time(epoch);
265  epoch += std::chrono::duration<double>(tf->sample_interval().value()*number_of_samples);
266  noise.next(*tf);
267  pipeline(*tf);
268  }
269 
270  auto const& acceleration_search_pipeline_object = pipeline.acceleration_search_pipeline();
271  typename TestAccelerationSearchTraits<NumericalT>::TestHandler const& dedispersion_pipeline_object = static_cast<typename TestAccelerationSearchTraits<NumericalT>::TestHandler const&>(pipeline.dedispersion_pipeline());
272  typename TestAccelerationSearchTraits<NumericalT>::FldoTestHandler const& fldo_pipeline_object = pipeline.fldo_handler();
273 
274  ASSERT_TRUE(acceleration_search_pipeline_object.wait_sift_handler_called());
275  ASSERT_TRUE(dedispersion_pipeline_object.wait_dedispersion_handler_called());
276  ASSERT_TRUE(fldo_pipeline_object.wait_fldo_handler_called());
277  }
278 };
279 
280 TEST_F(AccelerationSearchTest, simple_run_test_uint8)
281 {
283 }
284 
285 /*
286 TEST_F(AccelerationSearchTest, simple_run_test_uint16)
287 {
288  AccelerationSearchTests<uint16_t, TestAccelerationSearchTraits<uint16_t>>::run();
289 }
290 */
291 
292 } // namespace test
293 } // namespace pipeline
294 } // namespace cheetah
295 } // namespace ska
sift::ConfigType const & sift_config() const
return the sift module specific configuration parameters
The acceleration search pipeline.
Dm dm_start() const
the first dm condidate to try
describes dedispersion configuration
psbc::Config const & psbc_config() const
return the psbc module specific configuration parameters
TimePointType const & start_time() const
Some limits and constants for FLDO.
Definition: Brdz.h:35
Dm dm_end() const
the last dm condidate to try
sps::ConfigType< PoolManagerType > const & sps_config() const
return the sps module specific configuration parameters
Parse configuration parameters for a single beam in the pipeline instance of cheetah.
Definition: BeamConfig.h:48
Base class for Cheetah Pipeline Handlers.
Definition: Pipeline.h:37
fldo::ConfigType const & fldo_config() const
return the fldo module specific configuration parameters
Dm dm_step() const
the step size to take in generating dm trials between start and end
void operator()(typename BaseT::TimeFrequencyType &) override
called each time data becomes available