- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I need to do some interfacing between c and Fortran and am having huge problems with anything that isn't just a single value (i.e., I cannot seem to pass matrices and arrays properly).
I have a system with a Fortran main using a c library. The Fortran main calls the c lib, which is supposed to return some data previously received from another c program, via a socket. Everything in the c parts looks fine (i.e. the socket communication is not the problem) but I can't seem to properly pass the data from c to Fortran (Fortran to c is no issue). Either the data appears to be passed correctly but Fortran then just randomly freezes or the whole thing crashes as the c function returns (*** glibc detected *** double free or corruption(fasttop): 0x0804a008 ***Aborted...).
My Fortran part looks like this:
INTERFACE
SUBROUTINE recvDataFromC(sockID, nU, nC, timeStep, cAddress) BIND (C, NAME = "recvDataFromC")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: c_double, c_int, c_intptr_t
INTEGER (KIND = c_int), VALUE :: sockID
INTEGER (KIND = c_int) :: nU, nC
REAL (KIND = c_double) :: timeStep
INTEGER (KIND = c_intptr_t) :: cAddress
END SUBROUTINE recvDataFromC
END INTERFACE
To call the c function and then use data, I do the following (and if someone can point me to a better way of doing this (if there is one), that would be divine):
CALL recvDataFromC(sockID_, n_Unknowns_, n_Cells_, timeStep_, cAddress)
ALLOCATE (serialisedMatrix(n_Unknowns_ * n_Cells_))
c_matrix = TRANSFER (cAddress, c_matrix)
CALL C_F_POINTER(c_matrix, serialisedmatrix, [n_Unknowns_*n_Cells_])
where things are declared like this:
TYPE, BIND (C) :: matrixElement
REAL (KIND = c_double) :: val
END TYPE matrixElement
TYPE (matrixElement), ALLOCATABLE, TARGET :: serialisedMatrix(:)
TYPE (c_ptr) :: c_matrix
INTEGER (KIND = c_intptr_t) :: cAddress
INTEGER (KIND = c_int) :: n_Unknowns_, n_Cells_
REAL (KIND = c_double) :: timeStep_
From here, I expect to be able to access serialisedMatrix%val where val ranges from 1 to n_Unknowns_*n_Cells_.
The c function looks like this:
void recvDataFromClient(int sockID, int *nU, int *nC, double *timeStep, intptr_t *cAddress) {
double *serialisedMatrix;
*** read values for nU and nC from socket ***
if ( ( serialisedMatrix = malloc(sizeof(double) * (*nU) * (*nC))) == NULL) {
printf("\n malloc() failed.");
exit(1);
}
... read data from socket and put it into serialisedMatrix ...
*cAddress = serialisedMatrix;
}
sockID is received without problems, likewise is n_Unknowns_ , n_Cells_ and timeStep_ always returned without problems. The c address however, doesn't seem to work the way I want it to.
I would like to have the last line of recvDataFromC as
cAddress = (intptr_t*) serialisedMatrix;
but that causes the call to C_F_POINTER to fail.
Fortran part is compiled with (IFORT) 12.1.0 20110811 on a CentOS 5.x system using these flags:
-check all -debug all -traceback -fpe0 -fp-model precise -ftz -ftrapuv -fno-alias -I$(CLIBS)
wich CLIBS being the path to where the c part is compiled.
c part is compiled with
gcc -c -g -fPIC -Wall
I can't run this in idb, it claims that the source files don't exist and lists the process as deleted. gdb, also returns no useful information.
If anyone can help with how to accurately pass a matrix from c to Fortran I would be ultra mega thankful (if it helps, I already know the size of the matrix in advance), I've been fiddling around with this for days but getting nowhere.
Thanks!
/Cathleen
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page