Ejemplo n.º 1
0
        /// <summary>
        /// Uses Accelerator.AllocatePageLockedArray1D 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 PerformPinnedCopyUsingAllocatePageLockedArray(Accelerator accelerator, int dataSize)
        {
            using (var array = accelerator.AllocatePageLockedArray1D <int>(dataSize))
            {
                // Allocate buffer on this device
                using (var bufferOnGPU = accelerator.Allocate1D <int>(array.Length))
                {
                    var stream = accelerator.DefaultStream;

                    bufferOnGPU.View.CopyFromPageLockedAsync(stream, array);

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

                    // Wait for the copy operation to finish
                    stream.Synchronize();

                    // Retrieve the results into an existing page locked array
                    bufferOnGPU.View.CopyToPageLockedAsync(stream, array);

                    // Retrieve the results into a new array
                    // Rely on disabled (default) or automatic page locking behavior
                    var result1 = bufferOnGPU.GetAsArray1D();

                    // Explicitly retrieve the results into a new page locked array
                    var result2 = bufferOnGPU.View.GetAsPageLockedArray1D();
                }
            }
        }