I am trying to offload code the GPU using OpenMP 4+ directives. I am using ubuntu 16.04 with GCC 7.2 and for general cases it is working fine. My problem comes when I am trying to offload a code that has a call to the sqrtf function that is defined in "math.h". The troubeling code is this:
#pragma omp target teams distribute \
map(to:posx[:n],posy[:n],posz[:n]) \
map(from:frcx[:n],frcy[:n],frcz[:n])
for (int i = 0; i < n; i++) {
frcx[i] = 0.0f;
frcy[i] = 0.0f;
frcz[i] = 0.0f;
for (int j = 0; j < n; j++) {
float dx = posx[j] - posx[i];
float dy = posy[j] - posy[i];
float dz = posz[j] - posz[i];
float distSqr = dx*dx + dy*dy + dz*dz + SOFTENING;
float invDist = 1.0f / sqrtf(distSqr);
float invDist3 = invDist * invDist * invDist;
frcx[i] += dx * invDist3;
frcy[i] += dy * invDist3;
frcz[i] += dz * invDist3;
}
}
When I try to compile it with:
$ gcc -Wall -O2 -march=native -mtune=native -fopenmp -o nbody_cpu_arrays_parallel_gpu common_funcs.c nbody_cpu_arrays_parallel_gpu.c -lm
unresolved symbol sqrtf
collect2: error: ld returned 1 exit status
mkoffload: fatal error: x86_64-linux-gnu-accel-nvptx-none-gcc-7 returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: /usr/lib/gcc/x86_64-linux-gnu/7//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
How can I make use of square root operations (or other mathematical functions) when offloading OMP code to GPUs?
I encountered a similar issue. https://github.com/bisqwit/cpp_parallelization_examples/blob/master/README.md very helpfully describes the solution:
When offloading, you may get linker problems from math functions if
you do an optimized build. To resolve, add -foffload=-lm
-fno-fast-math -fno-associative-math
For reference, the errors I got with sqrt:
libgomp: Link error log ptxas application ptx input, line 138; error : Label expected for argument 0 of instruction 'call'
ptxas application ptx input, line 138; fatal : Call target not recognized
ptxas <macro util>, line 9; error : Illegal modifier '.div' for instruction 'mov'
ptxas fatal : Ptx assembly aborted due to errors
libgomp: cuLinkAddData (ptx_code) error: a PTX JIT compilation failed
libgomp: Cannot map target functions or variables (expected 2, have 4294967295)
And with sqrtf:
unresolved symbol sqrtf
collect2: error: ld returned 1 exit status
mkoffload: fatal error: x86_64-pc-linux-gnu-accel-nvptx-none-gcc returned 1 exit status
compilation terminated.
lto-wrapper: fatal error: gcc/x86_64-pc-linux-gnu/7.3.0//accel/nvptx-none/mkoffload returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
clang 9.0 now has the feature that replace the standard math library function with equivelant version of ptx code (nvidia gpu ), which is not yet supported by gcc 9.0.
Compile and run: https://www.hahnjo.de/blog/2018/10/08/clang-7.0-openmp-offloading-nvidia.html
commit of clang : https://reviews.llvm.org/D61399
Related
I have already taken a look at the question :Undefined reference to `pow' and `floor' but I'd like to know if there is anyway I could fix this permanently in my IDE which is Jet brains C Lion. I am using it on Pop OS. And I am very new to C as well as programming in general, it'd be better if I could just build and compile the program from my IDE rather than having to go to terminal every time.
Edit #1 : This is the code I am trying to compile rn. Its trying to find the reverse of a number rn.
#include <stdio.h>
#include <math.h>
int how_long(int x)
{
int count = 0;
while (x>0)
{
x /= 10;
count++;
}
return count;
}
int main()
{
int n = 1;
int num ;
int reverse_num = 0;
printf("Enter a number : ");
scanf("%i",&num);
int j = how_long(num);
while (num>0)
{
reverse_num += (num % 10) * (int)pow(10,j) ;
num -= (num%10) * (int)pow(10,n-1);
++n;
--j;
}
printf("%i",reverse_num);
return 0;
}
And the error I get on C Lion is :
[1/1] Linking C executable 5_2
FAILED: 5_2
: && /usr/bin/cc -g CMakeFiles/5_2.dir/main.c.o -o 5_2 && :
/usr/bin/ld: CMakeFiles/5_2.dir/main.c.o: in function `main':
/home/koustubhjain/CLionProjects/Assignments/5-2/main.c:30: undefined reference to `pow'
/usr/bin/ld: /home/koustubhjain/CLionProjects/Assignments/5-2/main.c:31: undefined reference to `pow'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Edit #2 :
I found this old forum post on C Lion's website : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206607085-CLion-Enabling-math-h-for-C-projects
but idk if it still works, and if it works, how do I add that to my CMake file ?
Here is my CMakeLists.txt file for reference :
cmake_minimum_required(VERSION 3.21)
project(5_2 C)
set(CMAKE_C_STANDARD 99)
add_executable(5_2 main.c)
Edit #3 : Edit #2 seems to have fixed it
I get following errors when trying compile using MPLAB X on Windows..
newmain.c:40:9: error: unknown configuration setting: 'JTAGEN'
newmain.c:61:2: error: 'LATA' undeclared (first use in this function)
newmain.c:62:2: error: 'TRISA' undeclared (first use in this function)
newmain.c:61:2: error: 'LATA' undeclared (first use in this function)
I tried this on 2 different machines (on Windows 8.1 and Vista), but they all gave me same error.
It seemed like that xc.h file is already included to project since I can open that file. And also I googled for this problem but there wasn't a solution for this.
Thank you very much if you can give me a possible way to solve this.
I also attached photo describing project properties for this project.
Following is full description of error :
- CLEAN SUCCESSFUL (total time: 52ms) make -f
nbproject/Makefile-default.mk SUBPROJECTS= .build-conf make[1]:
Entering directory 'Z:/Personal Data/MPLABXProjects/Lab01.X' make -f
nbproject/Makefile-default.mk
dist/default/production/Lab01.X.production.hex make[2]: Entering
directory 'Z:/Personal Data/MPLABXProjects/Lab01.X' "Z:\Program Files
(x86)\Microchip\xc32\v1.40\bin\xc32-gcc.exe" -g -x c -c
-mprocessor=32MX340F512H -MMD -MF build/default/production/newmain.o.d -o
build/default/production/newmain.o newmain.c newmain.c:40:9: error:
unknown configuration setting: 'JTAGEN' #pragma config JTAGEN = OFF
// JTAG Enable OFF (only use for '250)
nbproject/Makefile-default.mk:105: recipe for target
'build/default/production/newmain.o' failed ^ newmain.c: In function
'main': make[2]: Leaving directory 'Z:/Personal
Data/MPLABXProjects/Lab01.X' newmain.c:61:2: error: 'LATA' undeclared
(first use in this function) nbproject/Makefile-default.mk:78: recipe
for target '.build-conf' failed LATA = 0; // Set value of PORT A
output to 0. ^ make[1]: Leaving directory 'Z:/Personal
Data/MPLABXProjects/Lab01.X' nbproject/Makefile-impl.mk:39: recipe
for target '.build-impl' failed newmain.c:61:2: note: each undeclared
identifier is reported only once for each function it appears in
newmain.c:62:2: error: 'TRISA' undeclared (first use in this
function) TRISA = 0; // Set all pins on PORT A to output ^ make[2]:
*** [build/default/production/newmain.o] Error 1 make[1]: *** [.build-conf] Error 2 make: *** [.build-impl] Error 2 BUILD FAILED
(exit value 2, total time: 203ms)
And source is
#include <xc.h>
#pragma config FWDTEN = OFF, JTAGEN = OFF
void delay(void);
unsigned int ctr = 0;
unsigned int delayVal = 2048;
int main(void)
{
LATA = 0;
TRISA = 0xFF00;
while(1)
{
LATA = 0x0055;
delay();
LATA = 0x00AA;
delay();
ctr++;
}
}
void delay(void)
{
unsigned int i,j;
for (i = 0; i < delayVal; i++)
{
for (j = 0; j < 20; j++);
}
}
The errors you receive indicate that the compiler does not see any libraries to reference those registers to. It treats them as regular variables instead.
You should include the specific header file for the processor you are using and also check to see what registers are structured in there to make more sense of your problem.
I'm trying to build a basic FANN (Fast Artificial Neural Network) project on Windows with MinGW. However, whenever I try to link the executable, I run into a bunch of undefined reference to errors. Interestingly, if I don't link the library at all, I get more errors, implying that at least some of the library is working. The code for the file I'm trying to compile and link is:
#include "doublefann.h"
int main() {
const unsigned int num_input_neurons = 9;
const unsigned int num_output_neurons = 1;
const unsigned int num_layers = 3;
const unsigned int num_hidden_neurons = 9;
const float desired_error = (const float) 0;
const unsigned int max_epochs = 500000;
const unsigned int epochs_between_reports = 1000;
struct fann *ann = fann_create_standard(num_layers,
num_input_neurons,
num_hidden_neurons,
num_output_neurons);
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_train_on_file(ann,
"titanic-training.data",
max_epochs,
epochs_between_reports,
desired_error);
fann_save(ann, "titanic.net");
fann_destroy(ann);
return 0;
}
and the command I'm using to compile and link is:
gcc -Wall -Ifann\src\include titanic-train.c -Lfann\bin -lfanndouble -o titanic-train.exe
The errors I'm getting back are:
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
If I don't link the library at all, I instead get:
C:\Users\kunkelwe\AppData\Local\Temp\ccyOO3jL.o:titanic-train.c:(.text+0x67): undefined reference to `fann_create_standard'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Edit:
As per Haroogan's request, I ran nm fanndouble.lib. The output is rather extensive, so rather than paste it all here, I've made it available via pastebin here: http://pastebin.com/raw.php?i=vybFhEcX
I'm not familiar with nm, but it appears that the missing symbols do exist in the file.
Edit #2:
The contents of doublefann.h are: http://pastebin.com/mrHKJi8C
and the contents of fann.h, which it includes are: http://pastebin.com/gTrHCYAg
Could the problem just be solved by recompiling the library with MinGW?
Edit #3:
Making the changes that Haroogan suggested worked! In addition to those changes, I had to modify the CMakeLists.txt file for FANN by adding:
if (WIN32)
ADD_DEFINITIONS(-DFANN_DLL_EXPORTS)
endif (WIN32)
Then, running cmake -G "MinGW Makefiles" and then mingw32-make in the root of the project produced a file, libdoublefann.dll, that when linked against and included in the directory of the .exe, allowed me, finally, to run my program.
In doublefann.h on the line #116:
#if (_MSC_VER > 1300)
change to:
#if (_MSC_VER > 1300) || defined(__MINGW32__) || defined(__MINGW64__)
Furthermore, on the line #121:
#if defined(_MSC_VER) && (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))
change to:
#if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)) && \
(defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))
I'm new to programming and a bit confused with arrays, what seems to be wrong in this code, as eclipse output console is saying ** Build of configuration Debug for project Project **
Internal Builder is used for build **
gcc -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.c
gcc -oProject.exe main.o
C:...\Documents\eclipse\mingw\bin..\lib\gcc\mingw32\3.4.5........\mingw32\bin\ld.exe: cannot open output file Project.exe: Permission denied
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 472 ms.
I will really appreciate your help...
int main()
{
int box[2][2], rows, cols, x = 1;
for (rows=0; rows < 2; rows++)
{
for (cols=0; cols < 2; cols++)
{
box[rows][cols] = x++;
printf("%d", box[rows][cols]);
}
}
fflush(stdout);
getch();
return 0;
}
x++ is post increment so the value of x is used and then incremented and hence box[0][0] is 1
I am trying to write a program to approximate pi. It basically takes random points between 0.00 and 1.00 and compares them to the bound of a circle, and the ratio of points inside the circle to total points should approach pi (A very quick explanation, the specification goes in depth much more).
However, I am getting the following error when compiling with gcc:
Undefined first referenced
symbol in file
pow /var/tmp//cc6gSbfE.o
ld: fatal: symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
What is happening with this? I've never seen this error before, and I don't know why it's coming up. Here is my code (though I haven't fully tested it since I can't get past the error):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
float x, y;
float coordSquared;
float coordRoot;
float ratio;
int n;
int count;
int i;
printf("Enter number of points: ");
scanf("%d", &n);
srand(time(0));
for (i = 0; i < n; i++) {
x = rand();
y = rand();
coordSquared = pow(x, 2) + pow(y, 2);
coordRoot = pow(coordSquared, 0.5);
if ((x < coordRoot) && (y < coordRoot)) {
count++;
}
}
ratio = count / n;
ratio = ratio * 4;
printf("Pi is approximately %f", ratio);
return 0;
}
use -lm during compilation(or linking) to include math library.
Like this: gcc yourFile.c -o yourfile -lm
need to Link with -lm.
gcc test.c -o test -lm
The error is produced by the linker, ld. It is telling you that the symbol pow cannot be found (is undefined in all the object files handled by the linker). The solution is to include the library which includes the implementation of the pow() function, libm (m for math). [1] Add the -lm switch to your compiler command line invocation (after all the source file specifications) to do so, e.g.
gcc -o a.out source.c -lm
[1] Alternatively, you could have your own implementation of pow() in a separate translation unit or a library, but you would still have to tell the compiler/linker where to find it.