- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在尝试并行化当前在主函数中按顺序运行的推力 cuda 代码(因此不利用 GPU 的强大功能)。我本质上已经将功能代码放入一个仿函数中,thrust::for_each 可以使用 cuda 流调用。但是,如果我使用
定义仿函数__host__ __device__
VS2013 抛出各种警告说我正在尝试从设备启动主机功能。这些错误发生在我使用
定义 vector 的地方thrust::device_vector vect (size_vector);
以及一些 thrust::transform 函数。它特别引用了 thrust::device_malloc_allocator 的问题。如果我将仿函数严格定义为 host 仿函数,这些错误都会消失,但是当我使用分析器时,很明显只有 0.01% 的设备被使用,这让我相信 for_each 实际上不是在仿函数中启动推力代码。
编辑下面是一些编译并显示此错误的代码
#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <thrust/execution_policy.h>
#include <thrust/for_each.h>
#include <thrust/sequence.h>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cstdio>
#include <thread>
#include <thrust/copy.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/reduce.h>
using namespace std;
const int num_segs = 1; // number of segments to sort
const int num_vals = 5; // number of values in each segment
template <typename T>
struct sort_vector
{
T *Ddata;
T *vect3;
T *answer;
sort_vector(T *_Ddata, T *_vect3, float *a) : Ddata(_Ddata), vect3(_vect3), answer(a) {};
__host__ __device__ void operator()(int idx)
{
thrust::sort(thrust::seq, Ddata + idx*num_vals, Ddata + ((idx + 1)*num_vals));
thrust::device_ptr<float> vect3_ptr = thrust::device_pointer_cast(vect3);
thrust::device_vector<float> vect(10, 1);
thrust::device_vector<float> vect2(10, 3);
thrust::transform(thrust::device, vect.begin(), vect.end(), vect2.begin(), vect3_ptr, thrust::minus<float>());
*answer = thrust::reduce(thrust::device, Ddata + idx*num_vals, Ddata + ((idx + 1)*num_vals));
}
};
int main() {
thrust::device_vector<float> d_Ddata(num_segs*num_vals);
d_Ddata[0] = 50;
d_Ddata[1] = 9.5;
d_Ddata[2] = 30;
d_Ddata[3] = 8.1;
d_Ddata[4] = 1;
thrust::device_vector<float> d_Ddata2(num_segs*num_vals);
d_Ddata2[0] = 50;
d_Ddata2[1] = 20.5;
d_Ddata2[2] = 70;
d_Ddata2[3] = 8.1;
d_Ddata2[4] = 1;
thrust::device_vector<float> vect3(10, 0);
thrust::device_vector<float> vect4(10, 0);
cout << "original dut" << endl;
int g = 0;
while (g < num_segs*num_vals){
cout << d_Ddata[g] << endl;
g++;
}
thrust::device_vector<int> d_idxs(num_segs);
thrust::sequence(d_idxs.begin(), d_idxs.end());
thrust::device_vector<float> dv_answer(1);
thrust::device_vector<float> dv_answer2(1);
cudaStream_t s1, s2;
cudaStreamCreate(&s1);
cudaStreamCreate(&s2);
clock_t start;
double duration;
start = clock();
thrust::for_each(thrust::cuda::par.on(s1),
d_idxs.begin(),
d_idxs.end(), sort_vector<float>(thrust::raw_pointer_cast(d_Ddata.data()), thrust::raw_pointer_cast(vect3.data()), thrust::raw_pointer_cast(dv_answer.data())));
thrust::for_each(thrust::cuda::par.on(s2),
d_idxs.begin(),
d_idxs.end(), sort_vector<float>(thrust::raw_pointer_cast(d_Ddata2.data()), thrust::raw_pointer_cast(vect4.data()), thrust::raw_pointer_cast(dv_answer2.data())));
cudaStreamSynchronize(s1);
cudaStreamSynchronize(s2);
cout << "sorted dut" << endl;
int n = 0;
while (n < num_segs*num_vals){
cout << d_Ddata[n] << endl;
n++;
}
cout << "sum" << endl;
cout << dv_answer[0] << endl;
cout << dv_answer2[0] << endl;
cout << "vector subtraction" << endl;
int e = 0;
while (e < 10){
cout << vect3[e] << endl;
e++;
}
cudaStreamDestroy(s1);
cudaStreamDestroy(s2);
duration = (clock() - start) / (double)CLOCKS_PER_SEC;
cout << "time " << duration << endl;
cin.get();
return 0;
}
有没有可能 thrust::for_each 不能调用 __host__
仿函数?
某些推力调用是否与幕后的主机天生相关?
我能看到的唯一可能的解决方法是创建一个 __host__ __device__
函数,其中包含单独的主机和设备定义代码。也有可能我在研究这个主题时遗漏了一些东西。任何建议将不胜感激。
最佳答案
These errors are occurring in places were I am defining a vector
正如编译器清楚地告诉您的那样,问题在于 thrust::vector
中定义的构造函数和所有运算符目前都是仅限宿主 的函数。尝试在 __device__
函数中使用它们是非法的。
除了不尝试在设备代码中实例化 vector 外,没有其他解决方案。
关于c++ - 在 __host__ __device__ 仿函数中创建 Thrust::device_vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529322/
CUDA 专家,如果我在主机代码中定义了一个新类型: struct float_3{ float x; float y; float z; }; 并且我已经向设备传输了一些这种类型的数据,我可以
我正在尝试在 __device__ 变量上应用内核函数,根据规范,该变量驻留在“全局内存中” #include #include "sys_data.h" #include "my_helper.c
在 http://docs.nvidia.com/cuda/cuda-c-programming-guide/#device-variable-qualifier它说 __device_ 限定符变量具
这个问题在这里已经有了答案: Check whether the code is running on the GPU or CPU (2 个回答) 7年前关闭。 我想写一些主机和设备兼容的函数,并根
有什么区别,在 CUDA 程序中定义设备常量的最佳方法是什么?在 C++,主机/设备程序中,如果我想将常量定义在设备常量内存中,我可以这样做 __device__ __constant__ float
我有一个内核,它在 if 语句中调用 device 函数。代码如下: __device__ void SetValues(int *ptr,int id) { if(ptr[threadIdx
这个问题在这里已经有了答案: External calls are not supported - CUDA (1 个回答) 关闭 7 年前。 我试图了解如何在单独的头文件中解耦 CUDA __de
如果 CUDA 内核调用 __device__ 函数, block 中进入或退出 __device__ 函数的所有线程之间是否存在任何隐式同步? 如果不是那么这意味着 block 中的一些线程可能在
我正在尝试使用 NVCC 选项获取有关我的 CUDA 内核中寄存器使用情况的一些信息 --ptxas-options=v 虽然全局函数一切正常,但我在设备函数方面遇到了一些困难,因为 ptxas信息:
在 CUDA 代码的实现过程中,我经常需要一些实用函数,这些函数将从设备和主机代码中调用。所以我将这些函数声明为 __host__ __device__。这是可以的,可能的设备/主机不兼容可以通过#i
我有一些工作代码...我在其中分配了一个设备变量指针,如下所示: float *d_var; cudaMalloc(&d_var, sizeof(float) ); 稍后在我的代码中,我想将此 var
我对我编写的 CUDA 代码的行为感到困惑。我正在为名为 DimmedGridGPU 的类中的 __device__ 函数编写测试。此类以 int DIM 为模板,我遇到的函数是返回距离输入值 x 最
我正在使用 __device__ 说明符在设备上声明一个全局变量 myvar。我没有在任何地方将它设置为有意义的值(没有像您通常那样在我的内核启动方法中使用 cudaMemcpyToSymbol)。
我有一个在主机和设备代码上都使用的类,以便更轻松地传递数据。这个类有一些操作数据的方法。一个简单的例子是: struct Vector { float x, y, z; __host__ __
在 CUDA 函数类型限定符 __device__ 和 __host__ 中可以一起使用,在这种情况下函数是为主机和设备编译的。这允许消除复制粘贴。但是,没有 __host__ __device__
我想从执行基数排序的内核内部调用独占扫描函数。但是独占扫描只需要一半的线程来完成它的工作。 独占扫描算法中需要几个 __syncthreads()。如果我在开始时有一个声明,例如 if(threadI
我不明白为什么我会收到错误 dynamic initialization is not supported for __device__, __constant__, __shared__ varia
我正在 NVIDIA CUDA GPU 上研究马尔可夫链蒙特卡罗 (MCMC) 算法实现。 CPU MCMC 算法使用高质量的 Mersenne twiner 随机数生成器,我想在我编写的 GPU 内
CUDA 有没有办法在设备端函数中动态分配内存? 我找不到任何这样做的例子。 从 CUDA C 编程手册: B.15 Dynamic Global Memory Allocation void* ma
我在很多地方读到过,__device__ 函数几乎总是由 CUDA 编译器内联。那么,当我将代码从内核移动到内核调用的__device__函数时,(通常)使用的寄存器数量不会增加,这样说是否正确? 例
我是一名优秀的程序员,十分优秀!