防止两个对象内部出现混叠
我有一个与此类似的函数签名
void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out);
在内部,矩阵类有一个表示 mx n
组件的 float* data;
。我想告诉编译器 a
和 b
不会对输出矩阵进行别名,因此它不会执行大量的加载存储。
我该怎么做呢?我知道我可以传递指向函数签名的指针,并用 __restrict 标记指针(在 MSVC 中),但我想保留通过引用传递对象的习惯用法,其中对象包含指向内存的指针。
我还知道 __restrict 不适用于对象引用。
I have a function signature similiar to this
void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out);
Internally the matrix class has a float* data;
that represents the m x n
components. I'd like to tell the compiler that a
and b
do not alias the out matrix so it doesn't do a ton of load-stores.
How would I go about doing that? I know I could pass in pointers to the function signature and mark the pointers with __restrict
(in MSVC) but I'd like to keep the idiom of object passed by reference where the object contains pointers to memory.
I also know that __restrict
does not work on object references.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据优化器的工作方式,顶部的
assert(&in1 != &out && &in2 != &out)
可能会起到作用。您还可以删除 out 参数,并相信优化器会删除多余的副本(当然假设它是纯 out 参数)。如果代码是内联的候选代码,编译器可能会发现它本身没有任何别名。如果restrict
确实对引用参数不起作用,您可以为函数调用设置一个额外的级别,并将所有三个级别传递给接受正确限制的指针的第二个函数。希望该内容能为您内联。Depending on how the optimizer works, an
assert(&in1 != &out && &in2 != &out)
at the top may do the trick. You could also get rid of the out parameter, and trust the optimizer to get rid of the superfluous copies (assuming it is a pure out parameter, of course). If the code is a candidate for inlining the compiler may see nothing is aliased on it's own. Ifrestrict
really doesn't work on reference parameters, you can have an extra level to the function call, and pass all three to a second function that accepts pointers properly restricted. Hopefully, that one would get inlined for you.编写一个非导出(文件
静态
,私有
)乘法函数,该函数采用float*
参数,用restrict<标记参数/代码>。让
Multiply
调用此函数。Write a non-exported (file-
static
,private
) multiplication function that takesfloat*
arguments, mark the arguments withrestrict
. MakeMultiply
call this function.由于您似乎对 __restrict 指针感到满意,因此我会使用您所知道的内容,但您仍然可以包装它并使用引用提供接口:
使指针版本“非公开”,例如将其放置在“详细信息”命名空间中,给它内部链接(在这种情况下不适用),或者给它一个特殊的名称。您甚至可以使用局部变量而不是参数,并将函数体放在“else”中,但我发现上面的内容更干净。
Since you seem to be comfortable with __restrict pointers, I would use what you know, but you can still wrap it and provide an interface using references:
Make the pointer version "non-public", such as placing it in a "details" namespace, giving it internal linkage (not applicable in this exact case), or giving it a special name. You could even use local variables instead of parameters and put the function body within the "else", but I find the above cleaner.
宏包装器如何在编译时本身具有
__restrict
效果:(下面是伪代码,未检查):现在中间方法定义为,
最后只是在原始
Multiply
之后添加_
:因此最终效果将与您调用的完全相同:
How about a macro wrapper to have the
__restrict
effect at compile time itself: (below is pseudo code, not checked):Now the intermediate method is defined as,
And finally just add an
_
after your originalMultiply
:So final effect will be exactly same as you are calling: