Skip to content

Commit

Permalink
Improve GSL initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanzimanyi committed Jul 5, 2024
1 parent 1af400f commit de800e3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 58 deletions.
6 changes: 6 additions & 0 deletions meos/include/meos.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
/* GSL */
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
/* PostgreSQL */
#if MEOS
#include "postgres_int_defs.h"
Expand Down Expand Up @@ -353,6 +356,9 @@ extern bool meos_set_intervalstyle(char *newval, int extra);
extern char *meos_get_datestyle(void);
extern char *meos_get_intervalstyle(void);

extern gsl_rng *gsl_get_generation_rng(void);
extern gsl_rng *gsl_get_aggregation_rng(void);

extern void meos_initialize(const char *tz_str, error_handler_fn err_handler);
extern void meos_finalize(void);

Expand Down
58 changes: 56 additions & 2 deletions meos/src/general/meos.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef struct _stringlist
/*
* Add an item at the end of a stringlist.
*/
void
static void
add_stringlist_item(_stringlist **listhead, const char *str)
{
_stringlist *newentry = palloc(sizeof(_stringlist));
Expand Down Expand Up @@ -463,6 +463,8 @@ meos_initialize(const char *tz_str, error_handler_fn err_handler)
meos_initialize_timezone(tz_str);
/* Initialize PROJ */
_PJ_CONTEXT = proj_context_create();
/* Initialize GSL */
gsl_initialize();
return;
}

Expand All @@ -475,8 +477,60 @@ meos_finalize(void)
meos_finalize_timezone();
/* Finalize PROJ */
proj_context_destroy(_PJ_CONTEXT);
/* Finalize GSL */
gsl_rng_free(_GENERATION_RNG);
gsl_rng_free(_AGGREGATION_RNG);
return;
}

/*****************************************************************************/
#endif /* MEOS */

/***************************************************************************
* Initialize the Gnu Scientific Library
***************************************************************************/

/* Global variables */

static bool _GSL_INITIALIZED = false;
static gsl_rng *_GENERATION_RNG = NULL;
static gsl_rng *_AGGREGATION_RNG = NULL;

/**
* @brief Initialize the Gnu Scientific Library
*/
static void
gsl_initialize(void)
{
if (! _GSL_INITIALIZED)
{
gsl_rng_env_setup();
_GENERATION_RNG = gsl_rng_alloc(gsl_rng_default);
_AGGREGATION_RNG = gsl_rng_alloc(gsl_rng_ranlxd1);
_GSL_INITIALIZED = true;
}
return;
}

/**
* @brief Get the random generator used by the data generator
*/
gsl_rng *
gsl_get_generation_rng(void)
{
if (! _GSL_INITIALIZED)
gsl_initialize();
return _GENERATION_RNG;
}

/**
* @brief Get the random generator used by temporal aggregation
*/
gsl_rng *
gsl_get_aggregation_rng(void)
{
if (! _GSL_INITIALIZED)
gsl_initialize();
return _AGGREGATION_RNG;
}

/*****************************************************************************/
10 changes: 1 addition & 9 deletions meos/src/general/skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
#else
#include <utils/memutils.h>
#endif /* MEOS */
/* GSL */
#include <gsl/gsl_rng.h>
/* MEOS */
#include <meos.h>
#include <meos_internal.h>
Expand Down Expand Up @@ -106,10 +104,6 @@ ensure_same_skiplist_subtype(SkipList *state, uint8 subtype)
* Functions manipulating skip lists
*****************************************************************************/

/* Global variable for skip lists which require the gsl random generator */

static gsl_rng *_AGGREGATION_RNG = NULL;

#ifdef NO_FFSL
static int
ffsl(long int i)
Expand All @@ -127,9 +121,7 @@ ffsl(long int i)
static long int
gsl_random48()
{
if(! _AGGREGATION_RNG)
_AGGREGATION_RNG = gsl_rng_alloc(gsl_rng_ranlxd1);
return gsl_rng_get(_AGGREGATION_RNG);
return gsl_rng_get(gsl_get_aggregation_rng());
}

/**
Expand Down
61 changes: 14 additions & 47 deletions meos/src/point/tpoint_datagen.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,13 @@
#include <postgres.h>
#include <utils/float.h>
#include <utils/timestamp.h>
/* GSL */
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
/* MEOS */
#include <meos.h>
#include <meos_internal.h>
#include "point/tpoint_spatialfuncs.h"

/*****************************************************************************/

/* Global constant */

static const gsl_rng_type *_RNG_TYPE;

static bool _GSL_INITIALIZED = false;
static const gsl_rng_type *_RNG_TYPE;
static gsl_rng *_RNG;

/**
* @brief Initialize the Gnu Scientific Library
*/
static void
gsl_initialize(void)
{
gsl_rng_env_setup();
_RNG_TYPE = gsl_rng_default;
_RNG = gsl_rng_alloc(_RNG_TYPE);
_GSL_INITIALIZED = true;
return;
}

/**
* @brief Initialize the Gnu Scientific Library
*/
gsl_rng *
gsl_get_rng(void)
{
return _RNG;
}

/**
* @brief Return the angle in degrees between 3 points
*/
Expand Down Expand Up @@ -197,9 +164,6 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
int noAccel = 0, noDecel = 0, noStop = 0;
double twSumSpeed = 0.0, totalTravelTime = 0.0, totalWaitTime = 0.0;

if (! _GSL_INITIALIZED)
gsl_initialize();

/* First Pass: Compute the number of instants of the result */

for (i = 0; i < noEdges; i++)
Expand Down Expand Up @@ -291,9 +255,10 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
/* If the current speed is not considered as a stop, with
* a probability proportional to 1/maxSpeedEdge apply a
* deceleration event (p=90%) or a stop event (p=10%) */
if (gsl_rng_uniform(gsl_get_rng()) <= P_EVENT_C / maxSpeedEdge)
if (gsl_rng_uniform(gsl_get_generation_rng()) <= P_EVENT_C /
maxSpeedEdge)
{
if (gsl_rng_uniform(gsl_get_rng()) <= P_EVENT_P)
if (gsl_rng_uniform(gsl_get_generation_rng()) <= P_EVENT_P)
{
/* Apply stop event */
curSpeed = 0.0;
Expand All @@ -305,8 +270,8 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
else
{
/* Apply deceleration event */
curSpeed = curSpeed * gsl_ran_binomial(gsl_get_rng(), 0.5, 20) /
20.0;
curSpeed = curSpeed * gsl_ran_binomial(gsl_get_generation_rng(),
0.5, 20) / 20.0;
noDecel++;
if (verbosity == 3)
meos_error(INFO, MEOS_SUCCESS,
Expand Down Expand Up @@ -352,7 +317,8 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
/* If speed is zero add a wait time */
if (curSpeed < P_EPSILON_SPEED)
{
waitTime = gsl_ran_exponential(gsl_get_rng(), P_DEST_EXPMU);
waitTime = gsl_ran_exponential(gsl_get_generation_rng(),
P_DEST_EXPMU);
if (waitTime < P_EPSILON)
waitTime = P_DEST_EXPMU;
t = t + (int) (waitTime * 1e6); /* microseconds */
Expand All @@ -371,10 +337,10 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
curPos.y = p1.y + ((p2.y - p1.y) * fraction * (k + 1));
if (disturbData)
{
dx = (2.0 * P_GPS_STEPMAXERR * gsl_rng_uniform(gsl_get_rng())) -
P_GPS_STEPMAXERR;
dy = (2.0 * P_GPS_STEPMAXERR * gsl_rng_uniform(gsl_get_rng())) -
P_GPS_STEPMAXERR;
dx = (2.0 * P_GPS_STEPMAXERR *
gsl_rng_uniform(gsl_get_generation_rng())) - P_GPS_STEPMAXERR;
dy = (2.0 * P_GPS_STEPMAXERR *
gsl_rng_uniform(gsl_get_generation_rng())) - P_GPS_STEPMAXERR;
errx += dx;
erry += dy;
if (errx > P_GPS_TOTALMAXERR)
Expand Down Expand Up @@ -413,10 +379,11 @@ create_trip(LWLINE **lines, const double *maxSpeeds, const int *categories,
if (curSpeed > P_EPSILON_SPEED && i < noEdges - 1)
{
int nextCategory = categories[i + 1];
if (gsl_rng_uniform(gsl_get_rng()) <= P_DEST_STOPPROB[category][nextCategory])
if (gsl_rng_uniform(gsl_get_generation_rng()) <=
P_DEST_STOPPROB[category][nextCategory])
{
curSpeed = 0.0;
waitTime = gsl_ran_exponential(gsl_get_rng(), P_DEST_EXPMU);
waitTime = gsl_ran_exponential(gsl_get_generation_rng(), P_DEST_EXPMU);
if (waitTime < P_EPSILON)
waitTime = P_DEST_EXPMU;
t = t + (int) (waitTime * 1e6); /* microseconds */
Expand Down

0 comments on commit de800e3

Please sign in to comment.