# The three shared memory read and write rules

In this step, synchronizations are explained with built-in or explicit pairs of local memory fences and three read and write rules.

In this step, you will see that such synchronizations together with built in or explicit pair of local memory fences, there are three read and write rules provided.

Let’s define the following symbol for a synchronization:

Such synchronizations can be programmed using five different MPI RMA synchronizations.

In principle, if you want to program a synchronization from process 0 to process 1 there are 5 possibilities. The first is to use MPI_Win_post and MPI_Win_start, the second is MPI_Win_complete and MPI_Win_wait, the third option would be MPI_Win_fence (which is a synchronization in both directions), the fourth is to use a pair of MPI_Win_sync together with any other synchronization, and finally the fifth is to use MPI_Win_unlock and on the other side MPI_Win_lock, where you must guarantee that the lock comes after the unlock and not in the wrong order, as shown in the following figure:

If you do any of these 5 synchronizations, three rules are guaranteed:

The first rule is the write-read rule:
If you write a value val_1 into the variable A, then do one of the synchronizations and then load the variable A in the other process, you are guaranteed to load val_1. This is exactly what you need for our small example with the shared variable X.

The second guaranteed rule is the read-write rule. This means that you load variable B in process 0, then you do the synchronization and finally you overwrite B in process 1.

This overwriting of B by val_2 will not influence the loading of B before the synchronization, so you cannot accidentally load val_2 from B in process 0.

The third rule is the write-write rule. If you write the variable C on both sides and then load the C again in process 1, then you are guaranteed to get val_4 and not val_3.

In the next step, we will show an example of how you may really need these rules in your application. All these rules are important for correct shared memory programming. This example will show the write-read and the read-write rule together with any-process-synchronizations and pairs of MPI_Win_sync.