Skip to content

Commit 8830d65

Browse files
committed
Pufferfish SMID Utilities
1 parent fb55d3e commit 8830d65

19 files changed

+194
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: MrHua269 <wangxyper@163.com>
3+
Date: Tue, 6 Aug 2024 14:32:22 +0800
4+
Subject: [PATCH] Pufferfish SMID Utilities
5+
6+
7+
diff --git a/build.gradle.kts b/build.gradle.kts
8+
index 540fe7e2c110e79c3742f229b3ed8c54b101d260..3f04b36591899040a8b602db6475de492755ffb5 100644
9+
--- a/build.gradle.kts
10+
+++ b/build.gradle.kts
11+
@@ -130,6 +130,13 @@ val generateApiVersioningFile by tasks.registering {
12+
}
13+
}
14+
15+
+// Pufferfish Start
16+
+tasks.withType<JavaCompile> {
17+
+ val compilerArgs = options.compilerArgs
18+
+ compilerArgs.add("--add-modules=jdk.incubator.vector")
19+
+}
20+
+// Pufferfish End
21+
+
22+
tasks.jar {
23+
from(generateApiVersioningFile.map { it.outputs.files.singleFile }) {
24+
into("META-INF/maven/${project.group}/${project.name}")
25+
diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java
26+
new file mode 100644
27+
index 0000000000000000000000000000000000000000..3441cdad70da1bd523c5933b1a914688718c2657
28+
--- /dev/null
29+
+++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java
30+
@@ -0,0 +1,40 @@
31+
+package gg.pufferfish.pufferfish.simd;
32+
+
33+
+import java.util.logging.Level;
34+
+import java.util.logging.Logger;
35+
+import jdk.incubator.vector.FloatVector;
36+
+import jdk.incubator.vector.IntVector;
37+
+import jdk.incubator.vector.VectorSpecies;
38+
+
39+
+/**
40+
+ * Basically, java is annoying and we have to push this out to its own class.
41+
+ */
42+
+@Deprecated
43+
+public class SIMDChecker {
44+
+
45+
+ @Deprecated
46+
+ public static boolean canEnable(Logger logger) {
47+
+ try {
48+
+ if (SIMDDetection.getJavaVersion() < 17 || SIMDDetection.getJavaVersion() > 21) {
49+
+ return false;
50+
+ } else {
51+
+ SIMDDetection.testRun = true;
52+
+
53+
+ VectorSpecies<Integer> ISPEC = IntVector.SPECIES_PREFERRED;
54+
+ VectorSpecies<Float> FSPEC = FloatVector.SPECIES_PREFERRED;
55+
+
56+
+ logger.log(Level.INFO, "Max SIMD vector size on this system is " + ISPEC.vectorBitSize() + " bits (int)");
57+
+ logger.log(Level.INFO, "Max SIMD vector size on this system is " + FSPEC.vectorBitSize() + " bits (float)");
58+
+
59+
+ if (ISPEC.elementSize() < 2 || FSPEC.elementSize() < 2) {
60+
+ logger.log(Level.WARNING, "SIMD is not properly supported on this system!");
61+
+ return false;
62+
+ }
63+
+
64+
+ return true;
65+
+ }
66+
+ } catch (NoClassDefFoundError | Exception ignored) {} // Basically, we don't do anything. This lets us detect if it's not functional and disable it.
67+
+ return false;
68+
+ }
69+
+
70+
+}
71+
diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java
72+
new file mode 100644
73+
index 0000000000000000000000000000000000000000..a84889d3e9cfc4d7ab5f867820a6484c6070711b
74+
--- /dev/null
75+
+++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java
76+
@@ -0,0 +1,35 @@
77+
+package gg.pufferfish.pufferfish.simd;
78+
+
79+
+import java.util.logging.Logger;
80+
+
81+
+@Deprecated
82+
+public class SIMDDetection {
83+
+
84+
+ public static boolean isEnabled = false;
85+
+ public static boolean versionLimited = false;
86+
+ public static boolean testRun = false;
87+
+
88+
+ @Deprecated
89+
+ public static boolean canEnable(Logger logger) {
90+
+ try {
91+
+ return SIMDChecker.canEnable(logger);
92+
+ } catch (NoClassDefFoundError | Exception ignored) {
93+
+ return false;
94+
+ }
95+
+ }
96+
+
97+
+ @Deprecated
98+
+ public static int getJavaVersion() {
99+
+ // https://stackoverflow.com/a/2591122
100+
+ String version = System.getProperty("java.version");
101+
+ if(version.startsWith("1.")) {
102+
+ version = version.substring(2, 3);
103+
+ } else {
104+
+ int dot = version.indexOf(".");
105+
+ if(dot != -1) { version = version.substring(0, dot); }
106+
+ }
107+
+ version = version.split("-")[0]; // Azul is stupid
108+
+ return Integer.parseInt(version);
109+
+ }
110+
+
111+
+}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: MrHua269 <wangxyper@163.com>
3+
Date: Tue, 6 Aug 2024 14:31:20 +0800
4+
Subject: [PATCH] Pufferfish SMID Utilities
5+
6+
7+
diff --git a/build.gradle.kts b/build.gradle.kts
8+
index c2d3d699edfd60d773af96116c5663c812c691e9..1049681dfa7e48b4b6c29f4b1a09c689ac918500 100644
9+
--- a/build.gradle.kts
10+
+++ b/build.gradle.kts
11+
@@ -76,6 +76,14 @@ paperweight {
12+
craftBukkitPackageVersion.set("v1_21_R1") // also needs to be updated in MappingEnvironment
13+
}
14+
15+
+
16+
+// Pufferfish Start
17+
+tasks.withType<JavaCompile> {
18+
+ val compilerArgs = options.compilerArgs
19+
+ compilerArgs.add("--add-modules=jdk.incubator.vector")
20+
+}
21+
+// Pufferfish End
22+
+
23+
tasks.jar {
24+
archiveClassifier.set("dev")
25+
26+
diff --git a/src/main/java/me/earthme/luminol/config/modules/optimizations/SMIDConfig.java b/src/main/java/me/earthme/luminol/config/modules/optimizations/SMIDConfig.java
27+
new file mode 100644
28+
index 0000000000000000000000000000000000000000..b2b4a91925145b331a604384afee23ab276d76be
29+
--- /dev/null
30+
+++ b/src/main/java/me/earthme/luminol/config/modules/optimizations/SMIDConfig.java
31+
@@ -0,0 +1,52 @@
32+
+package me.earthme.luminol.config.modules.optimizations;
33+
+
34+
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
35+
+import com.mojang.logging.LogUtils;
36+
+import me.earthme.luminol.config.ConfigInfo;
37+
+import me.earthme.luminol.config.DoNotLoad;
38+
+import me.earthme.luminol.config.EnumConfigCategory;
39+
+import me.earthme.luminol.config.IConfigModule;
40+
+import org.slf4j.Logger;
41+
+
42+
+public class SMIDConfig implements IConfigModule {
43+
+ @DoNotLoad
44+
+ private static final Logger LOGGER = LogUtils.getLogger();
45+
+ @ConfigInfo(baseName = "enabled")
46+
+ public static boolean enabled = true;
47+
+
48+
+ @Override
49+
+ public EnumConfigCategory getCategory() {
50+
+ return EnumConfigCategory.OPTIMIZATIONS;
51+
+ }
52+
+
53+
+ @Override
54+
+ public String getBaseName() {
55+
+ return "use_smid";
56+
+ }
57+
+
58+
+ @Override
59+
+ public void onLoaded(CommentedFileConfig configInstance) {
60+
+ if (!enabled){
61+
+ return;
62+
+ }
63+
+
64+
+ // Attempt to detect vectorization
65+
+ try {
66+
+ SIMDDetection.isEnabled = SIMDDetection.canEnable(LOGGER);
67+
+ SIMDDetection.versionLimited = SIMDDetection.getJavaVersion() < 17 || SIMDDetection.getJavaVersion() > 21;
68+
+ } catch (NoClassDefFoundError | Exception ignored) {
69+
+ ignored.printStackTrace();
70+
+ }
71+
+
72+
+ if (SIMDDetection.isEnabled) {
73+
+ LOGGER.info("SIMD operations detected as functional. Will replace some operations with faster versions.");
74+
+ } else if (SIMDDetection.versionLimited) {
75+
+ LOGGER.warn("Will not enable SIMD! These optimizations are only safely supported on Java 17-21.");
76+
+ } else {
77+
+ LOGGER.warn("SIMD operations are available for your server, but are not configured!");
78+
+ LOGGER.warn("To enable additional optimizations, add \"--add-modules=jdk.incubator.vector\" to your startup flags, BEFORE the \"-jar\".");
79+
+ LOGGER.warn("If you have already added this flag, then SIMD operations are not supported on your JVM or CPU.");
80+
+ LOGGER.warn("Debug: Java: " + System.getProperty("java.version") + ", test run: " + SIMDDetection.testRun);
81+
+ }
82+
+ }
83+
+}

0 commit comments

Comments
 (0)