Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Martmists <[email protected]>
  • Loading branch information
Martmists-GH committed Jul 18, 2024
0 parents commit cf980cc
Show file tree
Hide file tree
Showing 68 changed files with 5,106 additions and 0 deletions.
456 changes: 456 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### IDE
.idea/

### Gradle
.gradle/
.kotlin/
build/

### CMake
cmake-build-*/
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
3-Clause BSD NON-AI License

Copyright 2024 Martmists

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.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

4. The source code and the binary form, and any modifications made to them may not be used for the purpose of training or improving machine learning algorithms,
including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives,
modifications, or updates based on the Software code. Any usage of the source code or the binary form in an AI-training dataset is considered a breach of this License.

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.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# NDArray.SIMD

A Kotlin NDArray library with built-in SIMD support.

### Installation

Coming soon:tm: to my Maven repo.

### Motivation

I basically made this because [Viktor](https://github.com/JetBrains-Research/viktor) didn't really utilize their SIMD capabilities.

As such, most of the JVM code for NDArray is mostly the same as Viktor.

### License

The nativeMain and jvmMain sourcesets are licensed under the [3-Clause BSD NON-AI License](https://github.com/non-ai-licenses/non-ai-licenses/blob/main/NON-AI-BSD3), with @Martmists-GH as the copyright holder.

The commonMain sourceset is mostly copied/adapted from Viktor, and as such is licensed under the [original MIT license](https://github.com/JetBrains-Research/viktor/blob/master/LICENSE), with JetBrains BioLabs as the copyright holder.
148 changes: 148 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import com.github.tomtzook.gcmake.tasks.CmakeBuildTask
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithSimulatorTests
import org.jetbrains.kotlin.gradle.targets.native.tasks.artifact.KotlinNativeLinkArtifactTask
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink

plugins {
kotlin("multiplatform") version "2.0.0"
id("io.github.tomtzook.gradle-cmake") version "1.2.2"
}

group = "com.martmists"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

cmake {
targets {
val simd by creating {
cmakeLists = file("cmake/CMakeLists.txt")

val linuxX64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/linux-x64.cmake")
}
val linuxArm64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/linux-arm64.cmake")
}
val mingwX64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/mingw-x64.cmake")
}
val mingwArm64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/mingw-arm64.cmake")
}
val macosX64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/macos-x64.cmake")
}
val macosArm64 by machines.customMachines.registering {
toolchainFile = file("cmake/toolchains/macos-arm64.cmake")
}

if (project.hasProperty("production")) {
targetMachines.add(linuxX64)
targetMachines.add(linuxArm64)
targetMachines.add(mingwX64)
// targetMachines.add(mingwArm64)
targetMachines.add(macosX64)
targetMachines.add(macosArm64)
} else {
targetMachines.add(linuxX64)
}

cmakeArgs = if (project.hasProperty("production")) {
listOf("-DCMAKE_BUILD_TYPE=Release")
} else {
listOf("-DCMAKE_BUILD_TYPE=Debug")
}
}
}
}

kotlin {
jvm()

val natives = if (project.hasProperty("production")) {
listOf(
linuxX64(),
linuxArm64(),
mingwX64(),
// mingwArm64(),
macosX64(),
macosArm64(),
)
} else {
when (System.getProperty("os.name")) {
"Linux" -> listOf(linuxX64())
"Windows" -> listOf(mingwX64())
"Mac OS X" -> listOf(macosX64())
else -> error("Unsupported OS")
}
}

for (native in natives) {
native.apply {
binaries {
sharedLib {
baseName = "ndarray_simd"
}
}

compilerOptions {
optIn = listOf(
"kotlin.experimental.ExperimentalNativeApi",
"kotlinx.cinterop.ExperimentalForeignApi",
)
}

compilations.named("main") {
val jni by cinterops.creating {
val javaHome = File(System.getProperty("java.home")!!)
defFile(project.projectDir.resolve("src/nativeMain/cinterops/jni.def"))
includeDirs(
javaHome.resolve("include"),
javaHome.resolve("include/linux"),
javaHome.resolve("include/darwin"),
javaHome.resolve("include/win32"),
)
}

val simd by cinterops.creating {
defFile(project.projectDir.resolve("src/nativeMain/cinterops/simd.def"))
includeDirs(
project.projectDir.resolve("src/lib"),
)

extraOpts("-libraryPath", projectDir.resolve("build/cmake/simd/${target.name}/").absolutePath)
}
}
}
}
}

tasks {
val cmakeBuild by existing

withType<KotlinNativeCompile> {
dependsOn(cmakeBuild)
}

val jvmProcessResources by existing(Copy::class) {
val binaryName = if (project.hasProperty("production")) {
"releaseShared"
} else {
"debugShared"
}

for (native in kotlin.targets.withType<KotlinNativeTarget>()) {
into("META-INF/natives/${native.targetName}") {
from(named(native.binaries.getByName(binaryName).linkTaskName)) {
exclude("**/*.h")
}
}
}
}
}
10 changes: 10 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.30.0)
project(simd)

file(GLOB_RECURSE source_files ../src/lib/cpp/*.cpp)

include_directories(../src/lib/public)

add_library(
simd STATIC ${source_files}
)
2 changes: 2 additions & 0 deletions cmake/arch/arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set("CMAKE_C_FLAGS" "-march=armv8-a --no-standard-libraries")
2 changes: 2 additions & 0 deletions cmake/arch/x64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(CMAKE_SYSTEM_PROCESSOR x86_64)
set("CMAKE_C_FLAGS" "-march=x86-64 --no-standard-libraries")
2 changes: 2 additions & 0 deletions cmake/platform/linux.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(CMAKE_SYSTEM_NAME Linux)

1 change: 1 addition & 0 deletions cmake/platform/macos.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(CMAKE_SYSTEM_NAME Darwin)
1 change: 1 addition & 0 deletions cmake/platform/windows.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set(CMAKE_SYSTEM_NAME Windows)
2 changes: 2 additions & 0 deletions cmake/toolchains/common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(CMAKE_C_FLAGS "-O2 -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
3 changes: 3 additions & 0 deletions cmake/toolchains/linux-arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/linux.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/arm64.cmake")
3 changes: 3 additions & 0 deletions cmake/toolchains/linux-x64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/linux.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/x64.cmake")
3 changes: 3 additions & 0 deletions cmake/toolchains/macos-arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/macos.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/arm64.cmake")
3 changes: 3 additions & 0 deletions cmake/toolchains/macos-x64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/macos.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/x64.cmake")
3 changes: 3 additions & 0 deletions cmake/toolchains/mingw-arm64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/windows.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/arm64.cmake")
3 changes: 3 additions & 0 deletions cmake/toolchains/mingw-x64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../platform/windows.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/../arch/x64.cmake")
9 changes: 9 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
org.gradle.parallel=true

kotlin.code.style=official
kotlin.native.ignoreDisabledTargets=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.enableCInteropCommonization.nowarn=true
kotlin.native.binary.sourceInfoType=libbacktrace

kotlin.native.cacheKind.linuxX64=none
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Mon Jul 15 14:03:33 CEST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit cf980cc

Please sign in to comment.