Boost Random and OpenMP

后端 未结 3 983
独厮守ぢ
独厮守ぢ 2021-01-03 04:58

I\'m getting a \"bus error\" from an OpenMP parallel section of code. I recreated a simple version of my problem below. The code essentially makes many calls to the function

3条回答
  •  隐瞒了意图╮
    2021-01-03 05:19

    I think the most convenient solution would involve a thread_local RNG and a seeding that involves the thread ID as a unique number for each thread, for example, you can do a XOR between the system time and the thread-id to seed the RNG. Something along the lines of (using C++11):

    #include 
    #include 
    
    #include 
    #include 
    
    boost::random::mt19937& get_rng_engine() {
      thread_local boost::random::mt19937 eng(
        reinterpret_cast(std::time(NULL)) ^ std::this_thread::get_id());
      return eng;
    };
    

    (NOTE: you can also use if you are going to use C++11)

    If you can't use C++11 then you can use boost::thread instead to have a similar behavior, see the Boost page on thread-local storage too.

提交回复
热议问题