Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<string.h>
- #include<stdio.h>
- #include<time.h>
- #include<math.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdlib.h>
- /**
- * @author cristian.chilipirea
- *
- */
- int N = 1024*1024*100;
- int P;
- int a;
- void* threadFunction(void *var)
- {
- int thread_id = *(int*)var;
- int start = thread_id * ceil((double)N/P);
- int end;
- if( N > (thread_id + 1) * ceil((double)N/P))
- end = (thread_id + 1) * ceil((double)N/P);
- else
- end = N;
- int c;
- for (int i = start; i < end; ++i)
- c = sqrt(a);
- }
- int main(int argc, char * argv[]) {
- P = atoi(argv[1]);
- clock_t begin = clock();
- {
- pthread_t tid[P];
- int thread_id[P];
- for(int i = 0; i < P; ++i)
- thread_id[i] = i;
- for(int i = 0; i < P; i++)
- pthread_create(&(tid[i]), NULL, threadFunction, &(thread_id[i]));
- for(int i = 0; i < P; i++)
- pthread_join(tid[i], NULL);
- }
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("%f\n",time_spent);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement