简体   繁体   中英

measured runtime from c++ “time.h” is double than real

I am running this pthread-c++ program (gauss elimination) on my laptop to measure its runtime. The program runs about 10 seconds in real but my output shows about 20 seconds. What is wrong with this program?

I used

g++ -pthread main.c

./a.out 32 2048

to run

#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <pthread.h>
#include <iostream>

typedef float Type;

void mat_rand (Type**, int, int);
Type** mat_aloc (int, int);
void mat_free (Type**);
void mat_print (Type**, int, int);
void* eliminate(void*);
unsigned int n, max_threads, active_threads, thread_length;
Type** A;
int current_row;

struct args
{
    int start;
    int end;
};
typedef struct args argument;

void *print_message_function( void *ptr );

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        printf ("Error!. Please Enter The Matrix Dimension and No. of Threads!\n");
        return 0;
    } else
    {
        n = atoi(argv[2]);
        max_threads = atoi(argv[1]);
        if (n > 4096)
        {
            printf ("The maximum allowed size is 4096!\n");
            return 0;
        }
        if (max_threads > 32)
        {
            printf ("The maximum allowed Threads Count is 32!\n");
            return 0;
        }
    }



    A = mat_aloc(n , n+1);
    mat_rand (A, n, n+1);
    //mat_print (A, n, n+1);

    std::clock_t start;
    double exe_time;
    start = std::clock();

    pthread_attr_t attr;
    pthread_attr_init(&attr);

    argument* thread_args = new argument[max_threads];
    pthread_t* thread = new pthread_t[max_threads];

    for (int i=0; i<n-1; i++)
    {
        current_row = i;

        if (max_threads >= n-i)
            active_threads = n-i-1;
        else
            active_threads = max_threads;

        thread_length = (n-i-1)/active_threads;
        for (int j=0; j<active_threads-1; j++)
        {
            thread_args[j].start = i+1+j*thread_length;
            thread_args[j].end = i+1+(j+1)*thread_length;
            pthread_create( &thread[j], &attr, eliminate, (void*) &thread_args[j]);
        }

        thread_args[active_threads-1].start = i+1+(active_threads-1)*thread_length;
        thread_args[active_threads-1].end = n-1;
        pthread_create(&thread[active_threads-1], &attr, eliminate, (void*) &thread_args[active_threads-1]);


        for (int j=0; j<active_threads; j++)
        {
            pthread_join(thread[j], NULL);
        }
    }

    exe_time = (clock() - start) / (double) CLOCKS_PER_SEC;
    printf("Execution time for Matrix of size %i: %f\n", n, exe_time);
    //mat_print (A, n, n+1);

    return 0;
}

void* eliminate(void* arg)
{
    Type k, row_constant;
    argument* info = (argument*) arg;
    row_constant = A[current_row][current_row];

    for (int i=info->start; i<=info->end; i++)
    {
        k = A[i][current_row] / row_constant;
        A[i][current_row] = 0;
        for (int j=current_row+1; j<n+1; j++)
        {
            A[i][j] -= k*A[current_row][j];
        }
    }
}

// matrix random values
void mat_rand (Type** matrix, int row, int column)
{
    for (int i=0; i<row; i++)
        for (int j=0; j<column; j++)
        {
            matrix[i][j] = (float)(1) + ((float)rand()/(float)RAND_MAX)*256;
        }
}

// allocates a 2d matrix
Type** mat_aloc (int row, int column)
{
    Type* temp = new Type [row*column];
    if (temp == NULL)
    {
        delete [] temp;
        return 0;
    }
    Type** mat = new Type* [row];
    if (temp == NULL)
    {
        delete [] mat;
        return 0;
    }
    for (int i=0; i<row; i++)
    {
        mat[i] = temp + i*column;
    }
    return mat;
}

// free memory of matrix
void mat_free (Type** matrix)
{
    delete[] (*matrix);
    delete[] matrix;
}

// print matrix
void mat_print (Type** matrix, int row, int column)
{
    for (int i=0; i<row; i++)
    {
        for (int j=0; j<column; j++)
        {
            std::cout<< matrix[i][j] << "\t\t";
        }
        printf("\n");
    }
    printf(".................\n");
}

clock reports CPU time used. If you have 2 CPUs and run a thread on each one for 10 seconds, clock will report 20 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM