diff --git a/README.md b/README.md index 6495c30..0eb4312 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/AwTYhPar) # Лабораторная работа № 1: определение достижимости параллелизма и реализация параллельных алгоритмов. Шаги выполнения: diff --git a/build.gradle.kts b/build.gradle.kts index 3341beb..bb005c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,6 @@ plugins { kotlin("jvm") version "1.9.20" + java application } @@ -12,6 +13,8 @@ repositories { dependencies { testImplementation(kotlin("test")) + testImplementation("org.openjdk.jcstress:jcstress-core:0.16") + testAnnotationProcessor("org.openjdk.jcstress:jcstress-core:0.16") } tasks.test { @@ -24,4 +27,27 @@ kotlin { application { mainClass.set("MainKt") -} \ No newline at end of file +} + +// JCStress runner task: runs JCStress tests located on the test runtime classpath +// Use: ./gradlew jcstress [-PjcstressArgs="-v -m quick"] +tasks.register("jcstress") { + group = "verification" + description = "Run JCStress stress tests" + mainClass.set("org.openjdk.jcstress.Main") + classpath = sourceSets.test.get().runtimeClasspath + dependsOn("testClasses") + + val argsProp = project.findProperty("jcstressArgs") as String? + if (!argsProp.isNullOrBlank()) { + args = argsProp.split("\\s+".toRegex()) + } +} + +tasks.named("test") { + useJUnitPlatform() + + minHeapSize = "512m" + maxHeapSize = "8000m" +} + diff --git a/jcstress-results-2025-10-07-17-07-38.bin.gz b/jcstress-results-2025-10-07-17-07-38.bin.gz new file mode 100644 index 0000000..5105e8b Binary files /dev/null and b/jcstress-results-2025-10-07-17-07-38.bin.gz differ diff --git a/jcstress-results-2025-10-07-17-08-30.bin.gz b/jcstress-results-2025-10-07-17-08-30.bin.gz new file mode 100644 index 0000000..5105e8b Binary files /dev/null and b/jcstress-results-2025-10-07-17-08-30.bin.gz differ diff --git a/jcstress-results-2025-10-07-17-08-55.bin.gz b/jcstress-results-2025-10-07-17-08-55.bin.gz new file mode 100644 index 0000000..5105e8b Binary files /dev/null and b/jcstress-results-2025-10-07-17-08-55.bin.gz differ diff --git a/jcstress-results-2025-10-07-17-09-34.bin.gz b/jcstress-results-2025-10-07-17-09-34.bin.gz new file mode 100644 index 0000000..5105e8b Binary files /dev/null and b/jcstress-results-2025-10-07-17-09-34.bin.gz differ diff --git a/jcstress-results-2025-10-07-17-10-17.bin.gz b/jcstress-results-2025-10-07-17-10-17.bin.gz new file mode 100644 index 0000000..0172ab7 Binary files /dev/null and b/jcstress-results-2025-10-07-17-10-17.bin.gz differ diff --git a/results/index.html b/results/index.html new file mode 100644 index 0000000..7de4c97 --- /dev/null +++ b/results/index.html @@ -0,0 +1,124 @@ + + + +Java Concurrency Stress test report + + + + + + + + +
+

+100% +

+ + + +
 
Overall pass rate: 1/1 
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
java.specification.nameJava Platform API Specification
java.specification.vendorOracle Corporation
java.specification.version21
java.vendorEclipse Adoptium
java.version21.0.8
java.vm.nameOpenJDK 64-Bit Server VM
java.vm.vendorEclipse Adoptium
java.vm.version21.0.8+9-LTS
os.archaarch64
os.nameMac OS X
os.version15.6
+
+
+

FAILED tests

+

Strong asserts were violated. Correct implementations should have no assert failures here.

+ +
+None! +
+
+
+

ERROR tests

+

Tests break for some reason, other than failing the assert. Correct implementations should have none.

+ +
+None! +
+
+
+

INTERESTING tests

+

Some interesting behaviors observed. This is for the plain curiosity.

+ +
+None! +
+
+
+

All tests

+

+ + + + + + + +
   org.itmo.JCBFSStressTest107PASSED
+
+ + diff --git a/results/org.itmo.JCBFSStressTest.html b/results/org.itmo.JCBFSStressTest.html new file mode 100644 index 0000000..4d6d7eb --- /dev/null +++ b/results/org.itmo.JCBFSStressTest.html @@ -0,0 +1,577 @@ + + + +Java Concurrency Stress test report + + + +

org.itmo.JCBFSStressTest

+

Description and references

+

null

+

Environment

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
java.specification.nameJava Platform API Specification
java.specification.vendorOracle Corporation
java.specification.version21
java.vendorEclipse Adoptium
java.version21.0.8
java.vm.nameOpenJDK 64-Bit Server VM
java.vm.vendorEclipse Adoptium
java.vm.version21.0.8+9-LTS
os.archaarch64
os.nameMac OS X
os.version15.6
+

Results

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Compilation ModeScheduling ClassJava OptionsStatusObserved States
10
Acceptable
Все вершины были обойдены
split
+    actor1: Interpreter
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1452051
split
+    actor1: C1
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1288211
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1503251
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1127158550]
+
OK1308691
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1214536811]
+
OK1359891
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1710329551]
+
OK1431571
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1922078931]
+
OK1349651
split
+    actor1: C2
+    actor2: Interpreter
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=545851348]
+
OK1728531
split
+    actor1: Interpreter
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1462291
split
+    actor1: C1
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1462291
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1431571
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1047453325]
+
OK1503251
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1714780508]
+
OK1462291
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=2046472483]
+
OK1329171
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=422445359]
+
OK1380371
split
+    actor1: C2
+    actor2: C1
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=480402827]
+
OK1411091
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1513491
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1764780768]
+
OK1411091
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1861118542]
+
OK1318931
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=2020796978]
+
OK1400851
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=567575326]
+
OK1431571
split
+    actor1: Interpreter
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=743746632]
+
OK1318931
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1493011
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1180520203]
+
OK1462291
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1746447779]
+
OK1421331
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1884818911]
+
OK1462291
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=437442740]
+
OK1800211
split
+    actor1: C1
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=722847980]
+
OK1421331
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant]
+
OK1923091
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=145015542]
+
OK1728531
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1455975245]
+
OK1390611
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=1786103352]
+
OK1462291
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=329137933]
+
OK1380371
split
+    actor1: C2
+    actor2: C2
+
    actor1: package group free, core group free
+    actor2: package group free, core group free
+
+
[-Dfile.encoding=UTF-8, -Duser.country=RU, -Duser.language=ru, -Duser.variant, -XX:+StressLCM, -XX:+StressGCM, -XX:+StressIGVN, -XX:+StressCCP, -XX:StressSeed=55694347]
+
OK1370131
+

Messages

+

VM Output Streams

+

VM Error Streams

+ + diff --git a/src/main/java/org/itmo/ConcurrentQueueBalancer.java b/src/main/java/org/itmo/ConcurrentQueueBalancer.java new file mode 100644 index 0000000..0b517df --- /dev/null +++ b/src/main/java/org/itmo/ConcurrentQueueBalancer.java @@ -0,0 +1,28 @@ +package org.itmo; + +import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +class ConcurrentQueueBalancer { + private AtomicInteger nextQueue; + private final List> queues; + + ConcurrentQueueBalancer(int size) { + nextQueue = new AtomicInteger(0); + queues = Stream.>generate(ConcurrentLinkedQueue::new).limit(size) + .collect(Collectors.toList()); + } + + void add(Integer value) { + Integer nextQueueIdx = nextQueue.getAndUpdate((val) -> (val + 1) % queues.size()); + queues.get(nextQueueIdx).add(value); + } + + List> getQueues() { + return queues; + } +} diff --git a/src/main/java/org/itmo/Graph.java b/src/main/java/org/itmo/Graph.java index 141a0b6..b3f6a20 100644 --- a/src/main/java/org/itmo/Graph.java +++ b/src/main/java/org/itmo/Graph.java @@ -1,13 +1,18 @@ package org.itmo; import java.util.*; -import java.util.concurrent.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; class Graph { + private final Integer CORES = Runtime.getRuntime().availableProcessors() * 2; private final int V; private final ArrayList[] adjList; + private final ExecutorService executor; Graph(int vertices) { this.V = vertices; @@ -15,6 +20,12 @@ class Graph { for (int i = 0; i < vertices; ++i) { adjList[i] = new ArrayList<>(); } + + executor = Executors.newFixedThreadPool(CORES); + } + + List[] getAdjList() { + return adjList; } void addEdge(int src, int dest) { @@ -24,9 +35,50 @@ void addEdge(int src, int dest) { } void parallelBFS(int startVertex) { + AtomicBoolean[] visited = new AtomicBoolean[V]; + for (Integer i = 0; i < V; i++) { + visited[i] = new AtomicBoolean(false); + } + visited[startVertex].set(true); + + ConcurrentQueueBalancer balancer = new ConcurrentQueueBalancer(CORES); + balancer.add(startVertex); + + while (true) { + ConcurrentQueueBalancer nextBalancer = new ConcurrentQueueBalancer(CORES); + + List> futures = balancer.getQueues().stream() + .filter((queue) -> !queue.isEmpty()) + .map((queue) -> executor.submit(() -> { + while (!queue.isEmpty()) { + Integer vertex = queue.poll(); + + for (int n : adjList[vertex]) { + Boolean isVisited = visited[n].getAndSet(true); + if (!isVisited) { + nextBalancer.add(n); + } + } + } + })).collect(Collectors.toList()); + + if (futures.isEmpty()) { + break; + } + + for (Future f : futures) { + try { + f.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + + balancer = nextBalancer; + } } - //Generated by ChatGPT + // Generated by ChatGPT void bfs(int startVertex) { boolean[] visited = new boolean[V]; diff --git a/src/test/java/org/itmo/BFSTest.java b/src/test/java/org/itmo/BFSTest.java index 7bf9098..77af020 100644 --- a/src/test/java/org/itmo/BFSTest.java +++ b/src/test/java/org/itmo/BFSTest.java @@ -4,18 +4,15 @@ import java.io.FileWriter; import java.io.IOException; -import java.nio.Buffer; -import java.util.HashSet; +import java.util.List; import java.util.Random; -import java.util.function.BiFunction; -import java.util.stream.IntStream; public class BFSTest { @Test public void bfsTest() throws IOException { - int[] sizes = new int[]{10, 100, 1000, 10_000, 10_000, 50_000, 100_000, 1_000_000, 2_000_000}; - int[] connections = new int[]{50, 500, 5000, 50_000, 100_000, 1_000_000, 1_000_000, 10_000_000, 10_000_000}; + int[] sizes = new int[] { 10, 100, 1000, 10_000, 10_000, 50_000, 100_000, 1_000_000, 2_000_000 }; + int[] connections = new int[] { 50, 500, 5000, 50_000, 100_000, 1_000_000, 1_000_000, 10_000_000, 10_000_000 }; Random r = new Random(42); try (FileWriter fw = new FileWriter("tmp/results.txt")) { for (int i = 0; i < sizes.length; i++) { @@ -34,7 +31,6 @@ public void bfsTest() throws IOException { } } - private long executeSerialBfsAndGetTime(Graph g) { long startTime = System.currentTimeMillis(); g.bfs(0); diff --git a/src/test/java/org/itmo/JCBFSStressTest.java b/src/test/java/org/itmo/JCBFSStressTest.java new file mode 100644 index 0000000..14a4812 --- /dev/null +++ b/src/test/java/org/itmo/JCBFSStressTest.java @@ -0,0 +1,85 @@ +package org.itmo; + +import java.util.List; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Stream; + +import org.openjdk.jcstress.annotations.Actor; +import org.openjdk.jcstress.annotations.Arbiter; +import org.openjdk.jcstress.annotations.Expect; +import org.openjdk.jcstress.annotations.JCStressTest; +import org.openjdk.jcstress.annotations.Outcome; +import org.openjdk.jcstress.annotations.State; +import org.openjdk.jcstress.infra.results.L_Result; + +@JCStressTest +@Outcome(id = "10", expect = Expect.ACCEPTABLE, desc = "Все вершины были обойдены") +@Outcome(expect = Expect.ACCEPTABLE_INTERESTING, desc = "Гонка данных: часть вершин не была обойдена") +@State +public class JCBFSStressTest { + private static final int CORES_NUM = 12; + private static final int VERTICES_NUM = 10; + + Graph graph = new Graph(VERTICES_NUM); + AtomicBoolean[] visited = new AtomicBoolean[VERTICES_NUM]; + ConcurrentQueueBalancer queueBalancer = new ConcurrentQueueBalancer(CORES_NUM); + ConcurrentQueueBalancer nextBalancer = new ConcurrentQueueBalancer(CORES_NUM); + + public JCBFSStressTest() { + graph.addEdge(0, 1); + graph.addEdge(0, 2); + graph.addEdge(0, 3); + graph.addEdge(0, 4); + + graph.addEdge(1, 5); + graph.addEdge(3, 6); + graph.addEdge(2, 7); + graph.addEdge(4, 8); + graph.addEdge(4, 9); + + for (int i = 0; i < VERTICES_NUM; ++i) { + visited[i] = new AtomicBoolean(false); + } + + for (int i = 0; i < 5; ++i) { + visited[i].set(true); + } + } + + @Actor + void actor1() { + Queue q = new ConcurrentLinkedQueue<>(); + q.add(1); + q.add(3); + step(q); + } + + @Actor + void actor2() { + Queue q = new ConcurrentLinkedQueue<>(); + q.add(2); + q.add(4); + step(q); + } + + void step(Queue vertecies) { + List[] adjList = graph.getAdjList(); + while (!vertecies.isEmpty()) { + Integer vertex = vertecies.poll(); + + for (int n : adjList[vertex]) { + Boolean isVisited = visited[n].getAndSet(true); + if (!isVisited) { + nextBalancer.add(n); + } + } + } + } + + @Arbiter + public void arbiter(L_Result r) { + r.r1 = Stream.of(visited).filter(AtomicBoolean::get).count(); + } +} diff --git a/src/test/java/org/itmo/RandomGraphGenerator.java b/src/test/java/org/itmo/RandomGraphGenerator.java index fdb888c..3f02e49 100644 --- a/src/test/java/org/itmo/RandomGraphGenerator.java +++ b/src/test/java/org/itmo/RandomGraphGenerator.java @@ -1,7 +1,9 @@ package org.itmo; import java.util.Arrays; +import java.util.HashSet; import java.util.Random; +import java.util.Set; import java.util.SplittableRandom; import java.util.concurrent.ForkJoinPool; import java.util.stream.IntStream; @@ -11,22 +13,31 @@ public class RandomGraphGenerator { private long pack(int u, int v) { return (((long) u) << 32) | (v & 0xffffffffL); } + private int unpackU(long key) { return (int) (key >>> 32); } + private int unpackV(long key) { return (int) (key & 0xffffffffL); } Graph generateGraph(Random r, int size, int numEdges) { - if (numEdges < size - 1) throw new IllegalArgumentException("We need min size-1 edges"); + if (size < 1) + throw new IllegalArgumentException("size must be >= 1"); + if (numEdges < size - 1) + throw new IllegalArgumentException("We need min size-1 edges"); long maxDirected = (long) size * (size - 1); - if (numEdges > maxDirected) throw new IllegalArgumentException("Too many edges for directed graph without self-loops"); + if (numEdges > maxDirected) + throw new IllegalArgumentException("Too many edges for directed graph without self-loops"); + + int[] perm = IntStream.range(0, size).toArray(); + for (int i = size - 1; i > 0; i--) { + int j = r.nextInt(i + 1); + int tmp = perm[i]; + perm[i] = perm[j]; + perm[j] = tmp; - int[] perm = java.util.stream.IntStream.range(0, size).toArray(); - for (int i = size - 1; i > 1; i--) { - int j = 1 + r.nextInt(i); - int tmp = perm[i]; perm[i] = perm[j]; perm[j] = tmp; } final int chainCount = size - 1; @@ -48,7 +59,8 @@ Graph generateGraph(Random r, int size, int numEdges) { final SplittableRandom base = new SplittableRandom(r.nextLong()); final SplittableRandom[] seeds = new SplittableRandom[threads]; - for (int t = 0; t < threads; t++) seeds[t] = base.split(); + for (int t = 0; t < threads; t++) + seeds[t] = base.split(); long[] finalKeys = keys; IntStream.range(0, threads).parallel().forEach(t -> { @@ -58,7 +70,8 @@ Graph generateGraph(Random r, int size, int numEdges) { for (int i = start; i < end; i++) { int u = rnd.nextInt(size); int v = rnd.nextInt(size - 1); - if (v >= u) v++; + if (v >= u) + v++; finalKeys[i] = pack(u, v); } }); @@ -74,7 +87,7 @@ Graph generateGraph(Random r, int size, int numEdges) { while (unique < numEdges) { int missing = numEdges - unique; - int extra = Math.max(missing / 2, 10_000); // небольшой запас + int extra = Math.max(missing / 2, 10_000); int add = missing + extra; long[] more = new long[unique + add]; @@ -82,7 +95,8 @@ Graph generateGraph(Random r, int size, int numEdges) { final SplittableRandom base2 = base.split(); final SplittableRandom[] seeds2 = new SplittableRandom[threads]; - for (int t = 0; t < threads; t++) seeds2[t] = base2.split(); + for (int t = 0; t < threads; t++) + seeds2[t] = base2.split(); final int offset2 = unique; final int chunk2 = (add + threads - 1) / threads; @@ -93,7 +107,8 @@ Graph generateGraph(Random r, int size, int numEdges) { for (int i = start; i < end; i++) { int u = rnd.nextInt(size); int v = rnd.nextInt(size - 1); - if (v >= u) v++; + if (v >= u) + v++; more[i] = pack(u, v); } }); @@ -109,6 +124,31 @@ Graph generateGraph(Random r, int size, int numEdges) { keys = more; } + Set chainSet = new HashSet<>(chainCount * 2); + for (int i = 1; i < size; i++) { + chainSet.add(pack(perm[i - 1], perm[i])); + } + + int p = 0; + for (int i = 0; i < unique && p < chainCount; i++) { + long e = keys[i]; + if (chainSet.remove(e)) { + // swap keys[p] и keys[i] + long tmp = keys[p]; + keys[p] = keys[i]; + keys[i] = tmp; + p++; + } + } + + SplittableRandom shuf = base.split(); + for (int i = p; i < numEdges; i++) { + int j = i + shuf.nextInt(unique - i); + long tmp = keys[i]; + keys[i] = keys[j]; + keys[j] = tmp; + } + Graph g = new Graph(size); for (int i = 0; i < numEdges; i++) { long key = keys[i]; diff --git a/tmp/results.txt b/tmp/results.txt index 027e7f9..c59da6f 100644 --- a/tmp/results.txt +++ b/tmp/results.txt @@ -1,32 +1,36 @@ Times for 10 vertices and 50 connections: Serial: 0 -Parallel: 0 +Parallel: 4 -------- Times for 100 vertices and 500 connections: Serial: 0 -Parallel: 0 +Parallel: 6 -------- Times for 1000 vertices and 5000 connections: -Serial: 1 -Parallel: 0 +Serial: 0 +Parallel: 4 -------- Times for 10000 vertices and 50000 connections: Serial: 3 -Parallel: 0 +Parallel: 12 -------- Times for 10000 vertices and 100000 connections: -Serial: 2 -Parallel: 0 +Serial: 1 +Parallel: 17 -------- Times for 50000 vertices and 1000000 connections: -Serial: 30 -Parallel: 0 +Serial: 15 +Parallel: 21 -------- Times for 100000 vertices and 1000000 connections: -Serial: 18 -Parallel: 0 +Serial: 12 +Parallel: 39 -------- Times for 1000000 vertices and 10000000 connections: -Serial: 307 -Parallel: 0 +Serial: 252 +Parallel: 418 +-------- +Times for 2000000 vertices and 10000000 connections: +Serial: 1613 +Parallel: 586 --------