restrict, static & inline Keywords in C Markus Fasselt Universität Hamburg Fakultät für Mathematik, Informatik und Naturwissenschaften Fachbereich Informatik Arbeitsbereich Wissenschaftliches Rechnen Seminar "Effiziente Programmierung in C" December 19, 2013
Table of Contents restrict 1 . . static 2 . . inline 3 . Markus Fasselt restrict, static & inline Keywords in C 2 / 46
http://www.flickr.com/photos/joanet/2995263088/ restrict
. alias . “In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.” . http://en.wikipedia.org/wiki/Aliasing_(computing) restrict • included in C99 • restrict is a type qualifier on pointer variables • does not restrict anything! • says there is no alias for an address / variable Markus Fasselt restrict, static & inline Keywords in C 4 / 46
restrict • included in C99 • restrict is a type qualifier on pointer variables • does not restrict anything! • says there is no alias for an address / variable . alias . “In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program.” . http://en.wikipedia.org/wiki/Aliasing_(computing) Markus Fasselt restrict, static & inline Keywords in C 5 / 46
1 void update_restrict( int * restrict a, int * restrict b, 2 { 3 4 5 } int * restrict c) *b += *c; *a += *c; *b += *c; *a += *c; Example Code 1 void update( int *a, int *b, int *c) 2 { 3 4 5 } Markus Fasselt restrict, static & inline Keywords in C 6 / 46
int * restrict c) *b += *c; *b += *c; *a += *c; *a += *c; Example Code 1 void update( int *a, int *b, int *c) 2 { 3 4 5 } 1 void update_restrict( int * restrict a, int * restrict b, 2 { 3 4 5 } Markus Fasselt restrict, static & inline Keywords in C 7 / 46
*a += *c; return 0; *b += *c; %d %d\n", a, b); int a = 1; int b = 2; int c = 3; printf("Actual Result: update_restrict(&a, &b, &c); update(&a, &b, &c); a = 1; b = 2; c = 3; // reset values printf("Expected Result: %d %d\n", a, b); Example Code 1 int main( int argc, char** argv) 2 { 3 4 5 6 7 8 9 10 11 12 13 14 } 1 void update( int *a, int *b, int *c) 2 { 3 4 5 } Markus Fasselt restrict, static & inline Keywords in C 8 / 46
%d %d\n", a, b); return 0; printf("Actual Result: 4 5 update_restrict(&a, &b, &c); a = 1; b = 2; c = 3; // reset values printf("Expected Result: %d %d\n", a, b); int a = 1; int b = 2; int c = 3; update(&a, &b, &c); Example Code 1 int main( int argc, char** argv) 2 { 3 4 5 6 7 8 9 10 11 12 13 14 } 1 Expected Result: 4 5 2 Actual Result: Markus Fasselt restrict, static & inline Keywords in C 9 / 46
1 void update_restrict( int *restrict a, int *restrict 1 update: 2 3 4 5 6 movq %rax, addq (%rdx), %rax addq ret (%rdi) %rax, (%rdx), %rax movq b, int *restrict c); (%rsi) How It Works 1 void update( int *a, int *b, int *c); Markus Fasselt restrict, static & inline Keywords in C 10 / 46
1 void update_restrict( int *restrict a, int *restrict %rdx c %rdi 0x123 1 b %rsi 0x124 2 %rdx reg 0x125 3 - %rax - %rdi %rsi a val var movq b, int *restrict c); movq (%rdx), %rax addq %rax, ret (%rdi) (%rdx), %rax addq %rax, (%rsi) How It Works 1 void update( int *a, int *b, int *c); ∗ val 1 update: 2 3 4 5 6 Markus Fasselt restrict, static & inline Keywords in C 11 / 46
1 void update_restrict( int *restrict a, int *restrict %rdx c %rdi 0x123 1 b %rsi 0x124 2 0x125 %rdx reg 3 - %rax 3 - %rdi %rsi a val var (%rdx), %rax b, int *restrict c); movq (%rdx), %rax addq %rax, movq (%rdi) addq %rax, (%rsi) ret How It Works 1 void update( int *a, int *b, int *c); ∗ val 1 update: 2 3 4 5 6 Markus Fasselt restrict, static & inline Keywords in C 12 / 46
1 void update_restrict( int *restrict a, int *restrict %rdx c %rdi 0x123 4 b %rsi 0x124 2 0x125 %rdx reg 3 - %rax 3 - %rdi %rsi a val var (%rdx), %rax b, int *restrict c); movq (%rdx), %rax addq %rax, movq (%rdi) addq %rax, (%rsi) ret How It Works 1 void update( int *a, int *b, int *c); ∗ val 1 update: 2 3 4 5 6 Markus Fasselt restrict, static & inline Keywords in C 13 / 46
1 void update_restrict( int *restrict a, int *restrict %rdx c %rdi 0x123 4 b %rsi 0x124 2 0x125 %rdx reg 3 - %rax 3 - %rdi %rsi a val var (%rdx), %rax b, int *restrict c); movq (%rdx), %rax addq %rax, movq (%rdi) addq %rax, (%rsi) ret How It Works 1 void update( int *a, int *b, int *c); ∗ val 1 update: 2 3 4 5 6 Markus Fasselt restrict, static & inline Keywords in C 14 / 46
1 void update_restrict( int *restrict a, int *restrict %rdx c %rdi 0x123 4 b %rsi 0x124 5 0x125 %rdx reg 3 - %rax 3 - %rdi %rsi a val var (%rdx), %rax b, int *restrict c); movq (%rdx), %rax addq %rax, movq (%rdi) addq %rax, (%rsi) ret How It Works 1 void update( int *a, int *b, int *c); ∗ val 1 update: 2 3 4 5 6 Markus Fasselt restrict, static & inline Keywords in C 15 / 46
%rdx addq %rsi %rdi ret (%rsi) %rax, addq (%rdi) %rax, addq (%rdx), %rax movq (%rsi) %rax, ret movq movq b, int *restrict c); (%rdi) %rax, addq (%rdx), %rax (%rdx), %rax How It Works 1 void update( int *a, int *b, int *c); 1 void update_restrict( int *restrict a, int *restrict 1 update: 1 update_restrict: 2 2 3 3 4 4 5 5 6 Markus Fasselt restrict, static & inline Keywords in C 16 / 46
%d %d\n", a, b); *a += *c; printf("Actual Result: *b += *c; int a = 1; update_restrict(&a, &b, &a); int b = 2; a = 1; b = 2; // reset values printf("Expected Result: %d %d\n", a, b); update(&a, &b, &a); Passing Incorrect Parameters 1 int main( int argc, char** argv) 2 { 3 4 5 6 7 8 9 10 11 12 13 } 1 void update( int *a, int *b, int *c) 2 { 3 4 5 } Markus Fasselt restrict, static & inline Keywords in C 17 / 46
a = 1; b = 2; // reset values update(&a, &b, &a); 2 3 %d %d\n", a, b); int a = 1; printf("Actual Result: int b = 2; update_restrict(&a, &b, &a); printf("Expected Result: %d %d\n", a, b); Passing Incorrect Parameters 1 int main( int argc, char** argv) 2 { 3 4 5 6 7 8 9 10 11 12 13 } 1 Expected Result: 2 4 2 Actual Result: Markus Fasselt restrict, static & inline Keywords in C 18 / 46
val 0x123 %rsi b 1 0x123 %rdi a c %rdx reg var ret (%rsi) 2 %rax, addq 1 (%rdi) %rax, addq - (%rdx), %rax movq %rax - b, int *restrict c); 0x124 Passing Incorrect Parameters 1 void update_restrict( int *restrict a, int *restrict ∗ val 1 update_restrict: 2 3 4 5 Markus Fasselt restrict, static & inline Keywords in C 19 / 46
%rdx ret c 2 0x124 %rsi b 1 0x123 %rdi a val reg var 1 0x123 (%rsi) %rax, addq - (%rdi) %rax, addq %rax (%rdx), %rax movq 1 - b, int *restrict c); Passing Incorrect Parameters 1 void update_restrict( int *restrict a, int *restrict ∗ val 1 update_restrict: 2 3 4 5 Markus Fasselt restrict, static & inline Keywords in C 20 / 46
%rdx ret c 2 0x124 %rsi b 2 0x123 %rdi a val reg var 2 0x123 (%rsi) %rax, addq - (%rdi) %rax, addq %rax (%rdx), %rax movq 1 - b, int *restrict c); Passing Incorrect Parameters 1 void update_restrict( int *restrict a, int *restrict ∗ val 1 update_restrict: 2 3 4 5 Markus Fasselt restrict, static & inline Keywords in C 21 / 46
%rdx ret c 3 0x124 %rsi b 2 0x123 %rdi a val reg var 2 0x123 (%rsi) %rax, addq - (%rdi) %rax, addq %rax (%rdx), %rax movq 1 - b, int *restrict c); Passing Incorrect Parameters 1 void update_restrict( int *restrict a, int *restrict ∗ val 1 update_restrict: 2 3 4 5 Markus Fasselt restrict, static & inline Keywords in C 22 / 46
*b += *c; { } *a += *c; for (i = 0; i < iterations; i++) Performance 1 void update( int *a, int *b, int *c) 2 { 3 4 5 6 7 8 } • 100.000.000.000 Iterations • without restrict: 249.086421s • with restrict: 83.690911s ⇒ Performance Benefit: 2,98x faster Markus Fasselt restrict, static & inline Keywords in C 23 / 46
Why should I use it? • Better Performance :) • restrict is recommended to be used in every new code • You don't need to, if you don't want to! • (However, be careful!) Markus Fasselt restrict, static & inline Keywords in C 24 / 46
http://www.flickr.com/photos/38102502@N07/3757737766/in/photolist-6J4owb static
Translation Unit (nota bene) • is the output of the preprocessor / input to the compiler • is a single .c file after preprocessing ▶ header files have been included ▶ #ifdef's have been evaluated ▶ macros have been expanded • basically, same translation unit means the same file Markus Fasselt restrict, static & inline Keywords in C 26 / 46
static • used in variable and function defintions • can be applied on 1. global variables and functions 2. variable definitions inside a block Markus Fasselt restrict, static & inline Keywords in C 27 / 46
Recommend
More recommend