Skip to content

Commit 0f19393

Browse files
committed
Add testCheckAvx512CompatibilityMatchesCpuinfo
1 parent 6e1c90f commit 0f19393

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.jbellis.jvector.vector.cnative;
18+
19+
import org.junit.Assume;
20+
import org.junit.Test;
21+
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.List;
26+
27+
import static org.junit.Assert.assertEquals;
28+
29+
public class NativeSimdOpsTest {
30+
31+
/**
32+
* Reads /proc/cpuinfo and returns true if all AVX-512 flags required by
33+
* check_avx512_compatibility() are present: avx512f, avx512cd, avx512dq,
34+
* avx512bw, avx512vl.
35+
*/
36+
private static boolean cpuinfoReportsAvx512() throws IOException {
37+
List<String> lines = Files.readAllLines(Path.of("/proc/cpuinfo"));
38+
List<String> required = List.of("avx512f", "avx512cd", "avx512dq", "avx512bw", "avx512vl");
39+
for (String line : lines) {
40+
if (line.startsWith("flags")) {
41+
String[] flags = line.split("\\s+");
42+
List<String> flagList = List.of(flags);
43+
return flagList.containsAll(required);
44+
}
45+
}
46+
return false;
47+
}
48+
49+
@Test
50+
public void testCheckAvx512CompatibilityMatchesCpuinfo() throws IOException {
51+
boolean libraryLoaded = LibraryLoader.loadJvector();
52+
Assume.assumeTrue("Native jvector library not available; skipping AVX-512 check", libraryLoaded);
53+
54+
boolean expectedFromCpuinfo = cpuinfoReportsAvx512();
55+
boolean actualFromNative = NativeSimdOps.check_avx512_compatibility();
56+
57+
assertEquals(
58+
"check_avx512_compatibility() should match AVX-512 flag presence in /proc/cpuinfo",
59+
expectedFromCpuinfo,
60+
actualFromNative);
61+
}
62+
}

0 commit comments

Comments
 (0)