Use accelmath.h with pgc++

Hi,

I was naively thinking that it’d be fine if I just simply replaced by <accelmath.h> for the acc code that used some (common and simple) math functions. For the following code sample, pgc++ complained that identifier “floorf” was not defined.

/*
$ pgcc --version

pgcc 19.4-0 LLVM 64-bit target on x86-64 Linux -tp penryn 
PGI Compilers and Tools
Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.

pgc++ --version

pgc++ 19.4-0 LLVM 64-bit target on x86-64 Linux -tp penryn 
PGI Compilers and Tools
Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.

pgcc -c -acc -ta:tesla eg.c
pgc++ -c -acc -ta:tesla eg.cc
*/

#include <accelmath.h>

#pragma acc routine seq
float f1(float x) {
  float y;
  y = ceilf(x);
  return y;
}

#pragma acc routine seq
float f2(float x) {
  float y;
  y = floorf(x);
  return y;
}

#pragma acc routine seq
float f3(float x) {
  float y;
  y = sqrtf(x);
  return y;
}

I had suspected that this was a C++11 compatibility issue because float std::floorf(float) and float std::ceilf(float) only became available since C++11 (ref. https://en.cppreference.com/w/cpp/numeric/math/ceil and https://en.cppreference.com/w/cpp/numeric/math/floor), but the source code (/opt/pgi/linux86-64-llvm/2019/include/accelmath.h) only safeguards “extern float floorf(float)” within #ifndef __cplusplus, which eventually confused me.

Apparently I cannot convert the whole project to C… What should I do to get around it?

Thanks.

stw

Hi stw,

The error looks to be coming from the host-side code generation. By default both a device and host version of the subroutine will be created. The solution is to include both “cmath” and “accelmath.h”:

% cat eg1.cc

#include <cmath>
#ifdef _OPENACC
#include <accelmath.h>
#endif

#pragma acc routine seq
float f1(float x) {
  float y;
  y = ceilf(x);
  return y;
}

#pragma acc routine seq
float f2(float x) {
  float y;
  y = floorf(x);
  return y;
}

#pragma acc routine seq
float f3(float x) {
  float y;
  y = sqrtf(x);
  return y;
}
% pgc++ eg1.cc -c -ta=tesla -Minfo -V19.4
f1(float):
      8, Generating acc routine seq
         Generating Tesla code
f2(float):
     15, Generating acc routine seq
         Generating Tesla code
f3(float):
     22, Generating acc routine seq
         Generating Tesla code

Hope this helps,
Mat