Skip to content

Commit 99524b4

Browse files
matajohcdrnet
authored andcommitted
Adding some missing exceptions and cleaning up comments. Also, removed unnecessary device capability queries.
1 parent a331517 commit 99524b4

8 files changed

+105
-29
lines changed

MathNet.Numerics.NativeProviders.sln

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Global
170170
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
171171
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|Win32.ActiveCfg = Debug|Any CPU
172172
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|x64.ActiveCfg = Debug|Any CPU
173+
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Debug|x64.Build.0 = Debug|Any CPU
173174
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
174175
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Any CPU.Build.0 = Release|Any CPU
175176
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
@@ -196,6 +197,7 @@ Global
196197
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-OpenBLAS|Mixed Platforms.Build.0 = Release|Any CPU
197198
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-OpenBLAS|Win32.ActiveCfg = Release|Any CPU
198199
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-OpenBLAS|x64.ActiveCfg = Release|Any CPU
200+
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release|x64.Build.0 = Release|Any CPU
199201
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Any CPU.ActiveCfg = Release|Any CPU
200202
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Any CPU.Build.0 = Release|Any CPU
201203
{3515A344-AB5F-41C7-A14C-04A79B3FFAB1}.Release-Signed|Mixed Platforms.ActiveCfg = Release|Any CPU
@@ -245,6 +247,7 @@ Global
245247
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
246248
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Debug|Win32.ActiveCfg = Debug|Any CPU
247249
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Debug|x64.ActiveCfg = Debug|Any CPU
250+
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Debug|x64.Build.0 = Debug|Any CPU
248251
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
249252
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release|Any CPU.Build.0 = Release|Any CPU
250253
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
@@ -271,6 +274,7 @@ Global
271274
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-OpenBLAS|Mixed Platforms.Build.0 = Release|Any CPU
272275
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-OpenBLAS|Win32.ActiveCfg = Release|Any CPU
273276
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-OpenBLAS|x64.ActiveCfg = Release|Any CPU
277+
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release|x64.Build.0 = Release|Any CPU
274278
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-Signed|Any CPU.ActiveCfg = Release|Any CPU
275279
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-Signed|Any CPU.Build.0 = Release|Any CPU
276280
{E79C0395-01DC-4BC9-B86C-ED45790892C5}.Release-Signed|Mixed Platforms.ActiveCfg = Release|Any CPU
@@ -344,6 +348,26 @@ Global
344348
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Mixed Platforms.Build.0 = Release|Any CPU
345349
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|Win32.ActiveCfg = Release|Any CPU
346350
{96B903EF-3EE1-4569-803C-0482D2F5ED37}.Release-Signed|x64.ActiveCfg = Release|Any CPU
351+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
352+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
353+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
354+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
355+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|Win32.ActiveCfg = Debug|Any CPU
356+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|x64.ActiveCfg = Debug|Any CPU
357+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Debug|x64.Build.0 = Debug|Any CPU
358+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
359+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|Any CPU.Build.0 = Release|Any CPU
360+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
361+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|Mixed Platforms.Build.0 = Release|Any CPU
362+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|Win32.ActiveCfg = Release|Any CPU
363+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|x64.ActiveCfg = Release|Any CPU
364+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release|x64.Build.0 = Release|Any CPU
365+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|Any CPU.ActiveCfg = Release|Any CPU
366+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|Any CPU.Build.0 = Release|Any CPU
367+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|Mixed Platforms.ActiveCfg = Release|Any CPU
368+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|Mixed Platforms.Build.0 = Release|Any CPU
369+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|Win32.ActiveCfg = Release|Any CPU
370+
{2386FAD1-BB99-4597-885C-8EF81D0637BA}.Release-Signed|x64.ActiveCfg = Release|Any CPU
347371
EndGlobalSection
348372
GlobalSection(SolutionProperties) = preSolution
349373
HideSolutionNode = FALSE

src/NativeProviders/CUDA/capabilities.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <stdio.h>
2+
13
#include "wrapper_cuda.h"
24
#include "cuda_runtime.h"
35
#include "cublas_v2.h"
@@ -19,20 +21,6 @@ extern "C" {
1921
int device;
2022
cudaDeviceProp prop;
2123

22-
if (cudaGetDeviceCount(&count))
23-
return 0;
24-
25-
if (count == 0)
26-
return 0;
27-
28-
if (cudaGetDevice(&device))
29-
return 0;
30-
31-
if (cudaGetDeviceProperties(&prop, device))
32-
return 0;
33-
34-
35-
3624
switch (capability)
3725
{
3826

@@ -62,10 +50,34 @@ extern "C" {
6250

6351
// COMMON/SHARED
6452
case 64:
53+
if (cudaGetDeviceCount(&count))
54+
return 0;
55+
56+
if (count == 0)
57+
return 0;
58+
59+
if (cudaGetDevice(&device))
60+
return 0;
61+
62+
if (cudaGetDeviceProperties(&prop, device))
63+
return 0;
64+
6565
return prop.major;
6666

6767
// LINEAR ALGEBRA
6868
case 128:
69+
if (cudaGetDeviceCount(&count))
70+
return 0;
71+
72+
if (count == 0)
73+
return 0;
74+
75+
if (cudaGetDevice(&device))
76+
return 0;
77+
78+
if (cudaGetDeviceProperties(&prop, device))
79+
return 0;
80+
6981
return prop.major >= 2;
7082

7183
// OPTIMIZATION
@@ -80,25 +92,25 @@ extern "C" {
8092
}
8193

8294
DLLEXPORT CudaResults createBLASHandle(cublasHandle_t *blasHandle){
83-
CudaResults ret;
84-
ret.blasStatus = cublasCreate(blasHandle);
95+
CudaResults ret = { cudaError_t::cudaSuccess, cublasStatus_t::CUBLAS_STATUS_SUCCESS, cusolverStatus_t::CUSOLVER_STATUS_SUCCESS };
96+
ret.blasStatus = cublasCreate(blasHandle);
8597
return ret;
8698
}
8799

88100
DLLEXPORT CudaResults destroyBLASHandle(cublasHandle_t blasHandle){
89-
CudaResults ret;
101+
CudaResults ret = { cudaError_t::cudaSuccess, cublasStatus_t::CUBLAS_STATUS_SUCCESS, cusolverStatus_t::CUSOLVER_STATUS_SUCCESS };
90102
ret.blasStatus = cublasDestroy(blasHandle);
91103
return ret;
92104
}
93105

94106
DLLEXPORT CudaResults createSolverHandle(cusolverDnHandle_t *solverHandle){
95-
CudaResults ret;
107+
CudaResults ret = { cudaError_t::cudaSuccess, cublasStatus_t::CUBLAS_STATUS_SUCCESS, cusolverStatus_t::CUSOLVER_STATUS_SUCCESS };
96108
ret.solverStatus = cusolverDnCreate(solverHandle);
97109
return ret;
98110
}
99111

100112
DLLEXPORT CudaResults destroySolverHandle(cusolverDnHandle_t solverHandle){
101-
CudaResults ret;
113+
CudaResults ret = { cudaError_t::cudaSuccess, cublasStatus_t::CUBLAS_STATUS_SUCCESS, cusolverStatus_t::CUSOLVER_STATUS_SUCCESS };
102114
ret.solverStatus = cusolverDnDestroy(solverHandle);
103115
return ret;
104116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public CuSolverException(int statusCode)
5858
/// <summary>
5959
/// Returns the appropriate error message for each status code.
6060
/// </summary>
61-
/// <param name="code">The status code returned from the API</param>
61+
/// <param name="statusCode">The status code returned from the API</param>
6262
/// <returns>The corresponding error message</returns>
6363
private static string GetErrorMessage(int statusCode)
6464
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public CuBLASException(int statusCode)
5858
/// <summary>
5959
/// Returns the appropriate error message for each status code.
6060
/// </summary>
61-
/// <param name="code">The status code returned from the API</param>
61+
/// <param name="statusCode">The status code returned from the API</param>
6262
/// <returns>The corresponding error message</returns>
6363
private static string GetErrorMessage(int statusCode)
6464
{

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ public override void CholeskyFactor(Complex[] a, int order)
477477

478478
int info = 0;
479479
HandleResults(SafeNativeMethods.z_cholesky_factor(_solverHandle, order, a, ref info));
480+
481+
if (info > 0)
482+
{
483+
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
484+
}
480485
}
481486

482487
/// <summary>
@@ -707,10 +712,15 @@ public override void SingularValueDecomposition(bool computeVectors, Complex[] a
707712
throw new ArgumentException(Resources.ArgumentArraysSameLength, "s");
708713
}
709714

710-
int info = 0;
711715
if (columnsA > rowsA || !computeVectors) // see remarks http://docs.nvidia.com/cuda/cusolver/index.html#cuds-lt-t-gt-gesvd
712716
base.SingularValueDecomposition(computeVectors, a, rowsA, columnsA, s, u, vt, new Complex[rowsA]);
713-
else HandleResults(SafeNativeMethods.z_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
717+
else
718+
{
719+
int info = 0;
720+
HandleResults(SafeNativeMethods.z_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
721+
if (info != 0)
722+
throw new NonConvergenceException();
723+
}
714724
}
715725
}
716726
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ public override void CholeskyFactor(Complex32[] a, int order)
477477

478478
int info = 0;
479479
HandleResults(SafeNativeMethods.c_cholesky_factor(_solverHandle, order, a, ref info));
480+
481+
if (info > 0)
482+
{
483+
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
484+
}
480485
}
481486

482487
/// <summary>
@@ -707,10 +712,15 @@ public override void SingularValueDecomposition(bool computeVectors, Complex32[]
707712
throw new ArgumentException(Resources.ArgumentArraysSameLength, "s");
708713
}
709714

710-
int info = 0;
711715
if (columnsA > rowsA || !computeVectors) // see remarks http://docs.nvidia.com/cuda/cusolver/index.html#cuds-lt-t-gt-gesvd
712716
base.SingularValueDecomposition(computeVectors, a, rowsA, columnsA, s, u, vt, new Complex32[rowsA]);
713-
else HandleResults(SafeNativeMethods.c_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
717+
else
718+
{
719+
int info = 0;
720+
HandleResults(SafeNativeMethods.c_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
721+
if (info != 0)
722+
throw new NonConvergenceException();
723+
}
714724
}
715725
}
716726
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ public override void CholeskyFactor(double[] a, int order)
477477

478478
int info = 0;
479479
HandleResults(SafeNativeMethods.d_cholesky_factor(_solverHandle, order, a, ref info));
480+
481+
if (info > 0)
482+
{
483+
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
484+
}
480485
}
481486

482487
/// <summary>
@@ -707,10 +712,15 @@ public override void SingularValueDecomposition(bool computeVectors, double[] a,
707712
throw new ArgumentException(Resources.ArgumentArraysSameLength, "s");
708713
}
709714

710-
int info = 0;
711715
if (columnsA > rowsA || !computeVectors) // see remarks http://docs.nvidia.com/cuda/cusolver/index.html#cuds-lt-t-gt-gesvd
712716
base.SingularValueDecomposition(computeVectors, a, rowsA, columnsA, s, u, vt, new double[rowsA]);
713-
else HandleResults(SafeNativeMethods.d_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
717+
else
718+
{
719+
int info = 0;
720+
HandleResults(SafeNativeMethods.d_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
721+
if (info != 0)
722+
throw new NonConvergenceException();
723+
}
714724
}
715725
}
716726
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ public override void CholeskyFactor(float[] a, int order)
477477

478478
int info = 0;
479479
HandleResults(SafeNativeMethods.s_cholesky_factor(_solverHandle, order, a, ref info));
480+
481+
if (info > 0)
482+
{
483+
throw new ArgumentException(Resources.ArgumentMatrixPositiveDefinite);
484+
}
480485
}
481486

482487
/// <summary>
@@ -707,10 +712,15 @@ public override void SingularValueDecomposition(bool computeVectors, float[] a,
707712
throw new ArgumentException(Resources.ArgumentArraysSameLength, "s");
708713
}
709714

710-
int info = 0;
711715
if (columnsA > rowsA || !computeVectors) // see remarks http://docs.nvidia.com/cuda/cusolver/index.html#cuds-lt-t-gt-gesvd
712716
base.SingularValueDecomposition(computeVectors, a, rowsA, columnsA, s, u, vt, new float[rowsA]);
713-
else HandleResults(SafeNativeMethods.s_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
717+
else
718+
{
719+
int info = 0;
720+
HandleResults(SafeNativeMethods.s_svd_factor(_solverHandle, computeVectors, rowsA, columnsA, a, s, u, vt, ref info));
721+
if (info != 0)
722+
throw new NonConvergenceException();
723+
}
714724
}
715725
}
716726
}

0 commit comments

Comments
 (0)