티스토리 뷰

la fermata, 개발/Pthread

False sharing 예제

창공미나래 2019. 2. 19. 23:08

#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

int numbers[NUM_NUMBERS];
int num_threads;

double *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] = 0.0;
        for(i = start ; i < end ; i++)
        {
            partial_sum[my_id] += 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 = (double *)malloc(num_threads * sizeof(double));

    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];

    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;

}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함