How to determine the optimal thread number in runtime for CPU-bound task?

junhaotee :

I'm implementing a program that normalizes a big dataset(single file), it's math-intensive calculation and therefore is CPU-bounded.

Having lookup some of the optimal threads questions, most leads to this:

Runtime.getRuntime().availableProcessors()

But with HT technology, a single core can handle two threads concurrently.

Some of the previous answers also state that the optimal threads = number of cores, I'm not sure if it's correct for CPU-bounded tasks.

Knowing each OS might perform parallel programming differently, should I be solely using availableProcessors() without having to consider elements like:

  • Thread(s) per core
  • core(s) per socket
  • CPU socket(s)

For example, should I be using availableProcessors() * Thread(s) per core to get the optimal threads? Does it create thread competition?

I'm looking for the recommended practice achieve this, without having to modify and rebuild the program when the program moves to the other machine.(the program is only tested on a local machine)

Thanks in advance.

user11600206 :

If you're trying to find an optimal thread number for a non-blocking CPU-bound tasks.

Javadoc for availableProcessors():

public int availableProcessors()
Returns the number of processors available to the Java virtual machine.
This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately.

Returns:
the maximum number of processors available to the virtual machine; never smaller than one
Since:
1.4

Please note that the line - Returns the number of processors available to the Java virtual machine.

Since JVM operates at the layer between OS and Java program, it does not return the number of physical processors, instead it returns the number of JVM logical processors.(The coordination between logical processors and physical processors depends on JVM and the OS the JVM is running.)

Therefore if you have

  • 1 CPU Socket
  • 4 Cores per Socket
  • 2 Threads per Core

the availableProcessors() should return an integer less than or equal to 8, depends on the runtime environment. Another example:

  • 2 CPU Sockets
  • 4 Cores per Socket
  • 2 Threads per Core

This will return an integer less than of equal to 16 depends on runtime environment.

Therefore the number optimal threads should be equal to the availableProcessors(). However, to get the actual optimal thread number, it's still best to perform a test on the environment. Since you'd like to prevent the complication of rebuilding, perhaps the best thing to do is to follow the Javadoc instruction.

It's also stated in the Javadoc: Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately..

Do query for this property if you wish to have less thread-racing overhead.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=131600&siteId=1