Help with pointers inside structs

Hi,

I have tried to tidy up my code a little by hiding a lot the messy pointers and such inside a struct. However I don’t seem to be able to either cudamemcopy or cudamalloc or both correctly. Here is a minimal code snippet

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NN 100

__host__ __inline__ void cudaErrorCheck( cudaError_t error ) {
    if( error != cudaSuccess ) {
        printf( "\n%s\n", cudaGetErrorString( error ) );
    }
}

typedef struct testStruct {
   
    double *d_pointer;
    
} testStruct;

__host__ void fcreate( testStruct hostStruct ) {
    
    cudaErrorCheck( cudaMalloc( (void**) &hostStruct.d_pointer, NN*sizeof(double) ) );
    
    return;
}

__host__ void finitialise( testStruct hostStruct ) {
    
    double numbers[NN] = { 0. };
    
    for (int i=0; i<NN; i++) {
        numbers[i] = (double)i;
    }
    
    cudaErrorCheck( cudaMemcpy( hostStruct.d_pointer, numbers, NN*sizeof(double), cudaMemcpyHostToDevice ) );

    return;
}

__host__ void fdestroy( testStruct hostStruct ) {
    
    cudaErrorCheck( cudaFree( hostStruct.d_pointer ) );
    
    return;
}

int main( ) {
    
    testStruct hostStruct;
    
    fcreate    ( hostStruct );
    finitialise( hostStruct );
    fdestroy   ( hostStruct );
    
    return 0;
}

The cudamemcpy gives the error invalid argument.

Does anyone have any ideas? Surely this is doable?

Thanks

I think the issue is that you’re passing “hostStruct” by value. Passing the struct by reference should fix it:

__host__ void fcreate( testStruct& hostStruct ) {
...
}

Awesome. Thanks. That seems to have fixed that error.

That has been bothering me for a while.

Such a quick response. Thanks again.