c - Multiple threads and CPU cache -
i implementing image filtering operation in c using multiple threads , making optimized possible. have 1 question though: if memory accessed thread-0, , concurrently if same memory accessed thread-1, cache ? question stems possibility these 2 threads running 2 different cores of cpu. way of putting is: cores share same common cache memory ?
suppose have memory layout following
int output[100];
assume there 2 cpu cores , hence spawn 2 threads work concurrently. 1 scheme divide memory 2 chunks, 0-49 , 50-99 , let each thread work on each chunk. way let thread-0 work on indices, 0 2 4 , on.. while other thread work on odd indices 1 3 5 .... later technique easier implement (specially 3d data) not sure if use cache efficiently way.
in general bad idea share overlapping memory regions if 1 thread processes 0,2,4... , other processes 1,3,5... although architectures may support this, architectures not, , can not specify on machines code run on. os free assign code core likes (a single one, 2 on same physical processor, or 2 cores on separate processors). each cpu has separate first level cache, if on same processor.
in situations 0,2,4.../1,3,5... slow down performance extremely possibly being slower single cpu. herb sutters "eliminate false sharing" demonstrates well.
using scheme [...n/2-1] , [n/2...n] scale better on systems. may lead super linear performance cache size of cpus in sum can possibly used. number of threads used should configurable , should default number of processor cores found.
Comments
Post a Comment