Beispiel #1
0
        public void ArrayViewMultidimensionalAccess(long length)
        {
            var extent = new LongIndex3D(length);

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

            using (var source = Accelerator.Allocate3DDenseXY <int>(extent))
            {
                source.AsContiguous().CopyFromCPU(
                    Accelerator.DefaultStream,
                    new ReadOnlySpan <int>(expectedData));
                Execute((int)extent.Size, buffer.View, source.View);
            }
            Verify(buffer.AsContiguous(), expectedData);
        }
Beispiel #2
0
        /// <summary>
        /// Allocates a 3D buffer on the given accelerator and transfers memory
        /// to and from the buffer.
        /// </summary>
        /// <param name="accelerator">The target accelerator.</param>
        static void Alloc3D(Accelerator accelerator)
        {
            Console.WriteLine($"Performing 3D allocation on {accelerator.Name}");
            var data = new int[AllocationSize1D, AllocationSize2D, AllocationSize3D];

            for (int i = 0; i < AllocationSize1D; ++i)
            {
                for (int j = 0; j < AllocationSize2D; ++j)
                {
                    for (int k = 0; k < AllocationSize3D; ++k)
                    {
                        data[i, j, k] = ((k * AllocationSize2D) + j) * AllocationSize1D + i;
                    }
                }
            }
            var targetData = new int[AllocationSize1D, AllocationSize2D, AllocationSize3D];

            using (var buffer = accelerator.Allocate3DDenseXY <int>(new LongIndex3D(AllocationSize1D, AllocationSize2D, AllocationSize3D)))
            {
                // Copy to accelerator
                buffer.CopyFromCPU(data);

                // Copy from accelerator
                buffer.CopyToCPU(targetData);
            }

            // Verify data
            for (int i = 0; i < AllocationSize1D; ++i)
            {
                for (int j = 0; j < AllocationSize2D; ++j)
                {
                    for (int k = 0; k < AllocationSize3D; ++k)
                    {
                        if (data[i, j, k] != targetData[i, j, k])
                        {
                            Console.WriteLine($"Error comparing data and target data at {i}, {j}, {k}: {targetData[i, j, k]} found, but {data[i, j, k]} expected");
                        }
                    }
                }
            }
        }