Example #1
0
        public void cudaHostRegister_cudaHostUnRegister_test()
        {
            var    length = 1024 * 1024;
            var    size   = 1024 * 1024 * sizeof(float);
            IntPtr ptr    = Marshal.AllocHGlobal(size);

            Assert.IsFalse(VirtualLock(ptr, (ulong)size));

            var status = CudaRuntimeApi.cudaHostRegister(ptr, (ulong)size, DriverTypes.cudaHostRegisterDefault);

            Assert.AreEqual(status, cudaError.cudaSuccess);
            Console.WriteLine($"ptr : {ptr}");

            float[] test   = new float[length];
            float[] result = new float[length];

            for (int i = 0; i < length; i++)
            {
                test[i] = Convert.ToSingle(i);
            }

            Marshal.Copy(test, 0, ptr, length);
            Marshal.Copy(ptr, result, 0, length);

            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual(result[i], test[i]);
            }

            status = CudaRuntimeApi.cudaHostUnregister(ptr);
            Assert.AreEqual(status, cudaError.cudaSuccess);

            Marshal.FreeHGlobal(ptr);
        }