C++数据结构和CUDA
C++ data structures and CUDA
我有一个可以是的结构
struct type1{ double a,b,c;}
也可以是
struct type2{ double a,b,c,d,e;}
在我的cuda代码宿主函数中,我有一些类似的东西
void compute(){
// some code
// data on devices (up to 10)
type *xxx[10]; // this is where i want either type1 or type2 structures
// the "type" is not known at compile time but i want to
// determine at runtime based on some input variable. this
// part is not real code rather this is what i want to achive.
int DevUsed; // some code to give value to int DevUsed
for(int idev=0;idev<DevUsed;idev++){
// set cuda device
if ( cudaMalloc(&xxx[iDev], sizeof(type)) != cudaSuccess )
// print error message;
cudaMemcpy(xxx[iDev], pIF1, sizeof(type), cudaMemcpyHostToDevice);
function2<<<grid, block>>>(xxx[iDev]); // where function2 is the kernel
}
}
我的问题是,用"type*xxx[10];"
C++模板就是为这种情况而设计的。
template <class T>
void compute(){
// some code
// data on devices (up to 10)
T xxx[10]; // this is where i want either type1 or type2 structures
// the "type" is not known at compile time but i want to
// determine at runtime based on some input variable. this
// part is not real code rather this is what i want to achive.
int DevUsed; // some code to give value to int DevUsed
for(int idev=0;idev<DevUsed;idev++){
// set cuda device
if ( cudaMalloc(&xxx[iDev], sizeof(T)) != cudaSuccess )
// print error message;
cudaMemcpy(xxx[iDev], pIF1, sizeof(T), cudaMemcpyHostToDevice);
function2<<<grid, block>>>(xxx[iDev]); // where function2 is the kernel
}
}
请注意,您还需要这两种类型的内核模板,如
template <class T>
__global__ void function2(T x)
{
//....
}
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 如何有效地将这种数据结构发送到CUDA
- 如何在.cpp文件中使用Cuda数据结构
- C++数据结构和CUDA