diff Matrix_Mult.c @ 0:c4b1849c05ef

init add
author Sean Halle <seanhalle@yahoo.com>
date Sun, 02 Feb 2014 17:58:41 -0800
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Matrix_Mult.c	Sun Feb 02 17:58:41 2014 -0800
     1.3 @@ -0,0 +1,165 @@
     1.4 +/*
     1.5 + *  Copyright 2009 OpenSourceStewardshipFoundation.org
     1.6 + *  Licensed under GNU General Public License version 2
     1.7 + *
     1.8 + * Author: seanhalle@yahoo.com
     1.9 + *
    1.10 + * Created on November 15, 2009, 2:35 AM
    1.11 + */
    1.12 +
    1.13 +#include <malloc.h>
    1.14 +#include <stdlib.h>
    1.15 +
    1.16 +#include "Matrix_Mult.h"
    1.17 +#include "ParamHelper/Param.h"
    1.18 + 
    1.19 +void
    1.20 +initialize_Input_Matrices_Via( Matrix  **leftMatrix, Matrix **rightMatrix,
    1.21 +                               ParamBag *paramBag )
    1.22 + { char *leftMatrixFileName, *rightMatrixFileName;
    1.23 +   int   leftMatrixRows, leftMatrixCols, rightMatrixRows, rightMatrixCols;
    1.24 +   
    1.25 +      ParamStruc *param;
    1.26 +      param = getParamFromBag( "leftMatrixRows", paramBag );
    1.27 +   leftMatrixRows = param->intValue;
    1.28 +      param = getParamFromBag( "leftMatrixCols", paramBag );
    1.29 +   leftMatrixCols = param->intValue;
    1.30 +   *leftMatrix = makeMatrix_WithResMat( leftMatrixRows, leftMatrixCols );
    1.31 +   
    1.32 +      param = getParamFromBag( "leftMatrixFileName", paramBag );
    1.33 +   leftMatrixFileName = param->strValue;  //no need to copy
    1.34 +   read_Matrix_From_File( *leftMatrix,  leftMatrixFileName );
    1.35 +   
    1.36 +      param = getParamFromBag( "rightMatrixRows", paramBag );
    1.37 +   rightMatrixRows = param->intValue;
    1.38 +      param = getParamFromBag( "rightMatrixCols", paramBag );
    1.39 +   rightMatrixCols = param->intValue;
    1.40 +   *rightMatrix = makeMatrix_WithResMat( rightMatrixRows, rightMatrixCols );
    1.41 +   
    1.42 +      param = getParamFromBag( "rightMatrixFileName", paramBag );
    1.43 +   rightMatrixFileName = param->strValue;
    1.44 +   read_Matrix_From_File( *rightMatrix, rightMatrixFileName );
    1.45 + }
    1.46 +
    1.47 +
    1.48 +void parseLineIntoRow( char *line, float32* row );
    1.49 +
    1.50 +
    1.51 + void
    1.52 +read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName )
    1.53 + { int    row, maxRead, numRows, numCols;
    1.54 +   float32 *matrixStart;
    1.55 +   size_t lineSz = 0;
    1.56 +   FILE  *file;
    1.57 +   char  *line = NULL;
    1.58 +   
    1.59 +   lineSz = 50000; //max length of line in a matrix data file
    1.60 +   line = (char *) malloc( lineSz );
    1.61 +   if( line == NULL ) printf( "no mem for matrix line" );
    1.62 +   
    1.63 +   numRows = matrixStruc->numRows;
    1.64 +   numCols = matrixStruc->numCols;
    1.65 +   matrixStart = matrixStruc->array;
    1.66 +
    1.67 +   file = fopen( matrixFileName, "r" );
    1.68 +   if( file == NULL ) { printf( "\nCouldn't open file!!\n"); exit(1);}
    1.69 +   fseek( file, 0, SEEK_SET );
    1.70 +   for( row = 0; row < numRows; row++ )
    1.71 +    {
    1.72 +      if( feof( file ) )  printf( "file ran out too soon" );
    1.73 +      maxRead = getline( &line, &lineSz, file );
    1.74 +      if( maxRead == -1 ) printf( "prob reading mat line");
    1.75 +      
    1.76 +      if( *line == '\n') continue; //blank line
    1.77 +      if( *line == '/' ) continue; //comment line
    1.78 +      
    1.79 +      parseLineIntoRow( line, matrixStart + row * numCols );
    1.80 +    }
    1.81 +   free( line );
    1.82 + }
    1.83 +
    1.84 +/*This function relies on each line having the proper number of cols.  It
    1.85 + * doesn't check, nor enforce, so if the file is improperly formatted it
    1.86 + * can write over unrelated memory
    1.87 + */
    1.88 + void
    1.89 +parseLineIntoRow( char *line, float32* row )
    1.90 + {
    1.91 +   char *valueStr, *searchPos;
    1.92 +   
    1.93 +      //read the float values
    1.94 +   searchPos = valueStr = line; //start
    1.95 +   
    1.96 +   for( ; *searchPos != 0; searchPos++)  //bit dangerous, should use buff len
    1.97 +    {
    1.98 +      if( *searchPos == '\n' ) //last col..  relying on well-formatted file
    1.99 +       { *searchPos = 0;
   1.100 +         *row = atof( valueStr );
   1.101 +         break;                                    //end FOR loop
   1.102 +       }
   1.103 +      if( *searchPos == ',' )
   1.104 +       { *searchPos = 0;                           //mark end of string
   1.105 +         *row = (float32) atof( valueStr );
   1.106 +         row += 1;                                 //address arith
   1.107 +            //skip any spaces before digits.. use searchPos + 1 to skip the 0
   1.108 +         for( ; *(searchPos + 1)== ' ' && *(searchPos + 1) !=0; searchPos++);
   1.109 +         valueStr = searchPos + 1;
   1.110 +       }
   1.111 +    }
   1.112 + }
   1.113 +
   1.114 + //==========================================================================
   1.115 +
   1.116 +/*In the "_Flat" version of constructor, do only malloc of the top data struc
   1.117 + * and set values in that top-level.  Don't malloc any sub-structures.
   1.118 + */
   1.119 + Matrix *
   1.120 +makeMatrix_Flat( int32 numRows, int32 numCols )
   1.121 + { Matrix * retMatrix;
   1.122 +   retMatrix = malloc( sizeof( Matrix ) );
   1.123 +   retMatrix->numRows = numRows;
   1.124 +   retMatrix->numCols = numCols;
   1.125 +
   1.126 +   return retMatrix;
   1.127 + }
   1.128 +
   1.129 + Matrix *
   1.130 +makeMatrix_WithResMat( int32 numRows, int32 numCols )
   1.131 + { Matrix * retMatrix;
   1.132 +   retMatrix = malloc( sizeof( Matrix ) );
   1.133 +   retMatrix->numRows = numRows;
   1.134 +   retMatrix->numCols = numCols;
   1.135 +   retMatrix->array  = malloc( numRows * numCols * sizeof(float32) );
   1.136 +
   1.137 +   return retMatrix;
   1.138 + }
   1.139 +
   1.140 + void
   1.141 +freeMatrix_Flat( Matrix * matrix )
   1.142 + { //( matrix );
   1.143 + }
   1.144 + void
   1.145 +freeMatrix( Matrix * matrix )
   1.146 + { free( matrix->array );
   1.147 +   free( matrix );
   1.148 + }
   1.149 +
   1.150 +void
   1.151 +printMatrix( Matrix *matrix )
   1.152 + { int r, c, numRows, numCols, rowsToPrint, colsToPrint, rowIncr, colIncr;
   1.153 +   float32 *matrixArray;
   1.154 +
   1.155 +   numRows = rowsToPrint = matrix->numRows;
   1.156 +   numCols = colsToPrint = matrix->numCols;
   1.157 +   matrixArray = matrix->array;
   1.158 +
   1.159 +   rowIncr = numRows/20; if(rowIncr == 0) rowIncr = 1;//20 to 39 rows printed
   1.160 +   colIncr = numCols/20; if(colIncr == 0) colIncr = 1;//20 to 39 cols printed
   1.161 +   for( r = 0; r < numRows; r += rowIncr )
   1.162 +    { for( c = 0; c < numCols; c += colIncr )
   1.163 +       { printf( "%3.1f | ", matrixArray[ r * numCols + c ] );
   1.164 +       }
   1.165 +      printf("\n");
   1.166 +    }
   1.167 + }
   1.168 +