Fortran C interoperability: Accessing constant variables (parameter) from modules

Fortran C interoperability: Accessing constant variables (parameter) from modules

Hi everybody,

I've been using the Fortran-C interoperability features for some time now but always for accessing variables. I cannot figure out how to access a constant from inside a module. I have the following example:

module testing
   integer, parameter ::  const_int=160
   integer :: var_int
end module testing

I compile with:

ifort -traceback -fPIC -module obj/Release/ -c testing_mod.f90 -o obj/Release/testing_mod.o

If I execute objdump -x testing_mod.o, I get:

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 testing_mod.f90
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .note.GNU-stack        0000000000000000 .note.GNU-stack
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .trace 0000000000000000 .trace
0000000000000000 g     F .text  0000000000000010 testing._
0000000000000004       O *COM*  0000000000000008 testing_mp_var_int_

I cannot see the const_int and I cannot access it from C. I can bind the variable (var_int) and select the name but when I try to bind the constant by adding bind(C, name="const_int"), I get:

testing_mod.f90(3): error #6406: Conflicting attributes or multiple declaration of name.   [CONST_INT]
   integer, parameter, bind(C, name="const_int") ::  const_int=160
-----------------------------------------------------^
compilation aborted for testing_mod.f90 (code 1)

Any idea on how to access it? I work under Debian Linux 7 and my compiler is: Version 14.0.2.144 Build 20140120

Thanks in advance,

Petros

7 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione

I found my answer... Really sorry for the spamming. I was searching with wrong keywords. --> http://software.intel.com/en-us/forums/topic/291657

Found a workaround:

module testing
   use iso_c_binding

   integer, parameter ::  const_int=160
   integer, protected, bind(C, name="const_int") ::  const_int_c
   data const_int_c/const_int/

   integer :: var_int
end module testing

Seems to work fine. Thanks again!

Citazione:

p3tris ha scritto:

I found my answer... Really sorry for the spamming. I was searching with wrong keywords. --> http://software.intel.com/en-us/forums/topic/291657

Found a workaround:

module testing
   use iso_c_binding

   integer, parameter ::  const_int=160
   integer, protected, bind(C, name="const_int") ::  const_int_c
   data const_int_c/const_int/

   integer :: var_int
end module testing

Seems to work fine. Thanks again!

Why can't you simply add an accessor method in your module instead?

Citazione:

FortranFan ha scritto:

Quote:

p3tris wrote:

I found my answer... Really sorry for the spamming. I was searching with wrong keywords. --> http://software.intel.com/en-us/forums/topic/291657

Found a workaround:

module testing
   use iso_c_binding

   integer, parameter ::  const_int=160
   integer, protected, bind(C, name="const_int") ::  const_int_c
   data const_int_c/const_int/

   integer :: var_int
end module testing

Seems to work fine. Thanks again!

 

Why can't you simply add an accessor method in your module instead?

Yes, that was my other option. But as I have more than one contstants to access, I prefered this one. I guess both work fine.

Citazione:

p3tris ha scritto:

Quote:

FortranFan wrote:

Why can't you simply add an accessor method in your module instead?

 

Yes, that was my other option. But as I have more than one contstants to access, I prefered this one. I guess both work fine.

Re: "both work fine" - some comments:

  • As one's program gets bigger and bigger and more coders work on it, I usually find such direct access to data is fraught with danger, especially in mixed-language environments.
  • The accessor method provides good data encapsulation methodology which is one of the benefits of using modules.
  • I'm not a fan of DATA statement and have "deprecated" its use in my code.  It can interfere with thread safety and hinder parallelization efforts

My 2 cents,

Citazione:

FortranFan ha scritto:

Quote:

p3tris wrote:

Quote:

FortranFan wrote:

Why can't you simply add an accessor method in your module instead?

 

Yes, that was my other option. But as I have more than one contstants to access, I prefered this one. I guess both work fine.

 

Re: "both work fine" - some comments:

  • As one's program gets bigger and bigger and more coders work on it, I usually find such direct access to data is fraught with danger, especially in mixed-language environments.
  • The accessor method provides good data encapsulation methodology which is one of the benefits of using modules.
  • I'm not a fan of DATA statement and have "deprecated" its use in my code.  It can interfere with thread safety and hinder parallelization efforts

My 2 cents,

Thanks! I think I'll create some accessors as you suggest. However, the DATA statement, I use it in other situations not relating to the C interface. Could you give me some source of information about its interference with parallelization? Or is it a personal observation?

DATA implies SAVE so you would need to jump through more hoops to achieve thread safety.

Lascia un commento

Eseguire l'accesso per aggiungere un commento. Non siete membri? Iscriviti oggi