可以在 GPU 上的浮点数组上进行数学运算的库

Library that can do math on float arrays on the GPU?

本文关键字:运算 GPU 数组      更新时间:2023-10-16

我想对 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,...]

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

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

推力。

此示例来自他们的网站:

#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));

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);
}

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