Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author: Wes Kendall
- // Copyright 2011 www.mpitutorial.com
- // This code is provided freely with the tutorials on mpitutorial.com. Feel
- // free to modify it for your own use. Any distribution of the code must
- // either provide a link to www.mpitutorial.com or keep this header intact.
- //
- // MPI_Send, MPI_Recv example. Communicates the number -1 from process 0
- // to process 1.
- //
- #include <mpi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define length 100
- int main(int argc, char** argv) {
- // Initialize the MPI environment
- MPI_Init(NULL, NULL);
- // Find out rank, size
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- // We are assuming at least 2 processes for this task
- if (world_size < 2) {
- fprintf(stderr, "World size must be greater than 1 for %s\n", argv[0]);
- MPI_Abort(MPI_COMM_WORLD, 1);
- }
- if (world_rank == 0) {
- printf("give message");
- char data0[length];
- scanf("%s", data0);
- MPI_Send(
- /* data = */ data0,
- /* count = */ length,
- /* datatype = */ MPI_CHAR,
- /* destination = */ 1,
- /* tag = */ 0,
- /* communicator = */ MPI_COMM_WORLD);
- } else if( world_rank==1 ) {
- char data[length];
- MPI_Recv(
- /* data = */ data,
- /* count = */ length,
- /* datatype = */ MPI_CHAR,
- /* source = */ MPI_ANY_SOURCE,
- /* tag = */ MPI_ANY_TAG,
- /* communicator = */ MPI_COMM_WORLD,
- /* status = */ MPI_STATUS_IGNORE);
- printf("message: %s", data);
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement