Changeset 5688 in /cluster/svnroot


Ignore:
Timestamp:
May 22, 2016 12:38:18 PM (5 years ago)
Author:
skylar
Message:

use unsigned ints where needed

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

Legend:

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

    r5687 r5688  
    1111// Take in an integer c and a message to print on failure
    1212// Allocate an array of c ints and return it
    13 int *safe_malloc_int(const int c,const char *msg) {
     13int *safe_malloc_int(const unsigned int c,const char *msg) {
    1414    int *m;
    1515    if((m = malloc(c*sizeof(int))) == NULL) {
     
    2222}
    2323
    24 int *init_random_seeds() {
    25     int i,*seeds;
     24// Take in an unsigned integer c and a message to prunsigned int on failure
     25// Allocate an array of c unsigned ints and return it
     26unsigned int *safe_malloc_unsigned_int(const unsigned int c,const char *msg) {
     27    unsigned int *m;
     28    if((m = malloc(c*sizeof(unsigned int))) == NULL) {
     29        fprintf(stderr,"%s failed at %s, line %d: %s\n",
     30                msg,__FILE__,__LINE__,strerror(errno));
     31        exit(EXIT_FAILURE);
     32    }
     33
     34    return m;
     35}
     36
     37// Return an array of random number seeds. Epoch time is used as a base, incremented by thread ID
     38unsigned int *init_random_seeds() {
     39    unsigned int i,*seeds;
    2640    time_t t = time(NULL);
    2741
    28     seeds = safe_malloc_int(NUM_THREADS,"Allocating random seed array");
     42    seeds = safe_malloc_unsigned_int(NUM_THREADS,"Allocating random seed array");
    2943
    3044    for(i=0;i<NUM_THREADS;i++) {
     
    3650}
    3751
    38 void init_array(int *m,const int c) {
     52// Takes in an int array of size c, and a pointer to a random seed. Sets a random number in each cell
     53void init_array(int *m,const int c,unsigned int *s) {
    3954    int i;
    4055
    4156    for(i=0;i<c;i++) {
    42        
     57        m[c] = rand_r(s);
    4358    }
    4459}
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.h

    r5687 r5688  
    2020
    2121// Take in a count of objects and a failure message, returns allocated array
    22 int *safe_malloc_int(const int,const char *);
     22int *safe_malloc_int(const unsigned int,const char *);
     23unsigned int *safe_malloc_unsigned_int(const unsigned int,const char *);
    2324// Returns allocated zero'd random seed array
    2425int *init_rand_seeds();
    2526
    26 // Returns an array of random seeds, one per thread
    27 int *init_random_seeds();
     27// Returns an unsigned int array of random seeds, one per thread
     28unsigned int *init_random_seeds();
    2829
    29 // Take in a reference to an allocated array and its size, initializes with random integers
    30 void init_array(int *,const int);
     30// Take in a reference to an allocated array its size, and a random seed. Initializes with random integers
     31void init_array(int *,const int,unsigned int *);
  • bccd-ng/bw-institute/2016/day06/matmul/serial.c

    r5687 r5688  
    66
    77int main(int argc,char **argv) {
    8     int c,m1_rows,m1_cols,m2_rows,m2_cols;
    9     int *m1,*m2,*dst_m,*random_seeds;
     8    int c;
     9    int *m1,*m2,*dst_m;
     10
     11    unsigned int m1_rows,m1_cols,m2_rows,m2_cols,m1_size,m2_size,dst_size,*random_seeds;
    1012
    1113    m1_rows = m1_cols = m2_rows = m2_cols = 0;
     
    3739    }
    3840
    39     m1      = safe_malloc_int(m1_rows*m1_cols,"Allocating first matrix");
    40     m2      = safe_malloc_int(m2_rows*m2_cols,"Allocating second matrix");
    41     dst_m   = safe_malloc_int(m1_rows*m2_cols,"Allocating destination matrix");
     41    m1_size     = m1_rows*m1_cols;
     42    m2_size     = m2_rows*m2_cols;
     43    dst_size    = m1_rows*m2_cols;   
     44
     45    m1          = safe_malloc_int(m1_size,"Allocating first matrix");
     46    m2          = safe_malloc_int(m2_size,"Allocating second matrix");
     47    dst_m       = safe_malloc_int(dst_size,"Allocating destination matrix");
    4248
    4349    random_seeds = init_random_seeds();
    4450
    4551    // Initialize matrices
     52    init_array(m1,m1_size,random_seeds);
     53    init_array(m2,m2_size,random_seeds);
     54    init_array(dst_m,dst_size,random_seeds);
    4655
    4756    exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.