Пример #1
0
 public Cluster(int _taskAmount, TimeConsumeSettings timeConsumeSettings1, TimeConsumeSettings timeConsumeSettings2)
 {
     thread1    = new Thread(timeConsumeSettings1);
     thread2    = new Thread(timeConsumeSettings2);
     memory     = new Memory();
     taskAmount = _taskAmount;
 }
Пример #2
0
 public Thread(Thread thread)
 {
     actions             = new List <IAction>(thread.actions);
     ReadStatus          = thread.ReadStatus;
     ComputeStatus       = thread.ComputeStatus;
     WriteStatus         = thread.WriteStatus;
     timeConsumeSettings = thread.timeConsumeSettings;
 }
Пример #3
0
 public Thread(TimeConsumeSettings _timeConsumeSettings)
 {
     actions             = new List <IAction>();
     timeConsumeSettings = _timeConsumeSettings;
 }
Пример #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            listView2.Items.Clear();

            int taskAmount = 10;
            TimeConsumeSettings timeConsumeSettings1 = new TimeConsumeSettings(3, 5, 4);
            TimeConsumeSettings timeConsumeSettings2 = new TimeConsumeSettings(6, 3, 2);

            label10.Text = "";
            label11.Text = "";

            try
            {
                timeConsumeSettings1.timeReadUnit = Convert.ToInt32(comboBox6.Text);
            }
            catch
            {
                comboBox6.Text = timeConsumeSettings1.timeReadUnit.ToString();
            }
            try
            {
                timeConsumeSettings1.timeComputeUnit = Convert.ToInt32(comboBox1.Text);
            }
            catch
            {
                comboBox1.Text = timeConsumeSettings1.timeComputeUnit.ToString();
            }
            try
            {
                timeConsumeSettings1.timeWriteUnit = Convert.ToInt32(comboBox2.Text);
            }
            catch
            {
                comboBox2.Text = timeConsumeSettings1.timeWriteUnit.ToString();
            }

            try
            {
                timeConsumeSettings2.timeReadUnit = Convert.ToInt32(comboBox3.Text);
            }
            catch
            {
                comboBox3.Text = timeConsumeSettings2.timeReadUnit.ToString();
            }
            try
            {
                timeConsumeSettings2.timeComputeUnit = Convert.ToInt32(comboBox4.Text);
            }
            catch
            {
                comboBox4.Text = timeConsumeSettings2.timeComputeUnit.ToString();
            }
            try
            {
                timeConsumeSettings2.timeWriteUnit = Convert.ToInt32(comboBox5.Text);
            }
            catch
            {
                comboBox5.Text = timeConsumeSettings2.timeWriteUnit.ToString();
            }

            bool isAllThreadsFinished = false;

            List <Cluster> clusters = new List <Cluster>()
            {
                new Cluster(taskAmount, timeConsumeSettings1, timeConsumeSettings2)
            };

            while (!isAllThreadsFinished)
            {
                isAllThreadsFinished = true;
                int currentSize = clusters.Count;
                for (int i = 0; i < currentSize; i++)
                {
                    if (clusters[i].taskAmount > 0)
                    {
                        isAllThreadsFinished = false;

                        MemoryStatus memStatus1 = clusters[i].thread1.TickAndCheck();
                        MemoryStatus memStatus2 = clusters[i].thread2.TickAndCheck();

                        if (memStatus1 == MemoryStatus.TaskFinishedAndMemoryNeeded)
                        {
                            clusters[i].taskAmount--;
                        }
                        if (memStatus2 == MemoryStatus.TaskFinishedAndMemoryNeeded)
                        {
                            clusters[i].taskAmount--;
                        }


                        if (memStatus1 == MemoryStatus.MemoryNotNeeded)
                        {
                            clusters[i].thread1.AddAction(true);
                        }
                        if (memStatus2 == MemoryStatus.MemoryNotNeeded)
                        {
                            clusters[i].thread2.AddAction(true);
                        }
                        if ((memStatus1 == MemoryStatus.MemoryNeeded || memStatus1 == MemoryStatus.TaskFinishedAndMemoryNeeded) &&
                            (memStatus2 == MemoryStatus.MemoryNotNeeded || memStatus2 == MemoryStatus.InProgress))
                        {
                            if (clusters[i].memory.IsMemoryOpen())
                            {
                                if (clusters[i].thread1.ReadStatus == ActionStatus.Active)
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings1.timeReadUnit);
                                }
                                else
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings1.timeWriteUnit);
                                }


                                clusters[i].thread1.AddAction(true);
                            }

                            else
                            {
                                clusters[i].thread1.AddAction(false);
                            }
                        }
                        else if ((memStatus2 == MemoryStatus.MemoryNeeded || memStatus2 == MemoryStatus.TaskFinishedAndMemoryNeeded) &&
                                 (memStatus1 == MemoryStatus.MemoryNotNeeded || memStatus1 == MemoryStatus.InProgress))
                        {
                            if (clusters[i].memory.IsMemoryOpen())
                            {
                                if (clusters[i].thread2.ReadStatus == ActionStatus.Active)
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings2.timeReadUnit);
                                }
                                else
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings2.timeWriteUnit);
                                }


                                clusters[i].thread2.AddAction(true);
                            }

                            else
                            {
                                clusters[i].thread2.AddAction(false);
                            }
                        }
                        else if ((memStatus1 == MemoryStatus.MemoryNeeded || memStatus1 == MemoryStatus.TaskFinishedAndMemoryNeeded) &&
                                 (memStatus2 == MemoryStatus.MemoryNeeded || memStatus2 == MemoryStatus.TaskFinishedAndMemoryNeeded))
                        {
                            if (!clusters[i].memory.IsMemoryOpen())
                            {
                                clusters[i].thread1.AddAction(false);
                                clusters[i].thread2.AddAction(false);
                            }
                            else
                            {
                                Cluster copyCluster = new Cluster(clusters[i]);

                                if (clusters[i].thread1.ReadStatus == ActionStatus.Active)
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings1.timeReadUnit);
                                }
                                else
                                {
                                    clusters[i].memory.AddAction(timeConsumeSettings1.timeWriteUnit);
                                }

                                clusters[i].thread1.AddAction(false);
                                clusters[i].thread2.AddAction(true);


                                if (copyCluster.thread2.ReadStatus == ActionStatus.Active)
                                {
                                    copyCluster.memory.AddAction(timeConsumeSettings2.timeReadUnit);
                                }
                                else
                                {
                                    copyCluster.memory.AddAction(timeConsumeSettings2.timeWriteUnit);
                                }

                                copyCluster.thread1.AddAction(true);
                                copyCluster.thread2.AddAction(false);

                                clusters.Add(copyCluster);
                            }
                        }
                    }
                }
            }

            int minTimeConsumed = clusters[0].AllTimeConsumed();
            int indexMin        = 0;

            for (int i = 1; i < clusters.Count; i++)
            {
                int timeConsumed = clusters[i].AllTimeConsumed();
                if (minTimeConsumed > timeConsumed)
                {
                    minTimeConsumed = timeConsumed;
                    indexMin        = i;
                }
            }
            List <ListViewItem> listViewItems1 = clusters[indexMin].thread1.Show();
            List <ListViewItem> listViewItems2 = clusters[indexMin].thread2.Show();

            foreach (ListViewItem View in listViewItems1)
            {
                listView1.Items.Add(View);
            }

            foreach (ListViewItem View in listViewItems2)
            {
                listView2.Items.Add(View);
            }
            int idealTimeConsume =
                ((timeConsumeSettings1.timeReadUnit + timeConsumeSettings2.timeReadUnit +
                  timeConsumeSettings1.timeComputeUnit + timeConsumeSettings2.timeComputeUnit +
                  timeConsumeSettings1.timeWriteUnit + timeConsumeSettings2.timeWriteUnit) * taskAmount
                 / 4);

            label10.Text         = idealTimeConsume.ToString() + " Ticks";
            label11.Text         = minTimeConsumed.ToString() + " Ticks";
            EfficiencyLabel.Text = "Efficiency: " + ((double)minTimeConsumed / idealTimeConsume - 1).ToString("0.##%") + " lower";
        }