From 8e4f7ffce630df87f062d385953079a34ca5ef0c Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Mon, 5 Jul 2021 19:47:51 +0100 Subject: [PATCH] Fetching JNoa --- kmath-noa/build.gradle.kts | 30 ++++++++++-- kmath-noa/src/main/cpp/CMakeLists.txt | 37 --------------- kmath-noa/src/main/cpp/jnoa.cc | 47 ------------------- .../java/space/kscience/kmath/noa/JNoa.java | 2 +- .../space_kscience_kmath_noa_JNoa.h | 0 5 files changed, 27 insertions(+), 89 deletions(-) delete mode 100644 kmath-noa/src/main/cpp/CMakeLists.txt delete mode 100644 kmath-noa/src/main/cpp/jnoa.cc rename kmath-noa/src/main/{cpp => resources}/space_kscience_kmath_noa_JNoa.h (100%) diff --git a/kmath-noa/build.gradle.kts b/kmath-noa/build.gradle.kts index b0eb27eb8..c4b244ec5 100644 --- a/kmath-noa/build.gradle.kts +++ b/kmath-noa/build.gradle.kts @@ -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 +} diff --git a/kmath-noa/src/main/cpp/CMakeLists.txt b/kmath-noa/src/main/cpp/CMakeLists.txt deleted file mode 100644 index a6a70abfe..000000000 --- a/kmath-noa/src/main/cpp/CMakeLists.txt +++ /dev/null @@ -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) diff --git a/kmath-noa/src/main/cpp/jnoa.cc b/kmath-noa/src/main/cpp/jnoa.cc deleted file mode 100644 index e5b385522..000000000 --- a/kmath-noa/src/main/cpp/jnoa.cc +++ /dev/null @@ -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 -#include - -#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(); -} \ No newline at end of file diff --git a/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java b/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java index ef8941848..b54b0e8c8 100644 --- a/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java +++ b/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java @@ -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); diff --git a/kmath-noa/src/main/cpp/space_kscience_kmath_noa_JNoa.h b/kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h similarity index 100% rename from kmath-noa/src/main/cpp/space_kscience_kmath_noa_JNoa.h rename to kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h