# Frame-Based and Subpicture-Based Parallelization Approaches of the HEVC Video Encoder
## 3 Frame-Based Parallel Algorithms
The DSM-AI algorithm, depicted in Figure 9, follows a structure that is similar to the DMG-AI algorithm, but only one MPI process is mapped into each computing node, regardless of the number of computing nodes and/or the number of available cores of each computing node. In the DSM-AI parallel algorithm, the intra-node parallelism is exploited through the fork-join thread model provided by OpenMP. The number of created threads can be specified by a fixed parameter or can be obtained during execution time, depending on the current state of the multicore processor (computing node), as shown in Algorithm 2. Figure 9: DSM-AI parallel algorithm. ```
1:In coding MPI process
2:{
3:Read parameter \(NoT\) (Number of Threads)
4:if\(NoT>0\)then
5: Send message to the coordinator process requesting \(NoT\) frames
6:elseif\(NoT=0\)then
7: Request the current optimal number of threads (\(CNoT\))
8: Set \(NoT=CNoT\)
9: Send message to the coordinator process requesting \(NoT\) frames
10:endif
11: Receive message with the number of frames to code (\(NoF\))
12:if\(NoF=0\)then
13: Finish coding procedure
14:elseif(\(NoT\geq NoF\))then
15: Create \(NoF\) threads to code \(NoF\) frames
16:else
17: Exit with error
18:endif
19: }
20:
21: In coordinator MPI process
22: {
23:repeat
24: Wait for a workload requesting message with \(NoT\) parameter
25:if Remaining frames \(>NoT\)then
26: Set \(NoF=NoT\)
27: Send message with first frame to code and \(NoF\)
28:elseif Remaining frames \(\leq NoT\)then
29: Set \(NoF=\) remaining frames
30: Send message with first frame to code and \(NoF\), intrinsically includes end of coding message
31:elseif Remaining frames \(=0\)then
32: Set \(NoF=0\)
33: Send end of coding message
34:endif
35:until Have sent end of coding message to all MPI coding processes
36: }
```  
**Algorithm 2** Setting the number of threads in the DSM-AI algorithm  
In Algorithm 2, when the parameter \"NoT\" (Number of Threads) is greater than zero, both the DSM-AI and the DMG-AI algorithms work in a similar way and their coding mapping procedures are the same, but the number of MPI communications is lower in the DSM-AI algorithm than in the DMG-AI algorithm. Moreover, no specific queuing system is required by the DSM-AI algorithm. When \"NoT\" is greater than zero, each MPI process encodes \"NoT\" consecutive frames. For that purpose, \"NoT\" threads are created in an OpenMP parallel section. On the other hand, when the parameter \"NoT\" is equal to zero, the number of threads created to encode consecutive frames depends on the current state of the multicore processor (i.e., the number of available threads). In this case, the current optimal number of threads (\"CNoT\") is requested to the system, and this number is added to the MPI message, which demands the new block of frames to be encoded. Therefore, each MPI process encodes a block of consecutive frames, but the size of these blocks is set up by the coding process instead of the coordinator process. The DSM-AI algorithm is not totally asynchronous, because the processing of the OpenMP threads is synchronous. Note that the MPI coding process sends an MPI message to the coordinator process, and this message includes the encoded data provided by all the OpenMP processes, i.e., the OpenMP parallel section must be already closed. However, the bitstream generated by the DSM-AI algorithm and by the DMG-AI algorithm, as well as the sequential algorithm, is exactly the same. The DSM-AI algorithm has been designed in order to make an efficient use of hardware platforms where other processes may be running (i.e., non-exclusive use computing platforms). However, when the optimal number of threads per computing node is a large number, the synchronization processes may reduce the parallel efficiency. Furthermore, in order to reduce the disk reading contentions, the frame reading processes are serialized. In order to solve that issue, we have developed Algorithm 3, which is used jointly with Algorithm 2. Algorithm 3 limits the number of OpenMP threads per MPI process.