chap3
This commit is contained in:
@@ -4,42 +4,42 @@
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#define THREADS
|
||||
|
||||
pthread_t threads[THREADS];
|
||||
|
||||
void* merge_sort(void* args) {
|
||||
margs_t* f = (margs_t*) args;
|
||||
if(f->start < f->final) {
|
||||
pthread_t t1, t2;
|
||||
index middle = (f->start + f->final) / 2;
|
||||
mrg_t* f = (mrg_t*) args;
|
||||
if(f->slice.start < f->slice.final) {
|
||||
int middle = (f->slice.start + f->slice.final) / 2;
|
||||
|
||||
margs_t args1 = {f->array, f->start, middle};
|
||||
assert(!pthread_create(&t1, NULL, merge_sort, (void*) &args1));
|
||||
margs_t args2 = {f->array, middle+1, f->final};
|
||||
assert(!pthread_create(&t2, NULL, merge_sort, (void*) &args2));
|
||||
mrg_t args1 = {f->array, f->_depth+1, {f->slice.start, middle}};
|
||||
mrg_t args2 = {f->array, f->_depth+1, { middle+1, f->slice.final}};
|
||||
|
||||
assert(pthread_join(t1, NULL));
|
||||
assert(pthread_join(t2, NULL));
|
||||
merge(f->array, f->start, middle, f->final);
|
||||
if(f->_depth * f->_depth < THREADS){
|
||||
pthread_t t1, t2;
|
||||
assert(!pthread_create(&t1, NULL, merge_sort, (void*) &args1));
|
||||
assert(!pthread_create(&t2, NULL, merge_sort, (void*) &args2));
|
||||
assert(!pthread_join(t1, NULL));
|
||||
assert(!pthread_join(t2, NULL));
|
||||
}else{
|
||||
merge_sort((void*) &args1);
|
||||
merge_sort((void*) &args2);
|
||||
}
|
||||
merge(f->array, f->slice.start, middle, f->slice.final);
|
||||
}
|
||||
int *ret = 0;
|
||||
return ret;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void merge(int array[], index start, index middle, index final) {
|
||||
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final) {
|
||||
|
||||
length countL = middle - start + 1;
|
||||
uint8_t countL = middle - start + 1;
|
||||
int *arrayL = malloc(countL * sizeof(int));
|
||||
for(index currentL = 0; currentL < countL; currentL ++)
|
||||
for(uint8_t currentL = 0; currentL < countL; currentL ++)
|
||||
arrayL[currentL] = array[start + currentL ];
|
||||
|
||||
length countR = final - middle;
|
||||
uint8_t countR = final - middle;
|
||||
int* arrayR = malloc(countR * sizeof(int));
|
||||
for(index currentR = 0; currentR < countR; currentR ++)
|
||||
for(uint8_t currentR = 0; currentR < countR; currentR ++)
|
||||
arrayR[currentR] = array[middle + 1 + currentR ];
|
||||
|
||||
index currentL, currentR, current;
|
||||
uint8_t currentL, currentR, current;
|
||||
for(currentL = 0, currentR = 0, current = start;
|
||||
current <= final && currentL < countL && currentR < countR;
|
||||
current ++)
|
||||
|
||||
Reference in New Issue
Block a user