1 /*
  2  * Copyright (c) 2020, 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 jdk.incubator.foreign.MemoryLayout;
 30 import jdk.incubator.foreign.MemorySegment;
 31 import jdk.incubator.foreign.ResourceScope;
 32 import jdk.incubator.foreign.SequenceLayout;
 33 
 34 import java.lang.invoke.VarHandle;
 35 import java.util.LinkedList;
 36 import java.util.List;
 37 import java.util.Spliterator;
 38 import java.util.concurrent.CountedCompleter;
 39 import java.util.concurrent.RecursiveTask;
 40 import java.util.concurrent.atomic.AtomicLong;
 41 import java.util.stream.LongStream;
 42 
 43 import jdk.incubator.foreign.ValueLayout;
 44 import org.testng.annotations.*;
 45 
 46 import static org.testng.Assert.*;
 47 
 48 public class TestSpliterator {
 49 
 50     static final VarHandle INT_HANDLE = MemoryLayout.sequenceLayout(ValueLayout.JAVA_INT)
 51             .varHandle(MemoryLayout.PathElement.sequenceElement());
 52 
 53     final static int CARRIER_SIZE = 4;
 54 
 55     @Test(dataProvider = "splits")
 56     public void testSum(int size, int threshold) {
 57         SequenceLayout layout = MemoryLayout.sequenceLayout(size, ValueLayout.JAVA_INT);
 58 
 59         //setup
 60         try (ResourceScope scope = ResourceScope.newSharedScope()) {
 61             MemorySegment segment = MemorySegment.allocateNative(layout, scope);
 62             for (int i = 0; i < layout.elementCount().getAsLong(); i++) {
 63                 INT_HANDLE.set(segment, (long) i, i);
 64             }
 65             long expected = LongStream.range(0, layout.elementCount().getAsLong()).sum();
 66             //serial
 67             long serial = sum(0, segment);
 68             assertEquals(serial, expected);
 69             //parallel counted completer
 70             long parallelCounted = new SumSegmentCounted(null, segment.spliterator(layout.elementLayout()), threshold).invoke();
 71             assertEquals(parallelCounted, expected);
 72             //parallel recursive action
 73             long parallelRecursive = new SumSegmentRecursive(segment.spliterator(layout.elementLayout()), threshold).invoke();
 74             assertEquals(parallelRecursive, expected);
 75             //parallel stream
 76             long streamParallel = segment.elements(layout.elementLayout()).parallel()
 77                     .reduce(0L, TestSpliterator::sumSingle, Long::sum);
 78             assertEquals(streamParallel, expected);
 79         }
 80     }
 81 
 82     @Test
 83     public void testSumSameThread() {
 84         SequenceLayout layout = MemoryLayout.sequenceLayout(1024, ValueLayout.JAVA_INT);
 85 
 86         //setup
 87         MemorySegment segment = MemorySegment.allocateNative(layout, ResourceScope.newImplicitScope());
 88         for (int i = 0; i < layout.elementCount().getAsLong(); i++) {
 89             INT_HANDLE.set(segment, (long) i, i);
 90         }
 91         long expected = LongStream.range(0, layout.elementCount().getAsLong()).sum();
 92 
 93         //check that a segment w/o ACQUIRE access mode can still be used from same thread
 94         AtomicLong spliteratorSum = new AtomicLong();
 95         segment.spliterator(layout.elementLayout())
 96                 .forEachRemaining(s -> spliteratorSum.addAndGet(sumSingle(0L, s)));
 97         assertEquals(spliteratorSum.get(), expected);
 98     }
 99 
100     @Test(expectedExceptions = IllegalArgumentException.class)
101     public void testBadSpliteratorElementSizeTooBig() {
102         MemorySegment.ofArray(new byte[2]).spliterator(ValueLayout.JAVA_INT);
103     }
104 
105     @Test(expectedExceptions = IllegalArgumentException.class)
106     public void testBadStreamElementSizeTooBig() {
107         MemorySegment.ofArray(new byte[2]).elements(ValueLayout.JAVA_INT);
108     }
109 
110     @Test(expectedExceptions = IllegalArgumentException.class)
111     public void testBadSpliteratorElementSizeNotMultiple() {
112         MemorySegment.ofArray(new byte[7]).spliterator(ValueLayout.JAVA_INT);
113     }
114 
115     @Test(expectedExceptions = IllegalArgumentException.class)
116     public void testBadStreamElementSizeNotMultiple() {
117         MemorySegment.ofArray(new byte[7]).elements(ValueLayout.JAVA_INT);
118     }
119 
120     @Test(expectedExceptions = IllegalArgumentException.class)
121     public void testBadSpliteratorElementSizeZero() {
122         MemorySegment.ofArray(new byte[7]).spliterator(MemoryLayout.sequenceLayout(0, ValueLayout.JAVA_INT));
123     }
124 
125     @Test(expectedExceptions = IllegalArgumentException.class)
126     public void testBadStreamElementSizeZero() {
127         MemorySegment.ofArray(new byte[7]).elements(MemoryLayout.sequenceLayout(0, ValueLayout.JAVA_INT));
128     }
129 
130     static long sumSingle(long acc, MemorySegment segment) {
131         return acc + (int)INT_HANDLE.get(segment, 0L);
132     }
133 
134     static long sum(long start, MemorySegment segment) {
135         long sum = start;
136         int length = (int)segment.byteSize();
137         for (int i = 0 ; i < length / CARRIER_SIZE ; i++) {
138             sum += (int)INT_HANDLE.get(segment, (long)i);
139         }
140         return sum;
141     }
142 
143     static class SumSegmentCounted extends CountedCompleter<Long> {
144 
145         final long threshold;
146         long localSum = 0;
147         List<SumSegmentCounted> children = new LinkedList<>();
148 
149         private Spliterator<MemorySegment> segmentSplitter;
150 
151         SumSegmentCounted(SumSegmentCounted parent, Spliterator<MemorySegment> segmentSplitter, long threshold) {
152             super(parent);
153             this.segmentSplitter = segmentSplitter;
154             this.threshold = threshold;
155         }
156 
157         @Override
158         public void compute() {
159             Spliterator<MemorySegment> sub;
160             while (segmentSplitter.estimateSize() > threshold &&
161                     (sub = segmentSplitter.trySplit()) != null) {
162                 addToPendingCount(1);
163                 SumSegmentCounted child = new SumSegmentCounted(this, sub, threshold);
164                 children.add(child);
165                 child.fork();
166             }
167             segmentSplitter.forEachRemaining(slice -> {
168                 localSum += sumSingle(0, slice);
169             });
170             tryComplete();
171         }
172 
173         @Override
174         public Long getRawResult() {
175             long sum = localSum;
176             for (SumSegmentCounted c : children) {
177                 sum += c.getRawResult();
178             }
179             return sum;
180         }
181      }
182 
183     static class SumSegmentRecursive extends RecursiveTask<Long> {
184 
185         final long threshold;
186         private final Spliterator<MemorySegment> splitter;
187         private long result;
188 
189         SumSegmentRecursive(Spliterator<MemorySegment> splitter, long threshold) {
190             this.splitter = splitter;
191             this.threshold = threshold;
192         }
193 
194         @Override
195         protected Long compute() {
196             if (splitter.estimateSize() > threshold) {
197                 SumSegmentRecursive sub = new SumSegmentRecursive(splitter.trySplit(), threshold);
198                 sub.fork();
199                 return compute() + sub.join();
200             } else {
201                 splitter.forEachRemaining(slice -> {
202                     result += sumSingle(0, slice);
203                 });
204                 return result;
205             }
206         }
207     }
208 
209     @DataProvider(name = "splits")
210     public Object[][] splits() {
211         return new Object[][] {
212                 { 10, 1 },
213                 { 100, 1 },
214                 { 1000, 1 },
215                 { 10000, 1 },
216                 { 10, 10 },
217                 { 100, 10 },
218                 { 1000, 10 },
219                 { 10000, 10 },
220                 { 10, 100 },
221                 { 100, 100 },
222                 { 1000, 100 },
223                 { 10000, 100 },
224                 { 10, 1000 },
225                 { 100, 1000 },
226                 { 1000, 1000 },
227                 { 10000, 1000 },
228                 { 10, 10000 },
229                 { 100, 10000 },
230                 { 1000, 10000 },
231                 { 10000, 10000 },
232         };
233     }
234 }