Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > pthread > pthread__Matrix_Mult__Bench
changeset 0:e48f13e138b0 tip
Initial add -- copy from VPThread version that works on 9x9
| author | Me |
|---|---|
| date | Fri, 17 Sep 2010 11:39:05 -0700 |
| parents | |
| children | |
| files | src/Application/Matrix_Mult.c src/Application/Matrix_Mult.h src/Application/VPThread__Matrix_Mult/Divide_Pr.c src/Application/VPThread__Matrix_Mult/EntryPoint.c src/Application/VPThread__Matrix_Mult/Result_Pr.c src/Application/VPThread__Matrix_Mult/VPThread__Matrix_Mult.h src/Application/VPThread__Matrix_Mult/Vector_Pr.c src/Application/main.c |
| diffstat | 8 files changed, 661 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/Application/Matrix_Mult.c Fri Sep 17 11:39:05 2010 -0700 1.3 @@ -0,0 +1,165 @@ 1.4 +/* 1.5 + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 1.6 + * Licensed under GNU General Public License version 2 1.7 + * 1.8 + * Author: seanhalle@yahoo.com 1.9 + * 1.10 + * Created on November 15, 2009, 2:35 AM 1.11 + */ 1.12 + 1.13 +#include <malloc.h> 1.14 +#include <stdlib.h> 1.15 + 1.16 +#include "Matrix_Mult.h" 1.17 +#include "ParamHelper/Param.h" 1.18 + 1.19 + 1.20 + 1.21 + void 1.22 +initialize_Input_Matrices_Via( Matrix **leftMatrix, Matrix **rightMatrix, 1.23 + ParamBag *paramBag ) 1.24 + { char *leftMatrixFileName, *rightMatrixFileName; 1.25 + int leftMatrixRows, leftMatrixCols, rightMatrixRows, rightMatrixCols; 1.26 + 1.27 + ParamStruc *param; 1.28 + param = getParamFromBag( "leftMatrixRows", paramBag ); 1.29 + leftMatrixRows = param->intValue; 1.30 + param = getParamFromBag( "leftMatrixCols", paramBag ); 1.31 + leftMatrixCols = param->intValue; 1.32 + *leftMatrix = makeMatrix_WithResMat( leftMatrixRows, leftMatrixCols ); 1.33 + 1.34 + param = getParamFromBag( "leftMatrixFileName", paramBag ); 1.35 + leftMatrixFileName = param->strValue; //no need to copy 1.36 + read_Matrix_From_File( *leftMatrix, leftMatrixFileName ); 1.37 + 1.38 + param = getParamFromBag( "rightMatrixRows", paramBag ); 1.39 + rightMatrixRows = param->intValue; 1.40 + param = getParamFromBag( "rightMatrixCols", paramBag ); 1.41 + rightMatrixCols = param->intValue; 1.42 + *rightMatrix = makeMatrix_WithResMat( rightMatrixRows, rightMatrixCols ); 1.43 + 1.44 + param = getParamFromBag( "rightMatrixFileName", paramBag ); 1.45 + rightMatrixFileName = param->strValue; 1.46 + read_Matrix_From_File( *rightMatrix, rightMatrixFileName ); 1.47 + } 1.48 + 1.49 + 1.50 +void parseLineIntoRow( char *line, float32* row ); 1.51 + 1.52 + 1.53 + void 1.54 +read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName ) 1.55 + { int row, maxRead, numRows, numCols; 1.56 + float32 *matrixStart; 1.57 + size_t lineSz = 0; 1.58 + FILE *file; 1.59 + char *line = NULL; 1.60 + 1.61 + lineSz = 50000; //max length of line in a matrix data file 1.62 + line = (char *) malloc( lineSz ); 1.63 + if( line == NULL ) printf( "no mem for matrix line" ); 1.64 + 1.65 + numRows = matrixStruc->numRows; 1.66 + numCols = matrixStruc->numCols; 1.67 + matrixStart = matrixStruc->matrix; 1.68 + 1.69 + file = fopen( matrixFileName, "r" ); 1.70 + if( file == NULL ) { printf( "\nCouldn't open file!!\n"); exit(1);} 1.71 + fseek( file, 0, SEEK_SET ); 1.72 + for( row = 0; row < numRows; row++ ) 1.73 + { 1.74 + if( feof( file ) ) printf( "file ran out too soon" ); 1.75 + maxRead = getline( &line, &lineSz, file ); 1.76 + if( maxRead == -1 ) printf( "prob reading mat line"); 1.77 + 1.78 + if( *line == '\n') continue; //blank line 1.79 + if( *line == '/' ) continue; //comment line 1.80 + 1.81 + parseLineIntoRow( line, matrixStart + row * numCols ); 1.82 + } 1.83 + free( line ); 1.84 + } 1.85 + 1.86 +/*This function relies on each line having the proper number of cols. It 1.87 + * doesn't check, nor enforce, so if the file is improperly formatted it 1.88 + * can write over unrelated memory 1.89 + */ 1.90 + void 1.91 +parseLineIntoRow( char *line, float32* row ) 1.92 + { 1.93 + char *valueStr, *searchPos; 1.94 + 1.95 + //read the float values 1.96 + searchPos = valueStr = line; //start 1.97 + 1.98 + for( ; *searchPos != 0; searchPos++) //bit dangerous, should use buff len 1.99 + { 1.100 + if( *searchPos == '\n' ) //last col.. relying on well-formatted file 1.101 + { *searchPos = 0; 1.102 + *row = atof( valueStr ); 1.103 + break; //end FOR loop 1.104 + } 1.105 + if( *searchPos == ',' ) 1.106 + { *searchPos = 0; //mark end of string 1.107 + *row = (float32) atof( valueStr ); 1.108 + row += 1; //address arith 1.109 + //skip any spaces before digits.. use searchPos + 1 to skip the 0 1.110 + for( ; *(searchPos + 1)== ' ' && *(searchPos + 1) !=0; searchPos++); 1.111 + valueStr = searchPos + 1; 1.112 + } 1.113 + } 1.114 + } 1.115 + 1.116 + //========================================================================== 1.117 + 1.118 +/*In the "_Flat" version of constructor, do only malloc of the top data struc 1.119 + * and set values in that top-level. Don't malloc any sub-structures. 1.120 + */ 1.121 + Matrix * 1.122 +makeMatrix_Flat( int32 numRows, int32 numCols ) 1.123 + { Matrix * retMatrix; 1.124 + retMatrix = malloc( sizeof( Matrix ) ); 1.125 + retMatrix->numRows = numRows; 1.126 + retMatrix->numCols = numCols; 1.127 + 1.128 + return retMatrix; 1.129 + } 1.130 + 1.131 + Matrix * 1.132 +makeMatrix_WithResMat( int32 numRows, int32 numCols ) 1.133 + { Matrix * retMatrix; 1.134 + retMatrix = malloc( sizeof( Matrix ) ); 1.135 + retMatrix->numRows = numRows; 1.136 + retMatrix->numCols = numCols; 1.137 + retMatrix->matrix = malloc( numRows * numCols * sizeof(float32) ); 1.138 + 1.139 + return retMatrix; 1.140 + } 1.141 + 1.142 + void 1.143 +freeMatrix_Flat( Matrix * matrix ) 1.144 + { //( matrix ); 1.145 + } 1.146 + void 1.147 +freeMatrix( Matrix * matrix ) 1.148 + { free( matrix->matrix ); 1.149 + free( matrix ); 1.150 + } 1.151 + 1.152 +void 1.153 +printMatrix( Matrix *matrix ) 1.154 + { int r, c, numRows, numCols; 1.155 + float32 *matrixArray; 1.156 + 1.157 + numRows = matrix->numRows; 1.158 + numCols = matrix->numCols; 1.159 + matrixArray = matrix->matrix; 1.160 + 1.161 + for( r = 0; r < numRows; r++ ) 1.162 + { for( c = 0; c < numCols; c++ ) 1.163 + { printf( "%f | ", *(matrixArray + r*numCols + c) ); 1.164 + } 1.165 + printf("\n"); 1.166 + } 1.167 + } 1.168 +
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/Application/Matrix_Mult.h Fri Sep 17 11:39:05 2010 -0700 2.3 @@ -0,0 +1,77 @@ 2.4 +/* 2.5 + * Copyright Oct 24, 2009 OpenSourceCodeStewardshipFoundation.org 2.6 + * Licensed under GNU General Public License version 2 2.7 + */ 2.8 + 2.9 +#ifndef MATRIX_MULT_H_ 2.10 +#define MATRIX_MULT_H_ 2.11 + 2.12 +#include <stdio.h> 2.13 +#include <unistd.h> 2.14 +#include <malloc.h> 2.15 + 2.16 +#include "../VPThread__lib/VMS/VMS_primitive_data_types.h" 2.17 +#include "ParamHelper/Param.h" 2.18 + 2.19 +//============================== Structures ============================== 2.20 + 2.21 +typedef 2.22 +struct 2.23 + { int32 numRows; 2.24 + int32 numCols; 2.25 + float32 *matrix; //2D, but dynamically sized, so use addr arith 2.26 + } 2.27 +Matrix; 2.28 + 2.29 +/* This is the "appSpecificPiece" that is carried inside a DKUPiece. 2.30 + * In the DKUPiece data struc it is declared to be of type "void *". This 2.31 + * allows the application to define any data structure it wants and put it 2.32 + * into a DKUPiece. 2.33 + * When the app specific info is used, it is in app code, so it is cast to 2.34 + * the correct type to tell the compiler how to access fields. 2.35 + * This keeps all app-specific things out of the DKU directory, as per the 2.36 + * DKU standard. */ 2.37 +typedef 2.38 +struct 2.39 + { 2.40 + // pointers to shared data.. the result matrix must be created when the 2.41 + // left and right matrices are put into the root ancestor DKUPiece. 2.42 + Matrix * leftMatrix; 2.43 + Matrix * rightMatrix; 2.44 + Matrix * resultMatrix; 2.45 + 2.46 + // define the starting and ending boundaries for this piece of the 2.47 + // result matrix. These are derivable from the left and right 2.48 + // matrices, but included them for readability of code. 2.49 + int prodStartRow, prodEndRow; 2.50 + int prodStartCol, prodEndCol; 2.51 + // Start and end of the portion of the left matrix that contributes to 2.52 + // this piece of the product 2.53 + int leftStartRow, leftEndRow; 2.54 + int leftStartCol, leftEndCol; 2.55 + // Start and end of the portion of the right matrix that contributes to 2.56 + // this piece of the product 2.57 + int rightStartRow, rightEndRow; 2.58 + int rightStartCol, rightEndCol; 2.59 + } 2.60 +MatrixProdPiece; 2.61 + 2.62 +//============================== Functions ================================ 2.63 +void readFile(); 2.64 + 2.65 +Matrix *makeMatrix( int32 numRows, int32 numCols ); 2.66 +Matrix *makeMatrix_Flat( int32 numRows, int32 numCols ); 2.67 +Matrix *makeMatrix_WithResMat( int32 numRows, int32 numCols ); 2.68 +void freeMatrix_Flat( Matrix * matrix ); 2.69 +void freeMatrix( Matrix * matrix ); 2.70 +void printMatrix( Matrix *matrix ); 2.71 + 2.72 +void read_Matrix_From_File( Matrix *matrixStruc, char *matrixFileName ); 2.73 + 2.74 +void 2.75 +initialize_Input_Matrices_Via( Matrix **leftMatrix, Matrix **rightMatrix, 2.76 + ParamBag *paramBag ); 2.77 + 2.78 +//=========================================================================== 2.79 + 2.80 +#endif /*MATRIX_MULT_H_*/
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/Application/VPThread__Matrix_Mult/Divide_Pr.c Fri Sep 17 11:39:05 2010 -0700 3.3 @@ -0,0 +1,116 @@ 3.4 +/* 3.5 + * Copyright 2009 OpenSourceStewardshipFoundation.org 3.6 + * Licensed under GNU General Public License version 2 3.7 + * 3.8 + * Author: seanhalle@yahoo.com 3.9 + * 3.10 + */ 3.11 + 3.12 + 3.13 +#include "VPThread__Matrix_Mult.h" 3.14 + 3.15 +/*Divider creates one processor for every row-col pair. 3.16 + * It hands them: 3.17 + * the name of the result processor that they should send their results to, 3.18 + * the left and right matrices, and the row and col they should multiply 3.19 + * the length of the vector 3.20 + * It first creates the result processor, then all the vector processors, 3.21 + * then does a receive of a message from the result processor that gives 3.22 + * the divider ownership of the result matrix. 3.23 + * Finally, the divider returns the result matrix out of the VPThread system. 3.24 + */ 3.25 +void divideIntoVectors( void *_dividerParams, VirtProcr *animatingThd ) 3.26 + { VirtProcr *resultsThd; 3.27 + DividerParams *dividerParams; 3.28 + ResultsParams *resultsParams; 3.29 + VectorParams *vectParams; 3.30 + Matrix *leftMatrix, *rightMatrix, *resultMatrix; 3.31 + void *msg; 3.32 + MatrixMultGlobals *globals; 3.33 + 3.34 +// printf("start divide\n"); fflush(stdin); 3.35 + 3.36 + dividerParams = (DividerParams *)_dividerParams; 3.37 + 3.38 + leftMatrix = dividerParams->leftMatrix; 3.39 + rightMatrix = dividerParams->rightMatrix; 3.40 + 3.41 + resultsParams = malloc( sizeof(ResultsParams) ); 3.42 + resultsParams->dividerThd = animatingThd; 3.43 + resultsParams->numCols = rightMatrix->numCols; 3.44 + resultsParams->numRows = leftMatrix->numRows; 3.45 + 3.46 + //=========== Set up global vars, including conds and mutexes =========== 3.47 + globals = malloc( sizeof(MatrixMultGlobals) ); 3.48 + VPThread__set_globals_to( globals ); 3.49 + 3.50 + globals->results_mutex = VPThread__make_mutex( animatingThd ); 3.51 + globals->results_cond = VPThread__make_cond( globals->results_mutex, 3.52 + animatingThd ); 3.53 + 3.54 + globals->vector_mutex = VPThread__make_mutex( animatingThd ); 3.55 + globals->vector_cond = VPThread__make_cond( globals->vector_mutex, 3.56 + animatingThd ); 3.57 + 3.58 + globals->start_mutex = VPThread__make_mutex( animatingThd ); 3.59 + globals->start_cond = VPThread__make_cond( globals->start_mutex, 3.60 + animatingThd ); 3.61 + //======================================================================== 3.62 + 3.63 + //get results-comm lock before create results-thd, to ensure it can't 3.64 + // signal that results are available before this thd is waiting on cond 3.65 + VPThread__mutex_lock( globals->results_mutex, animatingThd ); 3.66 + 3.67 + //also get the start lock & use to ensure no vector threads send a 3.68 + // signal before the results thread is waiting on vector cond 3.69 + VPThread__mutex_lock( globals->start_mutex, animatingThd ); 3.70 + 3.71 + 3.72 + VPThread__create_thread( &gatherResults, resultsParams, animatingThd ); 3.73 + 3.74 + //Now wait for results thd to signal that it has vector lock 3.75 + VPThread__cond_wait( globals->start_cond, animatingThd ); 3.76 + VPThread__mutex_unlock( globals->start_mutex, animatingThd );//done w/lock 3.77 + 3.78 + 3.79 + //make the vector thds 3.80 + int row, col; 3.81 + for( row = 0; row < leftMatrix->numRows; row++ ) 3.82 + { for( col = 0; col < rightMatrix->numCols; col++ ) 3.83 + { 3.84 + vectParams = malloc( sizeof(VectorParams) ); 3.85 + vectParams->myCol = col; 3.86 + vectParams->myRow = row; 3.87 + vectParams->vectLength = leftMatrix->numCols; 3.88 + vectParams->leftMatrix = leftMatrix; 3.89 + vectParams->rightMatrix = rightMatrix; 3.90 + 3.91 + VPThread__create_thread( &calcVector, vectParams, animatingThd ); 3.92 + } 3.93 + //=================== DEBUG =================== 3.94 + #ifdef PRINT_DEBUG_1 3.95 + printf("created thread: %d, %d\n", row, col); 3.96 + #endif 3.97 + //============================================== 3.98 + } 3.99 + 3.100 + //Wait for results thread to say results are good 3.101 + VPThread__cond_wait( globals->results_cond, animatingThd ); 3.102 + 3.103 + //The results of the all the work have to be linked-to from the data 3.104 + // struc given to the seed procr -- this divide func is animated by 3.105 + // that seed procr, so have to link results to the _dividerParams. 3.106 + resultMatrix = malloc( sizeof(Matrix) ); 3.107 + resultMatrix->numCols = rightMatrix->numCols; 3.108 + resultMatrix->numRows = leftMatrix->numRows; 3.109 + dividerParams->resultMatrix = resultMatrix; 3.110 + resultMatrix->matrix = globals->results; 3.111 + 3.112 + //done with communication, release lock 3.113 + VPThread__mutex_unlock( globals->results_mutex, animatingThd ); 3.114 + 3.115 + 3.116 + VPThread__dissipate_thread( animatingThd ); //all Thds dissipate when done 3.117 + //when all of the threads have dissipated, the "create seed and do 3.118 + // work" call in the entry point function returns 3.119 + }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/Application/VPThread__Matrix_Mult/EntryPoint.c Fri Sep 17 11:39:05 2010 -0700 4.3 @@ -0,0 +1,48 @@ 4.4 +/* 4.5 + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 4.6 + * Licensed under GNU General Public License version 2 4.7 + * 4.8 + * Author: seanhalle@yahoo.com 4.9 + * 4.10 + */ 4.11 + 4.12 +#include <math.h> 4.13 + 4.14 +#include "VPThread__Matrix_Mult.h" 4.15 + 4.16 + 4.17 + 4.18 +/*Every VPThread system has an "entry point" function that creates the first 4.19 + * processor, which starts the chain of creating more processors.. 4.20 + * eventually all of the processors will dissipate themselves, and 4.21 + * return. 4.22 + * 4.23 + *This entry-point function follows the same pattern as all entry-point 4.24 + * functions do: 4.25 + *1) it creates the params for the seed processor, from the 4.26 + * parameters passed into the entry-point function 4.27 + *2) it calls VPThread__create_seed_procr_and_do_work 4.28 + *3) it gets the return value from the params struc, frees the params struc, 4.29 + * and returns the value from the function 4.30 + * 4.31 + */ 4.32 +Matrix * 4.33 +multiplyTheseMatrices( Matrix *leftMatrix, Matrix *rightMatrix ) 4.34 + { Matrix *resMatrix; 4.35 + DividerParams *dividerParams; 4.36 + 4.37 + 4.38 + dividerParams = malloc( sizeof( DividerParams ) ); 4.39 + dividerParams->leftMatrix = leftMatrix; 4.40 + dividerParams->rightMatrix = rightMatrix; 4.41 + 4.42 + 4.43 + //create divider processor, start doing the work, and wait till done 4.44 + //This function is the "border crossing" between normal code and VPThread 4.45 + VPThread__create_seed_procr_and_do_work( ÷IntoVectors, dividerParams ); 4.46 + 4.47 + //get result matrix and return it 4.48 + resMatrix = dividerParams->resultMatrix; 4.49 + free( dividerParams ); 4.50 + return resMatrix; 4.51 + }
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/Application/VPThread__Matrix_Mult/Result_Pr.c Fri Sep 17 11:39:05 2010 -0700 5.3 @@ -0,0 +1,86 @@ 5.4 +/* 5.5 + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 5.6 + * Licensed under GNU General Public License version 2 5.7 + * 5.8 + * Author: seanhalle@yahoo.com 5.9 + * 5.10 + */ 5.11 + 5.12 +#include "VPThread__Matrix_Mult.h" 5.13 + 5.14 +/*The Result Processor gets a message from each of the vector processors, 5.15 + * puts the result from the message in its location in the result- 5.16 + * matrix, and increments the count of results. 5.17 + * 5.18 + *After the count reaches the point that all results have been received, it 5.19 + * returns the result matrix and dissipates. 5.20 + */ 5.21 +void gatherResults( void *_params, VirtProcr *animatingPr ) 5.22 + { VirtProcr *dividerPr; 5.23 + ResultsParams *params; 5.24 + int numRows, numCols, numCells, count=0; 5.25 + float32 *resultMatrixArray; 5.26 + void *msg; 5.27 + VectorParams *aResult; 5.28 + MatrixMultGlobals *globals =(MatrixMultGlobals *)VPThread__give_globals(); 5.29 + 5.30 + 5.31 + //get vector-comm lock before loop, so that this thd keeps lock after 5.32 + // one wait until it enters the next wait -- forces see-saw btwn 5.33 + // waiters and signalers -- wait-signal-wait-signal-... 5.34 + VPThread__mutex_lock( globals->vector_mutex, animatingPr ); 5.35 + 5.36 + //Tell divider that have the vector lock -- so it's sure won't miss any 5.37 + // signals from the vector-threads it's about to create 5.38 + VPThread__cond_signal( globals->start_cond, animatingPr ); 5.39 + VPThread__mutex_unlock( globals->start_mutex, animatingPr );//finish wait 5.40 + 5.41 + //===================== DEBUG ====================== 5.42 + #ifdef PRINT_DEBUG 5.43 + printf("**Result Pr has the lock**\n" ); 5.44 + fflush(stdin); 5.45 + #endif 5.46 + //==================================================== 5.47 + 5.48 + params = (ResultsParams *)_params; 5.49 + dividerPr = params->dividerThd; 5.50 + numCols = params->numCols; 5.51 + numRows = params->numRows; 5.52 + numCells = numRows * numCols; 5.53 + 5.54 + resultMatrixArray = malloc( numCells * sizeof( float32 ) ); 5.55 + 5.56 + 5.57 + while( count < numCells ) 5.58 + { 5.59 + //receive a vector-result from a vector-thread 5.60 + VPThread__cond_wait( globals->vector_cond, animatingPr ); 5.61 + 5.62 + aResult = globals->currVector; 5.63 + *(resultMatrixArray + aResult->myRow * numCols + aResult->myCol) = 5.64 + aResult->result; 5.65 + count++; 5.66 + //===================== DEBUG ====================== 5.67 + #ifdef PRINT_DEBUG_1 5.68 + if( count - count/numRows * numRows == 0 ) 5.69 + { printf("%d vector result: %f\n", count, aResult->result ); 5.70 + fflush(stdin); 5.71 + } 5.72 + #endif 5.73 + //==================================================== 5.74 + 5.75 + } 5.76 + //all comms done, release lock 5.77 + VPThread__mutex_unlock( globals->vector_mutex, animatingPr ); 5.78 + 5.79 + //Send result to divider (seed) thread 5.80 + // note, divider thd had to hold the results-comm lock before creating 5.81 + // this thread, to be sure no race 5.82 + VPThread__mutex_lock( globals->results_mutex, animatingPr ); 5.83 + globals->results = resultMatrixArray; 5.84 + VPThread__cond_signal( globals->results_cond, animatingPr ); 5.85 + VPThread__mutex_unlock( globals->results_mutex, animatingPr ); //releases 5.86 + //divider thread from its wait, at point this executes 5.87 + 5.88 + VPThread__dissipate_thread( animatingPr ); //frees any data owned by procr 5.89 + }
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/Application/VPThread__Matrix_Mult/VPThread__Matrix_Mult.h Fri Sep 17 11:39:05 2010 -0700 6.3 @@ -0,0 +1,75 @@ 6.4 +/* 6.5 + * Copyright Oct 24, 2009 OpenSourceCodeStewardshipFoundation.org 6.6 + * Licensed under GNU General Public License version 2 6.7 + */ 6.8 + 6.9 +#ifndef _VPThread__MATRIX_MULT_H_ 6.10 +#define _VPThread__MATRIX_MULT_H_ 6.11 + 6.12 +#include <stdio.h> 6.13 + 6.14 +#include "../../VPThread__lib/VPThread.h" 6.15 +#include "../Matrix_Mult.h" 6.16 + 6.17 +//============================== Structures ============================== 6.18 +typedef struct 6.19 + { 6.20 + Matrix *leftMatrix; 6.21 + Matrix *rightMatrix; 6.22 + Matrix *resultMatrix; 6.23 + } 6.24 +DividerParams; 6.25 + 6.26 +typedef struct 6.27 + { 6.28 + VirtProcr *dividerThd; 6.29 + int numRows; 6.30 + int numCols; 6.31 + } 6.32 +ResultsParams; 6.33 + 6.34 +typedef struct 6.35 + { VirtProcr *resultsThd; 6.36 + int myCol; 6.37 + int myRow; 6.38 + int vectLength; 6.39 + Matrix *leftMatrix; 6.40 + Matrix *rightMatrix; 6.41 + float32 result; 6.42 + } 6.43 +VectorParams; 6.44 + 6.45 +typedef struct 6.46 + { 6.47 + //for communicating vector results to results Thd 6.48 + int32 vector_mutex; 6.49 + int32 vector_cond; 6.50 + VectorParams *currVector; 6.51 + 6.52 + //for communicating results array back to seed (divider) Thd 6.53 + int32 results_mutex; 6.54 + int32 results_cond; 6.55 + float32 *results; 6.56 + 6.57 + //for ensuring results thd has vector lock before making vector thds 6.58 + int32 start_mutex; 6.59 + int32 start_cond; 6.60 + 6.61 + Matrix *rightMatrix; 6.62 + Matrix *resultMatrix; 6.63 + } 6.64 +MatrixMultGlobals; 6.65 + 6.66 + 6.67 +//============================= Processor Functions ========================= 6.68 +void divideIntoVectors( void *data, VirtProcr *animatingPr ); 6.69 +void calcVector( void *data, VirtProcr *animatingPr ); 6.70 +void gatherResults( void *data, VirtProcr *animatingPr ); 6.71 + 6.72 + 6.73 +//================================ Entry Point ============================== 6.74 +Matrix * 6.75 +multiplyTheseMatrices( Matrix *leftMatrix, Matrix *rightMatrix ); 6.76 + 6.77 + 6.78 +#endif /*_VPThread__MATRIX_MULT_H_*/
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/Application/VPThread__Matrix_Mult/Vector_Pr.c Fri Sep 17 11:39:05 2010 -0700 7.3 @@ -0,0 +1,59 @@ 7.4 +/* 7.5 + * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 7.6 + * Licensed under GNU General Public License version 2 7.7 + * 7.8 + * Author: SeanHalle@yahoo.com 7.9 + * 7.10 + */ 7.11 + 7.12 +#include "VPThread__Matrix_Mult.h" 7.13 + 7.14 +/*A Vector processor is created with an environment that holds two matrices, 7.15 + * the row and col that it owns, and the name of a result gathering 7.16 + * processor. 7.17 + *It calculates its vector product then sends the result to the result 7.18 + * processor, which puts it into the result matrix and returns that matrix 7.19 + * when all is done. 7.20 + */ 7.21 + void 7.22 +calcVector( void *data, VirtProcr *animatingPr ) 7.23 + { 7.24 + VectorParams *params; 7.25 + VirtProcr *resultPr; 7.26 + int myRow, myCol, vectLength, pos; 7.27 + float32 *leftMatrixArray, *rightMatrixArray, result = 0.0; 7.28 + Matrix *leftMatrix, *rightMatrix; 7.29 + MatrixMultGlobals *globals =(MatrixMultGlobals *)VPThread__give_globals(); 7.30 + 7.31 + params = (VectorParams *)data; 7.32 + myCol = params->myCol; 7.33 + myRow = params->myRow; 7.34 + vectLength = params->vectLength; 7.35 + leftMatrix = params->leftMatrix; 7.36 + rightMatrix = params->rightMatrix; 7.37 + leftMatrixArray = leftMatrix->matrix; 7.38 + rightMatrixArray = rightMatrix->matrix; 7.39 + //===================== DEBUG ====================== 7.40 + #ifdef PRINT_DEBUG_1 7.41 + if( myCol == 0 ) 7.42 + printf("start vector: %d, %d\n", myRow, myCol ); fflush(stdin); 7.43 + #endif 7.44 + //==================================================== 7.45 + 7.46 + for( pos = 0; pos < vectLength; pos++ ) 7.47 + { 7.48 + result += *(leftMatrixArray + myRow * vectLength + pos) * 7.49 + *(rightMatrixArray + pos * vectLength + myCol); 7.50 + } 7.51 + params->result = result; 7.52 + 7.53 + //Send result to results thread 7.54 + VPThread__mutex_lock( globals->vector_mutex, animatingPr );//only get 7.55 + //the lock when results thd is inside wait. 7.56 + globals->currVector = params; 7.57 + VPThread__cond_signal( globals->vector_cond, animatingPr ); 7.58 + VPThread__mutex_unlock( globals->vector_mutex, animatingPr );//release 7.59 + //wait-er -- cond_signal implemented such that wait-er gets lock, no other 7.60 + 7.61 + VPThread__dissipate_thread( animatingPr ); 7.62 + }
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/Application/main.c Fri Sep 17 11:39:05 2010 -0700 8.3 @@ -0,0 +1,35 @@ 8.4 +/* 8.5 + * Copyright Oct 24, 2009 OpenSourceCodeStewardshipFoundation.org 8.6 + * Licensed under GNU General Public License version 2 8.7 + * 8.8 + * author seanhalle@yahoo.com 8.9 + */ 8.10 + 8.11 +#include <malloc.h> 8.12 +#include <stdlib.h> 8.13 + 8.14 +#include "Matrix_Mult.h" 8.15 +#include "VPThread__Matrix_Mult/VPThread__Matrix_Mult.h" 8.16 + 8.17 +/** 8.18 + *Matrix multiply program written using VMS_HW piggy-back language 8.19 + * 8.20 + */ 8.21 +int main( int argc, char **argv ) 8.22 + { Matrix *leftMatrix, *rightMatrix, *resultMatrix; 8.23 + ParamBag *paramBag; 8.24 + 8.25 + paramBag = makeParamBag(); 8.26 + readParamFileIntoBag( argv[1], paramBag ); 8.27 + initialize_Input_Matrices_Via( &leftMatrix, &rightMatrix, paramBag ); 8.28 + 8.29 + resultMatrix = multiplyTheseMatrices( leftMatrix, rightMatrix ); 8.30 + 8.31 + printf("\nresult matrix: \n"); 8.32 + 8.33 +// printMatrix( resultMatrix ); 8.34 + 8.35 +// VPThread__print_stats(); 8.36 + 8.37 + exit(0); //cleans up 8.38 + }
