Podcast Lookup

Episode 20: GTC 2021 Preview & Thrust Algorithms

ADSP: Algorithms + Data Structures = Programs

  • April 9, 2021
  • 36 min
About this episodeIn this episode, Bryce and Conor give a preview of their GTC 2021 talks and talk about some Thrust algorithms. Date Recorded: 2021-04-03…

In this episode, Bryce and Conor give a preview of their GTC 2021 talks and talk about some Thrust algorithms.

Date Recorded: 2021-04-03
Date Released: 2021-04-09

Intro Song Info

Miss You by Sarah Jansen https://soundcloud.com/sarahjansenmusic
Creative Commons — Attribution 3.0 Unported — CC BY 3.0
Free Download / Stream: http://bit.ly/l-miss-you
Music promoted by Audio Library https://youtu.be/iYYxnasvfx8

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

Episode 20: GTC 2021 Preview & Thrust Algorithms

00:00—
  1. Conor

    Oh my god, I'm gonna need to get more water in a stick. This is delicious.

  2. Bryce

    It's it's water, Connor. It's water.

  3. Conor

    Welcome to ABSP the podcast, episode 20, recorded on April 2nd, 2021. My name is Connor, and today with my co-host Bryce, we talk about the upcoming GTC 2021 conference and thrust outbreak. Anyways, you were biking too, right?

  4. Bryce

    Yeah, I went for uh 29 kilometers this morning. Nice.

  5. Conor

    Did you go on the same route as usual where you uh you told me before?

  6. Bryce

    Yes. I have done 1,132 kilometers this year. That's a lot. Yeah. It does feel like a lot. Makes me fe it makes me feel worse that you went out and did 20k on foot this morning, though. I was probably faster than you though.

  7. Conor

    You probably definitely were faster than me. Definitely. I I took uh I w I run by the Toronto waterfront too, and so every once in a while at the beautiful parts I'd stop and walk because how long does 20k take you? Uh how long did it take me? Let's go to let's go to the app. It depends, but uh I mean today was a pretty average run. I wasn't running too fast, too slow. So yeah, 19.35k. Uh it took me an hour and 33 minutes.

  8. Bryce

    That's actually not yeah, that's not bad.

  9. Conor

    Um that's a four four kilomet it's a four minute fifty four minute fifty seconds per kilometer. Um so like in a in an actual race, I typically am running around like four minutes a kilometer, um, or four, like between four and four ten.

  10. Bryce

    So when's the last time that I've done like nineteen? Um usually if if I'm at like my weekly ride hat well, my weekly my like weekday ride had been just around 19.5 kilometers, and that takes me like 56 minutes. So you're not going that much slower than me. That's pretty impressive. Um you use uh you use Strava to track your stuff?

  11. Conor

    Um I have a Garmin Vivo Active 3 Music, uh, so it primarily uploads to Garmin, but I hook it up to Strava.

  12. Bryce

    Um you use the Garmin Connect too. I also have Garmin.

  13. Conor

    Yeah.

  14. Bryce

    Although I I had to the the Garmin like heart rate monitor, like a month ago, it just like stopped. It was like it continued reading the sensor, but it kept like giving it bad data. Like it would just be like your heart rate was 72 the entire time you were biking. And I'm like, well, that's just that's not that's not true. That's not correct. And it would be and it would be like this this uh uh 30k hour-long intensive bike ride that you did burned a hundred calories.

  15. Conor

    Yeah, that's definitely uh. I I just got a true.

  16. Bryce

    I just bought a new one. It goes on my ri on my arm, and uh it uh it works really good. And it's uh it's like I don't even I I forget that it's on. It's much better than the the one the strap around the chest one.

  17. Conor

    Yeah, I had one of those for a bit and I just really didn't like it. Um and I've heard that the heart rate monitors when you're running or doing any exercise on like a watch, it's it's garbage. Um like it's not very accurate, but I mean I don't really care if it's off by whatever five or ten. Like relatively, I think it's probably accurate. Like, like, you know, even if it's off by a bit, like the still the relative changes and it produces a pretty graph.

  18. Bryce

    Um yeah, well, but but for for me, because I'm biking, I have the heart rate, and I also have the cadence and the speed sensor. Um so like the Garmin stuff, it like it factors in all the sensor data to build like a model. Like it it'll it can do if you don't have the heart rate sensor, it'll still like try to compute how many calories you spent, but it's supposed to be like more accurate if it has that. Although apparently the best, the best, the all the new rage is to have a a power sensor where it measures the actual power that you put into the pedals.

  19. Conor

    We should hop into, I'm not sure if there's any way you want to start this in particularly. Uh I guess sort of I'm in control. Um, but first things we should first thing we should do, we should plug our we we do a terrible job of promoting, or at least I personally do. Like I I gave two talks in the last month. I don't even think I mentioned the ACCU one, and I can't remember if I post uh editing like added in at the end of one of the Sean Parent episodes that I was giving an APL talk. I know I added it to the show notes, but I don't think we ever explicitly anyways. The GTC conference is coming up. I'm speaking once. Uh we're both on the same panel, and I think you have like two or three.

  20. Bryce

    We'll probably explain what GTC is.

  21. Conor

    We'll we'll explain in a second. And you have like two or three talks. So yeah, we're not gonna spend the whole episode talking about this, but we should we should do a better job of promoting now that we have a quasi-platform uh to tell people about other stuff we're doing. So you go first. And you can you can explore you can explain what GTC is.

  22. Bryce

    You're the you're the I I think it I think it'll be more amusing for me to watch you explain it. So, Connor, what is GTC?

  23. Conor

    GTC stands for the GPU technology conference. Um there's a bunch of them that happen happen globally, so it uh I guess that didn't explain anything. It's uh the best way to explain this is like every major tech company has their own uh conference. Um so like Facebook has Fate, Google has Google IO, uh, Amazon, what is theirs called? ReInvent, re-colon invent.

  24. Bryce

    You said major tech company and then proceeded to list three companies, all of which are like an order of magnitude larger than our company.

  25. Conor

    Yeah, that's because we're putting we're putting NVIDIA up there. And also, you probably heard my phone in the background uh when I said Google I.O.

  26. Bryce

    Um, it thought you were talking to it?

  27. Conor

    It thought I was talking into it, yeah. Um but uh I mean NVIDIA, yeah, we're wait, we're we're right up there. I mean, we could argue that FAINVIDIA.

  28. Bryce

    We work at NVIDIA and GTC is NVIDIA's tech conference.

  29. Conor

    Yeah, I was getting there, man. I was getting there. And uh that's what I was trying to do.

  30. Bryce

    It's taken a long route to get there.

  31. Conor

    I'm I'm trying to say is that Fang, which stands for Facebook, Amazon, Apple, Netflix, Google, I mean, you just give it time and we could be the new N in Fang. Um Yeah, because you know what?

  32. Bryce

    I gotta be honest, look, any of our listeners that work at Netflix, this is this would be a good time for you to hop off. Like, I don't I don't know why they put Netflix into that group, because like one of those things is not like the other of those things in terms of like scope scale of the number of people that work there. Like, I understand like maybe market cap at one point and like breadth of what they work on. Like one of those things is not like the other. And that one's Netflix.

  33. Conor

    I think it just uh helped with the acronym. Um Right, right, right.

  34. Bryce

    That's it. I think they they needed they needed they needed an N. And I think they picked one.

  35. Conor

    Definitely uh M from Microsoft would be better, but like what are you gonna do? What are you gonna do with like M what are you gonna do?

  36. Bryce

    But yeah, like how would you how would you say that?

  37. Conor

    Yeah, so we just gotta we just Yeah uh because you have two A's.

  38. Bryce

    You could put the A's on either side of the M. Gamp. Gamp. That's what you call it.

  39. Conor

    Yeah, it doesn't roll off the tongue at all. Anyways, so I said we weren't gonna spend the whole episode talking about this. Yeah, yeah. That's what GTC is. And uh what are you what are you talking about? What it what should people and it's registration's free, it's online, it's a virtual conference. Uh some of the talks are pre-recorded with the um the the presenters that are gonna be live there answering questions in the chat. Um and other ones I believe are gonna be live. There's both Nvidia's that are presenting and industry professionals. And if you're interested in anything, AI, machine learning, you know, GPU accelerated related, plus like a billion, there's like a billion tracks. I don't know them all. Um it's an awesome conference, you should check it out. What are you talking about?

  40. Bryce

    Um I'm given two talks, and there's a number of panels. So um in my my job at NVIDIA, I'm our uh one of our architects for HPC programming models, and I uh I work a lot with our HBC compiler team, so I'll be giving our our annual update on all the new and cool things in NVIDIA's HBC compilers, um, NVC ⁇ and NV Fortran. And I was looking through my slide deck, which is now I just completed it yesterday. I was looking through it and I realized there are more Fortran slides than C slides. I don't know how this happened because like I am not a Fortran person. I just have some like very high quality Fortran people uh that have been helping me out, and uh there's a lot of cool stuff going on in Fortran, but I was just like, can I give a talk where there's less C than Fortran? Like, can I not not like can I physically go through with doing that?

  41. Conor

    Do you have a slide with the C logo and the uh Fortran logo juxtaposed? Because those are two beautiful logos.

  42. Bryce

    You would be you will be very excited about the use of programming language logos in this um uh slide deck. And not only programming language logos, but also um uh logos for um programming models like OpenACC and OpenMP. I I went very Connor with this route.

  43. Conor

    There we go, folks. You heard it here first on ADSP, the podcast. Uh you gotta go to Bryce's talk. I'm looking forward to it.

  44. Bryce

    It's called Inside, NVC ⁇ , and NV Fortran.

  45. Conor

    That's the first of your talks, correct?

  46. Bryce

    Yes. The other talk I'm giving a talk on, it's called just called the NVIDIA C standard library. And um, it's just gonna be about the NVIDIA C standard library that my team develops. And uh we got a bunch of exciting stuff that we're announcing at both of these talks. And then uh I think I'm doing three different panels. I've got one like users forum panel for uh uh uh for our C libraries. Then there's one that we've done in past years called Future of Standard C of Standard and CUDA C. I think that's it. And then there's another like programming models panel that I'm on. And that's uh that's what's going on. That's what I got. And you gotta talk too, right?

  47. Conor

    I do, yes. I recorded that, pre-recorded that on Thursday, and it is entitled Thrust and the C standard algorithms. Um it's basically uh it's a kind of comparison between Thrust, which is a parallel algorithms library based on the C standard algorithms, and uh it's a comparison between the two. So it's broken up into five sections. The first one focuses on algorithms that exist in Thrust that don't exist in the C standard algorithms. So here, actually, this is what we're gonna do. We're gonna turn this into two episodes. Uh the first one we're just gonna call like GTC preview. Um, and we're gonna include most of the stuff talking about our biking, because that's what I realized too. This podcast here, I'm going off on a tangent. Uh, this podcast was supposed to be inspired by Magic Read Along, and one of the great things that I love about their podcast is usually each of them are like 20 minutes long, and like the first half of it, they're usually just talking about like barbecuing and like just random stuff, and it's just just like it's like listening to two guys in a phone call. And I realized our podcast has become like quote unquote too polished, even though it's I interrupt to apologize for Bryce blowing out his mic with his laughter in three, two, one. So far from being like a polished. It's it's too polished for what I want.

  48. Bryce

    If uh if Jason Turner and Rob, the uh the hosts of CPPcast, an actual polished podcast from C developers, hear that, they are gonna lose it. They are gonna lose it. Okay, all right.

  49. Conor

    Yeah, all right. It's it's it's more it's too polished because that's the thing, is we usually do sort of chat before hopping into the episode, but then a lot of that gets cut um and we just sort of put the highlights in the the first few seconds. Anyways, so I'm gonna try and start to include a bit more of that. Anyways, back to uh so this is we're just gonna do GTC preview because I was gonna ask this in the episode. So let's play a little game, which is uh um a highlight from the first section of the talk.

  50. Bryce

    This is gonna I have the feeling this is gonna be a game which embarrasses me, the primary like tech lead for thrust, um, about my lack of knowledge of the thing for which I am tech lead. Yeah, we can actually I'm not even technically really the tech lead anymore. I've handed that off to um somebody else on my team. It's great, like like all the things that I used to be tech lead for, now I have other people that are tech lead for them. Um and I'm just sort of like meta tech lead. It's wonderful.

  51. Conor

    Meta tech lead, I like that. So so we'll actually we'll play all three. So this will be uh sort of we'll spoil uh uh the talk, the first section of the talk, but there's four other sections that talks about um awesome things in Thrust. So uh people will still want to tune in. But so there's there's three problems in the first section of the talk um that I use to sort of motivate. I put the 20 algorithms that exist in Thrust that don't exist in the C standard algorithms, and then I show three problems um which are solvable by one of those 20 algorithms. So I'll just I'll go through them with you. Um so the first one is what do you it's an algorithm, a single algorithm that is the combination of std iota and std str std transform, or in thrust land, actually, well, you know, but yeah, thrust sequence uh plus uh thrust transform. So there's one algorithm that was renamed in thrust um from the C standard algorithms, and that was IOTA. We renamed it to Sequence. So if you combine, no, no, no, no, no.

  52. Bryce

    I I I am almost uh no, we've talked about this before.

  53. Conor

    We talked about this literally when Sean was on.

  54. Bryce

    Yeah, yeah. I I'm almost certain that this was independent origin. Like I'd have to go check with the original, with the original author, but I'm I'm pretty certain, you know, IOTA was in Alex Stepanov's STL, but not in the standard library until 11. And Thrust added sequence, which is the equivalent of IOTA, um, you know, before IOTA was a thing in C. But I'm fairly certain that sequence that it was not an intentional renaming, it was just, you know, sequence was just the name that's a good one. Was it uh was it was it Jared?

  55. Conor

    Was it Jared Hoborov that uh do you think we could we could just we should just have him on? Um he'd be interesting to talk to about initially writing thrust if he wants to come on.

  56. Bryce

    Yeah, yeah.

  57. Conor

    Anyways, have you figured out what do you get when you combine iota and transform? A single thrust algorithm.

  58. Bryce

    When you combine Well, what what what are the what are the semantics of this algorithm?

  59. Conor

    So it's an algorithm that basically given a range and a unary function object, typically in the form of a lambda, it will basically fill the values in that specified range uh with the unary operation applied to each of the indices that correspond to that range starting at zero.

  60. Bryce

    Oh yeah. Um I don't know which one I'm I'm vaguely familiar with this pattern, uh, but no, I don't know which one it is.

  61. Conor

    The answer, and if so, if anyone is listening and was playing along, the answer is an algorithm called thrust tabulate. That's right.

  62. Bryce

    Yeah, tabulate.

  63. Conor

    Which I I think is awesome because I knew that algorithm, I knew the name and I knew it existed, but I had no idea what it did. Uh and I did not think that's what it did at all. Uh, but I think that's pretty um I think that's pretty cute. Uh because it is um it's a very actually common thing to do in APL where you use iota to generate a list of numbers and then you do some transformation to get what you actually want. So like I go on to show an example of generating the first um 10 odd numbers, which uh you could do a number of ways in C. You could do it with like a generate n or you could do it with an iota and then a transform, but you can do it in a single line with uh tabulate by just passing it a unary uh function object like a lambda that just goes uh times two plus one.

  64. Bryce

    I feel like we may have talked about it on a past episode.

  65. Conor

    Tabulate? Definitely not, because I had I have I had no idea what tabulate did uh one week or two weeks ago when I was preparing for the talk.

  66. Bryce

    Because um uh yeah, the it it it's just implemented as transform with a counting iterator.

  67. Conor

    Yep. That would make sense.

  68. Bryce

    Um and I could have sworn we talked about it because we talked about how it's one of the you know, I think I thought we talked about it because it's an example of, you know, one of the many thrust algorithms that are just implemented in terms of other thrust algorithms. Um But uh so actually interestingly, because of that, it means that it's fairly or because of how it's implemented in Thrust, which is with a counting iterator in Thrust Transform, it's super easy to express in C20 with ranges, because it's just a um a transform with an iota view. Yep, yep, yep. Exactly.

  69. Conor

    Yeah, and and for those of that that aren't familiar, uh counting iterator is one of the fancy iterators in Thrust, um, which are super useful. And I'm surprised they're actually not more prevalent in sort of C a counting iterator basically can be used in replacement of a std iota where you don't really actually need to construct the range. But I'm not sure that's actually the best way to explain it. Is that how how would you explain what a counting iterator is?

  70. Bryce

    Well, so so so um the first thing to understand about a counting iterator is counting iterator is one of like a class of iterators where um it does it's not an iterator to any actual underlying data. It's an it's an iterator that will like synthesize um uh artificial data. And in the case of a counting iterator, what it synthesizes is a range of numbers. Um so that range of numbers isn't actually stored in memory anywhere, it gets created um on the fly as it's needed. And so in the case of a counting iterator, it it synthesizes the range of uh of um uh you know like whole numbers, you know, one, two, three, four, five, six, seven, eight, nine, ten, etc.

  71. Conor

    Right. Right. And it's it's incredible they're like all of the fancy iterators are incredibly useful. Uh especially because like one of the weaknesses of C algorithms is when you just you just want to uh use an algorithm over a range of numbers without constructing a container. Um and that's exactly what counting iterator can be used for. Um or uh a super useful iterator that we've built into the libqd in in rapids is a counting transform iterator, which is basically just the combination of a counting iterator and a transform iterator. Um because a lot of times you you want something that's not just you know 0, 1, 2, 3, 4, 5. Uh you want, you know, every second odd number or whatever. Um and so you need a counting transform iterator. Anyways, enough about iterators because we need to get to the the next two problems.

  72. Bryce

    So thrust, I was I was just I was just I was just doing the research to make sure that my uh uh that I'm not incorrect here. Um and I'm yeah, I that I I'm I'm almost 100% certain that I'm I'm correct that we explicitly do say in the standard that uh forward iterator is required for the parallel algorithms. Um now a lot of implementations in practice like our implementation NVIDIA in practice only really works with random access. Um uh MSVCs does work with forward, but nobody nobody I think actually works with input iterators. And I I seem to recall that there was there was a uh some other problem related to ranges recently where I think it was with maybe split, um, where um uh there was some view in ranges that produced iterators of a category that was not sufficiently strong to actually be useful. This the superior string splitting paper from Barry.

  73. Conor

    Oh yes. We will link that in the um in the show notes. What did I just go Australian there for? Alright, problem number two. How do you how do you um what algorithm can you use to copy every second element from a ring track? And this is uh I think this is a fantastic question if I don't say so myself.

  74. Bryce

    Um I think we may actually know the answer to this.

  75. Conor

    Yes, gatherer, yes, gather is the answer. So some of you might have been thinking copy of it. But the thing about copyif is that copyif um you could actually technically uh in the non-parallel case have a mutating lambda with a uh a um uh a Boolean flag that you're sort of toggling. You could get that to work. But the thing is that a copyif takes a unary predicate, and typically that's applied to your elements, and the elements that return true for that unary predicate get copied. But what if you just want to do something not based on some predicate, but just based on indices? Um that is exactly what thrust gather is for. So gather is provided uh an input range, an output range, and uh a gather map, which is basically a set of indices that uh are going to be the ones that are copied or sort of gathered from uh your input range. And it's interesting because this algorithm name was one of the sort of famous algorithms that Sean Parent shows in his original C seasoning talk, um, which is actually the combination of two stable partitions. Um and the example he uses is sort of highlighting emails and then gathering them to a single point. Um but we have uh reused that name for this. And it's uh got a sibling algorithm called scatter, but we're not gonna talk about that. Problem number three, or do you want to make any comments about gather before we move on to the other?

  76. Bryce

    I I just want to note for the record, I got that one.

  77. Conor

    So we're one one for two.

  78. Bryce

    Validating that I am not all fan air.

  79. Conor

    Will he be one for three or two for three? That is the question we are about to answer. Um, did I just come off as Super Canadian there with my about? I'm not sure.

  80. Bryce

    Um you've tried to come off as as Australian earlier. So coming off as Canadian seems fine.

  81. Conor

    Let us know when we tweet this out um when this episode comes out. If uh what I just realized I said the word about and I wasn't sure if it was a Canadian or American. Question number three. Um this is one of my favorite problems. It's called maximum consecutive ones, MCO. Uh so you're given a sequence of ones and zeros, and you want to determine the maximum sequence of contiguous ones, uh, what the length of that maximum sequence is. Um so technically in Thrust uh you need um two algorithms to solve this. So you know what? If you get this one right, we're gonna give we're this one's worth two points if you get it right.

  82. Bryce

    If not, I'm gonna do it. Why do you need two algorithms to solve this?

  83. Conor

    Um well if you can come up with a single solution, uh, or an a solution that just uses a single algorithm, um go for it.

  84. Bryce

    But I I only know one that a solution that's thinking you could do it in a single pass with a scan. Maybe. Or like a like a uh uh a reduction, but I don't I don't think that'll actually work with a reduction.

  85. Conor

    Um I mean if it works with a scan, it'll work with a reduction, right? Because in this case, we're just reducing down to a single number, and a scan is just a reduction with the um incremental results output as well. So like the a property of a scan is that the final value is always the answer of the equivalent reduction.

  86. Bryce

    Um but but the other the a property that you get from the the um from a scan that you don't get from a reduction is that the inform you always get information f coming from the left that gets incorporated into your partial result. With a reduction, it all all the information's incorporated, but you don't necessarily know as much about the ordering. Whereas for a scan uh i anyways, I I I I I digress here.

  87. Conor

    Um Right, that does make sense. That does make sense what you just said.

  88. Bryce

    I'm not sure whether you can do it with a single pass, but okay, so so so you're saying two algorithms you want to count the number of. Um you can need two algorithms. I don't know, enlighten me.

  89. Conor

    Uh the key algorithm that you need is a segmented algorithm, aka reduced by key.

  90. Bryce

    Reduced by key. Okay.

  91. Conor

    I'm surprised I'm surprised actually you didn't get this one because this is a modified version of a um problem that I talked about in my first talk, algorithm intuition, called Dangerous Teams, where you want to identify Yeah, so anyways, the the the problem is a modified version of maximum consecutive ones. And then you came up to me after the talk, uh I think the first time I gave it back in like January of 2019 at the C Bay Area user group, and you pointed out, like, oh, I think you can just solve that one problem with a reduced by key. And I didn't know what reduced by key was at the time, and I said, hmm, I'm not sure. Uh and then later on, sure enough, that was the case. Um so basically a reduced by key by default is going to do basically segmented reductions, so reductions on different segments of your range, and it determines those segments by whether or not um they're basic the uh keys are equal to each other. So you uh have two input sequences, one's a sequence of keys and one's a sequence of values. For the purposes of solving this problem, we can just pass our range of ones and zeros as both the keys and the values, and then it's gonna determine the segments by um adjacent equal elements. So in this case, the segments will be all contiguous ones and then all contiguous zeros, then all contiguous ones, and your output keys will end up being just like one, zero, one, zero, one, zero. And the default reduction on each of those segments is uh plus reduction, but I believe uh you can um overload and pass in custom uh binary uh comparators for the segment determination, and then also uh uh a custom binary operation for the reduction. Um so it's a super, super powerful, and there's 12 different segmented algorithms. There's inclusive and exclusive scan uh by keys, unique by keys, sort by keys. Um it's this whole uh land of algorithms that doesn't even exist in the standard algorithm library, but is super simple.

  92. Bryce

    And so what's the second one that what's the second pass that you have?

  93. Conor

    So once once you've done the reduced by key, you basically have uh two output ranges. In my example, I show that we can just throw away the ones and zero keys, so you can just use a discart iterator, which is another fancy iterator and thrust. Um but you you do want the plus reduced uh contiguous values, and that's gonna be in an output range. And so then you just need to do a max element um on your output range uh to get the maximum of those of those plus reductions on the segments. So in the example I show you have uh one, one, one, zero, and then one, one, one, one. So three ones is zero and then four ones. So the your resulting uh plus reductions on the segments are gonna be three, zero, and four. And then you just do a max element on that, dereference it to get the maximum of your own.

  94. Bryce

    So it reminds me a lot of of the parallel word count example.

  95. Conor

    The parallel word count.

  96. Bryce

    Yeah, the parallel word count where you where you do it with the transform reduce um that takes uh two inputs and and then you have a binary um uh a binary transform that looks at you know two different characters and determines whether whether it's at the whether it's at the beginning of a next word.

  97. Conor

    So what is that algorithm doing? It's just basically creating a histogram.

  98. Bryce

    Okay, so so so what you want to do, you want to count the maximum length of run of a of a run of the single element, right?

  99. Conor

    Mm-hmm.

  100. Bryce

    Yeah, you can you can do this in a single pass. Um uh okay, so uh given two elements that are adjacent to each other, you can write a function that um uh returns whether or not um it's the end of a run, correct? Correct. Um okay, so that's your tr that's your transform function. Um uh so so so with that we can detect the end of a run. Now all we need is a way to detect the length of that run. And that's where I think you might you might need the scan. Um if you can get if you can if you can write a function like that which um uh gives you yeah, if you can if you can write some sort of reduction function which returns um zero if it's in the middle of a run, and if it's at the or or returns some some some n um sentinel value that maybe has metadata if you're in the middle of a run and it returns um uh some special value that says, hey, this is the end of the run, and um uh the length of the run. Um and then you can reduce over that um uh uh and that's that's still a single pass, because you can do a transform reduce in a single pass, and that reduction can just be like a max function.

  101. Conor

    Um wait though, I I still uh I'm I need clarification. Uh your parallel word count at the end of the day is returning you what? Just the word, like it's just the number of words in a string?

  102. Bryce

    Yes, yeah.

  103. Conor

    So you have you start off with a single string, but then you create uh two ranges by doing the zip tail. So basically you're looking at adjacent elements. Um But like I'm confused on why, like, isn't the simplest way to solve this problem just to count like the number of spaces?

  104. Bryce

    Yeah, you know, that that that's what it's doing. I'm I'm just using that as an example of like like that that that's that's the uh I I I think that you can write this as some form of a transform inclusive scan or transform reduce. Let's put it this way. You can definitely do this as a single pass in serial, right? Because what what you do is in serial, like if it's if this is just a fold, um, if you're not worried about parallelism, then your uh what it's just like a reduction where you accumulate, you know, you you keep count of how many repetitive elements I've had, and then when I find a new one, um uh I reset the count. Um uh and then like you like at that when you find that new one and reset the count, that that um uh like you you give a special mark to that to that um, so so yeah I I think I know exactly what you're trying to communicate.

  105. Conor

    And this is actually this gets highlighted in the second section of my talk, which is called uh thrust reduce versus std accumulate, and shows an example of um how do you sum up the even numbers in a list. Yeah and exactly what you just said, when you're doing this in serial, uh there's no accumulate if or reduce if in C. So to do this with a single algorithm, you can basically do a stdd accumulate, and then inside your custom uh you know lambda, you can basically bake in the predicate of checking whether it's even, and if so, uh adding to your accumulator parameter um the value, otherwise just add zero. But in when when porting this to a parallel algorithm, whether that's uh std reduce and C17 or thrust reduce, um you can't do this because you need the associative and commutative requirements on your binary operation. So the way to do this is many times when you have a serial uh algorithm that's using an accumulate, you need to upgrade to a transform reduce, where the transform is gonna be factoring out that unary operation. Uh in the case of the sum even elements, you're factoring out the is it even? So your unary operation is gonna be um if it's even, just return the value. Uh otherwise uh zero it out. And then you can do a parallel reduction, uh a plus reduction on those zeros and even elements. So for your string example, it's the exact same thing. You have a vector of characters representing a string. You first transform all of those characters either to a one if you're a space and a zero if you're a non-space, and then just do a plus reduction on all those spaces and you're done.

  106. Bryce

    Yeah.

  107. Conor

    There's a trickier way to do it with like adjacent elements, um, but the easiest way, um, pending that there's not some like case where you have some sort of justified string and there's extra spaces, obviously that won't work. Um but in that case, that's where you would use some sort of transform reduce where you do the zip tail trick. So pass it uh the range from zero to n minus one and one to n. And then you you just always want to check whenever you have a character on the left and a space on the right, and that's the end of a word, and count those. So um that's where you could basically transform all of those versions in like whenever you have a character on the left, space on the right, that's a one, everything else is a zero, then do your plus reduction, which is I think what you were explaining.

  108. Bryce

    Yeah. Yeah, but but okay, so so so the so the answer your answer to that question is you do a you do a reduce by key, and then what do you do?

  109. Conor

    Uh a max element. So basically you're doing two reduces. Correct. Uh segmented reduction and then uh reduction reduction. So we'll give I'm gonna give Bryce a point for that, because he actually got it right in the past.

  110. Bryce

    Um, even though he's he's I'm still pr I'm I'm like fairly convinced that you can find a way to express that in a single a single pass, in a single algorithm. Potentially, potentially.

  111. Conor

    Well let's let's leave it to the lessoners, listeners. Um if you can come up with a single algorithm to do that, I would be super curious. That's all for this episode. Thanks for listening and have a great day.