[40] | 1 | /*---------------------------------------------------------------------------- |
---|
| 2 | ADOL-C -- Automatic Differentiation by Overloading in C++ |
---|
| 3 | File: struct_buf.h |
---|
[42] | 4 | Revision: $Id: buffer_temp.h 607 2015-05-19 09:36:56Z stefan $ |
---|
[40] | 5 | Contents: - template class for linked list of Type buffers with constant length |
---|
| 6 | per buffer |
---|
| 7 | - intended to be used with structs |
---|
| 8 | |
---|
[439] | 9 | Copyright (c) Andreas Kowarz, Kshitij Kulshreshtha, Jean Utke |
---|
[40] | 10 | |
---|
| 11 | This file is part of ADOL-C. This software is provided as open source. |
---|
| 12 | Any use, reproduction, or distribution of the software constitutes |
---|
| 13 | recipient's acceptance of the terms of the accompanying license file. |
---|
| 14 | |
---|
| 15 | ----------------------------------------------------------------------------*/ |
---|
| 16 | |
---|
| 17 | #if !defined(ADOLC_STRUCT_BUF_H) |
---|
| 18 | #define ADOLC_STRUCT_BUF_H 1 |
---|
| 19 | |
---|
[541] | 20 | #include <adolc/internal/common.h> |
---|
[106] | 21 | #include "taping_p.h" |
---|
[40] | 22 | |
---|
| 23 | #if defined(__cplusplus) |
---|
| 24 | /****************************************************************************/ |
---|
| 25 | /* This is all C++ */ |
---|
| 26 | |
---|
| 27 | #include <cstdlib> |
---|
| 28 | |
---|
[439] | 29 | #define BUFFER Buffer<SubBufferElement, _subBufferSize> |
---|
| 30 | #define BUFFER_TEMPLATE template<class SubBufferElement, IndexType _subBufferSize> |
---|
[40] | 31 | |
---|
| 32 | typedef locint IndexType; |
---|
| 33 | |
---|
| 34 | BUFFER_TEMPLATE class Buffer { |
---|
| 35 | |
---|
| 36 | typedef void (*InitFunctionPointer) (SubBufferElement *subBufferElement); |
---|
| 37 | |
---|
[438] | 38 | static void zeroAll(SubBufferElement* subBufferElement); |
---|
| 39 | |
---|
[40] | 40 | typedef struct SubBuffer { |
---|
| 41 | SubBufferElement elements[_subBufferSize]; |
---|
| 42 | struct SubBuffer *nextSubBuffer; |
---|
[607] | 43 | SubBuffer(); |
---|
[40] | 44 | } |
---|
| 45 | SubBuffer; |
---|
| 46 | |
---|
| 47 | public: |
---|
| 48 | inline Buffer() { |
---|
| 49 | firstSubBuffer = NULL; |
---|
| 50 | numEntries = 0; |
---|
| 51 | subBufferSize = _subBufferSize; |
---|
[438] | 52 | initFunction = zeroAll; |
---|
[40] | 53 | } |
---|
| 54 | inline Buffer(InitFunctionPointer _initFunction) { |
---|
| 55 | firstSubBuffer = NULL; |
---|
| 56 | numEntries = 0; |
---|
| 57 | subBufferSize = _subBufferSize; |
---|
| 58 | initFunction = _initFunction; |
---|
| 59 | } |
---|
| 60 | inline ~Buffer(); |
---|
| 61 | |
---|
| 62 | inline void init(InitFunctionPointer _initFunction) { |
---|
| 63 | initFunction = _initFunction; |
---|
| 64 | } |
---|
[439] | 65 | SubBufferElement *append(); |
---|
[40] | 66 | SubBufferElement *getElement(IndexType index); |
---|
| 67 | |
---|
| 68 | private: |
---|
| 69 | SubBuffer *firstSubBuffer; |
---|
| 70 | InitFunctionPointer initFunction; |
---|
| 71 | IndexType subBufferSize; |
---|
| 72 | IndexType numEntries; |
---|
| 73 | }; |
---|
| 74 | |
---|
| 75 | BUFFER_TEMPLATE |
---|
[438] | 76 | void BUFFER::zeroAll(SubBufferElement* subBufferElement) { |
---|
| 77 | memset(subBufferElement,0,sizeof(*subBufferElement)); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | BUFFER_TEMPLATE |
---|
[607] | 81 | BUFFER::SubBuffer::SubBuffer() { |
---|
| 82 | memset(elements,0,sizeof(SubBufferElement)*_subBufferSize); |
---|
| 83 | nextSubBuffer = NULL; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | BUFFER_TEMPLATE |
---|
[40] | 87 | BUFFER::~Buffer() { |
---|
| 88 | SubBuffer *tmpSubBuffer = NULL; |
---|
| 89 | |
---|
| 90 | while (firstSubBuffer != NULL) { |
---|
| 91 | tmpSubBuffer = firstSubBuffer; |
---|
| 92 | firstSubBuffer = firstSubBuffer->nextSubBuffer; |
---|
[607] | 93 | for(int i = 0; i < subBufferSize; i++) |
---|
| 94 | if (tmpSubBuffer->elements[i].allmem != NULL) |
---|
| 95 | free(tmpSubBuffer->elements[i].allmem); |
---|
[40] | 96 | delete tmpSubBuffer; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | BUFFER_TEMPLATE |
---|
[439] | 101 | SubBufferElement *BUFFER::append() { |
---|
[40] | 102 | SubBuffer *currentSubBuffer=firstSubBuffer, *previousSubBuffer=NULL; |
---|
| 103 | IndexType index, tmp=numEntries; |
---|
| 104 | |
---|
| 105 | while (tmp>=subBufferSize) { |
---|
| 106 | previousSubBuffer=currentSubBuffer; |
---|
| 107 | currentSubBuffer=currentSubBuffer->nextSubBuffer; |
---|
| 108 | tmp-=subBufferSize; |
---|
| 109 | } |
---|
| 110 | if (currentSubBuffer==NULL) { |
---|
| 111 | currentSubBuffer=new SubBuffer; |
---|
| 112 | if (firstSubBuffer==NULL) firstSubBuffer=currentSubBuffer; |
---|
| 113 | else previousSubBuffer->nextSubBuffer=currentSubBuffer; |
---|
| 114 | currentSubBuffer->nextSubBuffer=NULL; |
---|
| 115 | } |
---|
| 116 | index=tmp; |
---|
| 117 | |
---|
[600] | 118 | currentSubBuffer->elements[index].allmem=NULL; |
---|
[40] | 119 | if (initFunction!=NULL) |
---|
| 120 | initFunction(&(currentSubBuffer->elements[index])); |
---|
| 121 | |
---|
| 122 | currentSubBuffer->elements[index].index=numEntries; |
---|
| 123 | ++numEntries; |
---|
| 124 | |
---|
| 125 | return ¤tSubBuffer->elements[index]; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | BUFFER_TEMPLATE |
---|
| 129 | SubBufferElement *BUFFER::getElement(IndexType index) { |
---|
| 130 | SubBuffer *currentSubBuffer=firstSubBuffer; |
---|
| 131 | |
---|
| 132 | if (index>=numEntries) fail(ADOLC_BUFFER_INDEX_TO_LARGE); |
---|
| 133 | while (index>=subBufferSize) { |
---|
| 134 | currentSubBuffer=currentSubBuffer->nextSubBuffer; |
---|
| 135 | index-=subBufferSize; |
---|
| 136 | } |
---|
| 137 | return ¤tSubBuffer->elements[index]; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | #endif /* __cplusplus */ |
---|
| 141 | |
---|
| 142 | #endif /* ADOLC_STRUCT_BUF_H */ |
---|
| 143 | |
---|