aboutsummaryrefslogtreecommitdiff
path: root/src/CollectData1D.c
blob: 0af3228f232ebeb64f397e2fdb70feab72fb1684 (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
#include <stdio.h>
#include <stdlib.h>

#include "cctk.h"
#include "CactusPUGH/PUGH/src/include/pugh.h"
#include "PUGHSlab.h"

static char *rcsid = "$Header$";

CCTK_FILEVERSION(CactusPUGH_PUGHSlab_CollectData1D_c)

/*#define DEBUGME  1*/


#define PICKUP_LINE_DATA(GH, cctk_type, vsize, start, directions, vdata, hdata)\
          {                                                                   \
            int i, dim, point [3];                                            \
            cctk_type *typed_data = (cctk_type *) hdata;                      \
                                                                              \
                                                                              \
            for (i = 0; i < vsize; i++)                                       \
            {                                                                 \
              for (dim = 0; dim < 3; dim++)                                   \
                point [dim] = start [dim] + i*directions [dim];               \
              *typed_data++ = ((cctk_type *) vdata)                           \
                [CCTK_GFINDEX3D (GH, point [0], point [1], point [2])];       \
            }                                                                 \
          }

/* local function prototypes */
int GetLocalLine(cGH *GH, pGExtras *extras,
                 int vardim, const int *S_l, const int *u_l, int ds, int len_l,
                 int *locstart, int *locend, int *nlocpoints);
#ifdef CCTK_MPI
static MPI_Datatype CCTKtoMPItype(pGH *pughGH, int cctktype);
#endif



static int CollectLocalData1D(cGH *GH, int vindex, int vtimelvl,
                              const int *origin, const int *directions,
                              int downsample, int length,
                              void **hdata, int *hsize)
{
  int idim, ierr;
  cGroup groupinfo;
  int locstart [3], locend [3];
  void *vdata = CCTK_VarDataPtrI (GH, vtimelvl, vindex);
  pGExtras *extras = ((pGA *) PUGH_pGH (GH)->variables [vindex][vtimelvl])->extras;



  CCTK_GroupData (CCTK_GroupIndexFromVarI (vindex), &groupinfo);

  if (groupinfo.dim != 3)
  {
    CCTK_WARN (1, "CollectLocalData1D() processes 3D variables only");
    return (-1);
  }

  ierr = GetLocalLine(GH, extras, 3, origin, directions, downsample, length, 
                          locstart, locend, hsize);
  if (ierr < 0)
  {
    CCTK_WARN (1, "GetLocalLine() failed");
    return (ierr);
  }

  /* Transform from global to local index coordinate */
  for(idim=0;idim<3;idim++)
  {  
    locstart[idim]-=GH->cctk_lbnd[idim];
    locend[idim]  -=GH->cctk_lbnd[idim];
  }

#ifdef DEBUGME
  printf("CollectLocal1D: %d npoints   lb: %d %d %d\n",
         *hsize,GH->cctk_lbnd[0],GH->cctk_lbnd[1],GH->cctk_lbnd[2]);
#endif

  *hdata= malloc (*hsize * CCTK_VarTypeSize (groupinfo.vartype));

  /* CCTK_VARIABLE_INT */
  switch (groupinfo.vartype)
  {
    case CCTK_VARIABLE_CHAR:
      PICKUP_LINE_DATA(GH, CCTK_BYTE, *hsize, locstart, directions, vdata, *hdata);
      break;

    case CCTK_VARIABLE_INT:
      PICKUP_LINE_DATA(GH, CCTK_INT, *hsize, locstart, directions, vdata, *hdata);
      break;

    case CCTK_VARIABLE_REAL:
      PICKUP_LINE_DATA(GH, CCTK_REAL, *hsize, locstart, directions, vdata, *hdata);
      break;

    case CCTK_VARIABLE_COMPLEX:
      PICKUP_LINE_DATA(GH, CCTK_COMPLEX, *hsize, locstart, directions, vdata, *hdata);
      break;

    default:
      CCTK_WARN (1, "Unsupported variable type");
      ierr = -1;
      break;
  }

  return (ierr);
}


int Hyperslab_CollectData1D(cGH *GH, int vindex, int vtimelvl,
                            const int *origin, const int *directions,
                            int downsample, int length,
                            void **hdata, int *hsize, int proc)
{

  void *loc_dptr;
  int   loc_dcount;
  void *all_dptr;
  int   all_dcount=0;
  int vtype, vtypesize;
  int myproc,nprocs;
  int ierr;

#ifdef CCTK_MPI
  int iproc;
  pGH *pughGH;   
  CCTK_INT  tmp, *rem_dcount;
  MPI_Datatype vmpitype;
  int *recvcnts, *displs;
#endif


  vtype    = CCTK_VarTypeI(vindex);
  vtypesize= CCTK_VarTypeSize(vtype);

  myproc   = CCTK_MyProc(GH); 
  nprocs   = CCTK_nProcs(GH);


  ierr = CollectLocalData1D(GH, vindex, vtimelvl,
                            origin, directions, downsample, length, 
                            &loc_dptr, &loc_dcount);

  if (ierr)
  {
    CCTK_WARN (1, "CollectLocalData1D() failed");
    return (ierr);
  }

#ifdef CCTK_MPI

  pughGH   = PUGH_pGH(GH);

  rem_dcount = NULL;
  recvcnts = NULL;
  displs = NULL;
  vmpitype= CCTKtoMPItype(pughGH, vtype);

  /* Send local number of data elements */
  tmp = (CCTK_INT) loc_dcount;

  if (proc < 0 || proc == myproc)
  {
    rem_dcount= (CCTK_INT *) malloc (nprocs * sizeof (CCTK_INT));
    recvcnts  = (int *) malloc (nprocs * sizeof (int));
    displs    = (int *) malloc (nprocs * sizeof (int));
  }

  if (proc < 0)
  {
    CACTUS_MPI_ERROR (MPI_Allgather (&tmp, 1, PUGH_MPI_INT,
                                     rem_dcount, 1, PUGH_MPI_INT,
                                     pughGH->PUGH_COMM_WORLD));
  } else {
    CACTUS_MPI_ERROR (MPI_Gather (&tmp, 1, PUGH_MPI_INT,
                                  rem_dcount, 1, PUGH_MPI_INT,
                                  proc, pughGH->PUGH_COMM_WORLD));
  }

  if (proc < 0 || proc == myproc)
  {
    displs[0]  = 0;
    all_dcount = recvcnts[0] = (int) rem_dcount[0];
    for (iproc=1;iproc<nprocs;iproc++)
    {
      recvcnts[iproc] = (int) rem_dcount[iproc];
      displs[iproc] = displs[iproc-1] + recvcnts[iproc-1];
      all_dcount   += recvcnts[iproc];
    }

#ifdef DEBUGME
    printf ("  local hsize: %d\n", loc_dcount);
    for (iproc=0;iproc<nprocs;iproc++)
      printf(" DISPLS[%d] rem: %d   displs: %d   all %d \n",
             iproc, recvcnts[iproc],displs[iproc],all_dcount);
    fflush(stdout);
#endif

    all_dptr = malloc (all_dcount * vtypesize);
  }
  else
  {
    all_dptr = NULL;
  }

  if (proc < 0)
  {
    CACTUS_MPI_ERROR (MPI_Allgatherv(loc_dptr, loc_dcount, vmpitype, 
                                     all_dptr, recvcnts, displs, vmpitype,
                                     pughGH->PUGH_COMM_WORLD));
  } else {
    CACTUS_MPI_ERROR (MPI_Gatherv(loc_dptr, loc_dcount, vmpitype, 
                                  all_dptr, recvcnts, displs, vmpitype,
                                  proc, pughGH->PUGH_COMM_WORLD));
  }

  if (proc < 0 || proc == myproc)
  {
    free (rem_dcount);
    free (recvcnts);
    free (displs);
  }

#else
  all_dptr = loc_dptr;
  all_dcount = loc_dcount;
#endif

  /* assign return values */
  if (proc < 0 || proc == myproc)
  {
    *hdata = all_dptr;
    *hsize = all_dcount;
  }

#ifdef DEBUGME
  printf(" CollectData1D done \n");
#endif
  return(0);
}


#ifdef CCTK_MPI
static MPI_Datatype CCTKtoMPItype(pGH *pughGH, int cctktype)
{
  MPI_Datatype mpitype;
  switch (cctktype) {

  case CCTK_VARIABLE_CHAR:
      mpitype  = PUGH_MPI_CHAR;
      break;

  case CCTK_VARIABLE_INT:
    mpitype = PUGH_MPI_INT;
    break;

  case CCTK_VARIABLE_REAL:
    mpitype = PUGH_MPI_REAL;
    break;

  case CCTK_VARIABLE_COMPLEX:
    mpitype = pughGH->PUGH_mpi_complex;
    break;

  default:
    CCTK_WARN (1, "Unsupported MPI variable type");
    mpitype = 0;
    break;
  }

  return(mpitype);
}
#endif