Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<mpi.h>
- void get_dims(int *n_p, int *local_n_p, int my_rank, int comm_sz, MPI_Comm comm);
- void allocate(double **local_a_pp, double **local_b_pp, double **local_c_pp, int n, int local_n, MPI_Comm comm);
- void read_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm);
- void print_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm);
- void multiply(double local_a[], double local_b[], double local_c[], double* local_sum, int local_n);
- void print_sum(double local_sum, double global_sum, int my_rank, MPI_Comm comm);
- int main(void) {
- int comm_sz, my_rank, n, local_n;
- double local_sum = 0.0; double global_sum = 0.0;
- double* local_a; double* local_b; double* local_c;
- MPI_Comm comm;
- MPI_Init(NULL, NULL);
- comm = MPI_COMM_WORLD;
- MPI_Comm_size(comm, &comm_sz);
- MPI_Comm_rank(comm, &my_rank);
- get_dims(&n, &local_n, my_rank, comm_sz, comm);
- allocate(&local_a, &local_b, &local_c, n, local_n, comm);
- read_vector(local_a, n, local_n, my_rank, comm);
- print_vector(local_a, n, local_n, my_rank, comm);
- read_vector(local_b, n, local_n, my_rank, comm);
- print_vector(local_b, n, local_n, my_rank, comm);
- multiply(local_a, local_b, local_c, &local_sum, local_n);
- print_vector(local_c, n, local_n, my_rank, comm);
- print_sum(local_sum, global_sum, my_rank, comm);
- free(local_a); free(local_b); free(local_c);
- MPI_Finalize();
- return 0;
- }
- void get_dims(int *n_p, int *local_n_p, int my_rank, int comm_sz, MPI_Comm comm) {
- if (my_rank == 0) {
- printf("Unesite velicinu vektora - n: \n");
- scanf("%d", n_p);
- }
- MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
- if (*n_p <= 0 || *n_p % comm_sz != 0) {
- if (my_rank == 0) {
- fprintf(stderr, "Proc %d > In %s, %s\n", my_rank, "get_dims", "input invalid.");
- fflush(stderr);
- }
- MPI_Finalize();
- exit(-1);
- }
- *local_n_p = *n_p / comm_sz;
- }
- void allocate(double **local_a_pp, double **local_b_pp, double **local_c_pp, int n, int local_n, MPI_Comm comm) {
- *local_a_pp = calloc(local_n, sizeof(double));
- *local_b_pp = calloc(local_n, sizeof(double));
- *local_c_pp = calloc(local_n, sizeof(double));
- }
- void read_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm) {
- double* vector = NULL;
- if (my_rank == 0) {
- vector = calloc(n, sizeof(double));
- printf("Enter the vector:\n");
- for (int i = 0; i < n; i++)
- scanf("%lf", &vector[i]);
- MPI_Scatter(vector, local_n, MPI_DOUBLE, local_vector, local_n, MPI_DOUBLE, 0, comm);
- free(vector);
- } else {
- MPI_Scatter(vector, local_n, MPI_DOUBLE, local_vector, local_n, MPI_DOUBLE, 0, comm);
- }
- }
- void print_vector(double local_vector[], int n, int local_n, int my_rank, MPI_Comm comm) {
- double* vector = NULL;
- if (my_rank == 0) {
- vector = calloc(n, sizeof(double));
- MPI_Gather(local_vector, local_n, MPI_DOUBLE, vector, local_n, MPI_DOUBLE, 0, comm);
- printf("\nVector: [ ");
- for (int i = 0; i < n; i++)
- printf("%.2lf ", vector[i]);
- printf("]\n");
- free(vector);
- } else {
- MPI_Gather(local_vector, local_n, MPI_DOUBLE, vector, local_n, MPI_DOUBLE, 0, comm);
- }
- }
- void multiply(double local_a[], double local_b[], double local_c[], double* local_sum, int local_n) {
- for (int local_i = 0; local_i < local_n; local_i++) {
- local_c[local_i] = local_a[local_i] * local_b[local_i];
- *local_sum += local_c[local_i];
- }
- }
- void print_sum(double local_sum, double global_sum, int my_rank, MPI_Comm comm) {
- MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, comm);
- if (my_rank == 0) {
- printf("Skalarni proizvod vektora je %lf\n", global_sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement