Timothy  0.9
Tissue Modelling Framework
 All Data Structures Files Functions Variables Typedefs Macros
utils.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 #include <string.h>
26 #include <inttypes.h>
27 #include <mpi.h>
28 #include <unistd.h>
29 
30 #if defined(__bg__) && defined(__bgq__)
31 #include <spi/include/kernel/process.h>
32 #include <spi/include/kernel/location.h>
33 #endif
34 
35 #include "global.h"
36 //#include "comm.h"
37 
46 {
47  volatile uint32_t i = 0x01234567;
48  /* return 0 for big endian, 1 for little endian. */
49  endian = (*((uint8_t *) (&i))) == 0x67;
50  return 0;
51 }
52 
57 void swap_Nbyte(char *data, int n, int m)
58 {
59  int i, j;
60  char old_data[16];
61 
62  for (j = 0; j < n; j++) {
63  memcpy(&old_data[0], &data[j * m], m);
64  for (i = 0; i < m; i++)
65  data[j * m + i] = old_data[m - i - 1];
66  }
67 }
68 
72 void stopRun(int ierr, char *name, char *file, int line)
73 {
74  switch (ierr) {
75  case 100:
76  fprintf(stderr, "Bad %s dimensions at %s, line %d\n", name, file,
77  line);
78  break;
79  case 101:
80  if (MPIrank == 0)
81  fprintf(stderr,
82  "Number of processes must be a power of two at %s, line %d\n",
83  file, line);
84  break;
85  case 102:
86  if (MPIrank == 0) {
87  fprintf(stderr,
88  "Bad or missing Program parameters, at %s, line %d\n", file,
89  line);
90  fprintf(stderr, "Usage:\n");
91  fprintf(stderr, "mpiexec -n NPROC ./timothy -p <ParameterFile>\n");
92  }
93  break;
94  case 103:
95  fprintf(stderr, "Failed %s MPI message at %s, line %d.\n", name, file,
96  line);
97  break;
98  case 106:
99  fprintf(stderr,
100  "Failed to allocate memory for %s array at %s, line %d\n",
101  name, file, line);
102  break;
103  case 107:
104  fprintf(stderr, "Too many exported particles. Adjust parameters.\n");
105  break;
106  case 108:
107  fprintf(stderr,
108  "Size of float does not divide the statistics table size.\n");
109  break;
110  case 109:
111  fprintf(stderr, "Max. number of cells per process exceeded.\n");
112  break;
113  case 110:
114  fprintf(stderr, "Too many exported cells on process %d. Abort.\n",
115  MPIrank);
116  break;
117  case 111:
118  fprintf(stderr,
119  "Error while reading simulation parameters. MAXMOVE out of range.\n");
120  break;
121  case 112:
122  fprintf(stderr, "Error in Zoltan library at %s, line %d.\n", file,
123  line);
124  break;
125  case 113:
126  fprintf(stderr,
127  "Error while opening povray output file at %s, line %d.\n",
128  file, line);
129  break;
130  case 114:
131  fprintf(stderr, "Field parameter %s missing at %s, line %d.\n", name,
132  file, line);
133  break;
134  case 115:
135  fprintf(stderr,
136  "Number of cells in the restart file is larger than MAXCELLS parameter at %s, line %d.\n",
137  file, line);
138  break;
139  case 116:
140  fprintf(stderr, "Bad value for parameter %s at %s, line %d.\n", name,
141  file, line);
142  break;
143  case 666:
144  fprintf(stderr, "Some devilish error at %s, line %d (lcf)\n", file,
145  line);
146  break;
147  case 898:
148  fprintf(stderr, "Error. tree_create_child_node()\n");
149  break;
150  case 999:
151  break;
152  default:
153  if (MPIrank == 0)
154  fprintf(stderr, "Error at %s, line %d\n", file, line);
155  break;
156  }
157  fflush(stderr);
158  MPI_Finalize();
159  exit(1);
160 }
161 
167 size_t getMemoryPerProcess(int32_t lsize)
168 {
169 #if defined(__bg__) && defined(__bgq__)
170  Personality_t pers;
171  int32_t msize;
172  Kernel_GetPersonality(&pers, sizeof(pers));
173  msize = pers.DDR_Config.DDRSizeMB;
174 #else
175 #if defined(_AIX)
176  int msize = sysconf(_SC_AIX_REALMEM) / 1024;
177 #endif /* aix */
178 #if defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
179  long psize = sysconf(_SC_PAGE_SIZE);
180  long npages = sysconf(_SC_PHYS_PAGES);
181  long msize = psize * npages / (1024 * 1024);
182 #endif /*linux */
183 #endif /* bgq */
184  return msize / lsize;
185 }
186 
193 void getLocalRankAndSize(int rank, int size, int32_t * lrank,
194  int32_t * lsize)
195 {
196  int i;
197  int32_t r, s;
198 #if defined(__bg__) && defined(__bgq__)
199  s = Kernel_ProcessCount();
200  r = Kernel_MyTcoord();
201 #else
202  int pnamelen;
203  char pname[MPI_MAX_PROCESSOR_NAME];
204  char pnametable[size][MPI_MAX_PROCESSOR_NAME];
205  MPI_Get_processor_name(pname, &pnamelen);
206  MPI_Allgather(pname, MPI_MAX_PROCESSOR_NAME, MPI_CHAR, pnametable,
207  MPI_MAX_PROCESSOR_NAME, MPI_CHAR, MPI_COMM_WORLD);
208  r = 0;
209  s = 0;
210  for (i = 0; i < size; i++)
211  if (!strcmp(pnametable[rank], pnametable[i])) {
212  if (i < rank)
213  r++;
214  s++;
215  }
216 #endif
217  *lrank = r;
218  *lsize = s;
219 }
void getLocalRankAndSize(int rank, int size, int32_t *lrank, int32_t *lsize)
Definition: utils.c:193
int MPIrank
Definition: global.h:134
float s
Definition: global.h:186
size_t getMemoryPerProcess(int32_t lsize)
Definition: utils.c:167
contains the most important global variables, arrays and defines
float m
Definition: global.h:188
int checkEndiannes()
Definition: utils.c:45
int endian
Definition: global.h:157
void swap_Nbyte(char *data, int n, int m)
Definition: utils.c:57
void stopRun(int ierr, char *name, char *file, int line)
Definition: utils.c:72