c++ - 在全局范围内有推力::device_vector
问题描述
我正在编写一个程序来计算三角形网格数据的许多属性。其中一些属性,我想使用推力:: 方法计算,其他属性需要使用 CUDA 内核中的原始内存指针计算。
要将数据传输到 GPU,我在transfer.cuthrust::device_vector
文件中得到了这个,(因为不支持在纯 C++ 代码中创建和操作s ):
// thrust vectors (global)
thrust::host_vector<glm::vec3> trianglethrust_host;
thrust::device_vector<glm::vec3> trianglethrust_device;
extern "C" void trianglesToGPU_thrust(const trimesh::TriMesh *mesh, float** triangles) {
// fill host vector
for (size_t i = 0; i < mesh->faces.size(); i++) {
// PUSHING DATA INTO HOST_VECTOR HERE (OMITTED FOR CLARITY)
}
// copy to GPU by assigning host vector to device vector, like in the Thrust documentation
trianglethrust_device = trianglethrust_host;
// save raw pointer
*triangles = (float*)thrust::raw_pointer_cast(&(trianglethrust_device[0]));
}
这个函数trianglestoGPU_thrust
是从我的 C++ 程序的 main 方法中调用的。一切正常,花花公子,直到程序退出,并且(全局定义的) trianglethrust_device 向量超出范围。Thrust 试图释放它,但 CUDA 上下文已经消失,导致cudaErrorInvalidDevicePointer
我的问题的最佳实践是什么?
TL;DR:我想要一个在我的程序期间存在的推力::device_vector,因为我想向它抛出推力:: 函数(如变换等),并通过 CUDA 中的原始指针访问来读取和操作它.
解决方案: 在我的情况下,我显然是在过程中的某个地方使用原始数据指针进行释放。删除那个免费的,并结束我的主循环
trianglethrust_device.clear();
trianglethrust_device.shrink_to_fit();
trianglethrust_device.device_vector~;
在 CUDA 运行时被拆除之前强制清除该向量。这行得通,但可能仍然是一种非常丑陋的方式。
我推荐罗伯特对此的回答,并将其标记为有效。
解决方案
正如您已经发现的那样,推力矢量容器本身不能放在文件范围内。
一种可能的解决方案是简单地在开头创建所需的向量main
,然后将对这些向量的引用传递给需要它们的任何函数。
如果您真的想要“全局行为”,您可以将指向向量的指针放在全局/文件范围内,然后在 main 的开头初始化所需的向量,并将全局范围内的指针设置为指向 main 中创建的向量。
根据评论中的问题,我猜主文件是.cpp
用主机编译器编译的文件是很重要/可取的。因此,我们可以使用前面提到的概念与堆上的向量分配相结合,以避免在程序终止之前解除分配。这是一个完整的例子:
$ cat main.cpp
#include "transfer.h"
int main(){
float **triangles, *mesh;
triangles = new float *[1];
mesh = new float[4];
mesh[0] = 0.1f; mesh[1] = 0.2f; mesh[2] = 0.3f;
trianglesToGPU_thrust(mesh, triangles);
do_global_work(triangles);
finish();
}
$ cat transfer.h
void trianglesToGPU_thrust(const float *, float **);
void do_global_work(float **);
void finish();
$ cat transfer.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include "transfer.h"
#include <iostream>
#include <cstdio>
#include <thrust/copy.h>
__global__ void k(float *data, size_t ds){
for (int i = 0; i < ds; i++) printf("%f,", data[i]);
}
// thrust vectors (global)
thrust::host_vector<float> *trianglethrust_host;
thrust::device_vector<float> *trianglethrust_device;
void trianglesToGPU_thrust(const float *mesh, float** triangles) {
//create vectors
trianglethrust_host = new thrust::host_vector<float>;
trianglethrust_device = new thrust::device_vector<float>;
// fill host vector
size_t i = 0;
while (mesh[i] != 0.0f) {
(*trianglethrust_host).push_back(mesh[i++]);
}
// copy to GPU by assigning host vector to device vector, like in the Thrust documentation
*trianglethrust_device = *trianglethrust_host;
// save raw pointer
*triangles = (float*)thrust::raw_pointer_cast(&((*trianglethrust_device)[0]));
}
void do_global_work(float** triangles){
std::cout << "from device vector:" << std::endl;
thrust::copy((*trianglethrust_device).begin(), (*trianglethrust_device).end(), std::ostream_iterator<float>(std::cout, ","));
std::cout << std::endl << "from kernel:" << std::endl;
k<<<1,1>>>(*triangles, (*trianglethrust_device).size());
cudaDeviceSynchronize();
std::cout << std::endl;
}
void finish(){
if (trianglethrust_host) delete trianglethrust_host;
if (trianglethrust_device) delete trianglethrust_device;
}
$ nvcc -c transfer.cu
$ g++ -c main.cpp
$ g++ -o test main.o transfer.o -L/usr/local/cuda/lib64 -lcudart
$ ./test
from device vector:
0.1,0.2,0.3,
from kernel:
0.100000,0.200000,0.300000,
$
这是另一种方法,与前面类似std::vector
,在全局范围内使用推力容器(仅transfer.cu
文件与前面的示例不同,main.cpp
并且transfer.h
相同):
$ cat transfer.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include "transfer.h"
#include <iostream>
#include <cstdio>
#include <thrust/copy.h>
#include <vector>
__global__ void k(float *data, size_t ds){
for (int i = 0; i < ds; i++) printf("%f,", data[i]);
}
// thrust vectors (global)
std::vector<thrust::host_vector<float> > trianglethrust_host;
std::vector<thrust::device_vector<float> > trianglethrust_device;
void trianglesToGPU_thrust(const float *mesh, float** triangles) {
//create vectors
trianglethrust_host.resize(1);
trianglethrust_device.resize(1);
// fill host vector
size_t i = 0;
while (mesh[i] != 0.0f) {
trianglethrust_host[0].push_back(mesh[i++]);
}
// copy to GPU by assigning host vector to device vector, like in the Thrust documentation
trianglethrust_device[0] = trianglethrust_host[0];
// save raw pointer
*triangles = (float*)thrust::raw_pointer_cast(trianglethrust_device[0].data());
}
void do_global_work(float** triangles){
std::cout << "from device vector:" << std::endl;
thrust::copy(trianglethrust_device[0].begin(), trianglethrust_device[0].end(), std::ostream_iterator<float>(std::cout, ","));
std::cout << std::endl << "from kernel:" << std::endl;
k<<<1,1>>>(*triangles, trianglethrust_device[0].size());
cudaDeviceSynchronize();
std::cout << std::endl;
}
void finish(){
trianglethrust_host.clear();
trianglethrust_device.clear();
}
$ nvcc -c transfer.cu
$ g++ -o test main.o transfer.o -L/usr/local/cuda/lib64 -lcudart
$ ./test
from device vector:
0.1,0.2,0.3,
from kernel:
0.100000,0.200000,0.300000,
$
推荐阅读
- swagger - Swashbuckle - 如何在 Schema 部分中包含参数类型
- javascript - Javascript存储来自foreach php函数的第一个值并不断重复它
- linux - 更简单的 SSH 方法来防止同时输入主机名和用户名?
- ms-office - Microsoft Office 加载项商店 - 将发布日期安排在未来
- three.js - 使用 mapbox 更新 3d 模型位置
- python - 在 Python 中访问字典对象转储中的项目
- javascript - 如何使用 js 为时间块着色?
- python - 如何在制作数组时不出现 indexerror
- tensorflow - Tensorflow:在 tf.gradients() 期间忽略特定依赖项
- r - 如何从 coxph 中提取极小的 p 值而不将其舍入为 0?