/********************************************************************** C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example : pthread-infinitynorm-rowwise.c Objective : Inifinity Norm of Matrix using Row Wise splitting. Input : Class Size, Number of threads. Output : Infinity Norm of Matrix (Using Row-Wise splitting). Created : August-2013 E-mail : hpcfte@cdac.in ************************************************************************/ #include #include #include #include #include #define MAXTHREADS 8 /* pthread mutex and condition variable declaration */ pthread_mutex_t mutex_norm = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t CurRow_norm = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex_Row = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER; /*global variable declaration */ pthread_mutex_t *mutex_Res; double NormRow = 0 ; double *Res; int dist_row,dist_col; int iteration; int rmajor,cmajor; int perfect_square; int row, col, currentRow = 0; double **InMat ; int numberOfThreads; double infinitynorm_row; /* Thread callback function */ void * doRowWise(int myId) { int CurRow = 0; int iCol,myRowSum; int mynorm; for (CurRow = ((myId - 1) * dist_row); CurRow <= ((myId * dist_row) - 1); CurRow++) { myRowSum = 0; for(iCol = 0 ;iCol < col; iCol++) myRowSum += InMat[CurRow][iCol]; if(mynorm < myRowSum ) mynorm = myRowSum; } pthread_mutex_lock(&mutex_norm); { if (NormRow < mynorm) NormRow = mynorm; } pthread_mutex_unlock(&mutex_norm); pthread_exit(NULL); } /* Main function starts*/ main(int argc, char *argv[]) { /* variable declaration */ int i, j,p,q,CLASS_SIZE,THREADS; int first_value_row,diff,matrix_size,ret_count; double time_start, time_end,memoryused=0.0; struct timeval tv; struct timezone tz; int counter, irow, icol, numberOfThreads,ithread; FILE *fp; char * CLASS; pthread_t *threads_row; pthread_attr_t ptr; printf("\n\t\t---------------------------------------------------------------------------"); printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)"); printf("\n\t\t Email : betatest@cdac.in"); printf("\n\t\t---------------------------------------------------------------------------"); printf("\n\t\t Objective : Dense Matrix Computations (Floating-Point Operations)\n "); printf("\n\t\t Computation of Infinity Norm of a Square Matrix - Rowwise partitioning;"); printf("\n\t\t..........................................................................\n"); /*Argument validation check*/ if( argc != 3 ){ printf("\t\t Very Few Arguments\n "); printf("\t\t Syntax : exec \n"); exit(-1); } else { CLASS = argv[1]; THREADS = atoi(argv[2]); } if( strcmp(CLASS, "A" )==0){ CLASS_SIZE = 1024; } if( strcmp(CLASS, "B" )==0){ CLASS_SIZE = 2048; } if( strcmp(CLASS, "C" )==0){ CLASS_SIZE = 4096; } numberOfThreads = THREADS; matrix_size = CLASS_SIZE; row = matrix_size; col = matrix_size; printf("\n\t\t Input Parameters :"); printf("\n\t\t CLASS : %c",*CLASS); printf("\n\t\t Matrix Size : %d",matrix_size); printf("\n\t\t Threads : %d",numberOfThreads); printf("\n"); if ((numberOfThreads != 1) && (numberOfThreads != 2) && (numberOfThreads != 4) && (numberOfThreads != 8)) { printf("\n Number of Threads must be 1 or 2 or 4 or 8. Aborting ...\n"); exit(0); } if(numberOfThreads > row) { printf("\nNumber of threads should be <= %d",row); exit(0); } /*....Memory Allocation....*/ InMat = (double **) malloc(sizeof(double) * row); for (i = 0; i < row; i++) InMat[i] = (double *) malloc(sizeof(double) * col); if(InMat == NULL) { printf("\n Not sufficient memory to accomadate InMat1"); exit(0); } memoryused = (row * col * sizeof(double)); /* Matrix and Vector Initialization */ for (i=0; i