Java Code Examples for org.graalvm.polyglot.Value#setArrayElement()

The following examples show how to use org.graalvm.polyglot.Value#setArrayElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void setElement(Value array, int index, Number value) {
    switch (dataTypeString) {
        case "char":
            array.setArrayElement(index, value.byteValue());
            break;
        case "short":
            array.setArrayElement(index, value.shortValue());
            break;
        case "int":
            array.setArrayElement(index, value.intValue());
            break;
        case "long":
            array.setArrayElement(index, value.longValue());
            break;
        case "float":
            array.setArrayElement(index, value.floatValue());
            break;
        case "double":
            array.setArrayElement(index, value.doubleValue());
            break;
        default:
            throw new RuntimeException("invalid type " + dataTypeString);
    }
}
 
Example 2
Source File: BindTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void callWithInoutArgument(String... bindArgs) {
    try (Context polyglot = Context.newBuilder().allowAllAccess(true).build()) {
        Value cu = polyglot.eval("grcuda", "CU");
        Value inoutDeviceArray = cu.getMember("DeviceArray").execute("int", numElements);
        for (int i = 0; i < numElements; i++) {
            inoutDeviceArray.setArrayElement(i, Integer.valueOf(i));
        }

        // get function from shared library
        Value bind = cu.getMember("bind");
        Value function = bindArgs.length > 1 ? bind.execute(dynamicLibraryFile, bindArgs[0], bindArgs[1])
                        : bind.execute(dynamicLibraryFile, bindArgs[0]);
        assertNotNull(function);

        // call function
        int blocks = 80;
        int threadsPerBlock = 256;
        function.execute(blocks, threadsPerBlock, inoutDeviceArray, numElements);

        // verify result
        for (int i = 0; i < numElements; i++) {
            assertEquals(i + 1, inoutDeviceArray.getArrayElement(i).asInt());
        }
    }
}
 
Example 3
Source File: DeviceArrayCopyFunctionTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDeviceArrayCopyFromDeviceArray() {
    final int numElements = 1000;
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        // create device array initialize its elements.
        Value sourceDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            sourceDeviceArray.setArrayElement(i, i + 1);
        }
        // create destination device array initialize its elements to zero.
        Value destinationDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            destinationDeviceArray.setArrayElement(i, 0);
        }
        destinationDeviceArray.invokeMember("copyFrom", sourceDeviceArray, numElements);
        // Verify content of device array
        for (int i = 0; i < numElements; ++i) {
            assertEquals(i + 1, destinationDeviceArray.getArrayElement(i).asInt());
        }
    }
}
 
Example 4
Source File: DeviceArrayCopyFunctionTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDeviceArrayCopyToDeviceArray() {
    final int numElements = 1000;
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        // create device array initialize its elements.
        Value sourceDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            sourceDeviceArray.setArrayElement(i, i + 1);
        }
        // create destination device array initialize its elements to zero.
        Value destinationDeviceArray = createDeviceArray.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            destinationDeviceArray.setArrayElement(i, 0);
        }
        sourceDeviceArray.invokeMember("copyTo", destinationDeviceArray, numElements);
        // Verify content of device array
        for (int i = 0; i < numElements; ++i) {
            assertEquals(i + 1, destinationDeviceArray.getArrayElement(i).asInt());
        }
    }
}
 
Example 5
Source File: BuildKernelTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testBuild1DKernelAndLaunch() {
    // Build inc_kernel, launch it, and check results.
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        final int numElements = 1000;
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        Value buildkernel = context.eval("grcuda", "buildkernel");
        Value incrKernel = buildkernel.execute(INCREMENT_KERNEL_SOURCE, INCREMENT_KERNEL_NIDL_SIGNATURE);
        assertNotNull(incrKernel);
        assertTrue(incrKernel.canExecute());
        assertEquals(0, incrKernel.getMember("launchCount").asInt());
        assertNotNull(incrKernel.getMember("ptx").asString());
        Value inDevArray = deviceArrayConstructor.execute("int", numElements);
        Value outDevArray = deviceArrayConstructor.execute("int", numElements);
        for (int i = 0; i < numElements; ++i) {
            inDevArray.setArrayElement(i, i);
            outDevArray.setArrayElement(i, 0);
        }
        // Execute kernel as <<<8, 128>>> that is 8 blocks with 128 threads each
        Value configuredIncKernel = incrKernel.execute(8, 128);
        assertTrue(configuredIncKernel.canExecute());
        configuredIncKernel.execute(outDevArray, inDevArray, numElements);
        // implicit sync

        // verify results
        for (int i = 0; i < numElements; ++i) {
            assertEquals(i, inDevArray.getArrayElement(i).asInt());
            assertEquals(i + 1, outDevArray.getArrayElement(i).asInt());
        }
        assertEquals(1, incrKernel.getMember("launchCount").asInt());
    }
}
 
Example 6
Source File: BindTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void callWithInAndOutArguments(String... bindArgs) {
    try (Context polyglot = Context.newBuilder().allowAllAccess(true).build()) {
        Value cu = polyglot.eval("grcuda", "CU");
        Value inDeviceArray = cu.getMember("DeviceArray").execute("int", numElements);
        Value outDeviceArray = cu.getMember("DeviceArray").execute("float", numElements);
        for (int i = 0; i < numElements; i++) {
            inDeviceArray.setArrayElement(i, Integer.valueOf(i));
            outDeviceArray.setArrayElement(i, Float.valueOf(0));
        }

        // get function from shared library
        Value bind = cu.getMember("bind");
        Value function = bindArgs.length > 1 ? bind.execute(dynamicLibraryFile, bindArgs[0], bindArgs[1])
                        : bind.execute(dynamicLibraryFile, bindArgs[0]);
        assertNotNull(function);

        // call function
        int blocks = 80;
        int threadsPerBlock = 256;
        function.execute(blocks, threadsPerBlock, outDeviceArray, inDeviceArray, numElements);

        // verify result
        for (int i = 0; i < numElements; i++) {
            assertEquals(i + 1.0f, outDeviceArray.getArrayElement(i).asFloat(), 1e-3f);
        }
    }
}
 
Example 7
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = PolyglotException.class)
public void testDeviceArrayAccessAfterFreeThrows() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 1000);
        deviceArray.invokeMember("free");
        deviceArray.setArrayElement(0, 42); // throws
    }
}
 
Example 8
Source File: BindKernelTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
void testWithSignature(String... bindArgs) {
    // Build inc_kernel symbol, launch it, and check results.
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        Value bindkernel = context.eval("grcuda", "bindkernel");
        Value incKernel = bindArgs.length > 1 ? bindkernel.execute(BindKernelTest.ptxFileName, bindArgs[0], bindArgs[1])
                        : bindkernel.execute(BindKernelTest.ptxFileName, bindArgs[0]);
        assertNotNull(incKernel);
        assertTrue(incKernel.canExecute());
        assertEquals(0, incKernel.getMember("launchCount").asInt());
        assertNotNull(incKernel.getMember("ptx").asString());
        Value inDevArray = deviceArrayConstructor.execute("int", NUM_ELEMENTS);
        Value outDevArray = deviceArrayConstructor.execute("int", NUM_ELEMENTS);
        for (int i = 0; i < NUM_ELEMENTS; ++i) {
            inDevArray.setArrayElement(i, i);
            outDevArray.setArrayElement(i, 0);
        }
        // <<<8, 128>>> 8 blocks a 128 threads
        Value configuredIncKernel = incKernel.execute(8, 128);
        assertTrue(configuredIncKernel.canExecute());
        configuredIncKernel.execute(outDevArray, inDevArray, NUM_ELEMENTS);
        // implicit synchronization

        // verify result
        for (int i = 0; i < NUM_ELEMENTS; ++i) {
            assertEquals(i, inDevArray.getArrayElement(i).asInt());
            assertEquals(i + 1, outDevArray.getArrayElement(i).asInt());
        }
        assertEquals(1, incKernel.getMember("launchCount").asInt());
    }
}
 
Example 9
Source File: DeviceArrayCopyFunctionTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDeviceArrayCopyToOffheapMemory() {
    final int numElements = 1000;
    final int numBytesPerInt = 4;
    final int numBytes = numElements * numBytesPerInt;
    try (OffheapMemory hostMemory = new OffheapMemory(numBytes)) {
        // create off-heap host memory of integers and initialize all elements to zero.
        LittleEndianNativeArrayView hostArray = hostMemory.getLittleEndianView();
        for (int i = 0; i < numElements; ++i) {
            hostArray.setInt(i, i);
        }
        try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
            // create DeviceArray and set its content [1, 2, 3, 4, ..., 1000]
            Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
            Value deviceArray = createDeviceArray.execute("int", numElements);
            for (int i = 0; i < numElements; ++i) {
                deviceArray.setArrayElement(i, i + 1);
            }
            // copy content of device array to off-heap host memory
            deviceArray.invokeMember("copyTo", hostMemory.getPointer(), numElements);

            // Verify content of device array
            for (int i = 0; i < numElements; ++i) {
                assertEquals(i + 1, hostArray.getInt(i));
            }
        }
    }
}
 
Example 10
Source File: BuildKernelTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void fillRandomMatrix(int numRows, int numCols, Value matrix) {
    Random rand = new Random(42);
    for (int i = 0; i < numRows * numCols; i++) {
        matrix.setArrayElement(i, (float) rand.nextGaussian());
    }
}