Thread: Extract 2D subarray using MPI_Type_vector()

  1. #1
    Registered User
    Join Date
    Nov 2009

    Extract 2D subarray using MPI_Type_vector()

    Hi all. The following code (which I am typing in, excuse my typos please) is not working as expected.

    #include <stdlib.h>
    #include <mpi.h>
    #include <stdio.h>
    int main(int argc, char **argv){
    double u[4][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
    double *recv_buff;
    int size, rank;
    MPI_Datatype matrix;
    MPI_Status status;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Type_vector(4,2,4, MPI_DOUBLE, &matrix);
    if(rank ==0){
       for (i = 0; i < 4; i++){
          for (j = 0; j<4; j++){
              printf("u[%i][%j]=%4.3f\n", u[i][j]);
    MPI_Send(u[0][0],1,matrix, 1,0, MPI_COMM_WORD);
    if (rank == 1){
    recv_buff = malloc(sizeof(double *) * 2);
    for (i = 0; i<2; i++)
       recv_buff[i] = malloc(sizeof(double) * 2);
       MPI_Recv(recv_buff[0][0], 1, matrix, 0,0,MPI_COMM_WORLD, &status);
       for (i = 0; i < 2; i++){
          for (j = 0; j<2; j++){
              printf("recv_buff[%i][%j]=%4.3f\n", recv_buff[i][j]);

    I was expecting the recv_buffer to contain the 4 top-left entries in the original grid, that is the values 1, 2, 5, 6

    This code doesnt seem to be working for me. I can't seem to figure out the values for the stride, or the allocations, or something, that will give me those values. Am I forgetting something? It had occured to me maybe i am declaring the arrays wrong, or something, but I am out of ideas. Thanks
    Last edited by DerekC; 03-05-2010 at 05:07 PM. Reason: code typo

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > MPI_Send(u[0][0],1,matrix, 1,0, MPI_COMM_WORD);
    You're passing the top-left element BY VALUE.

    Do you even get a warning?
    I get - error: incompatible type for argument 1 of ‘MPI_Send’

    > MPI_Recv(recv_buff[0][0], 1, matrix, 0,0,MPI_COMM_WORLD, &status);
    Again, passing by value rather than by reference.
    Even worse, MPI_Recv will assume that the single pointer is to CONTIGUOUS memory. Your fancy 2D pointer allocation isn't.

    Fix the send to be
    MPI_Send(u,1,matrix, 1,0, MPI_COMM_WORD);
    Fix the recv to be (for now)
    double temp[16] = { 0.0 };
    MPI_Recv(temp, 1, matrix, 0,0,MPI_COMM_WORLD, &status);
    // now print out all 16 entries
    When you're reliably receiving the values of interest in the first 4 entries, then you can think about how to allocate the memory for it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Thanks for the suggestions, I will check into it monday when i get access back to the compiler/system.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D in directX
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 01-25-2009, 11:23 AM
  2. Best Graphics Library for 2d RTS?
    By arew264 in forum Game Programming
    Replies: 4
    Last Post: 04-18-2007, 12:05 PM
  3. Replies: 16
    Last Post: 09-22-2006, 03:39 PM
  4. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  5. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM

Tags for this Thread