Global.cc 9.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
16
#include <stdarg.h>
#include <stdio.h>
#include <sstream>

17
#include "Global.h"
18
#include "Initfile.h"
19
20
21
22
23
24
25
26
#include "Element.h"
#include "Line.h"
#include "Triangle.h"
#include "Tetrahedron.h"

namespace AMDiS {

  const char *funcName = NULL;
27
28

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
29
  bool Msg::outputMainRank = true;
30
31
#endif

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
32
  ThreadPrivate<const char *> Msg::oldFuncName(NULL);
33
34
  std::ostream* Msg::out = NULL;
  std::ostream* Msg::error = NULL;
35
  int Global::dimOfWorld = 0;
36
  std::vector<std::vector<int> > Global::geoIndexTable;
37
38
39
40
  int Msg::msgInfo = 10;
  bool Msg::msgWait = true;

  Element *Global::referenceElement[4] = 
41
    { NULL, 
Thomas Witkowski's avatar
Thomas Witkowski committed
42
43
44
      new Line(NULL), 
      new Triangle(NULL), 
      new Tetrahedron(NULL) 
Thomas Witkowski's avatar
Thomas Witkowski committed
45
    };
46

47

48
49
50
51
52
  void Msg::wait(bool w)
  {
    FUNCNAME("Msg::wait()");

    if (w) {
53
      std::string line;
54
      MSG("wait for <enter> ...");
55
56
      std::cin >> line;
//       char* result = fgets(line, 9, stdin);
57
58
59
    }
  }

60

61
  void Msg::change_out(std::ostream  *fp)
62
63
64
65
  {
    FUNCNAME("Msg::change_out()");
  
    if (fp) {
66
67
      if (out && *out != std::cout && *out != std::cerr) {
	dynamic_cast< std::ofstream*>(out)->close();
68
69
70
71
72
73
74
	delete out;
      }

      out = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
75
    } 
76
77
  }

78

79
  void Msg::change_error_out(std::ofstream *fp)
80
  {
81
82
83
    FUNCNAME("Msg::change_error_out()");

    if (fp) {
84
      if (error && *error != std::cout && *error != std::cerr) {
85
	dynamic_cast< std::ofstream*>(error)->close();
86
	delete error;
87
      }
88
89
90
91
92
93
      
      error = fp;
    } else {
      ERROR("file pointer is pointer to nil;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
94
95
  }

96

97
98
  void Msg::open_error_file(const char *filename, OPENMODE type)
  {
99
    FUNCNAME("Msg::open_error_file()");
100
    std::ofstream *fp;
101

102
103
    if (filename && (fp = new std::ofstream(filename, type))) {
      if (error && *error != std::cout && *error != std::cerr) {
104
	dynamic_cast< std::ofstream*>(error)->close();
105
	delete error;
106
      }
107
108
109
110
111
112
113
114
115

      error = fp;
    } else {
      if (filename)
	ERROR("can not open %s;\n", filename);
      else
	ERROR("no filename specified;\n");
      ERROR("use previous stream for errors furthermore\n");
    }
116
117
  }

118

119
120
  void Msg::print_funcname(const char *funcName)
  {
121
122
123
124
125
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (outputMainRank && MPI::COMM_WORLD.Get_rank() != 0)
      return;
#endif

126
    if (!out) 
127
      out = &std::cout;
128

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
129
    if (funcName &&  oldFuncName.get() != funcName) {
130
      PRINT_LINE((*out), funcName << ":" << std::endl);
131
    } else if (!funcName) {
132
      PRINT_LINE((*out), "*unknown function*" << std::endl);
133
    }
134
135
    PRINT_LINE((*out), "               ");

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
136
    oldFuncName.set(funcName);
137
138
  }

139

140
  void Msg::print_error_funcname(const char *funcName, const char *file, int line)
141
142
143
  {
    static int old_line = -1;

144
    if (!error) 
145
      error = &std::cerr;
146

147
148
    std::stringstream oss;

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
149
    if (funcName && oldFuncName.get() != funcName) {
150
      oss << funcName << ": ";
151
    } else if (!funcName) {
152
      if (line-old_line > 5) 
153
	oss << "*unknown function*";
154
    }
155

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
156
    if (oldFuncName.get() != funcName) {
157
      oss << "ERROR in " << file << ", line " << line << std::endl;;
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
158
      oldFuncName.set(funcName);
159
    } else if (line - old_line > 5)
160
      oss << "ERROR in " << file << ", line " << line << "\n" << std::endl;
161

162
    PRINT_LINE((*error), oss.str());
163
164
165
    old_line = line;
  }

166

167
168
  void Msg::print_error_exit(const char *format, ...)
  {
169
170
    va_list arg;
    char buff[255];
171

172
    if (!error) 
173
      error = &std::cerr;
174
175
176

    va_start(arg, format);
    vsprintf(buff, format, arg);
177
    PRINT_LINE((*error), buff);
178
179
    va_end(arg);

180
    throw std::runtime_error(buff);
181
182
183
    exit(1);
  }

184

185
186
  void Msg::print_error(const char *format, ...)
  {
187
188
    va_list arg;
    char buff[255];
189
190


191
    if (!error) 
192
      error = &std::cerr;
193
194
195

    va_start(arg, format);
    vsprintf(buff, format, arg);
196
    PRINT_LINE((*error), buff);
197
198
199
    va_end(arg);
  }

200

201
202
203
204
  void Msg::print_warn_funcname(const char *funcName,
				const char *file, 
				int line)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
205
206
207
208
209
210
211
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#if (DEBUG == 0)
    if (outputMainRank && MPI::COMM_WORLD.Get_rank() != 0)
      return;
#endif
#endif

212
    static int old_line = -1;
213

214
    if (!out) 
215
      out = &std::cout;
216

217
218
    std::stringstream oss;

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
219
    if (funcName  &&  oldFuncName.get() != funcName) {
220
221
222
      oss << funcName << ": ";
    } else if (!funcName) {
      oss << "*unknown function*";
223
    }
224

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
225
    if (oldFuncName.get() != funcName) {
226
      oss << "WARNING in " << file << ", line " << line << std::endl;
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
227
      oldFuncName.set(funcName);
228
229
230
231
    } else if (line - old_line > 5) {
      oss << "WARNING in " << file << ", line " << line << std::endl;
    }

232
233
234
    if (oss.str() != "") 
      PRINT_LINE((*out), oss.str());
    
235
236
237
    old_line = line;
  }

238

239
240
  void Msg::print_warn(const char *format, ...)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
241
242
243
244
245
246
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#if (DEBUG == 0)
    if (outputMainRank && MPI::COMM_WORLD.Get_rank() != 0)
      return;
#endif
#endif
247
248
    va_list arg;
    char buff[255];
249

250
    if (!out) 
251
      out = &std::cout;
252
253
254

    va_start(arg, format);
    vsprintf(buff, format, arg);
255
    PRINT_LINE((*out), buff);
256
257
258
259
260
261
    va_end(arg);
  }


  void Msg::print(const char *format, ...)
  {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
262
#ifndef SUPPRESS_OUTPUT
263
264
265
266
267
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    if (outputMainRank && MPI::COMM_WORLD.Get_rank() != 0)
      return;
#endif

268
269
    va_list arg;
    char buff[255];
270
271

    if (!out) 
272
      out = &std::cout;
273
274
275

    va_start(arg, format);
    vsprintf(buff, format, arg);
276
    (*out) << buff;
277
    va_end(arg);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
278
#endif
279
280
  }

281

282
283
284
285
286
287
  void Global::init()
  {
    int d = -1;

    // get dimension
    TEST_EXIT(Parameters::initialized())("Parameters not initialized!\n");
288
    Parameters::get("dimension of world",d,0);
289
    TEST_EXIT(d > 0)("Cannot initialize dimension!\n");
290
    TEST_EXIT((d == 1) || (d == 2) || (d == 3))("Invalid world dimension %d!\n",d);
291
292
293
294

    // set dimension
    dimOfWorld = d;

295
296
297
298
299
    // prepare geoIndex-Table
    int geoTableSize = abs(static_cast<int>(MINPART)) + MAXPART + 1;
    geoIndexTable.resize(4);
    for (int i = 0; i < 4; i++) {
      geoIndexTable[i].resize(geoTableSize);
300
301
      for (int j = 0; j < geoTableSize; j++)
	geoIndexTable[i][j] = 0;      
302
303
    }

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    geoIndexTable[0][PARTS - MINPART] = 1;
    geoIndexTable[0][VERTEX - MINPART] = 1;
    geoIndexTable[0][EDGE - MINPART] = 0;
    geoIndexTable[0][FACE - MINPART] = 0;
    geoIndexTable[0][WORLD - MINPART] = dimOfWorld;

    for (int i = 1; i < 4; i++) {
      geoIndexTable[i][CENTER - MINPART] = referenceElement[i]->getGeo(CENTER);
      geoIndexTable[i][VERTEX - MINPART] = referenceElement[i]->getGeo(VERTEX);
      geoIndexTable[i][EDGE - MINPART] = referenceElement[i]->getGeo(EDGE);
      geoIndexTable[i][FACE - MINPART] = referenceElement[i]->getGeo(FACE);
      geoIndexTable[i][DIMEN - MINPART] = referenceElement[i]->getGeo(DIMEN);
      geoIndexTable[i][PARTS - MINPART] = referenceElement[i]->getGeo(PARTS);
      geoIndexTable[i][NEIGH - MINPART] = referenceElement[i]->getGeo(NEIGH);
      geoIndexTable[i][WORLD - MINPART] = dimOfWorld;
      geoIndexTable[i][BOUNDARY - MINPART] = referenceElement[i]->getGeo(BOUNDARY);
      geoIndexTable[i][PROJECTION - MINPART] = referenceElement[i]->getGeo(PROJECTION);
    }
322

323
    // set msgWait
324
    Msg::setMsgWait(!(Parameters::getMsgWait() == 0));
325
326
  }

327

328
329
  void Global::clear()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
332
    delete referenceElement[1];
    delete referenceElement[2];
    delete referenceElement[3];
333
  }
334

335

336
337
  int fac(int i)
  {
338
339
340
341
    if (i <= 1) 
      return 1;
    else 
      return i * fac(i - 1);
342
343
  }

344

345
346
347
348
349
350
  void waitSec(int seconds)
  {
    clock_t endwait = clock () + seconds * CLOCKS_PER_SEC;
    while (clock() < endwait) {}
  }

351

352
  void processMemUsage(double& vm_usage, double& resident_set, bool inMegaByte)
353
  {
354
#ifndef _WIN32
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
    using std::ios_base;
    using std::ifstream;
    using std::string;
    
    vm_usage     = 0.0;
    resident_set = 0.0;
    
    // 'file' stat seems to give the most reliable results    
    ifstream stat_stream("/proc/self/stat",ios_base::in);
    
    // dummy vars for leading entries in stat that we don't care about
    string pid, comm, state, ppid, pgrp, session, tty_nr;
    string tpgid, flags, minflt, cminflt, majflt, cmajflt;
    string utime, stime, cutime, cstime, priority, nice;
    string O, itrealvalue, starttime;
    
    // the two fields we want    
    unsigned long vsize;
    long rss;
    
    stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
		>> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
		>> utime >> stime >> cutime >> cstime >> priority >> nice
		>> O >> itrealvalue >> starttime >> vsize >> rss;

    // in case x86-64 is configured to use 2MB pages    
    long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024;
382
    vm_usage = vsize / 1024.0;
383
    resident_set = rss * page_size_kb;
384
385
386
387
388

    if (inMegaByte) {
      vm_usage /= 1024.0;
      resident_set /= 1024.0;
    }
389
390
391
#else
    ERROR("Function not available under MS Windows\n");
#endif
392
393
394
  }


395
}