From b69ec3f6d953e67422dd32b72688cba850fd1b2e Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Mon, 13 Jan 2014 15:56:57 -0800 Subject: Initial commit --- examples/simple.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 examples/simple.c (limited to 'examples') diff --git a/examples/simple.c b/examples/simple.c new file mode 100644 index 0000000..614205d --- /dev/null +++ b/examples/simple.c @@ -0,0 +1,92 @@ +/* simple.c -- basic ppamltracer example + * This file is in the public domain. + * + * Compile this with + * cc -std=c99 -lppamltracer -o simple simple.c + */ + +#include +#include + +#include + +// ppamltracer state +ppaml_tracer_t tracer; +ppaml_phase_t phase_fact; +ppaml_phase_t phase_fib; + + +// Prototypes +unsigned long long fact(const unsigned short); +unsigned long fib(const unsigned short); + + +int main(int argc, char *argv[]) +{ + /* Disable buffering on stdout so we can see the numbers as they are + * printed. */ + setvbuf(stdout, NULL, _IONBF, 0); + + // Start ppamltracer. + ppaml_tracer_init(&tracer, "/tmp/simple_report"); + + // Register the phases. + ppaml_phase_init(&tracer, &phase_fact, "fact"); + ppaml_phase_init(&tracer, &phase_fib, "fib"); + + // Print factorials. + printf("Factorials: "); + for (unsigned short i = 0; i < 21; i++) { + printf("%lld ", fact(i)); + } + printf("\n"); + + // Print Fibonacci numbers. + printf("Fibonacci numbers: "); + for (unsigned short i = 0; i < 28; i++) { + printf("%ld ", fib(i)); + } + printf("\n"); + + // Finalize ppamltracer and exit. + ppaml_phase_done(&phase_fact); + ppaml_phase_done(&phase_fib); + ppaml_tracer_done(&tracer); + return 0; +} + +unsigned long long fact(const unsigned short n) +{ + // Record that we have entered the 'fact' phase. + ppaml_phase_start(&phase_fact); + + // Compute the factorial. + unsigned long long result; + if (n == 0) { + result = 1; + } else { + result = (unsigned long long)n * fact(n - 1); + } + + // Record that we're exiting the 'fact' phase. + ppaml_phase_stop(&phase_fact); + return result; +} + +unsigned long fib(const unsigned short n) +{ + // Record that we have entered the 'fib' phase. + ppaml_phase_start(&phase_fib); + + // Compute the nth Fibonancci number. + unsigned long result; + if (n == 0 || n == 1) { + result = 1; + } else { + result = fib(n - 1) + fib(n - 2); + } + + // Record that we're exiting the 'fact' phase. + ppaml_phase_stop(&phase_fib); + return result; +} -- cgit v1.2.3