view Matrix_Mult.c @ 29:8972c00c00dd

Merge with perf_tuning_paper branch, which grabs fixes and best performing version
author Sean Halle <seanhalle@yahoo.com>
date Sun, 15 Jul 2012 01:27:39 -0700
parents 387f3084d9bb
children
line source
1 /*
2 * Copyright 2009 OpenSourceStewardshipFoundation.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 * Created on November 15, 2009, 2:35 AM
8 */
10 #include <malloc.h>
11 #include <stdlib.h>
13 #include "Matrix_Mult.h"
14 #include "../C_Libraries/ParamHelper/Param.h"
18 void
19 initialize_Input_Matrices_Via( Matrix **leftMatrix, Matrix **rightMatrix,
20 ParamBag *paramBag )
21 { char *leftMatrixFileName, *rightMatrixFileName;
22 int leftMatrixRows, leftMatrixCols, rightMatrixRows, rightMatrixCols;
24 ParamStruc *param;
25 param = getParamFromBag( "leftMatrixRows", paramBag );
26 leftMatrixRows = param->intValue;
27 param = getParamFromBag( "leftMatrixCols", paramBag );
28 leftMatrixCols = param->intValue;
29 *leftMatrix = makeMatrix_WithResMat( leftMatrixRows, leftMatrixCols );
31 param = getParamFromBag( "leftMatrixFileName", paramBag );
32 leftMatrixFileName = param->strValue; //no need to copy
33 read_Matrix_From_File( *leftMatrix, leftMatrixFileName );
35 param = getParamFromBag( "rightMatrixRows", paramBag );
36 rightMatrixRows = param->intValue;
37 param = getParamFromBag( "rightMatrixCols", paramBag );
38 rightMatrixCols = param->intValue;
39 *rightMatrix = makeMatrix_WithResMat( rightMatrixRows, rightMatrixCols );
41 param = getParamFromBag( "rightMatrixFileName", paramBag );
42 rightMatrixFileName = param->strValue;
43 read_Matrix_From_File( *rightMatrix, rightMatrixFileName );
44 }
47 void parseLineIntoRow( char *line, float32* row );
50 void
51 read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName )
52 { int row, maxRead, numRows, numCols;
53 float32 *matrixStart;
54 size_t lineSz = 0;
55 FILE *file;
56 char *line = NULL;
58 lineSz = 50000; //max length of line in a matrix data file
59 line = (char *) malloc( lineSz );
60 if( line == NULL ) printf( "no mem for matrix line" );
62 numRows = matrixStruc->numRows;
63 numCols = matrixStruc->numCols;
64 matrixStart = matrixStruc->array;
66 file = fopen( matrixFileName, "r" );
67 if( file == NULL ) { printf( "\nCouldn't open file!!\n"); exit(1);}
68 fseek( file, 0, SEEK_SET );
69 for( row = 0; row < numRows; row++ )
70 {
71 if( feof( file ) ) printf( "file ran out too soon" );
72 maxRead = getline( &line, &lineSz, file );
73 if( maxRead == -1 ) printf( "prob reading mat line");
75 if( *line == '\n') continue; //blank line
76 if( *line == '/' ) continue; //comment line
78 parseLineIntoRow( line, matrixStart + row * numCols );
79 }
80 free( line );
81 }
83 /*This function relies on each line having the proper number of cols. It
84 * doesn't check, nor enforce, so if the file is improperly formatted it
85 * can write over unrelated memory
86 */
87 void
88 parseLineIntoRow( char *line, float32* row )
89 {
90 char *valueStr, *searchPos;
92 //read the float values
93 searchPos = valueStr = line; //start
95 for( ; *searchPos != 0; searchPos++) //bit dangerous, should use buff len
96 {
97 if( *searchPos == '\n' ) //last col.. relying on well-formatted file
98 { *searchPos = 0;
99 *row = atof( valueStr );
100 break; //end FOR loop
101 }
102 if( *searchPos == ',' )
103 { *searchPos = 0; //mark end of string
104 *row = (float32) atof( valueStr );
105 row += 1; //address arith
106 //skip any spaces before digits.. use searchPos + 1 to skip the 0
107 for( ; *(searchPos + 1)== ' ' && *(searchPos + 1) !=0; searchPos++);
108 valueStr = searchPos + 1;
109 }
110 }
111 }
113 //==========================================================================
115 /*In the "_Flat" version of constructor, do only malloc of the top data struc
116 * and set values in that top-level. Don't malloc any sub-structures.
117 */
118 Matrix *
119 makeMatrix_Flat( int32 numRows, int32 numCols )
120 { Matrix * retMatrix;
121 retMatrix = malloc( sizeof( Matrix ) );
122 retMatrix->numRows = numRows;
123 retMatrix->numCols = numCols;
125 return retMatrix;
126 }
128 Matrix *
129 makeMatrix_WithResMat( int32 numRows, int32 numCols )
130 { Matrix * retMatrix;
131 retMatrix = malloc( sizeof( Matrix ) );
132 retMatrix->numRows = numRows;
133 retMatrix->numCols = numCols;
134 retMatrix->array = malloc( numRows * numCols * sizeof(float32) );
136 return retMatrix;
137 }
139 void
140 freeMatrix_Flat( Matrix * matrix )
141 { //( matrix );
142 }
143 void
144 freeMatrix( Matrix * matrix )
145 { free( matrix->array );
146 free( matrix );
147 }
149 void
150 printMatrix( Matrix *matrix )
151 { int r, c, numRows, numCols, rowsToPrint, colsToPrint, rowIncr, colIncr;
152 float32 *matrixArray;
154 numRows = rowsToPrint = matrix->numRows;
155 numCols = colsToPrint = matrix->numCols;
156 matrixArray = matrix->array;
158 rowIncr = numRows/20; if(rowIncr == 0) rowIncr = 1;//20 to 39 rows printed
159 colIncr = numCols/20; if(colIncr == 0) colIncr = 1;//20 to 39 cols printed
160 for( r = 0; r < numRows; r += rowIncr )
161 { for( c = 0; c < numCols; c += colIncr )
162 { printf( "%3.1f | ", matrixArray[ r * numCols + c ] );
163 }
164 printf("\n");
165 }
166 }