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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:06:18+00:00 2026-05-23T15:06:18+00:00

i am trying to use OpenMP in my program (i am newbie using OpenMP)

  • 0

i am trying to use OpenMP in my program (i am newbie using OpenMP) and the program return in two places errors.

Here is an example code:

#include <iostream>
#include <cstdint>
#include <vector>
#include <boost/multi_array.hpp>
#include <omp.h>

class CNachbarn {
public:
    CNachbarn () { a = 0; }
    uint32_t Get_Next_Neighbor() { return a++; }

private:
    uint32_t a;
};

class CNetwork {
public:
    CNetwork ( uint32_t num_elements_ );
    ~CNetwork();
    void Validity();
    void Clean();

private:
    uint32_t num_elements;
    uint32_t nachbar;

    std::vector<uint32_t> remove_node_v;
    CNachbarn *Nachbar;
};

CNetwork::CNetwork( uint32_t num_elements_  ) {
    num_elements = num_elements_;
    Nachbar = new CNachbarn();

    remove_node_v.reserve( num_elements );
}

CNetwork::~CNetwork() {
    delete Nachbar;
}

inline void CNetwork::Validity() {
    #pragma omp parallel for 
    for ( uint32_t i = 0 ; i < num_elements ; i++ ) {
        #pragma omp critical
        remove_node_v.push_back(i);
    }
}

void CNetwork::Clean () {
    #pragma omp parallel for
    for ( uint8_t j = 0 ; j < 2 ; j++ ) {
        nachbar = Nachbar->Get_Next_Neighbor();
        std::cout << "i: " << i << ", neighbor: " << nachbar << std::endl;
    }

    remove_node_v.clear();
}

int main() {
    uint32_t num_elements = 1u << 3;
    uint32_t i            = 0;
    CNetwork Network( num_elements );

    do {
        Network.Validity();
        Network.Clean();
    } while (++i < 2);

    return 0;
}

I would like to know

  1. if #pragma omp critical is a good solution for push_back()? (Does solve this problem?) would it be better to define for each thread its own vector and then combine them (using insert() )? or some kind of lock?

  2. In my original code i get a running error at: nachbar = Nachbar->Get_Next_Neighbor( &remove_node_v[i] ); but in this example not. Nether the less, i would like OpenMP to use as the number of cores CNachbarn classes, since CNachbarn is recursive computation and should not be influenced from the other threads. The question is how to do it smarty? (I dont think it is smart to define CNachbarn each time i start the for-loop, since i call this function more the million times in my simulation and time is important.

  • 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-23T15:06:19+00:00Added an answer on May 23, 2026 at 3:06 pm

    Concerning your first problem:
    Your function Validity is a perfect way to achieve below serial performance in a parallel loop. However, you already gave the correct answer. You should fill independent vectors for each thread and merge them afterwards.

    inline void CNetwork::Validity() {
    #pragma omp parallel for 
    for ( uint32_t i = 0 ; i < num_elements ; i++ ) {
        #pragma omp critical
        remove_node_v.push_back(i);
    }
    }
    

    EDIT: A possible remedy could look like this (if you require serial access to your elements, you need to change the loop a bit)

    inline void CNetwork::Validity() {
    remove_node_v.reserve(num_elements);
    #pragma omp parallel 
    {
      std::vector<uint32_t> remove_node_v_thread_local;
      uint32_t thread_id=omp_get_thread_num();
      uint32_t n_threads=omp_get_num_threads();
      for ( uint32_t i = thread_id ; i < num_elements ; i+=n_threads ) 
        remove_node_v_thread_local.push_back(i);
    
      #pragma omp critical
      remove_node_v.insert(remove_node_v.end(), remove_node_v_thread_local.begin(), remove_node_v_thread_local.end()); 
    }
    }
    

    Your second problem could be solved by defining an array of CNachbarn with the size of the maximum number of OMP threads possible, and access distinct elements of the array from each thread like:

    CNachbarn* meine_nachbarn=alle_meine_nachbarn[omp_get_thread_num()]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn how to use OpenMP by parallelizing a monte carlo code
I'm trying to use openmp to multithread a loop through std::set. When I write
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I'm a little confused here, I am trying use a partial view in a
I'm trying to point out the difference between serial and parallel program with OpenMP.
I am trying to use OpenMP for threading as it is cross platform. However
I'm trying to implement the distance matrix in parallel using openmp in which I
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]);
I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as

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.