Search code examples
cudacompiler-errorsthrust

Why compiler gives error?


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


}

In above code, why i cannot display vector values? it gives error at

void generateVector(count);

that says incomplete is not allowed Why? What's wrong here? What might be the possible solution?


Solution

  • You are calling the function generateVector incorrectly inside the function displayVector. It should be like this:

    generateVector(count);
    

    Also, you are creating vectors A and B inside the function generateVector which will be local to the function and thrust::generate will operate on these local vectors. The global vectors A and B would not be modified. You should remove the local vectors to achieve what you want. Instead call host_vector::resize for for the global vectors A and B to allocate memory.

    The final code should be like this:

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