Skip to content

Commit e6059e4

Browse files
committed
Missing model solutions
1 parent dec00a4 commit e6059e4

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

mpi/simple-datatypes/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Write a program that sends the highlighted elements of a 2D array
44
using user defined datatypes from one MPI task to another. Note the
55
different assignments for C and Fortran, and remember that C stores
66
arrays in a row-major order and Fortran in a column-major order. You can
7-
start from skeleton codes in [C](./c) or [Fortran](./fortran)
7+
start from skeleton codes in [C](skeleton.c) or [Fortran](skeleton.F90)
88

99
a)
1010

mpi/simple-datatypes/skeleton.F90

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ program datatype1
2929
end if
3030

3131

32-
!TODO: create datatype describing one row, use mpi_type_vector
32+
!TODO: create datatype
3333

34-
!TODO: send first row of matrix from rank 0 to 1
34+
!TODO: communicate with datatype
35+
36+
!TODO: free datatype
3537

3638
! Print out the result
3739
if (rank == 1) then
@@ -41,7 +43,6 @@ program datatype1
4143
end do
4244
end if
4345

44-
!TODO free datatype
4546

4647
call mpi_finalize(ierr)
4748

mpi/simple-datatypes/skeleton.c

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ int main(int argc, char **argv)
4444
//TODO: Free datatype
4545

4646
// Print out the result on rank 1
47-
// The application is correct if the first column has the values of rank 0
4847
if (rank == 1) {
4948
printf("Received data\n");
5049
for (i = 0; i < 8; i++) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
program datatype1
2+
use mpi_f08
3+
implicit none
4+
5+
integer, dimension(8,8) :: array
6+
integer :: rank, ierr
7+
type(mpi_datatype) :: subarray
8+
integer, dimension(2) :: sizes, subsizes, offsets
9+
integer :: i, j
10+
11+
call mpi_init(ierr)
12+
call mpi_comm_rank(MPI_COMM_WORLD, rank ,ierr)
13+
14+
! initialize arrays
15+
if (rank == 0) then
16+
do i=1,8
17+
do j=1,8
18+
array(i,j) = i*10 + j
19+
end do
20+
end do
21+
else
22+
array(:,:) = 0
23+
end if
24+
25+
if (rank == 0) then
26+
write(*,*) 'Data in rank 0'
27+
do i=1,8
28+
write(*,'(8I3)') array(i, :)
29+
end do
30+
end if
31+
32+
! create datatype
33+
sizes = 8
34+
subsizes = 4
35+
offsets = 2
36+
call mpi_type_create_subarray(2, sizes, subsizes, offsets, MPI_ORDER_FORTRAN, MPI_INTEGER, subarray, ierr)
37+
call mpi_type_commit(subarray, ierr)
38+
39+
! send first row of matrix
40+
if (rank == 0) then
41+
call mpi_send(array(1, 1), 1, subarray, 1, 1, MPI_COMM_WORLD, ierr)
42+
else if (rank == 1) then
43+
call mpi_recv(array(1, 1), 1, subarray, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE, &
44+
ierr)
45+
end if
46+
47+
! Print out the result
48+
if (rank == 1) then
49+
write(*,*) 'Received data'
50+
do i=1,8
51+
write(*,'(8I3)') array(i, :)
52+
end do
53+
end if
54+
55+
call mpi_type_free(subarray, ierr)
56+
call mpi_finalize(ierr)
57+
58+
end program datatype1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include <mpi.h>
3+
4+
int main(int argc, char **argv)
5+
{
6+
int rank;
7+
int array[8][8];
8+
MPI_Datatype subarray;
9+
int i, j;
10+
11+
MPI_Init(&argc, &argv);
12+
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
13+
14+
15+
// Initialize arrays
16+
if (rank == 0) {
17+
for (i = 0; i < 8; i++) {
18+
for (j = 0; j < 8; j++) {
19+
array[i][j] = (i + 1) * 10 + j + 1;
20+
}
21+
}
22+
} else {
23+
for (i = 0; i < 8; i++) {
24+
for (j = 0; j < 8; j++) {
25+
array[i][j] = 0;
26+
}
27+
}
28+
}
29+
30+
if (rank == 0) {
31+
printf("Data in rank 0\n");
32+
for (i = 0; i < 8; i++) {
33+
for (j = 0; j < 8; j++) {
34+
printf("%3d", array[i][j]);
35+
}
36+
printf("\n");
37+
}
38+
}
39+
40+
41+
int sizes[2] = {8, 8};
42+
int subsizes[2] = {4, 4};
43+
int offsets[2] = {2, 2};
44+
// Create datatype
45+
MPI_Type_create_subarray(2, sizes, subsizes, offsets, MPI_ORDER_C, MPI_INT, &subarray);
46+
MPI_Type_commit(&subarray);
47+
48+
// Send first column of matrix
49+
if (rank == 0) {
50+
MPI_Send(&array[0][0], 1, subarray, 1, 1, MPI_COMM_WORLD);
51+
} else if (rank == 1) {
52+
MPI_Recv(&array[0][0], 1, subarray, 0, 1, MPI_COMM_WORLD,
53+
MPI_STATUS_IGNORE);
54+
}
55+
56+
// Print out the result
57+
if (rank == 1) {
58+
printf("Received data\n");
59+
for (i = 0; i < 8; i++) {
60+
for (j = 0; j < 8; j++) {
61+
printf("%3d", array[i][j]);
62+
}
63+
printf("\n");
64+
}
65+
}
66+
67+
MPI_Type_free(&subarray);
68+
MPI_Finalize();
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)