是否有类似 C++ 的限制关键字之类的东西?指示 _iterators_ 没有别名
g++ 确实实现了指针的 __restrict__
,但我找不到有关迭代器的任何信息。我的总体意图是鼓励编译器对 stl 循环进行矢量化。
编辑:
即使编译器无法矢量化,__restrict__
关键字也应该能够告诉编译器循环内不需要不必要的重新加载。
g++ does implement __restrict__
for pointers, but I could not find anything about iterators. My overall intent is to encourage the compiler to vectorize stl loops.
Edit:
Even if the compiler is unable to vectorize, the __restrict__
keyword should be able to tell the compiler that no unnecessary reloads are necessary inside a loop.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你直接问题的答案。但是,编译器只能对 std::vector 的循环进行矢量化,因为它是唯一具有连续存储的容器(我认为),并且连续存储位置之间没有依赖关系(与例如
std::list
)。但我不知道如何让它这样做。更新
经过一些实验(可能与总体目标相关,也可能不相关),我发现在 ICC 中,以下内容不矢量化:
而以下内容
则 :显然,问题不在于迭代器,而在于循环构造内对 vec.end() 的调用,这显然无法被分解出来,尽管很明显循环体不会影响向量边界。
在 GCC 中,我无法对任何内容进行矢量化。这并不奇怪,因为 GCC 在发现 SSE 机会方面比 ICC 差很多。I don't know the answer to your direct question. However, the compiler would only ever be able to vectorize a loop for
std::vector
, as it's the only container (I think) that has contiguous storage, and no dependencies between successive storage locations (unlike e.g.std::list
). I don't know how to make it do so, though.Update
After some experimentation (which may or may not be relevant to the overall goal), I discovered that in ICC, the following does not vectorise:
whereas the following does:
So apparently, the problem is not so much iterators, but the call to
vec.end()
inside the loop construct, which apparently cannot be factored out, even though it's clear that the loop body doesn't affect the vector bounds.In GCC, I couldn't get anything to vectorise. This isn't surprising, because GCC is much worse than ICC at spotting SSE opportunities.采用这个 C++20 解决方案:
使用它,您可以简单地编写:
适用于任何容器。
Take this C++20-solution:
With that you could simply write:
Works with any container.