Search in sources :

Example 1 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult 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 TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult 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 3 with TimingResult

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

the class LvmGradientProcedureTest method gradientProcedureIsNotSlowerThanGradientCalculator.

private void gradientProcedureIsNotSlowerThanGradientCalculator(RandomSeed seed, final int nparams, final Type type) {
    Assumptions.assumeTrue(TestSettings.allow(TestComplexity.MEDIUM));
    final int iter = 1000;
    final double[][] alpha = new double[nparams][nparams];
    final double[] beta = new double[nparams];
    final ArrayList<double[]> paramsList = new ArrayList<>(iter);
    final ArrayList<double[]> yList = new ArrayList<>(iter);
    final int[] x = createFakeData(RngUtils.create(seed.getSeed()), nparams, iter, paramsList, yList);
    final int n = x.length;
    final FakeGradientFunction func = new FakeGradientFunction(blockWidth, nparams);
    final boolean mle = type != Type.LSQ;
    final FastLog fastLog = (type == Type.FAST_LOG_MLE) ? getFastLog() : null;
    final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, mle);
    for (int i = 0; i < paramsList.size(); i++) {
        calc.findLinearised(n, yList.get(i), paramsList.get(i), alpha, beta, func);
    }
    for (int i = 0; i < paramsList.size(); i++) {
        final LvmGradientProcedure p = LvmGradientProcedureUtils.create(yList.get(i), func, type, fastLog);
        p.gradient(paramsList.get(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 < iter; i++) {
                final GradientCalculator calc = GradientCalculatorUtils.newCalculator(nparams, mle);
                for (int j = loops; j-- > 0; ) {
                    calc.findLinearised(n, yList.get(i), paramsList.get(k++ % iter), alpha, beta, func);
                }
            }
        }
    };
    final long time1 = t1.getTime();
    final Timer t2 = new Timer(t1.loops) {

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

Example 4 with TimingResult

use of uk.ac.sussex.gdsc.test.utils.TimingResult 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)

Example 5 with TimingResult

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

the class RampedSelectionStrategyTest method speedTest.

private static void speedTest(final int size, boolean faster, int runs) {
    final long[] sum = RampedSelectionStrategy.createRampedSum(size);
    final TimingService ts = new TimingService(runs);
    ts.execute(new BaseTimingTask("search" + size) {

        @Override
        public Object getData(int index) {
            return sum;
        }

        @Override
        public Object run(Object data) {
            for (int key = (int) sum[sum.length - 1]; key-- > 0; ) {
                RampedSelectionStrategy.search(sum, key);
            }
            return null;
        }

        @Override
        public int getSize() {
            return 1;
        }
    });
    ts.execute(new BaseTimingTask("binarySearch" + size) {

        @Override
        public Object getData(int index) {
            return sum[index];
        }

        @Override
        public Object run(Object data) {
            for (int key = (int) sum[sum.length - 1]; key-- > 0; ) {
                RampedSelectionStrategy.binarySearch(sum, key);
            }
            return null;
        }

        @Override
        public int getSize() {
            return 1;
        }
    });
    final int n = ts.repeat();
    ts.repeat(n);
    if (logger.isLoggable(Level.INFO)) {
        logger.info(ts.getReport());
    }
    final TimingResult slow = ts.get((faster) ? ts.getSize() - 2 : ts.getSize() - 1);
    final TimingResult fast = ts.get((faster) ? ts.getSize() - 1 : ts.getSize() - 2);
    logger.log(TestLogUtils.getTimingRecord(slow, fast));
}
Also used : TimingResult(uk.ac.sussex.gdsc.test.utils.TimingResult) TimingService(uk.ac.sussex.gdsc.test.utils.TimingService) BaseTimingTask(uk.ac.sussex.gdsc.test.utils.BaseTimingTask)

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