| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2007, 2008, 2009, 2022 INCF |
| 4 |
* |
| 5 |
* MUSIC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* MUSIC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "music/music-config.hh" |
| 20 |
|
| 21 |
#if MUSIC_USE_MPI |
| 22 |
#include <mpi.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include "music/mpi_utils.hh" |
| 26 |
#include "music/error.hh" |
| 27 |
|
| 28 |
#include <iostream> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
namespace MUSIC { |
| 32 |
|
| 33 |
void |
| 34 |
error () |
| 35 |
{ |
| 36 |
#if MUSIC_USE_MPI |
| 37 |
MPI_Abort (MPI_COMM_WORLD, 1); |
| 38 |
#else |
| 39 |
abort(); |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
void |
| 45 |
hang () |
| 46 |
{ |
| 47 |
while (true) |
| 48 |
; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
void |
| 53 |
error (std::string msg) |
| 54 |
{ |
| 55 |
std::cerr << "Error in MUSIC library: " << msg << std::endl; |
| 56 |
error (); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
void |
| 61 |
error (std::ostringstream& ostr) |
| 62 |
{ |
| 63 |
error (ostr.str ()); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
static int |
| 68 |
getRank () |
| 69 |
{ |
| 70 |
#if MUSIC_USE_MPI |
| 71 |
int isInitialized; |
| 72 |
MPI_Initialized (&isInitialized); |
| 73 |
if (isInitialized) |
| 74 |
return mpi_get_rank (MPI_COMM_WORLD); |
| 75 |
#endif |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
void |
| 81 |
error0 (std::string msg) |
| 82 |
{ |
| 83 |
if (getRank () <= 0) |
| 84 |
error (msg); |
| 85 |
else |
| 86 |
// Give process #0 a chance to report the error |
| 87 |
hang (); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
void |
| 92 |
errorRank (std::string msg) |
| 93 |
{ |
| 94 |
std::ostringstream text; |
| 95 |
int rank = getRank (); |
| 96 |
if (rank >= 0) |
| 97 |
text << "rank #" << rank << ": "; |
| 98 |
text << msg; |
| 99 |
error (text.str ()); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
void |
| 104 |
checkOnce (bool& flag, std::string msg) |
| 105 |
{ |
| 106 |
if (flag) |
| 107 |
errorRank (msg); |
| 108 |
flag = true; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void |
| 113 |
checkInstantiatedOnce (bool& isInstantiated, std::string className) |
| 114 |
{ |
| 115 |
std::ostringstream msg; |
| 116 |
msg << className << " constructor was called a second time.\n" |
| 117 |
"Only one " << className << " object can exist at any instance of time."; |
| 118 |
checkOnce (isInstantiated, msg.str ()); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
void |
| 123 |
checkCalledOnce (bool& isCalled, std::string funcName, std::string suffix) |
| 124 |
{ |
| 125 |
std::ostringstream msg; |
| 126 |
msg << funcName << " called twice" << suffix; |
| 127 |
checkOnce (isCalled, msg.str ()); |
| 128 |
} |
| 129 |
|
| 130 |
} |