update 2 main to proper rules
This commit is contained in:
+100
-147
@@ -1,206 +1,159 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <algorithm>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <sstream>
|
#include <pthread.h>
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pthread.h>
|
#include <sstream>
|
||||||
#include <vector>
|
|
||||||
#include <mutex>
|
|
||||||
|
|
||||||
// ================= CONFIG =================
|
// ================= СТРУКТУРЫ =================
|
||||||
constexpr int kDefaultN = 20000;
|
constexpr int NIL = -1;
|
||||||
constexpr int kPreviewCount = 20;
|
|
||||||
|
|
||||||
// ================= GLOBAL CONTROL =================
|
struct Node {
|
||||||
|
int value;
|
||||||
|
int next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SortResult {
|
||||||
|
int head;
|
||||||
|
long long comparisons;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Args {
|
||||||
|
Node* pool;
|
||||||
|
int head;
|
||||||
|
int depth;
|
||||||
|
SortResult* res_out;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ================= ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ =================
|
||||||
int active_threads = 0;
|
int active_threads = 0;
|
||||||
int max_threads = 4;
|
int max_threads = 4;
|
||||||
|
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
|
// ================= УТИЛИТЫ =================
|
||||||
std::mutex log_mutex;
|
|
||||||
|
|
||||||
// ================= TIME =================
|
|
||||||
double get_time() {
|
|
||||||
timeval tv{};
|
|
||||||
gettimeofday(&tv, nullptr);
|
|
||||||
return tv.tv_sec + tv.tv_usec * 1e-6;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= SAFE LOGGING =================
|
// Формат лога адаптирован под ваш скрипт analyze_log.py
|
||||||
void log_start(int l, int r, int depth) {
|
void log_event(const std::string& type, int head, int depth) {
|
||||||
std::lock_guard<std::mutex> lock(log_mutex);
|
pthread_mutex_lock(&log_mutex);
|
||||||
|
// Используем head как идентификатор диапазона для парсера
|
||||||
std::cout << "START PID=" << getpid()
|
std::cout << type << " TID=" << pthread_self()
|
||||||
<< " TID=" << pthread_self()
|
|
||||||
<< " depth=" << depth
|
<< " depth=" << depth
|
||||||
<< " range=[" << l << "," << r << "] time=" << now()
|
<< " range=[" << head << "," << head << "]"
|
||||||
<< '\n';
|
<< " time=" << now() << std::endl;
|
||||||
|
pthread_mutex_unlock(&log_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_end(int l, int r, int depth) {
|
// ================= ЛОГИКА СПИСКА =================
|
||||||
std::lock_guard<std::mutex> lock(log_mutex);
|
void split_list(Node* pool, int head, int& left, int& right) {
|
||||||
|
if (head == NIL || pool[head].next == NIL) {
|
||||||
std::cout << "END PID=" << getpid()
|
left = head; right = NIL; return;
|
||||||
<< " TID=" << pthread_self()
|
|
||||||
<< " depth=" << depth
|
|
||||||
<< " range=[" << l << "," << r << "] time=" << now()
|
|
||||||
<< '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================= MERGE =================
|
|
||||||
void merge_range(int* arr, int l, int m, int r) {
|
|
||||||
std::vector<int> tmp;
|
|
||||||
tmp.reserve(r - l + 1);
|
|
||||||
|
|
||||||
int i = l;
|
|
||||||
int j = m + 1;
|
|
||||||
|
|
||||||
while (i <= m && j <= r) {
|
|
||||||
if (arr[i] <= arr[j]) tmp.push_back(arr[i++]);
|
|
||||||
else tmp.push_back(arr[j++]);
|
|
||||||
}
|
}
|
||||||
|
int slow = head, fast = pool[head].next;
|
||||||
while (i <= m) tmp.push_back(arr[i++]);
|
while (fast != NIL) {
|
||||||
while (j <= r) tmp.push_back(arr[j++]);
|
fast = pool[fast].next;
|
||||||
|
if (fast != NIL) { slow = pool[slow].next; fast = pool[fast].next; }
|
||||||
std::copy(tmp.begin(), tmp.end(), arr + l);
|
}
|
||||||
|
left = head;
|
||||||
|
right = pool[slow].next;
|
||||||
|
pool[slow].next = NIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= LOCAL SORT =================
|
SortResult merge_lists(Node* pool, int l, int r) {
|
||||||
void local_sort(int* arr, int l, int r) {
|
long long comps = 0;
|
||||||
if (l >= r) return;
|
if (l == NIL) return {r, 0};
|
||||||
|
if (r == NIL) return {l, 0};
|
||||||
int m = l + (r - l) / 2;
|
int res_head = NIL, tail = NIL;
|
||||||
local_sort(arr, l, m);
|
auto append = [&](int idx) {
|
||||||
local_sort(arr, m + 1, r);
|
if (res_head == NIL) res_head = tail = idx;
|
||||||
merge_range(arr, l, m, r);
|
else { pool[tail].next = idx; tail = idx; }
|
||||||
|
};
|
||||||
|
while (l != NIL && r != NIL) {
|
||||||
|
comps++;
|
||||||
|
if (pool[l].value <= pool[r].value) { int n = pool[l].next; append(l); l = n; }
|
||||||
|
else { int n = pool[r].next; append(r); r = n; }
|
||||||
|
}
|
||||||
|
if (l != NIL) pool[tail].next = l;
|
||||||
|
if (r != NIL) pool[tail].next = r;
|
||||||
|
return {res_head, comps};
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= THREAD ARG =================
|
// ================= РЕКУРСИЯ =================
|
||||||
struct Args {
|
SortResult parallel_list_sort(Node* pool, int head, int depth);
|
||||||
int* arr;
|
|
||||||
int l;
|
|
||||||
int r;
|
|
||||||
int depth;
|
|
||||||
};
|
|
||||||
|
|
||||||
void parallel_sort(int* arr, int l, int r, int depth);
|
|
||||||
|
|
||||||
void* thread_func(void* arg) {
|
void* thread_func(void* arg) {
|
||||||
Args* a = (Args*)arg;
|
Args* a = (Args*)arg;
|
||||||
parallel_sort(a->arr, a->l, a->r, a->depth);
|
*(a->res_out) = parallel_list_sort(a->pool, a->head, a->depth);
|
||||||
delete a;
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= PARALLEL SORT =================
|
SortResult parallel_list_sort(Node* pool, int head, int depth) {
|
||||||
void parallel_sort(int* arr, int l, int r, int depth) {
|
log_event("START", head, depth);
|
||||||
log_start(l, r, depth);
|
if (head == NIL || pool[head].next == NIL) {
|
||||||
|
log_event("END", head, depth);
|
||||||
if (l >= r) {
|
return {head, 0};
|
||||||
log_end(l, r, depth);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int m = l + (r - l) / 2;
|
int left_p, right_p;
|
||||||
|
split_list(pool, head, left_p, right_p);
|
||||||
|
|
||||||
pthread_t tid;
|
pthread_t tid;
|
||||||
bool spawned = false;
|
bool spawned = false;
|
||||||
|
SortResult res_right = {NIL, 0};
|
||||||
|
|
||||||
// ===== thread limit control =====
|
pthread_mutex_lock(&counter_mutex);
|
||||||
pthread_mutex_lock(&thread_mutex);
|
|
||||||
if (active_threads < max_threads) {
|
if (active_threads < max_threads) {
|
||||||
active_threads++;
|
active_threads++;
|
||||||
spawned = true;
|
spawned = true;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&thread_mutex);
|
pthread_mutex_unlock(&counter_mutex);
|
||||||
|
|
||||||
if (spawned) {
|
if (spawned) {
|
||||||
Args* args = new Args{arr, m + 1, r, depth + 1};
|
Args* args = new Args{pool, right_p, depth + 1, &res_right};
|
||||||
pthread_create(&tid, nullptr, thread_func, args);
|
pthread_create(&tid, nullptr, thread_func, args);
|
||||||
|
|
||||||
parallel_sort(arr, l, m, depth + 1);
|
SortResult res_left = parallel_list_sort(pool, left_p, depth + 1);
|
||||||
|
|
||||||
pthread_join(tid, nullptr);
|
pthread_join(tid, nullptr);
|
||||||
|
|
||||||
pthread_mutex_lock(&thread_mutex);
|
pthread_mutex_lock(&counter_mutex);
|
||||||
active_threads--;
|
active_threads--;
|
||||||
pthread_mutex_unlock(&thread_mutex);
|
pthread_mutex_unlock(&counter_mutex);
|
||||||
|
|
||||||
|
SortResult m = merge_lists(pool, res_left.head, res_right.head);
|
||||||
|
delete args;
|
||||||
|
log_event("END", m.head, depth);
|
||||||
|
return {m.head, res_left.comparisons + res_right.comparisons + m.comparisons};
|
||||||
} else {
|
} else {
|
||||||
parallel_sort(arr, l, m, depth + 1);
|
SortResult res_left = parallel_list_sort(pool, left_p, depth + 1);
|
||||||
parallel_sort(arr, m + 1, r, depth + 1);
|
SortResult res_r_seq = parallel_list_sort(pool, right_p, depth + 1);
|
||||||
}
|
SortResult m = merge_lists(pool, res_left.head, res_r_seq.head);
|
||||||
|
log_event("END", m.head, depth);
|
||||||
merge_range(arr, l, m, r);
|
return {m.head, res_left.comparisons + res_r_seq.comparisons + m.comparisons};
|
||||||
|
|
||||||
log_end(l, r, depth);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================= UTIL =================
|
|
||||||
bool parse_int(const char* s, int& out) {
|
|
||||||
try {
|
|
||||||
size_t p;
|
|
||||||
int v = std::stoi(s, &p);
|
|
||||||
if (s[p] != '\0' || v < 0) return false;
|
|
||||||
out = v;
|
|
||||||
return true;
|
|
||||||
} catch (...) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= MAIN =================
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
int n = kDefaultN;
|
int n = 10000;
|
||||||
|
if (argc >= 2) n = std::atoi(argv[1]);
|
||||||
|
if (argc >= 3) max_threads = std::atoi(argv[2]);
|
||||||
|
|
||||||
if (argc >= 2 && !parse_int(argv[1], n)) {
|
std::vector<Node> pool(n);
|
||||||
std::cerr << "Invalid N\n";
|
std::mt19937 rng(time(0));
|
||||||
return 1;
|
for (int i = 0; i < n; i++) {
|
||||||
|
pool[i].value = rng() % 100000;
|
||||||
|
pool[i].next = (i == n - 1) ? NIL : i + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc >= 3 && !parse_int(argv[2], max_threads)) {
|
parallel_list_sort(pool.data(), 0, 0);
|
||||||
std::cerr << "Invalid threads\n";
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<int> arr(n);
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
std::cout << "Before: ";
|
|
||||||
for (int i = 0; i < std::min(n, kPreviewCount); i++)
|
|
||||||
std::cout << arr[i] << " ";
|
|
||||||
std::cout << "\n\n";
|
|
||||||
|
|
||||||
double t1 = get_time();
|
|
||||||
|
|
||||||
parallel_sort(arr.data(), 0, n - 1, 0);
|
|
||||||
|
|
||||||
double t2 = get_time();
|
|
||||||
|
|
||||||
std::cout << "\nAfter: ";
|
|
||||||
for (int i = 0; i < std::min(n, kPreviewCount); i++)
|
|
||||||
std::cout << arr[i] << " ";
|
|
||||||
std::cout << "\n";
|
|
||||||
|
|
||||||
std::cout << "\nTime: " << (t2 - t1) << " sec\n";
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user