This commit is contained in:
Debucquoy
2023-12-21 23:36:03 +01:00
parent 990b77ed18
commit babed7eb09
35 changed files with 11061 additions and 39 deletions

View File

@ -2,7 +2,9 @@
CC = gcc
CFLAGS = -g -Wall
all: ex1 ex2 ex3 ex4 ex5 ex6 ex7 group
SRC = ex1 ex2 ex3 ex4 ex5 ex6 ex7 group
all: $(SRC)
group: group.o mergeSort.o
$(CC) $(CFLAGS) -o $@ $+
@ -15,9 +17,8 @@ group: group.o mergeSort.o
clean:
rm -f *.o core.*
mrproper: clean
rm -f ex1
rm -f plot.png
rm -f $(SRC)
run: group
./$<

View File

@ -6,18 +6,19 @@
#define printlist(s, x) printf("%s = [", #x); for (int i = 0; i < s; ++i) { printf("%d, ", x[i]); } printf("\b\b]\n");
#define listSize 10000000
#define listSize 1000000
int list[listSize];
int main(void)
{
// liste de nombre aléatoires
srand(time(NULL));
for (int i = 0; i < listSize; ++i) {
list[i] = rand();
list[i] = rand()%listSize;
}
/* printlist(listSize, list); */
margs_t args = {list, 0, 9};
mrg_t args = merge_init(list, listSize);
merge_sort(&args);
printlist(listSize, list);
}

View File

@ -0,0 +1,18 @@
.PHONY: clean, all
CC = gcc
CFLAGS = -g -Wall
all: group
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
group: group.o mergeSort.o
$(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
clean:
rm -f *.o core.*
rm -f group
run: group
./$<

View File

@ -0,0 +1,24 @@
#include "mergeSort.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define printlist(s, x) printf("%s = [", #x); for (int i = 0; i < s; ++i) { printf("%d, ", x[i]); } printf("\b\b]\n");
#define listSize 1000000
int list[listSize];
int main(void)
{
// liste de nombre aléatoires
srand(time(NULL));
for (int i = 0; i < listSize; ++i) {
list[i] = rand()%listSize;
}
/* printlist(listSize, list); */
mrg_t args = merge_init(list, listSize);
merge_sort(&args);
printlist(listSize, list);
}

View File

@ -0,0 +1,58 @@
#include "mergeSort.h"
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
void* merge_sort(void* args) {
mrg_t* f = (mrg_t*) args;
if(f->slice.start < f->slice.final) {
int middle = (f->slice.start + f->slice.final) / 2;
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}};
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);
}
return NULL;
}
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final) {
uint8_t countL = middle - start + 1;
int *arrayL = malloc(countL * sizeof(int));
for(uint8_t currentL = 0; currentL < countL; currentL ++)
arrayL[currentL] = array[start + currentL ];
uint8_t countR = final - middle;
int* arrayR = malloc(countR * sizeof(int));
for(uint8_t currentR = 0; currentR < countR; currentR ++)
arrayR[currentR] = array[middle + 1 + currentR ];
uint8_t currentL, currentR, current;
for(currentL = 0, currentR = 0, current = start;
current <= final && currentL < countL && currentR < countR;
current ++)
if(arrayL[currentL] <= arrayR[currentR])
array[current] = arrayL[currentL++];
else
array[current] = arrayR[currentR++];
while(currentR < countR)
array[current++] = arrayR[currentR++];
while(currentL < countL)
array[current++] = arrayL[currentL++];
free(arrayL);
free(arrayR);
}

View File

@ -0,0 +1,21 @@
#pragma once
#include <pthread.h>
#include <inttypes.h>
#define THREADS 4
#define merge_init(array, size) {(array), 0, {1, (size)}}
typedef struct{
int start;
int final;
} SLICE;
typedef struct {
int* array;
int _depth;
SLICE slice;
} mrg_t;
void* merge_sort(void*);
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final);

View File

@ -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 ++)

View File

@ -1,15 +1,21 @@
#pragma once
typedef unsigned int index;
typedef unsigned int length;
#include <pthread.h>
#include <inttypes.h>
typedef struct merge_args{
#define THREADS 4
#define merge_init(array, size) {(array), 0, {1, (size)}}
typedef struct{
int start;
int final;
} SLICE;
typedef struct {
int* array;
index start;
index final;
} margs_t;
int _depth;
SLICE slice;
} mrg_t;
void merge(int array[], index start, index middle, index final);
/* void merge_sort(int array [], index start , index final ); */
void* merge_sort(void*);
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final);

10000
bac2/os/chap3/plot Normal file

File diff suppressed because it is too large Load Diff