This repository was archived by the owner on Jun 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernels.hpp
74 lines (63 loc) · 2.63 KB
/
kernels.hpp
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
//
// Created by Hippolyte Barraud on 10/03/2018.
//
#ifndef MEM_BANDWIDTH_KERNELS_HPP
#define MEM_BANDWIDTH_KERNELS_HPP
#include <functional>
#include <algorithm>
template<typename T, int bytes, int flots>
struct kernel {
static constexpr auto bytes_per_iter = bytes * sizeof(T);
};
template<typename Container, typename T>
struct fill : public kernel<T, 1, 0> {
static constexpr auto name = "Fill";
static inline void run(Container & first, Container const&, Container const&) {
std::fill(first.begin(), first.end(), 3.14);
}
};
template<typename Container, typename T>
struct copy : public kernel<T, 2, 0> {
static constexpr auto name = "Copy";
static inline void run(Container & first, Container const& second, Container const&) {
volatile __attribute__((unused)) auto f = std::copy(second.begin(), second.end(), first.begin());
}
};
template<typename Container, typename T>
struct scale : public kernel<T, 2, 1> {
static constexpr auto name = "Scale";
static inline void run(Container & first, Container const& second, Container const&) {
std::transform(second.data(), second.data() + second.size(), first.data(), [](auto x) { return 3.14 * x; });
}
};
template<typename Container, typename T>
struct sum : public kernel<T, 3, 1> {
static constexpr auto name = "Sum";
static inline void run(Container & first, Container const& second, Container const& third) {
std::transform(second.data(), second.data() + second.size(), third.data(), first.data(), std::plus<T>());
}
};
template<typename Container, typename T>
struct triad : public kernel<T, 3, 2> {
static constexpr auto name = "Triad";
static inline void run(Container & first, Container const& second, Container const& third) {
std::transform(second.data(), second.data() + second.size(), third.data(), first.data(), [](const auto& x, const auto& y) {
return x + 3.14 * y;
});
}
};
template<typename Container, typename T>
struct vsum : public kernel<T, 1, 1> {
static constexpr auto name = "Vsum";
static inline void run(Container const&, Container const& second, Container const&) {
volatile __attribute__((unused)) auto f = std::accumulate(second.begin(), second.end(), 1.0, std::plus<T>());
}
};
template<typename Container, typename T>
struct vprod : public kernel<T, 1, 1> {
static constexpr auto name = "Vprod";
static inline void run(Container const&first, Container const&, Container const&) {
volatile __attribute__((unused)) auto f = std::accumulate(first.begin(), first.end(), 1.0, std::multiplies<T>());
}
};
#endif //MEM_BANDWIDTH_KERNELS_HPP