InitHighLevel
From PAPIDocs
Jump to: navigation, search

Example of Initializing the High-Level API

This code initializes the library, prints out the number of available hardware counters, and starts a couple counters counting.

C:

#include <papi.h>
 
main()
{
	int Events[2] = { PAPI_TOT_CYC, PAPI_TOT_INS };
	int num_hwcntrs = 0;

	/* Initialize the PAPI library and get the number of counters available */
	if ((num_hwcntrs = PAPI_num_counters()) <= PAPI_OK)  
		handle_error(1);

	printf("This system has %d available counters", num_hwcntrs);

	if (num_hwcntrs > 2)
		num_hwcntrs = 2;

	/* Start counting events */
	if (PAPI_start_counters(Events, num_hwcntrs) != PAPI_OK)
		handle_error(1);
}


Fortran:

program InitHighLevel

	implicit none

	INCLUDE 'f90papi.h'

	integer, dimension(2)	:: Events
	integer	:: check, num_hwcntrs

	! Get number of hardware counters and initialize PAPI library if necessary
	call PAPIF_num_counters(num_hwcntrs);
	if (num_hwcntrs .lt. 0) then
		print *, "Error initializing library"
		call abort
	end if

	print *, "This system has ", num_hwcntrs, " available counters"

	! Start two specific counters
	num_hwcntrs = 2
	Events(1) = PAPI_TOT_CYC
	Events(2) = PAPI_TOT_INS
	call PAPIF_start_counters(Events, num_hwcntrs, check)
	if (check .ne. PAPI_OK) then
		print *, "Counters failed to start."
		call abort
	end if

end program InitHighLevel
POSSIBLE OUTPUT (varies on different systems):
This system has 4 available counters
 

On success, PAPI_num_counters returns the number of hardware counters available on the system and on error, a non-zero error code is returned.

'Optionally', the PAPI library can be initialized explicitly by using'PAPI_library_init'. This can be useful if you wish to call PAPI low-level API functions before using the high-level functions.