int main(int argc, char *argv[]) { #ifndef DEBUG int nproc; int rank; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nproc); MPI_Comm_rank(MPI_COMM_WORLD, &rank); if (rank == 0) { FILE *fm = argc > 1 ? fopen(argv[1], "r") : fopen("matrixA.dat", "r"); FILE *fv = argc > 2 ? fopen(argv[2], "r") : fopen("vectorB.dat", "r"); int steps = argc > 3 ? atof(argv[3]) : 1; matrix a = create_matrix_from_file(fm); values b = create_vector_from_file(fv); double *x = initialize_x(a.n, 0.0); solve_parallel(&a, x, b.v, nproc, steps); print_vector(x, a.n); } else { matrix m = receive_matrix(); double *z = malloc(m.n*sizeof(*z)); values v; v.n = m.n; v.v = malloc(v.n*sizeof(*v.v)); while (1) { if (receive_vector(v) == exit_tag) break; range r = compute_range(v.n, nproc, rank); mul_matrix_row(&m, v.v, z, r.begin, r.end); MPI_Send(z+r.begin, r.end-r.begin, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD); } } MPI_Finalize(); #endif #ifdef DEBUG // test(); FILE *fm = argc > 1 ? fopen(argv[1], "r") : fopen("matrixA.dat", "r"); FILE *fv = argc > 2 ? fopen(argv[2], "r") : fopen("vectorB.dat", "r"); matrix m = create_matrix_from_file(fm); values vector = create_vector_from_file(fv); double *x = initialize_x(m.n, 0.0); solve(&m, x, vector.v); print_vector(x, m.n); #endif return 0; }
int main(int argc, char **argv) { int rank, procs; long long int my_sum = 0; long long int total_sum = 0; int vector[VECTOR_SIZE]; int chunk_size; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &procs); chunk_size = calc_chunk_for_proc(rank, VECTOR_SIZE, procs); if (rank == 0) { init_vector(vector); distribute_vector(vector, procs); } else { receive_vector(vector, chunk_size); } my_sum = sum_vector(vector, chunk_size); MPI_Reduce(&my_sum, &total_sum, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, MPI_COMM_WORLD); /* if (rank == 0) { long long int serial_sum = 0; for (int i = 0; i < VECTOR_SIZE; ++i) serial_sum += vector[i]; if (serial_sum == total_sum) { printf("DEBUG: Serial == parallel\n"); } else { printf("DEBUG: Serial != parallel, %d\n", abs(serial_sum - my_sum)); } } */ MPI_Finalize(); return(0); }