在这种情况下,我熟悉 __restrict 关键字在 C 中用于性能优化的用法,特别是 CUDA。
void Foo(const float* __restrict X, const float* __restrict Y);
我知道这个 Foo
函数有 __restrict
关键字,它向编译器表明 X 和 Y 保证指向不同的内存块。
当我们有一个指向别名限制的指针时会发生什么?
void Bar1(const float* const * __restrict X, const float* const * __restrict Y);
void Bar2(const float* const __restrict * __restrict X, const float* const __restrict * __restrict Y);
Bar1
是完全受限还是每个间接级别都需要受限,如 Bar2
所示?
哪个语法正确表明所有指针都可以利用或只读缓存?我是否需要“限制”两个指针或仅“限制”顶级变量名称?
最佳答案
Do I need to "restrict" both pointers or only the top level variable name?
限制指针的两个“级别”。
即使这对于启用非一致/只读缓存的使用不是必需的 - 这仍然是正确的选择,因为您在输入参数的描述中更加明确。您让使用您的函数的人更清楚,他们不应将内部指针指向重叠位置。
关于c - 指向指针别名的指针和 restrict 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66646856/