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