/****************************************************************************** C-DAC Tech Workshop : hyPACK-2013 October 15-18, 2013 Example : pthread-finding-k-matches.c Objective : Finding k matches in the given array Input : Number to be search Number Of Threads Output : Number of times search element found Time Taken for finding k matches(in Seconds). Created : August-2013 E-mail : hpcfte@cdac.in ****************************************************************************/ #include "pthread.h" #include #include #include #define ARRAYSIZE 100000000 #define MAXTHREADS 8 /* global declaration */ double a[ARRAYSIZE]; int count ,num_threads,iterations; double search_no ; pthread_mutex_t count_mutex; /*Thread callback function*/ void *find_entries(void *tid) { int i, start, *mytid, end; int local_count =0; /* Initialize my part of the global array and keep local count */ mytid = (int *) tid; start = (*mytid * iterations); end = start + iterations; printf ("Thread %d doing iterations %d to %d\n",*mytid,start,end-1); for (i=start; i < end ; i++) { if ( a[i] == search_no ) { local_count ++; } } /* Lock the mutex and update the global count, then exit */ pthread_mutex_lock (&count_mutex); count = count + local_count; pthread_mutex_unlock (&count_mutex); } /*Main function */ int main(int argc, char *argv[]) { /*variable declaration */ int i,start,ret_count; int *tids; pthread_t * threads; pthread_attr_t attr; double time_start, time_end; struct timeval tv; struct timezone tz; printf("\n\t\t---------------------------------------------------------------------------"); printf("\n\t\t Centre for Development of Advanced Computing (C-DAC): February-2008"); printf("\n\t\t Email : betatest@cdac.in"); printf("\n\t\t---------------------------------------------------------------------------"); printf("\n\t\t Objective : Finding k matches in the given Array"); printf("\n\t\t..........................................................................\n"); /*initializing Array */ for (i=0;i \n"); return ; } search_no = atoi(argv[1]); num_threads = atoi(argv[2]); if (num_threads > MAXTHREADS) { printf ("Number of thread should be less than or equal to 8\n"); return ; } iterations = ARRAYSIZE/num_threads; threads = (pthread_t *) malloc(sizeof(pthread_t) * num_threads); tids = (int *) malloc(sizeof(int) * num_threads); /*Taking start time */ gettimeofday(&tv, &tz); time_start = (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; /* Pthreads setup: initialize mutex and explicitly create threads in a joinable state (for portability). Pass each thread its loop offset */ ret_count = pthread_mutex_init(&count_mutex, NULL); if(ret_count) { printf("\n ERROR : Return code from pthread_mutex_init() is %d ",ret_count); exit(-1); } ret_count=pthread_attr_init(&attr); if(ret_count) { printf("\n ERROR : Return code from pthread_attr_init() is %d ",ret_count); exit(-1); } ret_count = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); if(ret_count) { printf("\n ERROR : Return code from pthread_attr_setdetachstate() is %d ",ret_count); exit(-1); } for (i=0; i