soccer jersey forums.silverfrost.com :: View topic - JAVA, Fortran and c
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

JAVA, Fortran and c

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Norm.Campbell



Joined: 31 Aug 2007
Posts: 66

PostPosted: Sun Jul 13, 2008 2:08 am    Post subject: JAVA, Fortran and c Reply with quote

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;

}
Back to top
View user's profile Send private message
Andrew



Joined: 09 Sep 2004
Posts: 232
Location: Frankfurt, Germany

PostPosted: Sun Jul 13, 2008 6:06 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Norm.Campbell



Joined: 31 Aug 2007
Posts: 66

PostPosted: Mon Jul 14, 2008 5:45 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group