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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:43+00:00 2026-05-26T18:39:43+00:00

I have a very weird problem using OpenMP in my C++ code: void update(double

  • 0

I have a very weird problem using OpenMP in my C++ code:

void update(double *source, double *target, int n)
{
    target[0] = source[0];
    target[n-1] = source[n-1];
    #pragma omp parallel for 
    for(int i = 1; i < n-1; ++i) 
        target[i] = (1.0/3.0) * (source[i-1] + source[i] + source[i+1]);
}

Both source and target are double arrays with n elements. The code works fine when using it without OpenMP. But as soon as I use the pragma, the code seems to get stuck in this loop. The thing is: I have absolutely NO IDEA why. Hope anyone can help me

  • 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-26T18:39:43+00:00Added an answer on May 26, 2026 at 6:39 pm

    How large is n?

    The default scheduling for a OpenMP parallel for directive is implementation specific. It looks like in GOMP (the OpenMP implementation used by gcc), the default is (dynamic,1) according to the documentation here. This means that each thread is accessing (at i-1 and i+1) memory locations that are loaded by neighboring threads, which could lead to poor cache utilization. On modern CPU architectures, stencil operations like this are frequently memory-bound and sensitive to caching. You could try specifying a schedule with larger chunks, for instance with:

    #pragma omp parallel for schedule(dynamic,1024)
    

    I’m just using 1024 here as an example. In practice, you should experiment to find the optimal chunking factor (or systematically search with a parameter sweep, a process often called “auto-tuning”). Or you could choose a value based more in theory, for instance by deriving it from the L1 or L2 cache size of your CPU.

    Or you could instead try static scheduling, since the amount of computation inside the for loop is uniform across threads, and the overhead of the dynamic scheduler may be causing a bottleneck. If you specify

    #pragma omp parallel for schedule(static)
    

    without a chunk size, then each thread will be assigned a single chunk of roughly the same size.

    Finally, you may also want to pin the OpenMP threads to their own CPU cores. You can do this using the GOMP_CPU_AFFINITY environment variable.

    Edit:

    I was just playing around with the following test program compiled with gcc 4.2.1, and I think the documentation I linked to above is incorrect. It looks like GOMP defaults to schedule(static).

    #include <stdio.h>
    #include <omp.h>
    
    int main(int argc, char** argv)
    {
        int i;
        #pragma omp parallel for
        for (i=0; i<15; i++) {
            int id = omp_get_thread_num();
            printf("%d assigned to thread %d\n", i, id);
        }
    }
    

    And the output with two threads is:

    $ ./test_sched | sort -n
    0 assigned to thread 0
    1 assigned to thread 0
    2 assigned to thread 0
    3 assigned to thread 0
    4 assigned to thread 0
    5 assigned to thread 0
    6 assigned to thread 0
    7 assigned to thread 0
    8 assigned to thread 1
    9 assigned to thread 1
    10 assigned to thread 1
    11 assigned to thread 1
    12 assigned to thread 1
    13 assigned to thread 1
    14 assigned to thread 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very weird problem. I am using PHP, in my PHP code,
I have a very weird problem: sometimes when I call nHibernate update to an
I have a very weird problem. This ONLY affects Firefox on Mac, using the
So I have a very weird problem. I am writing some code in VB.Net
I have a very weird problem. Have an application using Hibernate and spring.I have
I have a very weird problem.. I really do hope someone has an answer
I have a very weird problem in Firefox ( version 3.5.2), and I am
I have a very weird problem with PROLOG. I have used it before, but
I have seen some very weird for loops when reading other people's code. I
I have come across a very weird error. I'm on Solaris 10, using Ruby

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.