mirror of
https://github.com/boostorg/compute.git
synced 2026-01-30 07:42:41 +00:00
Merge pull request #273 from kylelutz/type-definition
Add type_definition<T>() function
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include <boost/compute/type_traits/is_vector_type.hpp>
|
||||
#include <boost/compute/type_traits/make_vector_type.hpp>
|
||||
#include <boost/compute/type_traits/scalar_type.hpp>
|
||||
#include <boost/compute/type_traits/type_definition.hpp>
|
||||
#include <boost/compute/type_traits/type_name.hpp>
|
||||
#include <boost/compute/type_traits/vector_size.hpp>
|
||||
|
||||
|
||||
26
include/boost/compute/type_traits/type_definition.hpp
Normal file
26
include/boost/compute/type_traits/type_definition.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//---------------------------------------------------------------------------//
|
||||
// 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_TYPE_DEFINITION_HPP
|
||||
#define BOOST_COMPUTE_TYPE_TRAITS_TYPE_DEFINITION_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace boost {
|
||||
namespace compute {
|
||||
|
||||
/// Returns the OpenCL type definition for \p T.
|
||||
template<class T>
|
||||
std::string type_definition();
|
||||
|
||||
} // end compute namespace
|
||||
} // end boost namespace
|
||||
|
||||
#endif // BOOST_COMPUTE_TYPE_TRAITS_TYPE_DEFINITION_HPP
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <boost/preprocessor/seq/fold_left.hpp>
|
||||
#include <boost/preprocessor/seq/transform.hpp>
|
||||
|
||||
#include <boost/compute/type_traits/type_definition.hpp>
|
||||
#include <boost/compute/type_traits/type_name.hpp>
|
||||
#include <boost/compute/detail/meta_kernel.hpp>
|
||||
#include <boost/compute/detail/variadic_macros.hpp>
|
||||
@@ -124,21 +125,27 @@ inline std::string adapt_struct_insert_member(T Struct::*, const char *name)
|
||||
"BOOST_COMPUTE_ADAPT_STRUCT() does not support structs with internal padding." \
|
||||
); \
|
||||
BOOST_COMPUTE_TYPE_NAME(type, name) \
|
||||
namespace boost { namespace compute { namespace detail { \
|
||||
namespace boost { namespace compute { \
|
||||
template<> \
|
||||
inline std::string type_definition<type>() \
|
||||
{ \
|
||||
std::stringstream declaration; \
|
||||
declaration << "typedef struct __attribute__((packed)) {\n" \
|
||||
BOOST_PP_SEQ_FOR_EACH( \
|
||||
BOOST_COMPUTE_DETAIL_ADAPT_STRUCT_INSERT_MEMBER, \
|
||||
type, \
|
||||
BOOST_COMPUTE_PP_TUPLE_TO_SEQ(members) \
|
||||
) \
|
||||
<< "} " << type_name<type>() << ";\n"; \
|
||||
return declaration.str(); \
|
||||
} \
|
||||
namespace detail { \
|
||||
template<> \
|
||||
struct inject_type_impl<type> \
|
||||
{ \
|
||||
void operator()(meta_kernel &kernel) \
|
||||
{ \
|
||||
std::stringstream declaration; \
|
||||
declaration << "typedef struct __attribute__((packed)) {\n" \
|
||||
BOOST_PP_SEQ_FOR_EACH( \
|
||||
BOOST_COMPUTE_DETAIL_ADAPT_STRUCT_INSERT_MEMBER, \
|
||||
type, \
|
||||
BOOST_COMPUTE_PP_TUPLE_TO_SEQ(members) \
|
||||
) \
|
||||
<< "} " << type_name<type>() << ";\n"; \
|
||||
kernel.add_type_declaration<type>(declaration.str()); \
|
||||
kernel.add_type_declaration<type>(type_definition<type>()); \
|
||||
} \
|
||||
}; \
|
||||
inline meta_kernel& operator<<(meta_kernel &k, type s) \
|
||||
|
||||
@@ -14,11 +14,14 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <boost/compute/function.hpp>
|
||||
#include <boost/compute/source.hpp>
|
||||
#include <boost/compute/algorithm/find_if.hpp>
|
||||
#include <boost/compute/algorithm/transform.hpp>
|
||||
#include <boost/compute/container/vector.hpp>
|
||||
#include <boost/compute/functional/field.hpp>
|
||||
#include <boost/compute/types/struct.hpp>
|
||||
#include <boost/compute/type_traits/type_name.hpp>
|
||||
#include <boost/compute/type_traits/type_definition.hpp>
|
||||
|
||||
namespace compute = boost::compute;
|
||||
|
||||
@@ -82,4 +85,43 @@ BOOST_AUTO_TEST_CASE(atom_struct)
|
||||
CHECK_RANGE_EQUAL(int, 3, atomic_numbers, (1, 1, 8));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(custom_kernel)
|
||||
{
|
||||
std::vector<chemistry::Atom> data;
|
||||
data.push_back(chemistry::Atom(1.f, 0.f, 0.f, 1));
|
||||
data.push_back(chemistry::Atom(0.f, 1.f, 0.f, 1));
|
||||
data.push_back(chemistry::Atom(0.f, 0.f, 0.f, 8));
|
||||
|
||||
compute::vector<chemistry::Atom> atoms(data.size(), context);
|
||||
compute::copy(data.begin(), data.end(), atoms.begin(), queue);
|
||||
|
||||
std::string source = BOOST_COMPUTE_STRINGIZE_SOURCE(
|
||||
__kernel void custom_kernel(__global const Atom *atoms,
|
||||
__global float *distances)
|
||||
{
|
||||
const uint i = get_global_id(0);
|
||||
const __global Atom *atom = &atoms[i];
|
||||
|
||||
const float4 center = { 0, 0, 0, 0 };
|
||||
const float4 position = { atom->x, atom->y, atom->z, 0 };
|
||||
|
||||
distances[i] = distance(position, center);
|
||||
}
|
||||
);
|
||||
|
||||
// add type definition for Atom to the start of the program source
|
||||
source = compute::type_definition<chemistry::Atom>() + "\n" + source;
|
||||
|
||||
compute::program program =
|
||||
compute::program::build_with_source(source, context);
|
||||
|
||||
compute::vector<float> distances(atoms.size(), context);
|
||||
|
||||
compute::kernel custom_kernel = program.create_kernel("custom_kernel");
|
||||
custom_kernel.set_arg(0, atoms);
|
||||
custom_kernel.set_arg(1, distances);
|
||||
|
||||
queue.enqueue_1d_range_kernel(custom_kernel, 0, atoms.size(), 1);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
Reference in New Issue
Block a user