-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockfree.java
More file actions
129 lines (123 loc) · 3.76 KB
/
lockfree.java
File metadata and controls
129 lines (123 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import javafx.util.Pair;
import java.util.concurrent.atomic.*;
import java.util.*;
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
class Node{
public Pair<Integer,Integer> value;
public AtomicReference<Node> next;
public Node(Pair<Integer,Integer> value) {
this.value = value;
next = new AtomicReference<Node>(null);
}
}
class lockfreeQueue{
public AtomicReference<Node> tail;
public AtomicReference<Node> head;
public Node sentinel;
lockfreeQueue(){
sentinel = new Node(new Pair<Integer,Integer>(-1,-1));
tail = new AtomicReference<Node>(null);
head = new AtomicReference<Node>(null);
tail.set(sentinel);
head.set(sentinel);
}
public void enq(Pair<Integer,Integer> value) {
Node node = new Node(value);
while (true) {
Node last = tail.get();
Node next = last.next.get();
if (last == tail.get()) {
if (next == null) {
if (last.next.compareAndSet(next, node)) {
tail.compareAndSet(last, node);
return;
}
} else {
tail.compareAndSet(last, next);
}
}
}
}
public Pair<Integer,Integer> deq() throws EmptyStackException {
while (true) {
Node first = head.get();
Node last = tail.get();
Node next = first.next.get();
if (first == head.get()) {
if (first == last) {
if (next == null) {
throw new EmptyStackException();
}
tail.compareAndSet(last, next);
} else {
Pair<Integer,Integer> value = next.value;
if (head.compareAndSet(first, next))
return value;
}
}
}
}
}
public class lockfree{
static int mult(int[] a,int[] b,int size){
int sum = 0;
for (int i = 0 ; i < size ; i++) {
sum += a[i]*b[i];
}
return sum;
}
public static void main(String[] args){
lockfreeQueue Q = new lockfreeQueue();
try{
Scanner sc = new Scanner(new File("inputMatrix3.txt"));
Integer sizeOfMatrix = sc.nextInt();
Integer NUMBER_OF_THREADS = sc.nextInt();
int[][] matrixA = new int[sizeOfMatrix][sizeOfMatrix];
int[][] matrixB = new int[sizeOfMatrix][sizeOfMatrix];
int[][] ansMatrix = new int[sizeOfMatrix][sizeOfMatrix];
for (int i = 0;i<sizeOfMatrix;i++) {
for (int j = 0;j<sizeOfMatrix;j++) {
matrixA[i][j] = sc.nextInt();
ansMatrix[i][j] = 0;
Q.enq(new Pair <Integer,Integer> (i,j));
}
}
for (int i = 0;i<sizeOfMatrix;i++) {
for (int j = 0;j<sizeOfMatrix;j++) {
matrixB[j][i] = sc.nextInt();
}
}
Thread[] threads = new Thread[NUMBER_OF_THREADS];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
while(true){
try{
Pair<Integer,Integer> p = Q.deq();
ansMatrix[p.getKey()][p.getValue()] = mult(matrixA[p.getKey()],matrixB[p.getValue()],sizeOfMatrix);
} catch(EmptyStackException e){
break;
}
}
}
});
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
PrintWriter writer = new PrintWriter("lockfreeOutput.txt", "UTF-8");
for (int i = 0;i<sizeOfMatrix;i++) {
for (int j = 0;j<sizeOfMatrix;j++) {
writer.print(ansMatrix[i][j]+" ");
}
writer.print("\n");
}
writer.close();
} catch(Exception e){
return;
}
}
}