# Parallel Computing of Edwards--Anderson Model
## 2 Algorithm
Knowing the boundary and all possible states of the added spin \(s_{m,n}\) (the indices \(m\) and \(n\) denote the vertical and horizontal coordinates, respectively) one can calculate the minimum value of \(E\) of two subsystems with boundaries differing in the state of only one spin, by the formula  
\[E(s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n}=\\ =min\Big{(}E(s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n-1 }+\Delta E((s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n-1},s_{m,n}=-1),\\ E(s_{1},s_{2},\ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n-1}+\Delta E ((s_{1},s_{2},\ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n-1},s_{m,n}=-1)\Big{)}\\ \text{and}\\ E(s_{1},s_{2},\ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n}=\\ =min\Big{(}E(s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n-1 }+\Delta E((s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n-1},s_{m,n}=+1),\\ E(s_{1},s_{2},\ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n-1}+\Delta E ((s_{1},s_{2},\ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n-1},s_{m,n}=+1)\Big{)}. \tag{5}\]  
Here \(\Delta E\) is the energy change after spin addition, the first argument \(\Delta E\) in brackets denotes the boundary state, the second one is the state of the added spin. Let us explain for better understanding that in the expressions (4) and (5) the values of spins \(s_{1},s_{2},s_{3},\ldots,s_{n},\ldots,s_{L_{x}}\) (excepting \(s_{n}\)) have the same values for a given boundary to the left and to the right of the equality sign. In the process of calculations we run over all states the boundaries. Thus, Formula (4) will be applied \(2^{N}/2\) times (as well as (5)) to calculate \(2^{N}\) new \(E\) when adding spin at position of the column number \(n\). The minimum energy of the subsystem of the new set for one boundary state is calculated using only two values of \(E\) of the previous set, distinguished by a single spin state with the column number \(n\) (to a given column with the number \(n\) a spin is added):  
\[E(s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n-1}\qquad E(s_{1},s_{2}, \ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n-1}\\ E(s_{1},s_{2},\ldots,s_{n}=-1,\ldots,s_{L_{x}})_{m,n}\qquad E(s_{1},s_{2}, \ldots,s_{n}=+1,\ldots,s_{L_{x}})_{m,n}\]  
Thus, to calculate a new set of minimal energies, all states must be grouped into pairs differing by the spin state at position \(n\). After adding the next spin already at the n+1 column position, the configurations of the spin boundaries of each pair will differ only by the state of n + 1 spin. An example of the pairing of a set of boundary spins for a lattice with \(L_{x}=5\) for all five steps of row filling is given in Figure 2. Two-headed arrows signify data exchange (swap). One part takes up the place of the other part which occupies in turn the place of the first part. The pair energies of this set \(E\) are determined by the energies of only one pair of the previous set. The number of boundary spin states is \(2^{5}\), so the number of pairs will be \(2^{4}\). Listing 1 gives an example of pseudocode for splitting the states of the boundary into pairs. According to the diagram in Figure 2, two nested loops are needed to split the elements into pairs. In the listing, these loops are executed with the use of variables \(index\_out\) and \(index\_in\). The variables \(state1\) and \(state2\) store the state numbers of the boundary of the respective paired states. The energies of the states are stored in the array \(E[\ldots]\). It can be seen that as the column number increases, the distance between the spin numbers increases as well. Due to the fact that the links change when a spin is added, it is clear that in the parallelisation scheme one will need to do transitions of data between threads in a non-trivial way. To understand how this should be done, let's rewrite the parallelisationscheme in a different way.