Advertisement
Artem_Chepurov

Untitled

Nov 20th, 2022
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "mpi.h"
  3. #include <cmath>
  4. #include <iostream>
  5.  
  6. int main(int argc, char** argv) {
  7.  
  8.     int size, rank, a, b;
  9.     MPI_Status status;
  10.     MPI_Init(&argc, &argv);
  11.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  12.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  13.     a = rank;
  14.     b = -1;
  15.     int s;
  16.     int* arr_B;
  17.     int* arr_A;
  18.     double start = 0, end = 0;
  19.     if (rank == 0) {
  20.         scanf_s("%d", &s);
  21.         printf("%d\n", s);
  22.         start = MPI_Wtime();
  23.     }
  24.        
  25.  
  26.         MPI_Bcast(&s, 1, MPI_INT, 0, MPI_COMM_WORLD);
  27.         arr_A = new int[s * s];
  28.         arr_B = new int[s * s];
  29.         if (rank ==0 )
  30.         {
  31.             for (int i = 0; i < s; i++)
  32.                 for (int j = 0; j < s; j++) {
  33.                     arr_A[i * s + j] = (rand() % 100 + 1);
  34.                     arr_B[i * s + j] = (rand() % 100 + 1);
  35.  
  36.                 }
  37.         }
  38.         MPI_Bcast(arr_B, s * s, MPI_INT, 0, MPI_COMM_WORLD);
  39.         int part_size = s * s / size;
  40.         std::cout << part_size;
  41.         int* arr_row = new int[part_size];
  42.  
  43.         MPI_Scatter(arr_A, part_size, MPI_INT, arr_row, part_size, MPI_INT, 0, MPI_COMM_WORLD);
  44.         int* arr_ans = new int[s * s / size];
  45.        
  46.         for (int i = 0; i < part_size / s; i++) {
  47.             for (int j = 0; j < start; j++) {
  48.                 int sum = 0;
  49.                 for (int k = 0; k < s; k++)
  50.                 {
  51.                     sum += arr_row[i * s + k] * arr_B[k * s + j];
  52.                 }
  53.                 arr_ans[i * s + j] = sum;
  54.             }
  55.  
  56.         }
  57.  
  58.         MPI_Gather(arr_ans, part_size, MPI_INT, arr_A, part_size, MPI_INT, 0, MPI_COMM_WORLD);
  59.        
  60.         if (rank == 0) {
  61.             end = MPI_Wtime();
  62.             std::cout << "TIME:" << end - start;
  63.            
  64.         }
  65.         delete[](arr_row);
  66.         delete[](arr_ans);
  67.         if (rank == 0) {
  68.             delete[](arr_B);
  69.             delete[](arr_A);
  70.         }
  71.         MPI_Barrier(MPI_COMM_WORLD);
  72.         MPI_Finalize();
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement