Autonomic Performance Environment for eXascale (APEX)  2.3.1
apex_api.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2020 Kevin Huck
3  * Copyright (c) 2014-2020 University of Oregon
4  *
5  * Distributed under the Boost Software License, Version 1.0. (See accompanying
6  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  */
8 
9 #pragma once
10 
11 /* required for Doxygen */
13 #ifndef DOXYGEN_SHOULD_SKIP_THIS
14 
15 /* This is annoying and confusing. We have to set a define so that the
16  * HPX config file will be included, which will define APEX_HAVE_HPX
17  * for us. We can't use the same name because then the macro is defined
18  * twice. So, we have a macro to make sure the macro is defined. */
19 #ifdef APEX_HAVE_HPX_CONFIG
20 #include <hpx/config.hpp>
21 #endif
22 
23 #include "apex_types.h"
24 #include "apex_options.hpp"
25 #include "apex_export.h"
26 #include "profiler.hpp"
27 #include "profile.hpp"
28 #include "task_wrapper.hpp"
29 #include <functional>
30 #include <stdio.h>
31 #include <string>
32 #include <set>
33 #include <vector>
34 #include <memory>
35 #include <sstream>
36 #include <utility>
37 #include <stdint.h>
38 
39 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
40 
41 class apex_tuning_request;
42 
50 namespace apex
51 {
52 
59 static std::shared_ptr<task_wrapper> null_task_wrapper(nullptr);
60 
61 // These are all static functions for the class. There should be only
62 // one APEX object in the process space.
63 
80 APEX_EXPORT uint64_t init(const char * thread_name,
81  const uint64_t comm_rank, const uint64_t comm_size);
82 
94 APEX_EXPORT std::string dump(bool reset);
95 
105 APEX_EXPORT void finalize(void);
106 
119 APEX_EXPORT void cleanup(void);
120 
121 /*
122  * Functions for starting, stopping timers
123  */
124 
139 APEX_EXPORT profiler * start(const std::string &timer_name);
140 
155 APEX_EXPORT profiler * start(const apex_function_address function_address);
156 
170 APEX_EXPORT void start(std::shared_ptr<task_wrapper> task_wrapper_ptr);
171 
184 APEX_EXPORT void stop(profiler * the_profiler, bool cleanup=true);
185 
197 APEX_EXPORT void stop(std::shared_ptr<task_wrapper> task_wrapper_ptr);
198 
212 APEX_EXPORT void yield(profiler * the_profiler);
213 
227 APEX_EXPORT void yield(std::shared_ptr<task_wrapper> task_wrapper_ptr);
228 
246 APEX_EXPORT profiler * resume(const std::string &timer_name);
247 
265 APEX_EXPORT profiler * resume(const apex_function_address function_address);
266 
283 APEX_EXPORT void resume(std::shared_ptr<task_wrapper> task_wrapper_ptr);
284 
285 /*
286  * Functions for resetting timer values
287  */
288 
299 APEX_EXPORT void reset(const std::string &timer_name);
300 
310 APEX_EXPORT void reset(apex_function_address function_address);
311 
320 APEX_EXPORT void set_state(apex_thread_state state);
321 
322 /*
323  * Function for sampling a counter value
324  */
325 
337 APEX_EXPORT void sample_value(const std::string &name, double value);
338 
353 APEX_EXPORT std::shared_ptr<task_wrapper> new_task(
354  const std::string &name,
355  const uint64_t task_id = UINTMAX_MAX,
356  const std::shared_ptr<apex::task_wrapper> parent_task = null_task_wrapper);
357 
371 APEX_EXPORT std::shared_ptr<task_wrapper> new_task(
372  const apex_function_address function_address,
373  const uint64_t task_id = UINTMAX_MAX,
374  const std::shared_ptr<apex::task_wrapper> parent_task = null_task_wrapper);
375 
385 APEX_EXPORT std::shared_ptr<task_wrapper> update_task(
386  std::shared_ptr<task_wrapper> wrapper,
387  const std::string &name);
388 
398 APEX_EXPORT std::shared_ptr<task_wrapper> update_task(
399  std::shared_ptr<task_wrapper> wrapper,
400  const apex_function_address function_address);
401 
411 APEX_EXPORT apex_event_type register_custom_event(const std::string &name);
412 
425 APEX_EXPORT void custom_event(apex_event_type event_type, void * custom_data);
426 
427 /*
428  * Utility functions
429  */
430 
436 APEX_EXPORT std::string& version(void);
437 
449 APEX_EXPORT void register_thread(const std::string &name);
450 
460 APEX_EXPORT void exit_thread(void);
461 
462 /*
463  * Policy Engine functions.
464  */
465 
480 APEX_EXPORT apex_policy_handle* register_policy(const apex_event_type when,
481  std::function<int(apex_context const&)> f);
482 
497 APEX_EXPORT std::set<apex_policy_handle*> register_policy(
498  std::set<apex_event_type> when, std::function<int(apex_context const&)> f);
499 
511 APEX_EXPORT apex_policy_handle* register_periodic_policy(
512  unsigned long period, std::function<int(apex_context const&)> f);
513 
526 APEX_EXPORT apex_policy_handle* sample_runtime_counter(
527  unsigned long period, const std::string & counter_name);
528 
529 
540 APEX_EXPORT void deregister_policy(apex_policy_handle * handle);
541 
552 APEX_EXPORT void stop_all_async_threads(void);
553 
564 APEX_EXPORT apex_profile* get_profile(apex_function_address function_address);
565 
576 APEX_EXPORT apex_profile* get_profile(const std::string &timer_name);
577 
578 #ifndef DOXYGEN_SHOULD_SKIP_THIS
579 
590 /* Disabled, after adding task_identifier support */
591 //APEX_EXPORT std::vector<std::string> get_available_profiles();
592 
600 APEX_EXPORT double current_power_high(void);
601 
602 #endif // DOXYGEN_SHOULD_SKIP_THIS
603 
636 APEX_EXPORT int setup_power_cap_throttling(void); // initialize
637 
653 APEX_EXPORT int setup_timer_throttling(apex_function_address the_address,
655  apex_optimization_method_t method, unsigned long update_interval);
656 
678 APEX_EXPORT int setup_throughput_tuning(apex_function_address the_address,
680  apex_event_type event_type, int num_inputs, long ** inputs, long * mins,
681  long * maxs, long * steps);
682 
703  std::function<double(void)> metric, apex_event_type event_type, int
704  num_inputs, long ** inputs, long * mins, long * maxs, long * steps);
705 
718  apex_tuning_request & request);
719 
737 APEX_EXPORT int setup_timer_throttling(const std::string &the_name,
739  unsigned long update_interval);
740 
741 #ifndef DOXYGEN_SHOULD_SKIP_THIS
742 APEX_EXPORT int startup_throttling(void);
743 #endif
744 
752 APEX_EXPORT int shutdown_throttling(void); // terminate
753 
762 APEX_EXPORT int get_thread_cap(void); // for thread throttling
763 #ifndef DOXYGEN_SHOULD_SKIP_THIS // test API stuff
764 APEX_EXPORT int get_input2(void); // for thread throttling
765 #endif
766 
775 APEX_EXPORT void set_thread_cap(int new_cap); // for thread throttling
776 
777 
785 APEX_EXPORT std::vector<std::pair<std::string,long*>> &
787 
796 APEX_EXPORT bool has_session_converged(apex_tuning_session_handle handle);
797 
804 APEX_EXPORT void get_best_values(apex_tuning_session_handle h);
805 
810 APEX_EXPORT void print_options(void);
811 
825 APEX_EXPORT void send (uint64_t tag, uint64_t size, uint64_t target);
826 
841 APEX_EXPORT void recv (uint64_t tag, uint64_t size,
842  uint64_t source_rank, uint64_t source_thread);
843 
854  private:
855  std::shared_ptr<apex::task_wrapper> twp;
856  bool timing;
857  public:
863  scoped_timer(uint64_t func) : twp(nullptr), timing(true) {
865  apex::start(twp);
866  }
872  scoped_timer(std::string func) : twp(nullptr), timing(true) {
873  twp = apex::new_task(func);
874  apex::start(twp);
875  }
882  scoped_timer(uint64_t func, std::shared_ptr<apex::task_wrapper> parent)
883  : twp(nullptr), timing(true) {
884  twp = apex::new_task((apex_function_address)func, UINTMAX_MAX, parent);
885  apex::start(twp);
886  }
893  scoped_timer(std::string func,
894  std::shared_ptr<apex::task_wrapper> parent)
895  : twp(nullptr), timing(true) {
896  twp = apex::new_task(func, UINTMAX_MAX, parent);
897  apex::start(twp);
898  }
903  void start(void) {
904  if (!timing) {
905  apex::start(twp);
906  timing = true;
907  }
908  }
913  void stop(void) {
914  if (timing) {
915  apex::stop(twp);
916  timing = false;
917  }
918  }
923  void yield(void) {
924  if (timing) {
925  apex::yield(twp);
926  timing = false;
927  }
928  }
933  void resume(void) {
934  if (!timing) {
935  apex::resume(twp);
936  timing = true;
937  }
938  }
943  stop();
944  }
945 
949  std::shared_ptr<apex::task_wrapper> get_task_wrapper(void) {
950  return twp;
951  }
952 };
953 
963 public:
968  scoped_thread(const std::string& thread_name) {
969  apex::register_thread(thread_name);
970  }
976  }
977 };
978 
979 } //namespace apex
980 
988 #define APEX_SCOPED_TIMER \
989  std::ostringstream _s_foo; \
990  _s_foo << __func__ << " [" << __FILE__ << ":" << __LINE__ << "]"; \
991  apex::scoped_timer __foo(_s_foo.str());
992 
apex::new_task
std::shared_ptr< task_wrapper > new_task(const std::string &name, const uint64_t task_id=UINTMAX_MAX, const std::shared_ptr< apex::task_wrapper > parent_task=null_task_wrapper)
Create a new task (dependency).
apex::yield
void yield(profiler *the_profiler)
Stop a timer, but don't increment the number of calls.
apex::stop
void stop(profiler *the_profiler, bool cleanup=true)
Stop a timer.
apex::register_thread
void register_thread(const std::string &name)
Register a new thread.
apex::reset
void reset(const std::string &timer_name)
Reset a timer or counter.
apex_event_type
Typedef for enumerating the different event types.
apex::cleanup
void cleanup(void)
Cleanup APEX.
apex::get_best_values
void get_best_values(apex_tuning_session_handle h)
Set a tuning session's values to the best known values.
apex::scoped_thread::scoped_thread
scoped_thread(const std::string &thread_name)
Constructor.
Definition: apex_api.hpp:968
apex::deregister_policy
void deregister_policy(apex_policy_handle *handle)
Deregister a policy with APEX.
apex::register_custom_event
apex_event_type register_custom_event(const std::string &name)
Register an event type with APEX.
apex::setup_timer_throttling
int setup_timer_throttling(apex_function_address the_address, apex_optimization_criteria_t criteria, apex_optimization_method_t method, unsigned long update_interval)
Setup throttling to optimize for the specified function.
apex::register_policy
apex_policy_handle * register_policy(const apex_event_type when, std::function< int(apex_context const &)> f)
Register a policy with APEX.
apex::scoped_timer::scoped_timer
scoped_timer(uint64_t func, std::shared_ptr< apex::task_wrapper > parent)
Register a new thread with APEX, then construct and start an APEX timer.
Definition: apex_api.hpp:882
apex::register_periodic_policy
apex_policy_handle * register_periodic_policy(unsigned long period, std::function< int(apex_context const &)> f)
Register a policy with APEX.
apex::scoped_timer::scoped_timer
scoped_timer(uint64_t func)
Construct and start an APEX timer.
Definition: apex_api.hpp:863
apex::scoped_timer::get_task_wrapper
std::shared_ptr< apex::task_wrapper > get_task_wrapper(void)
Get the internal task wrapper object.
Definition: apex_api.hpp:949
apex::stop_all_async_threads
void stop_all_async_threads(void)
Stop all asynchronous APEX background threads.
apex::scoped_timer
A convenience class for using APEX in C++ applications.
Definition: apex_api.hpp:853
apex::scoped_timer::scoped_timer
scoped_timer(std::string func, std::shared_ptr< apex::task_wrapper > parent)
Register a new thread with APEX, then construct and start an APEX timer.
Definition: apex_api.hpp:893
apex::init
uint64_t init(const char *thread_name, const uint64_t comm_rank, const uint64_t comm_size)
Intialize APEX.
apex::has_session_converged
bool has_session_converged(apex_tuning_session_handle handle)
Check whether a tuning session has converged.
apex::get_thread_cap
int get_thread_cap(void)
Get the current thread cap set by the throttling.
apex
The main APEX namespace.
Definition: apex_api.hpp:50
apex_tuning_session_handle
uint32_t apex_tuning_session_handle
Definition: apex_types.h:212
apex::start
profiler * start(const std::string &timer_name)
Start a timer.
apex::custom_event
void custom_event(apex_event_type event_type, void *custom_data)
Trigger a custom event.
apex::scoped_timer::stop
void stop(void)
Stop the APEX timer.
Definition: apex_api.hpp:913
apex::scoped_timer::start
void start(void)
Start the APEX timer.
Definition: apex_api.hpp:903
apex::exit_thread
void exit_thread(void)
Exit a thread.
apex::send
void send(uint64_t tag, uint64_t size, uint64_t target)
Notify APEX that the current thread is sending a parcel/message to another rank/locality/process.
apex::setup_throughput_tuning
int setup_throughput_tuning(apex_function_address the_address, apex_optimization_criteria_t criteria, apex_event_type event_type, int num_inputs, long **inputs, long *mins, long *maxs, long *steps)
Setup throttling to optimize for the specified function, using multiple input criteria.
apex::get_profile
apex_profile * get_profile(apex_function_address function_address)
Get the current profile for the specified function address.
apex::set_thread_cap
void set_thread_cap(int new_cap)
Set the current thread cap for throttling.
apex::scoped_timer::~scoped_timer
~scoped_timer()
Destructor.
Definition: apex_api.hpp:942
apex::null_task_wrapper
static std::shared_ptr< task_wrapper > null_task_wrapper(nullptr)
declare a default "null" pointer for std::shared_ptr<task_wrapper>& references
apex::scoped_timer::yield
void yield(void)
Yield the APEX timer.
Definition: apex_api.hpp:923
apex::scoped_timer::scoped_timer
scoped_timer(std::string func)
Construct and start an APEX timer.
Definition: apex_api.hpp:872
apex::resume
profiler * resume(const std::string &timer_name)
Resume a timer.
apex::version
std::string & version(void)
Return the APEX version.
apex::scoped_thread
A convenience class for creating a scoped thread.
Definition: apex_api.hpp:962
apex_types.h
apex::get_tunable_params
std::vector< std::pair< std::string, long * > > & get_tunable_params(apex_tuning_session_handle h)
Return a vector of the current tunable parameters.
apex_function_address
uintptr_t apex_function_address
Definition: apex_types.h:36
apex_optimization_method_t
apex_optimization_method_t
Definition: apex_types.h:122
apex::recv
void recv(uint64_t tag, uint64_t size, uint64_t source_rank, uint64_t source_thread)
Notify APEX that the current thread is receiving a parcel/message from another rank/locality/process.
apex::finalize
void finalize(void)
Finalize APEX.
apex::setup_power_cap_throttling
int setup_power_cap_throttling(void)
Initialize the power cap throttling policy.
apex::set_state
void set_state(apex_thread_state state)
Set the thread state.
apex::update_task
std::shared_ptr< task_wrapper > update_task(std::shared_ptr< task_wrapper > wrapper, const std::string &name)
Update a task (dependency).
apex::shutdown_throttling
int shutdown_throttling(void)
Terminate the throttling policy.
apex::sample_value
void sample_value(const std::string &name, double value)
Sample a state value.
apex_optimization_criteria_t
apex_optimization_criteria_t
Definition: apex_types.h:110
apex::scoped_timer::resume
void resume(void)
Resume the APEX timer.
Definition: apex_api.hpp:933
apex::dump
std::string dump(bool reset)
Dump output from APEX.
apex::print_options
void print_options(void)
Print out all configuration settings for APEX.
apex::sample_runtime_counter
apex_policy_handle * sample_runtime_counter(unsigned long period, const std::string &counter_name)
Periodically sample a runtime counter.
apex::setup_custom_tuning
apex_tuning_session_handle setup_custom_tuning(std::function< double(void)> metric, apex_event_type event_type, int num_inputs, long **inputs, long *mins, long *maxs, long *steps)
Setup tuning of specified parameters to optimize for a custom metric, using multiple input criteria.
apex::scoped_thread::~scoped_thread
~scoped_thread()
Destructor.
Definition: apex_api.hpp:974