I made a parallel code in C to verify its execution time. It was treated:
- Threads;
- Mutex;
- False Sharing;
- Synchronization.
When running time
of Linux with code execution, it was generally possible to compute at the following time:
Resultado da soma no modo concorrente = 36028797153181696
real 0m0.340s
user 0m1.300s
sys 0m0.000s
However, every + - 5 of these executions took a dramatically different time:
Resultado da soma no modo concorrente = 36028797153181696
real 0m1.863s
user 0m5.584s
sys 0m0.000s
What did you miss?
What do the times real
, user
, sys
mean?
The code that calculates the sum of elements from 1 ~ N
#include <stdio.h>
#include <pthread.h>
#define QTD 268435456 //1024//16384 //8192
#define QTD_N 4
unsigned long long int soma = 0;
unsigned long int porcao = QTD/QTD_N;
struct padding{
unsigned long long int s;//soma parcial;
unsigned int i,start,end;
unsigned int m; // identificador de parcela
char p[40];
};
pthread_mutex_t mutex_lock;
void *paralelo(void *region_ptr){
struct padding *soma_t;
soma_t = region_ptr;
soma_t->s = 0;
soma_t->start = soma_t->m * porcao + 1;
soma_t->end = (soma_t->m + 1) * porcao;
for(soma_t->i = soma_t->start; soma_t->i <= soma_t->end ; soma_t->i++){
soma_t->s += soma_t->i;
}
pthread_mutex_lock(&mutex_lock);
soma += soma_t->s;
pthread_mutex_unlock(&mutex_lock);
pthread_exit(NULL);
}
int main(void){
pthread_t thread[QTD_N];
struct padding soma_t[QTD_N];
int i;
void *status;
pthread_mutex_init(&mutex_lock,NULL);
for(i = 0 ; i < QTD_N ; i++){
soma_t[i].m = i;
pthread_create(&thread[i], NULL, paralelo, &soma_t[i]);
}
for(i = 0 ; i < QTD_N ; i++){
pthread_join(thread[i],&status);
}
pthread_mutex_destroy(&mutex_lock);
printf("Resultado da soma no modo concorrente = %lli\n",soma);
return 0;
}