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

The following examples show how to use org.graalvm.polyglot.Value#getMember() . 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: HashemInteropObjectTest.java    From mr-hashemi with Universal Permissive License v1.0 6 votes vote down vote up
@Test
public void testObject() {
    final Source src = Source.newBuilder("hashemi", "bebin azinja() {o = jadid(); o.a = 10; o.b = \"B\"; bede o;}", "testObject.hashem").buildLiteral();
    final Value obj = context.eval(src);
    Assert.assertTrue(obj.hasMembers());

    Value a = obj.getMember("a");
    Assert.assertNotNull(a);
    Assert.assertTrue(a.isNumber());
    Assert.assertEquals(10, a.asInt());

    Value b = obj.getMember("b");
    Assert.assertNotNull(b);
    Assert.assertTrue(b.isString());
    Assert.assertEquals("B", b.asString());

    obj.putMember("a", b);
    a = obj.getMember("a");
    Assert.assertTrue(a.isString());
    Assert.assertEquals("B", a.asString());

    obj.removeMember("a");
    Assert.assertFalse(obj.hasMember("a"));

    Assert.assertEquals("[b]", obj.getMemberKeys().toString());
}
 
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 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 3
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 4
Source File: DeviceTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testDeviceMemoryAllocationReducesReportedFreeMemory() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        Value device = ctx.eval("grcuda", "getdevice(0)");
        Value props = device.getMember("properties");
        device.invokeMember("setCurrent");
        long totalMemoryBefore = props.getMember("totalDeviceMemory").asLong();
        long freeMemoryBefore = props.getMember("freeDeviceMemory").asLong();
        assertTrue(freeMemoryBefore <= totalMemoryBefore);

        // allocate memory on device (unmanaged)
        long arraySizeBytes = freeMemoryBefore / 3;
        Value cudaMalloc = ctx.eval("grcuda", "cudaMalloc");
        Value cudaFree = ctx.eval("grcuda", "cudaFree");
        Value gpuPointer = null;
        try {
            gpuPointer = cudaMalloc.execute(arraySizeBytes);
            // After allocation total memory must be the same as before but
            // the free memory must be lower by at least the amount of allocated bytes.
            long totalMemoryAfter = props.getMember("totalDeviceMemory").asLong();
            long freeMemoryAfter = props.getMember("freeDeviceMemory").asLong();
            assertEquals(totalMemoryBefore, totalMemoryAfter);
            assertTrue(freeMemoryAfter <= (freeMemoryBefore - arraySizeBytes));
        } finally {
            if (gpuPointer != null) {
                cudaFree.execute(gpuPointer);
            }
        }
    }
}
 
Example 5
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 6
Source File: GraalEngine.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException {
    if (!(thiz instanceof Value)) {
        throw new IllegalArgumentException();
    }
    final Value thisValue = (Value) thiz;
    Value fn = thisValue.getMember(name);
    if (!fn.canExecute()) {
        throw new NoSuchMethodException(name);
    }
    Value result = fn.execute(args);
    return unbox(result);
}
 
Example 7
Source File: GraalScriptRunner.java    From citeproc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Recursively convert a JavaScript object to a map
 * @param obj the object to convert
 * @return the map
 */
private static Map<String, Object> convertObject(Value obj) {
    Map<String, Object> r = new LinkedHashMap<>();
    for (String k : obj.getMemberKeys()) {
        Value v = obj.getMember(k);
        Object o = convert(v);
        r.put(k, o);
    }
    return r;
}