Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory access vs. memory copy

I am writing an application in C++ that needs to read-only from the same memory many times from many threads. My question is from a performance point of view will it be better to copy the memory for each thread or give all threads the same pointer and have all of them access the same memory.

Thanks

like image 670
gerstla Avatar asked May 24 '12 08:05

gerstla


1 Answers

There is no definitive answer from the little information you have given about your target system and so on, but on a normal PC, most likely the fastest will be to not copy.

One reason copying could be slow, is that it might result in cache misses if the data area is large. A normal PC would cache read-only access to the same data area very efficiently between threads, even if those threads happen to run on different cores.

One of the benefits explicitly listed by Intel for their approach to caching is "Allows more data-sharing opportunities for threads running on separate cores that are sharing cache". I.e. they encourage a practice where you don't have to program the threads to explicitly cache data, the CPU will do it for you.

like image 191
Prof. Falken Avatar answered Oct 15 '22 00:10

Prof. Falken