//=-------------------------------------------------------------------------=// // Model management 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" #include "../include/arrows.h" #include "../include/scheduler.h" #define MAX_MODEL_NUMBER 1 #define MAX_MODEL_NAME_SIZE 255 #define ARROW_NUMBER 6 #define SITE_NUMBER 2 #define MAX_CYCLES 10 #define MAX_THREAD 0 #define XMAX 39 #define YMAX 0 #define ZMAX 0 #define SPACE_SIZE (XMAX+1) * (YMAX+1) * (ZMAX+1) static Model_t **loadedModel; static int loadedModelSize; // begins to 1 static Model_t **knownModel; static int knownModelSize; // begins to 1 Model_t *lastModel; Model_t **lastModelAddr; static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model); static void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowArray, Model_t *model); /* -------------------------------------------------------------------------- */ void ModelCreate(Model_t **newModel) // TODO manage deletion and empty slots { // increment index knownModelSize++; // create socket knownModel = (Model_t**) realloc(knownModel, knownModelSize * sizeof(Model_t*)); // populate socket knownModel[knownModelSize-1] = (Model_t*) calloc(1, sizeof(Model_t)); // populate model knownModel[knownModelSize-1]->id = knownModelSize; // return value *newModel = knownModel[knownModelSize-1]; lastModel = knownModel[knownModelSize-1]; lastModelAddr = &knownModel[knownModelSize-1]; // cont. model population knownModel[knownModelSize-1]->name = (char *) calloc(1, sizeof(char) * MAX_MODEL_NAME_SIZE); knownModel[knownModelSize-1]->space_xMax = XMAX; knownModel[knownModelSize-1]->space_yMax = YMAX; knownModel[knownModelSize-1]->space_zMax = ZMAX; knownModel[knownModelSize-1]->nmaxThread = MAX_THREAD; knownModel[knownModelSize-1]->nmaxCycles = MAX_CYCLES; knownModel[knownModelSize-1]->siteNumber = SITE_NUMBER; } void printModels(char *buf) { sprintf(buf + strlen(buf),"Known models\n"); for (int i = 0; i <= knownModelSize-1; i++) { sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s\n", knownModel[i]->id, knownModel[i], knownModel[i]->name); } sprintf(buf + strlen(buf), "\nLoaded models\n"); for (int i = 0; i <= loadedModelSize-1; i++) { sprintf(buf + strlen(buf), "id: %d, addr: %p, name: %s\n", loadedModel[i]->id, loadedModel[i], loadedModel[i]->name); } } int ModelLoad(int id) // TODO unload ! { if (id <= 0 || id > knownModelSize) { return -1; } printLog("Loading model id %d (/%d models)...\n", id, knownModelSize); // Creating structure for the Scheduler knownModel[id-1]->scheduler = (Scheduler_t*) calloc(1, sizeof(Scheduler_t)); loadedModel = (Model_t**) realloc(loadedModel, ++loadedModelSize * sizeof(Model_t*)); loadedModel = (Model_t**) realloc(loadedModel, loadedModelSize * sizeof(Model_t*)); loadedModel[loadedModelSize-1] = knownModel[id-1]; return loadedModelSize; } int ModelRun(int id) { if (id <= 0 || id > loadedModelSize) return 0; if (!loadedModel[id-1]->scheduler) return 0; if (loadedModel[id-1]->isRunning) return 0; loadedModel[id-1]->scheduler->globalDrawingSpace = (Space_t*) calloc(1, sizeof(Space_t)); // TODO free this loadedModel[id-1]->scheduler->nMaxThread = knownModel[id-1]->nmaxThread; loadedModel[id-1]->scheduler->nMaxCycles = knownModel[id-1]->nmaxCycles; // Preparing global drawing space ModelPrepareSpace(loadedModel[id-1]->scheduler->globalDrawingSpace, loadedModel[id-1]); loadedModel[id-1]->scheduler->arrowArray = (ArrowArray_t*) calloc(1, sizeof(ArrowArray_t)); // TODO free this ModelPrepareArrows(loadedModel[id-1]->scheduler->globalDrawingSpace, loadedModel[id-1]->scheduler->arrowArray, loadedModel[id-1]); loadedModel[id-1]->scheduler->pleaseStop = false; loadedModel[id-1]->isRunning = true; SchedInit(loadedModel[id-1]->scheduler); printLog("Model %d launched\n", id); return 1; } int ModelStop(int id) { if (id <= 0 || id > loadedModelSize) { return 0; } if (!loadedModel[id-1]->scheduler) { return 0; } loadedModel[id-1]->scheduler->pleaseStop = true; printLog("Model %d stop bit set\n", id); SchedWait(loadedModel[id-1]->scheduler); SchedDestroy(loadedModel[id-1]->scheduler); loadedModel[id-1]->isRunning = false; return 1; } void ModelDelete(int id) { free(knownModel[id]->name); knownModel[id]->name = NULL; free(knownModel[id]); knownModel[id] = NULL; } void ModelShutdown(void) { for (int i = 0; i < loadedModelSize; i++) { ModelStop(i); } } void ModelSystemInit(void) { loadedModel = (Model_t**) calloc(1, sizeof(Model_t*)); // XXX read known models from files knownModel = (Model_t**) calloc(1, sizeof(Model_t*)); knownModelSize = 0; loadedModelSize = 0; } void ModelSystemDestroy(void) { for (int i = 0; i < loadedModelSize; i++) { ModelDelete(i); } free(loadedModel); loadedModel = NULL; free(knownModel); knownModel = NULL; } /* -------------------------------------------------------------------------- */ 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)); } } static void ModelPrepareArrows(Space_t *globalDrawingSpace, ArrowArray_t *arrowArray, Model_t *model) { arrowArray->array = (Arrow_t*) calloc(ARROW_NUMBER, sizeof(Arrow_t)); arrowArray->size = ARROW_NUMBER; //XXX hardcoded // Creating some arrows XXX random walking /* In each cell: the West (left) site is 0, the East (right) site is 1 */ globalDrawingSpace->space[1].sites[1].nArrow = 1; arrowArray->array[0].siteId = 1; arrowArray->array[0].x = 1; globalDrawingSpace->space[2].sites[0].nArrow = 1; arrowArray->array[1].siteId = 0; arrowArray->array[1].x = 2; globalDrawingSpace->space[3].sites[1].nArrow = 1; arrowArray->array[2].siteId = 1; arrowArray->array[2].x = 3; globalDrawingSpace->space[4].sites[0].nArrow = 1; arrowArray->array[3].siteId = 0; arrowArray->array[3].x = 4; globalDrawingSpace->space[10].sites[1].nArrow = 1; arrowArray->array[4].siteId = 1; arrowArray->array[4].x = 10; globalDrawingSpace->space[11].sites[0].nArrow = 1; arrowArray->array[5].siteId = 0; arrowArray->array[5].x = 11; }