From 29e43c2f188892d1c8dbb75f39ccd74130ca15b6 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 23 Jun 2021 14:16:45 +0200 Subject: [PATCH] Model_t + lisibility --- include/base.h | 35 +++++++---- include/model.h | 9 ++- include/supervisor.h | 50 ++++++++++++++++ src/cmds.c | 41 +------------ src/model.c | 134 +++++++++++++++++++++++++++++++++++-------- src/scheduler.c | 20 +++---- src/server.c | 2 +- src/supervisor.c | 43 ++++++++++++++ 8 files changed, 245 insertions(+), 89 deletions(-) create mode 100644 include/supervisor.h create mode 100644 src/supervisor.c diff --git a/include/base.h b/include/base.h index 678a96d..fd7f6b5 100644 --- a/include/base.h +++ b/include/base.h @@ -51,19 +51,19 @@ struct { struct { char *label; - int narrow; + int nArrow; } typedef Site_t; struct { - int nsite; + int nSite; char *label; Site_t *sites; } typedef SpaceUnit_t; struct { - int xmax; - int ymax; - int zmax; + int xMax; + int yMax; + int zMax; size_t size; SpaceUnit_t *space; } typedef Space_t; @@ -82,8 +82,8 @@ struct { Space_t *globalDrawingSpace; IntArray_t *conditionTree; ArrowArray_t *arrowArray; - int nmaxThread; - int nmaxCycles; + int nMaxThread; + int nMaxCycles; int ruleRadius; pthread_t *id; bool pleaseStop; @@ -126,13 +126,28 @@ struct { /* -------------------------------------------------------------------------- */ +// +// Server +// +struct { + pthread_t *id; + bool pleaseStop; + int *workerCreationOccurency; +} typedef Supervisor_t; + +/* -------------------------------------------------------------------------- */ + // // Model // struct { int id; - int space_xmax; - int space_ymax; - int space_zmax; + int space_xMax; + int space_yMax; + int space_zMax; + int nmaxThread; + int nmaxCycles; int siteNumber; + Scheduler_t *scheduler; + Supervisor_t *supervisor; } typedef Model_t; diff --git a/include/model.h b/include/model.h index f63decc..29b4458 100644 --- a/include/model.h +++ b/include/model.h @@ -23,9 +23,12 @@ #include "../include/base.h" #endif -void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model); +void ModelSystemInit(void); -void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowList, - Model_t *model); +void ModelSystemDestroy(void); +int ModelLoad(int id); +void ModelCreate(Model_t **newModel); + +int ModelLoad(int id); diff --git a/include/supervisor.h b/include/supervisor.h new file mode 100644 index 0000000..724f3a6 --- /dev/null +++ b/include/supervisor.h @@ -0,0 +1,50 @@ +//=-------------------------------------------------------------------------=// +// Supervisor definition // +// // +// Copyright © 2021 The Gem-graph Project // +// // +// This file is part of gem-graph. // +// // +// This program is free software: you can redistribute it and/or modify // +// it under the terms of the GNU Affero General Public License as // +// published by the Free Software Foundation, either version 3 of the // +// License, or (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU Affero General Public License for more details. // +// // +// You should have received a copy of the GNU Affero General Public License // +// along with this program. If not, see . // +//=-------------------------------------------------------------------------=// + +#ifndef BASE_H + #include "../include/base.h" +#endif + +// -------------------------------------------------------------------------- // +// Supervisor init function // +// -------------------------------------------------------------------------- // +void SupervisorInit(Scheduler_t *scheduler); + +// -------------------------------------------------------------------------- // +// Supervisor destructor function // +// -------------------------------------------------------------------------- // +static inline void SupervisorDestroy(Scheduler_t *scheduler) +{ + free(scheduler->id); + free(scheduler->globalDrawingSpace->space); + free(scheduler->globalDrawingSpace); + + free(scheduler->arrowArray->array); + free(scheduler->arrowArray); +} + +// -------------------------------------------------------------------------- // +// Supervisor wait function // +// -------------------------------------------------------------------------- // +static inline void SupervisorWait(Scheduler_t *scheduler) +{ + pthread_join(*scheduler->id, NULL); +} diff --git a/src/cmds.c b/src/cmds.c index da1d1b6..862ddd8 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -23,48 +23,9 @@ #include "../include/scheduler.h" #include "../include/model.h" -#define ARROW_NUMBER 6 -#define SITE_NUMBER 2 -#define MAX_CYCLES 10 -#define MAX_THREAD 0 -#define XMAX 30 -#define YMAX 0 -#define ZMAX 0 -#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) - /* -------------------------------------------------------------------------- */ -void *launchScheduler(void *args) +void *CmdModel(void *args) { - Scheduler_t *scheduler0; - Model_t *model0; - // Creating structure for the Scheduler - scheduler0 = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); - - scheduler0->globalDrawingSpace = - (Space_t*) calloc(1, sizeof(Space_t)); - - // Creating structure for the model - model0 = (Model_t*) calloc(1, sizeof(Model_t)); - - model0->space_xmax = XMAX; - model0->siteNumber = SITE_NUMBER; - - // Preparing global drawing space - ModelPrepareSpace(scheduler0->globalDrawingSpace, model0); - - scheduler0->arrowArray = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); - - ModelPrepareArrows(scheduler0->globalDrawingSpace, scheduler0->arrowArray, model0); - - scheduler0->nmaxThread = MAX_THREAD; - scheduler0->nmaxCycles = MAX_CYCLES; - - // - // Creating the Scheduler thread - // - SchedInit(scheduler0); - - return scheduler0; } diff --git a/src/model.c b/src/model.c index bd1dab0..88fd7c7 100644 --- a/src/model.c +++ b/src/model.c @@ -21,47 +21,135 @@ #include "../include/base.h" #include "../include/arrows.h" +#include "../include/scheduler.h" #define MAX_MODEL_NUMBER 1 +#define ARROW_NUMBER 2 +#define SITE_NUMBER 2 +#define MAX_CYCLES 10 +#define MAX_THREAD 0 +#define XMAX 30 +#define YMAX 0 +#define ZMAX 0 +#define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) -static Model_t *loadedModels; +static Model_t **loadedModel; +static int loadedModelIndex; -void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model) +static Model_t **knownModel; +static int knownModelIndex; + +static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model); +static void ModelPrepareArrows(Space_t *globalDrawingSpace, + ArrowArray_t *arrowArray, + Model_t *model); + +/* -------------------------------------------------------------------------- */ + +void ModelSystemInit(void) { - globalDrawingSpace->size = (model->space_xmax+1) * (model->space_ymax+1) * (model->space_zmax+1); - globalDrawingSpace->xmax = model->space_xmax; - globalDrawingSpace->ymax = model->space_ymax; - globalDrawingSpace->zmax = model->space_zmax; + loadedModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t*)); + // XXX read known models from files + knownModel = (Model_t**) calloc(MAX_MODEL_NUMBER, sizeof(Model_t)); +} + +void ModelSystemDestroy(void) +{ + free(loadedModel); + free(knownModel); +} + +void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots +{ + knownModel = + (Model_t**) realloc(knownModel, ++knownModelIndex * sizeof(Model_t)); + knownModel[knownModelIndex] = (Model_t*) calloc(1, sizeof(Model_t)); + knownModel[knownModelIndex]->id = knownModelIndex; + *newModel = knownModel[knownModelIndex]; + + knownModel[knownModelIndex]->space_xMax = XMAX; + knownModel[knownModelIndex]->space_yMax = YMAX; + knownModel[knownModelIndex]->space_zMax = ZMAX; + knownModel[knownModelIndex]->nmaxThread = MAX_THREAD; + knownModel[knownModelIndex]->nmaxCycles = MAX_CYCLES; + knownModel[knownModelIndex]->siteNumber = SITE_NUMBER; +} + +int ModelLoad(int id) // TODO unload ! +{ + // Creating structure for the Scheduler + knownModel[id]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); + + knownModel[id]->scheduler->globalDrawingSpace = + (Space_t*) calloc(1, sizeof(Space_t)); + + knownModel[id]->scheduler->nMaxThread = knownModel[id]->nmaxThread; + knownModel[id]->scheduler->nMaxCycles = knownModel[id]->nmaxCycles; + + // Preparing global drawing space + ModelPrepareSpace(knownModel[id]->scheduler->globalDrawingSpace, + knownModel[id]); + + knownModel[id]->scheduler->arrowArray = + (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); + + ModelPrepareArrows(knownModel[id]->scheduler->globalDrawingSpace, + knownModel[id]->scheduler->arrowArray, knownModel[id]); + + loadedModel[loadedModelIndex] = knownModel[id]; + return loadedModelIndex++; +} + +void ModelRun(int id) +{ + // Creating structure for the Scheduler + SchedInit(knownModel[id]->scheduler); +} + +void ModelStop(int id) +{ + // Creating structure for the Scheduler + knownModel[id]->scheduler->pleaseStop = true; +} + +void ModelDelete(Model_t *newModel) +{ + return; +} + +/* -------------------------------------------------------------------------- */ + +static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model) +{ + globalDrawingSpace->size = + (model->space_xMax+1) * (model->space_yMax+1) * (model->space_zMax+1); + globalDrawingSpace->xMax = model->space_xMax; + globalDrawingSpace->yMax = model->space_yMax; + globalDrawingSpace->zMax = model->space_zMax; globalDrawingSpace->space = (SpaceUnit_t*) calloc(globalDrawingSpace->size, sizeof(SpaceUnit_t)); for (int i = 0; i < globalDrawingSpace->size; i++) { - globalDrawingSpace->space[i].nsite = model->siteNumber; - globalDrawingSpace->space[i].sites = (Site_t*) calloc(model->siteNumber, sizeof(Site_t)); + globalDrawingSpace->space[i].nSite = model->siteNumber; + globalDrawingSpace->space[i].sites = + (Site_t*) calloc(model->siteNumber, sizeof(Site_t)); } } -void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowArray, - Model_t *model) +static void ModelPrepareArrows(Space_t *globalDrawingSpace, + ArrowArray_t *arrowArray, + Model_t *model) { - arrowArray->array = (Arrow_t*) calloc(6, sizeof(Arrow_t)); - arrowArray->size = 6; + arrowArray->array = (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); + arrowArray->size = ARROW_NUMBER; //XXX hardcoded - // Creating some arrows - globalDrawingSpace->space[3].sites[1].narrow = 1; + // Creating some arrows XXX random walking + globalDrawingSpace->space[3].sites[1].nArrow = 1; arrowArray->array[0].siteId = 1; arrowArray->array[0].x = 3; - globalDrawingSpace->space[4].sites[0].narrow = 1; + globalDrawingSpace->space[4].sites[0].nArrow = 1; arrowArray->array[1].siteId = 0; arrowArray->array[1].x = 4; } - -void ModelInit(void) { - loadedModels = (Model_t*) calloc(MAX_MODEL_NUMBER, sizeof(Model_t)); -} - -void ModelDestroy(void) { - loadedModels = (Model_t*) calloc(MAX_MODEL_NUMBER, sizeof(Model_t)); -} diff --git a/src/scheduler.c b/src/scheduler.c index cce8345..93e1f45 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -83,10 +83,7 @@ static void *schedulerMain(void *scheduler) // area for a worker to operate on (as an approximation of a spheric one) Center_t *centersList, *workArea; Arrow_t *electedArrow; - // A cycle is defined as the total renew of all allocated workers. This - // measure is used to evaluate the scheduler efficiency. - // 1 cycle = n partial cycle / n max worker. - int createdWorkers; + int ncpu, nworker, err; // Getting scheduler argument structure @@ -96,7 +93,7 @@ static void *schedulerMain(void *scheduler) if (!args->nmaxThread) { // nmaxthread = 0 => no minimum ncpu = get_nprocs(); // allocating all the cpus available } else { // n thread = min(cpu, nmaxthread) - ncpu = MIN(get_nprocs(), args->nmaxThread); + ncpu = MIN(get_nprocs(), args->nMaxThread); } printLog("%d threads available.\n", ncpu); @@ -104,7 +101,6 @@ static void *schedulerMain(void *scheduler) workerArray = (Worker_t*) calloc(ncpu, sizeof(Worker_t)); nworker = 0; centersList = (Center_t*) calloc(1, sizeof(Center_t)); - createdWorkers = 0; // Initiate the arrowArray lock if (err = createArrowLock(args->arrowArray), err != 0) { // TODO destroy this @@ -114,8 +110,9 @@ static void *schedulerMain(void *scheduler) // // MAIN LOOP // - while (!args->pleaseStop && (createdWorkers / ncpu) <= args->nmaxCycles) { + while (!args->pleaseStop && 0 <= args->nMaxCycles) { //XXX count cycles //printLog("Scheduler #%lu online: cycle %d\n", *args->id, ncycles); + // // TODO statistics here @@ -134,9 +131,9 @@ static void *schedulerMain(void *scheduler) workArea = findWorkArea(centersList, electedArrow, args->ruleRadius, - args->globalDrawingSpace->xmax, - args->globalDrawingSpace->ymax, - args->globalDrawingSpace->zmax + args->globalDrawingSpace->xMax, + args->globalDrawingSpace->yMax, + args->globalDrawingSpace->zMax ); releaseArrowLock(args->arrowArray); } @@ -166,8 +163,7 @@ static void *schedulerMain(void *scheduler) workArea, nworker ); - // Increment partial cycle - createdWorkers++; + // Increment partial cycle TODO break; } } diff --git a/src/server.c b/src/server.c index 1c62d85..ac55834 100644 --- a/src/server.c +++ b/src/server.c @@ -131,7 +131,7 @@ static void *serverMain(void *server) printLog("Client request : %s\n",clientRequest); - for (int i = 0; i < LEN(cmdList); i++) { + for (int i = 0; i < LEN(cmdList); i++) { // TODO extract first command name if (strcmp(cmdList[i].name, clientRequest) == 0) { cmdList[i].execute(NULL); strcpy(serverAnswer,"OK\0"); diff --git a/src/supervisor.c b/src/supervisor.c new file mode 100644 index 0000000..7dd6c27 --- /dev/null +++ b/src/supervisor.c @@ -0,0 +1,43 @@ +//=-------------------------------------------------------------------------=// +// Supervisor module // +// // +// Copyright © 2021 The Gem-graph Project // +// // +// This file is part of gem-graph. // +// // +// This program is free software: you can redistribute it and/or modify // +// it under the terms of the GNU Affero General Public License as // +// published by the Free Software Foundation, either version 3 of the // +// License, or (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU Affero General Public License for more details. // +// // +// You should have received a copy of the GNU Affero General Public License // +// along with this program. If not, see . // +//=-------------------------------------------------------------------------=// + +#include "../include/base.h" + +static void *supervisorMain(void *scheduler); + +/* -------------------------------------------------------------------------- */ + +// -------------------------------------------------------------------------- // +// Supervisor init function // +// -------------------------------------------------------------------------- // +void SupervisorInit(Scheduler_t *scheduler) +{ + scheduler->id = (pthread_t*) calloc(1, sizeof(pthread_t)); + pthread_create(scheduler->id, NULL, supervisorMain, scheduler); +} + +// -------------------------------------------------------------------------- // +// Supervisor thread main function // +// -------------------------------------------------------------------------- // +static void *supervisorMain(void *scheduler) +{ + return NULL; +}