-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.cpp
More file actions
134 lines (112 loc) · 4.39 KB
/
data.cpp
File metadata and controls
134 lines (112 loc) · 4.39 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
#include "data.h"
#include <iostream>
#include <limits>
#include <numeric>
#include<random>
// void SharedData::proportion() {
// int num_slice = (T - 1) / FrePerSlicing + 1;
// //////////////////
// std::vector<int> block_size(num_slice, 0);
// std::vector<int> sum_slices(num_slice, 0);
// int sum = 0;
// for (int t = 0; t < num_slice; t++){
// int sum_slice = 0;
// for(int m = 0; m < M; m++){
// sum_slice += fre_read[m][t];
// }
// sum += sum_slice;
// sum_slices[t] = sum_slice;
// }
// for(int t = 0; t < num_slice; t++){
// block_size[t] = static_cast<int>(V * (static_cast<double>(sum_slices[t])/sum));
// }
// //normalize并对disk_partition[m][t]赋值
// std::vector<double> slice_proportion (M , 0.0);
// for (int m = 0; m < M ; m++) {
// slice_proportion[m] = static_cast<double>(current_slice_read[m])/ sum;
// disk_partition[m+1] = static_cast<int>(block_size[t] * slice_proportion[m]);
// }
// }
SharedData::SharedData():
request(MaxRequestNum),
object(MaxObjectNum),
readable_labels()
{
std::cin >> T >> M >> N >> V >> G >> K;
// Initialize data
disk = std::vector<std::vector<int>>(N + 1, std::vector<int>(V + 1, 0));
//disk_point = std::vector<int>(N + 1, 1);
disk_point = std::vector<std::vector<int>>(N + 1, std::vector<int>(2, 1));
fre_del = std::vector<std::vector<int>>(M, std::vector<int>((T - 1) / FrePerSlicing + 1, 0));
fre_write = std::vector<std::vector<int>>(M, std::vector<int>((T - 1) / FrePerSlicing + 1, 0));
fre_read = std::vector<std::vector<int>>(M, std::vector<int>((T - 1) / FrePerSlicing + 1, 0));
disk_partition = std::vector<int>(M + 1, 1 ); //==1是因为磁盘unit索引从1开始
tag_max_capacity = std::vector<int>(M + 1, 0);
label_assignment.resize(M + 1);
request_num = 0;
vec = std::vector<int>(M + 1, rand());
// Read pre-processed data
for (int i = 0; i < M; i++) {
for (int j = 0; j < (T - 1) / FrePerSlicing + 1; j++) {
std::cin >> fre_del.at(i).at(j);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < (T - 1) / FrePerSlicing + 1; j++) {
std::cin >> fre_write.at(i).at(j);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < (T - 1) / FrePerSlicing + 1; j++) {
std::cin >> fre_read.at(i).at(j);
}
}
std::vector<std::vector<double>> correlationMatrix = Count_Correlation(fre_read);// Calculate correlation matrix
std::vector<bool> paired(M + 1, false);
int count = 1;
int n = 1;
label_assignment[count] = 1;
paired[count] = true;
while (count < M) {
int next_m = n;
double max_correlation = std::numeric_limits<double>::lowest();
int next_n = -1;
// 2. Select the unpaired variable next_n with the highest correlation to next_m
for (int i = 1; i <= M; ++i) {
if (!paired[i] && correlationMatrix[next_m][i] > max_correlation) {
max_correlation = correlationMatrix[next_m][i];
next_n = i;
}
}
if (next_n == -1) break; // No more variables to pair
paired[next_n] = true;
label_assignment[++count] = next_n;
n = next_n;
}
// Assign values to the disk partition array based on pre-processed data
int total_capacity = 0;
for (int m = 1; m <= M ; m++) {
int tag_total_occupied = 0;
for (int j = 0; j < (T - 1) / FrePerSlicing + 1; j++) {
int diff = fre_write[m-1][j] - fre_del[m-1][j];
if (diff > 0) tag_total_occupied += diff;
}
tag_max_capacity[m] = tag_total_occupied; //in one slice
total_capacity += tag_max_capacity[m];
}
int available_space = static_cast<int>(V * (1 - buffer)); // Available disk space
int allocated_space = 0;
for (int m = 1; m <= M; m++) {
int label = label_assignment[m];
// int label = m;
double ratio = static_cast<double>(tag_max_capacity[label]) / total_capacity;
// Allocate disk space
int space_for_tag = static_cast<int>(available_space * ratio);
// Update disk_partition
disk_partition[m] = allocated_space + space_for_tag;
// Accumulate allocated space
allocated_space += space_for_tag;
}
printf("OK\n");
fflush(stdout);
}