Multi-thread program(process) on multicore-core processor(s) with hyperthreading

1k views Asked by At

For multicore computing, one thing confusing me from the beginning is the model of multicore hardware is too abstracted from the real machine.

I worked on a laptop with a single intel processor, containing 4 cores and support hyperthreading which makes the num of logical cores 8.

Suppose I have a Java program implemented a concurrent algorithm (it is said Java would use the OS rule of thread scheduling, so JVM won't affect the scheduling), and the program is purely CPU-bound.

My observations:

  • if the program(process) running less than 8 threads, parallelism of work increases as the num of threads increasing
  • when the total threads num is larger than 8, the performance become complicated, but usually no more improvement than running 8 threads; and for some certain algorithm, it is much worse, i.e.time-consumption increased hugely than running 8 threads.

My knowledge of this: As far as I know, the program I run is treated as a user process by the OS, and if the program created threads to try to gain parallelism, the OS will try to schedule these threads among the cores available. Any threads of the process on a same core may share the total execution time of the process on that core.

My questions: Suppose the CPU is only running my program. i.e. no other user processes.

  • if there is only 1 core of the CPU, the process will get no benefit of parallelism by multi-threading, since the total execution time of the process will not change. Is it true?

  • if there are more than one cores available, the OS will try to schedule the threads of the process evenly and fairly on different cores, and the process's threads on different cores get their own (extra) execution time, therefore speeding up. Is it true?

  • if there are n threads and m cores, where n>m, than some core may run more than 1 thread of the process, which may even harm the speed-up of parallelism because of "context switch" among threads on the same core and potentially side-effect of threads of the process running at different speed. Is this true?

Thanks very much!

1

There are 1 answers

1
Thilo On BEST ANSWER

if there is only 1 core of the CPU, the process will get no benefit of parallelism by multi-threading, since the total execution time of the process will not change. Is it true?

Only if you are 100% CPU-bound. If you have I/O waits, multiple threads can help a lot even on a single core.

if there are more than one cores available, the OS will schedule the threads of the process evenly and fairly on different cores

That seems to be up to the discretion of the OS. There could be all kinds of quota and priorities involved.

may even harm the speed-up of parallelism because of "context switch" among threads on the same core

It is true that there is overhead in managing extra threads (not just at the scheduling level, but also in synchronizing and communicating within your application), and if these threads cannot make productive use of otherwise idle CPU cores, then having less threads can actually improve performance.