View Javadoc
1   //******************************************************************************
2   //
3   // File:    BooleanMatrixBuf.java
4   // Package: edu.rit.mp.buf
5   // Unit:    Class edu.rit.mp.buf.BooleanMatrixBuf
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.BooleanBuf;
45  import edu.rit.mp.Buf;
46  import edu.rit.pj.reduction.BooleanOp;
47  import edu.rit.pj.reduction.Op;
48  import edu.rit.util.Arrays;
49  import edu.rit.util.Range;
50  
51  /**
52   * Class BooleanMatrixBuf provides a buffer for a matrix of Boolean items sent
53   * or received using the Message Protocol (MP). The matrix row and column
54   * strides may be 1 or greater than 1. While an instance of class
55   * BooleanMatrixBuf may be constructed directly, normally you will use a factory
56   * method in class {@linkplain edu.rit.mp.BooleanBuf BooleanBuf}. See that class
57   * for further information.
58   *
59   * @author Alan Kaminsky
60   * @version 05-Apr-2009
61   */
62  public class BooleanMatrixBuf
63          extends BooleanBuf {
64  
65  // Hidden data members.
66      boolean[][] myMatrix;
67      Range myRowRange;
68      Range myColRange;
69      int myLowerRow;
70      int myRowCount;
71      int myRowStride;
72      int myLowerCol;
73      int myColCount;
74      int myColStride;
75  
76  // Exported constructors.
77      /**
78       * Construct a new Boolean matrix buffer. It is assumed that the rows and
79       * columns of <code>theMatrix</code> are allocated and that each row of
80       * <code>theMatrix</code> has the same number of columns.
81       *
82       * @param theMatrix Matrix.
83       * @param theRowRange Range of rows to include.
84       * @param theColRange Range of columns to include.
85       */
86      public BooleanMatrixBuf(boolean[][] theMatrix,
87              Range theRowRange,
88              Range theColRange) {
89          super(theRowRange.length() * theColRange.length());
90          myMatrix = theMatrix;
91          myRowRange = theRowRange;
92          myColRange = theColRange;
93          myLowerRow = theRowRange.lb();
94          myRowCount = theRowRange.length();
95          myRowStride = theRowRange.stride();
96          myLowerCol = theColRange.lb();
97          myColCount = theColRange.length();
98          myColStride = theColRange.stride();
99      }
100 
101 // Exported operations.
102     /**
103      * {@inheritDoc}
104      *
105      * Obtain the given item from this buffer.
106      * <P>
107      * The <code>get()</code> method must not block the calling thread; if it does,
108      * all message I/O in MP will be blocked.
109      */
110     public boolean get(int i) {
111         return myMatrix[i2r(i) * myRowStride + myLowerRow][i2c(i) * myColStride + myLowerCol];
112     }
113 
114     /**
115      * {@inheritDoc}
116      *
117      * Store the given item in this buffer.
118      * <P>
119      * The <code>put()</code> method must not block the calling thread; if it does,
120      * all message I/O in MP will be blocked.
121      */
122     public void put(int i,
123             boolean item) {
124         myMatrix[i2r(i) * myRowStride + myLowerRow][i2c(i) * myColStride + myLowerCol] = item;
125     }
126 
127     /**
128      * {@inheritDoc}
129      *
130      * Copy items from the given buffer to this buffer. The number of items
131      * copied is this buffer's length or <code>theSrc</code>'s length, whichever is
132      * smaller. If <code>theSrc</code> is this buffer, the <code>copy()</code> method
133      * does nothing.
134      * @exception ClassCastException (unchecked exception) Thrown if
135      * <code>theSrc</code>'s item data type is not the same as this buffer's item
136      * data type.
137      */
138     public void copy(Buf theSrc) {
139         if (theSrc == this) {
140         } else if (theSrc instanceof BooleanMatrixBuf) {
141             BooleanMatrixBuf src = (BooleanMatrixBuf) theSrc;
142             Arrays.copy(src.myMatrix, src.myRowRange, src.myColRange,
143                     this.myMatrix, this.myRowRange, this.myColRange);
144         } else {
145             BooleanBuf.defaultCopy((BooleanBuf) theSrc, this);
146         }
147     }
148 
149     /**
150      * {@inheritDoc}
151      *
152      * Create a buffer for performing parallel reduction using the given binary
153      * operation. The results of the reduction are placed into this buffer.
154      * @exception ClassCastException (unchecked exception) Thrown if this
155      * buffer's element data type and the given binary operation's argument data
156      * type are not the same.
157      */
158     public Buf getReductionBuf(Op op) {
159         return new BooleanMatrixReductionBuf(myMatrix, myRowRange, myColRange, (BooleanOp) op);
160     }
161 
162 // Hidden operations.
163     /**
164      * {@inheritDoc}
165      *
166      * Send as many items as possible from this buffer to the given byte buffer.
167      * <P>
168      * The <code>sendItems()</code> method must not block the calling thread; if it
169      * does, all message I/O in MP will be blocked.
170      */
171     protected int sendItems(int i,
172             ByteBuffer buffer) {
173         int n = 0;
174         int r = i2r(i);
175         int row = r * myRowStride + myLowerRow;
176         int c = i2c(i);
177         int col = c * myColStride + myLowerCol;
178         int ncols = Math.min(myColCount - c, buffer.remaining());
179         while (r < myRowCount && ncols > 0) {
180             boolean[] myMatrix_row = myMatrix[row];
181             while (c < ncols) {
182                 buffer.put(myMatrix_row[col] ? (byte) 1 : (byte) 0);
183                 ++c;
184                 col += myColStride;
185             }
186             n += ncols;
187             ++r;
188             row += myRowStride;
189             c = 0;
190             col = myLowerCol;
191             ncols = Math.min(myColCount, buffer.remaining());
192         }
193         return n;
194     }
195 
196     /**
197      * {@inheritDoc}
198      *
199      * Receive as many items as possible from the given byte buffer to this
200      * buffer.
201      * <P>
202      * The <code>receiveItems()</code> method must not block the calling thread; if
203      * it does, all message I/O in MP will be blocked.
204      */
205     protected int receiveItems(int i,
206             int num,
207             ByteBuffer buffer) {
208         num = Math.min(num, buffer.remaining());
209         int n = 0;
210         int r = i2r(i);
211         int row = r * myRowStride + myLowerRow;
212         int c = i2c(i);
213         int col = c * myColStride + myLowerCol;
214         int ncols = Math.min(myColCount - c, num);
215         while (r < myRowCount && ncols > 0) {
216             boolean[] myMatrix_row = myMatrix[row];
217             for (c = 0; c < ncols; ++c) {
218                 myMatrix_row[col] = buffer.get() != 0;
219                 col += myColStride;
220             }
221             num -= ncols;
222             n += ncols;
223             ++r;
224             row += myRowStride;
225             col = myLowerCol;
226             ncols = Math.min(myColCount, num);
227         }
228         return n;
229     }
230 
231     /**
232      * Convert the given buffer index to a row index.
233      */
234     int i2r(int i) {
235         return myColCount == 0 ? i : i / myColCount;
236     }
237 
238     /**
239      * Convert the given buffer index to a column index.
240      */
241     int i2c(int i) {
242         return myColCount == 0 ? 0 : i % myColCount;
243     }
244 
245 }