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 *m_p, int *n_p, int *local_m_p, int my_rank, int comm_sz, MPI_Comm comm);
- void allocate(double **local_A_pp, double **v_pp, double **local_r_pp, int m, int local_m, int n, MPI_Comm comm);
- void read_matrix(double local_A[], int m, int local_m, int n, int my_rank, MPI_Comm comm);
- void read_vector(double v[], int n, int my_rank, MPI_Comm comm);
- void print_matrix(double local_A[], int m, int local_m, int n, int my_rank, MPI_Comm comm);
- void print_vector(double v[], int n, int my_rank);
- void gather_and_print_vector(double local_r[], int local_m, int m, int my_rank, MPI_Comm comm);
- void multiply(double local_A[], double v[], double local_r[], int local_m, int n, int my_rank);
- int main(void) {
- int comm_sz, my_rank, m, n, local_m;
- double* local_A;
- double* v; double* local_r;
- 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(&m, &n, &local_m, my_rank, comm_sz, comm);
- allocate(&local_A, &v, &local_r, m, local_m, n, comm);
- read_matrix(local_A, m, local_m, n, my_rank, comm);
- print_matrix(local_A, m, local_m, n, my_rank, comm);
- read_vector(v, n, my_rank, comm);
- print_vector(v, n, my_rank);
- multiply(local_A, v, local_r, local_m, n, my_rank);
- gather_and_print_vector(local_r, local_m, m, my_rank, comm);
- free(local_A); free(v); free(local_r);
- MPI_Finalize();
- return 0;
- }
- void get_dims(int *m_p, int *n_p, int *local_m_p, int my_rank, int comm_sz, MPI_Comm comm) {
- if (my_rank == 0) {
- printf("Unesite broj redova - m: \n");
- scanf("%d", m_p);
- printf("Unesite broj kolona - n: \n");
- scanf("%d", n_p);
- if (*m_p <= 0 || *n_p <= 0 || *m_p % comm_sz != 0) {
- fprintf(stderr, "Proc %d > In %s, %s\n", my_rank, "get_dims", "input invalid.");
- fflush(stderr);
- MPI_Finalize();
- exit(-1);
- }
- }
- MPI_Bcast(m_p, 1, MPI_INT, 0, comm);
- MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
- *local_m_p = *m_p / comm_sz;
- }
- void allocate(double **local_A_pp, double **v_pp, double **local_r_pp, int m, int local_m, int n, MPI_Comm comm) {
- *v_pp = calloc(n, sizeof(double));
- *local_r_pp = calloc(local_m, sizeof(double));
- *local_A_pp = calloc(local_m * n, sizeof(double));
- }
- void read_matrix(double local_A[], int m, int local_m, int n, int my_rank, MPI_Comm comm) {
- double* A = NULL;
- if (my_rank == 0) {
- A = calloc(m * n, sizeof(double));
- printf("Enter the matrix:\n");
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- scanf("%lf", &A[i*n+j]);
- MPI_Scatter(A, local_m*n, MPI_DOUBLE, local_A, local_m*n, MPI_DOUBLE, 0, comm);
- free(A);
- } else {
- MPI_Scatter(A, local_m*n, MPI_DOUBLE, local_A, local_m*n, MPI_DOUBLE, 0, comm);
- }
- }
- void print_matrix(double local_A[], int m, int local_m, int n, int my_rank, MPI_Comm comm) {
- double* A = NULL;
- if (my_rank == 0) {
- A = calloc(m * n, sizeof(double));
- MPI_Gather(local_A, local_m*n, MPI_DOUBLE, A, local_m*n, MPI_DOUBLE, 0, comm);
- printf("\nThe matrix:\n");
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- printf("%.2lf ", A[i*n+j]);
- }
- printf("\n");
- }
- printf("\n");
- free(A);
- } else {
- MPI_Gather(local_A, local_m*n, MPI_DOUBLE, A, local_m*n, MPI_DOUBLE, 0, comm);
- }
- }
- void read_vector(double v[], int n, int my_rank, MPI_Comm comm) {
- if (my_rank == 0) {
- printf("Enter the vector:\n");
- for (int i = 0; i < n; i++)
- scanf("%lf", &v[i]);
- }
- MPI_Bcast(v, n, MPI_DOUBLE, 0, comm);
- }
- void print_vector(double v[], int n, int my_rank) {
- if (my_rank == 0) {
- printf("\nVector: [ ");
- for (int i = 0; i < n; i++)
- printf("%.2lf ", v[i]);
- printf("]\n");
- }
- }
- void gather_and_print_vector(double local_r[], int local_m, int m, int my_rank, MPI_Comm comm) {
- double* r = NULL;
- if (my_rank == 0) {
- r = calloc(m, sizeof(double));
- MPI_Gather(local_r, local_m, MPI_DOUBLE, r, local_m, MPI_DOUBLE, 0, comm);
- printf("\nVector: [ ");
- for (int i = 0; i < m; i++)
- printf("%.2lf ", r[i]);
- printf("]\n");
- free(r);
- } else {
- MPI_Gather(local_r, local_m, MPI_DOUBLE, r, local_m, MPI_DOUBLE, 0, comm);
- }
- }
- void multiply(double local_A[], double v[], double local_r[], int local_m, int n, int my_rank) {
- for (int local_i = 0; local_i < local_m; local_i++) {
- local_r[local_i] = 0;
- for (int j = 0; j < n; j++) {
- local_r[local_i] += local_A[local_i*n+j] * v[j];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement