source: CIVL/examples/pthread/CDAC/pthread-jacobi.c@ 139c8d5

1.23 2.0 main test-branch
Last change on this file since 139c8d5 was 4a64ef2, checked in by John Edenhofner <johneden@…>, 11 years ago

Added new examples and test

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

  • Property mode set to 100644
File size: 8.0 KB
RevLine 
[4a64ef2]1/**************************************************************************************
2 C-DAC Tech Workshop : hyPACK-2013
3 October 15-18,2013
4 Example : pthread-jacobi.c
5
6 Objective : Jacobi method to solve AX = b matrix system of linear equations.
7
8 Input : Class Size
9 Number of Threads
10
11 Output : The solution of Ax = b or
12 The status of convergence for given bumber of iterations
13
14 Created : MAY-2013
15
16 E-mail : hpcfte@cdac.in
17
18*************************************************************************************/
19
20#include <stdio.h>
21#include<pthread.h>
22#include<sys/time.h>
23#include<stdlib.h>
24
25#define MAX_ITERATIONS 1000
26#define MAXTHREADS 8
27
28double Distance(double *X_Old, double *X_New, int matrix_size);
29pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
30
31double **Matrix_A, *RHS_Vector;
32double *X_New, *X_Old, *Bloc_X, rno,sum;
33
34int Number;
35void jacobi(int);
36
37main(int argc, char **argv)
38{
39
40 double diag_dominant_factor = 4.0000;
41 double tolerance = 1.0E-5;
42 /* .......Variables Initialisation ...... */
43 int matrix_size, NoofRows, NoofCols,CLASS_SIZE,THREADS;
44 int NumThreads,ithread;
45 double rowsum;
46 double sum;
47 int irow, icol, index, Iteration,iteration,ret_count;
48 double time_start, time_end,memoryused;
49 struct timeval tv;
50 struct timezone tz;
51 char * CLASS;
52 FILE *fp;
53
54 pthread_attr_t pta;
55 pthread_t *threads;
56
57 memoryused =0.0;
58
59
60 printf("\n\t\t---------------------------------------------------------------------------");
61 printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
62 printf("\n\t\t Email : hpcfte@cdac.in");
63 printf("\n\t\t---------------------------------------------------------------------------");
64 printf("\n\t\t Objective : To Solve AX=B Linear Equation (Jacobi Method)\n ");
65 printf("\n\t\t Performance for solving AX=B Linear Equation using JACOBI METHOD");
66 printf("\n\t\t..........................................................................\n");
67
68 if( argc != 3 ){
69
70 printf("\t\t Very Few Arguments\n ");
71 printf("\t\t Syntax : exec <Class-Size (Give A/B/C)> <Threads>\n");
72 exit(-1);
73 }
74 else {
75 CLASS = argv[1];
76 THREADS = atoi(argv[2]);
77 }
78 if (THREADS > MAXTHREADS ){
79 printf("\n Number of Threads must be less than or equal to 8. Aborting ...\n");
80 return;
81 }
82 if( strcmp(CLASS, "A" )==0){
83 CLASS_SIZE = 1024;
84 }
85 if( strcmp(CLASS, "B" )==0){
86 CLASS_SIZE = 2048;
87 }
88 if( strcmp(CLASS, "C" )==0){
89 CLASS_SIZE = 4096;
90 }
91
92
93 matrix_size = CLASS_SIZE;
94 NumThreads = THREADS;
95 printf("\n\t\t Matrix Size : %d",matrix_size);
96 printf("\n\t\t Threads : %d",NumThreads);
97
98 NoofRows = matrix_size;
99 NoofCols = matrix_size;
100
101
102 /* Allocate The Memory For Matrix_A and RHS_Vector */
103 Matrix_A = (double **) malloc(matrix_size * sizeof(double *));
104 RHS_Vector = (double *) malloc(matrix_size * sizeof(double));
105
106
107 /* Populating the Matrix_A and RHS_Vector */
108 rowsum = (double) (matrix_size *(matrix_size+1)/2.0);
109 for (irow = 0; irow < matrix_size; irow++)
110 {
111 Matrix_A[irow] = (double *) malloc(matrix_size * sizeof(double));
112 sum = 0.0;
113 for (icol = 0; icol < matrix_size; icol++)
114 {
115 Matrix_A[irow][icol]= (double) (icol+1);
116 if(irow == icol ) Matrix_A[irow][icol] = (double)(rowsum);
117 sum = sum + Matrix_A[irow][icol];
118 }
119 RHS_Vector[irow] = (double)(2*rowsum) - (double)(irow+1);
120 }
121
122 memoryused+=(NoofRows * NoofCols * sizeof(double));
123 memoryused+=(NoofRows * sizeof(double));
124
125 printf("\n");
126
127 if (NoofRows != NoofCols)
128 {
129 printf("Input Matrix Should Be Square Matrix ..... \n");
130 exit(-1);
131 }
132
133 /* Dynamic Memory Allocation */
134 X_New = (double *) malloc(matrix_size * sizeof(double));
135 memoryused+=(NoofRows * sizeof(double));
136 X_Old = (double *) malloc(matrix_size * sizeof(double));
137 memoryused+=(NoofRows * sizeof(double));
138 Bloc_X = (double *) malloc(matrix_size * sizeof(double));
139 memoryused+=(NoofRows * sizeof(double));
140
141 /* Calculating the time of Operation Start */
142 gettimeofday(&tv, &tz);
143 time_start= (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
144
145 /* Initailize X[i] = B[i] */
146
147 for (irow = 0; irow < matrix_size; irow++)
148 {
149 Bloc_X[irow] = RHS_Vector[irow];
150 X_New[irow] = RHS_Vector[irow];
151 }
152
153 /* Allocating the memory for user specified number of threads */
154 threads = (pthread_t *) malloc(sizeof(pthread_t) * NumThreads);
155
156 /* Initializating the thread attribute */
157 pthread_attr_init(&pta);
158
159 Iteration = 0;
160 do {
161 for(index = 0; index < matrix_size; index++)
162 X_Old[index] = X_New[index];
163 for(ithread=0;ithread<NumThreads;ithread++)
164 {
165
166 /* Creating The Threads */
167 ret_count=pthread_create(&threads[ithread],&pta,(void *(*) (void *))jacobi, (void *) (matrix_size/NumThreads));
168 if(ret_count)
169 {
170 printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
171 exit(-1);
172 }
173
174 }
175
176 Iteration++;
177 for (ithread=0; ithread<NumThreads; ithread++)
178 {
179 ret_count=pthread_join(threads[ithread], NULL);
180 if(ret_count)
181 {
182 printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
183 exit(-1);
184 }
185 }
186 ret_count=pthread_attr_destroy(&pta);
187 if(ret_count)
188 {
189 printf("\n ERROR : Return code from pthread_attr_destroy() is %d ",ret_count);
190 exit(-1);
191 }
192 } while ((Iteration < MAX_ITERATIONS) && (Distance(X_Old, X_New, matrix_size) >= tolerance));
193
194 /* Calculating the time at the end of Operation */
195 gettimeofday(&tv, &tz);
196 time_end= (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
197
198 printf("\n\t\t The Jacobi Method For AX=B .........DONE");
199 printf("\n\t\t Total Number Of Iterations : %d",Iteration);
200 printf("\n\t\t Memory Utilized : %lf MB",(memoryused/(1024*1024)));
201 printf("\n\t\t Time in Seconds (T) : %lf",(time_end - time_start));
202 printf("\n\t\t..........................................................................\n");
203
204 /* Freeing Allocated Memory */
205 free(X_New);
206 free(X_Old);
207 free(Matrix_A);
208 free(RHS_Vector);
209 free(Bloc_X);
210 free(threads);
211
212}
213
214double Distance(double *X_Old, double *X_New, int matrix_size)
215{
216 int index;
217 double Sum;
218
219 Sum = 0.0;
220 for (index = 0; index < matrix_size; index++)
221 Sum += (X_New[index] - X_Old[index]) * (X_New[index] - X_Old[index]);
222 return (Sum);
223}
224
225void jacobi(int Number)
226{
227 int i,j;
228
229 for(i = 0; i < Number; i++)
230 {
231 Bloc_X[i] = RHS_Vector[i];
232
233 for (j = 0;j<i;j++)
234 {
235 Bloc_X[i] -= X_Old[j] * Matrix_A[i][j];
236 }
237 for (j = i+1;j<Number;j++)
238 {
239 Bloc_X[i] -= X_Old[j] * Matrix_A[i][j];
240 }
241 Bloc_X[i] = Bloc_X[i] / Matrix_A[i][i];
242 }
243 for(i = 0; i < Number; i++)
244
245 {
246 X_New[i] = Bloc_X[i];
247 }
248}
Note: See TracBrowser for help on using the repository browser.