diff --git a/include/base.h b/include/base.h index eec6641..45f0157 100644 --- a/include/base.h +++ b/include/base.h @@ -22,7 +22,6 @@ #include #include -#include #include #include #include @@ -31,16 +30,21 @@ #include #include #include +#include #define BASE_H /* -------------------------------------------------------------------------- */ #define LOGMSG "<%s:%s()>" -#define printlog(FORMAT, ...) printf("\e[0m" LOGMSG " " FORMAT "\e[0m", \ - __FILE__,__func__, ##__VA_ARGS__) -#define printerr(FORMAT,...) fprintf(stderr, "\e[0m" LOGMSG " " FORMAT "\e[0m",\ - __FILE__,__func__, ##__VA_ARGS__) + +#define printlog(FORMAT, ...) printf("\e[0;36m" LOGMSG "\e[0m" " " FORMAT \ + "\e[0m", __FILE__,__func__, ##__VA_ARGS__) + +#define printerr(FORMAT,...) fprintf(stderr, "\e[0;31m" LOGMSG "\e[0m" " " \ + FORMAT "\e[0m", __FILE__,__func__, \ + ##__VA_ARGS__) + #define LEN(x) (sizeof(x) / sizeof((x)[0])) /* -------------------------------------------------------------------------- */ @@ -63,12 +67,79 @@ struct arrow_t { uint z; }; -struct model_t { +struct space_unit_t +{ + bool lock; + struct arrow_t **sites; // Array of struct arrow_t* elements + // - lenght is multiplicity +}; + +struct space_t +{ + // Dimensions of space. + // Note that a value 0 is not allowed, minimum is 1 + int x_dim; + int y_dim; + int z_dim; + + struct space_unit_t *units; // (flat) arraw of space_unit_t elements : + // - lenght is x_dim * y_dim * z_dim + // - access to the (x,y,z) is done by + // units[ x + // + dim_x * y + // + dim_x * dim_y * z ] +}; + +struct state_t +{ + // Metadata int id; -} + int owner_id; + time_t date; + + struct space_t *space; +}; + +struct model_t { + + // Metadata + int id; + int owner_id; + time_t date; + union version + { + int major; + int minor; + }; + + // User friendly metadata + char *owner_name; + char *model_name; + + // Model parameters + int multiplicity; // number of sites in a space_unit + int dimension; // number of space dimensions + + // Simulation parameters + int max_thread; + int max_cycles; + + // Handler to the current space of the model + struct space_t *space; + + // Handler to the saved states of the model + struct state_t **states; + +}; + +struct worker_t +{ + int id; + int status; +}; struct scheduler_t { int id; - struct model_t *models; + struct model_t **model; // Queue (array) of waiting models }; diff --git a/include/centers.h b/include/centers.h deleted file mode 100644 index 27e9e74..0000000 --- a/include/centers.h +++ /dev/null @@ -1,30 +0,0 @@ -//=-------------------------------------------------------------------------=// -// Local workers definition // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// 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 . // -//=-------------------------------------------------------------------------=// - -#pragma once -#ifndef BASE_H - #include "../include/base.h" -#endif - -/* -------------------------------------------------------------------------- */ - - diff --git a/include/model.h b/include/model.h index a991392..154aa9b 100644 --- a/include/model.h +++ b/include/model.h @@ -48,17 +48,6 @@ /* -------------------------------------------------------------------------- */ -bool model_init(const char *content, size_t length, const char *basename); -bool model_shutdown(void); - -char model_get_dim(void); -long model_get_dim_value(const char *axis); -char model_get_multiplicity(void); -bool model_get_next_state(char *new_state_id); -bool model_get_next_arrow(struct arrow_t *new_arrow, - const char *state_id, - char dimension); - // -------------------------------------------------------------------------- // // Model init function (and model discovery) // // -------------------------------------------------------------------------- // @@ -74,11 +63,15 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, // -------------------------------------------------------------------------- // // int model_load (int id); +bool model_init(const char *content, size_t length, const char *basename); + // -------------------------------------------------------------------------- // // Unload a model // // -------------------------------------------------------------------------- // // int model_unload (int id); +bool model_shutdown(void); + // -------------------------------------------------------------------------- // // Add a model to the known model list // // -------------------------------------------------------------------------- // @@ -103,3 +96,19 @@ bool model_get_next_arrow(struct arrow_t *new_arrow, // Stop and unload all loaded or running model // // -------------------------------------------------------------------------- // // void model_shutdown (void); + +// -------------------------------------------------------------------------- // +// Parsing primitives // +// -------------------------------------------------------------------------- // + +char model_get_dim(void); + +long model_get_dim_value(const char *axis); + +char model_get_multiplicity(void); + +bool model_get_next_state(char *new_state_id); + +bool model_get_next_arrow(struct arrow_t *new_arrow, + const char *state_id, + char dimension); diff --git a/include/scheduler.h b/include/scheduler.h index 1a4b057..bc36f8c 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -29,10 +29,24 @@ /* -------------------------------------------------------------------------- */ +enum +{ + // quitting values + SCHED_NORMAL_EXIT, + SCHED_OMP_ERROR, + SCHED_UNKNOWN_ERROR, + + // non-quitting / non-fatal errors + SCHED_NON_FATAL_ERRORS, //meta-code + SCHED_MODEL_ERROR, + + SCHED_CONTINUE +}; + // -------------------------------------------------------------------------- // -// Scheduler init function // +// Scheduler main function // // -------------------------------------------------------------------------- // -void sched_init(struct scheduler_t *scheduler); +int sched_start (struct scheduler_t *scheduler, struct parameters_t *parameters); // -------------------------------------------------------------------------- // // Scheduler content destructor function // diff --git a/include/worker.h b/include/worker.h index 1bdd01b..a95ab3c 100644 --- a/include/worker.h +++ b/include/worker.h @@ -27,10 +27,24 @@ /* -------------------------------------------------------------------------- */ +enum +{ + // quitting values + WORKER_NORMAL_EXIT, + WORKER_OMP_ERROR, + WORKER_UNKNOWN_ERROR, + + // non-quitting / non-fatal errors + WORKER_NON_FATAL_ERRORS, //meta-code + WORKER_MODEL_ERROR, + + WORKER_CONTINUE +}; + // -------------------------------------------------------------------------- // // Worker init function // // -------------------------------------------------------------------------- // -//void worker_init(worker_t *worker); +void worker_start(struct worker_t *worker, struct scheduler_t *scheduler); // -------------------------------------------------------------------------- // // Worker destructor function // diff --git a/manifest.scm b/manifest.scm index 01a3a6d..ffdc2c5 100644 --- a/manifest.scm +++ b/manifest.scm @@ -22,5 +22,6 @@ "glib" "mesa-headers" "mesa" + "libomp" ) ) diff --git a/src/centers.c b/src/centers.c deleted file mode 100644 index f9fdcdb..0000000 --- a/src/centers.c +++ /dev/null @@ -1,27 +0,0 @@ -//=-------------------------------------------------------------------------=// -// Centers management module // -// // -// Copyright © 2021 Libre en Communs (contact@a-lec.org) // -// Copyright © 2021 Adrien Bourmault (neox@a-lec.org) // -// // -// 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/centers.h" - -/* -------------------------------------------------------------------------- */ - - diff --git a/src/cmds.c b/src/cmds.c index c4ea45f..306d737 100644 --- a/src/cmds.c +++ b/src/cmds.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/cmds.h" #include "../include/scheduler.h" #include "../include/model.h" diff --git a/src/main.c b/src/main.c index 272f55e..3bd8ad7 100644 --- a/src/main.c +++ b/src/main.c @@ -21,8 +21,7 @@ //=-------------------------------------------------------------------------=// #include "../include/base.h" -#include "../include/server.h" -#include "../include/model.h" +#include "../include/scheduler.h" /* -------------------------------------------------------------------------- */ @@ -41,8 +40,9 @@ int main(int argc, char **argv) { int options; time_t t; - int returnValue = 0; + int returnValue = SCHED_CONTINUE; struct parameters_t parameters = {0}; + struct scheduler_t scheduler = {0}; while ((options = getopt(argc, argv, ":C:M:U:")) != -1) { switch (options) { @@ -133,6 +133,10 @@ int main(int argc, char **argv) /* free(server); */ /* server = NULL; */ + while (returnValue > SCHED_NON_FATAL_ERRORS) { + returnValue = sched_start (&scheduler, ¶meters); + } + free(parameters.userDir); free(parameters.modelDir); free(parameters.configDir); diff --git a/src/model.c b/src/model.c index ecd50ae..5e6e4d7 100644 --- a/src/model.c +++ b/src/model.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/model.h" #include "../include/arrows.h" #include "../include/scheduler.h" @@ -33,6 +34,8 @@ #define SUCCESSFUL_READ_ARROW_XY (READ_SITE | READ_WEIGHT | READ_X | READ_Y) #define SUCCESSFUL_READ_ARROW_XYZ (READ_SITE | READ_WEIGHT | READ_X | READ_Y | READ_Z) +/* -------------------------------------------------------------------------- */ + static xmlDocPtr model; static xmlHashTablePtr model_hashtable; diff --git a/src/scheduler.c b/src/scheduler.c index 402e744..5f0b488 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -20,11 +20,63 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/scheduler.h" -#include "../include/centers.h" #include "../include/worker.h" -#include "../include/arrows.h" -#include +/* -------------------------------------------------------------------------- */ + +static int sched_new_id (void) +{ + static int id = 0; + return id; +} + +int sched_start (struct scheduler_t *self, struct parameters_t *parameters) +{ + int n_threads = omp_get_max_threads(); + int n_arrows; + int iter_per_cycle; + struct worker_t **workers; + + self->id = sched_new_id(); + + printlog("Hey, I'm the scheduler %d and I can work with %d threads !\n", + self->id, + n_threads); + + workers = calloc(n_threads, sizeof(struct worker_t*)); + + //XXX + n_arrows = 1000; + iter_per_cycle = (n_arrows / n_threads) + 1; + + printlog("Needed iteration per cycle (%d arrows to work on) : %d\n", + n_arrows, + iter_per_cycle); + + printlog("Start of the simulation cycle\n"); + for (int iter = 0; iter < iter_per_cycle; iter++) { + printlog("Start of the thread cycle (iter %d)\n", iter); + + #pragma omp parallel + { + int thread_num; + thread_num = omp_get_thread_num(); + workers[thread_num] = calloc(1, sizeof(struct worker_t)); + + workers[thread_num]->id = thread_num; + worker_start(workers[thread_num], self); + + free(workers[thread_num]); + } + + printlog("End of the thread cycle\n"); + } + + free(workers); + + return SCHED_NORMAL_EXIT; +} diff --git a/src/server.c b/src/server.c index 9b89ffa..b2e0981 100644 --- a/src/server.c +++ b/src/server.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/server.h" #include "../include/cmds.h" diff --git a/src/supervisor.c b/src/supervisor.c index 3d8fae5..0ff04ec 100644 --- a/src/supervisor.c +++ b/src/supervisor.c @@ -20,6 +20,7 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/supervisor.h" /* -------------------------------------------------------------------------- */ diff --git a/src/worker.c b/src/worker.c index f758ed5..0ab7186 100644 --- a/src/worker.c +++ b/src/worker.c @@ -20,8 +20,31 @@ // along with this program. If not, see . // //=-------------------------------------------------------------------------=// +#include "../include/base.h" #include "../include/worker.h" /* -------------------------------------------------------------------------- */ +static int worker_new_id (void) +{ + static int id = 0; + return id; +} +void worker_start(struct worker_t *self, struct scheduler_t *scheduler) +{ + unsigned int random_time; + + random_time = (unsigned int)(rand() % 2); + + printlog("Coucou, c'est le worker %d (et je vais dormir %d s)\n", + self->id, + random_time); + + sleep(random_time); + + printlog("Fin du worker %d\n", + self->id); + + self->status = WORKER_NORMAL_EXIT; +}