View Javadoc
1   //******************************************************************************
2   //
3   // File:    Unsigned8BitIntegerMatrixBuf.java
4   // Package: edu.rit.mp.buf
5   // Unit:    Class edu.rit.mp.buf.Unsigned8BitIntegerMatrixBuf
6   //
7   // This Java source file is copyright (C) 2009 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.mp.buf;
41  
42  import java.nio.ByteBuffer;
43  
44  import edu.rit.mp.Buf;
45  import edu.rit.mp.Unsigned8BitIntegerBuf;
46  import edu.rit.pj.reduction.IntegerOp;
47  import edu.rit.pj.reduction.Op;
48  import edu.rit.util.Arrays;
49  import edu.rit.util.Range;
50  
51  /**
52   * Class Unsigned8BitIntegerMatrixBuf provides a buffer for a matrix of unsigned
53   * 8-bit integer items sent or received using the Message Protocol (MP). The
54   * matrix row and column strides may be 1 or greater than 1. While an instance
55   * of class Unsigned8BitIntegerMatrixBuf may be constructed directly, normally
56   * you will use a factory method in class {@linkplain
57   * edu.rit.mp.Unsigned8BitIntegerBuf Unsigned8BitIntegerBuf}. See that class for
58   * further information.
59   *
60   * @author Alan Kaminsky
61   * @version 05-Apr-2009
62   */
63  public class Unsigned8BitIntegerMatrixBuf
64          extends Unsigned8BitIntegerBuf {
65  
66  // Hidden data members.
67      int[][] myMatrix;
68      Range myRowRange;
69      Range myColRange;
70      int myLowerRow;
71      int myRowCount;
72      int myRowStride;
73      int myLowerCol;
74      int myColCount;
75      int myColStride;
76  
77  // Exported constructors.
78      /**
79       * Construct a new unsigned 8-bit integer matrix buffer. It is assumed that
80       * the rows and columns of <code>theMatrix</code> are allocated and that each
81       * row of <code>theMatrix</code> has the same number of columns.
82       *
83       * @param theMatrix Matrix.
84       * @param theRowRange Range of rows to include.
85       * @param theColRange Range of columns to include.
86       */
87      public Unsigned8BitIntegerMatrixBuf(int[][] theMatrix,
88              Range theRowRange,
89              Range theColRange) {
90          super(theRowRange.length() * theColRange.length());
91          myMatrix = theMatrix;
92          myRowRange = theRowRange;
93          myColRange = theColRange;
94          myLowerRow = theRowRange.lb();
95          myRowCount = theRowRange.length();
96          myRowStride = theRowRange.stride();
97          myLowerCol = theColRange.lb();
98          myColCount = theColRange.length();
99          myColStride = theColRange.stride();
100     }
101 
102 // Exported operations.
103     /**
104      * {@inheritDoc}
105      *
106      * Obtain the given item from this buffer.
107      * <P>
108      * The <code>get()</code> method must not block the calling thread; if it does,
109      * all message I/O in MP will be blocked.
110      */
111     public int get(int i) {
112         return myMatrix[i2r(i) * myRowStride + myLowerRow][i2c(i) * myColStride + myLowerCol];
113     }
114 
115     /**
116      * {@inheritDoc}
117      *
118      * Store the given item in this buffer.
119      * <P>
120      * The <code>put()</code> method must not block the calling thread; if it does,
121      * all message I/O in MP will be blocked.
122      */
123     public void put(int i,
124             int item) {
125         myMatrix[i2r(i) * myRowStride + myLowerRow][i2c(i) * myColStride + myLowerCol] = item;
126     }
127 
128     /**
129      * {@inheritDoc}
130      *
131      * Copy items from the given buffer to this buffer. The number of items
132      * copied is this buffer's length or <code>theSrc</code>'s length, whichever is
133      * smaller. If <code>theSrc</code> is this buffer, the <code>copy()</code> method
134      * does nothing.
135      * @exception ClassCastException (unchecked exception) Thrown if
136      * <code>theSrc</code>'s item data type is not the same as this buffer's item
137      * data type.
138      */
139     public void copy(Buf theSrc) {
140         if (theSrc == this) {
141         } else if (theSrc instanceof Unsigned8BitIntegerMatrixBuf) {
142             Unsigned8BitIntegerMatrixBuf src
143                     = (Unsigned8BitIntegerMatrixBuf) theSrc;
144             Arrays.copy(src.myMatrix, src.myRowRange, src.myColRange,
145                     this.myMatrix, this.myRowRange, this.myColRange);
146         } else {
147             Unsigned8BitIntegerBuf.defaultCopy((Unsigned8BitIntegerBuf) theSrc, this);
148         }
149     }
150 
151     /**
152      * {@inheritDoc}
153      *
154      * Create a buffer for performing parallel reduction using the given binary
155      * operation. The results of the reduction are placed into this buffer.
156      * @exception ClassCastException (unchecked exception) Thrown if this
157      * buffer's element data type and the given binary operation's argument data
158      * type are not the same.
159      */
160     public Buf getReductionBuf(Op op) {
161         return new Unsigned8BitIntegerMatrixReductionBuf(myMatrix, myRowRange, myColRange, (IntegerOp) op);
162     }
163 
164 // Hidden operations.
165     /**
166      * {@inheritDoc}
167      *
168      * Send as many items as possible from this buffer to the given byte buffer.
169      * <P>
170      * The <code>sendItems()</code> method must not block the calling thread; if it
171      * does, all message I/O in MP will be blocked.
172      */
173     protected int sendItems(int i,
174             ByteBuffer buffer) {
175         int n = 0;
176         int r = i2r(i);
177         int row = r * myRowStride + myLowerRow;
178         int c = i2c(i);
179         int col = c * myColStride + myLowerCol;
180         int ncols = Math.min(myColCount - c, buffer.remaining());
181         while (r < myRowCount && ncols > 0) {
182             int[] myMatrix_row = myMatrix[row];
183             while (c < ncols) {
184                 buffer.put((byte) myMatrix_row[col]);
185                 ++c;
186                 col += myColStride;
187             }
188             n += ncols;
189             ++r;
190             row += myRowStride;
191             c = 0;
192             col = myLowerCol;
193             ncols = Math.min(myColCount, buffer.remaining());
194         }
195         return n;
196     }
197 
198     /**
199      * {@inheritDoc}
200      *
201      * Receive as many items as possible from the given byte buffer to this
202      * buffer.
203      * <P>
204      * The <code>receiveItems()</code> method must not block the calling thread; if
205      * it does, all message I/O in MP will be blocked.
206      */
207     protected int receiveItems(int i,
208             int num,
209             ByteBuffer buffer) {
210         num = Math.min(num, buffer.remaining());
211         int n = 0;
212         int r = i2r(i);
213         int row = r * myRowStride + myLowerRow;
214         int c = i2c(i);
215         int col = c * myColStride + myLowerCol;
216         int ncols = Math.min(myColCount - c, num);
217         while (r < myRowCount && ncols > 0) {
218             int[] myMatrix_row = myMatrix[row];
219             for (c = 0; c < ncols; ++c) {
220                 myMatrix_row[col] = buffer.get() & 0xFF;
221                 col += myColStride;
222             }
223             num -= ncols;
224             n += ncols;
225             ++r;
226             row += myRowStride;
227             col = myLowerCol;
228             ncols = Math.min(myColCount, num);
229         }
230         return n;
231     }
232 
233     /**
234      * Convert the given buffer index to a row index.
235      */
236     int i2r(int i) {
237         return myColCount == 0 ? i : i / myColCount;
238     }
239 
240     /**
241      * Convert the given buffer index to a column index.
242      */
243     int i2c(int i) {
244         return myColCount == 0 ? 0 : i % myColCount;
245     }
246 
247 }