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 712549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:51:38+00:00 2026-05-14T04:51:38+00:00

I was searching Google for a page offering some simple OpenMp algorithms. Probably there

  • 0

I was searching Google for a page offering some simple OpenMp algorithms.
Probably there is an example to calculate min, max, median, average from a huge data array but I am not capable to find it.

At least I would normally try to divide the array into one chunk for each core and do some boundary calculation afterwards to get the result for the complete array.

I just didn’t want to reinvent the wheel.


Additional Remark:
I know that there are thousands of examples that work with simple reduction.
e.g. Calculating PI.

const int num_steps = 100000; 
double x, sum = 0.0; 
const double step = 1.0/double(num_steps); 
#pragma omp parallel for reduction(+:sum) private(x) 
for (int i=1;i<= num_steps; i++){ 
  x = double(i-0.5)*step; 
  sum += 4.0/(1.0+x*x); 
} 
const double pi = step * sum;

but when these kind of algorithms aren’t usable there are almost no examples left for reducing algorithms.

  • 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-05-14T04:51:38+00:00Added an answer on May 14, 2026 at 4:51 am

    OpenMP (at least 2.0) supports reduction for some simple operations, but not for max and min.

    In the following example the reduction clause is used to make a sum and a critical section is used to update a shared variable using a thread-local one without conflicts.

    #include <iostream>
    #include <cmath>
    
    int main()
    {
      double sum = 0;
      uint64_t ii;
      uint64_t maxii = 0;
      uint64_t maxii_shared = 0;
    #pragma omp parallel shared(maxii_shared) private(ii) firstprivate(maxii)
      {
    #pragma omp for reduction(+:sum) nowait
        for(ii=0; ii<10000000000; ++ii)
          {
            sum += std::pow((double)ii, 2.0);
            if(ii > maxii) maxii = ii;
          }
    #pragma omp critical 
        {
          if(maxii > maxii_shared) maxii_shared = maxii;
        }
      }
      std::cerr << "Sum: " << sum << " (" << maxii_shared << ")" << std::endl;
    }
    

    EDIT: a cleaner implementation:

    #include <cmath>
    #include <limits>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <tr1/random>
    
    // sum the elements of v
    double sum(const std::vector<double>& v)
    {
      double sum = 0.0;
    #pragma omp parallel for reduction(+:sum)
      for(size_t ii=0; ii< v.size(); ++ii)
        {
          sum += v[ii];
        }
      return sum;
    }
    
    // extract the minimum of v
    double min(const std::vector<double>& v)
    {
      double shared_min;
    #pragma omp parallel 
      {
        double min = std::numeric_limits<double>::max();
    #pragma omp for nowait
        for(size_t ii=0; ii<v.size(); ++ii)
          {
            min = std::min(v[ii], min);
          }
    #pragma omp critical 
        {
          shared_min = std::min(shared_min, min);
        }
      }
      return shared_min;
    }
    
    // generate a random vector and use sum and min functions.
    int main()
    {
      using namespace std;
      using namespace std::tr1;
    
      std::tr1::mt19937 engine(time(0));
      std::tr1::uniform_real<> unigen(-1000.0,1000.0);
      std::tr1::variate_generator<std::tr1::mt19937, 
        std::tr1::uniform_real<> >gen(engine, unigen);
    
      std::vector<double> random(1000000);
      std::generate(random.begin(), random.end(), gen);
    
      cout << "Sum: " << sum(random) << " Mean:" << sum(random)/random.size()
           << " Min:" << min(random) << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching on Google reveals x2 code snippets. The first result is to this code
I thought this was asked before, but 15 minutes of searching on Google and
I am searching for a program like Google Analytics or Clicky which runs on
Searching for some sample code for converting a point in WGS84 coordinate system to
Im searching a tool which allows me to specify some folders as bookmarks and
i want to scrape the top 10 search links from a google page on
I've been searching google for recommendations on conventions or best practices when it comes
Got a problem that seems to be fairly endemic with IE8 (searching on Google)
Searching for a string within a string is extremely well supported in .NET but
While searching the interweb for a solution for my VB.net problems I often find

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.