Last active
June 7, 2022 18:36
-
-
Save bast/bd83baf2c69ea61914e6 to your computer and use it in GitHub Desktop.
MPI: split loop with equal amount of work for each element among processes.
This file contains 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
// Copyright (c) 2015 Radovan Bast | |
// Licensed under the MIT license - http://opensource.org/licenses/MIT | |
#include <stdio.h> | |
#include <mpi.h> | |
int main (int argc, char *argv[]) | |
{ | |
int rank, comm_size; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
MPI_Comm_size(MPI_COMM_WORLD, &comm_size); | |
for (int i = 0; i < 17; i++) | |
{ | |
if (i%comm_size != rank) continue; | |
printf("rank %i working on element %i\n", rank, i); | |
// rest of work ... | |
} | |
MPI_Finalize(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much, your code helped me with my project.