From: VGoncalo Date: Wed, 25 Mar 2026 03:14:10 +0000 (+0000) Subject: improving matrix init X-Git-Url: https://vgcfreebox.myrthtech.pt/gitweb/ue-pp-squarematrixparallelisation.git/commitdiff_plain/03e6d97645ffb44eb652a00b7f87c327a855faaf?ds=sidebyside;hp=e1bc809d28992e9ab3b46078a6bd6de4618c1961 improving matrix init --- diff --git a/ImprovedParallelMatricSearch.c b/ImprovedParallelMatricSearch.c new file mode 100644 index 0000000..0df0ba1 --- /dev/null +++ b/ImprovedParallelMatricSearch.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +const int m_rows = 40000; +const int m_cols = 40000; +const int threads_to_use = 8; + +_Atomic int hv = 0; +int **matrix; + +typedef struct{ + int start; + int end; +} thread_args; + +void* parallel_search_cols(void *arg){ + thread_args *args = (thread_args *)arg; + int local_hv = 0; + for(int r = 0; r < m_rows; r++){ + for(int c = args->start; c < args->end; c++){ + if(matrix[r][c] > local_hv){ + local_hv = matrix[r][c]; + } + } + } + + if(local_hv > hv){ + hv = local_hv; + } + return NULL; +} + +void run_parallel_search(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); + } + + int chunk_size = m_cols / num_threads; + + for (int i = 0; i < num_threads; i++) { + args[i].start = i * chunk_size; + if (i == num_threads - 1) { + args[i].end = m_cols; + } else { + args[i].end = (i + 1) * chunk_size; + } + if (pthread_create(&threads[i], NULL, ¶llel_search_cols, &args[i]) != 0) { + perror("Failed to create thread"); + exit(1); + } + } + + for (int i = 0; i < num_threads; i++) { + pthread_join(threads[i], NULL); + } + free(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; + } + } +} + +int main(void){ + printf("1st --> Allocate memory\n"); + clock_t t_t1; t_t1 = clock(); + size_t rows_size = m_rows * sizeof(int*); + matrix = malloc(rows_size); + if(matrix == NULL){ + perror("malloc failled"); + return 1; + } + size_t data_size = (size_t)m_rows * m_cols * sizeof(int); + int *data; + if(posix_memalign((void**)&data, 64, data_size) != 0){ + perror("not able to alocate memory"); + free(matrix); + return 1; + } + t_t1 = clock() - t_t1; + double t1_ttaken = ((double)t_t1)/CLOCKS_PER_SEC; + printf(" %f sec\n",t1_ttaken); + + printf("2nd --> Populate the matrix\n"); + clock_t t_t2; t_t2 = clock(); + for(int r = 0; r Search matrix\n"); + clock_t t_t3; t_t3 = clock(); + run_parallel_search(threads_to_use); + t_t3 = clock() - t_t3; + double t3_ttaken = ((double)t_t3)/CLOCKS_PER_SEC; + printf(" %f sec\n",t3_ttaken); + + printf("Done\n The biggest value found is %d \n", hv); + free(data); + free(matrix); + return 0; +} diff --git a/MatrixParallelPrograming.pdf b/MatrixParallelPrograming.pdf index 14ce4f8..0ef406a 100644 Binary files a/MatrixParallelPrograming.pdf and b/MatrixParallelPrograming.pdf differ