-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.c
137 lines (125 loc) · 3.9 KB
/
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
#include "sys/stat.h"
#include "stdint.h"
#include "fcntl.h"
#include "sys/mman.h"
#include "stdio.h"
#include "sort.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "pthread.h"
#include "thread.h"
int get_file_size(char* filename) {
struct stat st;
if(stat(filename, &st) == -1) {
return -1;
}
off_t size = st.st_size;
return size;
}
int p_radix_sort(char* in, char* out) {
size_t pagesize = getpagesize();
int filesize = get_file_size(in);
if(filesize <= 0) {
fprintf(stderr, "An error has occurred\n");
exit(0);
}
// get quick approximates
float div_result = filesize / 100.0 / (float)THREADS;
int ARR_SIZE = (int)div_result;
if (ARR_SIZE - div_result < 0) ARR_SIZE += 1;
t_radix* thread_mem = malloc(THREADS*sizeof(t_radix));
memset(thread_mem, 0, THREADS*sizeof(t_radix));
// memory map records
struct stat sb;
int f = open(in, O_RDONLY);
fstat(f, &sb);
record* mapped_records = (record*) mmap((void*) (pagesize * (1<<20)), filesize, PROT_READ, MAP_SHARED, f, 0);
if(mapped_records == MAP_FAILED) {
printf("Failed to map memory :/\n");
exit(1);
}
int outfile = open(out, O_RDWR | O_CREAT, 0666);
// set filesize
lseek(outfile, filesize-1, SEEK_SET);
if(write(outfile, "", 1) == -1) {
printf("couldn't write\n");
}
record* mapped_out = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, outfile, 0);
if(mapped_out == MAP_FAILED) {
printf("Failed to map out memory :/\n");
exit(1);
}
// init structs
for(int i = 0; i < THREADS; i++) {
thread_mem[i].my_tid = i;
thread_mem[i].arr_start = mapped_records;
thread_mem[i].out = mapped_out;
}
// printf("INIT Diagnostic:\n\tfilesize: %i\n\tthreads: %i\n\tARR_SIZE: %i\n\tmem/thread: %li\n\n", filesize, THREADS, ARR_SIZE, sizeof(t_radix));
// alloc shared_memory
shared_memory* s_memory = malloc(sizeof(shared_memory));
if(!s_memory) {
printf("failed to alloc memory_lock");
exit(EXIT_FAILURE);
}
s_memory->lock = malloc(sizeof(pthread_mutex_t));
if(!s_memory->lock) {
printf("failed to allocated lock");
exit(EXIT_FAILURE);
}
s_memory->checkable = malloc(sizeof(pthread_cond_t));
if(!s_memory->checkable) {
printf("failed to allocated lock");
exit(EXIT_FAILURE);
}
pthread_mutex_init(s_memory->lock, NULL);
pthread_cond_init(s_memory->checkable, NULL);
s_memory->curr_idx = 0;
s_memory->t_turn = 0;
// alloc globals
globals* global = malloc(sizeof(globals));
if(!global) {
printf("failed to alloc globals");
exit(EXIT_FAILURE);
}
global->total_records = filesize / 100;
// alloc threads
pthread_t *threads = malloc(sizeof(pthread_t)*THREADS);
// create threads
for (int i = 0; i < THREADS; i++) {
// run t_run mehtod
thread_args* ta = malloc(sizeof(thread_args));
if(!ta) {
printf("failed to allocate ta");
exit(EXIT_FAILURE);
}
ta->thread_mem = thread_mem;
ta->s_memory = s_memory;
ta->global = global;
ta->my_tid = i;
int r = pthread_create(&threads[i], NULL, &t_run, ta);
if(r != 0) {
printf("Issue creating thread [%i]\n", i);
exit(1);
}
}
for(int i = 0; i < THREADS; i++) {
pthread_join(threads[i], NULL);
}
// make sure things are in mem correctly!
// for(int i = 0; i < global->total_records; i++) {
// printf("m:\t%i\n", mapped_out[i].key);
// }
msync(mapped_out, filesize, MS_ASYNC);
// free all of our stuff!
munmap(mapped_out, filesize);
munmap(mapped_records, filesize);
free(s_memory);
free(global);
free(thread_mem);
free(threads);
close(f);
close(outfile);
return 1;
}