Chapter 4.

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Advertisements

MPI Collective Communications
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
Distributed Memory Programming with MPI. What is MPI? Message Passing Interface (MPI) is an industry standard message passing system designed to be both.
ORNL is managed by UT-Battelle for the US Department of Energy Crash Course In Message Passing Interface Adam Simpson NCCS User Assistance.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Chapter 6 Parallel Sorting Algorithm Sorting Parallel Sorting Bubble Sort Odd-Even (Transposition) Sort Parallel Odd-Even Transposition Sort Related Functions.
2a.1 Message-Passing Computing More MPI routines: Collective routines Synchronous routines Non-blocking routines ITCS 4/5145 Parallel Computing, UNC-Charlotte,
1 MPI: Message-Passing Interface Chapter 2. 2 MPI - (Message Passing Interface) Message passing library standard (MPI) is developed by group of academics.
Part I MPI from scratch. Part I By: Camilo A. SilvaBIOinformatics Summer 2008 PIRE :: REU :: Cyberbridges.
1 Review –6 Basic MPI Calls –Data Types –Wildcards –Using Status Probing Asynchronous Communication Collective Communications Advanced Topics –"V" operations.
File Handling Spring 2013Programming and Data Structure1.
Parallel Programming with MPI Prof. Sivarama Dandamudi School of Computer Science Carleton University.
CS 838: Pervasive Parallelism Introduction to MPI Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from an online tutorial.
Message Passing Programming Model AMANO, Hideharu Textbook pp. 140-147.
Summary of MPI commands Luis Basurto. Large scale systems Shared Memory systems – Memory is shared among processors Distributed memory systems – Each.
File IO and command line input CSE 2451 Rong Shi.
Distributed-Memory (Message-Passing) Paradigm FDI 2004 Track M Day 2 – Morning Session #1 C. J. Ribbens.
Parallel Programming with MPI By, Santosh K Jena..
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
CSCI-455/522 Introduction to High Performance Computing Lecture 4.
An Introduction to MPI (message passing interface)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
2.1 Collective Communication Involves set of processes, defined by an intra-communicator. Message tags not present. Principal collective operations: MPI_BCAST()
Chapter 5. Nonblocking Communication MPI_Send, MPI_Recv are blocking operations Will not return until the arguments to the functions can be safely modified.
S an D IEGO S UPERCOMPUTER C ENTER N ATIONAL P ARTNERSHIP FOR A DVANCED C OMPUTATIONAL I NFRASTRUCTURE MPI 2 Part II NPACI Parallel Computing Institute.
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Message Passing Interface Using resources from
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
COMP7330/7336 Advanced Parallel and Distributed Computing MPI Programming: 1. Collective Operations 2. Overlapping Communication with Computation Dr. Xiao.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Introduction to parallel computing concepts and technics
MPI Basics.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
CS4402 – Parallel Computing
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Introduction to MPI.
File I/O.
MPI Message Passing Interface
CSC215 Lecture Input and Output.
Send and Receive.
CS111 Computer Programming
Parallel Programming with MPI and OpenMP
CS 584.
MPI_Bcast Bcast stands for broadcast, and is used to send data from one process to all other processes. The format for this function is: MPI_Bcast(&msg_address,#_elements,MPI_Type,
Send and Receive.
Introduction to Message Passing Interface (MPI)
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
Message Passing Models
CSE1320 Strings Dr. Sajib Datta
Lecture 14: Inter-process Communication
FILE HANDLING IN C.
Programming and Data Structure
File Input and Output.
File Handling.
Message-Passing Computing More MPI routines: Collective routines Synchronous routines Non-blocking routines ITCS 4/5145 Parallel Computing, UNC-Charlotte,
Lab Course CFD Parallelisation Dr. Miriam Mehl.
Line at a time I/O with fgets() and fputs()
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
Send and Receive.
Introduction to Parallel Computing with MPI
Hardware Environment VIA cluster - 8 nodes Blade Server – 5 nodes
Message-Passing Computing Message Passing Interface (MPI)
Parallel Processing - MPI
MPI Message Passing Interface
Some codes for analysis and preparation for programming
CS 584 Lecture 8 Assignment?.
Presentation transcript:

Chapter 4

MPI Send and Receive Sending and receiving are two foundational concepts of MPI Almost every single function in MPI can be implemented with basic send and receive calls

Overview of Sending and Receiving MPI’s send and receive calls operate in the following manner - Process 0 tries to send a message to process 1 - Process 0 packages the data into a buffer - Route the message to the right location -The location is defined by the process’s rank - Process 1 needs to acknowledge to want to receive 0’s data - Once process 1 acknowledges this, the data has been transmitted - Process 0 may go back to work

Overview of Sending and Receiving If Process 0 has to send different types of messages to processor 1 - MPI allows senders and receivers to specify message IDs with the message (tags) - Process 1 can request a message with a certain tag number - Messages with different tag numbers will be buffered until Processor is ready to receive them

Overview of Sending and Receiving Prototypes for MPI sending and receiving functions MPI_Send(void* data, int count, MPI_Datatype datatype, int destination, int tag, MPI_Comm communicator) MPI_Recv(void* data, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm communicator, MPI_Status* status)

Overview of Sending and Receiving The first argument is the data buffer The second and third arguments describe the count and type of elements that reside in the buffer MPI_Send sends the exact count of elements MPI_Recv will receive at most the count of elements The fourth and fifth arguments specify the rank of the sending/receiving process and the tag of the message The sixth argument specifies the communicator The last argument (for MPI_Recv only) provides information about the received message

MPI Data Types

Example 1 // Find out rank, size int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); int number; if (world_rank == 0) { number = -1; MPI_Send(&number, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); } else if (world_rank == 1) MPI_Recv(&number, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process 1 received number %d from process 0\n", number);

Example 2 int ping_pong_count = 0; int partner_rank = (world_rank + 1) % 2; while (ping_pong_count < PING_PONG_LIMIT) { if (world_rank == ping_pong_count % 2) { // Increment the ping pong count before you send it ping_pong_count++; MPI_Send(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD); printf("%d sent and incremented ping_pong_count " "%d to %d\n", world_rank, ping_pong_count, partner_rank); } else MPI_Recv(&ping_pong_count, 1, MPI_INT, partner_rank, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("%d received ping_pong_count %d from %d\n", world_rank, ping_pong_count, partner_rank);

Example 2 0 sent and incremented ping_pong_count 1 to 1 0 received ping_pong_count 2 from 1 0 sent and incremented ping_pong_count 3 to 1 0 received ping_pong_count 4 from 1 0 sent and incremented ping_pong_count 5 to 1 0 received ping_pong_count 6 from 1 0 sent and incremented ping_pong_count 7 to 1 0 received ping_pong_count 8 from 1 0 sent and incremented ping_pong_count 9 to 1 0 received ping_pong_count 10 from 1 1 sent and incremented ping_pong_count 1 to 0 1 received ping_pong_count 2 from 0 1 sent and incremented ping_pong_count 3 to 0 1 received ping_pong_count 4 from 0 1 sent and incremented ping_pong_count 5 to 0 1 received ping_pong_count 6 from 0 1 sent and incremented ping_pong_count 7 to 0 1 received ping_pong_count 8 from 0 1 sent and incremented ping_pong_count 9 to 0 1 received ping_pong_count 10 from 0

Example 3 int token; if (world_rank != 0) { MPI_Recv(&token, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("Process %d received token %d from process %d\n", world_rank, token, world_rank - 1); } else { // Set the token's value if you are process 0 token = -1; } MPI_Send(&token, 1, MPI_INT, (world_rank + 1) % world_size, 0, MPI_COMM_WORLD); // Now process 0 can receive from the last process. if (world_rank == 0) MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD, printf("Process %d received token %d from process %d\n", world_rank, token, world_size - 1);

Example 3 Initialize a value from process 0, and the value is passed around every single process Terminates when process zero receives the value from the last process Extra care is taken to assure that it doesn’t deadlock Process 0 makes sure that it has completed its first send before it tries to receive the value from the last process All of the other processes simply call MPI_Recv (receiving from their neighboring lower process) and then MPI_Send (sending the value to their neighboring higher process) to pass the value along the ring

Example 3 Process 1 received token -1 from process 0 Process 2 received token -1 from process 1 Process 3 received token -1 from process 2 Process 4 received token -1 from process 3 Process 0 received token -1 from process 4

File Input in C Use fopen() to create a new file or open an existing file Initialize an object of the type FILE FILE contains all the information necessary to control the stream Its prototype FILE *fopen( const char * filename, const char * mode );

File Input in C r Opens an existing text file for reading purpose. w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file. a Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content. r+ Opens a text file for reading and writing both. w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist. a+ Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

File Input in C Close a file using fclose() Its prototype int fclose( FILE *fp ); Return 0 on access, EOF if there is an error in closing the file EOF is a constant defined in the header file stdio.h.

File Input in C Reading a file using fgetc(), fgets(), and fscanf() fgetc() reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. It encounters a newline character '\n' or the end of the file EOF before they have read the maximum number of characters, then it returns only the characters read up to that point including new line character fscanf() function to read strings from a file but it stops reading after the first space character encounters.

Example 4 #include <stdio.h> main(int argc, char **argv) { FILE *fp; char buff[255]; fp = fopen(argv[1], "r"); fscanf(fp, "%s", buff); printf("1 : %s\n", buff ); fgets(buff, 255, (FILE*)fp); printf("2: %s\n", buff ); printf("3: %s\n", buff ); fclose(fp); }

Example 4 Assume the contents of the input file is This is a test program. It is used to test file input functions. When executing the program, its output is as follows. ./file input 1 : This 2: is a test program. 3: It is used to test file input functions.

Dynamic Memory Allocation in C The library stdlib.h should be included malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form Use free() explicitly to release space

Example 5 #include <stdio.h> #include <stdlib.h> main(int argc, char **argv){ FILE *fp; int n,i,*ptr,sum=0; fp=fopen(argv[1], "r"); fscanf(fp, "%d", &n); printf("The number of elements are %d.\n", n); ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); }

Example 5 for (i = 0; i < n; ++i) { if( fscanf(fp, "%d", ptr + i) != 1) { printf("wrong format: no enough numbers are given\n"); } printf("The elements of the array is: \n"); for(i=0;i<n;++i) { printf("%d ",*(ptr+i)); sum+=*(ptr+i); printf("\n"); printf("Sum=%d\n",sum); free(ptr);

Example 5 If the input file input1 is as follows cat input1 5 1 2 3 4 5 The output should be ./file1 input1 The number of elements are 5. The elements of the array is: Sum=15

MPI_Bcast() A broadcast is one of the standard collective communication techniques During a broadcast, one process sends the same data to all processes in a communicator One of the main uses of broadcasting is to send out user input to a parallel program, or send out configuration parameters to all processes

MPI_Bcast() Its prototype MPI_Bcast(void* data, int count, MPI_Datatype datatype, int root, MPI_Comm communicator) Each process has only one outgoing/incoming network link. The function is only using one network link from process 0 to send all the data More efficient implementation is a tree-based communication algorithm that can use more of the available network links at once

Example 6 #include <stdio.h> #include <mpi.h> int main(int argc, char **argv){ int i,myid, numprocs, count; int buffer[4]; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); count=4; if(myid == 0) { for(i=0;i<count;i++) buffer[i]=i; } MPI_Bcast(buffer,count,MPI_INT,source,MPI_COMM_WORLD); printf("%d ",buffer[i]); printf("This is from processor %d\n", myid); MPI_Finalize();

Example 6 The output should be 0 1 2 3 This is from processor 0