Skip to content

Instantly share code, notes, and snippets.

@hamidhaghdoost
Last active November 9, 2022 14:22
Show Gist options
  • Save hamidhaghdoost/aac4198dfb18a5301ffec76e6d486010 to your computer and use it in GitHub Desktop.
Save hamidhaghdoost/aac4198dfb18a5301ffec76e6d486010 to your computer and use it in GitHub Desktop.
MPI_Scatter example in c
// This code creates an array as count as our processes count and send every value of array to coresponding process and prints it in process...
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
#include "mpi.h"
#pragma comment(lib, "msmpi.lib")
void main(int argc, char *argv[])
{
int myid, numprocs;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
int *InputArr = new int[numprocs];
if(myid == 0)
{
srand(time(NULL));
for(int i = 0; i < numprocs; i++)
InputArr[i] = rand() % 100;
}
int *rec = new int[1];
MPI_Scatter(InputArr, 1, MPI_INT, rec, 1, MPI_INT, 0, MPI_COMM_WORLD);
printf("data in process[%d] = %d", myid, rec[0]);
MPI_Finalize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment