Example #1
0
        /// <summary>
        /// Copies the contents to this buffer to the given array.
        /// </summary>
        /// <param name="stream">The used accelerator stream.</param>
        /// <param name="target">The target array.</param>
        /// <param name="sourceOffset">The source offset.</param>
        /// <param name="targetOffset">The target offset.</param>
        /// <param name="extent">The length.</param>
        /// <remarks>
        /// Note that the output array will contain the data as a transposed array to
        /// match the source layout.
        /// </remarks>
        public void CopyTo(
            AcceleratorStream stream,
            T[,,] target,
            LongIndex3 sourceOffset,
            LongIndex3 targetOffset,
            LongIndex3 extent)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (sourceOffset.X < 0 || sourceOffset.Y < 0 || sourceOffset.Z < 0 ||
                sourceOffset.X >= Extent.X ||
                sourceOffset.Y >= Extent.Y ||
                sourceOffset.Z >= Extent.Z)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceOffset));
            }

            if (targetOffset.X < 0 || targetOffset.Y < 0 || targetOffset.Z < 0 ||
                targetOffset.X >= target.GetLongLength(0) ||
                targetOffset.Y >= target.GetLongLength(1) ||
                targetOffset.Z >= target.GetLongLength(2))
            {
                throw new ArgumentOutOfRangeException(nameof(targetOffset));
            }

            if (extent.X < 0 || extent.Y < 0 || extent.Z < 0 ||
                sourceOffset.X + extent.X > Extent.X ||
                sourceOffset.Y + extent.Y > Extent.Y ||
                sourceOffset.Z + extent.Z > Extent.Z ||
                targetOffset.X + extent.X > target.GetLongLength(0) ||
                targetOffset.Y + extent.Y > target.GetLongLength(1) ||
                targetOffset.Z + extent.Z > target.GetLongLength(2))
            {
                throw new ArgumentOutOfRangeException(nameof(extent));
            }

            var tempBuffer = new T[extent.Size];

            buffer.CopyTo(stream, tempBuffer, sourceOffset, 0, extent);

            for (long i = 0; i < extent.X; ++i)
            {
                for (long j = 0; j < extent.Y; ++j)
                {
                    for (long k = 0; k < extent.Z; ++k)
                    {
                        var sourceIdx = new LongIndex3(i, j, k).
                                        ComputeLinearIndex(extent);
                        target[
                            i + targetOffset.X,
                            j + targetOffset.Y,
                            k + targetOffset.Z] = tempBuffer[sourceIdx];
                    }
                }
            }
        }
Example #2
0
 public void CopyTo(
     T[][][] target,
     LongIndex3 sourceOffset,
     LongIndex3 targetOffset,
     LongIndex3 extent) =>
 CopyTo(
     Accelerator.DefaultStream,
     target,
     sourceOffset,
     targetOffset,
     extent);
Example #3
0
 /// <summary>
 /// Copies the contents of this buffer from the given array using
 /// the default accelerator stream.
 /// </summary>
 /// <param name="source">The source array.</param>
 /// <param name="sourceOffset">The source offset.</param>
 /// <param name="targetOffset">The target offset.</param>
 /// <param name="extent">The length.</param>
 /// <remarks>
 /// Note that the input array will stored as a transposed array to match the
 /// target layout.
 /// </remarks>
 public void CopyFrom(
     T[,,] source,
     LongIndex3 sourceOffset,
     LongIndex3 targetOffset,
     LongIndex3 extent) =>
 CopyFrom(
     Accelerator.DefaultStream,
     source,
     sourceOffset,
     targetOffset,
     extent);
Example #4
0
        /// <summary>
        /// Copies the current contents into a new 2D array.
        /// </summary>
        /// <param name="stream">The used accelerator stream.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="extent">The extent (number of elements).</param>
        /// <returns>A new array holding the requested contents.</returns>
        public T[,,] GetAs3DArray(
            AcceleratorStream stream,
            LongIndex3 offset,
            LongIndex3 extent)
        {
            if (extent.X < 1 || extent.Y < 1 || extent.Z < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(extent));
            }

            var result = new T[extent.X, extent.Y, extent.Z];

            CopyTo(stream, result, offset, LongIndex3.Zero, extent);
            return(result);
        }
Example #5
0
        public void ArrayViewMultidimensionalAccess(long length)
        {
            var extent = new LongIndex3(length);

            using var buffer = Accelerator.Allocate <int, LongIndex3>(extent);
            var expectedData = Enumerable.Range(0, (int)extent.Size).ToArray();

            using (var source = Accelerator.Allocate <int, LongIndex3>(extent))
            {
                source.CopyFrom(
                    Accelerator.DefaultStream,
                    expectedData,
                    0,
                    LongIndex3.Zero,
                    buffer.Length);
                Execute((int)extent.Size, buffer.View, source.View);
            }
            Verify(buffer, expectedData);
        }
Example #6
0
 /// <summary>
 /// Gets the part of this buffer on CPU memory as a 3D View.
 /// </summary>
 /// <param name="extent">The view extent.</param>
 /// <returns>The view.</returns>
 public ArrayView3D <T> As3DView(LongIndex3 extent) =>
 CPUView.BaseView.As3DView(extent);
Example #7
0
        public void CopyFrom(
            AcceleratorStream stream,
            T[][][] source,
            LongIndex3 sourceOffset,
            LongIndex3 targetOffset,
            LongIndex3 extent)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (sourceOffset.X < 0 || sourceOffset.Y < 0 || sourceOffset.Z < 0 ||
                sourceOffset.X >= source.LongLength)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceOffset));
            }

            if (targetOffset.X < 0 || targetOffset.Y < 0 || targetOffset.Z < 0 ||
                targetOffset.X >= Extent.X ||
                targetOffset.Y >= Extent.Y ||
                targetOffset.Z >= Extent.Z)
            {
                throw new ArgumentOutOfRangeException(nameof(targetOffset));
            }

            if (extent.X < 0 || extent.Y < 0 || extent.Z < 0 ||
                sourceOffset.X + extent.X > source.LongLength ||
                targetOffset.X + extent.X > Extent.X ||
                targetOffset.Y + extent.Y > Extent.Y ||
                targetOffset.Z + extent.Z > Extent.Z)
            {
                throw new ArgumentOutOfRangeException(nameof(extent));
            }

            var tempBuffer = new T[extent.Size];

            for (long i = 0; i < extent.X; ++i)
            {
                var subData = source[i + sourceOffset.X];
                if (subData == null)
                {
                    continue;
                }

                for (long j = 0; j < extent.Y; ++j)
                {
                    var subSubData = subData[j + sourceOffset.Y];
                    if (subSubData == null)
                    {
                        continue;
                    }

                    // Skip entries that are out of bounds
                    for (
                        long k = 0, e = IntrinsicMath.Min(
                            subSubData.LongLength,
                            extent.Z);
                        k < e;
                        ++k)
                    {
                        var targetIdx = new LongIndex3(i, j, k).
                                        ComputeLinearIndex(extent);
                        tempBuffer[targetIdx] = subSubData[k + sourceOffset.Z];
                    }
                }
            }

            buffer.CopyFrom(
                stream,
                tempBuffer,
                0,
                targetOffset,
                extent.Size);
        }
Example #8
0
 /// <summary>
 /// Returns a 3D view to this linear buffer.
 /// </summary>
 /// <param name="extent">The extent.</param>
 /// <returns>The 3D view.</returns>
 public ArrayView3D <T> As3DView(LongIndex3 extent) =>
 View.As3DView(extent);
Example #9
0
        /// <summary>
        /// Copies the contents of this buffer from the given array.
        /// </summary>
        /// <param name="stream">The used accelerator stream.</param>
        /// <param name="source">The source array.</param>
        /// <param name="sourceOffset">The source offset.</param>
        /// <param name="targetOffset">The target offset.</param>
        /// <param name="extent">The length.</param>
        /// <remarks>
        /// Note that the input array will stored as a transposed array to match the
        /// target layout.
        /// </remarks>
        public void CopyFrom(
            AcceleratorStream stream,
            T[,,] source,
            LongIndex3 sourceOffset,
            LongIndex3 targetOffset,
            LongIndex3 extent)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (sourceOffset.X < 0 || sourceOffset.Y < 0 || sourceOffset.Z < 0 ||
                sourceOffset.X >= source.GetLongLength(0) ||
                sourceOffset.Y >= source.GetLongLength(1) ||
                sourceOffset.Z >= source.GetLongLength(2))
            {
                throw new ArgumentOutOfRangeException(nameof(sourceOffset));
            }

            if (targetOffset.X < 0 || targetOffset.Y < 0 || targetOffset.Z < 0 ||
                targetOffset.X >= Extent.X ||
                targetOffset.Y >= Extent.Y ||
                targetOffset.Z >= Extent.Z)
            {
                throw new ArgumentOutOfRangeException(nameof(targetOffset));
            }

            if (extent.X < 0 || extent.Y < 0 || extent.Z < 0 ||
                sourceOffset.X + extent.X > source.GetLength(0) ||
                sourceOffset.Y + extent.Y > source.GetLength(1) ||
                sourceOffset.Z + extent.Z > source.GetLength(2) ||
                targetOffset.X + extent.X > Extent.X ||
                targetOffset.Y + extent.Y > Extent.Y ||
                targetOffset.Z + extent.Z > Extent.Z)
            {
                throw new ArgumentOutOfRangeException(nameof(extent));
            }

            var tempBuffer = new T[extent.Size];

            for (long i = 0; i < extent.X; ++i)
            {
                for (long j = 0; j < extent.Y; ++j)
                {
                    for (long k = 0; k < extent.Z; ++k)
                    {
                        var targetIdx = new LongIndex3(i, j, k).
                                        ComputeLinearIndex(extent);
                        tempBuffer[targetIdx] = source[
                            i + sourceOffset.X,
                            j + sourceOffset.Y,
                            k + sourceOffset.Z];
                    }
                }
            }

            buffer.CopyFrom(
                stream,
                tempBuffer,
                0,
                targetOffset,
                extent.Size);
        }
Example #10
0
 /// <summary>
 /// Copies the current contents into a new 2D array using
 /// the default accelerator stream.
 /// </summary>
 /// <param name="offset">The offset.</param>
 /// <param name="extent">The extent (number of elements).</param>
 /// <returns>A new array holding the requested contents.</returns>
 public T[,,] GetAs3DArray(LongIndex3 offset, LongIndex3 extent) =>
 GetAs3DArray(Accelerator.DefaultStream, offset, extent);