1 /*
  2  * Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @run testng TestSpliterator
 27  */
 28 
 29 import java.lang.foreign.*;
 30 
 31 import java.lang.invoke.VarHandle;
 32 import java.util.LinkedList;
 33 import java.util.List;
 34 import java.util.Spliterator;
 35 import java.util.concurrent.CountedCompleter;
 36 import java.util.concurrent.RecursiveTask;
 37 import java.util.concurrent.atomic.AtomicLong;
 38 import java.util.stream.LongStream;
 39 
 40 import org.testng.annotations.*;
 41 
 42 import static org.testng.Assert.*;
 43 
 44 public class TestSpliterator {
 45 
 46     final static int CARRIER_SIZE = 4;
 47 
 48     @Test(dataProvider = "splits")
 49     public void testSum(int size, int threshold) {
 50         SequenceLayout layout = MemoryLayout.sequenceLayout(size, ValueLayout.JAVA_INT);
 51 
 52         //setup
 53         try (Arena arena = Arena.ofShared()) {
 54             MemorySegment segment = arena.allocate(layout);;
 55             for (int i = 0; i < layout.elementCount(); i++) {
 56                 segment.setAtIndex(ValueLayout.JAVA_INT, i, i);
 57             }
 58             long expected = LongStream.range(0, layout.elementCount()).sum();
 59             //serial
 60             long serial = sum(0, segment);
 61             assertEquals(serial, expected);
 62             //parallel counted completer
 63             long parallelCounted = new SumSegmentCounted(null, segment.spliterator(layout.elementLayout()), threshold).invoke();
 64             assertEquals(parallelCounted, expected);
 65             //parallel recursive action
 66             long parallelRecursive = new SumSegmentRecursive(segment.spliterator(layout.elementLayout()), threshold).invoke();
 67             assertEquals(parallelRecursive, expected);
 68             //parallel stream
 69             long streamParallel = segment.elements(layout.elementLayout()).parallel()
 70                     .reduce(0L, TestSpliterator::sumSingle, Long::sum);
 71             assertEquals(streamParallel, expected);
 72         }
 73     }
 74 
 75     @Test
 76     public void testSumSameThread() {
 77         SequenceLayout layout = MemoryLayout.sequenceLayout(1024, ValueLayout.JAVA_INT);
 78 
 79         //setup
 80         Arena scope = Arena.ofAuto();
 81         MemorySegment segment = scope.allocate(layout);
 82         for (int i = 0; i < layout.elementCount(); i++) {
 83             segment.setAtIndex(ValueLayout.JAVA_INT, i, i);
 84         }
 85         long expected = LongStream.range(0, layout.elementCount()).sum();
 86 
 87         //check that a segment w/o ACQUIRE access mode can still be used from same thread
 88         AtomicLong spliteratorSum = new AtomicLong();
 89         segment.spliterator(layout.elementLayout())
 90                 .forEachRemaining(s -> spliteratorSum.addAndGet(sumSingle(0L, s)));
 91         assertEquals(spliteratorSum.get(), expected);
 92     }
 93 
 94     @Test(expectedExceptions = IllegalArgumentException.class)
 95     public void testBadSpliteratorElementSizeTooBig() {
 96         Arena scope = Arena.ofAuto();
 97         scope.allocate(2, 1)
 98                 .spliterator(ValueLayout.JAVA_INT);
 99     }
100 
101     @Test(expectedExceptions = IllegalArgumentException.class)
102     public void testBadStreamElementSizeTooBig() {
103         Arena scope = Arena.ofAuto();
104         scope.allocate(2, 1)
105                 .elements(ValueLayout.JAVA_INT);
106     }
107 
108     @Test(expectedExceptions = IllegalArgumentException.class)
109     public void testBadSpliteratorElementSizeNotMultiple() {
110         Arena scope = Arena.ofAuto();
111         scope.allocate(7, 1)
112                 .spliterator(ValueLayout.JAVA_INT);
113     }
114 
115     @Test(expectedExceptions = IllegalArgumentException.class)
116     public void testBadStreamElementSizeNotMultiple() {
117         Arena scope = Arena.ofAuto();
118         scope.allocate(7, 1)
119                 .elements(ValueLayout.JAVA_INT);
120     }
121 
122     @Test
123     public void testSpliteratorElementSizeMultipleButNotPowerOfTwo() {
124         Arena scope = Arena.ofAuto();
125         scope.allocate(12, 1)
126                 .spliterator(ValueLayout.JAVA_INT);
127     }
128 
129     @Test
130     public void testStreamElementSizeMultipleButNotPowerOfTwo() {
131         Arena scope = Arena.ofAuto();
132         scope.allocate(12, 1)
133                 .elements(ValueLayout.JAVA_INT);
134     }
135 
136     @Test(expectedExceptions = IllegalArgumentException.class)
137     public void testBadSpliteratorElementSizeZero() {
138         Arena scope = Arena.ofAuto();
139         scope.allocate(7, 1)
140                 .spliterator(MemoryLayout.sequenceLayout(0, ValueLayout.JAVA_INT));
141     }
142 
143     @Test(expectedExceptions = IllegalArgumentException.class)
144     public void testBadStreamElementSizeZero() {
145         Arena scope = Arena.ofAuto();
146         scope.allocate(7, 1)
147                 .elements(MemoryLayout.sequenceLayout(0, ValueLayout.JAVA_INT));
148     }
149 
150     @Test(expectedExceptions = IllegalArgumentException.class)
151     public void testHyperAligned() {
152         Arena scope = Arena.ofAuto();
153         MemorySegment segment = scope.allocate(8, 1);
154         // compute an alignment constraint (in bytes) which exceed that of the native segment
155         long bigByteAlign = Long.lowestOneBit(segment.address()) << 1;
156         segment.elements(MemoryLayout.sequenceLayout(2, ValueLayout.JAVA_INT.withByteAlignment(bigByteAlign)));
157     }
158 
159     static long sumSingle(long acc, MemorySegment segment) {
160         return acc + segment.getAtIndex(ValueLayout.JAVA_INT, 0);
161     }
162 
163     static long sum(long start, MemorySegment segment) {
164         long sum = start;
165         int length = (int)segment.byteSize();
166         for (int i = 0 ; i < length / CARRIER_SIZE ; i++) {
167             sum += segment.getAtIndex(ValueLayout.JAVA_INT, i);
168         }
169         return sum;
170     }
171 
172     static class SumSegmentCounted extends CountedCompleter<Long> {
173 
174         final long threshold;
175         long localSum = 0;
176         List<SumSegmentCounted> children = new LinkedList<>();
177 
178         private Spliterator<MemorySegment> segmentSplitter;
179 
180         SumSegmentCounted(SumSegmentCounted parent, Spliterator<MemorySegment> segmentSplitter, long threshold) {
181             super(parent);
182             this.segmentSplitter = segmentSplitter;
183             this.threshold = threshold;
184         }
185 
186         @Override
187         public void compute() {
188             Spliterator<MemorySegment> sub;
189             while (segmentSplitter.estimateSize() > threshold &&
190                     (sub = segmentSplitter.trySplit()) != null) {
191                 addToPendingCount(1);
192                 SumSegmentCounted child = new SumSegmentCounted(this, sub, threshold);
193                 children.add(child);
194                 child.fork();
195             }
196             segmentSplitter.forEachRemaining(slice -> {
197                 localSum += sumSingle(0, slice);
198             });
199             tryComplete();
200         }
201 
202         @Override
203         public Long getRawResult() {
204             long sum = localSum;
205             for (SumSegmentCounted c : children) {
206                 sum += c.getRawResult();
207             }
208             return sum;
209         }
210      }
211 
212     static class SumSegmentRecursive extends RecursiveTask<Long> {
213 
214         final long threshold;
215         private final Spliterator<MemorySegment> splitter;
216         private long result;
217 
218         SumSegmentRecursive(Spliterator<MemorySegment> splitter, long threshold) {
219             this.splitter = splitter;
220             this.threshold = threshold;
221         }
222 
223         @Override
224         protected Long compute() {
225             if (splitter.estimateSize() > threshold) {
226                 SumSegmentRecursive sub = new SumSegmentRecursive(splitter.trySplit(), threshold);
227                 sub.fork();
228                 return compute() + sub.join();
229             } else {
230                 splitter.forEachRemaining(slice -> {
231                     result += sumSingle(0, slice);
232                 });
233                 return result;
234             }
235         }
236     }
237 
238     @DataProvider(name = "splits")
239     public Object[][] splits() {
240         return new Object[][] {
241                 { 10, 1 },
242                 { 100, 1 },
243                 { 1000, 1 },
244                 { 10000, 1 },
245                 { 10, 10 },
246                 { 100, 10 },
247                 { 1000, 10 },
248                 { 10000, 10 },
249                 { 10, 100 },
250                 { 100, 100 },
251                 { 1000, 100 },
252                 { 10000, 100 },
253                 { 10, 1000 },
254                 { 100, 1000 },
255                 { 1000, 1000 },
256                 { 10000, 1000 },
257                 { 10, 10000 },
258                 { 100, 10000 },
259                 { 1000, 10000 },
260                 { 10000, 10000 },
261         };
262     }
263 }