On Thu, Apr 12, 2012 at 4:37 PM, Zhenglun (Alan) Wei <span dir="ltr">&lt;<a href="mailto:zhenglun.wei@gmail.com">zhenglun.wei@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

  

    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div style="font-family:-moz-fixed;font-size:14px" lang="x-western">Dear All,
      <br>
           I hope you&#39;re having a nice day.
      <br>
           I came across a problem while using MPI derived data type.
      Although it does not related to PETSc, I hope you guys can give me
      some suggestions or comments.
      <br>
           The reason I want to do this is that, in my PETSc code, I
      want to send a set of data. It is fairly easy to do that if data
      is contiguous; while it would be comparatively hard if it is not.
      I searched online and found the MPI derived data type may help me.
      What I learned are MPI_TYPE_CONTIGUOUS, MPI_TYPE_VECTOR  and
      MPI_TYPE_STRUCT. I did a test program, which basically combined
      several example programs in <a href="http://computing.llnl.gov/tutorials/mpi/#Derived_Data_Types" target="_blank">&quot;http://computing.llnl.gov/tutorials/mpi/#Derived_Data_Types&quot;</a>
      with all these three MPI derived data types. It works fine.
      (Please see my attachment for AlanFloatStruct.c). Briefly saying,
      it generates a 4 by 4 matrix &#39;a[i][j]&#39;, where &#39;a&#39; is a structure
      with members of &#39;u&#39; and &#39;v&#39;:
      <br>
      <br>
      a[i][j].u is
      <br>
         01   02   03   04      // 2 digits just for E-mail format.
      Actually, you may find that data type for member &#39;.u&#39; is &#39;double&#39;
      <br>
         05   06   07   08
      <br>
         09   10   11   12
      <br>
         13   14   15   16
      <br>
      a[i][j].v = 10*a[i][j].u  // I will neglect .v later, because I
      want to focus on .u member
      <br>
      <br>
           As I defined, &#39;j&#39; in &#39;a[i][j]&#39; is the inner loop. For
      example, a[0][0] = 1,  a[0][1] = 2, a[0][2] = 3 ... So, they are
      contiguous. Then, &#39;i&#39; is the outer loop, a[0][0] = 1, a[1][0] = 5
      ..., which are not contiguous. <br>
           My purpose is let rank = 0 to send the 4 contiguous data of
      a[n][m] (m = 0~3) to n&#39;th rank and store them to &#39;b[n][p]&#39; in the
      n&#39;th rank. Then, I want to send the 4 non-contiguous data of
      a[m][n] (m = 0~3) to n&#39;th rank and store them to &#39;c[p][n]&#39; in the
      n&#39;th rank. For example, if &#39;b&#39; and &#39;c&#39; are 4 by 4 matrix and
      initialized by &#39;-5&#39;, after communication with rank = 0, I expect
      in rank = 1
      <br>
      <br>
      b[i][j].u
      <br>
         -5   -5   -5   -5
      <br>
         05   06   07   08
      <br>
         -5   -5   -5   -5
      <br>
         -5   -5   -5   -5
      <br>
      c[i][j].v
      <br>
         -5   02   -5   -5
      <br>
         -5   06   -5   -5
      <br>
         -5   10   -5   -5
      <br>
         -5   14   -5   -5
      <br>
      <br>
          Everything is fine in AlanFloatStruct.c. <b>However, when I
        was trying to use &#39;calloc&#39; to allocate memory to &#39;a[][]&#39; rather
        than directly define &#39;a[][]&#39; as a structure matrix, the problem
        comes.</b> (see AlanTEST1.c). &#39;b&#39; matrix is ok, yet, it keeps
      giving me problems for &#39;c&#39; that
      <br>
      </div></div></blockquote><div><br></div><div>If you declare c[][] on the stack, most compilers will lay it out contiguously. However, if you separately allocate each row, they</div><div>need not be contiguous. If you really want to do this, allocate one big block, and setup the pointers to each row yourself.</div>
<div><br></div><div>  Thanks,</div><div><br></div><div>     Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div bgcolor="#FFFFFF" text="#000000">
<div style="font-family:-moz-fixed;font-size:14px" lang="x-western">c[i][j].u
      <br>
         -5   02   -5   -5
      <br>
         -5   00   -5   -5
      <br>
         -5   00   -5   -5
      <br>
         -5   00   -5   -5
      <br>
      <br>
          I guess it would be some problem for byte counting or padding
      of MPI data in &#39;calloc&#39;. I refer to &#39;Notes&#39; part in &#39;<a href="http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Type_struct/html" target="_blank">www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Type_struct/html</a>&#39;
      and made AlanTEST2.c. The problem is still there.
      <br>
          I wonder what should I for this? Or, does PETS have any
      alternative way to fix this problem. BTW, if you want to run my
      code, you need change the name of .c file to AlanTEST.c. My script
      file &#39;AlanRun&#39; is written based on AlanTEST.c.
      <br>
      <br>
      thank you so much in advance,
      <br>
      Alan
      <br>
      <br>
    </div>
  </div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>
-- Norbert Wiener<br>