c语言程序慢和CPU有关吗,c – Boost.Compute比普通CPU慢吗?

我刚开始玩Boost.Compute,看看它能为我们带来多少速度,我写了一个简单的程序:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

namespace compute = boost::compute;

int main()

{

// generate random data on the host

std::vector 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 device_vector(host_vector.size(), context);

// copy data from the host to the device

compute::copy(

host_vector.begin(), host_vector.end(), device_vector.begin(), queue

);

auto start = boost::chrono::high_resolution_clock::now();

compute::transform(device_vector.begin(),

device_vector.end(),

device_vector.begin(),

compute::sqrt(), queue);

auto ans = compute::accumulate(device_vector.begin(), device_vector.end(), 0, queue);

auto duration = boost::chrono::duration_cast<:chrono::milliseconds>(boost::chrono::high_resolution_clock::now() - start);

std::cout << "ans: " << ans << std::endl;

std::cout << "time: " << duration.count() << " ms" << std::endl;

std::cout << "-------------------\n";

}

}

std::cout << "====================plain====================\n";

auto start = boost::chrono::high_resolution_clock::now();

std::transform(host_vector.begin(),

host_vector.end(),

host_vector.begin(),

[](float v){ return std::sqrt(v); });

auto ans = std::accumulate(host_vector.begin(), host_vector.end(), 0);

auto duration = boost::chrono::duration_cast<:chrono::milliseconds>(boost::chrono::high_resolution_clock::now() - start);

std::cout << "ans: " << ans << std::endl;

std::cout << "time: " << duration.count() << " ms" << std::endl;

return 0;

}

这是我的机器上的示例输出(win7 64位):

====================Intel(R) OpenCL====================

device: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz

ans: 1931421

time: 64 ms

-------------------

device: Intel(R) HD Graphics 4600

ans: 1931421

time: 64 ms

-------------------

====================NVIDIA CUDA====================

device: Quadro K600

ans: 1931421

time: 4 ms

-------------------

====================plain====================

ans: 1931421

time: 0 ms

我的问题是:为什么普通(非opencl)版本更快?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值