source: CIVL/examples/pthread/CDAC/pthread-infinitynorm-rowwise.c@ 397ae5f

main test-branch
Last change on this file since 397ae5f was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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