-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
212 lines (182 loc) · 5.6 KB
/
graph.h
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string.h>
#include <CL/sycl.hpp>
#include "utils.h"
using namespace sycl;
#define ATOMIC_INT atomic_ref<int, memory_order::relaxed, memory_scope::device, access::address_space::global_space>
#define ATOMIC_FLOAT atomic_ref<float, memory_order::relaxed, memory_scope::device, access::address_space::global_space>
#define get_node(j) g->V[j]
#define get_neighbour(j) g->E[j]
#define get_parent(j) g->RE[j]
#define get_weight(j) g->W[j]
#define get_num_neighbours(j) g->I[j + 1] - g->I[j]
#define begin_neighbours(u) g->I[u]
#define end_neighbours(u) g->I[u + 1]
#define begin_parents(u) g->RI[u]
#define end_parents(u) g->RI[u + 1]
#define for_neighbours(u, j) for (j = begin_neighbours(u); j < end_neighbours(u); j++)
#define for_parents(u, j) for (j = begin_parents(u); j < end_parents(u); j++)
#define forall(N, NUM_THREADS) Q.submit([&](handler &h){ h.parallel_for(NUM_THREADS, [=](id<1> u){for (; u < N; u += NUM_THREADS)
#define end \
}); \
}).wait();
class graph
{
private:
int32_t num_nodes;
int32_t num_edges;
std::string gname;
public:
int *V, *I, *E, *W, *RE, *RI;
int get_num_nodes()
{
return num_nodes;
}
int get_num_edges()
{
return num_edges;
}
std::string get_graph_name()
{
return gname;
}
void set_num_nodes(int n)
{
num_nodes = n;
}
void set_num_edges(int n)
{
num_edges = n;
}
void set_graph_name(std::string name)
{
gname = name;
}
void load_graph(std::string name, queue Q)
{
std::vector<int> h_V, h_I, h_E, h_W, h_RE, h_RI;
load_from_file("csr_graphs/" + name + "/V", h_V);
load_from_file("csr_graphs/" + name + "/I", h_I);
load_from_file("csr_graphs/" + name + "/E", h_E);
load_from_file("csr_graphs/" + name + "/W", h_W);
load_from_file("csr_graphs/" + name + "/RE", h_RE);
load_from_file("csr_graphs/" + name + "/RI", h_RI);
int num_nodes = h_V.size();
int num_edges = h_E.size();
set_num_edges(num_edges);
set_num_nodes(num_nodes);
set_graph_name(name);
V = malloc_device<int>(num_nodes, Q);
I = malloc_device<int>((num_nodes + 1), Q);
E = malloc_device<int>(num_edges, Q);
W = malloc_device<int>(num_edges, Q);
RE = malloc_device<int>(num_edges, Q);
RI = malloc_device<int>((num_nodes + 1), Q);
Q.submit([&](handler &h)
{ h.memcpy(V, &h_V[0], h_V.size() * sizeof(int)); });
Q.submit([&](handler &h)
{ h.memcpy(I, &h_I[0], h_I.size() * sizeof(int)); });
Q.submit([&](handler &h)
{ h.memcpy(E, &h_E[0], h_E.size() * sizeof(int)); });
Q.submit([&](handler &h)
{ h.memcpy(W, &h_W[0], h_W.size() * sizeof(int)); });
Q.submit([&](handler &h)
{ h.memcpy(RE, &h_RE[0], h_RE.size() * sizeof(int)); });
Q.submit([&](handler &h)
{ h.memcpy(RI, &h_RI[0], h_RI.size() * sizeof(int)); });
Q.wait();
}
void free_memory(queue Q)
{
free(V, Q);
free(I, Q);
free(E, Q);
free(W, Q);
free(RE, Q);
free(RI, Q);
}
};
// initialize device arr with val, if needed set arr[pos] = pos_val
template <typename T>
void initialize(T *arr, T val, int NUM_THREADS, int N, queue Q, int pos = -1, T pos_val = -1)
{
int stride = NUM_THREADS;
Q.submit([&](handler &h)
{ h.parallel_for(NUM_THREADS, [=](id<1> i)
{
for (; i < N; i += stride)
{
arr[i] = val;
if (i == pos)
{
arr[pos] = pos_val;
}
} }); });
Q.wait();
}
// memcpy from src to dest
template <typename T>
void memcpy(T *dest, T *src, int N, queue Q)
{
Q.submit([&](handler &h)
{ h.memcpy(dest, src, N * sizeof(T)); })
.wait();
}
// copy contents of src to dest, both src and dest are of length N
template <typename T>
void copy(T *dest, T *src, int NUM_THREADS, int N, queue Q)
{
int stride = NUM_THREADS;
Q.submit([&](handler &h)
{ h.parallel_for(
NUM_THREADS, [=](id<1> i)
{
for (; i < N ;i += stride)
{
dest[i] = src[i];
} }); })
.wait();
}
// returns True if u and v are neighbours
int neighbours(int v, int w, graph *g)
{
int connected = 0;
int start_edge = begin_neighbours(v);
int end_edge = end_neighbours(v) - 1;
if (get_neighbour(start_edge) == w)
{
connected = 1;
}
else if (get_neighbour(end_edge) == w)
{
connected = 1;
}
else
{
int mid = start_edge + (end_edge - start_edge) / 2;
while (start_edge <= end_edge)
{
if (get_neighbour(mid) == w)
{
connected = 1;
break;
}
if (w < get_neighbour(mid))
{
end_edge = mid - 1;
}
else
{
start_edge = mid + 1;
}
mid = start_edge + (end_edge - start_edge) / 2;
}
}
return connected;
}