Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VReo > Reo__Matrix_Mult
view 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 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 "ParamHelper/Param.h"
16 void
17 initialize_Input_Matrices_Via( Matrix **leftMatrix, Matrix **rightMatrix,
18 ParamBag *paramBag )
19 { char *leftMatrixFileName, *rightMatrixFileName;
20 int leftMatrixRows, leftMatrixCols, rightMatrixRows, rightMatrixCols;
22 ParamStruc *param;
23 param = getParamFromBag( "leftMatrixRows", paramBag );
24 leftMatrixRows = param->intValue;
25 param = getParamFromBag( "leftMatrixCols", paramBag );
26 leftMatrixCols = param->intValue;
27 *leftMatrix = makeMatrix_WithResMat( leftMatrixRows, leftMatrixCols );
29 param = getParamFromBag( "leftMatrixFileName", paramBag );
30 leftMatrixFileName = param->strValue; //no need to copy
31 read_Matrix_From_File( *leftMatrix, leftMatrixFileName );
33 param = getParamFromBag( "rightMatrixRows", paramBag );
34 rightMatrixRows = param->intValue;
35 param = getParamFromBag( "rightMatrixCols", paramBag );
36 rightMatrixCols = param->intValue;
37 *rightMatrix = makeMatrix_WithResMat( rightMatrixRows, rightMatrixCols );
39 param = getParamFromBag( "rightMatrixFileName", paramBag );
40 rightMatrixFileName = param->strValue;
41 read_Matrix_From_File( *rightMatrix, rightMatrixFileName );
42 }
45 void parseLineIntoRow( char *line, float32* row );
48 void
49 read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName )
50 { int row, maxRead, numRows, numCols;
51 float32 *matrixStart;
52 size_t lineSz = 0;
53 FILE *file;
54 char *line = NULL;
56 lineSz = 50000; //max length of line in a matrix data file
57 line = (char *) malloc( lineSz );
58 if( line == NULL ) printf( "no mem for matrix line" );
60 numRows = matrixStruc->numRows;
61 numCols = matrixStruc->numCols;
62 matrixStart = matrixStruc->array;
64 file = fopen( matrixFileName, "r" );
65 if( file == NULL ) { printf( "\nCouldn't open file!!\n"); exit(1);}
66 fseek( file, 0, SEEK_SET );
67 for( row = 0; row < numRows; row++ )
68 {
69 if( feof( file ) ) printf( "file ran out too soon" );
70 maxRead = getline( &line, &lineSz, file );
71 if( maxRead == -1 ) printf( "prob reading mat line");
73 if( *line == '\n') continue; //blank line
74 if( *line == '/' ) continue; //comment line
76 parseLineIntoRow( line, matrixStart + row * numCols );
77 }
78 free( line );
79 }
81 /*This function relies on each line having the proper number of cols. It
82 * doesn't check, nor enforce, so if the file is improperly formatted it
83 * can write over unrelated memory
84 */
85 void
86 parseLineIntoRow( char *line, float32* row )
87 {
88 char *valueStr, *searchPos;
90 //read the float values
91 searchPos = valueStr = line; //start
93 for( ; *searchPos != 0; searchPos++) //bit dangerous, should use buff len
94 {
95 if( *searchPos == '\n' ) //last col.. relying on well-formatted file
96 { *searchPos = 0;
97 *row = atof( valueStr );
98 break; //end FOR loop
99 }
100 if( *searchPos == ',' )
101 { *searchPos = 0; //mark end of string
102 *row = (float32) atof( valueStr );
103 row += 1; //address arith
104 //skip any spaces before digits.. use searchPos + 1 to skip the 0
105 for( ; *(searchPos + 1)== ' ' && *(searchPos + 1) !=0; searchPos++);
106 valueStr = searchPos + 1;
107 }
108 }
109 }
111 //==========================================================================
113 /*In the "_Flat" version of constructor, do only malloc of the top data struc
114 * and set values in that top-level. Don't malloc any sub-structures.
115 */
116 Matrix *
117 makeMatrix_Flat( int32 numRows, int32 numCols )
118 { Matrix * retMatrix;
119 retMatrix = malloc( sizeof( Matrix ) );
120 retMatrix->numRows = numRows;
121 retMatrix->numCols = numCols;
123 return retMatrix;
124 }
126 Matrix *
127 makeMatrix_WithResMat( int32 numRows, int32 numCols )
128 { Matrix * retMatrix;
129 retMatrix = malloc( sizeof( Matrix ) );
130 retMatrix->numRows = numRows;
131 retMatrix->numCols = numCols;
132 retMatrix->array = malloc( numRows * numCols * sizeof(float32) );
134 return retMatrix;
135 }
137 void
138 freeMatrix_Flat( Matrix * matrix )
139 { //( matrix );
140 }
141 void
142 freeMatrix( Matrix * matrix )
143 { free( matrix->array );
144 free( matrix );
145 }
147 void
148 printMatrix( Matrix *matrix )
149 { int r, c, numRows, numCols, rowsToPrint, colsToPrint, rowIncr, colIncr;
150 float32 *matrixArray;
152 numRows = rowsToPrint = matrix->numRows;
153 numCols = colsToPrint = matrix->numCols;
154 matrixArray = matrix->array;
156 rowIncr = numRows/20; if(rowIncr == 0) rowIncr = 1;//20 to 39 rows printed
157 colIncr = numCols/20; if(colIncr == 0) colIncr = 1;//20 to 39 cols printed
158 for( r = 0; r < numRows; r += rowIncr )
159 { for( c = 0; c < numCols; c += colIncr )
160 { printf( "%3.1f | ", matrixArray[ r * numCols + c ] );
161 }
162 printf("\n");
163 }
164 }
