Skip to content

Instantly share code, notes, and snippets.

@usagi
Created January 4, 2014 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/8249766 to your computer and use it in GitHub Desktop.
Save usagi/8249766 to your computer and use it in GitHub Desktop.
cmake_minimum_required(VERSION 2.8.10)
project(parallel_sort)
add_custom_target(parallel_sort.g++.Ofast ALL g++ -std=c++11 -Ofast -march=native -fopenmp -o parallel_sort.g++.Ofast ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.g++.O3 ALL g++ -std=c++11 -O3 -march=native -fopenmp -o parallel_sort.g++.O3 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.g++.O2 ALL g++ -std=c++11 -O2 -march=native -fopenmp -o parallel_sort.g++.O2 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.g++.O1 ALL g++ -std=c++11 -O1 -march=native -fopenmp -o parallel_sort.g++.O1 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.g++.O0 ALL g++ -std=c++11 -O0 -march=native -fopenmp -o parallel_sort.g++.O0 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.g++.Os ALL g++ -std=c++11 -Os -march=native -fopenmp -o parallel_sort.g++.Os ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.O3 ALL clang++ -std=c++11 -O3 -march=native -o parallel_sort.clang++.O3 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.O2 ALL clang++ -std=c++11 -O2 -march=native -o parallel_sort.clang++.O2 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.O1 ALL clang++ -std=c++11 -O1 -march=native -o parallel_sort.clang++.O1 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.O0 ALL clang++ -std=c++11 -O0 -march=native -o parallel_sort.clang++.O0 ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.Os ALL clang++ -std=c++11 -Os -march=native -o parallel_sort.clang++.Os ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(parallel_sort.clang++.Oz ALL clang++ -std=c++11 -Oz -march=native -o parallel_sort.clang++.Oz ${PROJECT_SOURCE_DIR}/parallel_sort.cxx)
add_custom_target(test ALL ln -svf ${PROJECT_SOURCE_DIR}/test.sh)
#include <cstdint>
#include <iostream>
#include <fstream>
#include <random>
#include <chrono>
#include <vector>
#include <algorithm>
#if defined(__GNUC__) && defined(_OPENMP)
#define GNU_PARALLEL
#endif
#ifdef GNU_PARALLEL
#include <parallel/algorithm>
#endif
inline const std::chrono::nanoseconds time(const std::function<void()>& f)
{
const auto begin = std::chrono::high_resolution_clock::now();
f();
return std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - begin);
}
void randomize(std::vector<uint8_t>& vs)
{
std::random_device seed_gen;
std::mt19937_64 engine(seed_gen());
std::uniform_int_distribution<> dist(std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max() + 1);
for(auto& v: vs)
v = dist(engine);
}
void output_file(const std::vector<uint8_t>& vs, const std::string& filename = "/dev/null")
{
std::ofstream f(filename);
for(const auto v : vs)
f << v;
}
int main(const int an, const char* const* const as)
{
if(an < 3)
throw std::runtime_error("too few.; arg1: test size in bytes, arg2: number of repeat");
const auto test_size = size_t(std::atol(as[1]));
const auto number_of_repeat = size_t(std::atol(as[2]));
std::cout
<< "test size : " << test_size << " [bytes]" "\n"
"number of repeat: " << number_of_repeat << " [#]" "\n"
;
std::vector<uint8_t> data(test_size);
for(auto n = number_of_repeat; n; --n)
{
randomize(data);
std::cout << time([&](){ std::sort(std::begin(data), std::end(data)); }).count() << " ";
output_file(data, std::string(as[0]) + ".size-" + std::to_string(test_size) + ".std-sort.log");
#ifdef GNU_PARALLEL
randomize(data);
std::cout << time([&](){ __gnu_parallel::sort(std::begin(data), std::end(data)); }).count() << std::endl;
output_file(data, std::string(as[0]) + ".size-" + std::to_string(test_size) + ".gnu-parallel-sort.log");
#else
std::cout << "n/a" << std::endl;
#endif
}
}
#!/bin/sh
SIZE=${1-1000000}
REPEAT=${2-10}
find . -perm -100 -type f -name 'parallel_sort*' -print -exec {} $SIZE $REPEAT \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment