Beispiel #1
0
        public void AsSpan(int constant)
        {
            var exchangeBuffer = Accelerator.AllocateExchangeBuffer <int>(Length);

            for (int i = 0; i < Length; i++)
            {
                exchangeBuffer[i] = constant;
            }

            exchangeBuffer.CopyToAccelerator();
            var expected = Enumerable.Repeat(constant - 5, Length).ToArray();

            Accelerator.Synchronize();

            Execute(exchangeBuffer.Length, exchangeBuffer.View);
            Accelerator.Synchronize();

            // These should theoretically be the same because GetAsSpan
            // copies into cpuMemory.
            // Syncs on it's own
            Span <int> fromAccelerator = exchangeBuffer.GetAsSpan();

            for (int i = 0; i < Length; i++)
            {
                Assert.Equal(expected[i], fromAccelerator[i]);
            }
        }
        public void AsSpan(int constant)
        {
            var exchangeBuffer = Accelerator.AllocateExchangeBuffer <int>(Length);

            for (int i = 0; i < Length; i++)
            {
                exchangeBuffer[i] = constant;
            }

            exchangeBuffer.CopyToAcceleratorAsync();
            var expected = Enumerable.Repeat(constant - 5, Length).ToArray();

            Accelerator.Synchronize();

            Execute(exchangeBuffer.Length, exchangeBuffer.GPUView);
            Accelerator.Synchronize();

            exchangeBuffer.CopyFromAcceleratorAsync();
            Accelerator.Synchronize();
            var fromAccelerator = exchangeBuffer.CPUView.GetAs1DArray();

            for (int i = 0; i < Length; i++)
            {
                Assert.Equal(expected[i], fromAccelerator[i]);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Uses the exchange buffer to allocate pinned chunks of memory in CPU host memory.
        /// </summary>
        /// <param name="accelerator">The current accelerator.</param>
        /// <param name="dataSize">The number of elements to copy.</param>
        static void PerformPinnedCopyToExchangeBuffer(Accelerator accelerator, int dataSize)
        {
            // Allocate an exchange buffer that stores a buffer on the associated parent
            // device and a buffer of the same size in pinned CPU host memory
            using (var buffer = accelerator.AllocateExchangeBuffer <int>(dataSize))
            {
                // Access CPU copy
                buffer[0] = 42;
                buffer[1] = 23;

                var stream = accelerator.DefaultStream;

                // Allocate buffer on this device
                using (var bufferOnGPU = accelerator.Allocate <int>(buffer.Length))
                {
                    // Use CopyToAccelerator to copy information to the target device
                    // -> buffer.CopyToAccelerator();
                    // Note: use an accelerator stream to perform async copy operations
                    buffer.CopyToAccelerator(stream);

                    //
                    // Perform other operations...
                    //

                    stream.Synchronize();

                    // Use CopyFrom to copy information from the target device
                    // -> buffer.CopyFromAccelerator();
                    // Note: use an accelerator stream to perform async copy operations
                    buffer.CopyFromAccelerator(stream);

                    //
                    // Perform other operations...
                    //

                    stream.Synchronize();

                    // Access the updated CPU data
                    Console.WriteLine($"Data: {buffer[0]}, {buffer[1]}");
                }
            }
        }