CS3211-Tut9 - CS3211 Parallel and Concurrent Programming...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS3211 Parallel and Concurrent Programming – Guidelines for tutorial (29 March – 2 April 2010) Sample Exercises: [Please conduct these as an interactive discussion, rather than an evaluation. Please also make it clear to the students that they are not being evaluated for their performance in these exercises, so that they are not afraid to make mistakes while answering.] MPI usage instructions https://www.comp.nus.edu.sg/cf/tembusu/mpi.html 1. Hello world program – discussed in class. #include "mpi.h" #include <stdio.h> int main(int argc, char *argv[]) { int rank, size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); printf("Hello world from processor %d of %d\n", rank, size); printf("Hello world\n"); MPI_Finalize(); return 0; } 2. Write a program that takes data from process zero and sends it to all of the other processes by sending it in sequence. That is, process i should receive the data and send it to process i+1, until the last process is reached.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <stdio.h> #include "mpi.h" int main( argc, argv ) int argc; char **argv; { int rank, value, size; MPI_Status status; MPI_Init ( &argc, &argv ); MPI_Comm_rank ( MPI_COMM_WORLD, &rank ); MPI_Comm_size ( MPI_COMM_WORLD, &size ); do { if (rank == 0) { scanf( "%d", &value ); MPI_Send ( &value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD ); } else { MPI_Recv ( &value, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &status ); if (rank < size - 1) MPI_Send ( &value, 1, MPI_INT, rank + 1, 0, MPI_COMM_WORLD ); } printf( "Process %d got %d\n", rank, value ); fflush(stdout); } while (value >= 0); MPI_Finalize ( ); return 0; } Sample output % mpicc -o ring ring.c % mpirun -np 4 ring 10 Process 0 got 10 22 Process 0 got 22 -1 Process 0 got -1 Process 3 got 10 Process 3 got 22 Process 3 got -1 Process 2 got 10 Process 2 got 22 Process 2 got -1 Process 1 got 10 Process 1 got 22 Process 1 got -1 %
Background image of page 2
3. Write a program to test how fair the message passing implementation is. To do this, have all processes except process 0 send 100 messages to process 0. Have process 0 print out the messages as it receives them, using MPI_ANY_SOURCE and MPI_ANY_TAG in MPI_Recv. Is the MPI implementation fair? Answer:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 3211 taught by Professor Dunnowho during the Spring '11 term at National University of Singapore.

Page1 / 8

CS3211-Tut9 - CS3211 Parallel and Concurrent Programming...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online