如何访问和管理块存储的数据

How to access and manage block stored data

本文关键字:管理 存储 数据 何访问 访问      更新时间:2023-10-16

我想做的事情:我需要以块形式存储单元数据,即

然后,我需要有效地访问此数据,并在可能的情况下使用不错的语法。如果我可以定义要容易存储的数据,即通过将对象定义为我想存储的数据并将其传递给一些为我完成一切的"魔术"。

,那将是很棒的。

动机:为什么我需要这样做?缓存垃圾。在某些内部循环中,只有某些对象成员可以访问。对于我的应用程序,浪费一半具有未使用内存的缓存线是一个选择。我可以将指针存储在指向某些顺序内存区域的对象中。这会浪费内存,并迫使我在该区域使用其他语法。

我目前如何做:我有一个表单的容器:

template<class T> struct Container {
  char* data;
  Container(const int n) {
    data = new char[n*T::spaceRequirements()]; //< Data stored "block-wise"
    new(data) typename T::Flags[n]; //< Flags stored "cell-wise"
  }
  /// Destructor ommited for briefness.
};

在其中存储某些T型单元格的数据。我每个单元格需要一些标志,现在我使用std :: bitset存储它们,这意味着我需要以细胞的形式存储此块:

我正在描述每个单元格需要存储多少数据,这也提供了对数据的访问:

template<int nd> struct CellAccessor {
  /// Cell flags are stored cell-wise:
  typedef std::bitset<64> Flags;
  enum { DELETE = 0, ///< Cell marked for deletion
         REFINE = 1 ///< Cell marked for refinement
         //...
  }; ///< Enum for the flags.
  static inline Flags& flags(const int cellId) {
    return *reinterpret_cast<Flags*>(data + sizeof(Flags)*cellId); }
  template<int pId> static inline Flags::reference flags(const int cellId) {
    return flags(cellId)[pId]; } //< Cell-wise access to the properties
  /// The rest of the data is stored block-wise:
  static inline int& order(const int cellId) { ///< One int field.
    return *reinterpret_cast<int*>
        (data + maxNoCells*sizeof(Flags) + sizeof(int)*cellId);}
  /// Coordinate vector with nd components:
  static inline double& coordinates(const int cellId, const int i) {
    return *reinterpret_cast<double*>
        (data + maxNoCells*(sizeof(Flags)+sizeof(int))
         + maxNoCells*i*sizeof(double) + sizeof(double)*cellId); }
  template<int i> static inline double& coordinates(const int cellId) {
    return *reinterpret_cast<double*>
        (data +maxNoCells*(sizeof(Flags)+sizeof(int)+i*sizeof(double))
         + sizeof(double)*cellId); }
  /// Total amount of memory to allocate per cell: (used by Container)
  static inline int spaceRequirements() { return
        sizeof(Flags) // Flags
        + sizeof(int) // order
        + nd*sizeof(double) // coordinates
        ;}
  /// Constructor gets pointer to the beginning of the container 
  /// and the offset for the member variables:
  CellAccessor(char* d, int n){data = d; maxNoCells = n;}
 private:
  static char* data;  ///< Pointer to the beginning of the container.
  static int maxNoCells;  ///< Cell offset for the member variables.
};
template<int nd> char* CellAccessor<nd>::data = nullptr;
template<int nd> int CellAccessor<nd>::maxNoCells = 0;

我这样使用它:

int main() {
  int maxNoCells = 10000;   ///< Maximum number of cells (=cell offset).
  typedef CellAccessor<2> A;
  Container< A > cellData(maxNoCells);  ///< Allocate cell data.
  A cells(cellData.data,maxNoCells);  ///< Provides access to cell data.
  for(int i = 0; i < maxNoCells; ++i){
    cells.flags<A::DELETE>(i) = i%2==0 ? true : false;
    cells.flags<A::REFINE>(i) = i%2==0 ? false : true;
    cells.coordinates(i,0) = i;
    cells.coordinates<1>(i) = -((double)i);
    cells.order(i) = 2;
  }
}

PROS:

  • 数据以块形式形式,这是我需要的。

  • 语法还可以。

问题:

  • 我的课程做得太多:为用户提供对数据的访问,提供用于容器需要存储多少数据,并提供如何移动/复制/复制/换取我的数据结构的数据(哪个是树...)...

  • 没有迭代器,我将无法使用STL算法。我已经通过使它们存储单元索引并重新成熟的Cellaccessor类来实现迭代器(不好!Dry!)。

  • BITSet仍以细胞形式存储。我可以为块数据结构重新实现位置...

  • 数据和maxnocell是静态变量,但是如果需要,我可以使它们成为正常的成员变量。

问题:是否有任何有效的方法来存储"对象"(或我们在概念上通过对象理解)以块形式并访问它们,就好像它们存储在std容器中一样向量?

您想要的是"基于"列"内存访问的样式

您可以使用std::vector作为列类型轻松实现它,或使用自己的基础内存管理创建自己的"列"类型 - 但是std::vector应该可以正常工作

现在,一旦您拥有列类型,就可以创建"表"类型。

在某种程度上,您的桌子驾驶室只是向量的向量。您当然可以将其包装起来以获取更好的外观登录器(如果您想通过第(Object)首先访问(object)和columt(属性)。

这是我认为最好的一般方法。

即使在您的特定情况下 - 由于您想使用位长度标志保存内存,如Bart van Ingen Schenau所述,您可以使用vector<bool>,因此一般方法站立

我将使用Parrallal Arrays满足要求:

template <int nd>
class CellAccessor {
public:
    enum { DELETE = 0, ///< Cell marked for deletion
           REFINE = 1, ///< Cell marked for refinement
           //...
           NUM_FLAGS
    }; ///< Enum for the flags.
    CellAccessor(int numCells) {
        for (int i=0; i<NUM_FLAGS; i++) { m_flags[i] = new bool[numCells]; }
        m_order = new int[numCells];
        for (int i=0; i<nd; i++) { m_coordinates[i] = new double[numCells]; }
    }
    // Destructor, copy-constructor & assignment operator omitted for brevity
    template<int F> inline bool& flags(const int cellId) {
        return m_flags[F][cellId]; }
    inline bool& flags(const int cellId, int flag) {
        return m_flags[flag][cellId]; }
    inline int& order(const int cellId) {
        return m_order[cellId]; }
    template<int i> inline double& coordinates(const int cellId) {
        return m_coordinates[i][cellId]; }
    inline double& coordinates(const int cellId, int i) {
        return m_coordinates[i][cellId]; }
private:
    bool* m_flags[NUM_FLAGS];
    int*  m_order;
    double* m_coordinates[nd];
};

不确定正确理解的问题。看来您正在尝试以一个字节的顺序数组分配数据。为什么?但是无论如何,您只需使用数组即可完成:

class Cell {
   std::bitset<64> flags;
   int order;
   double coordinates[2];
}
int main() {
   const int maxNoCells = 10000;
   Cell cells[maxNoCells];
   for(int i = 0; i < maxNoCells; i++) {
       cells[i].flags = ...;
       cells[i].coordinates[0] = i;
       cells[i].coordinates[1] = -i;
       cells[i].order=2;
   }
}

,然后在需要时将其施放为(char *)单元。您的课程将以连续的内存条件分段方式分配。您可以将其用于读/写/网。唯一的问题是您必须照顾32/64位对齐,特别是如果是在不同体系结构之间共享。

这是另一个版本,如果您坚持在单独的循环中使用不同的字段:

,则可以更好地缓存:
template<int size>
class CellAccessor {
    std::bitset<64> flags[size];
    int order[size];
    double coordinates[size][2];
 public:
    std::bitset<64> &getFlags(int id) {
        return flags[id];
    }
    int &getOrder(int id) {
        return order[id];
    }
    ...
 }

main() {
    CellAccessor<10000> ca;
    for(...i++) {
        ca.getOrder(i) = 2;
        ca.getCoordinates(i)[0] = i;
        ...
    }
 }