-
Notifications
You must be signed in to change notification settings - Fork 5
/
SumParallel.h
557 lines (461 loc) · 20.3 KB
/
SumParallel.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// TODO: Implement a more efficient suggestion of using task_group to split the array into chunks with each returning its sum into one index of an array of sums
#pragma once
// Parallel Sum implementations
#ifndef _SumParallel_h
#define _SumParallel_h
#include "Configuration.h"
#include <thread>
#include <execution>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
#include <ratio>
#include <vector>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define __TBB_PREVIEW_TASK_ARENA_CONSTRAINTS_EXTENSION_PRESENT 1
#include <oneapi/tbb/task_arena.h>
#endif
#include "RadixSortMsdParallel.h"
#include "FillParallel.h"
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::milli;
using std::random_device;
using std::sort;
using std::vector;
namespace ParallelAlgorithms
{
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long Sum(unsigned long long in_array[], size_t l, size_t r)
{
unsigned long long sum = 0;
for (size_t current = l; current < r; current++)
sum += in_array[current];
//unsigned long long sum_left = std::accumulate(in_array + l, in_array + r, 0); // may be implemented using SIMD/SSE
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long Sum(unsigned in_array[], size_t l, size_t r)
{
unsigned long long sum = 0;
for (size_t current = l; current < r; current++)
sum += (unsigned long long)in_array[current];
//unsigned long long sum_left = std::accumulate(in_array + l, in_array + r, 0); // may be implemented using SIMD/SSE
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
// TODO: Not yet implemented
inline unsigned long long SumUnrolled(unsigned in_array[], size_t l, size_t r)
{
unsigned long long sum = 0;
for (size_t current = l; current < r; current++)
sum += (unsigned long long)in_array[current];
//unsigned long long sum_left = std::accumulate(in_array + l, in_array + r, 0); // may be implemented using SIMD/SSE
return sum;
}
#if 0
size_t last_by_four = l + ((r - l) / 4) * 4;
size_t current = l;
for (; current < last_by_four;) // Scan the array and count the number of times each digit value appears - i.e. size of each bin
{
countLeft_0[inArray[current]]++; current++;
countLeft_1[inArray[current]]++; current++;
countLeft_2[inArray[current]]++; current++;
countLeft_3[inArray[current]]++; current++;
}
#endif
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumBufferedLocally(unsigned in_array[], size_t l, size_t r)
{
const size_t BUFFER_DEPTH = 1024;
unsigned buffer_loc[BUFFER_DEPTH];
size_t num_buffers = (r - l + (BUFFER_DEPTH - 1)) / BUFFER_DEPTH;
unsigned long long sum = 0;
size_t current = l;
size_t i = 0;
for (; i < (num_buffers - 1); ++i)
{
std::copy(in_array + current, in_array + current + BUFFER_DEPTH, buffer_loc); // possibly using SIMD/SSE. TODO: Need to switch to Intel's unseq version
for (size_t j = 0; j < BUFFER_DEPTH; ++j)
sum += buffer_loc[j];
current += BUFFER_DEPTH;
}
for (; current < r; ++current)
sum += in_array[current];
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumBufferedLocally(unsigned long long in_array[], size_t l, size_t r)
{
const size_t BUFFER_DEPTH = 1024;
unsigned long long buffer_loc[BUFFER_DEPTH];
size_t num_buffers = (r - l + (BUFFER_DEPTH - 1)) / BUFFER_DEPTH;
unsigned long long sum = 0;
size_t current = l;
size_t i = 0;
for (; i < (num_buffers - 1); ++i)
{
std::copy(in_array + current, in_array + current + BUFFER_DEPTH, buffer_loc); // possibly using SIMD/SSE. TODO: Need to switch to Intel's unseq version
for (size_t j = 0; j < BUFFER_DEPTH; ++j)
sum += buffer_loc[j];
current += BUFFER_DEPTH;
}
for (; current < r; ++current)
sum += in_array[current];
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumBufferedExternally(unsigned in_array[], size_t l, size_t r, unsigned buffer[], size_t buffer_depth)
{
size_t num_buffers = (r - l + (buffer_depth - 1)) / buffer_depth;
unsigned long long sum = 0;
size_t current = l;
size_t i = 0;
for (; i < (num_buffers - 1); ++i)
{
std::copy(in_array + current, in_array + current + buffer_depth, buffer); // possibly using SIMD/SSE. TODO: Need to switch to Intel's unseq version
for (size_t j = 0; j < buffer_depth; ++j)
sum += buffer[j];
current += buffer_depth;
}
for (; current < r; ++current)
sum += in_array[current];
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
// 50% slower than non-buffered version
inline unsigned long long SumBufferedExternally(unsigned long long in_array[], size_t l, size_t r, unsigned long long buffer[], size_t buffer_depth)
{
size_t num_buffers = (r - l + (buffer_depth - 1)) / buffer_depth;
unsigned long long sum = 0;
size_t current = l;
size_t i = 0;
for (; i < (num_buffers - 1); ++i)
{
std::copy(in_array + current, in_array + current + buffer_depth, buffer); // possibly using SIMD/SSE. TODO: Need to switch to Intel's unseq version
for (size_t j = 0; j < buffer_depth; ++j)
sum += buffer[j];
current += buffer_depth;
}
for (; current < r; ++current)
sum += in_array[current];
return sum;
}
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallel(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
//if (((unsigned long long)(in_array + l) & 0x7) != 0)
// printf("Memory alignment is not on 8-byte boundary\n");
if ((r - l) <= parallelThreshold)
return Sum( in_array, l, r );
//return std::accumulate(in_array + l, in_array + r, 0ULL);
unsigned long long sum_left = 0, sum_right = 0;
size_t m = r / 2 + l / 2 + (r % 2 + l % 2) / 2; // average without overflow
#if defined(USE_PPL)
Concurrency::parallel_invoke(
#else
tbb::parallel_invoke(
#endif
[&] { sum_left = SumParallel(in_array, l, m, parallelThreshold); },
[&] { sum_right = SumParallel(in_array, m, r, parallelThreshold); }
);
// Combine left and right results
sum_left += sum_right;
return sum_left;
}
#if 0
inline unsigned long long SumParallel(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
//may return 0 when not able to detect
auto processor_count = std::thread::hardware_concurrency();
if (processor_count < 1)
{
processor_count = 1;
//cout << "Warning: Fewer than 1 processor core detected. Using only a single core.";
}
size_t length = r - l + 1;
if ((parallelThreshold * processor_count) < length)
parallelThreshold = length / processor_count;
return SumParallel_inner(in_array, l, r, parallelThreshold);
}
#endif
// Sum of an arbitrary numerical type to a 64-bit sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
template< class _Type >
inline long long SumParallel(_Type in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
//if (((unsigned long long)(in_array + l) & 0x7) != 0)
// printf("Memory alignment is not on 8-byte boundary\n");
if ((r - l) <= parallelThreshold)
{
long long sum_left = 0;
for (size_t current = l; current < r; current++)
sum_left += (long long)in_array[current];
//long long sum_left = std::accumulate(in_array + l, in_array + r, 0LL);
return sum_left;
}
long long sum_left = 0, sum_right = 0;
size_t m = r / 2 + l / 2 + (r % 2 + l % 2) / 2; // average without overflow
#if defined(USE_PPL)
Concurrency::parallel_invoke(
#else
tbb::parallel_invoke(
#endif
[&] { sum_left = SumParallel(in_array, l, m, parallelThreshold); },
[&] { sum_right = SumParallel(in_array, m, r, parallelThreshold); }
);
// Combine left and right results
sum_left += sum_right;
return sum_left;
}
// Non-recursive Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumNonRecursive(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 128 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
size_t i = 0;
for (; i < (num_tasks - 1); i++)
sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); // process full parallelThreshold chunks
sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); // process the last partial parallelThreshold chunk
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursive(unsigned in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursive(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveBufferedLocally(unsigned in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] { // process full parallelThreshold chunks
sum_array[i] = SumBufferedLocally(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1));
});
g.run([=] { // process the last partial parallelThreshold chunk
sum_array[num_tasks - 1] = SumBufferedLocally(in_array, l + parallelThreshold * i, r);
});
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveBufferedLocally(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
const size_t BUFFER_DEPTH_PER_TASK = 1024;
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] { // process full parallelThreshold chunks
sum_array[i] = SumBufferedLocally(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1));
});
g.run([=] { // process the last partial parallelThreshold chunk
sum_array[num_tasks - 1] = SumBufferedLocally(in_array, l + parallelThreshold * i, r);
});
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveBuffered(unsigned in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
const size_t BUFFER_DEPTH_PER_TASK = 1024;
unsigned* buffers = new unsigned[num_tasks * BUFFER_DEPTH_PER_TASK] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = SumBufferedExternally(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1), &buffers[i * BUFFER_DEPTH_PER_TASK], BUFFER_DEPTH_PER_TASK); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
// 50% slower than non-buffered version
inline unsigned long long SumParallelNonRecursiveBuffered(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
const size_t BUFFER_DEPTH_PER_TASK = 1024;
unsigned long long* buffers = new unsigned long long[num_tasks * BUFFER_DEPTH_PER_TASK] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = SumBufferedExternally(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1), &buffers[i * BUFFER_DEPTH_PER_TASK], BUFFER_DEPTH_PER_TASK); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursive(unsigned in_array[], size_t l, size_t r, unsigned long long* sum_array, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
//unsigned long long* sum_array = new unsigned long long[num_tasks] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
return sum;
}
// Non-recursive Parallel Sum
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursive(unsigned long long in_array[], size_t l, size_t r, unsigned long long* sum_array, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
//unsigned long long* sum_array = new unsigned long long[num_tasks] {};
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
return sum;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
// Non-recursive Parallel Sum without Hyperthreading
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveNoHyperthreading(unsigned long long in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
int no_ht_concurrency = tbb::info::default_concurrency(
tbb::task_arena::constraints{}.set_max_threads_per_core(1)
);
tbb::task_arena arena(no_ht_concurrency);
arena.execute([=] {
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
});
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum without Hyperthreading
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveNoHyperthreading(unsigned in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
int no_ht_concurrency = tbb::info::default_concurrency(
tbb::task_arena::constraints{}.set_max_threads_per_core(1)
);
tbb::task_arena arena(no_ht_concurrency);
arena.execute([=] {
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = Sum(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = Sum(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
});
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
// Non-recursive Parallel Sum without Hyperthreading
// left (l) boundary is inclusive and right (r) boundary is exclusive
inline unsigned long long SumParallelNonRecursiveBufferedLocallyNoHyperthreading(
unsigned in_array[], size_t l, size_t r, size_t parallelThreshold = 16 * 1024)
{
size_t num_tasks = (r - l + (parallelThreshold - 1)) / parallelThreshold;
unsigned long long* sum_array = new unsigned long long[num_tasks] {};
int no_ht_concurrency = tbb::info::default_concurrency(
tbb::task_arena::constraints{}.set_max_threads_per_core(1)
);
tbb::task_arena arena(no_ht_concurrency);
arena.execute([=] {
tbb::task_group g;
size_t i = 0;
for (; i < (num_tasks - 1); i++)
g.run([=] {sum_array[i] = SumBufferedLocally(in_array, l + parallelThreshold * i, l + parallelThreshold * (i + 1)); }); // process full parallelThreshold chunks
g.run([=] {sum_array[num_tasks - 1] = SumBufferedLocally(in_array, l + parallelThreshold * i, r); }); // process the last partial parallelThreshold chunk
g.wait(); // wait for all tasks to complete
});
unsigned long long sum = 0;
for (size_t i = 0; i < num_tasks; i++)
sum += sum_array[i];
delete[] sum_array;
return sum;
}
#endif
}
#endif