Skip to content

Commit

Permalink
consistently use GrCUDAException and GrCUDAInternalException, remove …
Browse files Browse the repository at this point in the history
…deprecated parse API usages (#19)
  • Loading branch information
lukasstadler authored Mar 4, 2020
1 parent 1d0ca13 commit 2597489
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 249 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -28,7 +28,11 @@
*/
package com.nvidia.grcuda;

import java.util.Arrays;
import java.util.Optional;

import com.oracle.truffle.api.TruffleException;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.nodes.Node;

public final class GrCUDAException extends RuntimeException implements TruffleException {
Expand All @@ -45,6 +49,23 @@ public GrCUDAException(String message, Node node) {
this.node = node;
}

public GrCUDAException(InteropException e) {
this(e.getMessage());
}

public GrCUDAException(int errorCode, String[] functionName) {
this("CUDA error " + errorCode + " in " + format(functionName));
}

public GrCUDAException(int errorCode, String message, String[] functionName) {
this(message + '(' + errorCode + ") in " + format(functionName));
}

public static String format(String... name) {
Optional<String> result = Arrays.asList(name).stream().reduce((a, b) -> a + "::" + b);
return result.orElse("<empty>");
}

@Override
public Node getLocation() {
return node;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand All @@ -25,30 +26,36 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.nvidia.grcuda.gpu;
package com.nvidia.grcuda;

import com.oracle.truffle.api.TruffleException;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.api.nodes.Node;

public class CUDAException extends RuntimeException implements TruffleException {
public final class GrCUDAInternalException extends RuntimeException implements TruffleException {
private static final long serialVersionUID = 8614211550329856579L;

private static final long serialVersionUID = 8808625867900726781L;
private final Node node;

public CUDAException(int errorCode, String functionName) {
super("CUDA error " + errorCode + " in " + functionName);
public GrCUDAInternalException(String message) {
this(message, null);
}

public CUDAException(int errorCode, String message, String functionName) {
super(message + '(' + errorCode + ") in " + functionName);
public GrCUDAInternalException(String message, Node node) {
super(message);
this.node = node;
}

public CUDAException(String message) {
super(message);
public GrCUDAInternalException(InteropException e) {
this(e.getMessage());
}

public boolean isInternalError() {
return true;
}

@Override
public Node getLocation() {
// null = location not available
return null;
return node;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -29,7 +29,6 @@
package com.nvidia.grcuda;

import com.nvidia.grcuda.cuml.CUMLRegistry;
import com.nvidia.grcuda.gpu.CUDAException;
import com.nvidia.grcuda.nodes.ExpressionNode;
import com.nvidia.grcuda.nodes.GrCUDARootNode;
import com.nvidia.grcuda.parser.ParserAntlr;
Expand Down Expand Up @@ -66,7 +65,7 @@ protected boolean isObjectOfLanguage(Object object) {
}

@Override
protected CallTarget parse(ParsingRequest request) throws CUDAException {
protected CallTarget parse(ParsingRequest request) {
ExpressionNode expression = new ParserAntlr().parse(request.getSource());
GrCUDARootNode newParserRoot = new GrCUDARootNode(this, expression);
return Truffle.getRuntime().createCallTarget(newParserRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -31,12 +31,15 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

import com.nvidia.grcuda.GrCUDAContext;
import com.nvidia.grcuda.GrCUDAException;
import com.nvidia.grcuda.GrCUDAInternalException;
import com.nvidia.grcuda.functions.ExternalFunctionFactory;
import com.nvidia.grcuda.functions.Function;
import com.nvidia.grcuda.functions.FunctionTable;
import com.nvidia.grcuda.gpu.CUDAException;
import com.nvidia.grcuda.gpu.UnsafeHelper;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
Expand Down Expand Up @@ -96,8 +99,7 @@ public Object call(Object[] arguments) throws ArityException {
return handle.getValue();
}
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAInternalException(e);
}
}
};
Expand All @@ -116,8 +118,7 @@ public Object call(Object[] arguments) throws ArityException, UnsupportedTypeExc
checkCUMLReturnCode(result, "cumlDestroy");
return result;
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAInternalException(e);
}
}
};
Expand All @@ -139,8 +140,7 @@ public Object call(Object[] arguments) {
cumlHandle = expectInt(result);
}
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAInternalException(e);
}

// Argument 0 is the function name in the frame, removing argument 0 and
Expand All @@ -155,7 +155,7 @@ public Object call(Object[] arguments) {
return result;
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAInternalException(e);
}
}
};
Expand All @@ -164,28 +164,28 @@ public Object call(Object[] arguments) {
}

private void cuMLShutdown() {
CompilerAsserts.neverPartOfCompilation();
if (cumlHandle != null) {
try {
Object result = INTEROP.execute(cumlDestroyFunction, cumlHandle);
checkCUMLReturnCode(result, CUMLFunctionNFI.CUML_CUMLDESTROY.getFunctionFactory().getName());
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAInternalException(e);
}
}
}

private static void checkCUMLReturnCode(Object result, String function) {
private static void checkCUMLReturnCode(Object result, String... function) {
int returnCode;
try {
returnCode = INTEROP.asInt(result);
} catch (UnsupportedMessageException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException("expected return code as Integer object in " + function + ", got " + result.getClass().getName());
throw new GrCUDAInternalException("expected return code as Integer object in " + GrCUDAException.format(function) + ", got " + result.getClass().getName());
}
if (returnCode != 0) {
CompilerDirectives.transferToInterpreter();
throw new CUDAException(returnCode, cumlReturnCodeToString(returnCode), function);
throw new GrCUDAException(returnCode, cumlReturnCodeToString(returnCode), function);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -28,8 +28,8 @@
*/
package com.nvidia.grcuda.functions;

import com.nvidia.grcuda.GrCUDAException;
import com.nvidia.grcuda.GrCUDALanguage;
import com.nvidia.grcuda.gpu.CUDAException;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.interop.ArityException;
Expand All @@ -54,7 +54,7 @@ public Object call(Object[] arguments) throws UnsupportedTypeException, ArityExc
return GrCUDALanguage.getCurrentLanguage().getContextReference().get().getCUDARuntime().getSymbol(libraryFile, symbolName, signature);
} catch (UnknownIdentifierException e) {
CompilerDirectives.transferToInterpreter();
throw new CUDAException(symbolName + " not found in " + libraryFile);
throw new GrCUDAException(symbolName + " not found in " + libraryFile);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -33,6 +33,7 @@

import com.nvidia.grcuda.DeviceArray;
import com.nvidia.grcuda.ElementType;
import com.nvidia.grcuda.GrCUDAException;
import com.nvidia.grcuda.MultiDimDeviceArray;
import com.nvidia.grcuda.TypeException;
import com.nvidia.grcuda.gpu.CUDARuntime;
Expand Down Expand Up @@ -62,23 +63,21 @@ public Object call(Object[] arguments) throws ArityException, UnsupportedTypeExc
Object arg = arguments[i];
if (INTEROP.isString(arg)) {
if (useColumnMajor.isPresent()) {
throw new RuntimeException("string option already provided");
throw new GrCUDAException("string option already provided");
} else {
String strArg = expectString(arg,
"string argument expected that specifies order ('C' or 'F')");
String strArg = expectString(arg, "string argument expected that specifies order ('C' or 'F')");
if (strArg.equals("f") || strArg.equals("F")) {
useColumnMajor = Optional.of(true);
} else if (strArg.equals("c") || strArg.equals("C")) {
useColumnMajor = Optional.of(false);
} else {
throw new RuntimeException("invalid string argument '" + strArg +
"', only \"C\" or \"F\" are allowed");
throw new GrCUDAException("invalid string argument '" + strArg + "', only \"C\" or \"F\" are allowed");
}
}
} else {
long n = expectLong(arg, "expected number argument for dimension size");
if (n < 1) {
throw new RuntimeException("array dimension less than 1");
throw new GrCUDAException("array dimension less than 1");
}
elementsPerDim.add(n);
}
Expand All @@ -91,7 +90,7 @@ public Object call(Object[] arguments) throws ArityException, UnsupportedTypeExc
long[] dimensions = elementsPerDim.stream().mapToLong(l -> l).toArray();
return new MultiDimDeviceArray(runtime, elementType, dimensions, useColumnMajor.orElse(false));
} catch (TypeException e) {
throw new RuntimeException(e.getMessage());
throw new GrCUDAException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -28,6 +28,7 @@
*/
package com.nvidia.grcuda.functions;

import com.nvidia.grcuda.GrCUDAException;
import com.nvidia.grcuda.gpu.CUDARuntime;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropException;
Expand Down Expand Up @@ -68,7 +69,7 @@ public ExternalFunction makeFunction(CUDARuntime cudaRuntime, String libraryPath
cudaRuntime.getSymbol(libraryPath, symbolName, nfiSignature));
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new RuntimeException(e);
throw new GrCUDAException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -31,6 +31,7 @@
import java.util.HashMap;
import java.util.Optional;

import com.nvidia.grcuda.GrCUDAInternalException;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

Expand All @@ -43,7 +44,7 @@ public FunctionTable registerFunction(Function function) {
String namespace = function.getNamespace();
String key = getKeyFromName(functionName, namespace);
if (functionMap.containsKey(key)) {
throw new RuntimeException("function '" + namespace + "::" + functionName + "' already exists.");
throw new GrCUDAInternalException("function '" + namespace + "::" + functionName + "' already exists.");
}
functionMap.put(key, function);
return this;
Expand Down
Loading

0 comments on commit 2597489

Please sign in to comment.