473,386 Members | 1,812 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,386 software developers and data experts.

C++ Fortran mixed programming

NM
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 memory allocation has to be done in
C++.

For arrays of structures and arrays (including multi-dimensional array) of
primitive data types declared in Fortran, I know the corresponding data
structures in C++. So I can declare those data structures in C++, allocate
them, and pass them as argument to Fortran subroutine. But when a member of
the structure is a pointer (declared in Fortran), I cannot figure out what
should be the corresponding data structures in C++. I have tried pointer to
the same type in C++, but it do not work and the Fortran subroutine will get
a segmentation fault when accessing the pointer.

I cannot change the data structures defined in Fortran. What I can do is to
use appropiate data structures in C++ and pass it to the Fortran subroutine
as argument.
Can anyone tell me what should be the data structure defined in C++ that
corresponds to the Fortran structure "node". Thanks in advance.

Here is my example fortran code

subroutine test_fortran_sub(NODES,MAXNODS)
c
implicit none

type node
integer :: order
double precision,dimension(:),pointer :: zdofs
end type node

integer :: MAXNODS
type(node) :: NODES(MAXNODS)

integer :: i,j,k

write(*,*) 'MAXNODS = ',MAXNODS
do i=1,MAXNODS
NODES(i)%order = i
do j = 1,5
NODES(i)%zdofs(j) = i+j
enddo
enddo

return
end
and Here is the example C++ code

#include <iostream>
using namespace std;

class node_struct {
public:
int order;
double *zdofs;
};

typedef node_struct *node;

extern "C" {
void test_fortran_sub_(node NODES,int *MAXNODS);
}

int main(void)
{
int MAXNODS = 1;
node NODES;

NODES = new node_struct[MAXNODS];

for (int i = 0; i < MAXNODS; i++ ) {
NODES[i].zdofs = new double[5];
}

test_fortran_sub_(NODES,&MAXNODS);

for (int i = 0; i < MAXNODS; i++) {
cout << "NODES[" << i << "].order = " << NODES[i].order << endl;
for (int j = 0; j < 5; j++)
cout << "NODES[" << i << "].zdofs["<<j<<"] = " << NODES[i].zdofs[j] <<
endl;
}
return 0;
}
Jul 22 '05 #1
4 2825
"NM" <nm@nm.com> writes:
But when a member of
the structure is a pointer (declared in Fortran), I cannot figure out what
should be the corresponding data structures in C++. I have tried pointer to
the same type in C++,
There is no direct correspondence between C/C++ pointers and Fortran ones.
There are vendor-specific solutions, but nothing portable until the C
interop stuff of Fortran 2003 (which some vendors are already
incorporating into their f95 compilers).

Except for scalars (which isn't what you have), it is pretty much guaranteed
not to look like a pointer to the corresponding type in C/C++. That's
because the Fortran pointer needs to store extra information about the
bounds; that information has to go somewhere, and the C/C++ pointer
doesn't have room for it.
Can anyone tell me what should be the data structure defined in C++ that
corresponds to the Fortran structure "node". Thanks in advance.


See your vendor's manuals for a vendor-specific answer. It *DOES*
vary among vendors. If your vendor doesn't document it (some do),
you can probably reverse engineer it, but be aware that the answer
will only be valid for that particular vendor.

--
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
Jul 22 '05 #2
NM wrote:
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 memory allocation has to be done in C++.

For arrays of structures and arrays (including multi-dimensional array)
of primitive data types declared in Fortran,
I know [that] the corresponding data structures in C++.
So I can declare those data structures in C++, allocate them,
and pass them as argument to Fortran subroutine.
But when a member of the structure is a pointer (declared in Fortran),
I cannot figure out what should be the corresponding data structures in C++.
I have tried pointer to the same type in C++,
but it do not work and the Fortran subroutine
will get a segmentation fault when accessing the pointer. I cannot change the data structures defined in Fortran.
What I can do is to use appropiate data structures in C++
and pass it to the Fortran subroutine as argument.
Can anyone tell me what should be the data structure defined in C++
that corresponds to the Fortran structure "node".

Here is my example fortran code

subroutine test_fortran_sub(NODES, MAXNODS)
c
implicit none

type node
integer:: order
double precision, dimension(:), pointer:: zdofs
end type node

integer:: MAXNODS
type (node):: NODES(MAXNODS)

integer:: i, j, k

write(*, *) 'MAXNODS = ', MAXNODS
do i = 1, MAXNODS
NODES(i)%order = i
do j = 1, 5
NODES(i)%zdofs(j) = i + j
enddo
enddo

return
end
and Here is the example C++ code

#include <iostream>
// using namespace std; #include <f77_adapter.h>
#include <f90_adapter.h>
class node_struct {
public:
//int order;
//double *zdofs; f77_integer order;
f90_1DPointer data; };

typedef node_struct *node;

extern "C" {
f77_subroutine test_fortran_sub_(node, f77_integer*); f77_subroutine initializenode(node, f77_integer*); }

int main(void) {
using namespace std;
int MAXNODS = 1;
node* NODES;

NODES = new node_struct[MAXNODS];

for (int i = 0; i < MAXNODS; ++i) {
//NODES[i].zdofs = new double[5]; const int size = 5;
f77_initializenode(&(NODE[i]), &size);
// use Fortran 90 to allocate storage }

test_fortran_sub_(NODES, &MAXNODS);

for (int i = 0; i < MAXNODS; ++i) {
cout << "NODES[" << i << "].order = " << NODES[i].order << endl; double* zdofs
= (double*)f90_PointerPointer(&(NODES[i].data)); for (int j = 0; j < 5; ++j) {
cout << "NODES[" << i << "].zdofs[" << j << "] = "
<< NODES[i].zdofs[j] << endl; } }
return 0;
}


The f77_adapter.h and f90_adapter.h header files are platform dependent.
Tell me which machine architecture, operating system and Fortran 90
compiler you are using and I'll try to sent you the right ones.
Jul 22 '05 #3
In comp.lang.fortran NM <nm@nm.com> wrote:
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 memory allocation has to be done in
C++. For arrays of structures and arrays (including multi-dimensional array) of
primitive data types declared in Fortran, I know the corresponding data
structures in C++.
Even that is compiler dependent (until the C interop features of
F2003 become available). You may also have more trouble mixing
Fortran and C++ than Fortran and C. Both C++ and Fortran need to
do some serious initialization at startup before transferring
control to your C++ main(). Will the Fortran runtime be initialized
properly? Maybe, but that's another set of system dependencies for
you to worry about.

[ ... ] But when a member of
the structure is a pointer (declared in Fortran), I cannot figure out what
should be the corresponding data structures in C++.


That's even less portable. Fortran pointers don't correspond to C++
pointers in any simple way.

If I were you, I would
1) write accessors in Fortran, callable from C,
and declare them as "extern C" on the C++ side;
2) do all the I/O on the C++ side to reduce the risk initialization
conflicts in the two runtime environments.
--
pa at panix dot com
Jul 22 '05 #4

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

Similar topics

44
by: Carl | last post by:
"Nine Language Performance Round-up: Benchmarking Math & File I/O" http://www.osnews.com/story.php?news_id=5602 I think this is an unfair comparison! I wouldn't dream of developing a numerical...
15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
7
by: Anonymous | last post by:
I have a mixed-language pgm (Fortran and C++) which needs to pass a few hundred values from C++ to Fortran. One way to do this is to have a Fortran module and a C++ structure (or class) with an...
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...
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...
3
by: Guch | last post by:
I have to do some job about image processing. I have chosen Blitz++ as my matrix manipulation library. But it is weak in numerical calculation. So I want to combine C++ and Fortran. C++ does the...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
1
by: Pawel_Iks | last post by:
Suppose I have lib.o file with fortran functions (I know their names and the lists of them arguments). How to write c++ program, which use one of these functions, for example I want to use fortran...
4
by: rudra | last post by:
dear friends, i am new to C/C++ and i need to call ranlux routine from my fortran code...here is a toy fortran code, which is calling the C++ program that uses ranlux(its actually C code given in...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.