Store reduction result on device

Hi,

I have a loop reduction and want to store its result in a device variable, I found a solution that works, however I was wondering if there is a better way to get it done.

int themax = 0;
#pragma acc parallel loop reduction(max:themax) present(arr)
for(int i = 0; i < size; ++i)
{
	if( arr[i] > themax )
		themax = arr[i];
}
#pragma acc serial
{
	max_ = themax;
}

(max_ is a member variable and is already allocated on the device)

Kind regards,
Rob

Hi Rob,

I would say try using the “max_” member directly in the reduction, but when I tried this I got incorrect answers. I sent a note to engineering to see if this is a compiler bug or if I’m missing something in the OpenACC standard stating this isn’t allowed.

For the time being, you’re method should be fine.

-Mat