use variables defined in cs_user_modules.f90 in a C routine

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Post Reply
jtache
Posts: 25
Joined: Thu Apr 19, 2012 4:01 pm

use variables defined in cs_user_modules.f90 in a C routine

Post by jtache »

Hello,

I search in the forum and user guide but I didn't find an answer. I'm using CS6.1.
I would like to import a module defined in cs_user_modules.f90 (specially vars which are used by a number of other Fortran routines) in a C routine (cs_user_postprocess.c).
How could I do that?

Thanks for your help!

Jeremie
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: use variables defined in cs_user_modules.f90 in a C routine

Post by Yvan Fournier »

Hello,

Actually, using "bind(C, name="c_name") with a Fortran variable declaration and declaring that in C should work.

For example, in Fortran:

Code: Select all

real(kind=c_double) :: w2
bind(C, name='c_w2'') :: w2
With the match in C:

Code: Select all

extern double c_w2;
Where in this case, the C name could be aligned with Fortran for clarity (w2 instead of c_w2)

If you have an array instead of a constant in Fortran passing a "modern Fortran" array (i.e. with a "dimension(:) :: x" type declaration) is more tricky than a basic "dimension(*) :: y" array, as the first type has metadata, while the second one is just a pointer. In that case, you need to check the iso_c_binding documentation for details. In that example, "c_loc(x)" can probably give you the pointer to raw data, as in C.

Also, if you have more complex structures, with allocatable or pointer members in Fortran, mapping them with iso_c_bindings may be more difficult, and not always possible, at least not 1 to 1.

Best regards,

Yvan
jtache
Posts: 25
Joined: Thu Apr 19, 2012 4:01 pm

Re: use variables defined in cs_user_modules.f90 in a C routine

Post by jtache »

Thank you very much for your detailed guideline.
Indeed it worked well with constant and arrays with constant dimensions.
I'm now in the process of working with allocatable arrays, it's a little bit more difficult...
I've seen some elements in EXAMPLES directory that should help me.

Best regards,
Jeremie
jtache
Posts: 25
Joined: Thu Apr 19, 2012 4:01 pm

Re: use variables defined in cs_user_modules.f90 in a C routine

Post by jtache »

Dear Yvan,

I am finally stuck with allocatable arrays in Fortran for using them in C.
I used sthe subroutines provided in the cs_user_module-user-arrays.f90 routine in EXAMPLES to declare, init and pass array pointer to C with th ehelp of c_loc().
I initialized my array in cs_user_initialization.f90 :

Code: Select all

CALL init_train_elt(a_number, ncel)
and tried to used this array in cs_user_postprocess.c.
Unfortunately I got an error message related to this pointer at execution :
SIGSEGV signal (forbidden memory area access) intercepted!
.
I don't understand what I did wrong maybe because I'm not a pure C-developper.
Could you please have a look on the sources and tell me if you see something strange?
Any help would be welcome.
Attachments
cs_user_postprocess.c
(4.27 KiB) Downloaded 65 times
cs_user_modules.f90
(3.26 KiB) Downloaded 61 times
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: use variables defined in cs_user_modules.f90 in a C routine

Post by Yvan Fournier »

Hello,

I thinks the issue may be due to the fact that you are using a double pointer in the C code. Handling Using a pointer of pointers is a complex way of handling multi-dimensional arrays in C, and requires allocating and computing values for the intermediate array). It is much simpler single-dimensional array, with a n equivalence between

c_array[j*m + i]

and

f_array(i, j)

when f_array's dimensions are (m, n)

Best regards,

Yvan
jtache
Posts: 25
Joined: Thu Apr 19, 2012 4:01 pm

Re: use variables defined in cs_user_modules.f90 in a C routine

Post by jtache »

Hello Yvan,

I understand the limit. Rewriting my code with one-dimensional Fortran arrays (and simple pointer in C), it worked perfectly.
Thanks for your help!

Best regards,
Jeremie
Post Reply