0

Is that a lot of memory access makes slow multithreading? Because I use pthread to multithread a great function who use a lot of memory access. And I have time CPU greater then if I call my function with 1 thread. And proportion of use CPUs is between 50% and 70%.

suspectus
  • 16,548
  • 8
  • 49
  • 57
  • 1
    Yes may be, because of context switch time among threads, always give enough work to a thread, design in parallel programming is very important – Grijesh Chauhan Jul 30 '13 at 15:51
  • Not every code is parallelliz... parall... threadable. It all depends on what you're actually doing inside. – Nbr44 Jul 30 '13 at 15:51
  • Read about memory thrashing here: "[What is “cache-friendly” code?](http://stackoverflow.com/a/16699282/335858)" – Sergey Kalinichenko Jul 30 '13 at 15:52
  • in the case of excessive crosstalk between threads, yes multithreading can be slower, at some level the code has to "synchronize" the memory between the various processes or threads. In general, it takes planning to make efficient parallel code, not just breaking up an algorithm that was written for in-order processing – ckozl Jul 30 '13 at 15:55
  • Try running the threads on a single core - if the performance is better than running the threads on multiple cores, this indicates cache bounce issues. If not, it indicates nothing :) – chill Aug 01 '13 at 08:46

2 Answers2

1

Aside (since you seem to be talking about memory access and not allocation), the default malloc has poor performance if you are allocating memory in parallel.

If you are looking for higher performance you may want to consider TCMalloc which scales significantly better with multithreaded allocations.

In general, keeping shared memory synchronised between threads is a nightmare that should probably be avoided if possible. See if you can avoid cache invalidations by adopting a message-passing paradigm (this may not be possible for your use-case).

Message passing with shared read-only memory is a good compromise for lowering cache traffic.

Aaron Cronin
  • 2,093
  • 14
  • 13
1

Don't guess; measure.

You don't say what OS you're using, but given pthreads I'm going to guess Linux. Use tools like Valgrind's callgrind and cachegrind to analyse where your program is spending its time. LTTng could also help you. Maybe perf also.

Yes, if your program is maxing out your memory bandwidth, or thrashing your cache, then multithreading could certainly slow down performance. This is especially true if the threads are trying to share any resources. BUT, you won't know if you don't look.

ams
  • 24,923
  • 4
  • 54
  • 75