Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> //comments written in english coz greek is broken
- #include <semaphore.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- struct thread_arg { //struct creation in order two pass multiple parameters within the pthread creation
- int *table;
- int k;
- };
- void *calculation(void *);
- pthread_mutex_t sum_mutex = PTHREAD_MUTEX_INITIALIZER;
- int maxp, n, p, *sum = 0;
- int main()
- {
- int i, j, k=0, check, *table;
- clock_t t;
- double time_taken;
- FILE *f=fopen ("C:\tmp\resultsLS.txt","w");
- if (f==NULL){
- printf("nError opening file!n");
- exit(1);
- }
- printf("Give the max number of pthreads usedn");
- scanf("%d", &maxp);
- printf("Give the number of elements of the tablen");
- do{
- printf("The number of elements must be an integral multiple of the number of threadsn");
- scanf("%d",&n);
- check=n%maxp;
- if(check!=0){
- printf("Jesus how hard is it?nTry one more timenGive the number of elements of the tablen");
- }
- }while(check!=0);
- table = (int*) malloc(n * sizeof(int));
- if(table == NULL){
- printf("Error! Memory not allocated.n");
- exit(0);
- }
- srand(time(NULL));
- for (i=0; i<n; i++){
- table[i]=(rand()%200);
- }
- printf("n--------------------------------n");
- printf("nArray elements: ");
- for (i=0; i<n; i++) {
- printf(" %d ", table[i]);
- }
- for(p=1;p<=maxp;p*2){
- fprintf(f, "t#Number of threadst%d", p);
- pthread_t threads[p];
- printf("n--------------------------------n");
- struct thread_arg th_args[p]; //table creation to store arguments of each pthread;
- t=clock();
- for(i=0;i<p;i++){ //thread creation
- th_args[i].table = table;
- th_args[i].k = k;
- pthread_create(&threads[i], NULL, calculation, &th_args[i]);
- k++; //k is a variable used to seperate table, explained later
- }
- t=clock()-t;
- time_taken=((double)t)*1000/CLOCKS_PER_SEC;
- fprintf(f,"nExecution time of function with %d threads: %d msn", i, (int)time_taken);
- for(i=0;i<p;i++){
- pthread_join(threads[i],NULL);
- }
- printf("--------------------------------n");
- printf("Sum of vector= %dn", *sum);
- *sum=0; //reset of sum
- }
- fclose(f);
- free(table);
- exit(0);
- return 0;
- }
- void *calculation(void *data){
- int i;
- int local_sum=0;
- int *table;
- int k;
- struct thread_arg *th_args = data;
- k = th_args->k;
- table = th_args->table;
- printf("--------------------------------n");
- for(i=(n/p)*k;i<(n/p)*(k+1);i++) //this algorithm seperates the table into equivelant pieces and
- { //every pthread is calculating its given piece, then stores that value in its local variable sum
- if((n/p)>n){ //and eventually it is updating the global variable
- pthread_exit(NULL);
- }
- local_sum+=pow(table[i], 2);
- printf("Thread's %lu calculation is %dn", pthread_self(), local_sum);
- }
- pthread_mutex_lock(&sum_mutex); //mutex used here to protect the critical code
- *sum += local_sum;
- pthread_mutex_unlock(&sum_mutex);
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement