CUDA:错误:"控制权转移绕过"在创建thrust :: device_ptr时

时间:2016-06-23 20:44:04

标签: 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):错误:控制转移绕过初始化:             变量&#34; dev_ips_range_end_ptr&#34; (42):这里

kernel.cu(32):错误:控制转移绕过初始化:             变量&#34; dev_ips_range_end_ptr&#34; (42):这里

kernel.cu(39):错误:控制转移绕过初始化:             变量&#34; dev_ips_range_end_ptr&#34; (42):这里

在&#34; /tmp/tmpxft_000022ad_00000000-9_kernel.cpp1.ii"的汇编中检测到3个错误。

相同的代码在Windows上的visual studio中没有任何问题。 如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

有些人可能会告诉你,在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);
}