Thread: Using OpenMP and STL vectors

  1. #16
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elkvis View Post
    using boost will simply require you to do explicitly in code, what OMP is doing for you implicitly. you'll have to explicitly create and lock mutexes to access the vector, among other things.
    Thanks. So I guess the ultimate solution would be to make one RNG for each thread? Do you know how one could do that?

  2. #17
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you're using gcc of a recent enough version, you can use the <random> header, and follow the information in the C++ reference

  3. #18
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elkvis View Post
    if you're using gcc of a recent enough version, you can use the <random> header, and follow the information in the C++ reference
    Thanks, but as far as I have read online, they are not thread safe either? Or have I misunderstood something? It was not entirely clear from the discussion, there are varying opinions.
    Last edited by Niels_M; 04-09-2013 at 03:20 PM.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    but if you create one per thread, in conjunction with std::thread or boost::thread, then you wouldn't need to worry about thread safety, because each thread owns its RNG. since you said you have 6 cores, you could theoretically start 6 threads to generate n/6 numbers, and then merge them all together into one vector at the end.

  5. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by Elkvis View Post
    but if you create one per thread, in conjunction with std::thread or boost::thread, then you wouldn't need to worry about thread safety, because each thread owns its RNG. since you said you have 6 cores, you could theoretically start 6 threads to generate n/6 numbers, and then merge them all together into one vector at the end.
    Ah, I see what you mean. That seems like a good approach. It is not stated 100% explicitly, but is this an example of how to achieve your suggestion? If yes, then it is not clear to me, where in that example a unique RNG is assigned to each thread.

    I'm very happy for your contribution so far. Hopefully I can nail this thing very soon and finally see some progress.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I'm not really very familiar with openmp, so I can't really say if that's what I was talking about. personally, I would do it explicitly with std::thread or boost::thread, but that's just what I'm most comfortable with.

  7. #22
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    OK, I'll have to keep working on this.. thanks to everyone for participating and helping me advance.

  8. #23
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Locking kills. Find an algorithm that doesn't require locking.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where is my flaw, regarding use of OpenMP critica
    By ass in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2011, 05:19 AM
  2. OpenMP question
    By oacikgoz in forum C++ Programming
    Replies: 0
    Last Post: 01-28-2009, 12:52 PM
  3. Thoughts on OpenMP
    By abachler in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2008, 04:25 PM
  4. Internals of OPENMP?
    By chiku123 in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2007, 10:19 AM
  5. openmp
    By l2u in forum Tech Board
    Replies: 0
    Last Post: 04-12-2007, 05:40 PM