| [4a64ef2] | 1 | /******************************************************************************
|
|---|
| 2 |
|
|---|
| 3 | C-DAC Tech Workshop : hyPACK-2013
|
|---|
| 4 | October 15-18,2013
|
|---|
| 5 | Example : pthread-finding-k-matches.c
|
|---|
| 6 |
|
|---|
| 7 | Objective : Finding k matches in the given array
|
|---|
| 8 |
|
|---|
| 9 | Input : Number to be search
|
|---|
| 10 | Number Of Threads
|
|---|
| 11 |
|
|---|
| 12 | Output : Number of times search element found
|
|---|
| 13 | Time Taken for finding k matches(in Seconds).
|
|---|
| 14 |
|
|---|
| 15 | Created : MAY-2013
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | E-mail : hpcfte@cdac.in
|
|---|
| 19 |
|
|---|
| 20 | ****************************************************************************/
|
|---|
| 21 |
|
|---|
| 22 | #include "pthread.h"
|
|---|
| 23 | #include <stdio.h>
|
|---|
| 24 | #include <stdlib.h>
|
|---|
| 25 | #include<sys/time.h>
|
|---|
| 26 |
|
|---|
| [afa100f] | 27 | #define ARRAYSIZE 1000
|
|---|
| [4a64ef2] | 28 | #define MAXTHREADS 8
|
|---|
| 29 |
|
|---|
| 30 | /* global declaration */
|
|---|
| 31 | double a[ARRAYSIZE];
|
|---|
| 32 | int count ,num_threads,iterations;
|
|---|
| 33 | double search_no ;
|
|---|
| 34 | pthread_mutex_t count_mutex;
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | /*Thread callback function*/
|
|---|
| 38 | void *find_entries(void *tid)
|
|---|
| 39 | {
|
|---|
| 40 | int i, start, *mytid, end;
|
|---|
| 41 | int local_count =0;
|
|---|
| 42 |
|
|---|
| 43 | /* Initialize my part of the global array and keep local count */
|
|---|
| 44 | mytid = (int *) tid;
|
|---|
| 45 | start = (*mytid * iterations);
|
|---|
| 46 | end = start + iterations;
|
|---|
| 47 | printf ("Thread %d doing iterations %d to %d\n",*mytid,start,end-1);
|
|---|
| 48 | for (i=start; i < end ; i++) {
|
|---|
| 49 | if ( a[i] == search_no ) {
|
|---|
| 50 | local_count ++;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /* Lock the mutex and update the global count, then exit */
|
|---|
| 55 | pthread_mutex_lock (&count_mutex);
|
|---|
| 56 | count = count + local_count;
|
|---|
| 57 | pthread_mutex_unlock (&count_mutex);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /*Main function */
|
|---|
| 61 | int main(int argc, char *argv[]) {
|
|---|
| 62 |
|
|---|
| 63 | /*variable declaration */
|
|---|
| 64 | int i,start,ret_count;
|
|---|
| 65 | int *tids;
|
|---|
| 66 | pthread_t * threads;
|
|---|
| 67 | pthread_attr_t attr;
|
|---|
| 68 | double time_start, time_end;
|
|---|
| 69 | struct timeval tv;
|
|---|
| 70 | struct timezone tz;
|
|---|
| 71 |
|
|---|
| 72 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 73 | printf("\n\t\t Centre for Development of Advanced Computing (C-DAC): February-2008");
|
|---|
| 74 | printf("\n\t\t Email : hpcfte@cdac.in");
|
|---|
| 75 | printf("\n\t\t---------------------------------------------------------------------------");
|
|---|
| 76 | printf("\n\t\t Objective : Finding k matches in the given Array");
|
|---|
| 77 | printf("\n\t\t..........................................................................\n");
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | /*initializing Array */
|
|---|
| 81 | for (i=0;i<ARRAYSIZE;i++){
|
|---|
| 82 | a[i] = (i %10)+1.0;
|
|---|
| 83 | }
|
|---|
| 84 | if (argc != 3) {
|
|---|
| 85 | printf ("Syntax : exec <Number to be search> <Number of thread>\n");
|
|---|
| [afa100f] | 86 | return 0;
|
|---|
| [4a64ef2] | 87 | }
|
|---|
| 88 |
|
|---|
| [afa100f] | 89 | search_no = 50;
|
|---|
| 90 | num_threads = 2;
|
|---|
| [4a64ef2] | 91 |
|
|---|
| 92 | if (num_threads > MAXTHREADS) {
|
|---|
| 93 | printf ("Number of thread should be less than or equal to 8\n");
|
|---|
| [afa100f] | 94 | return 0;
|
|---|
| [4a64ef2] | 95 | }
|
|---|
| 96 |
|
|---|
| 97 | iterations = ARRAYSIZE/num_threads;
|
|---|
| 98 |
|
|---|
| 99 | threads = (pthread_t *) malloc(sizeof(pthread_t) * num_threads);
|
|---|
| 100 | tids = (int *) malloc(sizeof(int) * num_threads);
|
|---|
| 101 |
|
|---|
| 102 | /*Taking start time */
|
|---|
| 103 |
|
|---|
| 104 | gettimeofday(&tv, &tz);
|
|---|
| 105 | time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
|
|---|
| 106 |
|
|---|
| 107 | /* Pthreads setup: initialize mutex and explicitly create threads in a
|
|---|
| 108 | joinable state (for portability). Pass each thread its loop offset */
|
|---|
| 109 |
|
|---|
| 110 | ret_count = pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 111 | if(ret_count)
|
|---|
| 112 | {
|
|---|
| 113 | printf("\n ERROR : Return code from pthread_mutex_init() is %d ",ret_count);
|
|---|
| 114 | exit(-1);
|
|---|
| 115 | }
|
|---|
| 116 | ret_count=pthread_attr_init(&attr);
|
|---|
| 117 | if(ret_count)
|
|---|
| 118 | {
|
|---|
| 119 | printf("\n ERROR : Return code from pthread_attr_init() is %d ",ret_count);
|
|---|
| 120 | exit(-1);
|
|---|
| 121 | }
|
|---|
| 122 | ret_count = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 123 | if(ret_count)
|
|---|
| 124 | {
|
|---|
| 125 | printf("\n ERROR : Return code from pthread_attr_setdetachstate() is %d ",ret_count);
|
|---|
| 126 | exit(-1);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | for (i=0; i<num_threads; i++) {
|
|---|
| 132 | tids[i] = i;
|
|---|
| 133 | ret_count = pthread_create(&threads[i], &attr,find_entries, (void *) &tids[i]);
|
|---|
| 134 | if(ret_count)
|
|---|
| 135 | {
|
|---|
| 136 | printf("\n ERROR : Return code from pthread_create() is %d ",ret_count);
|
|---|
| 137 | exit(-1);
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /* Wait for all threads to complete then print global count */
|
|---|
| 142 | for (i=0; i<num_threads; i++) {
|
|---|
| 143 | ret_count = pthread_join(threads[i], NULL);
|
|---|
| 144 | if(ret_count)
|
|---|
| 145 | {
|
|---|
| 146 | printf("\n ERROR : Return code from pthread_join() is %d ",ret_count);
|
|---|
| 147 | exit(-1);
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /*Taking End time */
|
|---|
| 152 | gettimeofday(&tv, &tz);
|
|---|
| 153 | time_end = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 | printf("Number of search element found in list Count= %d\n",count);
|
|---|
| 157 | printf("Time in Seconds (T) : %lf\n", time_end - time_start);
|
|---|
| 158 | /* Clean up and exit */
|
|---|
| 159 | ret_count = pthread_attr_destroy(&attr);
|
|---|
| 160 | if(ret_count)
|
|---|
| 161 | {
|
|---|
| 162 | printf("\n ERROR : Return code from pthread_attr_destroy() is %d ",ret_count);
|
|---|
| 163 | exit(-1);
|
|---|
| 164 | }
|
|---|
| 165 | ret_count = pthread_mutex_destroy(&count_mutex);
|
|---|
| 166 | if(ret_count)
|
|---|
| 167 | {
|
|---|
| 168 | printf("\n ERROR : Return code from pthread_mutex_destroy() is %d ",ret_count);
|
|---|
| 169 | exit(-1);
|
|---|
| 170 | }
|
|---|
| [afa100f] | 171 | free(threads);
|
|---|
| 172 | free(tids);
|
|---|
| [4a64ef2] | 173 | }
|
|---|