LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-17-2015, 08:26 AM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Rep: Reputation: Disabled
"C" & GFortran interoperability.


My environment is Ubuntu 12.04, I mostly code in GFortran but occasionally I need to use some C or C++ software. I use g++ compiler for C. This source explains how C routines can be called from Fortran code (page 53). The trouble is it is all predicated on implementation of C 2003 and possibly later versions of Gfortran, as I understand, and although I follow instruction to the letter the simple interface in Gfortran I designed does not compile.

I wonder if anybody has experience working with interoperability and could share the tips.

Thanks, - Alex
 
Old 03-18-2015, 08:22 AM   #2
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588

Rep: Reputation: Disabled
I did some mixing of FORTRAN and C -- about thirty years ago on VAX machines. I had problems too. At long last I found out (in the debugger) that the arguments during function calls were handed over in reverse order. E.g. C-routine(a,b,c) returned its values(!) to main-FORTRAN(c,b,a), i.e. in the wrong variables.

No idea whether that is your problem (I hope not, after thirty years, somebody should have had the leisure to document that) but I wanted to highlight the usefulnes of poking around with a debugger .
 
Old 03-18-2015, 12:18 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831Reputation: 4831
Quote:
Originally Posted by JZL240I-U View Post
I did some mixing of FORTRAN and C -- about thirty years ago on VAX machines. I had problems too. At long last I found out (in the debugger) that the arguments during function calls were handed over in reverse order. E.g. C-routine(a,b,c) returned its values(!) to main-FORTRAN(c,b,a), i.e. in the wrong variables.

No idea whether that is your problem (I hope not, after thirty years, somebody should have had the leisure to document that) but I wanted to highlight the usefulnes of poking around with a debugger .
I have a glimmer of remembering that! Not due to personal experience but once hearing how different languages treat the stack and push/pop. Maybe I'm all wrong, maybe they all treat the stack the same and I'm just confusing that with endian differences.

I subscribed to this wondering who uses FORTRAN still, and why?

It was old when I went to college and we were cringing that there was a course in FORTRAN but then we realized it was for E.C.E. majors not straight EE. So it became a tech elective which we elected not to take.
 
Old 03-18-2015, 12:31 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,707

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Note: I don't know Fortran.

Quote:
Originally Posted by AlexBB View Post
I use g++ compiler for C.
Unless you have used extern "C" this will be a problem. Interoperability with C++ is much harder. I suggest you avoid g++ and compile with gcc instead.

Quote:
This source explains how C routines can be called from Fortran code (page 53). The trouble is it is all predicated on implementation of C 2003 and possibly later versions of Gfortran,
That's just the gfortran official manual, so you can probably install the version that matches your compiler via the package manager, look for gfortran-doc or similar.
 
Old 03-18-2015, 01:26 PM   #5
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588

Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
I have a glimmer of remembering that! Not due to personal experience but once hearing how different languages treat the stack and push/pop.
Exactly. But those debuggers were fine tools, even then. I also did some C-programming on an Atari ST. You could there manipulate the arguments even after they were pushed onto the stack (verified a nasty error that way), or change values of variables after they were loaded into the registers. Ah, well, Motorola 68.000 processors, who understands Intel .

Quote:
Originally Posted by rtmistler View Post
I subscribed to this wondering who uses FORTRAN still, and why?
Why not? It's a tool, often used in physical sciences. I used it even in a project to create font-editors. Even Cobol is still in use and experts get very good pay to maintain quasi fossilized software.

Quote:
Originally Posted by rtmistler View Post
It was old when I went to college and we were cringing that there was a course in FORTRAN but then we realized it was for E.C.E. majors not straight EE. So it became a tech elective which we elected not to take.
Don't be afraid, I'm right here and it doesn't bite . It's just a language, easy to learn and nice for comparisons of concepts with other languages.
 
Old 03-18-2015, 06:08 PM   #6
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
I appreciate all post. First about Fortran. I used Fortran IV, years ago, and now found that modern fortran F90, Fortran 2003 are incredibly advanced over the old versions (pointers, allocatable arrays, etc, etc, etc). I do numerical simulations, it is all physics, the current problem is Fast Fourier Transform and the nice thing about Fortran is that there are thousands programs and routines on the Internet on all aspect of mathematics. Still I found a nice "C" code that works by itself, it compiles and runs, but I need to attach it to the major codes and they are in GFortran.

I never worked on VAX machines, vaguely remember there was a mini mainframe Prime, also DEC production it seems. Nice that my OP provoked some going down a memory lane.

One of the approaches I am pursuing now is to rewrite the C code into modern Fortran. I almost done with it but I still want to crack the problem of interoperability because so much code around is also in C or C++.

Thanks, - Alex
 
Old 03-19-2015, 03:27 AM   #7
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588

Rep: Reputation: Disabled
What exactly happens when you link C-modules to FORTRAN? Errors? Or can you run the application and it goes bonkers?
 
Old 03-20-2015, 04:20 PM   #8
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Compile errors, Sir. I will try to prepare a better account but in particular the gfortran compiler does not recognize the VALUE attribute. In order to communicate with C one has to write an interface module with declarations of the calling program acceptable to fortran.

Code:
interface
    subroutine kronrod ( n, eps, x, w1, w2 ) bind ( c )
      use iso_c_binding
      integer ( c_int ), VALUE :: n
      real ( c_double ), VALUE :: eps
      real ( c_double ) :: x(*)
      real ( c_double ) :: w1(*)
      real ( c_double ) :: w2(*)
    end subroutine kronrod
  end interface
C passes variables to subroutines by VALUE, Fortran by reference, this is why this VALUE attribute is needed. The compiler gives me an error on this.

Thanks, - Alex

Last edited by AlexBB; 03-21-2015 at 07:07 PM.
 
Old 03-23-2015, 02:24 AM   #9
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 21, MX-21, Manjaro
Posts: 4,588

Rep: Reputation: Disabled
I -- see. Hmm, I'm used to FORTRAN IV or 77 and the contemporaneous C-version, thus some sort of a fossil . I thought the newer FORTRANs could handle even pointers but I'm way out of my depth here so I'm afraid I won't be much of a help here. Anyway, post away your thoughts, sometimes just formulating them helps in clarifying. What, exactly, is the compiler message?

Last edited by JZL240I-U; 03-23-2015 at 02:27 AM.
 
Old 03-23-2015, 10:46 AM   #10
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Sorry for the lack of detail, I am away from my laptop now where most of this software resides. One thing I can mention though. The issue of interoperability has nothing to do with pointers in modern Fortran. Anyway I have Gfortran and I am not sure how much of the latest innovations it has absorbed. The mere fact is that yesterday I finished a program where I used Fortran-90 pointers and allocatable arrays and everything works now without a hitch. Since I did it first time in my life I had a lot of difficulties. Fortran 90 pointers are different from the C pointers. Very interesting, but they do work.

There are two other ways to do dynamic arrays in Fortran 90: automatic arrays and allocatable arrays.

Interoperability with C was introduced in 2003 I think. There is a chance Gfortran does not know about it.

Last edited by AlexBB; 03-23-2015 at 10:48 AM.
 
Old 03-23-2015, 11:50 AM   #11
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
@AlexBB - I have responded to some of your posts over on cplusplus.com regarding the CFFT class. If that is what you are trying to implement as a subroutine in fortran, I have a primitive version working. The fortran program calls the c++ function, passing in a complex array and the array size. The c++ function returns the array and the fortran program prints it out.

My experience with fortran was long ago (think punch cards and Holerith field) so the fortran part is pretty much internet search and brute force.

There were some quirks getting it to compile.

If you think it might help, I can post the code here.

Norm
 
Old 03-23-2015, 04:32 PM   #12
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Norm, thank you very much. Your offer is incredible. I sure will be interested. In a way the acuity of the problem faded away. I completely debugged the calling main I wrote to use CFFT, ran it and it worked beautifully. Since I did not have the interoperability (could not figure out how it worked) I finally rewrote the fft.cpp in GFortran and it began working yesterday. It works exactly like the C version.

HOWEVER, I will really appreciate a code sample. It is a great asset. It may serve me well in the future.

Thanks you, - Alex
 
Old 03-23-2015, 05:44 PM   #13
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I shortened your c++ fft function that writes to files for brevity. It works as well.

First the code:
fortran.f03
Code:
      CALL fft_function()
      END

      subroutine fft_function()
      use, intrinsic :: iso_c_binding
      include 'fftw3.f03'
      
      external fft
      integer size
      complex(C_DOUBLE_COMPLEX), dimension(256) :: array
      size = 256

      call fft(size, array)
! Print out the array passed back.
      do i = 1, size
        write(*,*) array(i)
      enddo
      end
main.cpp
Code:
#include "fft.h"
#include <cmath>
#include <vector>

double blackmanHarrisWindow (int n, int N) {
	double res, PI=3.141592653589793238462643383279502884197169;
	int N1;
	N1 = N-1;
	res = 0.35875-0.48829*cos(2.0*PI*n/N1)+0.14128*cos(4.0*PI*n/N1)+0.01168*cos(6.0*PI*n/N1);
	return res;
}

extern "C" {
	void fft_(int *n, complex array[])
	{
		int array_size = *n;
		std::vector<complex> Output;
		for (int jj = 0; jj < array_size; ++jj) {
			double xx = blackmanHarrisWindow(jj,array_size);
			array[jj] = complex(xx);
		}
		CFFT::Forward (array, &Output[0], array_size);
		CFFT::Inverse (&Output[0], array, array_size);
		return;
	}
} // extern "C"
To compile: (Note: all the files need to be in the same directory (complex.h, complex.cpp, fft.h, fft.cpp, main.cpp, fortran.f03))
g++ -c *.cpp
gfortran -I/usr/include -c fortran.f03
gfortran -lstdc++ -o application *.o

Problems that I ran into:
gfortran appends an underscore to c/c++ function names (void fft() fails to compile).
Having to put "-I/usr/include" on the gfortran command line is probably just a configuration issue.
Compiling will fail if the fortran file does not have the extension "f03" (for iso_c_binding).

As I said I know little to no fortran but the array printed out agrees with the array that your c++ program outputs.

Regards,
Norm
 
Old 03-23-2015, 06:02 PM   #14
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Norm, thank you. Looks gorgeous! It would have taken me a couple of weeks to work it all out. Will try it later tonight. Perhaps I will have some questions also. Please do not go far away :-)

Thanks again, - Alex
 
Old 03-23-2015, 08:40 PM   #15
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
Norm hi,

I tried to do gfortran fortran.f03 as well as gfortran -I/usr/include -c fortran.f03. In both cases the compile error was:

Code:
Error: Can't open included file 'fftw3.f03'
You most likely forgot to have it posted. This file has an Internet presence. I will try to find it tomorrow.

Thank you, - Alex
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need solution to Sed problem. Want to match "&" but not "&amp;" tells Linux - General 14 02-05-2012 08:59 AM
[SOLVED] "Insert" & "Delete" key returns "~" in a terminal. sharky Linux - General 15 04-26-2011 08:36 AM
"bad_pool_error" & "Your system is not fully acpi compliant get your Bios updated" errors in WinXP Aquarius_Girl General 10 07-30-2010 11:27 AM
LXer: Benefit of Microsoft and Novell "interoperability" LXer Syndicated Linux News 1 12-28-2006 07:15 PM
does the G95 or Gfortran support the concept of "module" in fortran95 ztdep Programming 1 09-08-2005 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration