写一个fortran代码使用MPI并行的例子
时间: 2023-06-06 17:04:42 浏览: 232
好的,以下是一个简单的使用MPI并行的Fortran代码例子:
```
program main
integer :: rank, size, ierr, i, sum = 0, my_sum = 0
integer, parameter :: n = 1000
integer, dimension(n) :: array
call MPI_Init(ierr)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierr)
call MPI_Comm_size(MPI_COMM_WORLD, size, ierr)
! Initialize array to rank
do i = 1, n
array(i) = rank
end do
! Compute sum of elements in array for this process
do i = 1 + (rank - 1) * (n / size), rank * (n / size)
my_sum = my_sum + array(i)
end do
! Combine all the sums
call MPI_Reduce(my_sum, sum, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
if (rank == 0) then
write(*,*) 'The sum of all elements in the array is', sum
end if
call MPI_Finalize(ierr)
end program main
```
阅读全文