Verified Commit 974f9c42 authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

woops

parents
IMAGES_FOLDER ?= $(INPUT_FOLDER)/images
INPUT_FOLDER ?= $(shell pwd)
OUTPUT_FOLDER ?= $(shell pwd)/dist
slides:
@pandoc slides.md \
--to revealjs \
--output barcelona.html \
--template template/index.html \
-V revealjs-url=template \
-V progress=false \
-V slideNumber=true \
-V history=true \
--standalone --slide-level 1
# Concurrency data correctness and rust
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <script src="https://twemoji.maxcdn.com/2/twemoji.min.js?11.0"></script> -->
<meta name="generator" content="pandoc">
<title>slides</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="template/css/reveal.css">
<style type="text/css">
code{white-space: pre;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.reveal a[href^="#"]:visited {
color: #8ea9d2;
}
</style>
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = "template/css/theme/.css";
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<link rel="stylesheet" href="template/css/theme/black.css" id="theme">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'template/css/print/pdf.css' : 'template/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="template/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="slide level1">
<h2 id="concurrency-data-correctness-and-rust">Concurrency, Data Correctness, and Rust</h2>
<aside class="notes">
<p>Yo and welcome to my talk</p>
</aside>
</section>
<section class="slide level1">
<h2 id="whoami1"><code>whoami(1)</code></h2>
<ul>
<li>Name: Katharina Fey</li>
<li>Occupation: Software researcher</li>
<li>Location: Berlin</li>
<li>Using Rust since 2017</li>
<li>Currently at Ockam as senior architect</li>
<li>Side project: irdest</li>
</ul>
</section>
<section id="what-are-all-these-buzzwords" class="slide level1">
<h1>What are all these buzzwords?!</h1>
<ul>
<li>Concurrency</li>
<li>Parallelism</li>
<li>Data correctness</li>
<li>Data race</li>
</ul>
<aside class="notes">
<p>Before we begin I want to make sure everybody has a good understanding of what problems we are trying to solve here. This talk contains some jargon that I would like to explain to you first.</p>
</aside>
</section>
<section class="slide level1">
<h2 id="concurrency">Concurrency</h2>
<ul>
<li>Split up work across “units of work”</li>
<li>Work units are paused and resumed sequentially</li>
<li>Fast switching gives the illusion of parallel processing</li>
</ul>
<aside class="notes">
<p>Doing multiple things at the same time, in different independent units of execution.</p>
</aside>
</section>
<section class="slide level1">
<p><img data-src="./imgs/concurrency1.png" /></p>
</section>
<section class="slide level1">
<h2 id="parallelism">Parallelism</h2>
<ul>
<li>Requires splitting up work across “units of work”</li>
<li>Execute different “work units” at the same time</li>
</ul>
<aside class="notes">
<p>Splitting program execution across different work threads (or processes or computers)</p>
</aside>
</section>
<section class="slide level1">
<p><img data-src="./imgs/parallelism1.png" /></p>
</section>
<section class="slide level1">
<h2 id="data-correctness">Data correctness</h2>
<ul>
<li>“Does X solve problem Y correctly?”</li>
<li>Requires a specification</li>
<li>Requires intensive testing</li>
</ul>
<aside class="notes">
<p>The assurance that an operation performed on a system yields the correct (and verifiable!) result</p>
</aside>
</section>
<section class="slide level1">
<h2 id="race-condition">Race condition</h2>
<ul>
<li>Happens when “data correctness” is not given</li>
<li>Invalid results because of parallelism or concurrency</li>
</ul>
<aside class="notes">
<p>This occurs when the above guarantee is not given, i.e. data is manipulated in a way that causes a “race condition” (will explain what this means!)</p>
</aside>
</section>
<section class="slide level1">
<p><img data-src="./imgs/race_condition1.png" /></p>
</section>
<section id="how-do-computers" class="slide level1">
<h1>How do computers?</h1>
</section>
<section class="slide level1">
<h2 id="how-do-computers-1">How do computers?</h2>
<p><img data-src="./imgs/cpu1.png" /></p>
<aside class="notes">
<p>It’s good to keep a model of our computer in mind when writing code. Many errors might seem obvious once we do this.</p>
<p>CPU: Executes instructions on cores and threads. Each thread has its own cache with a shared cache between them.</p>
<p>Memory: Connected to the CPU, usually <em>very</em> slow to access in CPU scale time. This is why values get (and stay) cached.</p>
<p>Synchronising Cache and Memory is the CPUs job. We don’t have to worry. BUT: concurrent prograbs CAN break this mechanism.</p>
</aside>
</section>
<section id="some-examples" class="slide level1">
<h1>Some examples</h1>
</section>
<section class="slide level1">
<h2 id="example-1">Example 1</h2>
<p><em>“Classic” race condition</em></p>
</section>
<section class="slide level1">
<h2 id="example-2">Example 2</h2>
<p><em>Other example</em></p>
</section>
<section id="solutions" class="slide level1">
<h1>Solutions</h1>
</section>
<section class="slide level1">
<h2 id="mutex">Mutex</h2>
<ul>
<li>What is the “critical section” of your program?</li>
<li>Lock section per execution thread</li>
</ul>
</section>
<section class="slide level1">
<h2 id="atomics">Atomics</h2>
<ul>
<li>Hardware enabled feature</li>
<li>Allow simple operations on memory with synchronisation</li>
</ul>
</section>
<section class="slide level1">
<h2 id="semaphore">Semaphore</h2>
<ul>
<li><p>Built on top of atomics</p></li>
<li><p>Counting and binary semaphores</p></li>
<li><p>https://www.baeldung.com/cs/semaphore-vs-mutex</p></li>
</ul>
</section>
<section id="in-practice" class="slide level1">
<h1>In practice</h1>
</section>
<section class="slide level1">
<h2 id="python-1">Python #1</h2>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">buffer</span> <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> here_be_race_conditions(d):</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">pass</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a>spawn(here_be_race_conditions(<span class="dv">1</span>))</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a>spawn(here_be_race_conditions(<span class="op">-</span><span class="dv">1</span>))</span></code></pre></div>
</section>
<section class="slide level1">
<h2 id="python-2">Python #2</h2>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">buffer</span> <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> here_be_race_conditions(d):</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">pass</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a>spawn(here_be_race_conditions(<span class="dv">5</span>))</span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>spawn(here_be_race_conditions(<span class="dv">12</span>))</span></code></pre></div>
</section>
<section id="rust" class="slide level1">
<h1>Rust</h1>
</section>
<section class="slide level1">
<h2 id="how-does-rust">How does Rust?</h2>
<p>Rust prevents many of these issues</p>
<ul>
<li>Memory management</li>
<li>A concurrency aware type system</li>
</ul>
</section>
<section class="slide level1">
<h2 id="memory-management">Memory management</h2>
<ul>
<li>The previous talk covered the basics so I won’t repeat them here</li>
<li>What “thread” owns what data</li>
<li>Show diagram of CPU and memory ownership</li>
</ul>
</section>
<section class="slide level1">
<h2 id="concurrency-typesystem">Concurrency typesystem</h2>
<ul>
<li><code>Send</code> – “Data can be sent to a different thread”</li>
<li><code>Sync</code> – “Data can be shared between different threads”</li>
</ul>
</section>
<section class="slide level1">
<h2 id="demo">Demo</h2>
<p>The python code doesn’t compile!</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode rust"><code class="sourceCode rust"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">fn</span> main() <span class="op">{</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">let</span> <span class="kw">mut</span> buffer <span class="op">=</span> <span class="dv">0</span><span class="op">;</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="pp">std::thread::</span>spawn(<span class="op">||</span> <span class="op">{</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>buffer <span class="op">+=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span>)<span class="op">;</span></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="pp">std::thread::</span>spawn(<span class="op">||</span> <span class="op">{</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a> <span class="op">*</span>buffer <span class="op">-=</span> <span class="dv">1</span><span class="op">;</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="op">}</span>)<span class="op">;</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div>
</section>
<section class="slide level1">
<h2 id="theory-in-rust">Theory in Rust</h2>
<ul>
<li><code>Mutex</code>, <code>RwLock</code></li>
<li><code>std::sync::atomics</code></li>
<li><code>Barrier</code></li>
</ul>
</section>
<section class="slide level1">
<h2 id="rc-vs-arc">Rc vs Arc</h2>
<ul>
<li>Arc is atomically reference counted</li>
<li>Needed for all sorts of concurrency structures</li>
</ul>
</section>
<section class="slide level1">
<h2 id="channels">Channels!</h2>
<ul>
<li>Don’t share data</li>
<li>Send it to your friend</li>
</ul>
</section>
<section class="slide level1">
<h2 id="how-to-use-all-this">How to use all this?</h2>
<ul>
<li>Don’t write your own concurrency toolkit</li>
<li>There are plenty already out there</li>
</ul>
</section>
<section class="slide level1">
<h2 id="crossbeam">crossbeam</h2>
<ul>
<li>It good</li>
</ul>
</section>
<section class="slide level1">
<h2 id="rayon">rayon</h2>
<ul>
<li>It also good</li>
</ul>
</section>
<section class="slide level1">
<h2 id="please-ask-questions">Please ask questions</h2>
</section>
<section class="slide level1">
<p>Bai bai</p>
</section>
</div>
</div>
<script src="template/lib/js/head.min.js"></script>
<script src="template/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Display controls in the bottom right corner
controls: true,
// Display a presentation progress bar
progress: false,
// Display the page number of the current slide
slideNumber: true,
// Push each slide change to the browser history
history: true,
// Enable keyboard shortcuts for navigation
keyboard: true,
// Enable the slide overview mode
overview: true,
// Vertical centering of slides
center: true,
// Enables touch navigation on devices with touch input
touch: true,
// Loop the presentation
loop: false,
// Change the presentation direction to be RTL
rtl: false,
// Turns fragments on and off globally
fragments: true,
// Flags if the presentation is running in an embedded mode,
// i.e. contained within a limited portion of the screen
embedded: false,
// Flags if we should show a help overlay when the questionmark
// key is pressed
help: true,
// Flags if speaker notes should be visible to all viewers
showNotes: false,
// Number of milliseconds between automatically proceeding to the
// next slide, disabled when set to 0, this value can be overwritten
// by using a data-autoslide attribute on your slides
autoSlide: 0,
// Stop auto-sliding after user input
autoSlideStoppable: true,
// Enable slide navigation via mouse wheel
mouseWheel: false,
// Opens links in an iframe preview overlay
previewLinks: false,
// Transition style
transition: 'none', // none/fade/slide/convex/concave/zoom
// Transition speed
transitionSpeed: 'default', // default/fast/slow
// Transition style for full page slide backgrounds
backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
// Number of slides away from the current that are visible
viewDistance: 3,
// Optional reveal.js plugins
dependencies: [
{ src: 'template/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'template/plugin/zoom-js/zoom.js', async: true },
{ src: 'template/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "reveal-dev";
buildInputs = with pkgs; [ gnumake pandoc inotify-tools ];
}
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="800"
height="350"
viewBox="0 0 211.66684 92.604197"
version="1.1"
id="svg5"
inkscape:export-filename="/home/Documents/talks/data-concurrency/imgs/race_condition1.png"
inkscape:export-xdpi="200.064"
inkscape:export-ydpi="200.064"
inkscape:version="1.1 (c68e22c387, 2021-05-23)"
sodipodi:docname="race_condition1.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#505050"
bordercolor="#eeeeee"
borderopacity="1"
inkscape:pageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="0"
inkscape:document-units="px"
showgrid="false"
width="1365px"
inkscape:zoom="1"
inkscape:cx="341.49999"
inkscape:cy="251.99999"
inkscape:window-width="1274"
inkscape:window-height="1364"
inkscape:window-x="0"
inkscape:window-y="48"
inkscape:window-maximized="1"
inkscape:current-layer="layer1"
inkscape:snap-to-guides="true"
inkscape:snap-midpoints="false"
inkscape:snap-nodes="true"
inkscape:snap-bbox="false"
inkscape:snap-global="true">
<inkscape:grid
type="xygrid"
id="grid13689" />
</sodipodi:namedview>
<defs
id="defs2">
<marker
style="overflow:visible"
id="Arrow2Mend"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Mend"
inkscape:isstock="true">
<path
transform="scale(-0.6)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round"
id="path17890" />
</marker>
<marker
style="overflow:visible"
id="Arrow2Lend"
refX="0"
refY="0"
orient="auto"
inkscape:stockid="Arrow2Lend"
inkscape:isstock="true">
<path
transform="matrix(-1.1,0,0,-1.1,-1.1,0)"
d="M 8.7185878,4.0337352 -2.2072895,0.01601326 8.7185884,-4.0017078 c -1.7454984,2.3720609 -1.7354408,5.6174519 -6e-7,8.035443 z"
style="fill:context-stroke;fill-rule:evenodd;stroke-width:0.625;stroke-linejoin:round"
id="path17884" />
</marker>
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<g
id="g63772"
transform="translate(0,5.9532893)">
<g
id="g63719">
<rect
style="fill:#666666;stroke:#666666;stroke-width:1.10778;stroke-linecap:square"
id="rect846"
width="31.965155"
height="22.704733"
x="17.607967"
y="31.824324" />
<text
xml:space="preserve"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:10.5833px;line-height:1.25;font-family:'Iosevka Kookie';-inkscape-font-specification:'Iosevka Kookie Bold';letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264583"
x="30.944719"
y="47.066051"
id="text2324"><tspan
sodipodi:role="line"
id="tspan2322"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-family:'Iosevka Kookie';-inkscape-font-specification:'Iosevka Kookie Bold';fill:#ffffff;stroke-width:0.264583"
x="30.944719"
y="47.066051">A</tspan></text>
</g>
<g
id="g53797"
transform="translate(-0.05423314,6.7313807)">
<path
style="fill:#999999;stroke:#666666;stroke-width:1.05833;stroke-linecap:round;stroke-linejoin:bevel;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1;marker-end:url(#Arrow2Mend);paint-order:normal"
d="m 51.744021,25.092943 h 35.83278"
id="path17861"
sodipodi:nodetypes="cc" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:5.64445px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264583"
x="61.844482"
y="21.622772"
id="text35790"><tspan
sodipodi:role="line"
id="tspan35788"
style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64445px;font-family:'Iosevka Kookie';-inkscape-font-specification:'Iosevka Kookie';fill:#ffffff;stroke-width:0.264583"
x="61.844482"
y="21.622772">set(4)</tspan></text>
</g>