Changeset 5691 in /cluster/svnroot


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

use struct for function simplicity

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

Legend:

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

    r5689 r5691  
    5050}
    5151
    52 // Takes in an int array of size c, and a pointer to a random seed. Sets a random number in each cell
    53 void init_array(int *m,const int c,unsigned int *s) {
     52// Takes in a matrix, and a pointer to an array of random seeds.
     53// Sets a random number in each cell
     54void init_matrix(struct matrix *m,unsigned int *s) {
    5455    int i,t;
    5556
     
    5758#pragma omp parallel
    5859    {
     60        // Initialize t to thread number if using OpenMP
    5961    t = omp_get_thread_num();
    6062#pragma omp for
    6163#else
     64    // When not using OpenMP, only one "thread" will be running
    6265    t = 1;
    6366#endif
    64     for(i=0;i<c;i++) {
    65         m[c] = rand_r(&s[t]);
     67    for(i=0;i<m->rows*m->cols;i++) {
     68        m->matrix[i] = rand_r(&s[t]);
    6669    }
    6770#ifdef _OPENMP
  • bccd-ng/bw-institute/2016/day06/matmul/matmul.h

    r5689 r5691  
    22 * $Id$
    33 */
     4
     5#ifndef MATMUL_H
     6#define MATMUL_H
    47
    58#include <stdlib.h> // malloc(3), calloc(3)
     
    1821#endif
    1922
     23struct matrix {
     24    int *matrix;
     25    unsigned int rows;
     26    unsigned int cols;
     27};
     28
    2029void usage();
    2130
     
    3039
    3140// Take in a reference to an allocated array its size, and a random seed. Initializes with random integers
    32 void init_array(int *,const int,unsigned int *);
     41void init_matrix(struct matrix *,unsigned int *);
     42
     43// Takes in a reference to two source matrices and their sizes, and a destination matrix and its size
     44void matmul(int *,unsigned int,int *,unsigned int,int *,unsigned int);
     45
     46#endif
  • bccd-ng/bw-institute/2016/day06/matmul/serial-omp.c

    r5689 r5691  
    77int main(int argc,char **argv) {
    88    int c;
    9     int *m1,*m2,*dst_m;
     9    struct matrix m1,m2,dst_m;
    1010
    11     unsigned int m1_rows,m1_cols,m2_rows,m2_cols,m1_size,m2_size,dst_size,*random_seeds;
     11    unsigned int *random_seeds;
    1212
    13     m1_rows = m1_cols = m2_rows = m2_cols = 0;
     13    m1.rows = m1.cols = m2.rows = m2.cols = 0;
    1414
    1515    while((c = getopt(argc,argv, "x:y:a:b:")) != -1) {
    1616        switch(c) {
    1717            case 'x':
    18                 m1_cols = atoi(optarg);
     18                m1.cols = atoi(optarg);
    1919                break;
    2020            case 'y':
    21                 m1_rows = atoi(optarg);
     21                m1.rows = atoi(optarg);
    2222                break;
    2323            case 'a':
    24                 m2_cols = atoi(optarg);
     24                m2.cols = atoi(optarg);
    2525                break;
    2626            case 'b':
    27                 m2_rows = atoi(optarg);
     27                m2.rows = atoi(optarg);
    2828                break;
    2929            case '?':
     
    3333    }
    3434
    35     if(m1_rows == 0 || m1_cols == 0 || m2_rows == 0 || m2_cols == 0) {
     35    if(m1.rows == 0 || m1.cols == 0 || m2.rows == 0 || m2.cols == 0) {
    3636        fprintf(stderr,"Supply row and column counts!\n");
    3737        usage();
     
    3939    }
    4040
    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");
     41    m1.matrix       = safe_malloc_int(m1.rows*m1.cols,"Allocating first matrix");
     42    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");
    4844
    4945    random_seeds = init_random_seeds();
    5046
    51     // 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);
     47    // Initialize source matrices
     48    init_matrix(&m1,random_seeds);
     49    init_matrix(&m2,random_seeds);
     50
     51    // Matrix computation
    5552
    5653    exit(EXIT_SUCCESS);
Note: See TracChangeset for help on using the changeset viewer.