-
-
Notifications
You must be signed in to change notification settings - Fork 69
Add API to sort array of custom objects #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c42a0d
Add method to sort array/vector of custom objects
8f8dd4a
Add benchmarks for objsort
38fbbc8
Use the permute array in-line
0d6ffa9
Use distance
9a92ab0
Use key-value sort instead of argsort
fbc033e
Add more distance metrics
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#include <cmath> | ||
|
||
static constexpr char x[] = "x"; | ||
static constexpr char euclidean[] = "euclidean"; | ||
static constexpr char taxicab[] = "taxicab"; | ||
static constexpr char chebyshev[] = "chebyshev"; | ||
|
||
template <const char* val> | ||
struct Point3D { | ||
double x; | ||
double y; | ||
double z; | ||
static constexpr std::string_view name {val}; | ||
Point3D() | ||
{ | ||
x = (double)rand() / RAND_MAX; | ||
y = (double)rand() / RAND_MAX; | ||
z = (double)rand() / RAND_MAX; | ||
} | ||
double distance() | ||
{ | ||
if constexpr (name == "x") { | ||
return x; | ||
} | ||
else if constexpr (name == "euclidean") { | ||
return std::sqrt(x * x + y * y + z * z); | ||
} | ||
else if constexpr (name == "taxicab") { | ||
return abs(x) + abs(y) + abs(z); | ||
} | ||
else if constexpr (name == "chebyshev") { | ||
return std::max(std::max(x, y), z); | ||
} | ||
} | ||
}; | ||
|
||
template <typename T> | ||
std::vector<T> init_data(const int size) | ||
{ | ||
srand(42); | ||
std::vector<T> arr; | ||
for (auto ii = 0; ii < size; ++ii) { | ||
T temp; | ||
arr.push_back(temp); | ||
} | ||
return arr; | ||
} | ||
|
||
template <typename T> | ||
struct less_than_key { | ||
inline bool operator()(T &p1, T &p2) | ||
{ | ||
return (p1.distance() < p2.distance()); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
static void scalarobjsort(benchmark::State &state) | ||
{ | ||
// set up array | ||
std::vector<T> arr = init_data<T>(state.range(0)); | ||
std::vector<T> arr_bkp = arr; | ||
// benchmark | ||
for (auto _ : state) { | ||
std::sort(arr.begin(), arr.end(), less_than_key<T>()); | ||
state.PauseTiming(); | ||
arr = arr_bkp; | ||
state.ResumeTiming(); | ||
} | ||
} | ||
|
||
template <typename T> | ||
static void simdobjsort(benchmark::State &state) | ||
{ | ||
// set up array | ||
std::vector<T> arr = init_data<T>(state.range(0)); | ||
std::vector<T> arr_bkp = arr; | ||
// benchmark | ||
for (auto _ : state) { | ||
x86simdsort::object_qsort(arr.data(), arr.size(), [](T p) -> double { | ||
return p.distance(); | ||
}); | ||
state.PauseTiming(); | ||
if (!std::is_sorted(arr.begin(), arr.end(), less_than_key<T>())) { | ||
std::cout << "sorting failed \n"; | ||
} | ||
arr = arr_bkp; | ||
state.ResumeTiming(); | ||
} | ||
} | ||
|
||
#define BENCHMARK_OBJSORT(func, T) \ | ||
BENCHMARK_TEMPLATE(func, T) \ | ||
->Arg(10e1) \ | ||
->Arg(10e2) \ | ||
->Arg(10e3) \ | ||
->Arg(10e4) \ | ||
->Arg(10e5) \ | ||
->Arg(10e6); | ||
|
||
BENCHMARK_OBJSORT(simdobjsort, Point3D<x>) | ||
BENCHMARK_OBJSORT(scalarobjsort, Point3D<x>) | ||
BENCHMARK_OBJSORT(simdobjsort, Point3D<taxicab>) | ||
BENCHMARK_OBJSORT(scalarobjsort, Point3D<taxicab>) | ||
BENCHMARK_OBJSORT(simdobjsort, Point3D<euclidean>) | ||
BENCHMARK_OBJSORT(scalarobjsort, Point3D<euclidean>) | ||
BENCHMARK_OBJSORT(simdobjsort, Point3D<chebyshev>) | ||
BENCHMARK_OBJSORT(scalarobjsort, Point3D<chebyshev>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
#include <stdint.h> | ||
#include <vector> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <numeric> | ||
|
||
#define XSS_EXPORT_SYMBOL __attribute__((visibility("default"))) | ||
#define XSS_HIDE_SYMBOL __attribute__((visibility("hidden"))) | ||
|
@@ -34,10 +36,49 @@ template <typename T> | |
XSS_EXPORT_SYMBOL std::vector<size_t> | ||
argselect(T *arr, size_t k, size_t arrsize, bool hasnan = false); | ||
|
||
// argselect | ||
// keyvalue sort | ||
template <typename T1, typename T2> | ||
XSS_EXPORT_SYMBOL void | ||
keyvalue_qsort(T1 *key, T2* val, size_t arrsize, bool hasnan = false); | ||
|
||
// sort an object | ||
template <typename T, typename Func> | ||
XSS_EXPORT_SYMBOL void object_qsort(T *arr, size_t arrsize, Func key_func) | ||
{ | ||
/* (1) Create a vector a keys */ | ||
using return_type_of = | ||
typename decltype(std::function {key_func})::result_type; | ||
std::vector<return_type_of> keys; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add: |
||
keys.reserve(arrsize); | ||
for (size_t ii = 0; ii < arrsize; ++ii) { | ||
keys[ii] = key_func(arr[ii]); | ||
} | ||
|
||
/* (2) Call arg based on keys using the keyvalue sort */ | ||
std::vector<size_t> arg(arrsize); | ||
std::iota(arg.begin(), arg.end(), 0); | ||
keyvalue_qsort(keys.data(), arg.data(), arrsize); | ||
|
||
/* (3) Permute obj array in-place */ | ||
std::vector<bool> done(arrsize); | ||
for (size_t i = 0; i < arrsize; ++i) | ||
{ | ||
if (done[i]) | ||
{ | ||
continue; | ||
} | ||
done[i] = true; | ||
size_t prev_j = i; | ||
size_t j = arg[i]; | ||
while (i != j) | ||
{ | ||
std::swap(arr[prev_j], arr[j]); | ||
done[j] = true; | ||
prev_j = j; | ||
j = arg[j]; | ||
} | ||
} | ||
} | ||
|
||
} // namespace x86simdsort | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably a C++17 technique (CTAD). If you need to support pre-C++17, you may need to rewrite it.