- Podcasts
- Programming Throwdown
- Programming for the GPU
About this episodeOn this episode we invite Mark Harris, Chief Technologist at NVIDIA, to talk about programming for the GPU. Show notes…
On this episode we invite Mark Harris, Chief Technologist at NVIDIA, to talk about programming for the GPU. Show notes: https://www.programmingthrowdown.com/episodes/54-programming-for-gpu
★ Support this podcast on Patreon ★Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Programming for the GPU
Okay, yeah. So my title at NVIDIA is Chief Technologist for GPU Computing Software, and my role is kind of twofold: one is inward-facing and one is outward-facing. So my inward-facing role is to help define our software strategy for computing at NVIDIA. And so for that's for things like CUDA programming, which we'll talk about more hopefully. And the other aspect is the external-facing role, which is a little bit of evangelism—giving talks at conferences such as the GPU Technology Conference. And I also run a blog called Parallel For All, which is on NVIDIA. Hopefully, you guys can drop a link after the show recording. On that blog, it's a developer blog written by developers for developers, and it's a deeply technical blog about parallel programming and GPU programming. So what can
You maybe just give us a little bit of background on what NVIDIA is, what they do as a company? I mean, I think most people have heard of them, but they just think of—well, I guess people don't go to Best Buy anymore, but you go to Amazon or Newegg or whatever the international equivalent is. I still go to Best
Okay. So well, what is NVIDIA? NVIDIA is a visual computing company. And what that means is that we focus on building solutions for all aspects of visual computing. We call ourselves the inventors of the GPU—that's the Graphics Processing Unit. I think NVIDIA coined that term back in like 1999 with the first GeForce product. And most consumers, would be familiar with, especially gamers would be familiar with our GeForce GPUs, which are graphics cards for making your games graphics look amazing and run really fast. But GPUs are used in a variety of computations. So we have four kind of focused business areas, and those are gaming, professional visualization, data center, and automotive. And visual computing or parallel computing are really important in all four of those. And it turns out that GPUs are very useful and good at accelerating those computations. So the obvious ones are gaming because the GPU was designed for computer graphics, and also professional visualization. But the area I work in is data center and parallel computing. So you know, GPUs, it turns out, are great at parallel computing because graphics is parallel, and I mean, I don't know how much detail you want me to go into about the history of that, but well,
Maybe I mean a simple way of saying it is that with graphics, you're trying to do two things: you're trying to figure out where a bunch of triangles are in space, and then you're also trying to draw sort of a bunch of pixels on the screen. And in both cases, they're kind of embarrassingly parallel. Like you have many triangles, and they can all be discovered rendered—you know, located independently. And you have many pixels; they can all be processed more or less independently. And so that makes the GPU kind of like ideal for doing a lot of these things in parallel.
Right, absolutely. Yeah. So I mean, you have millions of pixels to shade in every frame, and you're running—you know, 60 frames a second or whatever. And triangles, you have, I guess modern games probably have hundreds of thousands to millions of triangles per per frame too. So you're getting to the point of having, you know, pixel-size or sub-pixel triangles. And yeah, so back in the day, back when I was in grad school and actually way before that, people kind of recognized this with graphics hardware and they started hacking around on using graphics APIs and GPUs to do computing that the GPUs weren't really necessarily designed for. And so this is something that I focused on in grad school, and I called it GPGPU, which stands for General Purpose Computation on GPUs. And since then, that's become something that wasn't just grad students mucking around with graphics APIs and that. And back in 2006 or 2007, NVIDIA launched CUDA, which is a set of extensions to C and C++ that allow you to program GPUs for parallel computing in a traditional programming language rather than using a graphics API. Yeah.
Mean before? Oh, go ahead, Patrick. Yeah. So I kind of recall vaguely that time when people first started kind of doing the GPGPU stuff, and I mean, you might be able to fill me in where I'm misremembering or incorrect, Mark, but at first, you were writing in a language which is essentially a shader for the GPUs. You kind of had to still frame whatever problem you were doing in terms of trying to tell the graphics what to do. Then CUDA came out. What—what kind of did what was that process like? And what did NVIDIA really see that made them say, 'Hey, there's something here?' Yeah.
It's interesting. So my personal story for this was that I was an intern at NVIDIA in 2001, and that's when I sort of learned—my PhD was on cloud simulation and rendering. So not that this was before people were talking about the cloud, so I was rendering clouds, right? Like pictures of clouds, like balls of moisture, okay? Yeah, exactly, in the sky. And I had gotten this at NVIDIA; I learned from some of the engineers there about some of the things they'd done with shaders. Like one of the guys wrote some shallow water equation solvers in DirectX and The Game of Life—he had the Game of Life running in pixel shaders. And this was before, I think they called it Shader Model 2.0 or something like that. So there was no floating point on these GPUs at that point. So you had to kind of hack everything in fixed point. You had basically 10-bit precision in the pixel shaders. And that was fun, but I basically got the idea and I learned that NVIDIA was going to be coming out with GPUs with floating point pixel shaders in the near future. And so I went back to grad school, and I thought, 'Well, what if I did all of the simulation of the clouds on the GPU in addition to the rendering?' And so I basically started doing fluid simulation on the GPU. But I didn't know, and a bunch of other grad students were—you know, and researchers were doing stuff in similar areas. There was ray tracing going on in shaders, there FFTs even. And so I kind of started like—I
would actually render the edges, and it was so hard. I mean, this is pre-CUDA; it was so hard. I mean, it took like a week to figure out how to get it to compile. And then, oh, you don't have the right GPU. You have to go out and buy another one, and it was just... it could have made it so much easier.
Right. So I think that—I mean, while I was back in grad school and not at NVIDIA before I came on full time, obviously people at NVIDIA really saw this opportunity, and I believe that it was Jensen, our CEO, ultimately who was convinced. And then by the time I got back in 2003, there was already an effort to build NV50, which became G80, which was the first CUDA-capable GPU. And there was, you know, it's the first GPU with a dedicated computing mode with byte addressable memory instead of just pixel shaders, random access memory from the shader units. And in terms of—you're right, it was hard, and it was kind of fun. It was if you felt like, you know, a hacker getting this stuff to work. But when you got something to work, it kind of had this feeling of magic, right? Oh wow, my fluid simulation actually works. Or, you know, this thing—this reaction diffusion simulation that I wrote—that only really needs more way more precision than I have in these fixed-point pixel shaders actually works, and it felt like magic, which is really not a sustainable feeling in software development, right? So what NVIDIA did was to build hardware that was dedicated to computing as well as graphics, and then build software on top of that. And so we saw early on by talking from talking to potential customers that we would have to build something using languages that they're familiar with. And when we went around to customers, you know, we were talking to people in areas ranging from defense to oil and gas to fluid simulation like Cadence and Ansys—I guess was the company at the time; now I think it's Ansys—and they all said, 'Well, it's got to either be for... some said it's got to be Fortran. Some said it's got to be C or C++.' So we decided we were kind of afraid of Fortran at the time, so we were like, 'Well, we've got to build something that's based on C,' and that became CUDA. So it's basically C with some extensions, and it took away that magic feeling. You know, it did what it did. When you wrote a program, it did what you thought it should do rather than, 'Oh, maybe if I hack this this way, it'll work,' and then it does.
It took a while. Well, it certainly—it definitely paid off, but it also took a while. There was a lot of initial interest and adoption started immediately. You know, people were using CUDA 1.0. I still talk to customers who were like, 'Yeah, I've been using CUDA since it first came out,' or the beta or whatever. For building starting to build real software with it right away. But to really call it successful and actually see real applications that people could go and buy or download that accelerate with GPUs, probably took a couple years. And now, you know, it's—it's the point I wouldn't say CUDA's mainstream, but it's definitely something that real products and labs and researchers and all of these things all use, so just...
For people's clarity—I mean, if you're playing a video game, I don't know what the cool kids play these days, and it's running some amazing graphics. Is it the case that there's also CUDA programs doing general processing in the same pipeline, or is it typically that you run some specific scientific application that would use it?
So early on we made efforts to get CUDA into some games, and there are some ways that CUDA is used in games. For example, NVIDIA has a physics simulation library for games called PhysX, and it uses CUDA for, you know, cloth simulation, particle simulation, rigid body simulation things like that. But most games that are doing computing—and a lot of games do general purpose computing—they use compute shaders within the graphics API. So after CUDA came out, DirectX and OpenGL both introduced their own flavors of compute shaders that basically are able to do similar things to CUDA programs but within the graphics API so that you don't have to be juggling two different APIs. But they have largely the same programming model. You know, within the kernel—a kernel is what we call a parallel region of your program. Within the kernel, whether it's in the graphics API compute shader language or in CUDA C++, the programming model is basically the same with a few minor differences, so it may be...
So if you want to write a program for a GPU, you want to take advantage of all of the parallelism. So GPUs have now thousands of parallel cores. And if you can think of these—if you're coming from a graphics background, you can think of these as pixel shader cores. But really they're unified cores that do everything from transforming vertices for the vertex shader to shading pixels to just running compute instructions. And so you're basically writing—the way you can think about it is you look in your program for parallel regions that have parallelism. And what that means is you have loops typically in a program where the iterations of the loop are not dependent on each other, right? So they could be run at the same time. So you can think of kind of flattening out that loop and then running each iteration at the same time or many of the iterations at the same time on separate processors. And so that's what CUDA lets you do. And basically you write a program where—or you write a kernel program where within that function, the whole kernel is being executed by many threads simultaneously. So basically the code is single-threaded code but it's run in parallel across many threads. And you have...
Hinted at this, but you know when someone buys a computer, they buy, you know, a quad-core computer or they buy an i7 that has six cores, right? And you're saying the GPU has, you know, a thousand cores. So it's sound. I mean, just in a very naive way, you could say, 'Well, why would not—why not use the GPU for everything? It has a thousand cores. My CPU has four cores, right? So why would I—why would I ever use...'
The CPU? Well, I will say that the GPU is becoming increasingly important, but in fact, I was just looking at a I saw a die shot of a Broadwell CPU, and it's half GPU. The die is—oh, like literally, literally the actual cores. Well, this was a Broadwell, not a Xeon. Xeon is probably is not half GPU, but a just a regular core like i5 or something like that, with four cores. So the four cores take up, you know, maybe a quarter of the die area or something like that. But so why not use a GPU for everything? Well, the cores are different as you're hinting at. We call them CUDA Cores, but you know, that's a sort of a marketing name. But really they are individual processing elements that process instructions, but they—they have a they use a parallel execution model that that's called SIMT. You may have heard of SIMD. MD stands for Single Instruction Multiple Data, and what that means is that you have a single instruction, but it gets executed by on multiple data elements simultaneously. And so you can think of that as having multiple threads that—well, no, you can think of that as having a vector of data elements and you apply the same instruction to all the elements in that vector simultaneously. Yeah, like extra ALUs. That'd be that's SSE. Yeah, that's SSE. That's right, or AVX. And that's where you basically have a bunch of ALUs. SIMT is—it was an NVIDIA coin term, but it's been used more broadly since then. I think it stands for Single Instruction Multiple Threads. So now instead of just having a vector of data elements, you actually have multiple threads that execute the same instruction, and the difference, the important difference here is that each of these threads has its own program counter. Which means that they can branch to different instructions separately. Whereas with SIMD, the branch has to be wrapped around the whole vector effectively. So if you—if you need to make a decision, it has to be at the granularity of your vector size, right? If you need to make a decision in CUDA or in SIMT, then it can happen at a single-thread granularity. Of course, there's a cost to that because the hardware, although we have all these little cores, they do share instruction fetch and decode logic. And so you may end up with overhead of replays or predication of your instructions. I'm getting pretty technical here, but no, it's great. That's kind of the difference between GPU cores and vector units or SSE units. But where the real difference is in terms of your original question is that the cores are very lightweight cores on a GPU, and they don't have very good single-thread performance. They really get their performance in aggregate, right? From running many threads in parallel, usually doing the same thing, possibly branching and diverging some, but usually doing the same thing. CPU cores, on the other hand, are—they have a lot of things like branch prediction and big caches. They're optimized for latency. In other words, they're optimized to reduce latency, which means that if you only have one thing to do, you can do it really fast. On a GPU, if you only have one thing to do, you're leaving, you know, 999 cores idle. I gotcha. Right. So um uh the way we talk about it is that GPUs are optimized for throughput. CPUs are optimized for latency. Um there's a bit of gray area there because CPUs have AVX and they can do things in parallel too. Um it's just that the scale of parallelism is is lower on a CPU versus a GPU. Um and we're optimized for throughput, which means instead of trying to reduce latency, we try to hide it. So we always talk about latency hiding. GPUs are really good at hiding latency by executing other work while while we're waiting. So if there's a memory access that we have to wait for data to come into the cache from off-chip, then we do work in other groups of threads—possibly even the same instruction in other groups of threads—but um but we you know we issue we have other instructions to issue to hide that latency.
So one part of the screen, the triangles have all been positioned and you say, okay, this part of the screen is good to go. Let's start rendering pixels. Meanwhile, the next frame of triangles is already trying to get pushed to the screen at the same time? Like is that the kind of pipelining you're?
Talking about? It's yeah, there is pipelining involved, but it's it's also just about having um so if you if you think about your pixels, you have um some large triangle possibly that has, you know, hundreds of pixels it covers and they're all shaded with the same same pixel shader. So that pixel shader has to go and compute it, has to fetch from textures, it has to blend the colors that it gets. You know, you can do arbitrary computation now. So but those um pixels are grouped together into groups um in the hardware, and in CUDA we we refer to those as warps. It's a term that comes from weaving actually, but um um because you had parallel threads and weaving um um and so those warps uh so each warp is a group of 32 threads or 32 pixels. And um while one warp is waiting on a texture fetch, for example, or a memory load, then we can switch to instructions from another warp within within the um the what we call the multi-processor, and so the multi-processor can issue instructions from multiple warps simultaneously, or while while one warp is is waiting. Um so it's similar to pipelining. It's it's actually more similar to hyperthreading.
Yep, that makes sense. So for people who don't know, you can probably explain it better than I could, but I think a loose definition is hyperthreading is um you have, you know, on your CPU you have, you know, floating point unit, you have uh uh something just integer arithmetic. Um you have many of these little mini modules and you could sort of fake out having two or more threads. Um if one of them needs the Floating Point Unit and the other one needs the arithmetic unit at the same time, and it's as if you're executing them both in parallel.
Right. And on the CPU I believe um what hyperthreading requires ultimately is duplicating resources like the register file, right? So um and on the CPU, the register file is relatively small. There's at least on x86 CPUs there there's um well, the visible registers are fairly small, but um on a GPU, the register file is quite big. Um it's almost like uh this you know, a small cache except that it's a register file. So um it's like directly accessible by instructions. Um and so we actually have you know you know on the on the GPU, the the the cores that I talked about are grouped together into things called multi-processors. So for example, um a multi-processor on Pascal, the latest architecture we just announced, has like 64 CUDA cores and it has um I think 128 kilobyte or 256 kilobyte register file on that SM. Wow. Um so when you talk about a lot of registers, when you talk about
Having a multi-processor, you said you each core needs to be at a similar instruction—basically that you want to be executing the same thing as much as possible. Um I think this is really interesting because it really understanding how your program gets executed helps you design really good software or at least in the efficiency case. And um what is it that's actually different that causes you not to be able to get off that far? So you have the multi-processor, you have all these cores in it, and what is it that actually like you said you you duplicate some things, not other things? Like what is it actually that is preventing you from being able to have code running very different parts of the program?
Um so what's prevent uh what's preventing you having code well um but each core, each thread basically doesn't isn't being run on on a full-blown core that has a separate you know instruction fetch and decode and issue, right? So that logic is shared across basically 32 um cores. And so we group threads into groups of 32. Um that's what we call a warp. And uh I'm not sure if I'm answering your question. Yeah.
No, that's kind of what I was saying is so that the instructions are are fetched in a batch and that you want all of the cores and the multi-processor to execute that same thing. So if they get too far off, they need some instructions that another processor doesn't yet need or whatever, right? And then you get out of sync, and you'd have to add extra hardware to handle that, which would right?
Get you closer and closer to a yeah, yeah, exactly. Um yeah, that's right. And and so when you execute when you write code for GPU, you want to be aware of um kind of the branchiness of that code, right? So if you have a loop where you're processing a lot of data but each iteration of a loop there's you're checking conditions, and you know it's really data dependent. If every iteration is completely data dependent what it does, then performance may potentially suffer. But if you can kind of do some work ahead of time to to maybe reorder your data um sorting or something like that, um or binning so that um threads that are contiguous um in terms of their order—their thread ID or whatever—are accessing memory that's contiguous and also making decisions that are contiguous, then you're going to get much better performance. So we talk about sorry, go ahead. No, I
was just going to try to give an example. So you're saying like if you had even numbers you do operation A and an odd numbered indexes of the array you do operation B, then instead of running linearly through the array, you would want to maybe like process all the even numbers first and then all the odd numbers as opposed to even, odd, even, odd?
It. So how does somebody, if you have all of these threads all doing well, I guess doing the same thing but on different on different pieces of the data? How do you debug this? I mean, I imagine you don't step through the debugger like you do in with GDB and go line by line; that would probably be bad.
Um well, it's a great question. How do you how do you debug GPU programs? Um and we do have tools. Um uh we have very good tools now. In fact, um it's gotten a lot better. The CUDA 1.0 days that you know, we did introduce CUDA 1.0 with a debugger and a profiler, but they were very basic. Um so you do you can step through the instructions, and people do, and when you're really trying to figure out a difficult bug, you know, just like on a CPU, it really helps to have a debugger that lets you step in and inspect um memory locations and variable values and things like that. Um and and so you can do that, but there are a couple different modes that you can step right? So um we have a couple of tools. One is um on the Linux side, we have um what's called CUDA GDB, which is basically um a modified GDB that supports programming or debugging CUDA programs. Um on Windows, we have um something called N-sight—uh sorry, Visual Studio Edition—which is a plugin for Visual Studio that um gives you debugging and profiling inside the IDE for GPU programs. And that that insight um also has graphics profiling and debugging features. Um there's also a uh an Eclipse plugin called or an Eclipse um IDE called Inside Eclipse Edition for Linux and Mac that kind of wraps the CUDA GDB stuff as well as the profiler. Um anyway, so if you're stepping through a program running on the GPU, I talked about warps. One way to step through is to actually look at one thread and step each, you know, instruction for that or each line of code for that thread. Um another way to do it is to step a warp or um or to step all. the threads in what's called a thread block, um which is a CUDA construct. Um and uh there's different, you know, different reasons you might want to do that. You might want to actually look at the values held in variables for a number of threads at once, and you can do that in the debugger. So you're kind of doing parallel debugging, but or you might want to just, you know, focus on one thread to try to understand the logic a bit better. And so you can in N-sight Visual Studio Edition—at least I'm not sure about the CUDA GDB, probably also there too—you can toggle which way you want the the debugger to step. I mean, the hardware is always going to run things a warp at a time, but it look to you or only focus in on the values of a single thread if you want.
Yeah. Well, if you're debugging, if you're hitting a breakpoint, you you do need to freeze the program. And so that's actually requires hardware. Um and it's something that we've gradually improved. It used to be that you had to have um you couldn't have a display attached to the GPU you were debugging. And if you think about it, you know, the GPU has modes. It has graphics mode and compute mode. And if you freeze it in compute mode, then it can't service the display, which means you're you're running Windows and suddenly Windows freezes, right? So so you had to have a separate GPU in order to debug previously. Now we can do single GPU debugging, and with Pascal, which we just announced at GTC in March, um we have compute preemption, um which basically allows you to um, you know, just as it sounds, just with traditional preemption, you know, you basically can um store the state of the program and kick it out and switch to something else, some other application. And so that allows the debugger to um to step through programs and hit breakpoints um while um making sure the operating system is interactive on a single GPU system.
So one of the things you—I mean, obviously you're talking from the perspective of NVIDIA and CUDA, but I mean people will know. And you mentioned before looking at the die shot of some of the Broadwell chips or whatever having GPUs on the same die as a CPU. What are, you know, obviously there's probably some advantages and some disadvantages? You kind of speak to like what the difference is between a processor integrated with a GPU versus a discrete GPU?
Sure. So um yeah, I mean the majority of the products that NVIDIA sells—the GPUs that we sell are discrete GPUs. In other words, they're they're on a board that plugs into like a PCI Express socket, um and um they're separate from the CPU. And so uh well just a little bit on that when when you're programming um a program that uses the GPU, for example in CUDA, you're writing a heterogeneous program. You know, the program still needs to use the CPU, right? So um most programs have at least control from the CPU, if not significant computation there also. And so you have to take care of um the GPU and the CPU have separate memory, and so there are transfers that have to happen between the GPU and the CPU. Um and I can come back to that later. In fact, we should come back—remind me to talk about unified memory. Um but uh there are also processors that are integrated, as you mentioned. So um NVIDIA has a line of processors called Tegra, which are a System on a Chip. There's also, you know, as I mentioned, Broadwell, um Core CPUs have their um Iris Graphics on board, so they have a GPU uh integrated with the CPU on die. Um so these are kind of similar in some ways. Um the System on a Chip approach is is a bit broader that are the Tegra basically has a few ARM cores and then it has a GPU, and it also has a bunch bunch of other, you know, all the things that you need to build a whole um small system. And so Tegra is used in things for like laptop—sorry, not laptops—um tablet computers. Um like the Google Pixel C, I think has has Tegra in it. Um and uh it's also used in something we call Jetson, which is an embedded development kit which is aimed at people who are developing things like robots, drones, um other embedded systems. And um so to your question, you know what's the difference between these and the trade-offs? Well, if you have a certain die size, um if you can dedicate it all to GPU, obviously you're going to have a more powerful GPU. But if you have to split it half between GPU and CPU or GPU and CPU and other stuff, then the you know the amount of computational capability of each of those things goes down. So it's a it's a balancing act, right? What do you want to do? If you want to do high-end supercomputing, you know test NVIDIA Tesla GPUs are used in supercomputers like Titan at Oak Ridge National Labs. If you want to do that, then um uh the System on a Chip approach probably isn't the right way for you because you need the most powerful GPU with the highest memory bandwidth and the highest computational throughput, right? If you want to do if you want to build a robot where you need CPUs and GPUs and sensors and, you know, data inputs and all this kind of stuff, then an integrated processor that's really low power obviously makes a lot of sense. So you know we we build things for the whole spectrum from very low low power embedded to um places where we need power efficiency but the actual total system power is not much of not as much of an issue. Does that? I mean, yeah. And then what?
is it? So you talked about, you know, kind of transferring out over let's say PCI Express and that you know obviously passes certain data size that makes great sense as you said in like supercomputing. But then if you talk about how—how does as a programmer is there a way to kind of guide someone and say, hey, you could do this on the CPU or you could take the time to transmit to the GPU and then transmit it back? Um And how they kind of build that threshold in their mind about which one to do? Or is there even a way to like write a single program and then at runtime or at compile time it determines, you know, hey, based on this code size we're going to execute this in one versus the other? Yeah. Okay, there's.
a lot in that. I'm sorry, that's all right. Um so I'll go back to what I was talking about if you're on a system with a GPU and a CPU that are separate and they have separate memories. Up until CUDA 6, which we launched a couple years ago, you always had to explicitly manage all memory. And so, as you were talking about, you have to—you would have to create, let's say the data comes from a file. So your CPU loads that data from file into CPU memory. You then have to allocate GPU memory for that data and do an explicit memcopy between the CPU memory and the GPU memory. So that's, you know, CUDA has an API for that, CUDA memcopy, right? It works just like memcopy except it allows you to copy from the CPU to the GPU or the other direction—or from GPU memory pointer to another pointer. And there's a cost to that because PCI Express has a certain bandwidth. So, you know, given the data size, you know, the bandwidth of the PCI Express, you can estimate how long that's going to take. And so if you have a huge amount of data and a small amount of computation, and you're only going to do a small amount of computation on the GPU before you need to do something on the CPU—like, you know, I don't know send it on the network or write it back to disk or or whatever—then the overhead of transferring it might be higher than the computation cost that actually, you know, of performing the task on the GPU. And so there are trade-offs, as you hinted at. You need to decide whether it's worthwhile on the current hardware to transfer data to the GPU for processing. And there are many applications where it's obviously beneficial, but there are some applications where that trade-off is trickier, you know. And so there's a lot of things you can do like trying to overlap the communication with computation via pipelining. We have facilities in CUDA for streaming. So basically, you can associate computations and copies with separate streams of API commands. So that if they're independent, they can be overlapped. So what you could do is you could chunk your data up so that you transfer a little bit of it, you start processing on it, and then you start—you do another transfer on a separate stream simultaneously. Things like that. But yeah, there is a bit of a balancing act there, and it's it depends on the application. Sometimes it's trickier than others. That makes sense.
Or something. Yeah, so on Tegra, you have one memory. So, you know, it's shared between all the processors in the Integra. So, um, so you know, you can allocate a pointer and then just share it between CPU and GPU code. There's a couple things there, there's a couple gotchas on current Tegras, I believe, like on the the TK1—I'm not sure if it's true on the on the Tegra K1; I don't know about the Tegra X1—the caches were not coherent between the CPU and GPU. And so sometimes what seems like it should be free actually has a cost because of having to invalidate caches, right? Um sense. So,
So there's, you know, um there's a wide variety of people who want to leverage a GPU. There's people like Patrick who builds robots and underwater submarines in his garage. Um and there are people—no, that's not true, but that's a good idea. And there are people like me who know nothing about C. I tried to write a C program once for a company that Patrick and I worked for, and they kicked me off the project almost immediately. I had no idea what I was doing. And so I'm more of like a, you know, MATLAB, R, Python person. Um and so how does—how does sort of the CUDA ecosystem sort of cater to all of these different people who have different backgrounds? Um They apparently they don't do Fortran, so those people are sold, but for everybody else?
Yeah, but and great, that's a great question. Um so you said the word ecosystem, and we do talk about ecosystem, and NVIDIA a lot in terms of CUDA. And I know a lot of companies do that, but whenever you're building a platform, you care about the ecosystem. And so you're right, there are a lot of programmers, and there are many programming languages, and we would like to enable them all—or anybody that has parallel programs or a lot of data to process, anybody needs high bandwidth and throughput, we would like them to be using GPUs. And so um we try to enable as many ways of programming GPUs as possible to cater to those different needs. And so when we talk about the CUDA platform, we talk about three ways of programming. There's directives, which are which are basically hints to the compiler that you can add to loops in C or Fortran that allow the compiler to try to automatically parallelize those loops. And if you've heard of OpenMP, OpenMP is a is a compiler directives standard which enables you to specify, 'Oh, this loop is parallel, please.' You know, parallelize it for me, and that started on CPUs. There's work ongoing in OpenMP to support accelerators like GPUs, and we're involved in that. There's also another standard called OpenACC, which is another way to program, and it supports there's compilers for Fortran as well as C and C++. For OpenACC. Um so the second way is with libraries. You know, if you have a fairly standard computation or if you use an industry standard library for those computations, there's a good chance that there's already a drop-in replacement that targets GPUs. So, for example, there's a popular linear algebra library called or it's actually just an interface standard that many libraries implement called BLAS, that stands for Basic Linear Algebra Subroutines. And there's a cuBLAS that NVIDIA provides. Um there's cuFFT which does Fast Fourier Transforms if you use FFTW on the CPU, for example, or MKL on Intel processors. You know, we can—you can drop in cuFFT and accelerate those on the GPU. Um there, and then there's a number of other more kind of domain-specific libraries. There's libraries of solvers, cuSolver, cuSparse, which is for sparse linear algebra. And um basically a whole bunch. There's a—there's one that's getting a lot of interest now called cuDNN, which we can talk about more, which is for deep learning, deep neural networks. Um so that's the second way: libraries. And the third way is with programming languages, which is so I've talked a lot about CUDA, and what I really meant by that was CUDA C++ or C, um which basically is using NVIDIA's compiler NVCC to compile C or C++ with extensions for parallelism. But there's also CUDA Fortran, which is um which was created by a company called PGI, The Portland Group, which is now owned by NVIDIA. But um they started CUDA Fortran when they were an independent company. Um and CUDA Fortran basically takes that CUDA programming model that I talked about and introduces it to Fortran with extensions. Cool. Um there's even CUDA Python. Go ahead. No, no, keep going. So CUDA Python is was made by um this company Continuum Analytics in Austin, and they make um they make a product called Conda—Anaconda. Yeah. Um Anaconda is awesome. It's a Python um basically package manager. It's kind of like using apt-get in Linux or um or Ruby Gems if you're a Ruby programmer. Um and it lets you um basically manage packages, but what they are, they also have made a bunch of their own Python packages. One of which is called um Numba, which is an open source compiler for Python. And you might say, 'But wait, Python's not compiled; it's interpreted.' Well, what they've done is they've allowed you to put a little annotation on a function. You basically put @jit in front of a function, and that then it um generates uses LLVM to JIT compile that. Um so it'll run faster on your CPU. And they also have um um a CUDA jit and a Numba syntax um things to expose the CUDA programming model in Python. Cool. Yeah.
Another—a couple other resources I've used: Theano, which is pretty good. It's a Python-based kind of like a MATLAB-like environment, um right? But it runs on the GPU. And then there's also um TensorFlow, which is a new one that I've only done the little test app, so I haven't played much with TensorFlow, but it's also um gives you this MATLAB-like environment, but it—but under the hood, it's all running on the GPU. And I think they're both using cuBLAS, I believe, and cuDNN.
That absolutely yes, definitely they are. Um yeah, so these—I mean, you guys talked about these in the Scientific Python podcast that I listened to, and uh they are tensor libraries or and they're MATLAB-like, but I mean really they're being driven by deep neural networks work, right? So um and that's where we focus with cuDNN, but also cuBLAS. Um the linear algebra, so a lot of the computations on these tensors are basically just matrix vector multiplies or matrix matrix multiplies things like that, and that's where GPUs really excel. Um if you want to get peak performance on GPU, then just do large matrix-matrix multiplies, right? Um so uh yeah, so I guess what the last thing I want to say about the ecosystem is that we wanted to enable all these things, and so we've—we've worked in a few areas. One is the directives I talked about. One is in building libraries, you know, where it makes sense, where there is demand. And then the other is the compiler. Um we wanted to enable other compiler writers and developers to build compilers that could target GPUs. And so um we started using LLVM, which um I don't know if you guys have talked about LLVM on this show, but it's an open source um compiler toolchain, and it basically has uh become really popular and is being used in the back ends of a lot of different compilers for various languages. Um And by—by allowing—by basically what we provided is some extensions to LLVM. Actually, they're not even extensions because because LLVM is extensible, we're able to do it all entirely within that. So so um our extensions are actually a subset of MVV of LLVM rather. than a super set, um but basically some annotations that allow you in the low-level intermediate representation of LLVM to express the parallelism just as you would in CUDA kernels. And that enables LLVM compilers or LLVM-based compilers to target GPUs. And so we have a library called NVVM that will generate assembly code for the GPU from this extended LLVM IR, and we also open sourced a version of this, and it's included in LLVM. So that has enabled a number of developers such as Continuum Analytics, such as PGI, such as others, even Google, to target GPUs much easier and to build tools, language tools for them. Cool.
So if you're a student just starting out and you want to kind of get something up and running that's really cool, like you want to know in a day or in a week you want to go from intro to CUDA to having something kind of really cool that you could show your friends. What would you recommend? Like is there a cool demo that you recommend, or a site that has a cool, like for Ruby, there's the Rails for Zombies where you end up with this like Twitter-like website that you could show off. Is there something like that for CUDA?
So we should do like CUDA for Zombies or something like that. What I would recommend for people who specifically want to learn CUDA programming? CUDA C++. Check out—there's a Udacity course. It's actually a couple almost a couple years old now, but it would still be relevant. And I think it's called Parallel Programming. No, I'm not sure, Parallel Programming or something like that. Sort of parallel programming. Yeah, look it up. It's the instructors are Dave, that might be it. The instructors are Dave Lubeke, who's... Heads up Graphics Research at NVIDIA and John Owen D. Davis. Cool. Yeah.
Oh, we talked to some folks at Udacity, and there's—there's a show on Udacity for people who don't know what that is, but it's a great platform for learning almost anything technical. And now they're getting into other areas too. So there's an Udacity course on CUDA that all of you should check out. Yeah.
Yeah, yeah, definitely. Cool. And you know if you don't want to if you're not a C programmer, for example, if you want to use GPUs but you're not a C programmer, you guys already mentioned the tools of some of the tools that are available for Python. So Theano and TensorFlow. So I would say for Python programmers they should check those out, and there's a number of tutorials for those. Theano has a bit of a learning curve, I think, but...
Okay. And then number is the other one, but one thing I also want to mention is the SDK. The Compute SDK—I think we're calling it ComputeWorks SDK because we have a whole bunch of other Works SDKs at NVIDIA, including the CUDA Toolkit. And the CUDA Toolkit has a whole bunch of samples, like tons of samples, and they're nicely grouped into categories, including, you know, one of the categories is called Simple. You know, so you can look at that, like me. Yeah. So it's not necessarily because they're easy, but because they do simple things, but you know, so you can start with those. And there's cooler demos too in there if you want to do something fun. There's—there's um in the Simulation category there's one that I co-wrote with a guy called Lars Niland that's N-body, which gets used to demonstrate GPUs a lot. Basically it's simulating gravitational interactions of stars effectively. So it basically does this all-pairs computation of gravity between the stars, and it runs really fast on GPUs, and you can get really cool visuals out of it. It's got an OpenGL renderer that sounds awesome. Yeah, it's fun to play with. And then there's another one that's fun to play with. I think it's called Particles, um that former colleague of mine wrote, and it is a really cool demo with all these balls in a box, and you can just bash them around, and they collide, you know, so they don't pass through each other, and that's all done using CUDA. And then there's one I think there's a Smoke Particles one that does smoke simulation. I don't actually—I'm not sure it's just doing particle simulation, but it's rendering it to look like smoke with light scattering and stuff. So cool. Um so yeah, there are cool. demos if you want to.
Show off. So what is it like? Tell us kind of what a day at NVIDIA is like. And I know in your case you're Jason before we transition to that because everybody out there is probably thinking this—because I still am—you wanted us to remind you to talk about Unified Memory. Oh yeah, we should do that before.
Unified Memory is a feature of the CUDA programming model that we introduced in CUDA 6 a couple years ago. And that with the Pascal architecture, we're enhancing it a lot. So the idea is I mentioned that you have to explicitly transfer data from the GPU to the CPU, and it would be nice if that weren't the case. It would—it would be nice if you could just allocate data and then the GPU and the CPU could use it, and then you know behind the scenes maybe it would get migrated on demand to the processor that needs it. And that's what Unified Memory is. So Unified Memory in CUDA 6 was basically software which does page migration between the GPU and the CPU. So if you're familiar with virtual memory, you have pages, and when the CPU needs to access data that's in a page that possibly is not in memory—it's on disk—then it does a page fault. You know, it faults on that memory, and then a page fault handler runs, and it actually loads the data into memory, and then the CPU can proceed with accessing it. Well, before Pascal GPUs didn't have the ability to page fault, but we kind of looking forward to GPUs that did, we built Unified Memory so that you can still access memory from both the CPU and the GPU, and it gets migrated at the page level automatically. With Pascal, that because we can page fault, that means that you can just allocate data with cudaMallocManaged. It's called, and then when the CPU touches a page, it'll get that page will get faulted back to the CPU; when the GPU touches a page, it'll get faulted to the GPU. And while that may sound expensive, often those page faults are hideable. You know, as I was talking about latency hiding, you can just hide that with. other work. Um and it enables other things. Once you have hardware support, the Pascal GPUs have the ability to access a 49-bit virtual address, which is one bit larger than the CPU virtual address space. Which means that the GPU can access all its own memory as well as all the CPU memory and all the memory of any other GPU in the system. It has enough virtual address space for that. And so that means you can have a single virtual memory space, and the hardware just takes care of migrating the pages when they're needed. And with operating system support, that means that you can potentially support accessing memory even if it's just allocated with the system allocator, in other words malloc in C or new in C++. You can just allocate memory with malloc or new, pass that pointer to the GPU and access it, pass that pointer to another GPU and access it, use it on the CPU, etc., even if it's accessing more memory than the physical memory because this operating system handles virtual memory and paging out to disk and things like that. So this—it's a big step for heterogeneous in terms of making it easier but also enabling you to process data sizes that you possibly couldn't before because they wouldn't fit in GPU memory. Gotcha. What I heard was:
Magic. Yeah, right? It's like the magic eraser in Photoshop; it just works, right? But like what I just—it kind of ties into the whole LLVM thing where you know someone might just annotate a for loop and you want to send that to the GPU, and you want that process to be as painless as possible. You don't want to have to inject a bunch of copy-to-the-GPU commands into their code. Absolutely.
It absolutely ties into that. I think you're referring to the directives like OpenACC, right? Right. And um the PGI guys actually added a mode to the OpenACC compiler about a year ago that will automatically use Unified Memory behind the scenes. So in OpenACC, what you normally do is you have to—you first you annotate your loop. You say, 'Oh, this loop is parallel.' Parallel, but then you find oh it's slower because actually the compiler doesn't know which data it needs, and so it just copies all the data over for that loop. And even if some of it's read-only, for example, from the GPU or it's not accessed on the GPU, and so you can—you can go deeper in OpenACC and use these data directives to annotate, 'Oh, well copy this now,' or put a read-only copy on the GPU things like that. But with Unified Memory, you shouldn't have to do that. You I mean there's always you always know more about your program than the compiler does, so you can always help with performance by adding more information like that, but that becomes an optimization rather than a requirement, right? Yeah. And yeah, so Unified Memory really ties into the kind of automatic offloading approach. Gotcha. That makes
sense. That makes sense. So I know you work remotely, but sort of in general what is sort of day-to-day like? You have a lot of people who are in high school, in college—a lot of listeners who are in college—and they want to know a lot about industry and what it's like to work in different industries. So what's a typical day like at NVIDIA? Typical day.
With. Actually, so working remotely is good I think for me because well it allows me to live where I want. I don't know if we mentioned on the recording—I live in Australia. My family's originally from the U.S., but I now have an Australian family, and at least for now we you know we're living here, and I live you know in a beautiful place up in the mountains which I couldn't do in Silicon Valley or certainly couldn't afford to. So um the downside There's a downside. I mean, I'm remote. There's the obvious time zone issue. The downside of being remote is that you don't get to go into the office and work with your team directly every day. So if I were to give advice to young people starting out—because I think you were aiming towards that—go work in the office, go to the headquarters if you're going to a big company, unless you're working for a company that's distributed and that's the culture, and then you just have meetups and travel to meet each other now and then. Then you really want to experience the company culture. And I—and I did that. At first, I actually worked in the UK. I'd already been an intern in the home office, but I worked in the UK for a while in an office, and it makes a difference in terms of building your team and getting to know people. So if you work remotely, you really have to work to overcome the barriers of being remote and make sure people know you're there. So I have a lot of one-on-one meetings on the phone with people just so I'm staying in touch and staying in the loop, and so I can do my work. And then I travel. You know, I go to the U.S several times a year, but the times I would benefit, I guess, is that you know I get up in the morning and it's afternoon in California, and I have all my meetings early in the day, which is kind of a pain. They say you shouldn't start with meetings, but I have no choice. But after I'm done with my meetings, my whole afternoon is free to just focus on work. Whereas if you're in an office and you're involved in a lot of different things, that you end up getting called into tons of meetings, and it gets hard to get large blocks of time. And so I think it's important for—especially in engineering—to have large blocks of time because you really do have to have time to think.
Work. Yeah, definitely. Yeah. I mean, I agree that it's for people just starting out, I wouldn't recommend being remote first job. But you know, as in your case, once you know the team, there's a lot of companies that have a work from home day, maybe you know on Wednesdays everyone works from home or something like that, and in that case it's okay. You kind of work around that. Or even if you've been with a certain team for four or five six years and then you move off-site, you've built those relationships and you have those bonds, and then working from home can give you a lot of benefit. Thank you Mark so much for coming on the show. This is fascinating. I think yeah, it's all of us have benefited greatly from the work that you and other people of NVIDIA have done, both in ways for us to relax, play video games, and also in our day-to-day like at work making it so we can accelerate your programs. And as a person who does a lot of AI stuff, it's gone from things taking months to things taking hours, and that's just amazing. And I know Patrick's done a lot of high performance computing and things like that.
a lot. And yeah, we'll send the link to your blog. Your blog is Parallel For All. So people listening go to programmingthrowdown.com. We'll have a link to that. And thank you again. And we'll wrap it up. And thank you guys in the audience for supporting us on Patreon and the reviews and the comments feedback on social media, all of that. Yeah, we really appreciate all that. As you can tell, we've changed the format when we do interviews. People probably know this by now because we've done a few of them at this point. We'll do a programming language show next month, but we've just had some absolutely amazing people like Mark reach out to us, and so we definitely wanted to do this interview. And I hope you guys appreciate it. The intro music is Axo by Binar Pilot. Programming Throwdown is distributed under a Creative Commons Attribution ShareAlike 2.0 license. You're free to share, copy, distribute, transmit the work to remix, adapt the work, but you must provide attribution to Patrick and I, and ShareAlike in kind.
