clang and exceptions

This commit is contained in:
Roland Grinis 2021-07-05 10:34:01 +01:00
parent f4f5d65bd8
commit 675ad089fa
4 changed files with 81 additions and 9 deletions

View File

@ -27,14 +27,18 @@ val cppSources = projectDir.resolve("src/main/cpp")
val cudaHome: String? = System.getenv("CUDA_HOME") val cudaHome: String? = System.getenv("CUDA_HOME")
val cudaDefault = file("/usr/local/cuda").exists() val cudaDefault = file("/usr/local/cuda").exists()
val cudaFound = cudaHome?.isNotEmpty() ?: false or cudaDefault val cudaFound = cudaHome?.isNotEmpty() ?: false or cudaDefault
val cmakeArchive = "cmake-3.20.5-linux-x86_64" 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 torchArchive = "libtorch"
val cmakeCmd = "$thirdPartyDir/cmake/$cmakeArchive/bin/cmake" 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 ninjaCmd = "$thirdPartyDir/ninja/ninja"
val generateJNIHeader by tasks.registering { val generateJNIHeader by tasks.registering {
println(cmakeCmd)
doLast { doLast {
exec { exec {
workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa")) workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa"))
@ -50,6 +54,13 @@ val downloadCMake by tasks.registering(Download::class) {
overwrite(false) 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) { val downloadNinja by tasks.registering(Download::class) {
src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip") src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip")
dest(File("$thirdPartyDir/ninja", "ninja-linux.zip")) dest(File("$thirdPartyDir/ninja", "ninja-linux.zip"))
@ -72,6 +83,17 @@ val extractCMake by tasks.registering(Copy::class) {
into("$thirdPartyDir/cmake") 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) { val extractNinja by tasks.registering(Copy::class) {
dependsOn(downloadNinja) dependsOn(downloadNinja)
from(zipTree(downloadNinja.get().dest)) from(zipTree(downloadNinja.get().dest))
@ -86,6 +108,7 @@ val extractTorch by tasks.registering(Copy::class) {
val configureCpp by tasks.registering { val configureCpp by tasks.registering {
dependsOn(extractCMake) dependsOn(extractCMake)
dependsOn(extractClang)
dependsOn(extractNinja) dependsOn(extractNinja)
dependsOn(extractTorch) dependsOn(extractTorch)
onlyIf { !file(cppBuildDir).exists() } onlyIf { !file(cppBuildDir).exists() }
@ -100,6 +123,8 @@ val configureCpp by tasks.registering {
cmakeCmd, cmakeCmd,
cppSources, cppSources,
"-GNinja", "-GNinja",
"-DCMAKE_C_COMPILER=$clangCmd",
"-DCMAKE_CXX_COMPILER=$clangxxCmd",
"-DCMAKE_MAKE_PROGRAM=$ninjaCmd", "-DCMAKE_MAKE_PROGRAM=$ninjaCmd",
"-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive", "-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive",
"-DJAVA_HOME=$javaHome", "-DJAVA_HOME=$javaHome",

View File

@ -1,17 +1,47 @@
/* /*
* Copyright 2018-2021 KMath contributors. * BSD 2-Clause License
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. *
* 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 <noa/ghmc.hh> #include <noa/ghmc.hh>
#include <torch/torch.h> #include <torch/torch.h>
#include "space_kscience_kmath_noa_JNoa.h" #include "space_kscience_kmath_noa_JNoa.h"
JNIEXPORT jboolean JNICALL Java_space_kscience_kmath_noa_JNoa_cudaIsAvailable(JNIEnv *env, jclass)
JNIEXPORT jboolean JNICALL Java_space_kscience_kmath_noa_JNoa_cudaIsAvailable(JNIEnv *, 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(); return torch::cuda::is_available();
} }

View File

@ -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);
}
}

View File

@ -10,7 +10,12 @@ import kotlin.test.Test
class TestUtils { class TestUtils {
@Test @Test
fun checkCuda() { fun testExceptions() {
println(cudaAvailable()) try {
println(cudaAvailable())
} catch(e:NoaException) {
println(e)
println("ALL GOOD")
}
} }
} }