Advertisement
ridjis

skalar

Jan 16th, 2018
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<mpi.h>
  4.  
  5. void get_dims(int *n_p, int *local_n_p, int my_rank, int comm_sz, MPI_Comm comm);
  6. void allocate(double **local_a_pp, double **local_b_pp, double **local_c_pp, int n, int local_n, MPI_Comm comm);
  7. void read_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm);
  8. void print_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm);
  9. void multiply(double local_a[], double local_b[], double local_c[], double* local_sum, int local_n);
  10. void print_sum(double local_sum, double global_sum, int my_rank, MPI_Comm comm);
  11.  
  12. int main(void) {
  13.     int comm_sz, my_rank, n, local_n;
  14.     double local_sum = 0.0; double global_sum = 0.0;
  15.     double* local_a; double* local_b; double* local_c;
  16.     MPI_Comm comm;
  17.  
  18.     MPI_Init(NULL, NULL);
  19.     comm = MPI_COMM_WORLD;
  20.     MPI_Comm_size(comm, &comm_sz);
  21.     MPI_Comm_rank(comm, &my_rank);
  22.  
  23.     get_dims(&n, &local_n, my_rank, comm_sz, comm);
  24.     allocate(&local_a, &local_b, &local_c, n, local_n, comm);
  25.    
  26.     read_vector(local_a, n, local_n, my_rank, comm);
  27.     print_vector(local_a, n, local_n, my_rank, comm);
  28.  
  29.     read_vector(local_b, n, local_n, my_rank, comm);
  30.     print_vector(local_b, n, local_n, my_rank, comm);
  31.  
  32.     multiply(local_a, local_b, local_c, &local_sum, local_n);
  33.     print_vector(local_c, n, local_n, my_rank, comm);
  34.     print_sum(local_sum, global_sum, my_rank, comm);
  35.  
  36.     free(local_a); free(local_b); free(local_c);
  37.     MPI_Finalize();
  38.  
  39.     return 0;
  40. }
  41.  
  42. void get_dims(int *n_p, int *local_n_p, int my_rank, int comm_sz, MPI_Comm comm) {
  43.     if (my_rank == 0) {
  44.         printf("Unesite velicinu vektora - n: \n");
  45.         scanf("%d", n_p);
  46.     }
  47.     MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
  48.  
  49.     if (*n_p <= 0 || *n_p % comm_sz != 0) {
  50.         if (my_rank == 0) {
  51.             fprintf(stderr, "Proc %d > In %s, %s\n", my_rank, "get_dims", "input invalid.");
  52.             fflush(stderr);
  53.         }
  54.         MPI_Finalize();
  55.         exit(-1);
  56.     }
  57.  
  58.  
  59.     *local_n_p = *n_p / comm_sz;
  60. }
  61.  
  62. void allocate(double **local_a_pp, double **local_b_pp, double **local_c_pp, int n, int local_n, MPI_Comm comm) {
  63.     *local_a_pp = calloc(local_n, sizeof(double));
  64.     *local_b_pp = calloc(local_n, sizeof(double));
  65.     *local_c_pp = calloc(local_n, sizeof(double));
  66. }
  67.  
  68. void read_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm) {
  69.     double* vector = NULL;
  70.     if (my_rank == 0) {
  71.         vector = calloc(n, sizeof(double));
  72.         printf("Enter the vector:\n");
  73.         for (int i = 0; i < n; i++)
  74.             scanf("%lf", &vector[i]);
  75.  
  76.         MPI_Scatter(vector, local_n, MPI_DOUBLE, local_vector, local_n, MPI_DOUBLE, 0, comm);
  77.         free(vector);
  78.     } else {
  79.         MPI_Scatter(vector, local_n, MPI_DOUBLE, local_vector, local_n, MPI_DOUBLE, 0, comm);
  80.     }
  81. }
  82.  
  83. void print_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm) {
  84.     double* vector = NULL;
  85.     if (my_rank == 0) {
  86.         vector = calloc(n, sizeof(double));
  87.         MPI_Gather(local_vector, local_n, MPI_DOUBLE, vector, local_n, MPI_DOUBLE, 0, comm);
  88.  
  89.         printf("\nVector: [ ");
  90.         for (int i = 0; i < n; i++)
  91.             printf("%.2lf ", vector[i]);
  92.        
  93.         printf("]\n");
  94.        
  95.         free(vector);
  96.     } else {
  97.         MPI_Gather(local_vector, local_n, MPI_DOUBLE, vector, local_n, MPI_DOUBLE, 0, comm);
  98.     }
  99. }
  100.  
  101. void multiply(double local_a[], double local_b[], double local_c[], double* local_sum, int local_n) {
  102.     for (int local_i = 0; local_i < local_n; local_i++) {
  103.         local_c[local_i] = local_a[local_i] * local_b[local_i];
  104.         *local_sum += local_c[local_i];
  105.     }
  106. }
  107.  
  108. void print_sum(double local_sum, double global_sum, int my_rank, MPI_Comm comm) {
  109.     MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
  110.  
  111.     if (my_rank == 0) {
  112.         printf("Skalarni proizvod vektora je %lf\n", global_sum);
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement