MPQC  3.0.0-alpha
thread_wrap.h
1 //
2 // thread_wrap.h
3 //
4 // Copyright (C) 2014 David Hollman
5 //
6 // Author: David Hollman
7 // Maintainer: DSH
8 // Created: Feb 24, 2014
9 //
10 // This file is part of the SC Toolkit.
11 //
12 // The SC Toolkit is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Library General Public License as published by
14 // the Free Software Foundation; either version 2, or (at your option)
15 // any later version.
16 //
17 // The SC Toolkit is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Library General Public License for more details.
21 //
22 // You should have received a copy of the GNU Library General Public License
23 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
24 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 //
26 // The U.S. Government is granted a limited license as per AL 91-7.
27 //
28 
29 #ifndef _util_container_thread_wrap_h
30 #define _util_container_thread_wrap_h
31 
32 #include <vector>
33 #include <algorithm>
34 #include <iterator>
35 
36 namespace sc {
37 
38 template <typename STLContainer>
39 STLContainer merge(const std::vector<STLContainer>& containers) {
40  STLContainer rv;
41  for(auto&& c : containers) {
42  std::copy(c.begin(), c.end(), std::back_inserter(rv));
43  }
44  return rv;
45 }
46 
47 template <typename STLContainer>
48 STLContainer sum_merge(const std::vector<STLContainer>& containers) {
49  STLContainer rv = containers[0];
50  bool first = true;
51  for(auto&& c : containers) {
52  if(first) {
53  first = false;
54  continue;
55  }
56  size_t offset = 0;
57  for(auto&& item : c) {
58  MPQC_ASSERT(offset <= c.size());
59  rv[offset] += item;
60  ++offset;
61  }
62  }
63  return rv;
64 }
65 
66 
67 namespace detail {
68 
69  template <typename Container>
70  struct _Merger {
71  Container operator()(const std::vector<Container>& containers) const {
72  return merge(containers);
73  }
74  };
75 
76  template <typename Container>
77  struct SumMerger {
78  Container operator()(const std::vector<Container>& containers) const {
79  return sum_merge(containers);
80  }
81  };
82 
83 }
84 
85 
86 template <typename Container, typename Merger=detail::_Merger<Container>>
88 {
89 
90  int nthread_;
91  std::vector<Container> thread_containers_;
92  Merger merger_;
93 
94  public:
95 
97  : nthread_(0),
98  thread_containers_(0)
99  { }
100 
101  ThreadReplicated(int nthread)
102  : nthread_(nthread),
103  thread_containers_(nthread)
104  { }
105 
106  void set_nthread(int nthr) {
107  nthread_ = nthr;
108  thread_containers_.resize(nthr);
109  }
110 
111  Container& mine(int ithr) {
112  return thread_containers_[ithr];
113  }
114 
115  operator Container() const {
116  return merger_(thread_containers_);
117  }
118 
119  Container merged() const {
120  return merger_(thread_containers_);
121  }
122 
123 
124 };
125 
126 
127 
128 
129 
130 }
131 
132 #endif /* _util_container_thread_wrap_h */
sc::detail::SumMerger
Definition: thread_wrap.h:77
sc::detail::_Merger
Definition: thread_wrap.h:70
sc::ThreadReplicated
Definition: thread_wrap.h:87
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14

Generated at Sun Jan 26 2020 23:24:01 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.