Blame src/Matlab/PAPI_Matlab.readme

Packit 577717
Running PAPI's High Level API in the MATLAB Environment
Packit 577717
Packit 577717
If you have the desire to do this, you most likely already
Packit 577717
know why you want to make calls to PAPI inside of a 
Packit 577717
MATLAB environment.
Packit 577717
Packit 577717
If you don't know much about what composes PAPI's high 
Packit 577717
level API, you should probably take a look at this:
Packit 577717
Packit 577717
http://icl.cs.utk.edu/projects/papi/files/documentation/PAPI_USER_GUIDE_23.htm#WHAT_IS_HIGH_LEVEL_API
Packit 577717
Packit 577717
This section of the PAPI user guide covers C and FORTRAN
Packit 577717
calls, but at the moment, you can only make C calls
Packit 577717
from the MATLAB environment.
Packit 577717
Packit 577717
There is one overall function to call from Matlab; from there,
Packit 577717
you specify which of the 6 specific functions you want to call, 
Packit 577717
and then the arguments to each.  Here are some examples:
Packit 577717
Packit 577717
PAPI_num_counters - Returns the number of available
Packit 577717
  hardware counters on the system.
Packit 577717
Packit 577717
Ex:  num_counters = PAPI('num')
Packit 577717
Packit 577717
Packit 577717
PAPI_flips - Has 3 possibilities:
Packit 577717
  Initialize FLIP counting with:
Packit 577717
    PAPI('flips')
Packit 577717
  Record the number of floating point instructions since
Packit 577717
  initialization:
Packit 577717
    ops = PAPI('flips')
Packit 577717
  Record the number of floating point instructions and
Packit 577717
  the incremental rate of floating point execution
Packit 577717
  since initialization:
Packit 577717
    [ops, mflips] = PAPI('flips')
Packit 577717
  Use PAPI_stop_counters to stop counting flips and reset the counters.
Packit 577717
Packit 577717
PAPI_flops - Identical to PAPI_flips,
Packit 577717
  but counts floating point *operations* rather than instructions.
Packit 577717
  In most cases, these two are identical, but some instructions
Packit 577717
  (e.g. FMA) might contain multiple operations or vice versa.
Packit 577717
Packit 577717
PAPI_ipc - Has 3 possibilities:
Packit 577717
  Initialize instruction per cycle counting with:
Packit 577717
    PAPI('ipc', 0)
Packit 577717
  Record the number of instructions since
Packit 577717
  initialization:
Packit 577717
    ins = PAPI('ipc')
Packit 577717
  Record the number of instructions and
Packit 577717
  the incremental rate of instructions per cycle
Packit 577717
  since initialization:
Packit 577717
    [ins, ipc] = PAPI('ipc')
Packit 577717
  
Packit 577717
Packit 577717
PAPI_start_counters - Specify the events to count
Packit 577717
  (in text form or the actual numeric code; NOTE: make sure
Packit 577717
  to not confuse normal decimal and hexadecimal.) You cannot
Packit 577717
  specify more events than there are hardware counters.
Packit 577717
Packit 577717
  To begin counting cycles and instructions:
Packit 577717
    PAPI('start', 'PAPI_TOT_CYC', 'PAPI_TOT_INS');
Packit 577717
Packit 577717
Packit 577717
PAPI_read_counters - Simply specify the variables to read
Packit 577717
  the values into.  You cannot specify more variables
Packit 577717
  than there are hardware counters.  This will reset the
Packit 577717
  counters.
Packit 577717
Packit 577717
  To read the above events you just started:
Packit 577717
    [cycles, instructions] = PAPI('read');
Packit 577717
Packit 577717
Packit 577717
PAPI_accum_counters - This function adds the value you
Packit 577717
  pass to the readings in the hardware counter.  You
Packit 577717
  cannot specify more variables than there are hardware
Packit 577717
  counters.  This function will reset the counters.
Packit 577717
Packit 577717
  To add the values currently in the counters to the
Packit 577717
  previously read values:
Packit 577717
    [cycles, instructions] = PAPI('accum', cycles, instructions);
Packit 577717
Packit 577717
Packit 577717
PAPI_stop_counters - This function reads the value of
Packit 577717
  the running hardware counters into the variables
Packit 577717
  you specify.  You cannot specify more variables than
Packit 577717
  there are hardware counters.
Packit 577717
Packit 577717
  To stop the running counters you previously started
Packit 577717
  and record their values:
Packit 577717
    [cycles, instructions] = PAPI('stop');
Packit 577717
Packit 577717
Packit 577717
PAPI_Matlab.c, when compiled, functions simply as a
Packit 577717
wrapper.  In order to use the calls, you need to know a little
Packit 577717
about mex.  mex is simply the compiler you use to make your
Packit 577717
code run in the MATLAB environment.  If you don't know
Packit 577717
how to use mex, you might want to acquaint yourself a bit.
Packit 577717
Packit 577717
"mex -setup "might be needed if you encounter problems, but
Packit 577717
the simplest explanation might be to substitute "mex" for 
Packit 577717
"gcc" and you are on your way.
Packit 577717
Packit 577717
All the other rules for compiling PAPI are the same.  mex
Packit 577717
compilations can de done inside or outside of the Matlab
Packit 577717
environment, but in this case, it is recommended that you
Packit 577717
compile outside of Matlab.  For some reason, compiling 
Packit 577717
inside does not work on some systems.
Packit 577717
Packit 577717
So far, the Linux environment and the Windows environment
Packit 577717
have been tested, but _in theory_ this code should work anywhere 
Packit 577717
PAPI and Matlab both work.
Packit 577717
Packit 577717
The following instructions are for a Linux/Unix environment:
Packit 577717
Packit 577717
Assuming papi.h is present in /usr/local/include and libpapi.so
Packit 577717
is present in /usr/local/lib, the below should work.  If not,
Packit 577717
you may need to alter the compile strings and/or the #include
Packit 577717
statement in PAPI_Matlab.c.  Also, the compile string will be
Packit 577717
different for different platforms.
Packit 577717
Packit 577717
For instance, if I want to compile and run on a linux
Packit 577717
machine assuming PAPI_Matlab.c is in your current working
Packit 577717
directory (you'll have a different compile string on a 
Packit 577717
different architecture):
Packit 577717
Packit 577717
1. Compile the wrapper:
Packit 577717
mex -I/usr/local/include PAPI_Matlab.c /usr/local/lib/libpapi.so -output PAPI
Packit 577717
Packit 577717
2.  Start Matlab:
Packit 577717
matlab
Packit 577717
Packit 577717
3.  Run the code:
Packit 577717
a.  Find the number of hardware counters on your system:
Packit 577717
num_counters = PAPI('num')
Packit 577717
Packit 577717
Packit 577717
b.  Play with flips - the first makes sure the counters are stopped and clear;
Packit 577717
    the second initializes the counting;
Packit 577717
    the third returns the number of floating point instructions
Packit 577717
    since the first call, and the fourth line does the same as the
Packit 577717
    second AND reports the incremental rate of floating point
Packit 577717
    execution since the last call:
Packit 577717
PAPI('stop')
Packit 577717
PAPI('flips')
Packit 577717
ins = PAPI('flips')
Packit 577717
[ins, mflips] = PAPI('flips')
Packit 577717
Packit 577717
c.  Play with instructions per cycle - the first makes sure the 
Packit 577717
    counters are stopped and clear; the second initializes counting;
Packit 577717
    the third returns the number of instructions
Packit 577717
    since the first call, and the fourth line does the same as the
Packit 577717
    second AND reports the incremental rate of instructions per
Packit 577717
    cycle since the last call:
Packit 577717
PAPI('stop')
Packit 577717
PAPI('ipc')
Packit 577717
ins = PAPI('ipc')
Packit 577717
[ins, ipc] = PAPI('ipc')
Packit 577717
Packit 577717
d. Try the example m files included with the distribution:
Packit 577717
PAPIInnerProduct.m
Packit 577717
PAPIMatrixVector.m
Packit 577717
PAPIMatrixMatrix.m
Packit 577717
Packit 577717
e.  Start counting:
Packit 577717
PAPI('start', 'PAPI_TOT_CYC', 'PAPI_TOT_INS')
Packit 577717
Packit 577717
f.  Read the counters and reset:
Packit 577717
[cycles, instr] = PAPI('read')
Packit 577717
Packit 577717
g.  Add the current value of the counters to a previous read
Packit 577717
    and reset:
Packit 577717
[cycles, instr] = PAPI('accum', cycles, instr)
Packit 577717
Packit 577717
h.  Read the counters and stop them:
Packit 577717
[cycles, instr] = PAPI('stop')
Packit 577717
Packit 577717
You can pass as many events as you like to be counted or
Packit 577717
recorded, as long as that number does not exceed the number
Packit 577717
of available hardware counters.
Packit 577717
Packit 577717
Contact ralph@eecs.utk.edu or/and ptools-perfapi@icl.utk.edu
Packit 577717
with any questions regarding PAPI calls in Matlab - either errors or questions.
Packit 577717
Also, this has just been implemented, so changes could
Packit 577717
be coming..........