C++ CUDA 应用程序的易失性和运算符重载

发布于 2024-09-19 19:37:04 字数 522 浏览 10 评论 0原文

我有一个A类,我重载了它的operator=。但是,我需要做这样的事情:

volatile A x;
A y;
x = y;

编译时引发错误

error: no operator "=" matches these operands
       operand types are: volatile A = A

如果我删除了易失性,它是可以编译的。无论如何,是否可以在不删除“易失性”的情况下进行编译(并仍然保持易失性的行为)?


基本上,这是一个 CUDA 程序,其中“x”是共享内存(所有线程都可以访问和修改其值)。我希望它是“易失性”的,以避免编译器优化并重新使用该值而不是访问内存地址。

更多关于这个问题的信息:一开始 A 只是一个原始类型,例如整数,易失性按预期工作并且不会引起任何问题,现在我希望它是一个自定义类(例如整数 128 位)。我不知道为什么 C++ 在这种情况下会抱怨,但对于原始数据类型却不会。

提前致谢。

I have a class A that I overload its operator=. However it is required that I need to do something like this:

volatile A x;
A y;
x = y;

which raised an error while compiling

error: no operator "=" matches these operands
       operand types are: volatile A = A

If I removed volatile, it's compilable. Is there anyway to have this compiled without removing the "volatile" (and still keep the behavior of volatile) ?


Basically this is a CUDA program in which 'x' is a shared memory ( all threads can access and modify its value ). I want it to be "volatile" in order to avoid the compiler optimization and re-use the value instead of accessing the memory address.

More on the problem: at the beginning A is just a primitive type e.g integer, volatile worked as expected and doesn't cause any problem, now I want it to be a custom class ( integer 128-bit for example ). I'm not sure why C++ complain in this case but not with primitive data type.

Thanks in advance.

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

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

发布评论

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

评论(4

青丝拂面 2024-09-26 19:37:05

假设 易失性 限定是必需的,您必须向 A 添加易失性赋值运算符 (A& A::operator=(const A&) 易失性)。

const_cast(x) = y 会使其编译,但从技术上讲会导致未定义的行为,并且肯定会删除 volatile 提供的保证。

Assuming the volatile qualification is necessary, you'll have to add a volatile assignment operator to A (A& A::operator=(const A&) volatile).

const_cast<A&>(x) = y will make it compile, but will technically cause undefined behaviour, and will certainly remove the guarantees that volatile gives.

“挥发性在 C++ 线程中没有大量使用”注释与该问题无关,该问题是 CUDA 特定的。 CUDA 中的 warp 同步编码需要 volatile。

The "volatile isn't a lot of use in C++ threading" comment is irrelevant to the question, which is CUDA-specific. volatile is needed for warp synchronous coding in CUDA.

柠檬色的秋千 2024-09-26 19:37:05

易失性在 C++ 线程中用处不大(请参阅 Dave Butenhof 的解释 http:// /www.lambdacs.com/cpt/FAQ.html#Q56)。仅确保您的程序将从核心本地缓存中写入的数据刷新到其他程序可以看到共享内存中的更新的程度是不够的,并且考虑到当今几乎每个人的多核,这是一个严重的问题。我建议您使用适当的线程同步方法,例如 boost 的(如果您的可移植性需要与之匹配),或者可能使用 POSIX 互斥体和条件变量,而无法使用更多依赖于体系结构的技术,例如内存屏障或在核心之间隐式同步内存的原子操作。

我确信您希望它很快,但快速和不稳定通常不如缓慢和可靠有用,特别是如果您发布的产品仅在客户的硬件上不稳定。

volatile isn't a lot of use in C++ threading (see Dave Butenhof's explanation at http://www.lambdacs.com/cpt/FAQ.html#Q56). It's not sufficient to ensure your program flushes the data written out of core-local cache to a point where other programs can see the updates in shared memory, and given almost everyone's multi-core these days, that's a serious problem. I suggest you use proper threading synchronisation methods, such as boost's if your portability needs match it, or perhaps POSIX mutexes and condition variables, failing that more architecture dependent techniques like memory barriers or atomic operations that implicitly sync memory between cores.

I'm sure you want it to be fast, but fast and unstable isn't generally as useful as slow and reliable, especially if you ship a product that's only unstable on your customer's hardware.

栩栩如生 2024-09-26 19:37:05

声明一个复制构造函数

volatile A& operator=(volatile A&) volatile;

对我来说适用于 nvcc。请注意,您可能必须仅通过引用传递非基本类型。否则,每当非基本类型按值传递给非易失性参数时,您将需要更多的复制构造函数将易失性实例转换为非易失性实例。
它实际上归结为建立易失性正确性(很像 const 正确性)。

Declaring a copy constructor

volatile A& operator=(volatile A&) volatile;

worked for me with nvcc. Note that you may have to pass around the non-primitive type by reference only. Else you'll need more copy-constructors that convert volatile instances to non-volatile whenever the non-primitive type is passed by value to a non-volatile parameter.
It really boils down to establishing volatile-correctness (much like const-correctness).

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