forked from MohsenKoohi/LaganLighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartitioning.c
334 lines (268 loc) · 8.77 KB
/
partitioning.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef __PARTITIONING_C
#define __PARTITIONING_C
int serial_edge_partitioning(struct ll_400_graph* g, unsigned int* borders, int partitions)
{
assert(partitions > 0 && g->vertices_count > 0);
borders[0] = 0;
borders[partitions] = g->vertices_count;
if(g->vertices_count <= 4 * partitions)
{
unsigned int remained_vertices = g->vertices_count;
unsigned int remained_partitions = partitions;
for(int i=1;i<partitions;i++)
{
if(remained_vertices > 0)
{
unsigned int q = max(1,remained_vertices/remained_partitions);
borders[i] = borders[i-1] + q;
remained_vertices -= q;
}
else
borders[i] = g->vertices_count;
//printf("%d %d %d\n",i,borders[i], remained_vertices);
remained_partitions--;
}
}
else
{
unsigned long edges_per_thread = (g->edges_count + g->vertices_count) / partitions;
unsigned int last_m = 0;
for(unsigned int t = 1; t < partitions; t++)
{
unsigned long start = 0;
unsigned long end = g->vertices_count;
unsigned long target = t * edges_per_thread;
unsigned long m = (start + end)/2;
while(1)
{
unsigned long m_val = g->offsets_list[m] + m;
if( m_val == target )
break;
unsigned long b_val = g->offsets_list[m - 1] + m - 1;
if( b_val < target && m_val > target )
break;
if(m_val > target)
end = m;
if(m_val < target)
start = m;
unsigned long new_m = (start + end)/2;
if(new_m == m)
break;
m = new_m;
}
assert( m <= g->vertices_count );
if(m <= last_m)
m = last_m + 1;
if(m > g->vertices_count)
m = g->vertices_count;
borders[t] = m;
last_m = m;
}
}
//verify_serial_edge_partitioning(g, borders, partitions);
return 0;
}
int parallel_edge_partitioning(struct ll_400_graph* g, unsigned int* borders, int partitions)
{
assert(partitions > 0 && g->vertices_count > 0);
borders[0] = 0;
borders[partitions] = g->vertices_count;
if(g->vertices_count <= 4 * partitions)
{
unsigned int remained_vertices = g->vertices_count;
unsigned int remained_partitions = partitions;
for(int i=1;i<partitions;i++)
{
if(remained_vertices > 0)
{
unsigned int q = max(1,remained_vertices/remained_partitions);
borders[i] = borders[i-1] + q;
remained_vertices -= q;
}
else
borders[i] = g->vertices_count;
//printf("%d %d %d\n",i,borders[i], remained_vertices);
remained_partitions--;
}
}
else
{
unsigned long edges_per_thread = (g->edges_count + g->vertices_count) / partitions;
#pragma omp parallel for
for(unsigned int t = 1; t < partitions; t++)
{
unsigned long start = 0;
unsigned long end = g->vertices_count;
unsigned long target = t * edges_per_thread;
unsigned long m = (start + end)/2;
while(1)
{
unsigned long m_val = g->offsets_list[m] + m;
if( m_val == target )
break;
unsigned long b_val = g->offsets_list[m - 1] + m - 1;
if( b_val < target && m_val > target )
break;
if(m_val > target)
end = m;
if(m_val < target)
start = m;
unsigned long new_m = (start + end)/2;
if(new_m == m)
break;
m = new_m;
}
assert( m <= g->vertices_count );
borders[t] = m;
}
unsigned int last_m = 0;
for(unsigned int t = 1; t < partitions; t++)
{
if(borders[t] <= last_m)
borders[t] = last_m + 1;
if(borders[t] > g->vertices_count)
borders[t] = g->vertices_count;
last_m = borders[t];
}
}
//verify_serial_edge_partitioning(g, borders, partitions);
return 0;
}
struct dynamic_partitioning
{
struct par_env* pe;
unsigned int partitions_count;
unsigned int partitions_remained;
unsigned int threads_count;
unsigned int* threads_partitions_start;
unsigned int* threads_partitions_end;
unsigned int* threads_partitions_remained;
unsigned int* threads_last_steal_offset;
unsigned char* partitions_status;
};
struct dynamic_partitioning* dynamic_partitioning_initialize(struct par_env* pe, unsigned int partitions_count)
{
assert(partitions_count > 0 && pe != NULL);
struct dynamic_partitioning* dp = calloc(sizeof(struct dynamic_partitioning), 1);
assert(dp != NULL);
dp->partitions_count = partitions_count;
dp->pe = pe;
dp->threads_count = pe->threads_count;
dp->threads_partitions_start = calloc(pe->threads_count ,sizeof(unsigned int));
dp->threads_partitions_end = calloc(pe->threads_count ,sizeof(unsigned int));
dp->threads_partitions_remained = calloc(pe->threads_count,sizeof(unsigned int));
assert(dp->threads_partitions_start != NULL && dp->threads_partitions_end != NULL && dp->threads_partitions_remained != NULL);
dp->threads_last_steal_offset = calloc(pe->threads_count ,sizeof(unsigned int));
assert(dp->threads_last_steal_offset != NULL);
dp->partitions_status = calloc(dp->partitions_count, sizeof(unsigned char));
assert(dp->partitions_status != NULL);
dp->partitions_remained = partitions_count;
// Partitioning for nodes and threads
unsigned int remained_threads = pe->threads_count;
unsigned int remained_partitions = dp->partitions_count;
unsigned int offset = 0;
for(int t=0; t<pe->threads_count; t++)
{
dp->threads_partitions_start[t] = offset;
unsigned int quota = remained_partitions/remained_threads;
offset += quota;
dp->threads_partitions_end[t] = offset;
dp->threads_partitions_remained[t] = quota;
remained_threads--;
remained_partitions -= quota;
dp->threads_last_steal_offset[t] = 0;
// printf("\t\tPartitions for thread %u: %'u - %'u\n",t, dp->threads_partitions_start[t], dp->threads_partitions_end[t]);
}
return dp;
}
void dynamic_partitioning_release(struct dynamic_partitioning* dp)
{
assert(dp != NULL);
dp->pe = NULL;
free(dp->threads_partitions_start);
dp->threads_partitions_start = NULL;
free(dp->threads_partitions_end);
dp->threads_partitions_end = NULL;
free(dp->threads_partitions_remained);
dp->threads_partitions_remained = NULL;
free(dp->partitions_status);
dp->partitions_status = NULL;
free(dp->threads_last_steal_offset);
dp->threads_last_steal_offset = NULL;
free(dp);
return;
}
void dynamic_partitioning_reset(struct dynamic_partitioning* dp)
{
assert(dp->partitions_remained == 0);
dp->partitions_remained = dp->partitions_count;
#pragma omp parallel for
for(unsigned int i=0; i<dp->partitions_count; i++)
{
assert(dp->partitions_status[i] == 1);
dp->partitions_status[i] = 0;
}
for(unsigned int t=0; t<dp->threads_count; t++)
{
assert(dp->threads_partitions_remained[t] == 0);
dp->threads_partitions_remained[t] = dp->threads_partitions_end[t] - dp->threads_partitions_start[t];
dp->threads_last_steal_offset[t] = 0;
}
return;
}
unsigned int dynamic_partitioning_get_next_partition(struct dynamic_partitioning* dp, unsigned int thread_id, unsigned int prev_partition)
{
#define CHECK_AND_GET_PARTITION(__p, __thread_id) \
if(__sync_bool_compare_and_swap(&dp->partitions_status[__p], 0, 1)) \
{ \
while(1) \
{ \
unsigned int temp = __atomic_load_n(&dp->partitions_remained, __ATOMIC_SEQ_CST); \
if(__sync_bool_compare_and_swap(&dp->partitions_remained, temp, temp-1)) \
break; \
} \
while(1) \
{ \
unsigned int temp = __atomic_load_n(&dp->threads_partitions_remained[__thread_id], __ATOMIC_SEQ_CST); \
if(__sync_bool_compare_and_swap(&dp->threads_partitions_remained[__thread_id], temp, temp-1)) \
break; \
} \
return p; \
}
if( __atomic_load_n(&dp->partitions_remained, __ATOMIC_SEQ_CST) == 0)
return -1U;
unsigned int start_partition = prev_partition + 1;
if(prev_partition == -1U)
{
start_partition = dp->threads_partitions_start[thread_id];
dp->threads_last_steal_offset[thread_id] = 0;
}
while(dp->threads_last_steal_offset[thread_id] < dp->threads_count)
{
unsigned int target_thread_id = dp->pe->threads_next_threads[thread_id][dp->threads_last_steal_offset[thread_id]];
if( __atomic_load_n(&dp->threads_partitions_remained[target_thread_id], __ATOMIC_SEQ_CST) == 0)
{
dp->threads_last_steal_offset[thread_id]++;
continue;
}
if(target_thread_id == thread_id)
{
// We are processing our partitions therefore go in the ascending order
for(unsigned int p=start_partition; p<dp->threads_partitions_end[target_thread_id]; p++)
CHECK_AND_GET_PARTITION(p, target_thread_id);
}
else
{
start_partition = prev_partition - 1;
// We are processing partitions of other threads, so we start from the last partition of each victim thread
// To prevent interrupting normal order of the victim
if(start_partition >= dp->threads_partitions_end[target_thread_id] || start_partition < dp->threads_partitions_start[target_thread_id])
start_partition = dp->threads_partitions_end[target_thread_id] - 1;
for(unsigned int p=start_partition; p>=dp->threads_partitions_start[target_thread_id] && p != -1U; p--)
CHECK_AND_GET_PARTITION(p, target_thread_id);
}
dp->threads_last_steal_offset[thread_id]++;
}
return -1U;
}
#endif