iTranslated by AI
Running External Programs from Julia
According to this, Julia can call C and Fortran routines using ccall. However, since you have to compile them as shared libraries, it seems difficult to call a main program that exchanges data via standard input and standard output. The theme of this note is to make it possible to call programs from Julia without modifying the original program. Methods for calling external programs, not limited to C or Fortran, are generally described in
but there are few samples, and there are various pitfalls in a Windows environment, so I will provide supplementary explanations. The environment is as follows.
versioninfo()
Julia Version 1.6.2
Commit 1b93d53fc4 (2021-07-14 15:36 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Hello World!
Let's jump right in and do a Hello World!. Use run() when a return value is not needed (standard output is fine), and read() or readchomp() when you want to receive the result. Note that commands are enclosed in backticks.
run(`echo hello`)
read(`echo hello`, String)
readchomp(`echo hello`)
run(`cmd /C echo hello`)
hello
read(`cmd /C echo hello`, String)
"hello\r\n"
readchomp(`cmd /C echo hello`)
"hello"
run(`powershell /C echo hello`)
hello
read(`powershell /C echo hello`, String)
"hello\r\n"
readchomp(`powershell /C echo hello`)
"hello"
The types are as follows. Don't worry about the hello being interspersed in the output.
println("1. ", typeof(`cmd /C echo hello`))
println("2. ", typeof(run(`cmd /C echo hello`)))
println("3. ", typeof(read(`cmd /C echo hello`, String)))
println("4. ", typeof(readchomp(`cmd /C echo hello`)))
1. Cmd
hello
2. Base.Process
3. String
4. SubString{String}
You can also call programs like Notepad notepad.exe.
run(`notepad`)
run(`notepad.exe`)
run(`notepad.exe program1.f90`)
Standard Output Only
This is the same as Hello World!, but let's check it anyway. The following program1.f90 is a program that simply returns the numerical value 4 to standard output. We will call the compiled program1.exe and check the behavior of the standard output. (The files are in the repository. If gfortran is installed and the path is set, clicking compile.bat will compile it automatically.)
program main
write(6,*) 4
end program main
run(`program1`);
4
run(`program1.exe`);
4
run(`cmd /C program1.exe`)
4
read(`program1.exe`, String)
" 4\r\n"
readchomp(`program1.exe`)
" 4"
Passing a single variable from standard input
The following program2.f90 reads standard input with a read statement, squares the numerical value, and returns it to standard output. We will call the compiled program2.exe to check its operation. (The files are in the repository. If gfortran is installed and the path is set, clicking compile.bat will compile it automatically.)
program main
implicit none
integer x
read(5,*) x
write(6,*) x**2
end program main
5
First, standard input can be passed from the command line using <, but < should be enclosed in single quotes like '<'.
run(`cmd /C program2.exe '<' input2.txt`)
25
To execute without going through the command prompt, use pipeline(). You can pass a filename or a command to the stdin argument. Note that filenames are in double quotes, while commands are in backticks.
run(pipeline(`program2.exe`, stdin="input2.txt"))
25
run(pipeline(`program2.exe`, stdin=`cmd /C echo 6`))
36
You can also pass standard input using an open statement as follows. Since it seems you cannot receive the result this way, run is likely sufficient. One advantage, if any, is that you can use for loops or choose between write, print, and println between open and end.
open(`program2.exe`, "w", stdout) do io
println(io, 7)
end
49
io = open(`program2.exe`, "w", stdout)
println(io, 7)
close(io)
49
Passing two variables from standard input
The following program3.f90 reads standard input with a read statement, takes the sum of two numerical values, and returns it to standard output. It is basically the same as the previous example, but the Fortran program is set up to read two numerical values. We will call the compiled program3.exe to check its operation. (The files are in the repository. If gfortran is installed and the path is set, clicking compile.bat will compile it automatically.)
program main
implicit none
double precision x, y
read(5,*) x, y
write(6,*) x + y
end program main
5
6
run(pipeline(`program3.exe`, stdin="input3.txt"))
11.000000000000000
run(pipeline(`program3.exe`, stdin=`cmd /C echo 5 7`))
12.000000000000000
open(`program3.exe`, "w", stdout) do io
println(io, "5 8")
end
13.000000000000000
input = "5
9"
open(`program3.exe`, "w", stdout) do io
println(io, input)
end
14.000000000000000
open(`program3.exe`, "w", stdout) do io
println(io, 5)
println(io, 10)
end
15.000000000000000
If you want to pass the input as an array, handle it as follows.
input = [5,11]
run(pipeline(`program3.exe`, stdin=`cmd /C echo $input`))
16.000000000000000
input = [5,12]
open(`program3.exe`, "w", stdout) do io
for x in input
println(io, x)
end
end
17.000000000000000
Treating an external program as a function
Let's explain using program3.exe as an example. To treat this program as a function, a return value is required, so use read() or readchomp(). Since these results are strings, use parse() to further convert the type to a numerical value.
f(X) = parse(Float64,readchomp(pipeline(`program3.exe`, stdin=`cmd /C echo $X`)))
y = f([5,13])
18.0
Parameter optimization of external programs
Here we use Optim.jl. You must install it beforehand by running add Optim in package mode and declare using Optim in your notebook. First, let's look at an example of minimizing the Rosenbrock function.
# using Pkg
# Pkg.add("Optim")
using Optim
rosenbrock_julia(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
x0 = [0.0, 0.0]
opt = optimize(rosenbrock_julia, x0)
println(Optim.minimizer(opt))
println(Optim.minimum(opt))
[0.9999634355313174, 0.9999315506115275]
3.5255270584829996e-9
The following program4.f90 is an example that receives variables program4.exe as a Julia function and optimize
program main
implicit none
double precision x, y
read(5,*) x, y
write(6,*) 100.*(x*x-y)**2+(1.-x)**2
end program main
rosenbrock_fortran(x) = parse(Float64,readchomp(pipeline(`program4.exe`, stdin=`cmd /C echo $x`)))
x0 = [0.0, 0.0]
opt = optimize(rosenbrock_fortran, x0)
println(Optim.minimizer(opt))
println(Optim.minimum(opt))
[0.9999634355313174, 0.9999315506115275]
3.5255270584829996e-9
Exactly the same results were obtained in both the Julia-only example and the example linked with Fortran.
Discussion