How to get the max value in a vector when using cublas

As you know, cublasIsamax return the least index of the maximum element in a vector…

So do you guys know how to get the max VALUE directly? some hint or resource about how to implement cublasIsamax effectively is appreciated.

if not, is there some easy way to return only one element from the vector? for easy, I mean no need to create a host array to hold all the values and pick the one needed…

I found the reduction example provided in SDK… and I got the answer…

Hi. You can directly access the max value using a cudaMemcpy to a single value.

Suppose you have a device array devArray of array size arraySize and want to store the maximum value in the host memory variable max. You will also need an int variable to get the max value index: maxIndex.

// Host variable that will store the maximum value.
double max;

// Array maximum index (in FORTRAN base).
int maxIndex;

// Call cublas to get maxIndex: note that maxIndex is passed as a pointer to the cublas call.
cublasIdamax(cublasHandle, arraySize, devArray, 1, &maxIndex);

// Copy max value onto host variable: variable must be passed as pointer.
// We offset our array by the index returned by cublas. It is important to notice that
// we must reduce also by one since this is FORTRAN based indexing.
cudaMemcpy(&max, devArray+maxIndex-1, sizeof(double), cudaMemcpyDeviceToHost);

// We are not done yet, since the value may be negative.
max = (max >= 0) ? max : -max;

printf("Absolute maximum value of array is %lf.\n", max);

I hope this proves useful.

1 Like