Skip to content

Commit

Permalink
add DEFINE_FOR_ALL_PTYPE macro
Browse files Browse the repository at this point in the history
  • Loading branch information
OXPHOS committed Jul 11, 2016
1 parent 79c0037 commit 670fa14
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 137 deletions.
93 changes: 29 additions & 64 deletions src/shogun/mathematics/linalg/LinalgBackendBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,48 @@ namespace shogun
class LinalgBackendBase
{
public:
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(bool, Container); \
METHODNAME(char, Container); \
METHODNAME(int8_t, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
METHODNAME(uint16_t, Container); \
METHODNAME(int32_t, Container); \
METHODNAME(uint32_t, Container); \
METHODNAME(int64_t, Container); \
METHODNAME(uint64_t, Container); \
METHODNAME(float32_t, Container); \
METHODNAME(float64_t, Container); \
METHODNAME(floatmax_t, Container); \
METHODNAME(complex128_t, Container); \

/**
* Wrapper method of add operation the operation C = alpha*A + beta*B.
*
* @see linalg::add
*/
#define BACKEND_GENERIC_ADD(Type) \
virtual SGVector<Type> add(const SGVector<Type>& a, const SGVector<Type>& b, Type alpha, Type beta) const \
#define BACKEND_GENERIC_ADD(Type, Container) \
virtual Container<Type> add(const Container<Type>& a, const Container<Type>& b, Type alpha, Type beta) const \
{ \
SG_SNOTIMPLEMENTED; \
}

BACKEND_GENERIC_ADD(bool);
BACKEND_GENERIC_ADD(char);
BACKEND_GENERIC_ADD(int8_t);
BACKEND_GENERIC_ADD(uint8_t);
BACKEND_GENERIC_ADD(int16_t);
BACKEND_GENERIC_ADD(uint16_t);
BACKEND_GENERIC_ADD(int32_t);
BACKEND_GENERIC_ADD(uint32_t);
BACKEND_GENERIC_ADD(int64_t);
BACKEND_GENERIC_ADD(uint64_t);
BACKEND_GENERIC_ADD(float32_t);
BACKEND_GENERIC_ADD(float64_t);
BACKEND_GENERIC_ADD(floatmax_t);
BACKEND_GENERIC_ADD(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD, SGVector)
#undef BACKEND_GENERIC_ADD

/**
* Wrapper method of vector dot-product that works with generic vectors.
*
* @see linalg::dot
*/
#define BACKEND_GENERIC_DOT(Type) \
virtual Type dot(const SGVector<Type>& a, const SGVector<Type>& b) const \
#define BACKEND_GENERIC_DOT(Type, Container) \
virtual Type dot(const Container<Type>& a, const Container<Type>& b) const \
{ \
SG_SNOTIMPLEMENTED; \
}

BACKEND_GENERIC_DOT(bool);
BACKEND_GENERIC_DOT(char);
BACKEND_GENERIC_DOT(int8_t);
BACKEND_GENERIC_DOT(uint8_t);
BACKEND_GENERIC_DOT(int16_t);
BACKEND_GENERIC_DOT(uint16_t);
BACKEND_GENERIC_DOT(int32_t);
BACKEND_GENERIC_DOT(uint32_t);
BACKEND_GENERIC_DOT(int64_t);
BACKEND_GENERIC_DOT(uint64_t);
BACKEND_GENERIC_DOT(float32_t);
BACKEND_GENERIC_DOT(float64_t);
BACKEND_GENERIC_DOT(floatmax_t);
BACKEND_GENERIC_DOT(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_DOT, SGVector)
#undef BACKEND_GENERIC_DOT

/**
Expand All @@ -109,55 +99,30 @@ class LinalgBackendBase
*
* @see linalg::to_gpu
*/
#define BACKEND_GENERIC_TO_GPU(Type) \
virtual GPUMemoryBase<Type>* to_gpu(const SGVector<Type>&) const \
#define BACKEND_GENERIC_TO_GPU(Type, Container) \
virtual GPUMemoryBase<Type>* to_gpu(const Container<Type>&) const \
{ \
SG_SNOTIMPLEMENTED; \
}

BACKEND_GENERIC_TO_GPU(bool);
BACKEND_GENERIC_TO_GPU(char);
BACKEND_GENERIC_TO_GPU(int8_t);
BACKEND_GENERIC_TO_GPU(uint8_t);
BACKEND_GENERIC_TO_GPU(int16_t);
BACKEND_GENERIC_TO_GPU(uint16_t);
BACKEND_GENERIC_TO_GPU(int32_t);
BACKEND_GENERIC_TO_GPU(uint32_t);
BACKEND_GENERIC_TO_GPU(int64_t);
BACKEND_GENERIC_TO_GPU(uint64_t);
BACKEND_GENERIC_TO_GPU(float32_t);
BACKEND_GENERIC_TO_GPU(float64_t);
BACKEND_GENERIC_TO_GPU(floatmax_t);
BACKEND_GENERIC_TO_GPU(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGVector)
#undef BACKEND_GENERIC_TO_GPU

/**
* Wrapper method of fetching data from GPU memory.
*
* @see linalg::from_gpu
*/
#define BACKEND_GENERIC_FROM_GPU(Type) \
virtual void from_gpu(const SGVector<Type>&, Type* data) const \
#define BACKEND_GENERIC_FROM_GPU(Type, Container) \
virtual void from_gpu(const Container<Type>&, Type* data) const \
{ \
SG_SNOTIMPLEMENTED; \
}

BACKEND_GENERIC_FROM_GPU(bool);
BACKEND_GENERIC_FROM_GPU(char);
BACKEND_GENERIC_FROM_GPU(int8_t);
BACKEND_GENERIC_FROM_GPU(uint8_t);
BACKEND_GENERIC_FROM_GPU(int16_t);
BACKEND_GENERIC_FROM_GPU(uint16_t);
BACKEND_GENERIC_FROM_GPU(int32_t);
BACKEND_GENERIC_FROM_GPU(uint32_t);
BACKEND_GENERIC_FROM_GPU(int64_t);
BACKEND_GENERIC_FROM_GPU(uint64_t);
BACKEND_GENERIC_FROM_GPU(float32_t);
BACKEND_GENERIC_FROM_GPU(float64_t);
BACKEND_GENERIC_FROM_GPU(floatmax_t);
BACKEND_GENERIC_FROM_GPU(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGVector)
#undef BACKEND_GENERIC_FROM_GPU

#undef DEFINE_FOR_ALL_PTYPE
};

}
Expand Down
56 changes: 24 additions & 32 deletions src/shogun/mathematics/linalg/LinalgBackendEigen.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,44 @@ namespace shogun
class LinalgBackendEigen : public LinalgBackendBase
{
public:
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(bool, Container); \
METHODNAME(char, Container); \
METHODNAME(int8_t, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
METHODNAME(uint16_t, Container); \
METHODNAME(int32_t, Container); \
METHODNAME(uint32_t, Container); \
METHODNAME(int64_t, Container); \
METHODNAME(uint64_t, Container); \
METHODNAME(float32_t, Container); \
METHODNAME(float64_t, Container); \
METHODNAME(floatmax_t, Container); \
METHODNAME(complex128_t, Container); \

/** Implementation of @see LinalgBackendBase::add */
#define BACKEND_GENERIC_ADD(Type) \
virtual SGVector<Type> add(const SGVector<Type>& a, const SGVector<Type>& b, Type alpha, Type beta) const \
#define BACKEND_GENERIC_ADD(Type, Container) \
virtual Container<Type> add(const Container<Type>& a, const Container<Type>& b, Type alpha, Type beta) const \
{ \
return add_impl(a, b, alpha, beta); \
}

BACKEND_GENERIC_ADD(bool);
BACKEND_GENERIC_ADD(char);
BACKEND_GENERIC_ADD(int8_t);
BACKEND_GENERIC_ADD(uint8_t);
BACKEND_GENERIC_ADD(int16_t);
BACKEND_GENERIC_ADD(uint16_t);
BACKEND_GENERIC_ADD(int32_t);
BACKEND_GENERIC_ADD(uint32_t);
BACKEND_GENERIC_ADD(int64_t);
BACKEND_GENERIC_ADD(uint64_t);
BACKEND_GENERIC_ADD(float32_t);
BACKEND_GENERIC_ADD(float64_t);
BACKEND_GENERIC_ADD(floatmax_t);
BACKEND_GENERIC_ADD(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD, SGVector)
#undef BACKEND_GENERIC_ADD

/** Implementation of @see LinalgBackendBase::dot */
#define BACKEND_GENERIC_DOT(Type) \
virtual Type dot(const SGVector<Type>& a, const SGVector<Type>& b) const \
#define BACKEND_GENERIC_DOT(Type, Container) \
virtual Type dot(const Container<Type>& a, const Container<Type>& b) const \
{ \
return dot_impl(a, b); \
}

BACKEND_GENERIC_DOT(bool);
BACKEND_GENERIC_DOT(char);
BACKEND_GENERIC_DOT(int8_t);
BACKEND_GENERIC_DOT(uint8_t);
BACKEND_GENERIC_DOT(int16_t);
BACKEND_GENERIC_DOT(uint16_t);
BACKEND_GENERIC_DOT(int32_t);
BACKEND_GENERIC_DOT(uint32_t);
BACKEND_GENERIC_DOT(int64_t);
BACKEND_GENERIC_DOT(uint64_t);
BACKEND_GENERIC_DOT(float32_t);
BACKEND_GENERIC_DOT(float64_t);
BACKEND_GENERIC_DOT(floatmax_t);
BACKEND_GENERIC_DOT(complex128_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_DOT, SGVector)
#undef BACKEND_GENERIC_DOT

#undef DEFINE_FOR_ALL_PTYPE

private:
/** Eigen3 vector C = alpha*A + beta*B method */
template <typename T>
Expand Down
66 changes: 25 additions & 41 deletions src/shogun/mathematics/linalg/LinalgBackendViennacl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,74 +53,58 @@ class LinalgBackendViennaCL : public LinalgBackendGPUBase
friend struct GPUMemoryViennaCL;

public:
#define DEFINE_FOR_ALL_PTYPE(METHODNAME, Container) \
METHODNAME(char, Container); \
METHODNAME(uint8_t, Container); \
METHODNAME(int16_t, Container); \
METHODNAME(uint16_t, Container); \
METHODNAME(int32_t, Container); \
METHODNAME(uint32_t, Container); \
METHODNAME(float32_t, Container); \
METHODNAME(float64_t, Container); \

/** Implementation of @see LinalgBackendBase::add */
#define BACKEND_GENERIC_ADD(Type) \
virtual SGVector<Type> add(const SGVector<Type>& a, const SGVector<Type>& b, Type alpha, Type beta) const \
{ \
#define BACKEND_GENERIC_ADD(Type, Container) \
virtual Container<Type> add(const Container<Type>& a, const Container<Type>& b, Type alpha, Type beta) const \
{ \
return add_impl(a, b, alpha, beta); \
}

BACKEND_GENERIC_ADD(char);
BACKEND_GENERIC_ADD(uint8_t);
BACKEND_GENERIC_ADD(int16_t);
BACKEND_GENERIC_ADD(uint16_t);
BACKEND_GENERIC_ADD(int32_t);
BACKEND_GENERIC_ADD(uint32_t);
BACKEND_GENERIC_ADD(float32_t);
BACKEND_GENERIC_ADD(float64_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD, SGVector)
#undef BACKEND_GENERIC_ADD

/** Implementation of @see LinalgBackendBase::dot */
#define BACKEND_GENERIC_DOT(Type) \
virtual Type dot(const SGVector<Type>& a, const SGVector<Type>& b) const \
#define BACKEND_GENERIC_DOT(Type, Container) \
virtual Type dot(const Container<Type>& a, const Container<Type>& b) const \
{ \
return dot_impl(a, b); \
}

BACKEND_GENERIC_DOT(char);
BACKEND_GENERIC_DOT(uint8_t);
BACKEND_GENERIC_DOT(int16_t);
BACKEND_GENERIC_DOT(uint16_t);
BACKEND_GENERIC_DOT(int32_t);
BACKEND_GENERIC_DOT(uint32_t);
BACKEND_GENERIC_DOT(float32_t);
BACKEND_GENERIC_DOT(float64_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_DOT, SGVector)
#undef BACKEND_GENERIC_DOT

/** Implementation of @see LinalgBackendBase::to_gpu */
#define BACKEND_GENERIC_TO_GPU(Type) \
virtual GPUMemoryBase<Type>* to_gpu(const SGVector<Type>& vector) const \
#define BACKEND_GENERIC_TO_GPU(Type, Container) \
virtual GPUMemoryBase<Type>* to_gpu(const Container<Type>& vector) const \
{ \
return to_gpu_impl(vector); \
}

BACKEND_GENERIC_TO_GPU(char);
BACKEND_GENERIC_TO_GPU(uint8_t);
BACKEND_GENERIC_TO_GPU(int16_t);
BACKEND_GENERIC_TO_GPU(uint16_t);
BACKEND_GENERIC_TO_GPU(int32_t);
BACKEND_GENERIC_TO_GPU(uint32_t);
BACKEND_GENERIC_TO_GPU(float32_t);
BACKEND_GENERIC_TO_GPU(float64_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TO_GPU, SGVector)
#undef BACKEND_GENERIC_TO_GPU

/** Implementation of @see LinalgBackendGPUBase::from_gpu */
#define BACKEND_GENERIC_FROM_GPU(Type) \
virtual void from_gpu(const SGVector<Type>& vector, Type* data) const \
#define BACKEND_GENERIC_FROM_GPU(Type, Container) \
virtual void from_gpu(const Container<Type>& vector, Type* data) const \
{ \
return from_gpu_impl(vector, data); \
}

BACKEND_GENERIC_FROM_GPU(char);
BACKEND_GENERIC_FROM_GPU(uint8_t);
BACKEND_GENERIC_FROM_GPU(int16_t);
BACKEND_GENERIC_FROM_GPU(uint16_t);
BACKEND_GENERIC_FROM_GPU(int32_t);
BACKEND_GENERIC_FROM_GPU(uint32_t);
BACKEND_GENERIC_FROM_GPU(float32_t);
BACKEND_GENERIC_FROM_GPU(float64_t);
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_FROM_GPU, SGVector)
#undef BACKEND_GENERIC_FROM_GPU

#undef DEFINE_FOR_ALL_PTYPE

private:
/** ViennaCL vector C = alpha*A + beta*B method */
template <typename T>
Expand Down

0 comments on commit 670fa14

Please sign in to comment.