This project is read-only.

System.TypeInitializationException. InnerException: System.InvalidOperationException is thrown while testing.

Sep 5, 2013 at 6:20 AM
Edited Sep 6, 2013 at 10:00 AM

I'm using VS 2010 on a Windows 7 64x.

I've created a static class Cuda with the following code
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);


And I successfully use this GEMM function, but when I try to test my code with the test like this
namespace TestProject
    public class MatrixTest
        /// <summary>
        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);

I get the following exception:
  Message=Type Initializer "Cudafy.Host.CudafyHost" threw an exception.
       в 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.
            в 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()
It seems to me that the problem is with attempt of IDE use cudafy DLL twice.

How can i solve this problem?

=============== Edit ===============

I tried to use any functions from cudafy dll in my class MatrixTest. Well, it works. But not all. For example, Cudafy.Host.CudaGPU.GetDeviceCount() returns zero, like there is no cuda capable devices found.

And when i try to use smth like CudafyHost.GetDeviceCount(eGPUType.Cuda) or anything else from class CudafyHost, i get the above exception.
Sep 7, 2013 at 8:59 AM
Maybe you can try building CUDAfy from source to get more information. However I fear there is something wrong in your OS and you are missing some values for the PerformanceCounter, namely category "Memory". Having said that this should only be accessed if you are instantiating an EmulatedGPU.
Sep 7, 2013 at 10:55 AM
Edited Sep 7, 2013 at 11:02 AM
Okay, PerformanceCounter is created even non-Emulated mode. For some reason Memory is not in your categories. You can check which categories you have by reading this article.
I will also ensure that CUDAfy catches such cases in future, though it could be hiding further issues.
Also see
Sep 12, 2013 at 9:18 AM
Thank you very much for your answers.
But still, i cant get it, why those categories of counters are missing when testing, though, while running the programm itself everything is just fine? )
May be there are some settings in visual studio testing subsystem to be changed?
Well, ok, lets suppose i understand the exception now. So what should i do? Create those categories manually in my code when testing?
Sep 12, 2013 at 9:46 AM
This is an issue with Windows. For some reason the counters are missing from your system. You can either try to recreate them by following the Microsoft instructions or modify the CUDAfy source code to not use them - a graceful handling for this issue will be added to a future release.
Sep 12, 2013 at 10:05 AM
Edited Sep 12, 2013 at 10:06 AM
Ok, ty very much! )

I'll better try to compile from the source code and avoid useing those counters before the new release appears. I wanted to do that earlier cause i need the version for .NET 3.5
Sep 20, 2013 at 12:28 PM
Damn, i'm close to suicide!
Nothing works. I can't build the library from the sources...
In the OutDirectory in makes several dll files and CudafyNET.dll consists of only 4 kB (( so it doesnt contain any realization inside..
Sep 20, 2013 at 12:30 PM
Dear developers, could you please save a good guy's life and release a new version of dll with this issue fixed? )))
Sep 20, 2013 at 1:38 PM
When building from sources you will get multiple DLLs - you need to reference these. We use a commercial tool to then merge them into one DLL for release.
I will upload a DLL only release of 1.26 that you can use.
Sep 21, 2013 at 10:04 AM
V1.26 is available from the downloads section.
Sep 25, 2013 at 5:09 AM
Bless you, but unfortunatly it generates the same exception... ( Ok, i'll try to do smth with this myself. Ty very much for efforts anyway!!!)