File: PAPI_Matlab.c

package info (click to toggle)
papi 5.7.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,856 kB
  • sloc: ansic: 93,265; fortran: 3,338; xml: 2,460; makefile: 815; sh: 290
file content (249 lines) | stat: -rwxr-xr-x 8,188 bytes parent folder | download | duplicates (4)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

/****************************/                                                                                                     
/* THIS IS OPEN SOURCE CODE */                                                                                                     
/****************************/                                                                                                     
                                                                                                                                   
/**                                                                                                                                
  * @file:    PAPI_Matlab.c
  * CVS:     $Id$    
  * @author Joseph Thomas <jthomas@cs.utk.edu>
  *
  *	@brief PAPI Matlab integration.
  *	See PAPI_Matlab.readme for more information.
  */
#include "mex.h"
#include "matrix.h"
#include "papi.h"

static long long accum_error = 0;
static long long start_time = 0;

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]) {
  float real_time, proc_time, rate;
  int i;
  int number_of_counters;
  unsigned int mrows, nchars;
  unsigned int *events;
  unsigned int flop_events[2];
  long long ins = 0, *values, flop_values[2];
  long long elapsed_time;
  int result;
  char *input, *temp;
  char one_output[]	= "This function produces one output per running counter.";
  char no_input[]	= "This function expects no input.";
  char error_reading[]	= "Error reading the running counters.";

  /* Check for proper number of arguments. */
  if(nrhs < 1) {
    mexErrMsgTxt("This function expects input.");
  }
  nchars = mxGetNumberOfElements(prhs[0]);
  input = (char *)mxCalloc(nchars, sizeof(char) + 1);
  input = mxArrayToString(prhs[0]);

  if(!strncmp(input, "num", 3)) {
    if(nrhs != 1) {
      mexErrMsgTxt(no_input);
    }
    else if(nlhs != 1) {
      mexErrMsgTxt("This function produces one and only one output: counters.");
    }
    result = PAPI_num_counters();
    if(result < PAPI_OK) {
      mexPrintf("Error code: %d\n", result);
      mexErrMsgTxt("Error reading counters.");
    }
    plhs[0] = mxCreateDoubleScalar((double)result);
  }

  else if((!strncmp(input, "flip", 4)) || (!strncmp(input, "flop", 4))) {
    if(nrhs != 1) {
      mexErrMsgTxt(no_input);
    } else if(nlhs > 2) {
      if (input[2] == 'i')
	mexErrMsgTxt("This function produces 1 or 2 outputs: [ops, mflips].");
      else
	mexErrMsgTxt("This function produces 1 or 2 outputs: [ops, mflops].");
    }
    if (input[2] == 'i') {
      if(result = PAPI_flips( &real_time, &proc_time, &ins, &rate)<PAPI_OK) {
        mexPrintf("Error code: %d\n", result);
	     mexErrMsgTxt("Error getting flips.");
      }
    } else {
       if(result = PAPI_event_name_to_code("EMON_SSE_SSE2_COMP_INST_RETIRED_PACKED_DOUBLE", &(flop_events[0])) < PAPI_OK) {
          if(result = PAPI_flops( &real_time, &proc_time, &ins, &rate)<PAPI_OK) {
             mexPrintf("Error code: %d\n", result);
	          mexErrMsgTxt("Error getting flops.");
          }
       } else {
         if(start_time == 0) {
            flop_events[1] = PAPI_FP_OPS;
            start_time = PAPI_get_real_usec();
            if((result = PAPI_start_counters(flop_events, 2)) < PAPI_OK) {
               mexPrintf("Error code: %d\n", result);
               mexErrMsgTxt("Error getting flops.");
            } else {
               ins = 0;
               rate = 0;
            }
         } else {
            if((result = PAPI_read_counters(flop_values, 2)) < PAPI_OK) {
               mexPrintf("%d\n", result);
               mexErrMsgTxt(error_reading);
            } else {
               elapsed_time = PAPI_get_real_usec() - start_time;
               ins = (2*flop_values[0])+flop_values[1];
               rate = ((float)ins)/((float)elapsed_time);
            }
         }
       }
    }
    if(nlhs > 0) {
     plhs[0] = mxCreateDoubleScalar((double)(ins - accum_error));
      /* this call adds 7 fp instructions to the total */
      /* but apparently not on Pentium M with Matlab 7.0.4 */
/*      accum_error += 7; */
      if(nlhs == 2) {
        plhs[1] = mxCreateDoubleScalar((double)rate);
        /* the second call adds 4 fp instructions to the total */
      /* but apparently not on Pentium M with Matlab 7.0.4 */
/*        accum_error += 4; */
      }
    }
  }

  else if(!strncmp(input, "start", 5)) {
    if(nlhs != 0) {
      mexErrMsgTxt("This function produces no output.");
    }
    if(nrhs > (PAPI_num_counters() + 1)) {
      mexErrMsgTxt(one_output);
    }
    mrows = mxGetM(prhs[1]);
    events = (unsigned int *)mxCalloc(nrhs - 1, sizeof(int) + 1);
    for(i = 1; i < nrhs; i++) {
      if(mxIsComplex(prhs[i]) || !(mrows == 1) ) {
        mexErrMsgTxt("Input must be a list of strings.");
      }
      if(mxIsChar(prhs[i])) {
        nchars = mxGetNumberOfElements(prhs[i]);
        temp = (char *)mxCalloc(nchars, sizeof(char) + 1);
        temp = mxArrayToString(prhs[i]);
        if(result = PAPI_event_name_to_code(temp, &(events[i - 1])) < PAPI_OK) {
          mxFree(temp);
	  mexPrintf("Error code: %d\n", result);
          mexErrMsgTxt("Incorrect PAPI code given.");
       }
        mxFree(temp);
      }
      else {
        events[i - 1] = (unsigned int)mxGetScalar(prhs[i]);
      }
    }
    if((result = PAPI_start_counters(events, nrhs - 1)) < PAPI_OK) {
      mxFree(events);
      mexPrintf("Error code: %d\n", result);
      mexErrMsgTxt("Error initializing counters.");
    }
    mxFree(events);
  }

  else if(!strncmp(input, "stop", 4)) {
    if(nrhs != 1) {
      mexErrMsgTxt(no_input);
    }
	number_of_counters = PAPI_num_counters();
    if(nlhs > number_of_counters ) {
      mexErrMsgTxt(one_output);
    }
	if (nlhs == 0) 
	  values = (long long*)mxCalloc(number_of_counters, sizeof(long long));
	else 
	  values = (long long *)mxCalloc(nlhs, sizeof(long long) + 1);

	if (start_time == 0) {
	  if (nlhs == 0)
		result = PAPI_stop_counters(values, number_of_counters);
	  else
		result = PAPI_stop_counters(values, nlhs);
	} else {
	  start_time = 0;
	  result = PAPI_stop_counters(flop_values, 2);
	}

	if(result < PAPI_OK) {
	  if(result != PAPI_ENOTRUN) {
		mexPrintf("Error code: %d\n", result);
		mexErrMsgTxt("Error stopping the running counters.");
	  }
	}
	accum_error = 0;
	for(i = 0; i < nlhs; i++) {
	  plhs[i] = mxCreateDoubleScalar((double)values[i]);
	}
	mxFree(values);
  }

  else if(!strncmp(input, "read", 4)) {
    if(nrhs != 1) {
      mexErrMsgTxt(no_input);
    }
    if(nlhs > PAPI_num_counters()) {
      mexErrMsgTxt(one_output);
    }
    values = (long long *)mxCalloc(nlhs, sizeof(long long) + 1);
    if((result = PAPI_read_counters(values, nlhs)) < PAPI_OK) {
      mexPrintf("%d\n", result);
      mexErrMsgTxt(error_reading);
    }
    for(i = 0; i < nlhs; i++) {
      plhs[i] = mxCreateDoubleScalar((double)values[i]);
    }
    mxFree(values);
  }

  else if(!strncmp(input, "accum", 5)) {
    if(nrhs > PAPI_num_counters() + 1) {
      mexErrMsgTxt(no_input);
    }
    if(nlhs > PAPI_num_counters()) {
      mexErrMsgTxt(one_output);
    }
    values = (long long *)mxCalloc(nlhs, sizeof(long long) + 1);
    for(i = 0; i < nrhs - 1; i++) {
      values[i] = (long long)(*(mxGetPr(prhs[i + 1])));
    }
    if(result = PAPI_accum_counters(values, nlhs) < PAPI_OK) {
      mexPrintf("Error code: %d\n", result);
      mexErrMsgTxt(error_reading);
    }
    for(i = 0; i < nlhs; i++) {
      plhs[i] = mxCreateDoubleScalar((double)values[i]);
    }
    mxFree(values);
  }

  else if(!strncmp(input, "ipc", 3)) {
    if(nrhs != 1) {
      mexErrMsgTxt(no_input);
    } else if(nlhs > 2) {
      mexErrMsgTxt("This function produces 1 or 2 outputs: [ops, ipc].");
    }
    if(PAPI_ipc(&real_time, &proc_time, &ins, &rate)<PAPI_OK) {
      mexErrMsgTxt("Error getting instruction rate.");
    }
    if(nlhs > 0) {
      plhs[0] = mxCreateDoubleScalar((double)ins);
      if(nlhs == 2) {
        plhs[1] = mxCreateDoubleScalar((double)rate);
      }
    }
  }

  else {
    mexPrintf("Cannot find the command you specified.\n");
    mexErrMsgTxt("See the included readme file.");
  }
}