cpapi

Call PAPI_start() and PAPI_stop() in different functions?


Usally PAPI is used like this:

Initialize Lib
Add Event(s)
PAPI_start()
someCalculation()
PAPI_stop()

However, I have an application which times code like this:

start = time()
someCalculation()
t = time() - start;

I want to override this time() function, which is in another file. My code:

static int INIT=0;
int retval;
static int eventSet = PAPI_NULL;
long long res[1];

if(INIT){
        // eventSet is NOT PAPI_NULL here!
        if ((retval = PAPI_stop(eventSet, res)) != PAPI_OK)
                handle_error(retval);
        return res[0]/1e9;
}

INIT = 1;
retval = PAPI_library_init(PAPI_VER_CURRENT);

if ((retval = PAPI_create_eventset(&eventSet)) != PAPI_OK)
    handle_error(retval);

if ((retval = PAPI_add_named_event(eventSet, (char *) "rapl:::PP0_ENERGY:PACKAGE0")) != PAPI_OK)
    handle_error(retval);

if ((retval = PAPI_start(eventSet)) != PAPI_OK)
    handle_error(retval);

return 0.0;

When running the application I get PAPI error -9: EventSet is currently not running from PAPI_stop().

I assumed that it would be sufficient to share the eventSet variable between the two runs of the function. Unfortunately this doesn't seem to work as the event is not running when PAPI_stop() is called.

Is there any chance to fix this?


Solution

  • What happens when you call your timer the third time? You stopped the events already. You need to rethink your design, perhaps use PAPI_read – Anycorn