testing around with structs
This commit is contained in:
		
							
								
								
									
										18
									
								
								graph.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								graph.c
									
									
									
									
									
								
							| @@ -19,6 +19,24 @@ void random_adjacency(const ulong vertex_count, ulong matrix[vertex_count][verte | ||||
|     } | ||||
| } | ||||
|  | ||||
| void random_adjacency_struct(const Matrix *matrix) { | ||||
|     if (matrix->row_length != matrix->column_length) { | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     srand(time(NULL)); | ||||
|  | ||||
|     for (ulong row_index = 0; row_index < matrix->row_length; row_index++) { | ||||
|         for (ulong column_index = 0; column_index < matrix->column_length; column_index++) { | ||||
|             if (column_index == row_index) { | ||||
|                 matrix->values[row_index][column_index] = 0; | ||||
|             } else { | ||||
|                 matrix->values[row_index][column_index] = rand() % 2; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void calculate_distance_matrix(const ulong vertex_count, const ulong adjacency_matrix[vertex_count][vertex_count], ulong distance_matrix[vertex_count][vertex_count]) { | ||||
|     ulong power_matrix[vertex_count][vertex_count]; | ||||
|     ulong temp_power_matrix[vertex_count][vertex_count]; | ||||
|   | ||||
							
								
								
									
										4
									
								
								graph.h
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								graph.h
									
									
									
									
									
								
							| @@ -1,11 +1,13 @@ | ||||
| #ifndef GRAPH_H | ||||
| #define GRAPH_H | ||||
|  | ||||
| typedef unsigned long ulong; | ||||
| #include "matrix.h" | ||||
|  | ||||
| void random_adjacency(const ulong vertex_count, | ||||
|                       ulong matrix[vertex_count][vertex_count]); | ||||
|  | ||||
| void random_adjacency_struct(const Matrix *matrix); | ||||
|  | ||||
| void calculate_distance_matrix(const ulong vertex_count, | ||||
|                                const ulong adjacency_matrix[vertex_count][vertex_count], | ||||
|                                ulong distance_matrix[vertex_count][vertex_count]); | ||||
|   | ||||
							
								
								
									
										87
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										87
									
								
								main.c
									
									
									
									
									
								
							| @@ -5,8 +5,6 @@ | ||||
| #include <time.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| typedef struct matrix {ulong row_length; ulong column_length; ulong **values;} Matrix; | ||||
|  | ||||
| void benchmark_gemm() { | ||||
|     const ulong vertex_count = 100; | ||||
|     ulong adjacency_matrix1[vertex_count][vertex_count]; | ||||
| @@ -266,64 +264,43 @@ void test_with_dfs() { | ||||
|     // free(articulations); | ||||
| } | ||||
|  | ||||
| void print_matrix_struct(const Matrix *matrix) { | ||||
|     for (ulong column_index=0; column_index < matrix->column_length; column_index++) { | ||||
|         for (ulong row_index=0; row_index < matrix->row_length; row_index++) { | ||||
|             printf("%lu ", matrix->values[row_index][column_index]); | ||||
|         } | ||||
|         puts(""); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void gemm_structs(const Matrix *matrix1, const Matrix *matrix2, Matrix *output_matrix) { | ||||
|     ulong sum; | ||||
|  | ||||
|     for (ulong i = 0; i < matrix1->row_length; i++) { | ||||
|         for (ulong j = 0; j < matrix2->column_length; j++) { | ||||
|             sum = 0; | ||||
|  | ||||
|             for (ulong k = 0; k < matrix1->row_length; k++) { | ||||
|                 sum += matrix1->values[i][k] * matrix2->values[k][j]; | ||||
|             } | ||||
|  | ||||
|             output_matrix->values[i][j] = sum; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void random_adjacency_struct(const Matrix *matrix) { | ||||
|     if (matrix->row_length != matrix->column_length) { | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     srand(time(NULL)); | ||||
|  | ||||
|     for (ulong row_index = 0; row_index < matrix->row_length; row_index++) { | ||||
|         for (ulong column_index = 0; column_index < matrix->column_length; column_index++) { | ||||
|             if (column_index == row_index) { | ||||
|                 matrix->values[row_index][column_index] = 0; | ||||
|             } else { | ||||
|                 matrix->values[row_index][column_index] = rand() % 2; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void test_with_structs() { | ||||
|     ulong row_length = 5, column_length = 10; | ||||
|     ulong **values = (ulong**)malloc(row_length * sizeof(ulong*)); | ||||
|     for (ulong index = 0; index < row_length; index++) { | ||||
|         values[index] = (ulong*)malloc(column_length * sizeof(ulong)); | ||||
|     } | ||||
|     Matrix adjacency_matrix = {row_length, column_length, values}; | ||||
|     ulong row_length = 5, column_length = 5; | ||||
|  | ||||
|     random_adjacency_struct(&adjacency_matrix); | ||||
|     print_matrix_struct(&adjacency_matrix); | ||||
|     ulong **values1 = (ulong**)malloc(row_length * sizeof(ulong*)); | ||||
|     ulong **values2 = (ulong**)malloc(row_length * sizeof(ulong*)); | ||||
|     ulong **values3 = (ulong**)malloc(row_length * sizeof(ulong*)); | ||||
|  | ||||
|     for (ulong index = 0; index < row_length; index++) { | ||||
|         free(values[index]); | ||||
|         values1[index] = (ulong*)malloc(column_length * sizeof(ulong)); | ||||
|         values2[index] = (ulong*)malloc(column_length * sizeof(ulong)); | ||||
|         values3[index] = (ulong*)malloc(column_length * sizeof(ulong)); | ||||
|     } | ||||
|     free(values); | ||||
|  | ||||
|     Matrix adjacency_matrix1 = {row_length, column_length, values1}; | ||||
|     Matrix adjacency_matrix2 = {row_length, column_length, values2}; | ||||
|     Matrix dot_product = {row_length, column_length, values3}; | ||||
|  | ||||
|     random_adjacency_struct(&adjacency_matrix1); | ||||
|     print_matrix_struct(&adjacency_matrix1); | ||||
|     putchar('\n'); | ||||
|  | ||||
|     random_adjacency_struct(&adjacency_matrix2); | ||||
|     print_matrix_struct(&adjacency_matrix2); | ||||
|     putchar('\n'); | ||||
|  | ||||
|     gemm_basic_structs(&adjacency_matrix1, &adjacency_matrix2, &dot_product); | ||||
|     print_matrix_struct(&dot_product); | ||||
|     putchar('\n'); | ||||
|  | ||||
|     for (ulong index = 0; index < row_length; index++) { | ||||
|         free(values1[index]); | ||||
|         free(values2[index]); | ||||
|         free(values3[index]); | ||||
|     } | ||||
|     free(values1); | ||||
|     free(values2); | ||||
|     free(values3); | ||||
| } | ||||
|  | ||||
| int main(void) { | ||||
|   | ||||
							
								
								
									
										25
									
								
								matrix.c
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								matrix.c
									
									
									
									
									
								
							| @@ -14,6 +14,15 @@ void print_matrix(const ulong row_length, const ulong column_length, const ulong | ||||
|     } | ||||
| } | ||||
|  | ||||
| void print_matrix_struct(const Matrix *matrix) { | ||||
|     for (ulong column_index=0; column_index < matrix->column_length; column_index++) { | ||||
|         for (ulong row_index=0; row_index < matrix->row_length; row_index++) { | ||||
|             printf("%lu ", matrix->values[row_index][column_index]); | ||||
|         } | ||||
|         puts(""); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void gemm_basic(const ulong row_length1, const ulong column_length1, const ulong matrix1[row_length1][column_length1], | ||||
|                           const ulong row_length2, const ulong column_length2, const ulong matrix2[row_length2][column_length2], | ||||
|                           ulong output_matrix[row_length1][column_length2]) { | ||||
| @@ -32,6 +41,22 @@ void gemm_basic(const ulong row_length1, const ulong column_length1, const ulong | ||||
|     } | ||||
| } | ||||
|  | ||||
| void gemm_basic_structs(const Matrix *matrix1, const Matrix *matrix2, Matrix *output_matrix) { | ||||
|     ulong sum; | ||||
|  | ||||
|     for (ulong i = 0; i < matrix1->row_length; i++) { | ||||
|         for (ulong j = 0; j < matrix2->column_length; j++) { | ||||
|             sum = 0; | ||||
|  | ||||
|             for (ulong k = 0; k < matrix1->row_length; k++) { | ||||
|                 sum += matrix1->values[i][k] * matrix2->values[k][j]; | ||||
|             } | ||||
|  | ||||
|             output_matrix->values[i][j] = sum; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| int read_csv(const char *file_name, const ulong row_length, const ulong column_length, ulong output_matrix[row_length][column_length]) { | ||||
|     FILE *file_ptr; | ||||
|     ulong bufsize = row_length*2+1; // have to account for delimiters | ||||
|   | ||||
							
								
								
									
										6
									
								
								matrix.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								matrix.h
									
									
									
									
									
								
							| @@ -2,10 +2,13 @@ | ||||
| #define MATRIX_H | ||||
|  | ||||
| typedef unsigned long ulong; | ||||
| typedef struct matrix {ulong row_length; ulong column_length; ulong **values;} Matrix; | ||||
|  | ||||
| void print_matrix(const ulong row_length, const ulong column_length, | ||||
|                   const ulong matrix[row_length][column_length]); | ||||
|  | ||||
| void print_matrix_struct(const Matrix *matrix); | ||||
|  | ||||
| /* | ||||
|     First two matrices will be multiplied and | ||||
|     restult will be written to output_matrix. | ||||
| @@ -17,6 +20,9 @@ void gemm_basic(const ulong row_length1, const ulong column_length1, | ||||
|                 const ulong matrix2[row_length2][column_length2], | ||||
|                 ulong output_matrix[row_length1][column_length2]); | ||||
|  | ||||
| void gemm_basic_structs(const Matrix *matrix1, const Matrix *matrix2, | ||||
|                         Matrix *output_matrix); | ||||
|  | ||||
| int read_csv(const char *file_name, const ulong row_length, | ||||
|              const ulong column_length, | ||||
|              ulong output_matrix[row_length][column_length]); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user