msl.examples.loadlib.fortran32 module
A wrapper around a 32-bit FORTRAN library, fortran_lib32.
Example of a server that loads a 32-bit FORTRAN library, fortran_lib32,
in a 32-bit Python interpreter to host the library. The corresponding fortran64
module can be executed by a 64-bit Python interpreter and the Fortran64
class can send a request to the Fortran32
class which calls the 32-bit
library to execute the request and then return the response from the library.
- class msl.examples.loadlib.fortran32.Fortran32(host, port, **kwargs)[source]
Bases:
Server32
A wrapper around a 32-bit FORTRAN library, fortran_lib32.
This class demonstrates how to send/receive various data types to/from a 32-bit FORTRAN library via
ctypes
. For a summary of the FORTRAN data types see here.Note
Any class that is a subclass of
Server32
MUST provide two arguments in its constructor: host and port (in that order) and **kwargs. Otherwise theserver32
executable, seestart_server32
, cannot create an instance of theServer32
subclass.- sum_8bit(a, b)[source]
Add two 8-bit signed integers.
Python only has one
int
data type to represent integer values. Thesum_8bit()
method converts the data types of a and b to bectypes.c_int8
.The corresponding FORTRAN code is
function sum_8bit(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_8bit' :: sum_8bit implicit none integer(1) :: a, b, value value = a + b end function sum_8bit
See the corresponding 64-bit
sum_8bit()
method.
- sum_16bit(a, b)[source]
Add two 16-bit signed integers
Python only has one
int
data type to represent integer values. Thesum_16bit()
method converts the data types of a and b to bectypes.c_int16
.The corresponding FORTRAN code is
function sum_16bit(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_16bit' :: sum_16bit implicit none integer(2) :: a, b, value value = a + b end function sum_16bit
See the corresponding 64-bit
sum_16bit()
method.
- sum_32bit(a, b)[source]
Add two 32-bit signed integers.
Python only has one
int
data type to represent integer values. Thesum_32bit()
method converts the data types of a and b to bectypes.c_int32
.The corresponding FORTRAN code is
function sum_32bit(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_32bit' :: sum_32bit implicit none integer(4) :: a, b, value value = a + b end function sum_32bit
See the corresponding 64-bit
sum_32bit()
method.
- sum_64bit(a, b)[source]
Add two 64-bit signed integers.
Python only has one
int
data type to represent integer values. Thesum_64bit()
method converts the data types of a and b to bectypes.c_int64
.The corresponding FORTRAN code is
function sum_64bit(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'sum_64bit' :: sum_64bit implicit none integer(8) :: a, b, value value = a + b end function sum_64bit
See the corresponding 64-bit
sum_64bit()
method.
- multiply_float32(a, b)[source]
Multiply two FORTRAN floating-point numbers.
The corresponding FORTRAN code is
function multiply_float32(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'multiply_float32' :: multiply_float32 implicit none real(4) :: a, b, value value = a * b end function multiply_float32
See the corresponding 64-bit
multiply_float32()
method.
- multiply_float64(a, b)[source]
Multiply two FORTRAN double-precision numbers.
The corresponding FORTRAN code is
function multiply_float64(a, b) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'multiply_float64' :: multiply_float64 implicit none real(8) :: a, b, value value = a * b end function multiply_float64
See the corresponding 64-bit
multiply_float64()
method.
- is_positive(a)[source]
Returns whether the value of the input argument is > 0.
The corresponding FORTRAN code is
function is_positive(a) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'is_positive' :: is_positive implicit none logical :: value real(8) :: a value = a > 0.d0 end function is_positive
See the corresponding 64-bit
is_positive()
method.
- add_or_subtract(a, b, do_addition)[source]
Add or subtract two integers.
The corresponding FORTRAN code is
function add_or_subtract(a, b, do_addition) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'add_or_subtract' :: add_or_subtract implicit none logical :: do_addition integer(4) :: a, b, value if (do_addition) then value = a + b else value = a - b endif end function add_or_subtract
See the corresponding 64-bit
add_or_subtract()
method.
- factorial(n)[source]
Compute the n’th factorial.
The corresponding FORTRAN code is
function factorial(n) result(value) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'factorial' :: factorial implicit none integer(1) :: n integer(4) :: i double precision value if (n < 0) then value = 0.d0 print *, "Cannot compute the factorial of a negative number", n else value = 1.d0 do i = 2, n value = value * i enddo endif end function factorial
See the corresponding 64-bit
factorial()
method.
- standard_deviation(data)[source]
Compute the standard deviation.
The corresponding FORTRAN code is
function standard_deviation(a, n) result(var) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'standard_deviation' :: standard_deviation integer :: n ! the length of the array double precision :: var, a(n) var = SUM(a)/SIZE(a) ! SUM is a built-in fortran function var = SQRT(SUM((a-var)**2)/(SIZE(a)-1.0)) end function standard_deviation
See the corresponding 64-bit
standard_deviation()
method.
- besselJ0(x)[source]
Compute the Bessel function of the first kind of order 0 of x.
The corresponding FORTRAN code is
function besselj0(x) result(val) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'besselj0' :: besselj0 double precision :: x, val val = BESSEL_J0(x) end function besselJ0
See the corresponding 64-bit
besselJ0()
method.
- reverse_string(original)[source]
Reverse a string.
The corresponding FORTRAN code is
subroutine reverse_string(original, n, reversed) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'reverse_string' :: reverse_string !DEC$ ATTRIBUTES REFERENCE :: original, reversed implicit none integer :: i, n character(len=n) :: original, reversed do i = 1, n reversed(i:i) = original(n-i+1:n-i+1) end do end subroutine reverse_string
See the corresponding 64-bit
reverse_string()
method.
- add_1D_arrays(a1, a2)[source]
Perform an element-wise addition of two 1D double-precision arrays.
The corresponding FORTRAN code is
subroutine add_1d_arrays(a, in1, in2, n) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'add_1d_arrays' :: add_1d_arrays implicit none integer(4) :: n ! the length of the input arrays double precision :: in1(n), in2(n) ! the arrays to add (element-wise) double precision :: a(n) ! the array that will contain the element-wise sum a(:) = in1(:) + in2(:) end subroutine add_1D_arrays
See the corresponding 64-bit
add_1D_arrays()
method.
- matrix_multiply(a1, a2)[source]
Multiply two matrices.
The corresponding FORTRAN code is
subroutine matrix_multiply(a, a1, r1, c1, a2, r2, c2) !DEC$ ATTRIBUTES DLLEXPORT, ALIAS:'matrix_multiply' :: matrix_multiply implicit none integer(4) :: r1, c1, r2, c2 ! the dimensions of the input arrays double precision :: a1(r1,c1), a2(r2,c2) ! the arrays to multiply double precision :: a(r1,c2) ! resultant array a = MATMUL(a1, a2) end subroutine matrix_multiply
Note
FORTRAN stores multi-dimensional arrays in column-major order, as opposed to row-major order for C (Python) arrays. Therefore, the input matrices need to be transposed before sending the matrices to FORTRAN and also the result needs to be transposed before returning the result.
See the corresponding 64-bit
matrix_multiply()
method.