Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "mpi.h"
- int main(int argc, char* argv[]) {
- MPI_Init(&argc, &argv);
- double starttime, endtime;
- int world_rank, count_ranks, total;
- MPI_Comm_size(MPI_COMM_WORLD, &count_ranks);
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- /*
- // scan
- starttime = MPI_Wtime();
- MPI_Scan(&world_rank, &total, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
- endtime = MPI_Wtime();
- printf("[MPI process %d library] Total = %d.\n", world_rank, total);
- printf("[MPI process %d library] Time = %g ms\n", world_rank, (endtime - starttime));
- */
- // custom
- starttime = MPI_Wtime();
- if (world_rank == 0) {
- total = world_rank;
- MPI_Send(&total, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
- }
- else if (world_rank < count_ranks - 1) {
- MPI_Recv(&total, 1, MPI_INT, (world_rank - 1), 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- total += world_rank;
- MPI_Send(&total, 1, MPI_INT, (world_rank + 1), 0, MPI_COMM_WORLD);
- }
- else {
- MPI_Recv(&total, 1, MPI_INT, (world_rank - 1), 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- total += world_rank;
- }
- endtime = MPI_Wtime();
- printf("[MPI process %d] Total = %d.\n", world_rank, total);
- printf("[MPI process %d] Time = %g ms\n", world_rank, (endtime - starttime));
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement