Created
August 26, 2021 08:44
-
-
Save tomokinex/029493d9f2d177bf87983cf597932bf3 to your computer and use it in GitHub Desktop.
merge_sort_and_matrix_vector_mul
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#define vector_size (1 << 14) | |
int array[vector_size][vector_size] = {0}; | |
int in_vector[vector_size] = {0}; | |
int out_vector[vector_size] = {0}; | |
struct pthread_arg{ | |
int start; | |
int end; | |
int size; | |
}; | |
void matrix_mul(int start, int end, int size){ | |
for(int j=0;j<size;++j){ | |
for(int i=start;i<end;++i){ | |
out_vector[i] += array[j][i] * in_vector[j]; | |
} | |
} | |
} | |
void matrix_mul_thread_wrapper(struct pthread_arg* arg){ | |
matrix_mul(arg->start, arg->end, arg->size); | |
} | |
void main(int argc, char** argv) { | |
if (argc != 6){ | |
printf("This program required 5 argv, \n"); | |
printf(" argv[1] = input vector size (The upper size is 32K)\n"); | |
printf(" argv[2] = input_file_name1 : input matrix (Space delimited)\n"); | |
printf(" argv[3] = input_file_name2 : input vector (Space delimited)\n"); | |
printf(" argv[4] = output_file_name : output vector (Space delimited)\n"); | |
printf(" argv[5] = is_thread : 0 is single thread and others is multi thread (1 parent and 2 child thread)\n"); | |
} | |
int size = atoi(argv[1]); | |
int is_thread = atoi(argv[5]); | |
FILE * fp_in1 = fopen(argv[2], "r"); | |
for(int i=0;i<size;++i){ | |
for(int j=0;j<size;++j){ | |
fscanf(fp_in1, "%d", &array[j][i]); | |
} | |
} | |
fclose(fp_in1); | |
FILE * fp_in2 = fopen(argv[3], "r"); | |
for(int i=0;i<size;++i){ | |
fscanf(fp_in2, "%d", &in_vector[i]); | |
} | |
fclose(fp_in2); | |
clock_t start,end; | |
start = clock(); | |
if(is_thread != 0){ | |
void* result; | |
pthread_t thread_id1, thread_id2; | |
struct pthread_arg arg1; | |
struct pthread_arg arg2; | |
arg1.start = 0; | |
arg1.end = size/2; | |
arg1.size = size; | |
arg2.start = size/2; | |
arg2.end = size; | |
arg2.size = size; | |
pthread_create(&thread_id1,NULL,(void *)matrix_mul_thread_wrapper,&arg1); | |
pthread_create(&thread_id2,NULL,(void *)matrix_mul_thread_wrapper,&arg2); | |
pthread_join(thread_id1,&result); | |
pthread_join(thread_id2,&result); | |
}else{ | |
matrix_mul(0, size, size); | |
} | |
end = clock(); | |
printf("Executed time is %.2f seconds\n",(double)(end-start)/CLOCKS_PER_SEC); | |
FILE * fp_out = fopen(argv[4], "w"); | |
for(int i=0;i<size;++i){ | |
fprintf(fp_out, "%d ", out_vector[i]); | |
} | |
fclose(fp_out); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#define array_size (1 << 20) | |
int array[array_size] = {0}; | |
int buf[array_size] = {0}; | |
struct pthread_arg{ | |
int start; | |
int end; | |
}; | |
void merge(int start, int len){ | |
int i=start; | |
int j=start + len; | |
int i_bind = i + len; | |
int j_bind = j + len; | |
for(int k=0;k<len*2;++k){ | |
if(array[j] > array[i]){ | |
buf[k+start] = array[i]; | |
i++; | |
if(i == i_bind){ | |
for(int l=j;l<j_bind;++l){ | |
buf[k+start+l+1-j] = array[l]; | |
} | |
break; | |
} | |
}else{ | |
buf[k+start] = array[j]; | |
j++; | |
if(j == j_bind){ | |
for(int l=i;l<i_bind;++l){ | |
buf[k+start+l+1-i] = array[l]; | |
} | |
break; | |
} | |
} | |
} | |
for(int k=0;k<len*2;++k){ | |
array[start+k] = buf[start+k]; | |
} | |
} | |
void merge_sort(int start, int end){ | |
int len = end - start; | |
if(len > 2){ | |
int mid = (start + end) / 2; | |
merge_sort(start, mid); | |
merge_sort(mid, end); | |
merge(start, len / 2); | |
}else{ | |
if(len == 2){ | |
if(array[start] > array[end-1]){ | |
int t = array[end-1]; | |
array[end-1] = array[start]; | |
array[start] = t; | |
} | |
} | |
} | |
} | |
void merge_sort_thread_wrapper(struct pthread_arg* arg){ | |
int start = arg->start; | |
int end = arg->end; | |
int len = end - start; | |
int mid = (start + end)/2; | |
merge_sort(start, mid); | |
merge_sort(mid, end); | |
merge(start, len/2); | |
} | |
void main(int argc, char** argv) { | |
if (argc != 5){ | |
printf("This program required 4 argv, \n"); | |
printf(" argv[1] = The number of input array (Power of 2 is recommended and The upper size is 16M)\n"); | |
printf(" argv[2] = input_file_name : Array of int (Space delimited)\n"); | |
printf(" argv[3] = output_file_name : Array of int (Space delimited)\n"); | |
printf(" argv[4] = is_thread : 0 is single thread and others is multi thread (1 parent and 2 child thread)\n"); | |
return; | |
} | |
int num_array = atoi(argv[1]); | |
FILE * fp_in = fopen(argv[2], "r"); | |
for(int i=0;i<num_array;++i){ | |
fscanf(fp_in, "%d", &array[i]); | |
} | |
fclose(fp_in); | |
int is_thread = atoi(argv[4]); | |
clock_t start,end; | |
start = clock(); | |
if(is_thread != 0){ | |
void* result; | |
pthread_t thread_id1, thread_id2; | |
struct pthread_arg arg1; | |
struct pthread_arg arg2; | |
arg1.start = 0; | |
arg1.end = num_array/2; | |
arg2.start = num_array/2; | |
arg2.end = num_array; | |
pthread_create(&thread_id1,NULL,(void *)merge_sort_thread_wrapper,&arg1); | |
pthread_create(&thread_id2,NULL,(void *)merge_sort_thread_wrapper,&arg2); | |
pthread_join(thread_id1,&result); | |
pthread_join(thread_id2,&result); | |
merge(0, num_array/2); | |
}else{ | |
merge_sort(0, num_array); | |
} | |
end = clock(); | |
printf("Executed time is %.2f seconds\n",(double)(end-start)/CLOCKS_PER_SEC); | |
FILE * fp_out = fopen(argv[3], "w"); | |
for(int i=0;i<num_array;++i){ | |
fprintf(fp_out, "%d ", array[i]); | |
} | |
fclose(fp_out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment