15
15
import org .apache .commons .lang .StringUtils ;
16
16
import org .apache .logging .log4j .LogManager ;
17
17
import org .apache .logging .log4j .Logger ;
18
+
18
19
import oshi .util .platform .mac .SysctlUtil ;
19
20
20
21
import java .nio .file .Files ;
27
28
import java .util .stream .Stream ;
28
29
29
30
public class PlatformUtils {
30
-
31
+ private static volatile Boolean isAVX2Supported ;
32
+ private static volatile Boolean isAVX512Supported ;
31
33
private static final Logger logger = LogManager .getLogger (PlatformUtils .class );
32
34
33
35
/**
@@ -41,22 +43,26 @@ public class PlatformUtils {
41
43
*/
42
44
public static boolean isAVX2SupportedBySystem () {
43
45
if (!Platform .isIntel () || Platform .isWindows ()) {
44
- return false ;
46
+ isAVX2Supported = false ;
45
47
}
46
48
47
- if (Platform .isMac ()) {
49
+ if (isAVX2Supported != null ) {
50
+ return isAVX2Supported ;
51
+ }
48
52
53
+ if (Platform .isMac ()) {
49
54
// sysctl or system control retrieves system info and allows processes with appropriate privileges
50
55
// to set system info. This system info contains the machine dependent cpu features that are supported by it.
51
56
// On MacOS, if the underlying processor supports AVX2 instruction set, it will be listed under the "leaf7"
52
57
// subset of instructions ("sysctl -a | grep machdep.cpu.leaf7_features").
53
58
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctl.3.html
54
59
try {
55
- return AccessController .doPrivileged ((PrivilegedExceptionAction <Boolean >) () -> {
60
+ isAVX2Supported = AccessController .doPrivileged ((PrivilegedExceptionAction <Boolean >) () -> {
56
61
String flags = SysctlUtil .sysctl ("machdep.cpu.leaf7_features" , "empty" );
57
62
return (flags .toLowerCase (Locale .ROOT )).contains ("avx2" );
58
63
});
59
64
} catch (Exception e ) {
65
+ isAVX2Supported = false ;
60
66
logger .error ("[KNN] Error fetching cpu flags info. [{}]" , e .getMessage (), e );
61
67
}
62
68
@@ -70,17 +76,18 @@ public static boolean isAVX2SupportedBySystem() {
70
76
// https://ark.intel.com/content/www/us/en/ark/products/199285/intel-pentium-gold-g6600-processor-4m-cache-4-20-ghz.html
71
77
String fileName = "/proc/cpuinfo" ;
72
78
try {
73
- return AccessController .doPrivileged (
79
+ isAVX2Supported = AccessController .doPrivileged (
74
80
(PrivilegedExceptionAction <Boolean >) () -> (Boolean ) Files .lines (Paths .get (fileName ))
75
81
.filter (s -> s .startsWith ("flags" ))
76
82
.anyMatch (s -> StringUtils .containsIgnoreCase (s , "avx2" ))
77
83
);
78
84
79
85
} catch (Exception e ) {
86
+ isAVX2Supported = false ;
80
87
logger .error ("[KNN] Error reading file [{}]. [{}]" , fileName , e .getMessage (), e );
81
88
}
82
89
}
83
- return false ;
90
+ return isAVX2Supported ;
84
91
}
85
92
86
93
public static boolean isAVX512SupportedBySystem () {
@@ -97,7 +104,11 @@ private static boolean areAVX512FlagsAvailable(String[] avx512) {
97
104
// https://github.com/facebookresearch/faiss/blob/main/faiss/CMakeLists.txt
98
105
99
106
if (!Platform .isIntel () || Platform .isMac () || Platform .isWindows ()) {
100
- return false ;
107
+ isAVX512Supported = false ;
108
+ }
109
+
110
+ if (isAVX512Supported != null ) {
111
+ return isAVX512Supported ;
101
112
}
102
113
103
114
if (Platform .isLinux ()) {
@@ -110,16 +121,16 @@ private static boolean areAVX512FlagsAvailable(String[] avx512) {
110
121
String fileName = "/proc/cpuinfo" ;
111
122
112
123
try {
113
- return AccessController .doPrivileged ((PrivilegedExceptionAction <Boolean >) () -> {
124
+ isAVX512Supported = AccessController .doPrivileged ((PrivilegedExceptionAction <Boolean >) () -> {
114
125
Stream <String > linestream = Files .lines (Paths .get (fileName ));
115
126
String flags = linestream .filter (line -> line .startsWith ("flags" )).findFirst ().orElse ("" );
116
127
return Arrays .stream (avx512 ).allMatch (flags ::contains );
117
128
});
118
-
119
129
} catch (PrivilegedActionException e ) {
130
+ isAVX512Supported = false ;
120
131
logger .error ("[KNN] Error reading file [{}]. [{}]" , fileName , e .getMessage (), e );
121
132
}
122
133
}
123
- return false ;
134
+ return isAVX512Supported ;
124
135
}
125
136
}
0 commit comments