I'm trying to understand when JDK will autovectorize. I have the following set of questions (despite googling, reading, experimenting etc.). Given a simple loop as follows:
for(int i=0; size = size(); i < size; i++) {
a[i] = b[i] * c[i];
method1();
// someObject.method2();
// someHashMap.put(b[i], c[i]);
}
- Why is it necessary for the method call "method1" (that appears within the loop) to be inlined for autovectorization to ocurr? (I can't understand why that must be necessary....)
- Perhaps this a "silly" question, but what if "someObject.method2()" were uncommented. (And let's assume that method2 is huge method, ie many lines). Would that prevent autovectorization too? What if method2 were a tiny method (eg just 1 or 2 lines etc.?)
- What if the "someHashMap" line were uncommented? Would the fact that we have an object/variable that would be shared accross all the SIMD cause the autovectorization to fail too? (I can't see how it could work unless jdk would somehow insert a "syncronization" keyword automatically when accessing the common object/var of "someHashMap"
- It seems to me that the "streaming" interface would solve the problem implied in question #3 directly above, since the "collector" logic in streams would automatically take care of merging individual hashmaps and so we would not need any "synchronized" word. (And in general, it almost seems like the streaming API is a perfect API to allow jdk to automatically use autovectorization, so long as there are no "outside vars" (ie no side effects) when creating the streaming code...Does jdk/jit compiler automatically do autovectorization as a result when the code is written using the standard streaming interface? If not, wouldn't it make sense to do so (perhaps in a future jdk version or perhaps a jdk from some other vendor?)
- If the body of the loop contains many many if statements etc (lots of branching and let's say further that each branch does lots of computation), would that mean that a) autovectorization is probably a BAD idea (just as it would be for a GPU) and b) the jit compiler is smart enough to determine that autovectorization is a bad idea and so it won't autovectorize?
- I am currently using Oracle jdk8, but do the answers change above if one uses jdk9 or jdk10, etc.?