Example #1
0
 public Queues()
 {
     localQueues = new WorkStealingQueue <Task> [AssignmentMain.TotalNumberOfProcessors()];
     for (int i = 0; i < AssignmentMain.TotalNumberOfProcessors(); i++)
     {
         localQueues[i] = new WorkStealingQueue <Task>();
     }
 }
Example #2
0
 public Processor(int id, Queues queue)
 {
     id_           = id;
     iteration     = 0;
     localQueue    = queue.localQueues[id_];
     allProcQueues = queue;
     totalProcs    = AssignmentMain.TotalNumberOfProcessors();
     rand          = new Random();
 }
Example #3
0
        // main thread function
        // initially processor 0 has 1 task
        public void Run()
        {
            int  procToStealFrom = 0;
            int  MaxTasks        = 0;
            Task localTask       = null;

            while (!AssignmentMain.IsDone())
            {
                if (localQueue.Count > 0)
                {
                    bool Success = localQueue.LocalPop(ref localTask);
                    if (Success)
                    {
                        localTask.Execute(this);
                        //iteration++;
                    }
                }
                else
                {
                    // Find the processor with the most tasks and try to steal from them
                    for (int i = 0; i < totalProcs; i++)
                    {
                        if (i != id_)
                        {
                            if (allProcQueues.localQueues[i].Count > MaxTasks)
                            {
                                MaxTasks        = allProcQueues.localQueues[i].Count;
                                procToStealFrom = i;
                            }
                        }
                    }


                    // Try to steal from a random processor.

                    /*for (int i = 0; i < totalProcs; i++)
                     * {
                     *     procToStealFrom = rand.Next(totalProcs);
                     *
                     *     if (totalProcs != id_)
                     *     {
                     *         break;
                     *     }
                     * }*/

                    // Execute the task immediately after a sucessful steal
                    bool Success = allProcQueues.localQueues[procToStealFrom].TrySteal(ref localTask, 1);
                    if (Success)
                    {
                        localTask.Execute(this);
                    }
                }
            }
        }