diff --git a/lab1 b/lab1 index b10999b..281ff03 100755 Binary files a/lab1 and b/lab1 differ diff --git a/main.cpp b/main.cpp index 4ed4f53..5f95ab6 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,13 @@ struct SharedData { int arr[MAX]; }; +// ================= TIME ================= +double now() { + timeval t; + gettimeofday(&t, NULL); + return t.tv_sec + t.tv_usec / 1e6; +} + // ================= MERGE ================= void merge(int *arr, int l, int m, int r) { int *temp = new int[r - l + 1]; @@ -50,14 +57,27 @@ void local_sort(int *arr, int l, int r) { void parallel_sort(int *arr, int l, int r, int depth) { if (l >= r) return; - int m = (l + r) / 2; + double start = now(); - cout << "PID " << getpid() + pid_t pid = getpid(); + pid_t ppid = getppid(); + + cout << "[START] t=" << start + << " PID=" << pid + << " PPID=" << ppid << " depth=" << depth << " range=[" << l << "," << r << "]\n"; + int m = (l + r) / 2; + if (depth >= MAX_DEPTH) { local_sort(arr, l, r); + + double end = now(); + cout << "[END] t=" << end + << " PID=" << pid + << " duration=" << (end - start) + << " sec\n"; return; } @@ -79,6 +99,13 @@ void parallel_sort(int *arr, int l, int r, int depth) { waitpid(right, NULL, 0); merge(arr, l, m, r); + + double end = now(); + + cout << "[END] t=" << end + << " PID=" << pid + << " duration=" << (end - start) + << " sec\n"; } // ================= MAIN ================= @@ -95,19 +122,17 @@ int main() { for (int i = 0; i < n; i++) data->arr[i] = rand() % 100000; - timeval start, end; + double global_start = now(); - gettimeofday(&start, NULL); + cout << "Main PID=" << getpid() << "\n\n"; parallel_sort(data->arr, 0, n - 1, 0); - gettimeofday(&end, NULL); + double global_end = now(); - double time_spent = - (end.tv_sec - start.tv_sec) + - (end.tv_usec - start.tv_usec) / 1e6; - - cout << "\nExecution time: " << time_spent << " sec\n"; + cout << "\nExecution time: " + << (global_end - global_start) + << " sec\n"; // Проверка for (int i = 1; i < n; i++) {