compile with: mpicc mpi_example1.c -o mpi_example1
run with: mpirun -n 4 mpi_example1
or: mpirun -np 4 --host h1,h2,h3,h4 mpi_example1
or: mpirun -np 4 --hostfile mpi_hosts mpi_example1
or: mpirun -np 4 --hostfile mpi_hosts --map-by node mpi_example1
#include <mpi.h>
#include <stdio.h>
int main( int argc, char **argv )
{
int rank, size;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Get_processor_name(processor_name,&namelen);
printf( "Jestem %d z %d na %s\n", rank, size, processor_name );
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#define MSG_HELLO 100
#define MSG_SIZE 2
int main( int argc, char **argv )
{
int rank,size,sender=0,receiver=1;
int msg[MSG_SIZE];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if ( rank == sender )
{
msg[0] = rank;
MPI_Comm_size( MPI_COMM_WORLD, &size );
msg[1] = size;
printf("%d: Wysylam (%d %d) do %d\n", rank, msg[0], msg[1], receiver);
MPI_Send( msg, MSG_SIZE, MPI_INT, receiver, MSG_HELLO, MPI_COMM_WORLD );
}
else if ( rank == receiver )
{
MPI_Recv(msg, MSG_SIZE, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Get_count( &status, MPI_INT, &size);
printf("%d: Otrzymalem %d wartosci: (%d %d) od %d\n", rank, size, msg[0], msg[1], status.MPI_SOURCE);
}
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#define MSG_SIZE 2
int main(int argc, char **argv)
{
int rank,size,sender;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
int msg[MSG_SIZE];
sender = 0;
if ( rank == sender )
{
msg[0] = rank;
MPI_Comm_size( MPI_COMM_WORLD, &size );
msg[1] = size;
MPI_Bcast(msg, MSG_SIZE, MPI_INT, sender, MPI_COMM_WORLD);
}
else
{
MPI_Bcast( msg, MSG_SIZE, MPI_INT, sender, MPI_COMM_WORLD );
printf("%d: Otrzymalem bcast (%d %d) od %d\n", rank, msg[0], msg[1], sender);
}
MPI_Finalize();
}
#include <mpi.h>
#include <stdio.h>
#define MSG_SIZE 1
int main(int argc, char **argv)
{
int rank,receiver;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
int i=rank;
int sum=0;
receiver = 0;
printf("%d: Wysylam wartosc %d \n", rank, i );
MPI_Reduce(&i,&sum, MSG_SIZE, MPI_INT, MPI_SUM, receiver, MPI_COMM_WORLD);
if ( rank == receiver ) printf("%d: Otrzymalem sume %d \n", rank, sum );
MPI_Finalize();
}