-
Notifications
You must be signed in to change notification settings - Fork 16
/
Program.cs
180 lines (149 loc) · 5.12 KB
/
Program.cs
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
178
179
180
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Text;
const string OUTPUT_PATH = "outputs.txt";
const string MEASUREMENTS_PATH = "measurements.txt";
const int DEFAULT_CAPACITY = 64;
var expectOutput = File.ReadAllText(OUTPUT_PATH);
var stopwatch = Stopwatch.StartNew();
var got = Solution();
stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");
if (got == expectOutput)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
Console.WriteLine($"Expected:\n{expectOutput}");
Console.WriteLine($"Got:\n{got}");
}
static unsafe Dictionary<int, Status> RunInThread(byte* fileData, long pos, long end)
{
// Dictionary key 는 string 대신 hashCode 를 사용합니다
var localHashMap = new Dictionary<int, Status>(DEFAULT_CAPACITY);
var cityNameByteArray = new byte[DEFAULT_CAPACITY];
while (pos < end)
{
var hashCode = 0;
var measurement = 0;
var cityNameWriteIndex = 0;
do
{
var current = fileData[pos];
cityNameByteArray[cityNameWriteIndex] = current;
cityNameWriteIndex += 1;
hashCode = 31 * hashCode + (current & 0xff);
pos += 1L;
} while (fileData[pos] != ';');
pos += 1; // b[pos] == ';' 이므로 1 추가
do
{
measurement *= 10;
measurement += (fileData[pos] - '0');
pos += 1L;
} while (fileData[pos] != '\n');
pos += 1; // b[pos] == '\n' 이므로 1 추가
if (!localHashMap.TryGetValue(hashCode, out var status))
{
var cityName = Encoding.UTF8.GetString(cityNameByteArray, 0, cityNameWriteIndex);
localHashMap[hashCode] = new Status
{
cityName = cityName,
min = measurement,
max = measurement,
total = measurement,
count = 1
};
}
else
{
status.min = Math.Min(status.min, measurement);
status.max = Math.Max(status.max, measurement);
status.total += measurement;
status.count += 1;
}
}
return localHashMap;
}
static unsafe string Solution()
{
/////////////////////////////////////////////////////////////////////////////////////////
// 전역
var fileInfo = new FileInfo(MEASUREMENTS_PATH);
var fileSize = fileInfo.Length;
var processorCount = Environment.ProcessorCount;
var threadSize = processorCount - 1;
using var mmf = MemoryMappedFile.CreateFromFile(MEASUREMENTS_PATH, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
using var accessor = mmf.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
byte* fileData = null; // <- 전체 파일 데이터 포인터
var accessorSafeMemoryMappedViewHandle = accessor.SafeMemoryMappedViewHandle;
accessorSafeMemoryMappedViewHandle.AcquirePointer(ref fileData);
/////////////////////////////////////////////////////////////////////////////////////////
var hashMaps = new Dictionary<int, Status>[processorCount];
var threads = new Thread[threadSize];
var workSize = fileSize / processorCount;
var offset = 0L;
// 0부터 (N - 1) 까지
for (var i = 0; i < threadSize; ++i)
{
var currentIt = i;
var currentOffset = offset;
var currentEnd = offset + workSize;
while (fileData[currentEnd] != '\n')
{
currentEnd += 1L;
}
offset = currentEnd + 1L;
var thread = new Thread(() =>
{
hashMaps[currentIt] = RunInThread(fileData, currentOffset, currentEnd);
});
thread.UnsafeStart();
threads[i] = thread;
}
// 마지막
hashMaps[threadSize] = RunInThread(fileData, offset, fileSize);
for (var i = 0; i < threadSize; ++i)
{
threads[i].Join();
}
accessorSafeMemoryMappedViewHandle.ReleasePointer();
// Get Key List, then sort it
var globalHashMap = new Dictionary<int, Status>(DEFAULT_CAPACITY);
foreach (var hashMap in hashMaps)
{
foreach (var (hash, elem) in hashMap)
{
if (globalHashMap.TryGetValue(hash, out var status))
{
status.count += elem.count;
status.total += elem.total;
status.max = Math.Max(status.max, elem.max);
status.min = Math.Min(status.min, elem.min);
}
else
{
globalHashMap[hash] = elem;
}
}
}
var valueList = globalHashMap.Values.ToList();
valueList.Sort((lhs, rhs) => string.Compare(lhs.cityName, rhs.cityName, StringComparison.Ordinal));
var result = new StringBuilder();
foreach (var status in valueList)
{
var avg = status.total / status.count;
result.Append($"{status.cityName}={status.min};{status.max};{avg}({status.total}/{status.count})\n");
}
return result.ToString();
}
public class Status
{
public string cityName = null!;
public long min;
public long max;
public long total;
public long count;
};