Created
January 22, 2016 22:41
-
-
Save Sutil/3dc9831e81179b06d761 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 | |
void produtor() { | |
int prod, rec; | |
while(1) { | |
MPI_Recv(&rec, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
prod = rand() % 100; | |
printf("Produziu: %d\n", prod); | |
MPI_Send(&prod, 1, MPI_INT, 1, 0, MPI_COMM_WORLD); | |
sleep(2); | |
} | |
} | |
void consumidor() { | |
int i, rec = 0; | |
for(i = 0; i < N; i++) | |
MPI_Send(&rec, 1, MPI_INT, 0, 0, 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 == 0) { | |
produtor(); | |
} else { | |
consumidor(); | |
} | |
MPI_Finalize(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment