cuda - 为什么编译器会给出错误?

标签 cuda compiler-errors thrust

thrust::host_vector<int> A;
thrust::host_vector<int> B;

int rand_from_0_to_100_gen(void)
{
     return rand() % 100;
}


__host__ void generateVector(int count) {


    thrust::host_vector<int> A(count);
    thrust::generate(A.begin(),A.end(),rand_from_0_to_100_gen);

    thrust::host_vector<int> B(count);
    thrust::generate(B.begin(),B.end(),rand_from_0_to_100_gen);
}

__host__ void displayVector(int count){

    void generateVector(count);

    cout << A[1];


}

在上面的代码中,为什么我不能显示 vector 值?它给出了错误
void generateVector(count);

那说incomplete is not allowed为什么?怎么了可能的解决方案是什么?

最佳答案

您在函数generateVector中错误地调用了函数displayVector。应该是这样的:

generateVector(count);

同样,您将在函数A中创建 vector BgenerateVector,这些 vector 将在该函数本地,而thrust::generate将在这些局部 vector 上运行。全局 vector AB不会被修改。您应删除局部 vector 以实现所需的效果。而是为全局 vector host_vector::resizeA调用B来分配内存。

最终代码应如下所示:
thrust::host_vector<int> A;
thrust::host_vector<int> B;

int rand_from_0_to_100_gen(void)
{
    return rand() % 100;
}

__host__ void generateVector(int count) 
{
    A.resize(count);
    thrust::generate(A.begin(),A.end(),rand_from_0_to_100_gen);

    B.resize(count);
    thrust::generate(B.begin(),B.end(),rand_from_0_to_100_gen);
}

__host__ void displayVector(int count)
{
    generateVector(count);
    cout << A[1]<<endl;
}

关于cuda - 为什么编译器会给出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15782386/

相关文章:

performance - CUDA内核: performance drops by 10x when increased loop count by 10%

linux - Linux下可视化内存调试应用?

python - 在 Solaris 10 上安装 web2py : "ImportError: No module named _md5"

c++ - 函数声明无效。开发C++

cuda - 如何使用 CUDA Thrust 用最后一个非缺失值填充数组中的缺失值?

random - curandState_t 和 curandState 之间的区别

c# - 在 Nsight 或 Visual Profiler 中分析 ManagedCuda

scala - 获取 "value x is not a member of xyz"

cuda - 通过 CUDA Thrust 对具有偶数或奇数索引的元素求和

parallel-processing - 如何将传输数据与执行推力算法重叠?