Author Topic: Compiler plugin for nvcc compiler?  (Read 15474 times)

Offline Mark Knecht

  • Single posting newcomer
  • *
  • Posts: 5
Compiler plugin for nvcc compiler?
« on: February 07, 2011, 07:05:11 pm »
Hi,
   First post here. I'm a complete newbie programmer, an old retired guy just looking to keep my brain alive. I got interested in CUDA & C++ to solve some problems. I run Gentoo Linux, wrote some simple programs using vi and then compiling by hand using nvcc. The code has gotten much larger and now I'm looking for an IDE that I'd feel comfortable using day to day. Code::Blocks looks quite nice.

   My _immediate_ problems to solve (I currently think I have 2) are pretty simple, I think.

1) Have C::B understand that *.cu is a CUDA C++ source file
2) Have *.cu files be compiled using nvcc

There are other things, like CUDA lexicon coloring, etc., but I'm not looking for that right now. I just need to get through the program/compile/execute loop a few times with C::B to see if I like it.

PROBLEMS:

1) If I add a new *.cu file to a project Code::Blocks seems place it under 'Other', not 'Sources'. Can I change this so that the program understands *.cu file are source? If I let C::B call it a *.cpp file can I rename it *.cu?


2) From the C::B FAQ:
Quote
Q: What compiler can I use with Code::Blocks?

A: Code::Blocks philosophy is to be able to use any compiler on earth! Well, almost.

As a matter of fact it largely depends on the used compiler plugin. Some provided with the default Code::Blocks installation are GNU GCC (MinGW/Cygwin), MS Visual C++ Free Toolkit 2003, Borland's C++ Compiler 5.5, DigitalMars Free Compiler., OpenWatcom, Small Device C Compiler (SDCC) and others.

OK, my nvcc compiler isn't in the list right now as it's not part of the standard compiler plugin? Can it be added? Is that difficult? Can I do that myself, or does it require a developer to add it. There are required compiler options (for instance: -arch) so those would need to be supported.

Or, could/should I address this problem using a custom Makefile which it appears C::B supports already.

I'm really trying to see how I can fit this sort of programming into the way Code::Blocks works. It's a nice IDE and I'm sure I'd be more efficient using it, but at a minimum it needs to help me accomplish the two items above.

Thanks,
Mark

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Mark Knecht

  • Single posting newcomer
  • *
  • Posts: 5
Re: Compiler plugin for nvcc compiler?
« Reply #2 on: February 07, 2011, 09:09:50 pm »
Actually, that's quite exciting. I'm not there yet but I think I'm one step further.

OK, I build a simple CUDA example by hand using this Makefile:

Code
all: julia_cpu julia_gpu

clean:
        rm -f julia_cpu julia_gpu *.linkinfo

GSL_LIBS = $(shell gsl-config --libs)

julia_cpu: julia_cpu.cu
        nvcc -lglut --use_fast_math $+ -o $@ $(GSL_LIBS)

julia_gpu: julia_gpu.cu
        nvcc -lglut --use_fast_math $+ -o $@ $(GSL_LIBS)


which when run shows this being executed:

mark@c2stable ~/CODE/code/chapter04 $ make
nvcc -lglut --use_fast_math julia_cpu.cu -o julia_cpu -L/usr/lib64 -lgsl -lgslcblas -lm
nvcc -lglut --use_fast_math julia_gpu.cu -o julia_gpu -L/usr/lib64 -lgsl -lgslcblas -lm

I found the Advanced Compiler & Debugger stuff and made a *.cu extension , and then added the following:

nvcc -arch sm_20 --use_fast_math $includes -c $file -o $object

which seems to work correctly.

Compiling: main.cu
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
 
An object file, main.o, is generated. Big step forward! Thanks!!!!

OK, so now I need to add the stuff that gets linked, which is (I think) this stuff from the command line:

-lglut
-L/usr/lib64
-lgsl
-lgslcblas
-lm

All of these seem to be located in /usr/lib64/. I tried adding them to Project -> Build Options -> Link Settings but I haven't gotten it right. I have these on the left

glut
gsl
gslcblas
m

and on the right I have

-L/usr/lib64

but it doesn't link. Here's the Build log:

-------------- Build: Debug in julia_gpu ---------------

Compiling: main.cu
Linking console executable: bin/Debug/julia_gpu
obj/Debug/main.o: In function `HandleError(cudaError, char const*, int)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1e): undefined reference to `cudaGetErrorString'
obj/Debug/main.o: In function `start_thread(void* (*)(void*), void*)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x198): undefined reference to `pthread_create'
obj/Debug/main.o: In function `end_thread(unsigned long)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1bb): undefined reference to `pthread_join'
obj/Debug/main.o: In function `destroy_thread(unsigned long)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1d5): undefined reference to `pthread_cancel'
obj/Debug/main.o: In function `main':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x27f): undefined reference to `cudaMalloc'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x2f2): undefined reference to `cudaConfigureCall'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x334): undefined reference to `cudaMemcpy'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x351): undefined reference to `cudaFree'
obj/Debug/main.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x3c1): undefined reference to `__cudaUnregisterFatBinary'
obj/Debug/main.o: In function `__device_stub__Z14float_to_colorPhPKf(unsigned char*, float const*)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x14cf): undefined reference to `cudaSetupArgument'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x14f4): undefined reference to `cudaSetupArgument'
obj/Debug/main.o: In function `__device_stub__Z14float_to_colorP6uchar4PKf(uchar4*, float const*)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1573): undefined reference to `cudaSetupArgument'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1598): undefined reference to `cudaSetupArgument'
obj/Debug/main.o: In function `__device_stub__Z6kernelPh(unsigned char*)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1613): undefined reference to `cudaSetupArgument'
obj/Debug/main.o: In function `__sti____cudaRegisterAll_39_tmpxft_000005e1_00000000_4_main_cpp1_ii_68d40520()':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1667): undefined reference to `__cudaRegisterFatBinary'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x16c8): undefined reference to `__cudaRegisterFunction'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1718): undefined reference to `__cudaRegisterFunction'
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text+0x1768): undefined reference to `__cudaRegisterFunction'
obj/Debug/main.o: In function `cudaError cudaLaunch<char>(char*)':
tmpxft_000005e1_00000000-1_main.cudafe1.cpp:(.text._Z10cudaLaunchIcE9cudaErrorPT_[cudaError cudaLaunch<char>(char*)]+0x14): undefined reference to `cudaLaunch'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
19 errors, 0 warnings
 

So it's clearly not finding things. How can I get these libraries linked in??

Thanks for the help so far. Neat little tools the way the program is put together.

Cheers,
Mark

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Compiler plugin for nvcc compiler?
« Reply #3 on: February 07, 2011, 09:34:14 pm »
As fist step you should turn on full commandline logging to see what is really used to invoke compiler and linker:
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_How_do_I_troubleshoot_an_compiler_problem.3F

Offline Mark Knecht

  • Single posting newcomer
  • *
  • Posts: 5
Re: Compiler plugin for nvcc compiler?
« Reply #4 on: February 07, 2011, 10:40:29 pm »
OK, this is a _very_ sweet tool. I don't know how to program, have never used an IDE before, and was able to modify my settings and get a working executable!

Here's the current build log output:


-------------- Clean: Debug in julia_gpu ---------------

Cleaned "julia_gpu - Debug"

-------------- Build: Debug in julia_gpu ---------------

nvcc -arch sm_20 --use_fast_math  -c /home/mark/CODE/LearningC++/julia_gpu/main.cu -o obj/Debug/main.o 

nvcc -L/usr/lib64/  -o bin/Debug/julia_gpu obj/Debug/main.o    -lglut -lm -lgsl -lgslcblas
Output size is 56.99 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
 
and up popped the Julia Set graphics. Works perfectly.


Problem is I don't like how I got there which was:

1) A good thing - adding all the libraries to the project. That makes sense.

2) A bad thing - changing 'Toolchain Executables -> Linker for dynamic libs' from g++ to nvcc. That doesn't make sense. (To me)

If I leave it that way then a standard project C++ wouldn't use g++ anymore to link which isn't good..

It seems to me that I need a new project type - a CUDA project - which has nvcc chosen as the tool set. I'm looking at 'templates' but so far it seems to reuse whatever the global settings are so it fails when I set things back to g++.

How can I set up a specific project type that always has nvcc even for linking, but not effect other project types. Seems that the

Other Settings->Advanced

is trying to give me that option?

Thanks!

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Compiler plugin for nvcc compiler?
« Reply #5 on: February 07, 2011, 10:52:04 pm »
Copy the gcc toolchain and create one for cuda. ("Settings -> Compiler and debugger... -> Global compiler settings -> GNU GCC compiler -> Copy")
Create a sample project and save it as template ("File -> Save project as template...").
To reuse it choose "File -> New -> From template -> User templates".

Offline Mark Knecht

  • Single posting newcomer
  • *
  • Posts: 5
Re: Compiler plugin for nvcc compiler?
« Reply #6 on: February 07, 2011, 10:58:52 pm »
Like the Cross Compiler stuff in the Wiki, right?

http://wiki.codeblocks.org/index.php?title=Code::Blocks_and_Cross_Compilers

I'm working on it but I don't have the paths right yet. I'll get back to that later. For now I'm only doing CUDA so I can leave it alone.

One last thing. How do I set the lexar for the *.cpp file type to also be used for this new *.cu file? Currently I'm only seeing black & white. As CUDA files are essentially C++ I might as well use it.

You guys have been so helpful! Thanks! I messed around with Qt-Creator for a few days and couldn't get half this far.

Cheers,
Mark

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Compiler plugin for nvcc compiler?
« Reply #7 on: February 07, 2011, 11:04:59 pm »
"Settings -> Editor -> General settings -> Syntax highlighting for -> C/C++", then click on "Filemasks..." and add *.cu to the list.

Offline Mark Knecht

  • Single posting newcomer
  • *
  • Posts: 5
Re: Compiler plugin for nvcc compiler?
« Reply #8 on: February 07, 2011, 11:28:19 pm »
Lovely! I now have color and can build projects. Great results. I'm truly indebted!

I'll work on the template stuff a bit more before I ask more questions. Over an out for now.

Thanks,
Mark

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5490
Re: Compiler plugin for nvcc compiler?
« Reply #9 on: February 08, 2011, 07:10:53 am »
once you get everything really working ok, it would be nice to copy the section in your default.conf about the nvcc compiler, together with other things you adapted to get the file type recognized and all.

We might add it to the CB core then.

Just take your time, till you are sure everything is there. Most difficult parts are probably the compiler/linker options and the regexes for the parsing of the errors/warnings.

Offline zak

  • Multiple posting newcomer
  • *
  • Posts: 26
Re: Compiler plugin for nvcc compiler?
« Reply #10 on: August 04, 2012, 06:42:00 am »

Does is it possible to get this default.conv??
Thank you!