Skip to content

Commit

Permalink
Re-structure.
Browse files Browse the repository at this point in the history
* Put KmMatrix under gpu/matrix
* Use shared utils for both kmeans and KmMatrix.
* Rename the namespaces.
  • Loading branch information
trivialfis committed Jul 25, 2018
1 parent 450051e commit 6c8ff4e
Show file tree
Hide file tree
Showing 26 changed files with 263 additions and 358 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ if(USE_CUDA)
FILE(GLOB_RECURSE GPU_SOURCES
src/*.cu
src/*.cuh
src/gpu/kmeans/KmMatrix/*.cpp
src/gpu/kmeans/KmMatrix/*.hpp
src/gpu/matrix/*.cpp
src/common/*.cpp
src/common/*.h)

Expand Down
33 changes: 33 additions & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
*/
#pragma once
#include <vector>

#include <iostream>
#include <sstream>

#include "cblas/cblas.h"

#define USE_CUDA() 1

template<typename T>
void self_dot(std::vector<T> array_in, int n, int dim,
std::vector<T>& dots);
Expand All @@ -19,3 +25,30 @@ 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);
}
}
81 changes: 0 additions & 81 deletions src/gpu/kmeans/KmMatrix/KmConfig.h

This file was deleted.

69 changes: 0 additions & 69 deletions src/gpu/kmeans/KmMatrix/utils.cuh

This file was deleted.

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

Expand All @@ -13,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
6 changes: 3 additions & 3 deletions src/gpu/kmeans/kmeans_h2o4gpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,9 @@ int kmeans_fit(int verbose, int seed, int gpu_idtry, int n_gputry,
thrust::device,
data[i]->begin(), data[i]->end(), h_init_data.begin());
}
H2O4GPU::KMeans::KmMatrix<T> init_data(h_init_data, rows, cols);
H2O4GPU::KMeans::KmMatrix<T> final_centroids_matrix =
H2O4GPU::KMeans::KmeansLlInit<T>(seed, 1.5)(init_data, k);
h2o4gpu::kMeans::KmMatrix<T> init_data(h_init_data, rows, cols);
h2o4gpu::kMeans::KmMatrix<T> final_centroids_matrix =
h2o4gpu::kMeans::KmeansLlInit<T>(seed, 1.5)(init_data, k);
thrust::host_vector<T> final_centroids (final_centroids_matrix.size());
thrust::copy(
final_centroids_matrix.dev_ptr(),
Expand Down
32 changes: 17 additions & 15 deletions src/gpu/kmeans/kmeans_init.cu
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@

#include "kmeans_init.cuh"

#include "KmMatrix/KmMatrix.hpp"
#include "KmMatrix/Arith.hpp"
#include "KmMatrix/utils.cuh"
#include "KmMatrix/GpuInfo.cuh"
#include "KmMatrix/blas.cuh"
#include "../matrix/KmMatrix/KmMatrix.hpp"
#include "../matrix/KmMatrix/Arith.hpp"
#include "../matrix/KmMatrix/blas.cuh"
#include "../utils/utils.cuh"
#include "../utils/GpuInfo.cuh"

namespace H2O4GPU {
namespace KMeans {
namespace h2o4gpu {
namespace kMeans {

using namespace Matrix;

namespace kernel {
// X^2 + Y^2, here only calculates the + operation.
Expand Down Expand Up @@ -116,7 +118,7 @@ KmMatrix<T> PairWiseDistanceOp<T>::operator()(KmMatrix<T>& _data,
data_dot_.k_param(),
centroids_dot_.k_param());
CUDA_CHECK(cudaGetLastError());
safe_cuda(cudaGetLastError());
cublasHandle_t handle = GpuInfo::ins().cublas_handle();
Expand Down Expand Up @@ -191,7 +193,7 @@ KmMatrix<T> GreedyRecluster<T>::recluster(KmMatrix<T>& _centroids, size_t _k) {
void *d_temp_storage = NULL;
// determine the temp_storage_bytes
CUDA_CHECK(cub::DeviceHistogram::HistogramEven(
safe_cuda(cub::DeviceHistogram::HistogramEven(
d_temp_storage, temp_storage_bytes,
min_indices.dev_ptr(),
weights.dev_ptr(),
Expand All @@ -200,16 +202,16 @@ KmMatrix<T> GreedyRecluster<T>::recluster(KmMatrix<T>& _centroids, size_t _k) {
(T)min_indices.rows(),
(int)_centroids.rows()));
CUDA_CHECK(cudaMalloc((void**)&d_temp_storage, temp_storage_bytes));
CUDA_CHECK(cub::DeviceHistogram::HistogramEven(
safe_cuda(cudaMalloc((void**)&d_temp_storage, temp_storage_bytes));
safe_cuda(cub::DeviceHistogram::HistogramEven(
d_temp_storage, temp_storage_bytes,
min_indices.dev_ptr(), // d_samples
weights.dev_ptr(), // d_histogram
min_indices.rows() + 1, // num_levels
(T)0.0, // lower_level
(T)min_indices.rows(), // upper_level
(int)_centroids.rows())); // num_samples
CUDA_CHECK(cudaFree(d_temp_storage));
safe_cuda(cudaFree(d_temp_storage));
// Sort the indices by weights in ascending order, then use those at front
// as result.
Expand Down Expand Up @@ -352,7 +354,7 @@ KmeansLlInit<T, ReclusterPolicy>::operator()(KmMatrix<T>& _data, size_t _k) {
"k must be less than or equal to the number of data points"
", k: %lu, data points: %lu",
_k, _data.rows());
M_USER_ERROR(err_msg);
h2o4gpu_error(err_msg);
}
if (seed_ < 0) {
Expand Down Expand Up @@ -433,5 +435,5 @@ INSTANTIATE(int)
#undef INSTANTIATE
}
} // namespace Kmeans
} // namespace H2O4GPU
} // namespace kMeans
} // namespace h2o4gpu
21 changes: 11 additions & 10 deletions src/gpu/kmeans/kmeans_init.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@

#include <memory>

#include "KmMatrix/KmConfig.h"
#include "KmMatrix/KmMatrix.hpp"
#include "KmMatrix/utils.cuh"
#include "KmMatrix/Generator.hpp"
#include "KmMatrix/Generator.cuh"
#include "KmMatrix/GpuInfo.cuh"
#include "../matrix/KmMatrix/KmMatrix.hpp"
#include "../matrix/KmMatrix/Generator.hpp"
#include "../matrix/KmMatrix/Generator.cuh"
#include "../utils/GpuInfo.cuh"
#include "../utils/utils.cuh"

constexpr double ESP = 1e-8;

namespace H2O4GPU {
namespace KMeans {
namespace h2o4gpu {
namespace kMeans {

using namespace Matrix;

namespace detail {

Expand Down Expand Up @@ -202,7 +203,7 @@ struct KmeansLlInit : public KmeansInitBase<T> {

// FIXME: Make kmeans++ a derived class of KmeansInitBase

} // namespace Kmeans
} // namespace H2O4GPU
} // namespace kMeans
} // namespace h2o4gpu

#endif // KMEANS_INIT_H_
Loading

0 comments on commit 6c8ff4e

Please sign in to comment.