Skip to content

Commit

Permalink
format C benchmarks and adjust cog comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cmnrd committed Mar 8, 2024
1 parent 0279203 commit f2e327c
Show file tree
Hide file tree
Showing 23 changed files with 4,190 additions and 4,484 deletions.
262 changes: 125 additions & 137 deletions C/Savina/src/BenchmarkRunner.lf
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
target C;
target C

/**
* Reactor that starts the kernel of a benchmark, measures its runtime and outputs
* the results for a given number of iterations.
* Reactor that starts the kernel of a benchmark, measures its runtime and outputs the results for a
* given number of iterations.
*
* This reactor is instantiated by the main reactor of a benchmark and
* the startup reaction of this reactor is the starting point for that benchmark.
* The reactor runs a given number of iterations of the benchmark, measures
* the runtime of each iteration and outputs them. The benchmark itself is responsible
* to reset its state between the iterations.
* A benchmark can have an optional initialization phase that is run once before
* the first iteration and is not measured.
* A benchmark can have an optional cleanup phase after each iteration before
* the next iteration start which is not considered in the runtime measurement.
* This reactor is instantiated by the main reactor of a benchmark and the startup reaction of this
* reactor is the starting point for that benchmark. The reactor runs a given number of iterations
* of the benchmark, measures the runtime of each iteration and outputs them. The benchmark itself
* is responsible to reset its state between the iterations. A benchmark can have an optional
* initialization phase that is run once before the first iteration and is not measured. A benchmark
* can have an optional cleanup phase after each iteration before the next iteration start which is
* not considered in the runtime measurement.
*
* How to use:
* - Instantiate this reactor in the main reactor of the benchmark.
* - Connect the ports start, finish with
* the appropriate reactors of the benchmark.
* - Connect the ports start, finish with the appropriate reactors of the benchmark.
* - Create a startup reaction in the main reactor that calls printBenchmarkInfo(),
*
* Prototype startup reaction in the main reactor of a benchmark:
* runner = new BenchmarkRunner(num_iterations=num_iterations);
* reaction(startup) {=
* printBenchmarkInfo("ThreadRingReactorLFCppBenchmark");
* printSystemInfo();
* =}
* Prototype startup reaction in the main reactor of a benchmark: runner = new
* BenchmarkRunner(num_iterations=num_iterations); reaction(startup) {=
* printBenchmarkInfo("ThreadRingReactorLFCppBenchmark"); printSystemInfo();
* =}
*
* @param num_iterations How many times to execute the kernel of the benchmark to measure.
*
Expand All @@ -34,139 +29,132 @@ target C;
* @author Matt Chorlian
* @author Arthur Deng
*/

preamble {=
#include <stdio.h>
#include <stdio.h>
=}

reactor BenchmarkRunner(num_iterations:size_t=12) {

/** Signal to start execution. Set this input from a startup reaction in the main reactor. */
input inStart:bool;

/** Signals for starting and finishing the kernel and runtime measurement. */
output start:bool;
input finish:bool;
reactor BenchmarkRunner(num_iterations: size_t = 12) {
/** Signal to start execution. Set this input from a startup reaction in the main reactor. */
input inStart: bool

/** Signals for starting and finishing the kernel and runtime measurement. */
output start: bool
input finish: bool

/** Events to switch between the phases of running the iterations. */
logical action nextIteration:bool;
logical action done:bool;
/** Events to switch between the phases of running the iterations. */
logical action nextIteration: bool
logical action done: bool

/** Number of iterations already executed. */
state count:unsigned=0;
/** Number of iterations already executed. */
state count: unsigned = 0

/** Start time for runtime measurement. */
state startTime:instant_t;
/** Start time for runtime measurement. */
state startTime: instant_t

/** Runtime measurements. */
state measuredTimes: interval_t[];
/** Runtime measurements. */
state measuredTimes: interval_t[]

preamble {=
static double toMS(interval_t t) {
return t / 1000000.0;
}

reaction(startup) -> nextIteration {=
// Initialize an array of interval_t
self->measuredTimes = (interval_t *) calloc(self->num_iterations, sizeof(interval_t));
lf_schedule(nextIteration, 0);
=}
int comp (const void * elem1, const void * elem2) {
int f = *((double*)elem1);
int s = *((double*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}


reaction(nextIteration) -> start, done {=
if (self->count < self->num_iterations) {
self->startTime = lf_time_physical();
lf_set(start, true);
} else {
lf_schedule(done, 0);
static double median(double* execTimes, int size) {
if (size == 0) {
return 0.0;
}
=}

reaction(finish) -> nextIteration {=
interval_t end_time = lf_time_physical();
interval_t duration = end_time - self->startTime;
self->measuredTimes[self->count] = duration;
self->count += 1;

printf("Iteration %d - %.3f ms\n", self->count, toMS(duration));

lf_schedule(nextIteration, 0);
=}

reaction(done) {=
double* measuredMSTimes = getMSMeasurements(self->measuredTimes, self->num_iterations);
qsort(measuredMSTimes, self->num_iterations, sizeof(double), comp);

printf("Execution - Summary:\n");
printf("Best Time:\t %.3f msec\n", measuredMSTimes[0]);
printf("Worst Time:\t %.3f msec\n", measuredMSTimes[self->num_iterations - 1]);
printf("Median Time:\t %.3f msec\n", median(measuredMSTimes, self->num_iterations));
lf_request_stop();
=}

preamble {=

static double toMS(interval_t t) {
return t / 1000000.0;
}

int comp (const void * elem1, const void * elem2) {
int f = *((double*)elem1);
int s = *((double*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}

static double median(double* execTimes, int size) {
if (size == 0) {
return 0.0;
}

int middle = size / 2;
if(size % 2 == 1) {
return execTimes[middle];
} else {
return (execTimes[middle-1] + execTimes[middle]) / 2;
}
}

static double* getMSMeasurements(interval_t* measured_times, int num_iterations) {

double* msMeasurements = (double *) calloc(num_iterations, sizeof(double));
for (int i = 0; i < num_iterations; i++) {
msMeasurements[i] = toMS(measured_times[i]);
}

return msMeasurements;
int middle = size / 2;
if(size % 2 == 1) {
return execTimes[middle];
} else {
return (execTimes[middle-1] + execTimes[middle]) / 2;
}
=}
}

preamble {=
static double* getMSMeasurements(interval_t* measured_times, int num_iterations) {

void printBenchmarkInfo(char* benchmarkId) {
printf("Benchmark: %s\n", benchmarkId);
double* msMeasurements = (double *) calloc(num_iterations, sizeof(double));
for (int i = 0; i < num_iterations; i++) {
msMeasurements[i] = toMS(measured_times[i]);
}

void printSystemInfo() {

printf("System information\n");
printf("O/S Name: ");

#ifdef _WIN32
printf("Windows 32-bit");
#elif _WIN64
printf("Windows 64-bit");
#elif __APPLE__ || __MACH__
printf("Mac OSX");
#elif __linux__
printf("Linux");
#elif __FreeBSD__
printf("FreeBSD");
#elif __unix || __unix__
printf("Unix");
#else
printf("Other");
#endif

printf("\n");
}
=}
return msMeasurements;
}
=}

preamble {=
void printBenchmarkInfo(char* benchmarkId) {
printf("Benchmark: %s\n", benchmarkId);
}

void printSystemInfo() {

printf("System information\n");
printf("O/S Name: ");

#ifdef _WIN32
printf("Windows 32-bit");
#elif _WIN64
printf("Windows 64-bit");
#elif __APPLE__ || __MACH__
printf("Mac OSX");
#elif __linux__
printf("Linux");
#elif __FreeBSD__
printf("FreeBSD");
#elif __unix || __unix__
printf("Unix");
#else
printf("Other");
#endif

printf("\n");
}
=}

reaction(startup) -> nextIteration {=
// Initialize an array of interval_t
self->measuredTimes = (interval_t *) calloc(self->num_iterations, sizeof(interval_t));
lf_schedule(nextIteration, 0);
=}

reaction(nextIteration) -> start, done {=
if (self->count < self->num_iterations) {
self->startTime = lf_time_physical();
lf_set(start, true);
} else {
lf_schedule(done, 0);
}
=}

reaction(finish) -> nextIteration {=
interval_t end_time = lf_time_physical();
interval_t duration = end_time - self->startTime;
self->measuredTimes[self->count] = duration;
self->count += 1;

printf("Iteration %d - %.3f ms\n", self->count, toMS(duration));

lf_schedule(nextIteration, 0);
=}

reaction(done) {=
double* measuredMSTimes = getMSMeasurements(self->measuredTimes, self->num_iterations);
qsort(measuredMSTimes, self->num_iterations, sizeof(double), comp);

printf("Execution - Summary:\n");
printf("Best Time:\t %.3f msec\n", measuredMSTimes[0]);
printf("Worst Time:\t %.3f msec\n", measuredMSTimes[self->num_iterations - 1]);
printf("Median Time:\t %.3f msec\n", median(measuredMSTimes, self->num_iterations));
lf_request_stop();
=}
}
Loading

0 comments on commit f2e327c

Please sign in to comment.