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

Last change on this file since 427 was 427, checked in by kulshres, 6 years ago

Merge branch 'master' of 'gitclone' into svn trunk

The following changes were merged:
commit 02942597253216bdf58b5c7f2f9a5288ce274777
Author: Max Sagebaum <sagebaum@…>
Date: Wed Apr 3 10:55:33 2013 +0200

Allow adouble objects to be initialized late

This functionality is specially required for large codes that allocate
memory using malloc/calloc and we only wish to substitute the double
datatype with adouble for tracing purposes. With malloc/calloc the
adouble constructor is not called, creating problems later in the code.
Late initialization defers some of the initialization to the first use
of an allocated space as an adouble object. This is only possible because
the class heirarchy in ADOL-C does not require polymorphism, and virtual
tables to be allocated, which can only be done in a chained superclass
constructor call.

The functionality is disabled by default and can be enabled by a configure
switch --enable-lateinit

This code is experimental and has only be tested with particular codes.

From: Max Sagebaum <sagebaum@…>
Signed-off-by: Kshitij Kulshreshtha <kshitij@…>
CC: Andrea Walther <andrea.walther@…>
CC: Nicolas Gauger <gauger@…>

commit 768851025e58a7605f0e9b6a65f51bfc3c2977f4
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Apr 2 11:14:50 2013 +0200

add dvi file to gitignore

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

commit 80357742a527a4f4b9ef6323553279eb260cc7df
Author: Kshitij Kulshreshtha <kshitij@…>
Date: Tue Apr 2 11:14:37 2013 +0200

remove as many compiler warnings from gcc -Wall as possible

only -Wunused-value remains, as we do indeed ignore some
values computed when we are in INDO, NONL_IND or
INT_FOR, INT_REV modes.

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

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