Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
9
Indexable
Never
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace Problem01
{
    class Program
    {
        static byte[] Data_Global = new byte[1000000000];
        static long[] PartialSums; // ตัวแปรสำหรับเก็บผลรวมส่วน
        static int ChunkSize; // ขนาดของแต่ละชิ้นงาน

        static int ReadData()
        {
            int returnData = 0;
            FileStream fs = new FileStream("Problem01.dat", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();

            try 
            {
                Data_Global = (byte[]) bf.Deserialize(fs);
            }
            catch (SerializationException se)
            {
                Console.WriteLine("Read Failed:" + se.Message);
                returnData = 1;
            }
            finally
            {
                fs.Close();
            }

            return returnData;
        }

        static void SumChunk(object state)
        {
            int index = (int)state;

            long localSum = 0;
            int startIndex = index * ChunkSize;
            int endIndex = Math.Min(startIndex + ChunkSize, Data_Global.Length);

            for (int i = startIndex; i < endIndex; i++)
            {
                if (Data_Global[i] % 2 == 0)
                {
                    localSum -= Data_Global[i];
                }
                else if (Data_Global[i] % 3 == 0)
                {
                    localSum += Data_Global[i] * 2;
                }
                else if (Data_Global[i] % 5 == 0)
                {
                    localSum += Data_Global[i] / 2;
                }
                else if (Data_Global[i] % 7 == 0)
                {
                    localSum += Data_Global[i] / 3;
                }

                Data_Global[i] = 0;
            }

            PartialSums[index] = localSum;
        }

        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            int y;

            /* Read data from file */
            Console.Write("Data read...");
            y = ReadData();
            if (y == 0)
            {
                Console.WriteLine("Complete.");
            }
            else
            {
                Console.WriteLine("Read Failed!");
                return;
            }

            /* Start */
            Console.Write("\n\nWorking...");
            sw.Start();

            int numThreads = Environment.ProcessorCount;
            ChunkSize = Data_Global.Length / numThreads;
            PartialSums = new long[numThreads];
            Thread[] threads = new Thread[numThreads];

            for (int i = 0; i < numThreads; i++)
            {
                threads[i] = new Thread(SumChunk);
                threads[i].Start(i);
            }

            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            long finalSum = 0;
            foreach (long partialSum in PartialSums)
            {
                finalSum += partialSum;
            }

            sw.Stop();
            Console.WriteLine("Done.");

            /* Result */
            Console.WriteLine("Summation result: {0}", finalSum);
            Console.WriteLine("Time used: " + sw.ElapsedMilliseconds.ToString() + "ms");
        }
    }
}