├── .gitignore ├── LICENSE ├── README.md ├── docs ├── bindings.md ├── grcuda.md └── typemapping.md ├── examples ├── mandelbrot │ ├── README.md │ ├── app.js │ ├── license.txt │ └── package.json └── tensorrt │ ├── README.md │ ├── cpp │ ├── CMakeLists.txt │ ├── cmake │ │ └── FindTensorRT.cmake │ └── load_and_sample.cc │ ├── data │ └── download_mnist_test_digits.py │ ├── models │ └── .gitkeep │ ├── python │ ├── build_engine.py │ ├── load_and_sample.py │ └── model.py │ └── tensorrt_example.js ├── mx.grcuda ├── mx_grcuda.py └── suite.py ├── projects ├── com.nvidia.grcuda.parser.antlr │ ├── Makefile │ └── postprocessor.py ├── com.nvidia.grcuda.test │ └── src │ │ └── com │ │ └── nvidia │ │ └── grcuda │ │ └── test │ │ ├── BindKernelTest.java │ │ ├── BindTest.java │ │ ├── BuildKernelTest.java │ │ ├── CUBLASTest.java │ │ ├── DeviceArrayCopyFunctionTest.java │ │ ├── DeviceArrayFreeTest.java │ │ ├── DeviceArrayTest.java │ │ ├── DeviceTest.java │ │ ├── ManglingTest.java │ │ ├── MultiDimArrayTest.java │ │ ├── ParserTest.java │ │ └── functions │ │ └── map │ │ └── MapFunctionTest.java └── com.nvidia.grcuda │ ├── .checkstyle_checks.xml │ └── src │ └── com │ └── nvidia │ └── grcuda │ ├── Binding.java │ ├── DeviceArray.java │ ├── FunctionBinding.java │ ├── GPUPointer.java │ ├── GrCUDAContext.java │ ├── GrCUDAException.java │ ├── GrCUDAInternalException.java │ ├── GrCUDALanguage.java │ ├── GrCUDAOptions.java │ ├── KernelBinding.java │ ├── MultiDimDeviceArray.java │ ├── MultiDimDeviceArrayView.java │ ├── Namespace.java │ ├── NoneValue.java │ ├── Parameter.java │ ├── Type.java │ ├── TypeException.java │ ├── cublas │ └── CUBLASRegistry.java │ ├── cuml │ └── CUMLRegistry.java │ ├── functions │ ├── BindAllFunction.java │ ├── BindFunction.java │ ├── BindKernelFunction.java │ ├── BuildKernelFunction.java │ ├── CUDAFunction.java │ ├── DeviceArrayCopyFunction.java │ ├── DeviceArrayFunction.java │ ├── ExternalFunction.java │ ├── ExternalFunctionFactory.java │ ├── Function.java │ ├── GetDeviceFunction.java │ ├── GetDevicesFunction.java │ ├── HostFunction.java │ ├── MapDeviceArrayFunction.java │ ├── TypedDeviceArrayFunction.java │ ├── TypedMapDeviceArrayFunction.java │ └── map │ │ ├── ArgumentArray.java │ │ ├── ArgumentSet.java │ │ ├── MapArgObject.java │ │ ├── MapFunction.java │ │ ├── MappedFunction.java │ │ ├── ShredFunction.java │ │ └── ShreddedObject.java │ ├── gpu │ ├── CUDARuntime.java │ ├── ConfiguredKernel.java │ ├── Device.java │ ├── DeviceList.java │ ├── DriverAPIErrorMessages.java │ ├── GPUDeviceProperties.java │ ├── Kernel.java │ ├── KernelConfig.java │ ├── LazyKernel.java │ ├── LittleEndianNativeArrayView.java │ ├── NVRTCException.java │ ├── NVRuntimeCompiler.java │ ├── OffheapMemory.java │ └── UnsafeHelper.java │ ├── nodes │ ├── ArithmeticNode.java │ ├── ArrayNode.java │ ├── BinaryNode.java │ ├── CallNode.java │ ├── ExpressionNode.java │ ├── GrCUDARootNode.java │ ├── IdentifierNode.java │ ├── IntegerLiteral.java │ ├── RootNamespaceNode.java │ └── StringLiteral.java │ ├── parser │ ├── GrCUDAParserException.java │ ├── NIDLParserException.java │ ├── NodeFactory.java │ ├── ParserAntlr.java │ └── antlr │ │ ├── GrCUDA.g4 │ │ └── NIDL.g4 │ └── tensorrt │ └── TensorRTRegistry.java └── tensorrt ├── CMakeLists.txt ├── README.md ├── app └── libtrt_load_and_sample.c ├── cmake └── FindTensorRT.cmake ├── include └── trt.h └── src └── trt.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build.xml 2 | .classpath 3 | .factorypath 4 | .project 5 | *.settings 6 | nbproject 7 | *.jar 8 | *.zip 9 | *.sha1 10 | env 11 | *.pyc 12 | currentAnnotationProcessors 13 | mxbuild 14 | workingsets.xml 15 | .idea 16 | GRCUDA.dist 17 | GRCUDA_UNIT_TESTS.dist 18 | mx.grcuda/eclipse-launches 19 | .checkstyle 20 | *.bc 21 | *.iml 22 | /.metadata 23 | /dumps 24 | /hs_err*.log 25 | *.dist 26 | .cache 27 | *.o 28 | *.so 29 | /mx.imports 30 | .pydevproject 31 | .vscode 32 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDA.g4.stamp 33 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDA.interp 34 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDA.tokens 35 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDABaseListener.java 36 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDABaseVisitor.java 37 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDALexer.interp 38 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDALexer.java 39 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDALexer.tokens 40 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDAListener.java 41 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDAParser.java 42 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/GrCUDAVisitor.java 43 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDL.g4.stamp 44 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDL.interp 45 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDL.tokens 46 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDLLexer.interp 47 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDLLexer.java 48 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDLLexer.tokens 49 | /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/antlr/NIDLParser.java 50 | tensorrt/build 51 | examples/tensorrt/python/logs 52 | examples/tensorrt/cpp/build 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions 5 | are met: 6 | * Redistributions of source code must retain the above copyright 7 | notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright 9 | notice, this list of conditions and the following disclaimer in the 10 | documentation and/or other materials provided with the distribution. 11 | * Neither the name of NVIDIA CORPORATION nor the names of its 12 | contributors may be used to endorse or promote products derived 13 | from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | 28 | grCUDA depends on Truffle APIs licensed under the Universal Permissive 29 | License (UPL), Version 1.0 (https://opensource.org/licenses/UPL). 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # grCUDA: Polyglot GPU Access in GraalVM 2 | 3 | This Truffle language exposes GPUs to the polyglot [GraalVM](http://www.graalvm.org). The goal is to 4 | 5 | 1) make data exchange between the host language and the GPU efficient without burdening the programmer. 6 | 7 | 2) allow programmers to invoke _existing_ GPU kernels from their host language. 8 | 9 | Supported and tested GraalVM languages: 10 | 11 | - Python 12 | - JavaScript/NodeJS 13 | - Ruby 14 | - R 15 | - Java 16 | - C and Rust through the Graal Sulong Component 17 | 18 | A description of grCUDA and its the features can be found in the [grCUDA documentation](docs/grcuda.md). 19 | 20 | The [bindings documentation](docs/bindings.md) contains a tutorial that shows 21 | how to bind precompiled kernels to callables, compile and launch kernels. 22 | 23 | **Additional Information:** 24 | 25 | - [grCUDA: A Polyglot Language Binding for CUDA in GraalVM](https://devblogs.nvidia.com/grcuda-a-polyglot-language-binding-for-cuda-in-graalvm/). NVIDIA Developer Blog, 26 | November 2019. 27 | - [grCUDA: A Polyglot Language Binding](https://youtu.be/_lI6ubnG9FY). Presentation at Oracle CodeOne 2019, September 2019. 28 | - [Simplifying GPU Access](https://developer.nvidia.com/gtc/2020/video/s21269-vid). Presentation at NVIDIA GTC 2020, March 2020. 29 | 30 | ## Using grCUDA in the GraalVM 31 | 32 | grCUDA can be used in the binaries of the GraalVM languages (`lli`, `graalpython`, 33 | `js`, `R`, and `ruby)`. The JAR file containing grCUDA must be appended to the classpath 34 | or copied into `jre/languages/grcuda` of the Graal installation. Note that `--jvm` 35 | and `--polyglot` must be specified in both cases as well. 36 | 37 | The following example shows how to create a GPU kernel and two device arrays 38 | in JavaScript (NodeJS) and invoke the kernel: 39 | 40 | ```JavaScript 41 | // build kernel from CUDA C/C++ source code 42 | const kernelSource = ` 43 | __global__ void increment(int *arr, int n) { 44 | int idx = blockIdx.x * blockDim.x + threadIdx.x; 45 | if (idx < n) { 46 | arr[idx] += 1; 47 | } 48 | }` 49 | const cu = Polyglot.eval('grcuda', 'CU') // get grCUDA namespace object 50 | const incKernel = cu.buildkernel( 51 | kernelSource, // CUDA kernel source code string 52 | 'increment', // kernel name 53 | 'pointer, sint32') // kernel signature 54 | 55 | // allocate device array 56 | const numElements = 100 57 | const deviceArray = cu.DeviceArray('int', numElements) 58 | for (let i = 0; i < numElements; i++) { 59 | deviceArray[i] = i // ... and initialize on the host 60 | } 61 | // launch kernel in grid of 1 block with 128 threads 62 | incKernel(1, 128)(deviceArray, numElements) 63 | 64 | // print elements from updated array 65 | for (const element of deviceArray) { 66 | console.log(element) 67 | } 68 | ``` 69 | 70 | ```console 71 | $GRAALVM_DIR/bin/node --polyglot --jvm example.js 72 | 1 73 | 2 74 | ... 75 | 100 76 | ``` 77 | 78 | ### Calling existing compiled GPU Kernels 79 | 80 | The next example shows how to launch an __existing compiled__ GPU kernel from Python. 81 | The CUDA kernel 82 | 83 | ```C 84 | __global__ void increment(int *arr, int n) { 85 | auto idx = blockIdx.x * blockDim.x + threadIdx.x; 86 | if (idx < n) { 87 | arr[idx] += 1; 88 | } 89 | } 90 | ``` 91 | 92 | is compiled using `nvcc --cubin` into a cubin file. The kernel function can be loaded from the cubin and bound to a callable object in the host language, here Python. 93 | 94 | ```Python 95 | import polyglot 96 | 97 | num_elements = 100 98 | cu = polyglot.eval(language='grcuda', string='CU') 99 | device_array = cu.DeviceArray('int', num_elements) 100 | for i in range(num_elements): 101 | device_array[i] = i 102 | 103 | # bind to kernel from binary 104 | inc_kernel = cu.bindkernel('kernel.cubin', 105 | 'cxx increment(arr: inout pointer sint32, n: sint32)') 106 | 107 | # launch kernel as 1 block with 128 threads 108 | inc_kernel(1, 128)(device_array, num_elements) 109 | 110 | for i in range(num_elements): 111 | print(device_array[i]) 112 | ``` 113 | 114 | ```console 115 | nvcc --cubin --generate-code arch=compute_75,code=sm_75 kernel.cu 116 | $GRAALVM_DIR/bin/graalpython --polyglot --jvm example.py 117 | 1 118 | 2 119 | ... 120 | 100 121 | ``` 122 | 123 | For more details on how to invoke existing GPU kernels, see the 124 | Documentation on [polyglot kernel launches](docs/launchkernel.md). 125 | 126 | ## Installation 127 | 128 | grCUDA can be downloaded as a binary JAR from [grcuda/releases](https://github.com/NVIDIA/grcuda/releases) and manually copied into a GraalVM installation. 129 | 130 | 1. Download GraalVM CE 20.0.0 for Linux `graalvm-ce-java8-linux-amd64-20.0.0.tar.gz` 131 | from [GitHub](https://github.com/oracle/graal/releases) and untar it in your 132 | installation directory. 133 | 134 | ```console 135 | cd 136 | tar xfz graalvm-ce-java8-linux-amd64-20.0.0.tar.gz 137 | export GRAALVM_DIR=`pwd`/graalvm-ce-java8-20.0.0 138 | ``` 139 | 140 | 2. Download the grCUDA JAR from [grcuda/releases](https://github.com/NVIDIA/grcuda/releases) 141 | 142 | ```console 143 | cd $GRAALVM_DIR/jre/languages 144 | mkdir grcuda 145 | cp /grcuda-0.1.0.jar grcuda 146 | ``` 147 | 148 | 3. Test grCUDA in Node.JS from GraalVM. 149 | 150 | ```console 151 | cd $GRAALVM_DIR/bin 152 | ./node --jvm --polyglot 153 | > arr = Polyglot.eval('grcuda', 'int[5]') 154 | [Array: null prototype] [ 0, 0, 0, 0, 0 ] 155 | ``` 156 | 157 | 4. Download other GraalVM languages. 158 | 159 | ```console 160 | cd $GRAAL_VM/bin 161 | ./gu available 162 | ./gu install python 163 | ./gu install R 164 | ./gu install ruby 165 | ``` 166 | 167 | ## Instructions to build grCUDA from Sources 168 | 169 | grCUDA requires the [mx build tool](https://github.com/graalvm/mx). Clone the mx 170 | repository and add the directory into `$PATH`, such that the `mx` can be invoked from 171 | the command line. 172 | 173 | Build grCUDA and the unit tests: 174 | 175 | ```console 176 | cd 177 | mx build 178 | ``` 179 | 180 | Note that this will also checkout the graal repository. 181 | 182 | To run unit tests: 183 | 184 | ```bash 185 | mx unittest com.nvidia 186 | ``` 187 | -------------------------------------------------------------------------------- /docs/typemapping.md: -------------------------------------------------------------------------------- 1 | # Type Mapping for C++ 2 | 3 | grCUDA needs to interact with tree different type systems: 4 | 5 | - The Java types of the Truffle interop protocol (`boolean`, `byte`, `short`, `int`, `long`, `float`, `double`, `String`) 6 | as described in [InteropLibrary](https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/interop/InteropLibrary.html). 7 | These types are used in the interop with the different GraalVM langauges. 8 | - C++ language types (see [C++ Fundamental Types](https://en.cppreference.com/w/cpp/language/types) 9 | These types are used to used to bind to GPU kernels as well as to C++ host functions. 10 | - C interface types from TruffleNFI (`void`, `sint8`, `uint8`, `sint16`, `uint16`, `sint32`, `uint32`, `sint64`, `uint64`, 11 | `float`, `double`, `pointer`, `object`, `string`). 12 | [NativeSimpleType.java](https://github.com/oracle/graal/blob/master/truffle/src/com.oracle.truffle.nfi.spi/src/com/oracle/truffle/nfi/spi/types/NativeSimpleType.java) 13 | defines these types. grCUDA uses TruffleNFI to invoke native host functions. While TruffleNFI is designed for C APIs, the C++ types are necessary create the mangled symbols names to invoke with TruffleNFI. 14 | 15 | Types for clang/gcc under Linux (LP64) 16 | 17 | C++ Type |Bytes| Sym | NIDL Type | NFI | Java Type 18 | -------------------|-----|-----|--------------------|----------|---------- 19 | bool | 1 | b | bool | uint8 | boolean 20 | char | 1 | c | char | sint8 | byte 21 | unsigned char | 1 | h | uint8 | uint8 | short 22 | signed char | 1 | a | sint8 | sint8 | byte 23 | wchar_t | 4 | w | wchar | sint32 | int 24 | char8_t | 1 | Du | char8 | uint8 | short 25 | char16_t | 2 | Ds | char16 | uint16 | int 26 | char32_t | 4 | Di | char32 | uint32 | long 27 | (signed) short | 2 | s | sint16 | sint16 | short 28 | unsigned short | 2 | t | uint16 | uint16 | long 29 | (signed) int | 4 | i | sint32 | sint32 | int 30 | unsigned int | 4 | j | uint32 | uint32 | long 31 | (signed) long | 8 | l | sint64 | sint64 | long 32 | unsigned long | 8 | m | uint64 | uint64 | long+ 33 | (singed) long long | 8 | x | sll64 | sint64 | long 34 | unsigned long long | 8 | y | ull64 | uint64 | long+ 35 | float | 4 | f | float | float | float 36 | double | 8 | d | double | double | double 37 | long double | 16 | e | * | * | * 38 | void | 0 | v | void | void | void 39 | U * | 8 | PU | inout pointer V | pointer | n/a 40 | U * | 8 | PU | out pointer V | pointer | n/a 41 | const U * | 8 | PKU | in pointer V | pointer | n/a 42 | void * | 8 | Pv | inout pointer void | pointer | n/a 43 | void * | 8 | Pv | out pointer void | pointer | n/a 44 | const void * | 8 | PKv | in pointer void | pointer | n/a 45 | const char * | 8 | PKc | string | string | String 46 | 47 | (*) not supported in grCUDA or NFI 48 | (+) does not support all values (NFI limitation) 49 | 50 | ## Synonymous NIDL types 51 | 52 | Synonymous types are that can substituted without coercion: 53 | 54 | - char, uint8 55 | - bool, char8, uint8 56 | - char16, uint16 57 | - sint32, wchar 58 | - char32, uint32t 59 | - sint64, sll64 60 | - uint64, ull64 61 | - inout pointer V, out pointer U, synonymous(U, V), inout pointer void, out pointer void 62 | - in pointer V, in pointer U, synonymous(U, V), in pointer void 63 | -------------------------------------------------------------------------------- /examples/mandelbrot/README.md: -------------------------------------------------------------------------------- 1 | # Mandelbrot Web Application with Express 2 | 3 | This example demonstrates how grCUDA can be used in a Node.js 4 | web application with the Express framework. 5 | 6 | The code is described in the 7 | [NVIDIA Developer Blog on grCUDA](https://devblogs.nvidia.com/grcuda-a-polyglot-language-binding-for-cuda-in-graalvm/). 8 | For more details, see the blog. 9 | 10 | ## How to run the Example 11 | 12 | ```console 13 | $ npm i 14 | added 272 packages from 152 contributors and audited 272 packages in 90.639s 15 | 16 | 26 packages are looking for funding run `npm fund` for details 17 | 18 | found 0 vulnerabilities 19 | 20 | $ node --polyglot --jvm \ 21 | --vm.Dtruffle.class.path.append=../../mxbuild/dists/jdk1.8/grcuda.jar \ 22 | app.js 23 | Mandelbrot app listening on port 3000! 24 | ``` 25 | -------------------------------------------------------------------------------- /examples/mandelbrot/app.js: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 1993-2019, NVIDIA CORPORATION. All rights reserved. 2 | * 3 | * Redistribution and use in source and binary forms, with or without 4 | * modification, are permitted provided that the following conditions 5 | * are met: 6 | * * Redistributions of source code must retain the above copyright 7 | * notice, this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright 9 | * notice, this list of conditions and the following disclaimer in the 10 | * documentation and/or other materials provided with the distribution. 11 | * * Neither the name of NVIDIA CORPORATION nor the names of its 12 | * contributors may be used to endorse or promote products derived 13 | * from this software without specific prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | const express = require('express') 28 | const app = express() 29 | 30 | const kernelSrc = ` 31 | __global__ void mandelbrot(int *img, int width_pixel, int height_pixel, 32 | float w, float h, float x0, float y0, int max_iter) { 33 | const int x = blockIdx.x * blockDim.x + threadIdx.x; 34 | const int y = blockIdx.y * blockDim.y + threadIdx.y; 35 | float c_re = (w / width_pixel) * (x - width_pixel / 2) + x0; 36 | float c_im = (h / height_pixel) * (height_pixel / 2 - y) + y0; 37 | float z_re = 0, z_im = 0; 38 | int iter = 0; 39 | while ((z_re * z_re + z_im * z_im <= 4) && (iter < max_iter)) { 40 | float z_re_new = z_re * z_re - z_im * z_im + c_re; 41 | z_im = 2 * z_re * z_im + c_im; 42 | z_re = z_re_new; 43 | iter += 1; 44 | } 45 | img[y * width_pixel + x] = (iter == max_iter); 46 | }` 47 | const port = 3000 48 | const widthPixels = 128 49 | const heightPixels = 64 50 | const cu = Polyglot.eval('grcuda', 'CU') 51 | const kernel = cu.buildkernel(kernelSrc, 'mandelbrot', 52 | 'pointer, sint32, sint32, float, float, float, float, sint32') 53 | const blockSize = [32, 8] // thread block with 32x8 threads 54 | const grid = [widthPixels / blockSize[0], heightPixels / blockSize[1]] 55 | const kernelWithGrid = kernel(grid, blockSize) 56 | 57 | app.get('/', (req, res) => { 58 | const img = cu.DeviceArray('int', heightPixels, widthPixels) 59 | kernelWithGrid(img, widthPixels, heightPixels, 3.0, 2.0, -0.5, 0.0, 255) 60 | var textImg = '' 61 | for (var y = 0; y < heightPixels; y++) { 62 | for (var x = 0; x < widthPixels; x++) { 63 | textImg += (img[y][x] === 1) ? '*' : ' ' 64 | } 65 | textImg += '\n' 66 | } 67 | res.setHeader('Content-Type', 'text/plain') 68 | res.send(textImg) 69 | }) 70 | 71 | app.listen(port, () => console.log(`Mandelbrot app listening on port ${port}!`)) 72 | -------------------------------------------------------------------------------- /examples/mandelbrot/license.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 1993-2019, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /examples/mandelbrot/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mandelbrot", 3 | "version": "0.1.0", 4 | "description": "Mandelbrot Set as ASCII Art", 5 | "main": "app.js", 6 | "repository": "https://github.com/NVIDIA/grcuda/examples/mandelbrot", 7 | "scripts": {}, 8 | "author": "", 9 | "license": "SEE LICENSE IN license.txt", 10 | "dependencies": { 11 | "express": "^4.17.1" 12 | }, 13 | "devDependencies": { 14 | "standard": "^14.3.3" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /examples/tensorrt/cpp/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | project(load_and_sample) 28 | cmake_minimum_required(VERSION 3.12) 29 | 30 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 31 | find_package(TensorRT REQUIRED) 32 | 33 | find_package(CUDA REQUIRED) 34 | 35 | message(STATUS "TensorRT Headers: ${TENSORRT_INCLUDE_DIR}") 36 | message(STATUS "TensorRT Library: ${TENSORRT_LIBRARY}") 37 | message(STATUS "Found CUDA ${CUDA_VERSION_STRING}") 38 | 39 | 40 | add_executable(load_and_sample load_and_sample.cc) 41 | target_compile_features(load_and_sample PUBLIC cxx_std_14) 42 | target_include_directories(load_and_sample PRIVATE ${TENSORRT_INCLUDE_DIR} ${CUDA_INCLUDE_DIRS}) 43 | target_link_libraries(load_and_sample PRIVATE TensorRT::nvinfer ${CUDA_LIBRARIES}) 44 | -------------------------------------------------------------------------------- /examples/tensorrt/cpp/cmake/FindTensorRT.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | find_path(TENSORRT_INCLUDE_DIR NvInfer.h HINTS ${TENSORRT_DIR} PATH_SUFFIXES include) 28 | find_library(TENSORRT_LIBRARY NAMES nvinfer HINTS ${TENSORRT_DIR} PATH_SUFFIXES lib) 29 | 30 | if (TENSORRT_LIBRARY AND TENSORRT_INCLUDE_DIR) 31 | if (NOT TARGET TensorRT::nvinfer) 32 | add_library(TensorRT::nvinfer UNKNOWN IMPORTED) 33 | set_target_properties(TensorRT::nvinfer PROPERTIES INTERFACE_INCLUDE_DIRECTORY "${TENSORRT_INCLUDE_DIR}") 34 | set_target_properties(TensorRT::nvinfer PROPERTIES 35 | IMPORTED_LINK_INTERFACE_LANGUAGES "C" 36 | IMPORTED_LOCATION "${TENSORRT_LIBRARY}") 37 | endif() 38 | endif() 39 | 40 | include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) 41 | find_package_handle_standard_args(TensorRT 42 | REQUIRED_VARS TENSORRT_LIBRARY TENSORRT_INCLUDE_DIR) 43 | 44 | mark_as_advanced(TENSORRT_INCLUDE_DIR TENSORRT_LIBRARY) 45 | -------------------------------------------------------------------------------- /examples/tensorrt/data/download_mnist_test_digits.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions 7 | # are met: 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in the 12 | # documentation and/or other materials provided with the distribution. 13 | # * Neither the name of NVIDIA CORPORATION nor the names of its 14 | # contributors may be used to endorse or promote products derived 15 | # from this software without specific prior written permission. 16 | # 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | from functools import reduce 30 | from PIL import Image 31 | import tensorflow as tf 32 | 33 | def main(): 34 | """Downloads MNIST dataset through Keras, extracts the first ten 35 | unique digits from the test set and writes them out as in PGM. """ 36 | 37 | (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data() 38 | 39 | def lowest_index(agg, element): 40 | index, digit = element 41 | if digit not in agg: 42 | agg[digit] = index 43 | else: 44 | agg[digit] = min(agg[digit], index) 45 | return agg 46 | 47 | for digit, first_index in sorted(reduce(lowest_index, enumerate(y_test), {}).items()): 48 | file_name = f'{digit}.pgm' 49 | print('writing ' + file_name + '...') 50 | image = Image.fromarray(255 - x_test[first_index]) 51 | image.save(file_name) 52 | 53 | 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /examples/tensorrt/models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NVIDIA/grcuda/b1f531a844d2906080ab4c04a33e99fe6e04089c/examples/tensorrt/models/.gitkeep -------------------------------------------------------------------------------- /examples/tensorrt/python/build_engine.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 1993-2019 NVIDIA Corporation. All rights reserved. 3 | # 4 | # NOTICE TO LICENSEE: 5 | # 6 | # This source code and/or documentation ("Licensed Deliverables") are 7 | # subject to NVIDIA intellectual property rights under U.S. and 8 | # international Copyright laws. 9 | # 10 | # These Licensed Deliverables contained herein is PROPRIETARY and 11 | # CONFIDENTIAL to NVIDIA and is being provided under the terms and 12 | # conditions of a form of NVIDIA software license agreement by and 13 | # between NVIDIA and Licensee ("License Agreement") or electronically 14 | # accepted by Licensee. Notwithstanding any terms or conditions to 15 | # the contrary in the License Agreement, reproduction or disclosure 16 | # of the Licensed Deliverables to any third party without the express 17 | # written consent of NVIDIA is prohibited. 18 | # 19 | # NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE 20 | # LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE 21 | # SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS 22 | # PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. 23 | # NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED 24 | # DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, 25 | # NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. 26 | # NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE 27 | # LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY 28 | # SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY 29 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 30 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 31 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 32 | # OF THESE LICENSED DELIVERABLES. 33 | # 34 | # U.S. Government End Users. These Licensed Deliverables are a 35 | # "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT 36 | # 1995), consisting of "commercial computer software" and "commercial 37 | # computer software documentation" as such terms are used in 48 38 | # C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government 39 | # only as a commercial end item. Consistent with 48 C.F.R.12.212 and 40 | # 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all 41 | # U.S. Government End Users acquire the Licensed Deliverables with 42 | # only those rights set forth herein. 43 | # 44 | # Any use of the Licensed Deliverables in individual and commercial 45 | # software must include, in the user documentation and internal 46 | # comments to the code, the above Disclaimer and U.S. Government End 47 | # Users Notice. 48 | # 49 | import tensorrt as trt 50 | 51 | import sys, os 52 | sys.path.insert(1, os.path.join(sys.path[0], "..")) 53 | 54 | # You can set the logger severity higher to suppress messages (or lower to display more messages). 55 | TRT_LOGGER = trt.Logger(trt.Logger.WARNING) 56 | 57 | class ModelData(object): 58 | INPUT_NAME ="conv2d_input" 59 | INPUT_SHAPE = (1, 28, 28) 60 | OUTPUT_NAME = "dense_2/Softmax" 61 | 62 | def build_engine(model_file): 63 | # For more information on TRT basics, refer to the introductory samples. 64 | with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser: 65 | builder.max_workspace_size = 4 << 30 # 4 GiB 66 | builder.max_batch_size = 1 67 | # Parse the Uff Network 68 | parser.register_input(ModelData.INPUT_NAME, ModelData.INPUT_SHAPE) 69 | parser.register_output(ModelData.OUTPUT_NAME) 70 | parser.parse(model_file, network) 71 | # Build and return an engine. 72 | return builder.build_cuda_engine(network) 73 | 74 | def main(): 75 | if len(sys.argv) != 3: 76 | print(f'{sys.argv[0]} model_file.uff engine_file.engine') 77 | return 78 | 79 | model_file = sys.argv[1] 80 | engine_file = sys.argv[2] 81 | print(f'model file: {model_file}') 82 | 83 | with build_engine(model_file) as engine, open(engine_file, 'wb') as f: 84 | f.write(engine.serialize()) 85 | print(f'written engine to {engine_file}') 86 | 87 | 88 | if __name__ == '__main__': 89 | main() 90 | -------------------------------------------------------------------------------- /examples/tensorrt/python/model.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 1993-2019 NVIDIA Corporation. All rights reserved. 3 | # 4 | # NOTICE TO LICENSEE: 5 | # 6 | # This source code and/or documentation ("Licensed Deliverables") are 7 | # subject to NVIDIA intellectual property rights under U.S. and 8 | # international Copyright laws. 9 | # 10 | # These Licensed Deliverables contained herein is PROPRIETARY and 11 | # CONFIDENTIAL to NVIDIA and is being provided under the terms and 12 | # conditions of a form of NVIDIA software license agreement by and 13 | # between NVIDIA and Licensee ("License Agreement") or electronically 14 | # accepted by Licensee. Notwithstanding any terms or conditions to 15 | # the contrary in the License Agreement, reproduction or disclosure 16 | # of the Licensed Deliverables to any third party without the express 17 | # written consent of NVIDIA is prohibited. 18 | # 19 | # NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE 20 | # LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE 21 | # SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS 22 | # PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. 23 | # NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED 24 | # DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, 25 | # NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. 26 | # NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE 27 | # LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY 28 | # SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY 29 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 30 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 31 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 32 | # OF THESE LICENSED DELIVERABLES. 33 | # 34 | # U.S. Government End Users. These Licensed Deliverables are a 35 | # "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT 36 | # 1995), consisting of "commercial computer software" and "commercial 37 | # computer software documentation" as such terms are used in 48 38 | # C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government 39 | # only as a commercial end item. Consistent with 48 C.F.R.12.212 and 40 | # 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all 41 | # U.S. Government End Users acquire the Licensed Deliverables with 42 | # only those rights set forth herein. 43 | # 44 | # Any use of the Licensed Deliverables in individual and commercial 45 | # software must include, in the user documentation and internal 46 | # comments to the code, the above Disclaimer and U.S. Government End 47 | # Users Notice. 48 | # 49 | 50 | """This file contains functions for training a TensorFlow model""" 51 | 52 | import datetime 53 | import tensorflow as tf 54 | import numpy as np 55 | 56 | if not tf.__version__.startswith('1.'): 57 | print('Graph freezing only supported in TF 1.x') 58 | exit(1) 59 | 60 | 61 | def process_dataset(): 62 | # Import the data 63 | (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data() 64 | x_train, x_test = x_train / 255.0, x_test / 255.0 65 | 66 | # Reshape the data 67 | NUM_TRAIN = 60000 68 | NUM_TEST = 10000 69 | x_train = np.reshape(x_train, (NUM_TRAIN, 28, 28, 1)) 70 | x_test = np.reshape(x_test, (NUM_TEST, 28, 28, 1)) 71 | return x_train, y_train, x_test, y_test 72 | 73 | def create_model(): 74 | model = tf.keras.models.Sequential() 75 | model.add(tf.keras.layers.Conv2D(filters=6, kernel_size=(3, 3), activation='relu', input_shape=[28, 28, 1])) 76 | model.add(tf.keras.layers.AveragePooling2D()) 77 | model.add(tf.keras.layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu')) 78 | model.add(tf.keras.layers.AveragePooling2D()) 79 | model.add(tf.keras.layers.Flatten()) 80 | model.add(tf.keras.layers.Dense(units=120, activation='relu')) 81 | model.add(tf.keras.layers.Dense(units=84, activation='relu')) 82 | model.add(tf.keras.layers.Dense(units=10, activation='softmax')) 83 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 84 | return model 85 | 86 | def save(model, filename): 87 | # First freeze the graph and remove training nodes. 88 | output_names = model.output.op.name 89 | sess = tf.compat.v1.keras.backend.get_session() 90 | frozen_graph = tf.compat.v1.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), [output_names]) 91 | frozen_graph = tf.compat.v1.graph_util.remove_training_nodes(frozen_graph) 92 | # Save the model 93 | with open(filename, "wb") as ofile: 94 | ofile.write(frozen_graph.SerializeToString()) 95 | 96 | 97 | def main(): 98 | print('using TensorFlow version: ', tf.__version__) 99 | x_train, y_train, x_test, y_test = process_dataset() 100 | model = create_model() 101 | model.summary() 102 | 103 | # Train the model on the data 104 | num_epochs = 20 105 | log_dir = 'logs/train' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S') 106 | tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, update_freq='epoch', write_grads=True) 107 | model.fit(x_train, y_train, batch_size=128, validation_split=0.1, epochs=num_epochs, callbacks=[tensorboard_callback]) 108 | 109 | # Evaluate the model on test data 110 | log_dir = 'logs/test' + datetime.datetime.now().strftime('%Y%m%d-%H%M%S') 111 | tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, update_freq='batch', write_grads=True) 112 | eval_loss, eval_accuracy = model.evaluate(x_test, y_test, callbacks=[tensorboard_callback]) 113 | print(f'after {num_epochs} training epochs: eval loss={eval_loss}, eval accuracy={eval_accuracy}') 114 | 115 | # Save model 116 | save(model, filename="../models/lenet5.pb") 117 | 118 | 119 | if __name__ == '__main__': 120 | main() 121 | -------------------------------------------------------------------------------- /examples/tensorrt/tensorrt_example.js: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | // Redistribution and use in source and binary forms, with or without 3 | // modification, are permitted provided that the following conditions 4 | // are met: 5 | // * Redistributions of source code must retain the above copyright 6 | // notice, this list of conditions and the following disclaimer. 7 | // * Redistributions in binary form must reproduce the above copyright 8 | // notice, this list of conditions and the following disclaimer in the 9 | // documentation and/or other materials provided with the distribution. 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its 11 | // contributors may be used to endorse or promote products derived 12 | // from this software without specific prior written permission. 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 14 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 17 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 | // INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 'use strict' 25 | 26 | const fs = require('fs') 27 | 28 | // get grCUDA root namespace and trt namespace object 29 | const cu = Polyglot.eval('grcuda', 'CU') 30 | const trt = Polyglot.eval('grcuda', 'CU::TRT') 31 | 32 | // helper function to read PGM image file 33 | function readPGM (imageFile, imageDeviceArray) { 34 | const fileBuffer = fs.readFileSync(imageFile) 35 | let offset = 0 36 | let nlPos = fileBuffer.indexOf('\n', offset) 37 | const magic = fileBuffer.slice(offset, nlPos).toString() 38 | if (magic !== 'P5') { 39 | throw Error(`invalid file magic in ${imageFile}`) 40 | } 41 | console.log(magic) 42 | 43 | offset = nlPos + 1 44 | nlPos = fileBuffer.indexOf(' ', offset) 45 | const width = parseInt(fileBuffer.slice(offset, nlPos).toString()) 46 | 47 | offset = nlPos + 1 48 | nlPos = fileBuffer.indexOf('\n', offset) 49 | const height = parseInt(fileBuffer.slice(offset, nlPos).toString()) 50 | 51 | offset = nlPos + 1 52 | nlPos = fileBuffer.indexOf('\n', offset) 53 | const maxLevels = parseInt(fileBuffer.slice(offset, nlPos).toString()) 54 | offset = nlPos + 1 55 | const data = fileBuffer.slice(offset) 56 | 57 | const useUint16 = maxLevels > 255 58 | const numElements = width * height 59 | if (imageDeviceArray.length < numElements) { 60 | throw Error(`image file contains ${numElements} elements but buffer can hold only ${imageDeviceArray.length}`) 61 | } 62 | for (let row = 0; row < height; row += 1) { 63 | for (let col = 0; col < width; col += 1) { 64 | const idx = row * width + col 65 | let value 66 | if (useUint16) { 67 | value = data.readUInt16LE(idx) 68 | } else { 69 | value = data.readUInt8(idx) 70 | } 71 | imageDeviceArray[idx] = 1.0 - value / maxLevels 72 | } 73 | } 74 | } 75 | 76 | if (process.argv.length !== 3) { 77 | console.error('one single argument: pgm image file') 78 | process.exit(1) 79 | } 80 | const imageFile = process.argv[2] 81 | 82 | // create buffers 83 | const width = 28 84 | const height = 28 85 | const numInputElements = width * height 86 | const numClasses = 10 87 | const classProbabilities = cu.DeviceArray('float', numClasses) 88 | const inputImage = cu.DeviceArray('float', numInputElements) 89 | 90 | // load image file 91 | console.log(`loading ${imageFile}...`) 92 | readPGM(imageFile, inputImage) 93 | let str = '' 94 | for (let row = 0; row < height; row += 1) { 95 | for (let col = 0; col < width; col += 1) { 96 | str += inputImage[row * width + col] < 0.5 ? '@' : ' ' 97 | } 98 | str += '\n' 99 | } 100 | console.log(str) 101 | 102 | // instantiate inference runtime 103 | console.log('createInferRuntime()') 104 | const runtime = trt.createInferRuntime() 105 | 106 | // load engine file 107 | const engineFile = 'models/lenet5.engine' 108 | console.log(`deserializeCudaEngine(runtime=${runtime}, engineFileName='${engineFile}')`) 109 | const engine = trt.deserializeCudaEngine(runtime, engineFile) 110 | 111 | // get binding indices for input and output layers 112 | const inputName = 'conv2d_input' 113 | const outputName = 'dense_2/Softmax' 114 | const inputIndex = trt.getBindingIndex(engine, inputName) 115 | const outputIndex = trt.getBindingIndex(engine, outputName) 116 | console.log(`input index = ${inputIndex} for '${inputName}'`) 117 | console.log(`output index = ${outputIndex} for '${outputName}'`) 118 | const maxBatchSize = trt.getMaxBatchSize(engine) 119 | console.log(`max batch size = ${maxBatchSize}`) 120 | 121 | // create execution context 122 | console.log(`createExecutionContext(engine=${engine})`) 123 | const executionContext = trt.createExecutionContext(engine) 124 | 125 | // submit inference job 126 | const batchSize = 1 127 | const buffers = [] 128 | buffers[inputIndex] = inputImage 129 | buffers[outputIndex] = classProbabilities 130 | trt.enqueue(engine, batchSize, buffers) 131 | cu.cudaDeviceSynchronize() 132 | 133 | // print inferred class probabilities and make prediction 134 | let maxProb = 0 135 | let maxProbDigit = 0 136 | for (let digit = 0; digit < numClasses; digit += 1) { 137 | if (classProbabilities[digit] > maxProb) { 138 | maxProb = classProbabilities[digit] 139 | maxProbDigit = digit 140 | } 141 | console.log(`${digit}: ${classProbabilities[digit].toFixed(4)}`) 142 | } 143 | console.log(`prediction: ${maxProbDigit}`) 144 | 145 | // tear down 146 | trt.destroyExecutionContext(executionContext) 147 | trt.destroyEngine(engine) 148 | trt.destroyInferRuntime(runtime) 149 | -------------------------------------------------------------------------------- /mx.grcuda/mx_grcuda.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | import mx 28 | import mx_subst 29 | 30 | def _get_src_dir(projectname): 31 | for suite in mx.suites(): 32 | for p in suite.projects: 33 | if p.name == projectname: 34 | if len(p.source_dirs()) > 0: 35 | return p.source_dirs()[0] 36 | else: 37 | return p.dir 38 | mx.abort("Could not find src dir for project %s" % projectname) 39 | 40 | mx_subst.path_substitutions.register_with_arg('src_dir', _get_src_dir) 41 | 42 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda.parser.antlr/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 2 | # Copyright (c) 2019, Oracle and/or its affiliates. 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 | 29 | QUIETLY$(MX_VERBOSE) = @ 30 | 31 | SOURCE_GRCUDA=${PARSER_PATH}/GrCUDA.g4 32 | SOURCE_NIDL=${PARSER_PATH}/NIDL.g4 33 | STAMP_GRCUDA=${PARSER_PATH}/GrCUDA.g4.stamp 34 | STAMP_NIDL=${PARSER_PATH}/NIDL.g4.stamp 35 | 36 | GEN_FILES=${OUTPUT_PATH}/GrCUDALexer.java ${OUTPUT_PATH}/GrCUDAParser.java \ 37 | ${OUTPUT_PATH}/NIDLLexer.java ${OUTPUT_PATH}/NIDLParser.java 38 | 39 | .PHONY: default clean 40 | default: ${GEN_FILES} ${STAMP_GRCUDA} ${STAMP_NIDL} 41 | 42 | ${STAMP_GRCUDA}: ${SOURCE_GRCUDA} 43 | $(QUIETLY) touch $@ 44 | $(QUIETLY) ${JAVA_HOME}/bin/java -cp ${ANTLR_JAR} org.antlr.v4.Tool \ 45 | -no-visitor -no-listener -package ${PARSER_PKG} -o ${OUTPUT_PATH} ${SOURCE_GRCUDA} 46 | 47 | ${STAMP_NIDL}: ${SOURCE_NIDL} 48 | $(QUIETLY) touch $@ 49 | $(QUIETLY) ${JAVA_HOME}/bin/java -cp ${ANTLR_JAR} org.antlr.v4.Tool \ 50 | -no-visitor -no-listener -package ${PARSER_PKG} -o ${OUTPUT_PATH} ${SOURCE_NIDL} 51 | 52 | ${GEN_FILES}: %.java: ${STAMP_GRCUDA} ${STAMP_NIDL} 53 | $(QUIETLY) python3 ${POSTPROCESSOR} $@ 54 | 55 | clean: 56 | rm -f ${TARGETS} 57 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda.parser.antlr/postprocessor.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 2 | # Copyright (c) 2019, Oracle and/or its affiliates. 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 | """Fixes up the Java code generated by ANTLR4 to get checkstyle pass 29 | 30 | Adds copyright header, disables checkstyle and Eclipse code formatter. 31 | """ 32 | 33 | import re 34 | import sys 35 | 36 | 37 | JAVA_COPYRIGHT_HEADER = """\ 38 | /* 39 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 40 | * 41 | * Redistribution and use in source and binary forms, with or without 42 | * modification, are permitted provided that the following conditions 43 | * are met: 44 | * * Redistributions of source code must retain the above copyright 45 | * notice, this list of conditions and the following disclaimer. 46 | * * Redistributions in binary form must reproduce the above copyright 47 | * notice, this list of conditions and the following disclaimer in the 48 | * documentation and/or other materials provided with the distribution. 49 | * * Neither the name of NVIDIA CORPORATION nor the names of its 50 | * contributors may be used to endorse or promote products derived 51 | * from this software without specific prior written permission. 52 | * 53 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 54 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 57 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 61 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 63 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 | */ 65 | """ 66 | 67 | DISABLE_CHECKSTYLE = '// Checkstyle: stop' 68 | 69 | DISABLE_FORMATTER = '// @formatter:off' 70 | 71 | 72 | def transform(lines): 73 | """Adds header, disables checkstyle and formatter, then applies all 74 | transforms on every line.""" 75 | out_lines = [] 76 | out_lines.append(JAVA_COPYRIGHT_HEADER + '\n') 77 | out_lines.append(DISABLE_CHECKSTYLE + '\n') 78 | out_lines.append(DISABLE_FORMATTER + '\n') 79 | for line in lines: 80 | l = line 81 | # additional line specific transforms 82 | out_lines.append(l) 83 | 84 | # make sure that file ends with a new line 85 | if out_lines[-1][-1] != '\n': 86 | out_lines[-1] += '\n' 87 | return ''.join(out_lines) 88 | 89 | 90 | def main(): 91 | if len(sys.argv) < 2: 92 | print('file arguments missing', file=sys.stderr) 93 | sys.exit(1) 94 | 95 | file_names = sys.argv[1:] 96 | for file_name in file_names: 97 | with open(file_name, 'rt') as in_lines: 98 | out_lines = transform(in_lines) 99 | with open(file_name, 'w') as out_file: 100 | out_file.write(out_lines) 101 | 102 | if __name__ == '__main__': 103 | main() 104 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda.test/src/com/nvidia/grcuda/test/DeviceArrayFreeTest.java: -------------------------------------------------------------------------------- 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.assertTrue; 31 | 32 | import org.graalvm.polyglot.Context; 33 | import org.graalvm.polyglot.PolyglotException; 34 | import org.graalvm.polyglot.Value; 35 | import org.junit.Test; 36 | 37 | public class DeviceArrayFreeTest { 38 | 39 | // 40 | // Tests for 1-dim DeviceArray 41 | // 42 | 43 | @Test 44 | public void testCanInvokeFreeDeviceArray() { 45 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 46 | // create DeviceArray 47 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 48 | Value deviceArray = createDeviceArray.execute("int", 1000); 49 | assertTrue(deviceArray.canInvokeMember("free")); 50 | deviceArray.invokeMember("free"); 51 | // check that freed flag set 52 | assertTrue(deviceArray.hasMember("isMemoryFreed")); 53 | assertTrue(deviceArray.getMember("isMemoryFreed").asBoolean()); 54 | } 55 | } 56 | 57 | @Test(expected = PolyglotException.class) 58 | public void testDeviceArrayAccessAfterFreeThrows() { 59 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 60 | // create DeviceArray 61 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 62 | Value deviceArray = createDeviceArray.execute("int", 1000); 63 | deviceArray.invokeMember("free"); 64 | deviceArray.setArrayElement(0, 42); // throws 65 | } 66 | } 67 | 68 | @Test(expected = PolyglotException.class) 69 | public void testDeviceArrayDoubleFreeThrows() { 70 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 71 | // create DeviceArray 72 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 73 | Value deviceArray = createDeviceArray.execute("int", 1000); 74 | deviceArray.invokeMember("free"); 75 | deviceArray.invokeMember("free"); // throws 76 | } 77 | } 78 | 79 | // 80 | // Tests for Multi-dimensional DeviceArray 81 | // 82 | 83 | @Test 84 | public void testCanInvokeFreeMultiDimDeviceArray() { 85 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 86 | // create DeviceArray 87 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 88 | Value deviceArray = createDeviceArray.execute("int", 100, 100); 89 | assertTrue(deviceArray.canInvokeMember("free")); 90 | deviceArray.invokeMember("free"); 91 | // check that freed flag set 92 | assertTrue(deviceArray.hasMember("isMemoryFreed")); 93 | assertTrue(deviceArray.getMember("isMemoryFreed").asBoolean()); 94 | } 95 | } 96 | 97 | @Test(expected = PolyglotException.class) 98 | public void testMultiDimDeviceArrayAccessAfterFreeThrows() { 99 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 100 | // create DeviceArray 101 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 102 | Value deviceArray = createDeviceArray.execute("int", 100, 100); 103 | deviceArray.invokeMember("free"); 104 | deviceArray.getArrayElement(0).setArrayElement(0, 42); // throws 105 | } 106 | } 107 | 108 | @Test(expected = PolyglotException.class) 109 | public void testMultiDimDeviceArrayDoubleFreeThrows() { 110 | try (Context ctx = Context.newBuilder().allowAllAccess(true).build()) { 111 | // create DeviceArray 112 | Value createDeviceArray = ctx.eval("grcuda", "DeviceArray"); 113 | Value deviceArray = createDeviceArray.execute("int", 100, 100); 114 | deviceArray.invokeMember("free"); 115 | deviceArray.invokeMember("free"); // throws 116 | } 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda.test/src/com/nvidia/grcuda/test/ParserTest.java: -------------------------------------------------------------------------------- 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 | 29 | package com.nvidia.grcuda.test; 30 | 31 | import org.junit.Test; 32 | import com.nvidia.grcuda.parser.ParserAntlr; 33 | import com.nvidia.grcuda.parser.GrCUDAParserException; 34 | import com.oracle.truffle.api.source.Source; 35 | 36 | public class ParserTest { 37 | 38 | @Test 39 | public void testArrayExpressionWithIntLiteral() throws GrCUDAParserException { 40 | parseString("double[1000]"); 41 | } 42 | 43 | @Test 44 | public void testArrayExpressionWithIntExpr() throws GrCUDAParserException { 45 | parseString("double[(100+200)*4]"); 46 | } 47 | 48 | @Test(expected = GrCUDAParserException.class) 49 | public void testArrayExpressionWithEmptyBracketsFails() throws GrCUDAParserException { 50 | parseString("double[]"); 51 | } 52 | 53 | @Test(expected = GrCUDAParserException.class) 54 | public void testArrayExpressionWithIdentifierInBracketsFails() throws GrCUDAParserException { 55 | parseString("double[foo]"); 56 | } 57 | 58 | @Test 59 | public void testNoArgsCall() throws GrCUDAParserException { 60 | parseString("foo()"); 61 | parseString("foo( )"); 62 | } 63 | 64 | @Test 65 | public void testBindCall() throws GrCUDAParserException { 66 | parseString("bind(\"libml.so\", \"dbscanFitDouble\", \"(pointer, sint32, sint32, double, sint32, pointer): void\")"); 67 | } 68 | 69 | @Test 70 | public void testBuiltinCallable() throws GrCUDAParserException { 71 | parseString("cudaMallocManaged"); 72 | } 73 | 74 | @Test 75 | public void testBuiltinCallableColonColon() throws GrCUDAParserException { 76 | parseString("::cudaMallocManaged"); 77 | } 78 | 79 | @Test 80 | public void testCallableInNamespace() throws GrCUDAParserException { 81 | parseString("ML::dbscanFitDouble"); 82 | } 83 | 84 | @SuppressWarnings("static-method") 85 | private void parseString(String sourceStr) throws GrCUDAParserException { 86 | Source source = Source.newBuilder("cuda", sourceStr, "testsource").build(); 87 | new ParserAntlr().parse(source); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/FunctionBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import java.util.ArrayList; 32 | import java.util.Arrays; 33 | import java.util.stream.Collectors; 34 | 35 | public final class FunctionBinding extends Binding { 36 | 37 | private final Type returnType; 38 | 39 | private FunctionBinding(String name, ArrayList parameterList, 40 | Type returnType, boolean hasCxxMangledName) { 41 | super(name, parameterList, hasCxxMangledName); 42 | this.returnType = returnType; 43 | } 44 | 45 | public static FunctionBinding newCxxBinding(String name, ArrayList parameterList, Type returnType) { 46 | return new FunctionBinding(name, parameterList, returnType, true); 47 | } 48 | 49 | public static FunctionBinding newCBinding(String name, ArrayList parameterList, Type returnType) { 50 | return new FunctionBinding(name, parameterList, returnType, false); 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return super.toString() + " : " + returnType.toString().toLowerCase(); 56 | } 57 | 58 | @Override 59 | public String toNIDLString() { 60 | return name + "(" + getNIDLParameterSignature() + "): " + returnType.toString().toLowerCase(); 61 | } 62 | 63 | public String toNFISignature() { 64 | return "(" + Arrays.stream(parameters).map(Parameter::toNFISignatureElement).collect(Collectors.joining(", ")) + "): " + returnType.getNFITypeName(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/GPUPointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import com.oracle.truffle.api.interop.InteropLibrary; 32 | import com.oracle.truffle.api.interop.TruffleObject; 33 | import com.oracle.truffle.api.library.ExportLibrary; 34 | import com.oracle.truffle.api.library.ExportMessage; 35 | 36 | @ExportLibrary(InteropLibrary.class) 37 | public final class GPUPointer implements TruffleObject { 38 | 39 | private final long rawPointer; 40 | 41 | public GPUPointer(long rawPointer) { 42 | this.rawPointer = rawPointer; 43 | } 44 | 45 | public long getRawPointer() { 46 | return rawPointer; 47 | } 48 | 49 | @Override 50 | public String toString() { 51 | return "GPUPointer(address=0x" + Long.toHexString(rawPointer); 52 | } 53 | 54 | @ExportMessage 55 | @SuppressWarnings("static-method") 56 | boolean isPointer() { 57 | return true; 58 | } 59 | 60 | @ExportMessage 61 | long asPointer() { 62 | return rawPointer; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/GrCUDAException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import java.util.Arrays; 32 | import java.util.Optional; 33 | 34 | import com.oracle.truffle.api.TruffleException; 35 | import com.oracle.truffle.api.interop.InteropException; 36 | import com.oracle.truffle.api.nodes.Node; 37 | 38 | public final class GrCUDAException extends RuntimeException implements TruffleException { 39 | private static final long serialVersionUID = 8614211550329856579L; 40 | 41 | private final Node node; 42 | 43 | public GrCUDAException(String message) { 44 | this(message, null); 45 | } 46 | 47 | public GrCUDAException(String message, Node node) { 48 | super(message); 49 | this.node = node; 50 | } 51 | 52 | public GrCUDAException(InteropException e) { 53 | this(e.getMessage()); 54 | } 55 | 56 | public GrCUDAException(int errorCode, String[] functionName) { 57 | this("CUDA error " + errorCode + " in " + format(functionName)); 58 | } 59 | 60 | public GrCUDAException(int errorCode, String message, String[] functionName) { 61 | this(message + '(' + errorCode + ") in " + format(functionName)); 62 | } 63 | 64 | public static String format(String... name) { 65 | Optional result = Arrays.asList(name).stream().reduce((a, b) -> a + "::" + b); 66 | return result.orElse(""); 67 | } 68 | 69 | @Override 70 | public Node getLocation() { 71 | return node; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/GrCUDAInternalException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import com.oracle.truffle.api.TruffleException; 32 | import com.oracle.truffle.api.interop.InteropException; 33 | import com.oracle.truffle.api.nodes.Node; 34 | 35 | public final class GrCUDAInternalException extends RuntimeException implements TruffleException { 36 | private static final long serialVersionUID = 8614211550329856579L; 37 | 38 | private final Node node; 39 | 40 | public GrCUDAInternalException(String message) { 41 | this(message, null); 42 | } 43 | 44 | public GrCUDAInternalException(String message, Node node) { 45 | super(message); 46 | this.node = node; 47 | } 48 | 49 | public GrCUDAInternalException(InteropException e) { 50 | this(e.getMessage()); 51 | } 52 | 53 | public boolean isInternalError() { 54 | return true; 55 | } 56 | 57 | @Override 58 | public Node getLocation() { 59 | return node; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/GrCUDALanguage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import org.graalvm.options.OptionDescriptors; 32 | 33 | import com.nvidia.grcuda.nodes.ExpressionNode; 34 | import com.nvidia.grcuda.nodes.GrCUDARootNode; 35 | import com.nvidia.grcuda.parser.ParserAntlr; 36 | import com.oracle.truffle.api.CallTarget; 37 | import com.oracle.truffle.api.Truffle; 38 | import com.oracle.truffle.api.TruffleLanguage; 39 | import com.oracle.truffle.api.interop.TruffleObject; 40 | 41 | /** 42 | * grCUDA Truffle language that exposes the GPU device and CUDA runtime to polyglot Graal languages. 43 | */ 44 | @TruffleLanguage.Registration(id = GrCUDALanguage.ID, name = "grcuda", version = "0.1", internal = false) 45 | public final class GrCUDALanguage extends TruffleLanguage { 46 | 47 | public static final String ID = "grcuda"; 48 | 49 | @Override 50 | protected GrCUDAContext createContext(Env env) { 51 | if (!env.isNativeAccessAllowed()) { 52 | throw new GrCUDAException("cannot create CUDA context without native access"); 53 | } 54 | return new GrCUDAContext(env); 55 | } 56 | 57 | @Override 58 | protected boolean isObjectOfLanguage(Object object) { 59 | if (!(object instanceof TruffleObject)) { 60 | return false; 61 | } 62 | TruffleObject truffleObject = (TruffleObject) object; 63 | return truffleObject instanceof DeviceArray; 64 | } 65 | 66 | @Override 67 | protected CallTarget parse(ParsingRequest request) { 68 | ExpressionNode expression = new ParserAntlr().parse(request.getSource()); 69 | GrCUDARootNode newParserRoot = new GrCUDARootNode(this, expression); 70 | return Truffle.getRuntime().createCallTarget(newParserRoot); 71 | } 72 | 73 | public static GrCUDALanguage getCurrentLanguage() { 74 | return TruffleLanguage.getCurrentLanguage(GrCUDALanguage.class); 75 | } 76 | 77 | @Override 78 | protected void disposeContext(GrCUDAContext cxt) { 79 | cxt.disposeAll(); 80 | } 81 | 82 | @Override 83 | protected OptionDescriptors getOptionDescriptors() { 84 | return new GrCUDAOptionsOptionDescriptors(); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/GrCUDAOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import org.graalvm.options.OptionCategory; 32 | import org.graalvm.options.OptionKey; 33 | import org.graalvm.options.OptionStability; 34 | 35 | import com.nvidia.grcuda.cublas.CUBLASRegistry; 36 | import com.nvidia.grcuda.cuml.CUMLRegistry; 37 | import com.nvidia.grcuda.tensorrt.TensorRTRegistry; 38 | import com.oracle.truffle.api.Option; 39 | 40 | @Option.Group(GrCUDALanguage.ID) 41 | public final class GrCUDAOptions { 42 | private GrCUDAOptions() { 43 | // no instances 44 | } 45 | 46 | @Option(category = OptionCategory.USER, help = "Enable cuBLAS support.", stability = OptionStability.STABLE) // 47 | public static final OptionKey CuBLASEnabled = new OptionKey<>(true); 48 | 49 | @Option(category = OptionCategory.USER, help = "Set the location of the cublas library.", stability = OptionStability.STABLE) // 50 | public static final OptionKey CuBLASLibrary = new OptionKey<>(CUBLASRegistry.DEFAULT_LIBRARY); 51 | 52 | @Option(category = OptionCategory.USER, help = "Enable cuML support.", stability = OptionStability.STABLE) // 53 | public static final OptionKey CuMLEnabled = new OptionKey<>(true); 54 | 55 | @Option(category = OptionCategory.USER, help = "Set the location of the cuml library.", stability = OptionStability.STABLE) // 56 | public static final OptionKey CuMLLibrary = new OptionKey<>(CUMLRegistry.DEFAULT_LIBRARY); 57 | 58 | @Option(category = OptionCategory.USER, help = "Enable TensorRT support.", stability = OptionStability.STABLE) // 59 | public static final OptionKey TensorRTEnabled = new OptionKey<>(true); 60 | 61 | @Option(category = OptionCategory.USER, help = "Set the location of the TensorRT library.", stability = OptionStability.STABLE) // 62 | public static final OptionKey TensorRTLibrary = new OptionKey<>(TensorRTRegistry.DEFAULT_LIBRARY); 63 | 64 | } 65 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/KernelBinding.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import java.util.ArrayList; 32 | 33 | public final class KernelBinding extends Binding { 34 | 35 | private KernelBinding(String name, ArrayList parameterList, boolean hasCxxMangledName) { 36 | super(name, parameterList, hasCxxMangledName); 37 | } 38 | 39 | public static KernelBinding newCxxBinding(String name, ArrayList parameterList) { 40 | return new KernelBinding(name, parameterList, true); 41 | } 42 | 43 | public static KernelBinding newCBinding(String name, ArrayList parameterList) { 44 | return new KernelBinding(name, parameterList, false); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/NoneValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import com.oracle.truffle.api.interop.InteropLibrary; 32 | import com.oracle.truffle.api.interop.TruffleObject; 33 | import com.oracle.truffle.api.library.ExportLibrary; 34 | import com.oracle.truffle.api.library.ExportMessage; 35 | 36 | /** 37 | * None is a singleton object that will always returned of a function or a member returns `void`. 38 | * 39 | * This is to satisfy the post-condition in the Truffle call contract for invokeMember and execute, 40 | * which states the the result type must be either a boxed primitive type or a TruffleObject. 41 | * 42 | */ 43 | @ExportLibrary(InteropLibrary.class) 44 | public class NoneValue implements TruffleObject { 45 | 46 | private static final NoneValue singleton = new NoneValue(); 47 | 48 | public static NoneValue get() { 49 | return singleton; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "NoneValue"; 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return 123456789; 60 | } 61 | 62 | @Override 63 | public boolean equals(Object other) { 64 | return other instanceof NoneValue; 65 | } 66 | 67 | @ExportMessage 68 | public boolean isNull() { 69 | return true; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/TypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda; 30 | 31 | import com.oracle.truffle.api.CompilerAsserts; 32 | 33 | public class TypeException extends Exception { 34 | 35 | private static final long serialVersionUID = -7313402629647154160L; 36 | 37 | TypeException(String message) { 38 | super(message); 39 | CompilerAsserts.neverPartOfCompilation(); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/CUDAFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.GrCUDAException; 32 | import com.nvidia.grcuda.gpu.CUDARuntime; 33 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 34 | import com.oracle.truffle.api.interop.ArityException; 35 | import com.oracle.truffle.api.interop.InteropException; 36 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 37 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 38 | 39 | public final class CUDAFunction extends Function { 40 | 41 | public interface Spec { 42 | Object call(CUDARuntime cudaRuntime, Object[] args) throws InteropException, ArityException, UnsupportedTypeException; 43 | 44 | String getName(); 45 | } 46 | 47 | private final Spec function; 48 | private final CUDARuntime runtime; 49 | 50 | public CUDAFunction(Spec function, CUDARuntime runtime) { 51 | super(function.getName()); 52 | this.function = function; 53 | this.runtime = runtime; 54 | } 55 | 56 | @Override 57 | @TruffleBoundary 58 | protected Object call(Object[] arguments) throws ArityException, UnsupportedTypeException, UnsupportedMessageException { 59 | try { 60 | return function.call(runtime, arguments); 61 | } catch (InteropException e) { 62 | throw new GrCUDAException(e); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/DeviceArrayCopyFunction.java: -------------------------------------------------------------------------------- 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.functions; 29 | 30 | import com.nvidia.grcuda.DeviceArray; 31 | import com.oracle.truffle.api.CompilerDirectives; 32 | import com.oracle.truffle.api.interop.ArityException; 33 | import com.oracle.truffle.api.interop.InteropLibrary; 34 | import com.oracle.truffle.api.interop.TruffleObject; 35 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 36 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 37 | import com.oracle.truffle.api.library.CachedLibrary; 38 | import com.oracle.truffle.api.library.ExportLibrary; 39 | import com.oracle.truffle.api.library.ExportMessage; 40 | 41 | @ExportLibrary(InteropLibrary.class) 42 | public class DeviceArrayCopyFunction implements TruffleObject { 43 | 44 | public enum CopyDirection { 45 | FROM_POINTER, 46 | TO_POINTER 47 | } 48 | 49 | private final DeviceArray deviceArray; 50 | private final CopyDirection direction; 51 | 52 | public DeviceArrayCopyFunction(DeviceArray deviceArray, CopyDirection direction) { 53 | this.deviceArray = deviceArray; 54 | this.direction = direction; 55 | } 56 | 57 | @ExportMessage 58 | @SuppressWarnings("static-method") 59 | boolean isExecutable() { 60 | return true; 61 | } 62 | 63 | private static long extractPointer(Object valueObj, String argumentName, InteropLibrary access) throws UnsupportedTypeException { 64 | try { 65 | if (access.isPointer(valueObj)) { 66 | return access.asPointer(valueObj); 67 | } 68 | return access.asLong(valueObj); 69 | } catch (UnsupportedMessageException e) { 70 | CompilerDirectives.transferToInterpreter(); 71 | throw UnsupportedTypeException.create(new Object[]{valueObj}, "integer expected for " + argumentName); 72 | } 73 | } 74 | 75 | private static int extractNumber(Object valueObj, String argumentName, InteropLibrary access) throws UnsupportedTypeException { 76 | try { 77 | return access.asInt(valueObj); 78 | } catch (UnsupportedMessageException e) { 79 | CompilerDirectives.transferToInterpreter(); 80 | throw UnsupportedTypeException.create(new Object[]{valueObj}, "integer expected for " + argumentName); 81 | } 82 | } 83 | 84 | @ExportMessage 85 | Object execute(Object[] arguments, 86 | @CachedLibrary(limit = "3") InteropLibrary pointerAccess, 87 | @CachedLibrary(limit = "3") InteropLibrary numElementsAccess) throws UnsupportedTypeException, ArityException, IndexOutOfBoundsException { 88 | long numElements; 89 | if (arguments.length == 1) { 90 | numElements = deviceArray.getArraySize(); 91 | } else if (arguments.length == 2) { 92 | numElements = extractNumber(arguments[1], "numElements", numElementsAccess); 93 | } else { 94 | CompilerDirectives.transferToInterpreter(); 95 | throw ArityException.create(1, arguments.length); 96 | } 97 | long pointer = extractPointer(arguments[0], "fromPointer", pointerAccess); 98 | if (direction == CopyDirection.FROM_POINTER) { 99 | deviceArray.copyFrom(pointer, numElements); 100 | } 101 | if (direction == CopyDirection.TO_POINTER) { 102 | deviceArray.copyTo(pointer, numElements); 103 | } 104 | return deviceArray; 105 | } 106 | 107 | @Override 108 | public String toString() { 109 | return "DeviceArrayCopyFunction(deviceArray=" + deviceArray + ", direction=" + direction.name() + ")"; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/ExternalFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 32 | import com.oracle.truffle.api.interop.ArityException; 33 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 34 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 35 | 36 | public final class ExternalFunction extends Function { 37 | 38 | private final Object nfiCallable; 39 | 40 | public ExternalFunction(String name, Object nfiCallable) { 41 | super(name); 42 | this.nfiCallable = nfiCallable; 43 | } 44 | 45 | @Override 46 | @TruffleBoundary 47 | protected Object call(Object[] arguments) throws ArityException, UnsupportedTypeException, UnsupportedMessageException { 48 | return INTEROP.execute(nfiCallable, arguments); 49 | } 50 | 51 | @Override 52 | public String toString() { 53 | return "ExternalFunction(name=" + getName() + ", nfiCallable=" + nfiCallable + ")"; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/ExternalFunctionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.GrCUDAException; 32 | import com.nvidia.grcuda.gpu.CUDARuntime; 33 | import com.oracle.truffle.api.CompilerDirectives; 34 | import com.oracle.truffle.api.interop.InteropException; 35 | 36 | public final class ExternalFunctionFactory { 37 | 38 | private final String name; 39 | private final String symbolName; 40 | private final String nfiSignature; 41 | 42 | public ExternalFunctionFactory(String name, String symbolName, String nfiSignature) { 43 | this.name = name; 44 | this.symbolName = symbolName; 45 | this.nfiSignature = nfiSignature; 46 | } 47 | 48 | public String getName() { 49 | return name; 50 | } 51 | 52 | public String getSymbolName() { 53 | return symbolName; 54 | } 55 | 56 | public String getNFISignature() { 57 | return nfiSignature; 58 | } 59 | 60 | public ExternalFunction makeFunction(CUDARuntime cudaRuntime, String libraryPath, String hint) { 61 | try { 62 | return new ExternalFunction(name, cudaRuntime.getSymbol(libraryPath, symbolName, nfiSignature, hint)); 63 | } catch (InteropException e) { 64 | CompilerDirectives.transferToInterpreter(); 65 | throw new GrCUDAException(e); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/Function.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.oracle.truffle.api.CompilerAsserts; 32 | import com.oracle.truffle.api.CompilerDirectives; 33 | import com.oracle.truffle.api.interop.ArityException; 34 | import com.oracle.truffle.api.interop.InteropLibrary; 35 | import com.oracle.truffle.api.interop.TruffleObject; 36 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 37 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 38 | import com.oracle.truffle.api.library.ExportLibrary; 39 | import com.oracle.truffle.api.library.ExportMessage; 40 | 41 | @ExportLibrary(InteropLibrary.class) 42 | public abstract class Function implements TruffleObject { 43 | 44 | public static final InteropLibrary INTEROP = InteropLibrary.getFactory().getUncached(); 45 | 46 | private final String name; 47 | 48 | protected Function(String name) { 49 | this.name = name; 50 | } 51 | 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | protected static String expectString(Object argument, String errorMessage) throws UnsupportedTypeException { 57 | CompilerAsserts.neverPartOfCompilation(); 58 | try { 59 | return INTEROP.asString(argument); 60 | } catch (UnsupportedMessageException e) { 61 | throw UnsupportedTypeException.create(new Object[]{argument}, errorMessage); 62 | } 63 | } 64 | 65 | public static int expectInt(Object number) throws UnsupportedTypeException { 66 | CompilerAsserts.neverPartOfCompilation(); 67 | try { 68 | return INTEROP.asInt(number); 69 | } catch (UnsupportedMessageException e) { 70 | throw UnsupportedTypeException.create(new Object[]{number}, "expected integer number argument"); 71 | } 72 | } 73 | 74 | protected static long expectLong(Object number, String message) throws UnsupportedTypeException { 75 | CompilerAsserts.neverPartOfCompilation(); 76 | try { 77 | return INTEROP.asLong(number); 78 | } catch (UnsupportedMessageException e) { 79 | throw UnsupportedTypeException.create(new Object[]{number}, message); 80 | } 81 | } 82 | 83 | public static long expectLong(Object number) throws UnsupportedTypeException { 84 | return expectLong(number, "expected long number argument"); 85 | } 86 | 87 | protected static int expectPositiveInt(Object number) throws UnsupportedTypeException { 88 | int value = expectInt(number); 89 | if (value < 0) { 90 | CompilerDirectives.transferToInterpreter(); 91 | throw UnsupportedTypeException.create(new Object[]{number}, "expected positive int number argument"); 92 | } 93 | return value; 94 | } 95 | 96 | public static long expectPositiveLong(Object number) throws UnsupportedTypeException { 97 | long value = expectLong(number); 98 | if (value < 0) { 99 | CompilerDirectives.transferToInterpreter(); 100 | throw UnsupportedTypeException.create(new Object[]{number}, "expected positive long number argument"); 101 | } 102 | return value; 103 | } 104 | 105 | public static void checkArgumentLength(Object[] arguments, int expected) throws ArityException { 106 | if (arguments.length != expected) { 107 | CompilerDirectives.transferToInterpreter(); 108 | throw ArityException.create(expected, arguments.length); 109 | } 110 | } 111 | 112 | // InteropLibrary implementation 113 | 114 | @ExportMessage 115 | @SuppressWarnings("static-method") 116 | public final boolean isExecutable() { 117 | return true; 118 | } 119 | 120 | @ExportMessage 121 | public Object execute(Object[] arguments) throws ArityException, UnsupportedTypeException, UnsupportedMessageException { 122 | return call(arguments); 123 | } 124 | 125 | @SuppressWarnings("unused") 126 | protected Object call(Object[] arguments) throws ArityException, UnsupportedTypeException, UnsupportedMessageException { 127 | throw UnsupportedMessageException.create(); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/GetDeviceFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.gpu.CUDARuntime; 32 | import com.nvidia.grcuda.gpu.Device; 33 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 34 | import com.oracle.truffle.api.interop.ArityException; 35 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 36 | 37 | public class GetDeviceFunction extends Function { 38 | 39 | private final CUDARuntime runtime; 40 | 41 | public GetDeviceFunction(CUDARuntime runtime) { 42 | super("getdevice"); 43 | this.runtime = runtime; 44 | } 45 | 46 | @Override 47 | @TruffleBoundary 48 | public Object call(Object[] arguments) throws UnsupportedTypeException, ArityException { 49 | checkArgumentLength(arguments, 1); 50 | int deviceId = expectPositiveInt(arguments[0]); 51 | return new Device(deviceId, runtime); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/GetDevicesFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.gpu.CUDARuntime; 32 | import com.nvidia.grcuda.gpu.DeviceList; 33 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 34 | import com.oracle.truffle.api.interop.ArityException; 35 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 36 | 37 | public class GetDevicesFunction extends Function { 38 | private final CUDARuntime runtime; 39 | 40 | public GetDevicesFunction(CUDARuntime runtime) { 41 | super("getdevices"); 42 | this.runtime = runtime; 43 | } 44 | 45 | @Override 46 | @TruffleBoundary 47 | public Object call(Object[] arguments) throws UnsupportedTypeException, ArityException { 48 | checkArgumentLength(arguments, 0); 49 | int numDevices = runtime.cudaGetDeviceCount(); 50 | return new DeviceList(numDevices, runtime); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/HostFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.FunctionBinding; 32 | import com.nvidia.grcuda.GrCUDAException; 33 | import com.nvidia.grcuda.gpu.CUDARuntime; 34 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 35 | import com.oracle.truffle.api.interop.ArityException; 36 | import com.oracle.truffle.api.interop.UnknownIdentifierException; 37 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 38 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 39 | 40 | public class HostFunction extends Function { 41 | 42 | private final CUDARuntime cudaRuntime; 43 | private final FunctionBinding binding; 44 | private Object nfiCallable = null; 45 | 46 | public HostFunction(FunctionBinding binding, CUDARuntime runtime) { 47 | super(binding.getName()); 48 | this.binding = binding; 49 | this.cudaRuntime = runtime; 50 | } 51 | 52 | @Override 53 | @TruffleBoundary 54 | protected Object call(Object[] arguments) throws ArityException, UnsupportedTypeException, UnsupportedMessageException { 55 | assertFunctionResolved(); 56 | return INTEROP.execute(nfiCallable, arguments); 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "HostFunction(name=" + binding.getName() + ", nfiCallable=" + nfiCallable + ")"; 62 | } 63 | 64 | public void resolveSymbol() throws UnknownIdentifierException { 65 | synchronized (this) { 66 | if (nfiCallable == null) { 67 | nfiCallable = cudaRuntime.getSymbol(binding); 68 | assert nfiCallable != null : "NFI callable non-null"; 69 | } 70 | } 71 | } 72 | 73 | private void assertFunctionResolved() { 74 | synchronized (this) { 75 | if (nfiCallable == null) { 76 | try { 77 | nfiCallable = cudaRuntime.getSymbol(binding); 78 | } catch (UnknownIdentifierException e) { 79 | throw new GrCUDAException("symbol " + binding.getSymbolName() + " not found: " + e); 80 | } 81 | assert nfiCallable != null : "NFI callable non-null"; 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/TypedDeviceArrayFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.Type; 32 | import com.nvidia.grcuda.gpu.CUDARuntime; 33 | import com.oracle.truffle.api.CompilerDirectives; 34 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 35 | import com.oracle.truffle.api.interop.ArityException; 36 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 37 | 38 | /** 39 | * Special curried version of the device array creation function that is specific to a data type. 40 | */ 41 | public final class TypedDeviceArrayFunction extends Function { 42 | 43 | private final CUDARuntime runtime; 44 | private final Type elementType; 45 | 46 | public TypedDeviceArrayFunction(CUDARuntime runtime, Type elementType) { 47 | super("TypedDeviceArray"); 48 | this.runtime = runtime; 49 | this.elementType = elementType; 50 | } 51 | 52 | @Override 53 | @TruffleBoundary 54 | public Object call(Object[] arguments) throws ArityException, UnsupportedTypeException { 55 | if (arguments.length < 1) { 56 | CompilerDirectives.transferToInterpreter(); 57 | throw ArityException.create(1, arguments.length); 58 | } 59 | return DeviceArrayFunction.createArray(arguments, 0, elementType, runtime); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/TypedMapDeviceArrayFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.functions; 30 | 31 | import com.nvidia.grcuda.Type; 32 | import com.nvidia.grcuda.gpu.CUDARuntime; 33 | import com.oracle.truffle.api.CompilerDirectives; 34 | import com.oracle.truffle.api.dsl.Cached; 35 | import com.oracle.truffle.api.interop.ArityException; 36 | import com.oracle.truffle.api.interop.InteropLibrary; 37 | import com.oracle.truffle.api.library.ExportLibrary; 38 | import com.oracle.truffle.api.library.ExportMessage; 39 | 40 | /** 41 | * Special curried version of the device array mapping function that is specific to a data type. 42 | */ 43 | @ExportLibrary(InteropLibrary.class) 44 | public final class TypedMapDeviceArrayFunction extends Function { 45 | 46 | private final CUDARuntime runtime; 47 | private final Type elementType; 48 | 49 | public TypedMapDeviceArrayFunction(CUDARuntime runtime, Type elementType) { 50 | super("TypedMapDeviceArray"); 51 | this.runtime = runtime; 52 | this.elementType = elementType; 53 | } 54 | 55 | @ExportMessage 56 | public Object execute(Object[] arguments, 57 | @Cached MapArrayNode mapNode) throws ArityException { 58 | if (arguments.length != 1) { 59 | CompilerDirectives.transferToInterpreter(); 60 | throw ArityException.create(1, arguments.length); 61 | } 62 | return mapNode.execute(arguments[0], elementType, runtime); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/map/ArgumentArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Oracle and/or its affiliates. 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.functions.map; 29 | 30 | import com.oracle.truffle.api.CompilerDirectives; 31 | import com.oracle.truffle.api.interop.InteropLibrary; 32 | import com.oracle.truffle.api.interop.InvalidArrayIndexException; 33 | import com.oracle.truffle.api.interop.TruffleObject; 34 | import com.oracle.truffle.api.library.ExportLibrary; 35 | import com.oracle.truffle.api.library.ExportMessage; 36 | 37 | @ExportLibrary(InteropLibrary.class) 38 | public final class ArgumentArray implements TruffleObject { 39 | 40 | private final Object[] values; 41 | 42 | public ArgumentArray(Object[] values) { 43 | this.values = values; 44 | } 45 | 46 | @ExportMessage 47 | @SuppressWarnings("static-method") 48 | public boolean hasArrayElements() { 49 | return true; 50 | } 51 | 52 | @ExportMessage 53 | public long getArraySize() { 54 | return values.length; 55 | } 56 | 57 | @ExportMessage 58 | public boolean isArrayElementReadable(long index) { 59 | return index >= 0 && index < values.length; 60 | } 61 | 62 | @ExportMessage 63 | public Object readArrayElement(long index) throws InvalidArrayIndexException { 64 | if (!isArrayElementReadable(index)) { 65 | CompilerDirectives.transferToInterpreter(); 66 | throw InvalidArrayIndexException.create(index); 67 | } 68 | return values[(int) index]; 69 | } 70 | 71 | @ExportMessage 72 | public boolean isArrayElementModifiable(long index) { 73 | return isArrayElementReadable(index); 74 | } 75 | 76 | @ExportMessage 77 | @SuppressWarnings("static-method") 78 | public boolean isArrayElementInsertable(@SuppressWarnings("unused") long index) { 79 | return false; 80 | } 81 | 82 | @ExportMessage 83 | public void writeArrayElement(long index, Object value) throws InvalidArrayIndexException { 84 | if (!isArrayElementReadable(index)) { 85 | CompilerDirectives.transferToInterpreter(); 86 | throw InvalidArrayIndexException.create(index); 87 | } 88 | values[(int) index] = value; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/map/ArgumentSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, Oracle and/or its affiliates. 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.functions.map; 29 | 30 | import org.graalvm.collections.EconomicMap; 31 | 32 | import com.nvidia.grcuda.DeviceArray.MemberSet; 33 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 34 | import com.oracle.truffle.api.interop.InteropLibrary; 35 | import com.oracle.truffle.api.interop.TruffleObject; 36 | import com.oracle.truffle.api.library.ExportLibrary; 37 | import com.oracle.truffle.api.library.ExportMessage; 38 | 39 | @ExportLibrary(InteropLibrary.class) 40 | public final class ArgumentSet implements TruffleObject { 41 | 42 | final EconomicMap nameList = EconomicMap.create(); 43 | 44 | @ExportMessage 45 | @SuppressWarnings("static-method") 46 | boolean hasMembers() { 47 | return true; 48 | } 49 | 50 | @SuppressWarnings("static-method") 51 | @ExportMessage 52 | @TruffleBoundary 53 | Object getMembers(@SuppressWarnings("unused") boolean includeInternal) { 54 | String[] names = new String[nameList.size()]; 55 | int pos = 0; 56 | for (String name : nameList.getKeys()) { 57 | names[pos++] = name; 58 | } 59 | return new MemberSet(names); 60 | } 61 | 62 | @SuppressWarnings("static-method") 63 | @ExportMessage 64 | boolean isMemberReadable(@SuppressWarnings("unused") String member) { 65 | return true; 66 | } 67 | 68 | @ExportMessage 69 | @TruffleBoundary 70 | public Integer readMember(String member) { 71 | Integer index = nameList.get(member); 72 | if (index == null) { 73 | nameList.put(member, index = nameList.size()); 74 | } 75 | return index; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/functions/map/ShredFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. 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.functions.map; 29 | 30 | import com.nvidia.grcuda.functions.Function; 31 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 32 | import com.oracle.truffle.api.interop.ArityException; 33 | import com.oracle.truffle.api.interop.InteropLibrary; 34 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 35 | import com.oracle.truffle.api.library.ExportLibrary; 36 | import com.oracle.truffle.api.library.ExportMessage; 37 | 38 | @ExportLibrary(InteropLibrary.class) 39 | public final class ShredFunction extends Function { 40 | 41 | public ShredFunction() { 42 | super("shred"); 43 | } 44 | 45 | @Override 46 | @ExportMessage 47 | @TruffleBoundary 48 | public ShreddedObject execute(Object[] arguments) throws ArityException, UnsupportedTypeException { 49 | MapFunction.checkArity(arguments, 1); 50 | return new ShreddedObject(arguments[0]); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/ConfiguredKernel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; 32 | import com.oracle.truffle.api.interop.ArityException; 33 | import com.oracle.truffle.api.interop.InteropLibrary; 34 | import com.oracle.truffle.api.interop.TruffleObject; 35 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 36 | import com.oracle.truffle.api.library.CachedLibrary; 37 | import com.oracle.truffle.api.library.ExportLibrary; 38 | import com.oracle.truffle.api.library.ExportMessage; 39 | 40 | @ExportLibrary(InteropLibrary.class) 41 | public class ConfiguredKernel implements TruffleObject { 42 | 43 | private final Kernel kernel; 44 | private final KernelConfig config; 45 | 46 | public ConfiguredKernel(Kernel kernel, KernelConfig config) { 47 | this.kernel = kernel; 48 | this.config = config; 49 | } 50 | 51 | @ExportMessage 52 | boolean isExecutable() { 53 | return true; 54 | } 55 | 56 | @ExportMessage 57 | @TruffleBoundary 58 | Object execute(Object[] arguments, 59 | @CachedLibrary(limit = "3") InteropLibrary boolAccess, 60 | @CachedLibrary(limit = "3") InteropLibrary int8Access, 61 | @CachedLibrary(limit = "3") InteropLibrary int16Access, 62 | @CachedLibrary(limit = "3") InteropLibrary int32Access, 63 | @CachedLibrary(limit = "3") InteropLibrary int64Access, 64 | @CachedLibrary(limit = "3") InteropLibrary doubleAccess) throws UnsupportedTypeException, ArityException { 65 | kernel.incrementLaunchCount(); 66 | try (KernelArguments args = kernel.createKernelArguments(arguments, boolAccess, int8Access, int16Access, 67 | int32Access, int64Access, doubleAccess)) { 68 | kernel.getCudaRuntime().cuLaunchKernel(kernel, config, args); 69 | } 70 | return this; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "ConfiguredKernel(config=" + config + ", kernel=" + kernel + ')'; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/DeviceList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import java.util.Iterator; 32 | import java.util.NoSuchElementException; 33 | 34 | import com.oracle.truffle.api.CompilerDirectives; 35 | import com.oracle.truffle.api.interop.InteropLibrary; 36 | import com.oracle.truffle.api.interop.InvalidArrayIndexException; 37 | import com.oracle.truffle.api.interop.TruffleObject; 38 | import com.oracle.truffle.api.library.ExportLibrary; 39 | import com.oracle.truffle.api.library.ExportMessage; 40 | 41 | @ExportLibrary(InteropLibrary.class) 42 | public final class DeviceList implements TruffleObject, Iterable { 43 | 44 | private final Device[] devices; 45 | 46 | public DeviceList(int numDevices, CUDARuntime runtime) { 47 | devices = new Device[numDevices]; 48 | for (int deviceOrdinal = 0; deviceOrdinal < numDevices; ++deviceOrdinal) { 49 | devices[deviceOrdinal] = new Device(deviceOrdinal, runtime); 50 | } 51 | } 52 | 53 | // Java API 54 | 55 | public Iterator iterator() { 56 | return new Iterator() { 57 | int nextIndex = 0; 58 | 59 | public boolean hasNext() { 60 | return nextIndex < devices.length; 61 | } 62 | 63 | public Device next() { 64 | if (nextIndex < devices.length) { 65 | return devices[nextIndex++]; 66 | } else { 67 | CompilerDirectives.transferToInterpreter(); 68 | throw new NoSuchElementException(); 69 | } 70 | } 71 | }; 72 | } 73 | 74 | public int size() { 75 | return devices.length; 76 | } 77 | 78 | public Device getDevice(int deviceOrdinal) { 79 | if ((deviceOrdinal < 0) || (deviceOrdinal >= devices.length)) { 80 | CompilerDirectives.transferToInterpreter(); 81 | throw new IndexOutOfBoundsException(); 82 | } 83 | return devices[deviceOrdinal]; 84 | } 85 | 86 | @Override 87 | public String toString() { 88 | boolean notFirst = false; 89 | StringBuffer buf = new StringBuffer("["); 90 | for (Device device : devices) { 91 | if (notFirst) { 92 | buf.append(", "); 93 | } 94 | buf.append(device.toString()); 95 | notFirst = true; 96 | } 97 | buf.append(']'); 98 | return buf.toString(); 99 | } 100 | 101 | // Implementation of Truffle API 102 | 103 | @ExportMessage 104 | @SuppressWarnings("static-method") 105 | boolean hasArrayElements() { 106 | return true; 107 | } 108 | 109 | @ExportMessage 110 | public long getArraySize() { 111 | return devices.length; 112 | } 113 | 114 | @ExportMessage 115 | boolean isArrayElementReadable(long index) { 116 | return index >= 0 && index < devices.length; 117 | } 118 | 119 | @ExportMessage 120 | Object readArrayElement(long index) throws InvalidArrayIndexException { 121 | if ((index < 0) || (index >= devices.length)) { 122 | CompilerDirectives.transferToInterpreter(); 123 | throw InvalidArrayIndexException.create(index); 124 | } 125 | return devices[(int) index]; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/DriverAPIErrorMessages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import java.util.EnumSet; 32 | import java.util.HashMap; 33 | 34 | public final class DriverAPIErrorMessages { 35 | 36 | private static final HashMap codeMap = new HashMap<>(); 37 | 38 | static { 39 | EnumSet.allOf(ErrorCode.class).forEach(code -> codeMap.put(code.getErrorCode(), code.name())); 40 | } 41 | 42 | public static String getString(int errorCode) { 43 | String str = codeMap.get(errorCode); 44 | if (str == null) { 45 | return "unknown error code (" + errorCode + ")"; 46 | } 47 | return str; 48 | } 49 | 50 | /** 51 | * Error codes from cuda.h of the CUDA Runtime API. 52 | */ 53 | private enum ErrorCode { 54 | CUDA_SUCCESS(0), 55 | CUDA_ERROR_INVALID_VALUE(1), 56 | CUDA_ERROR_OUT_OF_MEMORY(2), 57 | CUDA_ERROR_NOT_INITIALIZED(3), 58 | CUDA_ERROR_DEINITIALIZED(4), 59 | CUDA_ERROR_PROFILER_DISABLED(5), 60 | CUDA_ERROR_PROFILER_NOT_INITIALIZED(6), 61 | CUDA_ERROR_PROFILER_ALREADY_STARTED(7), 62 | CUDA_ERROR_PROFILER_ALREADY_STOPPED(8), 63 | CUDA_ERROR_NO_DEVICE(100), 64 | CUDA_ERROR_INVALID_DEVICE(101), 65 | CUDA_ERROR_INVALID_IMAGE(200), 66 | CUDA_ERROR_INVALID_CONTEXT(201), 67 | CUDA_ERROR_CONTEXT_ALREADY_CURRENT(202), 68 | CUDA_ERROR_MAP_FAILED(205), 69 | CUDA_ERROR_UNMAP_FAILED(206), 70 | CUDA_ERROR_ARRAY_IS_MAPPED(207), 71 | CUDA_ERROR_ALREADY_MAPPED(208), 72 | CUDA_ERROR_NO_BINARY_FOR_GPU(209), 73 | CUDA_ERROR_ALREADY_ACQUIRED(210), 74 | CUDA_ERROR_NOT_MAPPED(211), 75 | CUDA_ERROR_NOT_MAPPED_AS_ARRAY(212), 76 | CUDA_ERROR_NOT_MAPPED_AS_POINTER(213), 77 | CUDA_ERROR_ECC_UNCORRECTABLE(214), 78 | CUDA_ERROR_UNSUPPORTED_LIMIT(215), 79 | CUDA_ERROR_CONTEXT_ALREADY_IN_USE(216), 80 | CUDA_ERROR_PEER_ACCESS_UNSUPPORTED(217), 81 | CUDA_ERROR_INVALID_PTX(218), 82 | CUDA_ERROR_INVALID_GRAPHICS_CONTEXT(219), 83 | CUDA_ERROR_NVLINK_UNCORRECTABLE(220), 84 | CUDA_ERROR_JIT_COMPILER_NOT_FOUND(221), 85 | CUDA_ERROR_INVALID_SOURCE(300), 86 | CUDA_ERROR_FILE_NOT_FOUND(301), 87 | CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND(302), 88 | CUDA_ERROR_SHARED_OBJECT_INIT_FAILED(303), 89 | CUDA_ERROR_OPERATING_SYSTEM(304), 90 | CUDA_ERROR_INVALID_HANDLE(400), 91 | CUDA_ERROR_NOT_FOUND(500), 92 | CUDA_ERROR_NOT_READY(600), 93 | CUDA_ERROR_ILLEGAL_ADDRESS(700), 94 | CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES(701), 95 | CUDA_ERROR_LAUNCH_TIMEOUT(702), 96 | CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING(703), 97 | CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED(704), 98 | CUDA_ERROR_PEER_ACCESS_NOT_ENABLED(705), 99 | CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE(708), 100 | CUDA_ERROR_CONTEXT_IS_DESTROYED(709), 101 | CUDA_ERROR_ASSERT(710), 102 | CUDA_ERROR_TOO_MANY_PEERS(711), 103 | CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED(712), 104 | CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED(713), 105 | CUDA_ERROR_HARDWARE_STACK_ERROR(714), 106 | CUDA_ERROR_ILLEGAL_INSTRUCTION(715), 107 | CUDA_ERROR_MISALIGNED_ADDRESS(716), 108 | CUDA_ERROR_INVALID_ADDRESS_SPACE(717), 109 | CUDA_ERROR_INVALID_PC(718), 110 | CUDA_ERROR_LAUNCH_FAILED(719), 111 | CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE(720), 112 | CUDA_ERROR_NOT_PERMITTED(800), 113 | CUDA_ERROR_NOT_SUPPORTED(801), 114 | CUDA_ERROR_UNKNOWN(999); 115 | 116 | private final int errorCode; 117 | 118 | ErrorCode(int errorCode) { 119 | this.errorCode = errorCode; 120 | } 121 | 122 | public int getErrorCode() { 123 | return errorCode; 124 | } 125 | 126 | @Override 127 | public String toString() { 128 | return name() + "(" + errorCode + ")"; 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/KernelConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import java.util.Arrays; 32 | import java.util.Objects; 33 | 34 | import com.oracle.truffle.api.CompilerAsserts; 35 | 36 | public final class KernelConfig { 37 | private final Dim3 gridSize; 38 | private final Dim3 blockSize; 39 | private final int dynamicSharedMemoryBytes; 40 | 41 | public KernelConfig(int numBlocks, int numThreadsPerBlock) { 42 | gridSize = new Dim3(numBlocks); 43 | blockSize = new Dim3(numThreadsPerBlock); 44 | dynamicSharedMemoryBytes = 0; 45 | } 46 | 47 | public KernelConfig(Dim3 gridSize, Dim3 blockSize) { 48 | this.gridSize = gridSize; 49 | this.blockSize = blockSize; 50 | this.dynamicSharedMemoryBytes = 0; 51 | } 52 | 53 | public KernelConfig(Dim3 gridSize, Dim3 blockSize, int sharedMemoryBytes) { 54 | this.gridSize = gridSize; 55 | this.blockSize = blockSize; 56 | this.dynamicSharedMemoryBytes = sharedMemoryBytes; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "KernelConfig(gridSize=" + gridSize + ", blockSize=" + blockSize + 62 | ", sharedMemoryBytes=" + dynamicSharedMemoryBytes + ", stream=" + getStream() + ')'; 63 | } 64 | 65 | public Dim3 getGridSize() { 66 | return gridSize; 67 | } 68 | 69 | public Dim3 getBlockSize() { 70 | return blockSize; 71 | } 72 | 73 | public int getDynamicSharedMemoryBytes() { 74 | return dynamicSharedMemoryBytes; 75 | } 76 | 77 | @SuppressWarnings("static-method") 78 | public int getStream() { 79 | return 0; // default stream 80 | } 81 | 82 | @Override 83 | public boolean equals(Object o) { 84 | if (this == o) { 85 | return true; 86 | } 87 | if (o == null || getClass() != o.getClass()) { 88 | return false; 89 | } 90 | KernelConfig that = (KernelConfig) o; 91 | return dynamicSharedMemoryBytes == that.dynamicSharedMemoryBytes && 92 | getStream() == that.getStream() && 93 | gridSize.equals(that.gridSize) && 94 | blockSize.equals(that.blockSize); 95 | } 96 | 97 | @Override 98 | public int hashCode() { 99 | return Objects.hash(gridSize, blockSize, dynamicSharedMemoryBytes, getStream()); 100 | } 101 | } 102 | 103 | final class Dim3 { 104 | private final int[] dims = new int[3]; 105 | 106 | Dim3(int x) { 107 | dims[0] = x; 108 | dims[1] = 1; 109 | dims[2] = 1; 110 | } 111 | 112 | Dim3(int x, int y) { 113 | dims[0] = x; 114 | dims[1] = y; 115 | dims[2] = 1; 116 | } 117 | 118 | Dim3(int x, int y, int z) { 119 | dims[0] = x; 120 | dims[1] = y; 121 | dims[2] = z; 122 | } 123 | 124 | public int getX() { 125 | return dims[0]; 126 | } 127 | 128 | public int getY() { 129 | return dims[1]; 130 | } 131 | 132 | public int getZ() { 133 | return dims[2]; 134 | } 135 | 136 | @Override 137 | public String toString() { 138 | CompilerAsserts.neverPartOfCompilation(); 139 | return "(" + dims[0] + ", " + dims[1] + ", " + dims[2] + ")"; 140 | } 141 | 142 | @Override 143 | public boolean equals(Object o) { 144 | if (this == o) { 145 | return true; 146 | } 147 | if (o == null || getClass() != o.getClass()) { 148 | return false; 149 | } 150 | Dim3 dim3 = (Dim3) o; 151 | return Arrays.equals(dims, dim3.dims); 152 | } 153 | 154 | @Override 155 | public int hashCode() { 156 | return Arrays.hashCode(dims); 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/LazyKernel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import com.nvidia.grcuda.KernelBinding; 32 | 33 | import com.oracle.truffle.api.interop.ArityException; 34 | import com.oracle.truffle.api.interop.InteropLibrary; 35 | import com.oracle.truffle.api.interop.TruffleObject; 36 | import com.oracle.truffle.api.interop.UnknownIdentifierException; 37 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 38 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 39 | import com.oracle.truffle.api.library.CachedLibrary; 40 | import com.oracle.truffle.api.library.ExportLibrary; 41 | import com.oracle.truffle.api.library.ExportMessage; 42 | 43 | @ExportLibrary(InteropLibrary.class) 44 | public final class LazyKernel implements TruffleObject { 45 | public static final InteropLibrary INTEROP = InteropLibrary.getFactory().getUncached(); 46 | 47 | private final KernelBinding binding; 48 | private final CUDARuntime cudaRuntime; 49 | private Kernel kernel; 50 | 51 | public LazyKernel(KernelBinding binding, CUDARuntime runtime) { 52 | this.binding = binding; 53 | this.cudaRuntime = runtime; 54 | } 55 | 56 | public String getKernelName() { 57 | return binding.getName(); 58 | } 59 | 60 | @ExportMessage 61 | @SuppressWarnings("static-method") 62 | boolean hasMembers() { 63 | return true; 64 | } 65 | 66 | @ExportMessage 67 | Object getMembers(boolean includeInternal) { 68 | assertKernelLoaded(); 69 | return kernel.getMembers(includeInternal); 70 | } 71 | 72 | @ExportMessage 73 | @SuppressWarnings("static-method") 74 | boolean isMemberReadable(String member) { 75 | assertKernelLoaded(); 76 | return kernel.isMemberReadable(member); 77 | } 78 | 79 | @ExportMessage 80 | Object readMember(String member) throws UnsupportedMessageException, UnknownIdentifierException { 81 | assertKernelLoaded(); 82 | return INTEROP.readMember(kernel, member); 83 | } 84 | 85 | @ExportMessage 86 | @SuppressWarnings("static-method") 87 | boolean isExecutable() { 88 | return true; 89 | } 90 | 91 | @ExportMessage 92 | Object execute(Object[] arguments, 93 | @CachedLibrary(limit = "3") InteropLibrary gridSizeAccess, 94 | @CachedLibrary(limit = "3") InteropLibrary gridSizeElementAccess, 95 | @CachedLibrary(limit = "3") InteropLibrary blockSizeAccess, 96 | @CachedLibrary(limit = "3") InteropLibrary blockSizeElementAccess, 97 | @CachedLibrary(limit = "3") InteropLibrary sharedMemoryAccess) throws UnsupportedTypeException, ArityException { 98 | assertKernelLoaded(); 99 | return kernel.execute(arguments, gridSizeAccess, gridSizeElementAccess, blockSizeAccess, blockSizeElementAccess, sharedMemoryAccess); 100 | } 101 | 102 | private void assertKernelLoaded() { 103 | synchronized (this) { 104 | if (kernel == null) { 105 | kernel = cudaRuntime.loadKernel(binding); 106 | assert kernel != null : "Loaded kernel non-null"; 107 | } 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/LittleEndianNativeArrayView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import com.nvidia.grcuda.Type; 32 | import sun.misc.Unsafe; 33 | 34 | /** 35 | * A non-owning view over native memory provided. No bounds checks are performed. 36 | */ 37 | public class LittleEndianNativeArrayView { 38 | 39 | private static final Unsafe unsafe = UnsafeHelper.getUnsafe(); 40 | private final long startAddress; 41 | private final long sizeInBytes; 42 | 43 | public void setByte(long index, byte value) { 44 | unsafe.putByte(startAddress + index * Type.CHAR.getSizeBytes(), value); 45 | } 46 | 47 | public void setChar(long index, char value) { 48 | unsafe.putChar(startAddress + index * Type.SINT16.getSizeBytes(), value); 49 | } 50 | 51 | public void setShort(long index, short value) { 52 | unsafe.putShort(startAddress + index * Type.SINT16.getSizeBytes(), value); 53 | } 54 | 55 | public void setInt(long index, int value) { 56 | unsafe.putInt(startAddress + index * Type.SINT32.getSizeBytes(), value); 57 | } 58 | 59 | public void setLong(long index, long value) { 60 | unsafe.putLong(startAddress + index * Type.SINT64.getSizeBytes(), value); 61 | } 62 | 63 | public void setFloat(long index, float value) { 64 | unsafe.putFloat(startAddress + index * Type.FLOAT.getSizeBytes(), value); 65 | } 66 | 67 | public void setDouble(long index, double value) { 68 | unsafe.putDouble(startAddress + index * Type.DOUBLE.getSizeBytes(), value); 69 | } 70 | 71 | public byte getByte(long index) { 72 | return unsafe.getByte(startAddress + index * Type.CHAR.getSizeBytes()); 73 | } 74 | 75 | public char getChar(long index) { 76 | return unsafe.getChar(startAddress + index * Type.CHAR.getSizeBytes()); 77 | } 78 | 79 | public short getShort(long index) { 80 | return unsafe.getShort(startAddress + index * Type.SINT32.getSizeBytes()); 81 | } 82 | 83 | public int getInt(long index) { 84 | return unsafe.getInt(startAddress + index * Type.SINT32.getSizeBytes()); 85 | } 86 | 87 | public long getLong(long index) { 88 | return unsafe.getLong(startAddress + index * Type.SINT64.getSizeBytes()); 89 | } 90 | 91 | public float getFloat(long index) { 92 | return unsafe.getFloat(startAddress + index * Type.FLOAT.getSizeBytes()); 93 | } 94 | 95 | public double getDouble(long index) { 96 | return unsafe.getDouble(startAddress + index * Type.DOUBLE.getSizeBytes()); 97 | } 98 | 99 | public long getStartAddress() { 100 | return startAddress; 101 | } 102 | 103 | public long getSizeInBytes() { 104 | return sizeInBytes; 105 | } 106 | 107 | @Override 108 | public String toString() { 109 | return String.format("LittleEndianNativearrayView(startAddress=0x%016x, sizeInBytes=%d)", 110 | startAddress, sizeInBytes); 111 | } 112 | 113 | LittleEndianNativeArrayView(long startAddress, long sizeInBytes) { 114 | this.startAddress = startAddress; 115 | this.sizeInBytes = sizeInBytes; 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/NVRTCException.java: -------------------------------------------------------------------------------- 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.gpu; 29 | 30 | import com.oracle.truffle.api.TruffleException; 31 | import com.oracle.truffle.api.nodes.Node; 32 | 33 | public class NVRTCException extends RuntimeException implements TruffleException { 34 | 35 | private static final long serialVersionUID = 7687673079396178282L; 36 | 37 | public NVRTCException(int errorCode, String functionName) { 38 | super("NVRTC error " + errorCode + " in " + functionName); 39 | } 40 | 41 | public NVRTCException(int errorCode, String message, String functionName) { 42 | super(message + '(' + errorCode + ") in " + functionName); 43 | } 44 | 45 | @Override 46 | public Node getLocation() { 47 | // null = location not available 48 | return null; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/gpu/OffheapMemory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.gpu; 30 | 31 | import java.lang.reflect.Field; 32 | import sun.misc.Unsafe; 33 | 34 | public final class OffheapMemory implements AutoCloseable { 35 | 36 | private static final Unsafe unsafe; 37 | private final long numBytes; 38 | private long address; 39 | 40 | public OffheapMemory(long numBytes) { 41 | this.numBytes = numBytes; 42 | this.address = unsafe.allocateMemory(numBytes); 43 | } 44 | 45 | public void freeMemory() { 46 | if (address != 0) { 47 | unsafe.freeMemory(address); 48 | address = 0; 49 | } 50 | } 51 | 52 | public long getSizeBytes() { 53 | return numBytes; 54 | } 55 | 56 | public long getPointer() { 57 | return address; 58 | } 59 | 60 | public LittleEndianNativeArrayView getLittleEndianView() { 61 | return new LittleEndianNativeArrayView(address, numBytes); 62 | } 63 | 64 | public void close() { 65 | freeMemory(); 66 | } 67 | 68 | static { 69 | try { 70 | Field f = Unsafe.class.getDeclaredField("theUnsafe"); 71 | f.setAccessible(true); 72 | unsafe = (Unsafe) f.get(null); 73 | } catch (NoSuchFieldException | IllegalAccessException e) { 74 | // this needs to be a RuntimeException since it is raised during static initialization 75 | throw new RuntimeException(e); 76 | } 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/ArithmeticNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.nodes; 30 | 31 | import com.nvidia.grcuda.GrCUDAException; 32 | import com.nvidia.grcuda.GrCUDAInternalException; 33 | import com.oracle.truffle.api.CompilerDirectives; 34 | import com.oracle.truffle.api.frame.VirtualFrame; 35 | 36 | public final class ArithmeticNode extends BinaryNode { 37 | 38 | private final Operation operation; 39 | 40 | public enum Operation { 41 | ADD, 42 | SUBTRACT, 43 | MULTIPLY, 44 | DIVIDE, 45 | MODULO 46 | } 47 | 48 | public ArithmeticNode(Operation operation, ExpressionNode leftNode, 49 | ExpressionNode rightNode) { 50 | super(leftNode, rightNode); 51 | this.operation = operation; 52 | } 53 | 54 | @Override 55 | public Object execute(VirtualFrame frame) { 56 | Object left = leftNode.execute(frame); 57 | Object right = rightNode.execute(frame); 58 | if (!(left instanceof Number) || !(right instanceof Number)) { 59 | CompilerDirectives.transferToInterpreter(); 60 | throw new GrCUDAException("operation expects integer types", this); 61 | } 62 | int leftInt = ((Number) left).intValue(); 63 | int rightInt = ((Number) right).intValue(); 64 | 65 | switch (operation) { 66 | case ADD: 67 | return leftInt + rightInt; 68 | case SUBTRACT: 69 | return leftInt - rightInt; 70 | case MULTIPLY: 71 | return leftInt * rightInt; 72 | case DIVIDE: 73 | return leftInt / rightInt; 74 | case MODULO: 75 | return leftInt % rightInt; 76 | } 77 | CompilerDirectives.transferToInterpreter(); 78 | throw new GrCUDAInternalException("fall-through in ArithmeticNode", this); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/ArrayNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.nodes; 30 | 31 | import java.util.ArrayList; 32 | 33 | import com.nvidia.grcuda.DeviceArray; 34 | import com.nvidia.grcuda.Type; 35 | import com.nvidia.grcuda.GrCUDAContext; 36 | import com.nvidia.grcuda.GrCUDAInternalException; 37 | import com.nvidia.grcuda.GrCUDALanguage; 38 | import com.nvidia.grcuda.MultiDimDeviceArray; 39 | import com.nvidia.grcuda.gpu.CUDARuntime; 40 | import com.oracle.truffle.api.CompilerDirectives; 41 | import com.oracle.truffle.api.dsl.CachedContext; 42 | import com.oracle.truffle.api.dsl.Specialization; 43 | import com.oracle.truffle.api.frame.VirtualFrame; 44 | 45 | public abstract class ArrayNode extends ExpressionNode { 46 | 47 | @Children private ExpressionNode[] sizeNodes; 48 | 49 | private final Type elementType; 50 | 51 | ArrayNode(Type elementType, ArrayList sizeNodes) { 52 | this.elementType = elementType; 53 | this.sizeNodes = new ExpressionNode[sizeNodes.size()]; 54 | sizeNodes.toArray(this.sizeNodes); 55 | } 56 | 57 | @Specialization 58 | Object doDefault(VirtualFrame frame, 59 | @CachedContext(GrCUDALanguage.class) GrCUDAContext context) { 60 | final CUDARuntime runtime = context.getCUDARuntime(); 61 | long[] elementsPerDim = new long[sizeNodes.length]; 62 | int dim = 0; 63 | for (ExpressionNode sizeNode : sizeNodes) { 64 | Object size = sizeNode.execute(frame); 65 | if (!(size instanceof Number)) { 66 | CompilerDirectives.transferToInterpreter(); 67 | throw new GrCUDAInternalException("size in dimension " + dim + " must be a number", this); 68 | } 69 | elementsPerDim[dim] = ((Number) size).longValue(); 70 | dim += 1; 71 | } 72 | if (sizeNodes.length == 1) { 73 | return new DeviceArray(runtime, elementsPerDim[0], elementType); 74 | } else { 75 | final boolean columnMajorOrder = false; 76 | return new MultiDimDeviceArray(runtime, elementType, elementsPerDim, columnMajorOrder); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/BinaryNode.java: -------------------------------------------------------------------------------- 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 | 29 | package com.nvidia.grcuda.nodes; 30 | 31 | public abstract class BinaryNode extends ExpressionNode { 32 | @Child protected ExpressionNode leftNode; 33 | @Child protected ExpressionNode rightNode; 34 | 35 | public BinaryNode(ExpressionNode leftChild, ExpressionNode rightChild) { 36 | leftNode = leftChild; 37 | rightNode = rightChild; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/CallNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.nodes; 30 | 31 | import java.util.Optional; 32 | import com.nvidia.grcuda.GrCUDAContext; 33 | import com.nvidia.grcuda.GrCUDAException; 34 | import com.nvidia.grcuda.GrCUDALanguage; 35 | import com.nvidia.grcuda.Namespace; 36 | import com.nvidia.grcuda.functions.Function; 37 | import com.oracle.truffle.api.CompilerDirectives; 38 | import com.oracle.truffle.api.dsl.CachedContext; 39 | import com.oracle.truffle.api.dsl.Specialization; 40 | import com.oracle.truffle.api.frame.VirtualFrame; 41 | import com.oracle.truffle.api.interop.ArityException; 42 | import com.oracle.truffle.api.interop.InteropLibrary; 43 | import com.oracle.truffle.api.interop.UnsupportedMessageException; 44 | import com.oracle.truffle.api.interop.UnsupportedTypeException; 45 | import com.oracle.truffle.api.library.CachedLibrary; 46 | 47 | public abstract class CallNode extends ExpressionNode { 48 | 49 | @Child private IdentifierNode identifier; 50 | @Children private ExpressionNode[] argumentNodes; 51 | 52 | public CallNode(IdentifierNode identifier, ExpressionNode[] argumentNodes) { 53 | this.identifier = identifier; 54 | this.argumentNodes = argumentNodes; 55 | } 56 | 57 | @Specialization 58 | Object doDefault(VirtualFrame frame, 59 | @CachedLibrary(limit = "2") InteropLibrary interop, 60 | @CachedContext(GrCUDALanguage.class) GrCUDAContext context) { 61 | String[] functionName = identifier.getIdentifierName(); 62 | Namespace namespace = context.getRootNamespace(); 63 | Optional maybeFunction = namespace.lookup(functionName); 64 | if (!maybeFunction.isPresent() || !(maybeFunction.get() instanceof Function)) { 65 | CompilerDirectives.transferToInterpreter(); 66 | throw new GrCUDAException("function '" + GrCUDAException.format(functionName) + "' not found", this); 67 | } 68 | Function function = (Function) maybeFunction.get(); 69 | Object[] argumentValues = new Object[argumentNodes.length]; 70 | for (int i = 0; i < argumentNodes.length; i++) { 71 | argumentValues[i] = argumentNodes[i].execute(frame); 72 | } 73 | try { 74 | return interop.execute(function, argumentValues); 75 | } catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) { 76 | CompilerDirectives.transferToInterpreter(); 77 | throw new GrCUDAException(e.getMessage(), this); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/ExpressionNode.java: -------------------------------------------------------------------------------- 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.nodes; 29 | 30 | import com.oracle.truffle.api.frame.VirtualFrame; 31 | import com.oracle.truffle.api.nodes.Node; 32 | import com.oracle.truffle.api.nodes.NodeInfo; 33 | 34 | @NodeInfo(description = "Abstract base node for all expressions") 35 | public abstract class ExpressionNode extends Node { 36 | 37 | public abstract Object execute(VirtualFrame frame); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/GrCUDARootNode.java: -------------------------------------------------------------------------------- 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.nodes; 29 | 30 | import com.nvidia.grcuda.GrCUDALanguage; 31 | import com.oracle.truffle.api.frame.VirtualFrame; 32 | import com.oracle.truffle.api.nodes.RootNode; 33 | 34 | public class GrCUDARootNode extends RootNode { 35 | 36 | @Child private ExpressionNode expression; 37 | 38 | public GrCUDARootNode(GrCUDALanguage language, ExpressionNode expressionNode) { 39 | super(language); 40 | this.expression = expressionNode; 41 | } 42 | 43 | @Override 44 | public Object execute(VirtualFrame frame) { 45 | return expression.execute(frame); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/IdentifierNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.nodes; 30 | 31 | import java.util.Optional; 32 | 33 | import com.nvidia.grcuda.GrCUDAContext; 34 | import com.nvidia.grcuda.GrCUDAException; 35 | import com.nvidia.grcuda.GrCUDALanguage; 36 | import com.nvidia.grcuda.Namespace; 37 | import com.oracle.truffle.api.CompilerDirectives; 38 | import com.oracle.truffle.api.dsl.CachedContext; 39 | import com.oracle.truffle.api.dsl.Specialization; 40 | 41 | public abstract class IdentifierNode extends ExpressionNode { 42 | 43 | private final String[] identifierName; 44 | 45 | public IdentifierNode(String... identifierName) { 46 | this.identifierName = identifierName; 47 | } 48 | 49 | public String[] getIdentifierName() { 50 | return identifierName; 51 | } 52 | 53 | @Specialization 54 | protected Object doDefault( 55 | @CachedContext(GrCUDALanguage.class) GrCUDAContext context) { 56 | Namespace rootNamespace = context.getRootNamespace(); 57 | Optional maybeFunction = rootNamespace.lookup(identifierName); 58 | if (!maybeFunction.isPresent()) { 59 | CompilerDirectives.transferToInterpreter(); 60 | throw new GrCUDAException("Function or namespace '" + GrCUDAException.format(identifierName) + "' not found", this); 61 | } 62 | return maybeFunction.get(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/IntegerLiteral.java: -------------------------------------------------------------------------------- 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.nodes; 29 | 30 | import com.oracle.truffle.api.frame.VirtualFrame; 31 | 32 | public class IntegerLiteral extends ExpressionNode { 33 | private final int value; 34 | 35 | public IntegerLiteral(int value) { 36 | this.value = value; 37 | } 38 | 39 | public int getValue() { 40 | return value; 41 | } 42 | 43 | @Override 44 | public Object execute(VirtualFrame frame) { 45 | return value; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/RootNamespaceNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.nodes; 30 | 31 | import com.nvidia.grcuda.GrCUDAContext; 32 | import com.nvidia.grcuda.GrCUDALanguage; 33 | import com.oracle.truffle.api.dsl.CachedContext; 34 | import com.oracle.truffle.api.dsl.Specialization; 35 | 36 | public abstract class RootNamespaceNode extends ExpressionNode { 37 | 38 | @Specialization 39 | protected Object doDefault( 40 | @CachedContext(GrCUDALanguage.class) GrCUDAContext context) { 41 | return context.getRootNamespace(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/nodes/StringLiteral.java: -------------------------------------------------------------------------------- 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.nodes; 29 | 30 | import com.oracle.truffle.api.frame.VirtualFrame; 31 | 32 | public class StringLiteral extends ExpressionNode { 33 | 34 | private final String value; 35 | 36 | public StringLiteral(String stringValue) { 37 | this.value = stringValue; 38 | } 39 | 40 | public String getValue() { 41 | return value; 42 | } 43 | 44 | @Override 45 | public Object execute(VirtualFrame frame) { 46 | return value; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/GrCUDAParserException.java: -------------------------------------------------------------------------------- 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.parser; 29 | 30 | import com.oracle.truffle.api.TruffleException; 31 | import com.oracle.truffle.api.nodes.Node; 32 | import com.oracle.truffle.api.source.Source; 33 | import com.oracle.truffle.api.source.SourceSection; 34 | 35 | public class GrCUDAParserException extends RuntimeException implements TruffleException { 36 | 37 | private static final long serialVersionUID = -6653370806148433373L; 38 | private final Source source; 39 | private final int line; 40 | private final int column; 41 | private final int length; 42 | 43 | public GrCUDAParserException(String message, Source source, int line, int charPositionInLine, int length) { 44 | super(message); 45 | this.source = source; 46 | this.line = line; 47 | this.column = charPositionInLine; 48 | this.length = length; 49 | } 50 | 51 | @Override 52 | public SourceSection getSourceLocation() { 53 | return source.createSection(line, column, length); 54 | } 55 | 56 | @Override 57 | public Node getLocation() { 58 | return null; 59 | } 60 | 61 | @Override 62 | public boolean isSyntaxError() { 63 | return true; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/NIDLParserException.java: -------------------------------------------------------------------------------- 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.parser; 29 | 30 | import com.oracle.truffle.api.TruffleException; 31 | import com.oracle.truffle.api.nodes.Node; 32 | 33 | public class NIDLParserException extends RuntimeException implements TruffleException { 34 | 35 | private static final long serialVersionUID = -7520277230665801341L; 36 | private final String message; 37 | private final String filename; 38 | private final int line; 39 | private final int column; 40 | 41 | public NIDLParserException(String message, String filename, int line, int charPositionInLine) { 42 | super(message); 43 | this.message = message; 44 | this.filename = filename; 45 | this.line = line; 46 | this.column = charPositionInLine; 47 | } 48 | 49 | @Override 50 | public String getMessage() { 51 | return "NIDL parse error: [" + filename + " " + line + ":" + column + "] " + message; 52 | } 53 | 54 | @Override 55 | public Node getLocation() { 56 | // null = location not available 57 | return null; 58 | } 59 | 60 | @Override 61 | public boolean isSyntaxError() { 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/NodeFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.parser; 30 | 31 | import java.util.ArrayList; 32 | import org.antlr.v4.runtime.Token; 33 | import com.nvidia.grcuda.Type; 34 | import com.nvidia.grcuda.GrCUDAInternalException; 35 | import com.nvidia.grcuda.TypeException; 36 | import com.nvidia.grcuda.nodes.ArithmeticNode; 37 | import com.nvidia.grcuda.nodes.ArrayNode; 38 | import com.nvidia.grcuda.nodes.ArrayNodeGen; 39 | import com.nvidia.grcuda.nodes.CallNode; 40 | import com.nvidia.grcuda.nodes.CallNodeGen; 41 | import com.nvidia.grcuda.nodes.ExpressionNode; 42 | import com.nvidia.grcuda.nodes.IdentifierNode; 43 | import com.nvidia.grcuda.nodes.IdentifierNodeGen; 44 | import com.nvidia.grcuda.nodes.IntegerLiteral; 45 | import com.nvidia.grcuda.nodes.StringLiteral; 46 | import com.oracle.truffle.api.source.Source; 47 | 48 | public class NodeFactory { 49 | 50 | private final Source source; 51 | 52 | public NodeFactory(Source source) { 53 | this.source = source; 54 | } 55 | 56 | public ArrayNode createArrayNode(Token typeToken, ArrayList sizeNodes) throws GrCUDAParserException { 57 | return ArrayNodeGen.create(lookupType(typeToken), sizeNodes); 58 | } 59 | 60 | public ArithmeticNode createBinary(Token opToken, ExpressionNode leftNode, ExpressionNode rightNode) { 61 | final ArithmeticNode result; 62 | switch (opToken.getText()) { 63 | case "+": 64 | result = new ArithmeticNode(ArithmeticNode.Operation.ADD, leftNode, rightNode); 65 | break; 66 | case "-": 67 | result = new ArithmeticNode(ArithmeticNode.Operation.SUBTRACT, leftNode, rightNode); 68 | break; 69 | case "*": 70 | result = new ArithmeticNode(ArithmeticNode.Operation.MULTIPLY, leftNode, rightNode); 71 | break; 72 | case "/": 73 | result = new ArithmeticNode(ArithmeticNode.Operation.DIVIDE, leftNode, rightNode); 74 | break; 75 | case "%": 76 | result = new ArithmeticNode(ArithmeticNode.Operation.MODULO, leftNode, rightNode); 77 | break; 78 | default: 79 | // should not happen due to lexer 80 | throw new GrCUDAInternalException("unexpected operation: " + opToken.getText()); 81 | } 82 | return result; 83 | } 84 | 85 | public CallNode createCallNode(Token identifierToken, ExpressionNode[] arguments) { 86 | return CallNodeGen.create(createIdentifier(identifierToken), arguments); 87 | } 88 | 89 | public IdentifierNode createIdentifier(Token identifierToken) { 90 | return IdentifierNodeGen.create(identifierToken.getText()); 91 | } 92 | 93 | public IdentifierNode createIdentifierInNamespace(Token identifierToken, Token namespaceToken) { 94 | return IdentifierNodeGen.create(namespaceToken.getText(), identifierToken.getText()); 95 | 96 | } 97 | 98 | public IntegerLiteral createIntegerLiteral(Token literalToken) { 99 | try { 100 | return new IntegerLiteral(Integer.parseInt(literalToken.getText())); 101 | } catch (NumberFormatException e) { 102 | // ignore parse error cannot happen due to regular expression in lexer 103 | throw new GrCUDAInternalException("unable to parse integer literal " + e.getMessage()); 104 | } 105 | } 106 | 107 | public StringLiteral createStringLiteral(Token literalToken) { 108 | String stringValue = literalToken.getText(); 109 | // Skip double-quotes at the beginning and at the end: "mystring" 110 | assert stringValue.startsWith("\"") && stringValue.endsWith("\""); 111 | return new StringLiteral(stringValue.substring(1, stringValue.length() - 1)); 112 | } 113 | 114 | private Type lookupType(Token typeToken) { 115 | try { 116 | return Type.fromGrCUDATypeString(typeToken.getText()); 117 | } catch (TypeException e) { 118 | throw new GrCUDAParserException(e.getMessage(), 119 | source, 120 | typeToken.getLine(), typeToken.getCharPositionInLine(), 121 | Math.max(typeToken.getStopIndex() - typeToken.getStartIndex(), 0)); 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /projects/com.nvidia.grcuda/src/com/nvidia/grcuda/parser/ParserAntlr.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. 3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of NVIDIA CORPORATION nor the names of its 14 | * contributors may be used to endorse or promote products derived 15 | * from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 25 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package com.nvidia.grcuda.parser; 30 | 31 | import com.nvidia.grcuda.nodes.ExpressionNode; 32 | import com.nvidia.grcuda.parser.antlr.GrCUDAParser; 33 | import com.oracle.truffle.api.CompilerAsserts; 34 | import com.oracle.truffle.api.source.Source; 35 | 36 | public final class ParserAntlr { 37 | 38 | public ParserAntlr() { 39 | } 40 | 41 | @SuppressWarnings("static-method") 42 | public ExpressionNode parse(Source source) throws GrCUDAParserException { 43 | CompilerAsserts.neverPartOfCompilation(); 44 | return GrCUDAParser.parseCUDA(source); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tensorrt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | project(libtrt LANGUAGES C CXX) 28 | cmake_minimum_required(VERSION 3.10) 29 | 30 | SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 31 | find_package(TensorRT REQUIRED) 32 | 33 | find_package(CUDA REQUIRED) 34 | 35 | message(STATUS "TensorRT Headers: ${TENSORRT_INCLUDE_DIR}") 36 | message(STATUS "TensorRT Library: ${TENSORRT_LIBRARY}") 37 | message(STATUS "Found CUDA ${CUDA_VERSION_STRING}") 38 | 39 | 40 | add_library(trt SHARED src/trt.cpp) 41 | target_compile_features(trt PUBLIC cxx_std_14) 42 | target_include_directories(trt PUBLIC include ${TENSORRT_INCLUDE_DIR} ${CUDA_INCLUDE_DIRS}) 43 | target_link_libraries(trt PRIVATE TensorRT::nvinfer ${CUDA_LIBRARIES}) 44 | 45 | 46 | add_executable(libtrt_load_and_sample app/libtrt_load_and_sample.c) 47 | target_link_libraries(libtrt_load_and_sample PUBLIC trt ${CUDA_LIBRARIES}) 48 | 49 | -------------------------------------------------------------------------------- /tensorrt/README.md: -------------------------------------------------------------------------------- 1 | # grCUDA and TensorRT 2 | 3 | This directory contains a wrapper library `libtrt.so` for TensorRT. 4 | It simplifies the use of the TensorRT inference library. 5 | 6 | ## Build libtrt 7 | 8 | Build that the grCUDA wrapper library `libtrt` for TensorRT. 9 | 10 | ```console 11 | $ cd ../tensorrt 12 | $ mkdir build 13 | $ cd build 14 | $ cmake .. -DTENSORRT_DIR=/usr/local/TensorRT-7.0.0.11/ 15 | $ make -j 16 | ``` 17 | 18 | You can validate the `libtrt` wrapper library using the C test 19 | application in `app/libtrt_load_and_sample.c`. 20 | 21 | If not already downloaded, download some test images from the MNIST data set 22 | into the `data` directory. Run the script `download_mnist_test_digits.py` in the 23 | `data` directory to download the MNIST data set and extract the first occurrence 24 | of the digits zero to nine in the test set. 25 | This script stores the images in PGM format as `0.pgm` to `9.pgm`. 26 | 27 | ```console 28 | $ cd tensorrt/build 29 | $ ./libtrt_load_and_sample ../../examples/tensorrt/models/lenet5.engine \ 30 | ../../examples/tensorrt/data/4.pgm 31 | creating inference runtime... 32 | Deserialize required 1135802 microseconds. 33 | input layer conv2d_input has binding index 0 34 | output layer dense_2/Softmax has binding index 1 35 | max batch size: 1 36 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 37 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 38 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 39 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 40 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 41 | @@@@@@@@@@@@@@@@@@@@@ @@@@@@ 42 | @@@@@@@@@@@@@@@@@@@@@ @@@@@@ 43 | @@@@@ @@@@@@@@@@@@@@ @@@@@@ 44 | @@@@ @@@@@@@@@@@@@@ @@@@@@ 45 | @@@@ @@@@@@@@@@@@@ @@@@@@@ 46 | @@@@ @@@@@@@@@@@@@ @@@@@@@ 47 | @@@@ @@@@@@@@@@@@@ @@@@@@@ 48 | @@@ @@@@@@@@@@@@@ @@@@@@@ 49 | @@@ @@@@@@@@@@@@ @@@@@@@@ 50 | @@@ @@@@@@@ @@@@@@@@ 51 | @@@ @@@ @@@@@@@@ 52 | @@@@@ @@@@@@@@ @@@@@@@@ 53 | @@@@@@@@@@@@@@@@@ @@@@@@@@ 54 | @@@@@@@@@@@@@@@@@ @@@@@@@@@ 55 | @@@@@@@@@@@@@@@@@ @@@@@@@@@ 56 | @@@@@@@@@@@@@@@@@ @@@@@@@@@ 57 | @@@@@@@@@@@@@@@@@ @@@@@@@@@ 58 | @@@@@@@@@@@@@@@@@ @@@@@@@@ 59 | @@@@@@@@@@@@@@@@@ @@@@@@@@ 60 | @@@@@@@@@@@@@@@@@@ @@@@@@@@ 61 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 62 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 63 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 64 | 0 0.00000 65 | 1 0.00001 66 | 2 0.00003 67 | 3 0.00001 68 | 4 0.99205 69 | 5 0.00000 70 | 6 0.00000 71 | 7 0.00006 72 | 8 0.00006 73 | 9 0.00778 74 | prediction: 4 75 | destroying inference runtime... 76 | ``` 77 | 78 | ## Use libtrt in grCUDA 79 | 80 | ```bash 81 | GRCUDA_JAR="$GRCUDA_BUILD_DIR/mxbuild/dists/jdk1.8/grcuda.jar" 82 | LIBTRT="$GRCUDA_BUILD_DIR/tensorrt/libtrt/build/libtrt.so" 83 | LD_LIBRARY_PATH="/usr/local/TensorRT-7.0.0.11/lib:$LD_LIBRARY_PATH" 84 | 85 | ${GRAALVM_DIR}/bin/node --polyglot --jvm \ 86 | --grcuda.TensorRTEnabled=true \ 87 | --grcuda.TensorRTLibrary=$LIBTRT \ 88 | --vm.Dtruffle.class.path.append=$GRCUDA_JAR \ 89 | tensorrt_example.js data/4.pgm 90 | ``` 91 | 92 | A complete example for Node.js is provided in the [examples directory](../examples/tensorrt/README.md). 93 | 94 | ## Functions in librtr 95 | 96 | ```text 97 | // Inference Runtime 98 | createInferRuntime(): sint32 // returns runtime handle 99 | destroyInferRuntime(rt_handle: sint32): void 100 | 101 | // Engine 102 | deserializeCudaEngine(rt_handle: sint32, engine_file_name: string): sint32 103 | // returns engine handle 104 | getBindingIndex(engine_handle: sint32, name: string): sint32 105 | getMaxBatchSize(engine_handle: sint32): sint32 106 | destroyEngine(engine_handle: sint32): void 107 | 108 | // Execution Context 109 | createExecutionContext(engine_handle: sint32): sint32 110 | // returns context handle 111 | destroyExecutionContext(context_handle: sint32): sint32 112 | 113 | // Enqueue inference for batch 114 | enqueue(context_handle: sint32, batch_size: sint32, 115 | buffers: arraylike): bool 116 | // returns 'true' if successful 117 | ``` 118 | 119 | ## Enqueue Inference Jobs 120 | 121 | Inference jobs are executed asynchronous after being submitted 122 | with `TRT::enqueue`. This function takes a bindings from a binding 123 | index to the respective input or output buffers. 124 | The bindings is specified in an array-like , e.g., 125 | an `Array` in JavaScript, and passed to `enqueue`. 126 | 127 | Example in JavaScript: 128 | 129 | ```javascript 130 | const imageDeviceArray = cu.DeviceArray(...) 131 | const classProbabilitiesDeviceArray = cu.DeviceArray(...) 132 | 133 | const inputIndex = trt.getBindingIndex(engine, 'conv2d_input') 134 | const outputIndex = trt.getBindingIndex(engine, 'dense_2/Softmax') 135 | 136 | const batchSize = 1 137 | const buffers = [] 138 | buffers[inputIndex] = imageDeviceArray 139 | buffers[outputIndex] = classProbabilitiesDeviceArray 140 | trt.enqueue(engine, batchSize, buffers) 141 | ``` 142 | -------------------------------------------------------------------------------- /tensorrt/cmake/FindTensorRT.cmake: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # * Redistributions of source code must retain the above copyright 7 | # notice, this list of conditions and the following disclaimer. 8 | # * Redistributions in binary form must reproduce the above copyright 9 | # notice, this list of conditions and the following disclaimer in the 10 | # documentation and/or other materials provided with the distribution. 11 | # * Neither the name of NVIDIA CORPORATION nor the names of its 12 | # contributors may be used to endorse or promote products derived 13 | # from this software without specific prior written permission. 14 | # 15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | find_path(TENSORRT_INCLUDE_DIR NvInfer.h HINTS ${TENSORRT_DIR} PATH_SUFFIXES include) 28 | find_library(TENSORRT_LIBRARY NAMES nvinfer HINTS ${TENSORRT_DIR} PATH_SUFFIXES lib) 29 | 30 | if (TENSORRT_LIBRARY AND TENSORRT_INCLUDE_DIR) 31 | if (NOT TARGET TensorRT::nvinfer) 32 | add_library(TensorRT::nvinfer UNKNOWN IMPORTED) 33 | set_target_properties(TensorRT::nvinfer PROPERTIES INTERFACE_INCLUDE_DIRECTORY "${TENSORRT_INCLUDE_DIR}") 34 | set_target_properties(TensorRT::nvinfer PROPERTIES 35 | IMPORTED_LINK_INTERFACE_LANGUAGES "C" 36 | IMPORTED_LOCATION "${TENSORRT_LIBRARY}") 37 | endif() 38 | endif() 39 | 40 | include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) 41 | find_package_handle_standard_args(TensorRT 42 | REQUIRED_VARS TENSORRT_LIBRARY TENSORRT_INCLUDE_DIR) 43 | 44 | mark_as_advanced(TENSORRT_INCLUDE_DIR TENSORRT_LIBRARY) 45 | -------------------------------------------------------------------------------- /tensorrt/include/trt.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without 4 | // modification, are permitted provided that the following conditions 5 | // are met: 6 | // * Redistributions of source code must retain the above copyright 7 | // notice, this list of conditions and the following disclaimer. 8 | // * Redistributions in binary form must reproduce the above copyright 9 | // notice, this list of conditions and the following disclaimer in the 10 | // documentation and/or other materials provided with the distribution. 11 | // * Neither the name of NVIDIA CORPORATION nor the names of its 12 | // contributors may be used to endorse or promote products derived 13 | // from this software without specific prior written permission. 14 | // 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 16 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 23 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | #ifndef TRT_H 28 | #define TRT_H 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | // 35 | // libtrt is a C-style wrapper for the most basic functions of TensorRT. 36 | // 37 | 38 | typedef int runtime_handle_t; 39 | typedef int engine_handle_t; 40 | typedef int context_handle_t; 41 | 42 | enum trt_error { 43 | TRT_OK = 0, 44 | TRT_INVALID_HANDLE = -1, 45 | TRT_UNABLE_TO_CREATE_RUNTIME = -2, 46 | TRT_ENGINE_DESERIALIZATION_ERROR = -3, 47 | TRT_ENGINE_FILE_NOT_FOUND = -4 48 | }; 49 | typedef enum trt_error trt_error_t; 50 | 51 | // IRuntime 52 | runtime_handle_t createInferRuntime(); 53 | engine_handle_t deserializeCudaEngine(runtime_handle_t handle, const char *engine_file_name); 54 | trt_error_t destroyInferRuntime(runtime_handle_t handle); 55 | 56 | // ICudaEngine 57 | context_handle_t createExecutionContext(engine_handle_t handle); 58 | int getBindingIndex(engine_handle_t handle, const char *name); 59 | int getMaxBatchSize(engine_handle_t handle); 60 | trt_error_t destroyEngine(engine_handle_t handle); 61 | 62 | // IExecutionContext 63 | int enqueue(context_handle_t handle, int batch_size, void **bindings, cudaStream_t stream, cudaEvent_t *input_consumed); 64 | trt_error_t destroyExecutionContext(context_handle_t handle); 65 | 66 | 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | #endif // TRT_H --------------------------------------------------------------------------------