View Javadoc
1   //******************************************************************************
2   //
3   // File:    FloatMatrixBuf.java
4   // Package: edu.rit.mp.buf
5   // Unit:    Class edu.rit.mp.buf.FloatMatrixBuf
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  import java.nio.FloatBuffer;
44  
45  import edu.rit.mp.Buf;
46  import edu.rit.mp.FloatBuf;
47  import edu.rit.pj.reduction.FloatOp;
48  import edu.rit.pj.reduction.Op;
49  import edu.rit.util.Arrays;
50  import edu.rit.util.Range;
51  
52  /**
53   * Class FloatMatrixBuf provides a buffer for a matrix of float items sent or
54   * received using the Message Protocol (MP). The matrix row and column strides
55   * may be 1 or greater than 1. While an instance of class FloatMatrixBuf may be
56   * constructed directly, normally you will use a factory method in class
57   * {@linkplain edu.rit.mp.FloatBuf FloatBuf}. See that class for further
58   * information.
59   *
60   * @author Alan Kaminsky
61   * @version 05-Apr-2009
62   */
63  public class FloatMatrixBuf
64          extends FloatBuf {
65  
66  // Hidden data members.
67      float[][] 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 float matrix buffer. It is assumed that the rows and
80       * columns of <code>theMatrix</code> are allocated and that each row of
81       * <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 FloatMatrixBuf(float[][] 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 float 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             float 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 FloatMatrixBuf) {
142             FloatMatrixBuf src = (FloatMatrixBuf) theSrc;
143             Arrays.copy(src.myMatrix, src.myRowRange, src.myColRange,
144                     this.myMatrix, this.myRowRange, this.myColRange);
145         } else {
146             FloatBuf.defaultCopy((FloatBuf) theSrc, this);
147         }
148     }
149 
150     /**
151      * {@inheritDoc}
152      *
153      * Create a buffer for performing parallel reduction using the given binary
154      * operation. The results of the reduction are placed into this buffer.
155      * @exception ClassCastException (unchecked exception) Thrown if this
156      * buffer's element data type and the given binary operation's argument data
157      * type are not the same.
158      */
159     public Buf getReductionBuf(Op op) {
160         return new FloatMatrixReductionBuf(myMatrix, myRowRange, myColRange, (FloatOp) op);
161     }
162 
163 // Hidden operations.
164     /**
165      * {@inheritDoc}
166      *
167      * Send as many items as possible from this buffer to the given byte buffer.
168      * <P>
169      * The <code>sendItems()</code> method must not block the calling thread; if it
170      * does, all message I/O in MP will be blocked.
171      */
172     protected int sendItems(int i,
173             ByteBuffer buffer) {
174         FloatBuffer floatbuffer = buffer.asFloatBuffer();
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, floatbuffer.remaining());
181         while (r < myRowCount && ncols > 0) {
182             float[] myMatrix_row = myMatrix[row];
183             while (c < ncols) {
184                 floatbuffer.put(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, floatbuffer.remaining());
194         }
195         buffer.position(buffer.position() + 4 * n);
196         return n;
197     }
198 
199     /**
200      * {@inheritDoc}
201      *
202      * Receive as many items as possible from the given byte buffer to this
203      * buffer.
204      * <P>
205      * The <code>receiveItems()</code> method must not block the calling thread; if
206      * it does, all message I/O in MP will be blocked.
207      */
208     protected int receiveItems(int i,
209             int num,
210             ByteBuffer buffer) {
211         FloatBuffer floatbuffer = buffer.asFloatBuffer();
212         num = Math.min(num, floatbuffer.remaining());
213         int n = 0;
214         int r = i2r(i);
215         int row = r * myRowStride + myLowerRow;
216         int c = i2c(i);
217         int col = c * myColStride + myLowerCol;
218         int ncols = Math.min(myColCount - c, num);
219         while (r < myRowCount && ncols > 0) {
220             float[] myMatrix_row = myMatrix[row];
221             for (c = 0; c < ncols; ++c) {
222                 myMatrix_row[col] = floatbuffer.get();
223                 col += myColStride;
224             }
225             num -= ncols;
226             n += ncols;
227             ++r;
228             row += myRowStride;
229             col = myLowerCol;
230             ncols = Math.min(myColCount, num);
231         }
232         buffer.position(buffer.position() + 4 * n);
233         return n;
234     }
235 
236     /**
237      * Convert the given buffer index to a row index.
238      */
239     int i2r(int i) {
240         return myColCount == 0 ? i : i / myColCount;
241     }
242 
243     /**
244      * Convert the given buffer index to a column index.
245      */
246     int i2c(int i) {
247         return myColCount == 0 ? 0 : i % myColCount;
248     }
249 
250 }