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

The following examples show how to use org.graalvm.polyglot.Value#getArrayElement() . 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: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCanReadSomeDeviceProperties() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        for (int i = 0; i < devices.getArraySize(); ++i) {
            Value device = devices.getArrayElement(i);
            Value prop = device.getMember("properties");
            // Sanity tests on some of the properties
            // device name is a non-zero string
            assertTrue(prop.getMember("deviceName").asString().length() > 0);

            // compute capability is at least compute Kepler (3.0)
            assertTrue(prop.getMember("computeCapabilityMajor").asInt() >= 3);

            // there is at least one multiprocessors
            assertTrue(prop.getMember("multiProcessorCount").asInt() > 0);

            // there is some device memory
            assertTrue(prop.getMember("totalDeviceMemory").asLong() > 0L);
        }
    }
}
 
Example 2
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testCanSelectDevice() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        if (devices.getArraySize() > 1) {
            Value firstDevice = devices.getArrayElement(0);
            Value secondDevice = devices.getArrayElement(1);
            secondDevice.invokeMember("setCurrent");
            assertFalse(firstDevice.invokeMember("isCurrent").asBoolean());
            assertTrue(secondDevice.invokeMember("isCurrent").asBoolean());

            firstDevice.invokeMember("setCurrent");
            assertTrue(firstDevice.invokeMember("isCurrent").asBoolean());
            assertFalse(secondDevice.invokeMember("isCurrent").asBoolean());
        } else {
            // only one device available
            Value device = devices.getArrayElement(0);
            device.invokeMember("setCurrent");
            assertTrue(device.invokeMember("isCurrent").asBoolean());
        }
    }
}
 
Example 3
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testGetDevicesMatchesAllGetDevice() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value devices = ctx.eval("grcuda", "getdevices()");
        Value getDevice = ctx.eval("grcuda", "getdevice");
        for (int i = 0; i < devices.getArraySize(); ++i) {
            Value deviceFromArray = devices.getArrayElement(i);
            Value deviceFromFunction = getDevice.execute(i);
            assertEquals(i, deviceFromArray.getMember("id").asInt());
            assertEquals(i, deviceFromFunction.getMember("id").asInt());
        }
    }
}
 
Example 4
Source File: MultiDimArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void test2DimArrayOutOfBoundsOnReadAccess() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArrayConstructor = context.eval("grcuda", "DeviceArray");
        final int numDim1 = 19;
        final int numDim2 = 53;
        Value matrix = deviceArrayConstructor.execute("int", numDim1, numDim2);
        assertEquals(numDim1, matrix.getArraySize());
        assertEquals(numDim2, matrix.getArrayElement(0).getArraySize());
        // out-of-bounds read access
        matrix.getArrayElement(numDim1);
    }
}
 
Example 5
Source File: DeviceArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDeviceArrayGetValue() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArray = context.eval("grcuda", dataTypeString + "[" + arrayLength + "]");
        assertTrue(deviceArray.hasArrayElements());
        assertEquals(arrayLength, deviceArray.getArraySize());
        Value firstElement = deviceArray.getArrayElement(0);
        checkEquality(0, firstElement);
    }
}
 
Example 6
Source File: DeviceArrayTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDeviceArraySetAndGetsSetValue() {
    try (Context context = Context.newBuilder().allowAllAccess(true).build()) {
        Value deviceArray = context.eval("grcuda", dataTypeString + "[" + arrayLength + "]");
        assertTrue(deviceArray.hasArrayElements());
        assertEquals(arrayLength, deviceArray.getArraySize());
        final Number value = (Number) testValue;
        setElement(deviceArray, 0, value);
        Value firstElement = deviceArray.getArrayElement(0);
        checkEquality(value, firstElement);
    }
}
 
Example 7
Source File: GraalScriptRunner.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively convert a JavaScript array to a list
 * @param arr the array to convert
 * @return the list
 */
private static List<Object> convertArray(Value arr) {
    List<Object> l = new ArrayList<>();
    for (int i = 0; i < arr.getArraySize(); ++i) {
        Value v = arr.getArrayElement(i);
        Object o = convert(v);
        l.add(o);
    }
    return l;
}