Skip to content

Commit 16851ac

Browse files
kjbartelcdrnet
authored andcommitted
OpenBlasLinearAlgerbraProvider implementation. It is basically just a copy of the MklLinearAlgebraProvider minus the vector methods not included in OpenBLAS. The OpenBlasLinearAlgerbraProvider replaces the GotoBlasLinearAlgebraProvider.
Added a new unit test project which is just a copy of the UnitTests-MKL project but with different compilation symbols (NATIVEMKL vs NATIVEOPENBLAS) to control which native provider is loaded. LinearAlgebraProviderTests should probably be in its own project.
1 parent 99246e9 commit 16851ac

21 files changed

+2212
-1224
lines changed

MathNet.Numerics.NativeProviders.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests-MKL", "src\UnitTe
2222
EndProject
2323
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenBLAS", "src\NativeProviders\Windows\OpenBLAS\OpenBLASWrapper.vcxproj", "{CB4011B6-E9A7-480B-A7B1-8492039DAAD1}"
2424
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests-OpenBLAS", "src\UnitTests\UnitTests-OpenBLAS.csproj", "{96B903EF-3EE1-4569-803C-0482D2F5ED37}"
26+
EndProject
2527
Global
2628
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2729
Debug|Any CPU = Debug|Any CPU
@@ -134,6 +136,24 @@ Global
134136
{CB4011B6-E9A7-480B-A7B1-8492039DAAD1}.Release-Signed|Win32.Build.0 = Release|Win32
135137
{CB4011B6-E9A7-480B-A7B1-8492039DAAD1}.Release-Signed|x64.ActiveCfg = Release|x64
136138
{CB4011B6-E9A7-480B-A7B1-8492039DAAD1}.Release-Signed|x64.Build.0 = Release|x64
139+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
140+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|Any CPU.Build.0 = Debug|Any CPU
141+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
142+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
143+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|Win32.ActiveCfg = Debug|Any CPU
144+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Debug|x64.ActiveCfg = Debug|Any CPU
145+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|Any CPU.ActiveCfg = Release|Any CPU
146+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|Any CPU.Build.0 = Release|Any CPU
147+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
148+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|Mixed Platforms.Build.0 = Release|Any CPU
149+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|Win32.ActiveCfg = Release|Any CPU
150+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release|x64.ActiveCfg = Release|Any CPU
151+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Any CPU.ActiveCfg = Release|Any CPU
152+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Any CPU.Build.0 = Release|Any CPU
153+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Mixed Platforms.ActiveCfg = Release|Any CPU
154+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Mixed Platforms.Build.0 = Release|Any CPU
155+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Win32.ActiveCfg = Release|Any CPU
156+
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|x64.ActiveCfg = Release|Any CPU
137157
EndGlobalSection
138158
GlobalSection(SolutionProperties) = preSolution
139159
HideSolutionNode = FALSE
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include "wrapper_common.h"
2+
#include "cblas.h"
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif /* __cplusplus */
7+
8+
/*
9+
Capability is supported if >0
10+
11+
Actual number can be increased over time to indicate
12+
extensions/revisions (that do not break compatibility)
13+
*/
14+
DLLEXPORT int query_capability(const int capability)
15+
{
16+
switch (capability)
17+
{
18+
19+
// SANITY CHECKS
20+
case 0: return 0;
21+
case 1: return -1;
22+
23+
// PLATFORM
24+
case 8:
25+
#ifdef _M_IX86
26+
return 1;
27+
#else
28+
return 0;
29+
#endif
30+
case 9:
31+
#ifdef _M_X64
32+
return 1;
33+
#else
34+
return 0;
35+
#endif
36+
case 10:
37+
#ifdef _M_IA64
38+
return 1;
39+
#else
40+
return 0;
41+
#endif
42+
43+
// COMMON/SHARED
44+
case 64: return 7; // revision
45+
case 66: return 1; // threading control
46+
47+
// LINEAR ALGEBRA
48+
case 128: return 1; // basic dense linear algebra
49+
50+
default: return 0; // unknown or not supported
51+
52+
}
53+
}
54+
55+
DLLEXPORT void set_max_threads(const blasint num_threads)
56+
{
57+
openblas_set_num_threads(num_threads);
58+
}
59+
60+
DLLEXPORT char* get_build_config()
61+
{
62+
return openblas_get_config();
63+
}
64+
65+
DLLEXPORT char* get_cpu_core()
66+
{
67+
return openblas_get_corename();
68+
}
69+
70+
DLLEXPORT int get_parallel_type()
71+
{
72+
return openblas_get_parallel();
73+
}
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif /* __cplusplus */
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
template <typename _T>
2+
struct complex
3+
{
4+
_T real, imag;
5+
6+
complex(_T _real = 0, _T _imag = 0)
7+
{
8+
real = _real;
9+
imag = _imag;
10+
}
11+
12+
complex(const complex<_T>& right)
13+
{
14+
real = right.real;
15+
imag = right.imag;
16+
}
17+
18+
complex& operator=(const complex& right)
19+
{
20+
real = right.real;
21+
imag = right.imag;
22+
return *this;
23+
}
24+
25+
complex& operator=(const _T& right)
26+
{
27+
real = right;
28+
imag = 0;
29+
return *this;
30+
}
31+
32+
template<typename _Other> inline
33+
complex& operator=(const complex<_Other>& right)
34+
{
35+
real = (_T)right.real;
36+
imag = (_T)right.imag;
37+
return *this;
38+
}
39+
};

src/NativeProviders/OpenBLAS/lapack.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
11
#include "cblas.h"
22

3+
#include "complex.h"
34
#define LAPACK_COMPLEX_CUSTOM
4-
5-
template <typename T>
6-
struct complex
7-
{
8-
T real, imag;
9-
10-
complex(T _real = 0, T _imag = 0)
11-
{
12-
real = _real;
13-
imag = _imag;
14-
}
15-
16-
complex(const complex<T>& right)
17-
{
18-
real = right.real;
19-
imag = right.imag;
20-
}
21-
22-
complex& operator=(const complex& right)
23-
{
24-
real = right.real;
25-
imag = right.imag;
26-
return *this;
27-
}
28-
29-
complex& operator=(const T& right)
30-
{
31-
real = right;
32-
imag = 0;
33-
return *this;
34-
}
35-
36-
template<typename _Other> inline
37-
complex& operator=(const complex<_Other>& right)
38-
{
39-
real = (T)right.real;
40-
imag = (T)right.imag;
41-
return *this;
42-
}
43-
};
44-
455
#define lapack_complex_float complex<float>
466
#define lapack_complex_double complex<double>
477

@@ -258,7 +218,7 @@ inline lapack_int complex_qr_solve_factored(lapack_int m, lapack_int n, lapack_i
258218
lapack_int info = 0;
259219
unmqr(&side, &tran, &m, &bn, &n, r, &m, tau, clone_b, &m, work, &len, &info);
260220
T one = { 1.0f, 0.0f };
261-
trsm(CblasColMajor, CblasLeft, CblasUpper, CblasNoTrans, CblasNonUnit, n, bn, &(one.real), &(r->real), m, &(clone_b->real), m);
221+
trsm(CblasColMajor, CblasLeft, CblasUpper, CblasNoTrans, CblasNonUnit, n, bn, &(one.real), &(r->real), m, &(clone_b->real), m);
262222
copyBtoX(m, n, bn, clone_b, x);
263223
delete[] clone_b;
264224
return info;

src/NativeProviders/Windows/OpenBLAS/OpenBLASWrapper.vcxproj

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@
6262
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
6363
</ImportGroup>
6464
<PropertyGroup Label="UserMacros">
65-
<OpenBLASIncludeDir>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\include</OpenBLASIncludeDir>
66-
<OpenBLASLibDir>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\x86</OpenBLASLibDir>
67-
<OpenBLASLibDir_x64>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\x64</OpenBLASLibDir_x64>
65+
<OpenBLASIncludeDir>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\include\</OpenBLASIncludeDir>
66+
<OpenBLASLibDir>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\x86\</OpenBLASLibDir>
67+
<OpenBLASLibDir_x64>$(ProjectDir)..\..\..\..\..\libs\OpenBLAS\x64\</OpenBLASLibDir_x64>
6868
</PropertyGroup>
6969
<PropertyGroup>
7070
<_ProjectFileVersion>11.0.50727.1</_ProjectFileVersion>
@@ -114,8 +114,7 @@
114114
<AdditionalLibraryDirectories>$(OpenBLASLibDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
115115
</Link>
116116
<PostBuildEvent>
117-
<Command>
118-
</Command>
117+
<Command>copy "$(OpenBLASLibDir)*.dll" $(OutputPath)</Command>
119118
</PostBuildEvent>
120119
</ItemDefinitionGroup>
121120
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -142,8 +141,7 @@
142141
<AdditionalLibraryDirectories>$(OpenBLASLibDir_x64);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
143142
</Link>
144143
<PostBuildEvent>
145-
<Command>
146-
</Command>
144+
<Command>copy "$(OpenBLASLibDir_x64)*.dll" $(OutputPath)</Command>
147145
</PostBuildEvent>
148146
</ItemDefinitionGroup>
149147
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -170,8 +168,7 @@
170168
<AdditionalLibraryDirectories>$(OpenBLASLibDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
171169
</Link>
172170
<PostBuildEvent>
173-
<Command>
174-
</Command>
171+
<Command>copy "$(OpenBLASLibDir)*.dll" $(OutputPath)</Command>
175172
</PostBuildEvent>
176173
</ItemDefinitionGroup>
177174
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -201,18 +198,21 @@
201198
<AdditionalLibraryDirectories>$(OpenBLASLibDir_x64);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
202199
</Link>
203200
<PostBuildEvent>
204-
<Command>
205-
</Command>
201+
<Command>copy "$(OpenBLASLibDir_x64)*.dll" $(OutputPath)</Command>
206202
</PostBuildEvent>
207203
</ItemDefinitionGroup>
208204
<ItemGroup>
209205
<ClCompile Include="..\..\Common\WindowsDLL.cpp" />
210206
<ClCompile Include="..\..\OpenBLAS\blas.c" />
207+
<ClCompile Include="..\..\OpenBLAS\capabilities.cpp" />
211208
<ClCompile Include="..\..\OpenBLAS\lapack.cpp" />
212209
</ItemGroup>
213210
<ItemGroup>
214211
<ResourceCompile Include="..\..\Common\resource.rc" />
215212
</ItemGroup>
213+
<ItemGroup>
214+
<ClInclude Include="..\..\OpenBLAS\complex.h" />
215+
</ItemGroup>
216216
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
217217
<ImportGroup Label="ExtensionTargets">
218218
</ImportGroup>

src/NativeProviders/Windows/OpenBLAS/OpenBLASWrapper.vcxproj.filters

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,18 @@
2424
<ClCompile Include="..\..\Common\WindowsDLL.cpp">
2525
<Filter>Source Files</Filter>
2626
</ClCompile>
27+
<ClCompile Include="..\..\OpenBLAS\capabilities.cpp">
28+
<Filter>Source Files</Filter>
29+
</ClCompile>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ResourceCompile Include="..\..\Common\resource.rc">
3033
<Filter>Resource Files</Filter>
3134
</ResourceCompile>
3235
</ItemGroup>
36+
<ItemGroup>
37+
<ClInclude Include="..\..\OpenBLAS\complex.h">
38+
<Filter>Header Files</Filter>
39+
</ClInclude>
40+
</ItemGroup>
3341
</Project>

src/Numerics/Control.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ public static void UseNativeMKL(
127127
{
128128
LinearAlgebraProvider = new Providers.LinearAlgebra.Mkl.MklLinearAlgebraProvider(consistency, precision, accuracy);
129129
}
130+
131+
public static void UseNativeOpenBLAS()
132+
{
133+
LinearAlgebraProvider = new Providers.LinearAlgebra.OpenBlas.OpenBlasLinearAlgebraProvider();
134+
}
135+
130136
#endif
131137

132138
/// <summary>

src/Numerics/Numerics.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@
157157
<Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Double.cs" />
158158
<Compile Include="Providers\LinearAlgebra\Acml\AcmlLinearAlgebraProvider.Single.cs" />
159159
<Compile Include="Providers\LinearAlgebra\Acml\SafeNativeMethods.cs" />
160-
<Compile Include="Providers\LinearAlgebra\GotoBlas\GotoBlasLinearAlgebraProvider.cs" />
161-
<Compile Include="Providers\LinearAlgebra\GotoBlas\GotoBlasLinearAlgebraProvider.Complex.cs" />
162-
<Compile Include="Providers\LinearAlgebra\GotoBlas\GotoBlasLinearAlgebraProvider.Complex32.cs" />
163-
<Compile Include="Providers\LinearAlgebra\GotoBlas\GotoBlasLinearAlgebraProvider.Double.cs" />
164-
<Compile Include="Providers\LinearAlgebra\GotoBlas\GotoBlasLinearAlgebraProvider.Single.cs" />
165-
<Compile Include="Providers\LinearAlgebra\GotoBlas\SafeNativeMethods.cs" />
160+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.cs" />
161+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Complex.cs" />
162+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Complex32.cs" />
163+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Double.cs" />
164+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Single.cs" />
165+
<Compile Include="Providers\LinearAlgebra\OpenBlas\SafeNativeMethods.cs" />
166166
<Compile Include="Providers\LinearAlgebra\ManagedLinearAlgebraProvider.Complex32.cs" />
167167
<Compile Include="Providers\LinearAlgebra\ManagedLinearAlgebraProvider.Complex.cs" />
168168
<Compile Include="Providers\LinearAlgebra\ManagedLinearAlgebraProvider.Double.cs" />
@@ -192,6 +192,7 @@
192192
<Compile Include="LinearAlgebra\Vector.BCL.cs" />
193193
<Compile Include="LinearAlgebra\Vector.Operators.cs" />
194194
<Compile Include="Exceptions.cs" />
195+
<Compile Include="Providers\LinearAlgebra\ProviderCapabilities.cs" />
195196
<Compile Include="Providers\NativeProviderLoader.cs" />
196197
<Compile Include="Random\SystemRandomSource.cs" />
197198
<Compile Include="Random\RandomSeed.cs" />

src/Numerics/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#else
7777
[assembly: InternalsVisibleTo("MathNet.Numerics.UnitTests")]
7878
[assembly: InternalsVisibleTo("MathNet.Numerics.UnitTestsMKL")]
79+
[assembly: InternalsVisibleTo("MathNet.Numerics.UnitTestsOpenBLAS")]
7980
[assembly: InternalsVisibleTo("Performance")]
8081
#endif
8182

0 commit comments

Comments
 (0)