Skip to main content Site map
HomeResource hub

Writing Energy Efficient Code

Bookmark this page Bookmarked

Writing Energy Efficient Code

Author(s)

Joe Wallwork

Estimated read time: 5 min
Sections in this article
Share on blog/article:
LinkedIn

Writing Energy Efficient Code

Why is it important to consider energy efficiency when writing code?

By considering energy efficiency when writing code, you can reduce the carbon footprint of your work and play a role in tackling climate change. How so? It is well established that the primary driver of climate change is carbon emissions from human activities. In the case of research software, a key component of this relates to the amount of energy required to run code. Writing energy efficient code allows you to reduce this, thereby reducing your carbon footprint. We all have a role to play, so the target audience for this guide includes software engineers, researchers who code, and managers of software projects.

Energy efficient code isn’t just better for the planet: it runs faster, too! A problem can usually be solved in code in many different ways and the number of operations involved in different implementations varies. Broadly, the more operations involved in an implementation, the more energy is required to run it on a computer. Therefore, writing energy efficient code is largely about finding implementations which minimise computational effort. Effective optimisation is a topic beyond the scope of this guide. However, for details on common optimisation patterns in scientific programming languages, we refer to this optimisations database.

What are common strategies for improving energy efficiency of code?

A common sign of energy inefficient code is redundant operations, or ‘dead code’. If the output of a computation isn’t used by a program then why compute it? Redundant operations can be avoided by adding logic that is activated or deactivated based on configuration options or by simply removing the offending lines.

Loops are prevalent in code for scientific computations, to which high proportions of the overall energy usage can often be attributed. The size of loop ranges and the number of nested loops are both factors that contribute to computational cost. Loop ordering is also important. Depending on whether the programming language being used makes use of row-major (e.g., Python, C, C++) or column-major (e.g., Fortran, R, MATLAB) array formats, some loop orderings can be significantly more expensive than others. Poorly ordered loops force the computer to access data for array entries in patterns which are misaligned with how they are stored in memory. The most efficient loops are written so that they traverse over entries stored contiguously in memory wherever possible.

Parallelism plays an important role in energy efficiency, with GPUs being notable as machines capable of massively parallel computations at a low energy cost per operation. When working with parallel programming models, it is important to avoid waiting, where possible. Running with many threads or processors is not much use when most of them are waiting for one to finish a calculation. If you’re writing code to be deployed in parallel, think carefully about whether you are creating any such ‘bottlenecks’. Relatedly, avoid unnecessary synchronisation between threads/processors, as these amount to waiting. It may be worth considering investigating load balancing - redistributing work between threads or processors - to reduce overall runtime and energy consumption.

What tools can I use to improve the energy efficiency of code?

The first thing to note is that you don’t have to be an expert in code optimisation to make quick energy efficiency wins. Software developers of well-established libraries have usually spent a large amount of time optimising them, especially those used in high-performance computing. Switching from hand-coded implementations to those imported from libraries such as BLAS or NumPy can deliver significant improvements.

A good way to start optimising your code is by measuring its performance. Tools such as CodeCarbon allow you to estimate the carbon footprint of running code. As such, you can use them to track progress and find out which changes make significant differences. While not typically linked directly to carbon emissions, profilers (e.g., PyInstrumentgprofscore-p) provide fine-grained information related to computational efficiency. This allows you to assess which parts of your code take longer to run than others. Informed by a profiler, the job of optimisation becomes much clearer.

Energy efficiency is particularly important for code that gets run often. As such, continuous integration and continuous development (CI/CD) workflows are key targets because they run regularly to assess whether a codebase continues functioning as expected. The green-ci tool provides guidelines and templates for energy efficient CI/CD workflows in the popular GitHub Actions framework.

Key takeaways

  • Greener code also means faster code, which consumes less energy to run.
  • Make use of well-established, optimised libraries where possible.
  • Use profilers and tools like CodeCarbon to identify targets for optimisation and estimate energy usage.
  • Use the output of such tools to make informed decisions about how to optimise your code.

More resources and support are available through the Society of Research Software Engineering’s Green Special Interest Group.

Acknowledgements

This guide was written by Joe Wallwork, Senior Research Software Engineer, Institute of Computing for Climate Science, University of Cambridge, and reviewed by Dimitris Theodorakis.

Find Joe on:

 

Back to Top Button Back to top