Silverfrost Forums

Welcome to our forums

JAVA, Fortran and c

13 Jul 2008 1:08 #3446

A colleague has produced a geochemical data analysis package, which has been developed in Java. He can provide a link / hook to my multivariate software for my in-house use, but the link will be via C.

I know nothing about JAVA, and not a lot about c.

Can you access Salford Fortran directly from JAVA?

I've had a quick look at the Salford manuals, but couldn't find any explicit examples of interfacing Fortran and c. I've reached that age in (semi-)retirement where cutting-and-pasting is much more efficient than starting from scratch.

So I'm pasting a simple example, in the hope that there is some kind soul out there who can spend a few minutes making the necessary changes to show me how to get the code to run.

  program test_wrapper

  dimension  xxx(10), yyy(10)
  

  npt = 10

  do ip = 1, npt

     xxx(ip) = ip
     
     yyy(ip) = 1.

  end do
  

  xv1 = 20.
  yv1 = 30.

  itt = 6

  call wrap_c_to_Fortran  ( itt, npt, xxx, yyy, xpr,
 1                                    xv1, yv1, pv1 )
  

  stop
  end

c I would like to be able to replace this subroutine c by C code (see my first try below)

c and make whatever changes are necessary to the remaining Fortran c and modified c code to have the example compile and run

  subroutine wrap_c_to_Fortran ( itt, npt, xxx, yyy, xpr,
 1                                         xv1, yv1, pv1 )

  dimension  xxx(*), yyy(*)
  
  

c write data

  write (itt,'(/ '' XXX '')' )
  
  write (itt,'( 10g12.4 )') (xxx(ip),ip=1,npt)

  do ip = 1, npt
  
     write (itt,'(/ '' XXX(i) '', g12.4 )') xxx(ip)

  end do
  

  write (itt,'(/ '' YYY '')' )
  
  write (itt,'( 10g12.4 )') (yyy(ip),ip=1,npt)

  do ip = 1, npt
  
     write (itt,'(/ '' YYY(i) '', g12.4 )') yyy(ip)

  end do

  write (itt,'(/ '' XV1 '', g12.4 )') xv1

  write (itt,'(/ '' YV1 '', g12.4 )') yv1

  

  call calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 )
  

  write (itt,'(/ '' XXX * YYY = '', g12.4 )') xpr

  write (itt,'(/ '' XV1 * YV1 = '', g12.4 )') pv1


  return
  end


  

  subroutine calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 )
  

  dimension  xxx(*), yyy(*)
  

  xpr = 0.
  
  do ip = 1, npt
    
     xpr = xpr + xxx(ip) * yyy(ip)

  end do
  
  pv1 = xv1 * yv1
     

  return
  end


  

#include <stdio.h>

extern int calc_prod ( long, long, double *, double *, double, double, double, double );

int wrap_c_to_Fortran ( long itt, long npt, // number of observations double *xxx, double *yyy, double xpr, double xv1, double yv1, double pv1 )

// xxx(ip) = ip yyy(ip) = 1. xpr = xpr + xxx(ip) * yyy(ip); ip = 1, npt

// xv1 = 20. yv1 = 30. pv1 = xv1 * yv1

{ short ip;

    long     npv1;
    

    for(ip=1;ip<=npt;ip++) 
       printf( '%12.4lf ', xxx[ip] );

    for(ip=1;ip<=npt;ip++)
       printf( '%12.4lf ', yyy[ip] );

    printf( '%12.4lf ', xv1 );

    printf( '%12.4lf ', yv1 );


    calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 );
  

    printf( '%12.4lf ', xpr );

    printf( '%12.4lf ', pv1 );

// sprintf(szBuff, '\n\nC V means - C V %3ld vs C V %ld', ncvY, ncvX); // write (itt,'(/ '' XXX * YYY = '', g12.4 )') xpr // write (itt,'(/ '' XV1 * YV1 = '', g12.4 )') pv1

    npv1 = pv1;
    
    return npv1;

}

13 Jul 2008 5:06 #3447

Java and FTN95 cannot directly communicate.

To provide a link between Fortran and C you will need to use C_EXTERNAL statements in the Fortran and export your C routines in a specific way. Its not so tricky and the manual has info on this at:

Win32 Platform->Mixed Language Programming->Calling C/C++ from FTN95

Basically you need to add extern 'C' to your C routine definitions and create corresponding C_EXTERNAL definitions in your Fortran. The C routines will need exporting from the DLL (depends on the C compiler you are using exactly how this is done). You can then link directly with the DLL using SLINK (no need for lib files).

Calling Java from C is more of an issue and requires the use of the JNI, the Java Native Interface. There is too much to discuss here for a quick reply, but the Java docs provide information on how to use JNI and hosting a JVM etc.

14 Jul 2008 4:45 #3449

Hi Andrew

Thanks for the prompt response.

Would you be able to make the modifications you list to the example code I included, as I much prefer cutting and pasting from examples these days.

Cheers

Norm

Please login to reply.