aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/gdata.cc
blob: 207cc262430e58319e0b1869c1ea4680bb196359 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <cassert>
#include <cstdlib>
#include <iostream>

#include <mpi.h>

#include "cctk.h"
#include "cctk_Parameters.h"

#include "util_ErrorCodes.h"
#include "util_Table.h"

#include "bbox.hh"
#include "commstate.hh"
#include "defs.hh"
#include "dist.hh"
#include "vect.hh"

#include "gdata.hh"

using namespace std;



// Hand out the next MPI tag
static int nexttag ()
{
  DECLARE_CCTK_PARAMETERS;

  int const min_tag = 100;
  static int last = 0;
  ++last;
  if (last >= max_mpi_tags) last = 0;
  return min_tag + last;
}



// Constructors
gdata::gdata (const int varindex_,
              const centering cent_,
              const operator_type transport_operator_,
              const int tag_)
  : varindex(varindex_),
    cent(cent_),
    transport_operator(transport_operator_),
    _has_storage(false),
    comm_active(false),
    tag(tag_ >= 0 ? tag_ : nexttag())
{
  DECLARE_CCTK_PARAMETERS;
  if (barriers) {
    MPI_Barrier (dist::comm());
  }
}

// Destructors
gdata::~gdata ()
{
  DECLARE_CCTK_PARAMETERS;
  if (barriers) {
    MPI_Barrier (dist::comm());
  }
}



// Processor management
void gdata::change_processor (comm_state& state,
                              const int newproc,
                              void* const mem)
{
  // if this function is being called with collective commbuffers turned on,
  // mimic the old state transitions here
  switch (state.thestate) {
  case state_post:
  case state_get_buffer_sizes:
    change_processor_recv (state, newproc, mem);
    change_processor_send (state, newproc, mem);
    break;
  case state_wait:
  case state_fill_send_buffers:
    change_processor_wait (state, newproc, mem);
    break;
  case state_empty_recv_buffers:
    break;
  default:
    assert(0 and "invalid state");
  }
}



// Data manipulators
void gdata::copy_from (comm_state& state,
                       const gdata* src, const ibbox& box)
{
  assert (has_storage() and src->has_storage());
  assert (all(box.lower()>=extent().lower()
          and box.lower()>=src->extent().lower()));
  assert (all(box.upper()<=extent().upper()
          and box.upper()<=src->extent().upper()));
  assert (all(box.stride()==extent().stride()
          and box.stride()==src->extent().stride()));
  assert (all((box.lower()-extent().lower())%box.stride() == 0
          and (box.lower()-src->extent().lower())%box.stride() == 0));

  if (box.empty()) return;
  if (dist::rank() != proc() and dist::rank() != src->proc()) return;

  switch (state.thestate) {
  case state_post:
    if (proc() == src->proc()) {
      copy_from_innerloop (src, box);
    } else {
      copy_from_post (state, src, box);
    }
    break;

  case state_wait:
    if (proc() != src->proc()) {
      copy_from_wait (state, src, box);
    }
    break;

  case state_get_buffer_sizes:
    // don't count processor-local copies
    if (proc() != src->proc()) {
      // if this is a destination processor: advance its recv buffer size
      vector<comm_state::procbufdesc>& procbufs =
        state.typebufs.at(c_datatype()).procbufs;
      if (proc() == dist::rank()) {
        procbufs.at(src->proc()).recvbufsize += box.size();
        state.typebufs.at(c_datatype()).in_use = true;
      }
      // if this is a source processor: increment its send buffer size
      if (src->proc() == dist::rank()) {
        procbufs.at(proc()).sendbufsize += box.size();
        state.typebufs.at(c_datatype()).in_use = true;
      }
    }
    break;

  case state_fill_send_buffers:
    // if this is a source processor: copy its data into the send buffer
    // (the processor-local case is also handled here)
    if (src->proc() == dist::rank()) {
      if (proc() == src->proc()) {
        copy_from_innerloop (src, box);
      } else {
        copy_into_sendbuffer (state, src, box);
      }
    }
    break;

  case state_empty_recv_buffers:
    // if this is a destination processor and data has already been received
    // from the source processor: copy it from the recv buffer
    if (proc() == dist::rank() and
        state.recvbuffers_ready.at(dist::size()*c_datatype() + src->proc())) {
      copy_from_recvbuffer (state, src, box);
    }
    break;

  default:
    assert(0 and "invalid state");
  }
}


void gdata::copy_from_post (comm_state& state,
                            const gdata* src, const ibbox& box)
{
  wtime_copyfrom_recv.start();

  if (dist::rank() == proc()) {

    // this processor receives data

    wtime_copyfrom_recvinner_allocate.start();
    comm_state::gcommbuf * b = make_typed_commbuf (box);
    int typesize;
    MPI_Type_size (b->datatype(), & typesize);
    wtime_copyfrom_recvinner_allocate.stop(b->size() * typesize);

    wtime_copyfrom_recvinner_recv.start();
    MPI_Irecv (b->pointer(), b->size(), b->datatype(), src->proc(),
               tag, dist::comm(), &b->request);
    wtime_copyfrom_recvinner_recv.stop(b->size() * typesize);
    state.requests.push_back (b->request);
    state.recvbufs.push (b);

  } else {
    // this processor sends data

    wtime_copyfrom_sendinner_allocate.start();
    comm_state::gcommbuf * b = src->make_typed_commbuf (box);
    int typesize;
    MPI_Type_size (b->datatype(), & typesize);
    wtime_copyfrom_sendinner_allocate.stop(b->size() * typesize);

    // copy data into send buffer
    wtime_copyfrom_sendinner_copy.start();
    const ibbox& ext = src->extent();
    ivect myshape = ext.shape() / ext.stride();
    ivect items = (box.upper() - box.lower()) / box.stride() + 1;
    ivect offs  = (box.lower() - ext.lower()) / ext.stride();
    char* send_buffer = (char*) b->pointer();
    int& datatypesize = state.typebufs.at(c_datatype()).datatypesize;

    double bytes = 0;
    for (int k = 0; k < items[2]; k++) {
      for (int j = 0; j < items[1]; j++) {
        int i = offs[0] + myshape[0]*((j+offs[1]) + myshape[1]*(k+offs[2]));
        memcpy (send_buffer, ((char*) src->storage()) + datatypesize*i,
                datatypesize * items[0]);
        send_buffer += datatypesize * items[0];
        bytes += datatypesize * items[0];
      }
    }
    wtime_copyfrom_sendinner_copy.stop(bytes);

    wtime_copyfrom_sendinner_send.start();
    MPI_Isend (b->pointer(), b->size(), b->datatype(), proc(),
               tag, dist::comm(), &b->request);
    wtime_copyfrom_sendinner_send.stop(b->size() * typesize);
    state.requests.push_back (b->request);
    state.sendbufs.push (b);
  }

  wtime_copyfrom_recv.stop();
}


void gdata::copy_from_wait (comm_state& state,
                            const gdata* src, const ibbox& box)
{
  wtime_copyfrom_wait.start();

  wtime_copyfrom_recvwaitinner_wait.start();
  if (not state.requests.empty()) {
    // wait for all requests at once
    MPI_Waitall (state.requests.size(), &state.requests.front(),
                 MPI_STATUSES_IGNORE);
    state.requests.clear();
  }
  wtime_copyfrom_recvwaitinner_wait.stop();

  queue<comm_state::gcommbuf*>* const bufs =
    dist::rank() == proc() ? &state.recvbufs : &state.sendbufs;
  comm_state::gcommbuf* b = bufs->front();

  // copy data out of receive buffer
  if (bufs == &state.recvbufs) {
    wtime_copyfrom_recvwaitinner_copy.start();
    const ibbox& ext = extent();
    ivect myshape = ext.shape() / ext.stride();
    ivect items = (box.upper() - box.lower()) / box.stride() + 1;
    ivect offs  = (box.lower() - ext.lower()) / ext.stride();
    const char* recv_buffer = (const char*) b->pointer();
    int& datatypesize = state.typebufs.at(c_datatype()).datatypesize;

    for (int k = 0; k < items[2]; k++) {
      for (int j = 0; j < items[1]; j++) {
        int i = offs[0] + myshape[0]*((j+offs[1]) + myshape[1]*(k+offs[2]));
        memcpy (((char*) storage()) + datatypesize*i, recv_buffer,
                datatypesize * items[0]);
        recv_buffer += datatypesize * items[0];
      }
    }
    wtime_copyfrom_recvwaitinner_copy.stop();
  }

  wtime_copyfrom_recvwaitinner_delete.start();
  bufs->pop();
  delete b;
  wtime_copyfrom_recvwaitinner_delete.stop();

  wtime_copyfrom_wait.stop();
}


// Copy processor-local source data into communication send buffer
// of the corresponding destination processor
void gdata::copy_into_sendbuffer (comm_state& state,
                                  const gdata* src, const ibbox& box)
{
  DECLARE_CCTK_PARAMETERS;
  
  if (proc() == src->proc()) {
    // copy on same processor
    copy_from_innerloop (src, box);
  } else {
    // copy to remote processor
    assert (src->_has_storage);
    int datatypesize = state.typebufs.at(c_datatype()).datatypesize;
    comm_state::procbufdesc& procbuf =
      state.typebufs.at(c_datatype()).procbufs.at(proc());
    assert (procbuf.sendbuf - procbuf.sendbufbase <=
            ((int)procbuf.sendbufsize - box.size()) * datatypesize);
    int const fillstate = procbuf.sendbuf + (int)box.size()*datatypesize -
                          procbuf.sendbufbase;
    assert (fillstate <= (int)procbuf.sendbufsize * datatypesize);

    // copy this processor's data into the send buffer
    const ibbox& ext = src->extent();
    ivect myshape = ext.shape() / ext.stride();
    ivect items = (box.upper() - box.lower()) / box.stride() + 1;
    ivect offs  = (box.lower() - ext.lower()) / ext.stride();
  
    assert (dim == 3);
    for (int k = 0; k < items[2]; k++) {
      for (int j = 0; j < items[1]; j++) {
        int i = offs[0] + myshape[0]*((j+offs[1]) + myshape[1]*(k+offs[2]));
        memcpy (procbuf.sendbuf,
                ((const char*) src->storage()) + datatypesize*i,
                datatypesize * items[0]);
        procbuf.sendbuf += datatypesize * items[0];
      }
    }
  
    if (not combine_sends) {
      // post the send if the buffer is full
      if (fillstate == (int)procbuf.sendbufsize * datatypesize) {
        wtime_commstate_isend.start();
        MPI_Isend (procbuf.sendbufbase, procbuf.sendbufsize,
                   state.typebufs.at(c_datatype()).mpi_datatype,
                   proc(), c_datatype(), dist::comm(),
                   &state.srequests.at(dist::size()*c_datatype() + proc()));
        wtime_commstate_isend.stop(procbuf.sendbufsize * datatypesize);
      }
    }
  }
}


// Copy processor-local destination data from communication recv buffer
// of the corresponding source processor
void gdata::copy_from_recvbuffer (comm_state& state,
                                  const gdata* src, const ibbox& box)
{
  int& datatypesize = state.typebufs.at(c_datatype()).datatypesize;
  comm_state::procbufdesc& procbuf =
    state.typebufs.at(c_datatype()).procbufs.at(src->proc());
  assert (procbuf.recvbuf - procbuf.recvbufbase <=
          ((int)procbuf.recvbufsize-box.size()) * datatypesize);

  // copy this processor's data from the recv buffer
  const ibbox& ext = extent();
  ivect myshape = ext.shape() / ext.stride();
  ivect items = (box.upper() - box.lower()) / box.stride() + 1;
  ivect offs  = (box.lower() - ext.lower()) / ext.stride();

  wtime_commstate_memcpy.start();
  double bytes = 0;
  assert (dim == 3);
  for (int k = 0; k < items[2]; k++) {
    for (int j = 0; j < items[1]; j++) {
      int i = offs[0] + myshape[0]*((j+offs[1]) + myshape[1]*(k+offs[2]));
      memcpy (((char*) storage()) + datatypesize*i,
              procbuf.recvbuf, datatypesize * items[0]);
      procbuf.recvbuf += datatypesize * items[0];
      bytes += datatypesize * items[0];
    }
  }
  wtime_commstate_memcpy.stop(bytes);
}


void gdata
::interpolate_from (comm_state& state,
                    const vector<const gdata*> srcs,
                    const vector<CCTK_REAL> times,
                    const ibbox& box, const CCTK_REAL time,
                    const int order_space,
                    const int order_time)
{
  assert (transport_operator != op_error);
  if (transport_operator == op_none) return;

  assert (has_storage());
  assert (all(box.lower()>=extent().lower()));
  assert (all(box.upper()<=extent().upper()));
  assert (all(box.stride()==extent().stride()));
  assert (all((box.lower()-extent().lower())%box.stride() == 0));
  assert (srcs.size() == times.size() and srcs.size()>0);
  for (int t=0; t<(int)srcs.size(); ++t) {
    assert (srcs.at(t)->has_storage());
    assert (all(box.lower()>=srcs.at(t)->extent().lower()));
    assert (all(box.upper()<=srcs.at(t)->extent().upper()));
  }
  assert (not box.empty());
  const gdata* src = srcs.at(0);
  if (dist::rank() != proc() and dist::rank() != src->proc()) return;

  switch (state.thestate) {
  case state_post:
    if (proc() == src->proc()) {
      interpolate_from_innerloop(srcs, times, box, time,
                                 order_space, order_time);
    } else {
      interpolate_from_post(state, srcs, times, box, time,
                            order_space, order_time);
    }
    break;
  case state_wait:
    if (proc() != src->proc()) {
      copy_from_wait (state, src, box);
    }
    break;
  case state_get_buffer_sizes:
    // don't count processor-local interpolations
    if (proc() != src->proc()) {
      // if this is a destination processor: increment its recv buffer size
      vector<comm_state::procbufdesc>& procbufs =
        state.typebufs.at(c_datatype()).procbufs;
      if (proc() == dist::rank()) {
        procbufs.at(src->proc()).recvbufsize += box.size();
        state.typebufs.at(c_datatype()).in_use = true;
      }
      // if this is a source processor: increment its send buffer size
      if (src->proc() == dist::rank()) {
        procbufs.at(proc()).sendbufsize += box.size();
        state.typebufs.at(c_datatype()).in_use = true;
      }
    }
    break;
  case state_fill_send_buffers:
    // if this is a source processor: interpolate its data into the send buffer
    // (the processor-local case is also handled here)
    if (src->proc() == dist::rank()) {
      if (proc() == src->proc()) {
        interpolate_from_innerloop(srcs, times, box, time,
                                   order_space, order_time);
      } else {
        interpolate_into_sendbuffer(state, srcs, times, box,
                                    time, order_space, order_time);
      }
    }
    break;
  case state_empty_recv_buffers:
    // if this is a destination processor and data has already been received
    // from the source processor: copy it from the recv buffer
    // (the processor-local case is not handled here)
    if (proc() == dist::rank() and
        state.recvbuffers_ready.at(dist::size()*c_datatype() + src->proc())) {
      copy_from_recvbuffer(state, src, box);
    }
    break;
  default:
    assert(0 and "invalid state");
  }
}


void gdata
::interpolate_from_post (comm_state& state,
                         const vector<const gdata*> srcs,
                         const vector<CCTK_REAL> times,
                         const ibbox& box,
                         const CCTK_REAL time,
                         const int order_space,
                         const int order_time)
{
  const gdata* src = srcs.at(0);
  if (dist::rank() == proc()) {
    // interpolate from other processor

    // this processor receives data

    comm_state::gcommbuf * b = make_typed_commbuf (box);
    int typesize;
    MPI_Type_size (b->datatype(), & typesize);

    wtime_commstate_interpolate_irecv.start();
    MPI_Irecv (b->pointer(), b->size(), b->datatype(), src->proc(),
               tag, dist::comm(), &b->request);
    wtime_commstate_interpolate_irecv.stop(b->size() * typesize);
    state.requests.push_back (b->request);
    state.recvbufs.push (b);
  } else {
    // this processor sends data

    comm_state::gcommbuf * b = src->make_typed_commbuf (box);
    int typesize;
    MPI_Type_size (b->datatype(), & typesize);

    gdata * tmp = src->make_typed (varindex, cent, transport_operator, tag);
    tmp->allocate (box, src->proc(), b->pointer());
    tmp->interpolate_from_innerloop (srcs, times, box, time,
                                     order_space, order_time);
    delete tmp;

    wtime_commstate_interpolate_from_isend.start();
    MPI_Isend (b->pointer(), b->size(), b->datatype(), proc(),
               tag, dist::comm(), &b->request);
    wtime_commstate_interpolate_from_isend.stop(b->size() * typesize);
    state.requests.push_back (b->request);
    state.sendbufs.push (b);
  }
}


// Interpolate processor-local source data into communication send buffer
// of the corresponding destination processor
void gdata
::interpolate_into_sendbuffer (comm_state& state,
                               const vector<const gdata*> srcs,
                               const vector<CCTK_REAL> times,
                               const ibbox& box,
                               const CCTK_REAL time,
                               const int order_space,
                               const int order_time)
{
  DECLARE_CCTK_PARAMETERS;
  
  if (proc() == srcs.at(0)->proc()) {
    // interpolate on same processor
    interpolate_from_innerloop (srcs, times, box, time,
                                order_space, order_time);
  } else {
    // interpolate to remote processor
    const gdata* src = srcs.at(0);
    assert (src->_has_storage);
    int& datatypesize = state.typebufs.at(c_datatype()).datatypesize;
    comm_state::procbufdesc& procbuf =
      state.typebufs.at(c_datatype()).procbufs.at(proc());
    assert (procbuf.sendbuf - procbuf.sendbufbase <=
            ((int)procbuf.sendbufsize - box.size()) * datatypesize);
    assert (src->has_storage());
    int const fillstate = (procbuf.sendbuf + box.size()*datatypesize) -
                          procbuf.sendbufbase;
    assert (fillstate <= (int)procbuf.sendbufsize * datatypesize);

    // interpolate this processor's data into the send buffer
    gdata* tmp = src->make_typed (varindex, cent, transport_operator, tag);
    tmp->allocate (box, src->proc(), procbuf.sendbuf);
    tmp->interpolate_from_innerloop (srcs, times, box, time,
                                     order_space, order_time);
    delete tmp;
  
    // advance send buffer to point to the next ibbox slot
    procbuf.sendbuf += datatypesize * box.size();
  
    if (not combine_sends) {
      // post the send if the buffer is full
      if (fillstate == (int)procbuf.sendbufsize*datatypesize) {
        wtime_commstate_interpolate_to_isend.start();
        MPI_Isend (procbuf.sendbufbase, procbuf.sendbufsize,
                   state.typebufs.at(c_datatype()).mpi_datatype,
                   proc(), c_datatype(), dist::comm(),
                   &state.srequests.at(dist::size()*c_datatype() + proc()));
        wtime_commstate_interpolate_to_isend.stop(procbuf.sendbufsize*datatypesize);
      }
    }
  }
}