可以在 GPU 上的浮点数组上进行数学运算的库
Library that can do math on float arrays on the GPU?
我想对 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)上运行的线程,每个线程负责计算的一小部分,并且您可以配置要运行的线程数。
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- GCC本机矩阵运算库
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 位阵列上的快速AND运算
- 字符串中int的加法运算
- 算术运算的结果类似于:C浮点变量中的1/3
- 如何使用OpenMP并行化此矩阵时间矢量运算
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 如何在两个 boost::multi_arrays (C++) 之间执行数学运算?
- 在 DirectX 11 中从 GPU 读回顶点缓冲区(并获取顶点)
- 跨平台 GPU 计算
- C++:从GPU内存(cudaMemcpy2D)获取BGR图像(cv::Mat)
- 请求最简单的 OpenMP 目标 GPU 示例
- 如何在 vtk Poly 数据上进行布尔运算?
- MSVC 无法根据模板参数进行数学运算,这是一个错误吗?
- 避免指针运算,修复叮当整齐错误
- DirectX 11 如何处理来自 GPU 上的 sharedHandle 的图像
- 编译 GPU 的张量流示例自定义操作
- 使用双精度的浮点运算
- 可以在 GPU 上的浮点数组上进行数学运算的库