Works under VS 2010 32 and 64. Works under VS 2012 32 but not on 64!

Feb 28, 2013 at 7:41 PM
This snippet work under VS 2010 32 and 64 and VS 2012 32 but not under VS 2012 64! I tried varying the .NET version of VS 2012 64 from 4.5 to 4 but that didn't work.

Is it the Path global variable? I made sure it is including the bin/amd64...

On VS 2012 64 will generate "Attempted to read or write protected memory." on "gen.SetPseudoRandomGeneratorSeed(SEED)" at runtime.

class Program
private const int RANDOM_NUMBERS = 10000;
private const int SEED = 777;
private const int CURAND_RNG_PSEUDO_MTGP32 = 141; 

static void Main(string[] args)
    float timer = 0;
        GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target);
        float[] devData = gpu.Allocate<float>(RANDOM_NUMBERS);
        float[] hostData = new float[RANDOM_NUMBERS];

        GPGPURAND gen = GPGPURAND.Create(gpu, (curandRngType)CURAND_RNG_PSEUDO_MTGP32);
        gen.GenerateNormal(devData, 0, 1);
        timer = gpu.StopTimer();
        gpu.CopyFromDevice(devData, hostData);
    catch (Exception ex)
    Console.WriteLine("{0:0,0} Random Numbers generated in {1:0.00}ms", RANDOM_NUMBERS, timer);
Any help would be appreciated.
Mar 21, 2013 at 10:55 AM
I've not yet tried CUDAfy or CUDA with VS2012. You can try looking at:
May 2, 2013 at 7:49 PM
I am running CUDA C under Visual Studio 2012 and compiling and the same example is working with me in C.
I am also using CUDAfy successfully with VS 2012 to load kernels. So, my problem is not in setting up CUDA. I am going to look at the source code and see what would the issue be.
May 2, 2013 at 9:28 PM
I downloaded the project source code, compiled in 64bit and referenced the DLLs, I had the same problem.

I copied my code to the Cudafy.Math.UnitTests unit tests and my code worked!

I went back to my VS 2012 and downgraded the .NET version from 4.5 to 4.0 and the issue was gone! So, this is definitely not working in 4.5 but working in 4.

Thank you and keep the good work :)
May 3, 2013 at 8:42 AM
Interesting. I would not have been surprised of issues with .NET 4.5 code needing cudafying since that uses reflection.
Can you try downloading the CUDAfy sources and compiling for 4.5?
May 3, 2013 at 9:46 AM

I did switch the whole CUDAfy solution to .NET 4.5 and compiled for Debug and x64 and the provided test works as expected. So, I think the moral of the story here is if you are using CUDAfy with .NET 4.5 then you need to compile it from the source.

If there is no legal problem with this (as violating an open source licence by doing so), could you add the NUnit/pNUnit/NUnit.Mocks to the "packages" directory, that is generated by NUGet? Otherwise, it will be a pain for whoever is using the source code to re-reference the NUnit .dlls in all the testing projects. Alternatively, I guess someone could turn on the download automatically from NUGet option (I just thought of that while writing the previous paragraph.
May 12, 2013 at 9:01 AM
Just tested Cudafy V1.22 (.NET 4.0) with a .NET 4.5 app and there were no issues. Curious as to what is happening with your case.
Jul 17, 2013 at 9:03 AM
If I want get the random value belong to -5 to 5 ,how can I set the parameter of gen.GenerateNormal(devData, 0, 1) ?
Oct 7, 2013 at 2:22 PM
Same problem here. All of p-invoke functions does not work in 64bit mode. (but works in 32bit mode)

And changing .Net framework version do not works for me! So I can't test my new code for cudafy!

My PC is Windows 8, VS2012 update 3. Any idea? (both latest source code from SVN and precompiled binary has same problem.)
Oct 7, 2013 at 3:31 PM
What error message do you see?
Oct 7, 2013 at 10:38 PM
AccessViolationException error was thrown, says "Attempted to read or write protected memory."

This was occurred when Driver class call native cuda functions.
Oct 8, 2013 at 8:35 AM
Edited Oct 8, 2013 at 8:36 AM
I've just run the Cudafy.Maths.UnitTests project as 64-bit without problem. This includes use of CURAND. Can you try a non-CUDAfy CUDA program and targeting .NET 4.0 if you are on 4.5 at the moment?
Oct 8, 2013 at 10:16 AM
I tested official cuda sample sources and works correctly in both 32,64bit. And I tried .Net 4.0 and 4.5.

I think AccessViolationException has occurred after I upgrade VS2012 and Windows 8.

With my previous PC environment (Windows 7, VS2010, same hardware), cudafy did works correctly. I can't figure out the reason of this.