## 5 The _DeepFogSim_ Toolkit: Software Architecture and Supported Resource Allocation Strategies
### Supported Resource Allocation Strategies  
In this subsection, we describe the supported resource allocation strategies provided by the _DeepFogSim_ toolkit and listed in Table 2.  
(a) The RAS function  
The _DynDef_RAS_ function implements the primal-dual adaptive iterations in (25) and (27) for the numerical evaluation of the solution of the optimization problem in (23). The goal is to perform the optimized constrained allocation of the computing and networking resources needed by the simulated hierarchical Fog platform of Figure 3 for sustaining the inference phase of the CDNN of Figure 1a running atop it. The input parameters of this function are the following four vectors: \(\left\{\vec{f}_{0},\vec{\vec{f}}_{0},\vec{\overline{R}}_{0},\vec{\overline{ \lambda}}_{0}\right\}\), which are needed for the initialization of the primal-dual iterations in (24) and (25). The values of these vectors are set by calling the main script of the _DeepFogSim_ simulator. The _DynDef_RAS_ function returns, by saving the values to the corresponding global variables, the following output variables:  
1. \([f_{mtx}]\in\mathbb{R}^{Q\times I_{MAX}}\) (bit/s): the matrix collecting the frequencies of the convolutional processors on a per-iteration basis;
2. \([\vec{f}_{mtx}]\in\mathbb{R}^{Q\times I_{MAX}}\) (bit/s): the matrix collecting the frequencies of the classifier processors on a per-iteration basis;
3. \([R_{mtx}]\in\mathbb{R}^{(M-1)\times I_{MAX}}\) (bit/s): the matrix collecting the frequencies of the network throughputs on a per-iteration basis;
4. \([\lambda_{mtx}]\in\mathbb{R}^{M\times I_{MAX}}\) (Joule): the matrix collecting the _lambda_ multipliers on a per-iteration basis;
5. \(\vec{\mathcal{E}_{TOT}}\in\mathbb{R}^{1\times I_{MAX}}\) (Joule): the vector collecting the total energy in (20) on a per-iteration basis;
6. \(\vec{\mathcal{E}_{COP}}\in\mathbb{R}^{1\times I_{MAX}}\) (Joule): the vector collecting the computing energy in (21) on a per-iteration basis; and
7. \(\vec{\mathcal{E}_{NET}}\in\mathbb{R}^{1\times I_{MAX}}\) (Joule): the vector collecting the network energy in (22) on a per-iteration basis,  
where the \(I_{MAX}\) parameter fixes the maximum number of the allowed primal-dual iterations. The last column of the previous matrices and vectors are the optimized (best) values of the allocated resources and consumed energy, respectively: \(\vec{f}^{(best)}\), \(\vec{\overline{f}}^{(best)}\), \(\vec{\overline{R}}^{(best)}\), \(\vec{\overline{\lambda}}^{(best)}\), \(\mathcal{E}_{TOT}^{(best)}\), \(\mathcal{E}_{COP}^{(best)}\), and \(\mathcal{E}_{NET}^{(best)}\), which represent the final output of the _DynDef_RAS_ function. Algorithm 1 presents a pseudo-code of the _DynDef_RAS_ function. An examination of this code points out that the asymptotic computational complexity of the _DynDef_RAS_ function scales linearly with \(I_{MAX}\). ```
function:\(DynDeF\_RAS\Big{(}\widetilde{f}_{0}\), \(\widetilde{\overline{f}}_{0}\), \(\overline{\mathcal{R}}_{0}\), \(\overline{\mathcal{\lambda}}_{0}\)). Input: The initialized values: \(\widetilde{f}_{0}\), \(\widetilde{\overline{f}}_{0}\), \(\overline{\mathcal{R}}_{0}\), \(\overline{\mathcal{\lambda}}_{0}\). Output:\(\widetilde{f}^{(best)}\), \(\widetilde{f}^{(best)}\), and \(\widetilde{R}^{(best)}\) vectors of the best resource allocation; scalar total energy \(\mathcal{E}_{TOT}^{(MAX)}\) (Joule), scalar computing energy \(\mathcal{E}_{COP}^{(MAX)}\) (Joule), and network energy \(\mathcal{E}_{NET}^{(MAX)}\) (Joule) consumed under the optimized allocation vectors.