Video
Đăng bởi Quản trị viên OpenER ·
Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
[MUSIC PLAYING] BORA NIKOLIC: Hello, and welcome back to our parallelism module.
So now we know how do SIMD extensions work, how are they supported by the instruction set architecture, how are they generally roughly implemented in hardware, and how do we use these instructions in assembly and perhaps as intrinsics in C.
So let's take a look at one example that puts things together and uses the SIMD instructions to perform matrix multiplication.
So we have seen this matrix multiplication as a motivating example early on. This is a really tiny matrix.
It is multiplying only 2 by 2 matrices, 2 by 2 square matrices, and produces another 2 by 2 square matrix.
But I've circled these elements that we are working with in a little bit different way than I've done it before.
Do want to highlight here that the way how data is organized in memory matters a lot. So be careful about that.
We are assuming that the data is stored in the column format. So in this case, A1,1 is next to A2,1.
They are neighbors in a column. So they should be stored next to each other in the memory.
But generally, in a larger matrix, A1,1 is not going to be next to A1,2. Okay.
So let's see what do we need to do in order to perform basically a half of this or a quarter of a matrix multiplication.
We'll use elements A1,1, A2,1 and multiply them in B1,1 to get this first two elements in C1,1 and C2,1.
That's where the parallelism is going to come from. So when we are doing matrix multiplication, we will first initialize the matrix. So we'll set everything to 0's.
And then we are going to be adding these element-wise products to that running sum. So in order to implement what I've just said, we need to do a couple of steps.
First, we need to load these operands into the XMM registers.
So if A1,1 and A2,1 are next to each other — they're stored in a column order — they're going to be read in order.
And they're going to be stored together as a packed double precision vector.
Now, there is another instruction that enables us to essentially read the next operand — which, by the way, are not necessarily stored next to each other because B1,1 — B matrix is also stored in a column order.
These two elements, B1,1 and B1,2, are not going to be next to each other. So we can read both of those in two separate instructions and make two copies of that.
There is a separate instruction — SSE instruction — that does that. So we got things where they are supposed to be. We can go ahead and multiply them.
But we are actually going to perform multiply and addition. Typically, these SSE instructions, or different kinds of vector instructions, support something that is called a fused multiply add.
So they do multiplication and addition in one instruction. That instruction may take more than one cycle.
But it's one take to do multiply add.
So in this case, it is spelled out as mm_add then with the contents over here of a multiple inside of it.
So that is essentially these two instructions one after another will first do parallel multiplication and then do parallel addition in XMM registers.
So as a result, in C1, we are going to have A1,1, B1,1 in the upper part of that register.
In the lower part of the register, we're going to have A2,1, B1,1. Essentially, this part over here.
And then in C2, we are going to have A1,1, B1,2, and A2,1, B1,2. Then we continue.
We get the next few elements of this matrix register. A is going to get loaded with the next elements in the array.
So will be the registers B1 and B2. These are all just XMM registers. And we turn the crank one more time. And we are done.
Our registers — destination registers — C1 and C2 contain four elements of our double precision result matrix. And that is it.
That's essentially what is the — how we can use these SIMD instructions to perform matrix multiplication multiple elements at a time.
The code is shown here. I'm not going to walk through it. It is reasonably straightforward. It is shown as an example of using intrinsics in the C code.
So what you'll find out here is a mix of standard C code with the intrinsics declarations and then intrinsic instructions.
And that's it. It's a relatively quick code. It is worth walking through it and understanding exactly how does it implement what I've shown in the previous few slides.
We are almost ready to wrap up our discussion about the SIMD architectures and their role in parallelism.
First, I want to make a quick remark about RISC-V. We've been talking about RISC-V so far. But we had to switch over to Intel x86 extensions for this module.
Why is that? Well, there are RISC-V vector extension — there is a RISC-V vector extension that is in development. It is still in the draft.
It is almost ready to go primetime. But we can't have any — there is no hardware that supports it. So we can't have projects, and labs, and so on.
Give it a year or two, and RISC-V vector extensions will be there. And we can do everything in the RISC-V world.
For now, we need to patch it up with Intel's x86, AVX, and SSEs.
What you'll find out — please, you can take a look, if you like, into the extension draft. There is a whole bunch of instructions there. Assumes 512 wide vectors.
In conclusion, we have examined Flynn's taxonomy of parallel architectures. And we have paid attention generally in SIMD and MIMD architectures.
And actually, what we have learned so far are the SIMD architectures. And we are going to revisit SIMD architectures when we take a look at GPUs a bit later in the course.
And, in the next few segments, we are going to take a look at MIMD instructions. We will also examine how this is implemented in case of Intel's AVX SIMD extension.
And we can see that there is one instruction it can fetch and another instruction will operate on a vector of data or multiple operands simultaneously.
And we have seen how we can use those, how we can directly access those as C intrinsics. That is it. It's time for a break.
And we'll continue with how do we work with multiple processors and multiple cores. See you later.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.