Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tensorflow and fortran 90? #33

Closed
xushanthu-2014 opened this issue May 31, 2021 · 1 comment
Closed

Support for tensorflow and fortran 90? #33

xushanthu-2014 opened this issue May 31, 2021 · 1 comment

Comments

@xushanthu-2014
Copy link

xushanthu-2014 commented May 31, 2021

Currently I am running an atmospheric model which was written by Fortran 90. In some subroutines I made some innovations using python (mainly tensorflow). Now I need to implement this python code into Fortran, so is there any wrapper to do this? just calling python within a Fortran subroutine?
I knew that your forpy could do this. But I am suspecting could it help me to import tensorflow into Fortran?

Here is the pseudo code of my basic thoughts:

SUBROUTINE demo(INPUTS, OUTPUTS)
! Definition of INPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: PINPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: POUTPUTS

PINPUTS = INPUTS

! using some python wrappers
POUTPUTS = python_wrapper(PINPUTS)

OUTPUTS = POUTPUTS
END SUBROUTINE demo

And in the python script "python_wrapper.py", I need to do:

import tensorflow as tf

# read saved model "model_saved";
# get the variables "OUTPUTS" passed from demo.f90;

prediction = model_saved.predict(OUTPUTS)

# then pass this variable "prediction" back to Fortran subroutine demo.f90;
# so that the "prediction" will be used for following calculation of the atmospheric model

And with forpy, can we simply the process and do the following in the demo.f90?

SUBROUTINE demo_sim(INPUTS, OUTPUTS)
! Definition of INPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: PINPUTS
REAL, DIMENSION(:,:,:,:), INTENT(IN) :: POUTPUTS

PINPUTS = INPUTS

! using forpy to import the tf model saved in my computer
python_model = forpy(./saved_model)
POUTPUTS = python_wrapper(PINPUTS)

OUTPUTS = POUTPUTS
END SUBROUTINE demo_sim

Or just in the demo.f90, can forpy do the wrapper things?

One more thing, the subroutine demo or. demo_sim needs to be called in other subroutine (for example, outer.f90) of the atmospheric model. So how can I use this subroutine? I see that in your readme file I need to write something like

# Python 3.8 and higher
gfortran -c forpy_mod.F90
gfortran intro_to_forpy.F90 forpy_mod.o `python3-config --ldflags --embed

So do I still need to write the above commands in the outer.f90?

Thanks a lot!

@ylikx
Copy link
Owner

ylikx commented Jun 4, 2021

Yes, it should be possible to do this with forpy. I think writing a python module to create a simple interface to tensorflow that is suited to your problem is a good approach. While you could import tensorflow and setup all the tensorflow stuff in Fortran in forpy, this would need a lot of boilerplate code.

I have not used tensorflow with forpy myself, but other people have:
#21 (comment)

It should also not be a problem to use the subroutine demo in several places in your code, I'd just recommend to do the initialisation of forpy (forpy_initialize) outside of the subroutine, e.g. in the beginning of your program.

@ylikx ylikx closed this as completed Jun 4, 2021
Repository owner locked and limited conversation to collaborators Jun 4, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants