-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
166 lines (148 loc) · 5.18 KB
/
thread.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
#include "sort.h"
#include "stdio.h"
#include "stdlib.h"
#include "thread.h"
#include "string.h"
#include "sys/mman.h"
#include "unistd.h"
#include "pthread.h"
#include "stdatomic.h"
#include "valgrind/callgrind.h"
// ABC implementation from
// https://brilliant.org/wiki/radix-sort/
//
// All shifting and masking original, array layouts from brilliant
// count_n = C
// lower = B (kind of, B doesn't hold the values, only pointers to the values)
// start = A
// sort BITS_AT_ONCE bits at a time
// NUM_POS_VALUES = 2^BITS_AT_ONCE
//
//
// assume iteration = 1, BITS AT_ONCE = 4
// start[k].key = 0x1A93CFD2
// mask << (iteration*BITS_AT_ONCE) = 0x000000F0
// start[k].key & mask = 0x000000D0
// start[k].key & mask >> (iteration*BITS_AT_ONCE) = 0x0000000D
// indexable ^^^^^^^^
//
// sorts keys in memory into lower
int counting_sort(record* start, int size, record* lower, int* count_n, shared_count* s_count, int iteration) {
key_t mask = (NUM_POS_VALUES-1);
// int C[NUM_POS_VALUES];
mask = mask << (iteration*BITS_AT_ONCE);
// count occurences of mask
for(int i = 0; i<size; i++) {
// eg. C[0xF & key] += 1
count_n[(unsigned)(start[i].key & mask) >> (iteration*BITS_AT_ONCE)] += 1;
// IMPORTANT TO USE UNSIGNED
}
// get sum-up_to[j]
// C[0] = count_n[0];
atomic_fetch_add_explicit(&s_count->remaining[0], count_n[0], memory_order_acq_rel);
for(int j = 1; j<NUM_POS_VALUES; j++) {
count_n[j] = count_n[j] + count_n[j-1];
atomic_fetch_add_explicit(&s_count->remaining[j], count_n[j], memory_order_acq_rel);
// C[j] = count_n[j];
}
// final sort
for(int k = size-1; k>=0; k--) {
count_n[(unsigned)(start[k].key & mask) >> (iteration*BITS_AT_ONCE)] -= 1;
lower[count_n[(unsigned)(start[k].key & mask) >> (iteration*BITS_AT_ONCE)]] = start[k];
}
// for(int i = 0; i < NUM_POS_VALUES; i++) {
// atomic_fetch_add_explicit(&s_count->remaining[i], count_n[i], memory_order_acq_rel);
// }
return 0;
}
int move_to_mem(t_radix* thread_mem, record* lower, thread_args* ta) {
t_radix* me = &thread_mem[ta->my_tid];
shared_memory* s_mem = ta->s_memory;
shared_count* s_count = ta->s_count;
globals* global = ta->global;
int tid = ta->my_tid;
int count_idx = 0;
int lower_idx = 0;
// reset idxes each round
if(tid == 0) {
while(1) {
if(pthread_mutex_lock(s_mem->lock) == -1) {
pthread_cond_wait(s_mem->checkable, s_mem->lock);
}
if(s_mem->t_turn % global->THREADS == tid) {
s_mem->c_t_arr = 0;
s_mem->c_t_idx = 0;
break;
}
pthread_mutex_unlock(s_mem->lock);
}
pthread_mutex_unlock(s_mem->lock);
}
while(count_idx < NUM_POS_VALUES) {
// wait for turn
while(1) {
// try to lock, if fail, wait
if(pthread_mutex_lock(s_mem->lock) == -1) {
pthread_cond_wait(s_mem->checkable, s_mem->lock);
}
if(s_mem->t_turn % global->THREADS == tid) {
break;
}
pthread_mutex_unlock(s_mem->lock);
}
// insert here!
int inserting = me->count_n[count_idx];
while(me->count_n[count_idx] != 0) {
// check for zeros (might not work in tests ) TODO
if(tid == global->THREADS - 1 && lower[lower_idx].key == 0) {
;;
} else {
// add into curr ptr!
thread_mem[s_mem->c_t_arr].arr_start[s_mem->c_t_idx] = lower[lower_idx];
}
for(int i = count_idx; i < NUM_POS_VALUES; i++) {
me->count_n[i] -= 1;
}
lower_idx += 1;
// calc new idx
if(s_mem->c_t_idx == global->ARR_SIZE - 1) {
s_mem->c_t_idx = 0;
s_mem->c_t_arr += 1;
} else {
s_mem->c_t_idx += 1;
}
}
for(int i = count_idx; i < NUM_POS_VALUES; i++) {
atomic_fetch_sub(&s_count->remaining[i], inserting);
}
count_idx += 1;
// printf("[%i] unlocked the lock!\n", tid);
s_mem->t_turn += 1;
pthread_cond_broadcast(s_mem->checkable);
pthread_mutex_unlock(s_mem->lock);
me->filled = count_idx;
}
return 0;
}
void* t_run(void* in_ta) {
// bullshit C setup
thread_args* ta = (thread_args*) in_ta;
// extract ta
globals* global = ta->global;
shared_count* s_count = ta->s_count;
t_radix* thread_mem = ta->thread_mem;
// setup lower
record* lower = malloc(global->ARR_SIZE * sizeof(record));
if(!lower) {
printf("failed to alloc lower");
exit(EXIT_FAILURE);
}
t_radix* me = &thread_mem[ta->my_tid];
for(int r = 0; r < KEY_BITS / BITS_AT_ONCE; r++) {
// for(int r = 0; r < 1; r++) {
counting_sort(me->arr_start, global->ARR_SIZE, lower, me->count_n, s_count, r);
move_to_mem(thread_mem, lower, ta);
}
free(lower);
return 0;
}