Timothy  0.9
Tissue Modelling Framework
 All Data Structures Files Functions Variables Typedefs Macros
domdec.c
Go to the documentation of this file.
1 /* **************************************************************************
2  * This file is part of Timothy
3  *
4  * Copyright (c) 2014/15 Maciej Cytowski
5  * Copyright (c) 2014/15 ICM, University of Warsaw, Poland
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * *************************************************************************/
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include "global.h"
27 
32 /* arrays and variables used by Zoltan library */
33 int changes; /* 1 if partitioning was changed, 0 otherwise */
34 int numGidEntries; /* number of integers used for a global ID */
35 int numLidEntries; /* number of integers used for a local ID */
36 int numImport; /* number of objects to be sent to me */
37 ZOLTAN_ID_PTR importGlobalGids; /* global IDs of objects to be sent to me */
38 ZOLTAN_ID_PTR importLocalGids; /* local IDs of objects to be sent to me */
39 int *importProcs; /* process rank for source of each incoming object */
40 int *importToPart; /* new partition for each incoming object */
41 int numExport; /* number of objects I must send to other processes */
42 ZOLTAN_ID_PTR exportGlobalGids; /* global IDs of the objects I must send */
43 ZOLTAN_ID_PTR exportLocalGids; /* local IDs of the objects I must send */
44 int *exportProcs; /* process to which I send each of the objects */
45 int *exportToPart; /* partition to which each object will belong */
46 
50 int ztnReturnDimension(void *data, int *ierr)
51 {
52  if (sdim == 3)
53  return 3;
54  if (sdim == 2)
55  return 2;
56 }
57 
61 void ztnReturnCoords(void *data, int numGidEntries, int numLidEntries,
62  ZOLTAN_ID_PTR globalId, ZOLTAN_ID_PTR localId,
63  double *geomVec, int *ierr)
64 {
65  if (sdim == 3) {
66  geomVec[0] = cells[localId[0]].x;
67  geomVec[1] = cells[localId[0]].y;
68  geomVec[2] = cells[localId[0]].z;
69  }
70  if (sdim == 2) {
71  geomVec[0] = cells[localId[0]].x;
72  geomVec[1] = cells[localId[0]].y;
73  }
74 }
75 
79 int ztnReturnNumNode(void *data, int *ierr)
80 {
81  return lnc;
82 }
83 
87 void ztnReturnOwnedNodes(void *data, int numGIdEntries, int numLIdEntries,
88  ZOLTAN_ID_PTR globalIds, ZOLTAN_ID_PTR localIds,
89  int wgtDim, float *objWgts, int *ierr)
90 {
91  int i;
92  struct cell *p = (struct cell *) data;
93  for (i = 0; i < lnc; i++) {
94  globalIds[i * numGIdEntries] = cells[i].gid;
95  localIds[i * numLIdEntries] = i;
96  objWgts[i] = 1.0;
97  //if(nc==1 || step==0) obj_wgts[i]=1.0;
98  //else obj_wgts[i]=cells[i].density;
99  }
100 }
101 
105 int ztnReturnParticleDataSize(void *data, int numGIdEntries,
106  int numLIdEntries, ZOLTAN_ID_PTR globalId,
107  ZOLTAN_ID_PTR localId, int *ierr)
108 {
109  return sizeof(struct cellData);
110 }
111 
115 void ztnPack(void *data, int numGIdEntries, int numLIdEntries,
116  ZOLTAN_ID_PTR globalId, ZOLTAN_ID_PTR localId, int dest,
117  int size, char *buf, int *ierr)
118 {
119  struct cellData *c = (struct cellData *) data;
120  memcpy(buf, &(c[localId[0]]), sizeof(struct cellData));
121  c[(int) (*localId)].gid = -1; /* mark local particle as exported */
122 }
123 
127 void ztnPre(void *data, int numGIdEntries, int numLIdEntries,
128  int numImport, ZOLTAN_ID_PTR importGlobalIds,
129  ZOLTAN_ID_PTR importLocalIds, int *importProcs,
130  int *importToPart, int numExport,
131  ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds,
132  int *exportProcs, int *exportToPart, int *ierr)
133 {
134  /* any pre communication operations should go here */
135 }
136 
140 void ztnMid(void *data, int numGIdEntries, int numLIdEntries,
141  int numImport, ZOLTAN_ID_PTR importGlobalIds,
142  ZOLTAN_ID_PTR importLocalIds, int *importProcs,
143  int *importToPart, int numExport,
144  ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds,
145  int *exportProcs, int *exportToPart, int *ierr)
146 {
147  int pos, i;
148  struct cellData *c = (struct cellData *) data;
149  pos = 0;
150  for (i = 0; i < lnc; i++) {
151  if (i != pos && c[i].gid != -1) {
152  c[pos] = c[i];
153  }
154  if (c[i].gid != -1)
155  pos++;
156  }
157  lnc = lnc - numExport;
158 }
159 
163 void ztnPost(void *data, int numGIdEntries, int numLIdEntries,
164  int numImport, ZOLTAN_ID_PTR importGlobalIds,
165  ZOLTAN_ID_PTR importLocalIds, int *importProcs,
166  int *importToPart, int numExport,
167  ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds,
168  int *exportProcs, int *exportToPart, int *ierr)
169 {
170  /* any post communication operations should go here */
171  /* gather number of cells from each process */
172  MPI_Allgather(&lnc, 1, MPI_LONG_LONG, tlnc, 1, MPI_LONG_LONG,
173  MPI_COMM_WORLD);
174 }
175 
179 void ztnUnpack(void *data, int numGIdEntries, ZOLTAN_ID_PTR globalId,
180  int size, char *buf, int *ierr)
181 {
182  struct cellData *c = (struct cellData *) data;
183  memcpy(&c[lnc], buf, sizeof(struct cellData));
184  lnc++;
185 }
186 
191 void decompositionInit(int argc, char **argv, MPI_Comm Comm)
192 {
193  int rc;
194  float version;
195 
196  rc = Zoltan_Initialize(argc, argv, &version);
197  if (rc != ZOLTAN_OK)
198  stopRun(112, NULL, __FILE__, __LINE__);
199 
200  if (MPIrank == 0)
201  printf("Zoltan Version %.3f. Initialized.\n", version);
202 
203  ztn = Zoltan_Create(MPI_COMM_WORLD);
204 
205  Zoltan_Set_Param(ztn, "IMBALANCE_TOL", "2.0");
206  Zoltan_Set_Param(ztn, "LB_METHOD", "HSFC"); /* Hilbert Space-Filling Curve Partitioning */
207  Zoltan_Set_Param(ztn, "NUM_GID_ENTRIES", "1"); /* global ID is 1 integer */
208  Zoltan_Set_Param(ztn, "NUM_LID_ENTRIES", "1"); /* local ID is 1 integer */
209  Zoltan_Set_Param(ztn, "OBJ_WEIGHT_DIM", "1"); /* we use object weights */
210  Zoltan_Set_Param(ztn, "DEBUG_LEVEL", "0"); /* quiet mode; no output unless an error or warning is produced */
211  Zoltan_Set_Param(ztn, "KEEP_CUTS", "1"); /* save the cuts for later use */
212  Zoltan_Set_Param(ztn, "AUTO_MIGRATE", "1"); /* use the auto migration mechanism */
213 
214  Zoltan_Set_Fn(ztn, ZOLTAN_NUM_GEOM_FN_TYPE,
215  (void (*)()) ztnReturnDimension, cells);
216  Zoltan_Set_Fn(ztn, ZOLTAN_GEOM_FN_TYPE, (void (*)()) ztnReturnCoords,
217  cells);
218  Zoltan_Set_Fn(ztn, ZOLTAN_NUM_OBJ_FN_TYPE, (void (*)()) ztnReturnNumNode,
219  cells);
220  Zoltan_Set_Fn(ztn, ZOLTAN_OBJ_LIST_FN_TYPE,
221  (void (*)()) ztnReturnOwnedNodes, cells);
222  Zoltan_Set_Fn(ztn, ZOLTAN_OBJ_SIZE_FN_TYPE,
223  (void (*)()) ztnReturnParticleDataSize, cells);
224  Zoltan_Set_Fn(ztn, ZOLTAN_PACK_OBJ_FN_TYPE, (void (*)()) ztnPack, cells);
225  Zoltan_Set_Fn(ztn, ZOLTAN_UNPACK_OBJ_FN_TYPE, (void (*)()) ztnUnpack,
226  cells);
227  Zoltan_Set_Fn(ztn, ZOLTAN_PRE_MIGRATE_PP_FN_TYPE, (void (*)()) ztnPre,
228  cells);
229  Zoltan_Set_Fn(ztn, ZOLTAN_MID_MIGRATE_PP_FN_TYPE, (void (*)()) ztnMid,
230  cells);
231  Zoltan_Set_Fn(ztn, ZOLTAN_POST_MIGRATE_PP_FN_TYPE, (void (*)()) ztnPost,
232  cells);
233 
234 }
235 
241 {
242  int rc;
243  int i;
244 
245  if (nc < MPIsize)
246  return;
247 
248  rc = Zoltan_LB_Partition(ztn, /* input (all remaining fields are output) */
249  &changes, /* 1 if partitioning was changed, 0 otherwise */
250  &numGidEntries, /* number of integers used for a global ID */
251  &numLidEntries, /* number of integers used for a local ID */
252  &numImport, /* number of objects to be sent to me */
253  &importGlobalGids, /* global IDs of objects to be sent to me */
254  &importLocalGids, /* local IDs of objects to be sent to me */
255  &importProcs, /* process rank for source of each incoming object */
256  &importToPart, /* new partition for each incoming object */
257  &numExport, /* number of objects I must send to other processes */
258  &exportGlobalGids, /* global IDs of the objects I must send */
259  &exportLocalGids, /* local IDs of the objects I must send */
260  &exportProcs, /* process to which I send each of the objects */
261  &exportToPart); /* partition to which each object will belong */
262 
263  if (rc != ZOLTAN_OK)
264  stopRun(112, NULL, __FILE__, __LINE__);
265 
266  /* free the arrays allocated by Zoltan_LB_Partiotion */
267  Zoltan_LB_Free_Part(&importGlobalGids, &importLocalGids, &importProcs,
268  &importToPart);
269  Zoltan_LB_Free_Part(&exportGlobalGids, &exportLocalGids, &exportProcs,
270  &exportToPart);
271 }
272 
278 {
279  Zoltan_Destroy(&ztn);
280 }
int64_t * tlnc
Definition: global.h:111
int MPIrank
Definition: global.h:134
#define lnc
Definition: global.h:102
void ztnReturnCoords(void *data, int numGidEntries, int numLidEntries, ZOLTAN_ID_PTR globalId, ZOLTAN_ID_PTR localId, double *geomVec, int *ierr)
Definition: domdec.c:61
void decompositionFinalize()
Definition: domdec.c:277
ZOLTAN_ID_PTR exportGlobalGids
Definition: domdec.c:42
int MPIsize
Definition: global.h:135
ZOLTAN_ID_PTR importGlobalGids
Definition: domdec.c:37
double size
Definition: global.h:72
ZOLTAN_ID_PTR importLocalGids
Definition: domdec.c:38
struct Zoltan_Struct * ztn
Definition: global.h:146
ZOLTAN_ID_TYPE gid
Definition: global.h:68
contains the most important global variables, arrays and defines
int numGidEntries
Definition: domdec.c:34
void decompositionInit(int argc, char **argv, MPI_Comm Comm)
Definition: domdec.c:191
void decompositionExecute()
Definition: domdec.c:240
void ztnPack(void *data, int numGIdEntries, int numLIdEntries, ZOLTAN_ID_PTR globalId, ZOLTAN_ID_PTR localId, int dest, int size, char *buf, int *ierr)
Definition: domdec.c:115
int changes
Definition: domdec.c:33
int sdim
Definition: global.h:160
int ztnReturnParticleDataSize(void *data, int numGIdEntries, int numLIdEntries, ZOLTAN_ID_PTR globalId, ZOLTAN_ID_PTR localId, int *ierr)
Definition: domdec.c:105
int ztnReturnDimension(void *data, int *ierr)
Definition: domdec.c:50
double x
Definition: global.h:69
ZOLTAN_ID_PTR exportLocalGids
Definition: domdec.c:43
struct cellData * cells
Definition: global.h:82
void ztnUnpack(void *data, int numGIdEntries, ZOLTAN_ID_PTR globalId, int size, char *buf, int *ierr)
Definition: domdec.c:179
int * importProcs
Definition: domdec.c:39
void ztnReturnOwnedNodes(void *data, int numGIdEntries, int numLIdEntries, ZOLTAN_ID_PTR globalIds, ZOLTAN_ID_PTR localIds, int wgtDim, float *objWgts, int *ierr)
Definition: domdec.c:87
double z
Definition: global.h:71
int numLidEntries
Definition: domdec.c:35
int * importToPart
Definition: domdec.c:40
double y
Definition: global.h:70
int numImport
Definition: domdec.c:36
void ztnPre(void *data, int numGIdEntries, int numLIdEntries, int numImport, ZOLTAN_ID_PTR importGlobalIds, ZOLTAN_ID_PTR importLocalIds, int *importProcs, int *importToPart, int numExport, ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds, int *exportProcs, int *exportToPart, int *ierr)
Definition: domdec.c:127
#define nc
Definition: global.h:93
void ztnPost(void *data, int numGIdEntries, int numLIdEntries, int numImport, ZOLTAN_ID_PTR importGlobalIds, ZOLTAN_ID_PTR importLocalIds, int *importProcs, int *importToPart, int numExport, ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds, int *exportProcs, int *exportToPart, int *ierr)
Definition: domdec.c:163
int * exportProcs
Definition: domdec.c:44
int numExport
Definition: domdec.c:41
void stopRun(int ierr, char *name, char *file, int line)
Definition: utils.c:72
void ztnMid(void *data, int numGIdEntries, int numLIdEntries, int numImport, ZOLTAN_ID_PTR importGlobalIds, ZOLTAN_ID_PTR importLocalIds, int *importProcs, int *importToPart, int numExport, ZOLTAN_ID_PTR exportGlobalIds, ZOLTAN_ID_PTR exportLocalIds, int *exportProcs, int *exportToPart, int *ierr)
Definition: domdec.c:140
int ztnReturnNumNode(void *data, int *ierr)
Definition: domdec.c:79
int * exportToPart
Definition: domdec.c:45