Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
program datatype1
use mpi
implicit none
integer, dimension(8,8) :: array
integer :: rank, ierr
integer :: indexedtype
integer, dimension(4) :: counts, displs
integer :: i, j
call mpi_init(ierr)
call mpi_comm_rank(MPI_COMM_WORLD, rank ,ierr)
! initialize arrays
if (rank == 0) then
do i=1,8
do j=1,8
array(i,j) = i*10 + j
end do
end do
else
array(:,:) = 0
end if
if (rank == 0) then
write(*,*) 'Data in rank 0'
do i=1,8
write(*,'(8I3)') array(i, :)
end do
end if
do i=1,4
counts(i) = i
displs(i) = i - 1 + 2 * (i - 1) * 8
end do
! create datatype
call mpi_type_indexed(4, counts, displs, MPI_INTEGER, indexedtype, ierr)
call mpi_type_commit(indexedtype, ierr)
! send first indexed of matrix
if (rank == 0) then
call mpi_send(array, 1, indexedtype, 1, 1, MPI_COMM_WORLD, ierr)
else if (rank == 1) then
call mpi_recv(array, 1, indexedtype, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, &
ierr)
end if
! Print out the result
if (rank == 1) then
write(*,*) 'Received data'
do i=1,8
write(*,'(8I3)') array(i, :)
end do
end if
call mpi_type_free(indexedtype, ierr)
call mpi_finalize(ierr)
end program datatype1