Skip to content

Kmeans|| #650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
626bad5
Fix building.
trivialfis Jul 6, 2018
a7cf462
Enable building tests with cmake and make swig optional.
trivialfis Jul 6, 2018
c39c1bb
Enable specifying CUDA compute capability.
trivialfis Jul 22, 2018
e28cd4b
[WIP, KMeans||] skeleton for the algorithm.
trivialfis Jul 13, 2018
1cdb79b
Basic for KmMatrix and KMeans.
trivialfis Jul 16, 2018
42e0341
[KmMatrix] Split up proxy, add tests, and fixes.
trivialfis Jul 16, 2018
79529c6
More tests and fixes for KmMatrix.
trivialfis Jul 16, 2018
3b19006
Fix KmMatrix initialization with size.
trivialfis Jul 17, 2018
b58d868
Add helper tools for GPU.
trivialfis Jul 17, 2018
6f771b8
cuBlas wrapper file. Only two functions are added.
trivialfis Jul 18, 2018
c901a7f
Factor out config code, cublas_handle, add MatrixDim.
trivialfis Jul 18, 2018
1db8282
Rename class member variables for CudaKmMatrix.
trivialfis Jul 19, 2018
27b399f
Add stack for KmMatrix, fix impl ownership bug.
trivialfis Jul 19, 2018
70d0a10
Add license.
trivialfis Jul 19, 2018
8f65ea5
Construct kmeans|| based on KmMatrix.
trivialfis Jul 20, 2018
b7e7446
Fix prob for kmeans||.
trivialfis Jul 20, 2018
d507259
Fix K-Means|| along with added documents.
trivialfis Jul 20, 2018
7a79169
Create Generatorbase and refactor code.
trivialfis Jul 21, 2018
8c21e8e
Rename shared memory structs, add license.
trivialfis Jul 23, 2018
458e54e
Fix handles_ memory leak.
trivialfis Jul 23, 2018
d3b18df
Add argmin op. Fix min op for redundant memory access.
trivialfis Jul 23, 2018
9d5a094
Add centroids weighting.
trivialfis Jul 23, 2018
27a5ec1
Add simple re-clustering.
trivialfis Jul 23, 2018
09635e0
Factor out basic arith ops, add tests for them.
trivialfis Jul 24, 2018
62694bf
Pass all tests for arith.
trivialfis Jul 24, 2018
ec61974
Add tests for distance_pairs.
trivialfis Jul 24, 2018
c5d8bfe
Pass pairwise distance test.
trivialfis Jul 24, 2018
940d20e
Add test for GreedyRecluster.
trivialfis Jul 24, 2018
2e47f71
Fix zero distance prob, and re-cluster with large centroids.
trivialfis Jul 25, 2018
b45825a
Extract Arith operation definitions to cuda file.
trivialfis Jul 25, 2018
c579d26
Improve printing matrix.
trivialfis Jul 25, 2018
ca412a4
Finish test for kmeans||.
trivialfis Jul 25, 2018
fcfeaf3
Remove warning message.
trivialfis Jul 25, 2018
ad18cbb
Remove Eigen require.
trivialfis Jul 25, 2018
d358c82
Fix compiler warnings.
trivialfis Jul 25, 2018
1b2b346
Add rows functions for KmMatrix.
trivialfis Jul 25, 2018
b1b2d51
Add SizeError.
trivialfis Jul 25, 2018
93ae39b
Add int generator, reshape for KmMatrix.
trivialfis Jul 25, 2018
c95c9ba
Add random kmeans init class.
trivialfis Jul 25, 2018
07c6c3e
Revert format change for kmeans_h2o4gpu.cu.
trivialfis Jul 25, 2018
adb6ffa
Revert warning flag
trivialfis Jul 25, 2018
450051e
Inject new kmeans|| into kmeans algorithm.
trivialfis Jul 25, 2018
6c8ff4e
Re-structure.
trivialfis Jul 25, 2018
9afa105
Add config for USE_CUDA.
trivialfis Jul 25, 2018
bf9e479
Fix doc for KmeansLlInit.
trivialfis Jul 26, 2018
10d8ac6
Remove SelfMinOp.
trivialfis Jul 26, 2018
7efaf03
Use cub function for calculating min.
trivialfis Jul 26, 2018
7468a0b
Use thrust to generate random numbers.
trivialfis Jul 26, 2018
f297074
Rename Generator to RandomGenerator.
trivialfis Jul 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 104 additions & 112 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,151 +7,143 @@ SET_DEFAULT_CONFIGURATION_RELEASE()

FIND_PACKAGE(OpenMP)
FIND_PACKAGE(BLAS REQUIRED)
FIND_PACKAGE(SWIG REQUIRED)
FIND_PACKAGE(PythonLibs REQUIRED) # SWIG

INCLUDE(${SWIG_USE_FILE})

#============= OPTIONS & SETTINGS
OPTION(USE_CUDA "Build with GPU acceleration" ON)
OPTION(USE_SWIG "Use swig to generate language bindings." ON)
OPTION(BUILD_TESTS "Build tests." ON)
OPTION(USE_SYSTEM_GTEST "Use system google tests." ON)
OPTION(DEV_BUILD "Dev build" OFF)

# Compiler flags
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w")

# PythonLibs' PYTHON_INCLUDE_PATH doesn't take into account virtualenv etc.
# Open to suggestions how to do this better.
EXECUTE_PROCESS(COMMAND python -c "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE PYTHON_INCLUDE_PATH_CUST
OUTPUT_STRIP_TRAILING_WHITESPACE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
SET(GPU_COMPUTE_VER "" CACHE STRING
"Semicolon separated list of compute versions to be built against, e.g. -DGPU_COMPUTE_VER='35;61'")

if(OpenMP_CXX_FOUND OR OPENMP_FOUND)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
#============= OPTIONS & SETTINGS

# TODO probably to be removed after POGS is out in favor of XGboost GLM
ADD_DEFINITIONS(
-D_GITHASH_=0
-DH2O4GPU_DOUBLE
-DH2O4GPU_SINGLE
)
-D_GITHASH_=0
-DH2O4GPU_DOUBLE
-DH2O4GPU_SINGLE)

#============= BUILD COMMON CPU/GPU CODE
FILE(GLOB_RECURSE COMMON_SOURCES
src/common/*.cpp
src/common/*.h
src/interface_c/*.cpp
src/interface_c/*.h
)
src/common/*.cpp
src/common/*.h
src/interface_c/*.cpp
src/interface_c/*.h)

INCLUDE_DIRECTORIES(
src/include
src/cpu/include
# Here and not in target_include_directories b/c cmake < 3.7 which we use in Dockerfiles does not support it
src/gpu/include
${PYTHON_INCLUDE_PATH}
${PYTHON_INCLUDE_PATH_CUST}
)
src/include
src/cpu/include
# Here and not in target_include_directories b/c cmake < 3.7 which we use in Dockerfiles does not support it
src/gpu/include)

ADD_LIBRARY(commonh2o4gpu OBJECT ${COMMON_SOURCES})
#============= BUILD COMMON CPU/GPU CODE

#============= BUILD CPU LIBRARY
FILE(GLOB_RECURSE CPU_SOURCES
src/cpu/*.cpp
src/cpu/*.h
)
src/cpu/*.cpp
src/cpu/*.h)

ADD_LIBRARY(cpuh2o4gpu STATIC ${CPU_SOURCES} $<TARGET_OBJECTS:commonh2o4gpu>)
TARGET_LINK_LIBRARIES(cpuh2o4gpu ${BLAS_LIBRARIES})
#============= BUILD CPU LIBRARY

#============= SWIG
SET(CMAKE_SWIG_FLAGS -Werror)
#============= SWIG

#============= CPU SWIG
SET_SOURCE_FILES_PROPERTIES(src/swig/ch2o4gpu_cpu.i PROPERTIES CPLUSPLUS ON)

if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
SWIG_ADD_MODULE(ch2o4gpu_cpu python src/swig/ch2o4gpu_cpu.i)
if (USE_CUDA)
SET(HG_USE_CUDA 1)
else()
SWIG_ADD_LIBRARY(ch2o4gpu_cpu LANGUAGE python SOURCES src/swig/ch2o4gpu_cpu.i)
endif()

SWIG_LINK_LIBRARIES(ch2o4gpu_cpu cpuh2o4gpu ${PYTHON_LIBRARIES})

SET_TARGET_PROPERTIES(${SWIG_MODULE_ch2o4gpu_cpu_REAL_NAME} PROPERTIES
LINK_FLAGS ${OpenMP_CXX_FLAGS})
#============= CPU SWIG
SET(HG_USE_CUDA 0)
endif(USE_CUDA)
ADD_SUBDIRECTORY(${CMAKE_CURRENT_LIST_DIR}/src/common)

if(USE_CUDA)
FIND_PACKAGE(CUDA 8.0 REQUIRED)
FIND_PACKAGE(NVML REQUIRED)

#============= BUILD GPU LIBRARY
ADD_DEFINITIONS(
-DCUDA_MAJOR=${CUDA_VERSION_MAJOR}
-DHAVECUDA
)

if(DEV_BUILD)
MESSAGE(STATUS "Building DEVELOPER compute capability version.")
SET(GPU_COMPUTE_VER 61 CACHE STRING
"Space separated list of compute versions to be built against")
else()
MESSAGE(STATUS "Building RELEASE compute capability version.")
SET(GPU_COMPUTE_VER 35;50;52;60;61 CACHE STRING
"Space separated list of compute versions to be built against")
endif()

if(((CUDA_VERSION_MAJOR EQUAL 9) OR (CUDA_VERSION_MAJOR GREATER 9)) AND NOT DEV_BUILD)
MESSAGE(STATUS "CUDA 9.0 detected, adding Volta compute capability (7.0).")
SET(GPU_COMPUTE_VER "${GPU_COMPUTE_VER};70")
endif()

SET(GENCODE_FLAGS "")
FORMAT_GENCODE_FLAGS("${GPU_COMPUTE_VER}" GENCODE_FLAGS)
SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler -fPIC; -std=c++11;--expt-extended-lambda;--expt-relaxed-constexpr;${GENCODE_FLAGS};-lineinfo; -w;")

FILE(GLOB_RECURSE GPU_SOURCES
src/*.cu
src/*.cuh
src/common/*.cpp
src/common/*.h
)

CUDA_ADD_LIBRARY(gpuh2o4gpu ${GPU_SOURCES} $<TARGET_OBJECTS:commonh2o4gpu> STATIC)

if($ENV{USENVTX})
MESSAGE(STATUS "Building with NVTX support on.")
SET(NVTX_LIBRARY nvToolsExt)
endif()

TARGET_LINK_LIBRARIES(gpuh2o4gpu
${CUDA_CUBLAS_LIBRARIES}
${CUDA_cusolver_LIBRARY}
${CUDA_cusparse_LIBRARY}
${BLAS_LIBRARIES}
${NVTX_LIBRARY}
${NVML_LIBRARY})
#============= BUILD GPU LIBRARY

#============= GPU SWIG
SET_SOURCE_FILES_PROPERTIES(src/swig/ch2o4gpu_gpu.i PROPERTIES CPLUSPLUS ON)

if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
SWIG_ADD_MODULE(ch2o4gpu_gpu python src/swig/ch2o4gpu_gpu.i)
else()
SWIG_ADD_LIBRARY(ch2o4gpu_gpu LANGUAGE python SOURCES src/swig/ch2o4gpu_gpu.i)
endif()
SWIG_LINK_LIBRARIES(ch2o4gpu_gpu gpuh2o4gpu ${PYTHON_LIBRARIES})

SET_TARGET_PROPERTIES(${SWIG_MODULE_ch2o4gpu_gpu_REAL_NAME} PROPERTIES
LINK_FLAGS ${OpenMP_CXX_FLAGS})
#============= GPU SWIG
FIND_PACKAGE(CUDA 8.0 REQUIRED)
FIND_PACKAGE(NVML REQUIRED)

#============= BUILD GPU LIBRARY
ADD_DEFINITIONS(
-DCUDA_MAJOR=${CUDA_VERSION_MAJOR}
-DHAVECUDA
)

if(DEV_BUILD AND NOT GPU_COMPUTE_VER)
MESSAGE(STATUS "Building DEVELOPER compute capability version.")
SET(GPU_COMPUTE_VER 61)
elseif(NOT GPU_COMPUTE_VER)
MESSAGE(STATUS "Building RELEASE compute capability version.")
SET(GPU_COMPUTE_VER 35;50;52;60;61)
endif()

if(((CUDA_VERSION_MAJOR EQUAL 9)
OR (CUDA_VERSION_MAJOR GREATER 9))
AND NOT DEV_BUILD
AND NOT GPU_COMPUTE_VER)
MESSAGE(STATUS "CUDA 9.0 detected, adding Volta compute capability (7.0).")
SET(GPU_COMPUTE_VER "${GPU_COMPUTE_VER};70")
endif()

SET(GENCODE_FLAGS "")
FORMAT_GENCODE_FLAGS("${GPU_COMPUTE_VER}" GENCODE_FLAGS)
MESSAGE("CUDA architecture flags ${GENCODE_FLAGS}")

SET(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS};-Xcompiler -fPIC; -std=c++11;--expt-extended-lambda;--expt-relaxed-constexpr;${GENCODE_FLAGS};-lineinfo;")

FILE(GLOB_RECURSE GPU_SOURCES
src/*.cu
src/*.cuh
src/gpu/matrix/*.cpp
src/common/*.cpp
src/common/*.h)

CUDA_ADD_LIBRARY(gpuh2o4gpu ${GPU_SOURCES} $<TARGET_OBJECTS:commonh2o4gpu> STATIC)

if($ENV{USENVTX})
MESSAGE(STATUS "Building with NVTX support on.")
SET(NVTX_LIBRARY nvToolsExt)
endif()

TARGET_LINK_LIBRARIES(gpuh2o4gpu
${CUDA_CUBLAS_LIBRARIES}
${CUDA_cusolver_LIBRARY}
${CUDA_cusparse_LIBRARY}
${BLAS_LIBRARIES}
${NVTX_LIBRARY}
${NVML_LIBRARY})
#============= BUILD GPU LIBRARY
endif()

if(USE_SWIG)
ADD_SUBDIRECTORY(${CMAKE_CURRENT_LIST_DIR}/src/swig)
endif(USE_SWIG)

#============= Tests
if(BUILD_TESTS)
ENABLE_TESTING()
if (USE_SYSTEM_GTEST)
FIND_PACKAGE(GTest REQUIRED)
else ()
ADD_SUBDIRECTORY(${CMAKE_CURRENT_LIST_DIR}/tests/googletest)
SET(GTEST_LIBRARIES gtest gtest_main)
endif (USE_SYSTEM_GTEST)
if (USE_CUDA)
FILE(GLOB_RECURSE CUDA_TEST_SOURCES "tests/cpp/*.cu")
CUDA_COMPILE(CUDA_TEST_OBJS ${CUDA_TEST_SOURCES})
endif(USE_CUDA)

ADD_EXECUTABLE(test-h2o4gpu ${CUDA_TEST_OBJS})
TARGET_LINK_LIBRARIES(test-h2o4gpu gpuh2o4gpu)
TARGET_LINK_LIBRARIES(test-h2o4gpu ${GTEST_LIBRARIES})

ADD_TEST(TestH2O4GPU test-h2o4gpu)
endif(BUILD_TESTS)
#============= Tests
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/utils.h.in ${CMAKE_CURRENT_SOURCE_DIR}/utils.h)
21 changes: 0 additions & 21 deletions src/common/utils.h

This file was deleted.

54 changes: 54 additions & 0 deletions src/common/utils.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a strange file name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the original utils.h from h2o4gpu with some functions moved from gpu code.
For the *.h.in, it's for cmake configuration, specifically USE_CUDA flag. I know compiling without CUDA is not supported. But anyway I tried to remain the possibility. If that a burden, I can remove it in no time.

* Copyright 2017-2018 H2O.ai, Inc.
* License Apache License Version 2.0 (see LICENSE for details)
*/
#pragma once
#include <vector>

#include <iostream>
#include <sstream>

#include "cblas/cblas.h"

#define USE_CUDA() @HG_USE_CUDA@

template<typename T>
void self_dot(std::vector<T> array_in, int n, int dim,
std::vector<T>& dots);

void compute_distances(std::vector<double> data_in,
std::vector<double> centroids_in,
std::vector<double> &pairwise_distances,
int n, int dim, int k);

void compute_distances(std::vector<float> data_in,
std::vector<float> centroids_in,
std::vector<float> &pairwise_distances,
int n, int dim, int k);

// Matrix host dev
#define HG_HOSTDEV __host__ __device__
#define HG_DEV __device__
#define HG_DEVINLINE __device__ __forceinline__
#define HG_HOSTDEVINLINE __host__ __device__ __forceinline__

#define h2o4gpu_error(x) error(x, __FILE__, __LINE__);

inline void error(const char* e, const char* file, int line)
{
std::stringstream ss;
ss << e << " - " << file << "(" << line << ")";
//throw error_text;
std::cerr << ss.str() << std::endl;
exit(-1);
}

#define h2o4gpu_check(condition, msg) check(condition, msg, __FILE__, __LINE__);

inline void check(bool val, const char* e, const char* file, int line)
{
if (!val)
{
error(e, file, line);
}
}
1 change: 1 addition & 0 deletions src/cpu/h2o4gpuglm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <algorithm>
#include <limits>
#include <deque>
#include <numeric>

#include <functional>
#include <cstring>
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/h2o4gpukmeans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <algorithm>
#include <vector>
//#include "mkl.h"
#include "cblas.h"
#include "cblas/cblas.h"
#include <atomic>
#include <csignal>

Expand Down
1 change: 1 addition & 0 deletions src/gpu/h2o4gpuglm.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <thrust/transform.h>

#include <algorithm>
#include <numeric>
#include <limits>
#include <deque>

Expand Down
4 changes: 2 additions & 2 deletions src/gpu/kmeans/kmeans_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
#pragma once
#include "../../common/logger.h"
#include "../utils/utils.cuh"
#include "stdio.h"
#define MAX_NGPUS 16

#define VERBOSE 0
Expand All @@ -12,8 +14,6 @@

// TODO(pseudotensor): Avoid throw for python exception handling. Need to avoid all exit's and return exit code all the way back.
#define gpuErrchk(ans) { gpu_assert((ans), __FILE__, __LINE__); }
#define safe_cuda(ans) throw_on_cuda_error((ans), __FILE__, __LINE__);
#define safe_cublas(ans) throw_on_cublas_error((ans), __FILE__, __LINE__);

#define CUDACHECK(cmd) do { \
cudaError_t e = cmd; \
Expand Down
Loading