Commit ca1c4e8c authored by mohamadbashar.disoki's avatar mohamadbashar.disoki

Update README.md

parents
# Makefile for MPI program
# Source file
SRC = hello_world.cpp
# number of process
NP = 4
# Compiler
CC = mpic++
# Executable name
EXE = ./out
all: $(EXE)
$(EXE): $(SRC)
$(CC) -o $(EXE) $(SRC)
clean:
rm -f $(EXE)
run:
mpirun -np $(NP) -f mpi_hosts $(EXE)
\ No newline at end of file
# MPI LAB
*topics*
* Blocking point-to-point communication
* Sending and receiving with MPI_Send and MPI_Recv
* Collective communication
* MPI_Bcast
* MPI_Scatter, MPI_Gather, and MPI_Allgather
*Some Useful links:*
* [What is a Makefile and how does it work?](https://opensource.com/article/18/8/what-how-makefile)
* [MPI Tutorial](https://mpitutorial.com/)
* [Simulating MPI Applications](https://simgrid.org/doc/latest/)
## Compile MPI programs
To compile your code using make, edit Makefile to specify program name, number of process, mpi compiler
```
$ make
```
## Run MPI programs
```
$ make run
```
## Clean MPI programs
```
$ make clean
```
\ No newline at end of file
master
slave1 user=mpiuser
slave2 user=mpiuser
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
void my_bcast(void* data, int count, MPI_Datatype datatype, int root,
MPI_Comm communicator) {
int world_rank;
MPI_Comm_rank(communicator, &world_rank);
int world_size;
MPI_Comm_size(communicator, &world_size);
if (world_rank == root) {
// If we are the root process, send our data to everyone
int i;
for (i = 0; i < world_size; i++) {
if (i != world_rank) {
MPI_Send(data, count, datatype, i, 0, communicator);
}
}
} else {
// If we are a receiver process, receive the data from the root
MPI_Recv(data, count, datatype, root, 0, communicator, MPI_STATUS_IGNORE);
}
}
int main(int argc, char** argv) {
MPI_Init(NULL, NULL);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int data;
if (world_rank == 0) {
data = 100;
printf("Process 0 broadcasting data %d\n", data);
my_bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
} else {
my_bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("Process %d received data %d from root process\n", world_rank, data);
}
MPI_Finalize();
}
\ No newline at end of file
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
const int PING_PONG_LIMIT = 10;
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// 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);
// We are assuming 2 processes for this task
if (world_size != 2) {
fprintf(stderr, "World size must be two for %s\n", argv[0]);
MPI_Abort(MPI_COMM_WORLD, 1);
}
int ping_pong_count = 0;
while (ping_pong_count < PING_PONG_LIMIT)
{
if (world_rank == 0)
{
ping_pong_count++;
MPI_Send(&ping_pong_count, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
printf("%d sent and incremented ping_pong_count %d to %d\n",
world_rank, ping_pong_count, 1);
MPI_Recv(&ping_pong_count, 1, MPI_INT, 1, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("%d received ping_pong_count %d from %d\n",
world_rank, ping_pong_count, 1);
}else
{
MPI_Recv(&ping_pong_count, 1, MPI_INT, 0, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("%d received ping_pong_count %d from %d\n",
world_rank, ping_pong_count, 0);
ping_pong_count++;
MPI_Send(&ping_pong_count, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
printf("%d sent and incremented ping_pong_count %d to %d\n",
world_rank, ping_pong_count, 0);
}
}
MPI_Finalize();
}
\ No newline at end of file
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
void prefix_mpi(int* block_array, int block_size, int* block_prefix, MPI_Comm communicator)
{
/*
Add your code here
*/
}
int main(int argc, char** args)
{
MPI_Init(&argc, &args);
int my_rank;
int com_size;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &com_size);
int total_array_size = 2048;
if (total_array_size % com_size != 0)
total_array_size = (total_array_size / com_size + 1) * com_size;
int block_size = total_array_size / com_size;
int* total_array = NULL;
int* total_prefix = NULL;
if (my_rank == 0)
{
total_array = (int*)malloc(total_array_size * sizeof(int));
total_prefix = (int*)malloc(total_array_size * sizeof(int));
for (int i = 0; i < total_array_size; i++)
total_array[i] = rand() % 11;
}
int* block_array = (int*)malloc(block_size * sizeof(int));
int* block_prefix = (int*)malloc(block_size * sizeof(int));
MPI_Scatter(total_array, block_size, MPI_INT,
block_array, block_size, MPI_INT, 0, MPI_COMM_WORLD);
prefix_mpi(block_array, block_size, block_prefix, MPI_COMM_WORLD);
MPI_Gather(block_prefix, block_size, MPI_INT,
total_prefix, block_size, MPI_INT, 0, MPI_COMM_WORLD);
int accum = 0;
if (my_rank == 0)
{
for (int i = 1; i < total_array_size; i++)
{
accum += total_array[i - 1];
if (total_prefix[i] != accum)
printf("Error at index %i: %i expected, %i computed\n", i, accum, total_prefix[i]);
}
printf("Test completed!\n");
free(total_array);
free(total_prefix);
}
free(block_array);
free(block_prefix);
MPI_Finalize();
return 0;
}
\ No newline at end of file
#include <mpi.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
void reduce_tree(
int* send_data,
int* recv_data,
int count,
MPI_Comm communicator)
{
/*
Add your code here
*/
}
void reduce_sequential(
int* send_data,
int* recv_data,
int count,
MPI_Comm communicator)
{
int my_rank;
int com_size;
MPI_Comm_rank(communicator, &my_rank);
MPI_Comm_size(communicator, &com_size);
int* gather_buffer = NULL;
if (my_rank == 0)
{
gather_buffer = (int*) calloc(count * com_size, sizeof(int));
}
MPI_Gather(send_data, count, MPI_INT, gather_buffer, count, MPI_INT, 0, communicator);
if (my_rank == 0)
{
memset(recv_data, 0, count * sizeof(int));
for (int p = 0; p < com_size; p++)
for (int i = 0; i < count; i++)
recv_data[i] += gather_buffer[count * p + i];
free(gather_buffer);
}
}
int main(int argc, char** args)
{
MPI_Init(&argc, &args);
int count = 10;
int max_value = 64;
int* recv_array_tree = NULL;
int* recv_array_sequential = NULL;
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank == 0)
{
recv_array_tree = (int*) malloc(count * sizeof(int));
recv_array_sequential = (int*) malloc(count * sizeof(int));
}
int* send_array = (int*)malloc(count * sizeof(int));
for (int i = 0; i < count; i++)
send_array[i] = my_rank;
reduce_tree(send_array, recv_array_tree, count, MPI_COMM_WORLD);
reduce_sequential(send_array, recv_array_sequential, count, MPI_COMM_WORLD);
if (my_rank == 0)
{
for (int i = 0; i < count; i++)
if (recv_array_tree[i] == recv_array_sequential[i])
printf("At index %i: reduce_tree is %i, reduce_sequential is %i\n",
i, recv_array_tree[i], recv_array_sequential[i]);
free(recv_array_tree);
free(recv_array_sequential);
}
free(send_array);
MPI_Finalize();
return 0;
}
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// 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 token;
// Receive from the lower process and send to the higher process. Take care
// of the special case when you are the first process to prevent deadlock.
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. This makes sure that at
// least one MPI_Send is initialized before all MPI_Recvs (again, to prevent
// deadlock)
if (world_rank == 0) {
MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("Process %d received token %d from process %d\n", world_rank, token,
world_size - 1);
}
MPI_Finalize();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment