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