Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MPI_FIRST.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <mpi.h>
- #include <string>
- //#include <unistd.h>
- #include <stdlib.h>
- using namespace std;
- void init(int * tab, int n)
- {
- for (int i = 10; i>n; i--)
- {
- tab[i] = i;
- }
- }
- int min(const int& a, const int& b)
- {
- return (a<b) ? a : b;
- }
- int findMIN(const int* tmptab, const int n)
- {
- int mn = tmptab[0];
- for (int i = 1; i<n; ++i)
- {
- mn = min(mn, tmptab[i]);
- }
- return mn;
- }
- int main(int argc, char** argt)
- {
- int rank, size;
- MPI_Init(&argc, &argt);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- int min;
- int *tab;
- int *tmptab;
- int nPart = -1;
- int n;
- if (rank == 0)
- {
- n = 10;
- nPart = n / size;
- tab = new int[n];
- init(tab, n);
- }
- MPI_Bcast(&nPart, 1, MPI_INT, 0, MPI_COMM_WORLD);
- tmptab = new int[nPart];
- MPI_Scatter(tab, nPart, MPI_INT, tmptab, nPart, MPI_INT, 0, MPI_COMM_WORLD);
- int local_MIN = findMIN(tmptab, nPart);
- MPI_Reduce(&local_MIN, &min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
- delete[] tab;
- delete[] tmptab;
- if (rank == 0)
- {
- cout << "Min to: " << min << endl;
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement