Skip to content

Commit

Permalink
Alternative proposal, anonymous namespaces in two separate header fil…
Browse files Browse the repository at this point in the history
…es (#66)
  • Loading branch information
bencopl committed Oct 20, 2022
1 parent 3b96742 commit a340182
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/c++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ include(CMakePackageConfigHelpers)
add_library(${CMAKE_PROJECT_NAME} SHARED
profiler.h profiler.cpp
hashtable.h hashtable.cpp
clock.h clock.cpp
clockmethods.h
clocktypes.h
)

# Link library to and external libs (also use project warnings and options).
Expand All @@ -23,7 +24,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE

set_project_warnings(${CMAKE_PROJECT_NAME})

set(PUBLIC_HEADER_FILES profiler.h hashtable.h clock.h)
set(PUBLIC_HEADER_FILES profiler.h hashtable.h clockmethods.h clocktypes.h)

# Set the properties of the library including, the version and language. Also
# set which header files are to be public and installed, this can be useful
Expand Down
6 changes: 0 additions & 6 deletions src/c++/clock.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/c++/clock.h

This file was deleted.

13 changes: 13 additions & 0 deletions src/c++/clockmethods.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CLOCK_METHODS_H
#define CLOCK_METHODS_H

namespace {

timepoint_t now()
{
return std::chrono::steady_clock::now();
}

} // end anonymous namespace

#endif
14 changes: 14 additions & 0 deletions src/c++/clocktypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef CLOCK_TYPES_H
#define CLOCK_TYPES_H

#include <chrono>

namespace {

// Time point and duration type definitions
using duration_t = std::chrono::duration<double>;
using timepoint_t = std::chrono::time_point<std::chrono::steady_clock,duration_t>;

}

#endif
10 changes: 5 additions & 5 deletions src/c++/hashtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

HashEntry::HashEntry(std::string_view region_name)
: region_name_(region_name)
, total_walltime_(Clock::duration_t::zero())
, self_walltime_(Clock::duration_t::zero())
, child_walltime_(Clock::duration_t::zero())
, total_walltime_(duration_t::zero())
, self_walltime_(duration_t::zero())
, child_walltime_(duration_t::zero())
, call_count_(0)
{}

Expand Down Expand Up @@ -55,7 +55,7 @@ size_t HashTable::query_insert(std::string_view region_name) noexcept
*
*/

void HashTable::update(size_t hash, Clock::duration_t time_delta)
void HashTable::update(size_t hash, duration_t time_delta)
{
// Assertions
assert (table_.size() > 0);
Expand All @@ -74,7 +74,7 @@ void HashTable::update(size_t hash, Clock::duration_t time_delta)
*
*/

void HashTable::add_child_time(size_t hash, Clock::duration_t time_delta)
void HashTable::add_child_time(size_t hash, duration_t time_delta)
{
// Assertions
assert (table_.size() > 0);
Expand Down
13 changes: 6 additions & 7 deletions src/c++/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
#include <string>
#include <string_view>

#include "clock.h"

#include "clocktypes.h"

/**
* @brief Structure to hold information for a particular routine.
Expand All @@ -47,9 +46,9 @@ struct HashEntry{

// Data members
std::string region_name_;
Clock::duration_t total_walltime_;
Clock::duration_t self_walltime_;
Clock::duration_t child_walltime_;
duration_t total_walltime_;
duration_t self_walltime_;
duration_t child_walltime_;
unsigned long long int call_count_;

};
Expand Down Expand Up @@ -80,12 +79,12 @@ class HashTable{

// Prototypes
size_t query_insert(std::string_view) noexcept;
void update(size_t, Clock::duration_t);
void update(size_t, duration_t);
void write();

// Member functions
std::vector<size_t> list_keys();
void add_child_time(size_t, Clock::duration_t);
void add_child_time(size_t, duration_t);
void compute_self_times();

// Getters
Expand Down
8 changes: 5 additions & 3 deletions src/c++/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cassert>
#include <chrono>

#include "clockmethods.h"

/**
* @brief Constructor
*
Expand All @@ -29,7 +31,7 @@ Profiler::Profiler(){
HashTable new_table(tid);
thread_hashtables_.push_back(new_table);

std::vector<std::pair<size_t,Clock::timepoint_t>> new_list;
std::vector<std::pair<size_t,timepoint_t>> new_list;
thread_traceback_.push_back(new_list);
}

Expand Down Expand Up @@ -60,7 +62,7 @@ size_t Profiler::start(std::string_view region_name)
size_t const hash = thread_hashtables_[tid].query_insert(region_name);

// Add routine to the traceback.
auto start_time = Clock::now();
auto start_time = now();
thread_traceback_[tid].push_back(std::make_pair(hash, start_time));

return hash;
Expand All @@ -75,7 +77,7 @@ void Profiler::stop(size_t const hash)
{

// First job: log the stop time.
auto stop_time = Clock::now();
auto stop_time = now();

// Determine the thread number
auto tid = static_cast<hashtable_iterator_t_>(0);
Expand Down
4 changes: 2 additions & 2 deletions src/c++/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class Profiler
// Data members
int max_threads_;
std::vector<HashTable> thread_hashtables_;
std::vector<std::vector<std::pair<size_t,Clock::timepoint_t>>> thread_traceback_;
std::vector<std::vector<std::pair<size_t,timepoint_t>>> thread_traceback_;

// Type definitions for vector array indexing.
typedef std::vector<HashTable>::size_type hashtable_iterator_t_;
typedef std::vector<std::vector<std::pair<size_t,Clock::timepoint_t>>>::size_type pair_iterator_t_;
typedef std::vector<std::vector<std::pair<size_t,timepoint_t>>>::size_type pair_iterator_t_;

public:

Expand Down

0 comments on commit a340182

Please sign in to comment.