public static unsafe PrimeBuffer Create <T>(PrimeContext ctx, PrimeBufferDesc desc, T[] data) where T : struct { var dataToCopy = new Memory <T>(data); var data2Cpy = dataToCopy.Pin(); var dataLength = Unsafe.SizeOf <T>() * data.Length; var dataPtr = IntPtr.Zero; if (desc.Type == RTPBufferType.CudaLinear) { //CudaDriverApi.CudaCall(CudaDriverApi.cuMemAlloc(ref dataPtr, (uint) dataLength)); CudaRunTimeApi.CudaCall(CudaRunTimeApi.cudaMalloc(ref dataPtr, (uint)dataLength)); CudaRunTimeApi.CudaCall(CudaRunTimeApi.cudaMemcpy(dataPtr.ToPointer(), new IntPtr(data2Cpy.Pointer), (uint)dataLength, CudaMemCpyKind.cudaMemcpyHostToDevice)); // CudaDriverApi.CudaCall(CudaDriverApi.cuMemcpyHtoD(ref dataPtr, data2Cpy.Pointer, (uint) dataLength)); } else { dataPtr = Marshal.AllocHGlobal(dataLength); GC.AddMemoryPressure(dataLength); var span = new Span <T>(data); MemoryHelper.CopyFromManaged(ref span, dataPtr, (uint)data.Length); // Unsafe.Copy(dataPtr.ToPointer(), ref data); } data2Cpy.Dispose(); var buffer = new PrimeBuffer(ctx, desc, dataPtr) { size = dataLength }; return(buffer); }
unsafe void SetDataInternal <T>(T[] data) where T : struct { if (_fmt.Type == RTPBufferType.Host) { var span = new Span <T>(data); MemoryHelper.CopyFromManaged(ref span, _dataPointer, (uint)data.Length); } else { var memory = new Memory <T>(data); var mh = memory.Pin(); CudaRunTimeApi.CudaCall(CudaRunTimeApi.cudaMemcpy(_dataPointer.ToPointer(), new IntPtr(mh.Pointer), (uint)(data.Length * Unsafe.SizeOf <T>()), CudaMemCpyKind.cudaMemcpyHostToDevice)); mh.Dispose(); } }