Search in sources :

Example 6 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterUsingForEach.

// Speed test forEach verses equivalent eval() function calls
@SpeedTag
@Test
void functionIsFasterUsingForEach() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final ErfGaussian2DFunction f1 = (ErfGaussian2DFunction) this.f1;
    final LocalList<double[]> params = new LocalList<>();
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double cz1 : testcz1) {
                        for (final double[] w1 : testw1) {
                            for (final double angle1 : testangle1) {
                                final double[] a = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
                                params.add(a);
                            }
                        }
                    }
                }
            }
        }
    }
    final double[][] x = params.toArray(new double[0][]);
    final int runs = 10000 / x.length;
    final TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    ts.execute(new ForEachTimingTask(f1, x, 2));
    ts.execute(new ForEachTimingTask(f1, x, 1));
    ts.execute(new ForEachTimingTask(f1, x, 0));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 1; i <= 3; i++) {
        final TimingResult slow = ts.get(-i - 3);
        final TimingResult fast = ts.get(-i);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 7 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.

the class ErfGaussian2DFunctionTest method functionIsFasterThanEquivalentGaussian2DFunction.

// Speed test verses equivalent Gaussian2DFunction
@SpeedTag
@Test
void functionIsFasterThanEquivalentGaussian2DFunction() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int flags = this.flags & ~GaussianFunctionFactory.FIT_ERF;
    final Gaussian2DFunction gf = GaussianFunctionFactory.create2D(1, maxx, maxy, flags, zModel);
    final boolean zDepth = (flags & GaussianFunctionFactory.FIT_Z) != 0;
    final LocalList<double[]> params1 = new LocalList<>();
    final LocalList<double[]> params2 = new LocalList<>();
    for (final double background : testbackground) {
        // Peak 1
        for (final double signal1 : testsignal1) {
            for (final double cx1 : testcx1) {
                for (final double cy1 : testcy1) {
                    for (final double cz1 : testcz1) {
                        for (final double[] w1 : testw1) {
                            for (final double angle1 : testangle1) {
                                double[] params = createParameters(background, signal1, cx1, cy1, cz1, w1[0], w1[1], angle1);
                                params1.add(params);
                                if (zDepth) {
                                    // Change to a standard free circular function
                                    params = params.clone();
                                    params[Gaussian2DFunction.X_SD] *= zModel.getSx(params[Gaussian2DFunction.Z_POSITION]);
                                    params[Gaussian2DFunction.Y_SD] *= zModel.getSy(params[Gaussian2DFunction.Z_POSITION]);
                                    params[Gaussian2DFunction.Z_POSITION] = 0;
                                    params2.add(params);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    final double[][] x = params1.toArray(new double[0][]);
    final double[][] x2 = (zDepth) ? params2.toArray(new double[0][]) : x;
    final int runs = 10000 / x.length;
    final TimingService ts = new TimingService(runs);
    ts.execute(new FunctionTimingTask(gf, x2, 1));
    ts.execute(new FunctionTimingTask(gf, x2, 0));
    ts.execute(new FunctionTimingTask(f1, x, 2));
    ts.execute(new FunctionTimingTask(f1, x, 1));
    ts.execute(new FunctionTimingTask(f1, x, 0));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 1; i <= 2; i++) {
        final TimingResult slow = ts.get(-i - 3);
        final TimingResult fast = ts.get(-i);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : LocalList(uk.ac.sussex.gdsc.core.utils.LocalList) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) Gaussian2DFunction(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Gaussian2DFunctionTest(uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest) Test(org.junit.jupiter.api.Test)

Example 8 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.

the class LvmGradientProcedureTest method gradientProcedureIsFasterUnrolledThanGradientProcedure.

private void gradientProcedureIsFasterUnrolledThanGradientProcedure(RandomSeed seed, final int nparams, final Type type, final boolean precomputed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 100;
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    createData(RngUtils.create(seed.getSeed()), 1, iter, paramsList, yList);
    // Remove the timing of the function call by creating a dummy function
    final FakeGradientFunction fgf = new FakeGradientFunction(blockWidth, nparams);
    final Gradient1Function func;
    if (precomputed) {
        final double[] b = SimpleArrayUtils.newArray(fgf.size(), 0.1, 1.3);
        func = OffsetGradient1Function.wrapGradient1Function(fgf, b);
    } else {
        func = fgf;
    }
    final FastLog fastLog = type == Type.FAST_LOG_MLE ? getFastLog() : null;
    final IntArrayFormatSupplier msgA = new IntArrayFormatSupplier("A [%d]", 1);
    final IntArrayFormatSupplier msgB = new IntArrayFormatSupplier("B [%d]", 1);
    for (int i = 0; i < paramsList.size(); i++) {
        final LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
        p1.gradient(paramsList.get(i));
        p1.gradient(paramsList.get(i));
        final LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
        p2.gradient(paramsList.get(i));
        p2.gradient(paramsList.get(i));
        // Check they are the same
        Assertions.assertArrayEquals(p1.getAlphaLinear(), p2.getAlphaLinear(), msgA.set(0, i));
        Assertions.assertArrayEquals(p1.beta, p2.beta, msgB.set(0, i));
    }
    // Realistic loops for an optimisation
    final int loops = 15;
    // Run till stable timing
    final Timer t1 = new Timer() {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                final LvmGradientProcedure p1 = createProcedure(type, yList.get(i), func, fastLog);
                for (int j = loops; j-- > 0; ) {
                    p1.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time1 = t1.getTime();
    final Timer t2 = new Timer(t1.loops) {

        @Override
        void run() {
            for (int i = 0, k = 0; i < paramsList.size(); i++) {
                final LvmGradientProcedure p2 = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
                for (int j = loops; j-- > 0; ) {
                    p2.gradient(paramsList.get(k++ % iter));
                }
            }
        }
    };
    final long time2 = t2.getTime();
    logger.log(TestLogUtils.getTimingRecord(new TimingResult(() -> String.format("%s, Precomputed=%b : Standard", type, precomputed), time1), new TimingResult(() -> String.format("Unrolled %d", nparams), time2)));
}
Also used : Gradient1Function(uk.ac.sussex.gdsc.smlm.function.Gradient1Function) OffsetGradient1Function(uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) ArrayList(java.util.ArrayList) IntArrayFormatSupplier(uk.ac.sussex.gdsc.test.utils.functions.IntArrayFormatSupplier) FakeGradientFunction(uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction) FastLog(uk.ac.sussex.gdsc.smlm.function.FastLog)

Example 9 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.

the class FastMathTest method cbrtIsFaster.

// @formatter:on
@SpeedTag
@Test
void cbrtIsFaster() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Q. What is a suitable range for this test?
    final int range = 5;
    final int steps = 10000;
    final double[] x = new double[steps];
    final double total = 2 * range;
    final double step = total / steps;
    for (int i = 0; i < steps; i++) {
        x[i] = -range + i * step;
    }
    final TimingService ts = new TimingService(5);
    ts.execute(new MathPow1_3(x));
    ts.execute(new FastMathPow1_3(x));
    ts.execute(new MathCbrt(x));
    ts.execute(new FastMathCbrt(x));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    final TimingResult fast = ts.get(-1);
    for (int k = 2; k <= 3; k++) {
        final TimingResult slow = ts.get(-k);
        logger.log(TestLogUtils.getTimingRecord(slow, fast));
    }
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) Test(org.junit.jupiter.api.Test)

Example 10 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult in project GDSC-SMLM by aherbert.

the class GaussianFilterTest method floatFilterIsFasterThanDoubleFilter.

@SpeedTag
@SeededTest
void floatFilterIsFasterThanDoubleFilter(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final UniformRandomProvider rg = RngUtils.create(seed.getSeed());
    final float[][] data = new float[10][];
    for (int i = 0; i < data.length; i++) {
        data[i] = createData(rg, size, size);
    }
    final TimingService ts = new TimingService();
    for (final double sigma : sigmas) {
        ts.execute(new MyTimingTask(new FloatFilter(false), data, sigma));
        ts.execute(new MyTimingTask(new DpFilter(false), data, sigma));
        ts.execute(new MyTimingTask(new DoubleFilter(false), data, sigma));
    }
    final int size = ts.getSize();
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    final int n = size / sigmas.length;
    for (int i = 0, j = size; i < sigmas.length; i++, j += n) {
        for (int k = 1; k < n; k++) {
            final TimingResult slow = ts.get(j + k);
            final TimingResult fast = ts.get(j);
            logger.log(TestLogUtils.getTimingRecord(slow, fast));
        }
    }
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SpeedTag(uk.ac.sussex.gdsc.test.junit5.SpeedTag) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Aggregations

TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)11 TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)9 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)6 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)5 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)5 Test (org.junit.jupiter.api.Test)3 FloatProcessor (ij.process.FloatProcessor)2 ArrayList (java.util.ArrayList)2 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)2 FakeGradientFunction (uk.ac.sussex.gdsc.smlm.function.FakeGradientFunction)2 FastLog (uk.ac.sussex.gdsc.smlm.function.FastLog)2 Gaussian2DFunctionTest (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 BaseTimingTask (uk.ac.sussex.gdsc.test.utils.BaseTimingTask)2 ImageProcessor (ij.process.ImageProcessor)1 Rectangle (java.awt.Rectangle)1 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)1 Fht (uk.ac.sussex.gdsc.core.ij.process.Fht)1 Gradient1Function (uk.ac.sussex.gdsc.smlm.function.Gradient1Function)1 OffsetGradient1Function (uk.ac.sussex.gdsc.smlm.function.OffsetGradient1Function)1 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)1