GCC fails to compile OpenMP offloading to GPU - c++

I'm using GCC 9.3 on Ubuntu 20.04. I want to offload the famous SAXPY example to GPU using OpenMP. I installed GCC's offloading capabilities by sudo apt install gcc-9-offload-nvptx . Then compiled the following code by g++ -fopenmp main.cpp:
int main()
{
const size_t kNumel = 999999;
float x[kNumel];
float y[kNumel];
for (size_t i=0 ;i <kNumel; i++)
{
x[i] = i;
y[i] = i;
}
const float kCoef = 1.23f;
#pragma omp target teams distribute parallel for
for (size_t i=0; i < kNumel; i++)
{
y[i] = kCoef*x[i] + y[i];
}
return 0;
}
BUT it doesn't compile and shows this error:
to1: error: ‘-fcf-protection=full’ is not supported for this target
mkoffload: fatal error: x86_64-linux-gnu-accel-nvptx-none-gcc-9 returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: /usr/lib/gcc/x86_64-linux-gnu/9//accel/nvptx-none/mkoffload
returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
I added -fno-stack-protector but the same error is reproduced.

Related

undefined reference to `__gcov_dump()

I try to test the __gcov_dump function via a simple example like this:
tmp.c:
#include <stdio.h>
#include <gcov.h>
int main()
{
int i, total;
total = 0;
for (i = 0; i < 10; i++)
total += i;
if (total != 45)
printf ("Failure\n");
else
printf ("Success\n");
__gcov_dump();
}
~/Downloads/gcovtest ❯ g++ --coverage -lgcov tmp.c
/tmp/user/ccdEPxlm.o: In function main': tmp.c:(.text+0xa3): undefined reference to __gcov_dump()'
collect2: error: ld returned 1 exit status
Leaving out -lgcov makes not difference.
It's an Ubuntu 18.04 LTE with a
g++ (Ubuntu 9.4.0-1ubuntu1~18.04.1sav0) 9.4.0
/usr/lib/gcc/x86_64-linux-gnu/9/libgcov.a is present and the program tries to open it as strace says.
Any ideas what could be wrong?
Regards
Ok, it seems I cannot use the standard gcov.h header when using g++. I have to declare the method manually with c linkage:
extern "C" void __gcov_dump();

Error when compiling MATIO library: "Undefined reference to 'Mat_Open'"

so after building and installing the MATIO library using CMake, I am now having problems compiling it. Here's my code:
#include <iostream>
#include <matio.h>
#define tS(x) std::cout<<"\t"<<(#x)<<" == "<<(x)<<"\n"
int main(int argc, char **argv)
{
const char *fileName = argc==1?"./S.mat":argv[1] ;
mat_t *mat = Mat_Open(fileName,MAT_ACC_RDONLY);
if(mat)
{
std::cout<<"A file was opened for reading\n\tmat == "<<mat<<"\n" ;
matvar_t *matVar=0 ;
std::cout<<"Writing out the data\n\n" ;
std::cout<<"x:\n" ;
matVar = Mat_VarRead(mat, (char*)"x") ;
if(matVar)
{
int xSize = matVar->nbytes/matVar->data_size ;
const double *xData = static_cast<const double*>(matVar->data) ;
for(int i=0; i<xSize; ++i)
{
std::cout<<"\tx["<<i<<"] = "<<xData[i]<<"\n" ;
}
std::cout<<"\n" ;
for(int i=0; i<matVar->rank; ++i)
{
std::cout<<"\tdim["<<i<<"] == "<<matVar->dims[i]<<"\n" ;
}
}
std::cout<<"y:\n" ;
matVar = Mat_VarRead(mat, (char*)"y") ;
int ySize = matVar->nbytes/matVar->data_size ;
const double *yData = static_cast<const double*>(matVar->data) ;
for(int i=0; i<ySize; ++i)
{
double d = yData[i] ;
std::cout<<"\ty["<<i<<"] = "<<d<<"\n" ;
}
std::cout<<"\n" ;
for(int i=0; i<matVar->rank; ++i)
{
std::cout<<"\tdim["<<i<<"] == "<<matVar->dims[i]<<"\n" ;
}
Mat_Close(mat);
}
else
{
std::cout<<"File cannot be opened\n" ;
return 1;
}
return 0;
}
However I get the following error:
C:\WINDOWS\system32\cmd.exe /C ""C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/mingw32-make.exe" -j4 SHELL=cmd.exe -e -f Makefile"
"----------Building project:[ Test - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test'
"C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe" -c "C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp" -std=c++14 -Wall -g -O0 -Wall -I/Users/RS3/matio/getopt -I/matio/include -L/matio/bin -lmatio -o ./Debug/main.cpp.o -I. -I. -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\eigen-3.3.9" -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\Halton" -I"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\quasimvnrnd"
"C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe" -o ./Debug/Test #"Test.txt" -L. -L"C:\Users\RS3\Desktop\SCC HiWi\Code\C++\myGmm\UQ\eigen-3.3.9" -static-libgcc -static-libstdc++
./Debug/main.cpp.o: In function `main':
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:38: undefined reference to `Mat_Open'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:46: undefined reference to `Mat_VarRead'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:63: undefined reference to `Mat_VarRead'
C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test/main.cpp:77: undefined reference to `Mat_Close'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[1]: *** [Test.mk:81: Debug/Test] Error 1
mingw32-make.exe[1]: Leaving directory 'C:/Users/RS3/Desktop/SCC HiWi/Code/C++/myGmm/UQ/Test'
mingw32-make.exe: *** [Makefile:5: All] Error 2
====4 errors, 0 warnings====
I looked for this error right and left and I know the linker can't find the library for some reason despite me having the following compiler options:
-g;-O0;-Wall;-I/Users/RS3/matio/getopt;-I/matio/include;-L/matio/bin;-lmatio
I'm using Codelite IDE.
I am not sure what I am missing and it's driving me crazy. Any help would be appreciated!

GCC OpenMP target ptxas error "Label expected for argument 0 of instruction 'call'" when using Eigen

I am trying to rewrite an algorithm parallelized with OpenMP to try out the target accel. device capabilities of OMP. I stumbled over the following problem (see example) when trying to use Eigen (3.4 rc1) within the OMP construct:
Minimal Example
#include <iostream>
#include <Eigen/Eigen>
#include <cmath>
using Eigen::MatrixXd;
int main() {
int n = 100000000;
double total = 0;
MatrixXd m(1,1);
m(0,0) = 1;
#pragma omp target teams distribute\
parallel for map(tofrom: total) map(to: n, m) reduction(+:total)
for (int i = 0; i < n; ++i) {
total +=m(0,0)* exp(sin(M_PI * (double) i/12345.6789));
}
std::cout << "total is " << total << '\n';
}
Compiling with (both gcc 9.3 and 10.2 tested, nvptx-none target, CUDA 10.1.243)
g++ -I ./eigen-3.4-rc1 -fopenmp -fcf-protection=none -fno-stack-protector -foffload=nvptx-none='--verbose -lm' eigen-total-omp.cxx -lm
Obvious Error (for full verbose output, see below)
ptxas /tmp/ccnYX8aR.o, line 262; error : Label expected for argument 0 of instruction 'call'
ptxas /tmp/ccnYX8aR.o, line 262; fatal : Call target not recognized
I haven't found explicit confirmation that Eigen works with the OMP target directive, however, it apparently should work with "normal" CUDA.
The error is not very helpful (or at least I cannot gleam insight from it), but moving the init. of the matrix object into the for loop produces one additional error:
ptxas /tmp/ccl3LNcx.o, line 277; error : Label expected for argument 0 of instruction 'call'
ptxas /tmp/ccl3LNcx.o, line 277; error : Function '_ZN5Eigen6MatrixIdLin1ELin1ELi0ELin1ELin1EEC1IiiEERKT_RKT0_' not declared in this scope
ptxas /tmp/ccl3LNcx.o, line 277; fatal : Call target not recognized
So my guess is that somehow the library is not seen within the loop by the compilers for the target device? Neither adding the Eigen path to -foffload nor (one of the only things I stumbled across) adding the -fno-exceptions flag changed anything.
Thank you for your time!
Full (verbose) error output
Using built-in specs.
COLLECT_GCC=x86_64-linux-gnu-accel-nvptx-none-gcc-9
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/accel/nvptx-none/lto-wrapper
Target: nvptx-none
Configured with: ../src/configure --prefix=/usr --libexecdir=/usr/lib --with-gcc-major-version-only --disable-bootstrap --disable-sjlj-exceptions --enable-newlib-io-long-long --target nvptx-none --enable-as-accelerator-for=x86_64-linux-gnu --enable-languages=c,c++,fortran,lto --enable-checking=release --with-system-zlib --without-isl --program-prefix=nvptx-none- --program-suffix=-9
Thread model: single
gcc version 9.3.0 (GCC)
COLLECT_GCC_OPTIONS='-m64' '-mgomp' '-fno-openacc' '-fPIC' '-foffload-abi=lp64' '-fopenmp' '-fcf-protection=none' '-v' '-v' '-o' '/tmp/ccDYQmTY.mkoffload'
/usr/lib/gcc/x86_64-linux-gnu/9/accel/nvptx-none/lto1 -quiet -dumpbase ccYsZMdw.o -m64 -mgomp -auxbase ccYsZMdw -version -fno-openacc -fPIC -foffload-abi=lp64 -fopenmp -fcf-protection=none #/tmp/ccllzqV5 -o /tmp/ccpHq0W6.s
GNU GIMPLE (GCC) version 9.3.0 (nvptx-none)
compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU GIMPLE (GCC) version 9.3.0 (nvptx-none)
compiled by GNU C version 9.3.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version none
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-m64' '-mgomp' '-fno-openacc' '-fPIC' '-foffload-abi=lp64' '-fopenmp' '-fcf-protection=none' '-v' '-v' '-o' '/tmp/ccDYQmTY.mkoffload'
/usr/lib/gcc/x86_64-linux-gnu/9/accel/nvptx-none/as -o /tmp/ccRXK6K4.o /tmp/ccpHq0W6.s
ptxas /tmp/ccRXK6K4.o, line 264; error : Label expected for argument 0 of instruction 'call'
ptxas /tmp/ccRXK6K4.o, line 264; fatal : Call target not recognized
ptxas fatal : Ptx assembly aborted due to errors
nvptx-as: ptxas returned 255 exit status
mkoffload: fatal error: x86_64-linux-gnu-accel-nvptx-none-gcc-9 returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: /usr/lib/gcc/x86_64-linux-gnu/9//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
Update 1: Accessing the data from a pointer throws an libgomp: cuCtxSynchronize error: an illegal memory access was encountered error
int main() {
int n = 10;
double total = 0;
MatrixXd m(1,1);
m(0,0) = 1;
double* array = m.data();
std::cout << "array: " << array[0] <<std::endl; //this works
#pragma omp target teams distribute\
parallel for map(tofrom: total) map(to: n, array) reduction(+:total)
for (int i = 0; i < n; ++i) {
total +=array[0]; //this trows an error
}
std::cout << "total is " << total << '\n';
}

"relocation R_X86_64_32 against xxx can not be used when making a shared object" error

I am using the <bcm2835.h> to connect on the GPIO of my Raspberry Pi. First I implemented a program in C and work perfectly. Now I am translating the same program to C++ and I am getting the error below when I try to start the <bcm2835.h>. My Eclipse IDE says /usr/local/lib/libbcm2835.a(bcm2835.o): relocation R_X86_64_32 against.rodata.str1.8' can not be used when making a shared object; recompile with -fPIConly when I use the functionbcm2835_init()`. I am already using -fPIC to generate the shared library.
class HCSR04r {
private:
int trigger;
int echo;
public:
HCSR04();
HCSR04(int trigger, int echo);
~HCSR04();
uint64_t cyclePulse(int trigger, int echo);
float distanceCentimeters();
};
#endif
#include <iostream>
#include <bcm2835.h>
#include "sensor/HCSR04.h"
HCSR04::HCSR04() {
this->echo = RPI_V2_GPIO_P1_13;
this->trigger = RPI_V2_GPIO_P1_15;
}
HCSR04::HCSR04(int trigger, int echo) {
this->echo = echo;
this->trigger = trigger;
}
HCSR04::~HCSR04() {
}
float HCSR04::distanceCentimeters() {
return (float) cyclePulse(trigger, echo) / 55.5;
}
uint64_t HCSR04::cyclePulse(int trigger, int echo) {
if (!bcm2835_init()) // THE ERROR IS BECAUSE THIS LINE
return 1;
uint64_t width;
//Close the bcm2835 bridge
bcm2835_close();
return width;
}
Now the error...
Building target: ../../../lib/libCommunicationLib.so
Invoking: GCC C++ Linker
g++ -shared -o "../../../lib/libCommunicationLib.so" ./code/utils/Metric.o ./code/sensor/GPS.o ./code/sensor/HCSR04.o ./code/sensor/ISensor.o ./code/CommunicationLib.o -lpthread -lbcm2835 -lgps
/usr/bin/x86_64-linux-gnu-ld: //usr/local/lib/libbcm2835.a(bcm2835.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
//usr/local/lib/libbcm2835.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [../../../lib/libCommunicationLib.so] Error 1
makefile:47: recipe for target '../../../lib/libCommunicationLib.so' failed

Another "undefined reference" error when linking boost libraries

I've seen several other posts that deal with this exact same issue. However, none of their solutions seem to work for me. I am compiling the following code:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/timer/timer.hpp>
using namespace boost::numeric::ublas;
int main(){
matrix<double> mat1 (3,3);
matrix<double> mat2 (3,3);
matrix<double> mat3 (3,3);
unsigned k=0;
for(unsigned i = 0; i < mat1.size1(); ++i){
for(unsigned j = 0; j < mat1.size2(); ++j){
mat1(i,j) = k;
mat2(i,j) = 2*k++;
}
}
k=0;
if(1){
boost::timer::auto_cpu_timer t;
while(k<1000){
mat3 = prod(mat1,mat2);
k++;
}
}
return 0;
}
I am compiling from the command line using:
$ g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer
and receiving the following error:
usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_timer.so:
undefined reference to `boost::chrono::steady_clock::now()'
collect2: error: ld returned 1 exit status
If I add -lboost_chrono when I compile, I get this error:
/usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_chrono.so:
undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
I can trace clock_gettime to sys/time.h. Unfortunately, I cannot find a corresponding .so file to link to. What am I missing here?
You must add -lrt to your link libraries
g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer -lboost_chrono -lrt
Update (2016-08-31)
This still seems to be an issue. When you lookup man clock_gettime, this leads to the solution (-lrt), but it also says
Link with -lrt (only for glibc versions before 2.17).
So when your glibc is newer, your problem might be something else.
Add -lrt to your g++ invocation – clock_gettime is in librt.so.