vectorization of array maximum

Michael J. Hennebry hennebry at plains.NoDak.edu
Sun Oct 7 02:22:35 AEST 1990


Try something like this:

float
findmax(a, size) float a[]; int size; {

int j, inc, inc2;

for(inc=1, inc2=2; inc<size; inc=inc2, inc2<<=1) {
   for(j=0; j<size; j+=inc2) {
      if(a[j] < a[j+inc]) a[j]=a[j+inc];
   }
}

return a[0];
}

this works if size is a power of two and you don't mind destroying a.
whether it is efficient depends on whether the compiler can
tell that the inner loop is vectorizable. This depends on inc, inc2 > 0

another possibility is

float 
findmax(a, work, size) float[], work[]; int size; {

int j, inc, inc2;
float *to, *from, *temp;

from=a; to=work;

for(inc=1, inc2=2; inc<size; inc=inc2, inc2<<=1, temp=from, from=to, to=temp) {
   for(j=0; j<size; j+=inc2) {
      if(from[j] < from[j+inc]) to[j]=from[j+inc];
      else                      to[j]=from[j];
   }
}
return from[0];
}

Having two 'arrays' may cause the compiler to infer that the inner
loop is vectorizable.

-- 
Mike    hennebry at plains.NoDak.edu
"Megalomania is such a *wonderful* illness." -- Asmodeus Mogart



More information about the Comp.unix.cray mailing list