Silverfrost Forums

Welcome to our forums

How to count function \"CALL\" ?

23 Jan 2019 4:43 #23173

Hello 😃

Having written with Silverfrost FTN95 a (large) program of astronomical calculations, I use it to calculate a number of characteristics of the Solar System. The results obtained are excellent, compared to those of the Official Data Centres (e.g. the C.D.S of Strasbourg), and furthermore, fast : the calculation of the characteristics of the Sun and all planets for the 365 days of a year takes only 1.8 seconds on a little amateur HP computer !

That said, just out of intellectual curiosity, I would like to know how many times this program calls trigonometric functions over multiple calculations of essential parameters and numerous iterations to solve certain differential equations.

Hence my question : is there an utility function or programming method to detect 'on the fly' and increment the number of calls to a COS(), a SIN(), and another ARCTG() ?

Thanks in advance for any suggestions or link Cordially, Sospel

23 Jan 2019 8:50 #23174

Sospel

There is no automatic way to count the number of calls. In a sense this is particularly true for functions like COS and SIN that don't require any external code.

You would need to do your own counting which would add a significant overhead.

24 Jan 2019 2:24 #23176

You should experiment with the FTN95 /timing option. I am a big fan of this.

you need to divide the code into routines:

you want to time (sim_ver1_tim.f90)

you don't want to time (called too many times) (sutil.f90)

and other un-timed utility routines (util.f90)

My batch file to achieve this to generate sim_tim.exe is:

set program=sim_tim
set trace=ftn95_tim.tce

now                             > %trace%

del *.obj                      >> %trace%
del *.mod                      >> %trace%

SET TIMINGOPTS=/TMO /DLM ,

ftn95 sim_ver1_tim     /timing >> %trace%
ftn95 sutil            /debug  >> %trace%
ftn95 util             /debug  >> %trace%

slink  main_tim.txt            >> %trace%
dir *.exe                      >> %trace%
type %trace%

%program% test_xx.txt  > %program%.tce

My file main_tim.txt is lo sim_ver1_tim.obj lo sutil.obj lo util.obj le \clearwin\saplib.mem\saplib.lib map sim_tim.map file sim_tim.exe

My file 'sim_ver1_tim.f90' consists of lots of include of .f90 files for the code to be tested for call counts and elapse times. ! Last change: JDC 11 Sep 2009 10:47 am include 'sim_data.f95' include 'yrdplt.f95' include 'get_historical_ships.f95' include 'sim_ver1.f95' include 'read_data.f95' include 'read_swell_history.f95' include 'initialise.f95' include 'open.f95' include 'stem_gen.f95' include 'next.f95' include 'day.f95' include 'vessel_move.f95' include 'vessel_ab.f95' include 'channel.f95' include 'tide.f95' include 'debal_calc.f95' include 'depart.f95' include 'train.f95' include 'train_move.f95' include 'pend.f95' include 'choose.f95' include 'choose_equip.f95' include 'pform.f95' include 'update.f95' include 'report.f95' include 'lotus.f95'

24 Jan 2019 8:04 #23179

I should have mentioned that FTN95 has the /PROFILE option on the command line that can be used with 64 bit compilation. This provides a count of how many times each line of the code is visited.

The documentation for the 32 bit debugger SDBG also mentions a PROFILE command but I have not tested this.

24 Jan 2019 8:17 #23183

@PaulLaidler and @JohnCampbell

hello 😃

thank you for your quick answers ! I thought a solution was not obvious 😦

I will try however to implement JohnCampbell’s method, but I think it will be difficult to adapt it to my software I will also see if the /PROFILE option is taken into account by my 32 bit system ?

Thank you again ! cordially SosPel

24 Jan 2019 10:56 #23184

You can over-ride the function and count the calls using something like the following:

function sin(x)
   real, intent(in) :: x
   real sin
   integer n
   common /sin_count/ n
   intrinsic asin
   n = n + 1
   sin = asin(x)
end function

program main
   integer n, i
   common /sin_count/ n
   external sin
   real x, y
   x = 0.0
   n = 0
   do i=1, 1000
      y = sin(x)
   end do
   do i=1, 1000
      y = sin(x)
   end do
   print *, 'number of calls = ', n
end program main

Not ideal, but OK if it's just to satisfy your curiosity.

25 Jan 2019 7:22 #23193

@davidb

You can over-ride the function and count the calls using something like the following:

Thank for your suggestion which seems to be easy to implement in my software: I think it will suffice for me to make a 'over-ride' for each trigonometric function then to run the execution. I had forgotten this possibility of FORT95, not always allowed in other languages

I’ll execute your example first to verify if I have 2,000 uses of the SIN() function ... This seems obvious , but I sometimes have functions with somewhat complicated arguments and we are never immune from a surprise ! Thank you again, Cordially Sospel

26 Jan 2019 12:47 #23194

In many cases, it is not necessary to hook a profiler to a library function entry point to count the number of function calls, especially if all the calls occur in source code that you have and can change.

If, for instance, you have a small number of references to sin() in your code, you can simply add statements such as 'nsin = nsin + 1' if the expression is scalar, or 'nsin = nsin + n' if the expression is a vector of size n, set nsin = 0 at the beginning of the program, and print nsin at the end.

This approach makes it unnecessary to reduce optimisation levels, as many compilers require in order to enable profiling.

Please login to reply.