|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, The Electrostatic-Sandbox Distributed Simulation Framework, jSnapLoader |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'AvrSandbox' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package electrostatic4j.snaploader.examples; |
| 33 | + |
| 34 | +import com.github.stephengold.joltjni.Jolt; |
| 35 | +import electrostatic4j.snaploader.LibraryInfo; |
| 36 | +import electrostatic4j.snaploader.LoadingCriterion; |
| 37 | +import electrostatic4j.snaploader.NativeBinaryLoader; |
| 38 | +import electrostatic4j.snaploader.filesystem.DirectoryPath; |
| 39 | +import electrostatic4j.snaploader.platform.NativeDynamicLibrary; |
| 40 | +import electrostatic4j.snaploader.platform.util.NativeVariant; |
| 41 | +import electrostatic4j.snaploader.platform.util.PlatformPredicate; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests selection between native libraries based on CPU features. |
| 45 | + * |
| 46 | + * @author Stephen Gold [email protected] |
| 47 | + */ |
| 48 | +public final class TestCpuFeatures { |
| 49 | + |
| 50 | + public static void main(String[] argv) { |
| 51 | + // Test for each of the relevant CPU features: |
| 52 | + System.out.println("avx = " + NativeVariant.Cpu.hasExtensions("avx")); |
| 53 | + System.out.println("avx2 = " + NativeVariant.Cpu.hasExtensions("avx2")); |
| 54 | + System.out.println("bmi1 = " + NativeVariant.Cpu.hasExtensions("bmi1")); |
| 55 | + System.out.println("f16c = " + NativeVariant.Cpu.hasExtensions("f16c")); |
| 56 | + System.out.println("fma = " + NativeVariant.Cpu.hasExtensions("fma")); |
| 57 | + System.out.println("sse4_1 = " + NativeVariant.Cpu.hasExtensions("sse4_1")); |
| 58 | + System.out.println("sse4_2 = " + NativeVariant.Cpu.hasExtensions("sse4_2")); |
| 59 | + |
| 60 | + // Define a custom predicate for Linux with all 7 CPU features: |
| 61 | + PlatformPredicate linuxWithFma = new PlatformPredicate( |
| 62 | + PlatformPredicate.LINUX_X86_64, |
| 63 | + "avx", "avx2", "bmi1", "f16c", "fma", "sse4_1", "sse4_2"); |
| 64 | + System.out.println("linuxWithFma = " + linuxWithFma.evaluatePredicate()); |
| 65 | + |
| 66 | + // Define a custom predicate for Windows with 4 CPU features: |
| 67 | + PlatformPredicate windowsWithAvx2 = new PlatformPredicate( |
| 68 | + PlatformPredicate.WIN_X86_64, |
| 69 | + "avx", "avx2", "sse4_1", "sse4_2"); |
| 70 | + System.out.println("windowsWithAvx2 = " + windowsWithAvx2.evaluatePredicate()); |
| 71 | + System.out.flush(); |
| 72 | + |
| 73 | + LibraryInfo info = new LibraryInfo( |
| 74 | + new DirectoryPath("linux/x86-64/com/github/stephengold"), |
| 75 | + "joltjni", DirectoryPath.USER_DIR); |
| 76 | + NativeBinaryLoader loader = new NativeBinaryLoader(info); |
| 77 | + NativeDynamicLibrary[] libraries = { |
| 78 | + new NativeDynamicLibrary("linux/x86-64-fma/com/github/stephengold", linuxWithFma), // must precede vanilla LINUX_X86_64 |
| 79 | + new NativeDynamicLibrary("linux/x86-64/com/github/stephengold", PlatformPredicate.LINUX_X86_64), |
| 80 | + new NativeDynamicLibrary("windows/x86-64-avx2/com/github/stephengold", windowsWithAvx2), // must precede vanilla WIN_X86_64 |
| 81 | + new NativeDynamicLibrary("windows/x86-64/com/github/stephengold", PlatformPredicate.WIN_X86_64) |
| 82 | + }; |
| 83 | + loader.registerNativeLibraries(libraries).initPlatformLibrary(); |
| 84 | + loader.setLoggingEnabled(true); |
| 85 | + loader.setRetryWithCleanExtraction(true); |
| 86 | + try { |
| 87 | + loader.loadLibrary(LoadingCriterion.CLEAN_EXTRACTION); |
| 88 | + } catch (Exception e) { |
| 89 | + throw new IllegalStateException("Failed to load the joltjni library!"); |
| 90 | + } |
| 91 | + System.err.flush(); |
| 92 | + |
| 93 | + // Invoke native code to obtain the configuration of the native library. |
| 94 | + String configuration = Jolt.getConfigurationString(); |
| 95 | + /* |
| 96 | + * Depending which native library was loaded, the configuration string |
| 97 | + * should be one of the following: |
| 98 | + * |
| 99 | + * On LINUX_X86_64 platforms, either |
| 100 | + * 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) |
| 101 | + * or |
| 102 | + * Single precision x86 64-bit with instructions: SSE2 (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) |
| 103 | + * |
| 104 | + * On WIN_X86_64 platforms, either |
| 105 | + * 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) |
| 106 | + * or |
| 107 | + * Single precision x86 64-bit with instructions: SSE2 (FP Exceptions) (Debug Renderer) (16-bit ObjectLayer) (Assertions) (ObjectStream) (Debug) (C++ RTTI) (C++ Exceptions) |
| 108 | + */ |
| 109 | + System.out.println(configuration); |
| 110 | + } |
| 111 | +} |
0 commit comments