how to use OpenMP in CodeLite? - c

I want to use OpenMP in CodeLite, but it doesn't work.
I have already choose -fopenmp in compile setting(actually it's the default setting).
My program is as follows:
#include <stdio.h>
#include <omp.h>
int main()
{
#pragma omp parallel
{
printf("Hello World!");
}
return 0;
}
The result of this program shows that "#include " doesn't work, and the "#pragma omp parallel" is ignored.

Make sure the openmp support library is installed, for example, on Linux with gcc, you should install the libgomp package, for example if you are on fedora, run dnf install libgomp, then try compile again.

It is necessary to pass -fopenmp to the linker to enable OpenMP features. The following steps should help you achieve that:
Right click on the project in the workspace view.
Select "Settings" near the bottom of this pop-up menu. Settings...->Linker->Linker Options.
Click into the semicolon delimited list to reveal ellipses and click on the ellipses.
Click the checkbox for "Enable OpenMP (linkage) [-fopenmp]"
Re-compile the code - It should work!

Related

How to use runtime tracing in OpenMP LLVM?

I want to enable runtime tracing and see the output. Something like the output of
KD_TRACE(10, ( buff, gtid, schedule, chunk, lb, ub, st ) );
in kmp_dispatch.cpp
Refer this
https://elixir.bootlin.com/llvm/latest/source/openmp/runtime/src/kmp_dispatch.cpp#L624
So, far I have followed the following tutorial:
https://passlab.github.io/CSE436536/Assignments/project_dev_setup.html
But I am not able to see any output from the tracer.
Is there a particular file or something where the output is logged? Or it is logged in the terminal?
I am compiling the openMP program like this:
clang omp1.c -L/PATH/llvm_work/openmp/BUILD/runtime/src -o omp1
ldd omp1
This is the output:
linux-vdso.so.1 (0x00007ffdae305000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca2f3bb000)
/lib64/ld-linux-x86-64.so.2 (0x00007fca2f7ac000)
I hope this is using the OpenMP I have build from source and not libomp.
omp1.c:
#include<stdio.h>
#include "omp.h"
int main()
{
int i=0;
#pragma omp parallel for schedule(static)
for(i=0;i<1000;++i)
{
int x = 4+i;
}
}
But when I am trying to run this program using the same command I am getting an error.
/tmp/omp2-d969a9.o: In function `main':
omp2.c:(.text+0x1c8): undefined reference to omp_set_num_threads
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone help me with correctly compiling the openMP programs with the openMP code I have built from source and also in using tracer?
Thank you.
You need to compile with -fopenmp flag. Also, you need to have the debug version of the runtime (built with debug info) + set the environment variable export KMP_DEBUG=511.
I think you have to tell the compiler, that you want to use OpenMP via -fopenmp:
clang -fopenmp omp1.c -L/PATH/llvm_work/openmp/BUILD/runtime/src -o omp1

Why code runner extension taking too much time to execute?

I have made a very simple program to test the VS code Runner extension.
My code is
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of entered numbers = %d\n",c);
return 0;
}
After running the code and waiting for many minutes it still shows
[Running] cd "/home/souro/Desktop/" && gcc souro.c -o souro && "/home/souro/Desktop/"souro
No output is showing it only shows running.
Try using the terminal for running the code, if you're currently using the output window then it might not be working.
Go to settings, in the search bar type 'terminal' and look for' code runner:run in terminal ' make sure to check the box and you should be good to go.
go to settings search for Code-runner: run in terminal
enable it
Go to VSCode settings and search for 'terminal', then scroll to find 'Code-runner: run in terminal' and enable it.
Check your IntelliSense settings, set the compiler path and IntelliSense mode to g++ and gccx-64 (if you are using C and C++ in windows). Reinstall the extension and create a new file of .c or .cpp and then try again.
After that make sure you run your output through terminal, which you can set as follows- Go to settings>extensions>C/C++>Run code configuration>Scroll down and then check the box which says run in terminal.

Getting OpenMP running in Code::Blocks

I am trying to teach myself OpenMP using Windows 7, but I am having a hard time getting Code::Blocks to compile a basic hello world program:
#include <omp.h>
#include <stdio.h>
int main()
{
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
I have made some progress, but there is one remaining persistent error that I can't get rid of.
I have -fopenmp in my compiler "Compiler->Compiler Settings->Other Options"
I have -gomp and -pthreads in "Compiler->Linker Settings->Other linker options"
I have C:\Program File (x86)\Codeblocks\MinGW\gcc\mingw32\bin in "Compiler->Toolchain exectuable->Additional Paths"
When I compile, I get the error: "ld.exe: cannot find -lpthread"
Can someone suggest what I might have set up wrong?
Thanks!
The linker complains about a missing library. pthreads is the library that implements the threading interface that your OpenMP implementation uses to do all the threading stuff.
The library is called "libpthread.a" (static version) and "libpthread.so" (dynamic version) on the disk. Try to find these two on the file system under your MinGW directory. They likely reside in a directory called "lib" or "lib64". If either one is missing, then you might need to install an additional package.
Cheers,
-michael

printing from cuda kernels

I am writing a cuda program and trying to print something inside the cuda kernels using the printf function. But when I am compiling the program then I am getting an error
error : calling a host function("printf") from a __device__/__global__ function("agent_movement_top") is not allowed
error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2008 -ccbin "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin" -I"C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include" -G --keep-dir "Debug" -maxrregcount=0 --machine 32 --compile -g -Xcompiler "/EHsc /nologo /Od /Zi /MDd " -o "Debug\test.cu.obj" "C:\Users\umdutta\Desktop\SANKHA_ALL_MATERIALS\PROGRAMMING_FOLDER\ABM_MODELLING_2D_3D\TRY_NUM_2\test_proj_test\test_proj\test_proj\test.cu"" exited with code 2.
I am using the card GTX 560 ti having a compute capability greater than 2.0 and when I have searched a bit about the printing from cuda kernels I also saw that I need to change the compiler from sm_10 to sm_2.0 to take the full advantage of the card. Also some suggested for cuPrintf to serve the purpose. I am bit confused what should I do and what should be the simplest and quickest way to get the printouts on my console screen. If I need to change the nvcc compiler from 1.0 to 2.0 then what should I do? One more thing I would like to mention that I am using windows 7.0 and programming in visual studio 2010. Thanks for all your help.
To enable use of plain printf() on devices of Compute Capability >= 2.0, it's important to compile for CC of at least CC 2.0 and disable the default, which includes a build for CC 1.0.
Right-click the .cu file in your project, select Properties, select Configuration Properties | CUDA C/C++ | Device. Click on the Code Generation line, click the triangle, select Edit. In the Code Generation dialog box, uncheck Inherit from parent or project defaults, type compute_20,sm_20 in the top window, click OK.
you can write this code to print whatever you want from inside the CUDA Kernel:
# if __CUDA_ARCH__>=200
printf("%d \n", tid);
#endif
and include < stdio.h >
One way of solving this problem is by using cuPrintf function which is capable of printing from the kernels. Copy the files cuPrintf.cu and cuPrintf.cuh from the folder
C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\src\simplePrintf
to the project folder. Then add the header file cuPrintf.cuh to your project and add
#include "cuPrintf.cu"
to your code. Then your code should be written in a format mentioned below :
#include "cuPrintf.cu"
__global__ void testKernel(int val)
{
cuPrintf("Value is: %d\n", val);
}
int main()
{
cudaPrintfInit();
testKernel<<< 2, 3 >>>(10);
cudaPrintfDisplay(stdout, true);
cudaPrintfEnd();
return 0;
}
By following the above procedure one can get a print on the console window from the device function.
Though I solved my issues in the above mentioned way I still don't have the solution of using printf from the device function. If it is true and absolutely necessary to upgrade my nvcc compiler from sm_10 to sm_21 to enable the printf feature then it would be very much helpful if someone could show me the light. Thanks for all your cooperation
I am using GTX 1650 also GTX1050, and c++11. For recent users, this is my suggestion:
In host function:
#include<iostream>
using namespace std;
cout<< .....(anything you want) << endl;
In kernel:
if(threadIdx.x==0){
printf("ss=%4.2f \n", ss);
}
Note that this "if" is quite important and I notice nobody mentioned this. Because you might use a lot of threads and you definitely do not want to print too much from every threads. Also 4.2f means 4 points and 2 for decimal. This can prevent print too much 00000. Also do not forget \n to jump line.
Also you can consider this to print shared memory value:
if(threadIdx.x==0){
for(int i=0;i<64;i++){
for(int j=0;j<8; j++){
printf("%4.2f ", ashare[i*8+j]);
}
printf("\n");
}
printf("\n");
}
This can print shared memory beautifully. Notice also need to restrict only in threadIdx.x==0

C get compilation options inside the code

I'm writing a GUI to an application, but the main developer wants to set in Makefile if the GUI get or not compiled with the rest. I'm putting all the GTK+ code in a separated file, but in the main file I need to test if the application is being compiled with the GUI or not, so how I can test this?
E.g:
if(COMPILED_WITH_GTK)
#include "my_gtk_stuffs.h"
Assuming that COMPILED_WITH_GTK is an argument to the compiler command in the Makefile (in the form of -DCOMPILED_WITH_GTK) you use a preprocessor directive.
#ifdef COMPILED_WITH_GTK
#include "my_gtk_stuffs.h"
#endif
This tells the preprocessor to only process the #include statement if COMPILED_WITH_GTK is defined.
Have a look here, as well.

Resources