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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:58+00:00 2026-05-26T11:16:58+00:00

I’m using OpenMP and need to use the fetch-and-add operation. However, OpenMP doesn’t provide

  • 0

I’m using OpenMP and need to use the fetch-and-add operation. However, OpenMP doesn’t provide an appropriate directive/call. I’d like to preserve maximum portability, hence I don’t want to rely on compiler intrinsics.

Rather, I’m searching for a way to harness OpenMP’s atomic operations to implement this but I’ve hit a dead end. Can this even be done? N.B., the following code almost does what I want:

#pragma omp atomic
x += a

Almost – but not quite, since I really need the old value of x. fetch_and_add should be defined to produce the same result as the following (only non-locking):

template <typename T>
T fetch_and_add(volatile T& value, T increment) {
    T old;
    #pragma omp critical
    {
        old = value;
        value += increment;
    }
    return old;
}

(An equivalent question could be asked for compare-and-swap but one can be implemented in terms of the other, if I’m not mistaken.)

  • 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-26T11:16:59+00:00Added an answer on May 26, 2026 at 11:16 am

    As of openmp 3.1 there is support for capturing atomic updates, you can capture either the old value or the new value. Since we have to bring the value in from memory to increment it anyways, it only makes sense that we should be able to access it from say, a CPU register and put it into a thread-private variable.

    There’s a nice work-around if you’re using gcc (or g++), look up atomic builtins:
    http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

    It think Intel’s C/C++ compiler also has support for this but I haven’t tried it.

    For now (until openmp 3.1 is implemented), I’ve used inline wrapper functions in C++ where you can choose which version to use at compile time:

    template <class T>
    inline T my_fetch_add(T *ptr, T val) {
      #ifdef GCC_EXTENSION
      return __sync_fetch_and_add(ptr, val);
      #endif
      #ifdef OPENMP_3_1
      T t;
      #pragma omp atomic capture
      { t = *ptr; *ptr += val; }
      return t;
      #endif
    }
    

    Update: I just tried Intel’s C++ compiler, it currently has support for openmp 3.1 (atomic capture is implemented). Intel offers free use of its compilers in linux for non-commercial purposes:

    http://software.intel.com/en-us/articles/non-commercial-software-download/

    GCC 4.7 will support openmp 3.1, when it eventually is released… hopefully soon 🙂

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

Sidebar

Related Questions

I am using OpenMP to parallelize loops. In normal case, one would use: #pragma
i am trying to use OpenMP in my program (i am newbie using OpenMP)
I'm normally used to using OpenMP for parallelizing tasks. However, I thought I might
I have a serial application that I parallelized using OpenMP. I simply added the
I'm working in C with openMP using gcc on a linux machine. In an
Using PyObjC , you can use Python to write Cocoa applications for OS X.
To loop over elements of a container, I would typically use an iterator, like
I am writing a multi-threaded program using OpenMP in C++. At one point my
I am considering using OpenMP for multithreading in C++. But I wonder if there
I have the following C/C++ code using OpenMP: int nProcessors=omp_get_max_threads(); if(argv[4]!=NULL){ printf(argv[4]: %s\n,argv[4]); nProcessors=atoi(argv[4]);

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.