1
我試圖使用gfortran在GNU平臺上編譯一些專有的Fortran代碼。有誰知道例程sortqq
(Intel)或qsort_up
(IBM)是否有開放源代碼的對應項?英特爾Fortan排序例程的開源版本
我試圖使用gfortran在GNU平臺上編譯一些專有的Fortran代碼。有誰知道例程sortqq
(Intel)或qsort_up
(IBM)是否有開放源代碼的對應項?英特爾Fortan排序例程的開源版本
libc包含qsort()。如果你在Fortran中爲它寫一個bind(c)接口,你可以調用它。
爲了讓您開始,這裏是提供模仿的專有功能my_qsort包裝樣品模塊:
$ cat sort.f90
module m
use, intrinsic :: iso_c_binding
implicit none
private
interface
subroutine qsort(base, nel, width, compar) bind(c, name='qsort')
import c_size_t, c_int
implicit none
type(*), intent(inout) :: base(*)
integer(c_size_t), value :: nel
integer(c_size_t), value :: width
abstract interface
function compar_iface(a, b) bind(c)
import c_int, c_ptr
implicit none
integer(c_int) compar_iface
type(c_ptr), value :: a, b
end function
end interface
procedure(compar_iface) compar
end subroutine
end interface
interface my_qsort
module procedure my_qsort_int4
module procedure my_qsort_int8
module procedure my_qsort_real4
module procedure my_qsort_real8
end interface
public my_qsort
contains
subroutine my_qsort_int4(a, nel)
integer(c_int), intent(inout) :: a(*)
integer(4), value :: nel
call qsort(a, int(nel, c_size_t), c_sizeof(a(1)), less_int4)
end subroutine
subroutine my_qsort_int8(a, nel)
integer(c_long_long), intent(inout) :: a(*)
integer(4), value :: nel
call qsort(a, int(nel, c_size_t), c_sizeof(a(1)), less_int8)
end subroutine
subroutine my_qsort_real4(a, nel)
real(c_float), intent(inout) :: a(*)
integer(4), value :: nel
call qsort(a, int(nel, c_size_t), c_sizeof(a(1)), less_real4)
end subroutine
subroutine my_qsort_real8(a, nel)
real(c_double), intent(inout) :: a(*)
integer(4), value :: nel
call qsort(a, int(nel, c_size_t), c_sizeof(a(1)), less_real8)
end subroutine
function less_int4(a, b) result(result)
integer(c_int) result
type(c_ptr), value :: a, b
integer(c_int), pointer :: ap, bp
call c_f_pointer(a, ap)
call c_f_pointer(b, bp)
result = int(ap - bp, c_int)
end function
function less_int8(a, b) result(result)
integer(c_int) result
type(c_ptr), value :: a, b
integer(c_long_long), pointer :: ap, bp
call c_f_pointer(a, ap)
call c_f_pointer(b, bp)
result = int(ap - bp, c_int)
end function
function less_real4(a, b) result(result)
integer(c_int) result
type(c_ptr), value :: a, b
real(c_float), pointer :: ap, bp
call c_f_pointer(a, ap)
call c_f_pointer(b, bp)
result = int(ap - bp, c_int)
end function
function less_real8(a, b) result(result)
integer(c_int) result
type(c_ptr), value :: a, b
real(c_double), pointer :: ap, bp
call c_f_pointer(a, ap)
call c_f_pointer(b, bp)
result = int(ap - bp, c_int)
end function
end module
program main
use m
implicit none
integer(4) a(10)
real(4) b(4)
a = [ 2, 6 , 1, 3, 10, 9, 7, 8, 4, 5 ]
call my_qsort(a, 10)
print *, a
b = [ 2.0, 5.0, 1.0, -5.0 ]
call my_qsort(b, 4)
print *, b
end program
$ gfortran sort.f90
$ ./a.out
1 2 3 4 5 6 7 8 9 10
-5.00000000 1.00000000 2.00000000 5.00000000
$
我得到了你的榜樣的工作,但是當我嘗試它推廣到我的具體情況,編譯器警告'less_real()'函數中的'a - b'可能會導致轉換中的值發生變化。我也遇到了一堆接口不匹配錯誤:類型在參數'a'(TYPE(*)/ REAL(8))中不匹配'等等[Here](https://gist.github.com/tyleransom/e118fd44a63a614138d4a28e4d14da7f)是一個包含所有調用以及完整錯誤輸出的GitHub gist。你碰巧知道這裏發生了什麼? –
在這種情況下'a-b'警告是無害的,但可以通過使用int()函數的顯式轉換來刪除。至於類型不匹配的錯誤,我的老版本gfortran沒有發現這種錯誤。我在編輯中解決了這兩個問題以回答問題。 –