-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcercises_1.cpp
More file actions
109 lines (95 loc) · 3.6 KB
/
excercises_1.cpp
File metadata and controls
109 lines (95 loc) · 3.6 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#include <chrono>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <algorithm> // for std::transform
#include <cmath> // for std::abs
// TODO: add C++ standard library includes as necessary
// #include <...>
/// Intialize vectors `x` and `y`: raw loop sequential version
void initialize(std::vector<double> &x, std::vector<double> &y) {
assert(x.size() == y.size());
for (std::size_t i = 0; i < x.size(); ++i) {
x[i] = (double)i;
y[i] = 2.;
}
}
/// DAXPY: AX + Y: sequential algorithm version
void daxpy(double a, std::vector<double> const &x, std::vector<double> &y) {
assert(x.size() == y.size());
// Sequential transform: apply a*x + y element-wise
std::transform(x.begin(), x.end(), // first input range
y.begin(), // second input range
y.begin(), // output range
[&](double xi, double yi) {
return a * xi + yi;
});
}
// Check solution
bool check(double a, std::vector<double> const &y);
int main(int argc, char *argv[]) {
// Read CLI arguments, the first argument is the name of the binary:
if (argc != 2) {
std::cerr << "ERROR: Missing length argument!" << std::endl;
return 1;
}
// Read length of vector elements
long long n = std::stoll(argv[1]);
// Allocate the vector
std::vector<double> x(n, 0.), y(n, 0.);
double a = 2.0;
initialize(x, y);
daxpy(a, x, y);
if (!check(a, y)) {
std::cerr << "ERROR!" << std::endl;
return 1;
}
std::cerr << "OK!" << std::endl;
// Measure bandwidth in [GB/s]
using clk_t = std::chrono::steady_clock;
daxpy(a, x, y);
auto start = clk_t::now();
int nit = 100;
for (int it = 0; it < nit; ++it) {
daxpy(a, x, y);
}
auto seconds = std::chrono::duration<double>(clk_t::now() - start).count(); // Duration in [s]
// Amount of bytes transferred from/to chip.
// x is read, y is read and written:
auto gigabytes = 3. * (double)x.size() * (double)sizeof(double) * (double)nit * 1.e-9; // GB
std::cerr << "Bandwidth [GB/s]: " << (gigabytes / seconds) << std::endl;
return 0;
}
bool check(double a, std::vector<double> const &y) {
double tolerance = 2. * std::numeric_limits<double>::epsilon();
for (std::size_t i = 0; i < y.size(); ++i) {
double should = a * i + 2.;
if (std::abs(y[i] - should) > tolerance)
return false;
}
return true;
}