|
1
|
|
|
2
|
- Process – a set of tasks for a processor to execute
- Distinct from processor
- Process created either statically or dynamically during execution of a
program
|
|
3
|
- source
- executable executable
- processor 0
processor 1
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
- #include <stdio.h>
- #include "mpi.h“
- main(int argc, char** argv)
- {
- int my_rank; /* Rank of process */
- int p; /* Number of processes */
- int source; /* Rank of sender */
- int dest; /* Rank of receiver */
- int tag = 50; /* Tag for messages */
- char message[100]; /* Storage for the message */
- MPI_Status status; /* Return status for receive */
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
|
|
10
|
- if (my_rank != 0)
- {
- sprintf(message, “Hello from process %d!", my_rank);
- dest = 0;
- /* Use strlen(message)+1 to include endofline */
- MPI_Send(message, strlen(message)+1, MPI_CHAR, dest,
- tag, MPI_COMM_WORLD);
- }
- else /* my_rank == 0 */
- {
- for (source = 1; source < p; source++)
- {
- MPI_Recv(message, 100, MPI_CHAR, source, tag,
- MPI_COMM_WORLD, &status);
- printf("%s\n", message);
- }
- }
- MPI_Finalize();
- } /* main */
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
- An alternative approach to grouping data is provided by the MPI
functions
- MPI_Pack and MPI_Unpack. MPI Pack allows one to explicitly store noncon-
- tiguous data in contiguous memory locations, and MPI Unpack can be used
- to copy data from a contiguous buer into noncontiguous memory locations.
|
|
18
|
- void Get_data4(int my_rank, float* a_ptr, float* b_ptr, int* n_ptr)
- {
- int root = 0; /* Argument to MPI_Bcast */
- char buffer[100]; /* Arguments to MPI_Pack/Unpack */
- int position; /* and MPI_Bcast*/
- if (my_rank == 0)
- {
- printf("Enter a, b, and n");
- scanf("%f %f %d", a_ptr, b_ptr, n_ptr);
- /* Now pack the data into buffer */
- position = 0; /* Start at beginning of buffer */
- MPI_Pack(a_ptr, 1, MPI_FLOAT, buffer, 100,&position,
MPI_COMM_WORLD);
- /* Position has been incremented by */
- /* sizeof(float) bytes */
- MPI_Pack(b_ptr, 1, MPI_FLOAT, buffer, 100, &position,
MPI_COMM_WORLD);
|
|
19
|
- MPI_Pack(n_ptr, 1, MPI_INT, buffer, 100, &position, MPI_COMM_WORLD);
- /* Now broadcast contents of buffer */
- MPI_Bcast(buffer, 100, MPI_PACKED, root, MPI_COMM_WORLD);
- }
- else
- {
- MPI_Bcast(buffer, 100, MPI_PACKED, root, MPI_COMM_WORLD);
- /* Now unpack the contents of buffer */
- position = 0;
- MPI_Unpack(buffer, 100, &position, a_ptr, 1, MPI_FLOAT,
MPI_COMM_WORLD);
- /* Again position incremented by sizeof(float) bytes */
- MPI_Unpack(buffer, 100, &position, b_ptr, 1, MPI_FLOAT,
MPI_COMM_WORLD);
- MPI_Unpack(buffer, 100, &position, n_ptr, 1, MPI_INT,
MPI_COMM_WORLD);
- }
- }
|
|
20
|
- In this code process 0 uses MPI_Pack to copy a to buffer and
- then append b and n. After the broadcast of buffer, the remaining processes
- use MPI_Unpack to extract a, b, and n from the buffer. The datatype for
the
- calls to MPI_Bcast is MPI_PACKED.
- The syntax is
- int MPI_Pack(void* pack_data, int in_count, MPI_Datatype datatype,
- void* buffer, int size, int*
position_ptr, MPI_Comm comm)
- int MPI_Unpack(void* buffer, int size, int* position_ptr, void*
unpack_data, int count, MPI_Datatype datatype, MPI_comm comm)
|
|
21
|
- MPI_COMM_WORLD
- Ability to define communicators, treat certain subsets of processes as a
communication universe
- MPI_Group MPI_GROUP_WORLD
- MPI_Group first_row_group
- /* Create the new communicator */
- MPI_Comm_create(MPI_COMM_WORLD, first_row_group,
- &first_row_comm);
|