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