-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmPAPI_toc.cpp
65 lines (56 loc) · 1.84 KB
/
mPAPI_toc.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
#include <mex.h>
#include <papi.h>
#include <cmath>
#include <vector>
#include "mPAPI_utils.hpp"
/*
* mPAPI_toc -- read values from hardware performance monitoring counters.
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (!mexIsLocked())
{
mexLock();
}
size_t K = mxGetNumberOfElements(prhs[0]);
// Check num. of first event set
double *events = mxGetPr(prhs[0]);
int event_set = static_cast<int>(round(events[0]));
int event_set_size = PAPI_num_events(event_set);
plhs[0] = mxCreateNumericMatrix(K, PAPI_num_events(event_set), mxINT64_CLASS, mxREAL);
long long *output = (long long *)mxGetData(plhs[0]);
std::vector<std::vector<long long>> results;
for (size_t k = 0; k < K; ++k)
{
event_set = static_cast<int>(round(events[k]));
if (event_set == PAPI_NULL)
{
mexErrMsgTxt("Register performance events before reading them.");
}
event_set_size = PAPI_num_events(event_set);
long long counter_values[event_set_size];
int retval;
if ((retval = PAPI_stop(event_set, counter_values)) != PAPI_OK)
{
mPAPI_mex_error_with_reason("Failed to stop and read PAPI counters", retval);
}
if ((retval = PAPI_reset(event_set)) != PAPI_OK)
{
mPAPI_mex_error_with_reason("Failed to reset the event set", retval);
}
// Insert into a vector
std::vector<long long> line;
for (int i = 0; i < event_set_size; ++i)
{
line.push_back(counter_values[i]);
}
results.push_back(line);
}
for (unsigned int row = 0; row < K; row++)
{
for (unsigned int col = 0; col < event_set_size; ++col)
{
output[col + row * event_set_size] = results[row][col];
}
}
}