Ejemplo n.º 1
0
        private void ConsumeTask()
        {
            Vector256 <long> localAccVx = Vector256 <long> .Zero;

            foreach (var block in _blocks.GetConsumingEnumerable())
            {
                localAccVx = Avx2.Add(localAccVx, DigitsSummer.SumVx3(block.Span));
            }
            long localRet = 0;

            for (int i = 0; i < Vector256 <long> .Count; ++i)
            {
                localRet += localAccVx.GetElement(i);
            }
            Interlocked.Add(ref _result, localRet);
        }
Ejemplo n.º 2
0
        private static ActionBlock <ReadOnlyMemory <char> > BuildSumBlock(int maxDegreeOfParallelism)
        {
            if (maxDegreeOfParallelism == 0)
            {
                return(new ActionBlock <ReadOnlyMemory <char> >(data =>
                {
                    var ret = DigitsSummer.SumV4(data.Span);
                    Interlocked.Add(ref _result, (long)ret);
                }));
            }
            var options = new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            };

            return(new ActionBlock <ReadOnlyMemory <char> >(data =>
            {
                var ret = DigitsSummer.SumV4(data.Span);
                Interlocked.Add(ref _result, (long)ret);
            }, options));
        }
Ejemplo n.º 3
0
        private void ProduceTask()
        {
            using var reader = new StreamReader(_fileName);
            while (true)
            {
                IMemoryOwner <char> lease = MemoryPool <char> .Shared.Rent(_bufferSize);

                int ret = reader.Read(lease.Memory.Span);
                if (ret >= _bufferSize)
                {
                    _blocks.Add(lease);
                }
                else// last chunk => compute sum directly here
                {
                    _blocks.CompleteAdding();
                    var sum = DigitsSummer.SumVx2A(lease.Memory.Span.Slice(0, ret));
                    Interlocked.Add(ref _result, sum);
                    lease.Dispose();
                    break;
                }
            }
        }