forked from sysprog21/mergesort-concurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
182 lines (154 loc) · 4.83 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "threadpool.h"
#include "list.h"
#include "merge_sort.h"
#include "file.c"
#define USAGE "usage: ./sort [thread_count] [input_file]\n"
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define ALIGN_FILE "./align.txt"
#define MAX_LAST_NAME_SIZE 14
struct {
pthread_mutex_t mutex;
} data_context;
static llist_t *tmp_list;
static llist_t *the_list = NULL;
static int thread_count = 0, data_count = 0, max_cut = 0;
static tpool_t *pool = NULL;
void merge_thread_lists(void *data)
{
llist_t *_list = (llist_t *) data;
if (_list->size < (uint32_t) data_count) {
pthread_mutex_lock(&(data_context.mutex));
llist_t *_t = tmp_list;
if (!_t) {
tmp_list = _list;
pthread_mutex_unlock(&(data_context.mutex));
} else {
/*
* If there is a local list left by other thread,
* pick it and create a task to merge the picked list
* and its own local list.
*/
tmp_list = NULL;
pthread_mutex_unlock(&(data_context.mutex));
tqueue_push(pool->queue, task_new(merge_thread_lists, sort_n_merge(_list, _t)));
}
} else {
/*
* All local lists are merged, push a termination task to task queue.
*/
the_list = _list;
tqueue_push(pool->queue, task_new(NULL, NULL));
}
}
void sort_local_list(void *data)
{
llist_t *local_list = (llist_t *) data;
merge_thread_lists(merge_sort(local_list));
}
void cut_local_list(void *data)
{
llist_t *list = (llist_t *) data, *local_list;
node_t *head, *tail;
int local_size = data_count / max_cut;
head = list->head;
for (int i = 0; i < max_cut - 1; ++i) {
/* Create local list container */
local_list = list_new();
local_list->head = head;
local_list->size = local_size;
/* Cut the local list */
tail = list_get(local_list, local_size - 1);
head = tail->next;
tail->next = NULL;
/* Create new task */
tqueue_push(pool->queue, task_new(sort_local_list, local_list));
}
/* The last takes the rest. */
local_list = list_new();
local_list->head = head;
local_list->size = list->size - local_size * (max_cut - 1);
tqueue_push(pool->queue, task_new(sort_local_list, local_list));
}
static void *task_run(void *data __attribute__ ((__unused__)))
{
while (1) {
task_t *_task = tqueue_pop(pool->queue);
if (_task) {
if (!_task->func) {
tqueue_push(pool->queue, _task);
break;
} else {
_task->func(_task->arg);
free(_task);
}
}
}
pthread_exit(NULL);
}
static uint32_t build_list_from_file(llist_t *_list, const char *filename)
{
FILE *fp = fopen(filename, "r");
char buffer[16];
while (fgets(buffer, 16, fp) != NULL) {
list_add(_list, atol(buffer));
}
fclose(fp);
return _list->size;
}
int main(int argc, char const *argv[])
{
if (argc < 3) {
printf(USAGE);
return -1;
}
thread_count = atoi(argv[1]);
#if defined NUMBER
/* Read data */
the_list = list_new();
data_count = build_list_from_file(the_list, argv[2]);
max_cut = MIN(thread_count, data_count);
#elif defined STRING
/* Read data */
file_align(argv[2], ALIGN_FILE, MAX_LAST_NAME_SIZE);
int fd = open(ALIGN_FILE, O_RDONLY | O_NONBLOCK);
off_t fs = fsize(ALIGN_FILE);
data_count = fs/MAX_LAST_NAME_SIZE;
the_list = list_new();
char *map = mmap(NULL, fs, PROT_READ, MAP_SHARED, fd, 0);
for (int i = 0; i < fs/MAX_LAST_NAME_SIZE; i++)
list_addp(the_list, map + MAX_LAST_NAME_SIZE * i);
max_cut = MIN(thread_count, data_count);
#endif
/* initialize tasks inside thread pool */
pthread_mutex_init(&(data_context.mutex), NULL);
tmp_list = NULL;
pool = (tpool_t *) malloc(sizeof(tpool_t));
tpool_init(pool, thread_count, task_run);
struct timeval start, end;
uint32_t consumed_tasks;
double duration;
/* Start when the first task launches. */
gettimeofday(&start, NULL);
/* launch the first task */
tqueue_push(pool->queue, task_new(cut_local_list, the_list));
/* release thread pool */
consumed_tasks = tpool_free(pool);
gettimeofday(&end, NULL);
/* Report */
duration = (end.tv_sec - start.tv_sec) * 1000 +
(double)(end.tv_usec - start.tv_usec) / 1000.0f;
#ifndef STRING
printf("#Total_tasks_consumed: %d\n", consumed_tasks);
printf("#Elapsed_time: %.3lf ms\n", duration);
printf("#Throughput: %d (per sec)\n", (uint32_t)(consumed_tasks * 1000 / duration));
#endif
/* Output sorted result */
list_print(the_list);
return 0;
}