-
Notifications
You must be signed in to change notification settings - Fork 3
NativeVariant.Cpu: add the capability to detect instruction-set extensions #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
324fa56
add a dependency on the oshi-core library (to obtain CPU feature flags)
stephengold dd7d34c
NativeVariant: add the Cpu.hasExtensions() method
stephengold 2680449
NativeVariant: solve Windows issue, eliminate caching of ISA extensions
stephengold 0baf927
NativeVariant: re-implement caching, fix Windows-on-ARM, add comments
stephengold b13ce2f
NativeVariant: document the names of some well-known ISA extensions
stephengold dd2be1b
NativeVariant: remove 2 HTML tags from the javadoc
stephengold 2799244
NativeVariant: move 2 fields to the Cpu inner class (for readability)
stephengold 7a9c74c
NativeVariant: readFeatureFlags() workaround for macOS
stephengold d7577e9
PlatformPredicate: add a combining constructor
stephengold 0309161
make the OSHI dependency into a transitive one
stephengold cb51869
snaploader-examples: added the TestCpuFeatures test
stephengold 73c35a7
snaploader-examples: register a Gradle task to run TestCpuFeatures
stephengold 4102293
TestCpuFeatures: specify CLEAN_EXTRACTION to loadLibrary()
stephengold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
snaploader-examples/src/main/java/electrostatic4j/snaploader/examples/TestCpuFeatures.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /* | ||
| * Copyright (c) 2025, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * 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. | ||
| * | ||
| * * Neither the name of 'AvrSandbox' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * 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 OWNER 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. | ||
| */ | ||
| package electrostatic4j.snaploader.examples; | ||
|
|
||
| import com.github.stephengold.joltjni.Jolt; | ||
| import electrostatic4j.snaploader.LibraryInfo; | ||
| import electrostatic4j.snaploader.LoadingCriterion; | ||
| import electrostatic4j.snaploader.NativeBinaryLoader; | ||
| import electrostatic4j.snaploader.filesystem.DirectoryPath; | ||
| import electrostatic4j.snaploader.platform.NativeDynamicLibrary; | ||
| import electrostatic4j.snaploader.platform.util.NativeVariant; | ||
| import electrostatic4j.snaploader.platform.util.PlatformPredicate; | ||
|
|
||
| /** | ||
| * Tests selection between native libraries based on CPU features. | ||
| * | ||
| * @author Stephen Gold [email protected] | ||
| */ | ||
| public final class TestCpuFeatures { | ||
|
|
||
| public static void main(String[] argv) { | ||
| // Test for each of the relevant CPU features: | ||
| System.out.println("avx = " + NativeVariant.Cpu.hasExtensions("avx")); | ||
| System.out.println("avx2 = " + NativeVariant.Cpu.hasExtensions("avx2")); | ||
| System.out.println("bmi1 = " + NativeVariant.Cpu.hasExtensions("bmi1")); | ||
| System.out.println("f16c = " + NativeVariant.Cpu.hasExtensions("f16c")); | ||
| System.out.println("fma = " + NativeVariant.Cpu.hasExtensions("fma")); | ||
| System.out.println("sse4_1 = " + NativeVariant.Cpu.hasExtensions("sse4_1")); | ||
| System.out.println("sse4_2 = " + NativeVariant.Cpu.hasExtensions("sse4_2")); | ||
|
|
||
| // Define a custom predicate for Linux with all 7 CPU features: | ||
| PlatformPredicate linuxWithFma = new PlatformPredicate( | ||
| PlatformPredicate.LINUX_X86_64, | ||
| "avx", "avx2", "bmi1", "f16c", "fma", "sse4_1", "sse4_2"); | ||
| System.out.println("linuxWithFma = " + linuxWithFma.evaluatePredicate()); | ||
|
|
||
| // Define a custom predicate for Windows with 4 CPU features: | ||
| PlatformPredicate windowsWithAvx2 = new PlatformPredicate( | ||
| PlatformPredicate.WIN_X86_64, | ||
| "avx", "avx2", "sse4_1", "sse4_2"); | ||
| System.out.println("windowsWithAvx2 = " + windowsWithAvx2.evaluatePredicate()); | ||
| System.out.flush(); | ||
|
|
||
| LibraryInfo info = new LibraryInfo( | ||
| new DirectoryPath("linux/x86-64/com/github/stephengold"), | ||
| "joltjni", DirectoryPath.USER_DIR); | ||
| NativeBinaryLoader loader = new NativeBinaryLoader(info); | ||
| NativeDynamicLibrary[] libraries = { | ||
| new NativeDynamicLibrary("linux/x86-64-fma/com/github/stephengold", linuxWithFma), // must precede vanilla LINUX_X86_64 | ||
| new NativeDynamicLibrary("linux/x86-64/com/github/stephengold", PlatformPredicate.LINUX_X86_64), | ||
| new NativeDynamicLibrary("windows/x86-64-avx2/com/github/stephengold", windowsWithAvx2), // must precede vanilla WIN_X86_64 | ||
| new NativeDynamicLibrary("windows/x86-64/com/github/stephengold", PlatformPredicate.WIN_X86_64) | ||
| }; | ||
| loader.registerNativeLibraries(libraries).initPlatformLibrary(); | ||
| loader.setLoggingEnabled(true); | ||
| loader.setRetryWithCleanExtraction(true); | ||
| try { | ||
| loader.loadLibrary(LoadingCriterion.CLEAN_EXTRACTION); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Failed to load the joltjni library!"); | ||
| } | ||
| System.err.flush(); | ||
|
|
||
| // Invoke native code to obtain the configuration of the native library. | ||
| String configuration = Jolt.getConfigurationString(); | ||
| /* | ||
| * Depending which native library was loaded, the configuration string | ||
| * should be one of the following: | ||
| * | ||
| * On LINUX_X86_64 platforms, either | ||
| * Single precision x86 64-bit with instructions: SSE2 SSE4.1 SSE4.2 AVX AVX2 F16C LZCNT TZCNT FMADD (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) | ||
| * or | ||
| * Single precision x86 64-bit with instructions: SSE2 (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) | ||
| * | ||
| * On WIN_X86_64 platforms, either | ||
| * Single precision x86 64-bit with instructions: SSE2 SSE4.1 SSE4.2 AVX AVX2 F16C LZCNT TZCNT (FP Exceptions) (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) | ||
| * or | ||
| * Single precision x86 64-bit with instructions: SSE2 (FP Exceptions) (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) | ||
| */ | ||
| System.out.println(configuration); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,5 +27,5 @@ jar { // assemble jar options [java -jar] | |
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| api('com.github.oshi:oshi-core:6.7.0') | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.