update 1 ig

This commit is contained in:
2026-04-28 14:22:11 +07:00
parent 3495bb02f4
commit 2645098d33
6 changed files with 153 additions and 144 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
*.xlsx *.xlsx
**/out **/out/**
**/log* **/log*
**/lab* **/lab*
+137 -128
View File
@@ -1,187 +1,196 @@
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream> #include <iostream>
#include <random> #include <vector>
#include <sstream>
#include <string>
#include <sys/ipc.h> #include <sys/ipc.h>
#include <sys/shm.h> #include <sys/shm.h>
#include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <ctime>
#include <iomanip>
#include <algorithm>
#include <random>
#include <sstream>
namespace { constexpr int NIL = -1;
constexpr int kDefaultN = 20000; struct Node {
constexpr int kDefaultMaxDepth = 3; int value;
constexpr int kPreviewCount = 20; int next;
};
struct SharedData {
int head;
Node nodes[1];
};
std::string now() { std::string now() {
timeval tv{}; timeval tv{};
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
std::ostringstream oss; std::ostringstream oss;
oss << tv.tv_sec << "." << std::setfill('0') << std::setw(6) << tv.tv_usec; oss << tv.tv_sec << "." << std::setfill('0') << std::setw(6) << tv.tv_usec;
return oss.str(); return oss.str();
} }
void log_start(int l, int r, int depth) { void log_start(int head, int depth) {
std::cout << "PROC_START pid=" << getpid() std::cout << "PROC_START pid=" << getpid()
<< " ppid=" << getppid() << " ppid=" << getppid()
<< " depth=" << depth << " depth=" << depth
<< " l=" << l << " l=" << head
<< " r=" << r << " r=0"
<< " ts=" << now() << " ts=" << now()
<< '\n' << std::flush; << '\n' << std::flush;
} }
void log_end(int l, int r, int depth) { void log_end(int head, int depth) {
std::cout << "PROC_END pid=" << getpid() std::cout << "PROC_END pid=" << getpid()
<< " ppid=" << getppid() << " ppid=" << getppid()
<< " depth=" << depth << " depth=" << depth
<< " l=" << l << " l=" << head
<< " r=" << r << " r=0"
<< " ts=" << now() << " ts=" << now()
<< '\n' << std::flush; << '\n' << std::flush;
} }
void merge_range(int* arr, int l, int m, int r) { int merge_lists(Node* pool, int left_head, int right_head) {
std::vector<int> temp; if (left_head == NIL) return right_head;
temp.reserve(r - l + 1); if (right_head == NIL) return left_head;
int i = l; int res_head = NIL;
int j = m + 1; int tail = NIL;
while (i <= m && j <= r) { auto append = [&](int node_idx) {
if (arr[i] <= arr[j]) { if (res_head == NIL) {
temp.push_back(arr[i++]); res_head = node_idx;
tail = node_idx;
} else { } else {
temp.push_back(arr[j++]); pool[tail].next = node_idx;
tail = node_idx;
}
};
while (left_head != NIL && right_head != NIL) {
if (pool[left_head].value <= pool[right_head].value) {
int next = pool[left_head].next;
append(left_head);
left_head = next;
} else {
int next = pool[right_head].next;
append(right_head);
right_head = next;
} }
} }
if (left_head != NIL) pool[tail].next = left_head;
while (i <= m) temp.push_back(arr[i++]); if (right_head != NIL) pool[tail].next = right_head;
while (j <= r) temp.push_back(arr[j++]); return res_head;
std::copy(temp.begin(), temp.end(), arr + l);
} }
void local_sort(int* arr, int l, int r) { void split_list(Node* pool, int head, int& left, int& right) {
if (l >= r) return; if (head == NIL || pool[head].next == NIL) {
left = head;
const int m = l + (r - l) / 2; right = NIL;
local_sort(arr, l, m);
local_sort(arr, m + 1, r);
merge_range(arr, l, m, r);
}
void parallel_sort(int* arr, int l, int r, int depth, int max_depth) {
log_start(l, r, depth);
if (l >= r) {
log_end(l, r, depth);
return; return;
} }
int slow = head;
int fast = pool[head].next;
while (fast != NIL) {
fast = pool[fast].next;
if (fast != NIL) {
slow = pool[slow].next;
fast = pool[fast].next;
}
}
left = head;
right = pool[slow].next;
pool[slow].next = NIL;
}
int local_list_sort(Node* pool, int head) {
if (head == NIL || pool[head].next == NIL) return head;
int left, right;
split_list(pool, head, left, right);
left = local_list_sort(pool, left);
right = local_list_sort(pool, right);
return merge_lists(pool, left, right);
}
int parallel_list_sort(Node* pool, int head, int depth, int max_depth) {
log_start(head, depth);
if (head == NIL || pool[head].next == NIL) {
log_end(head, depth);
return head;
}
const int m = l + (r - l) / 2;
if (depth >= max_depth) { if (depth >= max_depth) {
local_sort(arr, l, r); int result = local_list_sort(pool, head);
log_end(l, r, depth); log_end(result, depth);
return; return result;
} }
pid_t left = fork(); int left_part, right_part;
if (left == 0) { split_list(pool, head, left_part, right_part);
parallel_sort(arr, l, m, depth + 1, max_depth);
_exit(EXIT_SUCCESS); int pipe_l[2], pipe_r[2];
pipe(pipe_l);
pipe(pipe_r);
// Левый потомок
pid_t pid_l = fork();
if (pid_l == 0) {
close(pipe_l[0]); close(pipe_r[0]); close(pipe_r[1]);
int res = parallel_list_sort(pool, left_part, depth + 1, max_depth);
write(pipe_l[1], &res, sizeof(int));
close(pipe_l[1]);
_exit(0);
} }
if (left < 0) { // Правый потомок
std::perror("fork left"); pid_t pid_r = fork();
local_sort(arr, l, m); if (pid_r == 0) {
close(pipe_r[0]); close(pipe_l[0]); close(pipe_l[1]);
int res = parallel_list_sort(pool, right_part, depth + 1, max_depth);
write(pipe_r[1], &res, sizeof(int));
close(pipe_r[1]);
_exit(0);
} }
pid_t right = fork(); // Родитель ждет обоих
if (right == 0) { int sorted_l, sorted_r;
parallel_sort(arr, m + 1, r, depth + 1, max_depth); close(pipe_l[1]); close(pipe_r[1]);
_exit(EXIT_SUCCESS);
}
if (right < 0) { waitpid(pid_l, nullptr, 0);
std::perror("fork right"); read(pipe_l[0], &sorted_l, sizeof(int));
local_sort(arr, m + 1, r);
}
int left_status = 0; waitpid(pid_r, nullptr, 0);
if (left > 0 && waitpid(left, &left_status, 0) < 0) { read(pipe_r[0], &sorted_r, sizeof(int));
std::perror("waitpid left");
local_sort(arr, l, m);
} else if (left > 0 && (!WIFEXITED(left_status) || WEXITSTATUS(left_status) != 0)) {
local_sort(arr, l, m);
}
int right_status = 0; close(pipe_l[0]); close(pipe_r[0]);
if (right > 0 && waitpid(right, &right_status, 0) < 0) {
std::perror("waitpid right");
local_sort(arr, m + 1, r);
} else if (right > 0 && (!WIFEXITED(right_status) || WEXITSTATUS(right_status) != 0)) {
local_sort(arr, m + 1, r);
}
merge_range(arr, l, m, r); int final_head = merge_lists(pool, sorted_l, sorted_r);
log_end(l, r, depth); log_end(final_head, depth);
return final_head;
} }
bool parse_positive_int(const char* value, int& out) {
try {
size_t consumed = 0;
const int parsed = std::stoi(value, &consumed);
if (value[consumed] != '\0') return false;
if (parsed <= 0) return false;
out = parsed;
return true;
} catch (...) { return false; }
}
} // namespace
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int n = kDefaultN; int n = 1000;
int max_depth = kDefaultMaxDepth; int max_depth = 3;
if (argc > 1) n = std::atoi(argv[1]);
if (argc > 2) max_depth = std::atoi(argv[2]);
if (argc >= 2 && !parse_positive_int(argv[1], n)) { size_t shm_size = sizeof(int) + sizeof(Node) * n;
std::cerr << "Invalid array size: " << argv[1] << '\n'; int shmid = shmget(IPC_PRIVATE, shm_size, IPC_CREAT | 0666);
return EXIT_FAILURE; SharedData* data = (SharedData*)shmat(shmid, nullptr, 0);
}
if (argc >= 3 && !parse_positive_int(argv[2], max_depth)) { std::mt19937 rng(time(0));
std::cerr << "Invalid max depth: " << argv[2] << '\n'; std::uniform_int_distribution<int> dist(0, 9999);
return EXIT_FAILURE; for (int i = 0; i < n; ++i) {
data->nodes[i].value = dist(rng);
data->nodes[i].next = (i == n - 1) ? NIL : i + 1;
} }
const size_t shm_size = static_cast<size_t>(n) * sizeof(int); data->head = parallel_list_sort(data->nodes, data->head, 0, max_depth);
const int shmid = shmget(IPC_PRIVATE, shm_size, IPC_CREAT | 0600);
if (shmid < 0) {
std::perror("shmget");
return EXIT_FAILURE;
}
int* arr = static_cast<int*>(shmat(shmid, nullptr, 0)); shmdt(data);
if (arr == reinterpret_cast<void*>(-1)) { shmctl(shmid, IPC_RMID, nullptr);
std::perror("shmat"); return 0;
shmctl(shmid, IPC_RMID, nullptr);
return EXIT_FAILURE;
}
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(0, 99999);
for (int i = 0; i < n; ++i) arr[i] = dist(rng);
parallel_sort(arr, 0, n - 1, 0, max_depth);
if (shmdt(arr) < 0) std::perror("shmdt");
if (shmctl(shmid, IPC_RMID, nullptr) < 0) std::perror("shmctl IPC_RMID");
return EXIT_SUCCESS;
} }
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 62 KiB

+15 -15
View File
@@ -1,17 +1,17 @@
| PID | PPID | Depth | Start | End | Offset | Duration | | PID | PPID | Depth | Start | End | Offset | Duration |
|---|---|---|---|---|---|---| |---|---|---|---|---|---|---|
| 45686 | 45683 | 0 | 1777075170.175506 | 1777075170.185554 | 0.000000 | 0.010048 | | 27445 | 27444 | 0 | 1777360735.363420 | 1777360735.365566 | 0.000000 | 0.002146 |
| 45687 | 45686 | 1 | 1777075170.176607 | 1777075170.184457 | 0.001101 | 0.007850 | | 27446 | 27445 | 1 | 1777360735.363734 | 1777360735.365203 | 0.000314 | 0.001469 |
| 45689 | 45687 | 2 | 1777075170.177348 | 1777075170.182180 | 0.001842 | 0.004832 | | 27448 | 27446 | 2 | 1777360735.364001 | 1777360735.364799 | 0.000581 | 0.000798 |
| 45693 | 45689 | 3 | 1777075170.178106 | 1777075170.180091 | 0.002600 | 0.001985 | | 27452 | 27448 | 3 | 1777360735.364270 | 1777360735.364333 | 0.000850 | 0.000063 |
| 45696 | 45689 | 3 | 1777075170.178728 | 1777075170.180323 | 0.003222 | 0.001595 | | 27454 | 27448 | 3 | 1777360735.364405 | 1777360735.364467 | 0.000985 | 0.000062 |
| 45691 | 45687 | 2 | 1777075170.177532 | 1777075170.183675 | 0.002026 | 0.006143 | | 27450 | 27446 | 2 | 1777360735.364143 | 1777360735.365022 | 0.000723 | 0.000879 |
| 45695 | 45691 | 3 | 1777075170.178382 | 1777075170.182874 | 0.002876 | 0.004492 | | 27458 | 27450 | 3 | 1777360735.364584 | 1777360735.364655 | 0.001164 | 0.000071 |
| 45698 | 45691 | 3 | 1777075170.178693 | 1777075170.180896 | 0.003187 | 0.002203 | | 27456 | 27450 | 3 | 1777360735.364653 | 1777360735.364767 | 0.001233 | 0.000114 |
| 45688 | 45686 | 1 | 1777075170.176646 | 1777075170.184045 | 0.001140 | 0.007399 | | 27447 | 27445 | 1 | 1777360735.363775 | 1777360735.365355 | 0.000355 | 0.001580 |
| 45690 | 45688 | 2 | 1777075170.177346 | 1777075170.182832 | 0.001840 | 0.005486 | | 27449 | 27447 | 2 | 1777360735.364026 | 1777360735.364811 | 0.000606 | 0.000785 |
| 45694 | 45690 | 3 | 1777075170.178801 | 1777075170.180342 | 0.003295 | 0.001541 | | 27453 | 27449 | 3 | 1777360735.364323 | 1777360735.364385 | 0.000903 | 0.000062 |
| 45697 | 45690 | 3 | 1777075170.180099 | 1777075170.182005 | 0.004593 | 0.001906 | | 27455 | 27449 | 3 | 1777360735.364502 | 1777360735.364563 | 0.001082 | 0.000061 |
| 45692 | 45688 | 2 | 1777075170.178014 | 1777075170.183226 | 0.002508 | 0.005212 | | 27451 | 27447 | 2 | 1777360735.364192 | 1777360735.365179 | 0.000772 | 0.000987 |
| 45699 | 45692 | 3 | 1777075170.178864 | 1777075170.180396 | 0.003358 | 0.001532 | | 27457 | 27451 | 3 | 1777360735.364683 | 1777360735.364743 | 0.001263 | 0.000060 |
| 45700 | 45692 | 3 | 1777075170.181111 | 1777075170.182464 | 0.005605 | 0.001353 | | 27459 | 27451 | 3 | 1777360735.364906 | 1777360735.364965 | 0.001486 | 0.000059 |