Skip to content

Commit

Permalink
Apply clang-format change
Browse files Browse the repository at this point in the history
  • Loading branch information
OXPHOS committed Jun 5, 2017
1 parent 1372117 commit 60f81d8
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 164 deletions.
2 changes: 1 addition & 1 deletion src/shogun/mathematics/linalg/LinalgBackendBase.h
Expand Up @@ -521,7 +521,7 @@ class LinalgBackendBase
#undef DEFINE_FOR_ALL_PTYPE
#undef DEFINE_FOR_REAL_PTYPE
#undef DEFINE_FOR_NON_INTEGER_PTYPE
// clang-format on
// clang-format on
};

}
Expand Down
26 changes: 16 additions & 10 deletions src/shogun/mathematics/linalg/LinalgBackendEigen.h
Expand Up @@ -33,11 +33,11 @@
#ifndef LINALG_BACKEND_EIGEN_H__
#define LINALG_BACKEND_EIGEN_H__

#include <numeric>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/linalg/LinalgBackendBase.h>
#include <numeric>

namespace shogun
{
Expand Down Expand Up @@ -374,7 +374,7 @@ class LinalgBackendEigen : public LinalgBackendBase
#undef BACKEND_GENERIC_BLOCK_ROWWISE_SUM

#undef DEFINE_FOR_ALL_PTYPE
// clang-format on
// clang-format on
private:
/** Eigen3 vector result = alpha*A + beta*B method */
template <typename T>
Expand Down Expand Up @@ -455,7 +455,8 @@ class LinalgBackendEigen : public LinalgBackendBase
}

/** Eigen3 cross_entropy method
* The cross entropy is defined as \f$ H(P,Q) = - \sum_{ij} P[i,j]log(Q[i,j]) \f$
* The cross entropy is defined as \f$ H(P,Q) = - \sum_{ij}
* P[i,j]log(Q[i,j]) \f$
*/
template <typename T>
T cross_entropy_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const
Expand Down Expand Up @@ -578,22 +579,26 @@ class LinalgBackendEigen : public LinalgBackendBase
}

/** Eigen3 multiply_by_logistic_derivative method
* Performs the operation C(i,j) = C(i,j) * A(i,j) * (1.0-A(i,j)) for all i and j
* Performs the operation C(i,j) = C(i,j) * A(i,j) * (1.0-A(i,j)) for all i
* and j
*/
template <typename T>
void multiply_by_logistic_derivative_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;

result_eig = result_eig.array() * a_eig.array() * ((T)1 - a_eig.array());
result_eig =
result_eig.array() * a_eig.array() * ((T)1 - a_eig.array());
}

/** Eigen3 multiply_by_rectified_linear_derivative method
* Performs the operation C(i,j) = C(i,j) * (A(i,j)!=0) for all i and j
*/
template <typename T>
void multiply_by_rectified_linear_derivative_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;
Expand Down Expand Up @@ -666,7 +671,8 @@ class LinalgBackendEigen : public LinalgBackendBase
}

/** Eigen3 squared error method
* The squared error is defined as \f$ E(P,Q) = \frac{1}{2} \sum_{ij} (P[i,j]-Q[i,j])^2 \f$
* The squared error is defined as \f$ E(P,Q) = \frac{1}{2} \sum_{ij}
* (P[i,j]-Q[i,j])^2 \f$
*/
template <typename T>
T squared_error_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const
Expand Down Expand Up @@ -851,7 +857,7 @@ class LinalgBackendEigen : public LinalgBackendBase
#undef DEFINE_FOR_NON_INTEGER_PTYPE
#undef DEFINE_FOR_NON_INTEGER_REAL_PTYPE
#undef DEFINE_FOR_NUMERIC_PTYPE
// clang-format on
// clang-format on
};

}
Expand Down
6 changes: 3 additions & 3 deletions src/shogun/mathematics/linalg/LinalgBackendGPUBase.h
Expand Up @@ -49,8 +49,8 @@ namespace shogun
class LinalgBackendGPUBase : public LinalgBackendBase
{
public:
// clang-format off
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
// clang-format off
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(char, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
Expand Down Expand Up @@ -85,7 +85,7 @@ class LinalgBackendGPUBase : public LinalgBackendBase
#undef BACKEND_GENERIC_FROM_GPU

#undef DEFINE_FOR_ALL_PTYPE
// clang-format on
// clang-format on
};

}
Expand Down
150 changes: 89 additions & 61 deletions src/shogun/mathematics/linalg/LinalgBackendViennaCL.h
Expand Up @@ -281,7 +281,7 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
#undef BACKEND_GENERIC_FROM_GPU

#undef DEFINE_FOR_ALL_PTYPE
// clang-format on
// clang-format on
private:
/** Static cast @see GPUMemoryBase class to @see GPUMemoryViennaCL */
template <typename T, template<typename> class Container>
Expand Down Expand Up @@ -316,26 +316,31 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
}

/** ViennaCL cross_entropy method
* The cross entropy is defined as \f$ H(P,Q) = - \sum_{ij} P[i,j]log(Q[i,j]) \f$
* The cross entropy is defined as \f$ H(P,Q) = - \sum_{ij}
* P[i,j]log(Q[i,j]) \f$
*/
template <typename T>
T cross_entropy_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const
{
typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type aligned_t;
typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type
aligned_t;

GPUMemoryViennaCL<T>* p_gpu = cast_to_viennacl(p);
GPUMemoryViennaCL<T>* q_gpu = cast_to_viennacl(q);
GPUMemoryViennaCL<T>* result_gpu = new GPUMemoryViennaCL<T>(1);

viennacl::ocl::kernel& kernel = generate_cross_entropy_kernel<T>();
viennacl::ocl::enqueue(kernel(p_gpu->data_matrix(p.num_rows, p.num_cols),
cl_int(p.num_rows*p.num_cols), cl_int(p_gpu->m_offset),
q_gpu->data_matrix(q.num_rows, q.num_cols), cl_int(q_gpu->m_offset),
result_gpu->data_vector(1)));
viennacl::ocl::enqueue(
kernel(
p_gpu->data_matrix(p.num_rows, p.num_cols),
cl_int(p.num_rows * p.num_cols), cl_int(p_gpu->m_offset),
q_gpu->data_matrix(q.num_rows, q.num_cols), cl_int(q_gpu->m_offset),
result_gpu->data_vector(1)));

T* result = reinterpret_cast<T*>(SG_MALLOC(aligned_t, 1));
viennacl::backend::memory_read(*(result_gpu->m_data),
result_gpu->m_offset*sizeof(T), sizeof(T), result);
viennacl::backend::memory_read(
*(result_gpu->m_data), result_gpu->m_offset * sizeof(T), sizeof(T),
result);

return result[0];
}
Expand Down Expand Up @@ -468,66 +473,77 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
}

/** ViennaCL multiply_by_logistic_derivative method
* Performs the operation C(i,j) = C(i,j) * A(i,j) * (1.0-A(i,j) for all i and j
* Performs the operation C(i,j) = C(i,j) * A(i,j) * (1.0-A(i,j) for all i
* and j
*/
template <typename T>
void multiply_by_logistic_derivative_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);

const std::string operation = "return element2 * element1*(1.0-element1);";
const std::string operation =
"return element2 * element1*(1.0-element1);";

std::string kernel_name = "multiply_by_logistic_derivative_" +
std::string kernel_name =
"multiply_by_logistic_derivative_" +
linalg::implementation::ocl::get_type_string<T>();
viennacl::ocl::kernel& kernel = linalg::implementation::
ocl::generate_two_arg_elementwise_kernel<T>(kernel_name, operation);
viennacl::ocl::kernel& kernel =
linalg::implementation::ocl::generate_two_arg_elementwise_kernel<T>(
kernel_name, operation);

kernel.global_work_size(0,
linalg::implementation::ocl::align_to_multiple_1d(a.num_rows*a.num_cols));
kernel.global_work_size(
0, linalg::implementation::ocl::align_to_multiple_1d(
a.num_rows * a.num_cols));

viennacl::ocl::enqueue(kernel(
a_gpu->data_matrix(a.num_rows, a.num_cols), cl_int(a.num_rows*a.num_cols),
cl_int(a_gpu->m_offset),
a_gpu->data_matrix(a.num_rows, a.num_cols),
cl_int(a.num_rows * a.num_cols), cl_int(a_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset)));

result.gpu_ptr = std::shared_ptr<GPUMemoryBase<T>>(
result_gpu->clone_vector(result_gpu,a.num_rows*a.num_cols));
result_gpu->clone_vector(result_gpu, a.num_rows * a.num_cols));
}

/** ViennaCL multiply_by_rectified_linear_derivative method
* Performs the operation C(i,j) = C(i,j) * (A(i,j)!=0) for all i and j
*/
template <typename T>
void multiply_by_rectified_linear_derivative_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);

const std::string operation = "return element1==0 ? 0 : element2;";

std::string kernel_name = "multiply_by_rectified_linear_derivative_" +
std::string kernel_name =
"multiply_by_rectified_linear_derivative_" +
linalg::implementation::ocl::get_type_string<T>();
viennacl::ocl::kernel& kernel = linalg::implementation::
ocl::generate_two_arg_elementwise_kernel<T>(kernel_name, operation);

kernel.global_work_size(0,
linalg::implementation::ocl::align_to_multiple_1d(a.num_rows*a.num_cols));

viennacl::ocl::enqueue(kernel(
a_gpu->data_matrix(a.num_rows, a.num_cols), cl_int(a.num_rows*a.num_cols),
cl_int(a_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset)));
viennacl::ocl::kernel& kernel =
linalg::implementation::ocl::generate_two_arg_elementwise_kernel<T>(
kernel_name, operation);

kernel.global_work_size(
0, linalg::implementation::ocl::align_to_multiple_1d(
a.num_rows * a.num_cols));

viennacl::ocl::enqueue(
kernel(
a_gpu->data_matrix(a.num_rows, a.num_cols),
cl_int(a.num_rows * a.num_cols), cl_int(a_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset)));

result.gpu_ptr = std::shared_ptr<GPUMemoryBase<T>>(
result_gpu->clone_vector(result_gpu,a.num_rows*a.num_cols));
}
result_gpu->clone_vector(result_gpu, a.num_rows * a.num_cols));
}

/** Applies the elementwise rectified linear function f(x) = max(0,x) */
template <typename T>
Expand All @@ -538,21 +554,26 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase

const std::string operation = "return max((DATATYPE)0,element);";

std::string kernel_name = "rectified_linear_" +
std::string kernel_name =
"rectified_linear_" +
linalg::implementation::ocl::get_type_string<T>();
viennacl::ocl::kernel& kernel = linalg::implementation::
ocl::generate_single_arg_elementwise_kernel<T>(kernel_name, operation);
viennacl::ocl::kernel& kernel =
linalg::implementation::ocl::generate_single_arg_elementwise_kernel<
T>(kernel_name, operation);

kernel.global_work_size(0,
linalg::implementation::ocl::align_to_multiple_1d(a.num_rows*a.num_cols));
kernel.global_work_size(
0, linalg::implementation::ocl::align_to_multiple_1d(
a.num_rows * a.num_cols));

viennacl::ocl::enqueue(kernel(a_gpu->data_matrix(a.num_rows, a.num_cols),
cl_int(a.num_rows*a.num_cols), cl_int(a_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset)));
viennacl::ocl::enqueue(
kernel(
a_gpu->data_matrix(a.num_rows, a.num_cols),
cl_int(a.num_rows * a.num_cols), cl_int(a_gpu->m_offset),
result_gpu->data_matrix(result.num_rows, result.num_cols),
cl_int(result_gpu->m_offset)));

result.gpu_ptr = std::shared_ptr<GPUMemoryBase<T>>(
result_gpu->clone_vector(result_gpu,a.num_rows*a.num_cols));
result_gpu->clone_vector(result_gpu, a.num_rows * a.num_cols));
}

/** ViennaCL vector inplace scale method: result = alpha * A */
Expand Down Expand Up @@ -593,37 +614,44 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);

viennacl::ocl::kernel& kernel = generate_softmax_kernel<T>();
kernel.global_work_size(0, linalg::implementation::
ocl::align_to_multiple_1d(a.num_cols));
kernel.global_work_size(
0, linalg::implementation::ocl::align_to_multiple_1d(a.num_cols));

viennacl::ocl::enqueue(kernel(a_gpu->data_matrix(a.num_rows, a.num_cols),
cl_int(a.num_rows), cl_int(a.num_cols), cl_int(a_gpu->m_offset)));
viennacl::ocl::enqueue(
kernel(
a_gpu->data_matrix(a.num_rows, a.num_cols), cl_int(a.num_rows),
cl_int(a.num_cols), cl_int(a_gpu->m_offset)));

a.gpu_ptr = std::shared_ptr<GPUMemoryBase<T>>(
a_gpu->clone_vector(a_gpu,a.num_rows*a.num_cols));
a_gpu->clone_vector(a_gpu, a.num_rows * a.num_cols));
}

/** ViennaCL squared error method
* The squared error is defined as \f$ E(P,Q) = \frac{1}{2} \sum_{ij} (P[i,j]-Q[i,j])^2 \f$
* The squared error is defined as \f$ E(P,Q) = \frac{1}{2} \sum_{ij}
* (P[i,j]-Q[i,j])^2 \f$
*/
template <typename T>
T squared_error_impl(const SGMatrix<T>& p, const SGMatrix<T>& q) const
{
typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type aligned_t;
typedef typename std::aligned_storage<sizeof(T), alignof(T)>::type
aligned_t;

GPUMemoryViennaCL<T>* p_gpu = cast_to_viennacl(p);
GPUMemoryViennaCL<T>* q_gpu = cast_to_viennacl(q);
GPUMemoryViennaCL<T>* result_gpu = new GPUMemoryViennaCL<T>(1);

viennacl::ocl::kernel& kernel = generate_squared_error_kernel<T>();
viennacl::ocl::enqueue(kernel(p_gpu->data_matrix(p.num_rows, p.num_cols),
cl_int(p.num_rows*p.num_cols), cl_int(p_gpu->m_offset),
q_gpu->data_matrix(q.num_rows, q.num_cols), cl_int(q_gpu->m_offset),
result_gpu->data_vector(1)));
viennacl::ocl::enqueue(
kernel(
p_gpu->data_matrix(p.num_rows, p.num_cols),
cl_int(p.num_rows * p.num_cols), cl_int(p_gpu->m_offset),
q_gpu->data_matrix(q.num_rows, q.num_cols), cl_int(q_gpu->m_offset),
result_gpu->data_vector(1)));

T* result = reinterpret_cast<T*>(SG_MALLOC(aligned_t, 1));
viennacl::backend::memory_read(*(result_gpu->m_data),
result_gpu->m_offset*sizeof(T), sizeof(T), result);
viennacl::backend::memory_read(
*(result_gpu->m_data), result_gpu->m_offset * sizeof(T), sizeof(T),
result);

return result[0];
}
Expand Down Expand Up @@ -726,7 +754,7 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
// clang-format off
#undef DEFINE_FOR_ALL_PTYPE
#undef DEFINE_FOR_NON_INTEGER_PTYPE
// clang-format on
// clang-format on
};

}
Expand Down

0 comments on commit 60f81d8

Please sign in to comment.