From c25b735f84917dd2c0a0fa0edad270c20c568d89 Mon Sep 17 00:00:00 2001 From: Kevin Huck Date: Fri, 24 Jan 2020 09:14:38 -0800 Subject: [PATCH] Expanding the C++ demo to make it more useful --- src/examples/DemoCpp/demo.cpp | 137 +++++++++++++++++++++++++++------- 1 file changed, 110 insertions(+), 27 deletions(-) diff --git a/src/examples/DemoCpp/demo.cpp b/src/examples/DemoCpp/demo.cpp index aa772f71..94f01b16 100644 --- a/src/examples/DemoCpp/demo.cpp +++ b/src/examples/DemoCpp/demo.cpp @@ -3,39 +3,122 @@ #include #include -using namespace apex; using namespace std; +/* First method for timers: using a scoped timer object. + * This is the simplest method for timing C++ */ + +void scopedTimerExample1(void) { + /* This function is timed with a scoped timer using the function name */ + apex::scoped_timer(__func__); + usleep(2000); + return; +} + +void scopedTimerExample2(void) { + /* This function is timed with a scoped timer using the function address */ + apex::scoped_timer((apex_function_address)&scopedTimerExample2); + usleep(2000); + return; +} + +/* Second method for timers: using a task wrapper. + * The benefit of this case is that the timer can be yielded and resumed */ + +void taskWrapperExample1(void) { + /* This function is timed with a task_wrapper that is explicitly started/stopped */ + std::shared_ptr wrapper = apex::new_task(__func__); + apex::start(wrapper); + usleep(2000); + apex::yield(wrapper); + usleep(2000); + apex::start(wrapper); + usleep(2000); + apex::stop(wrapper); + return; +} + +void taskWrapperExample2(void) { + /* This function is timed with a task_wrapper that is explicitly started/stopped */ + std::shared_ptr wrapper = + apex::new_task((apex_function_address)&taskWrapperExample2); + apex::start(wrapper); + usleep(2000); + apex::yield(wrapper); + usleep(2000); + apex::start(wrapper); + usleep(2000); + apex::stop(wrapper); + return; +} + +/* Third example, using simple profiler objects */ + +void profilerExample1(void) { + /* This function is timed with a profiler object */ + auto * profiler = apex::start(__func__); + usleep(2000); + apex::stop(profiler); +} + +void profilerExample2(void) { + /* This function is timed with a profiler object */ + auto * profiler = apex::start((apex_function_address)&profilerExample2); + usleep(2000); + apex::stop(profiler); +} + void* someThread(void* tmp) { - int* tid = (int*)tmp; - char name[32]; - sprintf(name, "worker-thread#%d", *tid); - register_thread(name); - profiler* p = start((apex_function_address)someThread); - sample_value("/threadqueue{locality#0/total}/length", 2.0); - char counter[64]; - sprintf(counter, "/threadqueue{locality#0/%s}/length", name); - sample_value(counter, 2.0); - stop(p); - exit_thread(); - return NULL; + int* tid = (int*)tmp; + char name[32]; + sprintf(name, "worker-thread#%d", *tid); + /* Tell APEX that there is a new thread */ + apex::register_thread(name); + /* Time this thread */ + apex::profiler* p = apex::start((apex_function_address)someThread); + /* Sample a counter */ + apex::sample_value("test_counter_1", 2.0); + char counter[64]; + /* Sample another counter */ + sprintf(counter, "test_counter_%s", name); + apex::sample_value(counter, 2.0); + /* Stop timing the thread */ + apex::stop(p); + apex::exit_thread(); + return NULL; } int main (int argc, char** argv) { - init(argv[0], 0, 1); - cout << "APEX Version : " << version() << endl; - apex_options::print_options(); - profiler* p = start((apex_function_address)(main)); - pthread_t thread[2]; - int tid = 0; - pthread_create(&(thread[0]), NULL, someThread, &tid); - int tid2 = 1; - pthread_create(&(thread[1]), NULL, someThread, &tid2); - pthread_join(thread[0], NULL); - pthread_join(thread[1], NULL); - stop(p); - finalize(); - return 0; + /* Initialize APEX */ + apex::init(argv[0], 0, 1); + /* Get some version and verbose option information */ + cout << "APEX Version : " << apex::version() << endl; + apex::apex_options::print_options(); + apex::apex_options::use_screen_output(true); + /* Start a timer for the main function, using its address (requires binutils) */ + apex::profiler* p = apex::start((apex_function_address)(main)); + /* Launch two threads */ + pthread_t thread[2]; + int tid = 0; + pthread_create(&(thread[0]), NULL, someThread, &tid); + int tid2 = 1; + pthread_create(&(thread[1]), NULL, someThread, &tid2); + /* Join the 2 threads */ + pthread_join(thread[0], NULL); + pthread_join(thread[1], NULL); + + /* test some other timers */ + scopedTimerExample1(); + scopedTimerExample2(); + taskWrapperExample1(); + taskWrapperExample2(); + profilerExample1(); + profilerExample2(); + + /* Stop the main timer and exit */ + apex::stop(p); + apex::finalize(); + return 0; }