|
| 1 | +#include "../src/b63.h" |
| 2 | +#include "../src/counters/osx_kperf.h" |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <cstdint> |
| 6 | +#include <cstdio> |
| 7 | +#include <cstdlib> |
| 8 | +#include <iostream> |
| 9 | +#include <iterator> |
| 10 | +#include <pthread.h> |
| 11 | +#include <random> |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +struct A { |
| 15 | + A* next; |
| 16 | + int64_t payload; |
| 17 | +}; |
| 18 | + |
| 19 | +struct Test { |
| 20 | + Test(int64_t len, int64_t rep) : rep_(rep * len) { |
| 21 | + l.resize(len); |
| 22 | + for (int64_t i = 0; i < len; i++) { |
| 23 | + l[i].payload = i; |
| 24 | + } |
| 25 | + std::random_device rd; |
| 26 | + std::mt19937 g(rd()); |
| 27 | + std::shuffle(l.begin(), l.end(), g); |
| 28 | + |
| 29 | + for (int64_t i = 0; i + 1 < len; i++) { |
| 30 | + l[i].next = &l[i+1]; |
| 31 | + } |
| 32 | + l[len - 1].next = &l[0]; |
| 33 | + |
| 34 | + std::sort(l.begin(), l.end(), [](const A& a, const A& b){ |
| 35 | + return a.payload < b.payload; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + int64_t run() { |
| 40 | + A* curr = &l[0]; |
| 41 | + int64_t res = 0; |
| 42 | + for (int64_t r = 0; r < rep_; r++) { |
| 43 | + curr = curr->next; |
| 44 | + if (curr->payload % 3 == 0) { |
| 45 | + res += curr->payload; |
| 46 | + } |
| 47 | + } |
| 48 | + return res; |
| 49 | + } |
| 50 | + |
| 51 | + int64_t run_unrolled() { |
| 52 | + A* curr = &l[0]; |
| 53 | + int64_t res = 0; |
| 54 | + for (int64_t r = 0; r < rep_; r += 4) { |
| 55 | + curr = curr->next; |
| 56 | + if (curr->payload % 3 == 0) { |
| 57 | + res += curr->payload; |
| 58 | + } |
| 59 | + curr = curr->next; |
| 60 | + if (curr->payload % 3 == 0) { |
| 61 | + res += curr->payload; |
| 62 | + } |
| 63 | + curr = curr->next; |
| 64 | + if (curr->payload % 3 == 0) { |
| 65 | + res += curr->payload; |
| 66 | + } |
| 67 | + curr = curr->next; |
| 68 | + if (curr->payload % 3 == 0) { |
| 69 | + res += curr->payload; |
| 70 | + } |
| 71 | + } |
| 72 | + return res; |
| 73 | + } |
| 74 | + private: |
| 75 | + std::vector<A> l; |
| 76 | + int64_t rep_; |
| 77 | +}; |
| 78 | + |
| 79 | +const size_t kSize = (1 << 10); |
| 80 | + |
| 81 | +B63_BENCHMARK(icestorm, n) { |
| 82 | + pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); |
| 83 | + Test* t; |
| 84 | + B63_SUSPEND { |
| 85 | + t = new Test(kSize, n); |
| 86 | + } |
| 87 | + |
| 88 | + int64_t res = t->run(); |
| 89 | + |
| 90 | + B63_KEEP(res); |
| 91 | + B63_SUSPEND { |
| 92 | + delete t; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +B63_BENCHMARK(icestorm_unrolled, n) { |
| 97 | + pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); |
| 98 | + Test* t; |
| 99 | + B63_SUSPEND { |
| 100 | + t = new Test(kSize, n); |
| 101 | + } |
| 102 | + |
| 103 | + int64_t res = t->run_unrolled(); |
| 104 | + |
| 105 | + B63_KEEP(res); |
| 106 | + B63_SUSPEND { |
| 107 | + delete t; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +B63_BASELINE(firestorm, n) { |
| 112 | + pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0); |
| 113 | + Test* t; |
| 114 | + B63_SUSPEND { |
| 115 | + t = new Test(kSize, n); |
| 116 | + } |
| 117 | + |
| 118 | + int64_t res = t->run(); |
| 119 | + |
| 120 | + B63_KEEP(res); |
| 121 | + B63_SUSPEND { |
| 122 | + delete t; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +B63_BENCHMARK(firestorm_unrolled, n) { |
| 127 | + pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0); |
| 128 | + Test* t; |
| 129 | + B63_SUSPEND { |
| 130 | + t = new Test(kSize, n); |
| 131 | + } |
| 132 | + |
| 133 | + int64_t res = t->run_unrolled(); |
| 134 | + |
| 135 | + B63_KEEP(res); |
| 136 | + B63_SUSPEND { |
| 137 | + delete t; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +int main(int argc, char **argv) { |
| 142 | + srand(time(0)); |
| 143 | + B63_RUN_WITH("time", argc, argv); |
| 144 | + return 0; |
| 145 | +} |
0 commit comments