代做DTS202TC Foundation of Parallel Computing Lab 5: MPI代写Processing

- 首页 >> C/C++编程

DTS202TC Foundation of Parallel Computing

Lab 5: MPI

Task 1, OpenMPI

OpenMPI is one of the Message Passing Interface (MPI) implementations. Turn on the virtual machine from Lab 1, then verify the installation:

1 mpicc --version

Task 2, Simple OpenMPI Program

Type the following source code in a mpi hello.c file.

Compile the source code by:

1 mpicc -g -Wall -o mpi_hello mpi_hello.c

Run the program by:

1 mpiexec -n 4 ./mpi_hello

Figure 1: For task 2

Task 3

The following code estimates the value of the mathematical constant PI. Write an MPI version of estimate_pi that uses all available processes to do the work. Use MPI Send and MPI_Recv to communicate between processes.

1 /*Estimate PI*/

2

3 #include <stdio.h>

4 #include <math.h>

5 #include <stdlib.h>

6 #include <sys/time.h>

7

8 /* The argument now should be a double (not a pointer to a double) */

9 #define GET_TIME(now) { \

10 struct timeval t; \

11 gettimeofday(&t, NULL); \

12 now = t.tv_sec + t.tv_usec /1000000.0; \

13 }

14

15 double serial_pi(long long n) {

16 double sum = 0.0;

17 long long i;

18 double factor = 1.0;

19

20 for (i = 0; i < n; i++, factor = -factor) {

21 sum += factor/(2*i+1);

22 }

23 return 4.0*sum;

24

25 }

26

27 int main(int argc , char** argv) {

28 double start , finish;

29 GET_TIME(start);

30 double estimate_of_pi = serial_pi (1000000000) ;

31 printf("\nEstimated of pi:   %1.10f.\n", estimate_of_pi);

32 GET_TIME(finish);

33

34 printf("\nAcutal value of pi:   %1.10f.\n\n", atan(1) *4);

35 printf("The elapsed time is %e seconds\n", finish -start);

36 }

Analyse the performance of the serial and MPI versions by simply printing out the total execution time. Also compare the performance with different number of processes.

Task 4 (Optional)

Ask AI for more MPI practices if you have spare time.




站长地图