CUDA : 错误: 创建推力时"transfer of control bypasses initialization of"::d evice_ptr
CUDA : error: "transfer of control bypasses initialization of" when creating thrust::device_ptr
我在我的cuda -c应用程序中有此代码行:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <stdio.h>
#include <time.h>
#include <device_functions.h>
int main()
{
const int size = 32;
unsigned int * dev_ips_range_end;
unsigned int * ips_range_end = new unsigned int[size];
for (int i = 0; i < size; i++)
ips_range_end[i] = i;
cudaError_t cudaStatus;
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_ips_range_end, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "Problem !");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(dev_ips_range_end, ips_range_end, size * sizeof(unsigned char), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "Problem !");
goto Error;
}
thrust::device_ptr<unsigned int> dev_ips_range_end_ptr(dev_ips_range_end);
thrust::inclusive_scan(dev_ips_range_end_ptr, dev_ips_range_end_ptr + size, dev_ips_range_end_ptr);
return 0;
Error:
cudaFree(dev_ips_range_end);
}
这是我使用的命令和输出:
[test] $ nvcc -i/usr/local/cuda/包括-l/usr/local/cuda/lib kernel.cu -o test.runkernel.cu(27):错误:控制转移绕过: 变量" dev_ips_range_end_ptr"(42):这里
kernel.cu(32):错误:控制的传递绕过: 变量" dev_ips_range_end_ptr"(42):这里
kernel.cu(39):错误:控制的传递绕过: 变量" dev_ips_range_end_ptr"(42):这里
3在"/tmp/tmpxft_000022ad_0000000000-9_kernel.cpp1.ii"的汇编中检测到的错误。
在Windows上的Visual Studio中,相同的代码在没有任何问题上工作。如何解决此问题?
某些人可能会告诉您,在C/C 中使用goto
并不是一个好主意。但是为了避免参数,并允许您保留相同的代码结构,您可以在程序顶部声明推力设备指针(在任何goto
语句之前),然后在准备好使用时设置指针值,例如:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <stdio.h>
#include <time.h>
#include <device_functions.h>
int main()
{
const int size = 32;
unsigned int * dev_ips_range_end;
unsigned int * ips_range_end = new unsigned int[size];
for (int i = 0; i < size; i++)
ips_range_end[i] = i;
thrust::device_ptr<unsigned int> dev_ips_range_end_ptr;
cudaError_t cudaStatus;
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
cudaStatus = cudaMalloc((void**)&dev_ips_range_end, size * sizeof(int));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "Problem !");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(dev_ips_range_end, ips_range_end, size * sizeof(unsigned char), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "Problem !");
goto Error;
}
dev_ips_range_end_ptr = thrust::device_pointer_cast(dev_ips_range_end);
thrust::inclusive_scan(dev_ips_range_end_ptr, dev_ips_range_end_ptr + size, dev_ips_range_end_ptr);
return 0;
Error:
cudaFree(dev_ips_range_end);
}
相关文章:
- Usages of std::move
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- C++如何通过"constructor initialization"初始化行变量?
- 为什么"an inherited constructor is not a candidate for initialization from an expression of the same or
- 如何避免"invalid initialization of reference" C++中琐碎函子的错误
- JNI 失败并显示'Error occurred during initialization of VM'
- CUDA : 错误: 创建推力时"transfer of control bypasses initialization of"::d evice_ptr
- 仅当初始化与声明结合使用时"crosses initialization of variable"
- 另一个"invalid initialization of reference of type"错误
- "invalid initialization of non-const reference of type..." 克隆() 函数错误
- C++ "invalid initialization of non-const reference"错误
- C++:"invalid initialization of non-const reference of type ‘List&’ from a temporary of type ‘List*’"
- 位域"In-class initialization"结果为"error: lvalue required as left operand of assignment"
- "No matching constructor for initialization of. . ."
- 使用模板时出现奇怪的"invalid initialization of non-const reference"
- 如何解决棉绒错误"Reference initialization causes loss of const/volatile integrity"
- Initialization of std::initializer_list