user1726549
user1726549

Reputation:

How can I calculate the running time of a pthread matrix multiplication program?

I have created a matrix multiplication program, one in serial, and one using pthreads. I need to compare their running times. My serial code takes about 16 seconds to calculate 1000x1000 matrix multiplication, and I checked it using my stopwatch, and it is exactly as it should be. On the other hand, when I run my pthreads matrix multiplication program I get printed as a result something around 22-23 seconds, but the result gets printed on the terminal so much faster. I also used my stopwatch to check the time it takes to output the running time, and it was around 6 seconds, but it prints that it took around 23 seconds. I guess there is some other way in checking the running time of a pthread program. Below you can find my pthreads code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <assert.h>

int SIZE, NTHREADS;
int **A, **B, **C;

void init()
{
    int i, j;

    A = (int**)malloc(SIZE * sizeof(int *));
    for(i = 0; i < SIZE; i++)
        A[i] = malloc(SIZE * sizeof(int));

    B = (int**)malloc(SIZE * sizeof(int *));
    for(i = 0; i < SIZE; i++)
        B[i] = malloc(SIZE * sizeof(int));

    C = (int**)malloc(SIZE * sizeof(int *));
    for(i = 0; i < SIZE; i++)
        C[i] = malloc(SIZE * sizeof(int));

    srand(time(NULL));

    for(i = 0; i < SIZE; i++) {
        for(j = 0; j < SIZE; j++) {
            A[i][j] = rand()%100;
            B[i][j] = rand()%100;
        }
    }
}

void mm(int tid)
{
    int i, j, k;
    int start = tid * SIZE/NTHREADS;
    int end = (tid+1) * (SIZE/NTHREADS) - 1;

    for(i = start; i <= end; i++) {
        for(j = 0; j < SIZE; j++) {
            C[i][j] = 0;
            for(k = 0; k < SIZE; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

void *worker(void *arg)
{
    int tid = (int)arg;
    mm(tid);
}

int main(int argc, char* argv[])
{
    pthread_t* threads;
    int rc, i;

    if(argc != 3)
    {
        printf("Usage: %s <size_of_square_matrix> <number_of_threads>\n", argv[0]);
        exit(1);
    }

    SIZE = atoi(argv[1]);
    NTHREADS = atoi(argv[2]);
    init();
    threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));

    clock_t begin, end;
    double time_spent;


    begin = clock();

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_create(&threads[i], NULL, worker, (void *)i);
        assert(rc == 0);
    }

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_join(threads[i], NULL);
        assert(rc == 0);
    } 

    end = clock();

    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("Elapsed time: %.2lf seconds.\n", time_spent);

    for(i = 0; i < SIZE; i++)
        free((void *)A[i]);
    free((void *)A);

    for(i = 0; i < SIZE; i++)
        free((void *)B[i]);
    free((void *)B);

    for(i = 0; i < SIZE; i++)
        free((void *)C[i]);
    free((void *)C);

    free(threads);

    return 0;
}

Upvotes: 3

Views: 6987

Answers (2)

user2088790
user2088790

Reputation:

The easiest way I know of is with OpenMP. Link with -fopenmp

#include <omp.h>

int main() {
    double dtime = omp_get_wtime(); //value in seconds
    //run some code
    dtime = omp_get_wtime() - dtime;

}

Note that 16 seconds for 1000x1000 matrix multiplication is incredibly slow. My code does 1056x1056 in 0.03 seconds on my i7-2600k at 4.3 GHz and even that is less than 30% of the max theoretical speed.

Upvotes: 1

Michael Greene
Michael Greene

Reputation: 10423

This is how you get the CPU time that has elapsed, but not how to get the wall-clock time that has elapsed. For that, you will want to use either time (which only has second granularity), or clock_gettime with the CLOCK_MONOTONIC option, which would be preferred. You will need to link against the POSIX Realtime extensions (-lrt) for this.

struct timespec begin, end;
double elapsed;

clock_gettime(CLOCK_MONOTONIC, &begin);

// spawn threads to do work here

clock_gettime(CLOCK_MONOTONIC, &end);

elapsed = end.tv_sec - begin.tv_sec;
elapsed += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;

In your example, I'm guessing you used around 4 threads? The CPU time would then be (time used in CPU 1 + time used in CPU 2 + time used in CPU 3 + time used in CPU 4) which should be roughly 4 times the absolute time (6 vs. 23 seconds).

Upvotes: 2

Related Questions