From 00b52d21840ee28b775fe82fd4ebe00069761095 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Tue, 30 Mar 2021 21:14:30 +0300 Subject: [PATCH 01/24] WoMM --- .DS_Store | Bin 0 -> 6148 bytes .codecov.yml | 5 ++ .travis.yml | 32 ++++++++ CPPLINT.cfg | 4 + project/.DS_Store | Bin 0 -> 6148 bytes project/CMakeLists.txt | 57 ++++++++++++++ project/CMakeLists.txt.in | 15 ++++ project/include/search.h | 16 ++++ project/src/linear.c | 43 +++++++++++ project/src/main.c | 42 +++++++++++ project/src/parallel.c | 148 +++++++++++++++++++++++++++++++++++++ project/tests/linear.cpp | 66 +++++++++++++++++ project/tests/main.cpp | 8 ++ project/tests/parallel.cpp | 66 +++++++++++++++++ 14 files changed, 502 insertions(+) create mode 100644 .DS_Store create mode 100644 .codecov.yml create mode 100644 .travis.yml create mode 100644 CPPLINT.cfg create mode 100644 project/.DS_Store create mode 100644 project/CMakeLists.txt create mode 100644 project/CMakeLists.txt.in create mode 100644 project/include/search.h create mode 100644 project/src/linear.c create mode 100644 project/src/main.c create mode 100644 project/src/parallel.c create mode 100644 project/tests/linear.cpp create mode 100644 project/tests/main.cpp create mode 100644 project/tests/parallel.cpp diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5e22fcf8d6a84fa96695b47ec49586dd59f581cc GIT binary patch literal 6148 zcmeHKy-piJ5T3OStc_4yAVIlJIu~|8Mkyj4#uVw04v``txZ^B*(fRgdpRvFg-D{9H z;5F!Yg7gs&K$9*-W`Aq}289A4gl44KZ+3QO_x8Kf?hOF2Mn9+llmURFD$EtJc|_E> z(kaPV&nzO-bF|ZSc&!|Q%my)7?o`_|r;GMd<)zZ13w`{l(A8K+(tx4Jqx3|bhn z`z?$EIcUg!9Q5=&t{LFG$cy{s<>7F(zE%+{YxPk@3_pLWR>bN`eKacarH{2QUk^Gb zr^B<+`Gw(@;b*AjgT*oYMB~9WqobO8C_BkGWICcFn@vB`MDW|2Kff>kUadbC6+9y< zY5#AvW49YeGCsl;#=E%lXk-!t!~ikyk_^~mhR?s`q%;>XKn%PK2Gsc=P!&2BGlTl* zz($tgOAHVL_Zi5WZdu*`n@`vO_mikc3=jkFiUC$=y3Gb|$=2& literal 0 HcmV?d00001 diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 0000000..926aa8c --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,5 @@ +coverage: + status: + patch: + default: + target:80% diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..74ea9e8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,32 @@ + +dist: focal + +language: c + +os: linux + +compiler: gcc + +install: + - sudo apt-get install valgrind + - sudo apt-get install clang + - sudo apt-get install cppcheck + - sudo pip install cpplint + +script: + - cd project/ + - mkdir build + - cd build + - cmake .. + - make clean && make + - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c + - cpplint ../include/*.h ../src/*.c ../tests/*.cpp + - valgrind --leak-check=full --track-origins=yes ./main_linear.out + - valgrind --leak-check=full --track-origins=yes ./main_parallel.out + - valgrind --leak-check=full --track-origins=yes ./linear_tests + - valgrind --leak-check=full --track-origins=yes ./parallel_tests + + + +after_success: + - bash <(curl -s https://codecov.io/bash) diff --git a/CPPLINT.cfg b/CPPLINT.cfg new file mode 100644 index 0000000..e5ce69c --- /dev/null +++ b/CPPLINT.cfg @@ -0,0 +1,4 @@ +filter=-legal/copyright +filter=-build/include_subdir +filter=-build/include +filter=-readability/casting diff --git a/project/.DS_Store b/project/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..eaa8cb0e96b751d3369893e2dfbdd0c51b83818f GIT binary patch literal 6148 zcmeHK!A=4(5Ss;75t&-UbGz}K z8$|BJT+?9`FbYhc0&?%>z=aO@5WwjEeRJ;uKTgW!cQ%*F=H?eTFYv-r;nKc|x_06u zow)9_&*jsxliauzpF~kGj2-vXZ*?2RmHjA896xNeRkH83aLUD*A9_)@9(6)5R_oY$ zfb%@hH;QY$UbVJe5|!;*za)CQo8^+IR%-ozp0BQN>>V|4A0B&8{pT00D#Ig4*)rh( zUNIPFtWHsZhciw`M| zkso{)Gmo@k_~OhfVFWM=7zM@^kmrLymuL~D8r9K(jVb{Uv)HW+V_8ec96@LirW$bs zjpWRv@Dvi}L + +#define NULL_ENTRY (-1) +#define MMAP_ERR 2 +#define PID_ERR 3 +#define WRONG_OPEN 4 +#define MAX_PROCESS 6 +#define READ_SIZE 20 +#define BUFFER_SIZE 25 +#define FORMAT_STRING "%020d" + +int file_search(FILE** , const char* , size_t* , size_t size_to_find); +#endif // PROJECT_INCLUDE_SEARCH_H_ diff --git a/project/src/linear.c b/project/src/linear.c new file mode 100644 index 0000000..542ff53 --- /dev/null +++ b/project/src/linear.c @@ -0,0 +1,43 @@ +// Copyright 2021 bkz + +#include +#include +#include "../include/search.h" + +int compare_to_array(const char* to_compare, char buffer, size_t size_to_find) { + for (int i = 0; i < size_to_find; i++) { + if (to_compare[i] == buffer) { + return i; + } + } + return -1; +} + +int file_search(FILE** fp, const char* to_find, + size_t* found, size_t size_to_find) { + if (fp == NULL || *fp == NULL || to_find == NULL || found == NULL) { + return NULL_ENTRY; + } + struct stat file_stat; + fstat(fileno(*fp), &file_stat); + if ((file_stat.st_mode & S_IRUSR) == 0) { + return WRONG_OPEN; + } + if (file_stat.st_size == 0) { + return NULL_ENTRY; + } + char* file_in_memory = (char *)mmap(NULL, file_stat.st_size, + PROT_READ, MAP_SHARED, fileno(*fp), 0); + if (file_in_memory == NULL) { + return MMAP_ERR; + } + for (size_t i = 0; i < file_stat.st_size; i++) { + int position = compare_to_array(to_find, + file_in_memory[i], size_to_find); + if (position != -1) { + found[position]++; + } + } + munmap(file_in_memory, file_stat.st_size); + return 0; +} diff --git a/project/src/main.c b/project/src/main.c new file mode 100644 index 0000000..a8fadc7 --- /dev/null +++ b/project/src/main.c @@ -0,0 +1,42 @@ +// Copyright 2021 bkz + +#include + +#include +#include +#include +#include "../include/search.h" + +int main() { + // LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | + // fold -w 100 | head -n 100000 > bigfile.txt + FILE* p = fopen("bigfile.txt", "r"); + char* array = (char*) calloc(3, sizeof(char)); + size_t* found = (size_t*) calloc(3, sizeof(size_t)); + array[0] = 'a'; + array[1] = 'b'; + array[2] = '\n'; + struct timespec start, finish; + double elapsed; + + clock_gettime(CLOCK_MONOTONIC, &start); + + file_search(&p, array, found, 3); + + clock_gettime(CLOCK_MONOTONIC, &finish); + + elapsed = (double)(finish.tv_sec - start.tv_sec); + elapsed += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; + + for (int i=0; i < 3; i++) { + printf("%zu ", found[i]); + } + printf("\n%lf", elapsed); + fclose(p); + + + free(array); + free(found); + + return 0; +} diff --git a/project/src/parallel.c b/project/src/parallel.c new file mode 100644 index 0000000..8d9ad26 --- /dev/null +++ b/project/src/parallel.c @@ -0,0 +1,148 @@ +// Copyright 2021 bkz +#include +#include +#include "../include/search.h" +#include +#include +#include +#include +#include +#include + +void free_all_resources(size_t* divisions, int* processes ) { + if (divisions) { + free(divisions); + } + if (processes) { + free(processes); + } +} + +int calculate_process(size_t filesize) { + if (filesize < 1000) { + return 1; + } + if (filesize < 10000) { + return 2; + } + if (filesize < 100000) { + return 3; + } + if (filesize < 1000000) { + return 4; + } + return MAX_PROCESS; +} + +int create_division(int count, size_t filesize, size_t *divisions) { + size_t proc_range = filesize/count; + + if (divisions == NULL) { + return NULL_ENTRY; + } + for (int i = 1; i < count; i++) { + divisions[i] = divisions[i-1] + proc_range; + } + divisions[count] = filesize; + return 0; +} + +int fork_calculations(int* processes, int process_count) { + if (processes == NULL) { + return NULL_ENTRY; + } + for (int i=0; i< process_count; i++) { + int current_id = 0; + if ((current_id=fork()) == -1) { + return PID_ERR; + } + if (current_id == 0) { + return i; + } + processes[i] = current_id; + } + return getpid(); +} + +int file_search(FILE** fp, const char* to_find, + size_t* found, size_t size_to_find) { + if (fp == NULL || *fp == NULL || to_find == NULL || found == NULL) { + return NULL_ENTRY; + } + struct stat file_stat; + fstat(fileno(*fp), &file_stat); + if ((file_stat.st_mode & S_IRUSR) == 0) { + return WRONG_OPEN; + } + if (file_stat.st_size == 0) { + return NULL_ENTRY; + } + char* file_in_memory = (char *)mmap(NULL, file_stat.st_size, + PROT_READ, MAP_SHARED, fileno(*fp), 0); + if (file_in_memory == NULL) { + return MMAP_ERR; + } + // Предполагается работа с большим файлом, поэтому делим сам файл, + // а не массив проверяемых символов. + // calculate processes + int process_count = calculate_process(file_stat.st_size); + + // fragment array for processes rule + size_t* divisions = (size_t *)calloc(process_count + 1, sizeof(size_t)); + create_division(process_count, file_stat.st_size, divisions); + + pthread_mutex_t mutex_write_lock; + pthread_mutex_init(&mutex_write_lock, NULL); + + // setup pipe comms + int* processes = (int *)calloc(process_count, sizeof(pid_t)); + int pipes[MAX_PROCESS][2]; + memset(pipes, 0, MAX_PROCESS*2); + for (int i = 0; i < process_count; i++) { + pipe(pipes[i]); + } + // initialise processes + int current_id = fork_calculations(processes, process_count); + + // this is child process block + if (getpid() != current_id) { + write(pipes[current_id][1], "0", 1); + for (int j = 0; j < size_to_find; j++) { + int count = 0; + for (size_t i = divisions[current_id]; + i < divisions[current_id+1]; i++) { + if (file_in_memory[i] == to_find[j]) + count++; + } + char str[BUFFER_SIZE] = "0"; + snprintf(str, READ_SIZE + 1, FORMAT_STRING, count); + write(pipes[current_id][1], str, strlen(str)); + } + char* end_sig = malloc(sizeof(char)); + read(pipes[current_id][0], end_sig, sizeof(char)); + close(pipes[current_id][0]); + close(pipes[current_id][1]); + free_all_resources(divisions, processes); + exit(0); + } + + // this is parent process block + for (int i = 0; i < process_count; i++) { + waitpid(processes[i], NULL, 0); + for (int j = 0; j < size_to_find; j++) { + char str[BUFFER_SIZE] = "0"; + read(pipes[i][0], str, READ_SIZE); + int count = (int)strtol(str, NULL, 10); + found[j]+= count; + } + char buf = 'e'; + write(pipes[i][1], &buf, sizeof(char)); + close(pipes[i][0]); + close(pipes[i][1]); + } + + // free memory + munmap(file_in_memory, file_stat.st_size); + free_all_resources(divisions, processes); + return 0; +} diff --git a/project/tests/linear.cpp b/project/tests/linear.cpp new file mode 100644 index 0000000..793cf5e --- /dev/null +++ b/project/tests/linear.cpp @@ -0,0 +1,66 @@ +// Copyright 2021 +#include "gtest/gtest.h" + +extern "C" { +#include "../include/search.h" +} + +TEST(file_search, null_params) { + FILE* f1 = fopen("1.txt", "a"); + char* c1 = (char*)calloc(10, sizeof (char)); + size_t* found = (size_t*)calloc(10, sizeof (size_t)); + size_t stf = 0; + ASSERT_EQ(file_search(nullptr, c1, found, stf), NULL_ENTRY); + ASSERT_EQ(file_search(&f1, nullptr, found, stf), NULL_ENTRY); + ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); +} + + +TEST(file_search, empty_file) { + FILE* f1 = fopen("1.txt", "a"); + char* c1 = (char*)calloc(10, sizeof (char)); + size_t* found = (size_t*)calloc(10, sizeof (size_t)); + size_t stf = 0; + ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); +} + +TEST(file_search, premade_file) { + FILE* f1 = fopen("1.txt", "a"); + fputs("a a a a\n b\n b\n", f1); + char c1[3] = {'a', 'b', '\n'}; + size_t found[3] = {0, 0, 0}; + size_t stf = 3; + fclose(f1); + f1 = fopen("1.txt", "r"); + file_search(&f1, c1, found, stf); + fclose(f1); + remove("1.txt"); + ASSERT_EQ(found[0], 4); + ASSERT_EQ(found[1], 2); + ASSERT_EQ(found[2], 3); +} + +TEST(file_search, grep_random) { + FILE* f1 = fopen("1.txt", "a"); + char c1[3] = {'a', 'b', '\n'}; + size_t found[3] = {0, 0, 0}; + size_t stf = 3; + fclose(f1); + system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | " + "fold -w 100 | head -n 10000 > 1.txt "); + f1 = fopen("1.txt", "r"); + file_search(&f1, c1, found, stf); + fclose(f1); + remove("grep_results.txt"); + system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); + system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); + system("grep -o '\n' 1.txt | wc -l >> grep_results.txt"); + remove("1.txt"); + f1 = fopen("grep_results.txt", "r"); + int i1, i2, i3; + fscanf(f1, "%d %d %d", &i1, &i2, &i3); + fclose(f1); + ASSERT_EQ(found[0], i1); + ASSERT_EQ(found[1], i2); + ASSERT_EQ(found[2], i3); +} diff --git a/project/tests/main.cpp b/project/tests/main.cpp new file mode 100644 index 0000000..7b0b7e1 --- /dev/null +++ b/project/tests/main.cpp @@ -0,0 +1,8 @@ +// Copyright 2021 + +#include "gtest/gtest.h" + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/project/tests/parallel.cpp b/project/tests/parallel.cpp new file mode 100644 index 0000000..ef4c832 --- /dev/null +++ b/project/tests/parallel.cpp @@ -0,0 +1,66 @@ +// Copyright 2021 +#include "gtest/gtest.h" + +extern "C" { +#include "../include/search.h" +} + +TEST(file_search_parallel, null_params) { + FILE* f1 = fopen("1.txt", "a"); + char* c1 = (char*)calloc(10, sizeof (char)); + size_t* found = (size_t*)calloc(10, sizeof (size_t)); + size_t stf = 0; + ASSERT_EQ(file_search(nullptr, c1, found, stf), NULL_ENTRY); + ASSERT_EQ(file_search(&f1, nullptr, found, stf), NULL_ENTRY); + ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); +} + + +TEST(file_search_parallel, empty_file) { + FILE* f1 = fopen("1.txt", "a"); + char* c1 = (char*)calloc(10, sizeof (char)); + size_t* found = (size_t*)calloc(10, sizeof (size_t)); + size_t stf = 0; + ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); +} + +TEST(file_search_parallel, premade_file) { + FILE* f1 = fopen("1.txt", "a"); + fputs("a a a a\n b\n b\n", f1); + char c1[3] = {'a', 'b', '\n'}; + size_t found[3] = {0, 0, 0}; + size_t stf = 3; + fclose(f1); + f1 = fopen("1.txt", "r"); + file_search(&f1, c1, found, stf); + fclose(f1); + remove("1.txt"); + ASSERT_EQ(found[0], 4); + ASSERT_EQ(found[1], 2); + ASSERT_EQ(found[2], 3); +} + +TEST(file_search_parallel, grep_random) { + FILE* f1 = fopen("1.txt", "a"); + char c1[3] = {'a', 'b', '\n'}; + size_t found[3] = {0, 0, 0}; + size_t stf = 3; + fclose(f1); + system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | " + "fold -w 100 | head -n 10000 > 1.txt "); + f1 = fopen("1.txt", "r"); + file_search(&f1, c1, found, stf); + fclose(f1); + remove("grep_results.txt"); + system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); + system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); + system("grep -o '\n' 1.txt | wc -l >> grep_results.txt"); + remove("1.txt"); + f1 = fopen("grep_results.txt", "r"); + int i1, i2, i3; + fscanf(f1, "%d %d %d", &i1, &i2, &i3); + fclose(f1); + ASSERT_EQ(found[0], i1); + ASSERT_EQ(found[1], i2); + ASSERT_EQ(found[2], i3); +} From 874dbc7bdaa6d9a3881b127054d1693eadd0b65a Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Tue, 30 Mar 2021 21:23:31 +0300 Subject: [PATCH 02/24] version fix --- README.md | 19 +++++++++++++++++++ project/CMakeLists.txt | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c246e5e..5ac58b9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # cpp_2021 +[![Build Status](https://travis-ci.com/BorisKoz/cpp_2021.svg?branch=HW-2)](https://travis-ci.com/BorisKoz/cpp_2021) + + + +# Вариант #4 +Перед вами поставлена задача подсчета количества вхождений заданных символов в загруженный в оперативную память файл размером 100 Мб. Составьте наивный алгоритм подсчета вхождений символов, в затем реализуйте параллельную обработку текста несколькими процессами с учетом оптимизации работы с кэш-памятью. + +# На что необходимо обратить внимание: +- основная информация описана в https://park.mail.ru/blog/topic/view/14270/ +- параллельная реализация не должна быть осуществлена с помощью процессов, когда требуется реализация с помощью потоков (и наоборот); +- компиляция должна происходить с флагами -Wall -Werror -Wpedantic, то есть необработанных ворнингов быть не должно; +- количество потоков/процессов должно быть не захардкожено, а определяться в зависимости от возможностей системы (например, в зависимости от количества ядер процессора); +- при разработке обеих библиотек стоит делать общий интерфейс, не раскрывая особенностей реализации; +- библиотеки должны быть взаимозаменяемыми - конкретная реализация (последовательная/параллельная) - использоваться в зависимости от конфигурации сборки; +- юнит-тесты должны быть реализованы для обеих реализаций (последовательной/параллельной). Покрытие тестами должно быть максимально возможным; +- должны присутствовать стресс-тесты. Они могут быть реализованы внешним образом, запуская две разные программы - одну со статической библиотекой с последовательной реализацией, вторую - с динамической библиотекой с параллельной реализацией, и сравнивая их выводы друг с другом. +- для организации ввода/вывода больших данных полезно работать с файлами, а в программе - предусмотреть работу с универсальными потоками входных/выходных данных (или хотя бы перенаправлять ввод/вывод на уровне их запуска) +- если в задании сказано, что программа должна обрабатывать файлы объёмом 100 Мб – это лишь ориентир, на которых программа точно должна работать, и на котором имеет смысл делать замеры производительности и эффективности алгоритмов. Поэтому тесты на такой объём должны быть. Однако сама программа должна уметь работать с произвольными размерами входных данных +- измерение времени должно осуществляться внешним образом, а не внутри кода библиотек. При этом необходимо делать несколько замеров и усреднять. Стоит помнить о том, что clock() в многопоточных приложениях работает не так, как ожидается. diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index c49978b..678bcfe 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.17) +cmake_minimum_required(VERSION 3.15) project(files) configure_file(CMakeLists.txt.in From d6af849bb35d1a3b1de4e52310448eb8420b78e3 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Tue, 30 Mar 2021 21:28:11 +0300 Subject: [PATCH 03/24] fixed wait --- project/src/parallel.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/src/parallel.c b/project/src/parallel.c index 8d9ad26..0758fbd 100644 --- a/project/src/parallel.c +++ b/project/src/parallel.c @@ -1,5 +1,6 @@ // Copyright 2021 bkz #include +#include #include #include "../include/search.h" #include From abc27ed4ceae9b688cbb7f00be823f110af77af8 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Tue, 30 Mar 2021 21:38:40 +0300 Subject: [PATCH 04/24] valgrind + test fix --- project/src/main.c | 10 +++++----- project/tests/linear.cpp | 16 +++++++++------- project/tests/parallel.cpp | 10 ++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/project/src/main.c b/project/src/main.c index a8fadc7..2a1a547 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -8,8 +8,8 @@ #include "../include/search.h" int main() { - // LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | - // fold -w 100 | head -n 100000 > bigfile.txt + system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |" + " fold -w 100 | head -n 100000 > bigfile.txt"); FILE* p = fopen("bigfile.txt", "r"); char* array = (char*) calloc(3, sizeof(char)); size_t* found = (size_t*) calloc(3, sizeof(size_t)); @@ -32,9 +32,9 @@ int main() { printf("%zu ", found[i]); } printf("\n%lf", elapsed); - fclose(p); - - + if (p) { + fclose(p); + } free(array); free(found); diff --git a/project/tests/linear.cpp b/project/tests/linear.cpp index 793cf5e..6999361 100644 --- a/project/tests/linear.cpp +++ b/project/tests/linear.cpp @@ -13,6 +13,8 @@ TEST(file_search, null_params) { ASSERT_EQ(file_search(nullptr, c1, found, stf), NULL_ENTRY); ASSERT_EQ(file_search(&f1, nullptr, found, stf), NULL_ENTRY); ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); + free(c1); + free(found); } @@ -22,6 +24,8 @@ TEST(file_search, empty_file) { size_t* found = (size_t*)calloc(10, sizeof (size_t)); size_t stf = 0; ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); + free(c1); + free(found); } TEST(file_search, premade_file) { @@ -42,9 +46,9 @@ TEST(file_search, premade_file) { TEST(file_search, grep_random) { FILE* f1 = fopen("1.txt", "a"); - char c1[3] = {'a', 'b', '\n'}; - size_t found[3] = {0, 0, 0}; - size_t stf = 3; + char c1[2] = {'a', 'b'}; + size_t found[2] = {0, 0}; + size_t stf = 2; fclose(f1); system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | " "fold -w 100 | head -n 10000 > 1.txt "); @@ -54,13 +58,11 @@ TEST(file_search, grep_random) { remove("grep_results.txt"); system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); - system("grep -o '\n' 1.txt | wc -l >> grep_results.txt"); remove("1.txt"); f1 = fopen("grep_results.txt", "r"); - int i1, i2, i3; - fscanf(f1, "%d %d %d", &i1, &i2, &i3); + int i1, i2; + fscanf(f1, "%d %d", &i1, &i2); fclose(f1); ASSERT_EQ(found[0], i1); ASSERT_EQ(found[1], i2); - ASSERT_EQ(found[2], i3); } diff --git a/project/tests/parallel.cpp b/project/tests/parallel.cpp index ef4c832..030c7fe 100644 --- a/project/tests/parallel.cpp +++ b/project/tests/parallel.cpp @@ -13,6 +13,8 @@ TEST(file_search_parallel, null_params) { ASSERT_EQ(file_search(nullptr, c1, found, stf), NULL_ENTRY); ASSERT_EQ(file_search(&f1, nullptr, found, stf), NULL_ENTRY); ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); + free(c1); + free(found); } @@ -22,6 +24,8 @@ TEST(file_search_parallel, empty_file) { size_t* found = (size_t*)calloc(10, sizeof (size_t)); size_t stf = 0; ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); + free(c1); + free(found); } TEST(file_search_parallel, premade_file) { @@ -54,13 +58,11 @@ TEST(file_search_parallel, grep_random) { remove("grep_results.txt"); system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); - system("grep -o '\n' 1.txt | wc -l >> grep_results.txt"); remove("1.txt"); f1 = fopen("grep_results.txt", "r"); - int i1, i2, i3; - fscanf(f1, "%d %d %d", &i1, &i2, &i3); + int i1, i2; + fscanf(f1, "%d %d", &i1, &i2); fclose(f1); ASSERT_EQ(found[0], i1); ASSERT_EQ(found[1], i2); - ASSERT_EQ(found[2], i3); } From 6eaad0e95fa9b4c7f1c8a97fae6152e93657474a Mon Sep 17 00:00:00 2001 From: BorisKoz <55689768+BorisKoz@users.noreply.github.com> Date: Tue, 30 Mar 2021 21:44:26 +0300 Subject: [PATCH 05/24] codecov badge //TODO: normal input --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5ac58b9..a9c0a9c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status](https://travis-ci.com/BorisKoz/cpp_2021.svg?branch=HW-2)](https://travis-ci.com/BorisKoz/cpp_2021) - +[![codecov](https://codecov.io/gh/BorisKoz/cpp_2021/branch/HW-2/graph/badge.svg)](https://codecov.io/gh/BorisKoz/cpp_2021) # Вариант #4 From 6030e3c1dfbca0c8ba8482ef8500ce15a64cd338 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 16:49:48 +0300 Subject: [PATCH 06/24] made interface and yml --- .travis.yml | 5 ++-- project/include/search.h | 2 +- project/src/main.c | 64 +++++++++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/.travis.yml b/.travis.yml index 74ea9e8..334620c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,8 +21,9 @@ script: - make clean && make - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - - valgrind --leak-check=full --track-origins=yes ./main_linear.out - - valgrind --leak-check=full --track-origins=yes ./main_parallel.out + - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes ./main_linear.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes ./main_parallel.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests diff --git a/project/include/search.h b/project/include/search.h index 21aee88..09422e0 100644 --- a/project/include/search.h +++ b/project/include/search.h @@ -9,7 +9,7 @@ #define WRONG_OPEN 4 #define MAX_PROCESS 6 #define READ_SIZE 20 -#define BUFFER_SIZE 25 +#define BUFFER_SIZE 30 #define FORMAT_STRING "%020d" int file_search(FILE** , const char* , size_t* , size_t size_to_find); diff --git a/project/src/main.c b/project/src/main.c index 2a1a547..d602ab6 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -1,42 +1,60 @@ // Copyright 2021 bkz #include - +#include #include #include #include #include "../include/search.h" +#define TEST_SERIES_SIZE 3 +#define RESULTS_FILE "res.txt" -int main() { - system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |" - " fold -w 100 | head -n 100000 > bigfile.txt"); - FILE* p = fopen("bigfile.txt", "r"); - char* array = (char*) calloc(3, sizeof(char)); - size_t* found = (size_t*) calloc(3, sizeof(size_t)); - array[0] = 'a'; - array[1] = 'b'; - array[2] = '\n'; - struct timespec start, finish; - double elapsed; +int main(int argc, char* argv[]) { + if (argc != 2) { + fprintf(stderr, "No file input"); + return -1; + } + // открытие файла + FILE* p = fopen(argv[1], "r"); + if (!p) { + fprintf(stderr, "No such file"); + return -1; + } - clock_gettime(CLOCK_MONOTONIC, &start); + // считывание символов + int size_to_find = 0; + char to_find [BUFFER_SIZE] = ""; + size_t found[BUFFER_SIZE] = {0}; + memset(found, 0, BUFFER_SIZE); + scanf("%d", &size_to_find); + if (size_to_find > 30) { + fprintf(stderr, "Too much symbols"); + return -1; + } + scanf("%c", &to_find[0]); + for (int i = 0; i < size_to_find; i++) { + scanf("%c", &to_find[i]); + } - file_search(&p, array, found, 3); + //работа с файлом + double elapsed[TEST_SERIES_SIZE], average =0; + for (int i = 0; i < TEST_SERIES_SIZE; i++) { + struct timespec start, finish; - clock_gettime(CLOCK_MONOTONIC, &finish); + clock_gettime(CLOCK_MONOTONIC, &start); - elapsed = (double)(finish.tv_sec - start.tv_sec); - elapsed += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; + file_search(&p, to_find, found, size_to_find); - for (int i=0; i < 3; i++) { - printf("%zu ", found[i]); + clock_gettime(CLOCK_MONOTONIC, &finish); + elapsed[i] = (double)(finish.tv_sec - start.tv_sec); + elapsed[i] += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("elapsed: %lf\n", elapsed[i]); + average += elapsed[i]; } - printf("\n%lf", elapsed); + average = average / TEST_SERIES_SIZE; + printf("Series average: %lf\n", average); if (p) { fclose(p); } - free(array); - free(found); - return 0; } From 931e445d6d92f9f178090727cc0c3ae0b47e9f9e Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 16:56:07 +0300 Subject: [PATCH 07/24] fixed memset --- project/src/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/project/src/main.c b/project/src/main.c index d602ab6..1eb80ed 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -25,10 +25,11 @@ int main(int argc, char* argv[]) { int size_to_find = 0; char to_find [BUFFER_SIZE] = ""; size_t found[BUFFER_SIZE] = {0}; - memset(found, 0, BUFFER_SIZE); + memset(found, 0, BUFFER_SIZE * sizeof(*found)); scanf("%d", &size_to_find); if (size_to_find > 30) { fprintf(stderr, "Too much symbols"); + fclose(p); return -1; } scanf("%c", &to_find[0]); @@ -38,6 +39,7 @@ int main(int argc, char* argv[]) { //работа с файлом double elapsed[TEST_SERIES_SIZE], average =0; + memset(found, 0, TEST_SERIES_SIZE * sizeof(*elapsed)); for (int i = 0; i < TEST_SERIES_SIZE; i++) { struct timespec start, finish; From 52a32bf8470c8af94488d3060189ae86ec536620 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:05:40 +0300 Subject: [PATCH 08/24] fixed cpplint, now looking for leaks --- project/src/main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project/src/main.c b/project/src/main.c index 1eb80ed..e8323f3 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -23,7 +23,7 @@ int main(int argc, char* argv[]) { // считывание символов int size_to_find = 0; - char to_find [BUFFER_SIZE] = ""; + char to_find[BUFFER_SIZE] = ""; size_t found[BUFFER_SIZE] = {0}; memset(found, 0, BUFFER_SIZE * sizeof(*found)); scanf("%d", &size_to_find); @@ -37,8 +37,8 @@ int main(int argc, char* argv[]) { scanf("%c", &to_find[i]); } - //работа с файлом - double elapsed[TEST_SERIES_SIZE], average =0; + // работа с файлом + double elapsed[TEST_SERIES_SIZE], average = 0; memset(found, 0, TEST_SERIES_SIZE * sizeof(*elapsed)); for (int i = 0; i < TEST_SERIES_SIZE; i++) { struct timespec start, finish; From 44c013c3a4b57367390d151d2cd26c34857d9bd7 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:05:50 +0300 Subject: [PATCH 09/24] fixed cpplint, now looking for leaks --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 334620c..a8467a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,8 +22,8 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes ./main_linear.out bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes ./main_parallel.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main_linear.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main_parallel.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests From 5e7430051595ea90531472cc08862add76a9875f Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:10:55 +0300 Subject: [PATCH 10/24] fixing leaks of child procs --- project/src/parallel.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project/src/parallel.c b/project/src/parallel.c index 0758fbd..cb3c5ab 100644 --- a/project/src/parallel.c +++ b/project/src/parallel.c @@ -124,6 +124,8 @@ int file_search(FILE** fp, const char* to_find, close(pipes[current_id][0]); close(pipes[current_id][1]); free_all_resources(divisions, processes); + free(end_sig); + fclose(*fp); exit(0); } From fa395f936a4098005db1c97f876a7ea49a7ba90f Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:16:22 +0300 Subject: [PATCH 11/24] no VG time added --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index a8467a5..4f338e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,6 +26,10 @@ script: - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main_parallel.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests + - echo "these are clean times, no valgrind" + - echo "2 ab" | ./main_linear.out bigfile.txt + - echo "2 ab" | ./main_parallel.out bigfile.txt + From 04d6063f7af3153471d470b495ab3c9c448765d5 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:17:41 +0300 Subject: [PATCH 12/24] added comments --- project/src/parallel.c | 1 + 1 file changed, 1 insertion(+) diff --git a/project/src/parallel.c b/project/src/parallel.c index cb3c5ab..d9eedf0 100644 --- a/project/src/parallel.c +++ b/project/src/parallel.c @@ -67,6 +67,7 @@ int fork_calculations(int* processes, int process_count) { int file_search(FILE** fp, const char* to_find, size_t* found, size_t size_to_find) { + // correct checks if (fp == NULL || *fp == NULL || to_find == NULL || found == NULL) { return NULL_ENTRY; } From dc0b2a137db519eccc17f54437bd74b466543e26 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:28:24 +0300 Subject: [PATCH 13/24] added test 100Mb, was 10 --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4f338e5..b591269 100644 --- a/.travis.yml +++ b/.travis.yml @@ -26,7 +26,8 @@ script: - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main_parallel.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests - - echo "these are clean times, no valgrind" + - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt + - echo "these are clean times, no valgrind, on 100MB" - echo "2 ab" | ./main_linear.out bigfile.txt - echo "2 ab" | ./main_parallel.out bigfile.txt From 4d61da2030f9410fea456f437e3a0aef9d29ef8c Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Wed, 31 Mar 2021 17:33:30 +0300 Subject: [PATCH 14/24] stupid fix --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b591269..4bf6b10 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,8 +28,8 @@ script: - valgrind --leak-check=full --track-origins=yes ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - - echo "2 ab" | ./main_linear.out bigfile.txt - - echo "2 ab" | ./main_parallel.out bigfile.txt + - echo "2 ab" | ./main_linear.out bigfile2.txt + - echo "2 ab" | ./main_parallel.out bigfile2.txt From 3c828dbbb797ce2a6d83f4e81da8cd63b7fb840e Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:02:53 +0300 Subject: [PATCH 15/24] fixed almost all. parallel not by linear --- .travis.yml | 6 ++--- project/.DS_Store | Bin 6148 -> 8196 bytes project/CMakeLists.txt | 8 ++---- project/include/search.h | 2 +- project/src/main.c | 50 +++++++++++++++++++++++++++++++--- project/src/parallel.c | 54 +++++++++++++++++++------------------ project/tests/linear.cpp | 10 +++---- project/tests/parallel.cpp | 14 ++++++---- 8 files changed, 94 insertions(+), 50 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4bf6b10..3bd8193 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,14 +22,12 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main_linear.out bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main_parallel.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - - echo "2 ab" | ./main_linear.out bigfile2.txt - - echo "2 ab" | ./main_parallel.out bigfile2.txt + - echo "2 ab" | ./main.out bigfile2.txt diff --git a/project/.DS_Store b/project/.DS_Store index eaa8cb0e96b751d3369893e2dfbdd0c51b83818f..a032984ed1ff434e9c1613b7c09dc2d9e04162d1 100644 GIT binary patch delta 183 zcmZoMXmOBWU|?W$DortDU;r^WfEYvza8E20o2aMA$gweCH}hr%jz7$c**Q2SHn1>q zEaqWhR2F1NX2@koWXNVnWzc0vVkl+EWXNGi0kTtoA|QVHWFEE~#Hic+mxYmOvo_CT lW-bYCpxv$@>o*H>d}p4_FXB1bpNE5k5#ln2&G9^Qm;tTQBfS6s delta 108 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jG)aU^g=(+hQI8#?6*OpBOim ua564t=im@z2C4!A0d64S3evE#@H_Klei=`Yb_OPhQ6SS9HplbKVFm!q5D^Ce diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index 678bcfe..c8efb79 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -20,8 +20,6 @@ enable_testing() include_directories("${PROJECT_SOURCE_DIR}/include") - - set(INCLUDE ${PROJECT_SOURCE_DIR}/include) set(SOURCE ${PROJECT_SOURCE_DIR}/src) @@ -36,10 +34,8 @@ file(GLOB prod_sources "${PROJECT_SOURCE_DIR}/include/*.h" "${PROJECT_SOURCE_DIR}/src/main.c") -add_executable(main_linear.out ${PROJECT_SOURCE_DIR}/src/main.c) -add_executable(main_parallel.out ${PROJECT_SOURCE_DIR}/src/main.c) -target_link_libraries(main_linear.out bigfile_linear) -target_link_libraries(main_parallel.out bigfile_parallel) +add_executable(main.out ${PROJECT_SOURCE_DIR}/src/main.c) +target_link_libraries(main.out -ldl bigfile_linear) file(GLOB tests "${PROJECT_SOURCE_DIR}/tests/*.cpp") diff --git a/project/include/search.h b/project/include/search.h index 09422e0..4beb2f4 100644 --- a/project/include/search.h +++ b/project/include/search.h @@ -7,7 +7,7 @@ #define MMAP_ERR 2 #define PID_ERR 3 #define WRONG_OPEN 4 -#define MAX_PROCESS 6 +#define MAX_PROCESS 10 #define READ_SIZE 20 #define BUFFER_SIZE 30 #define FORMAT_STRING "%020d" diff --git a/project/src/main.c b/project/src/main.c index e8323f3..75d9d44 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -6,6 +6,7 @@ #include #include #include "../include/search.h" +#include #define TEST_SERIES_SIZE 3 #define RESULTS_FILE "res.txt" @@ -24,7 +25,7 @@ int main(int argc, char* argv[]) { // считывание символов int size_to_find = 0; char to_find[BUFFER_SIZE] = ""; - size_t found[BUFFER_SIZE] = {0}; + size_t found[BUFFER_SIZE]; memset(found, 0, BUFFER_SIZE * sizeof(*found)); scanf("%d", &size_to_find); if (size_to_find > 30) { @@ -37,7 +38,7 @@ int main(int argc, char* argv[]) { scanf("%c", &to_find[i]); } - // работа с файлом + // работа с файлом последовательно double elapsed[TEST_SERIES_SIZE], average = 0; memset(found, 0, TEST_SERIES_SIZE * sizeof(*elapsed)); for (int i = 0; i < TEST_SERIES_SIZE; i++) { @@ -52,9 +53,52 @@ int main(int argc, char* argv[]) { elapsed[i] += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; printf("elapsed: %lf\n", elapsed[i]); average += elapsed[i]; + memset(found, 0, BUFFER_SIZE * sizeof(*found)); } average = average / TEST_SERIES_SIZE; - printf("Series average: %lf\n", average); + printf("Linear series average: %lf\n", average); + if (p) { + fclose(p); + } + + // parallel run + p = fopen(argv[1], "r"); + void *parallel_lib = dlopen("./libbigfile_parallel.dylib", RTLD_LAZY); + if (!parallel_lib) { + fprintf(stderr, "LIBRARY NOT FOUND"); + return -1; + } + + int (*pointer)(); + *(void **)(&pointer) = dlsym(parallel_lib, "file_search"); + + double average_parallel = 0; + memset(found, 0, TEST_SERIES_SIZE * sizeof(*elapsed)); + for (int i = 0; i < TEST_SERIES_SIZE; i++) { + struct timespec start, finish; + + clock_gettime(CLOCK_MONOTONIC, &start); + + (*pointer)(&p, to_find, found, size_to_find); + + clock_gettime(CLOCK_MONOTONIC, &finish); + elapsed[i] = (double)(finish.tv_sec - start.tv_sec); + elapsed[i] += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("elapsed: %lf\n", elapsed[i]); + average_parallel += elapsed[i]; + for (int j = 0; j < 2; j++) { + printf("found %d : %zu \n", j , found[j]); + } + memset(found, 0, BUFFER_SIZE * sizeof(*found)); + } + average_parallel = average_parallel / TEST_SERIES_SIZE; + printf("Parallel series average: %lf\n", average_parallel); + dlclose(parallel_lib); + + (average > average_parallel ? + printf("parallel is faster : %lf vs %lf", average_parallel, average) : + printf("linear is faster : %lf vs %lf", average, average_parallel)); + if (p) { fclose(p); } diff --git a/project/src/parallel.c b/project/src/parallel.c index d9eedf0..368c442 100644 --- a/project/src/parallel.c +++ b/project/src/parallel.c @@ -6,9 +6,9 @@ #include #include #include -#include #include #include +#define unlikely(expr) __builtin_expect(!!(expr), 0) void free_all_resources(size_t* divisions, int* processes ) { if (divisions) { @@ -32,10 +32,12 @@ int calculate_process(size_t filesize) { if (filesize < 1000000) { return 4; } - return MAX_PROCESS; + return (MAX_PROCESS > (int)sysconf(_SC_NPROCESSORS_ONLN) ? + (int)sysconf(_SC_NPROCESSORS_ONLN) : MAX_PROCESS); } -int create_division(int count, size_t filesize, size_t *divisions) { +int create_division( + int count, size_t filesize, size_t *divisions) { size_t proc_range = filesize/count; if (divisions == NULL) { @@ -65,6 +67,25 @@ int fork_calculations(int* processes, int process_count) { return getpid(); } +int child_process_run(const char* file_in_memory, const size_t* divisions, + const int current_id, const int size_to_find, + const char* to_find, int pipes[MAX_PROCESS][2]) { + for (int j = 0; j < size_to_find; j++) { + int count = 0; + for (size_t i = divisions[current_id]; + i < divisions[current_id+1]; i++) { + if (unlikely(file_in_memory[i] == to_find[j])) + count++; + } + char str[BUFFER_SIZE] = "0"; + snprintf(str, READ_SIZE + 1, FORMAT_STRING, count); + write(pipes[current_id][1], str, strlen(str)); + } + close(pipes[current_id][0]); + close(pipes[current_id][1]); + return 0; +} + int file_search(FILE** fp, const char* to_find, size_t* found, size_t size_to_find) { // correct checks @@ -93,9 +114,6 @@ int file_search(FILE** fp, const char* to_find, size_t* divisions = (size_t *)calloc(process_count + 1, sizeof(size_t)); create_division(process_count, file_stat.st_size, divisions); - pthread_mutex_t mutex_write_lock; - pthread_mutex_init(&mutex_write_lock, NULL); - // setup pipe comms int* processes = (int *)calloc(process_count, sizeof(pid_t)); int pipes[MAX_PROCESS][2]; @@ -108,39 +126,23 @@ int file_search(FILE** fp, const char* to_find, // this is child process block if (getpid() != current_id) { - write(pipes[current_id][1], "0", 1); - for (int j = 0; j < size_to_find; j++) { - int count = 0; - for (size_t i = divisions[current_id]; - i < divisions[current_id+1]; i++) { - if (file_in_memory[i] == to_find[j]) - count++; - } - char str[BUFFER_SIZE] = "0"; - snprintf(str, READ_SIZE + 1, FORMAT_STRING, count); - write(pipes[current_id][1], str, strlen(str)); - } - char* end_sig = malloc(sizeof(char)); - read(pipes[current_id][0], end_sig, sizeof(char)); - close(pipes[current_id][0]); - close(pipes[current_id][1]); + child_process_run(file_in_memory, divisions, current_id, + size_to_find, to_find, pipes); free_all_resources(divisions, processes); - free(end_sig); fclose(*fp); exit(0); } // this is parent process block for (int i = 0; i < process_count; i++) { - waitpid(processes[i], NULL, 0); + while (!waitpid(processes[i], NULL, 0)) { + } for (int j = 0; j < size_to_find; j++) { char str[BUFFER_SIZE] = "0"; read(pipes[i][0], str, READ_SIZE); int count = (int)strtol(str, NULL, 10); found[j]+= count; } - char buf = 'e'; - write(pipes[i][1], &buf, sizeof(char)); close(pipes[i][0]); close(pipes[i][1]); } diff --git a/project/tests/linear.cpp b/project/tests/linear.cpp index 6999361..5a3ed26 100644 --- a/project/tests/linear.cpp +++ b/project/tests/linear.cpp @@ -39,9 +39,9 @@ TEST(file_search, premade_file) { file_search(&f1, c1, found, stf); fclose(f1); remove("1.txt"); - ASSERT_EQ(found[0], 4); - ASSERT_EQ(found[1], 2); - ASSERT_EQ(found[2], 3); + EXPECT_EQ(found[0], 4); + EXPECT_EQ(found[1], 2); + EXPECT_EQ(found[2], 3); } TEST(file_search, grep_random) { @@ -63,6 +63,6 @@ TEST(file_search, grep_random) { int i1, i2; fscanf(f1, "%d %d", &i1, &i2); fclose(f1); - ASSERT_EQ(found[0], i1); - ASSERT_EQ(found[1], i2); + EXPECT_EQ(found[0], i1); + EXPECT_EQ(found[1], i2); } diff --git a/project/tests/parallel.cpp b/project/tests/parallel.cpp index 030c7fe..659c1b4 100644 --- a/project/tests/parallel.cpp +++ b/project/tests/parallel.cpp @@ -6,6 +6,7 @@ extern "C" { } TEST(file_search_parallel, null_params) { + remove("1.txt"); FILE* f1 = fopen("1.txt", "a"); char* c1 = (char*)calloc(10, sizeof (char)); size_t* found = (size_t*)calloc(10, sizeof (size_t)); @@ -19,6 +20,7 @@ TEST(file_search_parallel, null_params) { TEST(file_search_parallel, empty_file) { + remove("1.txt"); FILE* f1 = fopen("1.txt", "a"); char* c1 = (char*)calloc(10, sizeof (char)); size_t* found = (size_t*)calloc(10, sizeof (size_t)); @@ -29,6 +31,7 @@ TEST(file_search_parallel, empty_file) { } TEST(file_search_parallel, premade_file) { + remove("1.txt"); FILE* f1 = fopen("1.txt", "a"); fputs("a a a a\n b\n b\n", f1); char c1[3] = {'a', 'b', '\n'}; @@ -39,12 +42,13 @@ TEST(file_search_parallel, premade_file) { file_search(&f1, c1, found, stf); fclose(f1); remove("1.txt"); - ASSERT_EQ(found[0], 4); - ASSERT_EQ(found[1], 2); - ASSERT_EQ(found[2], 3); + EXPECT_EQ(found[0], 4); + EXPECT_EQ(found[1], 2); + EXPECT_EQ(found[2], 3); } TEST(file_search_parallel, grep_random) { + remove("1.txt"); FILE* f1 = fopen("1.txt", "a"); char c1[3] = {'a', 'b', '\n'}; size_t found[3] = {0, 0, 0}; @@ -63,6 +67,6 @@ TEST(file_search_parallel, grep_random) { int i1, i2; fscanf(f1, "%d %d", &i1, &i2); fclose(f1); - ASSERT_EQ(found[0], i1); - ASSERT_EQ(found[1], i2); + EXPECT_EQ(found[0], i1); + EXPECT_EQ(found[1], i2); } From 4e9123e757c29da8101b6ae9422498e90805e95e Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:13:39 +0300 Subject: [PATCH 16/24] fixing non-link --- .travis.yml | 6 +++--- README.md | 1 + project/src/main.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3bd8193..992d3dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,9 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main.out bigfile.txt - - valgrind --leak-check=full --track-origins=yes ./linear_tests - - valgrind --leak-check=full --track-origins=yes ./parallel_tests + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all -s ./main.out bigfile.txt + - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./linear_tests + - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - echo "2 ab" | ./main.out bigfile2.txt diff --git a/README.md b/README.md index a9c0a9c..407e40c 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![codecov](https://codecov.io/gh/BorisKoz/cpp_2021/branch/HW-2/graph/badge.svg)](https://codecov.io/gh/BorisKoz/cpp_2021) +On my machine : parallel is faster : 0.152859 vs 1.536328s # Вариант #4 Перед вами поставлена задача подсчета количества вхождений заданных символов в загруженный в оперативную память файл размером 100 Мб. Составьте наивный алгоритм подсчета вхождений символов, в затем реализуйте параллельную обработку текста несколькими процессами с учетом оптимизации работы с кэш-памятью. diff --git a/project/src/main.c b/project/src/main.c index 75d9d44..229ebd8 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { // parallel run p = fopen(argv[1], "r"); - void *parallel_lib = dlopen("./libbigfile_parallel.dylib", RTLD_LAZY); + void *parallel_lib = dlopen("libbigfile_parallel.dylib", RTLD_LAZY); if (!parallel_lib) { fprintf(stderr, "LIBRARY NOT FOUND"); return -1; From eb2326d4c7d2c7b1795dacf28868528413040a93 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:25:28 +0300 Subject: [PATCH 17/24] fixing non-link 2 --- .travis.yml | 1 + project/tests/linear.cpp | 2 ++ project/tests/parallel.cpp | 25 +++++++++++++------------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 992d3dd..fc0c4c9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,7 @@ script: - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" + - ls -a - echo "2 ab" | ./main.out bigfile2.txt diff --git a/project/tests/linear.cpp b/project/tests/linear.cpp index 5a3ed26..eaee26d 100644 --- a/project/tests/linear.cpp +++ b/project/tests/linear.cpp @@ -15,6 +15,7 @@ TEST(file_search, null_params) { ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); free(c1); free(found); + fclose(f1); } @@ -26,6 +27,7 @@ TEST(file_search, empty_file) { ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); free(c1); free(found); + fclose(f1); } TEST(file_search, premade_file) { diff --git a/project/tests/parallel.cpp b/project/tests/parallel.cpp index 659c1b4..6a15476 100644 --- a/project/tests/parallel.cpp +++ b/project/tests/parallel.cpp @@ -6,7 +6,6 @@ extern "C" { } TEST(file_search_parallel, null_params) { - remove("1.txt"); FILE* f1 = fopen("1.txt", "a"); char* c1 = (char*)calloc(10, sizeof (char)); size_t* found = (size_t*)calloc(10, sizeof (size_t)); @@ -16,57 +15,59 @@ TEST(file_search_parallel, null_params) { ASSERT_EQ(file_search(&f1, c1, nullptr, stf), NULL_ENTRY); free(c1); free(found); + fclose(f1); + remove("1.txt"); } TEST(file_search_parallel, empty_file) { - remove("1.txt"); - FILE* f1 = fopen("1.txt", "a"); + FILE* f1 = fopen("2.txt", "a"); char* c1 = (char*)calloc(10, sizeof (char)); size_t* found = (size_t*)calloc(10, sizeof (size_t)); size_t stf = 0; ASSERT_EQ(file_search(&f1, c1, found, stf), NULL_ENTRY); free(c1); free(found); + fclose(f1); + remove("2.txt"); } TEST(file_search_parallel, premade_file) { - remove("1.txt"); - FILE* f1 = fopen("1.txt", "a"); + FILE* f1 = fopen("3.txt", "a"); fputs("a a a a\n b\n b\n", f1); char c1[3] = {'a', 'b', '\n'}; size_t found[3] = {0, 0, 0}; size_t stf = 3; fclose(f1); - f1 = fopen("1.txt", "r"); + f1 = fopen("3.txt", "r"); file_search(&f1, c1, found, stf); fclose(f1); - remove("1.txt"); EXPECT_EQ(found[0], 4); EXPECT_EQ(found[1], 2); EXPECT_EQ(found[2], 3); + remove("3.txt"); } TEST(file_search_parallel, grep_random) { - remove("1.txt"); - FILE* f1 = fopen("1.txt", "a"); + FILE* f1 = fopen("4.txt", "a"); char c1[3] = {'a', 'b', '\n'}; size_t found[3] = {0, 0, 0}; size_t stf = 3; fclose(f1); system("LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom | " - "fold -w 100 | head -n 10000 > 1.txt "); - f1 = fopen("1.txt", "r"); + "fold -w 100 | head -n 10000 > 4.txt "); + f1 = fopen("4.txt", "r"); file_search(&f1, c1, found, stf); fclose(f1); remove("grep_results.txt"); system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); - remove("1.txt"); + remove("4.txt"); f1 = fopen("grep_results.txt", "r"); int i1, i2; fscanf(f1, "%d %d", &i1, &i2); fclose(f1); EXPECT_EQ(found[0], i1); EXPECT_EQ(found[1], i2); + remove("grep_results.txt"); } From 316f36875734f5b6ea2526096246f6b7c69b11be Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:37:04 +0300 Subject: [PATCH 18/24] fixing nonlink 3 --- project/CMakeLists.txt | 3 ++- project/src/main.c | 5 +---- project/tests/parallel.cpp | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index c8efb79..756ba58 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -26,7 +26,8 @@ set(SOURCE ${PROJECT_SOURCE_DIR}/src) add_library(bigfile_linear STATIC ${INCLUDE}/search.h ${SOURCE}/linear.c) -add_library(bigfile_parallel SHARED +# changed to MODULE, otherwise is built different local-remote +add_library(bigfile_parallel MODULE ${INCLUDE}/search.h ${SOURCE}/parallel.c) diff --git a/project/src/main.c b/project/src/main.c index 229ebd8..288303a 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { // parallel run p = fopen(argv[1], "r"); - void *parallel_lib = dlopen("libbigfile_parallel.dylib", RTLD_LAZY); + void *parallel_lib = dlopen("libbigfile_parallel.so", RTLD_LAZY); if (!parallel_lib) { fprintf(stderr, "LIBRARY NOT FOUND"); return -1; @@ -86,9 +86,6 @@ int main(int argc, char* argv[]) { elapsed[i] += (double)(finish.tv_nsec - start.tv_nsec) / 1000000000.0; printf("elapsed: %lf\n", elapsed[i]); average_parallel += elapsed[i]; - for (int j = 0; j < 2; j++) { - printf("found %d : %zu \n", j , found[j]); - } memset(found, 0, BUFFER_SIZE * sizeof(*found)); } average_parallel = average_parallel / TEST_SERIES_SIZE; diff --git a/project/tests/parallel.cpp b/project/tests/parallel.cpp index 6a15476..45dd84a 100644 --- a/project/tests/parallel.cpp +++ b/project/tests/parallel.cpp @@ -60,8 +60,8 @@ TEST(file_search_parallel, grep_random) { file_search(&f1, c1, found, stf); fclose(f1); remove("grep_results.txt"); - system("grep -o 'a' 1.txt | wc -l >> grep_results.txt"); - system("grep -o 'b' 1.txt | wc -l >> grep_results.txt"); + system("grep -o 'a' 4.txt | wc -l >> grep_results.txt"); + system("grep -o 'b' 4.txt | wc -l >> grep_results.txt"); remove("4.txt"); f1 = fopen("grep_results.txt", "r"); int i1, i2; From 03728d61511385d5b094a0a124d3ca9ac9e57ec9 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:42:13 +0300 Subject: [PATCH 19/24] fixing nonlink 4 --- project/src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/src/main.c b/project/src/main.c index 288303a..64f4e5d 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) { // parallel run p = fopen(argv[1], "r"); - void *parallel_lib = dlopen("libbigfile_parallel.so", RTLD_LAZY); + void *parallel_lib = dlopen("./libbigfile_parallel.so", RTLD_LAZY); if (!parallel_lib) { fprintf(stderr, "LIBRARY NOT FOUND"); return -1; From 41449f80383226d82a765a9e33955cc18fffd709 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:48:53 +0300 Subject: [PATCH 20/24] deleted some remote debug --- .travis.yml | 1 - project/src/main.c | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index fc0c4c9..992d3dd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,6 @@ script: - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - - ls -a - echo "2 ab" | ./main.out bigfile2.txt diff --git a/project/src/main.c b/project/src/main.c index 64f4e5d..71dcd37 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -90,12 +90,11 @@ int main(int argc, char* argv[]) { } average_parallel = average_parallel / TEST_SERIES_SIZE; printf("Parallel series average: %lf\n", average_parallel); + average > average_parallel ? + printf("parallel is faster : %lf vs %lf", average_parallel, average) : + printf("linear is faster : %lf vs %lf", average, average_parallel); dlclose(parallel_lib); - (average > average_parallel ? - printf("parallel is faster : %lf vs %lf", average_parallel, average) : - printf("linear is faster : %lf vs %lf", average, average_parallel)); - if (p) { fclose(p); } From 9ae75bfa066765d64ad7c59a45c0162cb5e62ca7 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:49:33 +0300 Subject: [PATCH 21/24] deleted some remote debug --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 992d3dd..55b24f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,9 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all -s ./main.out bigfile.txt - - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./linear_tests - - valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./parallel_tests + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main.out bigfile.txt + - valgrind --leak-check=full --track-origins=yes ./linear_tests + - valgrind --leak-check=full --track-origins=yes - ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - echo "2 ab" | ./main.out bigfile2.txt From 062968bdab7bbb3e31a4b069e38a551698a37b79 Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:55:50 +0300 Subject: [PATCH 22/24] reachable leak + somewhy no print --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 55b24f4..e917957 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,9 +22,9 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - - valgrind --leak-check=full --track-origins=yes - ./parallel_tests + - valgrind --leak-check=full --track-origins=yes ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt - echo "these are clean times, no valgrind, on 100MB" - echo "2 ab" | ./main.out bigfile2.txt From 0d3c5e8ec3665515206fabd5184b5684ecdc31af Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 15:55:54 +0300 Subject: [PATCH 23/24] reachable leak + somewhy no print --- project/src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/src/main.c b/project/src/main.c index 71dcd37..a6662b9 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -91,8 +91,8 @@ int main(int argc, char* argv[]) { average_parallel = average_parallel / TEST_SERIES_SIZE; printf("Parallel series average: %lf\n", average_parallel); average > average_parallel ? - printf("parallel is faster : %lf vs %lf", average_parallel, average) : - printf("linear is faster : %lf vs %lf", average, average_parallel); + printf("parallel is faster : %lf vs %lf\n", average_parallel, average) : + printf("linear is faster : %lf vs %lf\n", average, average_parallel); dlclose(parallel_lib); if (p) { From c5047c4b32458951d0926e40d862d99a06d60d8d Mon Sep 17 00:00:00 2001 From: Kozhuro Date: Thu, 1 Apr 2021 16:05:51 +0300 Subject: [PATCH 24/24] ready for check --- .travis.yml | 2 +- README.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e917957..6d76398 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,7 +22,7 @@ script: - cppcheck --inconclusive --enable=all --language=c ../include/*.h ../src/*.c - cpplint ../include/*.h ../src/*.c ../tests/*.cpp - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 100000 > bigfile.txt - - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s --show-leak-kinds=all ./main.out bigfile.txt + - echo "2 ab" | valgrind --leak-check=full --track-origins=yes -s ./main.out bigfile.txt - valgrind --leak-check=full --track-origins=yes ./linear_tests - valgrind --leak-check=full --track-origins=yes ./parallel_tests - LC_CTYPE=C tr -dc A-Za-z0-9 < /dev/urandom |fold -w 100 | head -n 1000000 > bigfile2.txt diff --git a/README.md b/README.md index 407e40c..cf86ff5 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![codecov](https://codecov.io/gh/BorisKoz/cpp_2021/branch/HW-2/graph/badge.svg)](https://codecov.io/gh/BorisKoz/cpp_2021) On my machine : parallel is faster : 0.152859 vs 1.536328s +Remote : parallel is faster : 0.588880 vs 0.765509 # Вариант #4 Перед вами поставлена задача подсчета количества вхождений заданных символов в загруженный в оперативную память файл размером 100 Мб. Составьте наивный алгоритм подсчета вхождений символов, в затем реализуйте параллельную обработку текста несколькими процессами с учетом оптимизации работы с кэш-памятью.