LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

[GPU] Compilation problems with MSVC

Open tt83 opened this issue 9 years ago • 4 comments

LightGBM with GPU support does not compile with MSVC (VS 14 2015). There are problems:

  1. OpenCL kernels are included as string literals, but MSVC does not support string literals longer than 16380 characters. So it generates C2026 error. To solve this issue consider using xxd linux utility. It can translate text to unsigned char array. For example, xxd -i histogram16.cl produces this code: unsigned char histogram16_cl[] = { 0x23, 0x69, 0x66, 0x6e, 0x64, 0x65, 0x66, 0x20, 0x5f, 0x48, 0x49, 0x53, 0x54, 0x4f, 0x47, 0x52, 0x41, 0x4d, 0x5f, 0x31, 0x36, 0x5f, 0x4b, 0x45,... }; unsigned int histogram16_cl_len = 41560; So just run xxd -i histogram16.cl > xxd -i histogram16.cl.h, include histogram16.cl.h in implementation file (gpu_tree_learner.cpp) and use it like this:
//    kernel_source_ = kernel16_src_;
kernel_source_ = std::string(std::begin(histogram16_cl),  std::end(histogram16_cl));
  1. Somewhere in the code of gpu_tree_learner.cpp we can see dense_ind[dword_features_];, where dword_features_ non-constant data member. This is variable-length array which is not supported by C++11 standard (and MSVC too), but GCC (g++) extension allows this. This problem can be solved simply by replacing variable with constant.
  2. In the gpu_tree_learner.h used next structure:
  /*! \brief 4-byte feature tuple used by GPU kernels */
  struct Feature4 {
	  union {
		  unsigned char s[4];
		  struct {
			  unsigned char s0;
			  unsigned char s1;
			  unsigned char s2;
			  unsigned char s3;
		  };
	  };
  };

This is anonymous struct and also is not allowed by C++11 standard, but allowed by g++ and msvc as extension. I don't understand why union is needed here, but if so better to name structure, for example:

  /*! \brief 4-byte feature tuple used by GPU kernels */
  struct Feature4 {
	  union {
		  unsigned char s[4];
		  struct s4{
			  unsigned char s0;
			  unsigned char s1;
			  unsigned char s2;
			  unsigned char s3;
		  } ss;
	  };
  };

There are some other small issues generating warnings like "conversion from 'uint32_t' to 'unsigned char', possible loss of data". Maybe it makes sense to use more pedantic compiler flags to resolve them.

  1. Boost libraries linking problem. In boost by default enabled auto-linking for compilers that support this feature. With filesystem and system boost tries to autolink also chrono library which cannot be found - MSVC produces link error. Solution is to add definition -DBOOST_ALL_NO_LIB. Something like that for cmake:
if (WIN32)
  # disable autolinking in boost
  add_definitions( -DBOOST_ALL_NO_LIB )
endif()

tt83 avatar Apr 21 '17 11:04 tt83

ping @huanzhang12

Laurae2 avatar Apr 21 '17 14:04 Laurae2

@tt83 Thanks so much for testing the GPU code on MSVC! I was never able to test the code on MSVC and your feedback is very valuable.

2, 3 and 4 can be easily fixed. I have create a PR to fix these glitches. Could you please test the PR #443 and see if it eliminates all the warnings in MSVC?

The reason I put the GPU kernels as string literals is to avoid xxd. I used to use xxd for converting OpenCL code to a hex string when I use C89. But in C++11 I really want to avoid this ugly part. Not all platforms have the xxd utility, so converting the OpenCL kernels in compile time may add additional difficulty for building. We can certainly put converted OpenCL files directly into the repository, but it makes the OpenCL code unreadable, and creates additional maintenance burden as they have to be manually synchronized with the latest OpenCL code.

We probably need a different flow for adding these OpenCL kernels in MSVC, to workaround the string literal size limit. We can probably adding them as resource files? Can any MSVC expert @guolinke @chivee comment on this here?

huanzhang12 avatar Apr 22 '17 07:04 huanzhang12

Fixed in PR #443. This issue can be closed now ;)

huanzhang12 avatar Apr 24 '17 08:04 huanzhang12

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

github-actions[bot] avatar Aug 24 '23 02:08 github-actions[bot]