Start tracking a dedicated execution task. :param task_name: Name of the task to be isolated. :return: None
(self, task_name=None)
| 697 | self._scheduler_monitor_power.start() |
| 698 | |
| 699 | def start_task(self, task_name=None) -> None: |
| 700 | """ |
| 701 | Start tracking a dedicated execution task. |
| 702 | :param task_name: Name of the task to be isolated. |
| 703 | :return: None |
| 704 | """ |
| 705 | # if another instance of codecarbon is already running, stop here |
| 706 | if ( |
| 707 | hasattr(self, "_another_instance_already_running") |
| 708 | and self._another_instance_already_running |
| 709 | ): |
| 710 | logger.warning( |
| 711 | "Another instance of codecarbon is already running. Exiting." |
| 712 | ) |
| 713 | return |
| 714 | try: |
| 715 | _ = self._emissions |
| 716 | except AttributeError: |
| 717 | logger.error("Tracker not initialized. Please check the logs.") |
| 718 | return |
| 719 | |
| 720 | # Stop scheduler as we do not want it to interfere with the task measurement |
| 721 | if self._scheduler: |
| 722 | self._scheduler.stop() |
| 723 | |
| 724 | # Task background thread for measuring power |
| 725 | self._scheduler_monitor_power.start() |
| 726 | |
| 727 | if self._active_task: |
| 728 | logger.warning("A task is already under measure") |
| 729 | return |
| 730 | if not task_name: |
| 731 | task_name = uuid.uuid4().__str__() |
| 732 | if task_name in self._tasks.keys(): |
| 733 | task_name += "_" + uuid.uuid4().__str__() |
| 734 | self._last_measured_time = self._start_time = time.perf_counter() |
| 735 | |
| 736 | # Clear utilization history for fresh measurements |
| 737 | self._cpu_utilization_history.clear() |
| 738 | self._ram_utilization_history.clear() |
| 739 | self._ram_used_history.clear() |
| 740 | self._gpu_utilization_history.clear() |
| 741 | |
| 742 | # Read initial energy for hardware |
| 743 | for hardware in self._hardware: |
| 744 | hardware.start() |
| 745 | prepared_data_for_task_start = self._prepare_emissions_data() |
| 746 | self._active_task_emissions_at_start = dataclasses.replace( |
| 747 | prepared_data_for_task_start |
| 748 | ) |
| 749 | # The existing call to _compute_emissions_delta uses the result of _prepare_emissions_data. |
| 750 | # Let's make sure it uses the same one we captured. |
| 751 | self._compute_emissions_delta(prepared_data_for_task_start) |
| 752 | |
| 753 | self._tasks.update( |
| 754 | { |
| 755 | task_name: Task( |
| 756 | task_name=task_name, |