MADNESS  version 0.9
dist_cache.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2013 Virginia Tech
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id$
33  */
34 
35 #ifndef MADNESS_WORLD_DIST_CACHE_H__INCLUDED
36 #define MADNESS_WORLD_DIST_CACHE_H__INCLUDED
37 
38 #include <madness/world/worldexc.h>
40 #include <madness/world/worldfut.h>
41 
42 namespace madness {
43  namespace detail {
44 
46 
56  template <typename keyT>
57  class DistCache {
58  private:
59 
60  // Forward declarations
61  class Cache;
62  template <typename> class CacheData;
63 
66  typedef typename cache_container::datumT datum_type;
68 
69  static cache_container caches_;
70 
72 
74  class Cache {
75  public:
76 
78  virtual ~Cache() { }
79 
81 
84  template <typename valueT>
85  const madness::Future<valueT>& get() const {
86  MADNESS_ASSERT(this->get_type_info() == typeid(CacheData<valueT>));
87  return static_cast<const CacheData<valueT>*>(this)->value();
88  }
89 
90  private:
91 
93 
95  virtual const std::type_info& get_type_info() const = 0;
96 
97  }; // class Cache
98 
100 
102  template <typename valueT>
103  class CacheData : public Cache {
104  private:
105  madness::Future<valueT> value_;
106 
107  public:
108 
110  CacheData() : value_() { }
111 
113  CacheData(const madness::Future<valueT>& value) : value_(value) { }
114 
116  CacheData(const valueT& value) : value_(value) { }
117 
119  virtual ~CacheData() { }
120 
122 
124  const madness::Future<valueT>& value() const { return value_; }
125 
126  private:
127 
129 
131  virtual const std::type_info& get_type_info() const {
132  return typeid(CacheData<valueT>);
133  }
134 
135  }; // class CacheData
136 
137  public:
138 
140 
148  template <typename valueT>
149  static void set_cache_value(const keyT& key, const valueT& value) {
151 
152  // Retrieve the cached future
153  typename cache_container::accessor acc;
154  if(caches_.insert(acc, datum_type(key, static_cast<Cache*>(NULL)))) {
155 
156  // A new element was inserted, so create a new cache object.
157  acc->second = new CacheData<value_type>(value);
158  acc.release();
159 
160  } else {
161 
162  // The element already existed, so retrieve the data
163  Cache* cache = acc->second;
164  caches_.erase(acc);
165 
166  // Set the cache value
168  cache->template get<value_type>();
169  MADNESS_ASSERT(! f.probe());
170  f.set(value);
171 
172  // Cleanup cache
173  delete cache;
174  }
175  }
176 
178 
187  template <typename valueT>
188  static void get_cache_value(const keyT& key, madness::Future<valueT>& value) {
189  // Retrieve the cached future
190  typename cache_container::accessor acc;
191  if(caches_.insert(acc, datum_type(key, static_cast<Cache*>(NULL)))) {
192  // A new element was inserted, so create a new cache object.
193  acc->second = new CacheData<valueT>(value);
194  acc.release();
195  } else {
196  // The element already existed, so retrieve the data and
197  // remove the cache element.
198  Cache* cache = acc->second;
199  caches_.erase(acc);
200 
201  // Get the result
202  value.set(cache->template get<valueT>());
203  delete cache;
204  }
205  }
206 
208 
216  template <typename valueT>
217  static madness::Future<valueT> get_cache_value(const keyT& key) {
218  // Retrieve the cached future
219  typename cache_container::accessor acc;
220  if(caches_.insert(acc, datum_type(key, static_cast<Cache*>(NULL)))) {
221  // A new element was inserted, so create a new cache object.
222  acc->second = new CacheData<valueT>();
223  madness::Future<valueT> value(acc->second->template get<valueT>());
224  acc.release();
225 
226  return value;
227  } else {
228  // The element already existed, so retrieve the data and
229  // remove the cache element.
230  Cache* cache = acc->second;
231  caches_.erase(acc);
232 
233  // Get the result
234  madness::Future<valueT> value(cache->template get<valueT>());
235  delete cache;
236 
237  return value;
238  }
239  }
240 
241  }; // class DistCache
242 
243  template <typename keyT>
244  typename DistCache<keyT>::cache_container DistCache<keyT>::caches_;
245 
246  } // namespace detail
247 } // namespace madness
248 
249 #endif // MADNESS_WORLD_DIST_CACHE_H__INCLUDED
void release()
Definition: worldhashmap.h:382
Definition: worldhashmap.h:332
bool probe() const
Query the whether this future has been assigned.
Definition: worldfut.h:527
std::pair< const keyT, valueT > datumT
Definition: worldhashmap.h:401
NDIM & f
Definition: mra.h:2179
Implements Future.
static void get_cache_value(const keyT &key, madness::Future< valueT > &value)
Get the cache value accosted with key.
Definition: dist_cache.h:188
Definition: worldhashmap.h:57
Distributed caching utility.
Definition: dist_cache.h:57
std::size_t erase(const keyT &key)
Definition: worldhashmap.h:503
static madness::Future< valueT > get_cache_value(const keyT &key)
Get the cache value accosted with key.
Definition: dist_cache.h:217
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:222
std::pair< iterator, bool > insert(const datumT &datum)
Definition: worldhashmap.h:469
static void set_cache_value(const keyT &key, const valueT &value)
Set the cache value accosted with key.
Definition: dist_cache.h:149
Implements MadnessException.
void set(const Future< T > &other)
A.set(B) where A & B are futures ensures A has/will have the same value as B.
Definition: worldfut.h:473
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
T type
Definition: worldfut.h:130
Defines and implements a concurrent hashmap.