source: colin/trunk/src/ResponseCache_Local.cpp @ 5786

Revision 5786, 9.1 KB checked in by wehart, 5 years ago (diff)

Update of source to include Acro copyright statement

Line 
1/*  _________________________________________________________________________
2 *
3 *  Acro: A Common Repository for Optimizers
4 *  Copyright (c) 2008 Sandia Corporation.
5 *  This software is distributed under the BSD License.
6 *  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7 *  the U.S. Government retains certain rights in this software.
8 *  For more information, see the README.txt file in the top Acro directory.
9 *  _________________________________________________________________________
10 */
11
12/**
13 * \file ResponseCache_Local.cpp
14 *
15 * Implements the colin::ResponseCache_Local class.
16 */
17
18#include <colin/ResponseCache_Local.h>
19#include <colin/AppRequest.h>
20#include <colin/ResponseCacheFactory.h>
21
22using std::cerr;
23using std::endl;
24
25using std::set;
26using std::map;
27using std::pair;
28using std::string;
29using std::make_pair;
30
31using utilib::Any;
32using utilib::seed_t;
33
34namespace colin {
35
36namespace StaticInitializers {
37
38namespace {
39
40/// Function to create a new ResponseCache_Local
41ResponseCache* create_local_cache()
42{
43   return new ResponseCache_Local();
44}
45
46bool RegisterLocalCache()
47{
48   CacheFactory().declare_cache_type("Local", create_local_cache);
49   return true;
50}
51
52} // namespace colin::StaticInitializers::(local)
53
54extern const volatile bool local_cache = RegisterLocalCache();
55
56} // namespace colin::StaticInitializers
57
58
59
60//================================================================
61// ResponseCache_Local::Data Member Definitions
62//================================================================
63
64struct ResponseCache_Local::Data {
65   typedef map<string, Any> annotation_map_t;
66
67   struct Record {
68      Record()
69         : data(), annotations()
70      {}
71
72      CachedResponseData  data;
73      annotation_map_t    annotations;
74   };
75
76   typedef map<Key, Record>  cache_map_t;
77
78   typedef map<const OptApplication_Base*,  cache_map_t>  multicache_map_t;
79
80   multicache_map_t caches;
81};
82
83
84
85//================================================================
86// ResponseCache_Local Public Member Definitions
87//================================================================
88
89ResponseCache_Local::ResponseCache_Local()
90   : data(new Data())
91{}
92
93
94ResponseCache_Local::~ResponseCache_Local()
95{
96   delete data;
97}
98
99
100size_t ResponseCache_Local::size( const OptApplication_Base* app ) const
101{
102   Data::multicache_map_t::iterator c_it = data->caches.find(app);
103   if ( c_it == data->caches.end() )
104      return 0;
105   else
106      return c_it->second.size();
107}
108
109
110void ResponseCache_Local::clear( const OptApplication_Base* app )
111{
112   if ( app == NULL )
113      data->caches.clear();
114   else
115   {
116      Data::multicache_map_t::iterator c_it = data->caches.find(app);
117      if ( c_it == data->caches.end() )
118         EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::clear(): "
119                        "unknown application");
120      else
121         data->caches.erase(c_it);
122   }
123}
124
125
126size_t
127ResponseCache_Local::erase( const OptApplication_Base* app, Key key )
128{
129   Data::multicache_map_t::iterator c_it = data->caches.find(app);
130   if ( c_it == data->caches.end() )
131      return 0;
132   else
133      return c_it->second.erase(key);
134}
135
136
137
138CachedResponseData
139ResponseCache_Local::find( const OptApplication_Base* app, Key key ) const
140{
141   Data::multicache_map_t::iterator c_it = data->caches.find(app);
142   if ( c_it == data->caches.end() )
143      return CachedResponseData();
144
145   Data::cache_map_t::iterator it = c_it->second.find(key);
146   if ( it == c_it->second.end() )
147      return CachedResponseData();
148   else
149      return it->second.data;
150}
151
152
153void
154ResponseCache_Local::
155insert_info( const OptApplication_Base* app,
156             const Key key,
157             const utilib::seed_t seed,
158             const utilib::Any domain,
159             const std::map<response_info_t, const utilib::Any> &info )
160{
161   if ( app == NULL )
162      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::insert_info(): "
163                     "cannot insert into a cache with a NULL application");
164   if ( key.empty() )
165      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::insert_info(): "
166                     "cannot insert into a cache with an empty key");
167
168   Data::Record &cache_record = data->caches[app][key];
169   if ( cache_record.data.empty() )
170      cache_record.data = CachedResponseData(domain);
171   insert_impl(cache_record.data, seed, info);
172}
173
174
175size_t
176ResponseCache_Local::getResponses( AppRequest& request,
177                                   std::list<AppResponse> &ans,
178                                   size_t maxReturnCount ) const
179{
180   ans.clear();
181
182   // At this point, no more changes to the request will be honored
183   AppRequest tmp_r = request.replicate(request.seed());
184   request.finalize_request();
185   CoreRequestInfo cqi = get_core_request(request);
186
187   Data::multicache_map_t::iterator c_it = data->caches.find(cqi.app);
188   if ( c_it == data->caches.end() )
189      return 0;  // throw an exception???
190
191
192   Data::cache_map_t::iterator it;
193   Data::cache_map_t::iterator itEnd;
194   if ( cqi.domain.empty() )
195   {
196      // Check all stored domain points
197      it = c_it->second.begin();
198      itEnd = c_it->second.end();
199   }
200   else
201   {
202      // look for a specific domain point
203      itEnd = it = c_it->second.find(generate_key(cqi.domain));
204      if ( it == c_it->second.end() )
205         return 0;
206      ++itEnd;
207   }
208
209   size_t count = 0;
210   for( ; it != itEnd; ++it )
211   {
212      // Needed for all responses after the first (and silently ignored
213      // by the first)
214      request.finalize_request();
215
216      set<seed_t> seeds;
217      if ( ! it->second.data.get_seeds(cqi.requests, seeds) )
218         continue;
219     
220      CoreResponseInfo cri;
221      if (( cqi.seed != 0 ) || ( it->second.data.has_seed(0) ))
222      {
223         if ( seeds.count(cqi.seed) == 0 )
224            continue;
225         cri.seed = cqi.seed;
226      }
227      else
228         cri.seed = *seeds.begin();
229      it->second.data.get(cri.seed, cri.responses);
230      cri.domain = it->second.data.domain();
231
232      ans.push_back(form_response(request, cri));
233      ++count;
234
235      if ( count == maxReturnCount )
236         break;
237
238      request = tmp_r;
239      tmp_r = request.replicate(request.seed());
240   }
241   return count;
242}
243
244
245std::map<ResponseCache::Key, CachedResponseData>
246ResponseCache_Local::getRawData( const OptApplication_Base *app,
247                                 FilterFunctor& filter ) const
248{
249   map<Key, CachedResponseData> ans;
250
251   Data::multicache_map_t::iterator c_it = data->caches.find(app);
252   if ( c_it == data->caches.end() )
253      return ans;
254
255   Data::cache_map_t::iterator it = c_it->second.begin();
256   Data::cache_map_t::iterator itEnd = c_it->second.end();
257   for( ; it != itEnd; ++it )
258   {
259      if ( filter(it->first, it->second.data) )
260         ans.insert(ans.end(), *it);
261   }
262   return ans;
263}
264
265
266
267void
268ResponseCache_Local::annotate( const OptApplication_Base* app,
269                               Key key,
270                               std::string attribute,
271                               utilib::Any value )
272{
273   Data::multicache_map_t::iterator c_it = data->caches.find(app);
274   if ( c_it == data->caches.end() )
275      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::annotate(): "
276                     "unknown application");
277
278   Data::cache_map_t::iterator it = c_it->second.find(key);
279   if ( it == c_it->second.end() )
280      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::annotate(): "
281                     "key not present in cache");
282   
283   if ( ! it->second.annotations.insert( make_pair(attribute, value) ).second )
284      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::annotate(): "
285                     "duplicate annotation (" << attribute << ", "
286                     << value << ")");
287}
288
289
290utilib::Any
291ResponseCache_Local::get_annotation( const OptApplication_Base* app,
292                                     Key key,
293                                     std::string attribute ) const
294{
295   Data::multicache_map_t::iterator c_it = data->caches.find(app);
296   if ( c_it == data->caches.end() )
297      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::"
298                     "get_annotation(): unknown application");
299
300   Data::cache_map_t::iterator it = c_it->second.find(key);
301   if ( it == c_it->second.end() )
302      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::"
303                     "get_annotation(): key not present in cache");
304   
305   Data::annotation_map_t::iterator a_it
306      = it->second.annotations.find(attribute);
307   if ( a_it == it->second.annotations.end() )
308      return Any();
309   else
310      return a_it->second;
311}
312
313
314std::map<std::string, utilib::Any>
315ResponseCache_Local::
316get_annotations( const OptApplication_Base* app, Key key ) const
317{
318   Data::multicache_map_t::iterator c_it = data->caches.find(app);
319   if ( c_it == data->caches.end() )
320      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::"
321                     "get_annotations(): unknown application");
322
323   Data::cache_map_t::iterator it = c_it->second.find(key);
324   if ( it == c_it->second.end() )
325      EXCEPTION_MNGR(std::runtime_error, "ResponseCache_Local::"
326                     "get_annotations(): key not present in cache");
327   
328   return it->second.annotations;
329}
330
331
332
333} // namespace colin
Note: See TracBrowser for help on using the repository browser.