HPXCL
|
00001 // Copyright (c) 2013 Martin Stumpf 00002 // 00003 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00005 00006 #pragma once 00007 #ifndef HPX_OPENCL_KERNEL_HPP_ 00008 #define HPX_OPENCL_KERNEL_HPP_ 00009 00010 // Default includes 00011 #include <hpx/hpx.hpp> 00012 #include <hpx/config.hpp> 00013 00014 // Export definitions 00015 #include "export_definitions.hpp" 00016 00017 // Forward Declarations 00018 #include "fwd_declarations.hpp" 00019 00020 // OpenCL Headers 00021 #include "cl_headers.hpp" 00022 00023 // Crazy function overloading 00024 #include "util/enqueue_overloads.hpp" 00025 00026 00027 namespace hpx { 00028 namespace opencl { 00029 00055 template <std::size_t DIM> 00056 struct work_size 00057 { 00058 private: 00059 struct dimension 00060 { 00061 std::size_t offset; 00062 std::size_t size; 00063 std::size_t local_size; 00064 dimension(){ 00065 offset = 0; 00066 size = 0; 00067 local_size = 0; 00068 } 00069 }; 00070 private: 00071 // local_size be treated as NULL if all dimensions have local_size == 0 00072 dimension dims[DIM]; 00073 public: 00074 dimension& operator[](std::size_t idx){ return dims[idx]; } 00075 }; 00076 00082 class HPX_OPENCL_EXPORT kernel 00083 : public hpx::components::client_base<kernel, server::kernel> 00084 { 00085 00086 typedef hpx::components::client_base<kernel, server::kernel> base_type; 00087 00088 public: 00089 // Empty constructor, necessary for hpx purposes 00090 kernel(){} 00091 00092 // Constructor 00093 kernel(hpx::shared_future<hpx::naming::id_type> const& gid, 00094 hpx::naming::id_type device_gid_) 00095 : base_type(gid), device_gid(std::move(device_gid_)) 00096 {} 00097 00098 // initialization 00099 00100 00101 // /////////////////////////////////////////////// 00102 // Exposed Component functionality 00103 // 00104 00115 hpx::lcos::future<void> 00116 set_arg_async(cl_uint arg_index, const hpx::opencl::buffer &arg) const; 00117 00126 void 00127 set_arg(cl_uint arg_index, const hpx::opencl::buffer &arg) const; 00128 00137 template<std::size_t DIM, typename ...Deps> 00138 hpx::lcos::future<void> 00139 enqueue( hpx::opencl::work_size<DIM> size, 00140 Deps &&... dependencies ) const; 00141 00142 hpx::lcos::future<void> 00143 enqueue_impl( std::vector<std::size_t> && size_vec, 00144 hpx::opencl::util::resolved_events && deps ) const; 00145 00146 00147 private: 00148 hpx::naming::id_type device_gid; 00149 00150 private: 00151 // serialization support 00152 friend class hpx::serialization::access; 00153 00154 template <typename Archive> 00155 void serialize(Archive & ar, unsigned) 00156 { 00157 ar & hpx::serialization::base_object<base_type>(*this); 00158 ar & device_gid; 00159 } 00160 00161 }; 00162 00163 }} 00164 00165 00167 // IMPLEMENTATIONS 00168 // 00169 template<std::size_t DIM, typename ...Deps> 00170 hpx::future<void> 00171 hpx::opencl::kernel::enqueue( hpx::opencl::work_size<DIM> size, 00172 Deps &&... dependencies ) const 00173 { 00174 // combine dependency futures in one std::vector 00175 using hpx::opencl::util::enqueue_overloads::resolver; 00176 auto deps = resolver(std::forward<Deps>(dependencies)...); 00177 HPX_ASSERT(deps.are_from_device(device_gid)); 00178 00179 // extract information from work_size struct 00180 std::vector<std::size_t> size_vec(3*DIM); 00181 for(std::size_t i = 0; i < DIM; i++){ 00182 size_vec[i + 0*DIM] = size[i].offset; 00183 size_vec[i + 1*DIM] = size[i].size; 00184 size_vec[i + 2*DIM] = size[i].local_size; 00185 } 00186 00187 // forward to enqueue_impl 00188 return enqueue_impl( std::move(size_vec), std::move(deps) ); 00189 00190 } 00191 00192 #endif