Skip to content

Instantly share code, notes, and snippets.

@bast
Last active June 7, 2022 18:36
Show Gist options
  • Save bast/bd83baf2c69ea61914e6 to your computer and use it in GitHub Desktop.
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.
// 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();
}
@ricardobjr
Copy link

Thank you very much, your code helped me with my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment