Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: определение достижимости параллелизма и реализация параллельных алгоритмов.

Шаги выполнения:
Expand Down
22 changes: 21 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
kotlin("jvm") version "1.9.20"
java
application
}

Expand All @@ -12,10 +13,14 @@ repositories {

dependencies {
testImplementation(kotlin("test"))
testImplementation("org.openjdk.jcstress:jcstress-core:0.16")
testAnnotationProcessor("org.openjdk.jcstress:jcstress-core:0.16")
}

tasks.test {
useJUnitPlatform()
minHeapSize = "6G"
maxHeapSize = "10G"
}

kotlin {
Expand All @@ -24,4 +29,19 @@ kotlin {

application {
mainClass.set("MainKt")
}
}

// JCStress runner task: runs JCStress tests located on the test runtime classpath
// Use: ./gradlew jcstress [-PjcstressArgs="-v -m quick"]
tasks.register<JavaExec>("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())
}
}
Binary file added docs/performance_comparison_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/sync_error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions docs/thread1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
thread: 1

Times for 10 vertices and 50 connections:
Serial: 1
Parallel: 2
--------
Times for 100 vertices and 500 connections:
Serial: 0
Parallel: 1
--------
Times for 1000 vertices and 5000 connections:
Serial: 1
Parallel: 0
--------
Times for 10000 vertices and 50000 connections:
Serial: 3
Parallel: 3
--------
Times for 10000 vertices and 100000 connections:
Serial: 1
Parallel: 3
--------
Times for 50000 vertices and 1000000 connections:
Serial: 12
Parallel: 23
--------
Times for 100000 vertices and 1000000 connections:
Serial: 15
Parallel: 27
--------
Times for 1000000 vertices and 10000000 connections:
Serial: 260
Parallel: 385
--------
Times for 2000000 vertices and 10000000 connections:
Serial: 268
Parallel: 341
--------
38 changes: 38 additions & 0 deletions docs/thread12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
thread: 12

Times for 10 vertices and 50 connections:
Serial: 0
Parallel: 2
--------
Times for 100 vertices and 500 connections:
Serial: 0
Parallel: 1
--------
Times for 1000 vertices and 5000 connections:
Serial: 1
Parallel: 4
--------
Times for 10000 vertices and 50000 connections:
Serial: 3
Parallel: 8
--------
Times for 10000 vertices and 100000 connections:
Serial: 2
Parallel: 14
--------
Times for 50000 vertices and 1000000 connections:
Serial: 12
Parallel: 5
--------
Times for 100000 vertices and 1000000 connections:
Serial: 15
Parallel: 7
--------
Times for 1000000 vertices and 10000000 connections:
Serial: 177
Parallel: 54
--------
Times for 2000000 vertices and 10000000 connections:
Serial: 368
Parallel: 89
--------
38 changes: 38 additions & 0 deletions docs/thread2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
thread: 2

Times for 10 vertices and 50 connections:
Serial: 0
Parallel: 1
--------
Times for 100 vertices and 500 connections:
Serial: 0
Parallel: 1
--------
Times for 1000 vertices and 5000 connections:
Serial: 1
Parallel: 0
--------
Times for 10000 vertices and 50000 connections:
Serial: 3
Parallel: 3
--------
Times for 10000 vertices and 100000 connections:
Serial: 2
Parallel: 6
--------
Times for 50000 vertices and 1000000 connections:
Serial: 14
Parallel: 24
--------
Times for 100000 vertices and 1000000 connections:
Serial: 16
Parallel: 15
--------
Times for 1000000 vertices and 10000000 connections:
Serial: 271
Parallel: 209
--------
Times for 2000000 vertices and 10000000 connections:
Serial: 314
Parallel: 213
--------
38 changes: 38 additions & 0 deletions docs/thread4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
thread: 4

Times for 10 vertices and 50 connections:
Serial: 0
Parallel: 1
--------
Times for 100 vertices and 500 connections:
Serial: 0
Parallel: 1
--------
Times for 1000 vertices and 5000 connections:
Serial: 1
Parallel: 1
--------
Times for 10000 vertices and 50000 connections:
Serial: 3
Parallel: 3
--------
Times for 10000 vertices and 100000 connections:
Serial: 2
Parallel: 5
--------
Times for 50000 vertices and 1000000 connections:
Serial: 13
Parallel: 22
--------
Times for 100000 vertices and 1000000 connections:
Serial: 16
Parallel: 16
--------
Times for 1000000 vertices and 10000000 connections:
Serial: 257
Parallel: 104
--------
Times for 2000000 vertices and 10000000 connections:
Serial: 373
Parallel: 147
--------
38 changes: 38 additions & 0 deletions docs/thread8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
thread: 8

Times for 10 vertices and 50 connections:
Serial: 0
Parallel: 2
--------
Times for 100 vertices and 500 connections:
Serial: 0
Parallel: 1
--------
Times for 1000 vertices and 5000 connections:
Serial: 0
Parallel: 2
--------
Times for 10000 vertices and 50000 connections:
Serial: 3
Parallel: 7
--------
Times for 10000 vertices and 100000 connections:
Serial: 2
Parallel: 14
--------
Times for 50000 vertices and 1000000 connections:
Serial: 12
Parallel: 4
--------
Times for 100000 vertices and 1000000 connections:
Serial: 17
Parallel: 7
--------
Times for 1000000 vertices and 10000000 connections:
Serial: 285
Parallel: 75
--------
Times for 2000000 vertices and 10000000 connections:
Serial: 390
Parallel: 103
--------
Binary file added docs/thread_comparison_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 79 additions & 3 deletions src/main/java/org/itmo/Graph.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.itmo;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;

class Graph {
private final int V;
Expand All @@ -24,6 +26,80 @@ void addEdge(int src, int dest) {
}

void parallelBFS(int startVertex) {
AtomicIntegerArray visited = new AtomicIntegerArray(V);

List<Integer> currentFrontier = new ArrayList<>();
currentFrontier.add(startVertex);
visited.set(startVertex, 1);

int numThreads = Runtime.getRuntime().availableProcessors();
ExecutorService executor = Executors.newFixedThreadPool(numThreads);

try {
while (!currentFrontier.isEmpty()) {
final List<Integer> frontier = currentFrontier;
final int frontierSize = frontier.size();

List<List<Integer>> threadLocalBuffers = new ArrayList<>(numThreads);
for (int t = 0; t < numThreads; t++) {
threadLocalBuffers.add(new ArrayList<>());
}

int chunkSize = Math.max(1, (frontierSize + numThreads - 1) / numThreads);
CountDownLatch latch = new CountDownLatch(numThreads);

for (int threadId = 0; threadId < numThreads; threadId++) {
final int start = threadId * chunkSize;
final int end = Math.min(start + chunkSize, frontierSize);
final List<Integer> localBuffer = threadLocalBuffers.get(threadId);

if (start >= frontierSize) {
latch.countDown();
continue;
}

executor.execute(() -> {
try {
for (int i = start; i < end; i++) {
int vertex = frontier.get(i);

for (int neighbor : adjList[vertex]) {
if (visited.compareAndSet(neighbor, 0, 1)) {
localBuffer.add(neighbor);
}
}
}
} finally {
latch.countDown();
}
});
}

try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("BFS interrupted", e);
}

List<Integer> nextFrontier = new ArrayList<>();
for (List<Integer> buffer : threadLocalBuffers) {
nextFrontier.addAll(buffer);
}

currentFrontier = nextFrontier;
}
} finally {
executor.shutdown();
try {
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.currentThread().interrupt();
}
}
}

//Generated by ChatGPT
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/itmo/UnsafeCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.itmo;

public class UnsafeCounter {
private int counter = 0;

public void increment() {
counter++; // <-- гонка данных
}

public int get() {
return counter;
}
}
Loading