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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:48:37+00:00 2026-05-30T05:48:37+00:00

Is there a way to do parallel reduction of an array on CPU in

  • 0

Is there a way to do parallel reduction of an array on CPU in C/C++?. I recently learnt that it’s not possible using openmp. Any other alternatives?

  • 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-30T05:48:40+00:00Added an answer on May 30, 2026 at 5:48 am

    Added: Note that you can implement “custom” reduction with OpenMP, in the way described here.


    For C++: with parallel_reduce in Intel’s TBB (SO tag: tbb), you can make reduction on complex types such as arrays and structs. Though the amount of required code can be significantly bigger compared to OpenMP’s reduction clause.

    As an example, let’s parallelize a naive implementation of matrix-to-vector multiplication: y=Cx. Serial code consists of two loops:

    double x[N], y[M], C[N][M];
    // assume x and C are initialized, and y consists of zeros
    for(int i=0; i<N; ++i)
      for(int j=0; j<M; ++j) 
        y[j] += C[i][j]*x[i];
    

    Usually, to parallelize it the loops are exchanged to make the outer loop iterations independent and process them in parallel:

    #pragma omp parallel for
    for(int j=0; j<M; ++j) 
      for(int i=0; i<N; ++i)
        y[j] += C[i][j]*x[i];
    

    However it’s not always good idea. If M is small and N is large, swapping the loop won’t give enough parallelism (for example, think of calculating a weighted centroid of N points in M-dimensional space, with C being the array of points and x being the array of weights). So a reduction over an array (i.e. a point) would be helpful. Here is how it can be done with TBB (sorry, the code was not tested, errors are possible):

    struct reduce_body {
      double y_[M]; // accumulating vector
      double (& C_)[N][M]; // reference to a matrix
      double (& x_)[N];    // reference to a vector
    
      reduce_body( double (&C)[N][M], double (&x)[N] )  : C_(C), x_(x) {
        for (int j=0; j<M; ++j) y_[j] = 0.0; // prepare for accumulation
      }
      // splitting constructor required by TBB
      reduce_body( reduce_body& rb, tbb::split ) : C_(rb.C_), x_(rb.x_) { 
        for (int j=0; j<M; ++j) y_[j] = 0.0;
      }
      // the main computation method
      void operator()(const tbb::blocked_range<int>& r) {
        // closely resembles the original serial loop
        for (int i=r.begin(); i<r.end(); ++i) // iterates over a subrange in [0,N)
          for (int j=0; j<M; ++j)
            y_[j] += C_[i][j]*x_[i];
      }
      // the method to reduce computations accumulated in two bodies
      void join( reduce_body& rb ) {
        for (int j=0; j<M; ++j) y_[j] += rb.y_[j];
      }
    };
    double x[N], y[M], C[N][M];
    ...
    reduce_body body(C, x);
    tbb::parallel_reduce(tbb::blocked_range<int>(0,N), body);
    for (int j=0; j<M; ++j)
      y[j] = body.y_[j]; // copy to the destination array
    

    Disclaimer: I am affiliated with TBB.

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

Sidebar

Related Questions

Is there any way of doing parallel assignment in C++? Currently, the below compiles
Is there any way to check whether a file is locked without using a
Is there any way to build some time counter that enable parts of a
Is there any way to use Task Parallel Library in multi computer scenarios ?
Is there a way to guarantee order when using Parallel.ForEach() ? The collection I
Is there a way that I can (using tbb from intel) specify the number
Is there a way to find the name of the program that is running
Is there a way to enter browser mode in a parallel foreach call? I've
Is there a way to loop over multiple lists in parallel in a makefile
I am using Ant's <parallel> task to perform multiple simultaneous targets that use <exec>

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.