Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sort_by_keys_std_sort.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
20#include <algorithm>
21#include <utility>
22#include <vector>
23
24namespace shamalgs::primitives::device::details {
25
27 template<class Tkey, class Tval>
29 sham::DeviceBuffer<Tkey> &buf_key, sham::DeviceBuffer<Tval> &buf_values, u32 len) {
30
31 std::vector<Tkey> key_stdvec = buf_key.copy_to_stdvec();
32 std::vector<Tval> val_stdvec = buf_values.copy_to_stdvec();
33
34 std::vector<std::pair<Tkey, Tval>> zipped(len);
35 for (u32 i = 0; i < len; ++i) {
36 zipped[i] = std::make_pair(key_stdvec[i], val_stdvec[i]);
37 }
38
39 std::sort(zipped.begin(), zipped.end(), [](const auto &a, const auto &b) {
40 return a.first < b.first;
41 });
42
43 for (u32 i = 0; i < len; ++i) {
44 key_stdvec[i] = zipped[i].first;
45 val_stdvec[i] = zipped[i].second;
46 }
47
48 buf_key.copy_from_stdvec(key_stdvec);
49 buf_values.copy_from_stdvec(val_stdvec);
50 }
51
52} // namespace shamalgs::primitives::device::details
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
void sort_by_keys_std_sort(sham::DeviceBuffer< Tkey > &buf_key, sham::DeviceBuffer< Tval > &buf_values, u32 len)
Copy both buffers to host, std::sort the zipped key/value pairs, and copy back.