multithreading - Java - multithreaded code does not run faster on more cores -


i running multithreaded code on 4-core machine in hopes faster on single-core machine. here's idea: got fixed number of threads (in case 1 thread per core). every thread executes runnable of form:

private static int[] data; // data shared across threads   public void run() {      int = 0;      while (i++ < 5000) {          // work         (int j = 0; j < 10000 / numberofthreads) {             // each thread performs calculations , reads ,             // writes different part of data array         }          // wait other threads         barrier.await();     } } 

on quadcore machine, code performs worse 4 threads 1 thread. cyclicbarrier's overhead, have thought code should perform @ least 2 times faster. why run slower?

edit: here's busy wait implementation tried. unfortunately, makes program run slower on more cores (also being discussed in separate question here):

public void run() {      // work      synchronized (this) {          if (atomicint.decrementandget() == 0) {              atomicint.set(numberofoperations);              (int = 0; < threads.length; i++)                 threads[i].interrupt();         }     }      while (!thread.interrupted()) {} } 

adding more threads not guarenteed improve performance. there number of possible causes decreased performance additional threads:

  • coarse-grained locking may overly serialize execution - is, lock may result in 1 thread running @ time. overhead of multiple threads none of benefits. try reduce how long locks held.
  • the same applies overly frequent barriers , other synchronization structures. if inner j loop completes quickly, might spend of time in barrier. try more work between synchronization points.
  • if code runs quickly, there may no time migrate threads other cpu cores. isn't problem unless create lot of short-lived threads. using thread pools, or giving each thread more work can help. if threads run more second or each, unlikely problem.
  • if threads working on lot of shared read/write data, cache line bouncing may decrease performance. said, although results in performance degradation, alone unlikely result in performance worse single threaded case. try make sure data each thread writes separated other threads' data size of cache line (usually around 64 bytes). in particular, don't have output arrays laid out [thread a, b, c, d, a, b, c, d ...]

since haven't shown code, can't speak in more detail here.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -