Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.fortran

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 10-23-2006, 05:45 PM
JKB
Guest
 
Posts: n/a
Default gfortran and "Type/rank mismatch"

Hello,

I have found this code :

ubroutine GAMMALN(X, RESULTAT, SIGNE, ERREUR)
implicit none

real*8, intent(in) :: X

integer*8, intent(out), optional :: ERREUR

real*8, intent(out) :: RESULTAT
real*8, intent(out), optional :: SIGNE

integer*8 ERREUR_GAMMA

real*8 SIGNE_GAMMA

type GSL_SF_RESULT
real*8 VALEUR
real*8 ERREUR
end type

type(GSL_SF_RESULT) STRUCT_RESULTAT

interface
integer function gsl_sf_lngamma_sgn_e_wrapper(X, RESULTAT,
SIGNE)
implicit none
type GSL_SF_RESULT
real*8 VALEUR
real*8 ERREUR
end type
real*8, intent(in) :: X
type(GSL_SF_RESULT), intent(out) :: RESULTAT
real*8, intent(out) :: SIGNE
end function
end interface

ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
SIGNE_GAMMA)
RESULTAT = STRUCT_RESULTAT%VALEUR

if (present(SIGNE)) then
SIGNE = SIGNE_GAMMA
end if

if (present(ERREUR)) then
ERREUR = ERREUR_GAMMA
end if

return
end subroutine

that can be compiled with gfortran 4.0. With 4.1, I obtain the
following message :

In file fonctions_speciales.conv.f90:71

ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
1
Error: Type/rank mismatch in argument 'resultat' at (1)

Only one question (I think that this code has to compile...). Is
there a bug in gfortran 4.1 ? What is this kind of error ?

Thans in advance,

JKB

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 10-23-2006, 06:05 PM
FX
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

> In file fonctions_speciales.conv.f90:71
>
> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
> 1
> Error: Type/rank mismatch in argument 'resultat' at (1)


Sun compiler also gives:


ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
^
"foo.f90", Line = 35, Column = 52: ERROR: The type of the actual
argument, "type(GSL_SF_RESULT)", does not match "type(GSL_SF_RESULT)",
the type of the dummy argument.

--
FX
Reply With Quote
  #3 (permalink)  
Old 10-23-2006, 06:10 PM
JKB
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

Le 23-10-2006, à propos de
Re: gfortran and "Type/rank mismatch",
FX écrivait dans comp.lang.fortran :
>> In file fonctions_speciales.conv.f90:71
>>
>> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
>> 1
>> Error: Type/rank mismatch in argument 'resultat' at (1)

>
> Sun compiler also gives:
>
>
> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
> ^
> "foo.f90", Line = 35, Column = 52: ERROR: The type of the actual
> argument, "type(GSL_SF_RESULT)", does not match "type(GSL_SF_RESULT)",
> the type of the dummy argument.


I don't see the mistake... Any idea ?

JKB
Reply With Quote
  #4 (permalink)  
Old 10-23-2006, 06:21 PM
Richard Maine
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

JKB <knatschke@koenigsberg.fr> wrote:

[code elided]

> Only one question (I think that this code has to compile...). Is
> there a bug in gfortran 4.1 ? What is this kind of error ?


The code is incorrect. No it doesn't "have to compile". It was probably
compiled on some compiler that didn't catch the error. The error is that
the code has multiple definitions of the type gsl_sf_result. Each of
these definitions counts as a different type, no matter how simillar the
definitions look . Even if they are textually identical, they still
count as different types. Thus the types of your actual and dummy
arguments don't match.

If you want to pass arguments of derived type, you must do one of two
things.

1. Use only a single type definition. Pretty much the only way to do
this in most cases is to put the type definition in a module and USE the
module in the calling and called procedures. An internal procedure can
do it with host association, but that's pretty limitting.

The above approach is what I generally recommend. Many f90 features work
much more cleanly with modules. This is one of them. I see some other
signs of the code avoiding modules, though. In particular, if it used
module procedures, then the interface bodies would not be needed.

2. If you don't want to put the type definition in a module, then you
must declare it to be a sequence type. You do that by putting a sequence
statement in each copy of the type definition. The sequence statement
basically means that the type matches other types that are declared
sufficiently simillarly. I'll not quote the details of the required
similarity here (I'd have to look them up first to make sure I had them
right), but yours are certainly plenty similar.

The original code was clearly assuming that the types acted like
sequence types, even though they weren't declared that way.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Reply With Quote
  #5 (permalink)  
Old 10-23-2006, 06:28 PM
Craig Powers
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

JKB wrote:
> Le 23-10-2006, à propos de
> Re: gfortran and "Type/rank mismatch",
> FX écrivait dans comp.lang.fortran :
>>> In file fonctions_speciales.conv.f90:71
>>>
>>> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
>>> 1
>>> Error: Type/rank mismatch in argument 'resultat' at (1)

>> Sun compiler also gives:
>>
>>
>> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
>> ^
>> "foo.f90", Line = 35, Column = 52: ERROR: The type of the actual
>> argument, "type(GSL_SF_RESULT)", does not match "type(GSL_SF_RESULT)",
>> the type of the dummy argument.

>
> I don't see the mistake... Any idea ?


The types aren't sequence types, so the one in the interface is
different from the one in the parent, even though they have the same
names and contents (because the lack of SEQUENCE allows the compiler to
re-order the members at will, I believe).

Add SEQUENCE immediately after the TYPE line on both declarations to fix
this, e.g.

TYPE GSL_SF_RESULT
SEQUENCE
! ....
END TYPE
Reply With Quote
  #6 (permalink)  
Old 10-23-2006, 06:34 PM
JKB
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

Le 23-10-2006, à propos de
Re: gfortran and "Type/rank mismatch",
Richard Maine écrivait dans comp.lang.fortran :
> JKB <knatschke@koenigsberg.fr> wrote:
>
> [code elided]
>
>> Only one question (I think that this code has to compile...). Is
>> there a bug in gfortran 4.1 ? What is this kind of error ?

>
> The code is incorrect. No it doesn't "have to compile". It was probably
> compiled on some compiler that didn't catch the error. The error is that
> the code has multiple definitions of the type gsl_sf_result. Each of
> these definitions counts as a different type, no matter how simillar the
> definitions look . Even if they are textually identical, they still
> count as different types. Thus the types of your actual and dummy
> arguments don't match.


I don't understand. I only use this subroutine to call a C function
from a Fortran program. GSL_DF_RESULT is only defined in this
subroutine. I don't see the differences between the argument and the
dummy argument.

Regards,

JKB
Reply With Quote
  #7 (permalink)  
Old 10-23-2006, 06:36 PM
JKB
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

Le 23-10-2006, à propos de
Re: gfortran and "Type/rank mismatch",
Craig Powers écrivait dans comp.lang.fortran :
> JKB wrote:
>> Le 23-10-2006, à propos de
>> Re: gfortran and "Type/rank mismatch",
>> FX écrivait dans comp.lang.fortran :
>>>> In file fonctions_speciales.conv.f90:71
>>>>
>>>> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
>>>> 1
>>>> Error: Type/rank mismatch in argument 'resultat' at (1)
>>> Sun compiler also gives:
>>>
>>>
>>> ERREUR_GAMMA = gsl_sf_lngamma_sgn_e_wrapper(X, STRUCT_RESULTAT, &
>>> ^
>>> "foo.f90", Line = 35, Column = 52: ERROR: The type of the actual
>>> argument, "type(GSL_SF_RESULT)", does not match "type(GSL_SF_RESULT)",
>>> the type of the dummy argument.

>>
>> I don't see the mistake... Any idea ?

>
> The types aren't sequence types, so the one in the interface is
> different from the one in the parent, even though they have the same
> names and contents (because the lack of SEQUENCE allows the compiler to
> re-order the members at will, I believe).


OK. It compiles. I don't know this statement and I don't have this
one in the book I use...

> Add SEQUENCE immediately after the TYPE line on both declarations to fix
> this, e.g.


Thanks,

JKB
Reply With Quote
  #8 (permalink)  
Old 10-23-2006, 06:55 PM
Richard Maine
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

JKB <knatschke@koenigsberg.fr> wrote:

> Le 23-10-2006, à propos de
> Re: gfortran and "Type/rank mismatch",
> Richard Maine écrivait dans comp.lang.fortran :


> > The error is that
> > the code has multiple definitions of the type gsl_sf_result.


> I don't understand. I only use this subroutine to call a C function
> from a Fortran program. GSL_DF_RESULT is only defined in this
> subroutine. I don't see the differences between the argument and the
> dummy argument.


The "in this subroutine" bit doesn't matter. There are 2 definitions.
Count them. One is in the interface body, and one is outside of it. An
interface body has its own scope. (And it has to - there are debates
about whether it should be able to inherit names from its host, but none
about whether it has to have its own scope). If you see 2 definitions of
a type, no matter where they are, and if it isn't a sequence type, then
those 2 definitions don't count as the same. As mentioned before, it
doesn't matter that you can't see the difference. It only matters that
there are 2 of them.

Oh. And if it is a C routine, then you want the sequence attribute
anyway. That attribute increases the odds that the type will be laid out
in memory the same way as the C code expects. It doesn't guarantee such
a layout, but it improves the odds. In f2003, you can declare the type
to be BIND(C), which actually guarantees it. For now, use sequence.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Reply With Quote
  #9 (permalink)  
Old 10-23-2006, 07:10 PM
JKB
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

Le 23-10-2006, à propos de
Re: gfortran and "Type/rank mismatch",
Richard Maine écrivait dans comp.lang.fortran :
> JKB <knatschke@koenigsberg.fr> wrote:
>
>> Le 23-10-2006, à propos de
>> Re: gfortran and "Type/rank mismatch",
>> Richard Maine écrivait dans comp.lang.fortran :

>
>> > The error is that
>> > the code has multiple definitions of the type gsl_sf_result.

>
>> I don't understand. I only use this subroutine to call a C function
>> from a Fortran program. GSL_DF_RESULT is only defined in this
>> subroutine. I don't see the differences between the argument and the
>> dummy argument.

>
> The "in this subroutine" bit doesn't matter. There are 2 definitions.
> Count them. One is in the interface body, and one is outside of it. An
> interface body has its own scope. (And it has to - there are debates
> about whether it should be able to inherit names from its host, but none
> about whether it has to have its own scope). If you see 2 definitions of
> a type, no matter where they are, and if it isn't a sequence type, then
> those 2 definitions don't count as the same. As mentioned before, it
> doesn't matter that you can't see the difference. It only matters that
> there are 2 of them.
>
> Oh. And if it is a C routine, then you want the sequence attribute
> anyway. That attribute increases the odds that the type will be laid out
> in memory the same way as the C code expects. It doesn't guarantee such
> a layout, but it improves the odds. In f2003, you can declare the type
> to be BIND(C), which actually guarantees it. For now, use sequence.


Thanks for your explanation,

JKB
Reply With Quote
  #10 (permalink)  
Old 10-23-2006, 07:14 PM
Steven G. Kargl
Guest
 
Posts: n/a
Default Re: gfortran and "Type/rank mismatch"

In article <slrnejq2pr.3kl.knatschke@rayleigh.systella.fr>,
JKB <knatschke@koenigsberg.fr> writes:
> Le 23-10-2006, à propos de
> Re: gfortran and "Type/rank mismatch",
> Richard Maine écrivait dans comp.lang.fortran :
>> JKB <knatschke@koenigsberg.fr> wrote:
>>
>> [code elided]
>>
>>> Only one question (I think that this code has to compile...). Is
>>> there a bug in gfortran 4.1 ? What is this kind of error ?

>>
>> The code is incorrect. No it doesn't "have to compile". It was probably
>> compiled on some compiler that didn't catch the error. The error is that
>> the code has multiple definitions of the type gsl_sf_result. Each of
>> these definitions counts as a different type, no matter how simillar the
>> definitions look . Even if they are textually identical, they still
>> count as different types. Thus the types of your actual and dummy
>> arguments don't match.

>
> I don't understand. I only use this subroutine to call a C function
> from a Fortran program. GSL_DF_RESULT is only defined in this
> subroutine. I don't see the differences between the argument and the
> dummy argument.
>
> Regards,
>
> JKB


What Richard and Craig have told you is that two declarations
are not the same type. Even though "Declaration #1" and
"Declaration #2" below look the same a compiler is free
to write VALEUR, ERREUR into memory for #1 and ERREUR, VALEUR
into memory for #2.

subroutine GAMMALN(X, RESULTAT, SIGNE, ERREUR)

! Declaration #1
type GSL_SF_RESULT
real*8 VALEUR
real*8 ERREUR
end type

interface
integer function gsl_sf_lngamma_sgn_e_wrapper(X, RESULTAT,SIGNE)
! Declaration #2
type GSL_SF_RESULT
real*8 VALEUR
real*8 ERREUR
end type
end function
end interface

end subroutine

To guarantee the order of the derived type, you should change it to

type GSL_SF_RESULT
sequence
real*8 VALEUR
real*8 ERREUR
end type

A compiler can now infer that the two types are the same.

BTW, like Richard, I think the use of a module is a good idea.

--
Steve
http://troutmask.apl.washington.edu/~kargl/
Reply With Quote
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:41 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.