Search in sources :

Example 81 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class Statistic method demo1.

/**
 * Demonstrates usage of this class.
 */
public static void demo1() {
    double[][] values = { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 }, { 4, -8, -10 } };
    DoubleFactory2D factory = DoubleFactory2D.dense;
    DoubleMatrix2D A = factory.make(values);
    System.out.println("\n\nmatrix=" + A);
    System.out.println("\ncovar1=" + covariance(A));
// System.out.println(correlation(covariance(A)));
// System.out.println(distance(A,EUCLID));
// System.out.println(cern.colt.matrixpattern.Converting.toHTML(A.toString()));
// System.out.println(cern.colt.matrixpattern.Converting.toHTML(covariance(A).toString()));
// System.out.println(cern.colt.matrixpattern.Converting.toHTML(correlation(covariance(A)).toString()));
// System.out.println(cern.colt.matrixpattern.Converting.toHTML(distance(A,EUCLID).toString()));
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D) DoubleFactory2D(cern.colt.matrix.DoubleFactory2D)

Example 82 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class BenchmarkMatrix method run.

/**
 * Executes procedure repeatadly until more than minSeconds have elapsed.
 */
protected static void run(double minSeconds, String title, Double2DProcedure function, String[] types, int[] sizes, double[] densities) {
    // int[] sizes = {33,500,1000};
    // double[] densities = {0.001,0.01,0.99};
    // int[] sizes = {3,5,7,9,30,45,60,61,100,200,300,500,800,1000};
    // double[] densities = {0.001,0.01,0.1,0.999};
    // int[] sizes = {3};
    // double[] densities = {0.1};
    DoubleMatrix3D timings = DoubleFactory3D.dense.make(types.length, sizes.length, densities.length);
    cern.colt.Timer runTime = new cern.colt.Timer().start();
    for (int k = 0; k < types.length; k++) {
        // DoubleFactory2D factory = (k==0 ? DoubleFactory2D.dense : k==1 ? DoubleFactory2D.sparse : DoubleFactory2D.rowCompressed);
        // DoubleFactory2D factory = (k==0 ? DoubleFactory2D.dense : k==1 ? DoubleFactory2D.sparse : k==2 ? DoubleFactory2D.rowCompressed : DoubleFactory2D.rowCompressedModified);
        DoubleFactory2D factory = getFactory(types[k]);
        System.out.print("\n@");
        for (int i = 0; i < sizes.length; i++) {
            int size = sizes[i];
            System.out.print("x");
            for (int j = 0; j < densities.length; j++) {
                final double density = densities[j];
                System.out.print(".");
                // System.out.println("   doing density="+density+"...");
                float opsPerSec;
                // if (!((k==1 && density >= 0.1 && size >=100) || (size>5000 && (k==0 || density>1.0E-4) ))) {
                if (!((k > 0 && density >= 0.1 && size >= 500))) {
                    double val = 0.5;
                    // --> help gc before allocating new mem
                    function.A = null;
                    // --> help gc before allocating new mem
                    function.B = null;
                    // --> help gc before allocating new mem
                    function.C = null;
                    // --> help gc before allocating new mem
                    function.D = null;
                    DoubleMatrix2D A = factory.sample(size, size, val, density);
                    DoubleMatrix2D B = factory.sample(size, size, val, density);
                    function.setParameters(A, B);
                    // help gc
                    A = null;
                    // help gc
                    B = null;
                    double ops = function.operations();
                    double secs = BenchmarkKernel.run(minSeconds, function);
                    opsPerSec = (float) (ops / secs);
                } else {
                    // skip this parameter combination (not used in practice & would take a lot of memory and time)
                    opsPerSec = Float.NaN;
                }
                timings.set(k, i, j, opsPerSec);
            // System.out.println(secs);
            // System.out.println(opsPerSec+" Mops/sec\n");
            }
        }
    }
    runTime.stop();
    String sliceAxisName = "type";
    String rowAxisName = "size";
    // "density";
    String colAxisName = "d";
    // String[] sliceNames = {"dense", "sparse"};
    // String[] sliceNames = {"dense", "sparse", "rowCompressed"};
    String[] sliceNames = types;
    hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions;
    // {F.mean, F.median, F.sum};
    hep.aida.bin.BinFunction1D[] aggr = null;
    String[] rowNames = new String[sizes.length];
    String[] colNames = new String[densities.length];
    for (int i = sizes.length; --i >= 0; ) rowNames[i] = Integer.toString(sizes[i]);
    for (int j = densities.length; --j >= 0; ) colNames[j] = Double.toString(densities[j]);
    System.out.println("*");
    // show transposed
    String tmp = rowAxisName;
    rowAxisName = colAxisName;
    colAxisName = tmp;
    String[] tmp2 = rowNames;
    rowNames = colNames;
    colNames = tmp2;
    timings = timings.viewDice(0, 2, 1);
    System.out.println(new cern.colt.matrix.doublealgo.Formatter("%1.3G").toTitleString(timings, sliceNames, rowNames, colNames, sliceAxisName, rowAxisName, colAxisName, "Performance of " + title, aggr));
    /*
	title = "Speedup of dense over sparse";
	DoubleMatrix2D speedup = cern.colt.matrix.doublealgo.Transform.div(timings.viewSlice(0).copy(),timings.viewSlice(1));
	System.out.println("\n"+new cern.colt.matrix.doublealgo.Formatter("%1.3G").toTitleString(speedup,rowNames,colNames,rowAxisName,colAxisName,title,aggr));
	*/
    System.out.println("Run took a total of " + runTime + ". End of run.");
}
Also used : DoubleFactory2D(cern.colt.matrix.DoubleFactory2D) DoubleMatrix3D(cern.colt.matrix.DoubleMatrix3D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 83 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class BenchmarkMatrix method runSpecial.

/**
 * Executes procedure repeatadly until more than minSeconds have elapsed.
 */
protected static void runSpecial(double minSeconds, String title, Double2DProcedure function) {
    int[] sizes = { 10000 };
    double[] densities = { 0.00001 };
    boolean[] sparses = { true };
    DoubleMatrix2D timings = DoubleFactory2D.dense.make(sizes.length, 4);
    cern.colt.Timer runTime = new cern.colt.Timer().start();
    for (int i = 0; i < sizes.length; i++) {
        int size = sizes[i];
        double density = densities[i];
        boolean sparse = sparses[i];
        final DoubleFactory2D factory = (sparse ? DoubleFactory2D.sparse : DoubleFactory2D.dense);
        System.out.print("\n@");
        System.out.print("x");
        double val = 0.5;
        // --> help gc before allocating new mem
        function.A = null;
        // --> help gc before allocating new mem
        function.B = null;
        // --> help gc before allocating new mem
        function.C = null;
        // --> help gc before allocating new mem
        function.D = null;
        DoubleMatrix2D A = factory.sample(size, size, val, density);
        DoubleMatrix2D B = factory.sample(size, size, val, density);
        function.setParameters(A, B);
        // help gc
        A = null;
        // help gc
        B = null;
        float secs = BenchmarkKernel.run(minSeconds, function);
        double ops = function.operations();
        float opsPerSec = (float) (ops / secs);
        timings.viewRow(i).set(0, sparse ? 0 : 1);
        timings.viewRow(i).set(1, size);
        timings.viewRow(i).set(2, density);
        timings.viewRow(i).set(3, opsPerSec);
    // System.out.println(secs);
    // System.out.println(opsPerSec+" Mops/sec\n");
    }
    runTime.stop();
    hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions;
    // {F.mean, F.median, F.sum};
    hep.aida.bin.BinFunction1D[] aggr = null;
    String[] rowNames = null;
    String[] colNames = { "dense (y=1,n=0)", "size", "density", "flops/sec" };
    String rowAxisName = null;
    String colAxisName = null;
    System.out.println("*");
    System.out.println(new cern.colt.matrix.doublealgo.Formatter("%1.3G").toTitleString(timings, rowNames, colNames, rowAxisName, colAxisName, title, aggr));
    System.out.println("Run took a total of " + runTime + ". End of run.");
}
Also used : DoubleFactory2D(cern.colt.matrix.DoubleFactory2D) DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 84 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class BenchmarkMatrix2D method doubleBenchmark.

/**
 * Runs a bench on matrices holding double elements.
 */
public static void doubleBenchmark(int runs, int rows, int columns, String kind, boolean print, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
    System.out.println("benchmarking double matrix");
    // certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
    // this involves primarly read-loops
    cern.colt.Timer timer1 = new cern.colt.Timer();
    cern.colt.Timer timer2 = new cern.colt.Timer();
    cern.colt.Timer timer3 = new cern.colt.Timer();
    cern.colt.Timer timer4 = new cern.colt.Timer();
    cern.colt.Timer emptyLoop = new cern.colt.Timer();
    cern.colt.Timer emptyLoop2 = new cern.colt.Timer();
    emptyLoop.start();
    int dummy = 0;
    for (int i = 0; i < runs; i++) {
        for (int column = 0; column < columns; column++) {
            for (int row = 0; row < rows; row++) {
                dummy++;
            }
        }
    }
    emptyLoop.stop();
    // !!! so that the jitter can't optimize away the whole loop
    System.out.println(dummy);
    emptyLoop2.start();
    dummy = 3;
    double dummy2 = 0;
    for (int i = 0; i < runs; i++) {
        for (int value = 0, column = 0; column < columns; column++) {
            for (int row = 0; row < rows; row++) {
                dummy2 += dummy;
            }
        }
    }
    emptyLoop2.stop();
    // !!! so that the jitter can't optimize away the whole loop
    System.out.println(dummy2);
    long before = Runtime.getRuntime().freeMemory();
    long size = (((long) rows) * columns) * runs;
    DoubleMatrix2D matrix = null;
    if (kind.equals("sparse"))
        matrix = new SparseDoubleMatrix2D(rows, columns, initialCapacity, minLoadFactor, maxLoadFactor);
    else if (kind.equals("dense"))
        matrix = new DenseDoubleMatrix2D(rows, columns);
    else
        // else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
        throw new RuntimeException("unknown kind");
    System.out.println("\nNow filling...");
    // if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
    for (int i = 0; i < runs; i++) {
        matrix.assign(0);
        matrix.ensureCapacity(initialCapacity);
        if (kind.equals("sparse"))
            ((SparseDoubleMatrix2D) matrix).ensureCapacity(initialCapacity);
        timer1.start();
        int value = 0;
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                matrix.setQuick(row, column, value++);
            }
        }
        timer1.stop();
    }
    timer1.display();
    timer1.minus(emptyLoop).display();
    System.out.println(size / timer1.minus(emptyLoop).seconds() + " elements / sec");
    // invite gc
    Runtime.getRuntime().gc();
    try {
        Thread.currentThread().sleep(1000);
    } catch (InterruptedException exc) {
    }
    ;
    long after = Runtime.getRuntime().freeMemory();
    System.out.println("KB needed=" + (before - after) / 1024);
    System.out.println("bytes needed per non-zero=" + (before - after) / (double) matrix.cardinality());
    if (print) {
        System.out.println(matrix);
        if (kind.equals("sparse"))
            System.out.println("map=" + ((SparseDoubleMatrix2D) matrix).elements);
    }
    /*
	if (kind.equals("sparse")) {
		int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
		System.out.println("hashCollisions="+hashCollisions);
		System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
	}
	*/
    System.out.println("\nNow reading...");
    // if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
    timer2.start();
    double element = 0;
    for (int i = 0; i < runs; i++) {
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                element += matrix.getQuick(row, column);
            }
        }
    }
    timer2.stop().display();
    timer2.minus(emptyLoop2).display();
    System.out.println(size / timer2.minus(emptyLoop2).seconds() + " elements / sec");
    if (print)
        System.out.println(matrix);
    // if (kind.equals("sparse")) System.out.println("hashCollisions="+((SparseDoubleMatrix2D)matrix).elements.hashCollisions);
    // !!! so that the jitter can't optimize away the whole loop
    System.out.println(element);
    System.out.println("\nNow reading view...");
    DoubleMatrix2D view = matrix.viewPart(0, 0, rows, columns);
    timer4.start();
    element = 0;
    for (int i = 0; i < runs; i++) {
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                element += view.getQuick(row, column);
            }
        }
    }
    timer4.stop().display();
    timer4.minus(emptyLoop2).display();
    System.out.println(size / timer4.minus(emptyLoop2).seconds() + " elements / sec");
    if (print)
        System.out.println(view);
    // if (kind.equals("sparse")) System.out.println("hashCollisions="+((SparseDoubleMatrix2D)view).elements.hashCollisions);
    // !!! so that the jitter can't optimize away the whole loop
    System.out.println(element);
    System.out.println("\nNow removing...");
    before = Runtime.getRuntime().freeMemory();
    // if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
    for (int i = 0; i < runs; i++) {
        // initializing
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                matrix.setQuick(row, column, 1);
            }
        }
        timer3.start();
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < columns; column++) {
                matrix.setQuick(row, column, 0);
            }
        }
        timer3.stop();
    }
    timer3.display();
    timer3.minus(emptyLoop).display();
    System.out.println(size / timer3.minus(emptyLoop).seconds() + " elements / sec");
    // invite gc
    Runtime.getRuntime().gc();
    try {
        Thread.currentThread().sleep(1000);
    } catch (InterruptedException exc) {
    }
    ;
    after = Runtime.getRuntime().freeMemory();
    System.out.println("KB needed=" + (before - after) / 1024);
    System.out.println("KB free=" + (after / 1024));
    if (print)
        System.out.println(matrix);
    // if (kind.equals("sparse")) System.out.println("hashCollisions"+((SparseDoubleMatrix2D)matrix).elements.hashCollisions);
    System.out.println("bye bye.");
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Example 85 with DoubleMatrix2D

use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.

the class BenchmarkMatrix2D method doubleBenchmarkMult.

/**
 * Runs a bench on matrices holding double elements.
 */
public static void doubleBenchmarkMult(int runs, int rows, int columns, String kind, boolean print, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
    System.out.println("benchmarking double matrix");
    // certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
    // this involves primarly read-loops
    cern.colt.Timer timer1 = new cern.colt.Timer();
    cern.colt.Timer timer2 = new cern.colt.Timer();
    long size = (((long) rows) * columns) * runs;
    DoubleMatrix2D matrix = null;
    if (kind.equals("sparse"))
        matrix = new SparseDoubleMatrix2D(rows, columns, initialCapacity, minLoadFactor, maxLoadFactor);
    else if (kind.equals("dense"))
        matrix = new DenseDoubleMatrix2D(rows, columns);
    else
        // else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
        throw new RuntimeException("unknown kind");
    System.out.println("\nNow multiplying...");
    matrix.assign(1);
    // if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
    for (int i = 0; i < runs; i++) {
        timer1.start();
        cern.colt.matrix.doublealgo.Transform.mult(matrix, 3);
        timer1.stop();
    }
    timer1.display();
    System.out.println(size / timer1.seconds() + " elements / sec");
    if (print) {
        System.out.println(matrix);
    }
    /*
	if (kind.equals("sparse")) {
		int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
		System.out.println("hashCollisions="+hashCollisions);
		System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
	}
	*/
    System.out.println("\nNow multiplying2...");
    matrix.assign(1);
    // if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
    for (int i = 0; i < runs; i++) {
        timer2.start();
        cern.colt.matrix.doublealgo.Transform.mult(matrix, 3);
        timer2.stop();
    }
    timer2.display();
    System.out.println(size / timer2.seconds() + " elements / sec");
    if (print) {
        System.out.println(matrix);
    }
    /*
	if (kind.equals("sparse")) {
		int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
		System.out.println("hashCollisions="+hashCollisions);
		System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
	}
	*/
    System.out.println("bye bye.");
}
Also used : DoubleMatrix2D(cern.colt.matrix.DoubleMatrix2D)

Aggregations

DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)136 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)38 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)36 Algebra (cern.colt.matrix.linalg.Algebra)16 DoubleFactory2D (cern.colt.matrix.DoubleFactory2D)13 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)13 Node (edu.cmu.tetrad.graph.Node)11 Graph (edu.cmu.tetrad.graph.Graph)8 Test (org.junit.Test)6 DoubleMatrixReader (ubic.basecode.io.reader.DoubleMatrixReader)6 StringMatrixReader (ubic.basecode.io.reader.StringMatrixReader)6 DataSet (edu.cmu.tetrad.data.DataSet)5 DoubleArrayList (cern.colt.list.DoubleArrayList)4 TetradMatrix (edu.cmu.tetrad.util.TetradMatrix)4 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)4 AbstractFormatter (cern.colt.matrix.impl.AbstractFormatter)3 Endpoint (edu.cmu.tetrad.graph.Endpoint)3 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)3 BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)3 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)3