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

The following examples show how to use org.graalvm.polyglot.Value#invokeMember() . 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 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 2
Source File: DeviceArrayCopyFunctionTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testDeviceArrayCopyFromOffheapMemory() {
    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: [1, 2, 3, 4, ..., 1000]
        LittleEndianNativeArrayView hostArray = hostMemory.getLittleEndianView();
        for (int i = 0; i < numElements; ++i) {
            hostArray.setInt(i, i + 1);
        }
        try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
            // create DeviceArray and copy content from off-heap host memory into it
            Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
            Value deviceArray = createDeviceArray.execute("int", numElements);
            deviceArray.invokeMember("copyFrom", hostMemory.getPointer(), numElements);

            // Verify content of device array
            for (int i = 0; i < numElements; ++i) {
                assertEquals(i + 1, deviceArray.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: 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 6
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCanInvokeFreeDeviceArray() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 1000);
        assertTrue(deviceArray.canInvokeMember("free"));
        deviceArray.invokeMember("free");
        // check that freed flag set
        assertTrue(deviceArray.hasMember("isMemoryFreed"));
        assertTrue(deviceArray.getMember("isMemoryFreed").asBoolean());
    }
}
 
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: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(expected = PolyglotException.class)
public void testDeviceArrayDoubleFreeThrows() {
    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.invokeMember("free"); // throws
    }
}
 
Example 9
Source File: DeviceArrayFreeTest.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testCanInvokeFreeMultiDimDeviceArray() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 100, 100);
        assertTrue(deviceArray.canInvokeMember("free"));
        deviceArray.invokeMember("free");
        // check that freed flag set
        assertTrue(deviceArray.hasMember("isMemoryFreed"));
        assertTrue(deviceArray.getMember("isMemoryFreed").asBoolean());
    }
}
 
Example 10
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 testMultiDimDeviceArrayAccessAfterFreeThrows() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 100, 100);
        deviceArray.invokeMember("free");
        deviceArray.getArrayElement(0).setArrayElement(0, 42); // throws
    }
}
 
Example 11
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 testMultiDimDeviceArrayDoubleFreeThrows() {
    try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) {
        // create DeviceArray
        Value createDeviceArray = ctx.eval("grcuda", "DeviceArray");
        Value deviceArray = createDeviceArray.execute("int", 100, 100);
        deviceArray.invokeMember("free");
        deviceArray.invokeMember("free"); // throws
    }
}
 
Example 12
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));
            }
        }
    }
}