Skip to content

Commit f5b183e

Browse files
committed
revision hasher
1 parent d321be1 commit f5b183e

4 files changed

Lines changed: 84 additions & 33 deletions

File tree

include/osp/dag_divider/isomorphism_divider/EftSubgraphScheduler.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ class EftSubgraphScheduler {
105105
JobStatus status_ = JobStatus::WAITING;
106106
VWorkwT<GraphT> upwardRank_ = 0.0;
107107

108-
// --- Execution Tracking Members ---
109108
std::vector<unsigned> assignedWorkers_;
110109
double startTime_ = -1.0;
111110
double finishTime_ = -1.0;
@@ -321,8 +320,6 @@ class EftSubgraphScheduler {
321320
void ProcessCompletedJobs(std::vector<JobIdT> &runningJobs, std::vector<unsigned> &availableWorkers, unsigned &completedCount,
322321
double currentTime, const GraphT &graph) {
323322
const size_t numWorkerTypes = availableWorkers.size();
324-
325-
// Optimize removal loop
326323
for (size_t i = 0; i < runningJobs.size();) {
327324
Job &job = jobs_.at(runningJobs[i]);
328325
if (job.finishTime_ <= currentTime) {
@@ -344,7 +341,6 @@ class EftSubgraphScheduler {
344341
}
345342
}
346343

347-
// Fast removal: swap with last and pop
348344
runningJobs[i] = runningJobs.back();
349345
runningJobs.pop_back();
350346
} else {

include/osp/dag_divider/isomorphism_divider/HashComputer.hpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,50 @@ namespace osp {
3030
* This class provides an interface for obtaining hash values for individual vertices,
3131
* the full list of vertex hashes, the number of unique orbits, and the vertices belonging to specific orbits.
3232
*
33-
* @tparam index_type The type used for indexing vertices in the graph.
33+
* @tparam IndexType The type used for indexing vertices in the graph.
3434
*/
3535
template <typename IndexType>
3636
class HashComputer {
3737
public:
3838
virtual ~HashComputer() = default;
3939

40+
/**
41+
* @brief Gets the hash value of a specific vertex.
42+
* @param v The vertex index.
43+
* @return The computed hash value of the vertex.
44+
*/
4045
virtual std::size_t GetVertexHash(const IndexType &v) const = 0;
46+
47+
/**
48+
* @brief Gets the reference to the vector of all vertex hashes.
49+
* @return A const reference to the vector containing hashes for all vertices.
50+
*/
4151
virtual const std::vector<std::size_t> &GetVertexHashes() const = 0;
52+
53+
/**
54+
* @brief Gets the number of unique orbits (equivalence classes) found.
55+
* @return The number of orbits.
56+
*/
4257
virtual std::size_t NumOrbits() const = 0;
4358

59+
/**
60+
* @brief Gets the orbit (list of equivalent vertices) that a specific vertex belongs to.
61+
* @param v The vertex index.
62+
* @return A const reference to the vector of indices in the same orbit.
63+
*/
4464
virtual const std::vector<IndexType> &GetOrbit(const IndexType &v) const = 0;
65+
66+
/**
67+
* @brief Gets the map of all orbits.
68+
* @return A const reference to the map where keys are hash values and values are vectors of vertex indices.
69+
*/
4570
virtual const std::unordered_map<std::size_t, std::vector<IndexType>> &GetOrbits() const = 0;
4671

72+
/**
73+
* @brief Gets the orbit corresponding to a specific hash value.
74+
* @param hash The hash value of the orbit.
75+
* @return A const reference to the vector of vertex indices in the orbit.
76+
*/
4777
virtual const std::vector<IndexType> &GetOrbitFromHash(const std::size_t &hash) const = 0;
4878
};
4979

include/osp/dag_divider/isomorphism_divider/MerkleHashComputer.hpp

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright 2024 Huawei Technologies Co., Ltd.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
6+
you may obtain a copy of the License at
77
88
http://www.apache.org/licenses/LICENSE-2.0
99
@@ -18,9 +18,12 @@ limitations under the License.
1818

1919
#pragma once
2020

21+
#include <algorithm>
2122
#include <set>
2223
#include <stdexcept>
24+
#include <type_traits>
2325
#include <unordered_map>
26+
#include <utility>
2427
#include <vector>
2528

2629
#include "osp/auxiliary/hash_util.hpp"
@@ -32,6 +35,7 @@ limitations under the License.
3235
namespace osp {
3336

3437
/**
38+
* @class MerkleHashComputer
3539
* @brief Computes Merkle hashes for graph vertices to identify isomorphic orbits.
3640
*
3741
* The Merkle hash of a vertex is computed recursively based on its own properties
@@ -57,7 +61,7 @@ class MerkleHashComputer : public HashComputer<VertexIdxT<GraphT>> {
5761

5862
NodeHashFuncT nodeHashFunc_;
5963

60-
inline void ComputeHashesHelper(const VertexType &v, std::vector<std::size_t> &parentChildHashes) {
64+
void ComputeHashesHelper(const VertexType &v, std::vector<std::size_t> &parentChildHashes) {
6165
std::sort(parentChildHashes.begin(), parentChildHashes.end());
6266

6367
std::size_t hash = nodeHashFunc_(v);
@@ -76,64 +80,84 @@ class MerkleHashComputer : public HashComputer<VertexIdxT<GraphT>> {
7680

7781
template <typename RetT = void>
7882
std::enable_if_t<forward, RetT> ComputeHashes(const GraphT &graph) {
79-
vertexHashes_.resize(graph.NumVertices());
83+
const size_t numVertices = graph.NumVertices();
84+
vertexHashes_.resize(numVertices);
85+
std::vector<std::size_t> neighborHashes;
8086

8187
for (const VertexType &v : TopSortView(graph)) {
82-
std::vector<std::size_t> parentHashes;
88+
neighborHashes.clear();
8389
for (const VertexType &parent : graph.Parents(v)) {
84-
parentHashes.push_back(vertexHashes_[parent]);
90+
neighborHashes.push_back(vertexHashes_[parent]);
8591
}
86-
ComputeHashesHelper(v, parentHashes);
92+
ComputeHashesHelper(v, neighborHashes);
8793
}
8894
}
8995

9096
template <typename RetT = void>
9197
std::enable_if_t<not forward, RetT> ComputeHashes(const GraphT &graph) {
92-
vertexHashes_.resize(graph.NumVertices());
98+
const size_t numVertices = graph.NumVertices();
99+
vertexHashes_.resize(numVertices);
100+
std::vector<std::size_t> neighborHashes;
93101

94102
const auto topSort = GetTopOrderReverse(graph);
95103
for (auto it = topSort.cbegin(); it != topSort.cend(); ++it) {
96104
const VertexType &v = *it;
97-
std::vector<std::size_t> childHashes;
105+
neighborHashes.clear();
98106
for (const VertexType &child : graph.Children(v)) {
99-
childHashes.push_back(vertexHashes_[child]);
107+
neighborHashes.push_back(vertexHashes_[child]);
100108
}
101-
ComputeHashesHelper(v, childHashes);
109+
ComputeHashesHelper(v, neighborHashes);
102110
}
103111
}
104112

105113
public:
114+
/**
115+
* @brief Constructs the MerkleHashComputer and immediately computes the hashes.
116+
* @tparam Args Arguments forwarded to the NodeHashFuncT constructor.
117+
* @param graph The graph to process.
118+
* @param args Arguments for the node hash function.
119+
*/
106120
template <typename... Args>
107121
MerkleHashComputer(const GraphT &graph, Args &&...args)
108122
: HashComputer<VertexType>(), nodeHashFunc_(std::forward<Args>(args)...) {
109123
ComputeHashes(graph);
110124
}
111125

112-
virtual ~MerkleHashComputer() override = default;
126+
~MerkleHashComputer() override = default;
113127

114-
inline std::size_t GetVertexHash(const VertexType &v) const override { return vertexHashes_[v]; }
128+
std::size_t GetVertexHash(const VertexType &v) const override { return vertexHashes_[v]; }
115129

116-
inline const std::vector<std::size_t> &GetVertexHashes() const override { return vertexHashes_; }
130+
const std::vector<std::size_t> &GetVertexHashes() const override { return vertexHashes_; }
117131

118-
inline std::size_t NumOrbits() const override { return orbits_.size(); }
132+
std::size_t NumOrbits() const override { return orbits_.size(); }
119133

120-
inline const std::vector<VertexType> &GetOrbit(const VertexType &v) const override {
134+
const std::vector<VertexType> &GetOrbit(const VertexType &v) const override {
121135
return this->GetOrbitFromHash(this->GetVertexHash(v));
122136
}
123137

124-
inline const std::unordered_map<std::size_t, std::vector<VertexType>> &GetOrbits() const override { return orbits_; }
138+
const std::unordered_map<std::size_t, std::vector<VertexType>> &GetOrbits() const override { return orbits_; }
125139

126-
inline const std::vector<VertexType> &GetOrbitFromHash(const std::size_t &hash) const override { return orbits_.at(hash); }
140+
const std::vector<VertexType> &GetOrbitFromHash(const std::size_t &hash) const override { return orbits_.at(hash); }
127141
};
128142

143+
/**
144+
* @brief Checks if two graphs are isomorphic based on Merkle hashes.
145+
* @note This is a necessary but not sufficient condition for graph isomorphism in general cases,
146+
* but sufficient for the kinds of DAGs often encountered in this context.
147+
*
148+
* @tparam GraphT The graph type.
149+
* @tparam NodeHashFuncT The node hash function type.
150+
* @tparam forward Direction of hash computation.
151+
* @param g1 The first graph.
152+
* @param g2 The second graph.
153+
* @return True if they have the same orbit structure, false otherwise.
154+
*/
129155
template <typename GraphT, typename NodeHashFuncT = UniformNodeHashFunc<VertexIdxT<GraphT>>, bool forward = true>
130156
bool AreIsomorphicByMerkleHash(const GraphT &g1, const GraphT &g2) {
131-
// Basic check: Different numbers of vertices or edges mean they can't be isomorphic.
132157
if (g1.NumVertices() != g2.NumVertices() || g1.NumEdges() != g2.NumEdges()) {
133158
return false;
134159
}
135160

136-
// --- Compute Hashes in the Specified Direction ---
137161
MerkleHashComputer<GraphT, NodeHashFuncT, forward> hash1(g1);
138162
MerkleHashComputer<GraphT, NodeHashFuncT, forward> hash2(g2);
139163

include/osp/dag_divider/isomorphism_divider/PrecomputedHashComputer.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright 2024 Huawei Technologies Co., Ltd.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
6+
you may obtain a copy of the License at
77
88
http://www.apache.org/licenses/LICENSE-2.0
99
@@ -26,6 +26,7 @@ limitations under the License.
2626
namespace osp {
2727

2828
/**
29+
* @class PrecomputedHashComputer
2930
* @brief A class to store precomputed hash values for a set of objects and provide an orbit-based interface.
3031
*
3132
* This class takes a vector of hash values for objects indexed from 0 to n-1. It then computes the orbits
@@ -43,7 +44,7 @@ class PrecomputedHashComputer : public HashComputer<IndexType> {
4344
/**
4445
* @brief Construct a new Precomputed Hash Computer object.
4546
*
46-
* @param precomputed_hashes A vector of hash values for objects 0 to n-1.
47+
* @param precomputedHashes A vector of hash values for objects 0 to n-1.
4748
*/
4849
PrecomputedHashComputer(const std::vector<std::size_t> &precomputedHashes) : vertexHashes_(precomputedHashes) {
4950
for (std::size_t i = 0; i < vertexHashes_.size(); ++i) {
@@ -52,21 +53,21 @@ class PrecomputedHashComputer : public HashComputer<IndexType> {
5253
}
5354
}
5455

55-
virtual ~PrecomputedHashComputer() override = default;
56+
~PrecomputedHashComputer() override = default;
5657

57-
inline std::size_t GetVertexHash(const IndexType &v) const override { return vertexHashes_[v]; }
58+
std::size_t GetVertexHash(const IndexType &v) const override { return vertexHashes_[v]; }
5859

59-
inline const std::vector<std::size_t> &GetVertexHashes() const override { return vertexHashes_; }
60+
const std::vector<std::size_t> &GetVertexHashes() const override { return vertexHashes_; }
6061

61-
inline std::size_t NumOrbits() const override { return orbits_.size(); }
62+
std::size_t NumOrbits() const override { return orbits_.size(); }
6263

63-
inline const std::vector<IndexType> &GetOrbit(const IndexType &v) const override {
64+
const std::vector<IndexType> &GetOrbit(const IndexType &v) const override {
6465
return this->GetOrbitFromHash(this->GetVertexHash(v));
6566
}
6667

67-
inline const std::unordered_map<std::size_t, std::vector<IndexType>> &GetOrbits() const override { return orbits_; }
68+
const std::unordered_map<std::size_t, std::vector<IndexType>> &GetOrbits() const override { return orbits_; }
6869

69-
inline const std::vector<IndexType> &GetOrbitFromHash(const std::size_t &hash) const override { return orbits_.at(hash); }
70+
const std::vector<IndexType> &GetOrbitFromHash(const std::size_t &hash) const override { return orbits_.at(hash); }
7071
};
7172

7273
} // namespace osp

0 commit comments

Comments
 (0)