| Issue |
239427
|
| Summary |
[NVPTX][OpenMP] Significant performance differences between Clang and NVCC due to register pressure and load scheduling
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
ArnauCinca
|
We observed significant performance differences when compiling the [FIR](https://github.com/llnl/RAJAPerf/blob/develop/src/apps/FIR-OMPTarget.cpp) benchmark from [RAJAPerf](https://github.com/llnl/RAJAPerf/tree/develop) using Clang OpenMP target offloading, compared to equivalent CUDA implementations compiled with Clang (-x cuda) and NVCC.
We created a simplified reproducer based on the original FIR kernel, with several variants exploring how the representation of the iteration count affects loop unrolling and performance.
**Observations:**
- When the iteration count is a runtime parameter or variable, loop unrolling is limited. Using a compile-time constant (const int or macro) results in full unrolling.
- All three compilation approaches exhibit similar unrolling behavior, although OpenMP target performs slightly more unrolling in the limited-unrolling cases.
- The generated LLVM IR (where applicable) and PTX are largely similar, apart from differences related to unrolling.
- However, the final SASS shows differences in instruction scheduling. The OpenMP target version tends to issue many loads before processing their results, increasing register live ranges and pressure, and limiting the occupancy.
- The CUDA versions compiled with Clang and NVCC produce very similar code and achieve comparable performance. Their SASS interleaves loads and computations more effectively, allowing registers to be reused.
Since these differences are most apparent in SASS rather than PTX, they seem to arise during ptxas optimization and register allocation.
**Expected behavior:**
We would expect the OpenMP target implementation to achieve performance reasonably comparable to the equivalent CUDA implementations, particularly given the similarity of the generated PTX.
Could subtle differences in the PTX generated by the OpenMP target path, including the additional unrolling, be influencing ptxas scheduling and register-allocation decisions?
**Reproducers**
fir.cpp
```C++
#include <omp.h>
#ifndef FIR_COEFFLEN
#define FIR_COEFFLEN (16)
#endif
constexpr int threads_per_team = 256;
constexpr int iend = 1000000;
void fir(const double *in, double *out, const double *coeff, const int coefflen, int did )
{
const int grid_size = (iend + threads_per_team - 1) / threads_per_team;
#pragma omp target teams distribute parallel for num_teams(grid_size) \
thread_limit(threads_per_team) \
is_device_ptr(in, out, coeff) device( did )
for (int i = 0; i < iend; ++i) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
void fir_var(const double *in, double *out, const double *coeff, int did )
{
const int grid_size = (iend + threads_per_team - 1) / threads_per_team;
int coefflen = FIR_COEFFLEN;
#pragma omp target teams distribute parallel for num_teams(grid_size) \
thread_limit(threads_per_team) \
is_device_ptr(in, out, coeff) device( did )
for (int i = 0; i < iend; ++i) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
void fir_constant(const double *in, double *out, const double *coeff, int did )
{
const int grid_size = (iend + threads_per_team - 1) / threads_per_team;
const int coefflen = FIR_COEFFLEN;
#pragma omp target teams distribute parallel for num_teams(grid_size) \
thread_limit(threads_per_team) \
is_device_ptr(in, out, coeff) device( did )
for (int i = 0; i < iend; ++i) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
void fir_macro(const double *in, double *out, const double *coeff, int did )
{
const int grid_size = (iend + threads_per_team - 1) / threads_per_team;
#pragma omp target teams distribute parallel for num_teams(grid_size) \
thread_limit(threads_per_team) \
is_device_ptr(in, out, coeff) device( did )
for (int i = 0; i < iend; ++i) {
double sum = 0.0;
for (int j = 0; j < FIR_COEFFLEN; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
int main()
{
int did = omp_get_default_device();
double *in = (double *)omp_target_alloc((iend+FIR_COEFFLEN)*sizeof(double), did);
double *out = (double *)omp_target_alloc(iend*sizeof(double), did);
double *coeff = (double *)omp_target_alloc(FIR_COEFFLEN*sizeof(double), did);
if (!in || !out || !coeff) {
return -1;
}
fir(in, out, coeff, FIR_COEFFLEN, did);
fir_var(in, out, coeff, did);
fir_constant(in, out, coeff, did);
fir_macro(in, out, coeff, did);
omp_target_free( coeff, did );
omp_target_free( in, did );
omp_target_free( out, did );
}
```
fir_cuda.cu:
```cuda
#include <cuda_runtime.h>
#ifndef FIR_COEFFLEN
#define FIR_COEFFLEN (16)
#endif
constexpr int threads_per_team = 256;
constexpr int iend = 1000000;
__global__ void fir_kernel(const double *in, double *out, const double *coeff,
const int coefflen)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < iend) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
__global__ void fir_kernel_var(const double *in, double *out, const double *coeff)
{
int coefflen = FIR_COEFFLEN;
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < iend) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
__global__ void fir_kernel_constant(const double *in, double *out, const double *coeff)
{
const int coefflen = FIR_COEFFLEN;
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < iend) {
double sum = 0.0;
for (int j = 0; j < coefflen; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
__global__ void fir_kernel_macro(const double *in, double *out, const double *coeff)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < iend) {
double sum = 0.0;
for (int j = 0; j < FIR_COEFFLEN; ++j) {
sum += coeff[j] * in[i+j];
}
out[i] = sum;
}
}
int main()
{
double *in = nullptr;
double *out = nullptr;
double *coeff = nullptr;
cudaMalloc(&in, (iend + FIR_COEFFLEN) * sizeof(double));
cudaMalloc(&out, iend * sizeof(double));
cudaMalloc(&coeff, FIR_COEFFLEN * sizeof(double));
const dim3 grid_size((iend + threads_per_team - 1) / threads_per_team, 1, 1);
const dim3 block_size(threads_per_team, 1, 1);
fir_kernel<<<grid_size, block_size>>>(in, out, coeff, FIR_COEFFLEN);
cudaDeviceSynchronize();
fir_kernel_var<<<grid_size, block_size>>>(in, out, coeff);
cudaDeviceSynchronize();
fir_kernel_constant<<<grid_size, block_size>>>(in, out, coeff);
cudaDeviceSynchronize();
fir_kernel_macro<<<grid_size, block_size>>>(in, out, coeff);
cudaDeviceSynchronize();
cudaFree(coeff);
cudaFree(in);
cudaFree(out);
}
```
**Compilation commands:**
`clang++ -O3 -fopenmp --offload-arch=native fir.cpp -o fir`
`clang++ -O3 -x cuda --offload-arch=native -lcudart fir_cuda.cu -o fir_cuda_clang`
`nvcc -O3 -arch=native fir_cuda.cu -o fir_cuda_nvcc`
**Compilation environment**
- GPU: NVIDIA H100 80GB HBM3
- Clang version: 24.0.0git
- LLVM revision: 4cc8134b2e72081144f4f0338d8bf88963d1626b
- NVCC version: V13.1.115
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs