Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:20:58+00:00 2026-06-02T14:20:58+00:00

I’m having problems grasping why my function that finds maximum and minimum in a

  • 0

I’m having problems grasping why my function that finds maximum and minimum in a range of doubles using CUBLAS doesn’t work properly.

The code is as follows:

void findMaxAndMinGPU(double* values, int* max_idx, int* min_idx, int n)
{
    double* d_values;
    cublasHandle_t handle;
    cublasStatus_t stat;
    safecall( cudaMalloc((void**) &d_values, sizeof(double) * n), "cudaMalloc     (d_values) in findMaxAndMinGPU");
    safecall( cudaMemcpy(d_values, values, sizeof(double) * n, cudaMemcpyHostToDevice), "cudaMemcpy (h_values > d_values) in findMaxAndMinGPU");
    cublasCreate(&handle);

    stat = cublasIdamax(handle, n, d_values, sizeof(double), max_idx);
    if (stat != CUBLAS_STATUS_SUCCESS)
        printf("Max failed\n");

    stat = cublasIdamin(handle, n, d_values, sizeof(double), min_idx);
    if (stat != CUBLAS_STATUS_SUCCESS)
        printf("min failed\n");

    cudaFree(d_values);
    cublasDestroy(handle);
}

Where values is the values to search within. The max_idx and min_idx are the index of the found numbers in values.
The results from the CUBLAS-calls seems rather random and output wrong indexes.

Anyone with a golly good answer to my problem? I am a tad sad at the moment 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:21:00+00:00Added an answer on June 2, 2026 at 2:21 pm

    One of your arguments to both the cublasIdamax and cublasIdamin calls are wrong. The incx argument in BLAS level 1 calls should always be the stride of the input in words, not bytes. So I suspect that you want something more like:

    stat = cublasIdamax(handle, n, d_values, 1, max_idx);
    if (stat != CUBLAS_STATUS_SUCCESS)
        printf("Max failed\n");
    
    stat = cublasIdamin(handle, n, d_values, 1, min_idx);
    if (stat != CUBLAS_STATUS_SUCCESS)
        printf("min failed\n");
    

    By using sizeof(double) you are telling the routines to use a stride of 8, which will have the calls overrun the allocated storage of the input array and into uninitialised memory. I presume you actually have a stride of 1 in d_values.


    Edit: Here is a complete runnable example which works correctly. Note I switched the code to single precision because I don’t presently have access to double precision capable hardware:

    #include <cuda_runtime.h>
    #include <cublas_v2.h>
    #include <cstdio>
    #include <cstdlib>
    #include <sys/time.h>
    
    
    typedef float Real;
    
    void findMaxAndMinGPU(Real* values, int* max_idx, int* min_idx, int n)
    {
        Real* d_values;
        cublasHandle_t handle;
        cublasStatus_t stat;
        cudaMalloc((void**) &d_values, sizeof(Real) * n);
        cudaMemcpy(d_values, values, sizeof(Real) * n, cudaMemcpyHostToDevice);
        cublasCreate(&handle);
    
        stat = cublasIsamax(handle, n, d_values, 1, max_idx);
        if (stat != CUBLAS_STATUS_SUCCESS)
            printf("Max failed\n");
    
        stat = cublasIsamin(handle, n, d_values, 1, min_idx);
        if (stat != CUBLAS_STATUS_SUCCESS)
            printf("min failed\n");
    
        cudaFree(d_values);
        cublasDestroy(handle);
    }
    
    int main(void)
    {
        const int vmax=1000, nvals=10000;
    
        float vals[nvals];
        srand ( time(NULL) );
        for(int j=0; j<nvals; j++) {
           vals[j] = float(rand() % vmax);
        }
    
        int minIdx, maxIdx;
        findMaxAndMinGPU(vals, &maxIdx, &minIdx, nvals);
    
        int cmin = 0, cmax=0;
        for(int i=1; i<nvals; i++) {
            cmin = (vals[i] < vals[cmin]) ? i : cmin;
            cmax = (vals[i] > vals[cmax]) ? i : cmax;
        }
    
        fprintf(stdout, "%d %d %d %d\n", minIdx, cmin, maxIdx, cmax);
    
        return 0;
    }
    

    which when compiled and run gives this:

    $ g++ -I/usr/local/cuda/include -L/usr/local/cuda/lib cublastest.cc -lcudart -lcublas
    $ ./a.out
    273 272 85 84
    

    note that CUBLAS follows the FORTRAN convention and uses 1 indexing, rather than zero indexing, which is why there is a difference of 1 between the CUBLAS and CPU versions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.