aboutsummaryrefslogtreecommitdiff
path: root/src/PostSendGA.c
blob: 732ce52ab2021d04fe856466f24772679c2facf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
 /*@@
   @file      PostSendGA.c
   @date      Wed  Feb  2 11:58:14 2000
   @author    Gabrielle Allen
   @desc 
   The send of the three-way pugh communcations branch
   which is @seefile PostReceiveGA.c, @seefile PostSendGA.c
   and @seefile FinishReceiveGA.c. See the documentation for
   @seeroutine PostSendGA for details on this routine.
   @enddesc 
   @version $Header$
 @@*/

#include <stdio.h>

#include "cctk.h"
#include "pugh.h"

static char *rcsid = "$Header$";

void PostSendGA1(pGH *GH, pGA *GA, int dir);
void PostSendGA3(pGH *GH, pGA *GA, int dir);

/*#define DEBUG_PUGH*/

 /*@@
   @routine    PostSendGA
   @date       Thu Apr  3 11:58:59 1997
   @author     Paul Walker
   @desc 
   This routine posts an asyncronous MPI send of a buffer
   for a face (MPI_Isend). It does this by first packing up
   a send buffer (allocated in @seeroutine SetupPGF) then
   sending it out on the pipe.
   <p>
   Since this is an asynchronous communications model we
   assume that a recieve has been posted for the send.
   thus the correct calling order for this is as in
   @seeroutine SyncGF, eg, after a Recv.
   <p>
   Note this does <b>not</b> wait on the send buffer from previous
   communications. It is the users responsibility to wait on
   that buffer.
   @enddesc 
   @calledby   SyncGroupGF
   @history
   @hdate Nov 4 1998 @hauthor Gabrielle Allen
   @hdesc Allow for forced synchronization of all GFs with storage
   @endhistory
@@*/

void PostSendGA(pGH *GH, pGA *GA, int dir)
{

#ifdef MPI
  int stag, dircomp;
  double ts, tw, tp;
  MPI_Datatype mpi_type;
  MPI_Datatype *send_dt;
  MPI_Status ms;

  if (!(GA->storage)) 
  {
    return;
  }
  
  if (!(GH->forceSync || GA->docomm[dir])) 
  {
    return;
  }

  if (GA->connectivity->neighbours[GH->myproc][dir] < 0) 
  {
    return;
  }

  switch (GA->vtype) 
  {

    case CCTK_VARIABLE_CHAR:
      mpi_type = PUGH_MPI_CHAR;
      send_dt = GH->send_char_dt [GA->stagger];
      break;

    case CCTK_VARIABLE_INT:
      mpi_type = PUGH_MPI_INT;
      send_dt = GH->send_int_dt [GA->stagger];
      break;

    case CCTK_VARIABLE_REAL:
      mpi_type = PUGH_MPI_REAL;
      send_dt = GH->send_real_dt [GA->stagger];
      break;

    case CCTK_VARIABLE_COMPLEX:
      mpi_type = GH->pugh_mpi_complex;
      send_dt = GH->send_complex_dt [GA->stagger];
      break;

    default:
      CCTK_WARN (1, "Unknown variable type in PostSendGA");
      return;

  }

  ts = MPI_Wtime();
  dircomp = dir+1;                /* Complementary direction */
  if (dircomp %2 == 0) 
  {
    dircomp = dir-1;
  }

  /* Note this is the complement of the rtag set in PostReceive */
  stag = 1000 + dircomp + 2 * 
    (GA->connectivity->neighbours[GH->myproc][dir] + 
     GH->nprocs * GA->id);

  /* mod the tag to force MPI compliance */
  stag = stag % 32768;

#ifdef DEBUG_PUGH
  printf ("SEND: GA %s Side %d Proc %d stag %d Size %d to %d\n",
          GA->name,
          dir,
          GH->myproc,
          stag,
          GA->buffer_sz[dir],
          GA->connectivity->neighbours[GH->myproc][dir]);
#endif

  if (GH->commmodel == PUGH_DERIVEDTYPES)
  {
    CACTUS_MPI_ERROR (MPI_Isend (GA->data,
                      1, send_dt [dir],
                      GA->connectivity->neighbours [GH->myproc][dir], stag,
                      GH->PUGH_COMM_WORLD, &(GA->sreq [dir])));
  }

  if (GH->commmodel == PUGH_ALLOCATEDBUFFERS) 
  {
    if (GA->connectivity->dim == 3)
    {
      PostSendGA3(GH,GA,dir);
    }
    else if (GA->connectivity->dim == 1)
    {
      PostSendGA1(GH,GA,dir);
    }
    else
    {
      CCTK_WARN(0,"PUGH does not yet support this dimension");
    }

    tp = MPI_Wtime();
    
    /* post send */
    CACTUS_MPI_ERROR (MPI_Isend (GA->send_buffer [dir],
                      GA->buffer_sz [dir], mpi_type,
                      GA->connectivity->neighbours [GH->myproc][dir], stag,
                      GH->PUGH_COMM_WORLD, &(GA->sreq [dir])));
    tw = MPI_Wtime();
#ifdef COMM_TIMING
    printf ("GA %s Dir %d Time %f\n", GA->name, dir, tw-ts);
#endif
  }
#endif
}

void PostSendGA3(pGH *GH, pGA *GA, int dir)
{

  int           ii,jj,kk,xx;
  int           istart, iend;
  int           jstart, jend;
  int           kstart, kend;
  CCTK_CHAR    *char_data;
  CCTK_INT     *int_data;
  CCTK_REAL    *real_data;
  CCTK_COMPLEX *complex_data;

  /* Copy my information into the send buffer */
  istart = GA->extras->overlap[GA->stagger][0][dir][0];
  iend   = GA->extras->overlap[GA->stagger][1][dir][0];
  jstart = GA->extras->overlap[GA->stagger][0][dir][1];
  jend   = GA->extras->overlap[GA->stagger][1][dir][1];
  kstart = GA->extras->overlap[GA->stagger][0][dir][2];
  kend   = GA->extras->overlap[GA->stagger][1][dir][2];
    
  /* Great now copy */
  xx = 0;
  switch (GA->vtype) 
  {
    case CCTK_VARIABLE_CHAR:
      char_data = (CCTK_CHAR *) GA->send_buffer [dir];
      for (kk=kstart; kk<kend; kk++) 
      {
        for (jj=jstart; jj<jend; jj++)
        {
          for (ii=istart; ii<iend; ii++)
          {
            char_data [xx++] =
              ((CCTK_CHAR *) GA->data) [DATINDEX (GA->extras,ii,jj,kk)];
          }
        }
      }
      break;
    
    case CCTK_VARIABLE_INT:
      int_data = (CCTK_INT *) GA->send_buffer [dir];
      for (kk=kstart; kk<kend; kk++) 
      {
        for (jj=jstart; jj<jend; jj++)
        {
          for (ii=istart; ii<iend; ii++)
          {
            int_data [xx++] =
              ((CCTK_INT *) GA->data) [DATINDEX (GA->extras,ii,jj,kk)];
          }
        }
      }
      break;
      
    case CCTK_VARIABLE_REAL:
      real_data = (CCTK_REAL *) GA->send_buffer [dir];
      for (kk=kstart; kk<kend; kk++)
      { 
        for (jj=jstart; jj<jend; jj++)
        {
          for (ii=istart; ii<iend; ii++)
          {
            real_data [xx++] =
              ((CCTK_REAL *) GA->data) [DATINDEX (GA->extras,ii,jj,kk)];
          }
        }
      }
      break;
      
    case CCTK_VARIABLE_COMPLEX:
      complex_data = (CCTK_COMPLEX *) GA->send_buffer [dir];
      for (kk=kstart; kk<kend; kk++) 
      {
        for (jj=jstart; jj<jend; jj++)
        {
          for (ii=istart; ii<iend; ii++)
          {
            complex_data [xx++] =
              ((CCTK_COMPLEX *) GA->data) [DATINDEX (GA->extras,ii,jj,kk)];
          }
        }
      }
      break;
  }
}



void PostSendGA1(pGH *GH, pGA *GA, int dir)
{

  int ii,xx;
  int istart, iend;
  CCTK_CHAR *char_data;
  CCTK_INT *int_data;
  CCTK_REAL *real_data;
  CCTK_COMPLEX *complex_data;

  /* Copy my information into the send buffer */
  istart = GA->extras->overlap[GA->stagger][0][dir][0];
  iend   = GA->extras->overlap[GA->stagger][1][dir][0];
    
  /* Great now copy */
  xx = 0;
  switch (GA->vtype) 
  {
    case CCTK_VARIABLE_CHAR:
      char_data = (CCTK_CHAR *) GA->send_buffer [dir];
      for (ii=istart; ii<iend; ii++)
      {
        char_data [xx++] = ((CCTK_CHAR *) GA->data) [ii];
      }
      break;
    
    case CCTK_VARIABLE_INT:
      int_data = (CCTK_INT *) GA->send_buffer [dir];
      for (ii=istart; ii<iend; ii++)
      {
        int_data [xx++] = ((CCTK_INT *) GA->data) [ii];
      }
      break;
      
    case CCTK_VARIABLE_REAL:
      real_data = (CCTK_REAL *) GA->send_buffer [dir];
      for (ii=istart; ii<iend; ii++)
      {
        real_data [xx++] = ((CCTK_REAL *) GA->data) [ii];
      }
      break;
      
    case CCTK_VARIABLE_COMPLEX:
      complex_data = (CCTK_COMPLEX *) GA->send_buffer [dir];
      for (ii=istart; ii<iend; ii++)
      {
        complex_data [xx++] = ((CCTK_COMPLEX *) GA->data) [ii];
      }
      break;
  }
}