Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_ErrorReporter.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_EXPERIMENTAL_ERROR_REPORTER_HPP
45 #define KOKKOS_EXPERIMENTAL_ERROR_REPORTER_HPP
46 
47 #include <vector>
48 #include <Kokkos_Core.hpp>
49 #include <Kokkos_View.hpp>
50 #include <Kokkos_DualView.hpp>
51 
52 namespace Kokkos {
53 namespace Experimental {
54 
55 template <typename ReportType, typename DeviceType>
56 class ErrorReporter
57 {
58 public:
59 
60  typedef ReportType report_type;
61  typedef DeviceType device_type;
62  typedef typename device_type::execution_space execution_space;
63 
64  ErrorReporter(int max_results)
65  : m_numReportsAttempted(""),
66  m_reports("", max_results),
67  m_reporters("", max_results)
68  {
69  clear();
70  }
71 
72  int getCapacity() const { return m_reports.h_view.extent(0); }
73 
74  int getNumReports();
75 
76  int getNumReportAttempts();
77 
78  void getReports(std::vector<int> &reporters_out, std::vector<report_type> &reports_out);
79  void getReports( typename Kokkos::View<int*, typename DeviceType::execution_space >::HostMirror &reporters_out,
81 
82  void clear();
83 
84  void resize(const size_t new_size);
85 
86  bool full() {return (getNumReportAttempts() >= getCapacity()); }
87 
88  KOKKOS_INLINE_FUNCTION
89  bool add_report(int reporter_id, report_type report) const
90  {
91  int idx = Kokkos::atomic_fetch_add(&m_numReportsAttempted(), 1);
92 
93  if (idx >= 0 && (idx < static_cast<int>(m_reports.d_view.extent(0)))) {
94  m_reporters.d_view(idx) = reporter_id;
95  m_reports.d_view(idx) = report;
96  return true;
97  }
98  else {
99  return false;
100  }
101  }
102 
103 private:
104 
105  typedef Kokkos::View<report_type *, execution_space> reports_view_t;
106  typedef Kokkos::DualView<report_type *, execution_space> reports_dualview_t;
107 
108  typedef typename reports_dualview_t::host_mirror_space host_mirror_space;
109  Kokkos::View<int, execution_space> m_numReportsAttempted;
110  reports_dualview_t m_reports;
111  Kokkos::DualView<int *, execution_space> m_reporters;
112 
113 };
114 
115 
116 template <typename ReportType, typename DeviceType>
117 inline int ErrorReporter<ReportType, DeviceType>::getNumReports()
118 {
119  int num_reports = 0;
120  Kokkos::deep_copy(num_reports,m_numReportsAttempted);
121  if (num_reports > static_cast<int>(m_reports.h_view.extent(0))) {
122  num_reports = m_reports.h_view.extent(0);
123  }
124  return num_reports;
125 }
126 
127 template <typename ReportType, typename DeviceType>
128 inline int ErrorReporter<ReportType, DeviceType>::getNumReportAttempts()
129 {
130  int num_reports = 0;
131  Kokkos::deep_copy(num_reports,m_numReportsAttempted);
132  return num_reports;
133 }
134 
135 template <typename ReportType, typename DeviceType>
136 void ErrorReporter<ReportType, DeviceType>::getReports(std::vector<int> &reporters_out, std::vector<report_type> &reports_out)
137 {
138  int num_reports = getNumReports();
139  reporters_out.clear();
140  reporters_out.reserve(num_reports);
141  reports_out.clear();
142  reports_out.reserve(num_reports);
143 
144  if (num_reports > 0) {
145  m_reports.template sync<host_mirror_space>();
146  m_reporters.template sync<host_mirror_space>();
147 
148  for (int i = 0; i < num_reports; ++i) {
149  reporters_out.push_back(m_reporters.h_view(i));
150  reports_out.push_back(m_reports.h_view(i));
151  }
152  }
153 }
154 
155 template <typename ReportType, typename DeviceType>
156 void ErrorReporter<ReportType, DeviceType>::getReports(
159 {
160  int num_reports = getNumReports();
161  reporters_out = typename Kokkos::View<int*, typename DeviceType::execution_space >::HostMirror("ErrorReport::reporters_out",num_reports);
162  reports_out = typename Kokkos::View<report_type*, typename DeviceType::execution_space >::HostMirror("ErrorReport::reports_out",num_reports);
163 
164  if (num_reports > 0) {
165  m_reports.template sync<host_mirror_space>();
166  m_reporters.template sync<host_mirror_space>();
167 
168  for (int i = 0; i < num_reports; ++i) {
169  reporters_out(i) = m_reporters.h_view(i);
170  reports_out(i) = m_reports.h_view(i);
171  }
172  }
173 }
174 
175 template <typename ReportType, typename DeviceType>
176 void ErrorReporter<ReportType, DeviceType>::clear()
177 {
178  int num_reports=0;
179  Kokkos::deep_copy(m_numReportsAttempted, num_reports);
180  m_reports.template modify<execution_space>();
181  m_reporters.template modify<execution_space>();
182 }
183 
184 template <typename ReportType, typename DeviceType>
185 void ErrorReporter<ReportType, DeviceType>::resize(const size_t new_size)
186 {
187  m_reports.resize(new_size);
188  m_reporters.resize(new_size);
189  Kokkos::fence();
190 }
191 
192 
193 } // namespace Experimental
194 } // namespace kokkos
195 
196 #endif
197 
Declaration and definition of Kokkos::DualView.
std::enable_if< std::is_same< typename Kokkos::View< T, P...>::array_layout, Kokkos::LayoutLeft >::value||std::is_same< typename Kokkos::View< T, P...>::array_layout, Kokkos::LayoutRight >::value >::type resize(Kokkos::View< T, P...> &v, const size_t n0=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n1=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n2=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n3=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n4=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n5=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n6=KOKKOS_IMPL_CTOR_DEFAULT_ARG, const size_t n7=KOKKOS_IMPL_CTOR_DEFAULT_ARG)
Resize a view with copying old data to new data at the corresponding indices.
void deep_copy(const View< DT, DP...> &dst, typename ViewTraits< DT, DP...>::const_value_type &value, typename std::enable_if< std::is_same< typename ViewTraits< DT, DP...>::specialize, void >::value >::type *=0)
Deep copy a value from Host memory into a view.
View to an array of data.
void resize(DynRankView< T, P...> &v, const size_t n0=KOKKOS_INVALID_INDEX, const size_t n1=KOKKOS_INVALID_INDEX, const size_t n2=KOKKOS_INVALID_INDEX, const size_t n3=KOKKOS_INVALID_INDEX, const size_t n4=KOKKOS_INVALID_INDEX, const size_t n5=KOKKOS_INVALID_INDEX, const size_t n6=KOKKOS_INVALID_INDEX, const size_t n7=KOKKOS_INVALID_INDEX)
Resize a view with copying old data to new data at the corresponding indices.
View< typename traits::non_const_data_type, typename traits::array_layout, typename traits::host_mirror_space > HostMirror
Compatible HostMirror view.