-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce carbon footprint estimation to total energy amount #80
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
488f416
Compute footprint from jobinfo returned by configure module
tlestang 101fa7f
Remove unused functions to display carbon footprint values
tlestang 989a7e0
replace 'greenAlgorithmsCalculator' class by function
tlestang d6bf0c8
Add a test for carbon footprint estimation
tlestang 63f7c4c
Return carbon footprint as a float (gC02)
tlestang 004a840
docs: update quickstart with new configuration file structure
tlestang 6c540fd
[fix] Should not return config dictionary
tlestang b6f561c
[fix] Correct kwargs names
tlestang be06ab3
Delete example configuration file
tlestang 57e0200
Merge 'origin/main' into tl-refactor-footprint-continued
tlestang 103ed62
[fix] cats command output
tlestang 77a87df
[docs] Specify unit of power
tlestang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,178 +1,24 @@ | ||
import datetime | ||
from collections import namedtuple | ||
|
||
import yaml | ||
|
||
Estimates = namedtuple("Estimates", ["now", "best", "savings"]) | ||
|
||
|
||
class greenAlgorithmsCalculator: | ||
def __init__( | ||
self, | ||
config, | ||
partition, | ||
runtime, | ||
memory, | ||
cpus, | ||
gpus, | ||
averageBest_carbonIntensity, | ||
averageNow_carbonIntensity, | ||
): | ||
""" | ||
|
||
:param partition: [str] has to match one of the partitions in `config.yml` | ||
:param runtime: [datetime.timedelta] | ||
:param memory: [int] in GB | ||
:param cpus: [int] | ||
:param gpus: [int] | ||
:param averageBest_carbonIntensity: [float] in gCO2e/kWh | ||
:param averageNow_carbonIntensity: [float] in gCO2e/kWh | ||
""" | ||
# ### Load cluster specific info | ||
# with open(config, "r") as stream: | ||
# try: | ||
# self.cluster_info = yaml.safe_load(stream) | ||
# except yaml.YAMLError as exc: | ||
# print(exc) | ||
self.cluster_info = config | ||
|
||
### Load fixed parameters | ||
with open("fixed_parameters.yaml", "r") as stream: | ||
try: | ||
self.fParams = yaml.safe_load(stream) | ||
except yaml.YAMLError as exc: | ||
print(exc) | ||
|
||
self.partition = partition | ||
self.runtime = runtime | ||
self.memory = memory | ||
self.cpus = cpus | ||
self.gpus = gpus | ||
self.averageBest_carbonIntensity = averageBest_carbonIntensity | ||
self.averageNow_carbonIntensity = averageNow_carbonIntensity | ||
|
||
def formatText_footprint(self, footprint_g): | ||
""" | ||
Format the text to display the carbon footprint | ||
:param footprint_g: [float] carbon footprint, in gCO2e | ||
:return: [str] the text to display | ||
""" | ||
if footprint_g < 1e3: | ||
text_footprint = f"{footprint_g:,.0f} gCO2e" | ||
elif footprint_g < 1e6: | ||
text_footprint = f"{footprint_g / 1e3:,.0f} kgCO2e" | ||
else: | ||
text_footprint = f"{footprint_g / 1e3:,.0f} TCO2e" | ||
return text_footprint | ||
|
||
def formatText_treemonths(self, tm_float): | ||
""" | ||
Format the text to display the tree months | ||
:param tm_float: [float] tree-months | ||
:return: [str] the text to display | ||
""" | ||
tm = int(tm_float) | ||
ty = int(tm / 12) | ||
if tm < 1: | ||
text_trees = f"{tm_float:.3f} tree-months" | ||
elif tm == 1: | ||
text_trees = f"{tm_float:.1f} tree-month" | ||
elif tm < 6: | ||
text_trees = f"{tm_float:.1f} tree-months" | ||
elif tm <= 24: | ||
text_trees = f"{tm} tree-months" | ||
elif tm < 120: | ||
text_trees = f"{ty} tree-years and {tm - ty * 12} tree-months" | ||
else: | ||
text_trees = f"{ty} tree-years" | ||
return text_trees | ||
|
||
def formatText_driving(self, dist): | ||
""" | ||
Format the text to display the driving distance | ||
:param dist: [float] driving distance, in km | ||
:return: [str] text to display | ||
""" | ||
if dist < 10: | ||
text_driving = f"driving {dist:,.2f} km" | ||
else: | ||
text_driving = f"driving {dist:,.0f} km" | ||
return text_driving | ||
|
||
def formatText_flying(self, footprint_g, fParams): | ||
""" | ||
Format the text to display about flying | ||
:param footprint_g: [float] carbon footprint, in gCO2e | ||
:param fParams: [dict] Fixed parameters, from fixed_parameters.yaml | ||
:return: [str] text to display | ||
""" | ||
if footprint_g < 0.5 * fParams["flight_NY_SF"]: | ||
text_flying = f"{footprint_g / fParams['flight_PAR_LON']:,.2f} flights between Paris and London" | ||
elif footprint_g < 0.5 * fParams["flight_NYC_MEL"]: | ||
text_flying = f"{footprint_g / fParams['flight_NY_SF']:,.2f} flights between New York and San Francisco" | ||
else: | ||
text_flying = f"{footprint_g / fParams['flight_NYC_MEL']:,.2f} flights between New York and Melbourne" | ||
return text_flying | ||
|
||
def calculate_energies(self): | ||
### Power draw CPU and GPU | ||
partition_info = self.cluster_info["partitions"][self.partition] | ||
if partition_info["type"] == "CPU": | ||
TDP2use4CPU = partition_info["TDP"] | ||
TDP2use4GPU = 0 | ||
else: | ||
TDP2use4CPU = partition_info["TDP_CPU"] | ||
TDP2use4GPU = partition_info["TDP"] | ||
|
||
### Energy usage | ||
energies = { | ||
"energy_CPUs": self.runtime.total_seconds() | ||
/ 3600 | ||
* self.cpus | ||
* TDP2use4CPU | ||
/ 1000, # in kWh | ||
"energy_GPUs": self.runtime.total_seconds() | ||
/ 3600 | ||
* self.gpus | ||
* TDP2use4GPU | ||
/ 1000, # in kWh | ||
"energy_memory": self.runtime.total_seconds() | ||
/ 3600 | ||
* self.memory | ||
* self.fParams["power_memory_perGB"] | ||
/ 1000, # in kWh | ||
} | ||
|
||
energies["total_energy"] = self.cluster_info["PUE"] * ( | ||
energies["energy_CPUs"] | ||
+ energies["energy_GPUs"] | ||
+ energies["energy_memory"] | ||
) | ||
|
||
return energies | ||
|
||
def calculate_CF(self, energies): | ||
CF_best = { | ||
"CF_CPUs": energies["energy_CPUs"] * self.averageBest_carbonIntensity, | ||
"CF_GPUs": energies["energy_GPUs"] * self.averageBest_carbonIntensity, | ||
"CF_memory": energies["energy_memory"] * self.averageBest_carbonIntensity, | ||
"total_CF": energies["total_energy"] * self.averageBest_carbonIntensity, | ||
} | ||
|
||
CF_now = { | ||
"CF_CPUs": energies["energy_CPUs"] * self.averageNow_carbonIntensity, | ||
"CF_GPUs": energies["energy_GPUs"] * self.averageNow_carbonIntensity, | ||
"CF_memory": energies["energy_memory"] * self.averageNow_carbonIntensity, | ||
"total_CF": energies["total_energy"] * self.averageNow_carbonIntensity, | ||
} | ||
|
||
return CF_best, CF_now | ||
|
||
def get_footprint(self): | ||
energies = self.calculate_energies() | ||
CF_best, CF_now = self.calculate_CF(energies) | ||
best = CF_best["total_CF"] | ||
now = CF_now["total_CF"] | ||
|
||
return Estimates( | ||
*[self.formatText_footprint(e) for e in [now, best, now - best]] | ||
) | ||
def get_footprint_reduction_estimate( | ||
PUE: float, | ||
jobinfo: list[tuple[int, float]], | ||
runtime: datetime.timedelta, | ||
average_best_ci: float, # in gCO2/kWh | ||
average_now_ci: float, | ||
) -> Estimates: | ||
# energy in kWh | ||
energy = ( | ||
PUE | ||
* (runtime.total_seconds() / 3600) | ||
* sum([(nunits * power) for nunits, power in jobinfo]) | ||
/ 1000 | ||
) | ||
best = energy * average_best_ci | ||
now = energy * average_now_ci | ||
|
||
return Estimates(now, best, now - best) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
config
dict representing the yaml config file can now be confined to theconfigure
module and not appear in the__init__
module. Thejobinfo
list of(ndevice, power)
is all that is require for carbon footprint estimation down the line.