public static IPRAMMachine Setup(int size = 3, int limit = 2)
        {
            var processors = new List <Processor>();

            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    processors.Add(new Find3OnesInMatrixProcessor(row, column, size));
                }
            }

            var memory = new PRAM <MemoryTypes.CRCW>();
            var random = new Random();

            var matrix = new Matrix <int>(size, size);

            for (int i = 0; i < matrix.RowsCount; i++)
            {
                for (int j = 0; j < matrix.ColumnsCount; j++)
                {
                    matrix[i, j] = (random.Next(limit));
                }
            }

            memory.AddNamedMemory("Matrix", matrix);

            memory.AddNamedMemory("result", false);

            var machine = new PRAMMachine <MemoryTypes.CRCW>(processors, memory);

            return(machine);
        }
        /// <summary>
        /// This method prepares PRAM machine ready to start adding two vectors
        /// initialized with random numbers, ranging from 0 to limit. Prepares memory
        /// with two source vectors named 'a' and 'b' and result vector named 'c'.
        ///
        /// This implementation uses VectorAddingProcessor from this namespace.
        /// </summary>
        /// <param name="count">Length of vectors that are going to be added.</param>
        /// <param name="limit">Maximum value, defaults to 10</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count, int limit = 10)
        {
            Random     random = new Random();
            List <int> a      = new List <int>();

            for (int i = 0; i < count; i++)
            {
                a.Add(random.Next(limit));
            }
            List <int> b = new List <int>();

            for (int i = 0; i < count; i++)
            {
                b.Add(random.Next(limit));
            }
            List <Processor> processors = new List <Processor>();

            for (int i = 0; i < count; i++)
            {
                processors.Add(new VectorAddingProcessor());
            }
            PRAM <MemoryTypes.EREW> memory = new PRAM <MemoryTypes.EREW>();

            memory.AddNamedMemory("a", a);
            memory.AddNamedMemory("b", b);
            memory.AddNamedMemory("c", count, 0);
            PRAMMachine <MemoryTypes.EREW> machine = new PRAMMachine <MemoryTypes.EREW>(processors, memory);

            return(machine);
        }
Beispiel #3
0
        /// <summary>
        /// This method prepares PRAM machine ready to sort vector of integers which is
        /// initialized with random numbers, ranging from 0 to limit. Prepares memory
        /// with vectors named 'values' containing numbers to be sorted, 'ranks' which is
        /// used to store rank of every sorted value and 'result' to which final sorted
        /// sequence will be stored.
        ///
        /// This implementation uses SortingProcessor from this namespace.
        /// </summary>
        /// <param name="count">How many values are to be sorted.</param>
        /// <param name="limit">Maximum value, defaults to 30</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count, int limit = 30)
        {
            Random     random = new Random();
            List <int> values = new List <int>();

            for (int i = 0; i < count; i++)
            {
                values.Add(random.Next(30));
            }
            List <Processor> processors = new List <Processor>();

            // we add ranking processors
            for (int i = 0; i < count; i++)
            {
                processors.Add(new SortingProcessor(count));
            }
            // we add comparing processors for each unique pair of values
            for (int i = 0; i < count; i++)
            {
                for (int j = i + 1; j < count; j++)
                {
                    SortingProcessor processor = new SortingProcessor(count, i, j);
                    processors.Add(processor);
                }
            }
            PRAM <newCRCW> memory = new PRAM <newCRCW>();

            memory.AddNamedMemory("values", values);
            memory.AddNamedMemory("ranks", count, 0);
            memory.AddNamedMemory("result", count, 0);
            PRAMMachine <newCRCW> machine = new PRAMMachine <newCRCW>(processors, memory);

            return(machine);
        }
Beispiel #4
0
        /// <summary>
        /// This method prepares PRAM machine ready to calculate ranks of list nodes.
        /// Memory contains random list with two additional vectors used by the algorithm.
        ///
        /// This implementation uses ListRankingProcessor from this namespace
        /// </summary>
        /// <param name="count">List length</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count)
        {
            List <Processor> processors = new List <Processor>();

            for (int i = 0; i < count; i++)
            {
                processors.Add(new ListRankingProcessor(i));
            }
            List <int> next        = new List <int>(new int[count]);
            List <int> takenSpaces = new List <int>();
            Random     random      = new Random();
            int        prev        = random.Next(count);

            next[prev] = -1;
            takenSpaces.Add(prev);
            for (int i = count - 1; i > 0; i--)
            {
                int idx = random.Next(count);
                while (takenSpaces.Contains(idx))
                {
                    idx = random.Next(count);
                }
                takenSpaces.Add(idx);
                next[idx] = prev;
                prev      = idx;
            }
            PRAM <MemoryTypes.EREW> memory = new PRAM <MemoryTypes.EREW>();

            memory.AddNamedMemory("next", next);
            memory.AddNamedMemory("ranks", count, 0);
            memory.AddNamedMemory("pointers", count, 0);
            return(new PRAMMachine <MemoryTypes.EREW>(processors, memory));
        }
Beispiel #5
0
        /// <summary>
        /// This method prepares PRAM machine ready to multiply two random square matrixes.
        /// Matrixes are initialized with values ranging from 0 to limit. In memory matrixes
        /// are represented by vectors of length equal to size^2. Memory is initialized with
        /// three matrixes 'MatrixA' and 'MatrixB' containing initial vaules and 'MatrixC'
        /// in which the multiplication result will be stored. Apart from that memory has
        /// size^2 vectors for storing temporary results.
        ///
        /// This implementation uses MatrixMultiplicationProcessor from this namespace.
        /// </summary>
        /// <param name="size">Size of the matrix</param>
        /// <param name="limit">Maximum value, defaults to 5</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int size = 2, int limit = 5)
        {
            List <Processor> processors = new List <Processor>();

            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    for (int number = 0; number < size; number++)
                    {
                        processors.Add(new MatrixMultiplicationProcessor(number, row, column, size));
                    }
                }
            }
            PRAM <MemoryTypes.CREW> memory = new PRAM <MemoryTypes.CREW>();
            Random     random  = new Random();
            List <int> MatrixA = new List <int>();
            List <int> MatrixB = new List <int>();

            for (int i = 0; i < size * size; i++)
            {
                MatrixA.Add(random.Next(limit));
                MatrixB.Add(random.Next(limit));
            }
            memory.AddNamedMemory("MatrixA", MatrixA);
            memory.AddNamedMemory("MatrixB", MatrixB);
            memory.AddNamedMemory("MatrixC", size * size, 0);
            for (int i = 0; i < size * size; i++)
            {
                memory.AddNamedMemory("temp" + i.ToString(), size, 0);
            }
            PRAMMachine <MemoryTypes.CREW> machine = new PRAMMachine <MemoryTypes.CREW>(processors, memory);

            return(machine);
        }
Beispiel #6
0
 /// <summary>
 /// Constructor of PRAM machine
 /// </summary>
 /// <param name="processors">List of processors that will be used by machine</param>
 /// <param name="memory">Machines memory</param>
 public PRAMMachine(List <Processor> processors, PRAM <MemoryType> memory)
 {
     this.state      = PRAMState.Reading;
     this.tickCount  = 0;
     this.processors = processors;
     for (int i = 0; i < processors.Count; i++)
     {
         processors[i].Number = i;
     }
     this.memory    = memory;
     this.isStopped = false;
 }
Beispiel #7
0
        /// <summary>
        /// This method prepares PRAM machine ready to perform fast addition on a vector
        /// of integer values.
        /// Memory is initialized with vector 'data' that has length count and contains
        /// random integer values ranging from 0 to limit. Final value is stored in
        /// cell 0 of 'data' vector.
        ///
        /// This implementation uses FastAdditionProcessor from this namespace.
        /// </summary>
        /// <param name="count">Length of the 'data' vector.</param>
        /// <param name="limit">Maximum value, defaults to 10.</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count, int limit = 10)
        {
            List <Processor> processors = new List <Processor>();

            for (int i = 0; i < count; i++)
            {
                processors.Add(new FastAdditionProcessor(i, count));
            }
            PRAM <MemoryTypes.EREW> memory = new PRAM <MemoryTypes.EREW>();
            List <int> values = new List <int>();
            Random     random = new Random();

            for (int i = 0; i < 2 * count; i++)
            {
                values.Add(random.Next(limit));
            }
            memory.AddNamedMemory("data", values);
            IPRAMMachine machine = new PRAMMachine <MemoryTypes.EREW>(processors, memory);

            return(machine);
        }
        /// <summary>
        /// This method prepares PRAM machine ready to perform fast addition,
        /// using divide and conquer, on a vector of integer values.
        /// Memory is initialized with vector 'data' that has length count and contains
        /// random integer values ranging from 0 to limit. Apart from that memory has
        /// another vector named 'results' that is used to store temporary results.
        /// Final value is stored in cell 0 of 'results' vector.
        ///
        /// This implementation uses FastAdditionDivideAndConquerProcessor from this namespace.
        /// </summary>
        /// <param name="count">Length of the 'data' vector.</param>
        /// <param name="limit">Maximum value, defaults to 10.</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count, int limit = 10)
        {
            List <Processor> processors = new List <Processor>();

            for (int i = 0; i < (int)Math.Ceiling((double)count / Math.Log(count, 2)); i++)
            {
                processors.Add(new FastAdditionDivideAndConquerProcessor(i, count));
            }
            PRAM <MemoryTypes.EREW> memory = new PRAM <MemoryTypes.EREW>();
            List <int> values = new List <int>();
            Random     random = new Random();

            for (int i = 0; i < count; i++)
            {
                values.Add(random.Next(limit));
            }
            memory.AddNamedMemory("data", values);
            memory.AddNamedMemory("results", ((int)Math.Ceiling(Math.Log(count, 2))), 0);
            IPRAMMachine machine = new PRAMMachine <MemoryTypes.EREW>(processors, memory);

            return(machine);
        }
Beispiel #9
0
        /// <summary>
        /// This method prepares PRAM machine ready to multiply two random square matrixes.
        /// Matrixes are initialized with values ranging from 0 to limit. In memory matrixes
        /// are represented by vectors of length equal to size^2. Memory is initialized with
        /// three matrixes 'MatrixA' and 'MatrixB' containing initial vaules and 'MatrixC'
        /// in which the multiplication result will be stored. Apart from that memory has
        /// size^2 vectors for storing temporary results.
        ///
        /// This implementation uses MatrixMultiplicationUsingMatrixDataTypeProcessor from this namespace.
        /// </summary>
        /// <param name="size">Size of the matrix</param>
        /// <param name="limit">Maximum value, defaults to 5</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int size = 32, int limit = 9)
        {
            List <Processor> processors = new List <Processor>();

            for (int row = 0; row < size; row++)
            {
                for (int column = 0; column < size; column++)
                {
                    processors.Add(new MatrixMultiplicationUsingMatrixDataTypeProcessor(row, column, size));
                }
            }
            var memory = new PRAM <MemoryTypes.CREW>();
            var random = new Random();

            var MatrixA = new Matrix <int>(size, size);
            var MatrixB = new Matrix <int>(1, size);

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    MatrixA[i, j] = (random.Next(limit));
                }
                MatrixB[0, i] = i;
            }

            memory.AddNamedMemory("MatrixA", MatrixA);
            memory.AddNamedMemory("MatrixB", MatrixB);
            memory.AddNamedMemory("resault", 0);
            //for (int number = 0; number < size; number++)
            //{
            //    memory.AddNamedMemory("temp" + number.ToString(), new Matrix<int>(size,size));
            //}
            PRAMMachine <MemoryTypes.CREW> machine = new PRAMMachine <MemoryTypes.CREW>(processors, memory);

            return(machine);
        }
Beispiel #10
0
        /// <summary>
        /// This method prepares PRAM machine ready to perform quick logical and
        /// operation on vector of boolean values.
        /// Memory is initialized with vector 'data' that has length count and contains
        /// random boolean values. Apart from that memory contains one cell vector named
        /// 'result' initialized with true.
        ///
        /// This implementation uses LogicalAndProcessor from this namespace.
        /// </summary>
        /// <param name="conut">Size of the data.</param>
        /// <returns>Ready PRAM machine.</returns>
        public static IPRAMMachine Setup(int count)
        {
            Random     random = new Random();
            List <int> values = new List <int>();

            for (int i = 0; i < count; i++)
            {
                values.Add(random.Next(2));
            }
            List <Processor> processors = new List <Processor>();

            // we add ranking processors
            for (int i = 0; i < count; i++)
            {
                processors.Add(new LogicalAndProcessor(i));
            }
            PRAM <MemoryTypes.ERCW> memory = new PRAM <MemoryTypes.ERCW>();

            memory.AddNamedMemory("data", values);
            memory.AddNamedMemory("result", 1, 1);
            PRAMMachine <MemoryTypes.ERCW> machine = new PRAMMachine <MemoryTypes.ERCW>(processors, memory);

            return(machine);
        }