Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define  LICZNIK 3
  5.  
  6. main(int argc, char *argv[]) {
  7.         int numtasks, i,j, k , rank, a, rc;
  8.         int *tab;
  9.         int sum = 0, final = 0;
  10.         int *rec_data;
  11.         // liczba procesow
  12.         //numtasks = 4;
  13.         int *total_suma;
  14.  
  15.         total_suma = malloc(numtasks * sizeof(int));
  16.  
  17.  
  18.         rec_data = malloc(LICZNIK * sizeof(int));
  19.  
  20.         rc = MPI_Init(&argc, &argv);
  21.         MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
  22.  
  23. //      if(numtasks == 0) {
  24.                 //printf("Wystapil blad. \n");
  25.  
  26.  
  27.                 tab = malloc(LICZNIK * numtasks * sizeof(int));
  28.  
  29.  
  30.                 for(i = 0; i < LICZNIK * numtasks; i++) {
  31.                         tab[i] = i;
  32.                 }
  33.  
  34.                 MPI_Scatter(tab, LICZNIK, MPI_INT, rec_data, LICZNIK,  MPI_INT, 0, MPI_COMM_WORLD);
  35. //      }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.         MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  45.  
  46.         for(j = 0; j < LICZNIK; ++j) {
  47.                 sum = sum + rec_data[j];
  48.         }
  49.  
  50.         MPI_Gather(&sum, 1, MPI_INT, total_suma, 1, MPI_INT, 0, MPI_COMM_WORLD);
  51.  
  52. //      if(numtasks == 0) {
  53.  
  54.                 for (k=0; k < numtasks; ++k) {
  55.                         final = final + total_suma[k];
  56.                 }
  57.  
  58.                 printf("Ilosc procesow= %d Jestem procesem= %d Suma_lokalna: %d  \n", numtasks, rank, sum);
  59.                 printf("Total suma = %d ", final);
  60. //      }
  61.       MPI_Finalize();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement