| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2009 INCF |
| 4 |
* |
| 5 |
* MUSIC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* MUSIC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "music/BIFO.hh" |
| 20 |
|
| 21 |
#include "music/debug.hh" |
| 22 |
|
| 23 |
#if MUSIC_USE_MPI |
| 24 |
|
| 25 |
#include <cstring> |
| 26 |
#include "music/error.hh" |
| 27 |
|
| 28 |
namespace MUSIC { |
| 29 |
|
| 30 |
void |
| 31 |
BIFO::configure (int elementSize, int maxBlockSize) |
| 32 |
{ |
| 33 |
elementSize_ = elementSize; |
| 34 |
maxBlockSize_ = maxBlockSize; |
| 35 |
size = maxBlockSize_; |
| 36 |
buffer.resize (size); |
| 37 |
beginning = 0; |
| 38 |
end = 0; |
| 39 |
current = 0; |
| 40 |
top = 0; |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
// Duplicate the single element in the buffer to a total of nElements |
| 46 |
// 0 is allowed as argument in which case the buffer is emptied |
| 47 |
void |
| 48 |
BIFO::fill (int nElements) |
| 49 |
{ |
| 50 |
int neededSize = nElements * elementSize_; |
| 51 |
if (neededSize > size) |
| 52 |
grow (neededSize); |
| 53 |
|
| 54 |
if (end - current != elementSize_) |
| 55 |
error ("internal error: BIFO in erroneous state before fill"); |
| 56 |
|
| 57 |
beginning = 0; |
| 58 |
|
| 59 |
if (nElements == 0) |
| 60 |
{ |
| 61 |
end = 0; |
| 62 |
current = 0; |
| 63 |
top = 0; |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Here we use the assumption that vector memory is contiguous |
| 68 |
// Josuttis says this is the intention of STL even though the |
| 69 |
// first version of the report is not clear about this. |
| 70 |
|
| 71 |
if (current != 0) |
| 72 |
{ |
| 73 |
memcpy (&buffer[0], &buffer[current], elementSize_); |
| 74 |
current = 0; |
| 75 |
} |
| 76 |
|
| 77 |
top = elementSize_; |
| 78 |
end = nElements * elementSize_; |
| 79 |
while (top < end) |
| 80 |
{ |
| 81 |
memcpy (&buffer[top], &buffer[0], elementSize_); |
| 82 |
top += elementSize_; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
bool |
| 88 |
BIFO::isEmpty () |
| 89 |
{ |
| 90 |
return current == end; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
void* |
| 95 |
BIFO::insertBlock () |
| 96 |
{ |
| 97 |
beginning = end; // set insertion point to end of last block |
| 98 |
if (current <= end) // reading below inserting? |
| 99 |
{ |
| 100 |
// Inserting above current data |
| 101 |
|
| 102 |
|
| 103 |
// Is it time to wrap around the insertion point? This should |
| 104 |
// only be done if we can fit a maximal block below current |
| 105 |
// (current > maxBlockSize_). We need to use > since a |
| 106 |
// maximal block otherwise could cause an empty buffer. |
| 107 |
if (current > maxBlockSize_) |
| 108 |
beginning = 0; // Wrap around! |
| 109 |
else if (beginning + maxBlockSize_ > size) // Need to enlarge? |
| 110 |
// (The peculiar choice of growing just as much as we need |
| 111 |
// is made since buffers can be VERY large in large scale |
| 112 |
// simulations.) |
| 113 |
grow (beginning + maxBlockSize_); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
// Inserting below current data |
| 118 |
if (current - beginning <= maxBlockSize_) // Too tight? |
| 119 |
{ |
| 120 |
// delta is how much more space we need + epsilon (8 to be |
| 121 |
// nice to memory) |
| 122 |
int delta = maxBlockSize_ - (current - beginning) + 8; |
| 123 |
if (size - top < delta) // Need to enlarge? |
| 124 |
// (The peculiar choice of growing just as much as we need |
| 125 |
// is made since buffers can be VERY large in large scale |
| 126 |
// simulations.) |
| 127 |
grow (size + delta); |
| 128 |
// Now move entire upper chunk of data: |
| 129 |
memmove (&buffer[current + delta], &buffer[current], |
| 130 |
top - current); |
| 131 |
current += delta; |
| 132 |
top += delta; |
| 133 |
} |
| 134 |
} |
| 135 |
return static_cast<void*> (&buffer[beginning]); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void |
| 140 |
BIFO::trimBlock (int blockSize) |
| 141 |
{ |
| 142 |
if (blockSize > 0) |
| 143 |
{ |
| 144 |
end = beginning + blockSize; |
| 145 |
if (end > size) |
| 146 |
{ |
| 147 |
std::ostringstream msg; |
| 148 |
msg << "BIFO buffer overflow; received " |
| 149 |
<< blockSize / elementSize_ |
| 150 |
<< ", room " << (size - beginning) / elementSize_ |
| 151 |
<< ", permitted " << maxBlockSize_ / elementSize_; |
| 152 |
error (msg); |
| 153 |
} |
| 154 |
if (current <= end) |
| 155 |
top = end; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
void* |
| 161 |
BIFO::next () |
| 162 |
{ |
| 163 |
if (isEmpty ()) |
| 164 |
{ |
| 165 |
MUSIC_LOGR ("attempt to read from empty BIFO buffer"); |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
if (current == top) |
| 170 |
{ |
| 171 |
// wrap around |
| 172 |
current = 0; |
| 173 |
top = end; |
| 174 |
} |
| 175 |
|
| 176 |
// Here we use the assumption that vector memory is contiguous |
| 177 |
// Josuttis says this is the intention of STL even though the |
| 178 |
// first version of the report is not clear about this. |
| 179 |
void* memory = static_cast<void*> (&buffer[current]); |
| 180 |
/* remedius |
| 181 |
* could it be possible that elementSize > size of received data? no |
| 182 |
* changed from current+=elementSize_ to the following line: |
| 183 |
*/ |
| 184 |
current += std::min(elementSize_,top-current); |
| 185 |
return memory; |
| 186 |
} |
| 187 |
|
| 188 |
void |
| 189 |
BIFO::grow (int newSize) |
| 190 |
{ |
| 191 |
size = newSize; |
| 192 |
buffer.resize (size); |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
#endif |