// ENHANCEMENT: Optimization candidate. /// <summary> /// Gets an array of all genomes ordered by their distance from their current specie. /// </summary> private TGenome[] GetGenomesByDistanceFromSpecie(IList <TGenome> genomeList, IList <Specie <TGenome> > specieList) { // Build an array of all genomes paired with their distance from their centroid. int genomeCount = genomeList.Count; GenomeDistancePair <TGenome>[] genomeDistanceArr = new GenomeDistancePair <TGenome> [genomeCount]; Parallel.For(0, genomeCount, _parallelOptions, delegate(int i) { TGenome genome = genomeList[i]; double distance = _distanceMetric.MeasureDistance(genome.Position, specieList[genome.SpecieIdx].Centroid); genomeDistanceArr[i] = new GenomeDistancePair <TGenome>(distance, genome); }); // Sort list. Longest distance first. // TODO: Pass in parallel options. ParallelSort.QuicksortParallel(genomeDistanceArr); // Put the sorted genomes in an array and return it. TGenome[] genomeArr = new TGenome[genomeCount]; for (int i = 0; i < genomeCount; i++) { genomeArr[i] = genomeDistanceArr[i]._genome; } return(genomeArr); }
public void SortMultipleTimes() { var length01 = 100; var length02 = 1000; var parallelSort = new ParallelSort <DataWithIndex <float> >(); var array01 = SortUtilityTest.GenerateFloatNativeArray(length01, out var inputDeps); var counter01 = new NativeCounter(Allocator.TempJob); inputDeps = parallelSort.Sort(array01, array01.Length, 8, inputDeps); inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter01, array01.Length, inputDeps); var array02 = SortUtilityTest.GenerateFloatNativeArray(length02, out var moreFloatsHandle); inputDeps = JobHandle.CombineDependencies(inputDeps, moreFloatsHandle); var counter02 = new NativeCounter(Allocator.TempJob); inputDeps = parallelSort.Sort(array02, array02.Length, 3, inputDeps); inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter02, array02.Length, inputDeps); inputDeps.Complete(); var result01 = counter01.Value; var result02 = counter02.Value; counter01.Dispose(); array01.Dispose(); counter02.Dispose(); array02.Dispose(); parallelSort.Dispose(); Assert.True(result01 == length01); Assert.True(result02 == length02); }
public IHttpActionResult Sort() { _stopwatch = Stopwatch.StartNew(); ParallelSort.Sort <double>(data); _stopwatch.Stop(); SortInfo <double> info = new SortInfo <double>(); info.Name = "Sort"; info.Count = data.Length; info.Elapsed = _stopwatch.Elapsed; info.Array = data; return(Json(info)); }
static void FloatSort(int length, int concurrentJobs) { var array = SortUtilityTest.GenerateFloatNativeArray(length, out var inputDeps); var parallelSort = new ParallelSort <DataWithIndex <float> >(); var counter = new NativeCounter(Allocator.TempJob); inputDeps = parallelSort.Sort(array, array.Length, concurrentJobs, inputDeps: inputDeps); inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter, array.Length, inputDeps); inputDeps.Complete(); var result = counter.Value; counter.Dispose(); array.Dispose(); parallelSort.Dispose(); Assert.True(result == length); }
static DataWithIndex <int>[] IntSort(NativeArray <DataWithIndex <int> > array, int concurrentJobs, JobHandle inputDeps = default) { var length = array.Length; var parallelSort = new ParallelSort <DataWithIndex <int> >(); var counter = new NativeCounter(Allocator.TempJob); inputDeps = parallelSort.Sort(array, length, concurrentJobs, inputDeps); inputDeps = SortUtilityTest.CountSortedData(parallelSort.SortedData.AsDeferredJobArray(), counter, length, inputDeps); inputDeps.Complete(); var sortedArray = parallelSort.SortedData.ToArray(); var result = counter.Value; counter.Dispose(); array.Dispose(); parallelSort.Dispose(); Assert.True(result == length); return(sortedArray); }