com.oracle.truffle.api.CompilerDirectives.TruffleBoundary Java Examples

The following examples show how to use com.oracle.truffle.api.CompilerDirectives.TruffleBoundary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NVRuntimeCompiler.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@TruffleBoundary
public NVRTCResult nvrtcCompileProgram(NVRTCProgram program, String... opts) {
    if (opts.length == 0) {
        return nvrtcCompileProgramInternal(program, 0, 0L);
    } else {
        ArrayList<StringObject> optCStrings = new ArrayList<>(opts.length);
        try (UnsafeHelper.PointerArray optCStringArr = new PointerArray(opts.length)) {
            int idx = 0;
            for (String optString : opts) {
                UnsafeHelper.StringObject cString = UnsafeHelper.StringObject.fromJavaString(optString);
                optCStrings.add(cString);
                optCStringArr.setValueAt(idx, cString.getAddress());
            }
            NVRTCResult result = nvrtcCompileProgramInternal(program, opts.length,
                            optCStringArr.getAddress());
            for (UnsafeHelper.StringObject s : optCStrings) {
                s.close();
            }
            return result;
        }
    }
}
 
Example #2
Source File: MapArgObject.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@ExportMessage
@TruffleBoundary
Object execute(Object[] arguments) throws UnsupportedMessageException, UnsupportedTypeException, ArityException {
    if (value instanceof MapArgObjectMember) {
        MapArgObjectMember member = (MapArgObjectMember) value;
        if (MAP.equals(member.name)) {
            checkArity(arguments, 1);
            return new MapArgObject(member.parent).map(arguments[0]);
        } else if (SHRED.equals(member.name)) {
            checkArity(arguments, 0);
            CompilerAsserts.neverPartOfCompilation();
            return new MapArgObject(member.parent).shred();
        } else if (DESCRIBE.equals(member.name)) {
            checkArity(arguments, 0);
            CompilerAsserts.neverPartOfCompilation();
            return new MapArgObject(member.parent).describe();
        } else if (BIND.equals(member.name)) {
            checkArity(arguments, 3);
            return member.parent.bind(arguments[0], arguments[1], arguments[2]);
        }
    }
    CompilerDirectives.transferToInterpreter();
    throw UnsupportedMessageException.create();
}
 
Example #3
Source File: NVRuntimeCompiler.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
@TruffleBoundary
public void close() {
    if (!initialized) {
        nvrtcProgram.close();
        return;
    }
    NVRTCFunction function = NVRTCFunction.NVRTC_DESTROYPROGRAM;
    try {
        Object callable = getSymbol(function);
        Object result = INTEROP.execute(callable, nvrtcProgram.getAddress());
        checkNVRTCReturnCode(result, function.symbolName);
    } catch (InteropException e) {
        throw new GrCUDAInternalException(e);
    } finally {
        nvrtcProgram.close();
    }
}
 
Example #4
Source File: NVRuntimeCompiler.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@TruffleBoundary
public NVRTCProgram createProgram(String source, String programName) {
    NVRTCProgram program = new NVRTCProgram();
    try {
        Object callable = getSymbol(NVRTCFunction.NVRTC_CREATEPROGRAM);
        Object result = INTEROP.execute(callable,
                        program.getAddress(), source, programName,
                        0, // number of headers
                        0, // pointer to headers (NULL if numHeaders == 0)
                        0); // pointer to header names (NULL if numHeaders == 0)
        checkNVRTCReturnCode(result, NVRTCFunction.NVRTC_CREATEPROGRAM.symbolName);
        program.setInitialized();
        return program;
    } catch (InteropException e) {
        program.close();
        throw new GrCUDAInternalException(e);
    }
}
 
Example #5
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@TruffleBoundary
/**
 * Get function handle to kernel in module.
 *
 * @param kernelModule CUmodule containing the kernel function
 * @param kernelName
 * @return native CUfunction function handle
 */
public long cuModuleGetFunction(CUModule kernelModule, String kernelName) {
    try (UnsafeHelper.Integer64Object functionPtr = UnsafeHelper.createInteger64Object()) {
        Object callable = CUDADriverFunction.CU_MODULEGETFUNCTION.getSymbol(this);
        Object result = INTEROP.execute(callable,
                        functionPtr.getAddress(), kernelModule.getModulePointer(), kernelName);
        checkCUReturnCode(result, "cuModuleGetFunction");
        return functionPtr.getValue();
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #6
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@TruffleBoundary
public void cudaMemcpy(long destPointer, long fromPointer, long numBytesToCopy) {
    try {
        Object callable = CUDARuntimeFunction.CUDA_MEMCPY.getSymbol(this);
        if (numBytesToCopy < 0) {
            throw new IllegalArgumentException("requested negative number of bytes to copy " + numBytesToCopy);
        }
        // cudaMemcpyKind from driver_types.h (default: direction of transfer is inferred
        // from the pointer values, uses virtual addressing)
        final long cudaMemcpyDefault = 4;
        Object result = INTEROP.execute(callable, destPointer, fromPointer, numBytesToCopy, cudaMemcpyDefault);
        checkCUDAReturnCode(result, "cudaMemcpy");
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #7
Source File: ShredFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@ExportMessage
@TruffleBoundary
public ShreddedObject execute(Object[] arguments) throws ArityException, UnsupportedTypeException {
    MapFunction.checkArity(arguments, 1);
    return new ShreddedObject(arguments[0]);
}
 
Example #8
Source File: Namespace.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public Optional<Object> lookup(String... path) {
    if (path.length == 0) {
        return Optional.empty();
    }
    return lookup(0, path);
}
 
Example #9
Source File: ArgumentSet.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("static-method")
@ExportMessage
@TruffleBoundary
Object getMembers(@SuppressWarnings("unused") boolean includeInternal) {
    String[] names = new String[nameList.size()];
    int pos = 0;
    for (String name : nameList.getKeys()) {
        names[pos++] = name;
    }
    return new MemberSet(names);
}
 
Example #10
Source File: NVRuntimeCompiler.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public String nvrtcGetLoweredName(NVRTCProgram program, String kernelName) {
    try (UnsafeHelper.PointerObject cString = new UnsafeHelper.PointerObject()) {
        try {
            Object callable = getSymbol(NVRTCFunction.NVRTC_GETLOWEREDNAME);
            Object result = INTEROP.execute(callable, program.getValue(), kernelName, cString.getAddress());
            checkNVRTCReturnCode(result, NVRTCFunction.NVRTC_GETLOWEREDNAME.symbolName);
            return UnsafeHelper.StringObject.getUncheckedZeroTerminatedString(cString.getValueOfPointer());
        } catch (InteropException e) {
            throw new GrCUDAInternalException(e);
        }
    }
}
 
Example #11
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public void cuModuleUnload(CUModule module) {
    try {
        Object callable = CUDADriverFunction.CU_MODULEUNLOAD.getSymbol(this);
        Object result = INTEROP.execute(callable, module.modulePointer);
        checkCUReturnCode(result, "cuModuleUnload");
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #12
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public DeviceMemoryInfo cudaMemGetInfo() {
    final String symbol = "cudaMemGetInfo";
    final String nfiSignature = "(pointer, pointer): sint32";
    try (Integer64Object freeBytes = UnsafeHelper.createInteger64Object();
                    Integer64Object totalBytes = UnsafeHelper.createInteger64Object()) {
        Object callable = getSymbol(CUDA_RUNTIME_LIBRARY_NAME, symbol, nfiSignature);
        Object result = INTEROP.execute(callable, freeBytes.getAddress(), totalBytes.getAddress());
        checkCUDAReturnCode(result, symbol);
        return new DeviceMemoryInfo(freeBytes.getValue(), totalBytes.getValue());
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #13
Source File: HashemObjectType.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@TruffleBoundary
@Specialization(guards = {"!receiver.getShape().isValid()"})
static void updateShape(DynamicObject receiver, String name, Object value) {
    /*
     * Slow path that we do not handle in compiled code. But no need to invalidate compiled
     * code.
     */
    CompilerDirectives.transferToInterpreter();
    receiver.updateShape();
    writeUncached(receiver, name, value);
}
 
Example #14
Source File: UnsafeHelper.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
String getZeroTerminatedString() {
    byte[] bytes = new byte[maxLength];
    int offset = 0;
    while (offset < maxLength) {
        byte b = unsafe.getByte(getAddress() + offset);
        if (b == 0) {
            break;
        }
        bytes[offset] = b;
        offset += 1;
    }
    return new String(bytes, 0, offset, Charset.forName("ISO-8859-1"));
}
 
Example #15
Source File: HashemBigNumber.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@ExportMessage
@TruffleBoundary
short asShort() throws UnsupportedMessageException {
    if (fitsInShort()) {
        return value.shortValue();
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #16
Source File: HashemBigNumber.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@ExportMessage
@TruffleBoundary
byte asByte() throws UnsupportedMessageException {
    if (fitsInByte()) {
        return value.byteValue();
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #17
Source File: GetDeviceFunction.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@TruffleBoundary
public Object call(Object[] arguments) throws UnsupportedTypeException, ArityException {
    checkArgumentLength(arguments, 1);
    int deviceId = expectPositiveInt(arguments[0]);
    return new Device(deviceId, runtime);
}
 
Example #18
Source File: HashemBigNumber.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@ExportMessage
@TruffleBoundary
long asLong() throws UnsupportedMessageException {
    if (fitsInLong()) {
        return value.longValue();
    } else {
        throw UnsupportedMessageException.create();
    }
}
 
Example #19
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@TruffleBoundary
public Object call(CUDARuntime cudaRuntime, Object[] args) throws ArityException, UnsupportedTypeException, InteropException {
    checkArgumentLength(args, 1);
    int device = expectInt(args[0]);
    callSymbol(cudaRuntime, device);
    return NoneValue.get();
}
 
Example #20
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
private void cuCtxDestroy(long ctx) {
    try {
        Object callable = CUDADriverFunction.CU_CTXCREATE.getSymbol(this);
        Object result = INTEROP.execute(callable, ctx);
        checkCUReturnCode(result, "cuCtxDestroy");
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #21
Source File: HashemBigNumber.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
@Override
@TruffleBoundary
public boolean equals(Object obj) {
    if (obj instanceof HashemBigNumber) {
        return value.equals(((HashemBigNumber) obj).getValue());
    }
    return false;
}
 
Example #22
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public GPUPointer cudaMalloc(long numBytes) {
    try (UnsafeHelper.PointerObject outPointer = UnsafeHelper.createPointerObject()) {
        Object callable = CUDARuntimeFunction.CUDA_MALLOC.getSymbol(this);
        Object result = INTEROP.execute(callable, outPointer.getAddress(), numBytes);
        checkCUDAReturnCode(result, "cudaMalloc");
        long addressAllocatedMemory = outPointer.getValueOfPointer();
        return new GPUPointer(addressAllocatedMemory);
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #23
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
private long cuCtxCreate(int flags, int cudevice) {
    try (UnsafeHelper.PointerObject pctx = UnsafeHelper.createPointerObject()) {
        Object callable = CUDADriverFunction.CU_CTXCREATE.getSymbol(this);
        Object result = INTEROP.execute(callable, pctx.getAddress(), flags, cudevice);
        checkCUReturnCode(result, "cuCtxCreate");
        return pctx.getValueOfPointer();
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #24
Source File: CUBLASRegistry.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void registerCUBLASFunctions(Namespace namespace) {
    // Create function wrappers (decorators for all functions except handle con- and
    // destruction)
    for (ExternalFunctionFactory factory : functions) {
        final Function wrapperFunction = new Function(factory.getName()) {

            private Function nfiFunction;

            @Override
            @TruffleBoundary
            protected Object call(Object[] arguments) {
                ensureInitialized();

                Object[] argsWithHandle = new Object[arguments.length + 1];
                System.arraycopy(arguments, 0, argsWithHandle, 1, arguments.length);
                argsWithHandle[0] = cublasHandle;

                try {
                    if (nfiFunction == null) {
                        CompilerDirectives.transferToInterpreterAndInvalidate();
                        nfiFunction = factory.makeFunction(context.getCUDARuntime(), libraryPath, DEFAULT_LIBRARY_HINT);
                    }
                    Object result = INTEROP.execute(nfiFunction, argsWithHandle);
                    context.getCUDARuntime().cudaDeviceSynchronize();
                    checkCUBLASReturnCode(result, nfiFunction.getName());
                    return result;
                } catch (InteropException e) {
                    throw new GrCUDAInternalException(e);
                }
            }
        };
        namespace.addFunction(wrapperFunction);
    }
}
 
Example #25
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
private int cuDeviceGet(int deviceOrdinal) {
    assertCUDAInitialized();
    try (UnsafeHelper.Integer32Object deviceObj = UnsafeHelper.createInteger32Object()) {
        Object callable = CUDADriverFunction.CU_DEVICEGET.getSymbol(this);
        Object result = INTEROP.execute(callable, deviceObj.getAddress(), deviceOrdinal);
        checkCUReturnCode(result, "cuDeviceGet");
        return deviceObj.getValue();
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #26
Source File: TensorRTRegistry.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@TruffleBoundary
public Object call(Object[] arguments) {
    try {
        if (nfiFunction == null) {
            // load function symbol lazily
            CompilerDirectives.transferToInterpreterAndInvalidate();
            nfiFunction = factory.makeFunction(context.getCUDARuntime(), libraryPath, DEFAULT_LIBRARY_HINT);
        }
        return INTEROP.execute(nfiFunction, arguments);
    } catch (InteropException e) {
        CompilerDirectives.transferToInterpreter();
        throw new RuntimeException(e);
    }
}
 
Example #27
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public void cuCtxSynchronize() {
    assertCUDAInitialized();
    try {
        Object callable = CUDADriverFunction.CU_CTXSYNCHRONIZE.getSymbol(this);
        Object result = INTEROP.execute(callable);
        checkCUReturnCode(result, "cuCtxSynchronize");
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #28
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public void cudaSetDevice(int device) {
    try {
        Object callable = CUDARuntimeFunction.CUDA_SETDEVICE.getSymbol(this);
        Object result = INTEROP.execute(callable, device);
        checkCUDAReturnCode(result, "cudaSetDevice");
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #29
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@TruffleBoundary
public int cudaGetDeviceCount() {
    try (UnsafeHelper.Integer32Object deviceCount = UnsafeHelper.createInteger32Object()) {
        Object callable = CUDARuntimeFunction.CUDA_GETDEVICECOUNT.getSymbol(this);
        Object result = INTEROP.execute(callable, deviceCount.getAddress());
        checkCUDAReturnCode(result, "cudaGetDeviceCount");
        return deviceCount.getValue();
    } catch (InteropException e) {
        throw new GrCUDAException(e);
    }
}
 
Example #30
Source File: CUDARuntime.java    From grcuda with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@TruffleBoundary
public Object call(CUDARuntime cudaRuntime, Object[] args) throws ArityException, InteropException {
    checkArgumentLength(args, 0);
    try (UnsafeHelper.Integer32Object deviceId = UnsafeHelper.createInteger32Object()) {
        callSymbol(cudaRuntime, deviceId.getAddress());
        return deviceId.getValue();
    }
}