c++ - CUDA:错误:创建 thrust::device_ptr 时出现 "transfer of control bypasses initialization of"

标签 c++ c linux cuda

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

这是我使用的命令和输出:

[测试]$ nvcc -I/usr/local/cuda/include -L/usr/local/cuda/lib kernel.cu -o test.run kernel.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): 这里

编译“/tmp/tmpxft_000022ad_00000000-9_kernel.cpp1.ii”时检测到 3 个错误。

相同的代码在 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);
}

关于c++ - CUDA:错误:创建 thrust::device_ptr 时出现 "transfer of control bypasses initialization of",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001503/

相关文章:

c++ - 如何使用 if...else 语句从一组随机生成的数字中提取特定数字?

c++ - 如何 list.sort(member Function);?

c++ - 模板推导似乎是错误的

c++ - C 与 C++ 中的 Typedef 结构

c++ - 我如何将多维数组作为指针传递给c

c++ - 在实践现代 c++17 最佳实践的同时管理线程

c++ - 当我在 linux mint 中调用 new 运算符时出现段错误

c - ljmp 和 setjmp 之间的内存泄漏

mysql - 以 Linux root 用户身份显示 mysql 数据库

c++ - 为什么在 pragma omp critical 之后多次调用 printf 会产生乱码输出?