source: CIVL/examples/pthread/CDAC/pthread-loop-carried.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: 4.5 KB
Line 
1/***************************************************************************
2 C-DAC Tech Workshop : hyPACK-2013
3 October 15-18,2013
4
5 Example : pthread-loop-carried.c
6
7 Objective : Restructuring Loop-carried dependence to Loop-
8 independent dependence using pthread APIs
9
10 Input : Nothing.
11
12 Output : Time in Second for Serial And Parallel Execution.
13
14 Created : MAY-2013
15
16 E-mail : hpcfte@cdac.in
17
18****************************************************************************/
19
20/*Header files inclusion */
21
22#include<pthread.h>
23#include<stdio.h>
24#include<stdlib.h>
25#include<math.h>
26
27/* define size of thread and number of thread */
28#define N 100000000
29#define numThread 4
30 /* defining global variable */
31 const double up = 1.0;
32 double Sn = 1.0,origSn=1.0,Snp;
33 double opts[N+1];
34 double optp[N+1];
35 int dist;
36 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
37
38/*call back function for thread */
39
40void * dowork(int threadid)
41{
42 int n;
43 double Snpl;
44 /* logic for parallized the code using pow() function */
45 for(n=(threadid * dist) + 1 ; n<=((threadid +1) * dist); n++)
46 {
47 if(n==((threadid * dist)+1))
48 {
49 Snpl = origSn * pow(up,n);
50 }
51 else
52 {
53 Snpl = Snpl * up;
54 }
55 optp[n] = Snpl;
56 }
57
58 if(threadid == (numThread - 1))
59 {
60 Snp=Snpl;
61 }
62
63
64}
65/* main () starts */
66int main(int argc, char * argv [])
67{
68
69 /*variable declaration */
70 int n,t,status;
71 pthread_t threads[numThread];
72 pthread_attr_t pta;
73 int ret_count;
74 double time_start, time_end;
75 struct timeval tv;
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 : Write pthread code to illustrate restructuring Loop-carried dependence to Loop-independent dependence using pthread APIs. \n ");
81 printf("\n\t\t..........................................................................\n");
82
83
84 if ( numThread > 8 )
85 {
86 printf("\n Number of thread should be less than or equal to 8 \n");
87 return;
88 }
89
90 /*Taking start time for serial calculation */
91 gettimeofday(&tv,NULL);
92 time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
93
94 /* serial loop with loop carried Dependence */
95 for (n=0 ; n<=N; ++n)
96 {
97 opts[n] = Sn;
98 Sn = Sn * up;
99 }
100 /*Taking end time for serial calculation */
101 gettimeofday(&tv,NULL);
102 time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
103 printf("\n\t\t Time in serial Execution in Seconds (T) : %lf",(time_end - time_start));
104
105 dist = N/numThread;
106 optp[0]= origSn;
107 ret_count=pthread_attr_init(&pta);
108
109 /*Taking start time for parallel calculation */
110 gettimeofday(&tv,NULL);
111 time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
112
113 /* Loop for Ceating Thread's */
114 for(t=0;t<numThread;t++)
115 {
116 ret_count = pthread_create(&threads[t], &pta,(void *(*) (void *)) dowork, (void *)t);
117 if (ret_count)
118 {
119 printf("ERROR; return code from pthread_create() is %d\n", ret_count);
120 exit(-1);
121 }
122
123 }
124 /* Loop For Join Thread's */
125 for(t=0;t<numThread;t++)
126 {
127 pthread_join(threads[t],(void**) & status);
128
129 }
130
131
132 Snp = Snp * up;
133 /*Taking end time for parallel calculation */
134 gettimeofday(&tv,NULL);
135 time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
136 printf("\n\t\t Time in parallel Execution in Seconds (T) : %lf",(time_end - time_start));
137 printf("\n");
138
139 /*Loop For Comparning The Result */
140 for (n=0 ; n<=N; ++n)
141 {
142
143 if((float)opts[n] !=(float) optp[n])
144 {
145 printf("Result is not Same ");
146 return;
147 }
148 }
149
150
151 printf("\nserial Sn= %lf parallel Snp= %lf\n",Sn,Snp);
152 if((float) Sn!=(float)Snp)
153 {
154 printf("Both Serial(Sn) and Parallel (Snp) Variable are Not Same ");
155 }
156
157}
Note: See TracBrowser for help on using the repository browser.