Cleanup and submit

main
Gasper Spagnolo 2022-11-22 09:30:23 +01:00
parent dc7147eb8d
commit cd9863a782
2 changed files with 15 additions and 21 deletions

Binary file not shown.

View File

@ -5,8 +5,8 @@
#include <unistd.h>
#include <math.h>
#define N_THREADS 9
#define N_DIMENSIONS 200000000
#define N_THREADS 130
#define N_DIMENSIONS 2000000
pthread_barrier_t b;
long *p_s;
@ -14,10 +14,6 @@ long *q_s;
double res[N_THREADS];
int active_threads[N_THREADS];
// odd number of threads lock
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_spinlock_t slock;
void setup() {
p_s = (long *) malloc(N_DIMENSIONS * sizeof(long));
q_s = (long *) malloc(N_DIMENSIONS * sizeof(long));
@ -45,6 +41,7 @@ void *calculate(void* arg) {
int n_threads = N_THREADS;
// handle the odd number of threads!
if(n_threads % 2 == 1) {
if ( *myrank == N_THREADS - 1) {
res[*myrank - 1] += res[*myrank];
@ -55,14 +52,16 @@ void *calculate(void* arg) {
pthread_barrier_wait( &b );
// Do not split threads if there is a single thread
if (n_threads != 1) {
n_threads = N_THREADS / 2;
if (*myrank >= n_threads) {
active_threads[*myrank] = -1;
}
}
// Do a tree based sumation
while(1) {
if(active_threads[*myrank] != -1) {
res[*myrank] = res[*myrank * 2] + res[*myrank *2 + 1];
@ -90,14 +89,8 @@ int main() {
double dt = omp_get_wtime(); // start timing
int p[N_THREADS];
//if(N_THREADS %2 != 0 && N_THREADS >= 2) {
// printf("Number of threads must be even!\n");
// return 1;
//}
//
// barrier & lock
pthread_barrier_init( &b, NULL, N_THREADS );
pthread_spin_init(&slock, PTHREAD_PROCESS_PRIVATE);
for(int i = 0; i < N_THREADS; i++ ) {
p[i] = i;
@ -107,14 +100,15 @@ int main() {
for(int i=0; i<N_THREADS; i++)
pthread_join(t[i], NULL);
pthread_spin_destroy(&slock);
dt = omp_get_wtime() - dt; // finish timing
printf("Execution time: %lf, caluclation result: %lf ", dt, sqrt(res[0]));
return 0;
// 8 THREADS: Execution time: 0.807376, caluclation result: 49748.358094
// 4 THREADS: Execution time: 1.614672, caluclation result: 49748.358094
// 2 THREADS: Execution time: 3.223820, caluclation result: 57445.749660
// 1 THREADS: Execution time: 4.698835, caluclation result: 57445.749660
}
// EXECUTION TIME
// 8 THREADS: Execution time: 0.807376, caluclation result: 49748.358094, N_DIMENSIONS: 200000000
// 4 THREADS: Execution time: 1.614672, caluclation result: 49748.358094, N_DIMENSIONS: 200000000
// 2 THREADS: Execution time: 3.223820, caluclation result: 57445.749660, N_DIMENSIONS: 200000000
// 1 THREADS: Execution time: 4.698835, caluclation result: 57445.749660, N_DIMENSIONS: 200000000