{"id":747,"date":"2016-03-03T00:38:49","date_gmt":"2016-03-02T23:38:49","guid":{"rendered":"http:\/\/numbercrunch.de\/blog\/?p=747"},"modified":"2023-01-18T20:43:27","modified_gmt":"2023-01-18T19:43:27","slug":"boosting-numpy-with-mkl","status":"publish","type":"post","link":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/","title":{"rendered":"Boosting NumPy with MKL"},"content":{"rendered":"<p style=\"text-align: justify;\">The Intel Math Kernel Library (MKL) contains a collection of highly optimized numerical functions.\u00a0 Among others, it provides implementations of Blas functions and Lapack functions for various linear algebra problems.<\/p>\n<p style=\"text-align: justify;\">A program, which is dynamically linked against the standard Blas and Lapack libraries, can easily benefit from alternative optimized implementations by replacing <tt>libblas.so<\/tt> and <tt>liblapack.so<\/tt> by optimized variants.\u00a0 The MKL, however, does not come with a drop-in replacement for <tt>libblas.so<\/tt> and <tt>liblapack.so<\/tt>. But it comes with a tool for building such drop-in replacements.<\/p>\n<p style=\"text-align: justify;\">The MKL is shipped with the Intel\u00ae Parallel Studio XE and with the Intel\u00ae System Studio.\u00a0 The directory <tt>mkl\/tools\/builders<\/tt> under the installation directory of Intel\u00ae Parallel Studio XE and the Intel\u00ae System Studio contains a makefile to build <tt>libblas.so<\/tt> and <tt>liblapack.so<\/tt> on the basis of the MKL.\u00a0 I found it appropriate to tweak this makefile for better compatibility.<\/p>\n<ul>\n<li>Replace \u201c<tt>IFACE_COMP_PART=intel<\/tt>\u201d by \u201c<tt>IFACE_COMP_PART=gf<\/tt>\u201d.<\/li>\n<li>Replace \u201c<tt>IFACE_THREADING_PART=intel<\/tt>\u201d by \u201c<tt>IFACE_THREADING_PART=gnu<\/tt>\u201d.<\/li>\n<li>Replace \u201c<tt>LIBM=-lm<\/tt>\u201d by \u201c<tt>LIBM=-lm -mkl<\/tt>\u201d.<\/li>\n<li>Replace all occurrences of \u201c<tt>gcc<\/tt>\u201d by \u201c<tt>icc<\/tt>\u201d.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">With these little changes<\/p>\n<p><code># make libintel64 interface=lp64 export=blas_example_list name=libblas<\/code><\/p>\n<p style=\"text-align: justify;\">and<\/p>\n<p><code># make libintel64 interface=lp64 export=lapack_example_list name=liblapack<br \/>\n<\/code><\/p>\n<p style=\"text-align: justify;\">will build new <tt>libblas.so<\/tt> and <tt>liblapack.so<\/tt>.\u00a0 Now, one could overwrite the system&#8217;s <tt>libblas.so<\/tt> (and <tt>libblas.so.3<\/tt>) and <tt>liblapack.so<\/tt> (and <tt>liblapack.so.3<\/tt>) by the new version and all dynamically linked programs would benefit from the optimized libraries.\u00a0 On Debian-based systems, however, one should use the <a href=\"https:\/\/www.debian-administration.org\/article\/91\/Using_the_Debian_alternatives_system\" target=\"_blank\" rel=\"noopener\">alternatives mechanism<\/a> instead.\u00a0 This allows to switch easily between different Blas and Lapack implementations.\u00a0 The following commands add the optimized libraries and make them the default choice (paths may differ on different Linux installations):<\/p>\n<p><code># update-alternatives --install \/usr\/lib\/libblas.so libblas.so \/usr\/local\/intel\/mkl\/tools\/builder\/libblas.so 800<br \/>\n# update-alternatives --install \/usr\/lib\/libblas.so.3 libblas.so.3 \/usr\/local\/intel\/mkl\/tools\/builder\/libblas.so 800<br \/>\n# update-alternatives --install \/usr\/lib\/liblapack.so liblapack.so \/usr\/local\/intel\/mkl\/tools\/builder\/liblapack.so 800<br \/>\n# update-alternatives --install \/usr\/lib\/liblapack.so.3 liblapack.so.3 \/usr\/local\/intel\/mkl\/tools\/builder\/liblapack.so 800<\/code><\/p>\n<p style=\"text-align: justify;\">To test the effect of various Blas and Lapack implementations I wrote a small python script, which calculates the eigenvalues of Hermitian matrices of varying size.\u00a0 Depending of the chosen Blas and Lapack implementations this more than an order of magnitude faster.\u00a0 The Netlib reference implementation of Blas and Lapack is rather slow.\u00a0 OpenBlas is much faster than Netlib, but the MKL is even faster.\u00a0 Both are able to take advantage of multi-core architectures, which explains partly the performance boost.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\n\n# compatibility for Python 3\nfrom __future__ import absolute_import, division, print_function\nfrom pylab import *\nimport timeit\n\nT=timeit.default_timer\n\nsizes=array([64, 128, 256, 512, 1024, 2048, 4096])\ntimes=list()\nfor N in sizes:\n    A=randn(N, N)+1j*rand(N, N)\n    A=A+A.conj().T\n    start=T()\n    eigh(A)\n    end=T()\n    times.append(end-start)\n    print(N, end-start)\ntimes=array(times)\nsavetxt('bench.dat', vstack((sizes, times)).T, fmt='%i\\t%g')\n<\/pre>\n<p><a href=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\" rel=\"attachment wp-att-749\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-749\" src=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\" alt=\"Benchmark results for the code shown above on an Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz (dual core CPU).\" width=\"600\" height=\"450\" srcset=\"https:\/\/www.numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png 600w, https:\/\/www.numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot-300x225.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Benchmark results for the code shown above on an Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz (dual core CPU) with various Blas and Lapack implementations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Intel Math Kernel Library (MKL) contains a collection of highly optimized numerical functions.\u00a0 Among others, it provides implementations of Blas functions and Lapack functions for various linear algebra problems. A program, which is dynamically linked against the standard Blas and Lapack libraries, can easily benefit from alternative optimized implementations by replacing libblas.so and liblapack.so&hellip; <a href=\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Boosting NumPy with MKL<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-747","post","type-post","status-publish","format-standard","hentry","category-performance"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Boosting NumPy with MKL - Number Crunch<\/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:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Boosting NumPy with MKL - Number Crunch\" \/>\n<meta property=\"og:description\" content=\"The Intel Math Kernel Library (MKL) contains a collection of highly optimized numerical functions.\u00a0 Among others, it provides implementations of Blas functions and Lapack functions for various linear algebra problems. A program, which is dynamically linked against the standard Blas and Lapack libraries, can easily benefit from alternative optimized implementations by replacing libblas.so and liblapack.so&hellip; Continue reading Boosting NumPy with MKL\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\" \/>\n<meta property=\"og:site_name\" content=\"Number Crunch\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-02T23:38:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-18T19:43:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\" \/>\n<meta name=\"author\" content=\"Heiko Bauke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Heiko Bauke\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\"},\"author\":{\"name\":\"Heiko Bauke\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"headline\":\"Boosting NumPy with MKL\",\"datePublished\":\"2016-03-02T23:38:49+00:00\",\"dateModified\":\"2023-01-18T19:43:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\"},\"wordCount\":382,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"image\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\",\"articleSection\":[\"Performance\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\",\"name\":\"Boosting NumPy with MKL - Number Crunch\",\"isPartOf\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\",\"datePublished\":\"2016-03-02T23:38:49+00:00\",\"dateModified\":\"2023-01-18T19:43:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage\",\"url\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\",\"contentUrl\":\"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.numbercrunch.de\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Boosting NumPy with MKL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#website\",\"url\":\"https:\/\/www.numbercrunch.de\/blog\/\",\"name\":\"Number Crunch\",\"description\":\"A computational science blog.\",\"publisher\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413\",\"name\":\"Heiko Bauke\",\"logo\":{\"@id\":\"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Boosting NumPy with MKL - Number Crunch","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:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/","og_locale":"en_US","og_type":"article","og_title":"Boosting NumPy with MKL - Number Crunch","og_description":"The Intel Math Kernel Library (MKL) contains a collection of highly optimized numerical functions.\u00a0 Among others, it provides implementations of Blas functions and Lapack functions for various linear algebra problems. A program, which is dynamically linked against the standard Blas and Lapack libraries, can easily benefit from alternative optimized implementations by replacing libblas.so and liblapack.so&hellip; Continue reading Boosting NumPy with MKL","og_url":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/","og_site_name":"Number Crunch","article_published_time":"2016-03-02T23:38:49+00:00","article_modified_time":"2023-01-18T19:43:27+00:00","og_image":[{"url":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png","type":"","width":"","height":""}],"author":"Heiko Bauke","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Heiko Bauke","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#article","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/"},"author":{"name":"Heiko Bauke","@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"headline":"Boosting NumPy with MKL","datePublished":"2016-03-02T23:38:49+00:00","dateModified":"2023-01-18T19:43:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/"},"wordCount":382,"commentCount":0,"publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"image":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage"},"thumbnailUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png","articleSection":["Performance"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/","url":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/","name":"Boosting NumPy with MKL - Number Crunch","isPartOf":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage"},"image":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage"},"thumbnailUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png","datePublished":"2016-03-02T23:38:49+00:00","dateModified":"2023-01-18T19:43:27+00:00","breadcrumb":{"@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#primaryimage","url":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png","contentUrl":"https:\/\/numbercrunch.de\/blog\/wp-content\/uploads\/2016\/03\/heig_bench_plot.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.numbercrunch.de\/blog\/2016\/03\/boosting-numpy-with-mkl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.numbercrunch.de\/blog\/"},{"@type":"ListItem","position":2,"name":"Boosting NumPy with MKL"}]},{"@type":"WebSite","@id":"https:\/\/www.numbercrunch.de\/blog\/#website","url":"https:\/\/www.numbercrunch.de\/blog\/","name":"Number Crunch","description":"A computational science blog.","publisher":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.numbercrunch.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/e73eab65b1721dd0c3d408edb887e413","name":"Heiko Bauke","logo":{"@id":"https:\/\/www.numbercrunch.de\/blog\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/747"}],"collection":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/comments?post=747"}],"version-history":[{"count":5,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions\/954"}],"wp:attachment":[{"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.numbercrunch.de\/blog\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}