Ejemplo n.º 1
0
        public override async System.Threading.Tasks.Task Run()
        {
            await ClockAsync();

            Random rnd = new Random();

            // Testing data
            // Positions array is kept double so that the ulong bitstream can be
            // generated correctly
            double[] positions = new double[data_size];
            for (ulong k = 0; k < data_size; k++)
            {
                //     long random_number = rnd.Next(10,1000);
                //     double double_rnd_number = random_number;
                //     if(!positions.Contains((double) random_number))
                //         positions[k] = (double) random_number;
                //     else
                //         k--;
                // Non-random data:
                positions[k] = (double)k + 1;
            }

            // Write data to ram
            for (long k = 0; k < positions.Length; k++)
            {
                position_ramctrl.Address   = (int)k;
                position_ramctrl.Data      = Funcs.FromDouble(positions[k]);
                position_ramctrl.IsWriting = true;
                position_ramctrl.Enabled   = true;
                await ClockAsync();
            }
            bool running = true;

            position_ramctrl.Enabled = false;
            ready_signal.val         = data_size;
            ready_signal.valid       = true;
            await ClockAsync();

            ready_signal.valid = false;

            ulong          i = 0;
            ulong          j = 1;
            Queue <double> calculated_result_queue = new Queue <double>();

            while (running)
            {
                mag_sim_finished.valid = false;

                if (testing_result_input.valid)
                {
                    // TODO: Rename the control result to something with "control result"
                    double calculated_result = Sim_Funcs.Acceleration_Calc(positions[i], positions[j]);
                    calculated_result_queue.Enqueue(calculated_result);
                    if (i <= (uint)data_size - 2 && j <= (uint)data_size - 1)
                    {
                        double calc_result  = calculated_result_queue.Dequeue();
                        double input_result = Funcs.FromUlong(testing_result_input.val);

                        // Assertion for unittest
                        System.Diagnostics.Debug.Assert((Math.Abs(calc_result - input_result) < 1 / (Math.Pow(10, 7))), "SME acceleration did not match C# acceleration");

                        // if(Math.Abs(calc_result - input_result) > 1/(Math.Pow(10,7))){
                        //     Console.WriteLine("pos {0}: {1}, pos {2}: {3}", i, positions[i], j, positions[j]);
                        //     Console.WriteLine("Acceleration test sim - Got {0}, Expected {1}", input_result, calc_result);
                        // }

                        if (i >= data_size - 2)
                        {
                            running = false;
                            Console.WriteLine("Acceleration test successfully completed");
                        }
                        if (j >= data_size - 1)
                        {
                            i++;
                            j = i + 1;
                        }
                        else
                        {
                            j++;
                        }
                    }
                }
                await ClockAsync();
            }
            mag_sim_finished.valid = true;
        }
Ejemplo n.º 2
0
        public override async System.Threading.Tasks.Task Run()
        {
            await ClockAsync();

            bool   running = true;
            Random rnd     = new Random();

            // TODO: Update so that only one variable is needed to define if we
            // Random or sequential data

            //// Testing data

            // Generate random data for testing
            // double[] random_velocity_data = new double[data_size];
            double[] velocity_data = new double[data_size];

            for (long i = 0; i < data_size; i++)
            {
                // long random_number = rnd.Next(10,1000);
                // if(!random_velocity_data.Contains((double) random_number))
                //     random_velocity_data[i] = (double) random_number;
                // else
                //     i--;
                // Non-random data:
                velocity_data[i] = (double)i + 1;
            }

            // Creating random or non-random data variables as the external
            // variables in the formula
            // double[] random_acceleration_data = new double[data_size];
            double[] acceleration_data = new double[data_size];

            // Generate random data for testing
            for (ulong i = 0; i < data_size; i++)
            {
                // Random data
                // long random_number = rnd.Next(10,100);
                // if(!random_acceleration_data.Contains((double) random_number))
                //     random_acceleration_data[i] = (double) random_number;
                // else
                //     i--;
                // non-random data
                acceleration_data[i] = (double)i + 1;
            }

            // Write initial velocity data to ram
            for (int i = 0; i < data_size; i++)
            {
                // Data points
                velocity_ramctrl.Enabled   = true;
                velocity_ramctrl.Address   = i;
                velocity_ramctrl.Data      = Funcs.FromDouble(velocity_data[i]);
                velocity_ramctrl.IsWriting = true;

                await ClockAsync();
            }

            // Write initial acceleration data to ram
            for (uint i = 0; i < data_size / cache_size; i++)
            {
                acceleration_ramctrl.Enabled = true;
                acceleration_ramctrl.Address = i;
                for (int j = 0; j < cache_size; j++)
                {
                    acceleration_ramctrl.Data[j] = Funcs.FromDouble(acceleration_data[j + (i * (uint)cache_size)]);
                }
                acceleration_ramctrl.IsWriting = true;

                await ClockAsync();
            }
            acceleration_ramctrl.Enabled   = false;
            acceleration_ramctrl.IsWriting = false;
            velocity_ramctrl.Enabled       = false;
            velocity_ramctrl.IsWriting     = false;

            double[] updated_velocity = new double[data_size];
            // Calculate data for tests
            for (long i = 0; i < data_size; i++)
            {
                double update_result = Sim_Funcs.Update_Data_Calc(velocity_data[i], acceleration_data[i], timestep);
                updated_velocity[i] = update_result;
            }

            sim_ready.valid = true;
            await ClockAsync();

            sim_ready.valid = false;

            // Simulate the wait from the cache
            for (long i = 0; i < Math.Abs(data_size / cache_size); i++)
            {
                long random_wait = rnd.Next(5, 50);
                for (long j = 0; j < random_wait; j++)
                {
                    await ClockAsync();

                    data_ready.valid = false;
                }
                data_ready.valid = true;
            }
            await ClockAsync();

            data_ready.valid = false;

            int  k = 0;
            int  n = 0;
            bool receive_data_ready = false;

            while (running)
            {
                if (finished.valid)
                {
                    receive_data_ready = true;
                }

                if (receive_data_ready)
                {
                    if (k < data_size)
                    {
                        velocity_ramctrl.Enabled   = true;
                        velocity_ramctrl.Data      = 0;
                        velocity_ramctrl.IsWriting = false;
                        velocity_ramctrl.Address   = k;
                        k++;
                    }
                    else
                    {
                        velocity_ramctrl.Enabled = false;
                    }

                    if (k - n > 2 || k >= data_size)
                    {
                        double input_result = Funcs.FromUlong(velocity_ramresult.Data);

                        // Assertion for unittest
                        System.Diagnostics.Debug.Assert((Math.Abs(updated_velocity[n] - input_result) < 1 / (Math.Pow(10, 7))), "SME acceleration did not match C# velocity_update");

                        // if(updated_velocity[n] - input_result > 0.0f)
                        //     Console.WriteLine("Update data result - Got {0}, expected {1} at {2}",
                        //             input_result, updated_velocity[n], n);
                        n++;
                    }
                    if (n >= acceleration_data.Length)
                    {
                        running            = false;
                        receive_data_ready = false;
                        // TODO: Add looping functionality - see Lennard_Jones.Simulations
                    }
                }
                await ClockAsync();
            }
        }
Ejemplo n.º 3
0
        public override async System.Threading.Tasks.Task Run()
        {
            await ClockAsync();

            bool   running = true;
            Random rnd     = new Random();


            //// Testing data

            // Generate random data for testing
            double[] position_data = new double[data_size];
            for (long i = 0; i < data_size; i++)
            {
                long random_number = rnd.Next(10, 1000);
                if (!position_data.Contains((double)random_number))
                {
                    position_data[i] = (double)random_number;
                }
                else
                {
                    i--;
                }
            }

            // Creating random or non-random data variables as the external
            // variables in the formula
            double[] random_velocity_data = new double[data_size];
            double[] velocity_data        = new double[data_size];

            // Generate random data for testing
            for (ulong i = 0; i < data_size; i++)
            {
                long random_number = rnd.Next(10, 100);
                if (!random_velocity_data.Contains((double)random_number))
                {
                    random_velocity_data[i] = (double)random_number;
                }
                else
                {
                    i--;
                }
            }

            // Generate data for testing
            for (ulong i = 0; i < data_size; i++)
            {
                velocity_data[i] = i + 1;
            }


            // Write initial data to ram
            for (int i = 0; i < data_size; i++)
            {
                // Data points
                data_point_ramctrl.Enabled   = true;
                data_point_ramctrl.Address   = i;
                data_point_ramctrl.Data      = Funcs.FromDouble(position_data[i]);
                data_point_ramctrl.IsWriting = true;

                // External variable data points
                velocity_data_point_ramctrl.Enabled   = true;
                velocity_data_point_ramctrl.Address   = i;
                velocity_data_point_ramctrl.Data      = Funcs.FromDouble(random_velocity_data[i]);
                velocity_data_point_ramctrl.IsWriting = true;

                await ClockAsync();
            }
            velocity_data_point_ramctrl.Enabled   = false;
            velocity_data_point_ramctrl.IsWriting = false;
            data_point_ramctrl.Enabled            = false;
            data_point_ramctrl.IsWriting          = false;

            double[] updated_data_points = new double[data_size];
            // Calculate data for tests
            for (long i = 0; i < data_size; i++)
            {
                double update_result = Sim_Funcs.Update_Data_Calc(position_data[i], random_velocity_data[i], timestep);
                updated_data_points[i] = update_result;
            }

            sim_ready.valid = true;
            await ClockAsync();

            sim_ready.valid  = false;
            data_ready.valid = true;
            await ClockAsync();

            data_ready.valid = false;

            int  k = 0;
            int  n = 0;
            bool receive_data_ready = false;

            while (running)
            {
                if (finished.valid)
                {
                    receive_data_ready = true;
                }

                if (receive_data_ready)
                {
                    if (k < random_velocity_data.Length)
                    {
                        data_point_ramctrl.Enabled   = true;
                        data_point_ramctrl.Data      = 0;
                        data_point_ramctrl.IsWriting = false;
                        data_point_ramctrl.Address   = k;
                        k++;
                    }
                    else
                    {
                        data_point_ramctrl.Enabled = false;
                    }

                    if (k - n > 2 || k >= random_velocity_data.Length)
                    {
                        double input_result = Funcs.FromUlong(data_point_ramresult.Data);

                        // Assertion for unittest
                        System.Diagnostics.Debug.Assert((Math.Abs(updated_data_points[n] - input_result) < 1 / (Math.Pow(10, 7))), "SME acceleration did not match C# position_update");


                        // if(updated_data_points[n] - input_result > 0.0f)
                        //     Console.WriteLine("Update data result - Got {0}, expected {1} at {2}",
                        //             input_result, updated_data_points[n], n);
                        n++;
                    }
                    if (n >= random_velocity_data.Length)
                    {
                        running            = false;
                        receive_data_ready = false;
                        // TODO: Add looping functionality - see Lennard_Jones.Simulations
                    }
                }
                await ClockAsync();
            }
        }
Ejemplo n.º 4
0
        public override async System.Threading.Tasks.Task Run()
        {
            // Initial await
            await ClockAsync();

            double[] positions  = new double[data_size];
            double[] velocities = new double[data_size];

            for (int i = 0; i < data_size; i++)
            {
                if (init_position.valid && init_velocity.valid)
                {
                    // Get data from master simulation process
                    ulong position = init_position.val;
                    positions[i] = Funcs.FromUlong(position);
                    ulong velocity = init_velocity.val;
                    velocities[i] = Funcs.FromUlong(velocity);

                    // Write initial data to position ram
                    position_ramctrl.Address   = i;
                    position_ramctrl.Data      = position;
                    position_ramctrl.IsWriting = true;
                    position_ramctrl.Enabled   = true;

                    // Write initial data to velocity ram
                    init_velocity_ramctrl.Address = i;
                    // Initial value is 0
                    init_velocity_ramctrl.Data      = velocity;
                    init_velocity_ramctrl.IsWriting = true;
                    init_velocity_ramctrl.Enabled   = true;
                }
                else
                {
                    i--;
                }
                await ClockAsync();
            }

            // MD loop
            for (int k = 0; k < (uint)Number_of_loops.n; k++)
            {
                bool running = true;
                position_ramctrl.Enabled        = false;
                position_ramctrl.Data           = 0;
                position_ramctrl.IsWriting      = false;
                init_velocity_ramctrl.Enabled   = false;
                init_velocity_ramctrl.Data      = 0;
                init_velocity_ramctrl.IsWriting = false;
                acc_ready.val        = data_size;
                acc_ready.valid      = true;
                velocity_reset.valid = true;
                position_reset.valid = true;
                await ClockAsync();

                acc_ready.valid      = false;
                velocity_reset.valid = false;
                position_reset.valid = false;


                // Calculating data for verifying results
                double[] accelerations = new double[data_size];
                for (long i = 0; i < data_size; i++)
                {
                    for (long j = i + 1; j < data_size; j++)
                    {
                        double result = Sim_Funcs.Acceleration_2d_Calc(positions[i], positions[j]);
                        accelerations[i] += result;
                        accelerations[j] += -result;
                    }
                }

                double[] updated_velocity = new double[data_size];
                // Calculate data for tests
                for (long i = 0; i < data_size; i++)
                {
                    // Initial velocity is 0
                    double update_result = Sim_Funcs.Update_Data_Calc(velocities[i], accelerations[i], timestep);
                    updated_velocity[i] = update_result;
                }

                double[] updated_positions = new double[data_size];
                // Calculate data for tests
                for (long i = 0; i < data_size; i++)
                {
                    double update_result = Sim_Funcs.Update_Data_Calc(positions[i], updated_velocity[i], timestep);
                    updated_positions[i] = update_result;
                }

                int  m          = 0;
                int  n          = 0;
                bool data_ready = false;

                while (running)
                {
                    if (finished.valid)
                    {
                        data_ready = true;
                    }

                    if (data_ready)
                    {
                        if (m < data_size)
                        {
                            position_ramctrl.Enabled   = true;
                            position_ramctrl.Data      = 0;
                            position_ramctrl.IsWriting = false;
                            position_ramctrl.Address   = m;
                            m++;
                        }
                        else
                        {
                            position_ramctrl.Enabled = false;
                        }

                        if (m - n > 2 || m >= data_size)
                        {
                            double input_result = Funcs.FromUlong(position_ramresult.Data);
                            if (Math.Abs(updated_positions[n] - input_result) > 0.00001f)
                            {
                                Console.WriteLine("Update position result - Got {0}, expected {1} at {2}",
                                                  input_result, updated_positions[n], n);
                            }
                            n++;
                        }
                        if (n >= data_size)
                        {
                            running    = false;
                            data_ready = false;
                            positions  = updated_positions;
                            velocities = updated_velocity;
                        }
                    }
                    await ClockAsync();
                }
                Console.WriteLine("Loop {0} finished", k);
            }
            sim_finished.valid = true;
        }
Ejemplo n.º 5
0
        public override async System.Threading.Tasks.Task Run()
        {
            await ClockAsync();

            bool   running = true;
            Random rnd     = new Random();

            //// Testing data

            // Generate random data for testing
            double[] coord_data = new double[data_size];
            // for(long k = 0; k < data_size; k++){
            //     long random_number = rnd.Next(10,1000);
            //     if(!xcoord_data.Contains((double) random_number))
            //         xcoord_data[k] = (double) random_number;
            //     else
            //         k--;
            // }

            // Generate data for testing
            for (ulong k = 0; k < data_size; k++)
            {
                coord_data[k] = k + 1;
            }

            long           i = 0;
            long           j = 0;
            Queue <double> calculated_result_queue = new Queue <double>();

            while (running)
            {
                if (i < data_size)
                {
                    output.valid = true;
                    output.val   = Funcs.FromDouble(coord_data[i]);
                    i++;
                }
                if (input.valid)
                {
                    double calculated_result = Sim_Funcs.Magnitude_2d_Calc(coord_data[j]);
                    calculated_result_queue.Enqueue(calculated_result);
                    if (j < (uint)data_size)
                    {
                        double calc_result  = calculated_result_queue.Dequeue();
                        double input_result = Funcs.FromUlong(input.val);

                        // Assertion for unittest
                        System.Diagnostics.Debug.Assert((Math.Abs(calc_result - input_result) < 1 / (Math.Pow(10, 7))), "SME acceleration did not match C# magnitude");


                        // if(Math.Abs(calc_result - input_result) > 1/(Math.Pow(10,7)))
                        //     Console.WriteLine("Magnitude test sim - Got {0}, Expected {1}", input_result, calc_result);

                        if (j >= (uint)data_size - 1)
                        {
                            running = false;
                            Console.WriteLine("Magnitude test successfully completed");
                        }
                        else
                        {
                            j++;
                        }
                    }
                }
                await ClockAsync();
            }
        }