Browse Source

(committing very old stuff) Something with input/output

master
Joshua Moerman 8 years ago
parent
commit
f5d5731500
  1. 2
      CMakeLists.txt
  2. 6
      cl.hpp
  3. 105
      cl2.hpp

2
CMakeLists.txt

@ -10,8 +10,10 @@ add_library(${LIBNAME} ${headers} ${sources})
find_library(OPENCL_LIBRARY OpenCL)
set(libs ${OPENCL_LIBRARY})
set(${LIBNAME}_LIBRARIES ${LIBNAME} ${libs} CACHE INTERNAL "")
target_link_libraries(${LIBNAME} ${libs})
target_include_directories(${LIBNAME} PUBLIC ".")
install(TARGETS ${LIBNAME} DESTINATION lib)
install(FILES ${headers} DESTINATION include/J)

6
cl.hpp

@ -4442,7 +4442,7 @@ typedef T param_type; \
struct KernelArgumentHandler
{
static ::size_t size(const T&) { return sizeof(T); }
static T* ptr(T& value) { return &value; }
static typename std::remove_reference_t<T>* ptr(T& value) { return &value; }
};
template <>
@ -4607,7 +4607,7 @@ typedef T param_type; \
}
template <typename T>
cl_int setArg(cl_uint index, T value)
cl_int setArg(cl_uint index, T&& value)
{
return detail::errHandler(
::clSetKernelArg(
@ -12449,4 +12449,4 @@ typedef T param_type; \
#pragma pop_macro("max")
#endif // _WIN32
#endif // CL_HPP_
#endif // CL_HPP_

105
cl2.hpp

@ -13,64 +13,71 @@
#include <cassert>
#include <iostream>
// debugging tool
static void check(cl_int err){
assert(err == CL_SUCCESS);
namespace cl {
// debugging tool
inline void checky(cl_int err){
assert(err == CL_SUCCESS);
}
}
// simple variadic wrapper for kernels/arguments (1D)
struct KernelOp{
KernelOp(const cl::Program& program, const char* name, cl_int* err)
: kernel(program, name, err)
{}
template <typename... Args>
cl_int operator()(cl::CommandQueue & queue, size_t W, size_t H, Args&&... args){
return apply(queue, W, H, 0, std::forward<Args>(args)...);
}
KernelOp(const cl::Program& program, const char* name, cl_int* err = nullptr)
: kernel(program, name, err)
{}
template <typename... Args>
cl_int operator()(cl::CommandQueue & queue, size_t W, Args&&... args){
return apply(queue, W, 0, std::forward<Args>(args)...);
}
private:
cl::Kernel kernel;
template <typename T, typename... Args>
cl_int apply(cl::CommandQueue & queue, size_t W, size_t H, cl_uint n, T&& t, Args&&... args){
check(kernel.setArg(n, t));
return apply(queue, W, H, n+1, std::forward<Args>(args)...);
}
cl_int apply(cl::CommandQueue & queue, size_t W, size_t H, size_t){
return queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(W, H), cl::NullRange);
}
cl::Kernel kernel;
template <typename T, typename... Args>
cl_int apply(cl::CommandQueue & queue, size_t W, cl_uint n, T&& t, Args&&... args){
cl::checky(kernel.setArg(n, t));
return apply(queue, W, n+1, std::forward<Args>(args)...);
}
cl_int apply(cl::CommandQueue & queue, size_t W, size_t){
return queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(W));
}
};
// We need to put the op<<'s in namespace cl (ADL)
namespace cl {
std::ostream& operator<<(std::ostream& out, Platform const & platform){
return out << platform.getInfo<CL_PLATFORM_NAME>() << ", " << platform.getInfo<CL_PLATFORM_VERSION>();
}
std::ostream& operator<<(std::ostream& out, Device const & device){
out << device.getInfo<CL_DEVICE_NAME>() << ", " << device.getInfo<CL_DEVICE_VERSION>() << ", " << device.getInfo<CL_DRIVER_VERSION>();
return out;
}
std::ostream& operator<<(std::ostream& out, Context const & context){
Platform p;
context.getInfo(CL_CONTEXT_PLATFORM, &p);
out << "Platform:\t" << p << '\n';
auto devices = context.getInfo<CL_CONTEXT_DEVICES>();
out << "Number of devices:\t" << devices.size() << '\n';
int i = 0;
for(auto&& d : devices){
out << ++i << "\t" << d << '\n';
}
return out;
}
std::ostream& operator<<(std::ostream& out, Program const & program){
return out << "Kernels in program:\t" << program.getInfo<CL_PROGRAM_KERNEL_NAMES>();
}
inline std::ostream& operator<<(std::ostream& out, Platform const & platform){
return out << platform.getInfo<CL_PLATFORM_NAME>() << ", " << platform.getInfo<CL_PLATFORM_VERSION>();
}
inline std::ostream& operator<<(std::ostream& out, Device const & device){
out << device.getInfo<CL_DEVICE_NAME>() << ", " << device.getInfo<CL_DEVICE_VERSION>() << ", " << device.getInfo<CL_DRIVER_VERSION>();
return out;
}
inline std::ostream& operator<<(std::ostream& out, Context const & context){
Platform p;
context.getInfo(CL_CONTEXT_PLATFORM, &p);
out << "Platform:\t" << p << '\n';
auto devices = context.getInfo<CL_CONTEXT_DEVICES>();
out << "Number of devices:\t" << devices.size() << '\n';
int i = 0;
for(auto&& d : devices){
out << ++i << "\t" << d << '\n';
}
return out;
}
inline std::ostream& operator<<(std::ostream& out, Program const & program){
return out << "Kernels in program:\t" << program.getInfo<CL_PROGRAM_KERNEL_NAMES>();
}
inline std::ostream& operator<<(std::ostream& out, Image const & image){
out << "Image: " << image.getImageInfo<CL_IMAGE_WIDTH>() << "x" << image.getImageInfo<CL_IMAGE_HEIGHT>();
return out << " elements: " << image.getImageInfo<CL_IMAGE_ELEMENT_SIZE>() << " pitch: " << image.getImageInfo<CL_IMAGE_ROW_PITCH>();
}
}
#endif