Software Archive
Read-only legacy content

Sleep thread issues on Xeon Phi

Jonathan_J_
Beginner
291 Views

I am attempting to use the nanosleep function (defined here: http://linux.die.net/man/2/nanosleep) on the Xeon Phi. It is very similar to the standard (and by standard I just mean widely-used, I know there is no true standard) sleep function, but it is higher-resolution, a requirement for our software.

I know that no sleep function can be perfect, except maybe on embedded software, so I came in expecting some margin of error. However, I found that asking it to sleep for 5 ms (or 5000000 nanoseconds) resulting in it sleeping for approx 20 ms on the Xeon Phi. Did I discover a hardware bug, or is this a known issue? Is there a known better way to "sleep" a thread on a Xeon Phi? 

Code for reproducing below:

[cpp]

#include <stdint.h>
#include <time.h>
#include <iostream>


inline int64_t picosecondTime() {
    timespec systemtime;
    clock_gettime(CLOCK_REALTIME, &systemtime);
    return (int64_t) ((systemtime.tv_sec * 1000000000000UL) + (systemtime.tv_nsec * 1000UL));
}

int main(int argc, char *argv[]) {
    struct timespec tim;
    tim.tv_sec = 0;

    //time is picosecond granularity in software
    int64_t picosToSleep = 5000000000;

    //convert to nanoseconds, lowest granularity of sleep
    tim.tv_nsec = picosToSleep / 1000;

    int64_t currentTime = picosecondTime();

    int err = nanosleep(&tim, NULL);
    if (err == -1) {
        std::cout << "sleep error, disregard this test" << std::endl;
    }
    int64_t awokeTime = picosecondTime();

    int64_t shouldAwakeTime = (currentTime + picosToSleep);

    std::cout << "Went to sleep at: " << currentTime << std::endl;
    std::cout << "Should awake at: " << shouldAwakeTime << std::endl;
    std::cout << "Actually awoke at: " << awokeTime << std::endl;
    std::cout << "Is difference of: " << (awokeTime - shouldAwakeTime) << std::endl;

    return 0;
}

[/cpp]

0 Kudos
4 Replies
Loc_N_Intel
Employee
291 Views

Hi Jonathan,

Let me investigate the issue you found. Regards.

0 Kudos
Loc_N_Intel
Employee
291 Views

Hi Jonathan,

I confirmed that using nanosleep() and usleep() for 5 msec on Intel(R) Xeon Phi(TM), it is not accurate as it is in Xeon (the difference is ~5 msec on Xeon Phi and ~0.1 msec on Xeon). I will file a problem tracking for this. 

Thank you and regards.

0 Kudos
Yiannis_N_
Beginner
291 Views

Hi,

I know this is a quite old post but I've fallen into the same problem. Is there any newer information about this?

0 Kudos
JJK
New Contributor III
291 Views

All I can say is that I can reproduce the issue with mpss 3.4, 3,5 and 3.6 ; the clocksource on my MICs is set to "tsc" yet a  

nanosleep(50000000)

or even

usleep(5000)

result in an elapsed time of ~ 15 ms (instead of 5). This happens with both a 5120 and a 7120.

It does not depend on the version of ICC used - I can also reproduce it by building a binary using gcc-for-Phi.

 

 

0 Kudos
Reply