| [4a64ef2] | 1 | /*************************************************************************************
|
|---|
| 2 |
|
|---|
| 3 | C-DAC Tech Workshop : hyPACK-2013
|
|---|
| 4 | October 15-18,2013
|
|---|
| 5 | Example : pthread-infinitynorm-colwise.c
|
|---|
| 6 |
|
|---|
| 7 | Objective : Inifinity Norm of Matrix using Column Wise splitting
|
|---|
| 8 |
|
|---|
| 9 | Input : class size,
|
|---|
| 10 | Number of Threads
|
|---|
| 11 |
|
|---|
| 12 | Output : Infinity Norm of Matrix (Using Column-Wise splitting)
|
|---|
| 13 |
|
|---|
| 14 | Created : MAY-2013
|
|---|
| 15 | E-mail : hpcfte@cdac.in
|
|---|
| 16 |
|
|---|
| 17 | ***************************************************************************************/
|
|---|
| 18 |
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <pthread.h>
|
|---|
| 21 | #include <math.h>
|
|---|
| 22 | #include<stdlib.h>
|
|---|
| 23 | #include<sys/time.h>
|
|---|
| 24 |
|
|---|
| 25 | /* pthread mutex and condition variable declaration */
|
|---|
| 26 |
|
|---|
| 27 | pthread_mutex_t mutex_norm = PTHREAD_MUTEX_INITIALIZER;
|
|---|
| 28 | pthread_mutex_t CurRow_norm = PTHREAD_MUTEX_INITIALIZER;
|
|---|
| 29 | pthread_mutex_t mutex_Row = PTHREAD_MUTEX_INITIALIZER;
|
|---|
| 30 | pthread_cond_t count_threshold_cv = PTHREAD_COND_INITIALIZER;
|
|---|
| 31 |
|
|---|
| 32 | pthread_mutex_t *mutex_Res;
|
|---|
| 33 | pthread_mutex_t mutex_col;
|
|---|
| 34 |
|
|---|
| 35 | /*global variable declaration */
|
|---|
| 36 | double NormCol = 0;
|
|---|
| 37 | int dist_col;
|
|---|
| 38 | int iteration;
|
|---|
| 39 |
|
|---|
| 40 | int rmajor,cmajor;
|
|---|
| 41 | int perfect_square;
|
|---|
| 42 | int row, col;
|
|---|
| 43 | double **InMat,*Res;
|
|---|
| 44 | int numberOfThreads;
|
|---|
| 45 | double infinitynorm_col;
|
|---|
| 46 |
|
|---|
| 47 | /* Thread callback function */
|
|---|
| 48 | void *doColWise(int myId)
|
|---|
| 49 | {
|
|---|
| 50 | int iRow;
|
|---|
| 51 | int CurCol = 0; /*Which Column to operate on.*/
|
|---|
| 52 |
|
|---|
| 53 | for (CurCol = ((myId - 1) * dist_col); CurCol <= ((myId * dist_col) - 1); CurCol++)
|
|---|
| 54 | for(iRow = 0 ;iRow < row; iRow++)
|
|---|
| 55 | Res[iRow] += InMat[iRow][CurCol];
|
|---|
| 56 |
|
|---|
| 57 | pthread_exit(NULL);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /* Main function starts*/
|
|---|
| [afa100f] | 61 | int main(int argc, char *argv[])
|
|---|
| [4a64ef2] | 62 | {
|
|---|
| 63 | /* variable declaration */
|
|---|
| 64 | int i, j,p,q,CLASS_SIZE,THREADS,ret_count;
|
|---|
| 65 | int first_value_row,diff,matrix_size;
|
|---|
| 66 | double time_start, time_end,memoryused=0.0;
|
|---|
| 67 | struct timeval tv;
|
|---|
| 68 | struct timezone tz;
|
|---|
| 69 | int counter, irow, icol, numberOfThreads,ithread;
|
|---|
| 70 | FILE *fp;
|
|---|
| 71 | char * CLASS;
|
|---|
| 72 |
|
|---|
| 73 | pthread_t *threads_col;
|
|---|
| 74 | pthread_attr_t ptc;
|
|---|
| 75 |
|
|---|
| 76 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 77 | printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
|
|---|
| 78 | printf("\n\t\t Email : hpcfte@cdac.in");
|
|---|
| 79 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 80 | printf("\n\t\t Objective : Dense Matrix Computations (Floating-Point Operations)\n ");
|
|---|
| 81 | printf("\n\t\t Computation of Infinity Norm of a Square Matrix - Columnwise partitioning;");
|
|---|
| 82 | printf("\n\t\t..........................................................................\n");
|
|---|
| 83 | /*Argument validation check*/
|
|---|
| 84 | if( argc != 3 ){
|
|---|
| 85 | printf("\t\t Very Few Arguments\n ");
|
|---|
| 86 | printf("\t\t Syntax : exec <Class-Size(Give A/B/C_> <Threads>\n");
|
|---|
| 87 | exit(-1);
|
|---|
| 88 | }
|
|---|
| 89 | else {
|
|---|
| 90 | CLASS = argv[1];
|
|---|
| 91 | THREADS = atoi(argv[2]);
|
|---|
| 92 | }
|
|---|
| 93 | if( strcmp(CLASS, "A" )==0){
|
|---|
| 94 | CLASS_SIZE = 1024;
|
|---|
| 95 | }
|
|---|
| 96 | if( strcmp(CLASS, "B" )==0){
|
|---|
| 97 | CLASS_SIZE = 2048;
|
|---|
| 98 | }
|
|---|
| 99 | if( strcmp(CLASS, "C" )==0){
|
|---|
| 100 | CLASS_SIZE = 4096;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | numberOfThreads = THREADS;
|
|---|
| 105 | matrix_size = CLASS_SIZE;
|
|---|
| 106 | row = matrix_size;
|
|---|
| 107 | col = matrix_size;
|
|---|
| 108 |
|
|---|
| 109 | printf("\n\t\t Input Parameters :");
|
|---|
| 110 | printf("\n\t\t CLASS : %c",*CLASS);
|
|---|
| 111 | printf("\n\t\t Matrix Size : %d",matrix_size);
|
|---|
| 112 | printf("\n\t\t Threads : %d",numberOfThreads);
|
|---|
| 113 | printf("\n");
|
|---|
| 114 |
|
|---|
| 115 | if ((numberOfThreads != 1) && (numberOfThreads != 2) && (numberOfThreads != 4) && (numberOfThreads != 8))
|
|---|
| 116 | {
|
|---|
| 117 | printf("\n Number of Threads must be 1 or 2 or 4 or 8. Aborting ...\n");
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | if(numberOfThreads > row)
|
|---|
| 122 | {
|
|---|
| 123 | printf("\nNumber of threads should be <= %d",row);
|
|---|
| 124 | return;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /*....Memory Allocation....*/
|
|---|
| 128 |
|
|---|
| 129 | InMat = (double **) malloc(sizeof(double) * row);
|
|---|
| 130 | for (i = 0; i < row; i++)
|
|---|
| 131 | InMat[i] = (double *) malloc(sizeof(double) * col);
|
|---|
| 132 | if(InMat == NULL)
|
|---|
| 133 | {
|
|---|
| 134 | printf("\n Not sufficient memory to accomadate InMat");
|
|---|
| 135 | return;
|
|---|
| 136 | }
|
|---|
| 137 | memoryused = (row * col * sizeof(double));
|
|---|
| 138 |
|
|---|
| 139 | Res = (double *) malloc(sizeof(double) * row);
|
|---|
| 140 | if(Res == NULL)
|
|---|
| 141 | {
|
|---|
| 142 | printf("\n Not sufficient memory to accomadate Res");
|
|---|
| 143 | return;
|
|---|
| 144 | }
|
|---|
| 145 | memoryused += (row * sizeof(double));
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | /* Matrix and Vector Initialization */
|
|---|
| 149 | for (i=0; i<row;i++) {
|
|---|
| 150 | for (j = 0; j<col;j++) {
|
|---|
| 151 | InMat[i][j] = (double)(i + j);
|
|---|
| 152 | Res[i] = 0.0;
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | /* Column Wise Partitioning */
|
|---|
| 158 |
|
|---|
| 159 | dist_col = col / numberOfThreads;
|
|---|
| 160 |
|
|---|
| 161 | ret_count=pthread_attr_init(&ptc);
|
|---|
| 162 | if(ret_count)
|
|---|
| 163 | {
|
|---|
| 164 | printf("\n ERROR : Return code from pthread_attr_init() is %d ",ret_count);
|
|---|
| 165 | exit(-1);
|
|---|
| 166 | }
|
|---|
| 167 | mutex_Res = ( pthread_mutex_t * ) malloc(sizeof(pthread_mutex_t) * row);
|
|---|
| 168 | threads_col = ( pthread_t * ) malloc(sizeof(pthread_t) * numberOfThreads);
|
|---|
| 169 |
|
|---|
| 170 | /*Taking start time*/
|
|---|
| 171 | gettimeofday(&tv, &tz);
|
|---|
| 172 | time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
|
|---|
| 173 |
|
|---|
| 174 | /*Thread creation */
|
|---|
| 175 | for (ithread = 0; ithread < numberOfThreads; ithread++)
|
|---|
| 176 | {
|
|---|
| 177 | ret_count=pthread_create(&threads_col[ithread], &ptc, (void*(*)(void*)) doColWise, (void*) (ithread + 1));
|
|---|
| 178 | if(ret_count)
|
|---|
| 179 | {
|
|---|
| 180 | printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
|
|---|
| 181 | exit(-1);
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | /*Joining threads */
|
|---|
| 185 | for (counter=0; counter<numberOfThreads; counter++)
|
|---|
| 186 | {
|
|---|
| 187 | ret_count=pthread_join(threads_col[counter], NULL);
|
|---|
| 188 | if(ret_count)
|
|---|
| 189 | {
|
|---|
| 190 | printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
|
|---|
| 191 | exit(-1);
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | for (irow = 0 ; irow < row ; irow++)
|
|---|
| 195 | if (Res[irow] > NormCol)
|
|---|
| 196 | NormCol = Res[irow];
|
|---|
| 197 |
|
|---|
| 198 | printf("\n\t\t Col Wise partitioning - Infinity Norm of a Square Matrix.....Done ");
|
|---|
| 199 | ret_count=pthread_attr_destroy(&ptc);
|
|---|
| 200 | if(ret_count)
|
|---|
| 201 | {
|
|---|
| 202 | printf("\n ERROR : Return code from pthread_attr_destroy() is %d ",ret_count);
|
|---|
| 203 | exit(-1);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | free(InMat);
|
|---|
| 207 | free(threads_col);
|
|---|
| 208 | free(Res);
|
|---|
| 209 | free(mutex_Res);
|
|---|
| 210 | gettimeofday(&tv, &tz);
|
|---|
| 211 | time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
|
|---|
| 212 | printf("\n");
|
|---|
| 213 | printf("\n\t\t Memory Utilized : %lf MB",(memoryused/(1024*1024)));
|
|---|
| 214 | printf("\n\t\t Time in Seconds (T) : %lf",(time_end - time_start));
|
|---|
| 215 | printf("\n\t\t..........................................................................\n");
|
|---|
| 216 |
|
|---|
| 217 | }
|
|---|