Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mpi.h>
- #include<stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include <iostream>
- #define MINI 0
- #define MAXI 10
- #define DATA 0
- #define RESULT 1
- #define FINISH 2
- #define PACKAGESIZE 3
- int main(int argc, char** argv) {
- int myrank, proccount;
- int sentCount = 0;
- int i;
- MPI_Status status;
- long inputArgument = 200;
- int result[MAXI];
- int resultTemp[MAXI];
- int randomArray[10000];
- int packageArray[PACKAGESIZE];
- int resultSum = 0;
- // Initialize MPI
- MPI_Init(&argc, &argv);
- // find out my rank
- MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
- // find out the number of processes in MPI_COMM_WORLD
- MPI_Comm_size(MPI_COMM_WORLD, &proccount);
- for (i = MINI; i < MAXI; i++) {
- result[i] = 0;
- resultTemp[i] = 0;
- }
- for (i = 0; i < inputArgument; i++) {
- randomArray[i] = 0;
- //If inputArgument is not a multiple of PACKAGESIZE, fill out the redundant indexes with -1
- int modulo = inputArgument % PACKAGESIZE;
- if ( modulo != 0 && i > inputArgument-modulo) {
- randomArray[i] = -1;
- }
- }
- for (i = 0; i < PACKAGESIZE; i++) {
- packageArray[i] = 0;
- }
- if (proccount < 2)
- {
- printf("Run with at least 2 processes");
- MPI_Finalize();
- return -1;
- }
- if (inputArgument < PACKAGESIZE * (proccount-1)) {
- printf("Please give bigger number");
- MPI_Finalize();
- return -1;
- }
- if (inputArgument > 10000) {
- printf("Please give smaller number");
- }
- // now the master will distribute the data and slave processes will perform computations
- if (myrank == 0)
- {
- //Fill out array with random numbers 0-1000
- for (i = 0; i < inputArgument; i++) {
- randomArray[i] = rand() % MAXI + MINI;
- }
- // first distribute some ranges to all slaves
- for (i = 1; i < proccount; i++)
- {
- //Get part of randomArray
- memcpy(packageArray, randomArray + PACKAGESIZE * sentCount, sizeof(int) * PACKAGESIZE);
- // send it to process i
- MPI_Send(packageArray, PACKAGESIZE, MPI_INT, i, DATA, MPI_COMM_WORLD);
- sentCount++;
- }
- while (sentCount * PACKAGESIZE < inputArgument)
- {
- // distribute remaining subranges to the processes which have completed their parts
- MPI_Recv(&resultTemp, MAXI, MPI_INT, MPI_ANY_SOURCE, RESULT, MPI_COMM_WORLD, &status);
- //Add resultTemp to result array
- for (i = MINI; i < MAXI; i++) {
- result[i] += resultTemp[i];
- }
- // check the sender and send some more data
- memcpy(packageArray, randomArray + PACKAGESIZE * sentCount, sizeof(int) * PACKAGESIZE);
- MPI_Send(packageArray, PACKAGESIZE, MPI_INT, status.MPI_SOURCE, DATA, MPI_COMM_WORLD);
- sentCount++;
- }
- // now receive results from the processes
- for (i = 0; i < (proccount - 1); i++)
- {
- MPI_Recv(&resultTemp, MAXI, MPI_INT, MPI_ANY_SOURCE, RESULT, MPI_COMM_WORLD, &status);
- for (int i = MINI; i < MAXI; i++) {
- result[i] += resultTemp[i];
- }
- }
- // shut down the slaves
- for (i = 1; i < proccount; i++)
- {
- MPI_Send(NULL, 0, MPI_INT, i, FINISH, MPI_COMM_WORLD);
- }
- // now display the result
- printf("\nHi, I am process 0, the result is: ");
- for (i = 0; i < MAXI; i++) {
- resultSum += result[i];
- }
- printf("The sum: %i",resultSum);
- }
- else
- { // slave
- // this is easy - just receive data and do the work
- do
- {
- for (i = MINI; i < MAXI; i++) {
- resultTemp[i] = 0;
- }
- MPI_Probe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- if (status.MPI_TAG == DATA)
- {
- MPI_Recv(packageArray, PACKAGESIZE, MPI_INT, 0, DATA, MPI_COMM_WORLD, &status);
- // compute my part
- for (i = 0; i < sizeof(packageArray) / sizeof(packageArray[0]); i++) {
- if (packageArray[i] > -1) {
- resultTemp[packageArray[i]]++;
- }
- }
- // send the result back
- MPI_Send(&resultTemp, MAXI, MPI_INT, 0, RESULT, MPI_COMM_WORLD);
- }
- } while (status.MPI_TAG != FINISH);
- }
- // Shut down MPI
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement