vSMC
vSMC: Scalable Monte Carlo
cl_copy.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/opencl/internal/cl_copy.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_OPENCL_INTERNAL_COPY_HPP
33 #define VSMC_OPENCL_INTERNAL_COPY_HPP
34 
35 #include <vsmc/internal/common.hpp>
38 
39 namespace vsmc { namespace internal {
40 
41 template <typename ID>
42 class CLCopy
43 {
44  public :
45 
47 
48  CLCopy () : size_(0) {}
49 
50  std::size_t size () {return size_;}
51 
52  static manager_type &manager() {return manager_type::instance();}
53 
54  void operator() (const ::cl::Buffer &copy_from, const ::cl::Buffer &state)
55  {
56  cl_set_kernel_args(kernel_, 0, copy_from, state);
57  manager().run_kernel(kernel_, size_, configure_.local_size());
58  }
59 
60  void operator () (const ::cl::Buffer &idx, const ::cl::Buffer &tmp,
61  const ::cl::Buffer &state)
62  {
63  cl_set_kernel_args(kernel_post_, 0, idx, tmp, state);
64  manager().run_kernel(kernel_, size_, configure_post_.local_size());
65  }
66 
67  void build (std::size_t size, std::size_t state_size)
68  {
69  size_ = size;
70 
71  std::stringstream ss;
72 
73  ss << "#define Size " << size << "UL\n";
74  ss << "typedef struct {char c[" << state_size << "];} state_type;";
75 
76  ss << "__kernel void copy (__global const ulong *copy_from,\n";
77  ss << " __global state_type *state)\n";
78  ss << "{\n";
79  ss << " ulong to = get_global_id(0);\n";
80  ss << " if (to >= Size) return;\n";
81  ss << " ulong from = copy_from[to];\n";
82  ss << " if (to == from) return;\n";
83  ss << " state[to] = state[from];\n";
84  ss << "}\n";
85 
86  ss << "__kernel void copy_post (__global const char *idx,\n";
87  ss << " __global const state_type *tmp,\n";
88  ss << " __global state_type *state)\n";
89  ss << "{\n";
90  ss << " ulong id = get_global_id(0);\n";
91  ss << " if (id >= Size) return;\n";
92  ss << " if (idx[id] != 0) state[id] = tmp[id];\n";
93  ss << "}\n";
94 
95  program_ = manager().create_program(ss.str());
96  program_.build(manager().device_vec());
97  kernel_ = ::cl::Kernel(program_, "copy");
98  kernel_post_ = ::cl::Kernel(program_, "copy_post");
99  configure_.local_size(size, kernel_, manager().device());
100  configure_post_.local_size(size, kernel_post_, manager().device());
101  }
102 
103  ::cl::Program &program () {return program_;}
104  const ::cl::Program &program () const {return program_;}
105 
106  ::cl::Kernel &kernel () {return kernel_;}
107  const ::cl::Kernel &kernel () const {return kernel_;}
108 
109  CLConfigure &configure () {return configure_;}
110  const CLConfigure &configure () const {return configure_;}
111 
112  private :
113 
114  std::size_t size_;
115 
116  ::cl::Program program_;
117  ::cl::Kernel kernel_;
118  ::cl::Kernel kernel_post_;
119  CLConfigure configure_;
120  CLConfigure configure_post_;
121 }; // class CLCopy
122 
123 } } // namespace vsmc::internal
124 
125 #endif // VSMC_OPENCL_INTERNAL_COPY_HPP
Definition: adapter.hpp:37
CLConfigure & configure()
Definition: cl_copy.hpp:109
void build(std::size_t size, std::size_t state_size)
Definition: cl_copy.hpp:67
::cl::Kernel & kernel()
Definition: cl_copy.hpp:106
const ::cl::Program & program() const
Definition: cl_copy.hpp:104
CLManager< ID > manager_type
Definition: cl_copy.hpp:46
::cl::Program & program()
Definition: cl_copy.hpp:103
static manager_type & manager()
Definition: cl_copy.hpp:52
void cl_set_kernel_args(::cl::Kernel &,::cl_uint)
Definition: cl_manip.hpp:129
void operator()(const ::cl::Buffer &copy_from, const ::cl::Buffer &state)
Definition: cl_copy.hpp:54
const CLConfigure & configure() const
Definition: cl_copy.hpp:110
void run_kernel(const ::cl::Kernel &kern, std::size_t N, std::size_t local_size=0, const std::vector< ::cl::Event > *events=nullptr,::cl::Event *event=nullptr, bool block=true) const
Run a given kernel with one dimensional global size and local size on the current command queue...
Definition: cl_manager.hpp:348
std::size_t local_size() const
Configure OpenCL runtime behavior (used by MoveCL etc)
OpenCL Manager.
Definition: cl_manager.hpp:129
::cl::Program create_program(const std::string &source) const
Create a program given the source within the current context.
Definition: cl_manager.hpp:448
static CLManager< ID > & instance()
Get an instance of the manager singleton.
Definition: cl_manager.hpp:136
const ::cl::Kernel & kernel() const
Definition: cl_copy.hpp:107
std::size_t size()
Definition: cl_copy.hpp:50