title: MPI: How to use MPI_Win_allocate_shared properly
body: <p>I would like to use a shared memory between processes. I tried MPI_Win_allocate_shared but it gives me a strange error when I execute the program:</p>
  <p>Assertion failed in file <code>./src/mpid/ch3/include/mpid_rma_shm.h</code> at line 592: <code>local_target_rank &gt;= 0</code>
  <code>internal ABORT</code></p>
  <p>Here's my source:</p>
  <pre class="lang-c prettyprint-override"><code>    # include &lt;stdlib.h&gt;
      # include &lt;stdio.h&gt;
      # include &lt;time.h&gt;
      
      # include &quot;mpi.h&quot;
      
      int main ( int argc, char *argv[] );
      void pt(int t[], int s);
      
      int main ( int argc, char *argv[] )
      {
          int rank, size, shared_elem = 0, i;
          MPI_Init ( &amp;argc, &amp;argv );
          MPI_Comm_rank ( MPI_COMM_WORLD, &amp;rank );
          MPI_Comm_size ( MPI_COMM_WORLD, &amp;size );
          MPI_Win win;
          int *shared;
          
          if (rank == 0) shared_elem = size;
          MPI_Win_allocate_shared(shared_elem*sizeof(int), sizeof(int), MPI_INFO_NULL, MPI_COMM_WORLD, &amp;shared, &amp;win);
          if(rank==0)
          {
              MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, MPI_MODE_NOCHECK, win);
              for(i = 0; i &lt; size; i++)
              {
                  shared[i] = -1;
              }
              MPI_Win_unlock(0,win);
          }
          MPI_Barrier(MPI_COMM_WORLD);
          int *local = (int *)malloc( size * sizeof(int) );
          MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
          for(i = 0; i &lt; 10; i++)
          {
              MPI_Get(&amp;(local[i]), 1, MPI_INT, 0, i,1, MPI_INT, win);
          }
          printf(&quot;processus %d (avant): &quot;, rank);
          pt(local,size);
          MPI_Win_unlock(0,win);
          
          MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
          
          MPI_Put(&amp;rank, 1, MPI_INT, 0, rank, 1, MPI_INT, win);
          
          MPI_Win_unlock(0,win);
          
          MPI_Win_lock(MPI_LOCK_SHARED, 0, 0, win);
          for(i = 0; i &lt; 10; i++)
          {
              MPI_Get(&amp;(local[i]), 1, MPI_INT, 0, i,1, MPI_INT, win);
          }
          printf(&quot;processus %d (apres): &quot;, rank);
          pt(local,size);
          MPI_Win_unlock(0,win);
          
          
          MPI_Win_free(&amp;win);
          MPI_Free_mem(shared);
          MPI_Free_mem(local);
          MPI_Finalize ( );
          
          return 0;
      }
      
      void pt(int t[],int s)
      {
          int i = 0;
          while(i &lt; s)
          {
              printf(&quot;%d &quot;,t[i]);
              i++;
          }
          printf(&quot;\n&quot;);
      }
  </code></pre>
  <p>I get the following result:</p>
  <pre><code>processus 0 (avant): -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
  processus 0 (apres): 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 
  processus 4 (avant): 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 
  processus 4 (apres): 0 -1 -1 -1 4 -1 -1 -1 -1 -1 
  Assertion failed in file ./src/mpid/ch3/include/mpid_rma_shm.h at line 592: local_target_rank &gt;= 0
  internal ABORT - process 5
  Assertion failed in file ./src/mpid/ch3/include/mpid_rma_shm.h at line 592: local_target_rank &gt;= 0
  internal ABORT - process 6
  Assertion failed in file ./src/mpid/ch3/include/mpid_rma_shm.h at line 592: local_target_rank &gt;= 0
  internal ABORT - process 9
  </code></pre>
  <p>Can someone please help me figure out what's going wrong &amp; what that error means ? Thanks a lot.</p>