cudafy在测试时抛出异常

时间:2013-09-05 05:29:33

标签: invalidoperationexception cudafy.net

我在Windows 7 64x上使用VS 2010。

我使用以下代码

创建了一个静态类Cuda
namespace Network
{
    public static class Cuda
    {
        static GPGPU gpu= CudafyHost.GetDevice();
        static CudafyHost host = new CudafyHost();
        static GPGPUBLAS blas = GPGPUBLAS.Create(gpu);

        public static void GEMM(int m, int n, int k, float alpha, float[] a, float[] b, float beta, float[] c,Cudafy.Maths.BLAS.Types.cublasOperation Op)
        {
            float[] d_a = gpu.Allocate(a);
            float[] d_b = gpu.Allocate(b);
            float[] d_c = gpu.Allocate(c);

            gpu.CopyToDevice(a, d_a);
            gpu.CopyToDevice(b, d_b);
            gpu.CopyToDevice(c, d_c);

            blas.GEMM(m, n, k, alpha, d_a, d_b, beta, d_c, Op);

            gpu.CopyFromDevice(d_c, c);

            gpu.Free(d_a);
            gpu.Free(d_b);
            gpu.Free(d_c);
        }
    }
}

我成功使用了这个GEMM功能,但当我尝试用这样的测试来测试我的代码时

``
namespace TestProject
{
    [TestClass]
    public class MatrixTest
    {
        /// <summary>
        ///op_Multiply
        ///</summary>
        [TestMethod()]
        public void op_MultiplyTest()
        {


            Matrix a = new Matrix(2,3); 
            a.Data = new float[] { 1, 2, 3, 4, 5, 6 };

            Matrix b = new Matrix (3,4);
            b.Data = new float[] { 1, 0, 2, 3, 1, 2, 0, 4, 2, 1, 1, 0, 3, 0, 1, 1 };

            Matrix expected = new Matrix(2, 4);

            expected.Data = new float[] {11,14,16,22,22,28,4,6};

            Matrix actual;
            actual = (a * b);
            Assert.AreEqual(expected, actual);
        }
    }
}

我得到以下异常:

System.TypeInitializationException
Message=Type Initializer "Cudafy.Host.CudafyHost" threw an exception.
Source=Cudafy.NET
TypeName=Cudafy.Host.CudafyHost
StackTrace:
   в Cudafy.Host.CudafyHost.GetDevice(eGPUType type, Int32 deviceId)
   в Network.Cuda..cctor() в C:\Users\Dan\Documents\Visual Studio 2010\Projects\Network\Network\Cuda.cs:строка 14
InnerException: System.InvalidOperationException
   Message=Category does not exist.
   Source=System
   StackTrace:
        в System.Diagnostics.PerformanceCounterLib.CounterExists(String machine, String category, String counter)
        в System.Diagnostics.PerformanceCounter.InitializeImpl()
        в System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)
        в System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName)
        в Cudafy.Host.EmulatedGPU..ctor(Int32 deviceId)
        в Cudafy.Host.CudafyHost.DoCreateDevice(eGPUType target, Int32 deviceId)
        в Cudafy.Host.CudafyHost.CreateDevice(eGPUType type, Int32 deviceId)
        в Cudafy.Host.CudafyHost.GetDevice(eGPUType type, Int32 deviceId)
        в Cudafy.Host.CudafyHost..cctor()
   InnerException: 

在我看来,问题是尝试IDE使用cudafy DLL两次或初始化静态类两次...

我该如何解决?

===============编辑===============

我尝试在我的班级MatrixTest中使用cudafy dll中的任何函数。嗯,它的工作原理。 但不是所有的。例如,Cudafy.Host.CudaGPU.GetDeviceCount()返回零,就像找不到具有cuda功能的设备一样。

当我尝试使用像CudafyHost.GetDeviceCount(eGPUType.Cuda)或类CudafyHost中的任何其他东西时,我得到上述异常。

1 个答案:

答案 0 :(得分:0)

您看到的异常是由于系统上缺少性能类别。这显然是.NET偶尔出错的地方。见this thread for more information。下一个CUDAfy版本将更优雅地处理这样的错误。