X-Git-Url: https://vgcfreebox.myrthtech.pt/gitweb/ue-pp-squarematrixparallelisation.git/blobdiff_plain/03e6d97645ffb44eb652a00b7f87c327a855faaf..refs/heads/main:/ImprovedParallelMatricSearch.c?ds=sidebyside diff --git a/ImprovedParallelMatricSearch.c b/ImprovedParallelMatricSearch.c index 0df0ba1..b385384 100644 --- a/ImprovedParallelMatricSearch.c +++ b/ImprovedParallelMatricSearch.c @@ -5,7 +5,7 @@ const int m_rows = 40000; const int m_cols = 40000; -const int threads_to_use = 8; +const int threads_to_use = 4; _Atomic int hv = 0; int **matrix; @@ -15,6 +15,16 @@ typedef struct{ int end; } thread_args; +void* parallel_populate_matrix(void *arg){ + thread_args *args = (thread_args *)arg; + for(int r = 0; r < m_rows; r++){ + for(int c = args->start; c < args->end;c++){ + matrix[r][c] = r*m_cols+c; + } + } + return NULL; +} + void* parallel_search_cols(void *arg){ thread_args *args = (thread_args *)arg; int local_hv = 0; @@ -63,14 +73,40 @@ void run_parallel_search(int num_threads) { free(args); } -void init_parallel_matrix(int num_threads){ - for(int r = 0; r < m_rows; r++){ - for(int c = 0; c < m_cols; c++){ - matrix[r][c] = r*m_cols+c; +void init_parallel_matrix(int *data,int num_threads){ + pthread_t *threads = malloc(num_threads * sizeof(pthread_t)); + thread_args *args = malloc(num_threads * sizeof(thread_args)); + if (threads == NULL || args == NULL) { + perror("Failed to allocate memory for threads"); + exit(1); } - } + + for(int r = 0; r Allocate memory\n"); clock_t t_t1; t_t1 = clock(); @@ -93,10 +129,7 @@ int main(void){ printf("2nd --> Populate the matrix\n"); clock_t t_t2; t_t2 = clock(); - for(int r = 0; r