001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.util.parallel.partition;
031
032import java.util.ArrayList;
033import java.util.Arrays;
034import java.util.Collection;
035import java.util.Iterator;
036import java.util.List;
037
038import org.openimaj.util.function.Operation;
039
040/**
041 * A {@link RangePartitioner} partitions data of a known size into
042 * a predefined number of equally sized partitions. If the time taken
043 * for processing each partition with {@link Operation}s varies, 
044 * then this partitioner is not load-balancing (so threads may end up 
045 * waiting whilst others are still working).   
046 * 
047 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
048 *
049 * @param <T> Type of object in the partition
050 */
051public class RangePartitioner<T> implements Partitioner<T> {
052        private final List<T> data;
053        private final int numPartitions;
054        private final int partitionSize;
055        private int currentPartition = 0;
056        private int remainder;
057        
058        /**
059         * Construct with a {@link List} of data and the given number
060         * of partitions.
061         * @param list the data
062         * @param numPartitions the number of partitions
063         */
064        public RangePartitioner(List<T> list, int numPartitions) {
065                this.data = list;
066                
067                int ops = data.size();
068                double div = ops / (double)numPartitions;
069                
070                if (div >= 1) {
071                        partitionSize = (int) div;
072                        remainder = (int) ((div - partitionSize) * numPartitions);
073                        this.numPartitions = numPartitions;
074                } else {
075                        partitionSize = 1;
076                        remainder = 0;
077                        this.numPartitions = ops;
078                }
079        }
080        
081        /**
082         * Construct with a {@link Collection} of data and the given number
083         * of partitions.
084         * @param c the data
085         * @param numPartitions the number of partitions
086         */
087        public RangePartitioner(Collection<T> c, int numPartitions) {
088                this(new ArrayList<T>(c), numPartitions);
089        }
090        
091        /**
092         * Construct with an array of data and the given number
093         * of partitions.
094         * @param array the data
095         * @param numPartitions the number of partitions
096         */
097        public RangePartitioner(T[] array, int numPartitions) {
098                this(Arrays.asList(array), numPartitions);
099        }
100        
101        /**
102         * Construct with a {@link List} of data and the number of
103         * partitions equal to the number of hardware threads.
104         * 
105         * @param list the data
106         */
107        public RangePartitioner(List<T> list) {
108                this(list, Runtime.getRuntime().availableProcessors());
109        }
110        
111        /**
112         * Construct with a {@link Collection} of data and the number of
113         * partitions equal to the number of hardware threads.
114         * 
115         * @param c the data
116         */
117        public RangePartitioner(Collection<T> c) {
118                this(new ArrayList<T>(c), Runtime.getRuntime().availableProcessors());
119        }
120        
121        /**
122         * Construct with an array of data and the number of
123         * partitions equal to the number of hardware threads.
124         * 
125         * @param array the data
126         */
127        public RangePartitioner(T[] array) {
128                this(Arrays.asList(array), Runtime.getRuntime().availableProcessors());
129        }
130        
131        @Override
132        public Iterator<Iterator<T>> getPartitions() {
133                return new Iterator<Iterator<T>>() {
134                        int offset = 0;
135                        
136                        @Override
137                        public boolean hasNext() {
138                                return currentPartition < numPartitions;
139                        }
140
141                        @Override
142                        public Iterator<T> next() {
143                                int start = offset + currentPartition * partitionSize;
144                                int stop = Math.min(offset + (++currentPartition)*partitionSize, data.size());
145                                
146                                if (remainder > 0) {
147                                        stop++;
148                                        
149                                        remainder--;
150                                        offset++;
151                                }
152                                
153                                return data.subList(start, stop).iterator();
154                        }
155
156                        @Override
157                        public void remove() {
158                                throw new UnsupportedOperationException("Not supported");
159                        }
160                };
161        }
162
163}