Restricted pointer
From cppreference.com
This section is incomplete Reason: needs content |
[edit] Keywords
restrict
[edit] Example
This section is incomplete Reason: replace by a concise example |
To verify the 15% savings in this matrix-vector product, remove "restrict" from function Axb.
Run this code
#include <stdio.h> #include <time.h> /* #define restrict */ // Ax = b // Arrays A, x, and b could be aliased, so restrict pointers. void Axb (float * restrict A, float * restrict x, float * restrict b, int n) __attribute__ ((noinline)); void Axb (float * restrict A, float * restrict x, float * restrict b, int n) { int i; for (i=0; i<n/5; i++) { b[0] = A[0]*x[0] + A[1]*x[1] + A[2]*x[2] + A[3]*x[3] + A[4]*x[4]; b[1] = A[5]*x[0] + A[6]*x[1] + A[7]*x[2] + A[8]*x[3] + A[9]*x[4]; b[2] = A[10]*x[0] + A[11]*x[1] + A[12]*x[2] + A[13]*x[3] + A[14]*x[4]; b[3] = A[15]*x[0] + A[16]*x[1] + A[17]*x[2] + A[18]*x[3] + A[19]*x[4]; b[4] = A[20]*x[0] + A[21]*x[1] + A[22]*x[2] + A[23]*x[3] + A[24]*x[4]; A += 25; b += 5; } } /* A * x ==> b */ /* 50000x5 * 5x1 ==> 50000x1 */ #define NROW 50000 #define NCOL 5 float A[NROW*NCOL], x[NCOL], b[NROW]; int main(void) { // Initialize matrix A and vector x. int i; for (i=0; i<NCOL; ++i) x[i] = 1; for (i=0; i<NROW*NCOL; ++i) A[i] = i; clock_t t = clock(); // start clock for (i=0; i<10000; ++i) Axb(A,x,b,NROW); t = clock()-t; // stop clock printf("Time used: %f seconds\n", ((float)t)/CLOCKS_PER_SEC); return 0; }
Possible output:
Time used: 1.880000 seconds