Skip to content

Commit

Permalink
[WIP] Added CUDA support for data structures (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 authored Feb 28, 2020
1 parent f104191 commit 9b6750a
Show file tree
Hide file tree
Showing 13 changed files with 884 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ project(adaboost CXX)
# First, define all the compilation options.
option(BUILD_TESTS "Build tests." OFF)
option(INSTALL_GOOGLETEST "For installing GoogleTest on your system along with the build." OFF)
option(BUILD_CUDA "Build with CUDA support." OFF)

include(CheckLanguage)
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
else()
message(STATUS "Building without CUDA support as no CUDA compiler is found on this system.")
endif()


# Set required standard to C++11.
Expand Down Expand Up @@ -37,4 +46,6 @@ install(DIRECTORY adaboost DESTINATION ${CMAKE_INSTALL_PREFIX}/include
install(FILES
${CMAKE_BINARY_DIR}/libs/libadaboost_core.so
${CMAKE_BINARY_DIR}/libs/libadaboost_utils.so
${CMAKE_BINARY_DIR}/libs/libadaboost_cuda.so
${CMAKE_BINARY_DIR}/libs/libadaboost_cuda_wrappers.so
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
16 changes: 14 additions & 2 deletions adaboost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ add_library(adaboost_utils SHARED
utils/utils_impl.cpp)
add_library(adaboost_core SHARED
core/data_structures_impl.cpp
core/operations_impl.cpp)
core/operations_impl.cpp
utils/utils_impl.cpp)
if(BUILD_CUDA)
add_library(adaboost_cuda_wrappers SHARED
utils/cuda_wrappers_impl.cu)
add_library(adaboost_cuda SHARED
cuda/cuda_data_structures_impl.cu
utils/cuda_wrappers_impl.cu)
endif()
if(BUILD_TESTS)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LINK_LIBRARIES gtest gtest_main pthread adaboost_core adaboost_utils)
set(LINK_LIBRARIES gtest gtest_main pthread adaboost_core)
add_executable(test_core tests/test_core.cpp)
target_link_libraries(test_core ${LINK_LIBRARIES})
if(BUILD_CUDA)
add_executable(test_cuda tests/test_cuda.cpp)
target_link_libraries(test_cuda ${LINK_LIBRARIES} adaboost_cuda)
endif()
endif()
8 changes: 6 additions & 2 deletions adaboost/core/data_structures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ namespace adaboost
*/
unsigned int get_size() const;

data_type_vector* get_data_pointer() const;

/*
* Used for freeing memory.
*/
Expand All @@ -102,7 +104,7 @@ namespace adaboost
unsigned int rows, cols;

//! Array for storing data internally in matrices.
data_type_matrix **data;
data_type_matrix* data;

/*
* For reserving space in memory accoring to a given rows and columns.
Expand All @@ -112,7 +114,7 @@ namespace adaboost
* @param _rows Number of rows for which the space is to be reserved.
* @param _cols Number of columns for which the space is to be reserved.
*/
static data_type_matrix**
static data_type_matrix*
_reserve_space(unsigned int _rows,
unsigned int _cols);

Expand Down Expand Up @@ -174,6 +176,8 @@ namespace adaboost
*/
unsigned int get_cols() const;

data_type_matrix* get_data_pointer() const;

/*
* Used for freeing memory.
*/
Expand Down
32 changes: 19 additions & 13 deletions adaboost/core/data_structures_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include<adaboost/core/data_structures.hpp>
#include<adaboost/utils/utils.hpp>
#include<iostream>

namespace adaboost
{
Expand Down Expand Up @@ -66,6 +67,12 @@ namespace adaboost
return this->size;
}

template <class data_type_vector>
data_type_vector* Vector<data_type_vector>::get_data_pointer() const
{
return this->data;
}

template <class data_type_vector>
Vector<data_type_vector>::
~Vector()
Expand All @@ -84,16 +91,12 @@ namespace adaboost
}

template <class data_type_matrix>
data_type_matrix** Matrix<data_type_matrix>::
data_type_matrix* Matrix<data_type_matrix>::
_reserve_space(unsigned int _rows, unsigned int _cols)
{
adaboost::utils::check(_rows > 0, "Number of rows should be positive.");
adaboost::utils::check(_cols > 0, "Number of cols should be positive.");
data_type_matrix** new_data = new data_type_matrix*[_rows];
for(unsigned int i = 0; i < _rows; i++)
{
new_data[i] = new data_type_matrix[_cols];
}
data_type_matrix* new_data = new data_type_matrix[_rows*_cols];
return new_data;
}

Expand All @@ -114,7 +117,7 @@ namespace adaboost
"Row index out of range.");
adaboost::utils::check(y >= 0 && y < this->get_cols(),
"Column index out of range.");
return this->data[x][y];
return this->data[x*this->cols + y];
}

template <class data_type_matrix>
Expand All @@ -127,7 +130,7 @@ namespace adaboost
"Row index out of range.");
adaboost::utils::check(y >= 0 && y < this->get_cols(),
"Column index out of range.");
this->data[x][y] = value;
this->data[x*this->cols + y] = value;
}

template <class data_type_matrix>
Expand All @@ -138,7 +141,7 @@ namespace adaboost
{
for(unsigned int j = 0; j < this->cols; j++)
{
this->data[i][j] = value;
this->data[i*this->cols + j] = value;
}
}
}
Expand All @@ -157,16 +160,19 @@ namespace adaboost
return this->cols;
}

template <class data_type_matrix>
data_type_matrix* Matrix<data_type_matrix>::get_data_pointer() const
{
return this->data;
}

template <class data_type_matrix>
Matrix<data_type_matrix>::
~Matrix()
{
if(this->data != NULL)
{
for(unsigned int i = 0; i < this->rows; i++)
{
delete [] this->data[i];
}
delete [] this->data;
}
}

Expand Down
158 changes: 158 additions & 0 deletions adaboost/cuda/cuda_data_structures.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#ifndef CUDA_ADABOOST_CORE_DATA_STRUCTURES_HPP
#define CUDA_ADABOOST_CORE_DATA_STRUCTURES_HPP

#include<adaboost/core/data_structures.hpp>

namespace adaboost
{
namespace cuda
{
namespace core
{
/*
* This class represents the GPU version
* of adaboost::core::Vector.
*
* @tparam data_type_vector Data type of the elements
* supported by C++.
*/
template <class data_type_vector>
class VectorGPU: public adaboost::core::Vector<data_type_vector>
{
private:

//! Array for storing data on GPU.
data_type_vector* data_gpu;

//! The size of the vector stored on GPU.
unsigned size_gpu;

/*
* For reserving space in GPU memory accoring to a given size.
* Used in initializer list of parameterized constructors.
* Returns a new pointer.
*
* @param _size The size for which the space is to be reserved
* on GPU.
*/
static data_type_vector*
_reserve_space_gpu(unsigned _size_gpu);

public:

/*
* Default constructor.
* Sets VectorGPU::data_gpu to NULL and size_gpu to 0.
*/
VectorGPU();

/*
* Prameterized constructor.
*
* @param _size The size of the vector on GPU.
* Must be positive.
*/
VectorGPU(unsigned _size);

/*
* Used for filling the vector with a given value.
* If block size is passed 0 then the values are
* filled on the CPU otherwise they are filled on
* GPU.
*
* @param value The value with which the vector is
* to be populated.
* @param block_size The number of threads to be
* launched per block on GPU.
*/
void fill(data_type_vector value,
unsigned block_size=0);

/*
* Copies the data from GPU to CPU.
*/
void copy_to_host();

/*
* Copies the data from CPU to GPU.
*/
void copy_to_device();

/*
* Returns the size of the vector.
* By default returns the size of the
* vector on GPU.
*
* @param gpu If true then size of the
* vector on GPU otherwise size of the
* vector on CPU is returned.
*/
unsigned get_size(bool gpu=true) const;

/*
* Returns the data pointer, by default, of
* the vector on GPU.
*
* @param gpu If true then data pointer on GPU
* is returned otherwise the one on CPU
* is returned.
*/
data_type_vector* get_data_pointer(bool gpu=true) const;

/*
* Frees the memory from both CPU and GPU.
*/
~VectorGPU();
};

template <class data_type_vector>
void product_gpu(const VectorGPU<data_type_vector>& vec1,
const VectorGPU<data_type_vector>& vec2,
data_type_vector& result,
unsigned block_size=0);

template <class data_type_matrix>
class MatrixGPU: public adaboost::core::Matrix<data_type_matrix>
{
private:

data_type_matrix* data_gpu;

unsigned rows_gpu, cols_gpu;

static data_type_matrix* _reserve_space_gpu
(unsigned _rows_gpu, unsigned _cols_gpu);

public:

MatrixGPU();

MatrixGPU(unsigned _rows, unsigned _cols);

void fill(data_type_matrix value,
unsigned block_size_x=0,
unsigned block_size_y=0);

void copy_to_host();

void copy_to_device();

unsigned get_rows(bool gpu=true) const;

unsigned get_cols(bool gpu=true) const;

data_type_matrix* get_data_pointer(bool gpu=true) const;

~MatrixGPU();
};

template <class data_type_matrix>
void multiply_gpu(const MatrixGPU<data_type_matrix>& mat1,
const MatrixGPU<data_type_matrix>& mat2,
MatrixGPU<data_type_matrix>& result);

} // namespace core
} // namespace cuda
} // namespace adaboost

#endif
6 changes: 6 additions & 0 deletions adaboost/cuda/cuda_data_structures_impl.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CUDA_ADABOOST_CORE_DATA_STRUCTURES_IMPL_CU
#define CUDA_ADABOOST_CORE_DATA_STRUCTURES_IMPL_CU

#include "cuda_data_structures_impl.hpp"

#endif
Loading

0 comments on commit 9b6750a

Please sign in to comment.