Skip to content

Commit

Permalink
Replace clock_gettime with std::chrono::steady_clock (#3049)
Browse files Browse the repository at this point in the history
* Replace clock_gettime in HighResTimer with std::chrono::steady_clock in high_res_timer.hpp
* Move high_res_timer.hpp from src/utilities to include/cugraph/utilities
* Replace HighResClock (tests/utilities/high_res_clock.h) with HighResTimer

Authors:
  - Seunghwa Kang (https://github.com/seunghwak)

Approvers:
  - Chuck Hastings (https://github.com/ChuckHastings)
  - Naim (https://github.com/naimnv)

URL: #3049
  • Loading branch information
seunghwak authored Dec 15, 2022
1 parent b5becc5 commit c151bfb
Show file tree
Hide file tree
Showing 82 changed files with 719 additions and 894 deletions.
85 changes: 85 additions & 0 deletions cpp/include/cugraph/utilities/high_res_timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <cugraph/utilities/error.hpp>

#include <chrono>
#include <iostream>
#include <map>
#include <ostream>
#include <stack>
#include <string>
#include <tuple>

class HighResTimer {
public:
void start(std::string label)
{
start_stack.push(std::make_tuple(label, std::chrono::steady_clock::now()));
}

double stop()
{
auto stop_time = std::chrono::steady_clock::now();
auto [label, start_time] = start_stack.top();
start_stack.pop();

auto it = labeled_timers.find(label);
if (it == labeled_timers.end()) {
labeled_timers[label] = std::make_tuple(size_t{0}, double{0});
it = labeled_timers.find(label);
}

std::chrono::duration<double> diff = stop_time - start_time;

auto& timer = it->second;
std::get<0>(timer) += 1;
std::get<1>(timer) += diff.count();

return diff.count();
}

void display(std::ostream& os)
{
for (auto it = labeled_timers.begin(); it != labeled_timers.end(); ++it) {
auto [count, duration] = it->second;
os << it->first << " called " << count
<< " times, average time: " << duration / static_cast<double>(count) << " s." << std::endl;
}
}

void display(std::ostream& os, std::string label)
{
auto it = labeled_timers.find(label);
CUGRAPH_EXPECTS(it != labeled_timers.end(), "Invalid input argument: invalid label.");
auto [count, duration] = it->second;
os << it->first << " called " << count
<< " times, average time: " << duration / static_cast<double>(count) << " s." << std::endl;
}

void display_and_clear(std::ostream& os)
{
display(os);
labeled_timers.clear();
}

private:
std::map<std::string, std::tuple<size_t, double> /* # calls, aggregate duration in seconds */>
labeled_timers{};
std::stack<std::tuple<std::string, std::chrono::time_point<std::chrono::steady_clock>>>
start_stack{};
};
23 changes: 11 additions & 12 deletions cpp/src/community/detail/common_methods.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,38 @@
*/
#pragma once

#include <utilities/high_res_timer.hpp>
//#define TIMING

#include <cugraph/dendrogram.hpp>
#include <cugraph/edge_property.hpp>
#include <cugraph/edge_src_dst_property.hpp>
#include <cugraph/graph.hpp>
#include <cugraph/graph_view.hpp>
#ifdef TIMING
#include <cugraph/utilities/high_res_timer.hpp>
#endif

#include <raft/handle.hpp>
#include <rmm/device_uvector.hpp>

namespace cugraph {
namespace detail {

#ifdef TIMING
// Some timing functions
// Need to #define TIMING to have these functions actually time, otherwise
// this is a noop
template <bool multi_gpu>
void timer_start(raft::handle_t const& handle, HighResTimer& hr_timer, std::string const& region)
{
#ifdef TIMING
if constexpr (multi_gpu) {
if (handle.get_comms().get_rank() == 0) hr_timer.start(region);
} else {
hr_timer.start(region);
}
#endif
}

template <bool multi_gpu>
void timer_stop(raft::handle_t const& handle, HighResTimer& hr_timer)
{
#ifdef TIMING
if constexpr (multi_gpu) {
if (handle.get_comms().get_rank() == 0) {
handle.get_stream().synchronize();
Expand All @@ -57,20 +56,20 @@ void timer_stop(raft::handle_t const& handle, HighResTimer& hr_timer)
handle.get_stream().synchronize();
hr_timer.stop();
}
#endif
}

template <bool multi_gpu>
void timer_display(raft::handle_t const& handle, HighResTimer const& hr_timer, std::ostream& os)
void timer_display_and_clear(raft::handle_t const& handle,
HighResTimer const& hr_timer,
std::ostream& os)
{
#ifdef TIMING
if (multi_gpu) {
if (handle.get_comms().get_rank() == 0) hr_timer.display(os);
if (handle.get_comms().get_rank() == 0) hr_timer.display_and_clear(os);
} else {
hr_timer.display(os);
hr_timer.display_and_clear(os);
}
#endif
}
#endif

template <typename vertex_t, typename edge_t, typename weight_t, bool multi_gpu>
weight_t compute_modularity(
Expand Down
9 changes: 6 additions & 3 deletions cpp/src/community/egonet_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
#pragma once

//#define TIMING

#include <utilities/graph_utils.cuh>

#include <cugraph/algorithms.hpp>
Expand All @@ -23,6 +25,9 @@
#include <cugraph/graph_view.hpp>
#include <cugraph/utilities/error.hpp>
#include <cugraph/utilities/host_scalar_comm.hpp>
#ifdef TIMING
#include <cugraph/utilities/high_res_timer.hpp>
#endif

#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_vector.hpp>
Expand All @@ -36,8 +41,6 @@
#include <thrust/remove.h>
#include <thrust/transform.h>

#include <utilities/high_res_timer.hpp>

#include <cstddef>
#include <memory>
#include <numeric>
Expand Down Expand Up @@ -196,7 +199,7 @@ extract(raft::handle_t const& handle,

#ifdef TIMING
hr_timer.stop();
hr_timer.display(std::cout);
hr_timer.display_and_clear(std::cout);
#endif

// extract
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/community/legacy/leiden.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Leiden : public Louvain<graph_type> {
num_level++;
}

this->timer_display(std::cout);
this->timer_display_and_clear(std::cout);

return best_modularity;
}
Expand Down
17 changes: 8 additions & 9 deletions cpp/src/community/legacy/louvain.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@
*/
#pragma once

//#define TIMING

#include <cugraph/legacy/graph.hpp>

#include <converters/legacy/COOtoCSR.cuh>
#include <utilities/graph_utils.cuh>

#include <cugraph/dendrogram.hpp>
#ifdef TIMING
#include <cugraph/utilities/high_res_timer.hpp>
#endif

#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>

//#define TIMING

#ifdef TIMING
#include <utilities/high_res_timer.hpp>
#endif

#include <thrust/copy.h>
#include <thrust/distance.h>
#include <thrust/execution_policy.h>
Expand Down Expand Up @@ -197,7 +196,7 @@ class Louvain {
shrink_graph(current_graph);
}

timer_display(std::cout);
timer_display_and_clear(std::cout);

return best_modularity;
}
Expand All @@ -218,10 +217,10 @@ class Louvain {
#endif
}

void timer_display(std::ostream& os)
void timer_display_and_clear(std::ostream& os)
{
#ifdef TIMING
hr_timer_.display(os);
hr_timer_.display_and_clear(os);
#endif
}

Expand Down
18 changes: 17 additions & 1 deletion cpp/src/community/louvain_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
std::optional<edge_property_t<graph_view_t, weight_t>> current_edge_weights(handle);
std::optional<edge_property_view_t<edge_t, weight_t const*>> current_edge_weight_view(
edge_weight_view);
HighResTimer hr_timer;

weight_t best_modularity = weight_t{-1};
weight_t total_edge_weight =
Expand Down Expand Up @@ -93,8 +92,11 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
// Compute the vertex and cluster weights, these are different for each
// graph in the hierarchical decomposition
//

#ifdef TIMING
detail::timer_start<graph_view_t::is_multi_gpu>(
handle, hr_timer, "compute_vertex_and_cluster_weights");
#endif

vertex_weights_v =
compute_out_weight_sums(handle, current_graph_view, *current_edge_weight_view);
Expand Down Expand Up @@ -124,12 +126,17 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
vertex_weights_v.shrink_to_fit(handle.get_stream());
}

#ifdef TIMING
detail::timer_stop<graph_view_t::is_multi_gpu>(handle, hr_timer);
#endif

//
// Update the clustering assignment, this is the main loop of Louvain
//

#ifdef TIMING
detail::timer_start<graph_view_t::is_multi_gpu>(handle, hr_timer, "update_clustering");
#endif

next_clusters_v =
rmm::device_uvector<vertex_t>(dendrogram->current_level_size(), handle.get_stream());
Expand Down Expand Up @@ -211,7 +218,9 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
}
}

#ifdef TIMING
detail::timer_stop<graph_view_t::is_multi_gpu>(handle, hr_timer);
#endif

if (cur_Q <= best_modularity) { break; }

Expand All @@ -220,7 +229,10 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
//
// Contract the graph
//

#ifdef TIMING
detail::timer_start<graph_view_t::is_multi_gpu>(handle, hr_timer, "contract graph");
#endif

cluster_keys_v.resize(0, handle.get_stream());
cluster_weights_v.resize(0, handle.get_stream());
Expand All @@ -244,10 +256,14 @@ std::pair<std::unique_ptr<Dendrogram<vertex_t>>, weight_t> louvain(
current_edge_weight_view = std::make_optional<edge_property_view_t<edge_t, weight_t const*>>(
(*current_edge_weights).view());

#ifdef TIMING
detail::timer_stop<graph_view_t::is_multi_gpu>(handle, hr_timer);
#endif
}

#ifdef TIMING
detail::timer_display<graph_view_t::is_multi_gpu>(handle, hr_timer, std::cout);
#endif

return std::make_pair(std::move(dendrogram), best_modularity);
}
Expand Down
13 changes: 6 additions & 7 deletions cpp/src/linear_assignment/legacy/hungarian.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define TIMING

#include <cugraph/legacy/graph.hpp>
#include <cugraph/utilities/error.hpp>
#ifdef TIMING
#include <cugraph/utilities/high_res_timer.hpp>
#endif

#include <raft/solver/linear_assignment.cuh>

Expand All @@ -35,12 +40,6 @@
#include <iostream>
#include <limits>

//#define TIMING

#ifdef TIMING
#include <utilities/high_res_timer.hpp>
#endif

namespace cugraph {
namespace detail {

Expand Down Expand Up @@ -255,7 +254,7 @@ weight_t hungarian_sparse(raft::handle_t const& handle,
#ifdef TIMING
hr_timer.stop();

hr_timer.display(std::cout);
hr_timer.display_and_clear(std::cout);
#endif

return min_cost;
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/sampling/random_walks_impl.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ namespace detail {

inline uint64_t get_current_time_nanoseconds()
{
timespec current_time;
clock_gettime(CLOCK_REALTIME, &current_time);
return current_time.tv_sec * 1000000000 + current_time.tv_nsec;
auto cur = std::chrono::steady_clock::now();
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(cur.time_since_epoch()).count());
}

template <typename vertex_t, typename weight_t>
Expand Down
Loading

0 comments on commit c151bfb

Please sign in to comment.