Esempio n. 1
0
        /// <summary cref="MemoryBuffer{T, TIndex}.CopyFromView(
        /// AcceleratorStream, ArrayView{T}, LongIndex1)"/>
        protected internal unsafe override void CopyFromView(
            AcceleratorStream stream,
            ArrayView <T> source,
            LongIndex1 targetOffset)
        {
            var binding = Accelerator.BindScoped();

            var sourceAddress = new IntPtr(source.LoadEffectiveAddress());
            var targetAddress = new IntPtr(ComputeEffectiveAddress(targetOffset));
            var lengthInBytes = new IntPtr(source.LengthInBytes);

            switch (source.AcceleratorType)
            {
            case AcceleratorType.CPU:
                CudaException.ThrowIfFailed(
                    CurrentAPI.MemcpyHostToDevice(
                        targetAddress,
                        sourceAddress,
                        lengthInBytes,
                        stream));
                break;

            case AcceleratorType.Cuda:
                CudaException.ThrowIfFailed(
                    CurrentAPI.MemcpyDeviceToDevice(
                        targetAddress,
                        sourceAddress,
                        lengthInBytes,
                        stream));
                break;

            default:
                throw new NotSupportedException(
                          RuntimeErrorMessages.NotSupportedTargetAccelerator);
            }

            binding.Recover();
        }
Esempio n. 2
0
        /// <summary cref="MemoryBuffer{T, TIndex}.CopyToView(
        /// AcceleratorStream, ArrayView{T}, LongIndex1)"/>
        protected internal unsafe override void CopyToView(
            AcceleratorStream stream,
            ArrayView <T> target,
            LongIndex1 sourceOffset)
        {
            var binding = stream.BindScoped();

            var sourceAddress = ComputeEffectiveAddress(sourceOffset);
            var targetAddress = target.LoadEffectiveAddress();

            switch (target.AcceleratorType)
            {
            case AcceleratorType.CPU:
                Buffer.MemoryCopy(
                    sourceAddress,
                    targetAddress,
                    target.LengthInBytes,
                    target.LengthInBytes);
                break;

            case AcceleratorType.Cuda:
                CudaException.ThrowIfFailed(
                    CurrentAPI.MemcpyHostToDevice(
                        new IntPtr(targetAddress),
                        new IntPtr(sourceAddress),
                        new IntPtr(target.LengthInBytes),
                        stream));
                break;

            default:
                throw new NotSupportedException(
                          RuntimeErrorMessages.NotSupportedTargetAccelerator);
            }

            binding.Recover();
        }