-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
284 lines (242 loc) · 8.54 KB
/
util.h
File metadata and controls
284 lines (242 loc) · 8.54 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef UTIL_H
#define UTIL_H
#include <unordered_map>
#include <vector>
#include "EdgeInfo.h"
typedef EdgeType::IntType NumberType;
#include <mpi.h>
template <typename T>
MPI_Datatype get_mpi_type();
template <>
inline MPI_Datatype get_mpi_type<int>() { return MPI_INT; }
template <>
inline MPI_Datatype get_mpi_type<long>() { return MPI_LONG; }
template <>
inline MPI_Datatype get_mpi_type<unsigned long>() { return MPI_UNSIGNED_LONG; }
// template <>
// MPI_Datatype get_mpi_type<size_t>()
// {
// static_assert(sizeof(size_t) == sizeof(unsigned long), "Handle size_t mapping carefully!");
// return MPI_UNSIGNED_LONG;
// }
// example usage
// MPI_Datatype mpi_type = get_mpi_type<NumberType>();
//////////////// define new MPI_DATATYPE//////////////////////
struct MaxLoc
{
NumberType value;
NumberType node;
};
inline MPI_Datatype create_MPI_2NUM()
{
MPI_Datatype type;
MaxLoc temp;
int blocklengths[2] = {1, 1};
// Use your existing helper to deduce MPI type
MPI_Datatype mpi_num_type = get_mpi_type<NumberType>();
MPI_Datatype types[2] = {mpi_num_type, mpi_num_type};
MPI_Aint displacement[2];
MPI_Aint base;
MPI_Get_address(&temp, &base);
MPI_Get_address(&temp.value, &displacement[0]);
MPI_Get_address(&temp.node, &displacement[1]);
displacement[0] -= base;
displacement[1] -= base;
MPI_Type_create_struct(2, blocklengths, displacement, types, &type);
MPI_Type_commit(&type);
return type;
}
inline void maxloc_reduce(void *in, void *inout, int *len, MPI_Datatype *datatype)
{
MaxLoc *in_vals = static_cast<MaxLoc *>(in);
MaxLoc *inout_vals = static_cast<MaxLoc *>(inout);
for (int i = 0; i < *len; ++i)
{
if (in_vals[i].value > inout_vals[i].value)
{
inout_vals[i] = in_vals[i];
}
}
}
NumberType find_global_max_node_id(int local_size,
int world_size,
int rank,
NumberType (*id_local_to_global)(NumberType, int, int))
{
// 1. Compute local max
NumberType local_max = 0;
for (int local_idx = 0; local_idx < local_size; ++local_idx)
{
NumberType gid = id_local_to_global(local_idx, world_size, rank);
local_max = std::max(local_max, gid);
}
// 2. Reduce across all ranks
NumberType global_max;
MPI_Allreduce(&local_max,
&global_max,
1,
get_mpi_type<NumberType>(),
MPI_MAX,
MPI_COMM_WORLD);
return global_max;
}
//////////////// util used in selectSeed //////////////////////
// function to buffer collection of message to be sent in bigger batch
inline void add_count_batch(NumberType node_index,
int myrank,
int world_size,
std::unordered_map<int, std::vector<NumberType>> &count_buffers,
std::vector<NumberType> &local_count)
{
int destination = node_index % world_size;
if (destination == myrank)
{
NumberType local_index = node_index / world_size;
local_count[local_index]++;
return;
}
count_buffers[destination].push_back(node_index);
}
// function to buffer collection of message to be sent in bigger batch
inline void add_occurance_batch(NumberType row_index, NumberType col_index,
int myrank,
int world_size,
std::unordered_map<int, std::vector<NumberType>> &cooccur_buffers,
std::vector<std::vector<NumberType>> &local_C)
{
int destination = col_index % world_size;
if (destination == myrank)
{
// process locally
NumberType local_index = col_index / world_size;
local_C[row_index][local_index]++;
return;
}
cooccur_buffers[destination].push_back(row_index);
cooccur_buffers[destination].push_back(col_index);
}
inline void flush_count_messages(std::unordered_map<int, std::vector<NumberType>> &count_buffers,
int myrank, int world_size,
MPI_Datatype mpi_type)
{
for (auto &[dest, buffer] : count_buffers)
{
MPI_Send(buffer.data(), buffer.size(), mpi_type, dest, 0, MPI_COMM_WORLD);
}
// Send DONE message to each rank ≠ myrank
NumberType sentinel = static_cast<NumberType>(-1);
for (int rank = 0; rank < world_size; ++rank)
{
if (rank != myrank)
{
MPI_Send(&sentinel, 1, mpi_type, rank, 99, MPI_COMM_WORLD); // tag 99 = DONE
}
}
}
inline void flush_occurance_messages(std::unordered_map<int, std::vector<NumberType>> &cooccur_buffers,
int myrank, int world_size, MPI_Datatype mpi_type)
{
for (auto &[dest, buffer] : cooccur_buffers)
{
MPI_Send(buffer.data(), buffer.size(), mpi_type, dest, 1, MPI_COMM_WORLD);
}
// Send sentinel (-1, -1) to all other ranks
NumberType sentinel[2] = {static_cast<NumberType>(-1), static_cast<NumberType>(-1)};
for (int rank = 0; rank < world_size; ++rank)
{
if (rank != myrank)
{
MPI_Send(sentinel, 2, mpi_type, rank, 98, MPI_COMM_WORLD); // tag 98 = DONE
}
}
}
inline void receive_count_messages(std::vector<NumberType> &local_count,
int myrank, int world_size, MPI_Datatype mpi_type)
{
MPI_Status status;
int done_count = 0;
while (done_count < world_size - 1)
{
// Wait for any incoming message (either data or DONE)
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int tag = status.MPI_TAG;
if (tag == 99)
{
// DONE message: exactly one element
NumberType sentinel;
MPI_Recv(&sentinel, 1, mpi_type, status.MPI_SOURCE, 99, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
done_count++;
}
else
{
// Regular count message: determine length dynamically
int count;
MPI_Get_count(&status, mpi_type, &count);
std::vector<NumberType> buf(count);
MPI_Recv(buf.data(), count, mpi_type, status.MPI_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Apply all received updates
for (int i = 0; i < count; ++i)
{
NumberType node = buf[i];
int local_idx = node / world_size;
if (local_idx >= 0 && local_idx < static_cast<int>(local_count.size()))
{
local_count[local_idx]++;
}
}
}
}
}
inline void receive_occurance_messages(std::vector<std::vector<int>> &local_C,
int myrank, int world_size, MPI_Datatype mpi_type)
{
MPI_Status status;
int done_count = 0;
while (done_count < world_size - 1)
{
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int tag = status.MPI_TAG;
if (tag == 98)
{
NumberType sentinel[2];
MPI_Recv(sentinel, 2, mpi_type, status.MPI_SOURCE, 98, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
done_count++;
}
else
{
int count;
MPI_Get_count(&status, mpi_type, &count);
std::vector<NumberType> buf(count);
MPI_Recv(buf.data(), count, mpi_type, status.MPI_SOURCE, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// buf holds [row0,col0,row1,col1,...]
for (int i = 0; i < count; i += 2)
{
NumberType row = buf[i];
NumberType col = buf[i + 1];
int local_col = col / world_size;
if (row >= 0 && row < local_C.size() &&
local_col >= 0 && local_col < local_C[row].size())
{
local_C[row][local_col]++;
}
}
}
}
}
inline NumberType find_global_argmax(const std::vector<NumberType> &local_count, int myrank, int world_size,
MPI_Datatype maxloc_type, MPI_Op maxloc_op)
{
MaxLoc local = {-1, -1};
for (size_t i = 0; i < local_count.size(); ++i)
{
if (local_count[i] > local.value)
{
local.value = local_count[i];
local.node = static_cast<NumberType>(i * world_size + myrank);
}
}
MaxLoc global;
MPI_Allreduce(&local, &global, 1, maxloc_type, maxloc_op, MPI_COMM_WORLD);
return global.node;
}
#endif