1 //******************************************************************************
2 //
3 // File: SharedByteArrayBuf.java
4 // Package: edu.rit.mp.buf
5 // Unit: Class edu.rit.mp.buf.SharedByteArrayBuf
6 //
7 // This Java source file is copyright (C) 2007 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.ByteBuf;
46 import edu.rit.pj.reduction.ByteOp;
47 import edu.rit.pj.reduction.Op;
48 import edu.rit.pj.reduction.SharedByteArray;
49 import edu.rit.util.Range;
50
51 /**
52 * Class SharedByteArrayBuf provides a buffer for a multiple thread safe array
53 * of byte items sent or received using the Message Protocol (MP). The array
54 * element stride may be 1 or greater than 1. While an instance of class
55 * SharedByteArrayBuf may be constructed directly, normally you will use a
56 * factory method in class {@linkplain edu.rit.mp.ByteBuf ByteBuf}. See that
57 * class for further information.
58 *
59 * @author Alan Kaminsky
60 * @version 26-Oct-2007
61 */
62 public class SharedByteArrayBuf
63 extends ByteBuf {
64
65 // Hidden data members.
66 SharedByteArray myArray;
67 Range myRange;
68 int myArrayOffset;
69 int myStride;
70
71 // Exported constructors.
72 /**
73 * Construct a new shared byte array buffer.
74 *
75 * @param theArray Shared array.
76 * @param theRange Range of array elements to include in the buffer.
77 */
78 public SharedByteArrayBuf(SharedByteArray theArray,
79 Range theRange) {
80 super(theRange.length());
81 myArray = theArray;
82 myRange = theRange;
83 myArrayOffset = theRange.lb();
84 myStride = theRange.stride();
85 }
86
87 // Exported operations.
88 /**
89 * {@inheritDoc}
90 *
91 * Obtain the given item from this buffer.
92 * <P>
93 * The <code>get()</code> method must not block the calling thread; if it does,
94 * all message I/O in MP will be blocked.
95 */
96 public byte get(int i) {
97 return myArray.get(myArrayOffset + i * myStride);
98 }
99
100 /**
101 * {@inheritDoc}
102 *
103 * Store the given item in this buffer.
104 * <P>
105 * The <code>put()</code> method must not block the calling thread; if it does,
106 * all message I/O in MP will be blocked.
107 */
108 public void put(int i,
109 byte item) {
110 myArray.set(myArrayOffset + i * myStride, item);
111 }
112
113 /**
114 * {@inheritDoc}
115 *
116 * Create a buffer for performing parallel reduction using the given binary
117 * operation. The results of the reduction are placed into this buffer.
118 * @exception ClassCastException (unchecked exception) Thrown if this
119 * buffer's element data type and the given binary operation's argument data
120 * type are not the same.
121 */
122 public Buf getReductionBuf(Op op) {
123 return new SharedByteArrayReductionBuf(myArray, myRange, (ByteOp) op);
124 }
125
126 // Hidden operations.
127 /**
128 * {@inheritDoc}
129 *
130 * Send as many items as possible from this buffer to the given byte buffer.
131 * <P>
132 * The <code>sendItems()</code> method must not block the calling thread; if it
133 * does, all message I/O in MP will be blocked.
134 */
135 protected int sendItems(int i,
136 ByteBuffer buffer) {
137 int index = i;
138 int off = myArrayOffset + i * myStride;
139 while (index < myLength && buffer.remaining() >= 1) {
140 buffer.put(myArray.get(off));
141 ++index;
142 off += myStride;
143 }
144 return index - i;
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * Receive as many items as possible from the given byte buffer to this
151 * buffer.
152 * <P>
153 * The <code>receiveItems()</code> method must not block the calling thread; if
154 * it does, all message I/O in MP will be blocked.
155 */
156 protected int receiveItems(int i,
157 int num,
158 ByteBuffer buffer) {
159 int index = i;
160 int off = myArrayOffset + i * myStride;
161 int max = Math.min(i + num, myLength);
162 while (index < max && buffer.remaining() >= 1) {
163 myArray.set(off, buffer.get());
164 ++index;
165 off += myStride;
166 }
167 return index - i;
168 }
169
170 }