Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. // MPI_FIRST.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <mpi.h>
  7. #include <string>
  8. //#include <unistd.h>
  9. #include <stdlib.h>
  10.  
  11. using namespace std;
  12.  
  13. void init(int * tab, int n)
  14. {
  15.     for (int i = 10; i>n; i--)
  16.     {
  17.         tab[i] = i;
  18.     }
  19. }
  20.  
  21. int min(const int& a, const int& b)
  22. {
  23.     return (a<b) ? a : b;
  24. }
  25.  
  26. int findMIN(const int* tmptab, const int n)
  27. {
  28.     int mn = tmptab[0];
  29.  
  30.     for (int i = 1; i<n; ++i)
  31.     {
  32.         mn = min(mn, tmptab[i]);
  33.     }
  34.  
  35.     return mn;
  36. }
  37.  
  38. int main(int argc, char** argt)
  39. {
  40.     int rank, size;
  41.     MPI_Init(&argc, &argt);
  42.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  43.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  44.  
  45.     int min;
  46.     int *tab;
  47.     int *tmptab;
  48.     int nPart = -1;
  49.     int n;
  50.  
  51.     if (rank == 0)
  52.     {
  53.         n = 10;
  54.         nPart = n / size;
  55.         tab = new int[n];
  56.         init(tab, n);
  57.     }
  58.     MPI_Bcast(&nPart, 1, MPI_INT, 0, MPI_COMM_WORLD);
  59.     tmptab = new int[nPart];
  60.     MPI_Scatter(tab, nPart, MPI_INT, tmptab, nPart, MPI_INT, 0, MPI_COMM_WORLD);
  61.  
  62.     int local_MIN = findMIN(tmptab, nPart);
  63.  
  64.     MPI_Reduce(&local_MIN, &min, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  65.  
  66.     delete[] tab;
  67.     delete[] tmptab;
  68.     if (rank == 0)
  69.     {
  70.         cout << "Min to: " << min << endl;
  71.     }
  72.     MPI_Finalize();
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement