source: CIVL/examples/pthread/CDAC/pthread-findminimumvalue.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: 6.5 KB
Line 
1/*****************************************************************************
2
3
4 C-DAC Tech Workshop : hyPACK-2013
5 October 15-18,2013
6 Example : pthread-findminimumvalue.c
7
8 Objective : Finds the Minimum Value in the Integer List.
9
10 Input : Number of Threads,
11 Integer List Size.
12
13 Output : Minimum value in the Integer List
14
15 Created : MAY-2013
16
17 E-mail : hpcfte@cdac.in
18
19****************************************************************************/
20
21
22#include<pthread.h>
23#include<stdio.h>
24#include<sys/time.h>
25#include<stdlib.h>
26
27#define MIN_INT 0
28#define MAXTHREADS 8
29
30void *find_min(void *) ;
31
32pthread_mutex_t minimum_value_lock;
33
34long int partial_list_size;
35int minimum_value;
36long int *list;
37long int NumElements, CLASS_SIZE;
38int NumThreads;
39
40int main (int argc,char * argv[])
41{
42
43 pthread_t *threads;
44 pthread_attr_t pta;
45 int iteration,THREADS,ret_count;
46 double time_start, time_end;
47 struct timeval tv;
48 struct timezone tz;
49 double MemoryUsed = 0.0;
50 char * CLASS;
51
52 int counter;
53 printf("\n\t\t--------------------------------------------------------------------------");
54 printf("\n\t\t Centre for Development of Advanced Computing (C-DAC)");
55 printf("\n\t\t C-DAC Multi Core Benchmark Suite 1.0");
56 printf("\n\t\t Email : hpcfte@cdac.in");
57 printf("\n\t\t---------------------------------------------------------------------------");
58 printf("\n\t\t Objective : Sorting Single Dimension Array (Integer Operations)\n ");
59 printf("\n\t\t Performance of Sorting a Minimum value in a large Single Dimension Array ");
60 printf("\n\t\t on Multi Socket Multi Core Processor using 1/2/4/8 threads \n");
61 printf("\n\t\t..........................................................................\n");
62
63 if( argc != 3 ){
64
65 printf("\t\t Very Few Arguments\n ");
66 printf("\t\t Syntax : exec <Class-Size> <Threads>\n");
67 exit(-1);
68 }
69 else {
70 CLASS = argv[1];
71 THREADS = atoi(argv[2]);
72 }
73 if( strcmp(CLASS, "A" )==0){
74 CLASS_SIZE = 10000000;
75 }
76 if( strcmp(CLASS, "B" )==0){
77 CLASS_SIZE = 100000000;
78 }
79 if( strcmp(CLASS, "C" )==0){
80 CLASS_SIZE = 1000000000;
81 }
82
83 NumElements = CLASS_SIZE;
84 NumThreads = THREADS;
85 printf("\n\t\t Array Size : %ld",NumElements);
86 printf("\n\t\t Threads : %d",NumThreads);
87 printf("\n");
88
89 if (NumThreads < 1 )
90 {
91 printf("\n Number of threads must be greater than zero. Aborting ...\n");
92 return 0;
93 }
94
95 if ((NumThreads != 1) && (NumThreads != 2) && (NumThreads != 4) && (NumThreads != 8))
96 {
97 printf("\n Number of Threads must be 1 or 2 or 4 or 8. Aborting ...\n");
98 return 0;
99 }
100
101 if ( ( NumElements % NumThreads ) != 0 )
102 {
103 printf("\n Number of threads not a factor of Integer List size. Aborting.\n");
104 return 0 ;
105 }
106
107
108 partial_list_size = NumElements / NumThreads;
109
110 list = (long int *)malloc(sizeof(long int) * NumElements);
111 MemoryUsed += ( NumElements * sizeof(long int));
112
113 gettimeofday(&tv, &tz);
114 time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
115
116 for(counter = 0 ; counter < NumElements ; counter++){
117 srand48((unsigned int)NumElements);
118 list[counter] = (rand()%1000)+1.0;
119 }
120
121 threads = (pthread_t *)malloc(sizeof(pthread_t)*NumThreads);
122
123 minimum_value = list[0];
124
125 ret_count=pthread_mutex_init(&minimum_value_lock, NULL);
126 if(ret_count)
127 {
128 printf("\n ERROR : Return code from pthread_mutex_init() is %d ",ret_count);
129 exit(-1);
130 }
131
132 ret_count=pthread_attr_init(&pta);
133 if(ret_count)
134 {
135 printf("\n ERROR : Return code from pthread_attr_init() is %d ",ret_count);
136 exit(-1);
137 }
138
139 pthread_attr_setscope(&pta,PTHREAD_SCOPE_SYSTEM);
140
141 for(counter = 0 ; counter < NumThreads ; counter++)
142 {
143 ret_count=pthread_create(&threads[counter],&pta,(void *(*) (void *)) find_min,(void *) (counter+1));
144 if(ret_count)
145 {
146 printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
147 exit(-1);
148 }
149 }
150
151 for(counter = 0 ; counter < NumThreads ; counter++)
152 {
153 ret_count=pthread_join(threads[counter],NULL);
154 if(ret_count)
155 {
156 printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
157 exit(-1);
158 }
159 }
160 ret_count=pthread_attr_destroy(&pta);
161 if(ret_count)
162 {
163 printf("\n ERROR : Return code from pthread_attr_destroy() is %d ",ret_count);
164 exit(-1);
165 }
166
167 gettimeofday(&tv, &tz);
168 time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
169
170 printf("\n\t\t Minimum Value found in the Integer list : %d",minimum_value);
171 printf("\n\t\t Memory Utilised : %lf MB",(MemoryUsed / (1024*1024)));
172 printf("\n\t\t Time Taken in Seconds (T) : %lf Seconds",( time_end - time_start));
173 printf("\n\t\t..........................................................................\n");
174
175
176 free(list);
177 free(threads);
178 return 0;
179
180 }
181 void *find_min(void * myid ) {
182
183 int my_min;
184 long int counter;
185
186 int myId = (int)myid;
187
188 my_min = list[(myId-1)*partial_list_size];
189
190 for (counter = ((myId - 1) * partial_list_size); counter <= ((myId * partial_list_size) - 1); counter++){
191 if (list[counter] < my_min)
192 my_min = list[counter];
193 }
194
195 /* lock the mutex associated with minimum_value and update the variable as
196 required */
197
198 pthread_mutex_lock(&minimum_value_lock) ;
199 if (my_min < minimum_value)
200 minimum_value = my_min;
201
202 /* and unlock the mutex */
203 pthread_mutex_unlock(&minimum_value_lock) ;
204
205 pthread_exit(NULL);
206}
Note: See TracBrowser for help on using the repository browser.