开发者

c++ multithread optimization

in my code I have 2/4 threads performing montecarlo simulations. Each of them runs a number of experiments and they all collect the results into a stl vector. My question is this: suppose each thread runs 1000 experiments sequentially. Is is better to store the result into the shared vector one at the time, or every once in a while? If they wait until they have some consistent amount of data, writing into the vector will take lo开发者_Python百科nger, so I'm not sure whether the second solution is necessarily better than the first one.

PS each experiment is numerical computation, so no IO operations.

Thanks


If you are going to wait until all the results are computed before you use any of the results, preallocate space for 4,000 results in the vector and have each thread write into one range of elements in the vector. No locking is required because no two threads access the same element in the vector.

If you want to use the results as they are computed, use some sort of a concurrent queue data structure instead of a vector.


If you're only putting 2000 to 4000 elements in the vector I doubt it would make much of a difference either way.

Do whatever is most natural for the algorithm. If that doesn't work well enough look into doing it the other way.

After thinking about it for a bit, it might serve both purposes (simplicity and speed) to have each thread store results to a local vector then copy the contents of the local vector to the 'global' vector (protected by a lock) when the thread is done. Of course, that's as long as whatever's waiting for the results can wait until a thread is fully finished before getting an update.


a singly linked list may be a better choice than vector here.

If there is only one thread reading and one thread writing to a fifo .. you don't need any synchronization . The trick is to keep at least one 'dummy' element always in the list, and fifo is empty if head == tail . The head and tail pointers can be manipulated for push and pop, such that there is no need for synchronization..

Using this .. you can make several Q's .. which will not need any synchronization If new/delete is taking time .. you can have Q's to hold reusable elements.

best of luck .

remember .. Exactly one reader, and Exactly one writer .. no more, no less . the trick is createa LOT of Q's like this , Q to recycle objects also .. and you'll not need any thread synchronization stuff ...

If your Q's do run empty .. just a sleep() / wakeup() functionality is needed.

and in case i haven't already said .. Exactly one reader, and Exactly one writer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜