Could LFortran work as an embedded scripting language in a compiled program

@certik

Have you given any thought of allowing the scripting capability of LFortran to be embedded in a compiled program. Basically what I would like to do is have the ability to define some tasks (say on-the-fly plotting or specifying time varying initial conditions or loading functions (in FEM codes) as scripts instead of trying to account for every possible scenario of loading or plots etc in the main code base. The only other way to get something approaching this capability is to use shared objects (dlls) and build capability to load and unload them at runtime. The downside of this is it still forces the user to go through the standard compile process but now you have the complication of dealing with shared objects and all the issues that come with them if your main code is linked with static archives etc.

I’ve seen this type of capability in a Sandia code (CTH) where they use SLANG to provide a method to do on-the-fly plotting using a script thats part of the input file. I know other scripting languages like Lua can be embedded in compiled codes ( I believe Fortran is supported). I think it would be a nice feature if LFortran could be embedded in a compiled code and parse either standard Fortran or a defined subset on-the fly.

Just my 2 cents
RW

2 Likes

Yes, I discussed this exact idea with a few people already. Essentially LFortran could allow you to compile your main project (with any compiler), then call LFortran as a library from your project to allow runtime evaluation of scripts written in Fortran. That way you don’t have to embed Python or Lua, you just use Fortran. You can imagine implementing some kind of user function that gets compiled and used by the main code, and that user function can even run in some inner loop, because it will be fast. A lot of times users of computational codes written in Fortran already know some Fortran, so it would be natural to use it as a scripting language also.

2 Likes

@rwmsu ,

Instead have you considered the execute_command_line option in standard Fortran? It may be a poor Fortranner option until you’ve sophisticated options in better tooling, such as with LFortran.

# Say you have a script in hw.py
print("Python script says Hello World!")
! Invoke the Python script in Fortran
   print *, "Executing Python script:"
   call execute_command_line(command="hw.py")
end

C:\Temp>ifort /standard-semantics p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.3.0 Build 20210609_000000
Copyright (C) 1985-2021 Intel Corporation. All rights reserved.

Microsoft (R) Incremental Linker Version 14.29.30038.1
Copyright (C) Microsoft Corporation. All rights reserved.

-out:p.exe
-subsystem:console
p.obj

C:\Temp>p.exe
Executing Python script:
Python script says Hello World!

C:\Temp>

2 Likes

Plugging Julia in a Fortran process is also possible, and thanks to fpm, it is easy also.

Example

program main
use julia
use, intrinsic :: iso_c_binding
implicit none
real(kind=c_double), allocatable :: mat(:,:)

! Load Julia into Fortran process
call julia_init()

! Run Julia script.jl and save the return value into mat
call julia_run("script.jl", mat)

! Fortran can now use the values computed in Julia
print *, 'shape:', shape(mat)
print *, 'sum:', sum(mat)

! Bye
call julia_destroy()

end program

where script.jl is a Julia program,

using LinearAlgebra
# write code here...
# and return a matrix
return [1.1 2.2; 4.4 5.5]

This is in experimental status, but works (compute and access numbers, vectors, matrices and strings from Julia). The restriction is currently the GC, that is disabled. Need to translate Julia macros JL_GC_PUSH, JL_GC_POP “inline” in Fortran. I don’t know if this is possible and I welcome contributions under the MIT license!.

8 Likes

See also: j3-fortran/fortran_proposals: Dynamic Eval for Fortran

2 Likes

Yes, exactly as @une showed with Julia, I would like to see it with Fortran via LFortran.

One can easily call any language such as Lua, Python or Julia using this approach, but why not Fortran?

3 Likes