Exemplo n.º 1
0
        public void Allocate1D <T>(T _)
            where T : unmanaged
        {
            using var buffer = new MemoryBufferCache(Accelerator);

            // Allocate several elements one after another
            for (int i = 1; i <= 1024; i <<= 1)
            {
                var view = buffer.Allocate <T>(i);

                Assert.Equal(view.Length, i);
                Assert.Equal(view.LengthInBytes, Interop.SizeOf <T>() * i);

                Assert.True(buffer.CacheSizeInBytes >= Interop.SizeOf <T>() * i);
                Assert.True(buffer.GetCacheSize <T>() == i);
            }

            // "Allocate" the same elements in reverse order
            for (int i = 1024; i >= 1; i >>= 1)
            {
                var view = buffer.Allocate <T>(i);

                Assert.Equal(view.Length, i);
                Assert.Equal(view.LengthInBytes, Interop.SizeOf <T>() * i);

                Assert.True(buffer.CacheSizeInBytes >= Interop.SizeOf <T>() * i);
                Assert.True(buffer.GetCacheSize <T>() >= i);
            }
        }
Exemplo n.º 2
0
        private ArrayView <int> AllocateTempScanView <T>(ArrayView <T> input)
            where T : unmanaged
        {
            var tempSize = Accelerator.ComputeScanTempStorageSize <T>(input.Length);

            return(bufferCache.Allocate <int>(tempSize));
        }
Exemplo n.º 3
0
        internal void Initialize(Index3 groupDimension, int sharedMemSize)
        {
            sharedMemoryOffset = 0;
            sharedMemoryLock   = 0;
            advancedSharedMemoryBufferIndex = -1;

            var groupSize           = groupDimension.Size;
            var currentBarrierCount = groupBarrier.ParticipantCount;

            if (currentBarrierCount > groupSize)
            {
                groupBarrier.RemoveParticipants(currentBarrierCount - groupSize);
            }
            else if (currentBarrierCount < groupSize)
            {
                groupBarrier.AddParticipants(groupSize - currentBarrierCount);
            }

            if (sharedMemSize > 0)
            {
                SharedMemory = sharedMemoryBuffer.Allocate <byte>(sharedMemSize);
            }
            else
            {
                SharedMemory = new ArrayView <byte>();
            }
            if (sharedMemSize > SharedMemorySize)
            {
                throw new InvalidKernelOperationException();
            }
            currentSharedMemoryView = default;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructs a new CPU-based runtime context for parallel processing.
        /// </summary>
        /// <param name="multiprocessor">The target CPU multiprocessor.</param>
        protected CPURuntimeContext(CPUMultiprocessor multiprocessor)
        {
            Multiprocessor = multiprocessor
                             ?? throw new ArgumentNullException(nameof(multiprocessor));

            broadcastBuffer = new MemoryBufferCache(multiprocessor.Accelerator);
            broadcastBuffer.Allocate <int>(16);
        }
Exemplo n.º 5
0
        private ArrayView <int> AllocateTempScanView <T>(ArrayView <T> input)
            where T : unmanaged
        {
            var tempSize = Accelerator.ComputeScanTempStorageSize <T>(input.Length);

            IndexTypeExtensions.AssertIntIndexRange(tempSize);
            return(bufferCache.Allocate <int>(tempSize.ToIntIndex()));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Constructs a new CPU-based runtime context for parallel processing.
        /// </summary>
        /// <param name="accelerator">The target CPU accelerator.</param>
        /// <param name="numThreadsPerWarp">The number of threads per warp.</param>
        public CPURuntimeWarpContext(CPUAccelerator accelerator, int numThreadsPerWarp)
            : base(accelerator)
        {
            WarpSize = numThreadsPerWarp;

            shuffleBuffer = new MemoryBufferCache(accelerator);
            shuffleBuffer.Allocate <int>(2 * sizeof(int) * numThreadsPerWarp);
        }
Exemplo n.º 7
0
        private ArrayView <int> AllocateTempRadixSortView <T, TRadixSortOperation>(ArrayView <T> input)
            where T : unmanaged
            where TRadixSortOperation : struct, IRadixSortOperation <T>
        {
            var tempSize = Accelerator.ComputeRadixSortTempStorageSize <T, TRadixSortOperation>(input.Length);

            return(bufferCache.Allocate <int>(tempSize));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Constructs a new CPU-based runtime context for parallel processing.
        /// </summary>
        /// <param name="accelerator">The target CPU accelerator.</param>
        protected CPURuntimeContext(CPUAccelerator accelerator)
        {
            Accelerator = accelerator
                          ?? throw new ArgumentNullException(nameof(accelerator));
            barrier = new Barrier(0);

            broadcastBuffer = new MemoryBufferCache(accelerator);
            broadcastBuffer.Allocate <int>(16);
        }
Exemplo n.º 9
0
        private ArrayView <T> AllocateTempScanView <T>(ArrayView <T> input)
            where T : struct
        {
            var tempSize = Accelerator.ComputeScanTempStorageSize(input.Length);

            if (tempSize < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(input));
            }
            return(bufferCache.Allocate <T>(tempSize));
        }
        public void Initialize(Index3 groupDim, int sharedMemSize)
        {
            GroupDim = groupDim;

            var currentBarrierCount = groupBarrier.ParticipantCount;

            if (currentBarrierCount > GroupSize)
            {
                groupBarrier.RemoveParticipants(currentBarrierCount - GroupSize);
            }
            else if (currentBarrierCount < GroupSize)
            {
                groupBarrier.AddParticipants(GroupSize - currentBarrierCount);
            }

            if (sharedMemSize > 0)
            {
                SharedMemory = sharedMemoryBuffer.Allocate <byte>(sharedMemSize);
            }
            else
            {
                SharedMemory = new ArrayView <byte>();
            }
        }