Skip to content
Snippets Groups Projects
Commit c6aaa5b4 authored by Chao Zhan's avatar Chao Zhan
Browse files

update slides, add waitall

parent 370e4f35
Branches
No related tags found
No related merge requests found
PROG = nb_waitall
OBJECTS = nb_waitall.o
CC = mpicc
CFLAGS = -Wall -std=c11
CFLAGS += -I. # add the current directory to the include path
$(PROG): $(OBJECTS) # link the object files into a binary
$(CC) $(CFLAGS) $^ -o $@
.Phone: run
run: $(PROG) # build and run the program
mpirun ./$(PROG);
$(OBJECTS): %.o: %.c # compile the source files into object files
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean: # remove the object files and the binary
rm -f $(OBJECTS) $(PROG)
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
MPI_Init(&argc, &argv);
// Get the number of processes
int num_processes;
MPI_Comm_size(MPI_COMM_WORLD, &num_processes);
// Get the rank of the process
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Request *requests =
(MPI_Request *) malloc(num_processes * sizeof(MPI_Request));
MPI_Status *statuses =
(MPI_Status *) malloc(num_processes * sizeof(MPI_Status));
char *recv_buffers = (char *)malloc(num_processes * sizeof(char));
char send_buffers = 'A';
send_buffers += rank;
MPI_Isend(&send_buffers, 1, MPI_CHAR, 0, 0,
MPI_COMM_WORLD, &requests[rank]);
// all processes send to rank 0
if (rank == 0) {
for (int i = 0; i < num_processes; i++)
MPI_Irecv(&recv_buffers[i], 1, MPI_CHAR, i, 0,
MPI_COMM_WORLD, &requests[i]);
MPI_Waitall(num_processes, requests, statuses);
for (int i = 0; i < num_processes; i++) {
printf("Process 0 received token %c from process %d\n", recv_buffers[i],
statuses[i].MPI_SOURCE);
}
}
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
...@@ -600,3 +600,28 @@ If **MPI_Request** is Null, both **MPI_Wait** and **MPI_Test** returns immediate ...@@ -600,3 +600,28 @@ If **MPI_Request** is Null, both **MPI_Wait** and **MPI_Test** returns immediate
### Demo ### Demo
See live demo. See live demo.
---
title: Non-Blocking Request Testing 2
---
### Test and Wait on Many Requests
**MPI_Waitany / MPI_Testany**:
- Wait for one of the specified requests to complete and free it
- Test if one of the specified requests has completed and free it if it did
**MPI_Waitall / MPI_Testall**:
- Wait for all the specified requests to complete and free them
- Test if all of the specified requests have completed and free them if they have
**MPI_Waitsome / MPI_Testsome**
- Wait for any number of the specified requests to complete and free them
- Test if any number of the specified requests have completed and free these that have
Use **MPI_STATUSES_IGNORE** to ignore status from -all/-some operations.
See live demo.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment