Exemplo n.º 1
0
        public static List <int[]> GetNearestNeighbours(List <double[]> samples, NNAlgorithm nnMethod, int numThreads = 1)
        {
            int numDim     = samples[0].Length;
            int numSamples = samples.Count;

            if (numThreads == 1 || numSamples < 500)
            {
                return(nnMethod.GetNearestNeighbours(samples, 0, numSamples));
            }
            else
            {
                int[,] indexes = GetIndexes(samples.Count, numThreads);
                List <int[]> results = RunThreads(samples, nnMethod, indexes, numThreads);
                results.Sort(new IntComp()); // sort to make sure are in the order of the index and not the order they were threaded
                return(results);
            }
        }
Exemplo n.º 2
0
        private static List <int[]> RunThreads(List <double[]> samples, NNAlgorithm algorithm, int[,] indexes, int numThreads)
        {
            Task <List <int[]> >[] taskArray = new Task <List <int[]> > [numThreads];

            // start theads
            for (int iter = 0; iter < numThreads; iter++)
            {
                int tstart_idx = indexes[iter, 0]; // must copy local here see:
                int tend_idx   = indexes[iter, 1]; // must copy local here see:
                taskArray[iter] = Task <List <int[]> > .Factory.StartNew(() => { return(algorithm.GetNearestNeighbours(samples, tstart_idx, tend_idx)); });
            }

            // get results
            List <int[]> results = new List <int[]>();

            for (int iter = 0; iter < numThreads; iter++)
            {
                results.AddRange(taskArray[iter].Result); // .Result will block until thread is finished.
            }

            return(results);
        }