Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to adapt a code to Java Vector API?

Joao Valencio SilvaMar 28 2024 — edited Mar 28 2024

Hello, everyone,

I am trying to adapt a code so that it uses Java Vector API.

The original code (without vectorization) is:

int[] v1 = {311708933, 311708939, 311708941, 311708951, 311708959, 311708981, 311708987, 311709037, 311709067, 311709071, 311709077, 311709103, 311709121, 311709137, 311709143, 311709157, 311709163, 311709169, 311709173, 311709191, 311709227, 311709283, 311709287, 311709337, 311709367, 311709379, 311709397, 311709407, 311709427, 311709439, 311709469, 311709473};
int[] v2 = {150, 171, 180, 183, 184, 289, 301, 306, 358, 449, 486, 539, 567, 605, 643, 655, 678, 680, 699, 711, 733, 750, 786, 799, 810, 827, 846, 873, 903, 923, 939, 981};
int[] result = new int[v1.length];
for (int index = 0; index < v1.length; index++) {
    double d = v1[index] / (double) v2[index];
    result[index] = (int) ((Math.round(d * d) - 1) % 25) + 1; // How do I adapt the content of this loop?
}
for (int i = 0; i < result.length; i++) {
    System.out.println("result[" + i + "]: " + result[i]);
}

The new code, adapted to vectorization is:

int[] v1 = {311708933, 311708939, 311708941, 311708951, 311708959, 311708981, 311708987, 311709037, 311709067, 311709071, 311709077, 311709103, 311709121, 311709137, 311709143, 311709157, 311709163, 311709169, 311709173, 311709191, 311709227, 311709283, 311709287, 311709337, 311709367, 311709379, 311709397, 311709407, 311709427, 311709439, 311709469, 311709473};
int[] v2 = {150, 171, 180, 183, 184, 289, 301, 306, 358, 449, 486, 539, 567, 605, 643, 655, 678, 680, 699, 711, 733, 750, 786, 799, 810, 827, 846, 873, 903, 923, 939, 981};
int[] result = new int[v1.length];
var species = IntVector.SPECIES_PREFERRED;
for (int index = 0; index < v1.length; index += species.length()) {
    var V1 = IntVector.fromArray(species, v1, index);
    var V2 = IntVector.fromArray(species, v2, index);
    var RESULT = V1.div(V2).mul(V1.div(V2)); // How do I adapt the content of the original code?
    RESULT.intoArray(result, index);
}
for (int i = 0; i < result.length; i++) {
    System.out.println("result[" + i + "]: " + result[i]);
}

Thanks.

Comments
Post Details
Added on Mar 28 2024
2 comments
188 views