annotate Matrix_Mult.c @ 30:0d7177551bff

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