Beispiel #1
0
        /// <summary>
        /// Allocates a 2D buffer on the given accelerator and transfers memory
        /// to and from the buffer.
        /// </summary>
        /// <param name="accelerator">The target accelerator.</param>
        static void Alloc2D(Accelerator accelerator)
        {
            Console.WriteLine($"Performing 2D allocation on {accelerator.Name}");
            var data = new int[AllocationSize1D, AllocationSize2D];

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

            using (var buffer = accelerator.Allocate2DDenseY <int>(new LongIndex2D(AllocationSize1D, AllocationSize2D)))
            // You can also use:
            // using (var buffer = accelerator.Allocate2DDenseX<int>(new LongIndex2D(AllocationSize1D, AllocationSize2D)))
            {
                // 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)
                {
                    if (data[i, j] != targetData[i, j])
                    {
                        Console.WriteLine($"Error comparing data and target data at {i}, {j}: {targetData[i, j]} found, but {data[i, j]} expected");
                    }
                }
            }
        }