QMCPACK
ResourceCollection.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "ResourceCollection.h"
13 #include <iostream>
14 #include <Host/OutputManager.h>
15 
16 namespace qmcplusplus
17 {
18 ResourceCollection::ResourceCollection(const std::string& name) : name_(name), cursor_index_(0) {}
19 
20 ResourceCollection::ResourceCollection(const ResourceCollection& ref) : name_(ref.getName()), cursor_index_(0)
21 {
22  for (auto& res : ref.collection_)
23  addResource(std::unique_ptr<Resource>(res->makeClone()), true);
24 }
25 
27 {
28  std::cout << "list resources in " << getName() << std::endl;
29  std::cout << "-------------------------------" << std::endl;
30  for (int i = 0; i < collection_.size(); i++)
31  std::cout << "resource " << i << " name: " << collection_[i]->getName()
32  << " address: " << collection_[i].get() << std::endl;
33  std::cout << "-------------------------------" << std::endl << std::endl;
34 }
35 
36 size_t ResourceCollection::addResource(std::unique_ptr<Resource>&& res, bool noprint)
37 {
38  size_t index = collection_.size();
39  res->index_in_collection_ = index;
40  if (!noprint)
41  app_debug_stream() << "Multi walker shared resource \"" << res->getName() << "\" created in resource collection \""
42  << name_ << "\" index " << index << std::endl;
43  collection_.emplace_back(std::move(res));
44  return index;
45 }
46 
48 {
49  if (cursor_index_ >= collection_.size())
50  throw std::runtime_error("ResourceCollection::lendResource BUG no more resource to lend.");
51  if (cursor_index_ != collection_[cursor_index_]->index_in_collection_)
52  throw std::runtime_error(
53  "ResourceCollection::lendResource BUG mismatched cursor index and recorded index in the resource.");
54  return *collection_[cursor_index_++];
55 }
56 
58 {
59  if (cursor_index_ >= collection_.size())
60  throw std::runtime_error("ResourceCollection::takebackResource BUG cannot take back resources more than owned.");
62  throw std::runtime_error(
63  "ResourceCollection::takebackResource BUG mismatched cursor index and recorded index in the resource.");
64  if (&res != collection_[cursor_index_++].get())
65  throw std::runtime_error(
66  "ResourceCollection::takebackResource BUG the resource taken back mismatches the one lent.");
67 }
68 
69 } // namespace qmcplusplus
std::vector< std::unique_ptr< Resource > > collection_
size_t addResource(std::unique_ptr< Resource > &&res, bool noprint=false)
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Declaration of OutputManager class.
ResourceCollection(const std::string &name)
const std::string & getName() const
std::ostream & app_debug_stream()
Definition: OutputManager.h:71
void takebackResourceImpl(Resource &res)