Changeset 5685 in /cluster/svnroot


Ignore:
Timestamp:
May 19, 2016 11:22:05 PM (5 years ago)
Author:
skylar
Message:

allocate memory

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

Legend:

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

    r5684 r5685  
    99        $(CC) $(CFLAGS) -o $@ -c $<
    1010
    11 serial: serial.c matmul.c
     11serial: serial.c matmul.o
    1212        $(CC) $(CFLAGS) -o $@ matmul.o $<
    1313
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.c

    r5684 r5685  
    66
    77void usage() {
    8     fprintf(stderr,"serial -r <matrix-rows> -c <matrix-cols>\n");
     8    fprintf(stderr,"serial -x <m1-cols> -y <m1-rows> -a <m2-cols> -b <m2-rows>\n");
    99}
     10
     11int *safe_malloc(const int c,const char *msg) {
     12    int *m;
     13    if((m = malloc(c*sizeof(int))) == NULL) {
     14        fprintf(stderr,"%s failed at %s, line %d: %s\n",
     15                msg,__FILE__,__LINE__,strerror(errno));
     16        exit(EXIT_FAILURE);
     17    }
     18
     19    return m;
     20}
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.h

    r5684 r5685  
    1111
    1212void usage();
     13int *safe_malloc(const int,const char *);
  • bccd-ng/bw-institute/2016/day06/matmul/serial.c

    r5684 r5685  
    66
    77int main(int argc,char **argv) {
    8     int c,rows,cols;
     8    int c,m1_rows,m1_cols,m2_rows,m2_cols;
     9    int *m1,*m2,*dst_m;
    910
    10     rows = cols = 0;
     11    m1_rows = m1_cols = m2_rows = m2_cols = 0;
    1112
    12     while((c = getopt(argc,argv, "r:c:")) != -1) {
     13    while((c = getopt(argc,argv, "x:y:a:b:")) != -1) {
    1314        switch(c) {
    14             case 'r':
    15                 rows = atoi(optarg);
     15            case 'x':
     16                m1_cols = atoi(optarg);
    1617                break;
    17             case 'c':
    18                 cols = atoi(optarg);
     18            case 'y':
     19                m1_rows = atoi(optarg);
    1920                break;
     21            case 'a':
     22                m2_cols = atoi(optarg);
     23                break;
     24            case 'b':
     25                m2_rows = atoi(optarg);
    2026            case '?':
    2127                usage();
     
    2430    }
    2531
    26     if(rows == 0 || cols == 0) {
    27         fprintf(stderr,"Supply rows and columns!\n");
     32    if(m1_rows == 0 || m1_cols == 0 || m2_rows == 0 || m2_cols == 0) {
     33        fprintf(stderr,"Supply row and column counts!\n");
    2834        usage();
    2935        exit(EXIT_FAILURE);
    3036    }
    3137
     38    m1      = safe_malloc(m1_rows*m1_cols,"Allocating first matrix");
     39    m2      = safe_malloc(m2_rows*m2_cols,"Allocating second matrix");
     40    dst_m   = safe_malloc(m1_rows*m2_cols,"Allocating destination matrix");
     41
    3242    exit(EXIT_SUCCESS);
    3343}
Note: See TracChangeset for help on using the changeset viewer.