copmiling cuda code

1,828 views
Skip to first unread message

惠光李

unread,
Apr 25, 2017, 10:39:13 PM4/25/17
to bazel-discuss
i am new to bazel and i am looking for how to compile cuda using bazel. if people can provide me a link to a simple example, that will be great. thanks in advance for the help.

i did a search online and got some example code from https://github.com/Hibbert-pku/bazel_nvcc but it does not contain much explanation, i try to understand what those bazel code means online. following the README, i ran

bazel build cuda:all    # this works
bazel test tests:all      # does not work -- i show the log at the end of this message

i wonder if "bazel test tests:all" is really needed to demonstrate compiling cuda.

i cd into the bazel-bin directory, to run the executable "main" and got error message below. how can i fix this error?

hweekuans-MacBook-Pro:cuda hweekuan$ pwd; ./main
/Users/hweekuan/Downloads/bazel_nvcc-master/bazel-bin/cuda
dyld: lazy symbol binding failed: Symbol not found: __Z4testv
  Referenced from: /private/var/tmp/_bazel_hweekuan/0b84f6c3e6339ca2539ddc180fa50b4d/execroot/bazel_nvcc-master/bazel-out/local-fastbuild/bin/cuda/./main
  Expected in: flat namespace

dyld: Symbol not found: __Z4testv
  Referenced from: /private/var/tmp/_bazel_hweekuan/0b84f6c3e6339ca2539ddc180fa50b4d/execroot/bazel_nvcc-master/bazel-out/local-fastbuild/bin/cuda/./main
  Expected in: flat namespace

Abort trap: 6

------------------------------
error for running bazel test tests:all
hweekuans-MacBook-Pro:bazel_nvcc-master hweekuan$ bazel test tests:all
ERROR: /Users/hweekuan/Downloads/bazel_nvcc-master/tests/BUILD:5:1: no such package '@gtest//': Error downloading [https://googletest.googlecode.com/files/gtest-1.7.0.zip] to /private/var/tmp/_bazel_hweekuan/0b84f6c3e6339ca2539ddc180fa50b4d/external/gtest/gtest-1.7.0.zip: GET returned 404 Not Found and referenced by '//tests:all_tests'.
ERROR: Analysis of target '//tests:all_tests' failed; build aborted.
INFO: Elapsed time: 0.339s
ERROR: Couldn't start the build. Unable to run tests.

Damien Martin-Guillerez

unread,
Apr 26, 2017, 4:48:40 AM4/26/17
to 惠光李, bazel-discuss
Hi,

Tensorflow does NVCC compilation on both Linux and darwin. To configure their cuda build they use a special toolchain generation code at https://github.com/tensorflow/tensorflow/blob/master/third_party/gpus/cuda_configure.bzl. You might want to look into using it, they then invoke that code from the WORKSPACE and point the crosstool arg to the generated one.

I hope that can help you.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/3ef2746d-31bf-411d-9bf9-de214c797e21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

惠光李

unread,
Apr 26, 2017, 9:57:27 AM4/26/17
to bazel-discuss, hweeku...@gmail.com
thanks a lot. i looked at the code, as i am new to bazel, will take some time for me to take that and change it to suit my needs. anyway i should be learning.


在 2017年4月26日星期三 UTC+8下午4:48:40,Damien Martin-Guillerez写道:

惠光李

unread,
Apr 26, 2017, 10:49:40 AM4/26/17
to bazel-discuss, hweeku...@gmail.com
i managed to use a cheap trick to do this. other than being ugly i wonder if there are other disadvantage. any comments are welcomed. thanks.

what i did is create two directories at the root of my source code. one directory (cuda directory) contains all the cuda codes and the other directory (bazel directory) contains codes for bazel build, i.e. tensorflow library etc

in the the cuda directory, i use make utility to compile my cuda code into a .so file (call it libmycuda.so). in this way i encapsulate all cuda components into libmycuda.so file.

next i copy libmycuda.so and its associated header file into the bazel directory

cd into the bazel directory and build code using bazel, in my BUILD file, i added something like:

cc_library(
    name = "mycuda",
    srcs = ["libmycuda.so"],
    hdrs = ["mycuda.h"],
    linkopts = ["-W1,-rpath,./"],
)
cc_binary(
    name = "test",
    srcs = ["test.cc"],
    deps = ["mycuda"],
)






在 2017年4月26日星期三 UTC+8下午9:57:27,惠光李写道:

bre...@gmail.com

unread,
Jul 5, 2017, 5:56:43 PM7/5/17
to bazel-discuss, hweeku...@gmail.com
Hi Damien,

I am reading the code in cuda_configure.bzl, which you referred to in the reply. It seems to me that Tensorflow has created a rule called "tf_cuda_library" (https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tensorflow.bzl#L603). It seems to me that the only extra thing this rule does other than those in "cc_library" is to depend on "@local_config_cuda".

My question is how bazel is notified to use the CUDA toolchain (nvcc, etc) instead of the default toolchain in this case? Does it automatically pick CUDA toolchain if @local_config_cuda is used as a dependency?

Thanks!

Marcel Hlopko

unread,
Jul 19, 2017, 4:30:06 AM7/19/17
to bre...@gmail.com, bazel-discuss, hweeku...@gmail.com
Hi,

cc_cuda_library is just adding more dependencies to the library itself, it's not changing the toolchain. Toolchain must be changed explicitly using --config=cuda when invoking bazel (and that is defined in tools/bazel.rc to mean --crosstool_top=@local_config_cuda//crosstool:toolchain and that's what changes the toolchain.


For more options, visit https://groups.google.com/d/optout.
--
-- 
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle | Registergericht und -nummer: Hamburg, HRB 86891

Break

unread,
Jul 19, 2017, 11:09:18 AM7/19/17
to Marcel Hlopko, bazel-discuss, hweeku...@gmail.com
Thanks for the notes, Marcel! I figured this out reading more into Tensorflow's bazel implementation, and I can now compile Caffe with CUDA successfully. Thanks!
Reply all
Reply to author
Forward
0 new messages