Fetching JNoa

This commit is contained in:
Roland Grinis 2021-07-05 19:47:51 +01:00
parent 675ad089fa
commit 8e4f7ffce6
5 changed files with 27 additions and 89 deletions

View File

@ -22,7 +22,7 @@ val home: String = System.getProperty("user.home")
val javaHome: String = System.getProperty("java.home")
val thirdPartyDir = "$home/.konan/third-party/kmath-noa-${project.property("version")}"
val cppBuildDir = "$thirdPartyDir/cpp-build"
val cppSources = projectDir.resolve("src/main/cpp")
val jNoaDir = "$thirdPartyDir/jnoa/noa-kmath"
val cudaHome: String? = System.getenv("CUDA_HOME")
val cudaDefault = file("/usr/local/cuda").exists()
@ -42,7 +42,8 @@ val generateJNIHeader by tasks.registering {
doLast {
exec {
workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa"))
commandLine("$javaHome/bin/javac", "-h", cppSources , "JNoa.java")
commandLine("$javaHome/bin/javac", "-h",
projectDir.resolve("src/main/resources") , "JNoa.java")
}
}
}
@ -77,6 +78,12 @@ val downloadTorch by tasks.registering(Download::class) {
overwrite(false)
}
val downloadJNoa by tasks.registering(Download::class) {
src("https://github.com/grinisrit/noa/archive/refs/heads/kmath.zip")
dest(File("$thirdPartyDir/jnoa", "kmath.zip"))
overwrite(false)
}
val extractCMake by tasks.registering(Copy::class) {
dependsOn(downloadCMake)
from(tarTree(resources.gzip(downloadCMake.get().dest)))
@ -106,11 +113,18 @@ val extractTorch by tasks.registering(Copy::class) {
into("$thirdPartyDir/torch")
}
val extractJNoa by tasks.registering(Copy::class) {
dependsOn(downloadJNoa)
from(zipTree(downloadJNoa.get().dest))
into("$thirdPartyDir/jnoa")
}
val configureCpp by tasks.registering {
dependsOn(extractCMake)
dependsOn(extractClang)
dependsOn(extractNinja)
dependsOn(extractTorch)
dependsOn(extractJNoa)
onlyIf { !file(cppBuildDir).exists() }
doLast {
exec {
@ -121,15 +135,19 @@ val configureCpp by tasks.registering {
workingDir(cppBuildDir)
commandLine(
cmakeCmd,
cppSources,
jNoaDir,
"-GNinja",
"-DCMAKE_C_COMPILER=$clangCmd",
"-DCMAKE_CXX_COMPILER=$clangxxCmd",
"-DCMAKE_MAKE_PROGRAM=$ninjaCmd",
"-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive",
"-DJAVA_HOME=$javaHome",
"-DBUILD_NOA_KMATH=ON",
"-DCMAKE_BUILD_TYPE=Release",
"-DBUILD_NOA_CUDA=${if(!cudaFound) "ON" else "OFF"}"
"-DBUILD_NOA_CUDA=${if(!cudaFound) "ON" else "OFF"}",
"-DBUILD_NOA_TESTS=OFF",
"-DBUILD_NOA_BENCHMARKS=OFF",
"-DINSTALL_NOA=OFF"
)
}
}
@ -156,3 +174,7 @@ val buildCpp by tasks.registering {
}
tasks["compileJava"].dependsOn(buildCpp)
readme {
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE
}

View File

@ -1,37 +0,0 @@
cmake_minimum_required(VERSION 3.12)
project(JNOA LANGUAGES C CXX)
# Require C++17
set(CMAKE_CXX_STANDARD 17)
find_package(Torch REQUIRED)
find_package(JNI REQUIRED)
include(FetchContent)
if(NOT TARGET noa)
FetchContent_Declare(
noa
GIT_REPOSITORY https://github.com/grinisrit/noa.git
GIT_TAG kmath)
FetchContent_GetProperties(noa)
if(NOT noa_POPULATED)
FetchContent_Populate(noa)
set(INSTALL_NOA OFF CACHE BOOL "")
set(BUILD_NOA_TESTS OFF CACHE BOOL "")
set(BUILD_NOA_BENCHMARKS OFF CACHE BOOL "")
add_subdirectory(
${noa_SOURCE_DIR}
${noa_BINARY_DIR})
endif()
endif()
add_library(jnoa SHARED jnoa.cc)
target_include_directories(jnoa PRIVATE ${JNI_INCLUDE_DIRS})
target_link_libraries(jnoa PRIVATE torch NOA::NOA)
target_compile_options(jnoa PRIVATE -Wall -Wextra -Wpedantic -O3 -fPIC)

View File

@ -1,47 +0,0 @@
/*
* 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 <noa/ghmc.hh>
#include <torch/torch.h>
#include "space_kscience_kmath_noa_JNoa.h"
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();
}

View File

@ -9,7 +9,7 @@ public class JNoa {
static {
String jNoaPath = System.getProperty("user.home") +
"/.konan/third-party/kmath-noa-0.3.0-dev-14/cpp-build/libjnoa.so";
"/.konan/third-party/kmath-noa-0.3.0-dev-14/cpp-build/kmath/libjnoa.so";
try {
System.load(jNoaPath);