MPI example



Programming Assignment 1: Find the Partial Sum

1. Use MPI_Send () and MPI _Recv() to find the partial sum

(1) Log in penzias.csi.cuny.edu using your account and password. If you access it off campus, connect chizen.csi.cuny.edu first, and then ssh penzias.

(2) Create a new directory lab1 and enter the new directory.

(3) Write a program named parallelSum.c to complete the following tasks.

(a) Each processor will compute and print out a partial sum of N/p consecutive integers, where N and p are the total number of integers and the number of processors respectively. You can set N=1,000 in your program and define p=10 processors in PBS job script file, then processor 0 will print out the partial sum of 1+2+…+100, processor 1 will print out the partial sum of 101+102+…+200), …, and processor 9 will print out the partial sum of 901+902+…+1,000. (Note: You can declare a constant N=1,000 or use preprocessing statement #define N 1000.)

(c) All processors except processor 0 send their partial sums to processor 0 and print this message.

(d) After processor 0 receives the partial sums from other processors, it will print out the confirmation message to have received the value, add up all the partial sums including its own partial sum, and print out the total sum, the value of 1+2+3+… +1,000).

(4) Compile your program, using the following command:

mpicc parallelSum.c –o parallellSum

(5) create a job script named mpi.sh, type command of vi mpi.sh, then type the following commands:

#!/bin/bash

#BATCH --job-name slurm_mpi

#SBATCH --nodes 2

#SBATCH --ntasks 10

#SBATCH --ntasks-per-node 5

#SBATCH --mem 20000

#SBATCH --partition partedu

cd $SLURM_SUBMIT_DIR

echo 0 > cap

mpirun -np 10 ./hello

(6) Then run command:

chmod a+x mpi.sh

(7) Then run command:

sbatch mpi.sh

(8) Run the following command to check the status of your program:

squeue

(9) When you finish the program, check whether you have file named like slurm-xxxxx.out in the current directory. Open the file slurm-xxxxx.out and check the content. You should have the following contents:

:

Partial sum from processor 0 of total 10 processors is: 5050

Received 15050 from processor 1

Received 25050 from processor 2

Received 35050 from processor 3

Received 45050 from processor 4

Received 55050 from processor 5

Received 65050 from processor 6

Partial sum from process 2 of total 10 processors is: 25050

Send 25050 to processor 0 by processor 2

Partial sum from processor 1 of total 10 processors is: 15050

Partial sum from processor 6 of total 10 processors is: 65050

Partial sum from processor 4 of total 10 processors is: 45050

Send 45050 to processor 0 by processor 4

Partial sum from processor 5 of total 10 processors is: 55050

Send 55050 to processor 0 by processor 5

Partial sum from processor 3 of total 10 processors is: 35050

Send 15050 to processor 0 by processor 1

Send 65050 to processor 0 by processor 6

Send 35050 to processor 0 by processor 3

Received 75050 from processor 7

Partial sum from processor 7 of total 10 processors is: 75050

Send 75050 to processor 0 by processor 7

Partial sum from processor 9 of total 10 processors is: 95050

Send 95050 to processor 0 by processor 9

Partial sum from processor 8 of total 10 processors is: 85050

Received 85050 from processor 8

Received 95050 from processor 9

Total sum is 500500

Send 85050 to processor 0 by processor 8

2. Revise your program and script file accordingly so that your program is working for 20 nodes, and then run your program.

3. Use MPI_Bcast() and MPI_Reduce() routines to implement the program.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download