<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title></title>
	<link href="https://bcheng.me/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://bcheng.me"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2021-01-15T00:00:00+00:00</updated>
	<id>https://bcheng.me/atom.xml</id>
	<entry xml:lang="en">
		<title>Nvidia Compute + VFIO Setup</title>
		<published>2021-01-15T00:00:00+00:00</published>
		<updated>2021-01-15T00:00:00+00:00</updated>
		<link rel="alternate" href="https://bcheng.me/blog/nvidia-vfio-setup/" type="text/html"/>
		<id>https://bcheng.me/blog/nvidia-vfio-setup/</id>
		<content type="html">&lt;p&gt;I recently got my hands on a Nvidia RTX 2070 Super. Despite being much more
powerful than my RX580, at first I was going to use it for VM passthrough only,
and not for daily use.
However AMD recently discontinued support for ROCm on Polaris chips so I guess
I now need to use the 2070 for compute purposes such as machine learning &lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. 
Obviously these two scenarios require different drivers: &lt;code&gt;nvidia&lt;&#x2F;code&gt; for the host
compute, and &lt;code&gt;vfio-pci&lt;&#x2F;code&gt; for passthrough. Switching between the two is easy, but
requires a sequence of unbinds and binds for each of the (4!!) PCI devices that
are exposed.&lt;&#x2F;p&gt;
&lt;p&gt;To help ease this driver switching I wrote a wrote script called
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lolzballs&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bin&#x2F;pci-bind&quot;&gt;pci-bind&lt;&#x2F;a&gt;,
which handles the unbinding and binding a new driver for a group of devices with
a certain PCI address.&lt;&#x2F;p&gt;
&lt;p&gt;To bind &lt;code&gt;nvidia&lt;&#x2F;code&gt; for compute I can just run:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;sudo pci-bind nvidia 0000:0a:00
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Replacing &lt;code&gt;nvidia&lt;&#x2F;code&gt; with &lt;code&gt;vfio-pci&lt;&#x2F;code&gt; does the opposite, which is what I put at
the top of my &lt;code&gt;qemu&lt;&#x2F;code&gt; script.&lt;&#x2F;p&gt;
&lt;p&gt;On my system &lt;code&gt;lspci&lt;&#x2F;code&gt; gives:&lt;&#x2F;p&gt;
&lt;pre&gt;&lt;code&gt;...
0a:00.0 VGA compatible controller: NVIDIA Corporation TU104 [GeForce RTX 2070 SUPER] (rev a1)
0a:00.1 Audio device: NVIDIA Corporation TU104 HD Audio Controller (rev a1)
0a:00.2 USB controller: NVIDIA Corporation TU104 USB 3.1 Host Controller (rev a1)
0a:00.3 Serial bus controller [0c80]: NVIDIA Corporation TU104 USB Type-C UCSI Controller (rev a1)
...
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and each of these live in &lt;code&gt;&#x2F;sys&#x2F;bus&#x2F;pci&#x2F;devices&#x2F;0000:0a:00.{0-3}&lt;&#x2F;code&gt;. Hopefully you
can see where the &lt;code&gt;0000:0a:00&lt;&#x2F;code&gt; argument comes from (it&#x27;s just a prefix).&lt;&#x2F;p&gt;
&lt;p&gt;One issue I&#x27;ve experienced with this setup is that XWayland will pick up the
Nvidia drivers and hold &lt;code&gt;&#x2F;dev&#x2F;nvidia0&lt;&#x2F;code&gt; open so that the &lt;code&gt;nvidia&lt;&#x2F;code&gt; module will
refuse the unbind request. I found that this was due to &lt;code&gt;glvnd&lt;&#x2F;code&gt; loading the
Nvidia EGL ICD from &lt;code&gt;&#x2F;usr&#x2F;share&#x2F;glvnd&#x2F;egl_vendor.d&#x2F;10_nvidia.json&lt;&#x2F;code&gt;.
Unfortunately I couldn&#x27;t find a way to simply not install the Nvidia GL driver
but include the &lt;code&gt;nvidia&lt;&#x2F;code&gt; kernel driver (i.e. a headless driver of some sort) on
Arch. Anyways to stop &lt;code&gt;glvnd&lt;&#x2F;code&gt; from loading the &lt;code&gt;nvidia&lt;&#x2F;code&gt; driver, I simply added
the following to force EGL to load &lt;code&gt;mesa&lt;&#x2F;code&gt; only:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;export __EGL_VENDOR_LIBRARY_FILENAMES=&amp;#x2F;usr&amp;#x2F;share&amp;#x2F;glvnd&amp;#x2F;egl_vendor.d&amp;#x2F;50_mesa.json
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I also use flatpak, which will end up trying to install the Nvidia GL drivers
as a flatpak package. To disable this I just masked out the driver package:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;flatpak mask &amp;quot;org.freedesktop.Platform.GL.nvidia*&amp;quot;
&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
&lt;p&gt;screw Nvidia for making CUDA proprietary and not advancing open standards
like OpenCL, but also screw AMD for discontinuing ROCm support randomly on a
fairly recent card. Hopefully Intel comes up with something better, oh wait
they did and SYCL is just &lt;em&gt;another&lt;&#x2F;em&gt; standard.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Site Redesign</title>
		<published>2021-01-06T00:00:00+00:00</published>
		<updated>2021-01-06T00:00:00+00:00</updated>
		<link rel="alternate" href="https://bcheng.me/blog/redesign/" type="text/html"/>
		<id>https://bcheng.me/blog/redesign/</id>
		<content type="html">&lt;p&gt;Wow... it&#x27;s been a long time since I considered updating my website.&lt;&#x2F;p&gt;
&lt;p&gt;Ever since I started using sway, I started following the sourcehut guys&#x27;
(&lt;a href=&quot;https:&#x2F;&#x2F;drewdevault.com&quot;&gt;Drew Devault&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;emersion.fr&quot;&gt;Simon Ser&lt;&#x2F;a&gt;)
blogs. They have really influenced my philosophy, so I decided to redesign my
website using a static site generator (&lt;a href=&quot;https:&#x2F;&#x2F;getzola.org&quot;&gt;Zola&lt;&#x2F;a&gt;). The keener
eyed will notice a few stylistic similarities in the site (i.e. the layout of
the home page). No more node backend, no more authentication, no more dependencies
going out of date -- just plain HTML generated from Markdown.&lt;&#x2F;p&gt;
&lt;p&gt;In addition I&#x27;ve tried to minimize the number of styles I&#x27;m using while keeping
things aesthetically pleasing to make things as &lt;em&gt;fff&lt;&#x2F;em&gt;ast as possible. I quite
like how things turned out!&lt;&#x2F;p&gt;
&lt;p&gt;I hope to continue writing, maybe once or twice a month is a reasonable target
for me.&lt;&#x2F;p&gt;
&lt;p&gt;By the way, the source is available here: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lolzballs&#x2F;website&quot;&gt;https:&#x2F;&#x2F;github.com&#x2F;lolzballs&#x2F;website&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Visualising Electric Fields with WebGL</title>
		<published>2017-12-14T00:00:00+00:00</published>
		<updated>2017-12-14T00:00:00+00:00</updated>
		<link rel="alternate" href="https://bcheng.me/blog/visualising-electric-fields-with-webgl-kinda/" type="text/html"/>
		<id>https://bcheng.me/blog/visualising-electric-fields-with-webgl-kinda/</id>
		<content type="html">&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;static.bcheng.me&#x2F;electric-fields&#x2F;example.png&quot; alt=&quot;Simulator&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I recently saw a few blog posts (such as &lt;a href=&quot;https:&#x2F;&#x2F;lukezapart.com&#x2F;newtons-method&quot;&gt;this&lt;&#x2F;a&gt;), which inspired me to do something with WebGL. In Physics class we just finished learning about electric and magnetic fields, and I wanted to see what electric field lines would actually look like, as opposed to something that I just drew on paper. My visualisation can be seen &lt;a href=&quot;https:&#x2F;&#x2F;static.bcheng.me&#x2F;electric-fields&#x2F;&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;electric-field-lines&quot;&gt;Electric Field Lines&lt;&#x2F;h1&gt;
&lt;p&gt;An electric field is a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Vector_field&quot;&gt;vector field&lt;&#x2F;a&gt;, where each point in space has a direction and magnitude attached to it. In an electric field, these vectors represent the force that a charged object (1C) would experience when placed in that position. To illustrate this vector field, electric field lines are drawn emanating from a charge, which represents the path a charge would take, given an initial position, where the line starts.&lt;&#x2F;p&gt;
&lt;p&gt;My visualisation of this uses a normal 2D canvas for this (no WebGL &lt;em&gt;yet!&lt;&#x2F;em&gt;). A function called &lt;code&gt;getStrength&lt;&#x2F;code&gt; calculates the electric force at a given position. This calculation relies the superposition principle, where we sum up all the forces created by each charge in the simulation.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;point-charges&quot;&gt;Point Charges&lt;&#x2F;h3&gt;
&lt;p&gt;Each point charge creates a force F: \[ F = \frac{kqq_0}{r^2} \], where \(q\) is the charge of the source, \(q_0 = 1C\), and \(r\) is the distance from the charge to the position considered. Then initial starting points for paths are created, radially dividing point charges into divisions. These paths are then calculated using Euler&#x27;s method.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;plate-charges&quot;&gt;Plate Charges&lt;&#x2F;h3&gt;
&lt;p&gt;A plate charge is more difficult to calculate, as it is composed of infinitely many point charges of charge 0, which add up to a charge \(q\).
To compute the force exerted by a plate, we split up the plate into \(n\) point charges each with charge \(q \over n\), and compute the force exerted by these charges as a whole.&lt;&#x2F;p&gt;
&lt;p&gt;\[ F = \sum^n_{i = 0} \frac{k\frac{q}{n}}{r_i^2} \]&lt;&#x2F;p&gt;
&lt;p&gt;To extend this to 2D (forces are vectors, not scalars), we consider the vertical and horizontal components of this force separately. On each of those, we take the limit as \(n\) approaches infinity, resulting in an integral.&lt;&#x2F;p&gt;
&lt;p&gt;The force parallel to the plate results in
\[ F= \frac{kq}{l} \int_0^l \frac{r - p}{((r-p^2) + d^2)^\frac{3}{2}} dp \], where \(l\) is the length of the plate, \(r\) is the distance to the left endpoint of the plate (in direction parallel to the plate), and \(d\) is the distance to the plate in the direction perpendicular to the plate. (If only I knew linear algebra and vector calculus better...)&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;The expansion and vertical component of the force is left as an exercise to the reader.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;euler-s-method&quot;&gt;Euler&#x27;s method&lt;&#x2F;h1&gt;
&lt;p&gt;Given an initial position \(s_0\), we calculate the force \(F_0\) at that position using &lt;code&gt;getStrength&lt;&#x2F;code&gt;.
By Newton&#x27;s 2nd law we have \[F_0 = \frac{d^2s}{dt^2}\] for a unit mass.
After a small step \(dt\), the force will have moved this unit mass to a position \[s_1 = F_0 * dt\].
From here we calculate \(F_1\), again with &lt;code&gt;getStrength&lt;&#x2F;code&gt;, where we calculate a new position \(s_2\).
This process is repeated until a limit (currently 1000 in my program), where each position \(s\) is a point on the path.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;electric-potential&quot;&gt;Electric Potential&lt;&#x2F;h1&gt;
&lt;p&gt;Finally we get to the WebGL part! Since electric potential is a scalar value, I thought it would be a cool idea to show the electric potential around these charges using colours. To do this, we use a fragment shader where each fragment is coloured by the electric potential at that point. Electric potential is similar to electric field strength, but it represents the potential energy for a unit charge at a position and thus is directly inversely proportional to the distance.&lt;&#x2F;p&gt;
&lt;p&gt;\[ V = \frac{kqq_0}{r} \]&lt;&#x2F;p&gt;
&lt;p&gt;The calculations are all very similar to before, but this time there are no vector components!&lt;&#x2F;p&gt;
&lt;p&gt;For a plate charge the electric potential is calculated similarly to the electric field. There is only one scalar component now:&lt;&#x2F;p&gt;
&lt;p&gt;\[ V = \frac{kq}{l} \int_0^l \frac{1}{\sqrt{d^2 + (r- p)^2}} dp \]&lt;&#x2F;p&gt;
&lt;p&gt;From here, we can evaluate and simplify the definite integral, giving us:
\[ V = \ln\left(\frac{\sqrt{(l-r)^2 + d^2} + r - l}{\sqrt{r^2 + d^2} + r}\right) \].&lt;&#x2F;p&gt;
&lt;p&gt;Using this formula for a plate charge and the previous formula for a point charge, we calculate the total electric potential \(\sum V\) at a point. This point is then assigned a colour based on sign and magnitude, and rendered to the screen, all in a fragment shader!&lt;&#x2F;p&gt;
&lt;p&gt;Credits to &lt;a href=&quot;https:&#x2F;&#x2F;jackyliao.me&quot;&gt;Jacky Liao&lt;&#x2F;a&gt; who helped with a bunch of math and stuff!&lt;&#x2F;p&gt;
&lt;p&gt;This is my first blog post.&lt;&#x2F;p&gt;
</content>
	</entry>
</feed>
