WIP: integrating with model ?

This commit is contained in:
Adrien Bourmault 2024-03-20 23:59:00 +02:00 committed by Adrien Bourmault
parent 3760625632
commit 1625658b45
Signed by: neox
GPG Key ID: 95F65F55F682A17A
7 changed files with 165 additions and 160 deletions

View File

@ -27,4 +27,13 @@
/* -------------------------------------------------------------------------- */
/*
* Structure describing an arrow
*/
struct arrow_t {
uint load;
uint site;
uint x;
uint y;
uint z;
};

View File

@ -27,10 +27,12 @@
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <omp.h>
#include <math.h>
#define BASE_H
@ -56,117 +58,8 @@ struct parameters_t
char *modelDir;
};
/*
* Structure describing an arrow
*/
struct arrow_t {
uint load;
uint site;
uint x;
uint y;
uint z;
};
/*
* Structure describing a transition
*/
struct transition_t {
uint id;
struct condition_t *parent;
struct arrow_t *arrows;
};
/*
* Structure describing a condition
*/
struct condition_t {
uint id;
struct condition_t *parent;
struct arrow_t *arrows;
};
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
uint x_dim;
uint y_dim;
uint 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
uint id;
uint owner_id;
time_t date;
struct space_t *space;
};
struct model_t {
// Metadata
uint id;
uint owner_id;
time_t date;
union version
{
uint major;
uint minor;
};
// User friendly metadata
char *owner_name;
char *model_name;
// Model parameters
uint multiplicity; // number of sites in a space_unit
uint dimension; // number of space dimensions
// Simulation parameters
uint max_thread;
uint 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;
// Array of conditions
struct condition_t *conditions;
// Array of transitions
struct transition_t *transitions;
};
struct worker_t
{
uint id;
uint status;
struct arrow_t *elected_arrow;
};
struct scheduler_t
{
uint id;
uint n_workers;
bool pleaseStop;
struct model_t **models; // Queue (array) of waiting models
struct worker_t **workers; // Workers array
};

View File

@ -32,6 +32,8 @@
#include <libxml/parser.h>
#include <libxml/xmlschemas.h>
#include "../include/arrows.h"
/* -------------------------------------------------------------------------- */
#define MODEL_STRING_SIZE 64
@ -48,6 +50,74 @@
/* -------------------------------------------------------------------------- */
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;
};
// XML handlers
xmlDocPtr model;
xmlHashTablePtr model_hashtable;
// 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;
};
// -------------------------------------------------------------------------- //
// Model init function (and model discovery) //
// -------------------------------------------------------------------------- //
@ -63,14 +133,17 @@
// -------------------------------------------------------------------------- //
// int model_load (int id);
bool model_init(const char *content, size_t length, const char *basename);
bool model_init(struct model_t *self,
const char *content,
size_t length,
const char *basename);
// -------------------------------------------------------------------------- //
// Unload a model //
// -------------------------------------------------------------------------- //
// int model_unload (int id);
bool model_shutdown(void);
bool model_shutdown(struct model_t *self);
// -------------------------------------------------------------------------- //
// Add a model to the known model list //
@ -101,14 +174,15 @@ bool model_shutdown(void);
// Parsing primitives //
// -------------------------------------------------------------------------- //
char model_get_dim(void);
char model_get_dim(struct model_t *self);
long model_get_dim_value(const char *axis);
long model_get_dim_value(struct model_t *self, const char *axis);
char model_get_multiplicity(void);
char model_get_multiplicity(struct model_t *self);
bool model_get_next_state(char *new_state_id);
bool model_get_next_state(struct model_t *self, char *new_state_id);
bool model_get_next_arrow(struct arrow_t *new_arrow,
bool model_get_next_arrow(struct model_t *self,
struct arrow_t *new_arrow,
const char *state_id,
char dimension);

View File

@ -27,6 +27,9 @@
#include <sys/sysinfo.h>
#include "../include/worker.h"
#include "../include/model.h"
/* -------------------------------------------------------------------------- */
enum
@ -43,6 +46,16 @@ enum
SCHED_CONTINUE
};
struct scheduler_t
{
int id;
int n_workers;
int pleaseStop;
struct model_t **models;
struct worker_t **workers;
};
// -------------------------------------------------------------------------- //
// Scheduler main function //
// -------------------------------------------------------------------------- //

View File

@ -25,6 +25,9 @@
#include "../include/base.h"
#endif
#include "../include/scheduler.h"
#include "../include/arrows.h"
/* -------------------------------------------------------------------------- */
enum
@ -41,10 +44,21 @@ enum
WORKER_CONTINUE
};
struct scheduler_t;
struct worker_t
{
int id;
int thread_num;
int status;
struct arrow_t *elected_arrow;
};
// -------------------------------------------------------------------------- //
// Worker init function //
// -------------------------------------------------------------------------- //
void worker_start(struct worker_t *worker, struct scheduler_t *scheduler);
void worker_start(struct worker_t *self, struct scheduler_t *scheduler);
// -------------------------------------------------------------------------- //
// Worker destructor function //

View File

@ -23,7 +23,6 @@
#include "../include/base.h"
#include "../include/model.h"
#include "../include/arrows.h"
#include "../include/scheduler.h"
#define READ_SITE 1 << 0
#define READ_WEIGHT 1 << 1
@ -36,10 +35,10 @@
/* -------------------------------------------------------------------------- */
static xmlDocPtr model;
static xmlHashTablePtr model_hashtable;
bool model_init(const char *content, size_t length, const char *basename)
bool model_init(struct model_t *self,
const char *content,
size_t length,
const char *basename)
{
xmlNode *node;
@ -50,41 +49,41 @@ bool model_init(const char *content, size_t length, const char *basename)
*/
LIBXML_TEST_VERSION
model = xmlReadMemory(content, length, basename, NULL, 0);
self->model = xmlReadMemory(content, length, basename, NULL, 0);
if (model == NULL ) {
if (self->model == NULL ) {
return false;
}
node = xmlDocGetRootElement(model);
node = xmlDocGetRootElement(self->model);
if (node == NULL) {
fprintf(stderr, "Empty XML model !\n");
xmlFreeDoc(model);
xmlFreeDoc(self->model);
return false;
}
if (xmlStrcmp(node->name, (xmlChar *) "gem-graph-model")) {
fprintf(stderr, "document of the wrong type, root node != gem-graph-model\n");
xmlFreeDoc(model);
xmlFreeDoc(self->model);
return false;
}
model_hashtable = xmlHashCreate(0);
self->model_hashtable = xmlHashCreate(0);
if (model_hashtable == NULL) {
if (self->model_hashtable == NULL) {
fprintf(stderr, "Can't create model hash table !\n");
xmlFreeDoc(model);
xmlFreeDoc(self->model);
return false;
}
return true;
}
bool model_shutdown(void)
bool model_shutdown(struct model_t *self)
{
xmlFreeDoc(model);
xmlHashFree(model_hashtable, NULL);
xmlFreeDoc(self->model);
xmlHashFree(self->model_hashtable, NULL);
// Cleanup function for the XML library
xmlCleanupParser();
@ -95,7 +94,7 @@ bool model_shutdown(void)
return true;
}
/******************************************************************************/
/* -------------------------------------------------------------------------- */
static inline xmlNodePtr getNextChild(xmlNodePtr node, xmlChar *last)
{
@ -131,16 +130,16 @@ static inline xmlChar* getLastTag(xmlChar *path)
return path; // which is no more the given path but only its last tag !
}
/******************************************************************************/
/* -------------------------------------------------------------------------- */
static xmlNodePtr model_get_node(xmlChar *path)
static xmlNodePtr model_get_node(struct model_t *self, xmlChar *path)
{
xmlNodePtr node;
xmlChar *extrait;
xmlChar *reste, *last, *affich;
// Lookup for node from path in hash table
node = xmlHashLookup(model_hashtable, path);
node = xmlHashLookup(self->model_hashtable, path);
// Found a node in hash table
if (node) {
@ -151,7 +150,7 @@ static xmlNodePtr model_get_node(xmlChar *path)
reste = path;
affich = reste;
last = getLastTag(reste);
node = xmlDocGetRootElement(model);
node = xmlDocGetRootElement(self->model);
while (xmlStrchr (reste, '/')) {
extrait = getFirstTag(reste);
@ -166,7 +165,7 @@ static xmlNodePtr model_get_node(xmlChar *path)
while (node && xmlStrcmp(node->name, last)) {
node = node->next;
}
xmlHashAddEntry (model_hashtable, path, node);
xmlHashAddEntry (self->model_hashtable, path, node);
}
return node;
@ -176,7 +175,7 @@ static xmlNodePtr model_get_node(xmlChar *path)
return NULL;
}
/******************************************************************************/
/* -------------------------------------------------------------------------- */
static inline long model_get_node_long_attrib(xmlNodePtr node, char *id)
{
@ -221,13 +220,13 @@ static inline bool model_get_node_str_attrib(xmlNodePtr node,
return false;
}
/******************************************************************************/
/* -------------------------------------------------------------------------- */
char model_get_dim(void)
char model_get_dim(struct model_t *self)
{
xmlAttr *attribute;
xmlChar* value;
xmlNodePtr node = model_get_node(
xmlNodePtr node = model_get_node(self,
(xmlChar *)"parameters/space-param/dimension");
if (xmlHasProp (node, (xmlChar *) "z")) return 3;
@ -236,22 +235,22 @@ char model_get_dim(void)
return 0;
}
long model_get_dim_value(const char *axis)
long model_get_dim_value(struct model_t *self, const char *axis)
{
xmlAttr *attribute;
xmlChar *value;
long ret_value;
xmlNodePtr node = model_get_node(
xmlNodePtr node = model_get_node(self,
(xmlChar *)"parameters/space-param/dimension");
return model_get_node_long_attrib(node, axis);
}
char model_get_multiplicity(void)
char model_get_multiplicity(struct model_t *self)
{
xmlAttr *attribute;
xmlChar* value;
xmlNodePtr node = model_get_node(
xmlNodePtr node = model_get_node(self,
(xmlChar *)"parameters/space-param/site_multiplicity");
if (node->children)
@ -261,7 +260,7 @@ char model_get_multiplicity(void)
return 0;
}
bool model_get_next_state(char *new_state_id)
bool model_get_next_state(struct model_t *self, char *new_state_id)
{
static xmlNodePtr cur_node = NULL;
xmlAttr *attribute;
@ -269,7 +268,7 @@ bool model_get_next_state(char *new_state_id)
if (cur_node == NULL) {
// Get first state
cur_node = model_get_node((xmlChar *)"savedstates/state");
cur_node = model_get_node(self, (xmlChar *)"savedstates/state");
} else {
// Get next state
@ -287,7 +286,7 @@ bool model_get_next_state(char *new_state_id)
return false;
}
long model_get_state_arrows_count(const char *state_id)
long model_get_state_arrows_count(struct model_t *self, const char *state_id)
{
xmlNodePtr cur_node = NULL;
xmlAttr *attribute;
@ -301,7 +300,7 @@ long model_get_state_arrows_count(const char *state_id)
assert(state_id);
// Get first state node
cur_node = model_get_node((xmlChar *)"savedstates/state");
cur_node = model_get_node(self, (xmlChar *)"savedstates/state");
// Lookup in properties
while (cur_node && cur_node->properties) {
@ -338,7 +337,8 @@ long model_get_state_arrows_count(const char *state_id)
return value;
}
bool model_get_next_arrow(struct arrow_t *new_arrow,
bool model_get_next_arrow(struct model_t *self,
struct arrow_t *new_arrow,
const char *state_id,
char dimension)
{
@ -356,7 +356,7 @@ bool model_get_next_arrow(struct arrow_t *new_arrow,
if (cur_node == NULL) {
// Get first state node
cur_node = model_get_node((xmlChar *)"savedstates/state");
cur_node = model_get_node(self, (xmlChar *)"savedstates/state");
// Lookup in properties
while (cur_node && cur_node->properties) {
@ -470,3 +470,7 @@ bool model_get_next_arrow(struct arrow_t *new_arrow,
cur_node = NULL;
return false;
}
/* -------------------------------------------------------------------------- */

View File

@ -27,12 +27,10 @@
double truc;
static int worker_new_id (void)
{
static int id = 0;
return id;
}
//
//
//
//
void worker_start(struct worker_t *self, struct scheduler_t *scheduler)
{
// Locking ressources