我正在编写一个计算三角形网格数据的很多属性的程序。其中一些属性,我想使用thrust ::方法计算,其他属性需要使用CUDA内核中的原始内存指针计算。
为了将数据传输到GPU,我在transfer.cu文件中得到了这个,(因为不支持在纯C ++代码中创建和操作thrust::device_vector
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
is是从我的C ++程序的main方法调用的。一切正常,花花公子,直到程序退出,并且(全局定义的)trianglethrust_device向量超出范围。 Thrust试图释放它,但CUDA背景已经消失,导致cudaErrorInvalidDevicePointer
什么是我的问题的最佳做法?
TL; DR:我想要一个在我的程序期间存在的thrust :: device_vector,因为我想在它上面抛出thrust :: functions(比如transform等),以及通过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,
$