Skip to content

Commit

Permalink
new programs and exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeterd committed Oct 30, 2020
1 parent 7a3b0b4 commit 7aab742
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
5 changes: 4 additions & 1 deletion C/Advanced/Parallel/MPI/Praticas/mpi_bug5.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ int main(int argc, char *argv[]) {
}

/*
PROBLEMA: Tipos?
PROBLEMA: Quando faz send, o processo 1 ao recever, os valores recevidos sao todos 0 (zeros)
HIPOTSE1: Deslocaçao mal formatada!
DUVIDA: O que faz a funçao Type_indexed??
CORREÇAO: ???
Expand Down
47 changes: 47 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug7.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#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[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) {
dest = 1;
num = 1;
for (i = 1; i <= REQS; i++) {
MPI_Send(&num, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
if (i % DISP == 0)
printf("Proc %d has done %d sends\n", rank, i);
}
} else {
src = 0;
offset = 0;
for (i = 1; i <= REQS; i++) {
MPI_Irecv(&num, 1, MPI_INT, src, tag, MPI_COMM_WORLD, &reqs[offset]);
offset += 1;
if (i % DISP == 0)
printf("Proc %d has done %d irecvs\n", rank, i);
}
}
MPI_Waitall(REQS, reqs, MPI_STATUS_IGNORE);

MPI_Finalize();
}
88 changes: 88 additions & 0 deletions C/Advanced/Parallel/MPI/Praticas/mpi_bug8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>

#define ARRAYSIZE 160
#define MASTER 0

float data[ARRAYSIZE];
float do_work(int rank, int offset, int chunksize);

int main(int argc, char *argv[]) {
int numprocs, rank, tag1, tag2, src, dest, offset, chunksize, i, j;
float mysum, sum;

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

if (numprocs % 4 != 0) {
MPI_Finalize();
if (rank == 0)
printf("Number of procs must be divisible by 4!\n");
exit(0);
}

printf("Proc %d starting...\n", rank);
MPI_Barrier(MPI_COMM_WORLD);
chunksize = (ARRAYSIZE / numprocs);
tag1 = 1;
tag2 = 2;
if (rank == MASTER) {
sum = 0;
for (i = 0; i < ARRAYSIZE; i++) {
data[i] = i * 1.0;
sum = sum + data[i];
}
printf("Initial sum = %f\n", sum);
offset = chunksize;
for (dest = 1; dest < numprocs; dest++) {
MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);
MPI_Send(&data[offset], chunksize, MPI_FLOAT, dest, tag2, MPI_COMM_WORLD);
printf("Sent %d elements to proc %d offset %d\n", chunksize, dest, offset);
offset = offset + chunksize;
}
offset = 0;
mysum = do_work(rank, offset, chunksize);
sum = mysum;
for (src = 1; src < numprocs; src++) {
MPI_Recv(&offset, 1, MPI_INT, src, tag1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&data[offset], chunksize, MPI_FLOAT, src, tag2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&mysum, 1, MPI_FLOAT, src, tag2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
sum += mysum;
}
printf("Final sum = %f\n", sum);
printf("\nFinal results\n");
offset = 0;
for (i = 0; i < numprocs; i++) {
for (j = 0; j < chunksize; j++)
printf("%f ", data[offset + j]);
offset = offset + chunksize;
}
printf("\n");
} else {
MPI_Recv(&offset, 1, MPI_INT, MASTER, tag1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&data[offset], chunksize, MPI_FLOAT, MASTER, tag2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
mysum = do_work(rank, offset, chunksize);
MPI_Send(&offset, 1, MPI_INT, MASTER, tag1, MPI_COMM_WORLD);
MPI_Send(&data[offset], chunksize, MPI_FLOAT, MASTER, tag2, MPI_COMM_WORLD);
MPI_Reduce(&mysum, &sum, 1, MPI_FLOAT, MPI_SUM, MASTER, MPI_COMM_WORLD);
}

MPI_Finalize();
}


float do_work(int rank, int offset, int chunksize) {
float sum;
int i;

sum = 0;
for (i = offset; i < offset + chunksize; i++) {
data[i] = data[i] * 2;
sum = sum + data[i];
}
printf("Proc %d partial sum = %f\n", rank, sum);
return sum;
}

0 comments on commit 7aab742

Please sign in to comment.