Home > Archive > Fortran > March 2006 > Problems about Mixed programming with C++ and Fortran
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problems about Mixed programming with C++ and Fortran
|
|
|
| I am about to doing 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 logic control job, and Fortran sees to numerical
calculation problems.
My plan:
Encapsulating the Fortran codes as a DLL using compaq visual
Fortran.
Then invoke the functions in the DLL with C++.
So there is the problem:
How to pass a matrix of Blitz++ to Fortran,
and how to return a result matrix back to Blitz++?
Has anyone done this kind of job before? Help me , pls.
Any suggestion will be apprecited!
| |
| Guch Wu 2006-03-26, 7:01 pm |
| Help!
| |
| Craig Powers 2006-03-27, 4:00 am |
| Guch wrote:
> I am about to doing 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 logic control job, and Fortran sees to numerical
> calculation problems.
>
> My plan:
> Encapsulating the Fortran codes as a DLL using compaq visual
> Fortran.
> Then invoke the functions in the DLL with C++.
>
>
> So there is the problem:
> How to pass a matrix of Blitz++ to Fortran,
> and how to return a result matrix back to Blitz++?
>
>
> Has anyone done this kind of job before? Help me , pls.
> Any suggestion will be apprecited!
I'd suggest a close reading of the CVF manual on mixed language
programming and the Blitz++ manual. The former will be necessary to
figure out how to accept data from C++ in Fortran (and return it to
C++). The latter will be necessary to figure out how to get the data
out of Blitz++ structures into a form that can be passed to Fortran, and
then how to convert it back again.
Since the raison d'etre of Blitz++ is to provide numerics in C++, I'm a
little curious about why you want to bother with the library at all if
you're going to be doing your numerics in Fortran. Surely some
combination of a std::vector (or class wrapping it) and/or std::valarray
would be adequate if all you're doing on the C++ side is "logic
control"? I think you're going to find that if you have multiple
transitions between C++ and Fortran in a single sequence, the
performance will be horrible as you'll be engaging in a bunch of copying
of arrays. (That's based on a very brief glance through the Blitz++
manual online.)
I've done mixed language programming with CVF before, but the other
language has been VB rather than C++. I've never worked with Blitz++ at
all.
| |
| Guch Wu 2006-03-27, 8:00 am |
| Thank Craig Powers!
I have read the CVF manual and Blitz++, But I found that they are not
so clear about mixed programming with Fortran and C++. I can't handle
the problem without a sample.
I just want to do the pure numerical job in Fortran. There are many
image processing algorithms which need more logical control than , such
as encode, decode, and line detection.
Why don't you like Blitz++? It's an well designed and efficient matrix
manipulating library in C++.
| |
| Pierre Asselin 2006-03-27, 7:02 pm |
| Guch Wu <guchwu@gmail.com> wrote:
> [ ... ]
> Why don't you like Blitz++? It's an well designed and efficient matrix
> manipulating library in C++.
I think Craig was asking you why you *need* Blitz++ at all, given
that you plan to do the number-crunching in Fortran.
--
pa at panix dot com
| |
| Craig Powers 2006-03-28, 4:02 am |
| Guch Wu wrote:
> Thank Craig Powers!
>
> I have read the CVF manual and Blitz++, But I found that they are not
> so clear about mixed programming with Fortran and C++. I can't handle
> the problem without a sample.
That's because C++ doesn't really do interop on its own terms with any
other language outside of the .NET environment AFAIK (and there, it's
not really accurate to call the resulting language C++).
Hence, on the C++ side, you are looking for material on C and Fortran
interop. Basically, you'll be writing a C interface for a Fortran
function to make it callable from C++.
On the Fortran side, you are looking for material on C interop.
> I just want to do the pure numerical job in Fortran. There are many
> image processing algorithms which need more logical control than , such
> as encode, decode, and line detection.
>
> Why don't you like Blitz++? It's an well designed and efficient matrix
> manipulating library in C++.
I have nothing against Blitz++ at all. I was expressing concern about
the efficiency of using Blitz++ if you are going to have a lot of C++ ->
Fortran -> C++ round trips, since it appears to me as though their
native storage format is not amenable to passing directly to a Fortran
array on the other side -- if I read correctly, you would basically be
forced to do copy-in/copy-out, almost certainly incurring a significant
performance penalty on each trip. Since your reason for farming out
computations to Fortran is concern about the computational performance
of Blitz++, obviously performance is a big concern for you (and
presumably you have already confirmed via testing that Blitz++ performs
poorly for some of your calculations and cannot be optimized easily).
If you are doing *all* of your numerics in Fortran, I thought that you
might find it more efficient to use an STL structure that is better at
transparently packaging your data for transmission to and from Fortran.
If you are not doing all of your numerics in Fortran, then I would
imagine that Blitz++ might be appropriate for your usage, but I think
you'll need to be very careful about your interop, for the reasons I
laid out in the previous paragraph.
| |
| Joe Krahn 2006-03-28, 7:02 pm |
| Craig Powers wrote:
> Guch Wu wrote:
>
>
>
> That's because C++ doesn't really do interop on its own terms with any
> other language outside of the .NET environment AFAIK (and there, it's
> not really accurate to call the resulting language C++).
>
> Hence, on the C++ side, you are looking for material on C and Fortran
> interop. Basically, you'll be writing a C interface for a Fortran
> function to make it callable from C++.
>
The main problem is that C++ and Fortran will have different data
representations and probably incompatible arrays, so you may have to
duplicate entire arrays to transfer them.
One thing I was thinking about is a C++ library customized to be binary
compatible with a Fortran compiler. There could be variations to match
different Fortran compilers. This would allow seamless interoperability
of matrices between C++ and Fortran. This would produce a C++/Fortran
compatible API, even though there are no binary standard for Fortran.
I also have done some experimentation with building a Fortran interface
to C++ class libraries. This generally requires a plain C wrapper to
call the C++ functions, which can be called from Fortran. Calling a C++
class procedure means calling the Fortran module procedure, which calls
the C wrapper, which calls the C++ function. Having essentially two
wrapper functions is a bit of overhead, but less than the .NET way of
doing things. It is also very versatile, because it gives you a place to
manipulate data from both Fortran and C.
I have not yet experimented with making Fortran module/type procedures
accessible as a C++ class.
The hard part of designing good C++/Fortran interop is the choice of
trying to deal with non-portable simple C/Fortran calling, use !DEC$
directives available on some compilers, or wait for F2003. My current
thought is to get some good examples working for simple C/Fortran calls
which mostly work, and incorporate F2003 when it is available.
Joe
|
|
|
|
|