2
0
mirror of https://github.com/boostorg/compute.git synced 2026-01-31 08:02:16 +00:00

Merge pull request #224 from kylelutz/capture-containers-with-closure

Capture containers with closure
This commit is contained in:
Kyle Lutz
2014-08-09 09:00:43 -07:00
5 changed files with 221 additions and 15 deletions

View File

@@ -16,6 +16,7 @@
#include <boost/config.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/typeof/typeof.hpp>
@@ -27,6 +28,7 @@
#include <boost/compute/cl.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/type_traits/detail/capture_traits.hpp>
namespace boost {
namespace compute {
@@ -126,12 +128,6 @@ public:
return m_source;
}
/// \internal_
void recapture(const CaptureTuple &capture)
{
m_capture = capture;
}
/// \internal_
void define(std::string name, std::string value = std::string())
{
@@ -224,21 +220,24 @@ struct closure_signature_argument_inserter
}
template<class T>
void operator()(const T&)
void operator()(const T&) const
{
BOOST_ASSERT(n < m_capture_names.size());
// remove leading and trailing whitespace from variable name
boost::trim(m_capture_names[n]);
// get captured variable name
std::string variable_name = m_capture_names[n];
s << type_name<T>() << " " << m_capture_names[n];
// remove leading and trailing whitespace from variable name
boost::trim(variable_name);
s << capture_traits<T>::type_name() << " " << variable_name;
if(n+1 < m_last){
s << ", ";
}
n++;
}
size_t n;
mutable size_t n;
size_t m_last;
std::vector<std::string> m_capture_names;
std::stringstream &s;
@@ -248,7 +247,7 @@ template<class Signature, class CaptureTuple>
inline std::string
make_closure_declaration(const char *name,
const char *arguments,
const CaptureTuple&,
const CaptureTuple &capture_tuple,
const char *capture_string)
{
typedef typename
@@ -273,7 +272,7 @@ make_closure_declaration(const char *name,
closure_signature_argument_inserter j(
s, capture_string, boost::tuples::length<CaptureTuple>::value
);
mpl::for_each<CaptureTuple>(j);
fusion::for_each(capture_tuple, j);
s << ")";
return s.str();
@@ -336,12 +335,12 @@ make_closure_impl(const char *name,
#else
#define BOOST_COMPUTE_CLOSURE(return_type, name, arguments, capture, ...) \
::boost::compute::closure< \
return_type arguments, BOOST_TYPEOF(boost::make_tuple capture) \
return_type arguments, BOOST_TYPEOF(boost::tie capture) \
> name = \
::boost::compute::detail::make_closure_impl< \
return_type arguments \
>( \
#name, #arguments, boost::make_tuple capture, #capture, #__VA_ARGS__ \
#name, #arguments, boost::tie capture, #capture, #__VA_ARGS__ \
)
#endif

View File

@@ -23,6 +23,7 @@
#include <boost/compute/algorithm/fill.hpp>
#include <boost/compute/algorithm/swap_ranges.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
#include <boost/compute/type_traits/detail/capture_traits.hpp>
#include <boost/compute/detail/buffer_value.hpp>
namespace boost {
@@ -244,6 +245,23 @@ struct set_kernel_arg<array<T, N> >
}
};
// for capturing array<T, N> with BOOST_COMPUTE_CLOSURE()
template<class T, size_t N>
struct capture_traits<array<T, N> >
{
static std::string type_name()
{
return std::string("__global ") + ::boost::compute::type_name<T>() + "*";
}
};
// meta_kernel streaming operator for array<T, N>
template<class T, size_t N>
meta_kernel& operator<<(meta_kernel &k, const array<T, N> &array)
{
return k << k.get_buffer_identifier<T>(array.get_buffer());
}
} // end detail namespace
} // end compute namespace
} // end boost namespace

View File

@@ -35,6 +35,7 @@
#include <boost/compute/algorithm/fill_n.hpp>
#include <boost/compute/container/allocator.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
#include <boost/compute/type_traits/detail/capture_traits.hpp>
#include <boost/compute/detail/buffer_value.hpp>
#include <boost/compute/detail/iterator_range_size.hpp>
@@ -721,6 +722,23 @@ struct set_kernel_arg<vector<T, Alloc> >
}
};
// for capturing vector<T> with BOOST_COMPUTE_CLOSURE()
template<class T, class Alloc>
struct capture_traits<vector<T, Alloc> >
{
static std::string type_name()
{
return std::string("__global ") + ::boost::compute::type_name<T>() + "*";
}
};
// meta_kernel streaming operator for vector<T>
template<class T, class Alloc>
meta_kernel& operator<<(meta_kernel &k, const vector<T, Alloc> &vector)
{
return k << k.get_buffer_identifier<T>(vector.get_buffer());
}
} // end detail namespace
} // end compute namespace
} // end boost namespace

View File

@@ -0,0 +1,33 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_TYPE_TRAITS_DETAIL_CAPTURE_TRAITS_HPP
#define BOOST_COMPUTE_TYPE_TRAITS_DETAIL_CAPTURE_TRAITS_HPP
#include <boost/compute/type_traits/type_name.hpp>
namespace boost {
namespace compute {
namespace detail {
template<class T>
struct capture_traits
{
static std::string type_name()
{
return ::boost::compute::type_name<T>();
}
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_TYPE_TRAITS_DETAIL_CAPTURE_TRAITS_HPP