Changeset 5687 in /cluster/svnroot


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

simpler random number generation

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

Legend:

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

    r5686 r5687  
    99}
    1010
     11// Take in an integer c and a message to print on failure
     12// Allocate an array of c ints and return it
    1113int *safe_malloc_int(const int c,const char *msg) {
    1214    int *m;
     
    2022}
    2123
    22 struct random_data *safe_malloc_random_data(const int c,const char *msg) {
    23     struct random_data *m;
    24     if((m = malloc(c*sizeof(struct random_data))) == NULL) {
    25         fprintf(stderr,"%s failed at %s, line %d: %s\n",
    26                 msg,__FILE__,__LINE__,strerror(errno));
    27         exit(EXIT_FAILURE);
     24int *init_random_seeds() {
     25    int i,*seeds;
     26    time_t t = time(NULL);
     27
     28    seeds = safe_malloc_int(NUM_THREADS,"Allocating random seed array");
     29
     30    for(i=0;i<NUM_THREADS;i++) {
     31        // Guarantee different starting seeds for random numbers in each thread
     32        seeds[i] = t+i;
    2833    }
    2934
    30     return m;
    31 }
    32 
    33 struct random_data *init_random_bufs() {
    34     struct random_data *random_bufs;
    35 
    36     random_bufs = safe_malloc_random_data(NUM_THREADS,"Allocating random data array");
    37 
    38     return random_bufs;
     35    return seeds;
    3936}
    4037
     
    4340
    4441    for(i=0;i<c;i++) {
     42       
    4543    }
    4644}
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.h

    r5686 r5687  
    33 */
    44
    5 #include <stdlib.h>
    6 #include <stdio.h>
    7 #include <unistd.h>
    8 #include <string.h>
    9 #include <errno.h>
    10 #include <getopt.h>
     5#include <stdlib.h> // malloc(3), calloc(3)
     6#include <stdio.h> // printf(3)
     7#include <unistd.h> // exit(2)
     8#include <string.h> // strerror(3)
     9#include <errno.h> // errno mappings
     10#include <getopt.h> // getopt(3)
     11#include <time.h> // time(2)
    1112
    1213#ifdef OPENMP
     
    2021// Take in a count of objects and a failure message, returns allocated array
    2122int *safe_malloc_int(const int,const char *);
    22 struct random_data *safe_malloc_random_data(const int,const char *);
     23// Returns allocated zero'd random seed array
     24int *init_rand_seeds();
    2325
    24 // Returns an array of size threads of initialized random_r buffers
    25 struct random_data *init_random_bufs();
     26// Returns an array of random seeds, one per thread
     27int *init_random_seeds();
    2628
    2729// Take in a reference to an allocated array and its size, initializes with random integers
  • bccd-ng/bw-institute/2016/day06/matmul/serial.c

    r5686 r5687  
    77int main(int argc,char **argv) {
    88    int c,m1_rows,m1_cols,m2_rows,m2_cols;
    9     int *m1,*m2,*dst_m;
    10     struct random_data *random_bufs;
     9    int *m1,*m2,*dst_m,*random_seeds;
    1110
    1211    m1_rows = m1_cols = m2_rows = m2_cols = 0;
     
    4241    dst_m   = safe_malloc_int(m1_rows*m2_cols,"Allocating destination matrix");
    4342
    44     // Initialize random state
    45     random_bufs = safe_malloc_random_data(NUM_THREADS,"Allocating random data array");
     43    random_seeds = init_random_seeds();
     44
     45    // Initialize matrices
    4646
    4747    exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.