Skip to content

Commit 591e8dd

Browse files
committed
Minor refactoring
1 parent 73d5bc3 commit 591e8dd

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

NeuralNetwork.NET/cuDNN/GpuExtensions.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ public static unsafe DeviceMemory<float> AllocateDevice([NotNull] this Gpu gpu,
4242

4343
// Memory copy
4444
DeviceMemory<float> result_gpu = gpu.AllocateDevice<float>(source.Entities * length);
45-
CUDAInterop.CUDA_MEMCPY2D_st* ptSt = stackalloc CUDAInterop.CUDA_MEMCPY2D_st[1];
46-
ptSt[0] = new CUDAInterop.CUDA_MEMCPY2D_st
45+
CUDAInterop.CUDA_MEMCPY2D_st* ptSt = stackalloc[]
4746
{
48-
srcMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_HOST,
49-
srcHost = source.Ptr + sizeof(float) * offset,
50-
srcPitch = new IntPtr(sizeof(float) * source.Length),
51-
dstMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_DEVICE,
52-
dstDevice = result_gpu.Handle,
53-
dstPitch = new IntPtr(sizeof(float) * length),
54-
WidthInBytes = new IntPtr(sizeof(float) * length),
55-
Height = new IntPtr(source.Entities)
47+
new CUDAInterop.CUDA_MEMCPY2D_st
48+
{
49+
srcMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_HOST,
50+
srcHost = source.Ptr + sizeof(float) * offset,
51+
srcPitch = new IntPtr(sizeof(float) * source.Length),
52+
dstMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_DEVICE,
53+
dstDevice = result_gpu.Handle,
54+
dstPitch = new IntPtr(sizeof(float) * length),
55+
WidthInBytes = new IntPtr(sizeof(float) * length),
56+
Height = new IntPtr(source.Entities)
57+
}
5658
};
5759
CUDAInterop.cudaError_enum result = CUDAInterop.cuMemcpy2D(ptSt);
5860
return result == CUDAInterop.cudaError_enum.CUDA_SUCCESS
@@ -103,17 +105,19 @@ public static unsafe void CopyTo([NotNull] this DeviceMemory<float> source, in T
103105
if (destination.Length - offset < length) throw new ArgumentOutOfRangeException(nameof(offset), "The input offset isn't valid");
104106

105107
// Memory copy
106-
CUDAInterop.CUDA_MEMCPY2D_st* ptSt = stackalloc CUDAInterop.CUDA_MEMCPY2D_st[1];
107-
ptSt[0] = new CUDAInterop.CUDA_MEMCPY2D_st
108+
CUDAInterop.CUDA_MEMCPY2D_st* ptSt = stackalloc[]
108109
{
109-
srcMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_DEVICE,
110-
srcDevice = source.Handle,
111-
srcPitch = new IntPtr(sizeof(float) * length),
112-
dstMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_HOST,
113-
dstHost = destination.Ptr + sizeof(float) * offset,
114-
dstPitch = new IntPtr(sizeof(float) * destination.Length),
115-
WidthInBytes = new IntPtr(sizeof(float) * length),
116-
Height = new IntPtr(destination.Entities)
110+
new CUDAInterop.CUDA_MEMCPY2D_st
111+
{
112+
srcMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_DEVICE,
113+
srcDevice = source.Handle,
114+
srcPitch = new IntPtr(sizeof(float) * length),
115+
dstMemoryType = CUDAInterop.CUmemorytype_enum.CU_MEMORYTYPE_HOST,
116+
dstHost = destination.Ptr + sizeof(float) * offset,
117+
dstPitch = new IntPtr(sizeof(float) * destination.Length),
118+
WidthInBytes = new IntPtr(sizeof(float) * length),
119+
Height = new IntPtr(destination.Entities)
120+
}
117121
};
118122
CUDAInterop.cudaError_enum result = CUDAInterop.cuMemcpy2D(ptSt);
119123
if (result != CUDAInterop.cudaError_enum.CUDA_SUCCESS)

0 commit comments

Comments
 (0)