Stop tracking a dedicated execution task. Delta energy is computed by task, to isolate its contribution to total emissions. :return: EmissionData for an execution task
(self, task_name: str = None)
| 760 | self._active_task = task_name |
| 761 | |
| 762 | def stop_task(self, task_name: str = None) -> EmissionsData: |
| 763 | """ |
| 764 | Stop tracking a dedicated execution task. Delta energy is computed by task, to isolate its contribution to total |
| 765 | emissions. |
| 766 | :return: EmissionData for an execution task |
| 767 | """ |
| 768 | if self._scheduler_monitor_power: |
| 769 | self._scheduler_monitor_power.stop() |
| 770 | |
| 771 | task_name = task_name if task_name else self._active_task |
| 772 | if self._tasks.get(task_name) is None: |
| 773 | logger.warning("stop_task : No active task to stop.") |
| 774 | return None |
| 775 | self._measure_power_and_energy() |
| 776 | emissions_data = ( |
| 777 | self._prepare_emissions_data() |
| 778 | ) # This is emissions_data_at_stop |
| 779 | |
| 780 | if self._active_task_emissions_at_start is None: |
| 781 | logger.error( |
| 782 | f"Task {task_name}: _active_task_emissions_at_start was None. " |
| 783 | "This indicates an issue, possibly start_task was not called or was corrupted. " |
| 784 | "Reporting zero delta for this task to avoid errors." |
| 785 | ) |
| 786 | emissions_data_delta = dataclasses.replace(emissions_data) |
| 787 | # Zero out energy fields for the delta |
| 788 | emissions_data_delta.emissions = 0.0 |
| 789 | emissions_data_delta.emissions_rate = 0.0 |
| 790 | emissions_data_delta.cpu_energy = 0.0 |
| 791 | emissions_data_delta.gpu_energy = 0.0 |
| 792 | emissions_data_delta.ram_energy = 0.0 |
| 793 | emissions_data_delta.energy_consumed = 0.0 |
| 794 | else: |
| 795 | emissions_data_delta = dataclasses.replace(emissions_data) |
| 796 | emissions_data_delta.compute_delta_emission( |
| 797 | self._active_task_emissions_at_start |
| 798 | ) |
| 799 | |
| 800 | # Update global _previous_emissions state using the current totals at task stop. |
| 801 | self._compute_emissions_delta(emissions_data) |
| 802 | |
| 803 | task_duration = Time.from_seconds( |
| 804 | time.perf_counter() - self._tasks[task_name].start_time |
| 805 | ) |
| 806 | |
| 807 | # task_emission_data is the final delta object to be returned and stored |
| 808 | task_emission_data = emissions_data_delta |
| 809 | task_emission_data.duration = ( |
| 810 | task_duration.seconds |
| 811 | ) # Set the correct duration for the task |
| 812 | |
| 813 | self._tasks[task_name].emissions_data = task_emission_data |
| 814 | self._tasks[task_name].is_active = False |
| 815 | self._active_task = None |
| 816 | self._active_task_emissions_at_start = None # Clear task-specific start data |
| 817 | |
| 818 | return task_emission_data |
| 819 |