Ishan
Ishan

Reputation: 1

Creating a shared memory for processes part of a MPI Communicator

The shmget function is used to create a new shared memory segment or to locate an existing one based on a key. Shared memory segments are memory areas which can be shared by several processes.
If we have an ROW MPI communicator, is it possible to create a shared memory for the processes in the communicator using the shmget() function ?

https://mpitutorial.com/tutorials/introduction-to-groups-and-communicators/ Ref for creating MPI communicator.

I intend to use the shmget function and not the MPI_win_allocate function.

Till now I have the MPI row comm setup using the code from the above tutorial.

// Get the rank and size in the original communicator
   int world_rank, world_size;
   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
   MPI_Comm_size(MPI_COMM_WORLD, &world_size);

   int color = world_rank / 4; // Determine color based on row

   // Split the communicator based on the color and use the
   // original rank for ordering
   MPI_Comm row_comm;
   MPI_Comm_split(MPI_COMM_WORLD, color, world_rank, &row_comm);

    int row_rank, row_size;
    MPI_Comm_rank(row_comm, &row_rank);
    MPI_Comm_size(row_comm, &row_size);

    printf("WORLD RANK/SIZE: %d/%d \t ROW RANK/SIZE: %d/%d\n",
    world_rank, world_size, row_rank, row_size);

    MPI_Comm_free(&row_comm);

Upvotes: 0

Views: 291

Answers (1)

Victor Eijkhout
Victor Eijkhout

Reputation: 5842

You can get shared memory by MPI mechanisms: first MPI_Comm_split_type to get you a communicator per shared memory domain, and then MPI_Win_allocate_shared. You can then put your matrix row on that shared memory.

For details see https://theartofhpc.com/pcse/mpi-shared.html

Note that in general your approach doesn't work of first making a communicator per row, and then getting a shared memory on it. MPI typically runs on clusters with both distributed and shared memory, so you're limited in what processes you can group together in shared memory. Basically, that choice has been made for you.

I will also note that shared memory is hardly ever needed.

Upvotes: 1

Related Questions