-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtim_sort.c
168 lines (132 loc) · 5.48 KB
/
tim_sort.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include "tim_sort.h"
const int RUN = 32;
static void insertion_sort(int array[], size_t start, size_t end_exclusive) {
for (size_t i = start + 1; i < end_exclusive; i++) {
int buffer = array[i];
ssize_t j = (ssize_t) i - 1;
while (j >= (ssize_t) start && array[j] > buffer) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = buffer;
}
}
void merge_runs(int array[], size_t start, size_t middle, size_t end_exclusive) {
size_t left_array_size = middle - start + 1;
size_t right_array_size = end_exclusive - middle - 1;
int* left_array = malloc(left_array_size * sizeof(int));
int* right_array = malloc(right_array_size * sizeof(int));
for (size_t i = 0; i < left_array_size; i++)
left_array[i] = array[start + i];
for (size_t i = 0; i < right_array_size; i++)
right_array[i] = array[middle + 1 + i];
size_t left_i = 0,
right_i = 0,
dest_i = start;
// Merge 'left_array' and 'right_array' into 'array'
while (left_i < left_array_size && right_i < right_array_size) {
if (left_array[left_i] <= right_array[right_i]) {
array[dest_i] = left_array[left_i];
left_i++;
} else {
array[dest_i] = right_array[right_i];
right_i++;
}
dest_i++;
}
// Copy remaining of 'left_array'
while (left_i < left_array_size)
array[dest_i++] = left_array[left_i++];
// Copy remaining of 'right_array'
while (right_i < right_array_size)
array[dest_i++] = right_array[right_i++];
}
AlgorithmResult tim_sort_simple(int array[], size_t array_size) {
double start_step_1 = omp_get_wtime();
for (size_t i = 0; i < array_size; i += RUN)
insertion_sort(array, i, min(i + RUN, array_size));
double start_step_2 = omp_get_wtime();
for (size_t size = RUN; size < array_size; size *= 2) {
for (size_t left = 0; left < array_size; left += 2*size) {
size_t middle = left + size - 1;
size_t next_chunk_start = min(left + 2*size, array_size);
if (middle < next_chunk_start)
merge_runs(array, left, middle, next_chunk_start);
}
}
double end = omp_get_wtime();
return (AlgorithmResult) {start_step_2 - start_step_1, end - start_step_2 };
}
AlgorithmResult tim_sort_parallel_method1(int array[], size_t array_size) {
double start_step_1 = omp_get_wtime();
int threads_used = 0;
#pragma omp parallel default(none) shared(array, array_size, threads_used)
{
int thread_id = omp_get_thread_num();
int threads = omp_get_num_threads();
if (thread_id == 0)
threads_used = threads;
if (threads > array_size)
threads = (int) array_size;
if (thread_id < array_size) {
size_t chunk_size = array_size / threads;
size_t next_partition_start = (thread_id == threads - 1) ? array_size : (thread_id + 1) * chunk_size;
size_t start_index = thread_id * chunk_size;
tim_sort_simple(array + start_index, next_partition_start - start_index);
}
}
double start_step_2 = omp_get_wtime();
merge_partitioned_array(array, array_size, threads_used);
double end = omp_get_wtime();
return (AlgorithmResult) {start_step_2 - start_step_1, end - start_step_2 };
}
AlgorithmResult tim_sort_parallel_method2(int array[], size_t array_size) {
double start_step_1 = omp_get_wtime();
#pragma omp parallel default(none) shared(array_size, RUN, array)
{
int thread_id = omp_get_thread_num();
int threads = omp_get_num_threads();
if (threads > array_size)
threads = (int) array_size;
if (thread_id < array_size) {
size_t runs = array_size/RUN;
size_t runs_chunk_size = runs/threads;
size_t start = runs_chunk_size * thread_id * RUN;
size_t end = runs_chunk_size * (thread_id + 1) * RUN;
if (thread_id == threads - 1)
end = array_size;
for (size_t i = start; i < end; i += RUN)
insertion_sort(array, i, min(i + RUN, array_size));
}
}
double start_step_2 = omp_get_wtime();
for (size_t size = RUN; size < array_size; size *= 2) {
#pragma omp parallel default(none) shared(array_size, array, size)
{
int thread_id = omp_get_thread_num();
int threads = omp_get_num_threads();
if (threads > array_size)
threads = (int) array_size;
if (thread_id < array_size) {
size_t step = 2 * size;
size_t chunks = array_size / step;
size_t chunks_chunk_size = chunks / threads;
size_t start = chunks_chunk_size * thread_id * step;
size_t end = chunks_chunk_size * (thread_id + 1) * step;
if (thread_id == threads - 1)
end = array_size;
for (size_t left = start; left < end; left += step) {
size_t middle = left + size - 1;
size_t next_chunk_start = min(left + step, array_size);
if (middle < next_chunk_start)
merge_runs(array, left, middle, next_chunk_start);
}
}
}
}
double end = omp_get_wtime();
return (AlgorithmResult) {start_step_2 - start_step_1, end - start_step_2 };
}