Psi4
threaded_storage.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 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 Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #if !defined(psi_src_lib_libparallel_threaded_storage)
30 #define psi_src_lib_libparallel_threaded_storage
31 
32 #include <vector>
33 
34 namespace psi {
35 
36 template<typename T>
38 {
39  std::vector<T> storage_;
40 
41 public:
42 
43  threaded_storage(const T& value = T())
44  : storage_(Process::environment.get_n_threads(), value)
45  { }
46 
47  void initialize(const T& value) {
48  storage_.clear();
49  for (int i=0; i<Process::environment.get_n_threads(); ++i) {
50  storage_.push_back(value);
51  }
52  }
53 
54  T& operator*() {
55  return storage_[0];
56  }
57 
58  const T& operator[](size_t ind) const {
59  return storage_[ind];
60  }
61  T& operator[](size_t ind) {
62  return storage_[ind];
63  }
64 };
65 
66 }
67 
68 #endif
Definition: pointgrp.h:106
Definition: process.h:53
T & operator[](size_t ind)
Definition: threaded_storage.h:61
static Environment environment
Definition: process.h:138
value void initialize(const T &value)
Definition: threaded_storage.h:47
T & operator*()
Definition: threaded_storage.h:54
const T & operator[](size_t ind) const
Definition: threaded_storage.h:58
Definition: threaded_storage.h:37
int get_n_threads() const
Number of threads per process.
Definition: process.cc:186
Definition: PsiFileImpl.h:40
std::vector< T > storage_
Definition: threaded_storage.h:39