Discover number of CPU cores using threads

五迷三道 提交于 2020-01-06 19:48:09

问题


I have an assignment in which i have to write a program in C on Linux(i use CentOS), which uses threads/processes to determine the number of cores from the CPU. Firstly I tried to print the current time in mili/microseconds, because I know that there can run 1thread/core(or 2 with HT). But by miliseconds more than 10 threads printed the same amount of time and by microseconds none were identical. Secondly I tried to measure the execution time of threads with clock, given I have 4 cores, the execution time of 4 threads at the same time should take almost as long as executing 1.But none of my programs could bring me closer to the number of CPU's. Could you help me with some suggestions please?

Program printing current time:

pthread_t th[N];     

void* afis ()
{
    //time_t now;
    //time(&now);
    //printf("%s", ctime(&now));
    struct timeval start, end;
    long mtime, seconds, useconds;    

    gettimeofday(&start, NULL);
    // usleep(2000);
    gettimeofday(&end, NULL);

    seconds  = end.tv_sec  - start.tv_sec;
    useconds = end.tv_usec - start.tv_usec;

    mtime = seconds + useconds;

    printf("Thread with TID:%d   Elapsed time: %ld microsecons\n",(unsigned int)pthread_self(), mtime);
}     

int main()
{
    int i;
    for (i=0;i<N;i++)
    {
        pthread_create(&th[i],NULL,afis,NULL);
    }
    for(i=0;i<N;i++)
    {
        pthread_join(th[i],NULL);
    }
    return 0;
}

Program measuring processing time:

pthread_t th[N];    

void* func(void* arg)
{
    int x;
    int k;
    int n=(int)arg;
    for(k=0;k<10000000;k+=n)
    {
        x=0;
    }
}


int main()
{
    int i,j;
    for (i=0;i<N;i++)
    {
        clock_t start, end, total;
        start=clock();
        for(j=0;j<i;j++)
        {
            printf("execution nr: %d\n",i);
            pthread_create(&th[j],NULL,func,(int*)i);
        }
        for(j=0;j<i;j++)
        {
            pthread_join(th[j],NULL);
        }
        end=clock();
        printf("start = %ld, end = %ld\n", start, end);
        total=((double)(end-start) )/ CLOCKS_PER_SEC;
        printf("total=%ld\n",total);
    }

    return 0;
}

回答1:


What you should probably do is (in pseudo-code):

get the actual time (start time)
start 40 threads
    each busy for one second;
wait for all of them to stop
get the actual time (stop time)

If you analyze the time it took for the 40 threads to execute, you will know the number of cores, or at least you can make an assumption:

if the time was around 40s: you have one core
else if the time was around 20s: you have two
and so on

You could of course adapt the number of threads you start, aswell as the time you let them sleep, but I guess that if you sleep for a millisecond only you could get times that are not representative due to context switches and background tasks.


Instead of sleeping in the thread do something like:

highlyCpuIntensiveTask()
{
    //calculate something that takes up the time x (best would be above 1 second)
}

You execute it once without starting a thread, what would take up the time x. That time will be the reference time.

If, by adding more and more pthreads (y), executing the same function, you do not use up considerably more time than x, then you know that you do most probably have at least y cores. At some point, with z threads, the time will be around 2x, at which point you'll know that you have z-1 cores.




回答2:


#include <unistd.h>

int number_of_cores = sysconf(_SC_NPROCESSORS_ONLN);

This is not portable, works only for Linux afaik.



来源:https://stackoverflow.com/questions/26258092/discover-number-of-cpu-cores-using-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!