1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
void Get_input(int my_rank, int comm_sz, double* a_p, double* b_p, int* n_p) { if (my_rank == 0) { printf("Input a, b and n.\n"); scanf_s("%lf%lf%d", a_p, b_p, n_p); } MPI_Bcast(a_p, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(b_p, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(n_p, 1, MPI_INT, 0, MPI_COMM_WORLD); }
void Read_vector(double local_a[], int local_n, int n, char vector_name[], int my_rank, MPI_Comm comm) { double* a = NULL; if (my_rank == 0) { a = (double*) malloc(n * sizeof(double)); for (int i = 0; i < n; i++) { scanf_s("%lf", &a[i]); } MPI_Scatter(a, local_n, MPI_DOUBLE, local_a, local_n, MPI_DOUBLE, 0, comm); free(a); } else { MPI_Scatter(a, local_n, MPI_DOUBLE, local_a, local_n, MPI_DOUBLE, 0, comm); } }
void Print_vector(double local_b[], int local_n, int n, char title[], int my_rank, MPI_Comm comm) {
double* a = (double*)malloc(n * sizeof(double));
if (my_rank == 0) { MPI_Gather(local_b, local_n, MPI_DOUBLE, a, local_n, MPI_DOUBLE, 0, MPI_COMM_WORLD); printf("%s\n", title); for (int i = 0; i < n; i++) printf("%lf ", a[i]); printf("\n"); free(a); } else { MPI_Gather(local_b, local_n, MPI_DOUBLE, a, local_n, MPI_DOUBLE, 0, MPI_COMM_WORLD); } }
void Matirx_Mul_Vector(double local_A[], double local_x[], double local_y[], int local_m, int n,int local_n, int my_rank, MPI_Comm comm) { double* x;
x = (double*)malloc(n * sizeof(double));
MPI_Allgather(local_x, local_n, MPI_DOUBLE, x, local_n, MPI_DOUBLE, comm);
for (int local_i = 0; local_i < local_m; local_i++) { local_y[local_i] = 0.0; for (int j = 0; j < n; j++) local_y[local_i] += local_A[local_i * n + j] * x[j]; } free(x); }
|