# Enabling Heterogeneous IoT Networks over 5G Networks with Ultra-Dense Deployment--Using MEC/SDN
## 4 Energy-Aware and Latency-Aware Offloading Scheme
### Energy-Aware and Latency-Aware Offloading Algorithm for IoT Networks
Tasks that cannot be executed by the end-devices are offloaded via the appropriate communication link to the I-MEC server connected to the IoT gateway. The I-MEC server accepts the offloading process or rejects it based on the currently available resources at the I-MEC server and the required QoS of the offloaded task. If the currently available resources of the I-MEC server are not enough to handle the offloaded task, the task is then offloaded to the higher level, i.e., the second level of offloading. The second offloading level is represented by the cellular MEC server, i.e., C-MEC. I-MEC server offloads the unhandled tasks to the connected C-MEC server. C-MEC server accepts the offloaded task or rejects it based on the currently available resources of end-devices and the required QoS of the task. Tasks that cannot be handled by the C-MEC server are offloaded to the final offloading level represented by the core network cloud. Algorithm 1 presents the developed energy-aware, a latency-aware offloading algorithm for MEC-based IoT networks. End devices decide the main offloading decision based on the currently available resources, e.g., storage, processing and energy, at the device and the required QoS latency of the current task. Each device uses the program profile to calculate the size of the computing task, Z, and the required number of processing cycles, \(N_{CPU\_CYC}\) to handle the task. The decision engine of the end device is the hardware that implements our proposed offloading algorithm. It receives the information from the program profiler, i.e., \(Z\) and \(N_{CPU\_CYC}\), and the information of the currently available resources from the resources scheduler. Furthermore, the decision engine receives the required QoS latency, \(T_{\_QoS}\), of the current computing task, which represents the maximum allowable latency to handle the computing task. The decision engine uses these parameters to decide either the local execution or offloading. ```
1: Initialize \(T_{\_QoS}\), \(F_{\_Ht\_IoT}\), \(F_{\_Ht\_-\_MEC}\)
2: Calculate \(Z\), \(N_{\_CPU\_CYC\_Bit}\)
3: Calculate \(T_{\_CAC\_-\_IoT}\)
4: If (\(T_{\_exc\_IoT}\leq T_{\_QoS}\))
5:

6:

7:

8:

9:

10: \\ \end{tabular}

Figure 4: Main offloading levels of heterogeneous IoT networks. At first, the decision engine calculates the total time required to execute the task locally, \(T_{exc-IoT}\), at the IoT device. This time is calculated based on the currently available resources at the IoT device according to (1). Then, the decision engine makes the energy and time decisions of offloading, \(D_{Off-E-IoT}\) and \(D_{Off-T-IoT}\). The energy decision is calculated by comparing the residual energy of IoT device after task execution, \(E_{C-IoT}\), with the threshold energy, \(E_{thr-IoT}\), of IoT device. The threshold energy is predefined for IoT devices in a way to prolong the lifetime of IoT devices. If the residual energy of the IoT device after the local execution is higher than the threshold level of energy, the energy offloading decision is set to zero, and the local execution is recommended from the energy perspective. For a positive offloading decision, the task is directly offloaded to the I-MEC server. For a negative energy offloading decision, the time offloading decision is calculated. The time decision of offloading, \(D_{Off-T-IoT}\), is calculated by comparing the execution time, \(T_{exc-IoT}\), with the QoS latency, \(T_{\text{QoS}}\), as in (6). \[T_{exc-IoT}=\frac{N_{CPU-CYC}}{R_{IoT}}\quad,\ R_{IoT}\in f_{IoT}, \tag{1}\]

\[N_{CPU-CYC}=Z.N_{CPU-CYC-Bit}, \tag{2}\]

\[E_{L-exc-IoT}=N_{CPU-CYC}\delta_{IoT}, \tag{3}\]

\[E_{C-IoT}=E_{IoT}-E_{L-exc-IoT}, \tag{4}\]

\[D_{Off-E-IoT}=I(E_{C-IoT},E_{thr-IoT})=\left\{ \begin{array}{ll}1&\text{IF }\left(E_{C-IoT}\leq E_{thr-IoT}\right)\\ 0&\text{IF }\left(E_{C-IoT}>E_{thr-IoT}\right)\end{array} ,\right. \tag{5}\]

\[D_{Off-T-IoT}=I\big{(}T_{exc-IoT},T_{\text{QoS}}\big{)}=\left\{