类型别名和动态分配的数组

发布于 2024-11-08 21:59:00 字数 1067 浏览 4 评论 0 原文

我正在尝试通过 blitz++ 数组库中的编译器促进自动矢量化。出于这个原因,我想展示固定长度向量块中的数组数据的视图,这些向量已经很好地向量化了。但是,我无法弄清楚类型别名规则与动态分配的数组相结合意味着什么。

这是我的想法。当前的数组由以下部分组成:

T_numtype* restrict data_;

操作是通过循环这些数据来完成的。我想做的是将该数组的另一种视图呈现为 TinyVector 数组,它是一个固定长度向量,其操作完全使用表达式模板机制进行向量化。这个想法是,L 长度数组应该是 T_numtype[L]TinyVector[L/N]。有没有一种方法可以在不违反类型规则的情况下实现这一目标?

对于静态分配的数组,

union {
  T_numtype data_[L];
  TinyVector<T_numtype, N>[L/N];
};

我能想到的最接近的方法是定义

typedef union {
  T_numtype data_[N];
  TinyVector<T_numtype, N>;
} u;
u* data_;

然后分配它

data_ = new u[L/N];

但现在我似乎已经放弃了将整个数组作为平面数组进行寻址的权利T_numtype,因此要访问特定元素,我需要执行 data_[i/N].data_[i%N],这要复杂得多。

那么,有没有办法合法地创建 T_numtype data_[L]TinyVector[L/N] 的并集,其中 L 是动态确定的大小?

(我知道还有额外的对齐问题,即 N 必须是与 TinyVector 成员的对齐方式相同的值,否则数组中将会出现空洞。)

I'm trying to facilitate automatic vectorization by the compiler in the blitz++ array library. For this reason, I'd like to present a view of the array data that is in chunks of fixed-length vectors, which are already vectorized well. However, I can't figure out what the type aliasing rules imply in conjunction with dynamically allocated arrays.

Here's the idea. An array currently consists of

T_numtype* restrict data_;

Operations are done by looping over these data. What I would like to do is present an alternative view of this array as an array of TinyVector<T_numtype, N>, which is a fixed-length vector whose operations are totally vectorized using the expression template machinery. The idea would be that a L-length array should be either T_numtype[L] or TinyVector<T_numtype, N>[L/N]. Is there a way to accomplish this without running afoul of the type alasing rules?

For a statically allocated array, one would do

union {
  T_numtype data_[L];
  TinyVector<T_numtype, N>[L/N];
};

The closest I could think of is to define

typedef union {
  T_numtype data_[N];
  TinyVector<T_numtype, N>;
} u;
u* data_;

and then allocate it with

data_ = new u[L/N];

But it seems that now I have given up my right to address the entire array as a flat array of T_numtype, so to access a particular element I would need to do data_[i/N].data_[i%N], which is a lot more complicated.

So, is there a way to legally create a union of T_numtype data_[L] and TinyVector<T_numtype, N>[L/N] where L is a dynamically determined size?

(I'm aware that there are additional alignment concerns, i.e. N must be a value that is the same as the alignment of the TinyVector member, otherwise there will be holes in the array.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的年少不轻狂 2024-11-15 21:59:00

别名很难合法化。但是,如果某些“操作是通过循环这些数据来完成的”,那么这些操作是否要求这些数据恰好是 T_numtype 的数组?

最好将数据包装在一个具有 TinyVector[L/N] 类型或什至 std::vector 类型的数据成员的类中; > 因为 L 显然是在运行时确定的,并且为那些想要将整个数据作为单个序列循环的操作公开一对迭代器。

Aliasing is hard to make legal. However, if some "operations are done by looping over these data.", do those operations require that these data are exactly an array of T_numtype?

It may be better to wrap the data in a class with one data member of type TinyVector<T_numtype, N>[L/N] or even std::vector<TinyVector<T_numtype, N> > since that L is apparently determined at runtime, and expose a pair of iterators for those operations that want to loop over the entire data as a single sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文