Wednesday, 15 May 2013

c++ - Efficiency of Array with individual mutexes protecting them or one mutex protecting it -



c++ - Efficiency of Array with individual mutexes protecting them or one mutex protecting it -

so doing bit of thinking other day concurrency , wondering whether faster protect array individual mutexes each element or whether faster entire array utilize 1 mutex protect info in it. logically, figured programme execute faster invidual mutexes each thread need "checkout" element need, kinda sounds allow improve concurrency. if 1 able execute @ time waiting on mutex, certainly there lot of waiting going on. test theory, created set of tests this. in both functions, done mutex locked , random value written random location in array, difference beingness each element has it's own mutex in first function, , elements share mutex in second. left number of runs constant 25 average @ end of each test.

i ran with: num_elements = 10; num_threads = 5; results

num_elements = 100; num_threads = 5; results

num_elements = 10; num_threads = 10; results

num_elements = 100; num_threads = 10; results

num_elements = 10; num_threads = 15; results

num_elements = 100; num_threads = 15; results

for set utilize 10 elements in array, here graph of averages 2 different methods. here

for set utilize 100 elements in array, here graph of averages 2 different methods. here

for record done in mingw don't have working linux box ( because reasons), no other flags besides -c++11. can see, original theory, exclusively incorrect. apparently if entire array of values shares 1 mutex writing, quite bit faster each value having it's own lock. seems exclusively counter intuitive. question clever people out there, going on in scheme or elsewhere causing conundrum. please right thinking!

edit: noticed graphs didn't import correctly, ya'll have no thought going on. fixed

edit: on suggestion of @nanda, implemented 2 more similar tests, except utilizing thread pool of 4 threads, process same number of random assignments test vector other threads make. here updated tests, , here output file.on whimsey, decreased number of threads original 2 test used 4 (the number of cores on cpu), , output suggests, 2 methods very similar in average time elapsed. allows conclusion @nanda's reasoning correct, big number of runnable threads, (or more threads have cores), causes scheme have queue process threads causes big amount of delay. on whim, added "control" grouping speak asynchronous loops makes same number of random accesses randomely array. considerably faster concurrent methods of doing so. also, may notice, threadpool method performaing same amount of accesses original 2 methods, finish much quicker original 2 methods.

so here 2 new questions. why in world concurrent methods incredibly slow compared asynchronous method. also, why threadpool method of concurrency quite bit faster original method?

c++ multithreading c++11 mingw

No comments:

Post a Comment