# Using PAPI in PETSc code

I am trying to count the number of cache misses, total cycles, etc. per iteration of a for loop inside MatMult_SeqSBAIJ_2() . I've configured PETSc to use PAPI and I am using the PAPI API like shown in the skeletal code below. PAPI always fails (!PAPI_OK) while running ./main . Is there a setting in PAPI or PETSc that I am missing, which enables proper counter sampling inside PETSc?

main.c:

PetscInitialize(&argc,&args,(char*)0,help);
PAPI_library_init(PAPI_VER_CURRENT); //Initialize PAPI
/*Matrix and vector setup code here. */
MatMult_SeqSBAIJ_2(A,xx,zz)
/*Destroy matrix here*/


sbaij2.c :

PetscErrorCode MatMult_SeqSBAIJ_2(Mat A,Vec xx,Vec zz)
{
long long counters[2];
int PAPI_events[2] = {PAPI_TOT_CYC, PAPI_L1_MISSES};
int retval=0;

PAPI_start_counters( PAPI_events, 2 );

for (i=0; i<mbs;i++)
{
/*SpMV operations happen here*/

if ( (retval = PAPI_read_counters(counters, 2)) != PAPI_OK)
{
printf("Papi fails!\n");
}

}

PAPI_stop_counters( counters, 2 );

}

• Welcome to SciComp.SE! Since this is a question solely about configuring PETSc, you should post this on the PETSc mailing list; this site is about questions with a concrete scientific background. – Christian Clason Oct 11 '13 at 18:01
• @ChristianClason - we don't have a strong scientific requirement here. The FAQ states "technical computing" questions are welcome. – Aron Ahmadia Oct 12 '13 at 15:14
• @Aron - sorry, I was going by what seems to have become consensus after the FEniCS episode: debugging questions should be posted on package-specific fora (see these questions on meta). – Christian Clason Oct 12 '13 at 22:12
• @ChristianClason It's a nuanced thing. I'm willing to deal with questions like these when there's a reasonable generality to the topic and the questions show a reasonable amount of effort by the questioner. I'm happy to discuss this further with you on meta, chat, or email if you'd like to talk more about my feelings on this (I'm no longer a moderator, so my opinion is worth as much as yours :) – Aron Ahmadia Oct 13 '13 at 5:04

In general you want to know as much about your error as possible when debugging these sorts of problems. In this case, you're not providing enough information with your retval check. See the PAPI documentation on error-handling for more information, but to start, you may want to try:
retval = PAPI_read_counters(counters, 2));