Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 9c790338 authored by PIACIBELLO Cyrille's avatar PIACIBELLO Cyrille
Browse files

Tiny correction

parent 3610005a
Branches
Tags
No related merge requests found
...@@ -25,280 +25,280 @@ ...@@ -25,280 +25,280 @@
#include "FParticleType.hpp" #include "FParticleType.hpp"
/** /**
* @author Berenger Bramas (berenger.bramas@inria.fr) * @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FBasicParticle * @class FBasicParticle
* Please read the license * Please read the license
* *
* This class defines a container which can holds one type (AttributeClass) * This class defines a container which can holds one type (AttributeClass)
* for each particle. * for each particle.
* The memory is allocated for all informations, the positions and the * The memory is allocated for all informations, the positions and the
* request type. * request type.
* For example is one want to store a struct for each particle: * For example is one want to store a struct for each particle:
* @code * @code
* @code struct AStruct{ * @code struct AStruct{
* @code ... * @code ...
* @code }; * @code };
* @code FBasicParticleContainer<1, AStruct> container; * @code FBasicParticleContainer<1, AStruct> container;
* And then the access is done using: * And then the access is done using:
* @code AStruct* strucs = container.getAttributes<0>(); * @code AStruct* strucs = container.getAttributes<0>();
*/ */
template <unsigned NbAttributesPerParticle, class AttributeClass = FReal > template <unsigned NbAttributesPerParticle, class AttributeClass = FReal >
class FBasicParticleContainer : public FAbstractParticleContainer, public FAbstractSerializable { class FBasicParticleContainer : public FAbstractParticleContainer, public FAbstractSerializable {
protected: protected:
/** The number of particles in the container */ /** The number of particles in the container */
int nbParticles; int nbParticles;
/** 3 pointers to 3 arrays of real to store the position */ /** 3 pointers to 3 arrays of real to store the position */
FReal* positions[3]; FReal* positions[3];
/** The attributes requested by the user */ /** The attributes requested by the user */
AttributeClass* attributes[NbAttributesPerParticle]; AttributeClass* attributes[NbAttributesPerParticle];
/** The allocated memory */ /** The allocated memory */
int allocatedParticles; int allocatedParticles;
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
/** Ending call for pushing the attributes */ /** Ending call for pushing the attributes */
template<int index> template<int index>
void addParticleValue(const int /*insertPosition*/){ void addParticleValue(const int /*insertPosition*/){
} }
/** Filling call for each attributes values */ /** Filling call for each attributes values */
template<int index, typename... Args> template<int index, typename... Args>
void addParticleValue(const int insertPosition, const AttributeClass value, Args... args){ void addParticleValue(const int insertPosition, const AttributeClass value, Args... args){
// Compile test to ensure indexing // Compile test to ensure indexing
static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope."); static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope.");
// insert the value // insert the value
attributes[index][insertPosition] = value; attributes[index][insertPosition] = value;
// Continue for reamining values // Continue for reamining values
addParticleValue<index+1>( insertPosition, args...); addParticleValue<index+1>( insertPosition, args...);
} }
public: public:
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
FBasicParticleContainer(const FBasicParticleContainer&) = delete; FBasicParticleContainer(const FBasicParticleContainer&) = delete;
FBasicParticleContainer& operator=(const FBasicParticleContainer&) = delete; FBasicParticleContainer& operator=(const FBasicParticleContainer&) = delete;
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
/** Basic contructor */ /** Basic contructor */
FBasicParticleContainer() : nbParticles(0), allocatedParticles(0){ FBasicParticleContainer() : nbParticles(0), allocatedParticles(0){
memset(positions, 0, sizeof(positions[0]) * 3); memset(positions, 0, sizeof(positions[0]) * 3);
memset(attributes, 0, sizeof(attributes[0]) * NbAttributesPerParticle); memset(attributes, 0, sizeof(attributes[0]) * NbAttributesPerParticle);
} }
/** Simply dalloc the memory using first pointer /** Simply dalloc the memory using first pointer
*/ */
~FBasicParticleContainer(){ ~FBasicParticleContainer(){
FAlignedMemory::Dealloc16BAligned(positions[0]); FAlignedMemory::Dealloc32BAligned(positions[0]);
} }
/** /**
* @brief getNbParticles * @brief getNbParticles
* @return the number of particles * @return the number of particles
*/ */
int getNbParticles() const{ int getNbParticles() const{
return nbParticles; return nbParticles;
} }
/** /**
* @brief reset the number of particles * @brief reset the number of particles
* @warning Only the number of particles is set to 0, the particles are still here. * @warning Only the number of particles is set to 0, the particles are still here.
*/ */
void resetNumberOfParticles() void resetNumberOfParticles()
{ {
nbParticles = 0 ; nbParticles = 0 ;
} }
/** /**
* @brief getPositions * @brief getPositions
* @return a FReal*[3] to get access to the positions * @return a FReal*[3] to get access to the positions
*/ */
const FReal*const* getPositions() const { const FReal*const* getPositions() const {
return positions; return positions;
} }
/** /**
* @brief getWPositions * @brief getWPositions
* @return get the position in write mode * @return get the position in write mode
*/ */
FReal* const* getWPositions() { FReal* const* getWPositions() {
return positions; return positions;
}
/**
* @brief getAttribute
* @param index
* @return the attribute at index index
*/
AttributeClass* getAttribute(const int index) {
return attributes[index];
}
/**
* @brief getAttribute
* @param index
* @return
*/
const AttributeClass* getAttribute(const int index) const {
return attributes[index];
}
/**
* Get the attribute with a forcing compile optimization
*/
template <int index>
AttributeClass* getAttribute() {
static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope.");
return attributes[index];
}
/**
* Get the attribute with a forcing compile optimization
*/
template <int index>
const AttributeClass* getAttribute() const {
static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope.");
return attributes[index];
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/**
* Push called bu FSimpleLeaf
* Should have a particle position fallowed by attributes
*/
template<typename... Args>
void push(const FPoint& inParticlePosition, Args... args){
// enought space?
if( nbParticles == allocatedParticles ){
// allocate memory
const int moduloParticlesNumber = (32/sizeof(FReal)); // We want to be rounded to 32B
allocatedParticles = (FMath::Max(10,int(FReal(nbParticles+1)*1.5)) + moduloParticlesNumber - 1) & ~(moduloParticlesNumber-1);
// init with 0
const size_t allocatedBytes = (sizeof(FReal)*3 + sizeof(AttributeClass)*NbAttributesPerParticle)*allocatedParticles;
FReal* newData = reinterpret_cast<FReal*>(FAlignedMemory::Allocate32BAligned(allocatedBytes));
memset( newData, 0, allocatedBytes);
// copy memory
const char*const toDelete = reinterpret_cast<const char*>(positions[0]);
for(int idx = 0 ; idx < 3 ; ++idx){
memcpy(newData + (allocatedParticles * idx), positions[idx], sizeof(FReal) * nbParticles);
positions[idx] = newData + (allocatedParticles * idx);
}
// copy attributes
AttributeClass* startAddress = reinterpret_cast<AttributeClass*>(positions[2] + allocatedParticles);
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
memcpy(startAddress + (allocatedParticles * idx), attributes[idx], sizeof(AttributeClass) * nbParticles);
attributes[idx] = startAddress + (idx * allocatedParticles);
}
// delete old
FAlignedMemory::Dealloc32BAligned(toDelete);
} }
// insert particle data
/** positions[0][nbParticles] = inParticlePosition.getX();
* @brief getAttribute positions[1][nbParticles] = inParticlePosition.getY();
* @param index positions[2][nbParticles] = inParticlePosition.getZ();
* @return the attribute at index index // insert attribute data
*/ addParticleValue<0>( nbParticles, args...);
AttributeClass* getAttribute(const int index) { nbParticles += 1;
return attributes[index]; }
/**
* Push called usually by FTypedLeaf with the isTarget flag in addition
*/
template<typename... Args>
void push(const FPoint& inParticlePosition, const FParticleType /*particleType*/, Args... args){
push(inParticlePosition, args...);
}
/** set nb particles to 0 */
void clear(){
nbParticles = 0;
}
/** to enable rearranging
* indexesToRemove must be sorted
* it removes all the particles at position indexesToRemove
*/
void removeParticles(const int indexesToRemove[], const int nbParticlesToRemove){
int offset = 1;
int idxIndexes = 1;
int idxIns = indexesToRemove[0] + 1;
for( ; idxIns < nbParticles && idxIndexes < nbParticlesToRemove ; ++idxIns){
if( idxIns == indexesToRemove[idxIndexes] ){
idxIndexes += 1;
offset += 1;
}
else{
for(int idx = 0 ; idx < 3 ; ++idx){
positions[idx][idxIns-offset] = positions[idx][idxIns];
}
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
attributes[idx][idxIns-offset] = attributes[idx][idxIns];
}
}
} }
for( ; idxIns < nbParticles ; ++idxIns){
/** for(int idx = 0 ; idx < 3 ; ++idx){
* @brief getAttribute positions[idx][idxIns-offset] = positions[idx][idxIns];
* @param index }
* @return for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
*/ attributes[idx][idxIns-offset] = attributes[idx][idxIns];
const AttributeClass* getAttribute(const int index) const { }
return attributes[index];
} }
nbParticles -= nbParticlesToRemove;
/** }
* Get the attribute with a forcing compile optimization
*/ /////////////////////////////////////////////////////
template <int index> /////////////////////////////////////////////////////
AttributeClass* getAttribute() {
static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope."); /** The size to send a leaf */
return attributes[index]; int getSavedSize() const{
return int(sizeof(nbParticles) + nbParticles * (3 * sizeof(FReal) + NbAttributesPerParticle * sizeof(AttributeClass)));
}
/** Save the current cell in a buffer */
template <class BufferWriterClass>
void save(BufferWriterClass& buffer) const{
buffer << nbParticles;
for(int idx = 0 ; idx < 3 ; ++idx){
buffer.write(positions[idx], nbParticles);
} }
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
/** buffer.write(attributes[idx], nbParticles);
* Get the attribute with a forcing compile optimization
*/
template <int index>
const AttributeClass* getAttribute() const {
static_assert(index < NbAttributesPerParticle, "Index to get attributes is out of scope.");
return attributes[index];
} }
}
///////////////////////////////////////////////////// /** Restore the current cell from a buffer */
///////////////////////////////////////////////////// template <class BufferReaderClass>
void restore(BufferReaderClass& buffer){
/** buffer >> nbParticles;
* Push called bu FSimpleLeaf if( nbParticles >= allocatedParticles ){
* Should have a particle position fallowed by attributes // allocate memory
*/ const int moduloParticlesNumber = (32/sizeof(FReal)); // We want to be rounded to 32B
template<typename... Args> allocatedParticles = (FMath::Max(10,int(FReal(nbParticles+1)*1.5)) + moduloParticlesNumber - 1) & ~(moduloParticlesNumber-1);
void push(const FPoint& inParticlePosition, Args... args){ // init with 0
// enought space? const size_t allocatedBytes = (sizeof(FReal)*3 + sizeof(AttributeClass)*NbAttributesPerParticle)*allocatedParticles;
if( nbParticles == allocatedParticles ){ FReal* newData = reinterpret_cast<FReal*>(FAlignedMemory::Allocate32BAligned(allocatedBytes));
// allocate memory memset( newData, 0, allocatedBytes);
const int moduloParticlesNumber = (16/sizeof(FReal)); // We want to be rounded to 16B
allocatedParticles = (FMath::Max(10,int(FReal(nbParticles+1)*1.5)) + moduloParticlesNumber - 1) & ~(moduloParticlesNumber-1); FAlignedMemory::Dealloc32BAligned(positions[0]);
// init with 0 for(int idx = 0 ; idx < 3 ; ++idx){
const size_t allocatedBytes = (sizeof(FReal)*3 + sizeof(AttributeClass)*NbAttributesPerParticle)*allocatedParticles; positions[idx] = newData + (allocatedParticles * idx);
FReal* newData = reinterpret_cast<FReal*>(FAlignedMemory::Allocate16BAligned(allocatedBytes)); }
memset( newData, 0, allocatedBytes); AttributeClass* startAddress = reinterpret_cast<AttributeClass*>(positions[2] + allocatedParticles);
// copy memory for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
const char*const toDelete = reinterpret_cast<const char*>(positions[0]); attributes[idx] = startAddress + (idx * allocatedParticles);
for(int idx = 0 ; idx < 3 ; ++idx){ }
memcpy(newData + (allocatedParticles * idx), positions[idx], sizeof(FReal) * nbParticles);
positions[idx] = newData + (allocatedParticles * idx);
}
// copy attributes
AttributeClass* startAddress = reinterpret_cast<AttributeClass*>(positions[2] + allocatedParticles);
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
memcpy(startAddress + (allocatedParticles * idx), attributes[idx], sizeof(AttributeClass) * nbParticles);
attributes[idx] = startAddress + (idx * allocatedParticles);
}
// delete old
FAlignedMemory::Dealloc16BAligned(toDelete);
}
// insert particle data
positions[0][nbParticles] = inParticlePosition.getX();
positions[1][nbParticles] = inParticlePosition.getY();
positions[2][nbParticles] = inParticlePosition.getZ();
// insert attribute data
addParticleValue<0>( nbParticles, args...);
nbParticles += 1;
}
/**
* Push called usually by FTypedLeaf with the isTarget flag in addition
*/
template<typename... Args>
void push(const FPoint& inParticlePosition, const FParticleType /*particleType*/, Args... args){
push(inParticlePosition, args...);
} }
for(int idx = 0 ; idx < 3 ; ++idx){
/** set nb particles to 0 */ buffer.fillArray(positions[idx], nbParticles);
void clear(){
nbParticles = 0;
} }
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
/** to enable rearranging buffer.fillArray(attributes[idx], nbParticles);
* indexesToRemove must be sorted
* it removes all the particles at position indexesToRemove
*/
void removeParticles(const int indexesToRemove[], const int nbParticlesToRemove){
int offset = 1;
int idxIndexes = 1;
int idxIns = indexesToRemove[0] + 1;
for( ; idxIns < nbParticles && idxIndexes < nbParticlesToRemove ; ++idxIns){
if( idxIns == indexesToRemove[idxIndexes] ){
idxIndexes += 1;
offset += 1;
}
else{
for(int idx = 0 ; idx < 3 ; ++idx){
positions[idx][idxIns-offset] = positions[idx][idxIns];
}
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
attributes[idx][idxIns-offset] = attributes[idx][idxIns];
}
}
}
for( ; idxIns < nbParticles ; ++idxIns){
for(int idx = 0 ; idx < 3 ; ++idx){
positions[idx][idxIns-offset] = positions[idx][idxIns];
}
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
attributes[idx][idxIns-offset] = attributes[idx][idxIns];
}
}
nbParticles -= nbParticlesToRemove;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/** The size to send a leaf */
int getSavedSize() const{
return int(sizeof(nbParticles) + nbParticles * (3 * sizeof(FReal) + NbAttributesPerParticle * sizeof(AttributeClass)));
}
/** Save the current cell in a buffer */
template <class BufferWriterClass>
void save(BufferWriterClass& buffer) const{
buffer << nbParticles;
for(int idx = 0 ; idx < 3 ; ++idx){
buffer.write(positions[idx], nbParticles);
}
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
buffer.write(attributes[idx], nbParticles);
}
}
/** Restore the current cell from a buffer */
template <class BufferReaderClass>
void restore(BufferReaderClass& buffer){
buffer >> nbParticles;
if( nbParticles >= allocatedParticles ){
// allocate memory
const int moduloParticlesNumber = (16/sizeof(FReal)); // We want to be rounded to 16B
allocatedParticles = (FMath::Max(10,int(FReal(nbParticles+1)*1.5)) + moduloParticlesNumber - 1) & ~(moduloParticlesNumber-1);
// init with 0
const size_t allocatedBytes = (sizeof(FReal)*3 + sizeof(AttributeClass)*NbAttributesPerParticle)*allocatedParticles;
FReal* newData = reinterpret_cast<FReal*>(FAlignedMemory::Allocate16BAligned(allocatedBytes));
memset( newData, 0, allocatedBytes);
FAlignedMemory::Dealloc16BAligned(positions[0]);
for(int idx = 0 ; idx < 3 ; ++idx){
positions[idx] = newData + (allocatedParticles * idx);
}
AttributeClass* startAddress = reinterpret_cast<AttributeClass*>(positions[2] + allocatedParticles);
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
attributes[idx] = startAddress + (idx * allocatedParticles);
}
}
for(int idx = 0 ; idx < 3 ; ++idx){
buffer.fillArray(positions[idx], nbParticles);
}
for(unsigned idx = 0 ; idx < NbAttributesPerParticle ; ++idx){
buffer.fillArray(attributes[idx], nbParticles);
}
} }
}
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment