예제 #1
0
    public static void CorrelateDFE(double[,] data, int sizeTimeseries,
                                        int numTimeseries, double[] correlations)
    {
        // Calculates correlations on DFE.
                Stopwatch sw = new Stopwatch();
                sw.Start();

                // Make socket
                var socket = new TSocket("localhost", 9090);

                // Buffering is critical. Raw sockets are very slow
                var transport = new TBufferedTransport(socket);

                // Wrap in a protocol
                var protocol = new TBinaryProtocol(transport);

                // Create a client to use the protocol encoder
                var client = new correlationService.Client(protocol);

                sw.Stop();
                Console.WriteLine("Creating a client:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                try {
                        // Connect!
                        sw.Reset(); sw.Start();
                        transport.Open();
                        sw.Stop();
                        Console.WriteLine("Opening connection:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Initialize maxfile
                        sw.Reset(); sw.Start();
                        var maxfile = client.correlation_init();
                        sw.Stop();
                        Console.WriteLine("Initializing maxfile:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Load DFE
                        sw.Reset(); sw.Start();
                        var engine = client.max_load(maxfile, "*");
                        sw.Stop();
                        Console.WriteLine("Loading DFE:\t\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        int numTimesteps = sizeTimeseries;
                        double windowSize = (double)sizeTimeseries;
                        int numBursts = calcNumBursts(numTimeseries);

                        // Get loop length
                        sw.Reset(); sw.Start();
                        List<int> loopLength = new List<int>();
                        loopLength.Add(client.correlation_get_CorrelationKernel_loopLength());
                        sw.Stop();
                        Console.WriteLine("Getting Correlation Kernel loopLength:\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Prepare data for DFE
                        sw.Reset(); sw.Start();

                        double[] precalculations = new double [2 * numTimeseries * numTimesteps];
                        double[] dataPairs = new double [2 * numTimeseries * numTimesteps];

                        int burstSize = 384 / 2;  // for anything other than ISCA this should be 384
                        List<int> inMemLoad = new List<int>();
                        for(int i = 0; i < numBursts * burstSize; i++) {
                                inMemLoad.Add(0);
                        }

                        prepareDataForDFE (data, sizeTimeseries, numTimeseries, numTimesteps, windowSize,
                                           precalculations, dataPairs);

                        List<double> precalculationsVec = new List<double>();
                        List<double> dataPairsVec = new List<double>();
                        for(int i = 0; i < 2 * numTimeseries * numTimesteps; i++) {
                                precalculationsVec.Add(precalculations[i]);
                                dataPairsVec.Add(dataPairs[i]);
                        }

                        sw.Stop();
                        Console.WriteLine("Data reordering time:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Allocate and send input streams to server
                        sw.Reset(); sw.Start();
                        var loopLengthSize = 1;
                        var addressLoopLength = client.malloc_int32_t(loopLengthSize);
                        client.send_data_int32_t(addressLoopLength, loopLength);
                        sw.Stop();
                        var loopLengthTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tSending LoopLength:\t\t(size = {0} bit)\t\t{1}s",
                                          loopLengthSize * 32, loopLengthTime);

                        sw.Reset(); sw.Start();
                        var inMemLoadSize = numBursts * burstSize;
                        var addressInMemLoad = client.malloc_int32_t(inMemLoadSize);
                        client.send_data_int32_t(addressInMemLoad, inMemLoad);
                        sw.Stop();
                        var inMemLoadTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tSending InMemLoad:\t\t(size = {0} bit)\t{1}s",
                                          inMemLoadSize * 32, inMemLoadTime);

                        sw.Reset(); sw.Start();
                        var precalculationsSize = 2 * numTimeseries * numTimesteps;
                        var addressPrecalculations = client.malloc_double(precalculationsSize);
                        client.send_data_double(addressPrecalculations, precalculationsVec);
                        sw.Stop();
                        var precalculationsTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tSending Precalculations:\t(size = {0} bit)\t{1}s",
                                          precalculationsSize * 32, precalculationsTime);

                        sw.Reset(); sw.Start();
                        var dataPairsSize = 2 * numTimeseries * numTimesteps;
                        var addressDataPairs = client.malloc_double(dataPairsSize);
                        client.send_data_double(addressDataPairs, dataPairsVec);
                        sw.Stop();
                        var dataPairsTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tSending DataPairs:\t\t(size = {0} bit)\t{1}s",
                                          dataPairsSize * 64, dataPairsTime);

                        var time = loopLengthTime + inMemLoadTime + precalculationsTime + dataPairsTime;
                        var speed = (loopLengthSize * 32 + inMemLoadSize * 32 + precalculationsSize * 64 + dataPairsSize * 64) / time / 1000000;
                        Console.WriteLine("Sending input streams to server total time:\t{0}s\t(average speed = {1}Mb/s)", time, speed);

                        // Allocate memory for output stream on server
                        sw.Reset(); sw.Start();
                        var addressOutCorrelation = client.malloc_double(numTimesteps * loopLength[0] *
                                                                         correlationNumTopScores * correlationNumPipes +
                                                                         numBursts * 24);
                        var addressOutIndices = client.malloc_int32_t(2 * numTimesteps * loopLength[0] *
                                                                      correlationNumTopScores * correlationNumPipes);
                        sw.Stop();
                        Console.WriteLine("Allocating memory for output stream on server:\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Initialize LMem
                        sw.Reset(); sw.Start();

                        correlation_loadLMem_actions_t_struct actionsLoadLMem = new correlation_loadLMem_actions_t_struct();
                        actionsLoadLMem.Param_numBursts = numBursts;
                        actionsLoadLMem.Param_CorrelationKernel_loopLength = addressLoopLength;
                        actionsLoadLMem.Instream_in_memLoad = addressInMemLoad;

                        var addressActionsLoadLMem = client.send_correlation_loadLMem_actions_t(actionsLoadLMem);
                        client.correlation_loadLMem_run(engine, addressActionsLoadLMem);

                        sw.Stop();
                        Console.WriteLine("LMem initialization:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        //Executing correlation action
                        sw.Reset(); sw.Start();

                        correlation_actions_t_struct actions = new correlation_actions_t_struct() ;
                        actions.Param_numBursts = numBursts;                           // scalar input
                        actions.Param_numSteps = numTimesteps;                         // scalar input
                        actions.Param_numVariables = numTimeseries;                    // scalar input
                        actions.Param_outputLastStep = 1;                              // scalar input
                        actions.Param_windowSize = windowSize;                         // scalar input
                        actions.Instream_in_precalculations = addressPrecalculations;  // streaming reordered input
                        actions.Instream_in_variable_pair = addressDataPairs;          // streaming reordered input
                        actions.Outstream_out_correlation = addressOutCorrelation;     // streaming reordered input
                        actions.Outstream_out_indices = addressOutIndices;             // streaming reordered input
                        var addressActions = client.send_correlation_actions_t(actions);
                        client.correlation_run(engine, addressActions);

                        sw.Stop();
                        Console.WriteLine("Correlation time:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Unload DFE
                        sw.Reset(); sw.Start();
                        client.max_unload(engine);
                        sw.Stop();
                        Console.WriteLine("Unloading DFE:\t\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Get output stream from server
                        sw.Reset(); sw.Start();
                        var outCorrelationSize = numTimesteps * loopLength[0] * correlationNumTopScores * correlationNumPipes + numBursts * 24;
                        List<double> outCorrelation = new List<double>();
                        outCorrelation = client.receive_data_double(addressOutCorrelation, outCorrelationSize);
                        sw.Stop();
                        var outCorrelationTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tGet output stream Correlation:\t(size = {0} bit)\t{1}s",
                                          outCorrelationSize * 64, outCorrelationTime);

                        sw.Reset(); sw.Start();
                        var outIndicesSzie = 2 * numTimesteps * loopLength[0] * correlationNumTopScores * correlationNumPipes;
                        List<int> outIndices = new List<int>();
                        outIndices = client.receive_data_int32_t(addressOutIndices, outIndicesSzie);
                        sw.Stop();
                        var outIndicesTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tGet output stream outIndices:\t(size = {0} bit)\t{1}s",
                                          outIndicesSzie * 32, outIndicesTime);

                        sw.Reset(); sw.Start();
                        loopLengthSize = 1;
                        loopLength = client.receive_data_int32_t(addressLoopLength, loopLengthSize);
                        sw.Stop();
                        loopLengthTime = sw.Elapsed.TotalMilliseconds / 1000;
                        Console.WriteLine("\tGet output stream loopLength:\t(size = {0} bit)\t\t{1}s",
                                          loopLengthSize * 32, loopLengthTime);

                        time = loopLengthTime + outCorrelationTime + outIndicesTime;
                        speed = (loopLengthSize * 32 + outIndicesSzie * 32 + outCorrelationSize * 64) / time / 1000000;
                        Console.WriteLine("Getting output stream from server total time:\t{0}s\t(average speed = {1}Mb/s)", time, speed);

                        // Free allocated memory for streams on server
                        sw.Reset(); sw.Start();
                        client.free(addressLoopLength);
                        client.free(addressInMemLoad);
                        client.free(addressPrecalculations);
                        client.free(addressDataPairs);
                        client.free(addressOutCorrelation);
                        client.free(addressOutIndices);
                        client.free(addressActionsLoadLMem);
                        client.free(addressActions);
                        sw.Stop();
                        Console.WriteLine("Freeing allocated memory for streams on server:\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Free allocated maxfile data
                        sw.Reset(); sw.Start();
                        client.correlation_free();
                        sw.Stop();
                        Console.WriteLine("Freeing allocated maxfile data:\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Close!
                        sw.Reset(); sw.Start();
                        transport.Close();
                        sw.Stop();
                        Console.WriteLine("Closing connection:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

                        // Store data
                        sw.Reset(); sw.Start();

                        int position = 0;
                        int index = 0;
                        int start = (numTimesteps-1) * loopLength[0] * correlationNumTopScores * correlationNumPipes;

                        for (int i=0; i < numTimeseries; i++) {
                                for(int j = 0; j < i; j++) {
                                        correlations[index + j] = outCorrelation[start + position + j];
                                }
                                index += i;
                                position += ((i/12)+1)*12;
                        }

                        sw.Stop();
                        Console.WriteLine("LMem initialization:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);
                } catch (SocketException e) {
                        Console.WriteLine("Could not connect to the server: {0}.", e.Message);
                        Environment.Exit(-1);
                } catch (Exception e) {
                        Console.WriteLine("An error occured: {0}", e.Message);
                        Environment.Exit(-1);
                }
    }
예제 #2
0
    public static void CorrelateDFE(double[,] data, int sizeTimeseries,
                                    int numTimeseries, double[] correlations)
    {
        // Calculates correlations on DFE.
        Stopwatch sw = new Stopwatch();

        sw.Start();

        // Make socket
        var socket = new TSocket("localhost", 9090);

        // Buffering is critical. Raw sockets are very slow
        var transport = new TBufferedTransport(socket);

        // Wrap in a protocol
        var protocol = new TBinaryProtocol(transport);

        // Create a client to use the protocol encoder
        var client = new correlationService.Client(protocol);

        sw.Stop();
        Console.WriteLine("Creating a client:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

        try {
            // Connect!
            sw.Reset(); sw.Start();
            transport.Open();
            sw.Stop();
            Console.WriteLine("Opening connection:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Initialize maxfile
            sw.Reset(); sw.Start();
            var maxfile = client.correlation_init();
            sw.Stop();
            Console.WriteLine("Initializing maxfile:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Load DFE
            sw.Reset(); sw.Start();
            var engine = client.max_load(maxfile, "*");
            sw.Stop();
            Console.WriteLine("Loading DFE:\t\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            int    numTimesteps = sizeTimeseries;
            double windowSize   = (double)sizeTimeseries;
            int    numBursts    = calcNumBursts(numTimeseries);

            // Get loop length
            sw.Reset(); sw.Start();
            List <int> loopLength = new List <int>();
            loopLength.Add(client.correlation_get_CorrelationKernel_loopLength());
            sw.Stop();
            Console.WriteLine("Getting Correlation Kernel loopLength:\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Prepare data for DFE
            sw.Reset(); sw.Start();

            double[] precalculations = new double [2 * numTimeseries * numTimesteps];
            double[] dataPairs       = new double [2 * numTimeseries * numTimesteps];

            int        burstSize = 384 / 2;       // for anything other than ISCA this should be 384
            List <int> inMemLoad = new List <int>();
            for (int i = 0; i < numBursts * burstSize; i++)
            {
                inMemLoad.Add(0);
            }

            prepareDataForDFE(data, sizeTimeseries, numTimeseries, numTimesteps, windowSize,
                              precalculations, dataPairs);

            List <double> precalculationsVec = new List <double>();
            List <double> dataPairsVec       = new List <double>();
            for (int i = 0; i < 2 * numTimeseries * numTimesteps; i++)
            {
                precalculationsVec.Add(precalculations[i]);
                dataPairsVec.Add(dataPairs[i]);
            }

            sw.Stop();
            Console.WriteLine("Data reordering time:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Allocate and send input streams to server
            sw.Reset(); sw.Start();
            var loopLengthSize    = 1;
            var addressLoopLength = client.malloc_int32_t(loopLengthSize);
            client.send_data_int32_t(addressLoopLength, loopLength);
            sw.Stop();
            var loopLengthTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tSending LoopLength:\t\t(size = {0} bit)\t\t{1}s",
                              loopLengthSize * 32, loopLengthTime);

            sw.Reset(); sw.Start();
            var inMemLoadSize    = numBursts * burstSize;
            var addressInMemLoad = client.malloc_int32_t(inMemLoadSize);
            client.send_data_int32_t(addressInMemLoad, inMemLoad);
            sw.Stop();
            var inMemLoadTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tSending InMemLoad:\t\t(size = {0} bit)\t{1}s",
                              inMemLoadSize * 32, inMemLoadTime);

            sw.Reset(); sw.Start();
            var precalculationsSize    = 2 * numTimeseries * numTimesteps;
            var addressPrecalculations = client.malloc_double(precalculationsSize);
            client.send_data_double(addressPrecalculations, precalculationsVec);
            sw.Stop();
            var precalculationsTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tSending Precalculations:\t(size = {0} bit)\t{1}s",
                              precalculationsSize * 32, precalculationsTime);

            sw.Reset(); sw.Start();
            var dataPairsSize    = 2 * numTimeseries * numTimesteps;
            var addressDataPairs = client.malloc_double(dataPairsSize);
            client.send_data_double(addressDataPairs, dataPairsVec);
            sw.Stop();
            var dataPairsTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tSending DataPairs:\t\t(size = {0} bit)\t{1}s",
                              dataPairsSize * 64, dataPairsTime);

            var time  = loopLengthTime + inMemLoadTime + precalculationsTime + dataPairsTime;
            var speed = (loopLengthSize * 32 + inMemLoadSize * 32 + precalculationsSize * 64 + dataPairsSize * 64) / time / 1000000;
            Console.WriteLine("Sending input streams to server total time:\t{0}s\t(average speed = {1}Mb/s)", time, speed);

            // Allocate memory for output stream on server
            sw.Reset(); sw.Start();
            var addressOutCorrelation = client.malloc_double(numTimesteps * loopLength[0] *
                                                             correlationNumTopScores * correlationNumPipes +
                                                             numBursts * 24);
            var addressOutIndices = client.malloc_int32_t(2 * numTimesteps * loopLength[0] *
                                                          correlationNumTopScores * correlationNumPipes);
            sw.Stop();
            Console.WriteLine("Allocating memory for output stream on server:\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Initialize LMem
            sw.Reset(); sw.Start();

            correlation_loadLMem_actions_t_struct actionsLoadLMem = new correlation_loadLMem_actions_t_struct();
            actionsLoadLMem.Param_numBursts = numBursts;
            actionsLoadLMem.Param_CorrelationKernel_loopLength = addressLoopLength;
            actionsLoadLMem.Instream_in_memLoad = addressInMemLoad;

            var addressActionsLoadLMem = client.send_correlation_loadLMem_actions_t(actionsLoadLMem);
            client.correlation_loadLMem_run(engine, addressActionsLoadLMem);

            sw.Stop();
            Console.WriteLine("LMem initialization:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            //Executing correlation action
            sw.Reset(); sw.Start();

            correlation_actions_t_struct actions = new correlation_actions_t_struct();
            actions.Param_numBursts             = numBursts;                           // scalar input
            actions.Param_numSteps              = numTimesteps;                        // scalar input
            actions.Param_numVariables          = numTimeseries;                       // scalar input
            actions.Param_outputLastStep        = 1;                                   // scalar input
            actions.Param_windowSize            = windowSize;                          // scalar input
            actions.Instream_in_precalculations = addressPrecalculations;              // streaming reordered input
            actions.Instream_in_variable_pair   = addressDataPairs;                    // streaming reordered input
            actions.Outstream_out_correlation   = addressOutCorrelation;               // streaming reordered input
            actions.Outstream_out_indices       = addressOutIndices;                   // streaming reordered input
            var addressActions = client.send_correlation_actions_t(actions);
            client.correlation_run(engine, addressActions);

            sw.Stop();
            Console.WriteLine("Correlation time:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Unload DFE
            sw.Reset(); sw.Start();
            client.max_unload(engine);
            sw.Stop();
            Console.WriteLine("Unloading DFE:\t\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Get output stream from server
            sw.Reset(); sw.Start();
            var           outCorrelationSize = numTimesteps * loopLength[0] * correlationNumTopScores * correlationNumPipes + numBursts * 24;
            List <double> outCorrelation     = new List <double>();
            outCorrelation = client.receive_data_double(addressOutCorrelation, outCorrelationSize);
            sw.Stop();
            var outCorrelationTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tGet output stream Correlation:\t(size = {0} bit)\t{1}s",
                              outCorrelationSize * 64, outCorrelationTime);

            sw.Reset(); sw.Start();
            var        outIndicesSzie = 2 * numTimesteps * loopLength[0] * correlationNumTopScores * correlationNumPipes;
            List <int> outIndices     = new List <int>();
            outIndices = client.receive_data_int32_t(addressOutIndices, outIndicesSzie);
            sw.Stop();
            var outIndicesTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tGet output stream outIndices:\t(size = {0} bit)\t{1}s",
                              outIndicesSzie * 32, outIndicesTime);

            sw.Reset(); sw.Start();
            loopLengthSize = 1;
            loopLength     = client.receive_data_int32_t(addressLoopLength, loopLengthSize);
            sw.Stop();
            loopLengthTime = sw.Elapsed.TotalMilliseconds / 1000;
            Console.WriteLine("\tGet output stream loopLength:\t(size = {0} bit)\t\t{1}s",
                              loopLengthSize * 32, loopLengthTime);

            time  = loopLengthTime + outCorrelationTime + outIndicesTime;
            speed = (loopLengthSize * 32 + outIndicesSzie * 32 + outCorrelationSize * 64) / time / 1000000;
            Console.WriteLine("Getting output stream from server total time:\t{0}s\t(average speed = {1}Mb/s)", time, speed);

            // Free allocated memory for streams on server
            sw.Reset(); sw.Start();
            client.free(addressLoopLength);
            client.free(addressInMemLoad);
            client.free(addressPrecalculations);
            client.free(addressDataPairs);
            client.free(addressOutCorrelation);
            client.free(addressOutIndices);
            client.free(addressActionsLoadLMem);
            client.free(addressActions);
            sw.Stop();
            Console.WriteLine("Freeing allocated memory for streams on server:\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Free allocated maxfile data
            sw.Reset(); sw.Start();
            client.correlation_free();
            sw.Stop();
            Console.WriteLine("Freeing allocated maxfile data:\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Close!
            sw.Reset(); sw.Start();
            transport.Close();
            sw.Stop();
            Console.WriteLine("Closing connection:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);

            // Store data
            sw.Reset(); sw.Start();

            int position = 0;
            int index    = 0;
            int start    = (numTimesteps - 1) * loopLength[0] * correlationNumTopScores * correlationNumPipes;

            for (int i = 0; i < numTimeseries; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    correlations[index + j] = outCorrelation[start + position + j];
                }
                index    += i;
                position += ((i / 12) + 1) * 12;
            }

            sw.Stop();
            Console.WriteLine("LMem initialization:\t\t\t\t{0}s", sw.Elapsed.TotalMilliseconds / 1000);
        } catch (SocketException e) {
            Console.WriteLine("Could not connect to the server: {0}.", e.Message);
            Environment.Exit(-1);
        } catch (Exception e) {
            Console.WriteLine("An error occured: {0}", e.Message);
            Environment.Exit(-1);
        }
    }