Last active
June 15, 2020 14:46
-
-
Save hurbeana/9ce06843d794ab5ad66ef5b8e2258b3c to your computer and use it in GitHub Desktop.
Change the "output_rank" int to see outputs from different ranks (only one is shown at the time)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mpi.h> | |
#include <stdio.h> | |
int main(int argc, char *argv[]) { | |
MPI_Init(&argc, &argv); | |
MPI_Comm comm = MPI_COMM_WORLD; | |
int rank, output_rank = 0; | |
MPI_Comm_rank(comm, &rank); | |
int n = 3; | |
double a[] = { 1, 2, 3 }; | |
if(rank == output_rank) { | |
printf("Outputting for rank %d\n", rank); | |
for(int i = 0; i < n; i++) { | |
printf("a[%d] = %f\n", i, a[i]); | |
} | |
} | |
double sum = a[0]; | |
for (int i = 1; i < n; i++) sum += a[i]; | |
// MPI code/function to compute some partial sum required for rank | |
double prefix_sum[n]; | |
// Local prefix -sums computation for rank in local array a | |
if(rank == output_rank) { | |
for(int i = 0; i < n; i++) { | |
printf("prefix_sum[%d] = %f\n", i, prefix_sum[i]); | |
} | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment