|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | + * contributors may be used to endorse or promote products derived |
| 14 | + * from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | +package com.nvidia.grcuda.test; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertFalse; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | +import org.graalvm.polyglot.Context; |
| 34 | +import org.graalvm.polyglot.Value; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class DeviceTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testDeviceCount() { |
| 41 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 42 | + Value deviceCount = ctx.eval("grcuda", "cudaGetDeviceCount()"); |
| 43 | + assertTrue(deviceCount.isNumber()); |
| 44 | + assertTrue(deviceCount.asInt() > 0); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetDevicesLengthsMatchesDeviceCount() { |
| 50 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 51 | + Value deviceCount = ctx.eval("grcuda", "cudaGetDeviceCount()"); |
| 52 | + assertTrue(deviceCount.isNumber()); |
| 53 | + assertTrue(deviceCount.asInt() > 0); |
| 54 | + Value devices = ctx.eval("grcuda", "getdevices()"); |
| 55 | + assertEquals(deviceCount.asInt(), devices.getArraySize()); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testGetDevicesMatchesAllGetDevice() { |
| 61 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 62 | + Value devices = ctx.eval("grcuda", "getdevices()"); |
| 63 | + Value getDevice = ctx.eval("grcuda", "getdevice"); |
| 64 | + for (int i = 0; i < devices.getArraySize(); ++i) { |
| 65 | + Value deviceFromArray = devices.getArrayElement(i); |
| 66 | + Value deviceFromFunction = getDevice.execute(i); |
| 67 | + assertEquals(i, deviceFromArray.getMember("id").asInt()); |
| 68 | + assertEquals(i, deviceFromFunction.getMember("id").asInt()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testCanReadSomeDeviceProperties() { |
| 75 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 76 | + Value devices = ctx.eval("grcuda", "getdevices()"); |
| 77 | + for (int i = 0; i < devices.getArraySize(); ++i) { |
| 78 | + Value device = devices.getArrayElement(i); |
| 79 | + Value prop = device.getMember("properties"); |
| 80 | + // Sanity tests on some of the properties |
| 81 | + // device name is a non-zero string |
| 82 | + assertTrue(prop.getMember("deviceName").asString().length() > 0); |
| 83 | + |
| 84 | + // compute capability is at least compute Kepler (3.0) |
| 85 | + assertTrue(prop.getMember("computeCapabilityMajor").asInt() >= 3); |
| 86 | + |
| 87 | + // there is at least one multiprocessors |
| 88 | + assertTrue(prop.getMember("multiProcessorCount").asInt() > 0); |
| 89 | + |
| 90 | + // there is some device memory |
| 91 | + assertTrue(prop.getMember("totalDeviceMemory").asLong() > 0L); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testCanSelectDevice() { |
| 98 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 99 | + Value devices = ctx.eval("grcuda", "getdevices()"); |
| 100 | + if (devices.getArraySize() > 1) { |
| 101 | + Value firstDevice = devices.getArrayElement(0); |
| 102 | + Value secondDevice = devices.getArrayElement(1); |
| 103 | + secondDevice.invokeMember("setCurrent"); |
| 104 | + assertFalse(firstDevice.invokeMember("isCurrent").asBoolean()); |
| 105 | + assertTrue(secondDevice.invokeMember("isCurrent").asBoolean()); |
| 106 | + |
| 107 | + firstDevice.invokeMember("setCurrent"); |
| 108 | + assertTrue(firstDevice.invokeMember("isCurrent").asBoolean()); |
| 109 | + assertFalse(secondDevice.invokeMember("isCurrent").asBoolean()); |
| 110 | + } else { |
| 111 | + // only one device available |
| 112 | + Value device = devices.getArrayElement(0); |
| 113 | + device.invokeMember("setCurrent"); |
| 114 | + assertTrue(device.invokeMember("isCurrent").asBoolean()); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testDeviceMemoryAllocationReducesReportedFreeMemory() { |
| 121 | + try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { |
| 122 | + Value device = ctx.eval("grcuda", "getdevice(0)"); |
| 123 | + Value props = device.getMember("properties"); |
| 124 | + device.invokeMember("setCurrent"); |
| 125 | + long totalMemoryBefore = props.getMember("totalDeviceMemory").asLong(); |
| 126 | + long freeMemoryBefore = props.getMember("freeDeviceMemory").asLong(); |
| 127 | + assertTrue(freeMemoryBefore <= totalMemoryBefore); |
| 128 | + |
| 129 | + // allocate memory on device (unmanaged) |
| 130 | + long arraySizeBytes = freeMemoryBefore / 3; |
| 131 | + Value cudaMalloc = ctx.eval("grcuda", "cudaMalloc"); |
| 132 | + Value cudaFree = ctx.eval("grcuda", "cudaFree"); |
| 133 | + Value gpuPointer = null; |
| 134 | + try { |
| 135 | + gpuPointer = cudaMalloc.execute(arraySizeBytes); |
| 136 | + // After allocation total memory must be the same as before but |
| 137 | + // the free memory must be lower by at least the amount of allocated bytes. |
| 138 | + long totalMemoryAfter = props.getMember("totalDeviceMemory").asLong(); |
| 139 | + long freeMemoryAfter = props.getMember("freeDeviceMemory").asLong(); |
| 140 | + assertEquals(totalMemoryBefore, totalMemoryAfter); |
| 141 | + assertTrue(freeMemoryAfter <= (freeMemoryBefore - arraySizeBytes)); |
| 142 | + } finally { |
| 143 | + if (gpuPointer != null) { |
| 144 | + cudaFree.execute(gpuPointer); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments