473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Fortran-compiled DLLs in Python

Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O

h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance

Who can show us the Right way, riding the Python? THANKS!

stephen
Jul 18 '05 #1
7 4376
"byte biscuit" <st*************@casaccia.enea.it> writes:
Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O

h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance


Looking at your function description above, I would guess you have call
it in the following way (although the error you get is that c_long(40.0)
complains about the float argument):

dierr = c_int()
prop = (c_double * 16)() # create a 16 element double array

h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),
byref(dierr), prop)

Although I do know nothing about fortran (anymore).

Thomas
Jul 18 '05 #2
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which would seem to be the classical exception raised through a failed
function call...?

allthebest

luca (+stephen)


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Jul 18 '05 #3
"byte biscuit" <st*************@casaccia.enea.it> writes:
Hi there everybody!
The problem is the following:
we have a DLL (H2O.dll) - compiled in Visual Fortran - depending in turn on
another DLL.
H2O.dll contains only one (1) function, with a known name (WATER).
The WATER function is called with the following parameters:
- TT (datatype: double precision) [input]
- PP (datatype: double precision) [input]
- State (datatype: integer)
[input]
- DiERR (datatype: integer)
[output]
- SIG (datatype: double precision)
[output]
- Prop (datatype: double precision array[16 elements]) [output]

Obviously, we would like to make use of Python to handle the function and
pick out the results...
We thought of using the **ctypes** module to tackle the problem, but we have
stranded just about at the first attempt:
from ctypes import *
h2o = windll.H2O
h2o.WATER(c_long(40.0),c_long(1.0),c_int(2),c_int( dierr),c_int(Sig),c_long*1
6(prop))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int expected instead of float instance

Who can show us the Right way, riding the Python? THANKS!

stephen
--
http://mail.python.org/mailman/listinfo/python-list
"Luca Simonetti" <si********@tiscali.it> writes:
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which would seem to be the classical exception raised through a failed
function call...?


This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas
Jul 18 '05 #4
On Thu, 27 May 2004 13:36:26 +0000 (UTC), "Luca Simonetti"
<si********@tiscali.it> declaimed the following in comp.lang.python:
Hi Thomas!
an answer from the true creator of the ctypes module - nice!
anyway, we tried implementing your solution and got the following
Traceback Error:
h2o.WATER(c_double(40.0), c_double(1.0), c_int(2),byref(dierr), byref(sig), prop)

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation


C passes by value, and to get a "modifiable" argument, the
programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #5
I feel we're getting there...

recapitulating:
from ctypes import *
h2o = windll.H2O # loads DLL in python
tt=c_double(20.) # tt, pp and state are inputs, given values in C datatypes pp=c_double(1.0)
state=c_int(1)
dierr = c_int() #dierr, sig and prop are outputs, initialised in C datatypes sig = c_double()
prop = (c_double * 16)() # create a 16 element double array
access violation error:
h2o.WATER(tt, pp, state, byref(dierr), byref(sig), prop) Traceback (most recent call last):
File "<interactive input>", line 1, in ?
WindowsError: exception: access violation

which in fact - Dennis - is caused by non-referencing, because:
h2o.WATER(byref(tt), byref(pp), byref(state), byref(dierr), byref(sig),
prop)
returns without error.

It is interesting (for me, that is) to note that prop in fact does NOT need
referencing, whereas all other variables (input&output) DO. But, the
returned answer is:

0

which shouldn't be of course............... so there does seem to be a
problem in the definition/passing of the prop parameter, I would say?

stephen
"Thomas Heller" <th*****@python.net> ha scritto nel messaggio
news:ma*************************************@pytho n.org...
This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas

"Dennis Lee Bieber" <wl*****@ix.netcom.com> ha scritto nel messaggio
news:p0********************************@4ax.com...
C passes by value, and to get a "modifiable" argument, the programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #6
On Fri, 28 May 2004 12:06:33 +0200, "byte biscuit"
<st*************@casaccia.enea.it> declaimed the following in
comp.lang.python:

It is interesting (for me, that is) to note that prop in fact does NOT need
referencing, whereas all other variables (input&output) DO. But, the
returned answer is:
Okay, my "all" was a bit over done... Since C normally passes
(as a value) the address of the first element, that already matched what
FORTRAN expected.
0

which shouldn't be of course............... so there does seem to be a
problem in the definition/passing of the prop parameter, I would say?
I suspect the declaration...

code> prop = (c_double * 16)()

I've not used ctypes, so I don't fully understand the above, but
it looks very much like it is making 16 copies of the c_double FUNCTION,
and then invoking this "array" with no arguments. The closest thing I
find in the ctypes tutorial page implies you don't want the trailing ()

stephen
"Thomas Heller" <th*****@python.net> ha scritto nel messaggio
news:ma*************************************@pytho n.org...

This is the classical GP fault, which would normally crash a program.
ctypes catches it, but doesn't give more information (in current CVS, it
would have informed you about which memory location could not be written
or read, but that would probably not help you very much).
- Prop (datatype: double precision array[16 elements]) [output]


I do not know how fortran passes this parameter, in the example code I
posted I assumed that the caller allocates an array, passes a pointer to
it to fortran, and the fortran code fills in the elements.

Do you have C sample code that works?

Thomas

"Dennis Lee Bieber" <wl*****@ix.netcom.com> ha scritto nel messaggio
news:p0********************************@4ax.com...
C passes by value, and to get a "modifiable" argument, the

programmer has to explicitly pass an address (as the value) and then,
also, explicitly dereference through the value.

Traditionally, FORTRAN passes ALL arguments by address, not
value. This may vary a bit in more modern implementations -- character
strings may pass a "string descriptor", which is a structure containing
the address of the string (as normal) along with an added word
containing the size of the space allocated for the string.

I suspect you need to use byref() on all of the arguments.


-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #7

Did you consider f2py? If you do not have the source you will
need to write the interface by hand, something you need to do for ctypes
anyway.

http://cens.ioc.ee/projects/f2py2e/

Sincerely yours
Pierre Schnizer

--
Remove nospam from the adress for direct replies
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: NM | last post by:
Hi all I am trying to link C++ and Fortran.I am not very familiar with Fortran. I have been successful in linking C++ and fortran using g++ and ifc for simple program. So adding _ to at the end of...
4
by: NM | last post by:
Hello All I am writing some progam that involves both C++ and Fortran. Some of the existing code is in Fortran. The main program will be in C++ and it will call some Fortran subroutine. All the...
6
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow...
1
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of...
2
by: NM | last post by:
Hello all, I am supposed to do some mixed programming with c++ and fortran. I was succeeful in exchanging the 2D arrays from fortran to c++ and the other way, but was unable to that same with...
11
by: John Smith | last post by:
Ok, I know this is a common topic, but I didn't find any post that solves my current problem. I'm using Microsoft Visual C++ 6.0 (SP5), and Intel Fortran 7.1 Compiler for Windows. I'm trying to...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
8
by: Luna Moon | last post by:
Hi all, As a C/C++ programmer, there are a few reasons to use Fortran: (1) Fortran is very similar to Matlab and easy to port; (2) Fortran has support of complex numbers and vectorized numbers...
4
by: sara_patty | last post by:
fortran command for c command strcopy(infile, argv)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.