Hi,
I'm new to Fortran and require some help understanding the capabilities
of Fortran when it comes to calling C/C++ functions. Specifically I
would like to make use of C's memory manipulation functions, in
partucular realloc, memcpy and memmove.
I have writen some F95 code which calls a C function, the function
allocated some mem and populates an array. However the memory is not
availabe to me back in teh Fortran calling function. Any attemp to
access the data result in a seg fault.
Any advice on how to do this would be welcome!
Cheers,
Jon
|
|
0
|
|
|
|
Reply
|
jon.gill (11)
|
3/24/2005 12:41:41 PM |
|
Calling C from Fortran is often discussed here. Can you post a small
code example illustrating your problem and specify the compilers and
operating system?
May I ask why you do not allocate the memory in the Fortran code,
perhaps using ALLOCATABLE arrays?
|
|
0
|
|
|
|
Reply
|
beliavsky (2207)
|
3/24/2005 1:39:25 PM
|
|
"jon.gill@metoffice.gov.uk" wrote:
> I'm new to Fortran and require some help understanding the capabilities
> of Fortran when it comes to calling C/C++ functions. Specifically I
> would like to make use of C's memory manipulation functions, in
> partucular realloc, memcpy and memmove.
If that is all you wish to do, why bother with C? It is trivial to
write ones own 'realloc' routine in F95. Use F90 pointers, the ALLOCATE
and DEALLOCATE statements. The only 'hard' part is deciding how to move
the data when multi-dimensional arrays are reallocated. (E.g., Does one
treat the existing data as a submatrix? Or move it as a contiguous
chunk - like C's realloc would?)
For moving blocks of memory around, without regard to the rank of the
array, try the BLAS SCOPY/DCOPY/CCOPY/ZCOPY routines.
> I have writen some F95 code which calls a C function, the function
> allocated some mem and populates an array. However the memory is not
> availabe to me back in teh Fortran calling function. Any attemp to
> access the data result in a seg fault.
No doubt. If one really really needs to use non-Standard allocation
technique (e.g., for mmaping files), there is no way in F95 to build
up a F95 POINTER from an externally provided address. And a F95
POINTER maintains quite a bit more information than a simple address.
However there are a couple of ways of doing it, but details depend on
which compiler(s) you are using. ("Cray pointers" are a huge help - if your
compiler supports them.)
Eventually F2003 will be implemented and become widespread. Then
the new C Interop features will make this a bit easier. But I don't
know of any compiler which implements F2003 yet, so don't hold your
breath.
Walt
-...-
Walt Spector
(w6ws at earthlink dot net)
|
|
0
|
|
|
|
Reply
|
w6ws_xthisoutx (399)
|
3/24/2005 2:09:26 PM
|
|
Walter Spector wrote:
> "jon.gill@metoffice.gov.uk" wrote:
> > I'm new to Fortran and require some help understanding the
capabilities
> > of Fortran when it comes to calling C/C++ functions. Specifically
I
> > would like to make use of C's memory manipulation functions, in
> > partucular realloc, memcpy and memmove.
>
> If that is all you wish to do, why bother with C? It is trivial to
> write ones own 'realloc' routine in F95. Use F90 pointers, the
ALLOCATE
> and DEALLOCATE statements. The only 'hard' part is deciding how to
move
> the data when multi-dimensional arrays are reallocated. (E.g., Does
one
> treat the existing data as a submatrix? Or move it as a contiguous
> chunk - like C's realloc would?)
>
> For moving blocks of memory around, without regard to the rank of the
> array, try the BLAS SCOPY/DCOPY/CCOPY/ZCOPY routines.
>
> > I have writen some F95 code which calls a C function, the function
> > allocated some mem and populates an array. However the memory is
not
> > availabe to me back in teh Fortran calling function. Any attemp to
> > access the data result in a seg fault.
>
> No doubt. If one really really needs to use non-Standard allocation
> technique (e.g., for mmaping files), there is no way in F95 to build
> up a F95 POINTER from an externally provided address. And a F95
> POINTER maintains quite a bit more information than a simple address.
> However there are a couple of ways of doing it, but details depend on
> which compiler(s) you are using. ("Cray pointers" are a huge help -
if your
> compiler supports them.)
>
> Eventually F2003 will be implemented and become widespread. Then
> the new C Interop features will make this a bit easier. But I don't
> know of any compiler which implements F2003 yet, so don't hold your
> breath.
On this subject, is the F2k3 C_PTR just another name for a cray pointer
in most cases (same functionality, different syntax)?
>
> Walt
> -...-
> Walt Spector
> (w6ws at earthlink dot net)
|
|
0
|
|
|
|
Reply
|
gary.l.scott (237)
|
3/24/2005 6:53:38 PM
|
|
In article <1111690418.840398.58250@o13g2000cwo.googlegroups.com>,
gary.l.scott@lmco.com wrote:
> On this subject, is the F2k3 C_PTR just another name for a cray pointer
> in most cases (same functionality, different syntax)?
I wouldn't call the functionality of the F2k3 C_PTR quite the same as
that of Cray pointers, but there certainly are significant overlaps -
significant enough that I personally consider the C_PTR feature to be a
good candidate for a standardized replacement for Cray pointers. If
anything, I'd say that C_PTR has more functionality than Cray pointers
(and a syntax that is immeasurably better - in my own very personal
opinion, the syntax of Cray pointers is a counter-intuitive
abomination). The C_PTR feature wasn't originally explicitly designed as
a Cray pointer replacement, but it became evident to me (and some
others) fairly early on that it could also serve that purpose, even in
code that had nothing to do with C per se.
--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
|
|
0
|
|
|
|
Reply
|
nospam47 (9742)
|
3/24/2005 8:11:18 PM
|
|
Richard E Maine wrote:
>
> In article <1111690418.840398.58250@o13g2000cwo.googlegroups.com>,
> gary.l.scott@lmco.com wrote:
>
> > On this subject, is the F2k3 C_PTR just another name for a cray pointer
> > in most cases (same functionality, different syntax)?
>
> .... The C_PTR feature wasn't originally explicitly designed as
> a Cray pointer replacement, but it became evident to me (and some
> others) fairly early on that it could also serve that purpose, even in
> code that had nothing to do with C per se.
I agree - to a large extent.
However Cray Pointers, as traditionally implemented by Cray at least,
also infer that no aliasing is occuring. This means that the array
accesses are optimizable in the same manner that normal non-pointer
arrays are. So there can be performance implications compared with
F90 POINTERs and, I presume, F2003 C_PTRs.
Guess we need a RESTRICT attribute...
Walt
-...-
Walt Spector
(w6ws at earthlink dot net)
|
|
0
|
|
|
|
Reply
|
w6ws_xthisoutx (399)
|
3/25/2005 5:39:37 AM
|
|
|
5 Replies
33 Views
(page loaded in 0.076 seconds)
Similiar Articles: Calling DLL subroutine from C++ - comp.lang.fortranThe ALIAS attribute on the Fortran side avoids the stdcall name mangling. ... Creating and Calling C Function DLL from .NET - CodeProject Explain how to call C ... Calling MATLAB from fortran - comp.soft-sys.matlabCalling dll from Matlab - comp.soft-sys.matlab Use a C# dll from Java - comp.lang.java.help Basic Dll From C | eHow.com How to Call Visual ... Calling MATLAB from fortran ... Should I use C++ or Java for Numeric Intensive Calculations - comp ...Calling C++/java routines from Fortran 1) IMHO won't make any sense 2) May get very complicated But feel free to call C routines from Fortran which should not cause any ... problem with mixed c and fortran code - comp.lang.fortran ...... fcall_1_(unsigned char *); int main(void) { unsigned char c1,c2,*cp1,*cp2; c1='F';cp1=&c1; c2='A';cp2=&c2; printf("Printing from c before fortran call ... GUI for Fortran programs - comp.lang.fortranThis is usually done with a C interface, so if this would be possible to do with Fortran (as opposed to calling C functions from Fortran), then it might work. using gfortran to call windows api functions - comp.lang.fortran ...Sockets in gfortran? - comp.lang.fortran... char * msg, int n); /* Wrapper to call fortran ... in C, and calling the C socket functions from Fortran turns ... Too many arguments in call to 'shape' - comp.lang.fortran ...C:\gfortran\opengl\gridview>type bug1.f90 program bug1 use ISO ... input/output arguments - comp.soft-sys.matlab Too many arguments in call to 'shape' - comp.lang.fortran ..... libtiff - comp.lang.fortranI'm trying to link libtiff.lib (compiled from C code) to Fortran, using the Intel compilers on Windows. I have the calling of a C procedure from Fo... f95 to windows dll - comp.lang.fortranAlso both sides use call by name. 2. arguments must match in position, type and ... comp.os.ms-windows.programmer ... f95 to windows dll - comp.lang.fortran Excell calling ... Calling Matlab engine from C++ - comp.soft-sys.matlabcomp.soft-sys.matlab - page 2 Calling Matlab from Java #2 0 3 (10/14/2003 1:15:08 ... how to use the MATLAB engine library to call MATLAB software from C/C++ and Fortran ... Calling C and Fortran from R - School of Statistics : University ...Calling C and Fortran from R Contents. Why Call C or Fortran from R? C functions and Fortran subroutines callable from R; Compiling and Dynamic Loading Tutorial: Using C/C++ and Fortran togetherUsing C/C++ and Fortran together: This tutorial covers mixing C/C++ and FORTRAN together, allowing C/C++ to call FORTRAN functions and FORTRAN to call C/C++ functions. 7/25/2012 3:03:15 AM
|