<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yash  Kothari</title>
    <description>The latest articles on DEV Community by Yash  Kothari (@captaindavinci).</description>
    <link>https://dev.to/captaindavinci</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F255829%2F24e5468a-3d2a-433b-a642-b673e90cc1bb.png</url>
      <title>DEV Community: Yash  Kothari</title>
      <link>https://dev.to/captaindavinci</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/captaindavinci"/>
    <language>en</language>
    <item>
      <title>Genetic Algorithm to find optimal compiler flags</title>
      <dc:creator>Yash  Kothari</dc:creator>
      <pubDate>Thu, 21 May 2020 13:36:33 +0000</pubDate>
      <link>https://dev.to/captaindavinci/genetic-algorithm-to-find-optimal-compiler-flags-idk</link>
      <guid>https://dev.to/captaindavinci/genetic-algorithm-to-find-optimal-compiler-flags-idk</guid>
      <description>&lt;p&gt;&lt;em&gt;This post supports my entry into &lt;a href="https://github.com/education/graduation"&gt;$ git remote graduation&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Compiler flags offer control over which optimizations should be enabled/disabled during the compilation of a program. A compiler like GCC offers &lt;em&gt;~60&lt;/em&gt; flags related to different types of optimization, a list of these flags can be found &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html"&gt;here&lt;/a&gt;. These flags can affect the execution time, binary file size, power consumption, etc.&lt;/p&gt;

&lt;p&gt;This project focuses on finding optimal GCC flags for a given C program to improve its execution time and benchmark it using &lt;a href="http://vhosts.eecs.umich.edu/mibench/Publications/MiBench.pdf"&gt;MiBench&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Genetic Algorithm
&lt;/h2&gt;

&lt;p&gt;A large search space of about &lt;em&gt;2&lt;sup&gt;60&lt;/sup&gt;&lt;/em&gt; combination of flags makes it impossible to try all possibilities, an evolutionary algorithm starts with a random set of population and over generations of selection, cross-over and mutation tries to converge to a global optimal solution. Each member of the population has a DNA which is a binary string of 58 characters corresponding to the compiler flags.&lt;/p&gt;

&lt;p&gt;Pseudo code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;init_population()
calculate_fitness()
while generation &amp;lt; MAX_GENERATIONS:
    perform_selection()
    perform_mutation()
    calculate_fitness()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Selection involves,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elitism, maintaining the top &lt;em&gt;10%&lt;/em&gt; of the population of current generation in the next generation&lt;/li&gt;
&lt;li&gt;Crossover, selecting two parents and producing a child using one point cross over with &lt;em&gt;60%&lt;/em&gt; probability.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mutation performs a bit-flip at a random position in the DNA of a member with &lt;em&gt;1%&lt;/em&gt; probability.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results
&lt;/h2&gt;

&lt;p&gt;To conclude the project we decided to simulate the process of genetic algorithm over various generations by storing population data for each generation and plotting the fitness graph on a web browser. Here, is an example of one such plot,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t10qXsQ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pmqxr4qxaci9ir9tkjje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t10qXsQ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pmqxr4qxaci9ir9tkjje.png" alt="Fitness vs Generation graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fitness is calculated as, &lt;em&gt;1 / execution time&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tech stack
&lt;/h2&gt;

&lt;p&gt;The core algorithm was implemented using &lt;em&gt;Python&lt;/em&gt; and the front-end simulation was implemented using Angular. The data for each generation is stored in a JSON file. &lt;/p&gt;

&lt;p&gt;One of the most important task was to calculate the execution time, I used the &lt;code&gt;timeit&lt;/code&gt; and &lt;code&gt;subprocess&lt;/code&gt; module to accomplish this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        stmt = 'subprocess.run({}, stderr=subprocess.STDOUT,\
        stdout=subprocess.DEVNULL, check=True)'.format(cmd_list)
        return timeit.timeit(stmt=stmt,
                             setup='import subprocess',
                             number=iterations) / iterations
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I also learnt about how Angular updated in the DOM by evaluating expressions repeatedly, for my use case I needed more control over when the DOM gets updated and came across &lt;code&gt;ChangeDetectorRef&lt;/code&gt; which does exactly that. &lt;/p&gt;

&lt;h2&gt;
  
  
  Link to Code
&lt;/h2&gt;

&lt;p&gt;Code is available on &lt;a href="https://github.com/soumyas98/Compiler"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This project provided me with various opportunities to learn more about compilers, optimization, reading research papers, and trying out new things which were just out of my comfort zone. The next steps that I have in mind is to run it on a larger population and generation size, using different crossover and mutation rates.&lt;/p&gt;

&lt;p&gt;Thanks for reading! &lt;/p&gt;

</description>
      <category>octograd2020</category>
      <category>python</category>
      <category>angular</category>
      <category>devgrad2020</category>
    </item>
  </channel>
</rss>
