# Memory Consistency Model (strong or weak)

**URL:** <https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226>\
**Category:** CUDA Programming and Performance\
**Created:** [May 1, 2009, 5:57am UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226 "2009-05-01T05:57:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kuroi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Kuroi](https://forums.developer.nvidia.com/u/Kuroi)\
**Post date:** [May 1, 2009, 5:57am UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/1 "2009-05-01T05:57:20Z")

</div>

I have seen this topic before: [url=“[http://forums.nvidia.com/lofiversion/index.php?t28336.html](http://forums.nvidia.com/lofiversion/index.php?t28336.html)”][http://forums.nvidia.com/lofiversion/index.php?t28336.html[/url]](http://forums.nvidia.com/lofiversion/index.php?t28336.html%5B/url%5D) but I think it has not been answered…

Does CUDA support a strong or weak memory consistency model?

In other words, does it support a sequentially consistent memory model or not?

---

<div class="post-metadata">

**Author:** ![Smokey](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Smokey](https://forums.developer.nvidia.com/u/Smokey)\
**Post date:** [May 1, 2009, 6:19am UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/2 "2009-05-01T06:19:12Z")

</div>

> [@](#):
>
> I have seen this topic before: http://forums.nvidia.com/lofiversion/index.php?t28336.html but I think it has not been answered…
> 
> Does CUDA support a strong or weak memory consistency model?
> 
> In other words, does it support a sequentially consistent memory model or not?

I must admit I’ve never even heard of these memory consistency models before now (I’m guessing they apply more to distributed programming, than to parallel programming) - but I’d say CUDA is a mix of a weak memory consistency model and a sequential consitency model depending on what type of memory you’re referring to (shared, constant, texture, and global memory all behave differently, have different rules, and in some cases can or cannot be guaranteed to be visible to other threads and/or blocks and/or kernels depending on various circumstances).

Honestly though, it sounds like you haven’t read the CUDA programming guide - which will answer all of your questions, and more.

---

<div class="post-metadata">

**Author:** ![Kuroi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Kuroi](https://forums.developer.nvidia.com/u/Kuroi)\
**Post date:** [May 1, 2009, 3:14pm UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/3 "2009-05-01T15:14:41Z")

</div>

> [@](#):
>
> I must admit I’ve never even heard of these memory consistency models before now (I’m guessing they apply more to distributed programming, than to parallel programming) - but I’d say CUDA is a mix of a weak memory consistency model and a sequential consitency model depending on what type of memory you’re referring to (shared, constant, texture, and global memory all behave differently, have different rules, and in some cases can or cannot be guaranteed to be visible to other threads and/or blocks and/or kernels depending on various circumstances).
> 
> Honestly though, it sounds like you haven’t read the CUDA programming guide - which will answer all of your questions, and more.

Thanks for your answer. I have been reading the CUDA programming guide and could not find an answer… that is why I decided to post this topic.

Maybe I should have put an example:

Imagine you have 2 variables _x_ and _y_ that are initially 0, then:

Thread1 execute: x=1; a=y; (written in that order)

Thread2 execute: y=1; b=x; (written in that order)

In a **sequentially consistent memory model** , after you execute the code, it would be **imposible** to find that _a_ and _b_ are **both** equal to 0. You may get one of the following after the execution:

a=1

b=0

or

a=0

b=1

or

a=1

b=1

The last case would mean that Thread1 and Thread2 executed the code at exactly the same time (completely in parallel [External Media](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/thumbup.gif "Media hosted on another site. Click to open in a new tab."))

This would be a **strong memory consistency model**.

On the other hand, there are some systems were it is **possible** to find that _a_ and _b_ are **both** equal to 0… this is because the compiler may change the order of execution (a=y; x=1; in thread1, for example) as an optimization, or because the hardware scheduler decided to do the same change to speedup the execution… anyway, this would be a **weak memory consistency model**.

My question is which model NVIDIA-GPU/CUDA support? **strong** or **weak**?

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![Jamie\_K](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jamie\_K](https://forums.developer.nvidia.com/u/Jamie_K)\
**Post date:** [May 2, 2009, 6:34am UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/4 "2009-05-02T06:34:02Z")

</div>

I believe this is the reason for \_\_threadfence(). I think in general the model is weak, but \_\_threadfence() can be used to enforce ordering when you need it.

So for example  
Thread1 execute: x=1; \_\_threadfence(); a=y;  
Thread2 execute: y=1; \_\_threadfence(); b=x;

Then a=y must occur after x=1 has been flushed to memory and is visible to other threads, and likewise b=x must occur after y=1 is visible to other threads. There is no guarantee as to which will occur first, but at least one must write before the other reads it. Assuming appropriate use of volatile.

---

<div class="post-metadata">

**Author:** ![Kuroi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Kuroi](https://forums.developer.nvidia.com/u/Kuroi)\
**Post date:** [May 2, 2009, 5:42pm UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/5 "2009-05-02T17:42:25Z")

</div>

> [@](#):
>
> I believe this is the reason for \_\_threadfence(). I think in general the model is weak, but \_\_threadfence() can be used to enforce ordering when you need it.
> 
> So for example
> 
> Thread1 execute: x=1; \_\_threadfence(); a=y;
> 
> Thread2 execute: y=1; \_\_threadfence(); b=x;
> 
> Then a=y must occur after x=1 has been flushed to memory and is visible to other threads, and likewise b=x must occur after y=1 is visible to other threads. There is no guarantee as to which will occur first, but at least one must write before the other reads it. Assuming appropriate use of volatile.

Thanks for your answer, it really helped me [External Media](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/thumbup.gif "Media hosted on another site. Click to open in a new tab.").!

---

<div class="post-metadata">

**Author:** ![Keldor314](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Keldor314](https://forums.developer.nvidia.com/u/Keldor314)\
**Post date:** [May 4, 2009, 11:53am UTC](https://forums.developer.nvidia.com/t/memory-consistency-model-strong-or-weak/9226/6 "2009-05-04T11:53:02Z")

</div>

I believe that CUDA has strong consistency within a given warp, and weak overall. Thus, if thread1 and thread2 are in the same warp, then a and b will be equal to 1. If the threads are in different warps, all bets are off.

Of course, there’s no guarantee that the compiler won’t mess it up in either case.
