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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:50:24+00:00 2026-06-15T21:50:24+00:00

The following loop iterates over all edges of a graph, determines if the end

  • 0

The following loop iterates over all edges of a graph, determines if the end nodes belong to the same group, and then adds the edge weight to the total edge weight of that group.

// TODO: parallel
FORALL_EDGES_BEGIN((*G)) {
    node u = EDGE_SOURCE;
    node v = EDGE_DEST;
    cluster c = zeta[u];
    cluster d = zeta[v];
    if (c == d) {
        // TODO: critical section
        intraEdgeWeight[c] += G->weight(u, v);
    } // else ignore edge
} FORALL_EDGES_END();

I would like to parallelize it with OpenMP. I think that the code in the if statement is a critical section that might lead to a race condition and wrong results if threads are interrupted in the middle of it (correct?).

If the += operation could be made atomically, I believe that the problem is solved (correct?). However, I’ve looked up the atomic directive and there it states that:

Unfortunately, the atomicity setting can only be specified to simple
expressions that usually can be compiled into a single processor
opcode, such as increments, decrements, xors and the such. For
example, it cannot include function calls, array indexing, overloaded
operators, non-POD types, or multiple statements.

What should I use to parallelize this loop correctly?

  • 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-15T21:50:25+00:00Added an answer on June 15, 2026 at 9:50 pm

    Actually the accepted syntax for atomic update is:

    x++;

    x–;

    ++x;

    —x;

    x binop= expr;

    x = x binop expr;

    where x is a scalar l-value expression and expr is any expression, including function calls, with the only restriction being that it must be of scalar type. The compiler would take care to store the intermediate result in a temporary variable.

    Sometimes it’s better to consult the standards documents instead of reading tutorials on the Internet. Observe the OpenMP 3.1 standard Expample A.22.1c:

    float work1(int i)
    {
      return 1.0 * i;
    }
    ...
    #pragma omp atomic update
    x[index[i]] += work1(i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following loop since I have to repeat the same/similar code 25
Consider the following round trip iterator, which tries to iterate over all the elements
Say I have the following loop: i = 0 l = [0, 1, 2,
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {
I have the following loop inside an function init(); which is executed onload, I
I have the following loop... for ($i = 1; $i <= 10; $i++) {
I am using the following loop to send/attach multiple attachments to my email message.
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I am using the following loop to add items to an an array. I
I have the following loop: for(var myScreen in wizardScreens){ if(step==index)$(myScreen).show(); else $(myScreen).hide(); index++; }

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.