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

  • SEARCH
  • Home
  • 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 8146047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:51:47+00:00 2026-06-06T13:51:47+00:00

With OpenMP 3.1, it is possible to have a reduction clause with min :

  • 0

With OpenMP 3.1, it is possible to have a reduction clause with min:

double m;
#pragma omp parallel for reduction(min:m)
for (int i=0;i< n; i++){ 
  if (a[i]*2 < m) {
    m = a[i] * 2;
} 
return m;

Suppose I also need the index of the minimal element; is there a way to use the reduction clause for this? I believe the alternative is writing the reduction manually using nowait and critical.

  • 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-06T13:51:48+00:00Added an answer on June 6, 2026 at 1:51 pm

    Suppose I also need the index of the minimal element; is there a way to use the reduction clause for this?

    Unfortunately, no. the list of possible reductions in OpenMP is very … small. In particular, min and max are the only “higher-level” functions and they aren’t customisable. At all.

    I have to admit that I don’t like OpenMP’s approach to reductions, precisely because it’s not extensible in the slightest, it is designed only to work on special cases. Granted, those are interesting special cases but it’s still fundamentally a bad approach.

    For such operations, you need to implement the reduction yourself by accumulating thread-local results into thread-local variables and combining them at the end.

    The easiest way of doing this (and indeed quite close to how OpenMP implements reductions) is to have an array with elements for each thread, and using omp_get_thread_num() to access an element. Note however that this will lead to a performance degradation due to false sharing if the elements in the array share a cache line. To mitigate this, pad the array:

    struct min_element_t {
        double min_val;
        size_t min_index;
    };
    
    size_t const CACHE_LINE_SIZE = 1024; // for example.
    std::vector<min_element_t> mins(threadnum * CACHE_LINE_SIZE);
    
    #pragma omp parallel for
    for (int i = 0; i < n; ++i) {
        size_t const index = omp_get_thread_num() * CACHE_LINE_SIZE;
        // operate on mins[index] …
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have alike C++ program with openMP usage #pragma omp parallel for private(i) for
I am using openmp and my program looks like as follows: \#pragma omp parallel
I am studying OpenMP, and came across the following example: #pragma omp parallel shared(n,a,b,c,d,sum)
I am running the following OpenMP code #pragma omp parallel shared(S2,nthreads,chunk) private(a,b,tid) { tid
I have an OpenMP with C++ program. There are parallel regions that contain #pragma
Possible Duplicates: C/C++ pragma in define macro Conditional “pragma omp” How can I use
According to the OpenMP web site OpenMp is the de-facto standard for parallel programming
I have a serial application that I parallelized using OpenMP. I simply added the
I have a DLL project in VS 2008 Pro which uses OpenMP. I use
I am trying to parallelize my code using openmp. I have managed to parallelize

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.