Skip to content

Instantly share code, notes, and snippets.

@luehm
Last active February 8, 2024 19:50
Show Gist options
  • Save luehm/27d4a22538482dc74d7e99f06d93c6a9 to your computer and use it in GitHub Desktop.
Save luehm/27d4a22538482dc74d7e99f06d93c6a9 to your computer and use it in GitHub Desktop.
Matrix Multiply
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "t_pool.h"
#define DEBUG 1
typedef struct calc_args{
int **arr1;
int **arr2;
int ***ans;
int dim;
int row;
int col;
}calc_args;
int read_in(int ***array, char *file){
FILE *to_read;
char *line = NULL;
size_t num_read;
size_t max_to_read = 0;
int dimension = 0;
int i, j;
to_read = fopen(file, "r");
if(to_read == NULL){
printf("Please verify that %s exists and is properly typed (case matters).\n", file);
exit(1);
}
j = 0;
while(1){
num_read = getline(&line, &max_to_read,to_read);
if(line[0] == '#'){
sscanf(line+5, "%d", &dimension);
*array = malloc(dimension * sizeof(int *));
for(i = 0; i < dimension; i++){
(*array)[i] = malloc(dimension * sizeof(int));
}
if(DEBUG) printf("%s has %d lines.\n", file, dimension);
continue;
}else if(dimension == 0){
printf("Please ensure to format your first line of %s as such:\n", file);
printf("#N = [NUMBER]\n");
exit(1);
}else if(feof(to_read))
break;
char *num = strtok(line, " ");
for(i = 0; i < dimension; i++){
sscanf(num, "%d", &(*array)[j][i]);
//if(DEBUG)printf("%s", num);
num = strtok(NULL, " ");
}
j++;
}
fclose(to_read);
return dimension;
}
void calc_cell(void *args){
int **arr1 = ((calc_args *)args)->arr1;
int **arr2 = ((calc_args *)args)->arr2;
int ***ans = ((calc_args *)args)->ans;
int dim = ((calc_args *)args)->dim;
int row = ((calc_args *)args)->row;
int col = ((calc_args *)args)->col;
int i;
int sum = 0;
for(i = 0; i < dim; i++){
sum += arr1[row][i] * arr2[i][col];
}
(*ans)[row][col] = sum;
}
void print_test(void *arg){
printf("%s\n", (char *)arg);
}
void calc_matrix(int **arr1, int **arr2,int ***ans, int dim, int num_strings){
int row, col;
*ans = malloc(dim * sizeof(int *));
for(col = 0; col < dim; col++){
(*ans)[col] = malloc(dim * sizeof(int));
}
pool my_pool;
pool_init(&my_pool, num_strings);
for(row = 0; row < dim; row++){
for(col = 0; col < dim; col++){
calc_args *this_arg = malloc(sizeof(calc_args));
if(this_arg == NULL){
printf("There was a problem creating arguments struct for pool_add\n");
pool_end(&my_pool);
exit(1);
}
this_arg->arr1 = arr1;
this_arg->arr2 = arr2;
this_arg->ans = ans;
this_arg->dim = dim;
this_arg->row = row;
this_arg->col = col;
pool_add(&my_pool, calc_cell, (void *)this_arg);
//pool_add(&my_pool, print_test, "test");
}
}
pool_end(&my_pool);
}
void print_matrix(int **arr, int dimension){
int i, j;
for(i = 0; i < dimension; i++){
for(j = 0; j < dimension; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void save_matrix(int **ans, int dimension, char *file){
FILE *to_save;
int row, col;
to_save = fopen(file, "w");
if(to_save == NULL){
printf("Could not create output file.\n");
exit(1);
}
fprintf(to_save, "#N = %d\n", dimension);
for(row = 0; row < dimension; row++){
for(col = 0; col < dimension; col++){
fprintf(to_save, "%d ", ans[row][col]);
}
fprintf(to_save, "\n");
}
fclose(to_save);
}
void print_usage(void){
printf("Incorrect command-line arguments were given.\n");
printf("Usage:\n\tmatrix file1.dat file2.dat out.dat\n");
}
int main(int argc, char *argv[]){
int **array1;
int **array2;
int **out;
int dim1, dim2;
if(argc != 4){
print_usage();
exit(1);
}else{
dim1 = read_in(&array1, argv[1]);
dim2 = read_in(&array2, argv[2]);
//print_matrix(array1, dim1);
//print_matrix(array2, dim2);
calc_matrix(array1, array2, &out, dim1, 2);
//print_matrix(out, dim2);
save_matrix(out, dim1, argv[3]);
/* pool my;
pool_init(&my, 5);
pool_add(&my, print_test, "the best");
pool_end(&my);*/
free(array1);
free(array2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment