diff --git a/hw1_mm.pdf b/hw1_mm.pdf new file mode 100644 index 0000000..cb3442f Binary files /dev/null and b/hw1_mm.pdf differ diff --git a/ym474_hw1.pdf b/ym474_hw1.pdf new file mode 100644 index 0000000..7b16926 Binary files /dev/null and b/ym474_hw1.pdf differ diff --git a/ym474_mm_rbyc.c b/ym474_mm_rbyc.c new file mode 100644 index 0000000..31949f2 --- /dev/null +++ b/ym474_mm_rbyc.c @@ -0,0 +1,85 @@ +// a simple "row-by-colum" matrix matrix multiplication +// +// Author : Yuhua Ma (ym474) +// Date : 1/30/2020 +// How to compile : +// +// % cd _sourcefile's path_ +// % gcc -o rbyc ym474_mm_rbyc.c +// % ./rbyc _dim_n's value_ +// + +#include //printf +#include // malloc +#include /* for clock_gettime */ +#include /* for uint64 definition */ + +#define BILLION 1000000000L +void printMatrix(int *matrix, int row, int col); + +void printMatrix(int *matrix, int row, int col) +{ + int i,j; + for(i = 0; i < row; i++){ + for(j = 0; j < col; j++){ + printf("%d ",matrix[i*col+j]); + } + printf("\n"); + } + printf("\n"); +} + + +/* +createMatrix +rbyc multiplication +time consumption +*/ +int main(int argc, char *argv[]) +{ + // create matrix A,B,C using malloc + if (argc != 2) + { + printf("wrong number of input arguments\n"); + } + + int dim_n = atoi(argv[1]); + printf("dim_n = %d\n", dim_n); + + int* A= (int *)malloc(dim_n * dim_n * sizeof(int));; + int* B= (int *)malloc(dim_n * dim_n * sizeof(int));; + int* C = (int *)malloc(dim_n * dim_n * sizeof(int)); + + // fill-in matrix A, B + double drand48(); + void srand48(); + srand48(1); + int i,j,k; + for (i = 0; i < dim_n; i++) + { + for (j = 0; j < dim_n; j++) + { + *(A + i * dim_n + j) = (int)(drand48()*100); + *(B + i * dim_n + j) = (int)(drand48()*100); + } + } + + // calculate matrix C + struct timespec start,end; + double diff; + clock_gettime(CLOCK_MONOTONIC,&start); + for(i = 0; i < dim_n; i++){ + for(j = 0; j < dim_n; j++){ + for(k = 0; k < dim_n; k++){ + + *(C + i*dim_n + j) = *(C + i*dim_n + j) + (*(A + i*dim_n + k) * *(A + k*dim_n + j)); + } + } + } + + clock_gettime(CLOCK_MONOTONIC,&end); + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("timing(nsec) %lf\n", diff); + + return 0; +} \ No newline at end of file diff --git a/ym474_mm_tile.c b/ym474_mm_tile.c new file mode 100644 index 0000000..4036ef8 --- /dev/null +++ b/ym474_mm_tile.c @@ -0,0 +1,104 @@ +// a "tiled" matrix-matrix multiplication +// +// Author : Yuhua Ma (ym474) +// Date : 1/30/2020 +// +// % cd _sourcefile's path_ +// % gcc -o tile ym474_mm_tile.c +// % ./tile _dim_n's value_ _tile's value_ +// + +#include //printf +#include // malloc +#include /* for clock_gettime */ +#include /* for uint64 definition */ + +#define BILLION 1000000000L +void printMatrix(int *matrix, int row, int col); + +void printMatrix(int *matrix, int row, int col) +{ + int i,j; + for(i = 0; i < row; i++){ + for(j = 0; j < col; j++){ + printf("%d ",matrix[i*col+j]); + } + printf("\n"); + } + printf("\n"); +} + + +/* +createMatrix +tile multiplication +time consumption +*/ +int main(int argc, char *argv[]) +{ + // create matrix A,B,C using malloc + if (argc != 3) + { + printf("wrong number of input arguments\n"); + } + + int dim_n = atoi(argv[1]); + int tile_size = atoi(argv[2]); + int p = dim_n / tile_size; + printf("dim_n = %d\n", dim_n); + printf("tile_size = %d\n", tile_size); + printf("p = %d\n", p); + + int* A= (int *)malloc(dim_n * dim_n * sizeof(int));; + int* B= (int *)malloc(dim_n * dim_n * sizeof(int));; + int* C = (int *)malloc(dim_n * dim_n * sizeof(int)); + + // fill-in matrix A, B + double drand48(); + void srand48(); + srand48(1); + int i,j; + for (i = 0; i < dim_n; i++) + { + for (j = 0; j < dim_n; j++) + { + *(A + i * dim_n + j) = (int)(drand48()*100); + *(B + i * dim_n + j) = (int)(drand48()*100); + } + } + + // calculate matrix C + struct timespec start,end; + double diff; + clock_gettime(CLOCK_MONOTONIC,&start); + // define I, J, K for block's index + // define i_,j_,k_ for index of a block + // define i,j,k for real index + int I, J, K,k ,i_,j_,k_; + for(I = 0; I < p; I++){ + for(J = 0;J < p; J++){ + for(K = 0; K < p; K++){ + for(i_ = 0; i_ < tile_size; i_++){ + for(j_ = 0; j_ < tile_size; j_++){ + for(k_ = 0; k_ < tile_size; k_++){ + i = tile_size * I + i_; + j = tile_size * J + j_; + k = tile_size * K + k_; + + *(C + i*dim_n + j) = *(C + i*dim_n + j) + (*(A + i*dim_n + k) * *(A + k*dim_n + j)); + + } + } + } + + + } + } + } + + clock_gettime(CLOCK_MONOTONIC,&end); + diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + printf("timing(nsec) %lf\n", diff); + + return 0; +} \ No newline at end of file diff --git a/ym474_problem1.c b/ym474_problem1.c new file mode 100644 index 0000000..7aa499e --- /dev/null +++ b/ym474_problem1.c @@ -0,0 +1,58 @@ +// test memory access time by "touching" elements of a linear array A +// with a progressively growing stepsize (stride). +// +// Author : Yuhua Ma (ym474) +// Date : 2/1/2020 +// How to compile : +// +// % cd _sourcefile's path_ +// % gcc -o benchmark benchmark.c +// +// no std=c99 because clock_gettime() +// add -pthread because pthread_create,pthread_join +// % ./benchmark +// +// #include //pow() +#include //printf +#include // malloc +#include /* for clock_gettime */ +#include /* for uint64 definition */ + +#define BILLION 1E9L +#define MAX_LENGTH1 1 << 26 +#define MIN_SIZE1 1 << 10 + +void main() { + struct timespec start, end; + double diff; + float *A = (float *)malloc(sizeof(float) * MAX_LENGTH1); // whether init A[][] + // does not influence + // the experiment + // result + + int n, s, i, c, cnt = 10, tmp, j; + + FILE *f = fopen("problem1_data.txt", "w"); + + for (n = MIN_SIZE1; n <= MAX_LENGTH1; n *= 2) { + fprintf(f, "n = %ld\n", n); + for (s = 1; s <= n / 2; s *= 2) { + clock_gettime(CLOCK_MONOTONIC, &start); + for (c = 0; c < cnt * s; c++) { + for (i = 0; i < n; i += s) { + tmp = A[i]; // load A[i] + } + } + clock_gettime(CLOCK_MONOTONIC, &end); + diff = + BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec; + diff = diff / (n * cnt); + printf("array length(n) : %ld stride length(s) : %ld for s*n times. " + "Average timing(sec) %llu\n", + n, s, diff); + fprintf(f, "%lf\n", diff); + } + fprintf(f, "------------------\n"); + } + fclose(f); +} \ No newline at end of file