Beispiel #1
0
        /// <summary>
        /// Method passed to threads.
        /// A thread asks for a new starting vertices for his matcher from a vertex distributor.
        /// If there are no more vertices the method ends and notified the processor that the matcher ended search.
        /// </summary>
        /// <param name="o"> Class containing matcher and distributor and synchrinizer for the main thread. </param>
        private static void WorkMultiThreadSearch(object o)
        {
            JobMultiThreadSearch job = (JobMultiThreadSearch)o;

            int start = 0;
            int end   = 0;

            while (true)
            {
                // Ask for more vertices to iterate over.
                job.distributor.DistributeVertices(ref start, ref end);

                // No more vertices. The thread can end.
                if (start == -1 || end == -1)
                {
                    // Notify the processor that this matcher ended.
                    job.resultProcessor.Process(job.matcherID, null);
                    break;
                }
                else
                {
                    // Set the range of vertices to the matcher and start searching the graph.
                    job.matcher.SetStartingVerticesIndeces(start, end);
                    job.matcher.Search();
                }
            }
        }
Beispiel #2
0
        private void ParallelSearch()
        {
            var distributor = new VertexDistributor(this.graph.GetAllVertices(), this.helper.VerticesPerThread);

            // -1 because the last index is ment for the main app thread.
            Task[] tasks = new Task[this.helper.ThreadCount - 1];
            // Create task for each matcher except the last mather and enqueue them into thread pool.
            for (int i = 0; i < tasks.Length; i++)
            {
                var tmp = new JobMultiThreadSearch(distributor, this.matchers[i], i, this.resultProcessor);
                tasks[i] = Task.Factory.StartNew(() => WorkMultiThreadSearch(tmp));
            }

            // The last matcher is used by the main app thread.
            WorkMultiThreadSearch(new JobMultiThreadSearch(distributor, this.matchers[this.helper.ThreadCount - 1], this.helper.ThreadCount - 1, this.resultProcessor));

            Task.WaitAll(tasks);
        }