Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,500 --> 00:00:09,500
Hello and welcome. In this lecture, we're going to talk about the restrict type qualifier.
2
00:00:10,160 --> 00:00:13,560
So the restrict type qualifier, another optimization hint for the compiler
3
00:00:13,560 --> 00:00:17,060
just like the other type qualifiers. The compiler can choose to ignore it.
4
00:00:17,660 --> 00:00:21,330
It's used in pointer declarations as a type qualifier for pointers.
5
00:00:21,330 --> 00:00:24,690
It tells the compiler that a particular pointer is the only reference
6
00:00:24,690 --> 00:00:27,190
to the value it points to throughout its scope.
7
00:00:27,990 --> 00:00:31,550
So what it means is the same value is not referenced by any other pointer
8
00:00:31,550 --> 00:00:33,350
or variable within that scope.
9
00:00:33,350 --> 00:00:37,340
The pointer is the sole initial means of accessing a data object.
10
00:00:37,540 --> 00:00:41,140
This tells the compiler that it does not need to add any additional checks.
11
00:00:42,040 --> 00:00:45,440
So without using this keyword, the compiler has to assume the worst case.
12
00:00:45,940 --> 00:00:47,600
It's going to do extra things.
13
00:00:47,600 --> 00:00:51,600
It has to assume that some other identifier might have changed the data in
14
00:00:51,600 --> 00:00:53,100
between the two uses of the pointer.
15
00:00:54,100 --> 00:00:57,300
And so with the restrict keyword used the compiler is free to look
16
00:00:57,300 --> 00:01:00,600
for computational shortcuts, it can do some optimization.
17
00:01:00,600 --> 00:01:05,300
So if a programmer uses restrict and they violate the above condition,
18
00:01:05,300 --> 00:01:07,500
the result is going to be undefined behavior.
19
00:01:08,800 --> 00:01:11,600
It's not supported by c++,
20
00:01:11,600 --> 00:01:14,100
so it's not portable to c++.
21
00:01:14,700 --> 00:01:19,000
The syntax is a little different. You have the restrict keyword after the pointer.
22
00:01:19,660 --> 00:01:22,960
So you have int pointer restrict and then the name of the pointer.
23
00:01:23,660 --> 00:01:27,260
So you do again int pointer restrict in pointer b.
24
00:01:27,260 --> 00:01:29,920
This tells the compiler that for the duration of the scope
25
00:01:29,920 --> 00:01:32,820
in which int pointer a and int pointer b are defined,
26
00:01:32,820 --> 00:01:34,820
they will never access the same value.
27
00:01:36,320 --> 00:01:41,020
So their use for pointing to integers inside an array is mutually exclusive.
28
00:01:43,020 --> 00:01:45,020
They're never going to access that same value.
29
00:01:45,380 --> 00:01:47,380
Let's look at an example in code blocks.
30
00:01:49,980 --> 00:01:54,880
So here we are in code blocks. We're going to create some variables.
31
00:01:55,880 --> 00:02:00,080
We're going to create an array of 10 elements, so we're just going to say int array
32
00:02:01,440 --> 00:02:04,440
10, not the best name, but okay for now.
33
00:02:04,440 --> 00:02:08,040
We're then going to say int star restrict.
34
00:02:08,040 --> 00:02:11,340
So we're creating a restrict point here. We're going to say restar,
35
00:02:12,000 --> 00:02:14,500
and we're going to say int star.
36
00:02:14,500 --> 00:02:18,500
We're going to cast that because malloc returns a void pointer. We're going to say malloc,
37
00:02:18,500 --> 00:02:22,900
and we're going to say 10 times size of int,
38
00:02:24,100 --> 00:02:27,100
something like that.And we're going to have to include malloc.
39
00:02:31,780 --> 00:02:36,180
And then we're going to do int star
40
00:02:36,180 --> 00:02:40,380
par equals ar. We're going to assign a pointer to a pointer.
41
00:02:40,880 --> 00:02:42,480
So the pointer restar here
42
00:02:43,780 --> 00:02:47,880
is the sole initial means of access to the memory allocated by malloc.
43
00:02:48,480 --> 00:02:50,140
The pointer par here
44
00:02:50,940 --> 00:02:54,540
is neither the initial nor the sole means of access to the data in
45
00:02:54,540 --> 00:02:59,740
the ar array, sorry this has to be array so in the array,
46
00:03:00,730 --> 00:03:04,730
so it cannot be qualified as restrict. This cannot be qualified as restrict.
47
00:03:06,630 --> 00:03:08,130
So now let's add some more code.
48
00:03:10,130 --> 00:03:13,120
Here we're going to just do some assignments inside of an array.
49
00:03:16,120 --> 00:03:21,320
So we're going to create a local variable for the array int n.
50
00:03:23,680 --> 00:03:28,880
And then we're going to create a for loop where we're going to do some assignments.
51
00:03:30,880 --> 00:03:34,880
So we have four n equals zero n is less than ten we're iterating through the array.
52
00:03:34,880 --> 00:03:38,480
We're using par, and we're using restar.
53
00:03:38,480 --> 00:03:40,140
And then we're also going to use array
54
00:03:42,140 --> 00:03:46,130
and par and restar again. So restar is restrict.
55
00:03:46,130 --> 00:03:51,030
The compiler can actually replace these two statements on lines 12 and 15
56
00:03:51,030 --> 00:03:54,930
involving restar with a single statement having the same effect
57
00:03:54,930 --> 00:03:59,930
because restar is the sole initial means of access to the block of data points to.
58
00:04:00,530 --> 00:04:02,030
So it could actually --
59
00:04:02,690 --> 00:04:06,990
the compiler can do something like say restar
60
00:04:06,990 --> 00:04:10,990
n plus equals to eight.
61
00:04:10,990 --> 00:04:14,290
So when it goes to compile this code and it generates an assembly language,
62
00:04:14,950 --> 00:04:17,650
it can replace those two statements with just a single statement,
63
00:04:17,650 --> 00:04:22,550
it optimizes it right because that's the sole initial means of access for this restar pointer.
64
00:04:23,540 --> 00:04:27,340
So instead of having lines 12 and 15, get
65
00:04:27,340 --> 00:04:29,940
created assembly code you can just create that one statement.
66
00:04:31,440 --> 00:04:33,440
Now you can't do the same for par.
67
00:04:34,040 --> 00:04:37,840
It would actually be a computational error if you try to condense the
68
00:04:37,840 --> 00:04:40,400
two part statements on lines 11 and 14.
69
00:04:40,400 --> 00:04:44,200
You can't condense those into one so. You would not be able to do
70
00:04:44,200 --> 00:04:48,860
par of n plus equals eight right.
71
00:04:48,860 --> 00:04:50,960
This is going to give the wrong answer.
72
00:04:50,960 --> 00:04:54,860
And the reason it gives the wrong answer is because the loop uses array
73
00:04:56,160 --> 00:05:00,160
to change the value of the data between the two times that par
74
00:05:00,820 --> 00:05:02,120
accesses the same data.
75
00:05:03,480 --> 00:05:04,980
So it can't actually do that.
76
00:05:05,780 --> 00:05:08,780
And that's really the benefit of using the restrict.
77
00:05:09,380 --> 00:05:12,280
So understand that difference because restar
78
00:05:12,280 --> 00:05:15,580
is defined as restrict and it's the sole access
79
00:05:15,580 --> 00:05:18,180
it can actually do some compiler optimizations.
80
00:05:18,180 --> 00:05:20,840
Now you can also restrict with function parameters.
81
00:05:21,340 --> 00:05:24,700
You can use the restrict keyword as a qualifier for a function parameter
82
00:05:24,700 --> 00:05:27,300
that are pointers, so again it's only on pointers.
83
00:05:27,300 --> 00:05:30,850
So what this means is that the compiler can assume that no other identifiers
84
00:05:30,850 --> 00:05:33,850
modify the pointer to data within the body of the function.
85
00:05:33,850 --> 00:05:37,510
So the compiler can try optimizations it might not otherwise use.
86
00:05:38,170 --> 00:05:42,470
The c library has two functions for copying bytes from one location to the other
87
00:05:42,470 --> 00:05:47,130
in c99. The m copy and the m move.
88
00:05:48,030 --> 00:05:51,530
And if you looked at these function prototypes,
89
00:05:51,530 --> 00:05:56,520
you would see them defined like this. And you'll notice the restrict keyword is in there.
90
00:05:56,520 --> 00:06:00,820
But what these functions do is each one copies n bytes from location s2
91
00:06:01,720 --> 00:06:03,220
to location s1.
92
00:06:03,920 --> 00:06:07,920
So m copy requires that there be no overlap between the two locations.
93
00:06:08,580 --> 00:06:11,240
Memmove does not have this requirement.
94
00:06:12,240 --> 00:06:15,140
So declaring s1 and s2 as restrict
95
00:06:15,140 --> 00:06:18,940
means each pointer is the sole means of access for mem copy.
96
00:06:18,940 --> 00:06:21,200
They cannot access the same block of data
97
00:06:22,450 --> 00:06:25,450
and this matches the requirement that there be no overlap.
98
00:06:25,950 --> 00:06:28,950
Now mem move doesn't allow an overlap
99
00:06:29,350 --> 00:06:32,150
so it has to be more careful about copying the data
100
00:06:32,150 --> 00:06:35,050
so that it does not overwrite the data before it is used
101
00:06:35,050 --> 00:06:37,050
and that's why it doesn't use restrict.
102
00:06:37,410 --> 00:06:41,910
So that's mem copy and memmove. Let's look at our own function definition.
103
00:06:42,570 --> 00:06:45,170
And what we want to do here is
104
00:06:46,170 --> 00:06:47,770
we're going to create a function,
105
00:06:49,970 --> 00:06:51,970
we're just going to call it f1.
106
00:06:53,770 --> 00:06:58,130
And we're going to define it as first parameter n, second parameter
107
00:06:58,130 --> 00:07:02,130
is going to be a restricted pointer a1
108
00:07:02,130 --> 00:07:06,730
pointing to a foat and then it's also going to be a constant for the third parameter flow
109
00:07:06,730 --> 00:07:10,090
restrict a2.
110
00:07:13,390 --> 00:07:16,510
And then inside there, we're just going to basically
111
00:07:16,510 --> 00:07:19,170
have a local variable and a loop,
112
00:07:19,830 --> 00:07:21,330
something like this,
113
00:07:21,990 --> 00:07:26,990
i equals 0. And then we're going to assign something a1 and a2.
114
00:07:27,590 --> 00:07:30,290
Now a1 and a2 can be assumed
115
00:07:30,290 --> 00:07:32,490
to refer to disjoint array objects.
116
00:07:32,990 --> 00:07:35,590
They're both of them are restrict qualified.
117
00:07:36,890 --> 00:07:39,890
So what this implies is that each iteration of the loop
118
00:07:40,490 --> 00:07:42,490
is independent of the others.
119
00:07:42,490 --> 00:07:47,370
And so the loop can be progressively optimized because of that restrict.
120
00:07:47,370 --> 00:07:49,730
And so this is why you'd want to use restrict
121
00:07:50,630 --> 00:07:54,430
you know when you're the sole axis of a pointer and you can't optimize.
122
00:07:55,030 --> 00:07:57,130
Okay. So we might see restrict a lot more
123
00:07:57,130 --> 00:08:01,790
we talk about the pointer section with double pointers but understand the basics
124
00:08:01,790 --> 00:08:05,090
here of what the restrict can do concerning pointers. Thank you.
11515
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.