Skip to content

Instantly share code, notes, and snippets.

@wdv4758h
Created May 5, 2015 15:03
Show Gist options
  • Save wdv4758h/790ff9cd911af45cc4f3 to your computer and use it in GitHub Desktop.
Save wdv4758h/790ff9cd911af45cc4f3 to your computer and use it in GitHub Desktop.
Sleep Sort with OpenMP
// version 1, just print it
#include <cstdio>
#include <vector>
#include <unistd.h> // sleep
void sleep_sort(std::vector<unsigned long long> &data) {
const auto length = data.size();
#pragma omp parallel num_threads(length)
{
#pragma omp for
for (unsigned long i = 0; i < length; i++) {
sleep(data[i]);
printf("%llu\n", data[i]);
}
}
}
int main(int argc, char *argv[]) {
std::vector<unsigned long long> data(argc-1);
#pragma omp parallel for
for (int i = 0; i < argc-1; i++) {
sscanf(argv[i+1], "%llu", &data[i]);
}
sleep_sort(data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment