Package cc.redberry.performance

Source Code of cc.redberry.performance.PairECBenchmark0

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
*   Stanislav Poslavsky   <stvlpos@mail.ru>
*   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.performance;

import cc.redberry.core.context.CC;
import cc.redberry.core.parser.ParserIndices;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.random.TRandom;
import cc.redberry.core.transformations.RenameConflictingIndices;
import cc.redberry.transformation.ec.NaiveSumCollectIP;
import cc.redberry.transformation.ec.PairEC;
import cc.redberry.transformation.ec.ScalarsSplitter;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

public class PairECBenchmark0 {
    public static void main(String[] args) {
        go(1); //JVM cold start
        go(10);
    }

    public static void go(int trys) {
        try {
            //Threads from 1 to maxThread inclusively
            int maxThread = 1;

            //Initialization of outer statistic agregators
            DescriptiveStatistics[] statsMean = new DescriptiveStatistics[maxThread];
            DescriptiveStatistics[] statsSTD = new DescriptiveStatistics[maxThread];
            for (int threads = 1; threads <= maxThread; ++threads) {
                statsMean[threads - 1] = new DescriptiveStatistics();
                statsSTD[threads - 1] = new DescriptiveStatistics();
            }

            for (; trys-- > 0;) { //Each try == one random sum
                DescriptiveStatistics[] stats = new DescriptiveStatistics[maxThread];
                Sum[] sums = random2Sums();
                System.out.println("try " + trys);
                for (int threads = 1; threads <= maxThread; ++threads) {
                    DescriptiveStatistics ds = new DescriptiveStatistics();

                    for (int i = 0; i < 10; ++i) { //Each random sum repeatedly tested 1000 times
                        Sum[] sums_ = new Sum[]{sums[0].clone(), sums[1].clone()};
                        long start = System.nanoTime();
                        PairEC pec = new PairEC(sums_[0], sums_[1], NaiveSumCollectIP.FACTORY, ScalarsSplitter.INSTANCE, threads);
                        Sum res = (Sum) pec.result();
//                        System.out.println(res.size() + " " + res);
                        ds.addValue(System.nanoTime() - start);
                    }

                    stats[threads - 1] = ds; //Saving statistics for one thread
                }

                double norm = stats[0].getMean(); //Normalization to mean execution time in one thread

                for (int threads = 1; threads <= maxThread; ++threads) {
                    statsMean[threads - 1].addValue(stats[threads - 1].getMean() / norm);
                    statsSTD[threads - 1].addValue(stats[threads - 1].getStandardDeviation() / norm);
                }
            }

            //Outputting mean values of relative mean execution time and it's mean STD
            for (int threads = 1; threads <= maxThread; ++threads)
                System.out.println(threads + ": " + statsMean[threads - 1].getMean() + " +- " + statsSTD[threads - 1].getMean());

            System.out.println();

        } catch (InterruptedException e) {
            e.printStackTrace();
            System.exit(0);
        } finally {
        }
    }

    public static Sum[] random2Sums() { //Random sums pair with renamed indices
        Sum[] sums = random2Sums_();
        Product product = new Product(sums);
        RenameConflictingIndices.INSTANCE.transform(product);
        return new Sum[]{
                    (Sum) product.get(0),
                    (Sum) product.get(1)
                };
    }
    public static TRandom random;

    static {
        CC.getNameManager().reset(2312);
        random = new TRandom(1, 2, new int[]{1, 0, 0, 0}, new int[]{2, 0, 0, 0}, true, 7643543L);
    }

    public static Sum[] random2Sums_() { //Здесь могла бы быть ваша реклама
        return new Sum[]{
                    random.nextSum(20, 5, ParserIndices.parse("_mn")),
                    random.nextSum(20, 5, ParserIndices.parse("^mn"))
                };
    }
}
TOP

Related Classes of cc.redberry.performance.PairECBenchmark0

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.