Error using cublasIsamax()


I’m trying to get the maximum of a matrix. But using cublasIsamax() I get an error. Here is my code:

/* Includes, system */

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/* Includes, cuda */

#include "cublas.h"

/* Matrix size */

#define N  (4)

/* Main */

int main(int argc, char** argv)


    cublasStatus status;

    float* x;

    float ind;

    int n2 = N * N;

    int i;

    int incx;

/* Initialize CUBLAS */

    printf("simpleCUBLAS test running..\n");

    status = cublasInit();

if (status != CUBLAS_STATUS_SUCCESS) {

        fprintf (stderr, "!!!! CUBLAS initialization error\n");

        return EXIT_FAILURE;


/* Index of maximum*/

    x = (float*)malloc(n2 * sizeof(float));

    /* Fill the matrices with test data */

    for (i = 0; i < n2; i++) 

        x[i] = (float) i;

    incx =1;

    ind = cublasIsamax (n2, x,  incx);

    status= cublasGetError();

        if (status != CUBLAS_STATUS_SUCCESS) {

        fprintf (stderr, "!!!! CUBLAS error\n");

        return EXIT_FAILURE;


printf ("\n Index is %d\n", ind);

/* Memory clean up */


/* Shutdown */

    status = cublasShutdown();

    if (status != CUBLAS_STATUS_SUCCESS) {

        fprintf (stderr, "!!!! shutdown error (A)\n");

        return EXIT_FAILURE;


if (argc > 1) {

        if (!strcmp(argv[1], "-noprompt") ||

            !strcmp(argv[1], "-qatest") ) 


		return EXIT_SUCCESS;





        printf("\nPress ENTER to exit...\n");



    return EXIT_SUCCESS;


I get “CUDA error”, but I printed the matrix and it was ok.

Thanks in advance.

You have to pass a device memory pointer to any CUBLAS function, not a host memory pointer. So allocate some device memory using cudaMalloc or cublasAlloc, the copy the data from host memory to that device memory allocation using cublasSetVector or cudaMemcpy, then pass the device pointer to cublasIsamax.

Thanks a lot. Problem solved.