Home > database >  error: a value of type "double" cannot be assigned to an entity of type "double *&quo
error: a value of type "double" cannot be assigned to an entity of type "double *&quo

Time:09-26

Hi I am trying to execute reduce function below in cuda c :

    double *sum_a, *d_sum_a;
    sum_a = (double*)malloc(sizeof(double) * length);
    cudaMalloc(&d_sum_a, sizeof(double) * length);
    cudaMemcpy(d_sum_a, sum_a, sizeof(double) * length, cudaMemcpyHostToDevice);
    sum_a = reduce(a, length);
    cout << "reduce(a): " << sum_a << endl;

where reduce function is:

double reduce(double *arr, int length) {
    double sum = 0.0;
    for (int i = 0; i < length; i  ) {
        sum  = arr[i];
    }
    return sum;
}

However I face: error: a value of type "double" cannot be assigned to an entity of type "double *"

CodePudding user response:

Your immediate problem to make the code compile has been answered already, you are trying to assign a value to a pointer.

However, I suspect that this will not solve your actual problem, which seems to be that you are trying to parallelize a the reduce operation with cuda (which will not happen even if you fix the immediate problem).

You would need to declare your reduce operation as a device function with __global__ and change it around a bit more.

The easiest would be to look at other examples: Cuda By Example has an example in section 4.5 that I adapted to what you are probably trying to do:

__global__ void reduce( int *a, int length, int *result) {
    int sum = 0;
    for (int i = 0; i < length; i  ) {
        sum  = a[i];
    }
    result[0] = sum;
}

#define N   10
int main( void ) {
    int a[N];
    int result[1]; // result is a single value
    int *dev_a, *dev_result;
    // allocate the memory on the GPU - the result is only a single number
    HANDLE_ERROR( cudaMalloc( (void**)&dev_a, N * sizeof(int) ) );
    HANDLE_ERROR( cudaMalloc( (void**)&dev_result, 1 ) );
    // fill the array on the CPU
    for (int i=0; i<N; i  ) {
        a[i] = i * i;
    }

    // copy the array a
    HANDLE_ERROR( cudaMemcpy( dev_a, a, N * sizeof(int),
                              cudaMemcpyHostToDevice ) );
    // launch a single kernel thread to sum up the buffer
    reduce<<<1,1>>>( dev_a, N, dev_result );

    // copy the result back from the GPU to the CPU
    HANDLE_ERROR( cudaMemcpy( result, dev_result, 1,
                              cudaMemcpyDeviceToHost ) );
    // display the result
    printf( "%i\n", result[0] );
    // free the memory allocated on the GPU
    cudaFree( dev_a );
    cudaFree( dev_result );
    return 0;
}

CodePudding user response:

Your reduce function returns a type of double, not double *:

double reduce(double *arr, int length) {
^^^^^^

However you are attempting to assign it to a variable of type double *:

double *sum_a, *d_sum_a;
...
sum_a = reduce(a, length);

That is what the compiler is complaining about.

One possible fix is to change the definition of your variable:

double sum_a, *d_sum_a;

and delete this line altogether:

sum_a = (double*)malloc(sizeof(double) * length);

This really has nothing to do with cuda it is a question about proper C usage

  • Related