{"id":1434,"date":"2022-03-16T09:24:00","date_gmt":"2022-03-16T09:24:00","guid":{"rendered":"https:\/\/nag.com\/?post_type=insights&#038;p=1077"},"modified":"2023-07-05T10:29:52","modified_gmt":"2023-07-05T10:29:52","slug":"ad-myths-debunked-ill-have-to-re-write-my-libraries","status":"publish","type":"insights","link":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/","title":{"rendered":"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries"},"content":{"rendered":"<div class=\"container content-area-default \">\n    <div class=\"row justify-content--center\">\n        <div class=\"col-12 col-md-10 col-lg-8 col-xl-6\">\n            <p>This post is a part of the\u00a0AD Myths Debunked series.<\/p>\n<p>You\u2019re curious about AD and its benefits, but you\u2019ve heard that you may need to re-write your libraries in order to integrate it. That\u2019s far too much work, so you don\u2019t pursue the idea much further.<\/p>\n<p>We&#8217;ve encountered this scenario many times over the years. When we ask people why the integration cost would be so large, the response is typically \u201cwell, we have a large legacy code\u201d, and they usually go on to explain just how poorly built the legacy code is.<\/p>\n<p>So, we ask which aspects of this legacy code would make it difficult to implement AD? This question is never met with an answer.<\/p>\n<p>How hard is it to integrate AD into C++ code? What makes it difficult? Why do some people say that it\u2019s a huge job? Is this really a myth? <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG has helped dozens of clients to integrate our AD solution, dco\/c++ into very large and complex quant libraries, and never once has this led to a re-write of the code \u2013 not even close.<\/p>\n<p>The ease of integration depends on the sophistication of your AD tool. A poorly designed tool will make the job most unpleasant indeed! An enterprise-class product like dco\/c++ avoids many of the pitfalls that plague these other tools (for example, dco\/c++ doesn\u2019t assume that your code has any particular shape or form).<\/p>\n<p>When we talk about production C++ libraries, the only AD approach which consistently proves viable is the operator overloading approach. At a high level, the AD tool presents itself as a \u201cnumeric datatype\u201d and the idea is that instead of computing with doubles or floats, you compute with this datatype and the tool takes care of the rest.<\/p>\n<p>A very efficient AD tool (like dco\/c++) will make heavy use of expression templates to get the compiler to do as much of the heavy lifting as possible. Those of us who bear the scars of working with expression template libraries know that mixing such libraries is a recipe for pain. So, if your library uses Boost or Eigen (or some home-grown expression library), then you must proceed with caution. Luckily, dco\/c++ supports both Boost and Eigen, and is robust in the face of other expression template engines.<\/p>\n<p>But let\u2019s talk through a real example to put some numbers on the page. We recently (2021) applied dco\/c++ to the master branch of QuantLib. QuantLib is a large code with all the standard C++-isms one would expect: templates (although it is not templated on the arithmetic type), typedefs, polymorphism and virtual inheritance, design patterns, logging (via streams), and it also uses boost and a good portion of C++11 classes and algorithms. Code statistics of the project show the following:<\/p>\n<table style=\"border-collapse: collapse; width: 100%;\">\n<tbody>\n<tr>\n<td style=\"width: 20%;\">Language<\/td>\n<td style=\"width: 20%;\">files<\/td>\n<td style=\"width: 20%;\">blank lines<\/td>\n<td style=\"width: 20%;\">comments<\/td>\n<td style=\"width: 20%;\">code lines<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%;\">C++<\/td>\n<td style=\"width: 20%;\">915<\/td>\n<td style=\"width: 20%;\">23793<\/td>\n<td style=\"width: 20%;\">21250<\/td>\n<td style=\"width: 20%;\">220934<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 20%;\">C\/C++ Header<\/td>\n<td style=\"width: 20%;\">1374<\/td>\n<td style=\"width: 20%;\">27319<\/td>\n<td style=\"width: 20%;\">43601<\/td>\n<td style=\"width: 20%;\">83622<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<br \/>One of the engineers on <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG\u2019s AD team integrated AD into the whole of QuantLib in slightly less than a day. In doing so, a total number of 200 files changed with overall 625 insertions and 462 deletions (that&#8217;s characters), 90% of which could safely be done by a tool (indeed, <span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG has prototypes of such tools).<\/p>\n<p>This simple integration of dco\/c++ also achieved some impressive results:<\/p>\n<p>The performance of AD tools is measured by their \u201cadjoint factor\u201d, the ratio (adjoint runtime) \/ (original runtime). Speedup over finite differences is then roughly (# sensitivities) \/ (adjoint factor).<\/p>\n<ul>\n<li>For the multi-curve Swap pricing example (including building the curves) we got an adjoint factor of 1.3.<\/li>\n<li>For the basket option pricing example (Monte Carlo) we got an adjoint factor of 1.1.<\/li>\n<li>For the HestonSLV pricing example (PDE) we got an adjoint factor of around 15.<\/li>\n<\/ul>\n<p>Can these results be improved? Of course. And yes, we\u2019ve not yet addressed memory efficiency for reverse-mode (adjoint) AD, that\u2019s in our next Myth!<\/p>\n<p><span class=\"nag-n-override\" style=\"margin-left: 0 !important;\"><i>n<\/i><\/span>AG\u2019s AD toolset has been developed over the last 12 years and it builds upon a further 10 years of AD R&amp;D experience in C++. We know that details matter. Legacy codes are our business and we&#8217;ve not come across a single one which we couldn&#8217;t handle.<\/p>\n<p>Myths are narratives, which might sound like truths, and by talking through these in some detail and sharing our experiences, we hope to help businesses navigate these issues. Results matter, myths should not.<\/p>\n        <\/div>\n    <\/div>\n<\/div>\n\n\n<div class=\"gbc-title-banner tac tac-lg tac-xl\" style='border-radius: 0px; '>\n    <div class=\"container\" style='border-radius: 0px; '>\n        <div class=\"row justify-content--center\" >\n            <div class=\"col-12\"  >\n                <div class=\"wrap pv-4 \" style=\"0pxbackground-color: \">\n                                <div class=\"col-12 col-md-10 col-lg-8 col-xl-6  banner-content\"  >\n    \n                    \n                    <div class=\"mt-1 mb-1 content\"><\/div>\n\n                    \n                    <a href='https:\/\/nag.com\/automatic-differentiation\/' style='background-color: #ff7d21ff; color: #ffffffff; border-radius: 30px; font-weight: 600; ' class='btn mr-1  ' target=\"_blank\">Learn more about AD <i class='fas fa-angle-right'><\/i><\/a>                <\/div>\n                <\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The ease of integration depends on the sophistication of your AD tool. A poorly designed tool will make the job most unpleasant indeed! An enterprise-class product like dco\/c++ avoids many of the pitfalls that plague these other tools (for example, dco\/c++ doesn\u2019t assume that your code has any particular shape or form).<\/p>\n<p>When we talk about production C++ libraries, the only AD approach which consistently proves viable is the operator overloading approach. At a high level, the AD tool presents itself as a \u201cnumeric datatype\u201d and the idea is that instead of computing with doubles or floats, you compute with this datatype and the tool takes care of the rest.<\/p>\n","protected":false},"author":5,"featured_media":988,"parent":0,"menu_order":0,"template":"","meta":{"content-type":"","footnotes":""},"post-tag":[16,25],"class_list":["post-1434","insights","type-insights","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG\" \/>\n<meta property=\"og:description\" content=\"The ease of integration depends on the sophistication of your AD tool. A poorly designed tool will make the job most unpleasant indeed! An enterprise-class product like dco\/c++ avoids many of the pitfalls that plague these other tools (for example, dco\/c++ doesn\u2019t assume that your code has any particular shape or form). When we talk about production C++ libraries, the only AD approach which consistently proves viable is the operator overloading approach. At a high level, the AD tool presents itself as a \u201cnumeric datatype\u201d and the idea is that instead of computing with doubles or floats, you compute with this datatype and the tool takes care of the rest.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/\" \/>\n<meta property=\"og:site_name\" content=\"nAG\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-05T10:29:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1-1024x576.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@NAGTalk\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/\",\"url\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/\",\"name\":\"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG\",\"isPartOf\":{\"@id\":\"https:\/\/nag.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png\",\"datePublished\":\"2022-03-16T09:24:00+00:00\",\"dateModified\":\"2023-07-05T10:29:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage\",\"url\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png\",\"contentUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png\",\"width\":5333,\"height\":3000,\"caption\":\"Automatic Differentiation\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/nag.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Insights\",\"item\":\"https:\/\/nag.com\/insights\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/nag.com\/#website\",\"url\":\"https:\/\/nag.com\/\",\"name\":\"NAG\",\"description\":\"Robust, trusted numerical software and computational expertise.\",\"publisher\":{\"@id\":\"https:\/\/nag.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/nag.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/nag.com\/#organization\",\"name\":\"Numerical Algorithms Group\",\"alternateName\":\"NAG\",\"url\":\"https:\/\/nag.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/nag.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png\",\"contentUrl\":\"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png\",\"width\":1244,\"height\":397,\"caption\":\"Numerical Algorithms Group\"},\"image\":{\"@id\":\"https:\/\/nag.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/NAGTalk\",\"https:\/\/www.linkedin.com\/company\/nag\/\",\"https:\/\/www.youtube.com\/user\/NumericalAlgorithms\",\"https:\/\/github.com\/numericalalgorithmsgroup\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/","og_locale":"en_US","og_type":"article","og_title":"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG","og_description":"The ease of integration depends on the sophistication of your AD tool. A poorly designed tool will make the job most unpleasant indeed! An enterprise-class product like dco\/c++ avoids many of the pitfalls that plague these other tools (for example, dco\/c++ doesn\u2019t assume that your code has any particular shape or form). When we talk about production C++ libraries, the only AD approach which consistently proves viable is the operator overloading approach. At a high level, the AD tool presents itself as a \u201cnumeric datatype\u201d and the idea is that instead of computing with doubles or floats, you compute with this datatype and the tool takes care of the rest.","og_url":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/","og_site_name":"nAG","article_modified_time":"2023-07-05T10:29:52+00:00","og_image":[{"width":1024,"height":576,"url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1-1024x576.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@NAGTalk","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/","url":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/","name":"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries - nAG","isPartOf":{"@id":"https:\/\/nag.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage"},"image":{"@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage"},"thumbnailUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png","datePublished":"2022-03-16T09:24:00+00:00","dateModified":"2023-07-05T10:29:52+00:00","breadcrumb":{"@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#primaryimage","url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png","contentUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/05\/Blog_Post-Myth-1.png","width":5333,"height":3000,"caption":"Automatic Differentiation"},{"@type":"BreadcrumbList","@id":"https:\/\/nag.com\/insights\/ad-myths-debunked-ill-have-to-re-write-my-libraries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nag.com\/"},{"@type":"ListItem","position":2,"name":"Insights","item":"https:\/\/nag.com\/insights\/"},{"@type":"ListItem","position":3,"name":"AD Myths Debunked: I\u2019ll Have to Re-write My Libraries"}]},{"@type":"WebSite","@id":"https:\/\/nag.com\/#website","url":"https:\/\/nag.com\/","name":"NAG","description":"Robust, trusted numerical software and computational expertise.","publisher":{"@id":"https:\/\/nag.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nag.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/nag.com\/#organization","name":"Numerical Algorithms Group","alternateName":"NAG","url":"https:\/\/nag.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nag.com\/#\/schema\/logo\/image\/","url":"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png","contentUrl":"https:\/\/nag.com\/wp-content\/uploads\/2023\/11\/NAG-Logo.png","width":1244,"height":397,"caption":"Numerical Algorithms Group"},"image":{"@id":"https:\/\/nag.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/NAGTalk","https:\/\/www.linkedin.com\/company\/nag\/","https:\/\/www.youtube.com\/user\/NumericalAlgorithms","https:\/\/github.com\/numericalalgorithmsgroup"]}]}},"_links":{"self":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights"}],"about":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/types\/insights"}],"author":[{"embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/users\/5"}],"version-history":[{"count":5,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1434\/revisions"}],"predecessor-version":[{"id":3006,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/insights\/1434\/revisions\/3006"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/media?parent=1434"}],"wp:term":[{"taxonomy":"post-tag","embeddable":true,"href":"https:\/\/nag.com\/wp-json\/wp\/v2\/post-tag?post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}