Skip to content

Commit 58501d0

Browse files
author
Raghuveer Devulapalli
authored
Merge pull request #15 from r-devulap/maint
MAINT: cleanup code and split tests
2 parents 28c0b3e + a2c1c0b commit 58501d0

File tree

8 files changed

+128
-142
lines changed

8 files changed

+128
-142
lines changed

Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ TESTOBJS := $(filter-out $(TESTDIR)/main.o ,$(TESTOBJS))
1010
CXXFLAGS += -I$(SRCDIR) -I$(UTILS)
1111
GTESTCFLAGS = `pkg-config --cflags gtest`
1212
GTESTLDFLAGS = `pkg-config --libs gtest`
13+
MARCHFLAG = -march=icelake-client -O3
1314

1415
all : test bench
1516

17+
$(UTILS)/cpuinfo.o : $(UTILS)/cpuinfo.cpp
18+
$(CXX) $(CXXFLAGS) -c $(UTILS)/cpuinfo.cpp -o $(UTILS)/cpuinfo.o
19+
1620
$(TESTDIR)/%.o : $(TESTDIR)/%.cpp $(SRCS)
17-
$(CXX) -march=icelake-client -O3 $(CXXFLAGS) $(GTESTCFLAGS) -c $< -o $@
21+
$(CXX) $(CXXFLAGS) $(MARCHFLAG) $(GTESTCFLAGS) -c $< -o $@
1822

19-
test: $(TESTDIR)/main.cpp $(TESTOBJS) $(SRCS)
20-
$(CXX) tests/main.cpp $(TESTOBJS) $(CXXFLAGS) $(GTESTLDFLAGS) -o testexe
23+
test: $(TESTDIR)/main.cpp $(TESTOBJS) $(UTILS)/cpuinfo.o $(SRCS)
24+
$(CXX) tests/main.cpp $(TESTOBJS) $(UTILS)/cpuinfo.o $(MARCHFLAG) $(CXXFLAGS) $(GTESTLDFLAGS) -o testexe
2125

22-
bench: $(BENCHDIR)/main.cpp $(SRCS)
23-
$(CXX) $(BENCHDIR)/main.cpp $(CXXFLAGS) -march=icelake-client -O3 -o benchexe
26+
bench: $(BENCHDIR)/main.cpp $(SRCS) $(UTILS)/cpuinfo.o
27+
$(CXX) $(BENCHDIR)/main.cpp $(CXXFLAGS) $(UTILS)/cpuinfo.o $(MARCHFLAG) -o benchexe
2428

2529
clean:
26-
rm -f $(TESTDIR)/*.o testexe benchexe
30+
rm -f $(TESTDIR)/*.o testexe benchexe

src/avx512-64bit-keyvaluesort.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,6 @@ void heap_sort(type_t *keys, uint64_t *indexes, int64_t size)
803803
}
804804
}
805805

806-
template <typename T>
807-
struct sortkv_t {
808-
T key;
809-
uint64_t value;
810-
};
811806
template <typename vtype, typename type_t>
812807
void qsort_64bit_(type_t *keys,
813808
uint64_t *indexes,

tests/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* * SPDX-License-Identifier: BSD-3-Clause
44
* *******************************************/
55

6+
#include "avx512-16bit-qsort.hpp"
7+
#include "avx512-32bit-qsort.hpp"
8+
#include "avx512-64bit-keyvaluesort.hpp"
9+
#include "avx512-64bit-qsort.hpp"
610
#include <gtest/gtest.h>
711

812
int main(int argc, char **argv)

tests/test_all.cpp renamed to tests/test_keyvalue.cpp

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,17 @@
33
* * SPDX-License-Identifier: BSD-3-Clause
44
* *******************************************/
55

6-
#include "avx512-16bit-qsort.hpp"
7-
#include "avx512-32bit-qsort.hpp"
8-
#include "avx512-64bit-keyvaluesort.hpp"
9-
#include "avx512-64bit-qsort.hpp"
10-
#include "cpuinfo.h"
6+
#include "avx512-common-keyvaluesort.h"
117
#include "rand_array.h"
128
#include <gtest/gtest.h>
139
#include <vector>
1410

15-
template <typename T>
16-
class avx512_sort : public ::testing::Test {
17-
};
18-
TYPED_TEST_SUITE_P(avx512_sort);
19-
20-
TYPED_TEST_P(avx512_sort, test_arrsizes)
21-
{
22-
if (cpu_has_avx512bw()) {
23-
if ((sizeof(TypeParam) == 2) && (!cpu_has_avx512_vbmi2())) {
24-
GTEST_SKIP() << "Skipping this test, it requires avx512_vbmi2";
25-
}
26-
std::vector<int64_t> arrsizes;
27-
for (int64_t ii = 0; ii < 1024; ++ii) {
28-
arrsizes.push_back((TypeParam)ii);
29-
}
30-
std::vector<TypeParam> arr;
31-
std::vector<TypeParam> sortedarr;
32-
for (size_t ii = 0; ii < arrsizes.size(); ++ii) {
33-
/* Random array */
34-
arr = get_uniform_rand_array<TypeParam>(arrsizes[ii]);
35-
sortedarr = arr;
36-
/* Sort with std::sort for comparison */
37-
std::sort(sortedarr.begin(), sortedarr.end());
38-
avx512_qsort<TypeParam>(arr.data(), arr.size());
39-
ASSERT_EQ(sortedarr, arr);
40-
arr.clear();
41-
sortedarr.clear();
42-
}
43-
}
44-
else {
45-
GTEST_SKIP() << "Skipping this test, it requires avx512bw";
46-
}
47-
}
48-
49-
REGISTER_TYPED_TEST_SUITE_P(avx512_sort, test_arrsizes);
50-
51-
using Types = testing::Types<uint16_t,
52-
int16_t,
53-
float,
54-
double,
55-
uint32_t,
56-
int32_t,
57-
uint64_t,
58-
int64_t>;
59-
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort, Types);
60-
6111
template <typename K, typename V = uint64_t>
6212
struct sorted_t {
6313
K key;
6414
K value;
6515
};
16+
6617
template <typename K, typename V = uint64_t>
6718
bool compare(sorted_t<K, V> a, sorted_t<K, V> b)
6819
{
@@ -87,7 +38,8 @@ TYPED_TEST_P(TestKeyValueSort, KeyValueSort)
8738

8839
for (size_t ii = 0; ii < keysizes.size(); ++ii) {
8940
/* Random array */
90-
keys = get_uniform_rand_array_key<TypeParam>(keysizes[ii]);
41+
keys =
42+
get_uniform_rand_array_with_uniquevalues<TypeParam>(keysizes[ii]);
9143
values = get_uniform_rand_array<uint64_t>(keysizes[ii]);
9244
for (size_t i = 0; i < keys.size(); i++) {
9345
sorted_t<TypeParam, uint64_t> tmp_s;
@@ -113,4 +65,4 @@ TYPED_TEST_P(TestKeyValueSort, KeyValueSort)
11365
REGISTER_TYPED_TEST_SUITE_P(TestKeyValueSort, KeyValueSort);
11466

11567
using TypesKv = testing::Types<double, uint64_t, int64_t>;
116-
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefixKv, TestKeyValueSort, TypesKv);
68+
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefixKv, TestKeyValueSort, TypesKv);

tests/test_qsort.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*******************************************
2+
* * Copyright (C) 2022 Intel Corporation
3+
* * SPDX-License-Identifier: BSD-3-Clause
4+
* *******************************************/
5+
6+
#include "avx512-common-qsort.h"
7+
#include "cpuinfo.h"
8+
#include "rand_array.h"
9+
#include <gtest/gtest.h>
10+
#include <vector>
11+
12+
template <typename T>
13+
class avx512_sort : public ::testing::Test {
14+
};
15+
TYPED_TEST_SUITE_P(avx512_sort);
16+
17+
TYPED_TEST_P(avx512_sort, test_arrsizes)
18+
{
19+
if (cpu_has_avx512bw()) {
20+
if ((sizeof(TypeParam) == 2) && (!cpu_has_avx512_vbmi2())) {
21+
GTEST_SKIP() << "Skipping this test, it requires avx512_vbmi2";
22+
}
23+
std::vector<int64_t> arrsizes;
24+
for (int64_t ii = 0; ii < 1024; ++ii) {
25+
arrsizes.push_back((TypeParam)ii);
26+
}
27+
std::vector<TypeParam> arr;
28+
std::vector<TypeParam> sortedarr;
29+
for (size_t ii = 0; ii < arrsizes.size(); ++ii) {
30+
/* Random array */
31+
arr = get_uniform_rand_array<TypeParam>(arrsizes[ii]);
32+
sortedarr = arr;
33+
/* Sort with std::sort for comparison */
34+
std::sort(sortedarr.begin(), sortedarr.end());
35+
avx512_qsort<TypeParam>(arr.data(), arr.size());
36+
ASSERT_EQ(sortedarr, arr);
37+
arr.clear();
38+
sortedarr.clear();
39+
}
40+
}
41+
else {
42+
GTEST_SKIP() << "Skipping this test, it requires avx512bw";
43+
}
44+
}
45+
46+
REGISTER_TYPED_TEST_SUITE_P(avx512_sort, test_arrsizes);
47+
48+
using Types = testing::Types<uint16_t,
49+
int16_t,
50+
float,
51+
double,
52+
uint32_t,
53+
int32_t,
54+
uint64_t,
55+
int64_t>;
56+
INSTANTIATE_TYPED_TEST_SUITE_P(TestPrefix, avx512_sort, Types);

utils/cpuinfo.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************
2+
* * Copyright (C) 2022 Intel Corporation
3+
* * SPDX-License-Identifier: BSD-3-Clause
4+
* *******************************************/
5+
6+
#include "cpuinfo.h"
7+
8+
static void
9+
cpuid(uint32_t feature, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d)
10+
{
11+
__asm__ volatile(
12+
"cpuid"
13+
"\n\t"
14+
: "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
15+
: "a"(feature), "c"(0));
16+
}
17+
18+
int cpu_has_avx512_vbmi2()
19+
{
20+
uint32_t eax(0), ebx(0), ecx(0), edx(0);
21+
cpuid(0x07, &eax, &ebx, &ecx, &edx);
22+
return (ecx >> 6) & 0x1;
23+
}
24+
25+
int cpu_has_avx512bw()
26+
{
27+
uint32_t eax(0), ebx(0), ecx(0), edx(0);
28+
cpuid(0x07, &eax, &ebx, &ecx, &edx);
29+
return (ebx >> 30) & 0x1;
30+
}
31+
32+
// TODO:
33+
//int check_os_supports_avx512()
34+
//{
35+
// uint32_t eax(0), ebx(0), ecx(0), edx(0);
36+
// cpuid(0x01, &eax, &ebx, &ecx, &edx);
37+
// // XSAVE:
38+
// if ((ecx >> 27) & 0x1) {
39+
// uint32_t xget_eax, xget_edx, index(0);
40+
// __asm__ ("xgetbv" : "=a"(xget_eax), "=d"(xget_edx) : "c" (index))
41+
// }
42+
//
43+
//}

utils/cpuinfo.h

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,9 @@
33
* * SPDX-License-Identifier: BSD-3-Clause
44
* *******************************************/
55

6+
#include <stdint.h>
67
#include <cpuid.h>
78

8-
static void
9-
cpuid(uint32_t feature, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d)
10-
{
11-
__asm__ volatile(
12-
"cpuid"
13-
"\n\t"
14-
: "=a"(*a), "=b"(*b), "=c"(*c), "=d"(*d)
15-
: "a"(feature), "c"(0));
16-
}
9+
int cpu_has_avx512_vbmi2();
1710

18-
int cpu_has_avx512_vbmi2()
19-
{
20-
uint32_t eax(0), ebx(0), ecx(0), edx(0);
21-
cpuid(0x07, &eax, &ebx, &ecx, &edx);
22-
return (ecx >> 6) & 0x1;
23-
}
24-
25-
int cpu_has_avx512bw()
26-
{
27-
uint32_t eax(0), ebx(0), ecx(0), edx(0);
28-
cpuid(0x07, &eax, &ebx, &ecx, &edx);
29-
return (ebx >> 30) & 0x1;
30-
}
31-
32-
// TODO:
33-
//int check_os_supports_avx512()
34-
//{
35-
// uint32_t eax(0), ebx(0), ecx(0), edx(0);
36-
// cpuid(0x01, &eax, &ebx, &ecx, &edx);
37-
// // XSAVE:
38-
// if ((ecx >> 27) & 0x1) {
39-
// uint32_t xget_eax, xget_edx, index(0);
40-
// __asm__ ("xgetbv" : "=a"(xget_eax), "=d"(xget_edx) : "c" (index))
41-
// }
42-
//
43-
//}
11+
int cpu_has_avx512bw();

utils/rand_array.h

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,16 @@ static std::vector<T> get_uniform_rand_array(
4141
}
4242
return arr;
4343
}
44-
template <typename T>
45-
static std::vector<T> get_uniform_rand_array_key(
46-
int64_t arrsize,
47-
T max = std::numeric_limits<T>::max(),
48-
T min = std::numeric_limits<T>::min(),
49-
typename std::enable_if<std::is_integral<T>::value>::type * = 0)
50-
{
51-
std::vector<T> arr;
52-
std::random_device r;
53-
std::default_random_engine e1(r());
54-
std::uniform_int_distribution<T> uniform_dist(min, max);
55-
for (int64_t ii = 0; ii < arrsize; ++ii) {
56-
57-
while (true) {
58-
T tmp = uniform_dist(e1);
59-
auto iter = std::find(arr.begin(), arr.end(), tmp);
60-
if (iter == arr.end()) {
61-
arr.emplace_back(tmp);
62-
break;
63-
}
64-
}
65-
}
66-
return arr;
67-
}
6844

6945
template <typename T>
70-
static std::vector<T> get_uniform_rand_array_key(
46+
static std::vector<T> get_uniform_rand_array_with_uniquevalues(
7147
int64_t arrsize,
7248
T max = std::numeric_limits<T>::max(),
73-
T min = std::numeric_limits<T>::min(),
74-
typename std::enable_if<std::is_floating_point<T>::value>::type * = 0)
49+
T min = std::numeric_limits<T>::min())
7550
{
76-
std::random_device rd;
77-
std::mt19937 gen(rd());
78-
std::uniform_real_distribution<T> dis(min, max);
79-
std::vector<T> arr;
80-
for (int64_t ii = 0; ii < arrsize; ++ii) {
81-
82-
while (true) {
83-
T tmp = dis(gen);
84-
auto iter = std::find(arr.begin(), arr.end(), tmp);
85-
if (iter == arr.end()) {
86-
arr.emplace_back(tmp);
87-
break;
88-
}
89-
}
90-
}
51+
std::vector<T> arr = get_uniform_rand_array<T>(arrsize, max, min);
52+
typename std::vector<T>::iterator ip =
53+
std::unique(arr.begin(), arr.begin() + arrsize);
54+
arr.resize(std::distance(arr.begin(), ip));
9155
return arr;
92-
}
56+
}

0 commit comments

Comments
 (0)