Skip to content

Commit c906a2a

Browse files
committed
Native: cleanup
1 parent d9c3f27 commit c906a2a

File tree

9 files changed

+159
-63
lines changed

9 files changed

+159
-63
lines changed

src/NativeProviders/OpenBLAS/capabilities.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern "C" {
4141
#endif
4242

4343
// COMMON/SHARED
44-
case 64: return 7; // revision
44+
case 64: return 1; // revision
4545
case 66: return 1; // threading control
4646

4747
// LINEAR ALGEBRA

src/Numerics/Control.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public static void ConfigureAuto()
8282
case "CUDA":
8383
LinearAlgebraProvider = new Providers.LinearAlgebra.Cuda.CudaLinearAlgebraProvider();
8484
break;
85+
86+
case "OPENBLAS":
87+
LinearAlgebraProvider = new Providers.LinearAlgebra.OpenBlas.OpenBlasLinearAlgebraProvider();
88+
break;
8589
#endif
8690
default:
8791
LinearAlgebraProvider = new ManagedLinearAlgebraProvider();

src/Numerics/Numerics.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@
162162
<Compile Include="Providers\LinearAlgebra\Cuda\CudaLinearAlgebraProvider.cs" />
163163
<Compile Include="Providers\LinearAlgebra\Cuda\CudaLinearAlgebraProvider.Double.cs" />
164164
<Compile Include="Providers\LinearAlgebra\Cuda\CudaLinearAlgebraProvider.Single.cs" />
165+
<Compile Include="Providers\LinearAlgebra\Cuda\CudaProviderCapabilities.cs" />
165166
<Compile Include="Providers\LinearAlgebra\Cuda\SafeNativeMethods.cs" />
167+
<Compile Include="Providers\LinearAlgebra\Mkl\MklProviderCapabilities.cs" />
166168
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.cs" />
167169
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Complex.cs" />
168170
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasLinearAlgebraProvider.Complex32.cs" />
@@ -198,7 +200,7 @@
198200
<Compile Include="LinearAlgebra\Vector.BCL.cs" />
199201
<Compile Include="LinearAlgebra\Vector.Operators.cs" />
200202
<Compile Include="Exceptions.cs" />
201-
<Compile Include="Providers\LinearAlgebra\ProviderCapabilities.cs" />
203+
<Compile Include="Providers\LinearAlgebra\OpenBlas\OpenBlasProviderCapabilities.cs" />
202204
<Compile Include="Providers\NativeProviderLoader.cs" />
203205
<Compile Include="Random\SystemRandomSource.cs" />
204206
<Compile Include="Random\RandomSeed.cs" />

src/Numerics/Providers/LinearAlgebra/Cuda/CudaLinearAlgebraProvider.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,12 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Cuda
3939
/// </summary>
4040
public partial class CudaLinearAlgebraProvider : ManagedLinearAlgebraProvider, IDisposable
4141
{
42-
private int _nativeRevision;
43-
private bool _nativeIX86;
44-
private bool _nativeX64;
45-
private bool _nativeIA64;
46-
private IntPtr _blasHandle;
47-
private IntPtr _solverHandle;
48-
49-
50-
/// <summary>
51-
/// Constructor.
52-
/// </summary>
53-
[CLSCompliant(false)]
54-
public CudaLinearAlgebraProvider()
55-
{
56-
}
42+
int _nativeRevision;
43+
bool _nativeIX86;
44+
bool _nativeX64;
45+
bool _nativeIA64;
46+
IntPtr _blasHandle;
47+
IntPtr _solverHandle;
5748

5849
/// <summary>
5950
/// Initialize and verify that the provided is indeed available.
@@ -70,12 +61,12 @@ public override void InitializeVerify()
7061
a = SafeNativeMethods.query_capability(0);
7162
b = SafeNativeMethods.query_capability(1);
7263

73-
_nativeIX86 = SafeNativeMethods.query_capability(8) > 0;
74-
_nativeX64 = SafeNativeMethods.query_capability(9) > 0;
75-
_nativeIA64 = SafeNativeMethods.query_capability(10) > 0;
64+
_nativeIX86 = SafeNativeMethods.query_capability((int)ProviderPlatform.x86) > 0;
65+
_nativeX64 = SafeNativeMethods.query_capability((int)ProviderPlatform.x64) > 0;
66+
_nativeIA64 = SafeNativeMethods.query_capability((int)ProviderPlatform.ia64) > 0;
7667

77-
_nativeRevision = SafeNativeMethods.query_capability(64);
78-
linearAlgebra = SafeNativeMethods.query_capability(128);
68+
_nativeRevision = SafeNativeMethods.query_capability((int)ProviderConfig.Revision);
69+
linearAlgebra = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebra);
7970
}
8071
catch (DllNotFoundException e)
8172
{
@@ -180,14 +171,14 @@ private void Solver(int status)
180171

181172
default:
182173
throw new Exception("Unrecognized cuSolverDn status code: " + status);
183-
184-
185174
}
186175
}
187176

188177
public override string ToString()
189178
{
190-
return string.Format("Nvidia CUDA ({1}; revision {0})", _nativeRevision, _nativeIX86 ? "x86" : _nativeX64 ? "x64" : _nativeIA64 ? "IA64" : "unknown");
179+
return string.Format("Nvidia CUDA ({1}; revision {0})",
180+
_nativeRevision,
181+
_nativeIX86 ? "x86" : _nativeX64 ? "x64" : _nativeIA64 ? "IA64" : "unknown");
191182
}
192183

193184

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// <copyright file="CudaProviderCapabilities.cs" company="Math.NET">
2+
// Math.NET Numerics, part of the Math.NET Project
3+
// http://numerics.mathdotnet.com
4+
// http://github.com/mathnet/mathnet-numerics
5+
// http://mathnetnumerics.codeplex.com
6+
//
7+
// Copyright (c) 2009-2015 Math.NET
8+
//
9+
// Permission is hereby granted, free of charge, to any person
10+
// obtaining a copy of this software and associated documentation
11+
// files (the "Software"), to deal in the Software without
12+
// restriction, including without limitation the rights to use,
13+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
14+
// copies of the Software, and to permit persons to whom the
15+
// Software is furnished to do so, subject to the following
16+
// conditions:
17+
//
18+
// The above copyright notice and this permission notice shall be
19+
// included in all copies or substantial portions of the Software.
20+
//
21+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28+
// OTHER DEALINGS IN THE SOFTWARE.
29+
// </copyright>
30+
31+
namespace MathNet.Numerics.Providers.LinearAlgebra.Cuda
32+
{
33+
internal enum ProviderPlatform : int
34+
{
35+
x86 = 8,
36+
x64 = 9,
37+
ia64 = 10,
38+
}
39+
40+
internal enum ProviderConfig : int
41+
{
42+
Revision = 64,
43+
}
44+
45+
internal enum ProviderCapability : int
46+
{
47+
LinearAlgebra = 128,
48+
}
49+
}

src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ public override void InitializeVerify()
143143
a = SafeNativeMethods.query_capability(0);
144144
b = SafeNativeMethods.query_capability(1);
145145

146-
_nativeIX86 = SafeNativeMethods.query_capability(8) > 0;
147-
_nativeX64 = SafeNativeMethods.query_capability(9) > 0;
148-
_nativeIA64 = SafeNativeMethods.query_capability(10) > 0;
146+
_nativeIX86 = SafeNativeMethods.query_capability((int)ProviderPlatform.x86) > 0;
147+
_nativeX64 = SafeNativeMethods.query_capability((int)ProviderPlatform.x64) > 0;
148+
_nativeIA64 = SafeNativeMethods.query_capability((int)ProviderPlatform.ia64) > 0;
149149

150-
_nativeRevision = SafeNativeMethods.query_capability(64);
151-
linearAlgebra = SafeNativeMethods.query_capability(128);
150+
_nativeRevision = SafeNativeMethods.query_capability((int)ProviderConfig.Revision);
151+
linearAlgebra = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebra);
152152
}
153153
catch (DllNotFoundException e)
154154
{
@@ -169,14 +169,14 @@ public override void InitializeVerify()
169169
}
170170

171171
// set numerical consistency, precision and accuracy modes, if supported
172-
if (SafeNativeMethods.query_capability(65) > 0)
172+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Precision) > 0)
173173
{
174174
SafeNativeMethods.set_consistency_mode((int)_consistency);
175175
SafeNativeMethods.set_vml_mode((uint)_precision | (uint)_accuracy);
176176
}
177177

178178
// set threading settings, if supported
179-
if (SafeNativeMethods.query_capability(66) > 0)
179+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Threading) > 0)
180180
{
181181
SafeNativeMethods.set_max_threads(Control.MaxDegreeOfParallelism);
182182
}
@@ -187,7 +187,7 @@ public override void InitializeVerify()
187187
/// </summary>
188188
public void FreeBuffers()
189189
{
190-
if (SafeNativeMethods.query_capability(67) < 1)
190+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
191191
{
192192
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
193193
}
@@ -200,7 +200,7 @@ public void FreeBuffers()
200200
/// </summary>
201201
public void ThreadFreeBuffers()
202202
{
203-
if (SafeNativeMethods.query_capability(67) < 1)
203+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
204204
{
205205
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
206206
}
@@ -213,7 +213,7 @@ public void ThreadFreeBuffers()
213213
/// </summary>
214214
public void DisableMemoryPool()
215215
{
216-
if (SafeNativeMethods.query_capability(67) < 1)
216+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
217217
{
218218
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
219219
}
@@ -228,7 +228,7 @@ public void DisableMemoryPool()
228228
/// <returns>Returns the number of bytes allocated to all memory buffers.</returns>
229229
public long MemoryStatistics(out int allocatedBuffers)
230230
{
231-
if (SafeNativeMethods.query_capability(67) < 1)
231+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
232232
{
233233
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
234234
}
@@ -241,7 +241,7 @@ public long MemoryStatistics(out int allocatedBuffers)
241241
/// </summary>
242242
public void EnablePeakMemoryStatistics()
243243
{
244-
if (SafeNativeMethods.query_capability(67) < 1)
244+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
245245
{
246246
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
247247
}
@@ -254,7 +254,7 @@ public void EnablePeakMemoryStatistics()
254254
/// </summary>
255255
public void DisablePeakMemoryStatistics()
256256
{
257-
if (SafeNativeMethods.query_capability(67) < 1)
257+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
258258
{
259259
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
260260
}
@@ -269,7 +269,7 @@ public void DisablePeakMemoryStatistics()
269269
/// <returns>The peak number of bytes allocated to all memory buffers.</returns>
270270
public long PeakMemoryStatistics(bool reset = true)
271271
{
272-
if (SafeNativeMethods.query_capability(67) < 1)
272+
if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
273273
{
274274
throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
275275
}
@@ -279,9 +279,10 @@ public long PeakMemoryStatistics(bool reset = true)
279279

280280
public override string ToString()
281281
{
282-
return string.Format("Intel MKL ({1}; revision {0})", _nativeRevision, _nativeIX86 ? "x86" : _nativeX64 ? "x64" : _nativeIA64 ? "IA64" : "unknown");
282+
return string.Format("Intel MKL ({1}; revision {0})",
283+
_nativeRevision,
284+
_nativeIX86 ? "x86" : _nativeX64 ? "x64" : _nativeIA64 ? "IA64" : "unknown");
283285
}
284-
285286
}
286287
}
287288

src/Numerics/Providers/LinearAlgebra/ProviderCapabilities.cs renamed to src/Numerics/Providers/LinearAlgebra/Mkl/MklProviderCapabilities.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <copyright file="ProviderCapabilities.cs" company="Math.NET">
1+
// <copyright file="MklProviderCapabilities.cs" company="Math.NET">
22
// Math.NET Numerics, part of the Math.NET Project
33
// http://numerics.mathdotnet.com
44
// http://github.com/mathnet/mathnet-numerics
@@ -28,28 +28,25 @@
2828
// OTHER DEALINGS IN THE SOFTWARE.
2929
// </copyright>
3030

31-
namespace MathNet.Numerics.Providers.LinearAlgebra
31+
namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl
3232
{
33-
public enum ProviderPlatform : int
33+
internal enum ProviderPlatform : int
3434
{
3535
x86 = 8,
3636
x64 = 9,
3737
ia64 = 10,
38-
arm = 11,
3938
}
4039

41-
public enum ProviderConfig : int
40+
internal enum ProviderConfig : int
4241
{
4342
Revision = 64,
4443
Precision = 65,
4544
Threading = 66,
4645
Memory = 67,
4746
}
4847

49-
public enum ProviderCapability : int
48+
internal enum ProviderCapability : int
5049
{
5150
LinearAlgebra = 128,
52-
Optimization = 256,
53-
FFT = 384,
5451
}
55-
}
52+
}

src/Numerics/Providers/LinearAlgebra/OpenBlas/OpenBlasLinearAlgebraProvider.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030

3131
#if NATIVE
3232

33-
using MathNet.Numerics.Properties;
3433
using System;
35-
using System.Numerics;
36-
using System.Security;
3734

3835
namespace MathNet.Numerics.Providers.LinearAlgebra.OpenBlas
3936
{
@@ -49,29 +46,29 @@ public enum ParallelType : int
4946
/// </summary>
5047
public partial class OpenBlasLinearAlgebraProvider : ManagedLinearAlgebraProvider
5148
{
49+
int _nativeRevision;
5250
bool _nativeIX86;
5351
bool _nativeX64;
5452
bool _nativeIA64;
5553
bool _nativeARM;
5654

57-
public OpenBlasLinearAlgebraProvider()
58-
{
59-
60-
}
61-
6255
public override void InitializeVerify()
6356
{
64-
int linearAlgebra;
57+
int a, b, linearAlgebra;
6558
try
6659
{
6760
// Load the native library
6861
NativeProviderLoader.TryLoad(SafeNativeMethods.DllName);
6962

63+
a = SafeNativeMethods.query_capability(0);
64+
b = SafeNativeMethods.query_capability(1);
65+
7066
_nativeIX86 = SafeNativeMethods.query_capability((int)ProviderPlatform.x86) > 0;
7167
_nativeX64 = SafeNativeMethods.query_capability((int)ProviderPlatform.x64) > 0;
7268
_nativeIA64 = SafeNativeMethods.query_capability((int)ProviderPlatform.ia64) > 0;
7369
_nativeARM = SafeNativeMethods.query_capability((int)ProviderPlatform.arm) > 0;
7470

71+
_nativeRevision = SafeNativeMethods.query_capability((int)ProviderConfig.Revision);
7572
linearAlgebra = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebra);
7673
}
7774
catch (DllNotFoundException e)
@@ -87,6 +84,11 @@ public override void InitializeVerify()
8784
throw new NotSupportedException("OpenBLAS Native Provider does not support capability querying and is therefore not compatible. Consider upgrading to a newer version.", e);
8885
}
8986

87+
if (a != 0 || b != -1 || linearAlgebra <=0 || _nativeRevision < 1)
88+
{
89+
throw new NotSupportedException("OpenBLAS Native Provider too old or not compatible. Consider upgrading to a newer version.");
90+
}
91+
9092
// set threading settings, if supported
9193
if (SafeNativeMethods.query_capability((int)ProviderConfig.Threading) > 0)
9294
{
@@ -96,10 +98,9 @@ public override void InitializeVerify()
9698

9799
public override string ToString()
98100
{
99-
return string.Format("OpenBLAS\r\nProvider revision: {0}\r\nCPU core name: {1}\r\nLibrary config: {1})",
100-
SafeNativeMethods.query_capability((int)ProviderConfig.Revision),
101-
SafeNativeMethods.get_cpu_core(),
102-
SafeNativeMethods.get_build_config());
101+
return string.Format("OpenBLAS ({1}; revision {0})",
102+
_nativeRevision,
103+
_nativeIX86 ? "x86" : _nativeX64 ? "x64" : _nativeIA64 ? "IA64" : _nativeARM ? "ARM" : "unknown");
103104
}
104105
}
105106
}

0 commit comments

Comments
 (0)