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

add question code

parent 2fe16696
No related branches found
No related tags found
No related merge requests found
OBJECTS = q1.o q2.o q3.o q4.o q5.o
CC = mpicc
CFLAGS = -Wall -std=c11
CFLAGS += -I. # add the current directory to the include path
PHONY_RUNS = run1 run2 run3 run4 run5
PHONY_CLEANS = clean1 clean2 clean3 clean4 clean5
.PHONY: all
all: $(patsubst %.o,%,$(OBJECTS)) # build all binaries
.PHONY: $(PHONY_RUNS)
$(PHONY_RUNS): run%: q% # run a binary
time mpirun $<
$(patsubst %.o,%,$(OBJECTS)): %: %.o # link the object files into a binary
$(CC) $(CFLAGS) $^ -o $@
$(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) $(patsubst %.o,%,$(OBJECTS))
.PHONY: $(PHONY_RUNS)
$(PHONY_CLEANS): clean%: q% # run a binary
rm -f $<.o $<
# Code from the questions
This folder contains all the programs that are presented in the question PDF.
## Build
```c
# build all programs
make
# run specific program
make run1
make run2
...
make run5
# clean specific build
make clean1
make clean2
...
make clean5
# clean all builds
make clean
```
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv) {
int sbuffer = 1;
int rbuffer = 2;
const int buflen = 1;
MPI_Init(&argc, &argv);
// Get the number of processes
int nprocs;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
// Get the rank of the process
int procid;
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Send(&sbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD);
printf("Process %d blocking sent message to process %d\n", procid, p);
}
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Recv(&rbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("Process %d blocking received message from process %d\n", procid, p);
}
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
int main(int argc, char **argv) {
int sbuffer = 1;
int rbuffer = 2;
const int buflen = 1;
MPI_Init(&argc, &argv);
// Get the number of processes
int nprocs;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
// Get the rank of the process
int procid;
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Recv(&rbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d blocking received message from process %d\n", procid, p);
}
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Send(&sbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD);
printf("Process %d blocking sent message to process %d\n", procid, p);
}
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const int buflen = 1;
MPI_Init(&argc, &argv);
// Get the number of processes
int nprocs;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
int *sbuffers = calloc(nprocs, sizeof(int));
int rbuffer = 0;
MPI_Request *requests = (MPI_Request *) malloc(nprocs * sizeof(MPI_Request));
// Get the rank of the process
int procid;
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
int ireq = 0;
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Isend(&sbuffers[p], buflen, MPI_INT, p, 0, MPI_COMM_WORLD, &(requests[ireq++]));
printf("Process %d non-blocking sent message to process %d\n", procid, p);
}
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Recv(&rbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Process %d blocking received message from process %d\n", procid, p);
}
MPI_Waitall(nprocs-1, requests, MPI_STATUSES_IGNORE);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const int buflen = 1;
MPI_Init(&argc, &argv);
// Get the number of processes
int nprocs;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
int *rbuffers = calloc(nprocs, sizeof(int));
int sbuffer = 0;
MPI_Request *requests = (MPI_Request *) malloc(nprocs * sizeof(MPI_Request));
// Get the rank of the process
int procid;
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
int ireq = 0;
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Irecv(&rbuffers[p], buflen, MPI_INT, p, 0, MPI_COMM_WORLD, &(requests[ireq++]));
printf("Process %d non-blocking received message from process %d\n", procid, p);
}
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Send(&sbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD);
printf("Process %d blocking send message from process %d\n", procid, p);
}
MPI_Waitall(nprocs-1, requests, MPI_STATUSES_IGNORE);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
const int buflen = 1;
MPI_Init(&argc, &argv);
// Get the number of processes
int nprocs;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
int *rbuffers = calloc(nprocs, sizeof(int));
int sbuffer = 0;
MPI_Request *requests = (MPI_Request *) malloc(nprocs * sizeof(MPI_Request));
// Get the rank of the process
int procid;
MPI_Comm_rank(MPI_COMM_WORLD, &procid);
int ireq = 0;
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Irecv(&rbuffers[p], buflen, MPI_INT, p, 0, MPI_COMM_WORLD, &(requests[ireq++]));
printf("Process %d non-blocking received message from process %d\n", procid, p);
}
MPI_Waitall(nprocs-1, requests, MPI_STATUSES_IGNORE);
for (int p = 0; p < nprocs; p++) {
if (p != procid)
MPI_Send(&sbuffer, buflen, MPI_INT, p, 0, MPI_COMM_WORLD);
printf("Process %d blocking send message from process %d\n", procid, p);
}
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment