Monday, March 18, 2019

AMD GPU on Ubuntu

Summary: I upgraded my Ubuntu desktop to an AMD graphics card; here's my notes on the (very simple) process. You don't need AMDs closed drivers and you don't need any external repositories — everything you need for both graphics and OpenCL is already included in Ubuntu itself.

I built my own desktop in early 2016. Not a high-end system, but pretty fast, with plenty of memory and with great cost-performance. Overall, it's aging remarkably well and will be fine for at least another three years, possibly longer. The exception is the graphics card. The Nvidia 750ti I had was a midrange card when it launched in late 2013, and already a generation behind when I bought it(1).  It has served me well, but by now it's really starting to show its age. Some of my current games only run at the lowest possible graphical settings.

Do people still use desktops? Yes, many do. Do people bring their desktops — and monitor, and keyboard, and mouse — with them when they travel? No, normal people don't. Somebody must have forgotten to tell this guy. Supercomputing 17, Denver.

Worse, the Nvidia Linux drivers — not open source so they must be installed separately — have increasingly been causing problems. I've had screen tearing and other glitches for the past year. The driver now often fails to restart properly after waking the machine so I can no longer put it to sleep. Botched driver upgrades have left me without a usable desktop a couple of times. I don't know if my card is too old to be supported properly, or if the quality of the Nvidia drivers are declining.

AMD is Nvidia's main competitor, and the underdog in the GPU market. They provide full open source Linux drivers for their GPUs. For a few years now, any AMD card should theoretically work out of the box; in practice you always seemed to have to tweak your graphics settings, add unstable or unfinished external components or upgrade your kernel. But for the past year or so, people have increasingly insisted that yes, nowadays it really is as simple as just switching cards, with no messing about with your system.

I didn't think to take a picture of the card until I had already installed it. Besides, for all that I like computers in principle, seeing modern hardware if often as exciting as watching paint dry. Instead, here's a Cray 1 supercomputer on display at the Supercomputing conference in Dallas last year.

So I took the plunge. I got an AMD RX570 with 8GB of memory — a two-year old mid-range card from their previous generation. Not hugely fast, but inexpensive and great performance for the money. The hardware installation is straightforward: unplug and open your computer, pull out the old card then slot in new one. Remember to connect the PCIe power cord to the card, then close the case and restart.

My machine came back up with no issues. The desktop and 3D rendering all worked out of the box. I have no more tearing, and the system definitely feels more responsive. One game was confused by the Nvidia drivers still on the system so I removed them (you can do this from the software center, but it's faster on the command line):

  $ sudo apt purge nvidia*
  $ sudo apt autoremove


While OpenGL support is included out of the box in Ubuntu, the newer, faster Vulkan support is not (these are both standard libraries for rendering 3D graphics). You can install the Vulkan libraries with the following line:

  $ sudo apt install mesa-vulkan-drivers vulkan-utils mesa-vulkan-drivers:i386 libvulkan-dev dkms

What difference does Vulkan make? "The Talos Principle" is a fully immersive 3D puzzle game with a cool storyline that aims for a photorealistic graphical style. It can use either OpenGL or Vulkan. Here's a set of benchmarks with my old 750ti card and my new RX570, with OpenGL or Vulkan, at three different levels of graphical detail:

Dotted lines are OpenGL, solid lines are Vulkan. The blue pair is my old card, the red pair is the new one. The red, horizontal line marks 60 frames/s; you ideally want the graphics to be faster than this at all times.

Take-away: Vulcan is around 25% faster than OpenGL in this game, both for the old card and for the new one. That's a major speed difference just for changing a rendering library. Also, my new card is ~120% faster than my old one. Sounds reasonable for the same class of card about three years apart.


At this point I had a fully feature-complete, well-working graphics system. But GPUs are used for more than graphics these days. You can use GPUs as general accelerators for certain kinds of computations, deep learning and certain kinds of molecular dynamics in particular. We have several dozen compute nodes with very high-end GPUs at work for this purpose (and no, they don't do graphics at all, just computation).

Nvidia is completely dominant today with their CUDA library and compilers, but it's not the only alternative. If you want to do do real compute on an AMD card you should install ROCm, and I intend to do that in the near future. Meanwhile, you can install basic OpenCL 1.1 support using only the open tools in Ubuntu.

OpenCL is an open standard for programming GPUs, and you can use it on AMD GPUs, on Intel GPUs and on smartphones (yes, they have GPUs and yes, they can be useful for this in some cases) and it works on Nvidia GPUs as well. It's not as efficient as CUDA, and it's not as popular, but it still gets the job done, especially if you're just looking to learn.

Today, getting basic OpenCL support for AMD on Ubuntu is easy. Ubuntu already carries all packages you need. Install them with:

sudo apt install clinfo mesa-opencl-icd opencl-headers

"clinfo" is a utility that reports what kind of OpenCL support you have, if any. Run "clinfo" on the command line to see what your particular hardware supports.

"mesa-opencl-icd" (and the packages it pulls in) is the actual OpenCL 1.1 libraries, compiler and other stuff. The "opencl-headers" gives you a set of standard headers for your own code.

There is one single hack you do need to get OpenCL working completely transparently. For some reason, the OpenCL library doesn't get a symbolic link "", and this is needed to be able to link with the library without giving the linker the exact file. You can add the link yourself like this:

$ cd /usr/lib/x86_64-linux-gnu/
$ sudo ln
$ cd

Now you can take any OpenCL program, compile it and run on the GPU. For instance, download this vector addition example: vecAdd.c (from this short tutorial). Compile the program, then run it:

$ gcc vec_add.c -o vec_add  -lOpenCL -lm
$ ./vec_add
final result: 1.000000

There you have it. Full 3D graphics capability using OpenGL and Vulkan (and with working Proton support on Steam); and OpenCL 1.1 support both for running and developing code. All using open source and without enabling a single PPA or going outside the Ubuntu repos. Not bad, not bad at all.

1) Buying the newest, greatest high-end GPUs rarely makes sense. You pay a huge extra cost — in money and in power — for a bit of extra performance and perhaps some new functionality. But games are made to run well on mid-range cards or lower (for as many potential buyers as possible), so that extra cost is largely wasted. They also tend to be physically large and won't fit a small case; and they use a lot of power so you may need to upgrade your power supply and add fans. And as they're new, the drivers won't be as stable or polished. 

You'll spend a lot of money for a computer that's noisy, big, uses lots of power, with frequent graphical glitches and crashes. For all that, you get some nice, cool in-game graphical effects that you notice once then ignore, because you're too busy playing the actual game to notice.

Tuesday, February 19, 2019

The Last JLPT

I retake the Japanese Language Proficiency Test level N1 every year or so. I took level 2 many years ago, but I've never passed N1, the highest level. Honestly, the main reason is that I don't really study for it. The N1 is mostly useful for job hunting or entering university (neither of which apply to me) so I'm just not very motivated.

As it happened, the Naha Marathon was right on the morning of the JLPT. Spent half an hour watching the endless stream of runners passing by as they ran through the city center.

So why do I do it? In part I want to "catch them all". I have all the other levels, and getting the highest one would give me the complete set. But it's also a nice day away from home. I go to some area I'd normally wouldn't visit, have lunch, mill about with hordes of nervous people — mostly young, almost all from nearby Asian countries — then take a relaxing walk on the way back home.

The Convention Center is a really pleasant facility. Most of it — the park, especially — is open to anybody when there's no event happening here.

This year the test was at the Convention Center in XXX on Okinawa. It's a nice parkland area right next to the sea, with a beach and a marina, well worth visiting for an afternoon. The N1 test was in a single, huge room with over 200 seats. The echoes made the listening portion more challenging than usual, but it was bright and airy so not bad overall.

Part of the test hall. Bad shot; sorry about that.

The results came a couple of weeks ago. And I passed.

To be sure, I didn't pass by a lot — 103/180 points is only 3 points over the limit — but still. To nobody's surprise, my reading score was very good (I love reading, after all). The listening was also quite good — it had better be, living here — while the grammar score was lousy. I'm sure all my old language teachers would nod in recognition.

What does this mean? On one hand, I now have all the JLPT levels. On the other, I no longer have any motivation at all to study grammar in the future. And I no longer have a yearly excursion to some random area to look forward to. Maybe it's time to start studying for the Kanji kentei :)

Just a house. It's not notable, it doesn't appear in any guidebook or anything. But it is pretty neat, and I would never have seen it if I hadn't sat the JLPT nearby and walked back.

Tuesday, February 12, 2019


Sweden does not make it easy to renew your passport. The rules are simple: you need to apply either in Sweden or at your nearest embassy; and you need to pick it up in person at the embassy or at a consulate. Simple.

Welcome to Tokyo.

But we live on Okinawa, so I need a round-trip flight to Tokyo (15k yen). And they process applications only on weekday mornings so I need a hotel room (10k) and a vacation day. The passport itself costs almost 20k yen[1]. I have to pick up the passport in person[2] so a few weeks later I have to visit Tokyo or the Kobe or Fukuoka consulates all over again. Another round-trip flight and another day off work. At least I can stay in our apartment in Osaka instead of a hotel if I pick it up in Kobe.

Tokyo. We were lucky with the weather; it was generally sunny and 15-18 degrees while there. The week after it was apparently snowing.

The total cost of my Swedish passport becomes about 60k yen — 5000 SEK or 550 USD. Swedish passports expire after only 5 years, and foreign countries often require at least 6 months validity to enter the country, so I get to do all this again in about another four years.

What to do? Accept the inevitable, and make lemonade of the lemons that the Swedish government has handed you. We take a short winter holiday in Tokyo: go shopping, see a show, eat well and often, and generally enjoy ourselves over an extended weekend.

"Little Tokyo". A fun performance and good music!

We went to see "Little Tokyo", with and by Miyuki Nakajima. She's a singer and songwriter with a large and enthusiastic fanbase, and every few years she writes and produces a musical instead of her regular concert tour. The storyline is often convoluted and a bit difficult to follow, but you really mostly see them for the music anyway.

It was especially fun this year as "Little Tokyo" had a storyline that was actually easy to follow (without spoiling anything, it involved infidelity, ghosts, and animals turning into humans — very kabuki-like). The atmosphere is relaxed and the actors all seemed to enjoy themselves on stage. We had a lot of fun.

Runners along the Imperial palace course. The weather really was great.

I still enjoy running. Nowadays I try to run 4-5 times a week, for a total of 40km. Tokyo is a good place to run — it's largely flat, and has a lot of parks and other areas to run in. By far the most famous and popular route is the 5km circuit around the Imperial palace in the heart of Tokyo.

Imperial palace running course. Yes, those people are all running. Not a great picture — it's difficult to take a good picture while you're running.

How popular is the course? It features in guidebooks and in travel sites. The route has distance markers and permanent signs reminding runners about the rules (run counter-clockwise; keep to the left; be mindful of pedestrians and bicyclists). Running clubs set up camp on the open area at the south-east corner with drink stands, timers and baggage drops. Run stations (rental lockers and showers) is a thriving business in the area.

The outskirts of the Imperial palace is popular for all kinds of activities. Here a couple arriving for wedding pictures. If you run here you do need to be careful.

And for good reason. It really is a very pleasant course. The basic course is almost exactly 5km — short enough for beginners, and if you want to run longer you can either extend the course or run it multiple laps. There's not a single stop-light, and the course is meandering and varied, with a gentle slope up toward the north, then a fast downslope south. And as you run you have the Imperial palace grounds to your left, and central Tokyo defiling past on your right.

The moat by night. Not bad for a smartphone shot.

In all we spent three nights in Tokyo. We did a fair amount of shopping — I got three new books at the Maruzen store just northwest of Tokyo station; they have fairly good mathematics sections in both Japanese and English. We ate well, walked a lot and generally had a good time.

Also Tokyo. That's actually quite a cool bike.

1) If you apply in Sweden it only costs 350 SEK, or about 4000 yen.

2) Sweden — unlike almost every other country I know — doesn't send passports by post. It has to be picked up on site, in person only.