JayBeams  0.1
Another project to have fun coding.
bm_single_fft.cpp
Go to the documentation of this file.
1 #include <jb/clfft/plan.hpp>
6 
7 #include <boost/compute/command_queue.hpp>
8 #include <chrono>
9 #include <iostream>
10 #include <stdexcept>
11 #include <string>
12 
13 /// Helper types and functions for (trivial) benchmarks of clFFT.
14 namespace {
17 } // anonymous namespace
18 
19 int main(int argc, char* argv[]) {
20  auto testcases = create_testcases();
21  return jb::testing::microbenchmark_group_main(argc, argv, testcases);
22 }
23 
24 namespace {
25 // By default test with around one million samples
26 int nsamples = 1 << 20;
27 
28 /**
29  * The fixture for this benchmark.
30  *
31  * @tparam pipelined if true, the copy operations are pipelined with
32  * the computation operations.
33  */
34 template <bool pipelined>
35 class fixture {
36 public:
37  fixture(boost::compute::context& context, boost::compute::command_queue& q)
38  : fixture(nsamples, context, q) {
39  }
40  fixture(
41  int size, boost::compute::context& context,
42  boost::compute::command_queue& q)
43  : src(size)
44  , in(size, context)
45  , out(size, context)
46  , dst(size)
47  , queue(q)
48  , fft(jb::clfft::create_forward_plan_1d(out, in, context, queue)) {
49  }
50 
51  int run() {
52  boost::compute::copy(src.begin(), src.end(), in.begin(), queue);
53  fft.enqueue(out, in, queue).wait();
54  boost::compute::copy(out.begin(), out.end(), dst.begin(), queue);
55  return static_cast<int>(src.size());
56  }
57 
58 private:
59  typedef boost::compute::vector<std::complex<float>> invector;
60  typedef boost::compute::vector<std::complex<float>> outvector;
61  std::vector<std::complex<float>> src;
62  invector in;
63  outvector out;
64  std::vector<std::complex<float>> dst;
65  boost::compute::command_queue queue;
67 };
68 
69 template <>
70 int fixture<true>::run() {
71  using namespace boost::compute;
72  auto upload_done = copy_async(src.begin(), src.end(), in.begin(), queue);
73  auto fft_done =
74  fft.enqueue(out, in, queue, wait_list(upload_done.get_event()));
75  queue.enqueue_barrier();
76  auto download_done = copy_async(out.begin(), out.end(), dst.begin(), queue);
77  download_done.wait();
78  return static_cast<int>(src.size());
79 }
80 
81 template <bool pipelined>
82 std::function<void(config const&)> test_case() {
83  return [](config const& cfg) {
85  boost::compute::device device = jb::opencl::device_selector(cfg.opencl());
86  boost::compute::context context(device);
87  boost::compute::command_queue queue(context, device);
89  benchmark bm(cfg.microbenchmark());
90 
91  auto r = bm.run(context, queue);
92  bm.typical_output(r);
93  };
94 }
95 
98  {"complex:float:async", test_case<true>()},
99  {"complex:float:sync", test_case<false>()},
100  };
101 }
102 
103 } // anonymous namespace
plan< invector, outvector > create_forward_plan_1d(outvector const &out, invector const &in, boost::compute::context &ct, boost::compute::command_queue &q, int batch_size=1)
Create a forward DFT plan.
Definition: plan.hpp:255
Wrap clfftPlanHandle objects in a class that can automatically destroy them.
Definition: init.hpp:13
boost::compute::event enqueue(out_timeseries_type &out, in_timeseries_type const &in, boost::compute::command_queue &queue, boost::compute::wait_list const &wait=boost::compute::wait_list())
Enqueue the transform to be executed.
Definition: plan.hpp:107
boost::compute::device device_selector(config const &cfg)
Select an OpenCL device matching the current configuration.
The configuration shared by all OpenCL microbenchmarks.
results run(Args &&... args)
Run the microbenchmaark.
int microbenchmark_group_main(int argc, char *argv[], microbenchmark_group< microbenchmark_config > const &testcases)
Overload microbenchmark_group_main for jb::testing::microbenchmark_config.
std::map< std::string, std::function< void(config const &cfg)> > microbenchmark_group
Define a representation for a group of microbenchmarks.
Wrap clfftPlanHandle objects.
Definition: plan.hpp:35
void init(config const &cfg)
Initialize the logging functions using the configuration provided.
Definition: log.cpp:190
int main(int argc, char *argv[])
std::size_t nsamples(container_type const &a)
Count the elements in the last dimension of a vector-like container.
Run a micro-benchmark on a given class.