我正在學習有關Fortran中的BCASTing數據類型,並有一個代碼,它從終端獲取兩個值並在每個進程上顯示它們。對於integer/integer和integer/real類型的組合value1/value2,這適用,但對於整數/實數* 8組合,則失敗。派生的數據類型與MPI
的代碼是:
use mpi
implicit none
integer :: ierror, pid, ncpu, root = 0
integer :: counts, newtype, extent
integer, dimension(2) :: oldtypes, blockcounts, offsets
type value
integer :: value1 = 0
real*8 :: value2
end type
type (value) input
call MPI_INIT(ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, pid, ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, ncpu, ierror)
! setup of 1 MPI_INTEGER field: value1
offsets(1) = 0
oldtypes(1) = MPI_INTEGER
blockcounts(1) = 1
! setup of 1 MPI_REAL8 field: value2
call MPI_TYPE_EXTENT(MPI_INTEGER, extent, ierror) !determine offset of MPI_INTEGER
offsets(2) = blockcounts(1)*extent !offset is 1 MPI_INTEGER extents
oldtypes(2) = MPI_REAL8
blockcounts(2) = 1
! define struct type and commit
counts = 2 !for MPI_INTEGER + MPI_REAL8
call MPI_TYPE_STRUCT(counts, blockcounts, offsets, &
oldtypes, newtype, ierror)
call MPI_TYPE_COMMIT(newtype, ierror)
do while (input%value1 >= 0)
if (pid == root) then
read(*,*) input
write(*,*) 'input was: ', input
end if
call MPI_BCAST(input, 1, newtype, &
root, MPI_COMM_WORLD, ierror)
write(*,*), 'process ', pid, 'received: ', input
end do
call MPI_TYPE_FREE(newtype, ierror)
call MPI_FINALIZE(ierror)
它可以檢查通過改變相應的聲明和OLDTYPE該整數/整數和整數/實做工精細。整數/實數* 8組合失敗,例如,輸入-1 2.0生成:
input was: -1 2.0000000000000000
process 0 received: -1 2.0000000000000000
process 1 received: -1 0.0000000000000000
process 2 received: -1 0.0000000000000000
process 3 received: -1 0.0000000000000000
This線程有類似的問題,建議使用MPI_TYPE_EXTENT是不正確的,因爲可能有未考慮額外的填充。不幸的是,我還沒有能夠解決這個問題,希望這裏有人能夠啓發我。
提前THX
+1很好的答案和例子。 – casey
+1謝謝澄清。我已經在玩MPI_GET_ADDRESS,但沒有得到它的工作。你的例子非常漂亮! – nluigi