Created
January 22, 2016 23:06
-
-
Save Sutil/2177d85206a980324363 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <mpi.h> | |
#define N 100 | |
#define PRODUCER 0 | |
#define CONSUMER 0 | |
#define MAX_PRODUCERS 5 | |
MPI_Status status; | |
void produtor() { | |
int prod, rec; | |
while(1) { | |
MPI_Recv(&rec, 1, MPI_INT, MPI_ANY_SOURCE, CONSUMER, MPI_COMM_WORLD, &status); | |
prod = rand() % 100; | |
printf("Produziu: %d\n", prod); | |
MPI_Send(&prod, 1, MPI_INT, status.MPI_SOURCE, CONSUMER, MPI_COMM_WORLD); | |
sleep(2); | |
} | |
} | |
void consumidor() { | |
int i, rec = 0; | |
for(i = 0; i < N; i++){ | |
int produtor = rand() % MAX_PRODUCERS; | |
MPI_Send(&rec, 1, MPI_INT, produtor, PRODUCER, MPI_COMM_WORLD); | |
} | |
while(1) { | |
MPI_Recv(&rec, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
MPI_Send(&rec, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); | |
printf("Consumindo: %d\n", rec); | |
sleep(1); | |
} | |
} | |
int main() { | |
MPI_Init(NULL,NULL); | |
int size; | |
MPI_Comm_size(MPI_COMM_WORLD, &size); | |
int rank; | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
if (size != 2) | |
MPI_Abort(MPI_COMM_WORLD, 1); | |
if (rank <= MAX_PRODUCERS) { | |
produtor(status); | |
} else { | |
consumidor(); | |
} | |
MPI_Finalize(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment