【问题标题】:Why compiler gives error?为什么编译器会报错?
【发布时间】:2013-03-24 19:49:36
【问题描述】:
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];


}

在上面的代码中,为什么我不能显示向量值?它在

处给出错误
void generateVector(count);

上面写着incomplete is not allowed 为什么?这里有什么问题?可能的解决方案是什么?

【问题讨论】:

    标签: cuda compiler-errors thrust


    【解决方案1】:

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

    generateVector(count);
    

    此外,您正在函数generateVector 内创建向量AB,这将是函数的局部变量,thrust::generate 将对这些局部向量进行操作。全局向量 AB 不会被修改。您应该删除局部向量以实现您想要的。而是调用 host_vector::resize 为全局向量 AB 分配内存。

    最终的代码应该是这样的:

    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;
    }
    

    【讨论】:

    • 感谢您对 generateVector 和 displayVector 函数的帮助 还有一个参数定义为 int * hData 那可能是什么?
    • 这里没有 hData。家伙通过输入 int * hData 我不知道为什么来定义这个函数
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多