-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce.cc
87 lines (69 loc) · 2.26 KB
/
reduce.cc
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
/*
*
* Copyright (C) 2013 Virginia Tech
* See the file license.txt for copying permission.
*
* Justus Calvin
* Department of Chemistry, Virginia Tech
*
* reduce.cc
* Sep 20, 2013
*
*/
#include <iostream>
#include <world/world.h>
const int size = 300000000;
double reduce(register double* first, register double* const last) {
double value = 0.0;
for(;first != last; ++first)
value += *first;
return value;
}
double sum(double left, double right) {
return left + right;
}
madness::Future<double> task_reduce(madness::World* world,
double* first, double* last, const int block_size) {
if ((last - first) <= block_size) {
return madness::Future<double>(reduce(first, last));
} else {
double* middle = first + ((last - first) / 2);
madness::Future<double> leftsum =
world->taskq.add(& task_reduce, world, first, middle,
block_size, madness::TaskAttributes::hipri());
madness::Future<double> rightsum =
task_reduce(world, middle, last, block_size);
return world->taskq.add(& sum, leftsum, rightsum,
madness::TaskAttributes::hipri());
}
}
int main(int argc, char** argv) {
madness::World& world = madness::initialize(argc,argv);
// Allocate initialize vectors
double* x = new double[size];
std::fill_n(x, size, 1.0);
// Do serial reduce
const double serial_start = madness::wall_time();
reduce(x, x + size);
const double serial_time = madness::wall_time() - serial_start;
// Output serial results
std::cout << "Serial Time = " << serial_time << "\n\n";
// Iterate over different block sizes
std::cout << "Block size\tTime\t\tSpeedup"
<< "\n---------------------------------------\n";
for(int block_size = 8; block_size < size; block_size *= 2) {
// Do axpy in tasks with units of block_size elements
const double start = madness::wall_time(); // Start time
madness::Future<double> value =
task_reduce(& world, x, x + size, block_size);
// Wait for all tasks to finish (and process tasks)
value.get();
const double time = madness::wall_time() - start; // End time
// Display results
std::cout << " " << block_size << " \t"
<< time << "\t" << serial_time / time << "\n";
}
delete [] x;
madness::finalize();
return 0;
}