3

我想对 GPU 上的大型浮点数组进行基本的数学运算(加法、减法、除法、乘法),C++ 中是否有任何库可以实现这一点?

例如,在伪代码中:

A = [1,2,3,...]
B = [2,3,9,...]
C = A+B //[3,5,12,...]
D = A-B //[-1,-1,-6,...]
E = A/B //[0.5,0.6,0.3,...]
F = A*B //[2,6,27,...]
4

4 回答 4

7

看看Boost.Compute库。它是一个类似 C++ STL 的库,允许您在 GPU(或任何 OpenCL 兼容设备)上执行许多操作。与 Thrust 不同,它不仅限于 NVIDIA GPU。

源代码在这里:https ://github.com/boostorg/compute

于 2013-10-26T18:04:33.497 回答
4

推力

这个例子来自他们的网站:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <cstdlib>

int main(void)
{
    // generate 32M random numbers on the host
    thrust::host_vector<int> h_vec(32 << 20);
    thrust::generate(h_vec.begin(), h_vec.end(), rand);

    // transfer data to the device
    thrust::device_vector<int> d_vec = h_vec;

    // sort data on the device (846M keys per second on GeForce GTX 480)
    thrust::sort(d_vec.begin(), d_vec.end());

    // transfer data back to host
    thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());

    return 0;
}

他们的saxpy例子更接近你的要求;看片段:

thrust::transform(X.begin(), X.end(), Y.begin(), Y.begin(), saxpy_functor(A));
于 2013-10-26T17:31:33.447 回答
4

VexCL是另一个可以帮助您的库。从 v1.0.0 开始,它具有 OpenCL 和 CUDA 后端。这是一个最小的例子:

#include <vexcl/vexcl.hpp>

int main() {
    // Get all compute devices that support double precision.
    vex::Context ctx(vex::Filter::DoublePrecision);

    std::vector<double> a = {1, 2, 3, 4,  5};
    std::vector<double> b = {6, 7, 8, 9, 10};

    // Allocate memory and copy input data to compute devices.
    vex::vector<double> A(ctx, a);
    vex::vector<double> B(ctx, b);

    // Do the computations.
    vex::vector<double> C = A + B;
    vex::vector<double> D = A - B;
    vex::vector<double> E = A / B;
    vex::vector<double> F = A * B;

    // Get the results back to host.
    vex::copy(C, a);
}
于 2013-11-21T10:55:25.183 回答
1

OpenCL 就是这样一个“库”——从技术上讲,它不是一个库,而是一种基于 C99 的自己的语言。OpenCL 运行时系统将允许您在多个线程中创建在 GPU(或 CPU)上运行的线程,每个线程负责一小部分计算,并且您可以配置要运行的线程数。

于 2013-10-26T17:30:55.273 回答