Hi all. I am trying to use restricted pointers to allow the gcc compiler optimize the code, but I have not been able to make it work so far. I am testing with this code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
void vecmult(int n, int * restrict a, int * restrict b, int * restrict c)
{
int i;
for (i=0; i<n; ++i) {
a = b * c;
}
}
int main(){
int Nsteps = 100000;
int n = 1000;
int* a=NULL;
int* b=NULL;
int* c=NULL;
//allocate memory
a = malloc(n*sizeof(int));
b = malloc(n*sizeof(int));
c = malloc(n*sizeof(int));
//initialize arrays
for(int i = 0; i < n; ++i){
a = i;
b = 1;
c = 0;
}
//initialize time
struct timeval tim;
gettimeofday(&tim, NULL);
long tcpu = clock();
for(int i = 0; i < Nsteps; ++i){
vecmult(n, a, b, c);
}
//time difference evaluation
double t1 = tim.tv_sec + tim.tv_usec / 1000000.0;
double start = (double)(tcpu);
gettimeofday(&tim, NULL);
double t2 = tim.tv_sec + tim.tv_usec / 1000000.0;
tcpu = clock();
double stop = (double)(tcpu);
double t_elap = (t2 - t1);
double t_cpu = (stop - start) / 1000000.0;
//print
printf("%f %f\n",t_elap, t_cpu);
//deallocate memory
free(a);
free(b);
free(c);
//show that restrict does not work
int l = 1;
int* restrict p1=NULL;
int* restrict p2=NULL;
p1 = &l;
p2 = p1;
return 0;
}
The gcc-4.2 compiler does not even complaints for the last part with the -O3 and -std=c99 options. Dues someone knows how to make restricted pointers work? Thanks in advance.