{"id":1791,"date":"2010-07-22T20:28:19","date_gmt":"2010-07-23T00:28:19","guid":{"rendered":"http:\/\/www.easterbrook.ca\/steve\/?p=1791"},"modified":"2010-07-22T23:54:54","modified_gmt":"2010-07-23T03:54:54","slug":"a-portable-climate-model","status":"publish","type":"post","link":"http:\/\/www.easterbrook.ca\/steve\/2010\/07\/a-portable-climate-model\/","title":{"rendered":"A portable climate model"},"content":{"rendered":"<p>One of the exercises I set myself while visiting NCAR this month is to try porting the climate model <a title=\"CESM 1.0 website\" href=\"http:\/\/www.cesm.ucar.edu\/models\/cesm1.0\/\" target=\"_blank\">CESM1<\/a> to my laptop (a MacBook Pro). Partly because I wanted to understand what makes it tick, and partly because I thought it would be neat to be able to run it anywhere. At first I thought the idea was crazy &#8211; these things are designed to be run on <a title=\"E.g. the planned Cheyenne Supercomputing Center\" href=\"http:\/\/www2.ucar.edu\/news\/ncar-wyoming-supercomputing-center\" target=\"_blank\">big supercomputers<\/a>. But CESM is also intended to be portable, as part of a mission to support a broader community of model users. So, porting it to my laptop is a simple proof of concept test &#8211; if it ports easily, that&#8217;s a good sign that the code is robust.<\/p>\n<p>It took me several days of effort to complete the port, but most of that time was spent on two things that have very little to do with the model itself. The first was a compiler bug that I tripped over (yeah, yeah, blame the compiler, right?) and the second was the issue of getting all the necessary <a title=\"Here's a good list of packages for HPC on a Mac\" href=\"http:\/\/hpc.sourceforge.net\/\" target=\"_blank\">third party packages<\/a> installed. But in the end I was successful. I&#8217;ve just completed two very basic test runs of the model. The first is what&#8217;s known as an <a title=\"a list of all the CESM component sets\" href=\"http:\/\/www.cesm.ucar.edu\/models\/cesm1.0\/cesm\/cesm_doc\/x42.html#ccsm_component_sets\" target=\"_blank\">&#8216;X&#8217; component set<\/a>, in which all the major components (atmosphere, ocean, land, ice, etc) don&#8217;t actually do anything &#8211; this just tests that the framework code builds and runs. The second is an &#8220;A&#8221; compset at a low resolution, in which all the components are static data models (this ran for five days of simulation time in about 1.5 minutes). If I was going on to test the port correctly, there&#8217;s a whole sequence of <a title=\"Port validation tests (from the CESM user guide)\" href=\"http:\/\/www.cesm.ucar.edu\/models\/cesm1.0\/cesm\/cesm_doc\/x2245.html\" target=\"_blank\">port validation tests<\/a> that I ought to perform, for example to check that my runs are consistent with the benchmark runs, that I can stop and restart the model from the data files, that I get the same results in different model configurations, etc. And then eventually there&#8217;s the scientific validation tests &#8211; checks that the simulated climate in my ported model is realistic.<\/p>\n<p>But for now, I just want to reflect on the process of getting the model to build and run on a new (unsupported) platform. I&#8217;ll describe some of the issues I encountered, and then reflect on what I&#8217;ve learned about the model. First, some stats. The latest version of the model, <a title=\"release notes for CESM1.0\" href=\"http:\/\/www.cesm.ucar.edu\/models\/cesm1.0\/tags\/\" target=\"_blank\">CESM1.0<\/a> was released on June 25, 2010. It contains just under 1 million lines of code. Three quarters of this is Fortran (mainly Fortran 90), the rest is a mix of shell scripts (of several varieties), XML and HTML:<\/p>\n<div id=\"attachment_1793\" style=\"width: 502px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.easterbrook.ca\/steve\/wp-content\/Picture-33.png\"><img aria-describedby=\"caption-attachment-1793\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-1793\" title=\"Picture 3\" src=\"http:\/\/www.easterbrook.ca\/steve\/wp-content\/Picture-33.png\" alt=\"\" width=\"492\" height=\"332\" srcset=\"http:\/\/www.easterbrook.ca\/steve\/wp-content\/Picture-33.png 492w, http:\/\/www.easterbrook.ca\/steve\/wp-content\/Picture-33-300x202.png 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/a><p id=\"caption-attachment-1793\" class=\"wp-caption-text\">Lines of Code count for CESM v1.0 (not including build scripts), as calculated by cloc.sourceforge.net v1.51<\/p><\/div>\n<p>In addition to the model itself, there are another 12,000 lines of perl and shell script that handle the installing, configuring, building and running the model.<\/p>\n<p>The main issues that tripped me up were<\/p>\n<ul>\n<li>The compiler. I decided to use the gnu compiler package (<a title=\"gfortran site\" href=\"http:\/\/gcc.gnu.org\/wiki\/GFortran\" target=\"_blank\">gfortran<\/a>, included in the gcc package), because it&#8217;s free. But it&#8217;s not one of the compilers that&#8217;s supported for CESM, because in general CESM is used with commercial compilers (e.g. IBM&#8217;s) on the supercomputers. I grabbed the newest version of gcc that I could find a pre-built Mac binary for (v4.3.0), but it turned out not to be new enough &#8211; I spent a few hours diagnosing what turned out to be a (<a title=\"Here's my bug report.\" href=\"http:\/\/gcc.gnu.org\/bugzilla\/show_bug.cgi?id=44957\" target=\"_blank\">previously undiscovered?<\/a>) bug in gfortran v4.3.0 that&#8217;s fixed in newer versions (I switched to v4.4.1). And then there&#8217;s a whole bunch of compiler flags (mainly to do with compatibility for certain architectures and language versions) that are not compatible with the commercial compilers, which I needed to track down.<\/li>\n<li>Third party packages such as <a title=\"I used MPICH\" href=\"http:\/\/www.mcs.anl.gov\/research\/projects\/mpich2\/\" target=\"_blank\">MPI<\/a> (the message passing interface used for exchanging data between model components) and <a title=\"NetCDF site\" href=\"http:\/\/www.unidata.ucar.edu\/software\/netcdf\/\" target=\"_blank\">NetCDF<\/a> (the data formating standard used for geoscience data). It turns out that the Mac already has MPI installed, but without Fortran and Parallel IO support, so I had to rebuild it. And it took me a few rebuilds to get both these packages installed with all the right options.<\/li>\n<\/ul>\n<p>Once I&#8217;d got these sorted, and figured out which compiler flags I needed, the build went pretty smoothly, and I&#8217;ve had no problems so far running it. Which leads me to draw a number of (tentative) conclusions about portability. First, CESM is a little unusual compared to most climate models, because it is intended as a community effort, and hence portability is a high priority. It has already been ported to around 30 different platforms, including a variety\u00a0IBM and Cray supercomputers, and various\u00a0Linux clusters. Just the process of running the code through many different compilers shakes out not just portability issues, but good coding practices too, as different compilers tend to be picky about different language constructs.<\/p>\n<p>Second, in the process of building the model, it&#8217;s quite easy to see that it consists of a number of distinct components, written by different communities, to different coding standards. Most obviously, CESM itself is built from five different component models (atmosphere, ocean, sea ice, land ice, land surface), along with a coupler that allows them to interact. There&#8217;s a tension between the needs of scientists who develop code just for a particular component model (run as a standalone model) versus scientists who want to use a fully coupled model. <a title=\"E.g. see the CESM working groups\" href=\"http:\/\/www.cesm.ucar.edu\/working_groups\/\" target=\"_blank\">These communities<\/a> overlap, but not completely, and coordinating the different needs takes considerable effort. Sometimes code that makes sense in a standalone module will break the coupling scheme.<\/p>\n<p>But there&#8217;s another distinction that wasn&#8217;t obvious to me previously:<\/p>\n<ul>\n<li>Scientific code &#8211; the bulk of the Fortran code in the component modules. This includes the core numerical routines, radiation schemes, physics parameterizations, and so on. This code is largely written by domain experts (scientists), for whom scientific validity is the over-riding concern (and hence they tend to under-estimate the importance of portability, readability, maintainability, etc).<\/li>\n<li>Infrastructure code &#8211; including the coupler that allows the components to interact, the shared data handling routines, and a number of shared libraries. Most of this I could characterize as a modeling framework &#8211; it provides an overall architecture for a coupled model, and calls the scientific code as and when needed. This code is written jointly by the software engineering team and the various scientific groups.<\/li>\n<li>Installation code &#8211; including configuration and build scripts. These are distinct from the model itself, but intended to provide flexibility to the community to handle a huge variety of target architectures and model configurations. These are written exclusively by the software engineering team (I think!), and tend to suffer from a serious time crunch: making this code clean and maintainable is difficult, given the need to get a complex and ever-changing model working in reasonable time.<\/li>\n<\/ul>\n<p>In <a title=\"Serendipity: Better Science through Better Software\" href=\"http:\/\/www.easterbrook.ca\/steve\/?p=1744\" target=\"_blank\">an earlier post<\/a>, I described the rapid growth of complexity in earth system models as a major headache. This growth of complexity can be seen in all three types of software, but the complexity growth is compounded in the latter two: modeling frameworks need to support a growing diversity of earth system component models, which then leads to exponential growth in the number of possible model configurations that the build scripts have to deal with. Handling the growing complexity of the installation code is likely to be one of the biggest software challenges for the earth system modeling community in the next few years.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the exercises I set myself while visiting NCAR this month is to try porting the climate model CESM1 to my laptop (a MacBook Pro). Partly because I wanted to understand what makes it tick, and partly because I thought it would be neat to be able to run it anywhere. At first I [&hellip;]<\/p>\n","protected":false},"author":392,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/posts\/1791"}],"collection":[{"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/users\/392"}],"replies":[{"embeddable":true,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/comments?post=1791"}],"version-history":[{"count":3,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/posts\/1791\/revisions"}],"predecessor-version":[{"id":1796,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/posts\/1791\/revisions\/1796"}],"wp:attachment":[{"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/media?parent=1791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/categories?post=1791"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.easterbrook.ca\/steve\/wp-json\/wp\/v2\/tags?post=1791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}