< prev index next >

test/jdk/java/foreign/TestSpliterator.java

Print this page

 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<>();

 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<>();
< prev index next >