javaeclipsesecondary-indexesaparapi

How does aparapi getGlobalId works?


I am beginner with aparapi. I have a problem with getGlobalId. My code is very simple. I only want to add two arrays but the result is wrong. I debugged the program and i saw that getGlobalId is not taking the corrected values.

    My code is:
int size = 3;

    final float[] A = new float[size];

    final float[] B = new float[size];

    for (int i=0; i<size; i++) {

        A[i] = (float) Math.random()*100;

        B[i] = (float) Math.random()*100;
    }

    final float[] result = new float[size];

    Kernel kernel = new Kernel() {
        @Override
        public void run() {
            int i = getGlobalId();
            result[i] = A[i] + B[i];
        }
    };      

    Range range = Range.create(result.length);
    kernel.execute(range);

The result is:

28.15 + 85.24 = 0.00

74.07 + 80.04 = 0.00

15.51 + 98.64 = 0.00

And the error in console is:

com.aparapi.internal.opencl.OpenCLLoader SEVERE: Check your environment. Failed to load codegen native library or possibly failed to locate opencl native library (opencl.dll/opencl.so). Ensure that OpenCL is in your PATH (windows) or in LD_LIBRARY_PATH (linux).

Any solution about my problem?


Solution

  • It sounds like you are asking two questions.

    How getGlobalId works:

    getGlobalId() returns the index of the thread that the code is currently running in on the GPU. If you have an array of size 3, like in your example, getGlobalId() would return 0, 1, or 2 depending on the current GPU thread. Typically you use this number to access an element in an array, just like you are doing in your example. It seems like you are using getGlobalId() correctly.

    Cause of the error:

    It looks like you may not have OpenCL installed properly, or not installed at all. The OpenCL drivers do not come with Aparapi. They must be downloaded from your individual graphics card vendor's website. And that will only work if you have a graphics card with OpenCL capabilities.