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