Error with cudaMalloc/Memcpy and calculations Error: expression must have arithmetic or enum type

Hi,

I’ve been working a project with CUDA (first timer).

I have all libraries linked and setup so it compiles correctly but I am having an issue with some of my code when it comes time to perform calculations on some of the variables. I have looked all over the internet trying to find any help but it does not appear to have any useful results, so far.

I’ll post the snippets of code that is giving me issues since it spans over the program.

double dXmax;

double dXn;

double *dev_dXmax;

double *dev_dXn;

//other declarations 

dXn = 0.25;									//delta X (change in X)

cudaMalloc((void**)&dev_dXn, sizeof(double));

cudaMemcpy(dev_dXn, &dXn, sizeof(double), cudaMemcpyHostToDevice);

dXmax = 2; 

cudaMalloc((void**)&dev_dXmax, sizeof(double));

cudaMemcpy(dev_dXmax, &dXmax, sizeof(double), cudaMemcpyHostToDevice);

dev_dXn = dev_dXmax * 2.000 * (mtrand1.randExc() - 0.5);

The error: “expression must have arithmetic or enum type” is being thrown on double *dev_dXmax, on line:

dev_dXn = dev_dXmax * 2.000 * (mtrand1.randExc() - 0.5);

I’m assuming this is due to the pointers and references but I am having issues finding which is causing the problem.

Does anyone have any suggestions or help to guide me along here?

Hi,

I’ve been working a project with CUDA (first timer).

I have all libraries linked and setup so it compiles correctly but I am having an issue with some of my code when it comes time to perform calculations on some of the variables. I have looked all over the internet trying to find any help but it does not appear to have any useful results, so far.

I’ll post the snippets of code that is giving me issues since it spans over the program.

double dXmax;

double dXn;

double *dev_dXmax;

double *dev_dXn;

//other declarations 

dXn = 0.25;									//delta X (change in X)

cudaMalloc((void**)&dev_dXn, sizeof(double));

cudaMemcpy(dev_dXn, &dXn, sizeof(double), cudaMemcpyHostToDevice);

dXmax = 2; 

cudaMalloc((void**)&dev_dXmax, sizeof(double));

cudaMemcpy(dev_dXmax, &dXmax, sizeof(double), cudaMemcpyHostToDevice);

dev_dXn = dev_dXmax * 2.000 * (mtrand1.randExc() - 0.5);

The error: “expression must have arithmetic or enum type” is being thrown on double *dev_dXmax, on line:

dev_dXn = dev_dXmax * 2.000 * (mtrand1.randExc() - 0.5);

I’m assuming this is due to the pointers and references but I am having issues finding which is causing the problem.

Does anyone have any suggestions or help to guide me along here?

What are you trying to do/calculate for dev_dXn ?

Are you trying to perform pointer arithmetic ? or are you trying to assign a value to where the pointer points to ?

Anyway if you want to assign something to where the pointer points to you’ll either have to use asterix * for dereferencing or brackets for array acess.

Assuming the rest of your code is ok it’s either something like:

dev_dXn[ some index ] =

or

dev_dXn = (some typecast)(calculations);

I am not sure why you do void ** in the malloc call.

So it might have to be something like:

dev_dXn = (void *)(calculations);
or
dev_dXn = (void **)(calculations);

Probably the first one.

Also apperently dev_dXmax is a pointer type too and you cannot perform calculations with it.

So first you would have to typecast it to an arithmetic type for example:

dev_dXn = (void *)( (int)( (double)( dev_dXmax * calculations) ) );

And probably another typecast to int to round the double to an integer, since memory address probably don’t work for doubles… so typecasting directly to void* might not work.

Actually the int typecast also might not work, so you will first have to round your floating point calculations to an integer.

Alternatively you could also try and calculate the index in vIndex and then do something like this:

int vIndex;
vIndex = calculate index here.
dev_dXn[vIndex] = store some stuff/

Bye,
Skybuck.

What are you trying to do/calculate for dev_dXn ?

Are you trying to perform pointer arithmetic ? or are you trying to assign a value to where the pointer points to ?

Anyway if you want to assign something to where the pointer points to you’ll either have to use asterix * for dereferencing or brackets for array acess.

Assuming the rest of your code is ok it’s either something like:

dev_dXn[ some index ] =

or

dev_dXn = (some typecast)(calculations);

I am not sure why you do void ** in the malloc call.

So it might have to be something like:

dev_dXn = (void *)(calculations);
or
dev_dXn = (void **)(calculations);

Probably the first one.

Also apperently dev_dXmax is a pointer type too and you cannot perform calculations with it.

So first you would have to typecast it to an arithmetic type for example:

dev_dXn = (void *)( (int)( (double)( dev_dXmax * calculations) ) );

And probably another typecast to int to round the double to an integer, since memory address probably don’t work for doubles… so typecasting directly to void* might not work.

Actually the int typecast also might not work, so you will first have to round your floating point calculations to an integer.

Alternatively you could also try and calculate the index in vIndex and then do something like this:

int vIndex;
vIndex = calculate index here.
dev_dXn[vIndex] = store some stuff/

Bye,
Skybuck.

I guess I was doing the cudaMalloc and cudaMemcpy in the wrong areas.
I moved them around a little while working through other parts of code and it seemed to fix this issue.

Also the reason I was doing the void** in allocating memory is because i’ve been following examples from the “cude by example” book and it has void in there each time. Like I said I’m really feeling my way around some of this stuff with trial and error.

Thanks for the suggestions and help, skybuck.

I guess I was doing the cudaMalloc and cudaMemcpy in the wrong areas.
I moved them around a little while working through other parts of code and it seemed to fix this issue.

Also the reason I was doing the void** in allocating memory is because i’ve been following examples from the “cude by example” book and it has void in there each time. Like I said I’m really feeling my way around some of this stuff with trial and error.

Thanks for the suggestions and help, skybuck.