Rebuild/refactoring of scheduler main func

This commit is contained in:
Adrien Bourmault 2021-06-15 00:56:01 +02:00
parent 17166e59f6
commit 70bff11041
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
7 changed files with 137 additions and 110 deletions

Binary file not shown.

View File

@ -29,10 +29,16 @@
#define BASE_H #define BASE_H
struct {
int x;
int y;
int z;
} typedef Arrow_t;
struct { struct {
size_t size; size_t size;
bool *space; Arrow_t *space;
} typedef BoolArray_t; } typedef ArrowArray_t; //XXX
struct { struct {
size_t size; size_t size;
@ -42,24 +48,24 @@ struct {
#define LOGMSG "[%s]" #define LOGMSG "[%s]"
#define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__) #define printLog(FORMAT, ...) printf(LOGMSG " " FORMAT, __func__, ##__VA_ARGS__)
/* -------------------------------------------------------------------------- */
// //
// Scheduler // Scheduler
// //
struct { struct {
BoolArray_t *globalPreemptionSpace;
IntArray_t *globalDrawingSpace; IntArray_t *globalDrawingSpace;
IntArray_t *transitionsTree; IntArray_t *transitionsTree;
IntArray_t *arrowList; ArrowArray_t *arrowList;
int nmaxThread; int nmaxThread;
int nmaxCycles; int nmaxCycles;
int ruleRadius;
pthread_t *id; pthread_t *id;
bool stopped; bool stopped;
} typedef Scheduler_t; } typedef Scheduler_t;
struct { /* -------------------------------------------------------------------------- */
pthread_t *id;
} typedef Worker_t;
// //
// Local threads // Local threads
@ -76,4 +82,4 @@ struct Center_t {
struct { struct {
pthread_t *id; pthread_t *id;
Center_t *localWorkAreaCenter; Center_t *localWorkAreaCenter;
} typedef Thread_t; } typedef Worker_t;

View File

@ -26,21 +26,20 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Worker init function // // Worker init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *WorkerInit(Worker_t *parameters); pthread_t *WorkerInit(Worker_t *worker);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Worker destructor function // // Worker destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline int WorkerDestroy(pthread_t *schedThread) static inline void WorkerDestroy(Worker_t *worker)
{ {
free(schedThread); free(worker->id);
return 0;
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Worker wait function // // Worker wait function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void WorkerWait(pthread_t *schedThread) static inline void WorkerWait(Worker_t *worker)
{ {
pthread_join(*schedThread, NULL); pthread_join(*worker->id, NULL);
} }

View File

@ -26,21 +26,20 @@
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler init function // // Scheduler init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *SchedInit(Scheduler_t *parameters); pthread_t *SchedInit(Scheduler_t *scheduler);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler destructor function // // Scheduler destructor function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline int SchedDestroy(pthread_t *schedThread) static inline void SchedDestroy(Scheduler_t *scheduler)
{ {
free(schedThread); free(scheduler->id);
return 0;
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler wait function // // Scheduler wait function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static inline void SchedWait(pthread_t *schedThread) static inline void SchedWait(Scheduler_t *scheduler)
{ {
pthread_join(*schedThread, NULL); pthread_join(*scheduler->id, NULL);
} }

View File

@ -21,31 +21,31 @@
#include "../include/base.h" #include "../include/base.h"
static void *LittleWorker(void *parameters); static void *LittleWorker(void *worker);
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler init function // // Scheduler init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *WorkerInit(Scheduler_t *parameters) pthread_t *WorkerInit(Worker_t *worker)
{ {
parameters->id = (pthread_t*) malloc(sizeof(pthread_t)); worker->id = (pthread_t*) malloc(sizeof(pthread_t));
pthread_create(parameters->id, NULL, LittleWorker, parameters); pthread_create(worker->id, NULL, LittleWorker, worker);
return parameters->id; return worker->id;
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler thread main function // // Scheduler thread main function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static void *LittleWorker(void *params) static void *LittleWorker(void *worker)
{ {
Worker_t *parameters = (Worker_t*) params; Worker_t *args = (Worker_t*) worker;
int a = 4; int a = 4;
for (int i = 0; i < 45000; i++) { for (int i = 0; i < 45000; i++) {
for (int j = i; j < 45000; j++) { for (int j = i; j < 45000; j++) {
a = (a + (long)parameters->id) * i * j; a = (a + (long)args->id) * i * j;
} }
} }

View File

@ -29,28 +29,6 @@
#define SPACE_SIZE 10000 #define SPACE_SIZE 10000
#define MAX_THREAD 0 #define MAX_THREAD 0
void SchedulerCrashTest(void)
{
const int maxthread = 16;
Scheduler_t *scheduler0 =
(Scheduler_t*) calloc(1, sizeof(Scheduler_t));
pthread_t **schedThread =
(pthread_t**) malloc(sizeof(pthread_t*) * maxthread);
for (int i=0; i < maxthread; i++) {
schedThread[i] = SchedInit(scheduler0);
}
for (int i=0; i < maxthread; i++) {
SchedWait(schedThread[i]);
SchedDestroy(schedThread[i]);
}
free(scheduler0);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
time_t t; time_t t;
@ -70,19 +48,13 @@ int main(int argc, char **argv)
// //
scheduler0 = (Scheduler_t*) malloc(sizeof(Scheduler_t)); scheduler0 = (Scheduler_t*) malloc(sizeof(Scheduler_t));
scheduler0->globalPreemptionSpace =
(BoolArray_t*) malloc(sizeof(BoolArray_t));
scheduler0->globalPreemptionSpace->space =
(bool*) malloc(sizeof(bool)*SPACE_SIZE);
scheduler0->globalPreemptionSpace->size = SPACE_SIZE;
scheduler0->globalDrawingSpace = (IntArray_t*) malloc(sizeof(IntArray_t)); scheduler0->globalDrawingSpace = (IntArray_t*) malloc(sizeof(IntArray_t));
scheduler0->globalDrawingSpace->space = scheduler0->globalDrawingSpace->space =
(int*) malloc(sizeof(int)*SPACE_SIZE); (int*) malloc(sizeof(int)*SPACE_SIZE);
scheduler0->globalDrawingSpace->size = SPACE_SIZE; scheduler0->globalDrawingSpace->size = SPACE_SIZE;
scheduler0->arrowList = (IntArray_t*) malloc(sizeof(IntArray_t)); scheduler0->arrowList = (ArrowArray_t*) malloc(sizeof(ArrowArray_t));
scheduler0->arrowList->space = (int*) malloc(sizeof(int)*ARROW_NUMBER); scheduler0->arrowList->space = (Arrow_t*) malloc(sizeof(Arrow_t)*ARROW_NUMBER);
scheduler0->arrowList->size = ARROW_NUMBER; scheduler0->arrowList->size = ARROW_NUMBER;
scheduler0->nmaxThread = MAX_THREAD; scheduler0->nmaxThread = MAX_THREAD;
@ -93,16 +65,13 @@ int main(int argc, char **argv)
// //
SchedInit(scheduler0); SchedInit(scheduler0);
SchedWait(scheduler0->id); SchedWait(scheduler0);
SchedDestroy(scheduler0->id); SchedDestroy(scheduler0);
free(scheduler0->globalDrawingSpace->space); free(scheduler0->globalDrawingSpace->space);
free(scheduler0->globalDrawingSpace); free(scheduler0->globalDrawingSpace);
free(scheduler0->globalPreemptionSpace->space);
free(scheduler0->globalPreemptionSpace);
free(scheduler0->arrowList->space); free(scheduler0->arrowList->space);
free(scheduler0->arrowList); free(scheduler0->arrowList);

View File

@ -31,11 +31,11 @@ static void *GreatScheduler(void *parameters);
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler init function // // Scheduler init function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
pthread_t *SchedInit(Scheduler_t *parameters) pthread_t *SchedInit(Scheduler_t *scheduler)
{ {
parameters->id = (pthread_t*) malloc(sizeof(pthread_t)); scheduler->id = (pthread_t*) malloc(sizeof(pthread_t));
pthread_create(parameters->id, NULL, GreatScheduler, parameters); pthread_create(scheduler->id, NULL, GreatScheduler, scheduler);
return parameters->id; return scheduler->id;
} }
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
@ -68,56 +68,110 @@ static inline void centerRemove(Center_t *oldCenter)
if (oldCenter->next) oldCenter->next->prev = oldCenter->prev; if (oldCenter->next) oldCenter->next->prev = oldCenter->prev;
} }
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler area finder function // // Scheduler area finder function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static Center_t *findWorkArea(Center_t *centerList, int ruleRadius, size_t spaceSize) static Center_t *findWorkArea(Center_t *centersList, Arrow_t *electedArrow,
int ruleRadius, size_t spaceSize)
{ {
Center_t *myCenter = (Center_t*) malloc(sizeof(Center_t)); register Center_t *currentCenter = centersList;
centerAssign(myCenter, rand() % spaceSize, 0, 0); /* Je suis né quelque part.(ou pas !).*/
while (currentCenter){
//printLog("Center : %d\n", currentCenter->x);
if ( abs(electedArrow->x - currentCenter->x) >= ruleRadius
&& abs(electedArrow->y - currentCenter->y) >= ruleRadius
&& abs(electedArrow->z - currentCenter->z) >= ruleRadius
){
return currentCenter;
}
currentCenter = currentCenter->next;
}
while (centerList != NULL){
//printLog("Center : %d\n", centerList->x);
if (abs(myCenter->x - centerList->x) < ruleRadius){
printLog("FAIL: no work area available at %d\n", myCenter->x);
free(myCenter);
return NULL; return NULL;
}
centerList = centerList->next;
}
printLog("SUCCESS ! at %d: < I've found a center.\n", myCenter->x);
return myCenter;
} }
/* -------------------------------------------------------------------------- */
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
// Scheduler thread main function // // Scheduler thread main function //
// -------------------------------------------------------------------------- // // -------------------------------------------------------------------------- //
static void *GreatScheduler(void *params) static void *GreatScheduler(void *scheduler)
{ {
// récupération des paramètres et annonce Scheduler_t *args;
Scheduler_t *args = (Scheduler_t*) params; Worker_t *workerArray;
printLog("Scheduler id %lu: Bonjour I'm the boss !\n", *(args->id)); Center_t *centersList, *workArea;
Arrow_t *electedArrow;
long ncycles;
int ncpu, nworker;
int ncpu = get_nprocs();
// Getting scheduler argument structure
args = (Scheduler_t*) scheduler;
printLog("Scheduler #%lu online\n", *(args->id));
ncpu = get_nprocs();
printLog("%d CPUs available.\n", ncpu); printLog("%d CPUs available.\n", ncpu);
Thread_t *workerArray = (Thread_t*) malloc(sizeof(Thread_t) * 3 * ncpu); // Data structures
Center_t *centersList = (Center_t*) malloc(sizeof(Center_t)); workerArray = (Worker_t*) calloc(ncpu, sizeof(Worker_t));
nworker = 0;
centersList = (Center_t*) calloc(1, sizeof(Center_t));
ncycles = 0;
centerAssign(centersList, -1, -1, -1); //
// MAIN LOOP
//
while (!args->stopped && ncycles <= args->nmaxCycles) {
// Increment cycles
ncycles++;
Center_t *workArea; // TODO statistics here
int i = 0;
while (!args->stopped) { // Create a new thread
while ((workArea = findWorkArea(centersList, 4, 30))) { if (nworker < ncpu) {
i++; // Random choice of an arrow
electedArrow =
&args->arrowList->space[rand() % args->arrowList->size];
// Find a local area
workArea = findWorkArea(centersList, electedArrow, args->ruleRadius,
args->globalDrawingSpace->size);
// If a free area exists,
if (workArea) {
// preempt it,
centerAdd(centersList, workArea); centerAdd(centersList, workArea);
// find a worker socket,
for (int i = 0; i < ncpu; i++) {
if (!workerArray[i].id) {
// prepare the worker for the area,
workerArray[i].localWorkAreaCenter = workArea;
// create the worker,
WorkerInit(&workerArray[i]);
// and increment worker count.
nworker++;
break;
} }
printLog("New centers : %d\n", i);
} }
}
}
// TODO Delete finished workers
for (int i = 0; i < ncpu; i++) {
// XXX
}
}
// TODO Exiting scheduler properly
// Waiting for remaining workers
for (int i = 0; i < ncpu; i++) {
// XXX
}
printLog("Scheduler #%lu offline\n", *(args->id));
return NULL; return NULL;
} }