D
diegotorquemada
Hello all!
I am trying to learn to use the reserved word restrict. However, it seems that it is not working. The code with and without that keyword have the same size and running time, even though I am inducing pointer aliasing.
Can somebody please check my minimal example?
Thanks and kind regards!
Diego
/*
gcc -std=c99 -O3 -Wall -D USE_RESTRICT -o 08_restrict 08_restrict.c
ls -l
time -p ./08_restrict
*/
#include<stdio.h>
#include<stdlib.h>
#ifdef USE_RESTRICT
#else
#define restrict
#endif
const int N = 100000;
int *restrict x, *restrict y, *restrict z;
void fun(int *restrict a, int *restrict b, int *restrict c, int n);
int main()
{
x = malloc(N * sizeof(int));
y = malloc(N * sizeof(int));
z = malloc(N * sizeof(int));
if (x==NULL || y==NULL || z==NULL)
{
fprintf(stderr, "Error with malloc()!\n");
return EXIT_FAILURE;
}
// run this several times in order to have some average time
for (int i=0; i<10000; i++)
{
for (int j=0; j<N; j++) x[j] = y[j] = z[j] = j;
fun(x, y, z, N);
// fun(x, x, x, N);
}
free(x);
free(y);
free(z);
return EXIT_SUCCESS;
}
void fun(int *restrict a, int *restrict b, int *restrict c, int n)
{
for (int i=n; i<n; i++)
{
// The compiler has to read c in the second line because
// it may happen that either b points to a or c.
b = b + c;
a = a + b*c;
}
return;
}
I am trying to learn to use the reserved word restrict. However, it seems that it is not working. The code with and without that keyword have the same size and running time, even though I am inducing pointer aliasing.
Can somebody please check my minimal example?
Thanks and kind regards!
Diego
/*
gcc -std=c99 -O3 -Wall -D USE_RESTRICT -o 08_restrict 08_restrict.c
ls -l
time -p ./08_restrict
*/
#include<stdio.h>
#include<stdlib.h>
#ifdef USE_RESTRICT
#else
#define restrict
#endif
const int N = 100000;
int *restrict x, *restrict y, *restrict z;
void fun(int *restrict a, int *restrict b, int *restrict c, int n);
int main()
{
x = malloc(N * sizeof(int));
y = malloc(N * sizeof(int));
z = malloc(N * sizeof(int));
if (x==NULL || y==NULL || z==NULL)
{
fprintf(stderr, "Error with malloc()!\n");
return EXIT_FAILURE;
}
// run this several times in order to have some average time
for (int i=0; i<10000; i++)
{
for (int j=0; j<N; j++) x[j] = y[j] = z[j] = j;
fun(x, y, z, N);
// fun(x, x, x, N);
}
free(x);
free(y);
free(z);
return EXIT_SUCCESS;
}
void fun(int *restrict a, int *restrict b, int *restrict c, int n)
{
for (int i=n; i<n; i++)
{
// The compiler has to read c in the second line because
// it may happen that either b points to a or c.
b = b + c;
a = a + b*c;
}
return;
}