2016-12-24 01:03:10 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__FreeBSD__)
|
2015-11-21 12:47:41 +00:00
|
|
|
#include <malloc.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
#endif
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/bit_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/RadixSort.h>
|
|
|
|
#include <Common/Stopwatch.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <Core/Defines.h>
|
2015-11-21 12:47:41 +00:00
|
|
|
|
|
|
|
using Key = double;
|
|
|
|
|
|
|
|
void NO_INLINE sort1(Key * data, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::sort(data, data + size);
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NO_INLINE sort2(Key * data, size_t size)
|
|
|
|
{
|
2019-04-27 17:52:43 +00:00
|
|
|
radixSortLSD(data, size);
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NO_INLINE sort3(Key * data, size_t size)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::sort(data, data + size, [](Key a, Key b)
|
|
|
|
{
|
|
|
|
return RadixSortFloatTransform<uint32_t>::forward(ext::bit_cast<uint32_t>(a))
|
|
|
|
< RadixSortFloatTransform<uint32_t>::forward(ext::bit_cast<uint32_t>(b));
|
|
|
|
});
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-12-01 18:36:55 +00:00
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: program n method\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t n = DB::parse<size_t>(argv[1]);
|
|
|
|
size_t method = DB::parse<size_t>(argv[2]);
|
|
|
|
|
|
|
|
std::vector<Key> data(n);
|
|
|
|
|
2017-12-18 04:07:26 +00:00
|
|
|
// srand(time(nullptr));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
for (auto & elem : data)
|
|
|
|
elem = rand();
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
double elapsed = watch.elapsedSeconds();
|
|
|
|
std::cerr
|
|
|
|
<< "Filled in " << elapsed
|
|
|
|
<< " (" << n / elapsed << " elem/sec., "
|
|
|
|
<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n <= 100)
|
|
|
|
{
|
|
|
|
std::cerr << std::endl;
|
|
|
|
for (const auto & elem : data)
|
|
|
|
std::cerr << elem << ' ';
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
if (method == 1) sort1(data.data(), n);
|
|
|
|
if (method == 2) sort2(data.data(), n);
|
|
|
|
if (method == 3) sort3(data.data(), n);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
double elapsed = watch.elapsedSeconds();
|
|
|
|
std::cerr
|
|
|
|
<< "Sorted in " << elapsed
|
|
|
|
<< " (" << n / elapsed << " elem/sec., "
|
|
|
|
<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Stopwatch watch;
|
|
|
|
|
|
|
|
size_t i = 1;
|
|
|
|
while (i < n)
|
|
|
|
{
|
|
|
|
if (!(data[i - 1] <= data[i]))
|
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch.stop();
|
|
|
|
double elapsed = watch.elapsedSeconds();
|
|
|
|
std::cerr
|
|
|
|
<< "Checked in " << elapsed
|
|
|
|
<< " (" << n / elapsed << " elem/sec., "
|
|
|
|
<< n * sizeof(Key) / elapsed / 1048576 << " MB/sec.)"
|
|
|
|
<< std::endl
|
|
|
|
<< "Result: " << (i == n ? "Ok." : "Fail!") << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n <= 1000)
|
|
|
|
{
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
std::cerr << data[0] << ' ';
|
|
|
|
for (size_t i = 1; i < n; ++i)
|
|
|
|
{
|
|
|
|
if (!(data[i - 1] <= data[i]))
|
|
|
|
std::cerr << "*** ";
|
|
|
|
std::cerr << data[i] << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-11-21 12:47:41 +00:00
|
|
|
}
|