A simple parallel sort as a demonstration of how easy (and fast) this is in C++
Revisión | ed8c28789540c0e9f712bb34383bf6db22ed694b (tree) |
---|---|
Tiempo | 2018-12-16 15:54:51 |
Autor | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Switched to using raw arrays instead of vectors. <obj size
I didn't need any of the nice things vectors give you, no resizing
or anything. The executable shrank quite significantly, even
though I'm still using a deque.
@@ -4,11 +4,12 @@ | ||
4 | 4 | #include <deque> |
5 | 5 | #include <random> |
6 | 6 | #include <utility> |
7 | +#include <memory> | |
7 | 8 | #include <cstdint> |
8 | 9 | //#include <fstream> |
9 | 10 | |
10 | -using vecsize_t = ::std::vector<::std::uint32_t>::size_type; | |
11 | -using veciter_t = ::std::vector<::std::uint32_t>::iterator; | |
11 | +using vecsize_t = ::std::uint64_t; | |
12 | +using veciter_t = ::std::uint32_t *; | |
12 | 13 | |
13 | 14 | void gen_and_sort_section(const veciter_t &begin, const veciter_t &end, int seed) |
14 | 15 | { |
@@ -58,22 +59,25 @@ | ||
58 | 59 | |
59 | 60 | int main() |
60 | 61 | { |
61 | - using ::std::vector; | |
62 | 62 | using ::std::thread; |
63 | + using ::std::unique_ptr; | |
64 | + using ::std::make_unique; | |
65 | + using ::std::uint32_t; | |
63 | 66 | |
64 | 67 | constexpr auto size = 1ULL << 31; |
65 | - ::std::vector<::std::uint32_t> huge_array(size); | |
68 | + auto huge_array = make_unique<uint32_t[]>(size); | |
66 | 69 | const int cpucount = ::std::max(thread::hardware_concurrency() / 2, 1U); |
67 | 70 | const int sections = cpucount; |
68 | - vector<decltype(huge_array.begin())> partitions(sections + 1); | |
71 | + const int secmarkers = sections + 1; | |
72 | + auto partitions = make_unique<veciter_t[]>(secmarkers); | |
69 | 73 | |
70 | - partitions[0] = huge_array.begin(); | |
71 | - for (unsigned int i = 1; i < partitions.size(); ++i) { | |
74 | + partitions[0] = &(huge_array[0]); | |
75 | + for (int i = 1; i < secmarkers; ++i) { | |
72 | 76 | auto const prev_part = partitions[i - 1]; |
73 | - auto const remaining = partitions.size() - i; | |
74 | - partitions[i] = prev_part + (huge_array.end() - prev_part) / remaining; | |
77 | + auto const remaining = secmarkers - i; | |
78 | + partitions[i] = prev_part + (&(huge_array[size]) - prev_part) / remaining; | |
75 | 79 | } |
76 | - if (partitions[partitions.size() - 1] != huge_array.end()) { | |
80 | + if (partitions[secmarkers - 1] != &(huge_array[size])) { | |
77 | 81 | return 1; |
78 | 82 | } |
79 | 83 |