How to call VTune Resume and Pause API from Fortran and C/C++ code?

It seems that some users of VTune Amplifier XE have interest of using Resume & Pause API to collect performance data in specific sections of their code, see this article.

Also we noticed that some users said example code in that article only works on Windows, since __itt_pause() & __itt_resume() API are not exported APIs on Linux. 

$ nm -a /opt/intel/vtune_amplifier_xe_2011/lib64/libittnotify.a
0000000000001810 D __itt_resume_ptr__3_0
0000000000001808 D __itt_pause_ptr__3_0


We will provide two samples to demonstrate this feature.

Before you compile and link the sample code, please set up all tools' environment first, for example:

$source /opt/intel/composer_xe_2011_sp1.9.293/bin/compilervars.sh intel64
$source /opt/intel/vtune_amplifier_xe_2011/amplxe-vars.sh




Case 1: When the user directly calls above APIs __itt_resume() and __itt_pause() in C/C++ code, it still works. Because C/C++ compiler will know real APIs after precompiling include file in test code.

Here are C/C++ example code:

test.cpp

#include "stdio.h"
#include "ittnotify.h"

void foo_data_collected()
{
    for (int i=0; i<10000000L; i++);
}

void foo_data_not_collected()
{
    for (int i=0; i<10000000L; i++);
}


int main(int argc, char* argv[])
{

    // Assume that data collector was paused in user interface
    foo_data_not_collected();

    // Now resume data collecting
    __itt_resume();

    foo_data_collected();

    // Pasue data collecting again
    __itt_pause();

    foo_data_not_collected();

    // Resume data collecting again
    __itt_resume();

    foo_data_collected();


    // The user shouldn't see data collecting in foo_data_not_collected() in result

    return 1;
}



Now compile the sample code and link with VTune libraries, then run to collect the information in command line:
 

$ icpc -g test.cpp -I/opt/intel/vtune_amplifier_xe_2011/include /opt/intel/vtune_amplifier_xe_2011/lib64/libittnotify.a -lpthread -o test 
$ amplxe-cl -collect hotspots -start-paused -- ./test




Case 2: Users will meet some linking failure messages, if call those APIs directly from Fortran code.  Because Fortran compiler could not process ittnotify.h (interpret __itt_resume() & __itt_pause() ) which was designed for C/C++ code.

Thus, we need to create new APIs (implemented by C/C++) instead of original APIs, then called them from Fortran code.

See below example code:

api_fortran_itt.c

#include "ittnotify.h"

void fortran_itt_resume()
{
    __itt_resume();
}

void fortran_itt_pause()
{
    __itt_pause();
}



fortran_vtune.f90

subroutine data_not_collected_routine()
   implicit none
   integer i, total
   total = 0
   do i = 1, 10000000
     total = total + 1
   end do
end subroutine data_not_collected_routine

subroutine data_collected_routine()
   implicit none
   integer i, total
   total = 0
   do i = 1, 10000000
     total = total + 1
   end do
end subroutine data_collected_routine


MODULE ITT_FORTRAN
USE, INTRINSIC :: ISO_C_BINDING
INTERFACE
   SUBROUTINE FORTRAN_ITT_RESUME &
      BIND(C, NAME='fortran_itt_resume')
!      BIND(C, NAME='__itt_resume_ptr__3_0')
   END SUBROUTINE FORTRAN_ITT_RESUME

   SUBROUTINE FORTRAN_ITT_PAUSE &
      BIND(C, NAME='fortran_itt_pause')
!      BIND(C, NAME='__itt_pause_ptr__3_0')
   END SUBROUTINE
END INTERFACE
END MODULE

program test
   USE, INTRINSIC :: ISO_C_BINDING
   USE  ITT_FORTRAN

   CALL FORTRAN_ITT_RESUME()
   CALL data_collected_routine()
   CALL FORTRAN_ITT_PAUSE()
   CALL data_not_collected_routine()
   CALL FORTRAN_ITT_RESUME()
   CALL data_collected_routine()
end program test



Now compile, link and run get expected results from Fortran code:

$ icc -g -c -I /opt/intel/vtune_amplifier_xe_2011/include api_fortran_itt.c

$ ifort -g -c fortran_vtune.f90

$ ifort -g -o test_itt.ifort api_fortran_itt.o fortran_vtune.o /opt/intel/vtune_amplifier_xe_2011/lib64/libittnotify.a

$ amplxe-cl -collect hotspots -start-paused -- ./test_itt.ifort




Both case 1 and case 2 will collect performance data for preferred code sections, look like following: 

pause_resume_api.jpg

For more complete information about compiler optimizations, see our Optimization Notice.

Comments