Research Article

Teaching Scientific Computing: A Model-Centered Approach to Pipeline and Parallel Programming with C

Listing 1

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#define OUT_FILE “out_seq.txt” //results
#define MC 20 //number of Monte-Carlo simulations
#define N 1000 //number of clients
#define M 5 //number of servicing phases
//parameters of the exponential distributions
int lambda[M + 1]  =  {0};
double tau  =  0; //interarrival time
double st  =  0; //sojourn time
//sojourn time for the previous client
double st_prev[M]  =  {0};
//sojourn time for each MC trial
double results[MC]  =  {0};
int main(int argc, char argv) {
gsl_rng ran; //random generator
gsl_rng_env_setup();
ran  =  gsl_rng_alloc(gsl_rng_ranlxs2);
//init lambda (heavy traffic case)
lambda0]  =  30000;
for (int i  =  1; i < M; i++)lambda[i]  =  lambda[i − 1] − 25000/M;
lambda[M]  =  5000;
for (unsigned j  =  0; j < MC; j++) {
tau  =  gsl_ran_exponential(ran, 1.0/lambda0);
st  =  0;
for (unsigned i  =  0; i < M; i++) st_prev[i]  =  0.;
for (unsigned i  =  0; i < N; i++) {
for (unsigned t  =  0; t < M; t++) {
//recurrent equation
st += gsl_ran_exponential(ran, 1.0/lambda[t + 1]) + fmax(0.0, st_prev[t] − st − tau);
st_prev[t]  =  st;
results [j]  =  st;
}
//printing results to file
FILE fp;
const char DATA_FILE  =  OUT_FILE;
fp  =  fopen(DATA_FILE, “w”);
fprintf(fp, “%d%s%d%s%dn”, N, “,”, lambda0, “,”, lambda[M]);
for (int i  =  0; i < MC − 1; i++) fprintf(fp, “%f%s”, results[i], “,”);
fprintf(fp, “%fn”, results[MC − 1]);
fclose(fp);
gsl_rng_free(ran);
return(0);
}