티스토리 뷰
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <sys/time.h>
#define NUM_NUMBERS 1000000
#define NUM_ITERS 1000
#define NUM_THREADS 16
#define CACHE_LINE_SIZE 64
int numbers[NUM_NUMBERS];
int num_threads;
typedef struct {
double sum;
char pad[CACHE_LINE_SIZE - sizeof(double)];
} ps_t;
ps_t *partial_sum;
double get_elapsed(struct timeval, struct timeval);
void *par_sum(void *arg)
{
int i, iter;
long my_id = (long)arg;
int chunk = NUM_NUMBERS/ num_threads;
int start = my_id * chunk;
int end = start + chunk;
if(my_id == (num_threads - 1))
end = NUM_NUMBERS;
for(iter = 0; iter < NUM_ITERS; iter++)
{
partial_sum[my_id].sum = 0.0;
for(i = start ; i < end ; i++)
{
partial_sum[my_id].sum += sqrt((double)(numbers[i] % 100));
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
long i;
double sum = 0.0;
pthread_t *threads;
void *status;
struct timeval t_start, t_end;
if (argc > 1)
{
num_threads = atoi(argv[1]);
}
else
{
num_threads = NUM_THREADS;
}
for(i = 0; i < NUM_NUMBERS; i++)
{
numbers[i] = rand();
}
threads = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
partial_sum = (ps_t *)malloc(num_threads * sizeof(ps_t));
gettimeofday(&t_start, NULL);
for(i = 0; i < num_threads ; i++)
{
if(pthread_create(&threads[i], NULL, par_sum, (void *)i) != 0)
{
perror("ERROR : pthread_create()\n");
exit(EXIT_FAILURE);
}
}
for(i = 0; i < num_threads ; i++)
{
if(pthread_join(threads[i], &status) != 0)
{
perror("ERROR: pthreads_join()\n");
exit(EXIT_FAILURE);
}
}
for(i = 0; i < num_threads ; i++)
sum += partial_sum[i].sum;
gettimeofday(&t_end, NULL);
free(threads);
free(partial_sum);
printf("== Parallel Version ==\n");
printf("sum : %.4lf\n\n",sum);
printf("# of threads: %d\n",num_threads);
printf("elapsed time: %lf, sec\n",get_elapsed(t_start, t_end));
return 0;
}
double get_elapsed(struct timeval t_start, struct timeval t_end)
{
double elapsed;
if(t_end.tv_usec >= t_start.tv_usec)
{
elapsed = (double)(t_end.tv_sec - t_start.tv_sec) + 1.0e-6 * (t_end.tv_usec - t_start.tv_usec);
}
else
{
elapsed = (double)(t_end.tv_sec - t_start.tv_sec - 1) + 1.0e-6 * (1.0e+6 + t_end.tv_usec - t_start.tv_usec);
}
return elapsed;
}
' la fermata, 개발 > Pthread' 카테고리의 다른 글
False sharing 예제 (0) | 2019.02.19 |
---|---|
Thread Affinity 사용해보기 (0) | 2019.02.19 |
타일링을 이용한 행렬 곱셈 (0) | 2019.02.19 |
큐를 이용한 주사위 확률 구하기 (0) | 2019.02.19 |
병렬 계수 정렬(Parallel counting sort (0) | 2019.02.19 |