<commit_before>﻿// BASEDON: https://github.com/dotnet/corefx/blob/7ae1a252d7e68c5513d2658de7a401c37e9b0504/src/System.Threading.Tasks.Parallel/tests/ParallelForTest.cs

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// ParallelForTest.cs
//
// This file contains functional tests for Parallel.For and Parallel.ForEach
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-

// ReSharper disable CompareOfFloatsByEqualityOperator

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace System.Threading.Tasks.Tests
{
    public enum ActionWithLocal
    {
        // no ParallelLoopState<TLocal>
        None,

        // need ParallelLoopState<TLocal> and Action<TLocal> threadLocalFinally
        HasFinally
    }

    public enum ActionWithState
    {
        // no ParallelLoopState
        None,

        // need ParallelLoopState and will do Stop
        Stop
    }

    // List of APIs being tested
    public enum Api
    {
        For,
        For64,
        ForeachOnArray,
        ForeachOnList,
        Foreach
    }

    public enum DataSourceType
    {
        Partitioner,
        Collection
    }

    /// <summary>
    /// Partitioner types used for ParallelForeach with partitioners
    /// </summary>
    public enum PartitionerType
    {
        // Out of the box List Partitioner
        ListBalancedOob = 0,

        // Out of the box Array partitioner
        ArrayBalancedOob = 1,

        // Out of the box Enumerable partitioner
        EnumerableOob = 2,

        // out of the box range partitioner
        RangePartitioner = 3,

        // partitioner one chunk
        Enumerable1Chunk = 4
    }

    public enum StartIndexBase
    {
        Zero = 0,
        Int16 = short.MaxValue,
        Int32 = int.MaxValue,

        // Enum can't take a Int64.MaxValue
        Int64 = -1
    }

    public enum WithParallelOption
    {
        // no ParallelOptions
        None,

        // ParallelOptions created with DOP
        WithDop
    }

    public enum WorkloadPattern
    {
        Similar,
        Increasing,
        Decreasing,
        Random
    }

    public sealed class ParallelForTest
    {
        private const int _zetaSeedOffset = 10000;
        private readonly TestParameters _parameters;

        private readonly ThreadLocal<Random> _random = new(() => new Random(unchecked((int)(DateTime.Now.Ticks))));
        private readonly double[] _results;
        private readonly List<int>[] _sequences;
        private IList<int> _collection; // the collection used in Foreach

        // global place to store the workload result for verification

        private ParallelOptions _parallelOption;

        private OrderablePartitioner<int> _partitioner;

        // offset to the zeta seed to ensure result converge to the expected
        private OrderablePartitioner<Tuple<int, int>> _rangePartitioner;

        // data structure used with ParallelLoopState<TLocal>
        // each row is the sequence of loop "index" finished in the same thread
        private int _threadCount;

        // Random generator for WorkloadPattern == Random
        public ParallelForTest(TestParameters parameters)
        {
            _parameters = parameters;

            _results = new double[parameters.Count];

            if (parameters.LocalOption == ActionWithLocal.None)
            {
                return;
            }

            _sequences = new List<int>[1024];
            _threadCount = 0;
        }

        public static double ZetaSequence(int n)
        {
            double result = 0;
            for (int i = 1; i < n; i++)
            {
                result += 1.0 / ((double)i * (double)i);
            }

            return result;
        }

        internal void RealRun()
        {
            if (_parameters.Api == Api.For64)
                RunParallelFor64Test();
            else if (_parameters.Api == Api.For)
                RunParallelForTest();
            else
                RunParallelForeachTest();

            // verify result
            for (int i = 0; i < _parameters.Count; i++)
                Verify(i);

            // verify unique  index sequences if run WithLocal
            if (_parameters.LocalOption != ActionWithLocal.None)
                VerifySequences();
        }

        private static List<int> ThreadLocalInit()
        {
            return new List<int>();
        }

        // consolidate all the indexes of the global sequences into one list
        private List<int> Consolidate(out List<int> duplicates)
        {
            duplicates = new List<int>();
            List<int> processedIndexes = new List<int>();
            //foreach (List<int> perThreadSequences in sequences)
            for (int thread = 0; thread < _threadCount; thread++)
            {
                List<int> perThreadSequences = _sequences[thread];
                foreach (int i in perThreadSequences)
                {
                    if (processedIndexes.Contains(i))
                    {
                        duplicates.Add(i);
                    }
                    else
                    {
                        processedIndexes.Add(i);
                    }
                }
            }<commit_msg>Inline variable<commit_after>﻿// BASEDON: https://github.com/dotnet/corefx/blob/7ae1a252d7e68c5513d2658de7a401c37e9b0504/src/System.Threading.Tasks.Parallel/tests/ParallelForTest.cs

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
//
// ParallelForTest.cs
//
// This file contains functional tests for Parallel.For and Parallel.ForEach
//
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-

// ReSharper disable CompareOfFloatsByEqualityOperator

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace System.Threading.Tasks.Tests
{
    public enum ActionWithLocal
    {
        // no ParallelLoopState<TLocal>
        None,

        // need ParallelLoopState<TLocal> and Action<TLocal> threadLocalFinally
        HasFinally
    }

    public enum ActionWithState
    {
        // no ParallelLoopState
        None,

        // need ParallelLoopState and will do Stop
        Stop
    }

    // List of APIs being tested
    public enum Api
    {
        For,
        For64,
        ForeachOnArray,
        ForeachOnList,
        Foreach
    }

    public enum DataSourceType
    {
        Partitioner,
        Collection
    }

    /// <summary>
    /// Partitioner types used for ParallelForeach with partitioners
    /// </summary>
    public enum PartitionerType
    {
        // Out of the box List Partitioner
        ListBalancedOob = 0,

        // Out of the box Array partitioner
        ArrayBalancedOob = 1,

        // Out of the box Enumerable partitioner
        EnumerableOob = 2,

        // out of the box range partitioner
        RangePartitioner = 3,

        // partitioner one chunk
        Enumerable1Chunk = 4
    }

    public enum StartIndexBase
    {
        Zero = 0,
        Int16 = short.MaxValue,
        Int32 = int.MaxValue,

        // Enum can't take a Int64.MaxValue
        Int64 = -1
    }

    public enum WithParallelOption
    {
        // no ParallelOptions
        None,

        // ParallelOptions created with DOP
        WithDop
    }

    public enum WorkloadPattern
    {
        Similar,
        Increasing,
        Decreasing,
        Random
    }

    public sealed class ParallelForTest
    {
        private const int _zetaSeedOffset = 10000;
        private readonly TestParameters _parameters;

        private readonly ThreadLocal<Random> _random = new(() => new Random(unchecked((int)(DateTime.Now.Ticks))));
        private readonly double[] _results;
        private readonly List<int>[] _sequences;
        private IList<int> _collection; // the collection used in Foreach

        // global place to store the workload result for verification

        private ParallelOptions _parallelOption;

        private OrderablePartitioner<int> _partitioner;

        // offset to the zeta seed to ensure result converge to the expected
        private OrderablePartitioner<Tuple<int, int>> _rangePartitioner;

        // data structure used with ParallelLoopState<TLocal>
        // each row is the sequence of loop "index" finished in the same thread
        private int _threadCount;

        // Random generator for WorkloadPattern == Random
        public ParallelForTest(TestParameters parameters)
        {
            _parameters = parameters;

            _results = new double[parameters.Count];

            if (parameters.LocalOption == ActionWithLocal.None)
            {
                return;
            }

            _sequences = new List<int>[1024];
            _threadCount = 0;
        }

        public static double ZetaSequence(int n)
        {
            double result = 0;
            for (int i = 1; i < n; i++)
            {
                result += 1.0 / ((double)i * (double)i);
            }

            return result;
        }

        internal void RealRun()
        {
            if (_parameters.Api == Api.For64)
                RunParallelFor64Test();
            else if (_parameters.Api == Api.For)
                RunParallelForTest();
            else
                RunParallelForeachTest();

            // verify result
            for (int i = 0; i < _parameters.Count; i++)
                Verify(i);

            // verify unique  index sequences if run WithLocal
            if (_parameters.LocalOption != ActionWithLocal.None)
                VerifySequences();
        }

        private static List<int> ThreadLocalInit()
        {
            return new List<int>();
        }

        // consolidate all the indexes of the global sequences into one list
        private List<int> Consolidate(out List<int> duplicates)
        {
            duplicates = new List<int>();
            List<int> processedIndexes = new List<int>();
            for (int thread = 0; thread < _threadCount; thread++)
            {
                foreach (int i in _sequences[thread])
                {
                    if (processedIndexes.Contains(i))
                    {
                        duplicates.Add(i);
                    }
                    else
                    {
                        processedIndexes.Add(i);
                    }
                }
            }
{'fraction_non_alphanumeric': 0.11198268866648634, 'fraction_numerical': 0.011721215399873771, 'mean_word_length': 1.7126436781609196, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550128', 'n_tokens_mistral': 3119, 'n_tokens_neox': 2871, 'n_words': 805}