/
bfs.hxx
179 lines (150 loc) · 6.17 KB
/
bfs.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @file bfs.hxx
* @author Muhammad Osama (mosama@ucdavis.edu)
* @brief Breadth-First Search algorithm.
* @date 2020-11-23
*
* @copyright Copyright (c) 2020
*
*/
#pragma once
#include <gunrock/algorithms/algorithms.hxx>
namespace gunrock {
namespace bfs {
template <typename vertex_t>
struct param_t {
vertex_t single_source;
param_t(vertex_t _single_source) : single_source(_single_source) {}
};
template <typename vertex_t>
struct result_t {
vertex_t* distances;
vertex_t* predecessors; /// @todo: implement this.
result_t(vertex_t* _distances, vertex_t* _predecessors)
: distances(_distances), predecessors(_predecessors) {}
};
template <typename graph_t, typename param_type, typename result_type>
struct problem_t : gunrock::problem_t<graph_t> {
param_type param;
result_type result;
problem_t(graph_t& G,
param_type& _param,
result_type& _result,
std::shared_ptr<gcuda::multi_context_t> _context)
: gunrock::problem_t<graph_t>(G, _context),
param(_param),
result(_result) {}
using vertex_t = typename graph_t::vertex_type;
using edge_t = typename graph_t::edge_type;
using weight_t = typename graph_t::weight_type;
thrust::device_vector<vertex_t> visited; /// @todo not used.
void init() override {}
void reset() override {
auto n_vertices = this->get_graph().get_number_of_vertices();
auto d_distances = thrust::device_pointer_cast(this->result.distances);
thrust::fill(thrust::device, d_distances + 0, d_distances + n_vertices,
std::numeric_limits<vertex_t>::max());
thrust::fill(thrust::device, d_distances + this->param.single_source,
d_distances + this->param.single_source + 1, 0);
}
};
template <typename problem_t>
struct enactor_t : gunrock::enactor_t<problem_t> {
enactor_t(problem_t* _problem,
std::shared_ptr<gcuda::multi_context_t> _context)
: gunrock::enactor_t<problem_t>(_problem, _context) {}
using vertex_t = typename problem_t::vertex_t;
using edge_t = typename problem_t::edge_t;
using weight_t = typename problem_t::weight_t;
using frontier_t = typename enactor_t<problem_t>::frontier_t;
void prepare_frontier(frontier_t* f,
gcuda::multi_context_t& context) override {
auto P = this->get_problem();
f->push_back(P->param.single_source);
}
void loop(gcuda::multi_context_t& context) override {
// Data slice
auto E = this->get_enactor();
auto P = this->get_problem();
auto G = P->get_graph();
auto single_source = P->param.single_source;
auto distances = P->result.distances;
auto visited = P->visited.data().get();
auto iteration = this->iteration;
auto search = [distances, single_source, iteration] __host__ __device__(
vertex_t const& source, // ... source
vertex_t const& neighbor, // neighbor
edge_t const& edge, // edge
weight_t const& weight // weight (tuple).
) -> bool {
// If the neighbor is not visited, update the distance. Returning false
// here means that the neighbor is not added to the output frontier, and
// instead an invalid vertex is added in its place. These invalides (-1 in
// most cases) can be removed using a filter operator or uniquify.
// if (distances[neighbor] != std::numeric_limits<vertex_t>::max())
// return false;
// else
// return (math::atomic::cas(
// &distances[neighbor],
// std::numeric_limits<vertex_t>::max(), iteration + 1) ==
// std::numeric_limits<vertex_t>::max());
// Simpler logic for the above.
auto old_distance =
math::atomic::min(&distances[neighbor], iteration + 1);
return (iteration + 1 < old_distance);
};
auto remove_invalids =
[] __host__ __device__(vertex_t const& vertex) -> bool {
// Returning true here means that we keep all the valid vertices.
// Internally, filter will automatically remove invalids and will never
// pass them to this lambda function.
return true;
};
// Execute advance operator on the provided lambda
operators::advance::execute<operators::load_balance_t::block_mapped>(
G, E, search, context);
// Execute filter operator to remove the invalids.
// @todo: Add CLI option to enable or disable this.
// operators::filter::execute<operators::filter_algorithm_t::compact>(
// G, E, remove_invalids, context);
}
}; // struct enactor_t
/**
* @brief Run Breadth-First Search algorithm on a given graph, G, starting from
* the source node, single_source. The resulting distances are stored in the
* distances pointer. All data must be allocated by the user, on the device
* (GPU) and passed in to this function.
*
* @tparam graph_t Graph type.
* @param G Graph object.
* @param single_source A vertex in the graph (integral type).
* @param distances Pointer to the distances array of size number of vertices.
* @param predecessors Pointer to the predecessors array of size number of
* vertices. (optional, wip)
* @param context Device context.
* @return float Time taken to run the algorithm.
*/
template <typename graph_t>
float run(graph_t& G,
typename graph_t::vertex_type& single_source, // Parameter
typename graph_t::vertex_type* distances, // Output
typename graph_t::vertex_type* predecessors, // Output
std::shared_ptr<gcuda::multi_context_t> context =
std::shared_ptr<gcuda::multi_context_t>(
new gcuda::multi_context_t(0)) // Context
) {
using vertex_t = typename graph_t::vertex_type;
using param_type = param_t<vertex_t>;
using result_type = result_t<vertex_t>;
param_type param(single_source);
result_type result(distances, predecessors);
using problem_type = problem_t<graph_t, param_type, result_type>;
using enactor_type = enactor_t<problem_type>;
problem_type problem(G, param, result, context);
problem.init();
problem.reset();
enactor_type enactor(&problem, context);
return enactor.enact();
}
} // namespace bfs
} // namespace gunrock