Search in sources :

Example 1 with TimingService

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

the class JTransformsTest method jtransforms2DDhtIsFasterThanFht2.

@SpeedTag
@SeededTest
void jtransforms2DDhtIsFasterThanFht2(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Test the forward DHT of data. and reverse transform or the pre-computed correlation.
    final int size = 256;
    final int w = size / 4;
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    // Blob in the centre
    FloatProcessor fp = createProcessor(size, size / 2 - w / 2, size / 2 - w / 2, w, w, null);
    final Fht fht2 = new Fht((float[]) fp.getPixels(), size, false);
    fht2.transform();
    fht2.initialiseFastMultiply();
    // Random blobs, original and correlated
    final int N = 40;
    final float[][] data = new float[N * 2][];
    final int lower = w;
    final int upper = size - w;
    final int range = upper - lower;
    for (int i = 0, j = 0; i < N; i++) {
        final int x = lower + r.nextInt(range);
        final int y = lower + r.nextInt(range);
        fp = createProcessor(size, x, y, w, w, r);
        final float[] pixels = (float[]) fp.getPixels();
        data[j++] = pixels.clone();
        final Fht fht1 = new Fht(pixels, size, false);
        fht1.copyTables(fht2);
        fht2.transform();
        final float[] pixels2 = new float[pixels.length];
        fht2.conjugateMultiply(fht2, pixels2);
        data[j++] = pixels2;
    }
    // CommonUtils.setThreadsBeginN_1D_FFT_2Threads(Long.MAX_VALUE);
    // CommonUtils.setThreadsBeginN_1D_FFT_4Threads(Long.MAX_VALUE);
    CommonUtils.setThreadsBeginN_2D(Long.MAX_VALUE);
    final TimingService ts = new TimingService();
    ts.execute(new ImageJFhtSpeedTask(size, data));
    ts.execute(new ImageJFht2SpeedTask(size, data));
    ts.execute(new JTransformsDhtSpeedTask(size, data));
    ts.repeat();
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    // Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-2).getMean());
    final TimingResult slow = ts.get(-2);
    final TimingResult fast = ts.get(-1);
    logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
Also used : Fht(uk.ac.sussex.gdsc.core.ij.process.Fht) TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) FloatProcessor(ij.process.FloatProcessor) 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)

Example 2 with TimingService

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

the class FrcTest method computeSineIsFaster.

@SeededTest
void computeSineIsFaster() {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    final int steps = 100000;
    final double delta = 2 * Math.PI / steps;
    final double[] angle = new double[steps + 1];
    final double[] cosAngle = new double[steps + 1];
    for (int i = 0; i <= steps; i++) {
        angle[i] = i * delta;
        cosAngle[i] = Math.cos(angle[i]);
    }
    final TimingService ts = new TimingService(100);
    ts.execute(new MyTimingTask("sin") {

        @Override
        public Object run(Object data) {
            double value = 0;
            for (int i = 0; i < angle.length; i++) {
                value += Math.sin(angle[i]);
            }
            return value;
        }
    });
    ts.execute(new MyTimingTask("FastMath.sin") {

        @Override
        public Object run(Object data) {
            double value = 0;
            for (int i = 0; i < angle.length; i++) {
                value += FastMath.sin(angle[i]);
            }
            return value;
        }
    });
    ts.execute(new MyTimingTask("getSine") {

        @Override
        public Object run(Object data) {
            double value = 0;
            for (int i = 0; i < angle.length; i++) {
                value += Frc.getSine(angle[i], cosAngle[i]);
            }
            return value;
        }
    });
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-2).getMean());
    Assertions.assertTrue(ts.get(-1).getMean() < ts.get(-3).getMean());
}
Also used : TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 3 with TimingService

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

the class FrcTest method computeMirroredIsFaster.

@SeededTest
void computeMirroredIsFaster(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    // Sample lines through an image to create a structure.
    final int N = 2048;
    final double[][] data = new double[N * 2][];
    final UniformRandomProvider r = RngUtils.create(seed.getSeed());
    final SharedStateContinuousSampler gs = SamplerUtils.createGaussianSampler(r, 0, 5);
    for (int x = 0, y = 0, y2 = N, i = 0; x < N; x++, y++, y2--) {
        data[i++] = new double[] { x + gs.sample(), y + gs.sample() };
        data[i++] = new double[] { x + gs.sample(), y2 + gs.sample() };
    }
    // Create 2 images
    final Rectangle bounds = new Rectangle(0, 0, N, N);
    ImageJImagePeakResults i1 = createImage(bounds);
    ImageJImagePeakResults i2 = createImage(bounds);
    final int[] indices = SimpleArrayUtils.natural(data.length);
    PermutationSampler.shuffle(r, indices);
    for (final int i : indices) {
        final ImageJImagePeakResults image = i1;
        i1 = i2;
        i2 = image;
        image.add((float) data[i][0], (float) data[i][1], 1);
    }
    i1.end();
    i2.end();
    final ImageProcessor ip1 = i1.getImagePlus().getProcessor();
    final ImageProcessor ip2 = i2.getImagePlus().getProcessor();
    // Test
    final Frc frc = new Frc();
    FloatProcessor[] fft1;
    FloatProcessor[] fft2;
    fft1 = frc.getComplexFft(ip1);
    fft2 = frc.getComplexFft(ip2);
    final float[] dataA1 = (float[]) fft1[0].getPixels();
    final float[] dataB1 = (float[]) fft1[1].getPixels();
    final float[] dataA2 = (float[]) fft2[0].getPixels();
    final float[] dataB2 = (float[]) fft2[1].getPixels();
    final float[] numerator = new float[dataA1.length];
    final float[] absFft1 = new float[dataA1.length];
    final float[] absFft2 = new float[dataA1.length];
    final TimingService ts = new TimingService(10);
    ts.execute(new MyTimingTask("compute") {

        @Override
        public Object run(Object data) {
            Frc.compute(numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirrored") {

        @Override
        public Object run(Object data) {
            Frc.computeMirrored(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    ts.execute(new MyTimingTask("computeMirroredFast") {

        @Override
        public Object run(Object data) {
            Frc.computeMirroredFast(N, numerator, absFft1, absFft2, dataA1, dataB1, dataA2, dataB2);
            return null;
        }
    });
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport(size));
    }
    // The 'Fast' method may not always be faster so just log results
    final TimingResult slow = ts.get(-3);
    final TimingResult fast = ts.get(-2);
    final TimingResult fastest = ts.get(-1);
    logger.log(TestLogUtils.getTimingRecord(slow, fastest));
    logger.log(TestLogUtils.getTimingRecord(fast, fastest));
    // It should be faster than the non mirrored version
    Assertions.assertTrue(ts.get(-1).getMean() <= ts.get(-3).getMean());
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) FloatProcessor(ij.process.FloatProcessor) SharedStateContinuousSampler(org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler) Rectangle(java.awt.Rectangle) ImageJImagePeakResults(uk.ac.sussex.gdsc.smlm.ij.results.ImageJImagePeakResults) ImageProcessor(ij.process.ImageProcessor) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest)

Example 4 with TimingService

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

the class ErfTest method powerApproxIsFaster.

@Test
void powerApproxIsFaster() {
    // Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int range = 5000;
    final int steps = 100000;
    final double[] x = new double[steps];
    final double step = range / steps;
    for (int i = 0; i < steps; i++) {
        x[i] = i * step;
    }
    final TimingService ts = new TimingService(5);
    ts.execute(new PowTimingTask(new MathPow4(), x));
    ts.execute(new PowTimingTask(new FastMathPow4(), x));
    ts.execute(new PowTimingTask(new Pow4(), x));
    ts.execute(new PowTimingTask(new MathPow16(), x));
    ts.execute(new PowTimingTask(new FastMathPow16(), x));
    ts.execute(new PowTimingTask(new Pow16(), x));
    final int size = ts.getSize();
    ts.repeat(size);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    for (int i = 0; i < 2; i++) {
        final int j = -(1 + i * 3);
        Assertions.assertTrue(ts.get(j).getMean() < ts.get(j - 1).getMean());
        Assertions.assertTrue(ts.get(j).getMean() < ts.get(j - 2).getMean());
    }
}
Also used : TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) SeededTest(uk.ac.sussex.gdsc.test.junit5.SeededTest) Test(org.junit.jupiter.api.Test)

Example 5 with TimingService

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

the class GaussianFilterTest method floatFilterInternalIsFasterThanDoubleFilterInternal.

@SpeedTag
@SeededTest
void floatFilterInternalIsFasterThanDoubleFilterInternal(RandomSeed seed) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.HIGH));
    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(true), data, sigma));
        ts.execute(new MyTimingTask(new DpFilter(false), data, sigma));
        ts.execute(new MyTimingTask(new DoubleFilter(true), 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

TimingService (uk.ac.sussex.gdsc.test.utils.TimingService)23 SeededTest (uk.ac.sussex.gdsc.test.junit5.SeededTest)15 UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)14 SpeedTag (uk.ac.sussex.gdsc.test.junit5.SpeedTag)13 TimingResult (uk.ac.sussex.gdsc.test.utils.TimingResult)9 Test (org.junit.jupiter.api.Test)7 LocalList (uk.ac.sussex.gdsc.core.utils.LocalList)6 Gaussian2DFunction (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunction)4 BaseTimingTask (uk.ac.sussex.gdsc.test.utils.BaseTimingTask)3 FloatProcessor (ij.process.FloatProcessor)2 MultivariateNormalMixtureExpectationMaximization (org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization)2 SharedStateContinuousSampler (org.apache.commons.rng.sampling.distribution.SharedStateContinuousSampler)2 DenseMatrix64F (org.ejml.data.DenseMatrix64F)2 GradientCalculator (uk.ac.sussex.gdsc.smlm.fitting.nonlinear.gradient.GradientCalculator)2 ValueProcedure (uk.ac.sussex.gdsc.smlm.function.ValueProcedure)2 Gaussian2DFunctionTest (uk.ac.sussex.gdsc.smlm.function.gaussian.Gaussian2DFunctionTest)2 ImageProcessor (ij.process.ImageProcessor)1 Rectangle (java.awt.Rectangle)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1