Changeset 5692 in /cluster/svnroot


Ignore:
Timestamp:
May 22, 2016 7:29:41 PM (5 years ago)
Author:
skylar
Message:

matmul function done

Location:
bccd-ng/bw-institute/2016/day06/matmul
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • bccd-ng/bw-institute/2016/day06/matmul/Makefile

    r5689 r5692  
    33CC                      = gcc
    44CFLAGS          = -Wall -gstabs+
     5ifeq ($(DEBUG),1)
     6        CFLAGS  += -DDEBUG
     7endif
    58OMP_FLAGS       = -fopenmp
    69
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.c

    r5691 r5692  
    4545        // Guarantee different starting seeds for random numbers in each thread
    4646        seeds[i] = t+i;
     47#ifdef DEBUG
     48        fprintf(stderr,"seeds[%d] = %u\n",i,seeds[i]);
     49#endif
    4750    }
    4851
     
    6366#else
    6467    // When not using OpenMP, only one "thread" will be running
    65     t = 1;
     68    t = 0;
    6669#endif
    6770    for(i=0;i<m->rows*m->cols;i++) {
    68         m->matrix[i] = rand_r(&s[t]);
     71        // Limit matrix size < 10
     72        m->matrix[i] = rand_r(&s[t]) % 10;
    6973    }
    7074#ifdef _OPENMP
     
    7276#endif
    7377}
     78
     79// Takes in a reference to a matrix and a row/column coordinate
     80// Returns the one-dimensional index in the matrix for the coordinate
     81unsigned int coord(const struct matrix *m,const unsigned int row,const unsigned int col) {
     82    return row*m->cols+col;
     83}
     84
     85// Takes in a matrix and prints it to STDOUT
     86void print_matrix(const struct matrix *m) {
     87    int row,col;
     88
     89    for(row=0;row<m->rows;row++) {
     90        for(col=0;col<m->cols;col++) {
     91            printf("%d ",m->matrix[coord(m,row,col)]);
     92        }
     93        puts("");
     94    }
     95}
     96
     97void matmul(struct matrix *m1,struct matrix *m2,struct matrix *dst_m) {
     98    unsigned int dst_row,dst_col,i,dst_coord;
     99
     100    for(dst_row=0;dst_row<dst_m->rows;dst_row++) {
     101        for(dst_col=0;dst_col<dst_m->cols;dst_col++) {
     102#ifdef DEBUG
     103            fprintf(stderr,"Calculating (%d,%d)\n",dst_row,dst_col);
     104#endif
     105            dst_coord = coord(dst_m,dst_row,dst_col);
     106            // Make sure destination matrix is initialized
     107            dst_m->matrix[dst_coord] = 0;
     108            for(i=0;i<m1->rows;i++) {
     109                dst_m->matrix[dst_coord] +=
     110                    m1->matrix[coord(m1,dst_row,i)]
     111                    * m2->matrix[coord(m2,i,dst_col)];
     112            }
     113#ifdef DEBUG
     114            fprintf(stderr,"m1(%d,%d) * m2(%d,%d) = dst_m(%d,%d) (%d)\n",
     115                    dst_row,i,i,dst_col,dst_row,dst_col,
     116                    dst_m->matrix[dst_coord]
     117                   );
     118#endif
     119        }
     120    }
     121}
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.h

    r5691 r5692  
    2121#endif
    2222
     23// Structure defining a matrix and its attributes
    2324struct matrix {
    2425    int *matrix;
     
    4142void init_matrix(struct matrix *,unsigned int *);
    4243
    43 // Takes in a reference to two source matrices and their sizes, and a destination matrix and its size
    44 void matmul(int *,unsigned int,int *,unsigned int,int *,unsigned int);
     44// Takes in a matrix and row/column coordinate. Returns the one-dimensional
     45// index into the given matrix.
     46unsigned int coord(const struct matrix *,const unsigned int,const unsigned int);
     47
     48// Takes in a matrix. Prints it to STDOUT
     49void print_matrix(const struct matrix *);
     50
     51// Takes in two sources matrices and a destination matrix
     52// Populates destination matrix by multiplying the first two matrices
     53void matmul(struct matrix *,struct matrix *,struct matrix *);
    4554
    4655#endif
  • bccd-ng/bw-institute/2016/day06/matmul/serial-omp.c

    r5691 r5692  
    3939    }
    4040
     41    if(m1.cols != m2.rows) {
     42        fprintf(stderr,"Invalid matrix dimensions!\n");
     43        exit(EXIT_FAILURE);
     44    }
     45
     46    dst_m.rows = m1.rows;
     47    dst_m.cols = m2.cols;
     48
    4149    m1.matrix       = safe_malloc_int(m1.rows*m1.cols,"Allocating first matrix");
    4250    m2.matrix       = safe_malloc_int(m2.rows*m2.cols,"Allocating second matrix");
    43     dst_m.matrix    = safe_malloc_int(m1.rows*m2.cols,"Allocating destination matrix");
     51    dst_m.matrix    = safe_malloc_int(dst_m.rows*dst_m.cols,"Allocating destination matrix");
    4452
    4553    random_seeds = init_random_seeds();
     
    4957    init_matrix(&m2,random_seeds);
    5058
     59    puts("Matrix 1\n");
     60    print_matrix(&m1);
     61    puts("");
     62    puts("Matrix 2\n");
     63    print_matrix(&m2);
     64    puts("");
     65
    5166    // Matrix computation
     67    matmul(&m1,&m2,&dst_m);
     68
     69    puts("Destination matrix\n");
     70    print_matrix(&dst_m);
     71
     72    // Free heap memory
     73    free(m1.matrix);
     74    free(m2.matrix);
     75    free(dst_m.matrix);
    5276
    5377    exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.