Commit e87ffa4b authored by Ciarán Ó Rourke's avatar Ciarán Ó Rourke
Browse files

Add Fortran collective communication solutions

parent f848e48a
......@@ -53,12 +53,12 @@ int main(int argc, char *argv[]) {
print_complex_number(&z, rank);
/*
* create MPI_CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
* create CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
* is dealing with
*/
MPI_Datatype MPI_CUSTOM_COMPLEX;
MPI_Type_contiguous(2, MPI_INT, &MPI_CUSTOM_COMPLEX);
MPI_Type_commit(&MPI_CUSTOM_COMPLEX);
MPI_Datatype CUSTOM_COMPLEX;
MPI_Type_contiguous(2, MPI_INT, &CUSTOM_COMPLEX);
MPI_Type_commit(&CUSTOM_COMPLEX);
/* TODO: create the complex summation custom operation */
......@@ -66,6 +66,7 @@ int main(int argc, char *argv[]) {
/* TODO: print the result on the root process */
MPI_Type_free(&CUSTOM_COMPLEX)
MPI_Finalize();
return 0;
}
......@@ -36,11 +36,11 @@ end subroutine print_complex_number
subroutine create_random_complex_number(z)
implicit none
type(comp_int), intent(out) :: z
real :: rvec(2)
real :: rvec(10)
call random_number(rvec)
z%a = rvec(1)*20 - 10
z%b = rvec(2)*20 - 10
z%a = rvec(9)*20 - 10
z%b = rvec(10)*20 - 10
return
end subroutine create_random_complex_number
......@@ -51,7 +51,7 @@ program complex_algebra
use mpi
use mod_comp
implicit none
integer :: ierror, rank, MPI_CUSTOM_COMPLEX
integer :: ierror, rank, CUSTOM_COMPLEX
type(comp_int) :: z
integer (kind=4) :: i,seed(33)
......@@ -60,7 +60,7 @@ program complex_algebra
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierror)
seed = 10244 + rank
seed = 10244 + rank*1000
i = 33
call random_seed(size=i)
call random_seed(put=seed)
......@@ -70,11 +70,11 @@ program complex_algebra
call print_complex_number(z, rank)
!
! create MPI_CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
! create CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
! is dealing with
!
call MPI_Type_contiguous(2, MPI_INTEGER4, MPI_CUSTOM_COMPLEX, ierror)
call MPI_Type_commit(MPI_CUSTOM_COMPLEX, ierror)
call MPI_Type_contiguous(2, MPI_INTEGER4, CUSTOM_COMPLEX, ierror)
call MPI_Type_commit(CUSTOM_COMPLEX, ierror)
!! TODO: create the complex summation custom operation !!
......@@ -82,7 +82,8 @@ program complex_algebra
!! TODO: print the result on the root process !!
call MPI_Finalize(ierror);
call MPI_Type_free(CUSTOM_COMPLEX, ierror)
call MPI_Finalize(ierror)
end program complex_algebra
......@@ -53,20 +53,20 @@ int main(int argc, char *argv[]) {
print_complex_number(&z, rank);
/*
* create MPI_CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
* create CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
* is dealing with
*/
MPI_Datatype MPI_CUSTOM_COMPLEX;
MPI_Type_contiguous(2, MPI_INT, &MPI_CUSTOM_COMPLEX);
MPI_Type_commit(&MPI_CUSTOM_COMPLEX);
MPI_Datatype CUSTOM_COMPLEX;
MPI_Type_contiguous(2, MPI_INT, &CUSTOM_COMPLEX);
MPI_Type_commit(&CUSTOM_COMPLEX);
/* create the complex summation custom operation */
MPI_Op MPI_COMPLEX_SUM;
MPI_Op_create(complex_sum, 1, &MPI_COMPLEX_SUM);
MPI_Op COMPLEX_SUM;
MPI_Op_create(complex_sum, 1, &COMPLEX_SUM);
/* perform the reduction */
Complex z_result;
MPI_Reduce(&z, &z_result, 1, MPI_CUSTOM_COMPLEX, MPI_COMPLEX_SUM, 0, MPI_COMM_WORLD);
MPI_Reduce(&z, &z_result, 1, CUSTOM_COMPLEX, COMPLEX_SUM, 0, MPI_COMM_WORLD);
/* print the result on the root process */
MPI_Barrier(MPI_COMM_WORLD);
......@@ -75,6 +75,9 @@ int main(int argc, char *argv[]) {
print_complex_number(&z_result, rank);
}
MPI_Op_free(&CUSTOM_COMPLEX);
MPI_Type_free(&CUSTOM_COMPLEX)
MPI_Finalize();
return 0;
}
module mod_comp
type comp_int
integer (kind=4) :: a,b
end type
contains
subroutine complex_sum(in, inout, len)
implicit none
type(comp_int), intent(in) :: in(len)
type(comp_int), intent(inout) :: inout(len)
integer (kind=4), intent(in) :: len
integer (kind=4) :: i
type(comp_int) :: z
do i = 1, len
z%a = in(i)%a + inout(i)%a;
z%b = in(i)%b + inout(i)%b;
inout(i) = z
end do
return
end subroutine complex_sum
subroutine print_complex_number(z, rank)
implicit none
type(comp_int), intent(in) :: z
integer :: rank
write(6,'(a,i0,a,i0,a,i0,a)') ' Rank ',rank,': z = ',z%a,' + ',z%b,'i'
end subroutine print_complex_number
subroutine create_random_complex_number(z)
implicit none
type(comp_int), intent(out) :: z
real :: rvec(10)
call random_number(rvec)
z%a = rvec(9)*20 - 10
z%b = rvec(10)*20 - 10
return
end subroutine create_random_complex_number
end module mod_comp
program complex_algebra
use mpi
use mod_comp
implicit none
integer :: ierror, rank, CUSTOM_COMPLEX, COMPLEX_SUM
type(comp_int) :: z, z_result
integer (kind=4) :: i,seed(33)
call MPI_Init(ierror)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierror)
seed = 10244 + rank*1000
i = 33
call random_seed(size=i)
call random_seed(put=seed)
call create_random_complex_number(z)
call print_complex_number(z, rank)
!
! create CUSTOM_COMPLEX datatype so MPI can know what memory pattern it
! is dealing with
!
call MPI_Type_contiguous(2, MPI_INTEGER4, CUSTOM_COMPLEX, ierror)
call MPI_Type_commit(CUSTOM_COMPLEX, ierror)
! create the complex summation custom operation
call MPI_Op_create(complex_sum, .TRUE., COMPLEX_SUM, ierror)
! perform the reduction
call MPI_Reduce(z, z_result, 1, CUSTOM_COMPLEX, COMPLEX_SUM, 0, MPI_COMM_WORLD, ierror)
! print the result on the root process
if (rank .eq. 0) then
write(6,*) ' Result: '
call print_complex_number(z_result, rank)
endif
call MPI_Op_free(COMPLEX_SUM, ierror)
call MPI_Type_free(CUSTOM_COMPLEX, ierror)
call MPI_Finalize(ierror)
end program complex_algebra
program hello_world
use mpi
implicit none
integer :: ierror, rank, unisize
integer (kind=4) :: i
call MPI_Init(ierror)
call MPI_Comm_rank(MPI_COMM_WORLD, rank, ierror)
call MPI_Comm_size(MPI_COMM_WORLD, unisize, ierror)
! loop over processes in order
do i = 0,unisize-1
if (rank .EQ. i) then
write(6,*) ' I am process ',rank,' of ',unisize,' processes'
endif
!
! impose a barrier between prints to ensure correct order of writes to
! stdout
!
call MPI_Barrier(MPI_COMM_WORLD, ierror)
end do
call MPI_Finalize(ierror)
end program hello_world
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment