From 675ad089fa582c60aea7ed9ff631056b41451cee Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 5 Jul 2021 10:34:01 +0100 Subject: [PATCH] clang and exceptions --- kmath-noa/build.gradle.kts | 27 +++++++++++- kmath-noa/src/main/cpp/jnoa.cc | 42 ++++++++++++++++--- .../kscience/kmath/noa/NoaException.java | 12 ++++++ .../space/kscience/kmath/noa/TestUtils.kt | 9 +++- 4 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 kmath-noa/src/main/java/space/kscience/kmath/noa/NoaException.java diff --git a/kmath-noa/build.gradle.kts b/kmath-noa/build.gradle.kts index 5a87ac6d6..b0eb27eb8 100644 --- a/kmath-noa/build.gradle.kts +++ b/kmath-noa/build.gradle.kts @@ -27,14 +27,18 @@ val cppSources = projectDir.resolve("src/main/cpp") val cudaHome: String? = System.getenv("CUDA_HOME") val cudaDefault = file("/usr/local/cuda").exists() val cudaFound = cudaHome?.isNotEmpty() ?: false or cudaDefault + val cmakeArchive = "cmake-3.20.5-linux-x86_64" +val clangArchive = "clang+llvm-12.0.0-x86_64-linux-gnu-ubuntu-16.04" val torchArchive = "libtorch" val cmakeCmd = "$thirdPartyDir/cmake/$cmakeArchive/bin/cmake" +val clangRootDir = "$thirdPartyDir/clang/$clangArchive" +val clangCmd = "$clangRootDir/bin/clang" +val clangxxCmd = "$clangRootDir/bin/clang++" val ninjaCmd = "$thirdPartyDir/ninja/ninja" val generateJNIHeader by tasks.registering { - println(cmakeCmd) doLast { exec { workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa")) @@ -50,6 +54,13 @@ val downloadCMake by tasks.registering(Download::class) { overwrite(false) } +val downloadClang by tasks.registering(Download::class) { + val tarFile = "$clangArchive.tar.xz" + src("https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.0/$tarFile") + dest(File("$thirdPartyDir/clang", tarFile)) + overwrite(false) +} + val downloadNinja by tasks.registering(Download::class) { src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip") dest(File("$thirdPartyDir/ninja", "ninja-linux.zip")) @@ -72,6 +83,17 @@ val extractCMake by tasks.registering(Copy::class) { into("$thirdPartyDir/cmake") } +val extractClang by tasks.registering { + dependsOn(downloadClang) + onlyIf { !file(clangRootDir).exists() } + doLast { + exec { + workingDir("$thirdPartyDir/clang") + commandLine("tar", "-xf", "$clangArchive.tar.xz") + } + } +} + val extractNinja by tasks.registering(Copy::class) { dependsOn(downloadNinja) from(zipTree(downloadNinja.get().dest)) @@ -86,6 +108,7 @@ val extractTorch by tasks.registering(Copy::class) { val configureCpp by tasks.registering { dependsOn(extractCMake) + dependsOn(extractClang) dependsOn(extractNinja) dependsOn(extractTorch) onlyIf { !file(cppBuildDir).exists() } @@ -100,6 +123,8 @@ val configureCpp by tasks.registering { cmakeCmd, cppSources, "-GNinja", + "-DCMAKE_C_COMPILER=$clangCmd", + "-DCMAKE_CXX_COMPILER=$clangxxCmd", "-DCMAKE_MAKE_PROGRAM=$ninjaCmd", "-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive", "-DJAVA_HOME=$javaHome", diff --git a/kmath-noa/src/main/cpp/jnoa.cc b/kmath-noa/src/main/cpp/jnoa.cc index ba446b955..e5b385522 100644 --- a/kmath-noa/src/main/cpp/jnoa.cc +++ b/kmath-noa/src/main/cpp/jnoa.cc @@ -1,17 +1,47 @@ /* - * Copyright 2018-2021 KMath contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + * BSD 2-Clause License + * + * Copyright (c) 2021, Roland Grinis, GrinisRIT ltd. (roland.grinis@grinisrit.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - #include #include - #include "space_kscience_kmath_noa_JNoa.h" - -JNIEXPORT jboolean JNICALL Java_space_kscience_kmath_noa_JNoa_cudaIsAvailable(JNIEnv *, jclass) +JNIEXPORT jboolean JNICALL Java_space_kscience_kmath_noa_JNoa_cudaIsAvailable(JNIEnv *env, jclass) { + jclass noa_exception = env->FindClass("space/kscience/kmath/noa/NoaException"); + try + { + std::cout << (torch::rand({2, 3}) + torch::rand({3, 2})) << "\n"; + } + catch (const std::exception &e) + { + std::cout << "caught that in C++\n"; + env->ThrowNew(noa_exception, e.what()); + } return torch::cuda::is_available(); } \ No newline at end of file diff --git a/kmath-noa/src/main/java/space/kscience/kmath/noa/NoaException.java b/kmath-noa/src/main/java/space/kscience/kmath/noa/NoaException.java new file mode 100644 index 000000000..4b68f82f9 --- /dev/null +++ b/kmath-noa/src/main/java/space/kscience/kmath/noa/NoaException.java @@ -0,0 +1,12 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.noa; + +public class NoaException extends Exception { + public NoaException(String errorMessage) { + super(errorMessage); + } +} diff --git a/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt b/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt index a3bbaa610..96aa2cd9d 100644 --- a/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt +++ b/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt @@ -10,7 +10,12 @@ import kotlin.test.Test class TestUtils { @Test - fun checkCuda() { - println(cudaAvailable()) + fun testExceptions() { + try { + println(cudaAvailable()) + } catch(e:NoaException) { + println(e) + println("ALL GOOD") + } } } \ No newline at end of file