File: environment.cc

package info (click to toggle)
rheolef 7.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 88,200 kB
  • sloc: cpp: 110,259; sh: 16,733; makefile: 5,406; python: 1,391; yacc: 218; javascript: 203; xml: 191; awk: 61; sed: 5
file content (143 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
/// 
/// =========================================================================
// boost::mpi::environment-like interface, with thread options for scotch lib

#include "rheolef/environment.h"
#ifndef _RHEOLEF_HAVE_MPI

namespace rheolef {

environment::environment (int& argc, char**& argv, const environment_option_type& opt)
 : _oldcw(0)
{
}
environment::~environment()
{
}

} // namespace rheolef

#else // _RHEOLEF_HAVE_MPI

// support MPI-2 (with warnings, but changed to error by the compiler flags)
#ifdef MPI_VERSION
# if (MPI_VERSION >= 2)
#  ifndef MPI_Errhandler_set
#   define MPI_Errhandler_set	MPI_Comm_set_errhandler
#  endif
#  ifndef MPI_Attr_get
#   define MPI_Attr_get		MPI_Comm_get_attr
#  endif
# endif
#endif

namespace rheolef {

bool environment::initialized() {
  int flag;
  MPI_Initialized(&flag);
  return flag != 0;
}
bool environment::finalized() {
  int flag;
  MPI_Finalized(&flag);
  return flag != 0;
}
environment::environment (int& argc, char**& argv, const environment_option_type& opt)
 : _rheolef_has_init(false), _oldcw(0)
{
  if (!initialized()) {
    int status = 0;
    if (opt.thread_level == environment_option_type::no_thread) {
      status = MPI_Init (&argc, &argv);
    } else {
      int obtained_thread_level;
      status = MPI_Init_thread (&argc, &argv, opt.thread_level, &obtained_thread_level);
      if (obtained_thread_level != opt.thread_level) {
        warning_macro ("mpi_init: obtained thread level="<<obtained_thread_level <<" while asking for thread level="<<opt.thread_level);
      }
    }
    check_macro (status == MPI_SUCCESS, "mpi init failed");
  }
  if (!_rheolef_has_init) {
#ifdef _RHEOLEF_HAVE_OBSOLETE_MPI_V1
    MPI_Errhandler_set      (MPI_COMM_WORLD, MPI_ERRORS_RETURN);
#else
    MPI_Comm_set_errhandler (MPI_COMM_WORLD, MPI_ERRORS_RETURN);
#endif
    _rheolef_has_init = true;
  }
}
environment::~environment()
{
  if (_rheolef_has_init && !finalized()) {
    boost::mpi::detail::mpi_datatype_cache().clear();
    MPI_Finalize();
  }
}
void environment::abort(int errcode)
{
  MPI_Abort(MPI_COMM_WORLD, errcode);
}
int environment::max_tag()
{
  int* max_tag_value;
  int found = 0;
  MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &max_tag_value, &found);
  assert_macro (found != 0, "MPI_Attr_get: error");
  return *max_tag_value - _num_reserved_tags;
}
int environment::collectives_tag()
{
  return max_tag() + 1;
}
boost::optional<int> environment::host_rank()
{
  int* host;
  int found = 0;
  MPI_Attr_get(MPI_COMM_WORLD, MPI_HOST, &host, &found);
  if (!found || *host == MPI_PROC_NULL) {
    return boost::optional<int>();
  } else {
    return *host;
  }
}
boost::optional<int> environment::io_rank()
{
  int* io;
  int found = 0;
  MPI_Attr_get(MPI_COMM_WORLD, MPI_IO, &io, &found);
  if (!found || *io == MPI_PROC_NULL) {
    return boost::optional<int>();
  } else {
    return *io;
  }
}
std::string environment::processor_name()
{
  char name[MPI_MAX_PROCESSOR_NAME];
  int len;
  MPI_Get_processor_name(name, &len);
  return std::string(name, len);
}

} // namespace rheolef
#endif // _RHEOLEF_HAVE_MPI