#ifdef _OPENMP
#include <omp.h>
#endif
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#ifndef ELEM_COUNT
#define ELEM_COUNT (1 << 29)
#elif ELEM_COUNT <= 0
#error Don't you think you're smarter than me?
#endif
#ifdef MAX_ELEMENT
int next_element() {
return rand() % MAX_ELEMENT
; }
#else
int next_element() {
}
#endif
void print_array(int* arr) {
for (int i = 1; i < ELEM_COUNT; ++i) {
}
}
int main() {
int* from
= (int*)malloc(((size_t)ELEM_COUNT
) * sizeof(int)); int* to
= (int*)malloc(((size_t)ELEM_COUNT
) * sizeof(int)); if (!from || !to) {
printf("Buy and install more RAM, looser!\n"); }
for (int i = 0; i < ELEM_COUNT; ++i) {
from[i] = next_element();
}
#ifdef DEBUG
printf("Going to sort array:\n"); print_array(from);
#else
#endif
struct timeval start, end;
gettimeofday(&start, NULL);
for (int size = 1; size < ELEM_COUNT; size += size) {
int* stop = from + ELEM_COUNT;
#pragma omp parallel for
for (int i = 0; i < ELEM_COUNT; i += size + size) {
int* a = from + i;
int* aend = a + size;
if (aend > stop) {
aend = stop;
}
int* b = aend;
int* bend = b + size;
if (bend > stop) {
bend = stop;
}
int* out = to + i;
while (a < aend && b < bend) {
if (*a < *b) {
*out++ = *a++;
} else {
*out++ = *b++;
}
}
if (a < aend) {
while (a < aend) {
*out++ = *a++;
}
} else {
while (b < bend) {
*out++ = *b++;
}
}
}
int* t = from;
from = to;
to = t;
}
gettimeofday(&end, NULL);
#ifdef DEBUG
print_array(from);
#endif
((double)(end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) / 1000000.0));
#ifdef _OPENMP
printf("Threads: %d\n", omp_get_max_threads
()); #endif
return EXIT_SUCCESS;
}
I2lmZGVmIF9PUEVOTVAKI2luY2x1ZGUgPG9tcC5oPgojZW5kaWYKI2luY2x1ZGUgPHN0ZGRlZi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoj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cmludF9hcnJheShmcm9tKTsKI2Vsc2UKICAgIHByaW50ZigiQXJyYXkgcHJlcGFyZWRcbiIpOwojZW5kaWYKICAgIHN0cnVjdCB0aW1ldmFsIHN0YXJ0LCBlbmQ7CiAgICBnZXR0aW1lb2ZkYXkoJnN0YXJ0LCBOVUxMKTsKICAgIGZvciAoaW50IHNpemUgPSAxOyBzaXplIDwgRUxFTV9DT1VOVDsgc2l6ZSArPSBzaXplKSB7CiAgICAgICAgaW50KiBzdG9wID0gZnJvbSArIEVMRU1fQ09VTlQ7CiNwcmFnbWEgb21wIHBhcmFsbGVsIGZvcgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgRUxFTV9DT1VOVDsgaSArPSBzaXplICsgc2l6ZSkgewogICAgICAgICAgICBpbnQqIGEgPSBmcm9tICsgaTsKICAgICAgICAgICAgaW50KiBhZW5kID0gYSArIHNpemU7CiAgICAgICAgICAgIGlmIChhZW5kID4gc3RvcCkgewogICAgICAgICAgICAgICAgYWVuZCA9IHN0b3A7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW50KiBiID0gYWVuZDsKICAgICAgICAgICAgaW50KiBiZW5kID0gYiArIHNpemU7CiAgICAgICAgICAgIGlmIChiZW5kID4gc3RvcCkgewogICAgICAgICAgICAgICAgYmVuZCA9IHN0b3A7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaW50KiBvdXQgPSB0byArIGk7CiAgICAgICAgICAgIHdoaWxlIChhIDwgYWVuZCAmJiBiIDwgYmVuZCkgewogICAgICAgICAgICAgICAgaWYgKCphIDwgKmIpIHsKICAgICAgICAgICAgICAgICAgICAqb3V0KysgPSAqYSsrOwogICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAqb3V0KysgPSAqYisrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChhIDwgYWVuZCkgewogICAgICAgICAgICAgICAgd2hpbGUgKGEgPCBhZW5kKSB7CiAgICAgICAgICAgICAgICAgICAgKm91dCsrID0gKmErKzsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHdoaWxlIChiIDwgYmVuZCkgewogICAgICAgICAgICAgICAgICAgICpvdXQrKyA9ICpiKys7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgaW50KiB0ID0gZnJvbTsKICAgICAgICBmcm9tID0gdG87CiAgICAgICAgdG8gPSB0OwogICAgfQogICAgZ2V0dGltZW9mZGF5KCZlbmQsIE5VTEwpOwojaWZkZWYgREVCVUcKICAgIHByaW50ZigiQXJyYXkgc29ydGVkOlxuIik7CiAgICBwcmludF9hcnJheShmcm9tKTsKI2VuZGlmCiAgICBwcmludGYoIlRpbWUgdGFrZW46ICVmc1xuIiwKICAgICAgICAoKGRvdWJsZSkoZW5kLnR2X3NlYyAtIHN0YXJ0LnR2X3NlYykgKyAoZG91YmxlKShlbmQudHZfdXNlYyAtIHN0YXJ0LnR2X3VzZWMpIC8gMTAwMDAwMC4wKSk7CiNpZmRlZiBfT1BFTk1QCiAgICBwcmludGYoIlRocmVhZHM6ICVkXG4iLCBvbXBfZ2V0X21heF90aHJlYWRzKCkpOwojZW5kaWYKICAgIHJldHVybiBFWElUX1NVQ0NFU1M7Cn0=