annotate Matrix_Mult.c @ 3:a6b3cab179b1

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