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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:08:04+00:00 2026-05-23T03:08:04+00:00

I write simple C++ code that compute array reduction sum, but with OpenMP reduction

  • 0

I write simple C++ code that compute array reduction sum, but with OpenMP reduction program works slowly. There are two variants of program: one is simplest sum, another – sum of complex math function. In code complex variant is commented.

#include <iostream>
#include <omp.h>
#include <math.h>

using namespace std;

#define N 100000000
#define NUM_THREADS 4

int main() {

  int *arr = new int[N];

  for (int i = 0; i < N; i++) {
    arr[i] = i;
  }

  omp_set_num_threads(NUM_THREADS);
  cout << NUM_THREADS << endl;

  clock_t start = clock();
  int sum = 0;
  #pragma omp parallel for reduction(+:sum)
  for (int i = 0; i < N; i++) {
    // sum += sqrt(sqrt(arr[i] * arr[i])); // complex variant
    sum += arr[i]; // simple variant
  }

  double diff = ( clock() - start ) / (double)CLOCKS_PER_SEC;
  cout << "Time " << diff << "s" << endl;

  cout << sum << endl;

  delete[] arr;

  return 0;
}

I compile it by ICPC and GCC:

icpc reduction.cpp -openmp -o reduction -O3
g++ reduction.cpp -fopenmp -o reduction -O3

Processor: Intel Core 2 Duo T5850, OS: Ubuntu 10.10

There are execution time of simple and complex variants, compiled with and without OpenMP.

Simple variant “sum += arr[i];”:

icpc
0.1s without OpenMP
0.18s with OpenMP

g++
0.11c without OpenMP
0.17c with OpenMP

Complex variant “sum += sqrt(sqrt(arr[i] * arr[i]));”:

icpc
2,92s without OpenMP
3,37s with OpenMP

g++ 
47,97s without OpenMP
48,2s with OpenMP

In system monitor I see that 2 cores works in program with OpenMP and 1 core works in program without OpenMP. I’ll try several numbers of threads in OpenMP and dont have speedup. I don’t understand why reduction is slow.

  • 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-23T03:08:04+00:00Added an answer on May 23, 2026 at 3:08 am

    The function clock() measures processor time consumed by whole process, so printed time shows sum of time consumed by all threads. If you want to see wall-time (real time elapsed from the begin to the end), use e.g. times() function on the POSIX system

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

Sidebar

Related Questions

I'm trying to think of clever, clear, and simple ways to write code that
I am trying to write a simple matrixMultiplication application that multiplies two square matrices
I am trying to write simple Visual Studio Add-In for code generation. In my
Purpose: Create a program that takes two separate files, opens and reads them, assigns
I need to write a code that compares performance of Java's ArrayList with Scala's
I needed some simple string encryption, so I wrote the following code (with a
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I want to write a simple chat-client in Ruby for the terminal. The Problem
I try to write a simple client/server application (all application is a bluetooth service
I need to write a simple command-line application in Java. It would be nice

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.