# Calling Fortran subroutine from Julia passing array arguments

**URL:** https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340
**Category:** General Usage
**Tags:** fortran
**Created:** [April 11, 2022, 10:28am UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340 "2022-04-11T10:28:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@Marie](https://discourse.julialang.org/u/Marie)
#### Post date: [April 11, 2022, 10:28am UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/1 "2022-04-11T10:28:24Z")

</div>

Fortran subroutine looks like this:

```julia
 subroutine MatMult(a, b, m, n, k, answer)
        !DEC$ ATTRIBUTES DLLEXPORT :: MatMult
        !DEC$ ATTRIBUTES ALIAS: 'MatMult' :: MatMult
        !DEC$ ATTRIBUTES REFERENCE :: a
        !DEC$ ATTRIBUTES REFERENCE :: b
        !DEC$ ATTRIBUTES REFERENCE :: answer
        !DEC$ ATTRIBUTES VALUE :: m, n, k
        integer, intent(in) :: m, n, k
        real(8), intent(in) :: a(m, n)
        real(8), intent(in) :: b(n, k)
        real(8), intent(out) :: answer(m, k)
        !this is just a test
        answer = transpose(matmul(transpose(a), transpose(b)));
    end subroutine

```

Fortran’s code is compiled from f90 into a dll.  
In Julia I call MatMult passing my arrays

```julia
function mult()
    matrixSize = 2
    mA = [[2 3] ; [4 5]] 
    mB = [[2 1] ; [4 1]] 
    mC = zeros(Float64, matrixSize, matrixSize)
    ccall((:MatMult, "FortranWorker.dll"), Cvoid, (Ptr{Matrix{Float64}}, Ptr{Matrix{Float64}}, Int64, Int64, Int64,Ptr{Matrix{Float64}}), mA, mB, matrixSize, matrixSize, matrixSize, mC)
end

```

The array returned contains all zeroes. Unfortunately I can’t step into fortran code when debugging in VS Code. I’ve tried compiling fortran code as .so library - not the dll - but then Julia pretends not see my .so library. Could be because fortran is in f90 format and not in f95?  
Any suggestions are appreciated!!!

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 11, 2022, 1:37pm UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/2 "2022-04-11T13:37:07Z")

</div>

This works:

```julia
% gfortran teste.f90 -shared -o test.so

```

```julia
julia> a = rand(3,3); b = rand(3,3); 

julia> answer = zeros(3,3);

julia> ccall((:matmult_, "./test.so"), Nothing, (Ref{Float64}, Ref{Float64}, Ref{Int}, Ref{Int}, Ref{Int}, Ref{Float64}), a, b, 3, 3, 3, answer)

julia> answer
3×3 Matrix{Float64}:
 0.181291 0.829994 1.25334
 0.311001 0.804451 0.874293
 0.386725 0.91879 0.93275

```

I’m not sure if this is a toy example, but of course you can do in Julia:

```julia
julia> transpose(a) * transpose(b)
3×3 Matrix{Float64}:
 0.181291 0.311001 0.386725
 0.829994 0.804451 0.91879
 1.25334 0.874293 0.93275

#or

julia> using LinearAlgebra

julia> mul!(answer, transpose(a), transpose(b))
3×3 Matrix{Float64}:
 0.181291 0.311001 0.386725
 0.829994 0.804451 0.91879
 1.25334 0.874293 0.93275

```

ps: I didn’t transpose the answer in my examples, and I noticed not that you do it in the fortran code.

---

<div class="post-metadata">

### Author: ![Marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@Marie](https://discourse.julialang.org/u/Marie)
#### Post date: [April 11, 2022, 1:52pm UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/3 "2022-04-11T13:52:07Z")

</div>

Where is your test.so located? I put mine in the same folder as Julia source code - but it tells me it cannot find that file

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 11, 2022, 1:58pm UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/4 "2022-04-11T13:58:46Z")

</div>

Here in the same directory where I started Julia. Note the `./` in `./test.so`, without it it does not find the library.

---

<div class="post-metadata">

### Author: ![Marie](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@Marie](https://discourse.julialang.org/u/Marie)
#### Post date: [April 11, 2022, 2:10pm UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/5 "2022-04-11T14:10:16Z")

</div>

julia\> ccall((:MatMult, “./test.so”), Nothing, (Ref{Float64}, Ref{Float64}, Ref{Int}, Ref{Int}, Ref{Int}, Ref{Float64}), a, b, 3, 3, 3, answer)  
ERROR: could not load library “./test.so”  
The specified module could not be found.

So sorry for taking your time on this - not sure why it’s not finding that library. Could be smth wrong with the lib itself? I did get compilation warning when creating the .so from fortran source: Warning: Legacy Extension: REAL array index at (1)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [April 11, 2022, 2:13pm UTC](https://discourse.julialang.org/t/calling-fortran-subroutine-from-julia-passing-array-arguments/79340/6 "2022-04-11T14:13:25Z")

</div>

I think you have not started Julia in the same directory where the library is.

Also, you need to use `:matmult_` (like I posted), otherwise it does not work either (don’t ask me why one needs that underline at the end).
