Skip to content

Commit

Permalink
first CPU version
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiashuai committed Nov 13, 2017
1 parent aedac40 commit 09fd02a
Show file tree
Hide file tree
Showing 47 changed files with 557 additions and 498 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
.idea
.git
.*
!.gitignore
!.gitmodules
build
dataset/*
!dataset/*.sh
!dataset/test_dataset.txt
*tags
19 changes: 12 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ cmake_minimum_required(VERSION 2.8.12)
set(PROJECT_NAME_STR thundersvm)
project(${PROJECT_NAME_STR} C CXX)

set(USE_CUDA OFF CACHE BOOL "Compile with CUDA")
set(USE_CUDA ON CACHE BOOL "Compile with CUDA")
set(USE_EIGEN OFF CACHE BOOL "Compile with Eigen")

find_package(Threads REQUIRED)
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

#find_package(MKL REQUIRED)
#include_directories(${MKL_INCLUDE_DIRS})
Expand All @@ -26,8 +22,17 @@ if (USE_CUDA)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -std=c++11 -Wno-deprecated-gpu-targets)
set(LINK_LIBRARY ${CUDA_cusparse_LIBRARY})
else ()
include_directories(${PROJECT_SOURCE_DIR}/eigen)
message("Compile without CUDA")
find_package(OpenMP)
if (OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif ()
endif ()

if (USE_EIGEN)
message("using Eigen for acceleration")
include_directories(${PROJECT_SOURCE_DIR}/eigen)
endif ()


Expand Down
3 changes: 3 additions & 0 deletions include/thundersvm/clion_cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#ifndef THUNDERSVM_CLION_CUDA_H
#define THUNDERSVM_CLION_CUDA_H

#include <config.h>
#ifdef __JETBRAINS_IDE__
#ifdef USE_CUDA
#include "math.h"
#define __CUDACC__ 1
#define __host__
Expand Down Expand Up @@ -281,4 +283,5 @@ extern dim3 blockDim;
extern uint3 threadIdx;
extern int warpsize;
#endif
#endif
#endif //THUNDERSVM_CLION_CUDA_H
1 change: 1 addition & 0 deletions include/thundersvm/config.h.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#cmakedefine DATASET_DIR "@DATASET_DIR@"
#cmakedefine USE_CUDA
#cmakedefine USE_EIGEN
10 changes: 5 additions & 5 deletions include/thundersvm/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class DataSet {
public:
struct node{
node(int index, real value):index(index), value(value){}
node(int index, float_type value) : index(index), value(value) {}
int index;
real value;
float_type value;
};
typedef vector<vector<DataSet::node>> node2d;

DataSet();

DataSet(const DataSet::node2d &instances, int n_features, const vector<real> &y);
DataSet(const DataSet::node2d &instances, int n_features, const vector<float_type> &y);
void load_from_file(string file_name);

void group_classes(bool classification = true);
Expand All @@ -32,7 +32,7 @@ class DataSet {

const vector<int> &label() const;

const vector<real> &y() const;
const vector<float_type> &y() const;
const node2d & instances() const;
const node2d instances(int y_i) const;
const node2d instances(int y_i, int y_j) const;
Expand All @@ -41,7 +41,7 @@ class DataSet {
const vector<int> original_index(int y_i, int y_j) const;

private:
vector<real> y_;
vector<float_type> y_;
node2d instances_;
size_t total_count_;
size_t n_features_;
Expand Down
30 changes: 17 additions & 13 deletions include/thundersvm/kernel/kernelmatrix_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@
#include <thundersvm/syncdata.h>

namespace svm_kernel {
void get_working_set_ins(const SyncData<real> &val, const SyncData<int> &col_ind, const SyncData<int> &row_ptr,
const SyncData<int> &data_row_idx, SyncData<real> &data_rows, int m);
void
get_working_set_ins(const SyncData<float_type> &val, const SyncData<int> &col_ind, const SyncData<int> &row_ptr,
const SyncData<int> &data_row_idx, SyncData<float_type> &data_rows, int m);

void
RBF_kernel(const SyncData<real> &self_dot0, const SyncData<real> &self_dot1, SyncData<real> &dot_product, int m,
RBF_kernel(const SyncData<float_type> &self_dot0, const SyncData<float_type> &self_dot1,
SyncData<float_type> &dot_product, int m,
int n,
real gamma);
float_type gamma);

void
RBF_kernel(const SyncData<int> &self_dot0_idx, const SyncData<real> &self_dot1, SyncData<real> &dot_product, int m,
int n, real gamma);
RBF_kernel(const SyncData<int> &self_dot0_idx, const SyncData<float_type> &self_dot1,
SyncData<float_type> &dot_product, int m,
int n, float_type gamma);

void poly_kernel(SyncData<real> &dot_product, real gamma, real coef0, int degree, int mn);
void poly_kernel(SyncData<float_type> &dot_product, float_type gamma, float_type coef0, int degree, int mn);

void sigmoid_kernel(SyncData<real> &dot_product, real gamma, real coef0, int mn);
void sigmoid_kernel(SyncData<float_type> &dot_product, float_type gamma, float_type coef0, int mn);

void sum_kernel_values(const SyncData<real> &coef, int total_sv, const SyncData<int> &sv_start,
const SyncData<int> &sv_count, const SyncData<real> &rho, const SyncData<real> &k_mat,
SyncData<real> &dec_values, int n_classes, int n_instances);
void sum_kernel_values(const SyncData<float_type> &coef, int total_sv, const SyncData<int> &sv_start,
const SyncData<int> &sv_count, const SyncData<float_type> &rho,
const SyncData<float_type> &k_mat,
SyncData<float_type> &dec_values, int n_classes, int n_instances);

void dns_csr_mul(int m, int n, int k, const SyncData<real> &dense_mat, const SyncData<real> &csr_val,
void dns_csr_mul(int m, int n, int k, const SyncData<float_type> &dense_mat, const SyncData<float_type> &csr_val,
const SyncData<int> &csr_row_ptr, const SyncData<int> &csr_col_ind, int nnz,
SyncData<real> &result);
SyncData<float_type> &result);
}
#endif //THUNDERSVM_KERNELMATRIX_KERNEL_H

21 changes: 13 additions & 8 deletions include/thundersvm/kernel/smo_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ namespace svm_kernel {
}

void
c_smo_solve(const SyncData<int> &y, SyncData<real> &f_val, SyncData<real> &alpha, SyncData<real> &alpha_diff,
const SyncData<int> &working_set, real Cp, real Cn, const SyncData<real> &k_mat_rows,
const SyncData<real> &k_mat_diag, int row_len, real eps, SyncData<real> &diff, int max_iter);
c_smo_solve(const SyncData<int> &y, SyncData<float_type> &f_val, SyncData<float_type> &alpha,
SyncData<float_type> &alpha_diff,
const SyncData<int> &working_set, float_type Cp, float_type Cn, const SyncData<float_type> &k_mat_rows,
const SyncData<float_type> &k_mat_diag, int row_len, float_type eps, SyncData<float_type> &diff,
int max_iter);

void
nu_smo_solve(const SyncData<int> &y, SyncData<real> &f_val, SyncData<real> &alpha, SyncData<real> &alpha_diff,
const SyncData<int> &working_set, real C, const SyncData<real> &k_mat_rows,
const SyncData<real> &k_mat_diag, int row_len, real eps, SyncData<real> &diff, int max_iter);
nu_smo_solve(const SyncData<int> &y, SyncData<float_type> &f_val, SyncData<float_type> &alpha,
SyncData<float_type> &alpha_diff,
const SyncData<int> &working_set, float_type C, const SyncData<float_type> &k_mat_rows,
const SyncData<float_type> &k_mat_diag, int row_len, float_type eps, SyncData<float_type> &diff,
int max_iter);

void
update_f(SyncData<real> &f, const SyncData<real> &alpha_diff, const SyncData<real> &k_mat_rows, int n_instances);
update_f(SyncData<float_type> &f, const SyncData<float_type> &alpha_diff, const SyncData<float_type> &k_mat_rows,
int n_instances);

void sort_f(SyncData<real> &f_val2sort, SyncData<int> &f_idx2sort);
void sort_f(SyncData<float_type> &f_val2sort, SyncData<int> &f_idx2sort);
}

#endif //THUNDERSVM_SMO_KERNEL_H
20 changes: 11 additions & 9 deletions include/thundersvm/kernelmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
class KernelMatrix{
public:
explicit KernelMatrix(const DataSet::node2d &instances, SvmParam param);
void get_rows(const SyncData<int> &idx, SyncData<real> &kernel_rows) const;
void get_rows(const DataSet::node2d &instances, SyncData<real> &kernel_rows) const;

const SyncData<real> &diag() const;
void get_rows(const SyncData<int> &idx, SyncData<float_type> &kernel_rows) const;

void get_rows(const DataSet::node2d &instances, SyncData<float_type> &kernel_rows) const;

const SyncData<float_type> &diag() const;

size_t n_instances() const { return n_instances_; };//number of instances
size_t n_features() const { return n_features_; };//number of features
Expand All @@ -26,20 +28,20 @@ class KernelMatrix{

KernelMatrix(const KernelMatrix &);

SyncData<real> val_;
SyncData<float_type> val_;
SyncData<int> col_ind_;
SyncData<int> row_ptr_;
SyncData<real> diag_;
SyncData<real> self_dot_;
SyncData<float_type> diag_;
SyncData<float_type> self_dot_;
size_t nnz_;
size_t n_instances_;
size_t n_features_;
SvmParam param;

void dns_csr_mul(const SyncData<real> &dense_mat, int n_rows, SyncData<real> &result) const;
void dns_csr_mul(const SyncData<float_type> &dense_mat, int n_rows, SyncData<float_type> &result) const;

void get_dot_product(const SyncData<int> &idx, SyncData<real> &dot_product) const;
void get_dot_product(const SyncData<int> &idx, SyncData<float_type> &dot_product) const;

void get_dot_product(const DataSet::node2d &instances, SyncData<real> &dot_product) const;
void get_dot_product(const DataSet::node2d &instances, SyncData<float_type> &dot_product) const;
};
#endif //THUNDERSVM_KERNELMATRIX_H
20 changes: 0 additions & 20 deletions include/thundersvm/kernelmatrix_kernel_openmp.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/thundersvm/model/nusvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class NuSVC : public SVC {
protected:
void train_binary(const DataSet &dataset, int i, int j, SyncData<real> &alpha, real &rho) override;
void train_binary(const DataSet &dataset, int i, int j, SyncData<float_type> &alpha, float_type &rho) override;
};

#endif //THUNDERSVM_NUSVC_H
2 changes: 1 addition & 1 deletion include/thundersvm/model/oneclass_svc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OneClassSVC : public SvmModel {
public:
void train(const DataSet &dataset, SvmParam param) override;

vector<real> predict(const DataSet::node2d &instances, int batch_size) override;
vector<float_type> predict(const DataSet::node2d &instances, int batch_size) override;

};

Expand Down
10 changes: 5 additions & 5 deletions include/thundersvm/model/svc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ class SVC : public SvmModel {

void train(const DataSet &dataset, SvmParam param) override;

vector<real> predict(const DataSet::node2d &instances, int batch_size) override;
vector<float_type> predict(const DataSet::node2d &instances, int batch_size) override;

protected:
virtual void train_binary(const DataSet &dataset, int i, int j, SyncData<real> &alpha, real &rho);
virtual void train_binary(const DataSet &dataset, int i, int j, SyncData<float_type> &alpha, float_type &rho);

void model_setup(const DataSet &dataset, SvmParam &param) override;

private:

vector<real> predict_label(const SyncData<real> &dec_values, int n_instances) const;
vector<float_type> predict_label(const SyncData<float_type> &dec_values, int n_instances) const;

void probability_train(const DataSet &dataset);

void multiclass_probability(const vector<vector<real>> &r, vector<real> &p) const;
void multiclass_probability(const vector<vector<float_type>> &r, vector<float_type> &p) const;

vector<real> c_weight;
vector<float_type> c_weight;


};
Expand Down
14 changes: 7 additions & 7 deletions include/thundersvm/model/svmmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class SvmModel {
public:
virtual void train(const DataSet &dataset, SvmParam param) = 0;

virtual vector<real> predict(const DataSet::node2d &instances, int batch_size);
virtual vector<float_type> predict(const DataSet::node2d &instances, int batch_size);

virtual vector<real> cross_validation(DataSet dataset, SvmParam param, int n_fold);
virtual vector<float_type> cross_validation(DataSet dataset, SvmParam param, int n_fold);

virtual void save_to_file(string path);

Expand All @@ -28,18 +28,18 @@ class SvmModel {

virtual void model_setup(const DataSet &dataset, SvmParam &param);

void predict_dec_values(const DataSet::node2d &instances, SyncData<real> &dec_values, int batch_size) const;
void predict_dec_values(const DataSet::node2d &instances, SyncData<float_type> &dec_values, int batch_size) const;

SvmParam param;
SyncData<real> coef;
SyncData<float_type> coef;
DataSet::node2d sv;
SyncData<int> n_sv;//the number of sv in each class
SyncData<real> rho;
SyncData<float_type> rho;
int n_classes = 2;
size_t n_binary_models;
int n_total_sv;
vector<real> probA;
vector<real> probB;
vector<float_type> probA;
vector<float_type> probB;

//for classification
vector<int> label;
Expand Down
2 changes: 1 addition & 1 deletion include/thundersvm/model/svr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SVR : public SvmModel {
void train(const DataSet &dataset, SvmParam param) override;

protected:
void save_svr_coef(const SyncData<real> &alpha_2, const DataSet::node2d &instances);
void save_svr_coef(const SyncData<float_type> &alpha_2, const DataSet::node2d &instances);
};

#endif //THUNDERSVM_SVR_H
24 changes: 14 additions & 10 deletions include/thundersvm/solver/csmosolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@

class CSMOSolver {
public:
void solve(const KernelMatrix &k_mat, const SyncData<int> &y, SyncData<real> &alpha, real &rho,
SyncData<real> &f_val, real eps, real Cp, real Cn, int ws_size) const;
void solve(const KernelMatrix &k_mat, const SyncData<int> &y, SyncData<float_type> &alpha, float_type &rho,
SyncData<float_type> &f_val, float_type eps, float_type Cp, float_type Cn, int ws_size) const;

protected:
void init_f(const SyncData<real> &alpha, const SyncData<int> &y, const KernelMatrix &k_mat,
SyncData<real> &f_val) const;
void init_f(const SyncData<float_type> &alpha, const SyncData<int> &y, const KernelMatrix &k_mat,
SyncData<float_type> &f_val) const;

virtual void select_working_set(vector<int> &ws_indicator, const SyncData<int> &f_idx2sort, const SyncData<int> &y,
const SyncData<real> &alpha, real Cp, real Cn, SyncData<int> &working_set) const;
const SyncData<float_type> &alpha, float_type Cp, float_type Cn,
SyncData<int> &working_set) const;

virtual real
calculate_rho(const SyncData<real> &f_val, const SyncData<int> &y, SyncData<real> &alpha, real Cp, real Cn) const;
virtual float_type
calculate_rho(const SyncData<float_type> &f_val, const SyncData<int> &y, SyncData<float_type> &alpha, float_type Cp,
float_type Cn) const;

virtual void
smo_kernel(const SyncData<int> &y, SyncData<real> &f_val, SyncData<real> &alpha, SyncData<real> &alpha_diff,
const SyncData<int> &working_set, real Cp, real Cn, const SyncData<real> &k_mat_rows,
const SyncData<real> &k_mat_diag, int row_len, real eps, SyncData<real> &diff, int max_iter) const;
smo_kernel(const SyncData<int> &y, SyncData<float_type> &f_val, SyncData<float_type> &alpha,
SyncData<float_type> &alpha_diff,
const SyncData<int> &working_set, float_type Cp, float_type Cn, const SyncData<float_type> &k_mat_rows,
const SyncData<float_type> &k_mat_diag, int row_len, float_type eps, SyncData<float_type> &diff,
int max_iter) const;
};

#endif //THUNDERSVM_CSMOSOLVER_H

0 comments on commit 09fd02a

Please sign in to comment.