-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.cpp
118 lines (101 loc) · 2.75 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <mpi.h>
#include <omp.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "util.h"
extern int procid, nprocs;
extern bool verbose, debug, verify;
double timer() {
struct timeval tp;
gettimeofday(&tp, NULL);
return ((double)(tp.tv_sec) + 1e-6 * tp.tv_usec);
}
void throw_err(char const* err_message) {
fprintf(stderr, "Error: %s\n", err_message);
MPI_Abort(MPI_COMM_WORLD, 1);
}
void throw_err(char const* err_message, int32_t task) {
fprintf(stderr, "Task %d Error: %s\n", task, err_message);
MPI_Abort(MPI_COMM_WORLD, 1);
}
void throw_err(char const* err_message, int32_t task, int32_t thread) {
fprintf(stderr, "Task %d Thread %d Error: %s\n", task, thread, err_message);
MPI_Abort(MPI_COMM_WORLD, 1);
}
void quicksort_dec(double* arr1, uint64_t* arr2, int64_t left, int64_t right) {
int64_t i = left;
int64_t j = right;
double temp;
uint64_t temp2;
double pivot = arr1[(left + right) / 2];
while (i <= j) {
while (arr1[i] > pivot) {
i++;
}
while (arr1[j] < pivot) {
j--;
}
if (i <= j) {
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
temp2 = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp2;
++i;
--j;
}
}
if (j > left) quicksort_dec(arr1, arr2, left, j);
if (i < right) quicksort_dec(arr1, arr2, i, right);
}
void quicksort_inc(uint64_t* arr1, int64_t left, int64_t right) {
int64_t i = left;
int64_t j = right;
int64_t temp;
uint64_t pivot = arr1[(left + right) / 2];
while (i <= j) {
while (arr1[i] < pivot) {
i++;
}
while (arr1[j] > pivot) {
j--;
}
if (i <= j) {
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
++i;
--j;
}
}
if (j > left) quicksort_inc(arr1, left, j);
if (i < right) quicksort_inc(arr1, i, right);
}
uint64_t xs1024star_next(xs1024star_t* xs) {
const uint64_t s0 = xs->s[xs->p];
uint64_t s1 = xs->s[xs->p = (xs->p + 1) & 15];
s1 ^= s1 << 31;
xs->s[xs->p] = s1 ^ s0 ^ (s1 >> 11) ^ (s0 >> 30);
return xs->s[xs->p] * uint64_t(1181783497276652981U);
}
double xs1024star_next_real(xs1024star_t* xs) {
const uint64_t s0 = xs->s[xs->p];
uint64_t s1 = xs->s[xs->p = (xs->p + 1) & 15];
s1 ^= s1 << 31;
xs->s[xs->p] = s1 ^ s0 ^ (s1 >> 11) ^ (s0 >> 30);
double ret = (double)(xs->s[xs->p] * uint64_t(1181783497276652981U));
return ret /= (double)uint64_t(18446744073709551615U);
}
void xs1024star_seed(uint64_t seed, xs1024star_t* xs) {
for (uint64_t i = 0; i < 16; ++i) {
uint64_t z = (seed += uint64_t(0x9E3779B97F4A7C15));
z = (z ^ (z >> 30)) * uint64_t(0xBF58476D1CE4E5B9);
z = (z ^ (z >> 27)) * uint64_t(0x94D049BB133111EB);
xs->s[i] = z ^ (z >> 31);
}
xs->p = 0;
}