package inn.ocsf.bee.freigeld.cl;

import com.aparapi.Kernel;
import com.aparapi.Range;
import com.aparapi.device.Device;

import java.util.logging.Logger;

public class Sample0 implements Runnable {

    private final Logger log = Logger.getLogger(getClass().getName());

    @Override
    public void run() {
        final int R = 1024;
        final int[] buffer_$constant$ = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};

        final float[] resultF = new float[R];
        final boolean[] resultB = new boolean[R];
        final byte[] resultH = new byte[R];
        final short[] resultS = new short[R];
        final int[] resultI = new int[R];
        final long[] resultL = new long[R];
        final Pt[] resultO = new Pt[R];

        for (int i = 0; i < R; i++)
            resultO[i] = new Pt();

        Kernel kernel = new Kernel() {

            protected @PrivateMemorySpace(4)
            final short[] myBuffer = new short[4];
            @Constant
            final int[] myBufferConstant = new int[]{0, 1, 2, 3};
            @Local
            final int[] buffer = new int[R];

            @Override
            public void run() {
                int i = getGlobalId();
                for (int j = 0; j < myBuffer.length; j++) {
                    myBuffer[j] = (short) j;
                }
                buffer[i] = -i;
                localBarrier();
                resultB[i] = i % 2 == 0;
                resultL[i] = buffer_$constant$[i % 4];
                resultI[i] = myBufferConstant[i % 4];
                resultS[i] = myBuffer[i % 4];
                resultH[i] = (byte) buffer[i];
                resultF[i] = resultI[i];
                resultO[i].setX(i);
                resultO[i].setY(i);
            }
        };
        Device device = Device.best();
        Range range = Range.create(device, R);
        log.info("opencl run");
        kernel.execute(range, 1024);
        log.info("done opencl run");
    }

}
