473,574 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing a 2 dimensional array from c++ to fortran

Sam

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 the array in fortran and then accessing it in my c++ code.

Say in my c++ code I have;

extern "C" { void foo_(float **, int &, int &); }

int main()
{
............
..............
int rows, columns;
float **array2d; // Now i need to pass this to the fortran code, the
allocation of the array and
// and filling it is done in the fortran code.
// How do i pass this to the fortran subroutine now, i did it this way

foo_(array2d, rows, columns);

// One more question, do i need to do the allocation and stuff here
again
// Do some stuff to manipulate with the array2d

return 0;

}

and my Fortran routine:

subroutine foo(arr,rows,co lumns)
integer rows, columns
real*8 ,allocatable:: arr (:,:)
! do some stuff to allocate the arrray and fil the stuff

end subroutine
Any help would be greatly appreciated

Regards
Sam

Jul 23 '05 #1
1 6412
Sam wrote:
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 the array in Fortran and then
accessing it in my C++ code.

Say in my C++ code I have;

extern "C" { void foo_(float **, int&, int&); }
Fortran won't know what to do with an array of pointers to float.
int main(int argc, char* argv[]) {
// ...
// ...
int rows, columns;
float **array2d; // Now I need to pass this to the Fortran code,
// the allocation of the array and filling it
// is done in the Fortran code.
// How do I pass this to the fortran subroutine now, I did it this way

foo_(array2d, rows, columns);

// One more question,
// "Do I need to do the allocation and stuff here again?"
// Do some stuff to manipulate with the array2d.

return 0;
}

and my Fortran routine:

subroutine foo(arr,rows,co lumns)
integer rows, columns
real*8 ,allocatable:: arr (:,:)
! Do some stuff to allocate the arrray and fill the stuff

end subroutine foo
Any help would be greatly appreciated
To do this,
you must pass a Fortran 90 pointer [to a 2D array]
to a Fortran 90 subroutine foo which allocates memory
for the 2D array and assigns a pointer to the array
to the pointer that you passed into subroutine foo.
Your C++ program must include f90_adapter.h
which contains an f90_2DPointer type definition
for a Fortran 90 2D array pointer (for Absoft in this case).
Function main(int, char**) invokes
function f90_2DPointerPo inter(const f90_2DPointer*)
to retrieve the pointer to the beginning of the array
from the Fortran 90 pointer object.
cat foo.f90 subroutine foo(rows, columns, arr)
integer, intent(in):: rows, columns
integer:: row, column
double precision, pointer, dimension(:,:): : arr
double precision, pointer, dimension(:,:): : tmp
allocate(tmp(ro ws, columns))
do column = 1, columns
do row = 1, rows
tmp(row, column) = row + 10*column
end do
end do
arr => tmp
end subroutine foo
f90 -YEXT_NAMES=LCS -YEXT_SFX=_ -c foo.f90
cat f77_adapter.h #ifndef guard_f77_adapt er_h
#define guard_f77_adapt er_h

#include<stdint .h>
#include<stddef .h>
#include<string .h>

/*
* All Fortran 77 type names are of the form:
*
* f77_<type>
*
* where
*
* <type> in {subroutine, character, integer<size>, logical<size>,
* real_4, real_8, single, double, offset, length} and
* <size> in {, _1, _2, _4}.
*
* Note that the <size> parameter includes the empty string.
*/

typedef void f77_subroutine;

typedef char f77_character;

typedef int8_t f77_integer_1;
typedef int16_t f77_integer_2;
typedef int32_t f77_integer_4;
typedef f77_integer_4 f77_integer;

typedef uint8_t f77_logical_1;
typedef uint16_t f77_logical_2;
typedef uint32_t f77_logical_4;
typedef f77_logical_4 f77_logical;

typedef float f77_real_4;
typedef double f77_real_8;
typedef f77_real_4 f77_real;
typedef f77_real_4 f77_single;
typedef f77_real_8 f77_double;

typedef int f77_offset;
typedef size_t f77_length;

#endif/*guard_f77_adap ter_h */
cat f90_adapter.h #ifndef guard_f90_adapt er_h
#define guard_f90_adapt er_h

#include<f77_ad apter.h>

#ifdef __cplusplus
#define INLINE inline
#define STATIC
#define PRIVATE private:
#define PUBLIC public:
#else /*__cplusplus */
#define INLINE inline
#define STATIC static
#define PRIVATE
#define PUBLIC
#endif/*__cplusplus */

/*
* All Fortran 90 pointer type names are of the form:
*
* f90_<rank><type >Pointer
*
* where
*
* <rank> in {, 1D, 2D, 3D, 4D, 5D, 6D, 7D},
* <type> in {, single, double, integer<size>, logical<size>,
character}
* and
* <size> in {, _1, _2, _3, _4}.
*
* Note that all three parameters include the empty string.
*/

struct f90_Pointer; /* forward declaration */

#ifdef __cplusplus
extern "C" {
INLINE STATIC const
void* f90_PointerPoin ter(const struct f90_Pointer*);
}
#endif/*__cplusplus */

typedef struct f90_Pointer { /* generic scalar pointer */
PRIVATE
void* P[6];
#ifdef __cplusplus
public:
friend const void* f90_PointerPoin ter(const struct f90_Pointer*);
#endif/*__cplusplus */
} f90_Pointer;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC const
void* f90_PointerPoin ter(const f90_Pointer* p) {
return p->P[0]; }
#ifdef __cplusplus
}
#endif/*__cplusplus */

struct f90_triplet; /* forward declaration */

#ifdef __cplusplus
extern "C" {
INLINE STATIC
size_t f90_tripletExte nt(const struct f90_triplet*);
INLINE STATIC
ptrdiff_t f90_tripletStri de(const struct f90_triplet*);
}
#endif/*__cplusplus */

typedef struct f90_triplet {
PRIVATE
f77_integer lbound;
f77_integer ubound;
f77_integer stride;
#ifdef __cplusplus
public:
friend size_t f90_tripletExte nt(const struct f90_triplet*);
friend ptrdiff_t f90_tripletStri de(const struct f90_triplet*);
#endif/*__cplusplus */
} f90_triplet;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC
size_t f90_tripletExte nt(const f90_triplet* p) {
return (p->lbound < p->ubound)? (p->ubound - p->lbound + 1):
(p->lbound - p->ubound + 1); }

INLINE STATIC
ptrdiff_t f90_tripletStri de(const f90_triplet* p) {
return p->stride; }
#ifdef __cplusplus
}
#endif/*__cplusplus */

struct f90_1DPointer; /* forward declaration */

#ifdef __cplusplus
extern "C" {
INLINE STATIC const
void* f90_1DPointerPo inter(const struct f90_1DPointer*) ;
INLINE STATIC
size_t f90_1DPointerEx tent1(const struct f90_1DPointer*) ;
INLINE STATIC
ptrdiff_t f90_1DPointerSt ride1(const struct f90_1DPointer*) ;
}
#endif/*__cplusplus */

typedef struct f90_1DPointer { /* generic 1D array pointer */
PRIVATE
void* P[6];
f90_triplet T[1];
#ifdef __cplusplus
public:
friend const void* f90_1DPointerPo inter(const struct f90_1DPointer*) ;
friend size_t f90_1DPointerEx tent1(const struct f90_1DPointer*) ;
friend ptrdiff_t f90_1DPointerSt ride1(const struct f90_1DPointer*) ;
#endif/*__cplusplus */
} f90_1DPointer;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC const
void* f90_1DPointerPo inter(const f90_1DPointer* p) {
return p->P[0]; }

INLINE STATIC
size_t f90_1DPointerEx tent1(const f90_1DPointer* p) {
return f90_tripletExte nt(&(p->T[0])); }

INLINE STATIC
ptrdiff_t f90_1DPointerSt ride1(const f90_1DPointer* p) {
return f90_tripletStri de(&(p->T[0])); }
#ifdef __cplusplus
}
#endif/*__cplusplus */

struct f90_2DPointer; /* forward declaration */

#ifdef __cplusplus
extern "C" {
INLINE STATIC const
void* f90_2DPointerPo inter(const struct f90_2DPointer*) ;
INLINE STATIC
size_t f90_2DPointerEx tent1(const struct f90_2DPointer*) ;
INLINE STATIC
ptrdiff_t f90_2DPointerSt ride1(const struct f90_2DPointer*) ;
INLINE STATIC
size_t f90_2DPointerEx tent2(const struct f90_2DPointer*) ;
INLINE STATIC
ptrdiff_t f90_2DPointerSt ride2(const struct f90_2DPointer*) ;
}
#endif/*__cplusplus */

typedef struct f90_2DPointer { /* generic 2D array pointer */
PRIVATE
void* P[6];
f90_triplet T[2];
#ifdef __cplusplus
public:
friend const void* f90_2DPointerPo inter(const struct f90_2DPointer*) ;
friend size_t f90_2DPointerEx tent1(const struct f90_2DPointer*) ;
friend ptrdiff_t f90_2DPointerSt ride1(const struct f90_2DPointer*) ;
friend size_t f90_2DPointerEx tent2(const struct f90_2DPointer*) ;
friend ptrdiff_t f90_2DPointerSt ride2(const struct f90_2DPointer*) ;
#endif/*__cplusplus */
} f90_2DPointer;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC const
void* f90_2DPointerPo inter(const f90_2DPointer* p) {
return p->P[0]; }

INLINE STATIC
size_t f90_2DPointerEx tent1(const f90_2DPointer* p) {
return f90_tripletExte nt(&(p->T[0])); }

INLINE STATIC
ptrdiff_t f90_2DPointerSt ride1(const f90_2DPointer* p) {
return f90_tripletStri de(&(p->T[0])); }

INLINE STATIC
size_t f90_2DPointerEx tent2(const f90_2DPointer* p) {
return f90_tripletExte nt(&(p->T[1])); }

INLINE STATIC
ptrdiff_t f90_2DPointerSt ride2(const f90_2DPointer* p) {
return f90_tripletStri de(&(p->T[1])); }
#ifdef __cplusplus
}
#endif/*__cplusplus */

struct f90_characterPo inter; /* forward declaration */

#ifdef __cplusplus
extern "C" {
INLINE STATIC const
char* f90_characterPo interPointer(
const struct f90_characterPo inter*);
INLINE STATIC
size_t f90_characterPo interLength(
const struct f90_characterPo inter*);
}
#endif/*__cplusplus */

typedef struct f90_characterPo inter { /* character string scalar
pointer */
PRIVATE
char* P[6];
#ifdef __cplusplus
public:
friend const char* f90_characterPo interPointer(
const struct f90_characterPo inter*);
friend size_t f90_characterPo interLength(
const struct f90_characterPo inter*);
#endif/*__cplusplus */
} f90_characterPo inter;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC const
char* f90_characterPo interPointer(co nst f90_characterPo inter*
p) {
return p->P[0]; }

INLINE STATIC
size_t f90_characterPo interLength(con st f90_characterPo inter* p) {
return (size_t)p->P[1]; }
#ifdef __cplusplus
}
#endif/*__cplusplus */

struct f90_1Dcharacter Pointer; /* forward de3claration */
#ifdef __cplusplus
extern "C" {
INLINE STATIC const
char* f90_1Dcharacter PointerPointer( const
f90_1Dcharacter Pointer*);

INLINE STATIC
size_t f90_1Dcharacter PointerExtent1( const
f90_1Dcharacter Pointer*);

INLINE STATIC
ptrdiff_t f90_1Dcharacter PointerStride1( const
f90_1Dcharacter Pointer*);

INLINE STATIC
size_t f90_1Dcharacter PointerLength(c onst
f90_1Dcharacter Pointer*);
}
#endif/*__cplusplus */

typedef struct f90_1Dcharacter Pointer {/* 1D character string array
pointer*/
PRIVATE
char* P[6];
f90_triplet T[1];
#ifdef __cplusplus
public:
friend const char* f90_1Dcharacter PointerPointer(
const struct f90_1Dcharacter Pointer*);
friend size_t f90_1Dcharacter PointerExtent1(
const struct f90_1Dcharacter Pointer*);
friend ptrdiff_t f90_1Dcharacter PointerStride1(
const struct f90_1Dcharacter Pointer*);
friend size_t f90_1Dcharacter PointerLength(
const struct f90_1Dcharacter Pointer*);
#endif/*__cplusplus */
} f90_1Dcharacter Pointer;

#ifdef __cplusplus
extern "C" {
#endif/*__cplusplus */
INLINE STATIC const
char* f90_1Dcharacter PointerPointer( const
f90_1Dcharacter Pointer* p) { return p->P[0]; }

INLINE STATIC
size_t f90_1Dcharacter PointerExtent1( const
f90_1Dcharacter Pointer* p) { return f90_tripletExte nt(&(p->T[0])); }

INLINE STATIC
ptrdiff_t f90_1Dcharacter PointerStride1( const
f90_1Dcharacter Pointer* p) { return f90_tripletStri de(&(p->T[0])); }

INLINE STATIC
size_t f90_1Dcharacter PointerLength(c onst
f90_1Dcharacter Pointer* p) {
return (size_t)p->P[2]; }
#ifdef __cplusplus
}
#endif/*__cplusplus */

#undef PRIVATE
#undef PUBLIC
#undef STATIC
#undef INLINE

#endif/*guard_f90_adap ter_h */
cat main.cc #include <iostream>
#include <f90_adapter. h>

extern "C" {
f77_subroutine foo_(
const
f77_integer&, // rows
const
f77_integer&, // columns
f90_2DPointer& // 2D array
);
}
int main(int argc, char* argv[]) {
const
f77_integer rows = 3;
const
f77_integer columns = 5;
f90_2DPointer arr;
foo_(columns, rows, arr);
f77_double* A = (f77_double*)f9 0_2DPointerPoin ter(&arr);
for (f77_integer row = 0; row < rows; ++row) {
for (f77_integer column = 0; column < columns; ++column) {
std::cout << ' ' << A[row*columns + column];
}
std::cout << std::endl;
}

return 0;
}
g++ -I. -L/opt/absoft/lib -Wall -ansi -pedantic \ -o main main.cc foo.o -lfio -lf77math ./main

11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
Jul 23 '05 #2

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

Similar topics

2
3315
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 the 3D arrays, the values passed are not all the same. I am also pasting the fortran and c++ codes so that you could have a look at them. ////C++...
2
3544
by: Jonathan Underwood | last post by:
Hi I have a 2 dimensional array allocated as a contiguous chunk of memory, as described in Question 6.16 in the FAQ (i.e. in the manner of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html). I have a function declared as func(..., array,...) and I wish to pass my 2D array to that function. As I understand it, this should be...
10
3136
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I...
3
2070
by: Alex Munk | last post by:
I would like to be able to pass a 2-dimensional array to a function as a string parameter. In the called function I would like to be able to convert the string back into a 2-dimensional array. Example Function1 { int TheArray; Function2(TheArray.ToString(); } Function2 (string parm) { .... In here I would like to be able to convert parm...
3
9339
by: SQLScott | last post by:
I have looked all over and I cannot find an example or information on passing a multi-dimensional array. Well, that is not true. I found a close example in C++ but it didn't work when I "converted it" to VB.Net. My receiving method looks like this: Public DoIt(ByRef Array( , ) as string) as boolean but what I am not clear on is what...
4
6371
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically allocated a set of pointer to pointers as my multi-dimensional arrays. Here is my code (i have omitted checking calloc's return value to make this...
2
1715
by: nleahcim | last post by:
Hi - I am working on writing a number of matrix manipulation functions. The most basic one was a printing algorithm - and it shows the problem I'm having. I'm passing it a pointer a mutidimensional array (matrix) as well as the number of rows and columns. Problem is I don't know what type the pointer to the multi-dimensional matrix should be....
272
13971
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
2
4426
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
0
8110
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6514
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3775
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.