M
M
Marat2016-05-26 12:50:25
Java
Marat, 2016-05-26 12:50:25

How to optimize static methods + parallel computing issues?

There is a MyMath class, which implements mathematical calculations in the form of static methods. Each of the static methods receives a parameter and uses only it or the method's local primitive variables during calculations (i.e., it is thread-safe). Some of the methods take quite a long time to calculate (some even within a few seconds).
These methods are expected to be called from many different threads. These threads are created in different places like:

Runnable myTask = () -> { ... у1=MyMath.calc1(x1); ... уN=MyMath.calcN(xN) ...};
new Thread(myTask).start()
;
How to create all this competently from the point of view of performance and usability of the code (as long as the methods are static classes + call from the created threads)?
Can we put the same for all ThreadPoolExecutor + calculations in it, designed as Callable ?
Maybe when dynamically creating a thread, put the body of the function into it (then there will be duplication, which is bad, but the execution speed will increase)?
As I understand it, there is no analogue of the c-th inline.
Let be
class MyMAth {
  public static double myCalc(double x) {
     double res=0;
     for(i=0;i< 1000000;i++) 
       res += страшная матан формула, зависящая от i и от x;
     return res;
  }
}

If, for example, 50 threads call MyMath.myCalc(x), then the processor, when switching threads every time: when the function calculation is interrupted, it stops the next iteration of the loop + saves the state of the variables, and when it wakes up, restores the state of the same loop for other variables + continues to calculate.
Question 2 - When executed, does it additionally copy the static function code (which is identical) separately for each process (to be further cached in a code block for each core) or not? Or, when switching between threads, does it simply copy/restore the state of registers and other service areas, and then copy the static function code section again each time?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily, 2016-05-26
@Applez

Creating a thread is expensive, it's better to always have a pool (if you can roughly estimate how many threads you need, then FixedThreadPool, if not, then let the JVM choose and use CachedThreadPool). Regarding inline-methods, it has already been discussed a million times, the JVM is pretty smart to decide whether to inline methods, you just need to warm it up. Information can be obtained from here . More specifically, this paragraph:
Method Inlining
The frequency of virtual method invocations in the Java programming language is an important optimization bottleneck. Once the Java HotSpot adaptive optimizer has gathered information during execution about program hot spots, it not only compiles the hot spot into native code, but also performs extensive method inlining on that code.
Inlining has important benefits. It dramatically reduces the dynamic frequency of method invocations, which saves the time needed to perform those method invocations. But even more importantly, inlining produces much larger blocks of code for the optimizer to work on. This creates a situation that significantly increases the effectiveness of traditional compiler optimizations, overcoming a major obstacle to increased Java programming language performance.
Inlining is synergistic with other code optimizations, because it makes them more effective. As the Java HotSpot compiler matures, the ability to operate on large, inlined blocks of code will open the door to a host of even more advanced optimizations in the future.
If your task is executed without blocks / interruptions to wait for external actions, then you will not be able to influence the operation of the processor / JVM and everything else, in this case I suggest forgetting about processors / registers / etc.
For the rest, read the documentation, find out what compiles into what and how it works, it's too long to describe it here.
Good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question