source: trunk/ADOL-C/src/storemanager.h @ 595

Last change on this file since 595 was 595, checked in by kulshres, 4 years ago

Merge changes from 'master' branch of 'gitclone'

The following changesets have been merged:

commit 3931a4393e4003928eed197011a8dc75aab95514
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Feb 13 16:55:03 2015 +0100

Visual Studio Compatibility

unfortunately visual studio 2010 does not support C++11 standard.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 023a1f5062e61a5761f15a7c6ec93d05ec9e4bf3
Author: kulshres <kulshres@94ac48a7-3327-4b6a-8511-9a4036a20e83>
Date: Mon Feb 9 16:12:23 2015 +0000

correct ColPack? download website

git-svn-id: https://projects.coin-or.org/svn/ADOL-C/trunk@594 94ac48a7-3327-4b6a-8511-9a4036a20e83

commit 6a1c37c41b89b9c13b3702035d16ad78c5730166
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Feb 4 18:01:44 2015 +0100

scoping changes

forward declarations with friends and declspec(dllexport) for windows
are a bit tricky.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 199e0da8bbf28c4819cd47b5acfd5cd8a18d55a3
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Feb 4 17:42:37 2015 +0100

Add operations vec_dot and vec_axpy

This will reduce tape length and provide speed.
Dot-products and axpy operations done with
adoubles and for-loops require many temporary
adub variables and become slow.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit d5f729d55ed250384fe62c64f6b5dcc5e8c09748
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Feb 3 15:30:04 2015 +0100

read unsigned values from .adolcrc

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit b7a9e1062c345624f27039b2a590f09bd8e70a70
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jan 30 17:12:02 2015 +0100

specify initial size of live variable store in adolcrc

if one knows approximately how many live variables
there are going to be in the program one can
specify it in the .adolcrc file with INITLIVE
This will prevent the many initially required
new and delete calls while the store grows dynamically
when adouble objects are initialized

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 9ce7f63b4c7a7099781a2d2f6c7a962e24932c65
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jan 30 16:07:53 2015 +0100

provide a conversion to adouble* from advector

the memory will still be managed inside advector

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 26ac5de41e0ef0e0faae26e32ea3b377570b7ef0
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jan 30 16:06:53 2015 +0100

streamline advector class

since we now use c++11 we can use the default-insertion
instead of having to copy. This saves operations.
so we don't need the dflt field anymore.
We can also use the adolc_vec_copy() function to
implement the copy constructor.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 2b2f878a05082d777ebbe5e3d445751ca68759cb
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Jan 30 15:56:05 2015 +0100

add a faster vector copy operation

this should in principle be faster than calling
n assignment operators of adouble and it also
shortens the trace somewhat.

Can only be used if the arrays were allocated
with contiguous locations.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit ef9babd35faea9e766c6c861d99b001510d7e5d4
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Jan 28 14:01:06 2015 +0100

try disabling multithreading support in boost

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 766ac8360bb952b68ce345ea29ee96be65044c29
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Jan 27 14:28:23 2015 +0100

add DLL_EXPORT to function

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit fe580956f2664a743f1a981464fd398aabd9acbc
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Jan 27 12:41:51 2015 +0100

allow edf structures to be initialised to zero from outside the library

This is required to properly deallocate the memory, which is user
allocated and set as pointers in this structure, to ensure that
the structure does not call for this memory after the user deallocates
it.

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 4c793005996f0af6b4a95788283e3baab233d3b8
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jan 26 13:56:35 2015 +0100

more streamlining of next_loc()/free_loc()

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 6686a248a2598a306f4502acd3158c4945d45705
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jan 26 13:20:14 2015 +0100

a couple of warning fixes

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 7a87f9f5ed97d0af7db7674229483aa782abd970
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jan 26 11:17:10 2015 +0100

streamline StoreManagerLocintBlock::free_loc() slightly

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 1e4b7a739849098fc33174138b237e5d44462822
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jan 26 11:08:41 2015 +0100

Also check for boost::system in code

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 1f9eb738280610aad49b1dff623e8aaaca9903d6
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Mon Jan 26 11:05:02 2015 +0100

use a pool allocator from the boost library if available

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 0498360e12d8590f67f19f9a1bca7dff1edb00a2
Author: kulshres <kulshres@94ac48a7-3327-4b6a-8511-9a4036a20e83>
Date: Tue Jan 6 14:48:24 2015 +0000

copy paste typos

Thanks to Mu Wang <wangmu0701@…>

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

git-svn-id: https://projects.coin-or.org/svn/ADOL-C/trunk@593 94ac48a7-3327-4b6a-8511-9a4036a20e83

commit 80f4a743262d32daf1869cfbc4486c977de1e3a4
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Dec 24 14:45:32 2014 +0100

Scatterv/Allgatherv? and compile fixes

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 6caec6d3e19f234da22241e97999cb2eebb40be2
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Wed Dec 24 01:04:48 2014 +0100

Implement Gatherv and some compile fixes

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 439ab813d8b08c0a17c6afb6180ebe914acc3e20
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Dec 23 19:56:45 2014 +0100

Don't use uninitialised pointer for status in MPI calls

From: Max Sagebaum <max.sagebaum@…>
Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit fb911b73d6f9e4729fdf5148671b3c23ed9252b8
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Fri Dec 19 11:02:14 2014 +0100

Implement AMPI calls in forward mode

Send,Recv,Gather,Scatter,Allgather

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

commit 5eecda924840d63fa7309bfc3e4758b28f7e41ec
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Dec 16 15:55:32 2014 +0100

try out some c++11 features since we mandate it anyway

use forward_list instead of list since its unidirectional

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

Signed-off-by: Kshitij Kulshreshtha <kshitij@…>

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1// -*- c++ -*- hello emacs...
2/*----------------------------------------------------------------------------
3 ADOL-C--  Automatic Differentiation by Overloading in C++ - simplified
4 File:     storemanager.h
5 Revision: $Id: storemanager.h 595 2015-02-16 11:08:54Z kulshres $
6 Contents: storemanager.h contains definitions of abstract interface
7           class StoreManager and some derived classes implementing the
8           desired functionality.
9
10 Copyright (c) 2006 Johannes Willkomm <johannes.willkomm@rwth-aachen.de>
11               2011-2013 Kshitij Kulshreshtha <kshitij@math.upb.de>
12               2012 Benjamin Letschert <letschi@mail.upb.de>
13               2013 Jean Utke <utke@mcs.anl.gov>
14
15 This file is part of ADOL-C.
16
17 The classes StoreManagerXYZ basically takes the global double *store pointer
18 into their obhut and implement next_loc and free_loc.
19
20 They basic idea is taken from "The C++ Programming Language" by Bjarne
21 Stroustrup, from the chapter 19 on iterators and allocators.
22
23 To understand how and why they work do the following:
24 1) Have a look at StoreManagerInSitu and convince yourself that it is
25    exactly the same as the solution presented in the Stroustrup book,
26    except that we always have just one big array instead of a linked
27    list of chunks.
28
29    This means in particular that we have to copy the values from the
30    old array into the lower half of the new one (we always double the size).
31
32 2) Have a look at StoreManagerLocint and convince yourself that these do
33    the same as StoreManagerInSitu except that the linked list of free
34    slots is maintained in a completely different portion of memory. This
35    means the values in freed slots remain untouched until they are
36    allocated again.
37
38
39 3) Have a look a class StoreManagerLocintBlock. This class uses a list of
40    of free blocks of different sizes instead of free locations.
41
42 class StoreManagerInSitu
43 An unsafe implementation is provided as well, but commented out.
44 It does not use the indexFeld array which saves between
45 25% and 50% memory relative to the above safe implementation.
46
47 It is most closely modelled after the example found in the
48 Stroustrup book.
49
50 It appears that it works very well, if one does not use the
51 trace_on(tag, 1); ... trace_off(); reverse(); way of using ADOL-C.
52 If the first sweep is forward it works fine.
53 Therefore I left it in here as a comment so an interested user
54 with acute main memory scarcity may give it a try.
55           
56
57 History:
58          20120427 bl:     add blocking store management
59          20110208 kk:     incorporated in ADOL-C; moved some code arround
60          20060507 jw:     begin
61
62----------------------------------------------------------------------------*/
63
64#ifndef ADOL_C__STOREMANAGER_H
65#define ADOL_C__STOREMANAGER_H
66
67#include <adolc/internal/common.h>
68#include <forward_list>
69#if defined(HAVE_BOOST_POOL_POOL_ALLOC_HPP) && defined(HAVE_BOOST_SYSTEM)
70#include <boost/pool/pool_alloc.hpp>
71#define USE_BOOST_POOL 1
72#endif
73
74class Keeper;
75
76class StoreManager {
77  friend class Keeper;
78protected:
79  static size_t const initialSize = 4;
80  double myGcTriggerRatio;
81  size_t myGcTriggerMaxSize;
82  virtual void grow(size_t mingrow = 0) = 0;
83public:
84  StoreManager() : myGcTriggerRatio(1.5), myGcTriggerMaxSize(initialSize) {}
85  virtual ~StoreManager() {}
86  virtual locint next_loc() = 0;
87  virtual void free_loc(locint) = 0;
88  virtual void ensure_block(size_t n) = 0;
89  void setStoreManagerControl(double gcTriggerRatio, size_t gcTriggerMaxSize) { myGcTriggerRatio=gcTriggerRatio; myGcTriggerMaxSize=gcTriggerMaxSize;}
90  double gcTriggerRatio() const {return myGcTriggerRatio;}
91  size_t gcTriggerMaxSize() const {return myGcTriggerMaxSize;}
92
93//   // effectively the current size of the store array
94  virtual size_t maxSize() const = 0;
95
96//   // the number of slots currently in use
97  virtual size_t size() const = 0;
98};
99
100class StoreManagerLocint : public StoreManager {
101protected:
102  double * &storePtr;
103  locint * indexFree;
104  locint head;
105  size_t &maxsize;
106  size_t &currentfill;
107  virtual void grow(size_t mingrow = 0);
108public:
109
110  StoreManagerLocint(double * &storePtr, size_t &size, size_t &numlives);
111  StoreManagerLocint(const StoreManagerLocint *const stm, double * &storePtr, size_t &size, size_t &numLives);
112
113  virtual ~StoreManagerLocint();
114  virtual inline size_t size() const { return currentfill; }
115
116  virtual inline size_t maxSize() const { return maxsize; }
117
118  virtual inline bool realloc_on_next_loc() const { 
119      return (head == 0);
120  }
121
122  virtual locint next_loc();
123  virtual void free_loc(locint loc); 
124  virtual void ensure_block(size_t n) {}
125};
126
127class StoreManagerLocintBlock : public StoreManager {
128protected:
129    double * &storePtr;
130    struct FreeBlock {
131        locint next; // next location
132        size_t size; // number of following free locations
133        FreeBlock(): next(0), size(0) {}
134        FreeBlock(const struct FreeBlock &block) :
135            next(block.next),size(block.size) {}
136        FreeBlock(const locint& n, const size_t& s) :
137            next(n), size(s) {}
138        bool operator<(const struct FreeBlock& b) const {
139            return (next < b.next);
140        }
141    };
142
143    std::forward_list<struct FreeBlock
144#if USE_BOOST_POOL
145                      , boost::fast_pool_allocator<struct FreeBlock> 
146#endif
147                      >  indexFree;
148    size_t &maxsize;
149    size_t &currentfill;
150
151    void consolidateBlocks();
152#ifdef ADOLC_LOCDEBUG
153    unsigned int ensure_blockCallsSinceLastConsolidateBlocks;
154#endif
155    /**
156     * when minGrow is specified we asssume that we have already
157     * search the blocks and found no block with minGrow locations in it
158     */
159    virtual void grow(size_t minGrow=0 );
160public:
161    StoreManagerLocintBlock(double * &storePtr, size_t &size, size_t &numlives);
162    StoreManagerLocintBlock(const StoreManagerLocintBlock *const stm, double * &storePtr, size_t &size, size_t &numLives);
163
164    virtual ~StoreManagerLocintBlock();
165    virtual inline size_t size() const { return currentfill; }
166
167    virtual inline size_t maxSize() const { return maxsize; }
168
169    virtual locint next_loc();
170    virtual void free_loc(locint loc);
171    virtual void ensure_block(size_t n);
172};
173
174#if 0
175/* This implementation is unsafe in that using tace_on with keep=1 and
176   reverse mode directly afterwards will yield incorrect results.
177   For all other purposes it seem to work just fine, so it's left here
178   for reference as a comment.
179*/
180
181/* unsafe - use with care */
182
183class StoreManagerInSitu : public StoreManager {
184  //  static size_t const initialeGroesse = 512;
185protected:
186  double * &storePtr;
187  struct Link {
188    struct Link *next;
189  };
190  Link *head;
191  size_t groesse;
192  size_t anzahl;
193public:
194  size_t maxIndexUsed;
195
196  StoreManager(double * &storePtr) :
197    storePtr(storePtr),
198    head(0),
199    groesse(initialeGroesse),
200    anzahl(0),
201    maxIndexUsed(0)
202  {
203    // while a place in store is unused we want to place
204    // a Link stucture (i.e. a pointer) there
205    assert(sizeof(double) >= sizeof(void*));
206    assert(sizeof(double) >= sizeof(Link));
207    std::cerr << "StoreManager::StoreManager()\n";
208  }
209
210  virtual ~StoreManager() {
211    if (storePtr) {
212      delete [] storePtr;
213      storePtr = 0;
214    }
215    std::cerr << "StoreManager::~StoreManager()\n";
216  }
217
218  virtual inline size_t size() const { return anzahl; }
219
220  virtual inline size_t maxSize() const { return groesse; }
221
222  virtual locint next_loc(size_t n = 1) {
223    assert(n == 1);
224    if (head == 0) {
225      grow();
226    }
227    assert(head);
228    double * const dPtr = reinterpret_cast<double*>(head);
229    head = head->next;
230    ++anzahl;
231    locint const result = dPtr - storePtr;
232    maxIndexUsed = std::max((locint)maxIndexUsed, result);
233    return result;
234  }
235
236  virtual void free_loc(locint loc) {
237    assert(loc < groesse);
238    Link *returned = reinterpret_cast<Link*>(storePtr + loc);
239    returned->next = head;
240    head = returned;
241    --anzahl;
242  }
243private:
244  void grow() {
245    size_t const alteGroesse = groesse;
246    groesse *= 2;
247    assert(alteGroesse == initialeGroesse or size() == alteGroesse);
248    std::cerr << "StoreManager::grow(): increase size to " << groesse << "\n";
249    double *const oldStore = storePtr;
250    std::cerr << "StoreManager::grow(): allocate " << groesse * sizeof(double) << " B\n";
251    storePtr = new double[groesse];
252    size_t i = 0;
253    if (alteGroesse != initialeGroesse) { // nicht beim ersten Mal
254      std::cerr << "StoreManager::grow(): copy values\n";
255      for ( ; i < alteGroesse; ++i) {
256        storePtr[i] = oldStore[i];
257      }
258      std::cerr << "StoreManager::grow(): free " << alteGroesse * sizeof(double) << " B\n";
259      delete [] oldStore;
260    }
261    head = reinterpret_cast<Link*>(storePtr + i);
262    for ( ; i < groesse-1; ++i) {
263      reinterpret_cast<Link*>(storePtr + i)->next
264        = reinterpret_cast<Link*>(storePtr + i + 1);
265    }
266    reinterpret_cast<Link*>(storePtr + i)->next = 0;
267  }
268
269};
270#endif /* 0 */
271
272#endif /* ADOL_C__STOREMANAGER_H */
273
Note: See TracBrowser for help on using the repository browser.