Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoma1002 authored Feb 7, 2020
1 parent 041dfa2 commit 2a9911a
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 0 deletions.
Binary file added hw1_mm.pdf
Binary file not shown.
Binary file added ym474_hw1.pdf
Binary file not shown.
85 changes: 85 additions & 0 deletions ym474_mm_rbyc.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h> //printf
#include <stdlib.h> // malloc
#include <time.h> /* for clock_gettime */
#include <stdint.h> /* 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;
}
104 changes: 104 additions & 0 deletions ym474_mm_tile.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h> //printf
#include <stdlib.h> // malloc
#include <time.h> /* for clock_gettime */
#include <stdint.h> /* 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;
}
58 changes: 58 additions & 0 deletions ym474_problem1.c
Original file line number Diff line number Diff line change
@@ -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 <math.h> //pow()
#include <stdio.h> //printf
#include <stdlib.h> // malloc
#include <time.h> /* for clock_gettime */
#include <stdint.h> /* 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);
}

0 comments on commit 2a9911a

Please sign in to comment.