CUDA : 错误: 创建推力时"transfer of control bypasses initialization of"::d evice_ptr

CUDA : error: "transfer of control bypasses initialization of" when creating thrust::device_ptr

本文关键字:of initialization bypasses evice ptr control transfer 创建 错误 CUDA      更新时间:2023-10-16

我在我的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);
}