OpenMP:如何刷新指针目标?

发布于 2024-10-14 15:30:55 字数 710 浏览 6 评论 0原文

我刚刚注意到以下代码无法在 OpenMP 中编译(在 GCC 4.5.1 下):

struct job {
    unsigned busy_children;
};

job* j = allocateJob(…);
// …

#pragma omp flush(j->busy_children)

编译器抱怨参数列表中的 -> 需要刷新,并根据 OpenMP规范是正确的:flush 期望“id-表达式”列表作为参数,这基本上意味着只允许(限定的)ID,不允许表达式。

此外,规范中还提到了刷新和指针:

如果列表中存在指针,则刷新指针本身,而不是指针引用的内存块。

当然。然而,由于 OpenMP 也不允许我取消引用指针,所以我基本上无法刷新指针对象(指针目标)。

– 那么参考文献呢?规范没有提到它们,但我不确定以下内容是否一致,并且实际上会刷新指针。

unsigned& busy_children = j->busy_children;
#pragma omp flush(busy_children)

这保证有效吗?

如果没有,我怎样才能冲洗指针?

I’ve just noticed that the following code doesn’t compile in OpenMP (under GCC 4.5.1):

struct job {
    unsigned busy_children;
};

job* j = allocateJob(…);
// …

#pragma omp flush(j->busy_children)

The compiler complains about the -> in the argument list to flush, and according to the OpenMP specification it’s right: flush expects as arguments a list of “id-expression”s, which basically means only (qualified) IDs are allowed, no expressions.

Furthermore, the spec says this about flush and pointers:

If a pointer is present in the list, the pointer itself is flushed, not the memory block to which the pointer refers.

Of course. However, since OpenMP also doesn’t allow me to dereference the pointers I basically cannot flush a pointee (pointer target).

– So what about references? The spec doesn’t mention them but I’m not confident that the following is conformant, and will actually flush the pointee.

unsigned& busy_children = j->busy_children;
#pragma omp flush(busy_children)

Is this guaranteed to work?

If not, how can I flush a pointee?

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

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

发布评论

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

评论(3

小瓶盖 2024-10-21 15:30:55

长期以来,flush指令一直让OpenMP ARB头疼。以至于有人讨论要完全删除它——尽管这会带来其他问题。使用flush(list)是非常难以正确使用的,甚至OpenMP专家也很难正确使用。它的问题在于它的定义方式可以由编译器在代码中移动。这意味着您应该远离使用flush(list)。

至于您关于能够刷新指针的问题,只有一种方法可以做到这一点,那就是使用刷新(没有列表)。这将刷新您的整个线程环境,因此编译器无法移动。这看起来“很严厉”,但编译器实际上非常擅长在没有列表的情况下使用flush来刷新必要的内容。

The flush directive has given the OpenMP ARB a headache for a long time. So much so, that there has been talk about removing it completely - though that creates other problems. Using flush(list), is extremely difficult to get correct and even the OpenMP experts have a great deal of trouble getting it correct. The problem with it, is that the way it is defined it can be moved around in your code by the compiler. That means that you should stay away from using flush(list).

As for your question about being able to flush a pointee, there is only one way to do that and that is to use flush (without a list). This will flush your entire thread environment and as such, can not be moved by the compiler. It seems "heavy handed", but the compilers are actually pretty good about flushing what is necessary when using flush without a list.

纵山崖 2024-10-21 15:30:55

OpenMP 规范没有直接说明变量的类型,但 MSDN 表示“刷新指令中指定的变量不得具有引用类型”。这让我觉得这并不能保证有效。带有空变量列表的 flush 指令应该刷新所有内存,这样您就可以安全地使用它。

OpenMP specification doesn't directly says about the type of a variable, but MSDN says that "A variable specified in a flush directive must not have a reference type". This make me think that this is not guaranteed to work. The flush directive with empty variable list should flush all memory so this is what you can safely use.

撩发小公举 2024-10-21 15:30:55

无法刷新取消引用的指针的原因是因为只有硬件寄存器中的值才需要刷新。在 OpenMP 下,永远不需要刷新不在硬件寄存器中(例如,在高速缓存或内存中)的内容,因为 OpenMP 假定有一个一致的高速缓存,可以保证当相同地址时,所有线程将始终看到相同的值。取消引用。硬件协议保证缓存一致性,使多个本地缓存的行为就像一个共享的全局缓存。

The reason you cannot flush a dereferenced pointer is because flush is only needed for values in hardware registers. There is never any need under OpenMP to flush something that is NOT in a hardware register (for example, in cache or memory), since OpenMP assumes a coherent cache memory that guarantees that all threads will always see the same value when the same address is dereferenced. Hardware protocols guarantee cache coherency, making the multiple local caches behave like one shared global cache.

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