Skip to content

Commit

Permalink
Some examples of mpi
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeterd committed Oct 30, 2020
1 parent 9acc17a commit 7a3b0b4
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 0 deletions.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi1
Binary file not shown.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi2
Binary file not shown.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi3
Binary file not shown.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi4
Binary file not shown.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi5
Binary file not shown.
Binary file added C/Advanced/Parallel/MPI/Praticas/mpi6
Binary file not shown.
44 changes: 44 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int numprocs, rank, tag, num, i;
MPI_Status status;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != 2) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with 2 processes!\n");
exit(0);
}

printf("Proc %d starting...\n", rank);
tag = 0;
if (rank == 0) {
for (i = 0; i < 10; i++) {
num = i * 10;
MPI_Send(&num, 1, MPI_INT, 1, tag, MPI_COMM_WORLD);
printf("Proc %d sent int '%d'\n", rank, num);
}
}
if (rank == 1) {
for (i = 0; i < 10; i++) {
MPI_Recv(&num, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
printf("Proc %d received int '%d'\n", rank, num);
}
}

MPI_Finalize();
}

/**
PROBLEMA: Linha 31 tipo da menssagem errado!
CORREÇAO: Mudar MPI_CHAR para MPI_INT!
*/
48 changes: 48 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int numprocs, rank, tag, count;
char inmsg, outmsg;
MPI_Status status;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != 2) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with 2 processes!\n");
exit(0);
}

printf("Proc %d starting...\n", rank);
tag = rank;
if (rank == 0) {
outmsg = 'x';
MPI_Send(&outmsg, 1, MPI_CHAR, 1, tag, MPI_COMM_WORLD);
printf("Proc %d sent char '%c'\n", rank, outmsg);
MPI_Recv(&inmsg, 1, MPI_CHAR, 1, tag, MPI_COMM_WORLD, &status);
printf("Proc %d received char '%c'\n", rank, inmsg);
} else if (rank == 1) {
outmsg = 'y';
MPI_Recv(&inmsg, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD, &status);
printf("Proc %d received char '%c'\n", rank, inmsg);
MPI_Send(&outmsg, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
printf("Proc %d sent char '%c'\n", rank, outmsg);
}
MPI_Get_count(&status, MPI_CHAR, &count);
printf("Proc %d received %d char(s) from proc %d with tag %d\n",
rank, count, status.MPI_SOURCE, status.MPI_TAG);

MPI_Finalize();
}

/*
PROBLEMA: Ambos os processos ficam bloqueados no MPI_RECV
CORREÇAO: No Revc e Send do processo 1 mudar a tag para 0
*/
59 changes: 59 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MSGSIZE 34000 // try with different sizes!

int main(int argc, char *argv[]) {
int numprocs, rank, tag, src, dest, count, i;
char data[MSGSIZE];

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != 2) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with 2 processes!\n");
exit(0);
}

printf("Proc %d starting...\n", rank);
tag = 0;
dest = 1;
src = 0;
count = 0;
if (rank == 0) {
for (i = 0; i < MSGSIZE; i++)
data[i] = 'x';
while (count < MSGSIZE) {
MPI_Send(data, MSGSIZE, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
count ++;
printf("Sent message number %d to proc %d...\n", count, dest);
}
}
if (rank == 1) {
while (count < MSGSIZE) {
MPI_Recv(data, MSGSIZE, MPI_CHAR, src, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
count++;
printf("Received message number %d from proc %d...\n", count, src);
//sleep(1);
}
for(i=0; i<MSGSIZE; i++) {
printf("%c%d", data[i], i);
}
printf("\n");
}

MPI_Finalize();
}

/*
PROBLEMA: Loop infinitos nos dois processos
CORREÇAO: condiçao de paragem
*/
48 changes: 48 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 4

int main(int argc, char *argv[]) {
int numprocs, rank, tag, src, dest, i;
float a[SIZE][SIZE] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 };
float b[SIZE];
MPI_Datatype rowtype;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != SIZE) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with %d processes!\n", SIZE);
exit(0);
}

printf("Proc %d starting...\n", rank);
tag = 0;
src = 0;

MPI_Type_contiguous(SIZE, MPI_FLOAT, &rowtype);
MPI_Type_commit(&rowtype);

if (rank == 0) {
for (i = 0; i < numprocs; i++)
MPI_Send(&a[i][0], 1, rowtype, i, tag, MPI_COMM_WORLD);
}
MPI_Recv(b, SIZE, rowtype, src, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Proc %d received b = %3.1f %3.1f %3.1f %3.1f\n", rank, b[0], b[1], b[2], b[3]);

MPI_Type_free(&rowtype);
MPI_Finalize();
}

/*
PROBLEMA: Processadores != 0 nao sabiam o que era o rowtype, ou seja, nao tava inicializado
CORREÇAO: Meter o MPI_Type_* para fora do rank 0 para que todos soubessem o que era o tipo rowtype
*/
51 changes: 51 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 4

int main(int argc, char *argv[]) {
int numprocs, rank, tag, src, dest, i, blocklengths[2], displacements[2];
float a[SIZE][SIZE] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
float b[SIZE];
MPI_Datatype indextype;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != SIZE) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with %d processes!\n", SIZE);
exit(0);
}

printf("Proc %d starting...\n", rank);
tag = 0;
src = 0;
blocklengths[0] = 2;
blocklengths[1] = 3;
displacements[0] = 5;
displacements[1] = 12;
MPI_Type_indexed(2, blocklengths, displacements, MPI_FLOAT, &indextype);
MPI_Type_commit(&indextype);
if (rank == 0)
for (i = 0; i < numprocs; i++){
MPI_Send(a, 1, indextype, i, tag, MPI_COMM_WORLD);
printf("Proc %d sending info...\n", rank);
}
MPI_Recv(b, SIZE, indextype, src, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Proc %d received b = %3.1f %3.1f %3.1f %3.1f\n", rank, b[0], b[1], b[2], b[3]);

MPI_Type_free(&indextype);
MPI_Finalize();
}

/*
PROBLEMA: Tipos?
CORREÇAO: ???
*/
45 changes: 45 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define REQS 1000
#define DISP 100

int main(int argc, char *argv[]) {
int numprocs, rank, tag, src, dest, num, offset, i;
MPI_Request reqs[4 * REQS];

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

if (numprocs != 2) {
MPI_Finalize();
if (rank == 0)
printf("Run the program with 2 processes!\n");
exit(0);
}

printf("Proc %d starting...\n", rank);
MPI_Barrier(MPI_COMM_WORLD);
tag = 0;
if (rank == 0) {
src = 1;
offset = 0;
} else {
src = 0;
offset = 2 * REQS;
}
dest = src;
num = 1;
for (i = 1; i <= REQS; i++) {
MPI_Isend(&num, 1, MPI_INT, dest, tag, MPI_COMM_WORLD, &reqs[offset]);
MPI_Irecv(&num, 1, MPI_INT, src, tag, MPI_COMM_WORLD, &reqs[offset + 1]);
offset += 2;
if (i % DISP == 0)
printf("Proc %d has done %d isends/irecvs\n", rank, i);
}
MPI_Waitall(2 * REQS, reqs, MPI_STATUS_IGNORE);

MPI_Finalize();
}

0 comments on commit 7a3b0b4

Please sign in to comment.