From c1c3b95477dadf19360c0ac1042a7de3f1c367cc Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Tue, 9 Feb 2010 01:24:21 +0800 Subject: [PATCH] native wrappers: wrapped cholesky, dot product, and added add/subtract/multiple vector functions for MKL --- src/NativeWrappers/ATLAS/ATLASWrapper.vcproj | 12 +++- src/NativeWrappers/ATLAS/blas.h | 5 ++ src/NativeWrappers/ATLAS/lapack.cpp | 64 +++++++++++++++++ src/NativeWrappers/Common/blas.c | 30 ++++++-- src/NativeWrappers/Common/common.h | 10 +++ src/NativeWrappers/MKL/MKLWrapper.vcproj | 16 ++++- src/NativeWrappers/MKL/blas.h | 7 +- src/NativeWrappers/MKL/lapack.cpp | 72 ++++++++++++++++++++ src/NativeWrappers/MKL/vector_functions.c | 14 ++++ src/NativeWrappers/NativeWrappers.sln | 1 + 10 files changed, 220 insertions(+), 11 deletions(-) create mode 100644 src/NativeWrappers/ATLAS/lapack.cpp create mode 100644 src/NativeWrappers/Common/common.h create mode 100644 src/NativeWrappers/MKL/lapack.cpp create mode 100644 src/NativeWrappers/MKL/vector_functions.c diff --git a/src/NativeWrappers/ATLAS/ATLASWrapper.vcproj b/src/NativeWrappers/ATLAS/ATLASWrapper.vcproj index c2804186..f3ad54fa 100644 --- a/src/NativeWrappers/ATLAS/ATLASWrapper.vcproj +++ b/src/NativeWrappers/ATLAS/ATLASWrapper.vcproj @@ -40,7 +40,7 @@ + + @@ -191,6 +195,10 @@ RelativePath=".\blas.h" > + + j; ++j) + { + a[index + j] = 0; + } + } + return info; + } + + DLLEXPORT int d_cholesky_factor(int n, double* a){ + int info = clapack_dpotrf(CblasColMajor, CblasLower, n, a, n); + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = 0; + } + } + return info; + } + + DLLEXPORT int c_cholesky_factor(int n, Complex8 a[]){ + int info = clapack_cpotrf(CblasColMajor, CblasLower, n, a, n); + Complex8 zero; + zero.real = 0.0; + zero.real = 0.0; + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = zero; + } + } + return info; + } + + DLLEXPORT int z_cholesky_factor(int n, Complex16 a[]){ + int info = clapack_zpotrf(CblasColMajor, CblasLower, n, a, n); + Complex16 zero; + zero.real = 0.0; + zero.real = 0.0; + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = zero; + } + } + return info; + } +} \ No newline at end of file diff --git a/src/NativeWrappers/Common/blas.c b/src/NativeWrappers/Common/blas.c index a37144e1..b8314040 100644 --- a/src/NativeWrappers/Common/blas.c +++ b/src/NativeWrappers/Common/blas.c @@ -1,10 +1,5 @@ #include "blas.h" - -#ifdef _WINDOWS - #define DLLEXPORT __declspec( dllexport ) -#else - #define DLLEXPORT -#endif +#include "common.h" DLLEXPORT void s_axpy( const int n, const float alpha, const float x[], float y[]){ cblas_saxpy(n, alpha, x, 1, y, 1); @@ -38,3 +33,26 @@ DLLEXPORT void z_scale(const int n, const Complex16 alpha, Complex16 x[]){ cblas_zscal(n, &alpha, x, 1); } +DLLEXPORT float s_dot_product(const int n, const float x[], const float y[]){ + return cblas_sdot(n, x, 1, y, 1); +} + +DLLEXPORT double d_dot_product(const int n, const double x[], const double y[]){ + return cblas_ddot(n, x, 1, y, 1); +} + +DLLEXPORT Complex8 c_dot_product(const int n, const Complex8 x[], const Complex8 y[]){ + Complex8 ret; + cblas_cdotu_sub(n, x, 1, y, 1, &ret); + return ret; +} + +DLLEXPORT Complex16 z_dot_product(const int n, const Complex16 x[], const Complex16 y[]){ + Complex16 ret; + cblas_zdotu_sub(n, x, 1, y, 1, &ret); + return ret; +} + + + + diff --git a/src/NativeWrappers/Common/common.h b/src/NativeWrappers/Common/common.h new file mode 100644 index 00000000..c93ea46d --- /dev/null +++ b/src/NativeWrappers/Common/common.h @@ -0,0 +1,10 @@ +#ifndef COMMON_H +#define COMMON_H + +#ifdef _WINDOWS + #define DLLEXPORT __declspec( dllexport ) +#else + #define DLLEXPORT +#endif + +#endif diff --git a/src/NativeWrappers/MKL/MKLWrapper.vcproj b/src/NativeWrappers/MKL/MKLWrapper.vcproj index 4f746b15..d3244cad 100644 --- a/src/NativeWrappers/MKL/MKLWrapper.vcproj +++ b/src/NativeWrappers/MKL/MKLWrapper.vcproj @@ -118,7 +118,7 @@ + + + + @@ -349,6 +357,10 @@ RelativePath=".\blas.h" > + + j; ++j) + { + a[index + j] = 0; + } + } + return info; + } + + DLLEXPORT int d_cholesky_factor(int n, double* a){ + char uplo = 'L'; + int info = 0; + DPOTRF(&uplo, &n, a, &n, &info); + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = 0; + } + } + return info; + } + + DLLEXPORT int c_cholesky_factor(int n, Complex8 a[]){ + char uplo = 'L'; + int info = 0; + Complex8 zero; + zero.real = 0.0; + zero.real = 0.0; + CPOTRF(&uplo, &n, a, &n, &info); + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = zero; + } + } + return info; + } + + DLLEXPORT int z_cholesky_factor(int n, Complex16 a[]){ + char uplo = 'L'; + int info = 0; + Complex16 zero; + zero.real = 0.0; + zero.real = 0.0; + ZPOTRF(&uplo, &n, a, &n, &info); + for (int i = 0; i < n; ++i) + { + int index = i * n; + for (int j = 0; j < n && i > j; ++j) + { + a[index + j] = zero; + } + } + return info; + } +} \ No newline at end of file diff --git a/src/NativeWrappers/MKL/vector_functions.c b/src/NativeWrappers/MKL/vector_functions.c new file mode 100644 index 00000000..1751c5d2 --- /dev/null +++ b/src/NativeWrappers/MKL/vector_functions.c @@ -0,0 +1,14 @@ +#include "mkl_vml.h" +#include "common.h" + +DLLEXPORT void d_vector_add( const int n, const double x[], const double y[], double ret[]){ + vdAdd( n, x, y, ret ); +} + +DLLEXPORT void d_vector_subtract( const int n, const double x[], const double y[], double ret[]){ + vdSub( n, x, y, ret ); +} + +DLLEXPORT void d_vector_multiply( const int n, const double x[], const double y[], double ret[]){ + vdMul( n, x, y, ret ); +} diff --git a/src/NativeWrappers/NativeWrappers.sln b/src/NativeWrappers/NativeWrappers.sln index d6c3e8c6..24222cd2 100644 --- a/src/NativeWrappers/NativeWrappers.sln +++ b/src/NativeWrappers/NativeWrappers.sln @@ -4,6 +4,7 @@ Microsoft Visual Studio Solution File, Format Version 10.00 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{5A0892FF-82CE-40FC-BCE1-73810C615F52}" ProjectSection(SolutionItems) = preProject Common\blas.c = Common\blas.c + Common\common.h = Common\common.h Common\resource.h = Common\resource.h Common\resource.rc = Common\resource.rc Common\WindowsDLL.cpp = Common\WindowsDLL.cpp