Skip to content

Commit

Permalink
Optimize vector with Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
lamyj committed Dec 5, 2023
1 parent 10003c5 commit d81a5b9
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/sycomore/epg/Discrete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include <xsimd/xsimd.hpp>
#include <xtensor/xview.hpp>

#include "sycomore/Array.h"
#include "sycomore/epg/Base.h"
Expand Down
5 changes: 3 additions & 2 deletions src/sycomore/epg/Discrete.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <xsimd/xsimd.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/epg/Base.h"
#include "sycomore/epg/robin_hood.h"
#include "sycomore/Quantity.h"
Expand Down Expand Up @@ -114,7 +115,7 @@ class Discrete: public Base
void bulk_motion(Quantity const & duration, Quantity const & gradient);

private:
using Orders = std::vector<long long, xsimd::aligned_allocator<long long, 64>>;
using Orders = Buffer<long long>;
Quantity _bin_width;
Orders _orders;

Expand All @@ -130,7 +131,7 @@ class Discrete: public Base
std::vector<Model::Population> F, F_star, Z;

// Diffusion-related data.
std::vector<Real, xsimd::aligned_allocator<Real, 64>> k;
Buffer<Real> k;

Cache(std::size_t pools);

Expand Down
3 changes: 2 additions & 1 deletion src/sycomore/epg/Discrete3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <xtensor/xio.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/epg/Base.h"
#include "sycomore/epg/robin_hood.h"
#include "sycomore/epg/operators.h"
Expand Down Expand Up @@ -386,7 +387,7 @@ ::bin_width() const

Discrete3D::Cache
::Cache(std::size_t pools)
: orders(0), F(pools), F_star(pools), Z(pools)
: orders(0), F(pools), F_star(pools), Z(pools), k(3)
{
// Nothing else.
}
Expand Down
7 changes: 4 additions & 3 deletions src/sycomore/epg/Discrete3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <xtensor/xarray.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/epg/Base.h"
#include "sycomore/epg/robin_hood.h"
#include "sycomore/Quantity.h"
Expand Down Expand Up @@ -121,7 +122,7 @@ class Discrete3D: public Base

private:
using Bin = std::array<int64_t, 3>;
using Orders = std::vector<Bin::value_type, xsimd::aligned_allocator<Bin::value_type, 64>>;
using Orders = Buffer<Bin::value_type>;
Orders _orders;

Quantity _bin_width;
Expand All @@ -130,7 +131,7 @@ class Discrete3D: public Base
class Cache
{
public:
using RealVector = std::vector<Real, xsimd::aligned_allocator<Real, 64>>;
using RealVector = Buffer<Real>;

// Shift-related data.
// Mapping between a normalized (i.e. folded) order and its location in
Expand All @@ -140,7 +141,7 @@ class Discrete3D: public Base
std::vector<Model::Population> F, F_star, Z;

// Diffusion-related data.
std::vector<RealVector, xsimd::aligned_allocator<RealVector, 64>> k{3};
std::vector<RealVector, xsimd::aligned_allocator<RealVector, 64>> k;
RealVector b_L_D;
RealVector b_T_plus_D;
RealVector b_T_minus_D;
Expand Down
6 changes: 3 additions & 3 deletions src/sycomore/epg/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ ::_initialize(Vector3R const & M0, std::size_t initial_size)
{
for(auto & item: this->F)
{
item.resize(initial_size);
item.resize(initial_size, 0);
}
for(auto & item: this->F_star)
{
item.resize(initial_size);
item.resize(initial_size, 0);
}
for(auto & item: this->Z)
{
item.resize(initial_size);
item.resize(initial_size, 0);
}

auto const M_plus = Complex(M0[0], M0[1]);
Expand Down
4 changes: 2 additions & 2 deletions src/sycomore/epg/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <xsimd/xsimd.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/Quantity.h"
#include "sycomore/Species.h"
#include "sycomore/sycomore.h"
Expand All @@ -21,8 +22,7 @@ class Model
{
public:
/// @brief Populations of the states
using Population = std::vector<
Complex, xsimd::aligned_allocator<Complex, 64>>;
using Population = Buffer<Complex>;

/// @brief Kind of the model, determines the number of pools
enum Kind { SinglePool, Exchange, MagnetizationTransfer };
Expand Down
10 changes: 5 additions & 5 deletions src/sycomore/epg/Regular.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "Regular.h"

#include <cmath>
#include <vector>

#include <xsimd/xsimd.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/epg/Base.h"
#include "sycomore/epg/operators.h"
#include "sycomore/epg/simd_api.h"
Expand Down Expand Up @@ -282,7 +282,7 @@ ::bulk_motion(Quantity const & duration, Quantity const & gradient)
return;
}

std::vector<Real, xsimd::aligned_allocator<Real, 64>> k(this->size());
Buffer<Real> k(this->size());
for(std::size_t i=0; i<k.size(); ++i)
{
k[i] = delta_k*i;
Expand Down Expand Up @@ -328,9 +328,9 @@ ::_shift(int n)
auto & Z = this->_model.Z[pool];
if(size >= F.size())
{
F.resize(F.size()+100, 0);
F_star.resize(F_star.size()+100, 0);
Z.resize(Z.size()+100, 0);
F.resize(F.size()*2, 0);
F_star.resize(F_star.size()*2, 0);
Z.resize(Z.size()*2, 0);
}

if(n == +1)
Expand Down
5 changes: 2 additions & 3 deletions src/sycomore/epg/Regular.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#ifndef _fbf381fe_fd75_427e_88de_a033418c943c
#define _fbf381fe_fd75_427e_88de_a033418c943c

#include <vector>

#include <xsimd/xsimd.hpp>

#include "sycomore/Array.h"
#include "sycomore/Buffer.h"
#include "sycomore/epg/Base.h"
#include "sycomore/Quantity.h"
#include "sycomore/Species.h"
Expand Down Expand Up @@ -145,7 +144,7 @@ class Regular: public Base
{
public:
// Diffusion-related data.
std::vector<Real, xsimd::aligned_allocator<Real, 64>> k;
Buffer<Real> k;

void update_diffusion(std::size_t size, Real unit_dephasing);
};
Expand Down

0 comments on commit d81a5b9

Please sign in to comment.