Hi,

When I tried the following Fortran routine (abc.f), it worked using the 
following shared file and ccall. 
But when I tried after changing  DOUBLE PRECISION with REAL, it did not 
work. How should it be fixed.

This is what I saw on the Internet and worked:
  
abc.f file I saw on the Internet

       SUBROUTINE MULTIPLY(A,B,C) 
        DOUBLE PRECISION A,B,C 
        C = A*B 
        RETURN 
        END 

gfortran abc.f -o abc.so -shared -fPIC

a = 2.0
b = 4.2
n = Float64[1.0]
 
  ccall((:multiply_, "abc.so"), Void, 
(Ptr{Float64},Ptr{Float64},Ptr{Float64}),&a,&b, n)

julia> n
1-element Array{Float64,1}:
 8.4

Again ccall worked with DOUBLE PRECISION.

When I used REAL like the following:
      SUBROUTINE MULTIPLY(A,B,C) 
        DOUBLE PRECISION A,B,C 
        C = A*B 
        RETURN 
        END 

and used the same procedure like

gfortran abc.f -o abc.so -shared -fPIC

a = 2.0
b = 4.2
julia> n = Array(Float64)
0-dimensional Array{Float64,0}:
0.0

julia> ccall((:multiply_, "abc.so"), Void,  
(Ptr{Float64},Ptr{Float64},Ptr{Float64}), &a,  &b, n)

julia> n
0-dimensional Array{Float64,0}:
1.061e-314 

The default value of 0.0 changed to 1.061e-314, which is not the correct 
answer.

When I assigned a specific number to n: 
julia> n = Float64[1.0]
1-element Array{Float64,1}:
 1.0

julia> ccall((:multiply_, "abc.so"), Void,  
(Ptr{Float64},Ptr{Float64},Ptr{Float64}), &a,  &b, n)

Then the array element value was not changed.
julia> n
1-element Array{Float64,1}:
 1.0

I am wondering why it does not work with REAL variables. 
I tried Logical, Int, and Character variables, along with Double Precision.
All worked, except for REAL.

Thanks,

On Sunday, May 31, 2015 at 8:53:15 PM UTC-4, Eduardo Lenz wrote:
>
>
> Hi
> I am using the dsplp subroutine  (Linear Programming) from LINPACK. It is 
> also in FORTRAN and I made a simple interface to use it in Julia. Although 
> the data format used internally by the dsplp subroutine is diferent, I made 
> the following function in julia to emulate the linpro package from Scilab 
> (without the equality constraint, but it can be changed).
>
> The julia function is 
>
> <script src="https://gist.github.com/anonymous/9e4bcda40a5e66f9f104.js
> "></script>
>
> and the fortran subroutine is 
>
> <script src="https://gist.github.com/CodeLenz/223f3efe4b9e386059e2.js
> "></script>
>
> To generate the .so, I use this fortran subroutine and some of the fortran 
> functions from LINPACK;
>
> The usage is simple like this
>
> function Linpro_Test()
>
> # Min 2x[1] + 3x[2]
> # S.T 
> #         -x[1] - x[2] <= -1.0
> #
> #       0<= x[1] <= 5
> #       0<= x[2] <= 5
> #
> # Solution x = [1,0]
>
>   n = 2
>   m = 1
>   p = [2.0, 3.0]
>   C = [-1.0 -1.0]
>   b = [-1.0]
>   ci = [0.0 , 0.0]
>   cs = [5.0 , 5.0]
>
>   x,lagr,info = Linpro_Slatec(n,m,p,C,b,ci,cs)
>
>   println("\n Result ",x)
>   println("\n Lagr      ",lagr)
>   println("\n Info      ",info)
>
> end
>
>
>
> On Saturday, May 30, 2015 at 10:52:03 PM UTC-3, Jiahao Chen wrote:
>>
>> It would be great if you could clean up your example and add it to the 
>> documentation.
>>
>> Thanks,
>>
>> Jiahao Chen
>> Research Scientist
>> MIT CSAIL
>>
>> On Sun, May 31, 2015 at 3:17 AM, Andre Bieler <[email protected]> 
>> wrote:
>>
>>> Ok so I have a few simple examples working for ccalling fortran 
>>> functions and subroutines from Julia.
>>> Maybe someone will find this useful examples when first looking into 
>>> calling fortran from julia.
>>>
>>> compile the following fortran mod
>>> ```
>>> !fileName = simplemodule.f95
>>> module simpleModule
>>>
>>> implicit none
>>>
>>> contains
>>> function foo(x)
>>>   integer :: foo, x
>>>   foo = x * 2
>>> end function foo
>>>
>>> subroutine bar(x, a, b)
>>>   integer, intent(in) :: x
>>>   integer, intent(out) :: a, b
>>>   
>>>   a = x + 3
>>>   b = x * 3
>>> end subroutine bar
>>>
>>> subroutine keg(x, a, b)
>>>   real*8, intent(in) :: x
>>>   real*8, intent(out) :: a, b
>>>   
>>>   a = x + 3.0
>>>   b = x * 3.0
>>> end subroutine keg
>>>
>>> subroutine ruf(x, y)
>>>   real*8, dimension(3), intent(in) :: x
>>>   real*8, dimension(3), intent(out) :: y
>>>   integer :: i
>>>   
>>>   DO i = 1, 3
>>>     y(i) = 2*x(i)
>>>   END DO
>>> end subroutine ruf
>>>
>>> end module simplemodule
>>> ```
>>>
>>
>>

Reply via email to