Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Problem with function call in ifort

Balamurugan_R_
Beginner
603 Views

Dear all,

Iam working on linpack benchmark where in i need to supply a high resolution timer function. The intrinsic function second() does not provide the required resolution. So i had to use a function based on gettimeofday() function. The function was written in C and was compiled using gcc to create a *.o file.



The fortran program was modified to use the call the c functions binary object. All was working fine except that when the function is called it returns some weird results in the main program but the function itself seems OK.



I am attaching the code and the results here.





FORTRAN CODE:



c The program simulates the linpack second function call at its simplest form.

c The linpack code cannot be changed in any form as it is said in the site

c

PROGRAM b2

double precision :: t1,t2

t1=second1()

c

c DO SOME JOB

c

c elapsed_time =second1() - t1

c print elapsed_time

c

print *,"FORTRAN: MAIN CALL:",t1

END

c

c END OF PROGRAM b2

c

double precision function second1()

implicit none

external mysecond

double precision :: v1,v2

c making a call to the external c function

call mysecond(v1)

v2=v1

second1=v2

print *,"FORTRAN: AT FUNCTION CALL:",v1

print *,"FORTRAN: CHECK ASSIGNMENT:",v2

RETURN

end





C CODE:

#include

#include

#include time.h>

double mysecond_(double *val)

{

struct timeval tv;

gettimeofday(&tv, NULL);

*val = tv.tv_sec + tv.tv_usec / 1.e6;

printf("VALUE FROM C:%f ",*val);

}





RESULT OBTAINED:



[root@master tests]# ifort b2.f mysecond.o



[root@master tests]# ./a.out

VALUE FROM C:63937.754206

FORTRAN: AT FUNCTION CALL: 63937.7542060000

FORTRAN: CHECK ASSIGNMENT: 63937.7542060000

FORTRAN: MAIN CALL: 3.315236911126272E-018



[root@master tests]# ./a.out

VALUE FROM C:63945.945295

FORTRAN: AT FUNCTION CALL: 63945.9452950000

FORTRAN: CHECK ASSIGNMENT: 63945.9452950000

FORTRAN: MAIN CALL: 1.71327996253967



------------------------------------------------------------



The value from the C program is returned to the Fortran function successfully but not to the main program that is calling the function. What could be the problem?





Regards

Bala

0 Kudos
8 Replies
Steven_L_Intel1
Employee
603 Views
You have to declare second1 as double precision in the main program.
0 Kudos
Balamurugan_R_
Beginner
603 Views
Dear Sir,
Thank you! i have another query.
Is there any option with the compiler to declarea function withinthe code as external. The problem is that the external function namehas a conflict with the intrinsic function and the rule to benchmark is that the code shouldnot be changed.
is it possible?
Regards
Balamurugan.R
0 Kudos
Steven_L_Intel1
Employee
603 Views
The EXTERNAL statement declares a name as external. Which intrinsic function are you having a problem with?
0 Kudos
Balamurugan_R_
Beginner
602 Views
Dear Sir, The problem is that i have to benchmark a machine with Xeon DP on brandon board. Iam to use the linpack benchmark. For n=100 matrix i have to supply a external second function to time the loop execution time. I have written a gettimeofday() function with c and the object file of the c code is compiled with the fortran program using ifort to create the executable. In the fortran code the second function is not declared external. So, is there any way that i can call the external c function from the fortran code without changing the fortran code. Possibly a complier option or some addtions in c code Thank you Balamurugan.R
0 Kudos
bpichon
Beginner
602 Views
Try to use the Fortran intrinsics related to timing :
TIME_AND_DATE( ... ) for elapsed time
CPU_TIME ( ... ) for runnig time
At my opinion, it is more simple to use Fortran intrinsics to external C function to do the same thing !
0 Kudos
Steven_L_Intel1
Employee
602 Views
There is also SYSTEM_CLOCK as an intrinsic, which returns elapsed time.
0 Kudos
TimP
Honored Contributor III
602 Views
SYSTEM_CLOCK() in ifort has some interesting behaviors, depending on argument data type. It looks difficult to take advantage of these, while retaining some degree of portability to other compilers, which may have only a 32-bit (default integer) version.
0 Kudos
Steven_L_Intel1
Employee
603 Views
The argument kind just determines what the resolution is. If you use the arguments to get the count rate and count max, you can write portable code using it.
0 Kudos
Reply