dgMessage.cpp 3.29 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
#include "dgMessage.h"
Jonathan Lambrechts's avatar
petsc    
Jonathan Lambrechts committed
2
#include "dgConfig.h"
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
3
#include <cstdio>
4
#include <string>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
5
6
#include <cstdlib>
#include <cstdarg>
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
7
#include <string>
Jonathan Lambrechts's avatar
petsc    
Jonathan Lambrechts committed
8
9
10
#if defined(HAVE_PETSC)
#include "petsc.h"
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
11
#include "mpi.h"
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
12
13
14
15
int Msg::_commRank = 0;
int Msg::_commSize = 1;

void Msg::Barrier(){
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
16
17
18
#ifdef HAVE_MPI
  MPI_Barrier(MPI_COMM_WORLD);
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
19
20
}

Jonathan Lambrechts's avatar
petsc    
Jonathan Lambrechts committed
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
void Msg::Exit(int i)
{
  if(i){
#if defined(HAVE_SLEPC)
    SlepcFinalize();
#endif
#if defined(HAVE_PETSC)
    PetscFinalize();
#endif
#if defined(HAVE_MPI)
    // force general abort (wven if the fatal error occurred on 1 cpu only)
    MPI_Abort(MPI_COMM_WORLD, i);
    int finalized;
    MPI_Finalized(&finalized);
    if (!finalized)
      MPI_Finalize();
#endif
    exit(i);
  }
#if defined(HAVE_SLEPC)
  SlepcFinalize();
#endif
#if defined(HAVE_PETSC)
  PetscFinalize();
#endif
#if defined(HAVE_MPI)
  int finalized; //Some PETSc versions call MPI_FINALIZE
  MPI_Finalized(&finalized);
  if (!finalized)
    MPI_Finalize();
#endif
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
  exit(i);
}

void Msg::Info(const char *fmt, ...)
{
  char str[5000];
  va_list args;
  va_start(args, fmt);
  vsnprintf(str, sizeof(str), fmt, args);
  va_end(args);
  if(_commSize > 1)
    fprintf(stdout, "Info    : [rank %3d] %s\n", _commRank, str);
  else
    fprintf(stdout, "Info    : %s\n", str);
  fflush(stdout);
}

void Msg::Error(const char *fmt, ...)
{
  char str[5000];
  va_list args;
  va_start(args, fmt);
  vsnprintf(str, sizeof(str), fmt, args);
  va_end(args);
  if(_commSize > 1)
    fprintf(stdout, "Error    : [rank %3d] %s\n", _commRank, str);
  else
    fprintf(stdout, "Error    : %s\n", str);
  fflush(stdout);
}

void Msg::Warning(const char *fmt, ...)
{
  char str[5000];
  va_list args;
  va_start(args, fmt);
  vsnprintf(str, sizeof(str), fmt, args);
  va_end(args);
  if(_commSize > 1)
    fprintf(stdout, "Warning    : [rank %3d] %s\n", _commRank, str);
  else
    fprintf(stdout, "Warning    : %s\n", str);
  fflush(stdout);
}

void Msg::Fatal(const char *fmt, ...)
{
  char str[5000];
  va_list args;
  va_start(args, fmt);
  vsnprintf(str, sizeof(str), fmt, args);
  va_end(args);
  if(_commSize > 1)
    fprintf(stdout, "Fatal    : [rank %3d] %s\n", _commRank, str);
  else
    fprintf(stdout, "Fatal    : %s\n", str);
  fflush(stdout);
  exit(1);
}

void Msg::Debug(const char *fmt, ...)
{
  char str[5000];
  va_list args;
  va_start(args, fmt);
  vsnprintf(str, sizeof(str), fmt, args);
  va_end(args);
  if(_commSize > 1)
    fprintf(stdout, "Debug    : [rank %3d] %s\n", _commRank, str);
  else
    fprintf(stdout, "Debug    : %s\n", str);
  fflush(stdout);
}
Jonathan Lambrechts's avatar
petsc    
Jonathan Lambrechts committed
125
126
127
128
129
130
131
132
133

void Msg::Init(int argc, char **argv)
{
#if defined(HAVE_MPI)
  int flag;
  MPI_Initialized(&flag);
  if(!flag) MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &_commRank);
  MPI_Comm_size(MPI_COMM_WORLD, &_commSize);
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
134
  //MPI_Errhandler_set(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
Jonathan Lambrechts's avatar
petsc    
Jonathan Lambrechts committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#endif
#if defined(HAVE_PETSC)
  int sargc = 0;
  char **sargv = new char*[argc + 1];
  // prune argv from gmsh-specific options that make PETSc verbose
  for(int i = 0; i < argc; i++){
    std::string val(argv[i]);
    if(val != "-info" && val != "-help" && val != "-v")
      sargv[sargc++] = argv[i];
  }
  PetscInitialize(&sargc, &sargv, PETSC_NULL, PETSC_NULL);
  PetscPopSignalHandler();
#if defined(HAVE_SLEPC)
  SlepcInitialize(&sargc, &sargv, PETSC_NULL, PETSC_NULL);
#endif
  delete [] sargv;
#endif
}