通过对这些库的粗略理解,它们看起来非常相似.我知道VexCL和Boost.Compute使用OpenCl作为后端(尽管v1.0版本VexCL也支持CUDA作为后端)并且Thrust使用CUDA.除了不同的后端,这些之间的区别是什么.
具体来说,他们解决了什么问题空间,为什么我要使用其中一个.
另外,在Thrust常见问题解答中说明了这一点
OpenCL支持的主要障碍是缺少OpenCL编译器和运行时支持C++模板
如果是这种情况,VexCL和Boost.Compute怎么可能存在.
以下代码分别使用boost.compute和opencl c ++包装器添加两个向量.结果显示boost.compute比opencl c ++包装器慢近20倍.我想知道我是否错过使用boost.compute或它确实很慢.平台:win7,vs2013,提升1.55,boost.compute 0.2,ATI Radeon HD 4600
代码使用c ++包装器:
#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <boost/timer/timer.hpp>
#include <boost/smart_ptr/scoped_array.hpp>
#include <fstream>
#include <numeric>
#include <algorithm>
#include <functional>
int main(){
static char kernelSourceCode[] = "\
__kernel void vadd(__global int * a, __global int * b, __global int * c){\
size_t i = get_global_id(0);\
\
c[i] = a[i] + b[i];\
}\
";
using type = boost::scoped_array<int>;
size_t const BUFFER_SIZE = 1UL << 13;
type A(new int[BUFFER_SIZE]);
type B(new int[BUFFER_SIZE]);
type C(new int[BUFFER_SIZE]); …Run Code Online (Sandbox Code Playgroud) 我内核的函数签名如下:
template< size_t S, typename Field, typename Type1, typename Type2>
void kernel(const Type1 arg1, const Type2 arg2, Field *results) {
// S is known at compile time
// Field might be float or double
// Type1 is an object holding data and also methods
// Type2 is an object holding data and also methods
// The computation start here
}
Run Code Online (Sandbox Code Playgroud)
我知道可以使用c ++的一部分功能来编写内核,使用AMD对OpenCL实现的扩展,但结果代码仅限于在AMD卡上运行.
2.0之前版本的OpenCL语言标准规范限制程序员使用C99编写内核,我相信版本2.1和2.2还没有广泛用于Linux发行版.但是,我在这里发现Boost :: compute在某种程度上允许在内核规范中使用c ++特性的子集.但是,如果可以使用Boos :: compute在上面的代码片段中实现内核签名,则不清楚.在哪种程度上可以实现这样的内核?代码示例将非常感激.
我刚开始玩Boost.Compute,看看它能为我们带来多少速度,我写了一个简单的程序:
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/foreach.hpp>
#include <boost/compute/core.hpp>
#include <boost/compute/platform.hpp>
#include <boost/compute/algorithm.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/functional/math.hpp>
#include <boost/compute/types/builtin.hpp>
#include <boost/compute/function.hpp>
#include <boost/chrono/include.hpp>
namespace compute = boost::compute;
int main()
{
// generate random data on the host
std::vector<float> host_vector(16000);
std::generate(host_vector.begin(), host_vector.end(), rand);
BOOST_FOREACH (auto const& platform, compute::system::platforms())
{
std::cout << "====================" << platform.name() << "====================\n";
BOOST_FOREACH (auto const& device, platform.devices())
{
std::cout << "device: " << device.name() << std::endl;
compute::context context(device);
compute::command_queue queue(context, device);
compute::vector<float> device_vector(host_vector.size(), context);
// …Run Code Online (Sandbox Code Playgroud) 我正在探索boost_compute.不幸的是,文档页面和示例比我需要了解的更少.
鉴于以下缩小的代码:
BOOST_COMPUTE_FUNCTION(bool, add, (int* values, int* results, int constant),
{
// Whats the indexing variable?
// In opencl it would be get_global_id(0)
int index = // ?
results[index] = values[index] + values[index + 1] + values[index + 2] + constant;
});
void compute(float* results, compute::context* ctx, compute::command_queue* queue)
{
compute::vector<float> device_values(100, *ctx);
compute::vector<float> device_results(98, *ctx);
compute::copy(
parameters->values.begin(), parameters->values.end(), device_values.begin(), *queue
);
// Actual computation
// HOW TO CALL 'add' for every device_results element?
compute::copy(
device_results.begin(), device_results.end(), results, *queue
); …Run Code Online (Sandbox Code Playgroud) 我现在正在学习 boost::compute openCL 包装库。我正在经历非常缓慢的复制过程。
如果我们将 CPU 到 CPU 的复制速度缩放为 1,那么 GPU 到 CPU、GPU 到 GPU、CPU 到 GPU 的复制速度有多快?
我不需要精确的数字。只是一个一般的想法将是一个很大的帮助。例如,CPU-CPU 至少比 GPU-GPU 快 10 倍。