Fortran32¤
Wrapper around a 32-bit FORTRAN library.
Example of a server that loads a 32-bit library, fortran_lib32, in a 32-bit Python interpreter to host the library. The corresponding Fortran64 class is created in a 64-bit Python interpreter and the Fortran64 class sends requests to the Fortran32 class which calls the 32-bit library to execute the request and then returns the response from the library.
Fortran32 ¤
Fortran32(host, port)
Bases: Server32
Wrapper around a 32-bit FORTRAN library.
This class demonstrates how to pass various data types to/from a 32-bit FORTRAN library via ctypes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
host
|
str
|
The IP address (or hostname) to use for the server. |
required |
port
|
int
|
The port to open for the server. |
required |
Source code in src/msl/examples/loadlib/fortran32.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
add_1d_arrays ¤
add_1d_arrays(a1, a2)
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 Fortran64.add_1d_arrays method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a1
|
Sequence[float]
|
First array. |
required |
a2
|
Sequence[float]
|
Second array. |
required |
Returns:
Type | Description |
---|---|
list[float]
|
The element-wise addition of |
Source code in src/msl/examples/loadlib/fortran32.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
add_or_subtract ¤
add_or_subtract(a, b, *, do_addition)
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 Fortran64.add_or_subtract method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First integer. |
required |
b
|
int
|
Second integer. |
required |
do_addition
|
bool
|
Whether to add or subtract the numbers. |
required |
Return
a + b
if do_addition
is True
else a - b
.
Source code in src/msl/examples/loadlib/fortran32.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
besselJ0 ¤
besselJ0(x)
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 Fortran64.besselJ0 method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
The value to compute |
required |
Returns:
Type | Description |
---|---|
float
|
The value of |
Source code in src/msl/examples/loadlib/fortran32.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
factorial ¤
factorial(n)
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 Fortran64.factorial method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
The integer to computer the factorial of. The maximum allowed value is 127. |
required |
Returns:
Type | Description |
---|---|
float
|
The factorial of |
Source code in src/msl/examples/loadlib/fortran32.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
is_positive ¤
is_positive(a)
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 Fortran64.is_positive method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
float
|
Double-precision number. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether the value of |
Source code in src/msl/examples/loadlib/fortran32.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
matrix_multiply ¤
matrix_multiply(a1, a2)
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 multidimensional arrays in column-major order, as opposed to row-major order like 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.
See the corresponding Fortran64.matrix_multiply method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a1
|
Sequence[Sequence[float]]
|
First matrix. |
required |
a2
|
Sequence[Sequence[float]]
|
Second matrix. |
required |
Returns:
Type | Description |
---|---|
list[list[float]]
|
The product, |
Source code in src/msl/examples/loadlib/fortran32.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
|
multiply_float32 ¤
multiply_float32(a, b)
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 Fortran64.multiply_float32 method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
float
|
First floating-point number. |
required |
b
|
float
|
Second floating-point number. |
required |
Returns:
Type | Description |
---|---|
float
|
The product, |
Source code in src/msl/examples/loadlib/fortran32.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
multiply_float64 ¤
multiply_float64(a, b)
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 Fortran64.multiply_float64 method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
float
|
First double-precision number. |
required |
b
|
float
|
Second double-precision number. |
required |
Returns:
Type | Description |
---|---|
float
|
The product, |
Source code in src/msl/examples/loadlib/fortran32.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
reverse_string ¤
reverse_string(original)
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 Fortran64.reverse_string method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
original
|
str
|
The original string. |
required |
Returns:
Type | Description |
---|---|
str
|
The string reversed. |
Source code in src/msl/examples/loadlib/fortran32.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
|
standard_deviation ¤
standard_deviation(data)
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 Fortran64.standard_deviation method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Sequence[float]
|
The values to compute the standard deviation of. |
required |
Returns:
Type | Description |
---|---|
float
|
The standard deviation of |
Source code in src/msl/examples/loadlib/fortran32.py
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
sum_16bit ¤
sum_16bit(a, b)
Add two 16-bit signed integers.
Python only has one int data type to represent integer values.
This method converts the data types of a
and b
to be
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 Fortran64.sum_16bit method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First 16-bit signed integer. |
required |
b
|
int
|
Second 16-bit signed integer. |
required |
Returns:
Type | Description |
---|---|
int
|
The sum, |
Source code in src/msl/examples/loadlib/fortran32.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
sum_32bit ¤
sum_32bit(a, b)
Add two 32-bit signed integers.
Python only has one int data type to represent integer values.
This method converts the data types of a
and b
to be
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 Fortran64.sum_32bit method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First 32-bit signed integer. |
required |
b
|
int
|
Second 32-bit signed integer. |
required |
Returns:
Type | Description |
---|---|
int
|
The sum, |
Source code in src/msl/examples/loadlib/fortran32.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
sum_64bit ¤
sum_64bit(a, b)
Add two 64-bit signed integers.
Python only has one int data type to represent integer values.
This method converts the data types of a
and b
to be
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 Fortran64.sum_64bit method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First 64-bit signed integer. |
required |
b
|
int
|
Second 64-bit signed integer. |
required |
Returns:
Type | Description |
---|---|
int
|
The sum, |
Source code in src/msl/examples/loadlib/fortran32.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
|
sum_8bit ¤
sum_8bit(a, b)
Add two 8-bit signed integers.
Python only has one int data type to represent integer values.
This method converts the data types of a
and b
to be
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 Fortran64.sum_8bit method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First 8-bit signed integer. |
required |
b
|
int
|
Second 8-bit signed integer. |
required |
Returns:
Type | Description |
---|---|
int
|
The sum, |
Source code in src/msl/examples/loadlib/fortran32.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|