Skip to content

Commit d983f36

Browse files
committed
Added property to check Cuda support
1 parent ce67c57 commit d983f36

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

NeuralNetwork.NET/APIs/CuDnnNetworkLayers.cs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
using JetBrains.Annotations;
1+
using System;
2+
using System.Linq;
3+
using JetBrains.Annotations;
24
using NeuralNetworkNET.APIs.Enums;
35
using NeuralNetworkNET.APIs.Interfaces;
46
using NeuralNetworkNET.APIs.Structs;
57
using NeuralNetworkNET.Cuda.Layers;
8+
using NeuralNetworkNET.Extensions;
69
using NeuralNetworkNET.Networks.Activations;
710

811
namespace NeuralNetworkNET.APIs
@@ -12,6 +15,27 @@ namespace NeuralNetworkNET.APIs
1215
/// </summary>
1316
public static class CuDnnNetworkLayers
1417
{
18+
/// <summary>
19+
/// Gets whether or not the Cuda acceleration is supported on the current system
20+
/// </summary>
21+
public static bool IsCudaSupportAvailable
22+
{
23+
[Pure]
24+
get
25+
{
26+
try
27+
{
28+
// Calling this directly would could a crash in the <Module> loader due to the missing .dll files
29+
return CuDnnSupportHelper.IsGpuAccelerationSupported();
30+
}
31+
catch (TypeInitializationException)
32+
{
33+
// Missing .dll file
34+
return false;
35+
}
36+
}
37+
}
38+
1539
/// <summary>
1640
/// Creates a new fully connected layer with the specified number of input and output neurons, and the given activation function
1741
/// </summary>
@@ -80,5 +104,44 @@ public static INetworkLayer Inception(
80104
in TensorInfo input, in InceptionInfo info,
81105
BiasInitializationMode biasMode = BiasInitializationMode.Zero)
82106
=> new CuDnnInceptionLayer(input, info, biasMode);
107+
108+
#region Feature helper
109+
110+
/// <summary>
111+
/// A private class that is used to create a new standalone type that contains the actual test method (decoupling is needed to &lt;Module&gt; loading crashes)
112+
/// </summary>
113+
private static class CuDnnSupportHelper
114+
{
115+
/// <summary>
116+
/// Checks whether or not the Cuda features are currently supported
117+
/// </summary>
118+
public static bool IsGpuAccelerationSupported()
119+
{
120+
try
121+
{
122+
// CUDA test
123+
using (Alea.Gpu gpu = Alea.Gpu.Default)
124+
{
125+
if (gpu == null) return false;
126+
if (!Alea.cuDNN.Dnn.IsAvailable) return false; // cuDNN
127+
using (Alea.DeviceMemory<float> sample_gpu = gpu.AllocateDevice<float>(1024))
128+
{
129+
Alea.deviceptr<float> ptr = sample_gpu.Ptr;
130+
void Kernel(int i) => ptr[i] = i;
131+
Alea.Parallel.GpuExtension.For(gpu, 0, 1024, Kernel); // JIT test
132+
float[] sample = Alea.Gpu.CopyToHost(sample_gpu);
133+
return Enumerable.Range(0, 1024).Select<int, float>(i => i).ToArray().ContentEquals(sample);
134+
}
135+
}
136+
}
137+
catch
138+
{
139+
// Missing .dll or other errors
140+
return false;
141+
}
142+
}
143+
}
144+
145+
#endregion
83146
}
84147
}

Unit/NeuralNetwork.NET.Cuda.Unit/GpuExtensionsTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Alea;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using NeuralNetworkNET.APIs;
34
using NeuralNetworkNET.APIs.Structs;
45
using NeuralNetworkNET.Cuda.Extensions;
56
using NeuralNetworkNET.Extensions;
@@ -13,6 +14,12 @@ namespace NeuralNetworkNET.Cuda.Unit
1314
[TestCategory(nameof(GpuExtensionsTest))]
1415
public class GpuExtensionsTest
1516
{
17+
[TestMethod]
18+
public void CudaSupport()
19+
{
20+
Assert.IsTrue(CuDnnNetworkLayers.IsCudaSupportAvailable);
21+
}
22+
1623
[TestMethod]
1724
public void CopyToRows()
1825
{

Unit/NeuralNetwork.NET.Unit/MiscTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using NeuralNetworkNET.APIs;
67
using NeuralNetworkNET.Extensions;
78

89
namespace NeuralNetworkNET.Unit
@@ -32,5 +33,11 @@ public void Fill()
3233
v.Fill(() => 1.8f);
3334
Assert.IsTrue(v.All(f => f.EqualsWithDelta(1.8f)));
3435
}
36+
37+
[TestMethod]
38+
public void CudaSupport()
39+
{
40+
Assert.IsFalse(CuDnnNetworkLayers.IsCudaSupportAvailable);
41+
}
3542
}
3643
}

0 commit comments

Comments
 (0)