Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
add timer.
Browse files Browse the repository at this point in the history
  • Loading branch information
BullDemonKing committed Dec 9, 2018
1 parent fc5f3e2 commit 3ee7bec
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/operator/contrib/dgl_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ typedef int64_t dgl_id_t;

////////////////////////////// Graph Sampling ///////////////////////////////


class Timer {
public:
Timer() {
reset();
}

// Reset start time
void reset() {
begin = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(begin-begin);
}

// Code start
void tic() {
begin = std::chrono::high_resolution_clock::now();
}

// Code end
float toc() {
duration += std::chrono::duration_cast<std::chrono::microseconds>
(std::chrono::high_resolution_clock::now()-begin);
return get();
}

// Get the time duration
float get() {
return (float)duration.count();
}

protected:
std::chrono::high_resolution_clock::time_point begin;
std::chrono::microseconds duration;
};

/*
* ArrayHeap is used to sample elements from vector
*/
Expand Down Expand Up @@ -555,6 +590,8 @@ static void SampleSubgraph(const NDArray &csr,
size_t num_seeds = seed_arr.shape().Size();
CHECK_GE(max_num_vertices, num_seeds);

Timer timer;
timer.tic();
const dgl_id_t* val_list = csr.data().dptr<dgl_id_t>();
const dgl_id_t* col_list = csr.aux_data(csr::kIdx).dptr<dgl_id_t>();
const dgl_id_t* indptr = csr.aux_data(csr::kIndPtr).dptr<dgl_id_t>();
Expand Down Expand Up @@ -582,11 +619,13 @@ static void SampleSubgraph(const NDArray &csr,
neigh_pos.reserve(num_seeds);
std::vector<dgl_id_t> neighbor_list;
size_t num_edges = 0;
printf("init: %f\n", timer.toc());

// sub_vers is used both as a node collection and a queue.
// In the while loop, we iterate over sub_vers and new nodes are added to the vector.
// A vertex in the vector only needs to be accessed once. If there is a vertex behind idx
// isn't in the last level, we will sample its neighbors. If not, the while loop terminates.
timer.tic();
size_t idx = 0;
while (idx < sub_vers.size() &&
sub_ver_mp.size() < max_num_vertices) {
Expand Down Expand Up @@ -655,9 +694,11 @@ static void SampleSubgraph(const NDArray &csr,
break;
}
}
printf("loop: %f\n", timer.toc());

// Copy sub_ver_mp to output[0]
// Copy layer
timer.tic();
size_t num_vertices = sub_ver_mp.size();
std::sort(sub_vers.begin(), sub_vers.end(),
[](const std::pair<dgl_id_t, dgl_id_t> &a1, const std::pair<dgl_id_t, dgl_id_t> &a2) {
Expand All @@ -674,6 +715,7 @@ static void SampleSubgraph(const NDArray &csr,
// The last element stores the actual
// number of vertices in the subgraph.
out[max_num_vertices] = sub_ver_mp.size();
printf("copy out: %f\n", timer.toc());

// Copy sub_probability
if (sub_prob != nullptr) {
Expand All @@ -686,6 +728,7 @@ static void SampleSubgraph(const NDArray &csr,
}
}
}
timer.tic();
// Construct sub_csr_graph
TShape shape_1(1);
TShape shape_2(1);
Expand Down Expand Up @@ -732,6 +775,7 @@ static void SampleSubgraph(const NDArray &csr,
for (size_t i = num_vertices+1; i <= max_num_vertices; ++i) {
indptr_out[i] = indptr_out[i-1];
}
printf("copy csr: %f\n", timer.toc());
}

/*
Expand Down

0 comments on commit 3ee7bec

Please sign in to comment.