Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
pth_trap.c
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//gcc 7.4.0 //Implements the trapezoidal rule using pthreads #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <sys/time.h> long thread_count; double global_sum; pthread_mutex_t mutex; long flag; sem_t sem; double a; double b; long n; double h; double f(double x); void* Thread_trap_bw(void* rank); void* Thread_trap_mutex(void* rank); void* Thread_trap_sem(void* rank); void Trap_bw(); void Trap_mutex(); void Trap_sem(); int main(void) { //thread_count = strtol(argv[1], NULL, 10); thread_count = 2; printf("Enter a:\n"); scanf("%lf", &a); printf("Enter b:\n"); scanf("%lf", &b); printf("Enter n:\n"); scanf("%ld", &n); h = (b - a)/n; Trap_bw(); Trap_mutex(); Trap_sem(); return 0; } double f(double x) { return x * x; } void Trap_bw() { struct timeval t; double start, finish; long thread; pthread_t* thread_handles; gettimeofday(&t, NULL); start = t.tv_sec + t.tv_usec / 1000000.0; thread_handles = malloc(thread_count * sizeof(pthread_t)); global_sum = 0; flag = 0; for(thread = 0; thread < thread_count; thread++) { pthread_create(&thread_handles[thread], NULL, Thread_trap_bw, (void*) thread); } for(thread = 0; thread < thread_count; thread++) { pthread_join(thread_handles[thread], NULL); } free(thread_handles); gettimeofday(&t, NULL); finish = t.tv_sec + t.tv_usec / 1000000.0; printf("The elapsed time for busy-waiting = %e ms\n", (finish - start) * 1000); } void Trap_mutex() { struct timeval t; double start, finish; long thread; pthread_t* thread_handles; gettimeofday(&t, NULL); start = t.tv_sec + t.tv_usec / 1000000.0; pthread_mutex_init(&mutex, NULL); thread_handles = malloc(thread_count * sizeof(pthread_t)); global_sum = 0; for(thread = 0; thread < thread_count; thread++) { pthread_create(&thread_handles[thread], NULL, Thread_trap_mutex, (void*) thread); } for(thread = 0; thread < thread_count; thread++) { pthread_join(thread_handles[thread], NULL); } pthread_mutex_destroy(&mutex); free(thread_handles); gettimeofday(&t, NULL); finish = t.tv_sec + t.tv_usec / 1000000.0; printf("The elapsed time for mutex = %e ms\n", (finish - start) * 1000); } void Trap_sem() { struct timeval t; double start, finish; long thread; pthread_t* thread_handles; gettimeofday(&t, NULL); start = t.tv_sec + t.tv_usec / 1000000.0; sem_init(&sem, 0, 1); thread_handles = malloc(thread_count * sizeof(pthread_t)); global_sum = 0; for(thread = 0; thread < thread_count; thread++) { pthread_create(&thread_handles[thread], NULL, Thread_trap_sem, (void*) thread); } for(thread = 0; thread < thread_count; thread++) { pthread_join(thread_handles[thread], NULL); } sem_destroy(&sem); free(thread_handles); gettimeofday(&t, NULL); finish = t.tv_sec + t.tv_usec / 1000000.0; printf("The elapsed time for semaphore = %e ms\n", (finish - start) * 1000); } void* Thread_trap_bw(void* rank) { long i; double x; long my_rank = (long) rank; long local_n = n / thread_count; double local_a = a + my_rank * local_n * h; double local_b = local_a + local_n * h; double local_sum = (f(local_a) + f(local_b))/2.0; for(i = 1; i <= local_n - 1; i++) { x = local_a + i * h; local_sum += f(x); } local_sum = local_sum * h; while(flag != my_rank); global_sum += local_sum; flag = (flag + 1) % thread_count; return NULL; } void* Thread_trap_mutex(void* rank) { long i; double x; long my_rank = (long) rank; long local_n = n / thread_count; double local_a = a + my_rank * local_n * h; double local_b = local_a + local_n * h; double local_sum = (f(local_a) + f(local_b))/2.0; for(i = 1; i <= local_n - 1; i++) { x = local_a + i * h; local_sum += f(x); } local_sum = local_sum * h; pthread_mutex_lock(&mutex); global_sum += local_sum; pthread_mutex_unlock(&mutex); return NULL; } void* Thread_trap_sem(void* rank) { long i; double x; long my_rank = (long) rank; long local_n = n / thread_count; double local_a = a + my_rank * local_n * h; double local_b = local_a + local_n * h; double local_sum = (f(local_a) + f(local_b))/2.0; for(i = 1; i <= local_n - 1; i++) { x = local_a + i * h; local_sum += f(x); } local_sum = local_sum * h; sem_wait(&sem); global_sum += local_sum; sem_post(&sem); return NULL; }
gcc
0 100 256
Show compiler warnings
[
+
] Compiler args
[
-
]
Show input
Compilation time: 0.22 sec, absolute running time: 0.15 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 0,46 sec
edit mode
|
history
|
discussion