| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2009, 2022 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/sampler.hh" |
| 20 |
|
| 21 |
#include "music/debug.hh" |
| 22 |
|
| 23 |
#if MUSIC_USE_MPI |
| 24 |
|
| 25 |
// array_data.hh needs to be included first since it causes inclusion |
| 26 |
// of mpi.h (in data_map.hh). mpi.h must be included before other |
| 27 |
// header files on BG/L |
| 28 |
#include "music/array_data.hh" |
| 29 |
#include "music/index_map_factory.hh" |
| 30 |
#include "music/error.hh" |
| 31 |
#include "music/mpi_utils.hh" |
| 32 |
|
| 33 |
#include <cstring> |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
namespace MUSIC { |
| 38 |
|
| 39 |
Sampler::Sampler () |
| 40 |
: dataMap_ (0), interpolationDataMap_ (0) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
Sampler::~Sampler () |
| 46 |
{ |
| 47 |
if (dataMap_ != 0) |
| 48 |
delete dataMap_; |
| 49 |
if (interpolationDataMap_ != 0) |
| 50 |
delete interpolationDataMap_; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
/* |
| 55 |
* Called during port mapping |
| 56 |
*/ |
| 57 |
void |
| 58 |
Sampler::configure (DataMap* dataMap) |
| 59 |
{ |
| 60 |
dataMap_ = dataMap->copy (); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
void |
| 65 |
Sampler::initialize () |
| 66 |
{ |
| 67 |
elementSize = mpi_get_type_size (dataMap_->type ()); |
| 68 |
|
| 69 |
size = 0; |
| 70 |
IndexMap* indices = dataMap_->indexMap (); |
| 71 |
IndexMapFactory newIndices; |
| 72 |
for (IndexMap::iterator i = indices->begin (); |
| 73 |
i != indices->end (); |
| 74 |
++i) |
| 75 |
{ |
| 76 |
int localIndex = size; |
| 77 |
newIndices.add (i->begin (), i->end (), localIndex); |
| 78 |
size += i->end () - i->begin (); |
| 79 |
} |
| 80 |
newIndices.build (); |
| 81 |
|
| 82 |
prevSample_ = new ContDataT[elementSize * size]; |
| 83 |
sample_ = new ContDataT[elementSize * size]; |
| 84 |
interpolationData_ = new ContDataT[elementSize * size]; |
| 85 |
|
| 86 |
interpolationDataMap_ = new ArrayData (interpolationData_, |
| 87 |
dataMap_->type (), |
| 88 |
&newIndices); |
| 89 |
|
| 90 |
MUSIC_LOGR ("prev = " << static_cast<void*> (prevSample_) |
| 91 |
<< ", sample = " << static_cast<void*> (sample_) |
| 92 |
<< ", interp = " << static_cast<void*> (interpolationData_)); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/* |
| 97 |
* Called during configuration of interpolating connectors |
| 98 |
*/ |
| 99 |
DataMap* |
| 100 |
Sampler::interpolationDataMap () |
| 101 |
{ |
| 102 |
if (interpolationDataMap_ == 0) |
| 103 |
initialize (); |
| 104 |
|
| 105 |
return interpolationDataMap_; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
void |
| 110 |
Sampler::newSample () |
| 111 |
{ |
| 112 |
hasSampled = false; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void |
| 117 |
Sampler::sampleOnce () |
| 118 |
{ |
| 119 |
if (!hasSampled) |
| 120 |
{ |
| 121 |
sample (); |
| 122 |
hasSampled = true; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
void |
| 128 |
Sampler::sample () |
| 129 |
{ |
| 130 |
ContDataT* dest = insert (); |
| 131 |
|
| 132 |
int pos = 0; |
| 133 |
IndexMap* indices = dataMap_->indexMap (); |
| 134 |
for (IndexMap::iterator i = indices->begin (); |
| 135 |
i != indices->end (); |
| 136 |
++i) |
| 137 |
{ |
| 138 |
ContDataT* src = static_cast<ContDataT*> (dataMap_->base ()); |
| 139 |
int iSize = elementSize * (i->end () - i->begin ()); |
| 140 |
memcpy (dest + pos, |
| 141 |
src + elementSize * (i->begin () - i->local ()), |
| 142 |
iSize); |
| 143 |
pos += iSize; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
ContDataT* |
| 149 |
Sampler::insert () |
| 150 |
{ |
| 151 |
swapBuffers (sample_, prevSample_); |
| 152 |
return sample_; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
void |
| 157 |
Sampler::swapBuffers (ContDataT*& b1, ContDataT*& b2) |
| 158 |
{ |
| 159 |
ContDataT* tmp; |
| 160 |
tmp = b1; |
| 161 |
b1 = b2; |
| 162 |
b2 = tmp; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
void |
| 167 |
Sampler::interpolate (double interpolationCoefficient) |
| 168 |
{ |
| 169 |
interpolateTo (interpolationDataMap_, interpolationCoefficient); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
void |
| 174 |
Sampler::interpolateToApplication (double interpolationCoefficient) |
| 175 |
{ |
| 176 |
interpolateTo (dataMap_, interpolationCoefficient); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
void |
| 181 |
Sampler::interpolateTo (DataMap* dataMap, double interpolationCoefficient) |
| 182 |
{ |
| 183 |
int pos = 0; |
| 184 |
IndexMap* indices = dataMap->indexMap (); |
| 185 |
for (IndexMap::iterator i = indices->begin (); |
| 186 |
i != indices->end (); |
| 187 |
++i) |
| 188 |
{ |
| 189 |
int localIndex = i->begin () - i->local (); |
| 190 |
int iSize = i->end () - i->begin (); |
| 191 |
if (dataMap->type () == MPI_DOUBLE) |
| 192 |
interpolate (pos, iSize, interpolationCoefficient, |
| 193 |
static_cast<double*> (dataMap->base ()) + localIndex); |
| 194 |
else if (dataMap->type () == MPI_FLOAT) |
| 195 |
interpolate (pos, iSize, interpolationCoefficient, |
| 196 |
static_cast<float*> (dataMap->base ()) + localIndex); |
| 197 |
else |
| 198 |
error ("internal error in Sampler::interpolateTo"); |
| 199 |
pos += iSize; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void |
| 205 |
Sampler::interpolate (int from, |
| 206 |
int n, |
| 207 |
double interpolationCoefficient, |
| 208 |
double* dest) |
| 209 |
{ |
| 210 |
MUSIC_LOGR ("interpolate to dest = " << static_cast<void*> (dest) |
| 211 |
<< ", begin = " << from |
| 212 |
<< ", length = " << n); |
| 213 |
double* prev = (static_cast<double*> (static_cast<void*> (prevSample_)) |
| 214 |
+ from); |
| 215 |
double* succ = (static_cast<double*> (static_cast<void*> (sample_)) |
| 216 |
+ from); |
| 217 |
for (int i = 0; i < n; ++i) |
| 218 |
dest[i] = ((1.0 - interpolationCoefficient) * prev[i] |
| 219 |
+ interpolationCoefficient * succ[i]); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
void |
| 224 |
Sampler::interpolate (int from, |
| 225 |
int n, |
| 226 |
float interpolationCoefficient, |
| 227 |
float* dest) |
| 228 |
{ |
| 229 |
float* prev = (static_cast<float*> (static_cast<void*> (prevSample_)) |
| 230 |
+ from); |
| 231 |
float* succ = (static_cast<float*> (static_cast<void*> (sample_)) |
| 232 |
+ from); |
| 233 |
for (int i = 0; i < n; ++i) |
| 234 |
dest[i] = ((1.0 - interpolationCoefficient) * prev[i] |
| 235 |
+ interpolationCoefficient * succ[i]); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
} |
| 240 |
#endif |