GPUs and Vectorisation

This page considers further some of the points raised by the mapping of OpenMP to GPUs.

Consider the rather bizarre function

void vadd(float *a, int off, int n){
  int i;

#pragma omp target map(tofrom:a[0:n])
#pragma omp simd
  for(i=0;i<n-off;i++)
    a[i+off]=a[i]+1;
}

and consider it being called with a a long array all of whose elements are one, and with off=1. In the absence of any vectorisation or other optimisation, this is then

  a[1]=a[0]+1;
  a[2]=a[1]+1;
  a[3]=a[2]+1;
  ...

with a[0]=1. So it returns an array of consecutive integers, starting at one.

Now consider what happens if compiled with little optimisation save for using a vector length of four.

  vreg=(a[0],a[1],a[2],a[3])
  (a[1],a[2],a[3],a[4])=vreg+(1,1,1,1)
# a is now 1 2 2 2 2 1 1 1 1 1 1 ...
  vreg=(a[4],a[5],a[6],a[7])
  (a[5],a[6],a[7],a[8])=vreg+(1,1,1,1)

  ...

Now the first four elements of the array returned will be 1,2,2,2 and all subsequent groups of four will be 2,3,2,2.

A compiler can do this only if told to vectorise with a directive such as omp simd. Otherwise, to ensure that the correct answer is given even for off=1 it cannot perform optimisations which result in reads being performed before the writes of an earlier iteration.

With gcc, no optimisation gives consecutive integers (vectorisation not performed), -fopenmp -O1 gives groups of four, and -fopenmp -O1 -mavx2 gives groups of eight.

Note that unrolling might appear to make the vector length longer than the physical hardware length, but GCC's unrolling does not have this affect.

GPU results

The above code compiled with GCC version 13

$ gcc -fopenmp -foffload=nvptx-none -fcf-protection=none -fno-stack-protector -no-pie -O1 -c vadd.c

results in the array being returned with all but the first group of 32 being 2,3,2 repeated 30 times. So the GPU appears to have a vector length of 32.

If compiled with clang (v18), then, when compiling for the host a vector length of eight appears to be used. When compiling for the GPU clang reports that it cannot vectorise the loop, and running produces consecutive integers.

As for Nvidia's compiler, it fails to vectorise this when compiling for the CPU, and goes into an infinite loop when compiling with -mp=gpu.

The dream of OpenMP producing vendor-neutral portability for GPU offloading might not be fully realised yet.