<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>mercuriete's blog</title>
    <description>A blackhole of knowledge I learnt by failing in my daily life projects
</description>
    <link>https://mercuriete.github.io/</link>
    <atom:link href="https://mercuriete.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 10 Mar 2019 12:22:45 +0000</pubDate>
    <lastBuildDate>Sun, 10 Mar 2019 12:22:45 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>Keep Gentoo sane and not becoming insane</title>
        <description>&lt;p&gt;Keeping updated a Gentoo instalation is a task quite a few difficult. In this post we learn how to keep up with the updates.&lt;/p&gt;

&lt;h5 id=&quot;tldr&quot;&gt;TL;DR&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
eix-sync
emerge &lt;span class=&quot;nt&quot;&gt;--update&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--deep&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--changed-use&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--newuse&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--with-bdeps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;y @world &lt;span class=&quot;nt&quot;&gt;--ask&lt;/span&gt;
emerge @preserved-rebuild
emerge &lt;span class=&quot;nt&quot;&gt;--depclean&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--ask&lt;/span&gt;
revdep-rebuild
revdep-rebuild.sh &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--pretend&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://gist.github.com/mercuriete/650a7d4431ba4ec660ff59ab4b3365ff&quot;&gt;See Gist&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;eix-sync&quot;&gt;Eix-sync&lt;/h5&gt;
&lt;p&gt;eix-sync is a amazing tool that allows us to download the new state of portage. Doing an analogy to debian based distros is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;apt-get update&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In addition, checks the differences between the last time you launch that command and show you problems with your installed packages.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;* Calling eix-diff
[U]  ==  dev-libs/openssl  (1.0.2q@03/03/19;  0.9.8z_p8-r1(0.9.8)^d  1.0.2q^d  -&amp;gt;  0.9.8z_p8-r1(0.9.8)^d  1.0.2r^d): full-strength general purpose cryptography li
brary (including SSL and TLS)
[&amp;gt;]  ==  sys-power/bbswitch  (0.8-r1  -&amp;gt;  0.8-r2): Toggle discrete NVIDIA Optimus graphics card
* Time statistics:
26 seconds for syncing
7 seconds for eix-update
1 seconds for eix-diff
34 seconds total
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For example, in this time, we got updates for the package called openssl that is already installed and bbswitch that is not installed in our system.&lt;/p&gt;

&lt;p&gt;There are several codes for package status:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;[U]:&lt;/strong&gt; the package is installed and there is a new update available.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[&amp;gt;]:&lt;/strong&gt; there are new updates for a package that is not installed (useless information but nice to know it).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[&amp;lt;]:&lt;/strong&gt; Gentoo stop supporting a package and is not in portage anymore. You can keep your package installed but probably in a few month a dependency problem will appear and you will be forced to search an alternative.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[?]:&lt;/strong&gt; There is something wrong with your package. Usully this means you have an unstable package installed and is only unmasked for a given version. The system is telling you that you need to update by hand this package unmasking a new version of this package.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[UD]:&lt;/strong&gt; You had a unstable version installed but you forgot to update and now there is a new “old” version stable and the system is forcing you to downgrade. The solution is downgrade or unmask a new version.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is very important to read carefully the information given by this command because if you ignore it, you will have problems in the future.&lt;/p&gt;

&lt;h5 id=&quot;emerge-update-wtf_are_those_arguments&quot;&gt;Emerge –update –wtf_are_those_arguments&lt;/h5&gt;
&lt;p&gt;This command is already in the official documentation: &lt;a href=&quot;https://wiki.gentoo.org/wiki/Gentoo_Cheat_Sheet#Package_upgrades&quot;&gt;See documentation&lt;/a&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;–update:&lt;/strong&gt; we are going to update packages&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;–deep:&lt;/strong&gt; we are searching through the dependency tree in DFS (deep search) looking for dependency updates&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;–changed-use:&lt;/strong&gt; we will rebuild a package that is not updated but you changed the USE flags that are affecting to this package.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;–newuse:&lt;/strong&gt; we will rebuild a package that is not updated but someone (usually the package mantainer) adds a new USE flag.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;–with-bdeps=y:&lt;/strong&gt; we will be updating the build deps and not only the runtime deps. For example, gcc, g++, bison, flex, are usually build dependencies but not used in runtime. Keep your toolchain updated is very important for new optimizations and stability.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;@world:&lt;/strong&gt; This is the package set you want to update, you can put here a single package or a set. World package means “all installed packages by me”:  &lt;a href=&quot;https://wiki.gentoo.org/wiki/World_set_(Portage)&quot;&gt;See documentation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;–ask:&lt;/strong&gt; Stupid mode. The command treats you as a stupid and ask everything. This argument is very important because It resolves unmasking problems such as a package is requesting an unstable package or you are trying to install a privative package without accepting the EULA (for example chrome-binary-plugins if you want see netflix)&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;emerge-preserved-rebuild&quot;&gt;emerge @preserved-rebuild&lt;/h5&gt;
&lt;p&gt;Sometimes, hardly ever, you will encounter a weird problem.&lt;/p&gt;

&lt;p&gt;You are updating a given library but some other package depends on this, the system detects that there are some linking incompatibility and the system keeps two copies of the library for avoid break the application.&lt;/p&gt;

&lt;p&gt;What emerge preserved-rebuild does is rebuild all applications that depends on this library in order to be able to delete the “preserved library”.&lt;/p&gt;

&lt;p&gt;This not happens very often because emerge rebuilds applications automatically, this only happens when the emerge process is stopped badly and you don’t gave It the opportunity to keep the system in a sane state.&lt;/p&gt;

&lt;h5 id=&quot;emerge-depclean-ask&quot;&gt;emerge –depclean –ask&lt;/h5&gt;
&lt;p&gt;This deletes orphan packages (packages installed that nobody want them).&lt;/p&gt;

&lt;p&gt;If you are a debian based user, this is similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;apt-get autoremove&lt;/code&gt;.
–ask argument stops the execution and prompt you a Yes/No confirmation, useful to avoid delete packages that you want to keep but you forgot to rebuild the application with that version of the library. This usually happens when you update llvm but you forgot to rebuild mesa.&lt;/p&gt;

&lt;h5 id=&quot;revdep-rebuild&quot;&gt;revdep-rebuild&lt;/h5&gt;
&lt;p&gt;This command is very useful to keep your applications in a sane state.&lt;/p&gt;

&lt;p&gt;What this command does is keep the links working properly. &lt;a href=&quot;https://wiki.gentoo.org/wiki/Gentoolkit/es#revdep-rebuild&quot;&gt;See documentation&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Iterate over all applications&lt;/li&gt;
  &lt;li&gt;check the links that points to shared libraries&lt;/li&gt;
  &lt;li&gt;If the link is broken check to what package belongs that file&lt;/li&gt;
  &lt;li&gt;Installs or rebuild the library package&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This situation is not very common but when It happen is difficult to track down.&lt;/p&gt;

&lt;h5 id=&quot;revdep-rebuild--u-pretend&quot;&gt;revdep-rebuild -u –pretend&lt;/h5&gt;
&lt;p&gt;This command is very important, It will save you hours and hours of debugging and frustration.&lt;/p&gt;

&lt;p&gt;What this command does is:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Iterate over all applications&lt;/li&gt;
  &lt;li&gt;Check all dinamic libraries and symbols that are exported from the library and imported to the application required to work. like: &lt;code class=&quot;highlighter-rouge&quot;&gt;readelf -d $executable | grep 'NEEDED'&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Figure out what package contains that library. like: &lt;code class=&quot;highlighter-rouge&quot;&gt;equery belongs $file&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Suggest you to rebuild that package.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This problem should never appears but the reality is that you can have problems with the hard drive (corrupted files), or you can have a big update of gcc.&lt;/p&gt;

&lt;p&gt;When this events happens, you think your dependencies are correct but in reality the symbols that exports the library are different breaking the ABI and the application got broken.&lt;/p&gt;

&lt;p&gt;With this advise you can avoid a full system recompilation that is suggested in official documentation: &lt;a href=&quot;https://wiki.gentoo.org/wiki/Upgrading_GCC#ABI_changes&quot;&gt;See documentation&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h5&gt;
&lt;p&gt;The Gentoo official documentation is great but is splited in several locations so you have to learn which combination of commands are suitable for you.&lt;/p&gt;

&lt;p&gt;In addition, keeping up with the updates without breaking the system is difficult so this is why Gentoo is not the best distribution for newbies.&lt;/p&gt;

&lt;p&gt;Given that, the decision of moving from a given gcc version to another is something that someone have to do. I mean wheather you are building your packages or a package mantainer are building this for you, ABI changes are problem that someone have to solve. The only difference between Ubuntu and Gentoo is that those problems are resolved by other people that are not you, but the problems is still there.&lt;/p&gt;

&lt;p&gt;With Gentoo you will learn more about linking problems, ABI breakages and some other problems that you are not facing in a binary distribution like Ubuntu or Windows or MacOSX&lt;/p&gt;

&lt;p&gt;I Hope you enjoy this post.&lt;/p&gt;

</description>
        <pubDate>Sun, 10 Mar 2019 12:00:00 +0000</pubDate>
        <link>https://mercuriete.github.io/article/update-gentoo</link>
        <guid isPermaLink="true">https://mercuriete.github.io/article/update-gentoo</guid>
        
        
        <category>gentoo</category>
        
      </item>
    
      <item>
        <title>Functional Programming with C++ 2017</title>
        <description>&lt;p&gt;Designing high performance applications is difficult without the knowledge of parallel programing. We will achieve the full usage of all cpu cores using functional programming and C++ 2017 parallel algorithms.&lt;/p&gt;

&lt;h5 id=&quot;tldr&quot;&gt;TL;DR&lt;/h5&gt;
&lt;p&gt;Parallel Euclidean distance:&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;\sqrt {\sum _{i=1}^{n}(x_{i}-y_{i})^{2}}&lt;/script&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;//euclidean distance
&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euclidean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform_reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;},&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);}&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euclidean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;result:&lt;/p&gt;
&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h5 id=&quot;disclaimer&quot;&gt;DISCLAIMER&lt;/h5&gt;
&lt;p&gt;C++ 17 Standards are too new to be production ready. There are no big compiler vendors that provide STL parallel algorithms at this moment. All this examples are created using Intel® Parallel STL.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017&quot;&gt;Status of gcc C++ 2017 Support&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blogs.msdn.microsoft.com/vcblog/2017/12/19/c17-progress-in-vs-2017-15-5-and-15-6/&quot;&gt;Status of MSVC++ 2017 Support&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will do a small tutorial for set up the environment but is only for linux + g++ + gentoo.
If you don’t have this environment, you have to do by your own.&lt;/p&gt;

&lt;h5 id=&quot;intel-parallel-stl-installation&quot;&gt;Intel® Parallel STL Installation&lt;/h5&gt;
&lt;p&gt;&lt;a href=&quot;https://software.intel.com/en-us/get-started-with-pstl&quot;&gt;See documentation&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Install gcc with support for OpenMP and C++11
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;USE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;openmp&quot;&lt;/span&gt; emerge sys-devel/gcc &lt;span class=&quot;nt&quot;&gt;--ask&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Install Intel® TBB 2018
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;emerge &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;dev-cpp/tbb-2018.20180312 &lt;span class=&quot;nt&quot;&gt;--ask&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Clone the Intel® Parallel STL
    &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/intel/parallelstl.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Remember the path of include folder for later&lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;functional-programming&quot;&gt;Functional Programming&lt;/h5&gt;
&lt;p&gt;Functional programming is a programming paradigm that allows the programmer to relax the definition of the control flow relying in our compiler .&lt;/p&gt;

&lt;p&gt;When using imperative programming we are telling to the compiler exactly what It have to do, ending in a not too much performant code.&lt;/p&gt;

&lt;h6 id=&quot;example-for-loop&quot;&gt;Example for loop&lt;/h6&gt;
&lt;ul&gt;
  &lt;li&gt;Imperative
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;something&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;You as a programmer are telling to the compiler to do the following:&lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;Initialize iterator&lt;/li&gt;
      &lt;li&gt;do something with an array&lt;/li&gt;
      &lt;li&gt;Increment iterator&lt;/li&gt;
      &lt;li&gt;Test condition and jump backwards&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see this definition is too strict and the compiler can’t optimize your code because It can’t understand what you want to do.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Functional Programming
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;for_each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;something&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;););&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;As you can see the meaning of this loop has changed a little bit to the following:&lt;/p&gt;
    &lt;ol&gt;
      &lt;li&gt;Do some stuff with each element. I don’t care how you are doing It. But I still want to preserve the execution order.&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;c-2017-parallel-algorithms&quot;&gt;C++ 2017 Parallel Algorithms&lt;/h5&gt;
&lt;p&gt;Once we understand the power of functional programming and the compiler have more room to improve our code, C++17 defines a way to run code very efficiently.&lt;/p&gt;
&lt;h6 id=&quot;execution-policies&quot;&gt;Execution Policies&lt;/h6&gt;
&lt;p&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/execution_policy_tag_t&quot;&gt;See documentation&lt;/a&gt;
A execution policy is a way to hint the compiler If you want to continue executing your code sequentially or if you want the compiler parallelize for you.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;std::execution::seq&lt;/strong&gt;: I want to preserve the original sequential behaviour.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;std::execution::par&lt;/strong&gt;: I want the compiler to parallelize my code&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;std::execution::par_unseq&lt;/strong&gt;: The compiler tries to parallelize and vectorize the loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;example-of-parallel-stl-code&quot;&gt;Example of Parallel STL Code&lt;/h5&gt;
&lt;p&gt;Understanding the STL Algorithms is not in the scope of this tutorial. It is granted you are a c++ developer and know some of the following algorithms:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/for_each&quot;&gt;std::for_each&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/reduce&quot;&gt;std::reduce&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/transform&quot;&gt;std::transform&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.cppreference.com/w/cpp/algorithm/transform_reduce&quot;&gt;std::transform_reduce&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;cmath&amp;gt;
#include &quot;pstl/algorithm&quot;
#include &quot;pstl/execution&quot;
#include &quot;pstl/numeric&quot;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MAX_SIZE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//initialize vector with zeroes
&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;//initialize vector with ones
&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;});&lt;/span&gt;

   &lt;span class=&quot;c1&quot;&gt;//get some random numbers
&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RAND_MAX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);});&lt;/span&gt;

   &lt;span class=&quot;c1&quot;&gt;//calculate ABS
&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);});&lt;/span&gt;

   &lt;span class=&quot;n&quot;&gt;for_each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This code computes the absolute value of a integer vector&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;First we initialize the vector with zeroes and then with ones
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;c1&quot;&gt;//initialize vector with zeroes
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MAX_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//initialize vector with ones
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Second we generate some random numbers
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//get some random numbers
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RAND_MAX&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Third we compute the abs()
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//calculate ABS
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;abs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Print the vector in std out sequentially to avoid data races
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;for_each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;parallel-euclidean-distance&quot;&gt;Parallel Euclidean Distance&lt;/h5&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;\sqrt {\sum _{i=1}^{n}(x_{i}-y_{i})^{2}}&lt;/script&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;   &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;//euclidean distance
&lt;/span&gt;   &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euclidean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transform_reduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;execution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;par&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;begin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
                      &lt;span class=&quot;c1&quot;&gt;//initial reduce value
&lt;/span&gt;                      &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;c1&quot;&gt;//reduce function: sum
&lt;/span&gt;                      &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;},&lt;/span&gt;
                      &lt;span class=&quot;c1&quot;&gt;//transform function: substract^2
&lt;/span&gt;                      &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);}&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;euclidean&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ol&gt;
  &lt;li&gt;First we initialize the input vectors
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;vector&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;For every pair from x and y, compute transform (x-y)^2
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;The result of the transform will be used in the parallel reduce adding all elements.
    &lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;auto&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;compiling-our-code&quot;&gt;Compiling our code&lt;/h5&gt;
&lt;p&gt;After having our environment installed, we need to compile our code.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;CPATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/path_to_parallelstl/include g++ &lt;span class=&quot;nt&quot;&gt;-std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;c++17 main.cpp &lt;span class=&quot;nt&quot;&gt;-ltbb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
  &lt;li&gt;We are forcing to use the new stl with the CPATH env var.&lt;/li&gt;
  &lt;li&gt;We are forcing C++17 standard&lt;/li&gt;
  &lt;li&gt;We are linking with &lt;a href=&quot;https://www.threadingbuildingblocks.org/&quot;&gt;Intel® TBB Threading Building Blocks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h5&gt;
&lt;p&gt;Learning parallel programming is too difficult because our mindset is too much attached to imperative programming.&lt;/p&gt;

&lt;p&gt;When we start to code in functional programming, mutual exclusion and data races are less common because our data structure is independant between iterations.&lt;/p&gt;

&lt;p&gt;We still have to be careful about thread safe algorithm. But If you use lambdas and the input and output are different in each iteration of the &lt;em&gt;loop&lt;/em&gt;, You will end up with high performant code.&lt;/p&gt;

&lt;h5 id=&quot;next-steps&quot;&gt;Next Steps&lt;/h5&gt;
&lt;p&gt;Nowadays, a computer is a heterogeneous system with too many processors.&lt;/p&gt;

&lt;p&gt;Some of them are called CPU, others are called GPU and others are called FPGA.&lt;/p&gt;

&lt;p&gt;Khronos(R) Group is investing in &lt;a href=&quot;https://github.com/KhronosGroup/SyclParallelSTL&quot;&gt;SYCL&lt;/a&gt; library. A high performance library to bring C++ 2017 code in all this devices.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=4r6FXxknJEA&quot;&gt;Single source SYCL C++ on Xilinx FPGA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.codeplay.com/products/computesuite/computecpp&quot;&gt;ComputeCPP&lt;/a&gt;: A library for execute C++ code in GPUs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I Hope you enjoy this post.&lt;/p&gt;
</description>
        <pubDate>Sat, 01 Sep 2018 12:00:00 +0000</pubDate>
        <link>https://mercuriete.github.io/article/functional-programming-cpp</link>
        <guid isPermaLink="true">https://mercuriete.github.io/article/functional-programming-cpp</guid>
        
        
        <category>c++</category>
        
        <category>functional</category>
        
      </item>
    
      <item>
        <title>Scalable web applications with swarm</title>
        <description>&lt;p&gt;Designing horizontal scalable application can be a difficult task if you don’t know the right tools to achieve this pourpose. In this post we make use of docker swarm to create an horizontal scalable web application.&lt;/p&gt;

&lt;h5 id=&quot;tldr&quot;&gt;TL;DR&lt;/h5&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker swarm init &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
git clone https://github.com/mercuriete/laravel-docker-redis.git &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;laravel-docker-redis &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
docker stack deploy &lt;span class=&quot;nt&quot;&gt;--compose-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;docker-stack-compose.yml laravel &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
sleep 5 &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
docker service scale &lt;span class=&quot;nv&quot;&gt;laravel_php&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;disclaimer&quot;&gt;Disclaimer&lt;/h5&gt;
&lt;p&gt;The scope of this post is not teaching you how to create an application in a given framework. I decided to use php and laravel because I thought was a practical approach instead of making a academic tutorial without real application in real life.&lt;/p&gt;

&lt;p&gt;If you had never heard about docker or what is a stateless web application. This topics are out of the scope of this post.&lt;/p&gt;

&lt;p&gt;Given that you know what are docker containers or how to create a stateless web application, you can continue, otherwise I will encourage you to read the docs.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://docs.docker.com/compose/wordpress/&quot;&gt;Some docker tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://whatis.techtarget.com/definition/stateless-app&quot;&gt;Some definition about Stateless App&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;swarm-init&quot;&gt;Swarm init&lt;/h5&gt;
&lt;p&gt;First of all we need to have up and running our docker swarm cluster. In this tutorial we do the examples with a cluster of only one node. You can think this is kind of pointless but in a docker swarm adding more nodes or scaling your application is so easy that once you learn with one node, the rest is the same than with one node.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker swarm init
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;output:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Swarm initialized: current node &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;RANDOM_STRING&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; is now a manager.  
To add a worker to this swarm, run the following &lt;span class=&quot;nb&quot;&gt;command&lt;/span&gt;:  
docker swarm join &lt;span class=&quot;nt&quot;&gt;--token&lt;/span&gt; RANDOM_STRING 192.168.1.43:2377  
To add a manager to this swarm, run &lt;span class=&quot;s1&quot;&gt;'docker swarm join-token manager'&lt;/span&gt; and follow the instructions.  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this command you are ready to deploy swarm stacks.&lt;/p&gt;

&lt;h5 id=&quot;cloning-the-repository&quot;&gt;Cloning the repository&lt;/h5&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/mercuriete/laravel-docker-redis.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h5 id=&quot;understanding-the-application&quot;&gt;Understanding the application&lt;/h5&gt;
&lt;p&gt;This application is a example of a php application with a mysql database and a redis database for authentication.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://g.gravizo.com/svg?  digraph G {    nginx [shape=box]    php [shape=box]    database [shape=box]    redis [shape=box]    nginx -&amp;gt; php;    php -&amp;gt; database;    php -&amp;gt; redis;  } &quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Nginx&lt;/strong&gt;: Nginx is the entry point of our application. It translates all request on something php likes. See &lt;a href=&quot;https://raw.githubusercontent.com/mercuriete/laravel-docker-redis-nginx/master/default.conf&quot;&gt;default.conf&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Php&lt;/strong&gt;: This is our application. Is made with laravel php framework. This is the component we want to scale. This application can potentially have hundreds of request that consumes lots of cpu resources.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Database&lt;/strong&gt;: This is our persistence. We need to ensure this container always is back up in a regular basis and uses a persistent volume. How to create persistent volumes is out of scope of this tutorial.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Redis&lt;/strong&gt;: This is the mecanism that allows the application to be Stateless. It records all session tokens in order to avoid to have any state on the php application. We don’t need to persist the state of redis, but if you lose the redis information, all your customers are logged out from your application. Be careful about how many times you restart this container.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;deploying-our-application&quot;&gt;Deploying our application&lt;/h5&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker stack deploy &lt;span class=&quot;nt&quot;&gt;--compose-file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;docker-stack-compose.yml laravel
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;output:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Creating network laravel_default  
Creating service laravel_database  
Creating service laravel_redis  
Creating service laravel_nginx  
Creating service laravel_php  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we have up and running our stack. We can check it with the following command.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker service &lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;output:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ID NAME MODE REPLICAS IMAGE PORTS  
05kyuuxj36bj laravel_database replicated 0/1 mysql:5.7  
ofgn8q9rmmpw laravel_nginx replicated 0/1 mercuriete/laravel-redis-nginx:latest &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;:80-&amp;gt;80/tcp  
lzdl6gdr9irr laravel_php replicated 0/1 mercuriete/laravel-redis:latest  
39anu4sg762z laravel_redis replicated 1/1 redis:4.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h5 id=&quot;scaling-our-application&quot;&gt;Scaling our application&lt;/h5&gt;
&lt;p&gt;We need to scale our application horizontally in order to comply with the new amount of users that required more performance in our application.&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker service scale &lt;span class=&quot;nv&quot;&gt;laravel_php&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;output:&lt;/p&gt;
&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;laravel_php scaled to 2  
overall progress: 2 out of 2 tasks  
1/2: running &lt;span class=&quot;o&quot;&gt;[==================================================&amp;gt;]&lt;/span&gt;  
2/2: running &lt;span class=&quot;o&quot;&gt;[==================================================&amp;gt;]&lt;/span&gt;  
verify: Service converged
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;now our application should be like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://g.gravizo.com/svg?  digraph G {    nginx [shape=box]    routing_mesh [shape=box]    php1 [shape=box]    php2 [shape=box]    database [shape=box]    redis [shape=box]    nginx -&amp;gt; routing_mesh;    routing_mesh -&amp;gt; php1;    routing_mesh -&amp;gt; php2;    php1 -&amp;gt; database;    php1 -&amp;gt; redis;    php2 -&amp;gt; database;    php2 -&amp;gt; redis;  } &quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we have 2 instances of php behind a load balancer. This balancer is called Ingress and sometimes docker call it &lt;em&gt;“the routing mesh”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;You can read more documentation &lt;a href=&quot;https://docs.docker.com/engine/swarm/ingress/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://docs.docker.com/engine/swarm/images/ingress-routing-mesh.png&quot; alt=&quot;routing mesh&quot; /&gt;&lt;/p&gt;

&lt;h5 id=&quot;rolling-updates&quot;&gt;Rolling Updates&lt;/h5&gt;
&lt;p&gt;Rolling updates is a feature of docker swarm that allows you to upgrade your containers with a new release of your code with 0 downtime even in only 1 node cluster.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;First we have to make a commit of our code and wait to our CI tool to recreate the image and upload It to our registry. We are using dockerhub automated builds to achieve this.&lt;/li&gt;
  &lt;li&gt;Type in the shell the followint command. &lt;em&gt;Update delay&lt;/em&gt; is wait until the next instance is recreated. Under the hood swarm is recreating the service one by one with a delay of 30 seconds. This means we have some time the application with both versions of our application at the same time, but is up all the time.
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker service update laravel_php &lt;span class=&quot;nt&quot;&gt;--update-delay&lt;/span&gt; 30s &lt;span class=&quot;nt&quot;&gt;--image&lt;/span&gt; mercuriete/laravel-redis  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
    &lt;p&gt;output:&lt;/p&gt;
    &lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;laravel_php  
overall progress: 2 out of 2 tasks  
1/2: running &lt;span class=&quot;o&quot;&gt;[==================================================&amp;gt;]&lt;/span&gt;  
2/2: running &lt;span class=&quot;o&quot;&gt;[==================================================&amp;gt;]&lt;/span&gt;  
verify: Service converged  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h5 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h5&gt;
&lt;p&gt;Nowadays we have more tools to achieve horizontal scaling in web applications.
I remember when I had to configure HAProxy in an 8 node distributed application. It was a nightmare.&lt;/p&gt;

&lt;p&gt;In Addition, we can deploy our releases in a way that nobody can realise whether our application is being updated or not.&lt;/p&gt;

&lt;h5 id=&quot;next-steps&quot;&gt;Next steps&lt;/h5&gt;
&lt;p&gt;In the following post we will be learning to use &lt;a href=&quot;https://portainer.io/&quot;&gt;&lt;strong&gt;portainer&lt;/strong&gt;&lt;/a&gt; to do the same tutorial but with a graphical frontend with a few clicks.&lt;/p&gt;

</description>
        <pubDate>Sat, 25 Aug 2018 12:00:00 +0000</pubDate>
        <link>https://mercuriete.github.io/article/scalable-web-applications</link>
        <guid isPermaLink="true">https://mercuriete.github.io/article/scalable-web-applications</guid>
        
        
        <category>swarm</category>
        
        <category>docker</category>
        
        <category>php</category>
        
        <category>laravel</category>
        
      </item>
    
      <item>
        <title>Detecting memory leaks in c++ code</title>
        <description>&lt;p&gt;Memory leaks are a very common problem in c++ development that can cause severe
problems due to difficult to found the problem.&lt;/p&gt;

&lt;h5 id=&quot;windows-version&quot;&gt;Windows version&lt;/h5&gt;

&lt;p&gt;For finding memory leaks in Microsoft Visual Studio builds we are using CRT library following the official documentation.
&lt;a href=&quot;https://docs.microsoft.com/visualstudio/debugger/finding-memory-leaks-using-the-crt-library&quot;&gt;Finding Memory Leaks Using the CRT Library&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I created a helper to make more usable that library. I will explain it line by line
&lt;a href=&quot;https://github.com/mercuriete/cpp-memory-leak-detection/blob/master/lib/include/memory_leak.h&quot;&gt;Memory leak detection helper&lt;/a&gt;&lt;/p&gt;

&lt;h5 id=&quot;preprocesor-header&quot;&gt;Preprocesor header&lt;/h5&gt;
&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#ifndef MEMORY_LEAK_HPP
#define MEMORY_LEAK_HPP
#if defined(_WIN32) || defined(_WIN64)
#ifdef _DEBUG
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;We only want to generate code in windows architectures and only when debugging.&lt;/p&gt;

&lt;h5 id=&quot;macro-for-new-expansion&quot;&gt;macro for new expansion&lt;/h5&gt;
&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This automatically expands a new sentence into a new ( _NORMAL_BLOCK , FILE , LINE )&lt;/p&gt;

&lt;p&gt;The purpouse of this macro is to have more accurate line count in the memory leak reports&lt;/p&gt;

&lt;h5 id=&quot;library-initialization&quot;&gt;library initialization&lt;/h5&gt;
&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialize_memory_leak_detector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;initializing memory leak detector&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;_CrtSetDbgFlag&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_CRTDBG_ALLOC_MEM_DF&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_CRTDBG_LEAK_CHECK_DF&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_leak_initizalization_result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;initialize_memory_leak_detector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This global variable is useless but this triggers a initialization before the main was called. Probably this sentence is called before your code is started.&lt;/p&gt;

&lt;p&gt;This function sets debug flags according with the microsoft documentation for dump a memory leak report on exit of your application&lt;/p&gt;

&lt;h5 id=&quot;at-exit-handler&quot;&gt;at exit handler&lt;/h5&gt;
&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;atexit_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
   &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;ending memory leak detector and dumping report&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_CrtDumpMemoryLeaks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;memory leak detected. please debug with Visual Studio&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;no memory leaks detected&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   &lt;span class=&quot;c1&quot;&gt;//Hahahahaha exit on an exit handler!
&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//feel free to blame me if you get something really bad.
&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;memory_leak_dump_report_at_exit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;atexit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;atexit_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;This function is registered at start of your application and is called just before you application is exiting. This dumps a memory leak report on debug logger that is not available to read in continuous integration tools. This is why we exit with an exit code to break the continous integration pipeline once we detect a memory leak.&lt;/p&gt;

&lt;p&gt;As you can see, I am calling exit inside an at exit handler. This is totally forbidden due to a possible infinite loop but it works as I intended.&lt;/p&gt;

&lt;h5 id=&quot;linux--mac-version&quot;&gt;Linux &amp;amp; Mac version&lt;/h5&gt;
&lt;p&gt;On Unix systems ASAN library from google will be used. This library is integrated in g++ and clang compilers and can be used as easy as setting a flag during comilation.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/mercuriete/cpp-memory-leak-detection/blob/develop/CMakeLists.txt#L11&quot;&gt;example on github&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The only thing we need to do is pass to the compiler the flags:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-g -O0 -fsanitize=leak&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;-g: is for debugging symbols&lt;/p&gt;

&lt;p&gt;-o0 is for don’t optimization for accurate line number count&lt;/p&gt;

&lt;p&gt;-fsanitize=leak is for activate ASAN with low overhead only detecting leaks.&lt;/p&gt;

&lt;p&gt;Then when you start you application you will need to add an enviromental variable
&lt;a href=&quot;https://github.com/mercuriete/cpp-memory-leak-detection/blob/develop/.travis.yml#L107&quot;&gt;example on travis&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ASAN_OPTIONS=detect_leaks=1 ./app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;with that done you will have something like this:
&lt;a href=&quot;https://travis-ci.org/mercuriete/cpp-memory-leak-detection/jobs/417463458#L686&quot;&gt;travis build log&lt;/a&gt;&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Direct leak of 8 byte(s) in 1 object(s) allocated from:
 #0 0x7fb8069879f8 in operator new(unsigned long) (liblsan.so.0+0xf9f8)
 #1 0x40bd8c in main /app/src/main.cpp:13
 #2 0x7fb8060a2f44 in __libc_start_main (libc.so.6+0x21f44)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;then you can go to the &lt;a href=&quot;https://github.com/mercuriete/cpp-memory-leak-detection/blob/develop/app/src/main.cpp#L13&quot;&gt;offending line&lt;/a&gt; and check what is happening&lt;/p&gt;

&lt;div class=&quot;language-cpp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leaking_pointer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;leaking object&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//You forgot to delete pointer
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leaking_pointer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;leaking_pointer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;delete&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;leaking_pointer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Is really obvious whats going on. You forgot to delete a pointer before reuse for another instance.&lt;/p&gt;

&lt;h5 id=&quot;conclusions&quot;&gt;Conclusions&lt;/h5&gt;
&lt;p&gt;Given that I have a bias about linux vs windows, Is obvious to understand that on the windows counterpart you have to modify your code a little bit in order to enable memory sanitizer tools.
With ASAN you only need to compile you code with debug symbols and ASAN will take care of analyze the heap for you.&lt;/p&gt;

&lt;p&gt;Summarizing, there are some tools that can improve our code quality with a little bit of effort. This days are not acceptable to have such a problem comparing with days when instrumenting your application were more difficult.&lt;/p&gt;
</description>
        <pubDate>Sat, 18 Aug 2018 12:00:00 +0000</pubDate>
        <link>https://mercuriete.github.io/article/detecting-memory-leaks</link>
        <guid isPermaLink="true">https://mercuriete.github.io/article/detecting-memory-leaks</guid>
        
        
        <category>c++</category>
        
      </item>
    
  </channel>
</rss>
