Exemplo n.º 1
0
        private int[] RunSimdOperation(int[] vector1, int[] vector2, Action <SimpleMemory> operation)
        {
            SimdOperations.ThrowIfVectorsNotEquallyLong(vector1, vector2);

            var originalElementCount = vector1.Length;

            vector1 = vector1.PadToMultipleOf(MaxDegreeOfParallelism);
            vector2 = vector2.PadToMultipleOf(MaxDegreeOfParallelism);

            var elementCount = vector1.Length;
            var memory       = new SimpleMemory(1 + elementCount * 2);

            memory.WriteInt32(VectorsElementCountInt32Index, elementCount);

            for (int i = 0; i < elementCount; i++)
            {
                memory.WriteInt32(VectorElementsStartInt32Index + i, vector1[i]);
                memory.WriteInt32(VectorElementsStartInt32Index + elementCount + i, vector2[i]);
            }

            operation(memory);

            var result = new int[elementCount];

            for (int i = 0; i < elementCount; i++)
            {
                result[i] = memory.ReadInt32(ResultVectorElementsStartInt32Index + i);
            }

            return(result.CutToLength(originalElementCount));
        }
Exemplo n.º 2
0
        public int Run(int startIndex, int length)
        {
            var memory = new SimpleMemory(startIndex + length < 2 ? 2 : startIndex + length);

            memory.WriteInt32(Run_StartIndexInt32Index, startIndex);
            memory.WriteInt32(Run_LengthInt32Index, length);
            Run(memory);
            return(memory.ReadInt32(Run_StartIndexInt32Index));
        }
Exemplo n.º 3
0
        public IEnumerable <Fix64> ParallelizedCalculateIntegerSumUpToNumbers(int[] numbers)
        {
            if (numbers.Length != MaxDegreeOfParallelism)
            {
                throw new ArgumentException(
                          "Provide as many numbers as the degree of parallelism of Fix64Calculator is (" +
                          MaxDegreeOfParallelism + ")");
            }

            var memory = new SimpleMemory(2 * MaxDegreeOfParallelism);

            for (int i = 0; i < numbers.Length; i++)
            {
                memory.WriteInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i, numbers[i]);
            }

            ParallelizedCalculateIntegerSumUpToNumbers(memory);

            var results = new Fix64[MaxDegreeOfParallelism];

            for (int i = 0; i < MaxDegreeOfParallelism; i++)
            {
                var itemOutputStartIndex = ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i * 2;

                results[i] = Fix64.FromRawInts(new[]
                {
                    memory.ReadInt32(itemOutputStartIndex),
                    memory.ReadInt32(itemOutputStartIndex + 1)
                });
            }

            return(results);
        }
        /// <summary>
        /// Creates a <see cref="SimpleMemory"/> instance that stores the image.
        /// </summary>
        /// <param name="image">The image to process.</param>
        /// <param name="contrastValue">The contrast difference value.</param>
        /// <returns>The instance of the created <see cref="SimpleMemory"/>.</returns>
        private SimpleMemory CreateSimpleMemory(Bitmap image, int contrastValue)
        {
            var pixelCount = image.Width * image.Height;
            var cellCount  =
                pixelCount +
                (pixelCount % MaxDegreeOfParallelism != 0 ? MaxDegreeOfParallelism : 0) +
                3;
            var memory = new SimpleMemory(cellCount);

            memory.WriteUInt32(ChangeContrast_ImageWidthIndex, (uint)image.Width);
            memory.WriteUInt32(ChangeContrast_ImageHeightIndex, (uint)image.Height);
            memory.WriteInt32(ChangeContrast_ContrastValueIndex, contrastValue);

            for (int x = 0; x < image.Height; x++)
            {
                for (int y = 0; y < image.Width; y++)
                {
                    var pixel = image.GetPixel(y, x);

                    // This leaves 1 byte unused in each memory cell, but that would make the whole logic a lot more
                    // complicated, so good enough for a sample; if we'd want to optimize memory usage, that would be
                    // needed.
                    memory.Write4Bytes(
                        x * image.Width + y + ChangeContrast_ImageStartIndex,
                        new[] { pixel.R, pixel.G, pixel.B });
                }
            }

            return(memory);
        }
Exemplo n.º 5
0
        public static uint CalculateFactorial(this RecursiveAlgorithms recursiveAlgorithms, short number)
        {
            var memory = new SimpleMemory(2);

            memory.WriteInt32(RecursiveAlgorithms.CalculateFactorial_InputShortIndex, number);
            recursiveAlgorithms.CalculateFactorial(memory);
            return(memory.ReadUInt32(RecursiveAlgorithms.CalculateFactorial_OutputUInt32Index));
        }
Exemplo n.º 6
0
        public virtual void CalculateIntegerSumUpToNumber(SimpleMemory memory)
        {
            var number = memory.ReadInt32(CalculateLargeIntegerSum_InputInt32Index);

            var a = new Fix64(1);
            var b = a;

            for (var i = 1; i < number; i++)
            {
                a += b;
            }

            var integers = a.ToIntegers();

            memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index, integers[0]);
            memory.WriteInt32(CalculateLargeIntegerSum_OutputInt32Index + 1, integers[1]);
        }
Exemplo n.º 7
0
        public int Run(int input)
        {
            var memory = new SimpleMemory(1);

            memory.WriteInt32(Run_InputOutputInt32Index, input);
            Run(memory);
            return(memory.ReadInt32(Run_InputOutputInt32Index));
        }
Exemplo n.º 8
0
        public uint CalculateFibonacchiSeries(short number)
        {
            var memory = new SimpleMemory(2);

            memory.WriteInt32(CalculateFibonacchiSeries_InputShortIndex, number);
            CalculateFibonacchiSeries(memory);
            return(memory.ReadUInt32(CalculateFibonacchiSeries_OutputUInt32Index));
        }
Exemplo n.º 9
0
        public uint CalculateFactorial(short number)
        {
            var memory = new SimpleMemory(2);

            memory.WriteInt32(CalculateFactorial_InputShortIndex, number);
            CalculateFactorial(memory);
            return(memory.ReadUInt32(CalculateFactorial_OutputUInt32Index));
        }
Exemplo n.º 10
0
        public static int Run(this Loopback loopback, int input)
        {
            var memory = new SimpleMemory(1);

            memory.WriteInt32(Loopback.Run_InputOutputInt32Index, input);
            loopback.Run(memory);
            return(memory.ReadInt32(Loopback.Run_InputOutputInt32Index));
        }
Exemplo n.º 11
0
        private static (SimpleMemory, SimpleMemoryAccessor) GenerateMemory(PayloadType type, int cellCount, string inputFileName)
        {
            Console.WriteLine("Generating memory.");
            var memory   = new SimpleMemory(cellCount);
            var accessor = new SimpleMemoryAccessor(memory);

            switch (type)
            {
            case PayloadType.ConstantIntOne:
                for (int i = 0; i < memory.CellCount; i++)
                {
                    memory.WriteInt32(i, 1);
                }
                break;

            case PayloadType.Counter:
                for (int i = 0; i < memory.CellCount; i++)
                {
                    memory.WriteInt32(i, i);
                }
                break;

            case PayloadType.Random:
                var random = new Random();
                for (int i = 0; i < memory.CellCount; i++)
                {
                    memory.WriteInt32(i, random.Next(int.MinValue, int.MaxValue));
                }
                break;

            case PayloadType.BinaryFile:
                using (var fileStream = File.OpenRead(inputFileName))
                {
                    int prefixBytes = 4 * SimpleMemory.MemoryCellSizeBytes;
                    var data        = new byte[fileStream.Length + prefixBytes];
                    fileStream.Read(data, prefixBytes, (int)fileStream.Length);
                    accessor.Set(data, 4);
                }
                break;

            default:
                throw new ArgumentException($"Unknown payload type: {type}.");
            }

            return(memory, accessor);
        }
Exemplo n.º 12
0
        public virtual void ParallelizedCalculateIntegerSumUpToNumbers(SimpleMemory memory)
        {
            var numbers = new int[MaxDegreeOfParallelism];

            var tasks = new Task <TaskResult> [MaxDegreeOfParallelism];

            for (int i = 0; i < MaxDegreeOfParallelism; i++)
            {
                var upToNumber = memory.ReadInt32(ParallelizedCalculateLargeIntegerSum_Int32NumbersStartIndex + i);

                tasks[i] = Task.Factory.StartNew(
                    upToNumberObject =>
                {
                    var a = new Fix64(1);
                    var b = a;

                    for (var j = 1; j < (int)upToNumberObject; j++)
                    {
                        a += b;
                    }

                    var integers = a.ToIntegers();

                    return(new TaskResult
                    {
                        Fix64Low = integers[0],
                        Fix64High = integers[1]
                    });
                }, upToNumber);
            }

            Task.WhenAll(tasks).Wait();

            for (int i = 0; i < MaxDegreeOfParallelism; i++)
            {
                var itemOutputStartIndex = ParallelizedCalculateLargeIntegerSum_OutputInt32sStartIndex + i * 2;

                memory.WriteInt32(itemOutputStartIndex, tasks[i].Result.Fix64Low);
                memory.WriteInt32(itemOutputStartIndex + 1, tasks[i].Result.Fix64High);
            }
        }
Exemplo n.º 13
0
        private void RunSimdOperation(SimpleMemory memory, SimdOperation operation)
        {
            var elementCount = memory.ReadInt32(VectorsElementCountInt32Index);

            int i = 0;

            while (i < elementCount)
            {
                var vector1      = new int[MaxDegreeOfParallelism];
                var vector2      = new int[MaxDegreeOfParallelism];
                var resultVector = new int[MaxDegreeOfParallelism];

                for (int m = 0; m < MaxDegreeOfParallelism; m++)
                {
                    vector1[m] = memory.ReadInt32(VectorElementsStartInt32Index + i + m);
                }

                for (int m = 0; m < MaxDegreeOfParallelism; m++)
                {
                    vector2[m] = memory.ReadInt32(VectorElementsStartInt32Index + i + m + elementCount);
                }

                switch (operation)
                {
                case SimdOperation.Add:
                    resultVector = SimdOperations.AddVectors(vector1, vector2, MaxDegreeOfParallelism);
                    break;

                case SimdOperation.Subtract:
                    resultVector = SimdOperations.SubtractVectors(vector1, vector2, MaxDegreeOfParallelism);
                    break;

                case SimdOperation.Multiply:
                    resultVector = SimdOperations.MultiplyVectors(vector1, vector2, MaxDegreeOfParallelism);
                    break;

                case SimdOperation.Divide:
                    resultVector = SimdOperations.DivideVectors(vector1, vector2, MaxDegreeOfParallelism);
                    break;

                default:
                    break;
                }

                for (int m = 0; m < MaxDegreeOfParallelism; m++)
                {
                    memory.WriteInt32(ResultVectorElementsStartInt32Index + i + m, resultVector[m]);
                }

                i += MaxDegreeOfParallelism;
            }
        }
Exemplo n.º 14
0
        public virtual void Run(SimpleMemory memory)
        {
            var startIndex = memory.ReadInt32(Run_StartIndexInt32Index);
            var length     = memory.ReadInt32(Run_LengthInt32Index);
            var endIndex   = startIndex + length;

            for (int i = startIndex; i < endIndex; i++)
            {
                // Adding 1 to the input so it's visible whether this actually has run, not just the untouched data was
                // sent back.
                memory.WriteInt32(i, memory.ReadInt32(i) + 1);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Creates a <see cref="SimpleMemory"/> object filled with the input values.
        /// </summary>
        /// <param name="iterationsCount">The number of iterations the algorithm uses for calculations.</param>
        /// <returns>Returns a <see cref="SimpleMemory"/> object containing the input values.</returns>
        private static SimpleMemory CreateSimpleMemory(int iterationsCount)
        {
            var simpleMemory = new SimpleMemory(10 + iterationsCount * 3);

            simpleMemory.WriteInt32(MonteCarloAlgorithm.MonteCarloAlgorithm_IterationsCountIndex, iterationsCount);

            for (int i = 0; i < iterationsCount * 3; i++)
            {
                simpleMemory.WriteUInt32(MonteCarloAlgorithm.MonteCarloAlgorithm_RandomNumbersStartIndex + i, (uint)(_random.Next(101)));
            }

            return(simpleMemory);
        }
Exemplo n.º 16
0
        public Fix64 CalculateIntegerSumUpToNumber(int input)
        {
            var memory = new SimpleMemory(2);

            memory.WriteInt32(CalculateLargeIntegerSum_InputInt32Index, input);

            CalculateIntegerSumUpToNumber(memory);

            return(Fix64.FromRawInts(new[]
            {
                memory.ReadInt32(CalculateLargeIntegerSum_OutputInt32Index),
                memory.ReadInt32(CalculateLargeIntegerSum_OutputInt32Index + 1)
            }));
        }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a <see cref="SimpleMemory"/> instance that stores the image.
        /// </summary>
        /// <param name="image">The image to process.</param>
        /// <param name="topLeft">Top left value.</param>
        /// <param name="topMiddle">Top middle value.</param>
        /// <param name="topRight">Top right value.</param>
        /// <param name="middleLeft">Middle left value.</param>
        /// <param name="pixel">The current pixel value.</param>
        /// <param name="middleRight">Middle right value.</param>
        /// <param name="bottomLeft">Bottom left value.</param>
        /// <param name="bottomMiddle">Bottom middle value.</param>
        /// <param name="bottomRight">Bottom right value.</param>
        /// <param name="factor">The value to divide the summed matrix values with.</param>
        /// <param name="offset">Offset value added to the result.</param>
        /// <returns>The instance of the created <see cref="SimpleMemory"/>.</returns>
        private SimpleMemory CreateSimpleMemory(
            Bitmap image,
            int topLeft, int topMiddle, int topRight,
            int middleLeft, int pixel, int middleRight,
            int bottomLeft, int bottomMiddle, int bottomRight,
            int factor = 1, int offset = 0)
        {
            var memory = new SimpleMemory(image.Width * image.Height * 6 + 13);

            memory.WriteUInt32(FilterImage_ImageWidthIndex, (uint)image.Width);
            memory.WriteUInt32(FilterImage_ImageHeightIndex, (uint)image.Height);
            memory.WriteInt32(FilterImage_TopLeftIndex, topLeft);
            memory.WriteInt32(FilterImage_TopMiddleIndex, topMiddle);
            memory.WriteInt32(FilterImage_TopRightIndex, topRight);
            memory.WriteInt32(FilterImage_MiddleLeftIndex, middleLeft);
            memory.WriteInt32(FilterImage_PixelIndex, pixel);
            memory.WriteInt32(FilterImage_MiddleRightIndex, middleRight);
            memory.WriteInt32(FilterImage_BottomLeftIndex, bottomLeft);
            memory.WriteInt32(FilterImage_BottomMiddleIndex, bottomMiddle);
            memory.WriteInt32(FilterImage_BottomRightIndex, bottomRight);
            memory.WriteInt32(FilterImage_FactorIndex, factor);
            memory.WriteInt32(FilterImage_OffsetIndex, offset);

            int size = image.Width * image.Height;

            for (int x = 0; x < image.Height; x++)
            {
                for (int y = 0; y < image.Width; y++)
                {
                    var pixelValue = image.GetPixel(y, x);

                    memory.WriteUInt32((x * image.Width + y) * 3 + FilterImage_ImageStartIndex, pixelValue.R);
                    memory.WriteUInt32((x * image.Width + y) * 3 + 1 + FilterImage_ImageStartIndex, pixelValue.G);
                    memory.WriteUInt32((x * image.Width + y) * 3 + 2 + FilterImage_ImageStartIndex, pixelValue.B);

                    memory.WriteUInt32((x * image.Width + y) * 3 + (size * 3) + FilterImage_ImageStartIndex, pixelValue.R);
                    memory.WriteUInt32((x * image.Width + y) * 3 + 1 + (size * 3) + FilterImage_ImageStartIndex, pixelValue.G);
                    memory.WriteUInt32((x * image.Width + y) * 3 + 2 + (size * 3) + FilterImage_ImageStartIndex, pixelValue.B);
                }
            }

            return(memory);
        }
Exemplo n.º 18
0
 public virtual void Run(SimpleMemory memory)
 {
     // Adding 1 to the input so it's visible whether this actually has run, not just the untouched data was
     // sent back.
     memory.WriteInt32(Run_InputOutputInt32Index, memory.ReadInt32(Run_InputOutputInt32Index) + 1);
 }