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

Calling fortran code from C

oguren
Beginner
1,195 Views
Hi,
I need to compute hankel functions with complex argumant in c code. I try to use amos' codes written in fortran. I get some errors. Could you help me please?

$ ifort -r8 -c *.f

$ icc -c testhankel.c

$ icc -o hank testhankel.o zbesh.o zbesj.o d1mach.o i1mach.o zabs_org.o zuoik.o zbknu.o zacon.o zbunk.o zmlt.o zunk1.o zunk2.o zs1s2.o zseri.o zshch.o zsqrt_org.o zdiv.o zuchk.o dgamln.o zlog_org.o zexp_org.o zairy.o zunhj.o zunik.o xerror.o zkscl.o zbinu.o zacai.o zasyi.o zmlri.o zwrsk.o zbuni.o zrati.o zuni1.o zuni2.o -lm

d1mach.o: In function `d1mach_':
d1mach.f:(.text+0xa2): undefined reference to `for_write_seq_lis'
d1mach.f:(.text+0xbb): undefined reference to `for_write_seq_lis_xmit'
d1mach.f:(.text+0xe2): undefined reference to `for_write_seq_lis_xmit'
d1mach.f:(.text+0xff): undefined reference to `for_stop_core'
d1mach.f:(.text+0x12a): undefined reference to `for_stop_core'
i1mach.o: In function `i1mach_':
i1mach.f:(.text+0x4e): undefined reference to `for_write_seq_fmt'
i1mach.f:(.text+0x6b): undefined reference to `for_stop_core'
xerror.o: In function `xerror_':
xerror.f:(.text+0x72): undefined reference to `for_write_seq_fmt'
xerror.f:(.text+0xd2): undefined reference to `for_write_seq_lis'
xerror.f:(.text+0x116): undefined reference to `for_write_seq_fmt'

0 Kudos
6 Replies
TimP
Honored Contributor III
1,195 Views
You have Fortran WRITE in those subroutines. You will need to include the Fortran run-time libraries in the link, or comment out those WRITEs. Should you hit a case which activates those WRITEs, you have a potential conflict if they use the same output stream as your C code.
0 Kudos
oguren
Beginner
1,195 Views

Thanks for your reply,

I include run-time libraries. It runs but gives wrong result. I'm confusedI write a test program in fortran named test.f and run as

$ifort -r8 -i4 -c test.f zbesh.f zbesj.f d1mach.f i1mach.f zabs_org.f zuoik.f zbknu.f zacon.f zbunk.f zmlt.f zunk1.f zunk2.f zs1s2.f zseri.f zshch.f zsqrt_org.f zdiv.f zuchk.f dgamln.f zlog_org.f zexp_org.f zairy.f zunhj.f zunik.f xerror.f zkscl.f zbinu.f zacai.f zasyi.f zmlri.f zwrsk.f zbuni.f zrati.f zuni1.f zuni2.f

$ifort -o test test.o zbesh.o zbesj.o d1mach.o i1mach.o zabs_org.o zuoik.o zbknu.o zacon.o zbunk.o zmlt.o zunk1.o zunk2.o zs1s2.o zseri.o zshch.o zsqrt_org.o zdiv.o zuchk.o dgamln.o zlog_org.o zexp_org.o zairy.o zunhj.o zunik.o xerror.o zkscl.o zbinu.o zacai.o zasyi.o zmlri.o zwrsk.o zbuni.o zrati.o zuni1.o zuni2.o

$./test

It gives totaly true result as MATLAB besselh function

But when I call same programs from C routine , it gives completely wrong results. my commands like this:

$ icc -c testhankel.c

$ ifort -r8 -i4 -c zbesh.f zbesj.f d1mach.f i1mach.f zabs_org.f zuoik.f zbknu.f zacon.f zbunk.f zmlt.f zunk1.f zunk2.f zs1s2.f zseri.f zshch.f zsqrt_org.f zdiv.f zuchk.f dgamln.f zlog_org.f zexp_org.f zairy.f zunhj.f zunik.f xerror.f zkscl.f zbinu.f zacai.f zasyi.f zmlri.f zwrsk.f zbuni.f zrati.f zuni1.f zuni2.f

$ icc -o hank testhankel.o zbesh.o zbesj.o d1mach.o i1mach.o zabs_org.o zuoik.o zbknu.o zacon.o zbunk.o zmlt.o zunk1.o zunk2.o zs1s2.o zseri.o zshch.o zsqrt_org.o zdiv.o zuchk.o dgamln.o zlog_org.o zexp_org.o zairy.o zunhj.o zunik.o xerror.o zkscl.o zbinu.o zacai.o zasyi.o zmlri.o zwrsk.o zbuni.o zrati.o zuni1.o zuni2.o -L/RS/progs/intel/Compiler/11.1/056/lib/intel64 -lifcore -limf

0 Kudos
mecej4
Honored Contributor III
1,195 Views

You did not show any of the code in testhankel.c that gave you "wrong" results, so I content myself with giving you a C sample code that gives the same results as Fortran. I downloaded the AMOSLIB sources from Netlib, made the necessary configuration changes to d1mach.f and i1mach.f, and ran the commands

[bash]ifort -w -c *.f
gcc -c testhankel.c
ifort -nofor-main *.o
./a.out [/bash] with testhankel.c containing

[bash]#include <stdio.h>
extern void zbesh_(double *,double *,double *,int *,int *, int *,double *,double*,int *,int *);
int main(int argc,char *argv[]){
int i,ierr,nz,m=1,n=3,kode=2;
double zr=7e-2,zi=5e-2,cyr[3],cyi[3],fnu=0.0;
zbesh_(&zr,&zi,&fnu,&kode,&m,&n,cyr,cyi,&nz,&ierr); ...
[/bash] and obtained the results

[bash] 5.0982e-01 -1.7590e+00
-4.8843e+00 -6.0601e+00
-1.7481e+02 -4.6887e+01 [/bash] which agree with the output

[bash] nZ = 0 IERR = 0
0.5098e+00 -0.1759e+01
-0.4884e+01 -0.6060e+01
-0.1748e+03 -0.4689e+02 [/bash]
from the equivalent Fortran driver:

[bash] PROGRAM TZBESH
IMPLICIT NONE
DOUBLE PRECISION ZR,ZI,FNU,CYR(3),CYI(3)
integer m,n,nz,ierr,kode,i
ZR=7d-2
ZI=5d-2
FNU=0d0
M=1
N=3
KODE=2
CALL ZBESH(ZR, ZI, FNU, KODE, M, N, CYR, CYI, NZ, IERR)
write(*,*)' nZ = ',nz,' IERR = ',ierr
write(*,'(1x,2E12.4)')(CYR(i),CYI(i),i=1,3)
stop
end [/bash]

0 Kudos
Muhammet_M_
Beginner
1,195 Views

Hello, I need help with hankel function.

I downloaded amoslib from netlib (zbesh.f and dependencies)

ifort -w -c *.f
gcc -c -lm wire.c
ifort -nofor-main *.o

then ./a.out

gives me very different answer. I am suspecting

that the problem is in i1mach fortran routine. I did not customize d1mach because

i read that it gets values from i1mach.

wire.c

extern  void zbesh_ (double*, double*, double*, int*, int*, int*, double*, dou
ble*, int*, int*);

void main (void)
{
  double zr = 0.07;   double zi = 0.05;   double fnu;
  int kode, M, N, nz, ierr, i;
  double cyi[2], cyr[2];

  fnu = 0.0;   kode = 1;   M = 1;   N = 1;
 
  zbesh_(&zr, &zi, &fnu, &kode, &M, &N, cyr, cyi, &nz, &ierr);

  for (i=0; i<2; i++)
    printf("real %e\t imag %e \n", cyr, cyi);

I have 64 bit laptop (cpu is  Intel(R) Core(TM) i5 CPU       M 450  @ 2.40GHz)

and my custom i1mach.f file below

      DATA IMACH( 1) /          5 /
      DATA IMACH( 2) /          6 /
      DATA IMACH( 3) /          7 /
      DATA IMACH( 4) /          0 /
      DATA IMACH( 5) /         8 /
      DATA IMACH( 6) /          8 /
      DATA IMACH( 7) /          2 /
      DATA IMACH( 8) /         31 /
      DATA IMACH( 9) / 2147483647 /
      DATA IMACH(10) /          2 /
      DATA IMACH(11) /         24 /
      DATA IMACH(12) /       -126 /
      DATA IMACH(13) /        127 /
      DATA IMACH(14) /         53 /
      DATA IMACH(15) /      -1022 /
      DATA IMACH(16) /       1023 /

what am I missing here?

thanks in advance

0 Kudos
mecej4
Honored Contributor III
1,195 Views

Muhammet M. wrote:

gives me very different answer. I am suspecting that the problem is in i1mach fortran routine. I did not customize d1mach because I read that it gets values from i1mach.

"Different answer" compared to what? For your argument values, zbesh gives Hν(1)(z) = 0.600799   - 1.635214*I, which agrees with the result from Maple.

You do need to remove comment marks from the lines of d1mach.f that correspond to "IBM PC", since the routine d1mach gets called soon after entry into zbesh, and the version of d1mach provided in the Netlib amos directory does not call i1mach().

Comments to IDZ forum managers: The inline code in my post of 05/16/2010 above was shown correctly in the old version of the forum. Now that this thread has been reactivated, I see that the new forum software has swallowed C include-file names, removed line endings and, in general, made the code unusable. I have tried to edit and restore the code to the point where it is readable in the new forum software, but I have not run the inline code through a compiler to verify the edits.

0 Kudos
Muhammet_M_
Beginner
1,195 Views

Thanks mecej4

It worked after I uncommented the IBM PC constants.

0 Kudos
Reply