FVariadicParticleContainer.hpp 6.68 KB
Newer Older
1 2 3
#ifndef FVARIADICPARTICLECONTAINER_H
#define FVARIADICPARTICLECONTAINER_H

4 5 6
#include "Utils/FGlobal.hpp"

#include "Utils/FPoint.hpp"
7 8 9 10 11
#include "Utils/variadic_container.hpp"
#include "Utils/FAlignedAllocator.hpp"


namespace scalfmm {
12
namespace details {
13

14 15 16 17 18 19 20
/**
 * \brief Helper to generate FVariadicParticleContainer base class
 *
 * \copydetails FVariadicParticleContainer
 */
template<typename Particle, class Allocator>
struct FVariadicParticleContainerBase {
21

22 23 24 25 26 27 28 29
    /// Expand a tuple types and get the corresponding variadic_vector
    template<typename... Ts>
    static auto expand(const std::tuple<Ts...>&)
        -> variadic_vector<Allocator, Ts...>;

    /// Type of the base class
    using type = decltype(expand(std::declval<Particle>().as_tuple()));
};
30

31
}} // close namespace scalfmm::details
32

33 34 35 36 37 38 39 40 41 42
/**
 * \brief Particle container defined from a structure
 *
 * transparently stores particles in a structure of arrays manner instead of an
 * array of structures.
 *
 * \tparam Particle  The particle descriptor to store
 * \tparam Allocator The allocator to use
 *
 * The Particle type must define an `as_tuple` method that returns a tuple
43 44
 * representation of the particle. It is expected to hold position values before
 * attribute values.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
 *
 * ~~~
 * posX, posY, poZ, attributes...
 * ~~~
 *
 *
 * *Example*:
 *
 * ~~~{.cpp}
 * struct particle_t {
 *     double x, y;
 *     float phi;
 *
 *     auto as_tuple() const {
 *         return std::make_tuple(x, y, phi);
 *     }
 * };
 *
 * FVariadicparticlecontainer<particle_t> container;
 * ~~~
 *
 */
67 68 69 70
template<typename Particle, class Allocator = FAlignedAllocator<128,char> >
class FVariadicParticleContainer
    : public scalfmm::details::FVariadicParticleContainerBase<Particle, Allocator>::type
{
71
    using FBase = typename scalfmm::details::FVariadicParticleContainerBase<Particle, Allocator>::type;
72 73 74

public:

75 76 77 78
    /// Alias to the particle type
    using particle_t = Particle;

    // Inherit contructors
79 80
    using FBase::FBase;

81 82
    /**
     * \brief Get particle count in the container
83 84 85 86 87 88 89 90
     *
     * \return Particle count
     */
    FSize getNbParticles() const {
        return FSize(FBase::size());
    }

    //DELETE
91 92
    /**
     * \brief Clears the container
93
     */
94
    [[gnu::deprecated("Use clear() method instead")]]
95 96 97 98
    void resetNumberOfParticles() {
        FBase::clear();
    }

99 100
    /**
     * \brief Push a particle in the container
101 102 103
     *
     * \param particle Particle to push
     */
104
    void push(const Particle& particle) {
105
        this->push_back(particle.as_tuple());
106 107
    }

108 109
    /**
     * \brief Push a particle in the container
110 111 112 113 114 115 116 117 118 119
     *
     * \tparam Args Parameter pack of types convertible to the Attribute type.
     *
     * \note The pack size must be at most AttributeCount, if it is less,
     * the remaining attributes are zero constructed (i.e. `Attribute(0)`
     * is called to build them).
     *
     * \param position The particle position
     * \param args The particle attributes
     */
120 121
    template<typename... Args>
    void push(const typename particle_t::position_t& position, Args... args) {
122 123 124 125 126 127 128 129 130 131 132 133 134
        using vt = typename FBase::value_type;

        constexpr auto Dim = Particle::position_t::Dim;
        constexpr int vt_size = std::tuple_size<vt>::value;
        constexpr int missing_offset = Dim + sizeof...(args);
        constexpr int missing_size = vt_size - missing_offset;

        this->push_impl(
            inria::make_index_sequence<Dim>{},
            inria::make_index_sequence<missing_size>{},
            position, args...);
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148
  template<typename Position, typename... Args, std::size_t... Is,
	   std::size_t... Js, typename... Ts >
  void push_impl(inria::index_sequence<Is...>,
		 inria::index_sequence<Js...>,
		 const Position& position,
		 Args... args)
  {
    using vt = typename FBase::value_type;
    constexpr auto Dim = Particle::position_t::Dim;
    constexpr int missing_offset = Dim + sizeof...(args);

    this->push_back(std::get<Is>(position)..., args...,
		    std::tuple_element_t<missing_offset + Js, vt>{}...);
  }
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    /**
     * \brief Get size to send the container
     *
     * \return The size of the serialised particle container in bytes
     *
     * \note Implementation is #getSavedSize_impl
     */
    FSize getSavedSize() const {
        return this->getSavedSize_impl(this->data());
    }
private:
    /**
     * \brief Implementation of #getSavedSize
     *
     * \tparam Types The container types
     *
     * \return The size of the serialised particle container in bytes
     */
    template<class... Types>
    FSize getSavedSize_impl(std::tuple<Types*...>) const {
170
        FSize tmp = sizeof(FSize);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        // Sum, for T in Types, of (sizeof(T) * this->size())
        auto l = {(tmp += sizeof(Types) * this->size(), 0)..., 0};
        (void)l;
        return tmp;
    }

public:

    /**
     * \brief Serialize particle container to buffer
     *
     * \tparam BufferWriter Buffer class
     *
     * \param buffer Buffer in which to save the container
     *
     * \note Implementation is #save_impl
     */
    template<class BufferWriter>
    void save(BufferWriter& buffer) const {
        this->save_impl(buffer, FBase::indices);
    }
private:
    /**
     * \brief Implementation of #save
     *
     * \tparam BufferWriter Buffer class
     * \tparam Is Index pack for the container types
     *
     * \note Writes #getSavedSize bytes into the buffer
     */
    template<class BufferWriter, std::size_t ... Is>
    void save_impl(BufferWriter& buffer, inria::index_sequence<Is...>) const {
203
        buffer << FSize(this->size());
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        // For each sub-array `s`, write `s` to the buffer
        auto l = {
            (buffer.write(std::get<Is>(this->data()), this->size()),0)...,
            0};
        (void)l;
    }

public:
    /** \brief Restore container content from a buffer
     *
     * \tparam BufferReader Buffer class
     *
     * \param buffer Buffer from which to restore the buffer
     *
     * \note Implementation is #restore_impl
     */
    template<class BufferReader>
    void restore(BufferReader& buffer) {
        this->restore_impl(buffer, FBase::indices);
223
    }
224 225 226 227 228 229 230 231 232 233 234 235 236
private:
    /**
     * \brief Implementation of #restore
     *
     * \tparam BufferReader Buffer class
     * \tparam Is Index pack for the container types
     *
     * \note Reads #getSavedSize bytes from the buffer
     */
    template<class BufferReader, std::size_t ... Is>
    void restore_impl(BufferReader& buffer, inria::index_sequence<Is...>) {
        FSize particle_count = 0;
        buffer >> particle_count;
237

238
        this->resize(particle_count);
239

240 241 242 243
        auto l = {
            (buffer.fillArray(std::get<Is>(this->data()), this->size()),0)...,
            0};
        (void)l;
244
    }
245

246 247 248 249

};

#endif /* FVARIADICPARTICLECONTAINER_H */