# Parallel Computing of Edwards--Anderson Model
## 2 Algorithm
Namely we can write the spin state numbers of the pair together (one under the other), as done in Figure 3.  
```
for(introw=0;row<Ly;row++) { intmax_step_out=(2^Lx)/2,max_step_in=1; intstate1=0,state2=max_step_in; for(intcol=0;col<Lx;col++) { for(intindex_out=0;index_out<max_step_out;index_out++) { for(intindex_in=0;index_in<max_step_in;index_in++) {  state1=index_out+max_step_in*2+index_in;  state2=state1+max_step_in;....  calc_E(...) //(Elstate1],Elstate2))>(Elstate1],Elstate2]).... } max_step_out=max_step_out/2;max_step_in=max_step_in*2; // { } }
```  
**Listing 1**Pseudocode of enumeration rows and columns, splitting states into pairs, calculating new energy minima. If we wanted variables with paired states storing energy to be in adjacent positions of memory, then we would move the data each time (while adding spin) as shown by the arrows in Figure 3. E.g., in Figure 2 in the second row the configuration 1 must interact with the configuration 3, 2 with 4 and etc. Exchanging the locations of data in memory according the arrows in the first row on the scheme of Figure 3 we get pairs 1 and 3, 2 and 4 on the adjacent cells of the memory. From the diagram in Figure 3 one can understand how to move data between threads when doing parallelisation. As the column number increases, the difference of the pairing state numbers increases. So, starting from a certain \(n_{exch}\) column number at the position of which spin must be added, it is necessary to move data between threads according to the Figure 3. One can see that in the blocks with data circled by a dotted line in the figure, the state numbers are ordered. This makes it possible to construct the following data exchange scheme. When dealing with subsystems with the number of top row spins \(n<n_{exch}\), no data is moved within the thread. The splitting of elements into pairs and energy calculation is done in Listing 1. Then, when the column number \(n\) is such that \(n\geq n_{exch}\), we do a data exchange between streams so that the paired elements are arranged as in Figure 3. The scheme of data exchange between threads will look as shown in Figure 4. It should be emphasized that the scheme in Figure 4 literally the same as one as in Figure 1:0 Pulling numbers with their arrows and lining them up in one row we get the scheme in Figure 1:1 Also each 2 areas (pair of _E_) of the memory are joined into one thread (they are enumerated). This circuit is made for 16 threads. For 16 streams 4 exchange operations will be performed to sequentially form four new subsystems. If data with a pair of \(E\) is located within one thread after data, the energy will be calculated according to the scheme of Figure 1:2 The code for calculating the energy minima within each thread is given in Listing 2. The paired elements do not change their occupying locations when adding a spin with number \(n\) horizontally such that \(n\geq n_{exch}\). The listing demonstrates this process by calculating the variables \(state1\) and \(state2\). **Listing 2. ** Pseudocode of splitting states into pairs, calculating new energy minima for subsystems with column number \(n\geq n_{exch}\). \(Nthr\) is the number of thread. _startstate_1 and _startstate_2 are the numbers of start states stored inside the memory blocks and transferred by threads. ```
intmax_step_in=(2^Lx)/Nthr/2; intstate1=0,state2=max_step_in; for(intindex_in=0;index_in<max_step_in;index_in++) { state1=startstate1+max_step_in; state2=startstate2+max_step_in;....  calc_E(...) //(Elstate1],Elstate2))=>(Elstate1],Elstate2])....
```  
Figure 1:3 Partitioning of the states of the lattice spin boundary with \(L_{x}=5\) into pairs when extending the row number \(m+1\). To complete the row, 5 spins must be added. Starting from the second row, pairing no longer takes place between adjacent memory cells. Thus, the arrows can intersect many times. The numbers under the arrows signify the locations of the memory. In turn, the each location corresponds to the given state of a spin boundary.