-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDistribution.java
More file actions
177 lines (129 loc) · 4.22 KB
/
Copy pathThreadDistribution.java
File metadata and controls
177 lines (129 loc) · 4.22 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.util.*;
public class ThreadDistribution {
public static void main(String args[])
{
//this can be a driver function
Scanner scanner = new Scanner(System.in);
System.out.println("Enter no of files");
String s = scanner.nextLine();
int n =Integer.parseInt(s);
List<File> files= new ArrayList<File>();
String fileNamePrefix = "File";
System.out.println("Enter " + n + " values(one value per line) denoting the no of bytes in each file");
for(int i =0;i<n;i++)
{
s = scanner.nextLine();
long b =Integer.parseInt(s);
File file = new File(fileNamePrefix+(i+1),b);
files.add(file);
}
System.out.println("Enter no of threads");
s = scanner.nextLine();
int t =Integer.parseInt(s);
List<List<FileRange>> split = getSplits(files,t);
printAns(split);
}
static List<List<FileRange>> getSplits(List<File> files, int N)
{
List<List<FileRange>> ans = null;
long totalBytes = 0;
for(int i=0;i<files.size();i++)
{
totalBytes+=files.get(i).length;
}
// System.out.println("Total bytes are " + totalBytes);
long X = -1, P = -1;
for(int p =0;p<=N;p++)
{
//check if this is a valid value of P
double x = (totalBytes -(double)p) / (double)(N);
if(x<0)
continue;
//see if x is a integer value
if(x==Math.ceil(x))
{
//X is valid
X = (long)x;
P =p;
break;
}
}
if(X == -1 || P == -1)
{
//didn't find a possible combination
return null;
}
// System.out.println("X is " + X + " P is: " + P);
//now (N-P) thread will get (X) bytes and P threads will get (X+1) bytes.
long T1 = N - P;
long T2 = P;
long X1 = X;
long X2 = X+1;
// System.out.println(T1 + " threads will get " + X1 + " bytes and " + T2 + " threads will get " + X2 + " bytes");
//now we will create segments for each thread
ans = new ArrayList<List<FileRange>>();
int fileNo = 0;
long threadCount=1;
long byteStartIndex = 0;
//First T1 thread will be taking X1 bytes and T2 threads will be taking X2 bytes
while(threadCount<=N && fileNo<files.size())
{
List<FileRange> listOfFileRangesForCurrentThread = new ArrayList<FileRange>();
long totalBytesNeededByCurrentThread;
if(threadCount<=T1)
{
totalBytesNeededByCurrentThread = X1;
}
else
{
totalBytesNeededByCurrentThread = X2;
}
while(totalBytesNeededByCurrentThread>0 && fileNo<files.size())
{
File curFile = files.get(fileNo);
long curFileEndByteIndex = curFile.length-1;
long totalBytesAvailableInThisFile = (curFileEndByteIndex - byteStartIndex +1);
if(totalBytesAvailableInThisFile == totalBytesNeededByCurrentThread)
{
FileRange fileRange = new FileRange(curFile.filename,byteStartIndex,curFileEndByteIndex);
listOfFileRangesForCurrentThread.add(fileRange);
fileNo++;
threadCount++;
byteStartIndex = 0;
totalBytesNeededByCurrentThread = 0; //no more bytes needed for current thread
}
else if(totalBytesAvailableInThisFile > totalBytesNeededByCurrentThread)
{
FileRange fileRange = new FileRange(curFile.filename,byteStartIndex,byteStartIndex+totalBytesNeededByCurrentThread-1);
listOfFileRangesForCurrentThread.add(fileRange);
threadCount++;
byteStartIndex = byteStartIndex + totalBytesNeededByCurrentThread;
totalBytesNeededByCurrentThread = 0;
}
else
{
FileRange fileRange = new FileRange(curFile.filename,byteStartIndex,curFileEndByteIndex);
listOfFileRangesForCurrentThread.add(fileRange);
totalBytesNeededByCurrentThread = totalBytesNeededByCurrentThread - (curFileEndByteIndex-byteStartIndex+1);
byteStartIndex = 0;
fileNo++;
}
}
ans.add(listOfFileRangesForCurrentThread);
}
return ans;
}
static void printAns(List<List<FileRange>> threadDistribution)
{
for(int i=0;i<threadDistribution.size();i++)
{
List<FileRange> fileRanges = threadDistribution.get(i);
System.out.println(" \n For thread:" + (i+1) + "\n");
for(int j=0;j<fileRanges.size();j++)
{
FileRange fileRange = fileRanges.get(j);
System.out.println(fileRange.filename + " " + fileRange.startOffset + " " + fileRange.endOffset);
}
}
}
}