All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.meteoinfo.chart.jogl.mc.BenchmarkHandler Maven / Gradle / Ivy

There is a newer version: 3.8
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meteoinfo.chart.jogl.mc;

import java.io.*;
import java.util.ArrayList;

/**
 * Created by Primoz on 11. 07. 2016.
 */
public class BenchmarkHandler {

    public static void benchmarkChar(File inputFile, File outFile, final int[] size, final float voxSize[], final char isoValue, int nThreadsMin, int nThreadsMax, int iterations) {
        char[] scalarField;

        if (inputFile != null) {
            System.out.println("PROGRESS: Reading input data.");
            try {
                int idx = 0;
                scalarField = new char[size[0] * size[1] * size[2]];

                DataInputStream in = new DataInputStream(new FileInputStream(inputFile));
                while (in.available() > 0) {
                    // Size does not match
                    if (idx >= scalarField.length) {
                        in.close();
                        System.out.println("Invalid volume size was specified.");
                        return;
                    }

                    scalarField[idx++] = (char) in.readByte();
                }

                in.close();

                // Size does not match
                if (idx != scalarField.length) {
                    System.out.println("Invalid volume size was specified.");
                    return;
                }
            }
            catch (Exception e) {
                System.out.println("Something went wrong while reading the volume");
                return;
            }
        }
        else {
            System.out.println("PROGRESS: Generating volume data.");
            scalarField = VolumeGenerator.generateScalarFieldChar(size);
        }

        final char[] finalScalarField = scalarField;

        System.out.println("PROGRESS: Performing benchmark.");
        StringBuilder benchmarkResults = new StringBuilder();

        for (int nThreads = nThreadsMin; nThreads <= nThreadsMax; nThreads++) {

            final ArrayList times = new ArrayList<>();

            for (int it = 0; it < iterations; it++) {

                // TIMER
                final long start = System.currentTimeMillis();

                ArrayList threads = new ArrayList<>();
                final ArrayList> results = new ArrayList<>();


                // Thread work distribution
                int remainder = size[2] % nThreads;
                int segment = size[2] / nThreads;

                // Z axis offset for vertice position calculation
                int zAxisOffset = 0;


                for (int i = 0; i < nThreads; i++) {
                    // Distribute remainder among first (remainder) threads
                    int segmentSize = (remainder-- > 0) ? segment + 1 : segment;

                    // Padding needs to be added to correctly close the gaps between segments
                    final int paddedSegmentSize = (i != nThreads - 1) ? segmentSize + 1 : segmentSize;


                    // Finished callback
                    final CallbackMC callback = new CallbackMC() {
                        @Override
                        public void run() {
                            results.add(getVertices());
                        }
                    };

                    // Java...
                    final int finalZAxisOffset = zAxisOffset;

                    // Start the thread
                    Thread t = new Thread() {
                        public void run() {
                            MarchingCubes.marchingCubesChar(finalScalarField, new int[]{size[0], size[1], paddedSegmentSize}, size[2], voxSize, isoValue, finalZAxisOffset, callback);
                        }
                    };

                    threads.add(t);
                    t.start();

                    // Correct offsets for next iteration
                    zAxisOffset += segmentSize;
                }

                // Join the threads
                for (int i = 0; i  0) {
                    // Size does not match
                    if (idx >= scalarField.length) {
                        in.close();
                        System.out.println("Invalid volume size was specified.");
                        return;
                    }

                    scalarField[idx++] = in.readShort();
                }

                in.close();

                // Size does not match
                if (idx != scalarField.length) {
                    System.out.println("Invalid volume size was specified.");
                    return;
                }
            }
            catch (Exception e) {
                System.out.println("Something went wrong while reading the volume");
                return;
            }
        }
        else {
            System.out.println("PROGRESS: Generating volume data.");
            scalarField = VolumeGenerator.generateScalarFieldShort(size);
        }

        final short[] finalScalarField = scalarField;

        System.out.println("PROGRESS: Performing benchmark.");
        StringBuilder benchmarkResults = new StringBuilder();

        for (int nThreads = nThreadsMin; nThreads <= nThreadsMax; nThreads++) {

            final ArrayList times = new ArrayList<>();

            for (int it = 0; it < iterations; it++) {

                // TIMER
                final long start = System.currentTimeMillis();

                ArrayList threads = new ArrayList<>();
                final ArrayList> results = new ArrayList<>();


                // Thread work distribution
                int remainder = size[2] % nThreads;
                int segment = size[2] / nThreads;

                // Z axis offset for vertice position calculation
                int zAxisOffset = 0;


                for (int i = 0; i < nThreads; i++) {
                    // Distribute remainder among first (remainder) threads
                    int segmentSize = (remainder-- > 0) ? segment + 1 : segment;

                    // Padding needs to be added to correctly close the gaps between segments
                    final int paddedSegmentSize = (i != nThreads - 1) ? segmentSize + 1 : segmentSize;


                    // Finished callback
                    final CallbackMC callback = new CallbackMC() {
                        @Override
                        public void run() {
                            results.add(getVertices());
                        }
                    };

                    // Java...
                    final int finalZAxisOffset = zAxisOffset;

                    // Start the thread
                    Thread t = new Thread() {
                        public void run() {
                            MarchingCubes.marchingCubesShort(finalScalarField, new int[]{size[0], size[1], paddedSegmentSize}, size[2], voxSize, isoValue, finalZAxisOffset, callback);
                        }
                    };

                    threads.add(t);
                    t.start();

                    // Correct offsets for next iteration
                    zAxisOffset += segmentSize;
                }

                // Join the threads
                for (int i = 0; i  0) {
                    // Size does not match
                    if (idx >= scalarField.length) {
                        in.close();
                        System.out.println("Invalid volume size was specified.");
                        return;
                    }

                    scalarField[idx++] = in.readInt();
                }

                in.close();

                // Size does not match
                if (idx != scalarField.length) {
                    System.out.println("Invalid volume size was specified.");
                    return;
                }
            }
            catch (Exception e) {
                System.out.println("Something went wrong while reading the volume");
                return;
            }
        }
        else {
            System.out.println("PROGRESS: Generating volume data.");
            scalarField = VolumeGenerator.generateScalarFieldInt(size);
        }

        final int[] finalScalarField = scalarField;

        System.out.println("PROGRESS: Performing benchmark.");
        StringBuilder benchmarkResults = new StringBuilder();

        for (int nThreads = nThreadsMin; nThreads <= nThreadsMax; nThreads++) {

            final ArrayList times = new ArrayList<>();

            for (int it = 0; it < iterations; it++) {

                // TIMER
                final long start = System.currentTimeMillis();

                ArrayList threads = new ArrayList<>();
                final ArrayList> results = new ArrayList<>();


                // Thread work distribution
                int remainder = size[2] % nThreads;
                int segment = size[2] / nThreads;

                // Z axis offset for vertice position calculation
                int zAxisOffset = 0;


                for (int i = 0; i < nThreads; i++) {
                    // Distribute remainder among first (remainder) threads
                    int segmentSize = (remainder-- > 0) ? segment + 1 : segment;

                    // Padding needs to be added to correctly close the gaps between segments
                    final int paddedSegmentSize = (i != nThreads - 1) ? segmentSize + 1 : segmentSize;


                    // Finished callback
                    final CallbackMC callback = new CallbackMC() {
                        @Override
                        public void run() {
                            results.add(getVertices());
                        }
                    };

                    // Java...
                    final int finalZAxisOffset = zAxisOffset;

                    // Start the thread
                    Thread t = new Thread() {
                        public void run() {
                            MarchingCubes.marchingCubesInt(finalScalarField, new int[]{size[0], size[1], paddedSegmentSize}, size[2], voxSize, isoValue, finalZAxisOffset, callback);
                        }
                    };

                    threads.add(t);
                    t.start();

                    // Correct offsets for next iteration
                    zAxisOffset += segmentSize;
                }

                // Join the threads
                for (int i = 0; i  0) {
                    // Size does not match
                    if (idx >= scalarField.length) {
                        in.close();
                        System.out.println("Invalid volume size was specified.");
                        return;
                    }

                    scalarField[idx++] = in.readFloat();
                }

                in.close();

                // Size does not match
                if (idx != scalarField.length) {
                    System.out.println("Invalid volume size was specified.");
                    return;
                }
            }
            catch (Exception e) {
                System.out.println("Something went wrong while reading the volume");
                return;
            }
        }
        else {
            System.out.println("PROGRESS: Generating volume data.");
            scalarField = VolumeGenerator.generateScalarFieldFloat(size);
        }

        final float[] finalScalarField = scalarField;

        System.out.println("PROGRESS: Performing benchmark.");
        StringBuilder benchmarkResults = new StringBuilder();

        for (int nThreads = nThreadsMin; nThreads <= nThreadsMax; nThreads++) {

            final ArrayList times = new ArrayList<>();

            for (int it = 0; it < iterations; it++) {

                // TIMER
                final long start = System.currentTimeMillis();

                ArrayList threads = new ArrayList<>();
                final ArrayList> results = new ArrayList<>();


                // Thread work distribution
                int remainder = size[2] % nThreads;
                int segment = size[2] / nThreads;

                // Z axis offset for vertice position calculation
                int zAxisOffset = 0;


                for (int i = 0; i < nThreads; i++) {
                    // Distribute remainder among first (remainder) threads
                    int segmentSize = (remainder-- > 0) ? segment + 1 : segment;

                    // Padding needs to be added to correctly close the gaps between segments
                    final int paddedSegmentSize = (i != nThreads - 1) ? segmentSize + 1 : segmentSize;


                    // Finished callback
                    final CallbackMC callback = new CallbackMC() {
                        @Override
                        public void run() {
                            results.add(getVertices());
                        }
                    };

                    // Java...
                    final int finalZAxisOffset = zAxisOffset;

                    // Start the thread
                    Thread t = new Thread() {
                        public void run() {
                            MarchingCubes.marchingCubesFloat(finalScalarField, new int[]{size[0], size[1], paddedSegmentSize}, size[2], voxSize, isoValue, finalZAxisOffset, callback);
                        }
                    };

                    threads.add(t);
                    t.start();

                    // Correct offsets for next iteration
                    zAxisOffset += segmentSize;
                }

                // Join the threads
                for (int i = 0; i  0) {
                    // Size does not match
                    if (idx >= scalarField.length) {
                        in.close();
                        System.out.println("Invalid volume size was specified.");
                        return;
                    }

                    scalarField[idx++] = in.readDouble();
                }

                in.close();

                // Size does not match
                if (idx != scalarField.length) {
                    System.out.println("Invalid volume size was specified.");
                    return;
                }
            }
            catch (Exception e) {
                System.out.println("Something went wrong while reading the volume");
                return;
            }
        }
        else {
            System.out.println("PROGRESS: Generating volume data.");
            scalarField = VolumeGenerator.generateScalarFieldDouble(size);
        }

        final double[] finalScalarField = scalarField;

        System.out.println("PROGRESS: Performing benchmark.");
        StringBuilder benchmarkResults = new StringBuilder();

        for (int nThreads = nThreadsMin; nThreads <= nThreadsMax; nThreads++) {

            final ArrayList times = new ArrayList<>();

            for (int it = 0; it < iterations; it++) {

                // TIMER
                final long start = System.currentTimeMillis();

                ArrayList threads = new ArrayList<>();
                final ArrayList> results = new ArrayList<>();


                // Thread work distribution
                int remainder = size[2] % nThreads;
                int segment = size[2] / nThreads;

                // Z axis offset for vertice position calculation
                int zAxisOffset = 0;


                for (int i = 0; i < nThreads; i++) {
                    // Distribute remainder among first (remainder) threads
                    int segmentSize = (remainder-- > 0) ? segment + 1 : segment;

                    // Padding needs to be added to correctly close the gaps between segments
                    final int paddedSegmentSize = (i != nThreads - 1) ? segmentSize + 1 : segmentSize;


                    // Finished callback
                    final CallbackMC callback = new CallbackMC() {
                        @Override
                        public void run() {
                            results.add(getVertices());
                        }
                    };

                    // Java...
                    final int finalZAxisOffset = zAxisOffset;

                    // Start the thread
                    Thread t = new Thread() {
                        public void run() {
                            MarchingCubes.marchingCubesDouble(finalScalarField, new int[]{size[0], size[1], paddedSegmentSize}, size[2], voxSize, isoValue, finalZAxisOffset, callback);
                        }
                    };

                    threads.add(t);
                    t.start();

                    // Correct offsets for next iteration
                    zAxisOffset += segmentSize;
                }

                // Join the threads
                for (int i = 0; i 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy