public IsNumberPrimeCalculator(List<long> primeNumbers, Queue<long> _queue)
 {
     //set as BoundBuffers
     BoundBuff = new BoundBuffer<long>();
     BoundBuff.SetList(primeNumbers);
     BoundBuff.SetQueue(_queue);
 }
Beispiel #2
0
    public void BindBuffer(string nameInShader, Func <ComputeBuffer> lambda)
    {
        BoundBuffer attribute = new BoundBuffer();

        attribute.nameInShader = nameInShader;
        attribute.lambda       = lambda;

        bool replaced = false;
        int  id       = 0;

        foreach (BoundBuffer ba in boundBufferList)
        {
            if (ba.nameInShader == nameInShader)
            {
                boundBufferList[id] = attribute;
                //DebugThis( ba.nameInShader + " is being rebound" );
                replaced = true;
                break;
            }

            id++;
        }

        if (replaced == false)
        {
            boundBufferList.Add(attribute);
        }
    }
        public void Run(NumberReader reader)
        {
            var results = new List<long>();
            var numbersToCheck = new BoundBuffer<long>();
            RwLock _wlock = new RwLock();

            StartComputationThreads(results, numbersToCheck);

            var progressMonitor = new ProgressMonitor(results);

            new Thread(progressMonitor.Run) {IsBackground = true}.Start();

            _wlock.WriteLock();
            foreach (var value in reader.ReadIntegers()) {
                numbersToCheck.Enqueue(value);                //critical section
            }

            while (numbersToCheck.Count() > 0)
            {
                Thread.Sleep(100); // wait for the computation to complete. New Lock?
            }

            Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount);
            _wlock.WriteUnLock();
        }
Beispiel #4
0
 private static List<Thread> CreateThreads(List<long> results, BoundBuffer<long> numbersToCheck)
 {
     var threadCount = Environment.ProcessorCount*2;
     Console.WriteLine("Using {0} compute threads and 1 I/O thread", threadCount);
     var threads =
         (from threadNumber in Sequence.Create(0, threadCount)
             let calculator = new IsNumberPrimeCalculator(results, numbersToCheck)
             let newThread =
                 new Thread(calculator.CheckIfNumbersArePrime) {
                     IsBackground = true,
                     Priority = ThreadPriority.BelowNormal
                 }
             select newThread).ToList();
     return threads;
 }
Beispiel #5
0
        public void Run(NumberReader reader)
        {
            BoundBuff = new BoundBuffer<long>();

            StartComputationThreads(BoundBuff.GetList(), BoundBuff.GetQueue());

            var progressMonitor = new ProgressMonitor(BoundBuff.GetList());

            new Thread(progressMonitor.Run) {IsBackground = true}.Start();

            foreach (var value in reader.ReadIntegers()) {
                BoundBuff.Enqueue(value);
            }

            while (BoundBuff.GetQueue().Count > 0)
            {
                Thread.Sleep(100); // wait for the computation to complete.
            }
            Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount);
        }
 public IsNumberPrimeCalculator(ICollection<long> primeNumbers, BoundBuffer<long> numbersToCheck)
 {
     _primeNumbers = primeNumbers;
     _numbersToCheck = numbersToCheck;
 }
 private static void StartComputationThreads(List<long> results, BoundBuffer<long> numbersToCheck)
 {
     var threads = CreateThreads(results, numbersToCheck);
     threads.ForEach(thread => thread.Start());
 }