Skip to content

Instantly share code, notes, and snippets.

@savioserra
Last active October 6, 2017 14:20
Show Gist options
  • Save savioserra/2e3132122bbf541f403402b1335a5e7c to your computer and use it in GitHub Desktop.
Save savioserra/2e3132122bbf541f403402b1335a5e7c to your computer and use it in GitHub Desktop.
MPI
#include <stdio.h>
#include <string.h>
#include <mpi.h>
int main (int argc, char** argv)
{
int stackSize, currentRank;
char vetor[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &stackSize);
MPI_Comm_rank(MPI_COMM_WORLD, &currentRank);
if (currentRank == 0)
strcpy(vetor, "Hi, I'm Process #0");
MPI_Bcast(&vetor, 100, MPI_CHAR, 0, MPI_COMM_WORLD);
printf("Rank #%i: %s\n", currentRank, vetor);
MPI_Finalize();
}
#include <stdio.h>
#include <string.h>
#include <mpi.h>
int main (int argc, char** argv)
{
int stackSize, currentRank;
char vetor[100];
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &stackSize);
MPI_Comm_rank(MPI_COMM_WORLD, &currentRank);
if (currentRank == 0)
for (int rank = 1; rank < stackSize; rank++)
{
strcpy(vetor, "Hi, I'm Process #0");
MPI_Send(&vetor, 100, MPI_CHAR, rank, 123, MPI_COMM_WORLD);
}
else
{
MPI_Recv(vetor, 100, MPI_CHAR, 0, 123, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("Rank%i: %s\n", currentRank, vetor);
}
MPI_Finalize();
}
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
double f(double dx)
{
return (4 * sqrt(1 - x * x));
}
double calcularArea(double dx, double x1, double x2)
{
return (dx * (f(x1) + f(x2)) / 2);
}
double receiveResults(int stackSize, int root)
{
double finalResult;
double parcialResult[1];
for (int rank = 1; rank < stackSize; rank++)
{
MPI_Recv(&parcialResult, 1, MPI_DOUBLE, rank, 123, MPI_COMM_WORLD);
finalResult += parcialResult[0];
}
return finalResult;
}
void sendArgs(double *args, double pontoInicial, double pontoFinal)
{
for (int rank = 1; rank < stackSize; rank++)
{
args[0] = pontoInicial;
args[1] = pontoFinal;
args[2] = intervalo;
MPI_Send(args, 3, MPI_DOUBLE, rank, 123, MPI_COMM_WORLD);
pontoInicial += intervalo;
pontoFinal += intervalo;
}
}
int main(int argc, char **argv)
{
// [ a, b ] -> Intervalo
// Nt -> Numero de trapezios
MPI_Init(&argc, &argv);
int stackSize, currentRank;
double pontoInicial, pontoFinal, intervalo;
MPI_Comm_size(MPI_COMM_WORLD, &stackSize);
MPI_Comm_rank(MPI_COMM_WORLD, &currentRank);
double args[3]; // [pInicial, pFinal, intervalo]
if (currentRank == 0)
{
int numTrapezios = atoi(argv[3]);
pontoInicial = atoi(argv[1]);
pontoFinal = atoi(argv[2]);
intervalo = (pontoInicial - pontoFinal) / numTrapeziosParciais;
sendArgs();
receiveResults(stackSize, 0);
}
else
{
MPI_Recv(&args, 3, MPI_DOUBLE, 0, 123, MPI_COMM_WORLD);
pontoInicial = args[0];
pontoFinal = args[1];
intervalo
while ()
}
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment