-3

I have a question about multithreading (parallelism) in java. Indeed, I realized two program to compute a Mandelbrot Set :

  1. The first launches n threads and each thread computes a part of the height of the Mandelbrot (Example : https://www.logre.eu/mediawiki/images/4/49/Mandelbrot_h_block.png).

  2. The second launches a pool of n threads where each thread computes a line of pixels of the Mandelbrot (Example : https://www.logre.eu/mediawiki/images/f/f2/Mandelbrot_horizontal.png).

I made different profiling on a two cores machine and I don't understand why the first program is faster than the second if the number of threads (n) is greater than the number of cores. This is the opposite if the number of threads is lower than the number of cores.

Can anyone help me?

Note : Is there also a limit of threads to compute this program ?

Steve23
  • 113
  • 5
  • 2
    This is missing all the detail required. Without seeing the code or how you profiled it, we don't know you've done that correctly and fairly. Without seeing the timings we don't know how significant the difference is. We don't even know `n`. – weston May 11 '16 at 08:39

3 Answers3

1

Since threads in a pool are consuming the same amount of memory as individual threads, the advantage is on the ctx switching and how thread pool is creating/deleting active threads. If you have much more threads than cores to run them the program will spend too much time in context switching.

If you have let's say 1000 threads on a 2 core (as you said) you are going to have too much context switching and for that reason the thread pool will perform better.

So I guess that you aren't creating so many threads on both cases and the difference isn't notable.

If the difference is notable maybe you can try showing us your code in order to give you some further information.

Check about the number of threads that you can compute in another post.

Community
  • 1
  • 1
jsurf
  • 189
  • 3
  • 11
0

You're comparing apples and oranges. Two different implementations and even different numbers of threads.

You should not use more threads than CPU cores your machine have. If n >> number of CPU cores, a large part of CPU utilization will be devoted to context switching between threads.

carbolymer
  • 1,439
  • 1
  • 15
  • 30
0

I don't think you should be trying to make any claims about a number of threads smaller or greater than the number of cores, if you test this only on a dual-core machine, for two reasons

  • Firstly, with only 2 cores, almost every multi-threading system uses more threads than you have cores, so this metric becomes rather meaningless.

  • More importantly, without testing on a machine with a different number of cores, you can't know whether it is about more or fewer threads than cores, or just about more or fewer than 2 threads.

Having said this, you have used two completely different algorithms. It seems that the one just scales better with more threads than the other, and the crossover point happens to be at 2 threads. Without knowing the details of the algorithms, we can't say anything about why this is the case.

Oebele
  • 581
  • 1
  • 4
  • 17