source: CIVL/examples/pthread/CDAC/pthread-findminimumvalue.c@ 367a390

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