kmath/kmath-noa/build.gradle.kts

201 lines
5.9 KiB
Plaintext
Raw Normal View History

2021-06-26 00:36:01 +03:00
/*
* 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.
*/
import de.undercouch.gradle.tasks.download.Download
plugins {
kotlin("jvm")
id("ru.mipt.npm.gradle.common")
id("de.undercouch.download")
}
2021-08-02 00:04:08 +03:00
description = "Wrapper for the Differentiable Computation library NOA on top of LibTorch"
2021-06-26 00:36:01 +03:00
dependencies {
2021-07-12 23:12:02 +03:00
implementation(project(":kmath-tensors"))
2021-06-26 00:36:01 +03:00
}
2021-06-27 18:28:28 +03:00
val home: String = System.getProperty("user.home")
val javaHome: String = System.getProperty("java.home")
val thirdPartyDir = "$home/.konan/third-party/noa-v0.0.1"
2021-06-26 00:36:01 +03:00
val cppBuildDir = "$thirdPartyDir/cpp-build"
2021-07-05 21:47:51 +03:00
val jNoaDir = "$thirdPartyDir/jnoa/noa-kmath"
2021-06-26 00:36:01 +03:00
val cudaHome: String? = System.getenv("CUDA_HOME")
val cudaDefault = file("/usr/local/cuda").exists()
2021-08-02 11:34:44 +03:00
val cudaFound = (cudaHome?.isNotEmpty() ?: false) or cudaDefault
2021-07-05 12:34:01 +03:00
2021-06-28 00:12:02 +03:00
val cmakeArchive = "cmake-3.20.5-linux-x86_64"
2021-06-26 00:36:01 +03:00
val torchArchive = "libtorch"
2021-08-02 00:04:08 +03:00
val clangArchive = "clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu-16.04"
2021-06-26 00:36:01 +03:00
2021-06-28 00:12:02 +03:00
val cmakeCmd = "$thirdPartyDir/cmake/$cmakeArchive/bin/cmake"
val ninjaCmd = "$thirdPartyDir/ninja/ninja"
2021-08-02 00:04:08 +03:00
val clangRootDir = "$thirdPartyDir/clang/$clangArchive"
val clangCmd = "$clangRootDir/bin/clang"
val clangxxCmd = "$clangRootDir/bin/clang++"
2021-06-28 00:12:02 +03:00
val generateJNIHeader by tasks.registering {
doLast {
exec {
workingDir(projectDir.resolve("src/main/java/space/kscience/kmath/noa"))
2021-07-05 22:20:44 +03:00
commandLine(
"$javaHome/bin/javac", "-h",
projectDir.resolve("src/main/resources"), "JNoa.java"
)
2021-06-28 00:12:02 +03:00
}
}
}
2021-06-26 00:36:01 +03:00
val downloadCMake by tasks.registering(Download::class) {
val tarFile = "$cmakeArchive.tar.gz"
src("https://github.com/Kitware/CMake/releases/download/v3.20.5/$tarFile")
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/cmake", tarFile))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
2021-07-05 12:34:01 +03:00
2021-06-26 00:36:01 +03:00
val downloadNinja by tasks.registering(Download::class) {
src("https://github.com/ninja-build/ninja/releases/download/v1.10.2/ninja-linux.zip")
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/ninja", "ninja-linux.zip"))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
2021-08-02 00:04:08 +03:00
val downloadClang by tasks.registering(Download::class) {
val tarFile = "$clangArchive.tar.xz"
src("https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/$tarFile")
dest(File("$thirdPartyDir/clang", tarFile))
overwrite(false)
}
2021-06-26 00:36:01 +03:00
val downloadTorch by tasks.registering(Download::class) {
val torchVersion = "$torchArchive-shared-with-deps-1.9.0%2B"
val cudaUrl = "https://download.pytorch.org/libtorch/cu111/${torchVersion}cu111.zip"
val cpuUrl = "https://download.pytorch.org/libtorch/cpu/${torchVersion}cpu.zip"
val url = if (cudaFound) cudaUrl else cpuUrl
src(url)
2021-06-28 00:12:02 +03:00
dest(File("$thirdPartyDir/torch", "$torchArchive.zip"))
2021-06-26 00:36:01 +03:00
overwrite(false)
}
2021-07-05 22:20:44 +03:00
fun downloadJNoaHelper(update: Boolean) = tasks.registering(Download::class) {
2021-07-05 21:47:51 +03:00
src("https://github.com/grinisrit/noa/archive/refs/heads/kmath.zip")
dest(File("$thirdPartyDir/jnoa", "kmath.zip"))
2021-07-05 22:20:44 +03:00
overwrite(update)
2021-07-05 21:47:51 +03:00
}
2021-07-05 22:20:44 +03:00
val downloadJNoa by downloadJNoaHelper(false)
val reDownloadJNoa by downloadJNoaHelper(true)
2021-06-26 00:36:01 +03:00
val extractCMake by tasks.registering(Copy::class) {
dependsOn(downloadCMake)
from(tarTree(resources.gzip(downloadCMake.get().dest)))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/cmake")
2021-06-26 00:36:01 +03:00
}
val extractNinja by tasks.registering(Copy::class) {
dependsOn(downloadNinja)
from(zipTree(downloadNinja.get().dest))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/ninja")
2021-06-26 00:36:01 +03:00
}
2021-08-02 00:04:08 +03:00
val extractClang by tasks.registering {
dependsOn(downloadClang)
onlyIf { !file(clangRootDir).exists() }
doLast {
exec {
workingDir("$thirdPartyDir/clang")
commandLine("mkdir", clangArchive)
}
exec {
workingDir("$thirdPartyDir/clang")
commandLine("tar", "-xf", "$clangArchive.tar.xz",
"-C", clangArchive, "--strip-components", "1")
}
}
}
2021-06-26 00:36:01 +03:00
val extractTorch by tasks.registering(Copy::class) {
dependsOn(downloadTorch)
from(zipTree(downloadTorch.get().dest))
2021-06-28 00:12:02 +03:00
into("$thirdPartyDir/torch")
}
2021-07-05 21:47:51 +03:00
val extractJNoa by tasks.registering(Copy::class) {
dependsOn(downloadJNoa)
from(zipTree(downloadJNoa.get().dest))
into("$thirdPartyDir/jnoa")
}
2021-06-28 00:12:02 +03:00
val configureCpp by tasks.registering {
dependsOn(extractCMake)
dependsOn(extractNinja)
2021-08-10 08:51:46 +03:00
dependsOn(extractClang)
2021-06-28 00:12:02 +03:00
dependsOn(extractTorch)
2021-07-05 21:47:51 +03:00
dependsOn(extractJNoa)
2021-06-28 00:12:02 +03:00
onlyIf { !file(cppBuildDir).exists() }
doLast {
exec {
workingDir(thirdPartyDir)
commandLine("mkdir", "-p", cppBuildDir)
}
exec {
workingDir(cppBuildDir)
commandLine(
cmakeCmd,
2021-07-05 21:47:51 +03:00
jNoaDir,
2021-06-28 00:12:02 +03:00
"-GNinja",
"-DCMAKE_MAKE_PROGRAM=$ninjaCmd",
2021-08-10 08:51:46 +03:00
"-DCMAKE_C_COMPILER=$clangCmd",
"-DCMAKE_CXX_COMPILER=$clangxxCmd",
2021-06-28 00:12:02 +03:00
"-DCMAKE_PREFIX_PATH=$thirdPartyDir/torch/$torchArchive",
"-DJAVA_HOME=$javaHome",
2021-08-01 11:19:00 +03:00
"-DBUILD_JNOA=ON",
2021-06-28 00:12:02 +03:00
"-DCMAKE_BUILD_TYPE=Release",
2021-07-05 21:47:51 +03:00
"-DBUILD_NOA_TESTS=OFF",
"-DBUILD_NOA_BENCHMARKS=OFF",
"-DINSTALL_NOA=OFF"
2021-06-28 00:12:02 +03:00
)
}
}
}
val cleanCppBuild by tasks.registering {
onlyIf { file(cppBuildDir).exists() }
doLast {
exec {
workingDir(thirdPartyDir)
commandLine("rm", "-rf", cppBuildDir)
}
}
}
val buildCpp by tasks.registering {
dependsOn(configureCpp)
doLast {
exec {
workingDir(cppBuildDir)
2021-08-01 11:19:00 +03:00
commandLine(cmakeCmd, "--build", ".", "--config", "Release", "--target", "jnoa")
2021-06-28 00:12:02 +03:00
}
}
}
2021-07-13 14:19:03 +03:00
tasks["compileJava"].dependsOn(buildCpp)
tasks {
withType<Test>{
2021-08-01 11:19:00 +03:00
systemProperty("java.library.path", "$cppBuildDir/jnoa")
2021-07-31 00:33:43 +03:00
//systemProperty("java.library.path",
2021-08-01 11:19:00 +03:00
// "${System.getProperty("user.home")}/devspace/noa/cmake-build-release/jnoa")
}
2021-07-13 14:19:03 +03:00
}
2021-07-05 21:47:51 +03:00
readme {
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE
}