seanhalle@0: /* seanhalle@0: * Copyright 2009 OpenSourceStewardshipFoundation.org seanhalle@0: * Licensed under GNU General Public License version 2 seanhalle@0: * seanhalle@0: * Author: seanhalle@yahoo.com seanhalle@0: * seanhalle@0: * Created on November 15, 2009, 2:35 AM seanhalle@0: */ seanhalle@0: seanhalle@0: #include seanhalle@0: #include seanhalle@0: seanhalle@0: #include "Matrix_Mult.h" seanhalle@0: #include "ParamHelper/Param.h" seanhalle@0: seanhalle@0: void seanhalle@0: initialize_Input_Matrices_Via( Matrix **leftMatrix, Matrix **rightMatrix, seanhalle@0: ParamBag *paramBag ) seanhalle@0: { char *leftMatrixFileName, *rightMatrixFileName; seanhalle@0: int leftMatrixRows, leftMatrixCols, rightMatrixRows, rightMatrixCols; seanhalle@0: seanhalle@0: ParamStruc *param; seanhalle@0: param = getParamFromBag( "leftMatrixRows", paramBag ); seanhalle@0: leftMatrixRows = param->intValue; seanhalle@0: param = getParamFromBag( "leftMatrixCols", paramBag ); seanhalle@0: leftMatrixCols = param->intValue; seanhalle@0: *leftMatrix = makeMatrix_WithResMat( leftMatrixRows, leftMatrixCols ); seanhalle@0: seanhalle@0: param = getParamFromBag( "leftMatrixFileName", paramBag ); seanhalle@0: leftMatrixFileName = param->strValue; //no need to copy seanhalle@0: read_Matrix_From_File( *leftMatrix, leftMatrixFileName ); seanhalle@0: seanhalle@0: param = getParamFromBag( "rightMatrixRows", paramBag ); seanhalle@0: rightMatrixRows = param->intValue; seanhalle@0: param = getParamFromBag( "rightMatrixCols", paramBag ); seanhalle@0: rightMatrixCols = param->intValue; seanhalle@0: *rightMatrix = makeMatrix_WithResMat( rightMatrixRows, rightMatrixCols ); seanhalle@0: seanhalle@0: param = getParamFromBag( "rightMatrixFileName", paramBag ); seanhalle@0: rightMatrixFileName = param->strValue; seanhalle@0: read_Matrix_From_File( *rightMatrix, rightMatrixFileName ); seanhalle@0: } seanhalle@0: seanhalle@0: seanhalle@0: void parseLineIntoRow( char *line, float32* row ); seanhalle@0: seanhalle@0: seanhalle@0: void seanhalle@0: read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName ) seanhalle@0: { int row, maxRead, numRows, numCols; seanhalle@0: float32 *matrixStart; seanhalle@0: size_t lineSz = 0; seanhalle@0: FILE *file; seanhalle@0: char *line = NULL; seanhalle@0: seanhalle@0: lineSz = 50000; //max length of line in a matrix data file seanhalle@0: line = (char *) malloc( lineSz ); seanhalle@0: if( line == NULL ) printf( "no mem for matrix line" ); seanhalle@0: seanhalle@0: numRows = matrixStruc->numRows; seanhalle@0: numCols = matrixStruc->numCols; seanhalle@0: matrixStart = matrixStruc->array; seanhalle@0: seanhalle@0: file = fopen( matrixFileName, "r" ); seanhalle@0: if( file == NULL ) { printf( "\nCouldn't open file!!\n"); exit(1);} seanhalle@0: fseek( file, 0, SEEK_SET ); seanhalle@0: for( row = 0; row < numRows; row++ ) seanhalle@0: { seanhalle@0: if( feof( file ) ) printf( "file ran out too soon" ); seanhalle@0: maxRead = getline( &line, &lineSz, file ); seanhalle@0: if( maxRead == -1 ) printf( "prob reading mat line"); seanhalle@0: seanhalle@0: if( *line == '\n') continue; //blank line seanhalle@0: if( *line == '/' ) continue; //comment line seanhalle@0: seanhalle@0: parseLineIntoRow( line, matrixStart + row * numCols ); seanhalle@0: } seanhalle@0: free( line ); seanhalle@0: } seanhalle@0: seanhalle@0: /*This function relies on each line having the proper number of cols. It seanhalle@0: * doesn't check, nor enforce, so if the file is improperly formatted it seanhalle@0: * can write over unrelated memory seanhalle@0: */ seanhalle@0: void seanhalle@0: parseLineIntoRow( char *line, float32* row ) seanhalle@0: { seanhalle@0: char *valueStr, *searchPos; seanhalle@0: seanhalle@0: //read the float values seanhalle@0: searchPos = valueStr = line; //start seanhalle@0: seanhalle@0: for( ; *searchPos != 0; searchPos++) //bit dangerous, should use buff len seanhalle@0: { seanhalle@0: if( *searchPos == '\n' ) //last col.. relying on well-formatted file seanhalle@0: { *searchPos = 0; seanhalle@0: *row = atof( valueStr ); seanhalle@0: break; //end FOR loop seanhalle@0: } seanhalle@0: if( *searchPos == ',' ) seanhalle@0: { *searchPos = 0; //mark end of string seanhalle@0: *row = (float32) atof( valueStr ); seanhalle@0: row += 1; //address arith seanhalle@0: //skip any spaces before digits.. use searchPos + 1 to skip the 0 seanhalle@0: for( ; *(searchPos + 1)== ' ' && *(searchPos + 1) !=0; searchPos++); seanhalle@0: valueStr = searchPos + 1; seanhalle@0: } seanhalle@0: } seanhalle@0: } seanhalle@0: seanhalle@0: //========================================================================== seanhalle@0: seanhalle@0: /*In the "_Flat" version of constructor, do only malloc of the top data struc seanhalle@0: * and set values in that top-level. Don't malloc any sub-structures. seanhalle@0: */ seanhalle@0: Matrix * seanhalle@0: makeMatrix_Flat( int32 numRows, int32 numCols ) seanhalle@0: { Matrix * retMatrix; seanhalle@0: retMatrix = malloc( sizeof( Matrix ) ); seanhalle@0: retMatrix->numRows = numRows; seanhalle@0: retMatrix->numCols = numCols; seanhalle@0: seanhalle@0: return retMatrix; seanhalle@0: } seanhalle@0: seanhalle@0: Matrix * seanhalle@0: makeMatrix_WithResMat( int32 numRows, int32 numCols ) seanhalle@0: { Matrix * retMatrix; seanhalle@0: retMatrix = malloc( sizeof( Matrix ) ); seanhalle@0: retMatrix->numRows = numRows; seanhalle@0: retMatrix->numCols = numCols; seanhalle@0: retMatrix->array = malloc( numRows * numCols * sizeof(float32) ); seanhalle@0: seanhalle@0: return retMatrix; seanhalle@0: } seanhalle@0: seanhalle@0: void seanhalle@0: freeMatrix_Flat( Matrix * matrix ) seanhalle@0: { //( matrix ); seanhalle@0: } seanhalle@0: void seanhalle@0: freeMatrix( Matrix * matrix ) seanhalle@0: { free( matrix->array ); seanhalle@0: free( matrix ); seanhalle@0: } seanhalle@0: seanhalle@0: void seanhalle@0: printMatrix( Matrix *matrix ) seanhalle@0: { int r, c, numRows, numCols, rowsToPrint, colsToPrint, rowIncr, colIncr; seanhalle@0: float32 *matrixArray; seanhalle@0: seanhalle@0: numRows = rowsToPrint = matrix->numRows; seanhalle@0: numCols = colsToPrint = matrix->numCols; seanhalle@0: matrixArray = matrix->array; seanhalle@0: seanhalle@0: rowIncr = numRows/20; if(rowIncr == 0) rowIncr = 1;//20 to 39 rows printed seanhalle@0: colIncr = numCols/20; if(colIncr == 0) colIncr = 1;//20 to 39 cols printed seanhalle@0: for( r = 0; r < numRows; r += rowIncr ) seanhalle@0: { for( c = 0; c < numCols; c += colIncr ) seanhalle@0: { printf( "%3.1f | ", matrixArray[ r * numCols + c ] ); seanhalle@0: } seanhalle@0: printf("\n"); seanhalle@0: } seanhalle@0: } seanhalle@0: