forked from LLNL/Caliper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcali-perfproblem-branch-mispred.cpp
83 lines (60 loc) · 1.65 KB
/
cali-perfproblem-branch-mispred.cpp
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
// Copyright (c) 2019, Lawrence Livermore National Security, LLC.
// See top-level LICENSE file for details.
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <caliper/cali.h>
#include <caliper/cali_datatracker.h>
int* init(size_t arraySize, bool sort)
{
CALI_CXX_MARK_FUNCTION;
int *data =
static_cast<int*>(cali_datatracker_allocate_dimensional("data", sizeof(int), &arraySize, 1));
std::srand(1337);
for (size_t c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;
if (sort)
std::sort(data, data + arraySize);
return data;
}
void work(int *data, size_t arraySize)
{
CALI_CXX_MARK_FUNCTION;
long sum = 0;
for (size_t i = 0; i < 100000; ++i)
{
// Primary loop
for (size_t c = 0; c < arraySize; ++c)
{
if (data[c] >= 128)
sum += data[c];
}
}
std::cout << "sum = " << sum << std::endl;
}
void cleanup(int *data)
{
CALI_CXX_MARK_FUNCTION;
cali_datatracker_free(data);
}
void benchmark(size_t arraySize, bool sort)
{
CALI_CXX_MARK_FUNCTION;
cali::Annotation sorted("sorted");
sorted.set(sort);
std::cout << "Initializing benchmark data with sort = " << sort << std::endl;
int *data = init(arraySize, sort);
std::cout << "Calculating sum of values >= 128" << std::endl;
work(data, arraySize);
std::cout << "Cleaning up" << std::endl;
cleanup(data);
std::cout << "Done!" << std::endl;
}
int main(int argc, char *argv[])
{
CALI_CXX_MARK_FUNCTION;
// Generate data
size_t arraySize = 32768;
benchmark(arraySize, true);
benchmark(arraySize, false);
}